1
Fork 0

add support for extended aggregate syntax

This commit is contained in:
Jake Wheat 2013-12-13 23:18:30 +02:00
parent 5e56a4b560
commit 81e7aa818b
4 changed files with 41 additions and 16 deletions

View file

@ -148,10 +148,19 @@ to be.
> app :: P ScalarExpr > app :: P ScalarExpr
> app = App <$> identifierString > app = do
> -- support for count(*) > i <- identifierString
> <*> parens (choice[(:[]) <$> try star > _ <- symbol "("
> ,commaSep scalarExpr']) > d <- try duplicates
> es <- choice [(:[]) <$> try star
> ,commaSep scalarExpr']
> od <- try $ optionMaybe orderBy
> _ <- symbol ")"
> case (d,od) of
> (Nothing,Nothing) ->
> return $ App i es
> _ -> return $ AggregateApp i d es (fromMaybe [] od)
> scase :: P ScalarExpr > scase :: P ScalarExpr
> scase = > scase =
@ -406,9 +415,10 @@ attempt to fix the precedence and associativity. Doesn't work
= query expressions = query expressions
> duplicates :: P Duplicates > duplicates :: P (Maybe Duplicates)
> duplicates = option All $ try $ choice [All <$ keyword_ "all" > duplicates = optionMaybe $ try $
> ,Distinct <$ keyword "distinct"] > choice [All <$ keyword_ "all"
> ,Distinct <$ keyword "distinct"]
> selectItem :: P (Maybe String, ScalarExpr) > selectItem :: P (Maybe String, ScalarExpr)
> selectItem = flip (,) <$> scalarExpr <*> optionMaybe (try alias) > selectItem = flip (,) <$> scalarExpr <*> optionMaybe (try alias)
@ -472,9 +482,9 @@ attempt to fix the precedence and associativity. Doesn't work
> having = optionalScalarExpr "having" > having = optionalScalarExpr "having"
> orderBy :: P [(ScalarExpr,Direction)] > orderBy :: P [(ScalarExpr,Direction)]
> orderBy = option [] (try (keyword_ "order") > orderBy = try (keyword_ "order")
> *> keyword_ "by" > *> keyword_ "by"
> *> commaSep1 ob) > *> commaSep1 ob
> where > where
> ob = (,) <$> scalarExpr > ob = (,) <$> scalarExpr
> <*> option Asc (choice [Asc <$ keyword_ "asc" > <*> option Asc (choice [Asc <$ keyword_ "asc"
@ -491,13 +501,13 @@ attempt to fix the precedence and associativity. Doesn't work
> queryExpr = > queryExpr =
> try (keyword_ "select") >> > try (keyword_ "select") >>
> Select > Select
> <$> duplicates > <$> (fromMaybe All <$> duplicates)
> <*> selectList > <*> selectList
> <*> from > <*> from
> <*> swhere > <*> swhere
> <*> sgroupBy > <*> sgroupBy
> <*> having > <*> having
> <*> orderBy > <*> option [] orderBy
> <*> limit > <*> limit
> <*> offset > <*> offset

View file

@ -30,6 +30,15 @@ back into SQL source text. It attempts to format the output nicely.
> scalarExpr (App f es) = text f <> parens (commaSep (map scalarExpr es)) > scalarExpr (App f es) = text f <> parens (commaSep (map scalarExpr es))
> scalarExpr (AggregateApp f d es od) =
> text f
> <> parens ((case d of
> Just Distinct -> text "distinct"
> Just All -> text "all"
> Nothing -> empty)
> <+> commaSep (map scalarExpr es)
> <+> orderBy od)
> scalarExpr (SpecialOp nm [a,b,c]) | nm `elem` ["between", "not between"] = > scalarExpr (SpecialOp nm [a,b,c]) | nm `elem` ["between", "not between"] =
> sep [scalarExpr a > sep [scalarExpr a
> ,text nm <+> scalarExpr b > ,text nm <+> scalarExpr b

View file

@ -21,6 +21,9 @@
> | Star > | Star
> | Star2 String > | Star2 String
> | App String [ScalarExpr] > | App String [ScalarExpr]
> | AggregateApp String (Maybe Duplicates)
> [ScalarExpr]
> [(ScalarExpr,Direction)]
> -- the binop, prefixop and postfix op > -- the binop, prefixop and postfix op
> -- are used for symbol and keyword operators > -- are used for symbol and keyword operators
> -- these are used even for the multiple keyword > -- these are used even for the multiple keyword

View file

@ -173,10 +173,13 @@
> aggregates :: TestItem > aggregates :: TestItem
> aggregates = Group "aggregates" $ map (uncurry TestScalarExpr) > aggregates = Group "aggregates" $ map (uncurry TestScalarExpr)
> [{-("count(*)",NumLit "1") > [("count(*)",App "count" [Star])
> ,("sum(a order by a)",NumLit "1") > ,("sum(a order by a)"
> ,("sum(all a)",NumLit "1") > ,AggregateApp "sum" Nothing [Iden "a"] [(Iden "a", Asc)])
> ,("count(distinct a)",NumLit "1")-} > ,("sum(all a)"
> ,AggregateApp "sum" (Just All) [Iden "a"] [])
> ,("count(distinct a)"
> ,AggregateApp "count" (Just Distinct) [Iden "a"] [])
> ] > ]
> windowFunctions :: TestItem > windowFunctions :: TestItem