From 74c6d39b236588e60c7830f140c18e2e6e26f6bb Mon Sep 17 00:00:00 2001
From: Jake Wheat <jakewheatmail@gmail.com>
Date: Tue, 17 Dec 2013 19:27:09 +0200
Subject: [PATCH] get group by variations working

---
 Language/SQL/SimpleSQL/Parser.lhs        | 14 +++++++++++++-
 tools/Language/SQL/SimpleSQL/GroupBy.lhs | 15 ++++++++++++---
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/Language/SQL/SimpleSQL/Parser.lhs b/Language/SQL/SimpleSQL/Parser.lhs
index 8638bc8..13a4dc7 100644
--- a/Language/SQL/SimpleSQL/Parser.lhs
+++ b/Language/SQL/SimpleSQL/Parser.lhs
@@ -601,7 +601,19 @@ where, having, limit, offset).
 > sgroupBy :: P [GroupingExpr]
 > sgroupBy = try (keyword_ "group")
 >            *> keyword_ "by"
->            *> commaSep1 (SimpleGroup <$> scalarExpr)
+>            *> commaSep1 groupingExpression
+>   where
+>     groupingExpression =
+>       choice
+>       [try (keyword_ "cube") >>
+>        Cube <$> parens (commaSep groupingExpression)
+>       ,try (keyword_ "rollup") >>
+>        Rollup <$> parens (commaSep groupingExpression)
+>       ,GroupingParens <$> parens (commaSep groupingExpression)
+>       ,try (keyword_ "grouping") >> keyword_ "sets" >>
+>        GroupingSets <$> parens (commaSep groupingExpression)
+>       ,SimpleGroup <$> scalarExpr
+>       ]
 
 > having :: P ScalarExpr
 > having = keywordScalarExpr "having"
diff --git a/tools/Language/SQL/SimpleSQL/GroupBy.lhs b/tools/Language/SQL/SimpleSQL/GroupBy.lhs
index 98ff090..b36cfbd 100644
--- a/tools/Language/SQL/SimpleSQL/GroupBy.lhs
+++ b/tools/Language/SQL/SimpleSQL/GroupBy.lhs
@@ -38,9 +38,14 @@ sure which sql version they were introduced, 1999 or 2003 I think).
 
 > newGroupBy :: TestItem
 > newGroupBy = Group "newGroupBy" $ map (uncurry TestQueryExpr)
->     [
-
-group by ()
+>     [("select * from t group by ()", ms [GroupingParens []])
+>     ,("select * from t group by grouping sets ((), (a))"
+>      ,ms [GroupingSets [GroupingParens []
+>                        ,GroupingParens [SimpleGroup $ Iden "a"]]])
+>     ,("select * from t group by cube(a,b)"
+>      ,ms [Cube [SimpleGroup $ Iden "a", SimpleGroup $ Iden "b"]])
+>     ,("select * from t group by rollup(a,b)"
+>      ,ms [Rollup [SimpleGroup $ Iden "a", SimpleGroup $ Iden "b"]])
 
 
 GROUP BY a
@@ -224,3 +229,7 @@ GROUP BY CUBE(MONTH(SALES_DATE),REGION)
 ORDER BY MONTH, REGION
 
 >     ]
+>   where
+>     ms g = makeSelect {qeSelectList = [(Nothing,Star)]
+>                       ,qeFrom = [TRSimple "t"]
+>                       ,qeGroupBy = g}