1
Fork 0

add directions to order by

This commit is contained in:
Jake Wheat 2013-12-13 17:08:10 +02:00
parent 2c1eedb70f
commit 1a8551825d
5 changed files with 27 additions and 10 deletions

View file

@ -267,10 +267,14 @@ to be.
> having :: P (Maybe ScalarExpr) > having :: P (Maybe ScalarExpr)
> having = optionMaybe (try (keyword_ "having") *> scalarExpr) > having = optionMaybe (try (keyword_ "having") *> scalarExpr)
> orderBy :: P [ScalarExpr] > orderBy :: P [(ScalarExpr,Direction)]
> orderBy = option [] (try (keyword_ "order") > orderBy = option [] (try (keyword_ "order")
> *> keyword_ "by" > *> keyword_ "by"
> *> commaSep1 scalarExpr) > *> commaSep1 ob)
> where
> ob = (,) <$> scalarExpr
> <*> option Asc (choice [Asc <$ keyword_ "asc"
> ,Desc <$ keyword_ "desc"])
> queryExpr :: P QueryExpr > queryExpr :: P QueryExpr
> queryExpr = > queryExpr =

View file

@ -111,10 +111,13 @@ back into SQL source text. It attempts to format the output nicely.
> having = maybe empty > having = maybe empty
> (\w -> sep [text "having" > (\w -> sep [text "having"
> ,nest 4 $ scalarExpr w]) > ,nest 4 $ scalarExpr w])
> orderBy :: [ScalarExpr] -> Doc > orderBy :: [(ScalarExpr,Direction)] -> Doc
> orderBy [] = empty > orderBy [] = empty
> orderBy os = sep [text "order by" > orderBy os = sep [text "order by"
> ,nest 4 $ commaSep $ map scalarExpr os] > ,nest 4 $ commaSep $ map f os]
> where
> f (e,Asc) = scalarExpr e
> f (e,Desc) = scalarExpr e <+> text "desc"
= utils = utils

View file

@ -6,6 +6,7 @@
> ,TableRef(..) > ,TableRef(..)
> ,JoinType(..) > ,JoinType(..)
> ,JoinCondition(..) > ,JoinCondition(..)
> ,Direction(..)
> ) where > ) where
@ -30,9 +31,11 @@
> ,qeWhere :: Maybe ScalarExpr > ,qeWhere :: Maybe ScalarExpr
> ,qeGroupBy :: [ScalarExpr] > ,qeGroupBy :: [ScalarExpr]
> ,qeHaving :: Maybe ScalarExpr > ,qeHaving :: Maybe ScalarExpr
> ,qeOrderBy :: [ScalarExpr] > ,qeOrderBy :: [(ScalarExpr,Direction)]
> } deriving (Eq,Show) > } deriving (Eq,Show)
> data Direction = Asc | Desc deriving (Eq,Show)
> makeSelect :: QueryExpr > makeSelect :: QueryExpr
> makeSelect = Select {qeSelectList = [] > makeSelect = Select {qeSelectList = []
> ,qeFrom = [] > ,qeFrom = []

7
TODO
View file

@ -1,10 +1,13 @@
check tpch
add tests to cabal add tests to cabal
haddock haddock
dialect switching dialect switching
refactor the join parsing
left factor parsing code left factor parsing code
@ -19,8 +22,6 @@ emacs parse error formatting
= sql support = sql support
decimal literals, split string and number literals
order by directions
distinct/all distinct/all
limit,offset, top limit,offset, top
@ -47,6 +48,8 @@ other missing operators
except, intersect, union except, intersect, union
-- add docs and haddocks at this point
review identifiers review identifiers
order by nulls first/last order by nulls first/last

View file

@ -199,9 +199,13 @@
> orderBy :: TestItem > orderBy :: TestItem
> orderBy = Group "orderBy" $ map (uncurry TestQueryExpr) > orderBy = Group "orderBy" $ map (uncurry TestQueryExpr)
> [("select a from t order by a" > [("select a from t order by a"
> ,ms [Iden "a"]) > ,ms [(Iden "a", Asc)])
> ,("select a from t order by a, b" > ,("select a from t order by a, b"
> ,ms [Iden "a", Iden "b"]) > ,ms [(Iden "a", Asc), (Iden "b", Asc)])
> ,("select a from t order by a asc"
> ,ms [(Iden "a", Asc)])
> ,("select a from t order by a desc, b desc"
> ,ms [(Iden "a", Desc), (Iden "b", Desc)])
> ] > ]
> where > where
> ms o = makeSelect {qeSelectList = [(Nothing,Iden "a")] > ms o = makeSelect {qeSelectList = [(Nothing,Iden "a")]
@ -231,7 +235,7 @@
> ,qeGroupBy = [Iden "a"] > ,qeGroupBy = [Iden "a"]
> ,qeHaving = Just $ Op ">" [App "count" [NumLit "1"] > ,qeHaving = Just $ Op ">" [App "count" [NumLit "1"]
> ,NumLit "5"] > ,NumLit "5"]
> ,qeOrderBy = [Iden "s"] > ,qeOrderBy = [(Iden "s", Asc)]
> } > }
> ) > )
> ] > ]