From 1a8551825d0828d93a1c0ae0caed471cb4a5f403 Mon Sep 17 00:00:00 2001 From: Jake Wheat Date: Fri, 13 Dec 2013 17:08:10 +0200 Subject: [PATCH] add directions to order by --- Language/SQL/SimpleSQL/Parser.lhs | 8 ++++++-- Language/SQL/SimpleSQL/Pretty.lhs | 7 +++++-- Language/SQL/SimpleSQL/Syntax.lhs | 5 ++++- TODO | 7 +++++-- Tests.lhs | 10 +++++++--- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Language/SQL/SimpleSQL/Parser.lhs b/Language/SQL/SimpleSQL/Parser.lhs index 8255512..e79b6e0 100644 --- a/Language/SQL/SimpleSQL/Parser.lhs +++ b/Language/SQL/SimpleSQL/Parser.lhs @@ -267,10 +267,14 @@ to be. > having :: P (Maybe ScalarExpr) > having = optionMaybe (try (keyword_ "having") *> scalarExpr) -> orderBy :: P [ScalarExpr] +> orderBy :: P [(ScalarExpr,Direction)] > orderBy = option [] (try (keyword_ "order") > *> keyword_ "by" -> *> commaSep1 scalarExpr) +> *> commaSep1 ob) +> where +> ob = (,) <$> scalarExpr +> <*> option Asc (choice [Asc <$ keyword_ "asc" +> ,Desc <$ keyword_ "desc"]) > queryExpr :: P QueryExpr > queryExpr = diff --git a/Language/SQL/SimpleSQL/Pretty.lhs b/Language/SQL/SimpleSQL/Pretty.lhs index f2d420a..84fd064 100644 --- a/Language/SQL/SimpleSQL/Pretty.lhs +++ b/Language/SQL/SimpleSQL/Pretty.lhs @@ -111,10 +111,13 @@ back into SQL source text. It attempts to format the output nicely. > having = maybe empty > (\w -> sep [text "having" > ,nest 4 $ scalarExpr w]) -> orderBy :: [ScalarExpr] -> Doc +> orderBy :: [(ScalarExpr,Direction)] -> Doc > orderBy [] = empty > 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 diff --git a/Language/SQL/SimpleSQL/Syntax.lhs b/Language/SQL/SimpleSQL/Syntax.lhs index 8203b5e..dc100e4 100644 --- a/Language/SQL/SimpleSQL/Syntax.lhs +++ b/Language/SQL/SimpleSQL/Syntax.lhs @@ -6,6 +6,7 @@ > ,TableRef(..) > ,JoinType(..) > ,JoinCondition(..) +> ,Direction(..) > ) where @@ -30,9 +31,11 @@ > ,qeWhere :: Maybe ScalarExpr > ,qeGroupBy :: [ScalarExpr] > ,qeHaving :: Maybe ScalarExpr -> ,qeOrderBy :: [ScalarExpr] +> ,qeOrderBy :: [(ScalarExpr,Direction)] > } deriving (Eq,Show) +> data Direction = Asc | Desc deriving (Eq,Show) + > makeSelect :: QueryExpr > makeSelect = Select {qeSelectList = [] > ,qeFrom = [] diff --git a/TODO b/TODO index 9854ed6..3a39dc9 100644 --- a/TODO +++ b/TODO @@ -1,10 +1,13 @@ +check tpch + add tests to cabal haddock dialect switching +refactor the join parsing left factor parsing code @@ -19,8 +22,6 @@ emacs parse error formatting = sql support -decimal literals, split string and number literals -order by directions distinct/all limit,offset, top @@ -47,6 +48,8 @@ other missing operators except, intersect, union +-- add docs and haddocks at this point + review identifiers order by nulls first/last diff --git a/Tests.lhs b/Tests.lhs index a4d06de..6dc992b 100644 --- a/Tests.lhs +++ b/Tests.lhs @@ -199,9 +199,13 @@ > orderBy :: TestItem > orderBy = Group "orderBy" $ map (uncurry TestQueryExpr) > [("select a from t order by a" -> ,ms [Iden "a"]) +> ,ms [(Iden "a", Asc)]) > ,("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 > ms o = makeSelect {qeSelectList = [(Nothing,Iden "a")] @@ -231,7 +235,7 @@ > ,qeGroupBy = [Iden "a"] > ,qeHaving = Just $ Op ">" [App "count" [NumLit "1"] > ,NumLit "5"] -> ,qeOrderBy = [Iden "s"] +> ,qeOrderBy = [(Iden "s", Asc)] > } > ) > ]