Skip to content

Commit ef8438a

Browse files
JakeWheatprescientmoon
authored andcommitted
adjust makeSelect helper to be new type, lib and tests now compile without any warnings
1 parent 89517d5 commit ef8438a

File tree

14 files changed

+274
-243
lines changed

14 files changed

+274
-243
lines changed

Language/SQL/SimpleSQL/Parse.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,10 @@ import Data.Maybe (catMaybes, isJust)
231231
import Data.Text (Text)
232232
import qualified Data.Text as T
233233

234-
import Language.SQL.SimpleSQL.Syntax
234+
import Language.SQL.SimpleSQL.Syntax
235235
import Language.SQL.SimpleSQL.Dialect
236236
import qualified Language.SQL.SimpleSQL.Lex as L
237237

238-
239238
------------------------------------------------------------------------------
240239

241240
-- = Public API
@@ -1522,7 +1521,7 @@ queryExpr = E.makeExprParser qeterm qeOpTable
15221521
<*> selectList
15231522
<*> (optional tableExpression) <?> "table expression"
15241523
mkSelect d sl Nothing =
1525-
makeSelect{qeSetQuantifier = d, qeSelectList = sl}
1524+
toQueryExpr $ makeSelect {msSetQuantifier = d, msSelectList = sl}
15261525
mkSelect d sl (Just (TableExpression f w g h od ofs fe)) =
15271526
Select d sl f w g h od ofs fe
15281527
values = keyword_ "values"

Language/SQL/SimpleSQL/Syntax.hs

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ module Language.SQL.SimpleSQL.Syntax
2323
,OdbcLiteralType(..)
2424
-- * Query expressions
2525
,QueryExpr(..)
26-
,makeSelect
2726
,SetOperatorName(..)
2827
,Corresponding(..)
2928
,Alias(..)
@@ -60,6 +59,10 @@ module Language.SQL.SimpleSQL.Syntax
6059
,GrantOptionFor(..)
6160
-- * Comment
6261
,Comment(..)
62+
63+
,makeSelect
64+
,toQueryExpr
65+
,MakeSelect(..)
6366
) where
6467

6568
import Data.Text (Text)
@@ -377,31 +380,6 @@ TODO: add queryexpr parens to deal with e.g.
377380
I'm not sure if this is valid syntax or not.
378381
-}
379382

380-
-- | Helper/'default' value for query exprs to make creating query
381-
-- expr values a little easier. It is defined like this:
382-
--
383-
-- > makeSelect :: QueryExpr
384-
-- > makeSelect = Select {qeSetQuantifier = SQDefault
385-
-- > ,qeSelectList = []
386-
-- > ,qeFrom = []
387-
-- > ,qeWhere = Nothing
388-
-- > ,qeGroupBy = []
389-
-- > ,qeHaving = Nothing
390-
-- > ,qeOrderBy = []
391-
-- > ,qeOffset = Nothing
392-
-- > ,qeFetchFirst = Nothing}
393-
394-
makeSelect :: QueryExpr
395-
makeSelect = Select {qeSetQuantifier = SQDefault
396-
,qeSelectList = []
397-
,qeFrom = []
398-
,qeWhere = Nothing
399-
,qeGroupBy = []
400-
,qeHaving = Nothing
401-
,qeOrderBy = []
402-
,qeOffset = Nothing
403-
,qeFetchFirst = Nothing}
404-
405383
-- | Represents the Distinct or All keywords, which can be used
406384
-- before a select list, in an aggregate/window function
407385
-- application, or in a query expression set operator.
@@ -744,3 +722,50 @@ data PrivilegeAction =
744722
newtype Comment = BlockComment Text
745723
deriving (Eq,Show,Read,Data,Typeable)
746724

725+
data MakeSelect
726+
= MakeSelect
727+
{msSetQuantifier :: SetQuantifier
728+
,msSelectList :: [(ScalarExpr,Maybe Name)]
729+
,msFrom :: [TableRef]
730+
,msWhere :: Maybe ScalarExpr
731+
,msGroupBy :: [GroupingExpr]
732+
,msHaving :: Maybe ScalarExpr
733+
,msOrderBy :: [SortSpec]
734+
,msOffset :: Maybe ScalarExpr
735+
,msFetchFirst :: Maybe ScalarExpr
736+
}
737+
738+
-- | Helper/'default' value for query exprs to make creating query
739+
-- expr values a little easier. It is defined like this:
740+
--
741+
-- > makeSelect :: MakeSelect
742+
-- > makeSelect
743+
-- > = MakeSelect
744+
-- > {msSetQuantifier = SQDefault
745+
-- > ,msSelectList = []
746+
-- > ,msFrom = []
747+
-- > ,msWhere = Nothing
748+
-- > ,msGroupBy = []
749+
-- > ,msHaving = Nothing
750+
-- > ,msOrderBy = []
751+
-- > ,msOffset = Nothing
752+
-- > ,msFetchFirst = Nothing}
753+
-- >
754+
-- > Example, to create a select query expression with a select list 'sl':
755+
-- > toQueryExpr $ makeSelect {msSelectList = sl}
756+
757+
makeSelect :: MakeSelect
758+
makeSelect
759+
= MakeSelect
760+
{msSetQuantifier = SQDefault
761+
,msSelectList = []
762+
,msFrom = []
763+
,msWhere = Nothing
764+
,msGroupBy = []
765+
,msHaving = Nothing
766+
,msOrderBy = []
767+
,msOffset = Nothing
768+
,msFetchFirst = Nothing}
769+
770+
toQueryExpr :: MakeSelect -> QueryExpr
771+
toQueryExpr (MakeSelect q sl f w g h o ff fetch) = Select q sl f w g h o ff fetch

changelog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
parses from and pretty prints to strict Text
1010
use strict Text instead of String everywhere
1111
tested with latest three main ghc releases (9.8.1, 9.6.4, and 9.4.8) and stack lts 22.5
12+
the makeSelect helper is now a distinct type, code using it will need some trivial
13+
tweaks, this is change so that code using makeSelect doesn't emit warnings
1214
0.6.1 added odbc handling to sqlsqerver dialect
1315
added sqlserver dialect case for convert function
1416
0.6.0

tools/Language/SQL/SimpleSQL/FullQueries.hs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import Language.SQL.SimpleSQL.Syntax
1111
fullQueriesTests :: TestItem
1212
fullQueriesTests = Group "queries" $ map (uncurry (TestQueryExpr ansi2011))
1313
[("select count(*) from t"
14-
,makeSelect
15-
{qeSelectList = [(App [Name Nothing "count"] [Star], Nothing)]
16-
,qeFrom = [TRSimple [Name Nothing "t"]]
14+
,toQueryExpr $ makeSelect
15+
{msSelectList = [(App [Name Nothing "count"] [Star], Nothing)]
16+
,msFrom = [TRSimple [Name Nothing "t"]]
1717
}
1818
)
1919

@@ -23,18 +23,18 @@ fullQueriesTests = Group "queries" $ map (uncurry (TestQueryExpr ansi2011))
2323
\ group by a\n\
2424
\ having count(1) > 5\n\
2525
\ order by s"
26-
,makeSelect
27-
{qeSelectList = [(Iden [Name Nothing "a"], Nothing)
26+
,toQueryExpr $ makeSelect
27+
{msSelectList = [(Iden [Name Nothing "a"], Nothing)
2828
,(App [Name Nothing "sum"]
2929
[BinOp (Iden [Name Nothing "c"])
3030
[Name Nothing "+"] (Iden [Name Nothing "d"])]
3131
,Just $ Name Nothing "s")]
32-
,qeFrom = [TRSimple [Name Nothing "t"], TRSimple [Name Nothing "u"]]
33-
,qeWhere = Just $ BinOp (Iden [Name Nothing "a"]) [Name Nothing ">"] (NumLit "5")
34-
,qeGroupBy = [SimpleGroup $ Iden [Name Nothing "a"]]
35-
,qeHaving = Just $ BinOp (App [Name Nothing "count"] [NumLit "1"])
32+
,msFrom = [TRSimple [Name Nothing "t"], TRSimple [Name Nothing "u"]]
33+
,msWhere = Just $ BinOp (Iden [Name Nothing "a"]) [Name Nothing ">"] (NumLit "5")
34+
,msGroupBy = [SimpleGroup $ Iden [Name Nothing "a"]]
35+
,msHaving = Just $ BinOp (App [Name Nothing "count"] [NumLit "1"])
3636
[Name Nothing ">"] (NumLit "5")
37-
,qeOrderBy = [SortSpec (Iden [Name Nothing "s"]) DirDefault NullsOrderDefault]
37+
,msOrderBy = [SortSpec (Iden [Name Nothing "s"]) DirDefault NullsOrderDefault]
3838
}
3939
)
4040
]

tools/Language/SQL/SimpleSQL/GroupBy.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ groupByTests = Group "groupByTests"
1818
simpleGroupBy :: TestItem
1919
simpleGroupBy = Group "simpleGroupBy" $ map (uncurry (TestQueryExpr ansi2011))
2020
[("select a,sum(b) from t group by a"
21-
,makeSelect {qeSelectList = [(Iden [Name Nothing "a"],Nothing)
21+
,toQueryExpr $ makeSelect {msSelectList = [(Iden [Name Nothing "a"],Nothing)
2222
,(App [Name Nothing "sum"] [Iden [Name Nothing "b"]],Nothing)]
23-
,qeFrom = [TRSimple [Name Nothing "t"]]
24-
,qeGroupBy = [SimpleGroup $ Iden [Name Nothing "a"]]
23+
,msFrom = [TRSimple [Name Nothing "t"]]
24+
,msGroupBy = [SimpleGroup $ Iden [Name Nothing "a"]]
2525
})
2626

2727
,("select a,b,sum(c) from t group by a,b"
28-
,makeSelect {qeSelectList = [(Iden [Name Nothing "a"],Nothing)
28+
,toQueryExpr $ makeSelect {msSelectList = [(Iden [Name Nothing "a"],Nothing)
2929
,(Iden [Name Nothing "b"],Nothing)
3030
,(App [Name Nothing "sum"] [Iden [Name Nothing "c"]],Nothing)]
31-
,qeFrom = [TRSimple [Name Nothing "t"]]
32-
,qeGroupBy = [SimpleGroup $ Iden [Name Nothing "a"]
31+
,msFrom = [TRSimple [Name Nothing "t"]]
32+
,msGroupBy = [SimpleGroup $ Iden [Name Nothing "a"]
3333
,SimpleGroup $ Iden [Name Nothing "b"]]
3434
})
3535
]
@@ -51,9 +51,9 @@ newGroupBy = Group "newGroupBy" $ map (uncurry (TestQueryExpr ansi2011))
5151
,ms [Rollup [SimpleGroup $ Iden [Name Nothing "a"], SimpleGroup $ Iden [Name Nothing "b"]]])
5252
]
5353
where
54-
ms g = makeSelect {qeSelectList = [(Star,Nothing)]
55-
,qeFrom = [TRSimple [Name Nothing "t"]]
56-
,qeGroupBy = g}
54+
ms g = toQueryExpr $ makeSelect {msSelectList = [(Star,Nothing)]
55+
,msFrom = [TRSimple [Name Nothing "t"]]
56+
,msGroupBy = g}
5757

5858
randomGroupBy :: TestItem
5959
randomGroupBy = Group "randomGroupBy" $ map (ParseQueryExpr ansi2011)

tools/Language/SQL/SimpleSQL/MySQL.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ backtickQuotes = Group "backtickQuotes" (map (uncurry (TestScalarExpr mysql))
3030
limit :: TestItem
3131
limit = Group "queries" ( map (uncurry (TestQueryExpr mysql))
3232
[("select * from t limit 5"
33-
,sel {qeFetchFirst = Just (NumLit "5")}
33+
,toQueryExpr $ sel {msFetchFirst = Just (NumLit "5")}
3434
)
3535
]
3636
++ [ParseQueryExprFails mysql "select a from t fetch next 10 rows only;"
3737
,ParseQueryExprFails ansi2011 "select * from t limit 5"]
3838
)
3939
where
4040
sel = makeSelect
41-
{qeSelectList = [(Star, Nothing)]
42-
,qeFrom = [TRSimple [Name Nothing "t"]]
41+
{msSelectList = [(Star, Nothing)]
42+
,msFrom = [TRSimple [Name Nothing "t"]]
4343
}

tools/Language/SQL/SimpleSQL/Odbc.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ odbcTests = Group "odbc" [
3232
,Group "outer join" [
3333
TestQueryExpr ansi2011 {diOdbc=True}
3434
"select * from {oj t1 left outer join t2 on expr}"
35-
$ makeSelect
36-
{qeSelectList = [(Star,Nothing)]
37-
,qeFrom = [TROdbc $ TRJoin (TRSimple [Name Nothing "t1"]) False JLeft (TRSimple [Name Nothing "t2"])
35+
$ toQueryExpr $ makeSelect
36+
{msSelectList = [(Star,Nothing)]
37+
,msFrom = [TROdbc $ TRJoin (TRSimple [Name Nothing "t1"]) False JLeft (TRSimple [Name Nothing "t2"])
3838
(Just $ JoinOn $ Iden [Name Nothing "expr"])]}]
3939
,Group "check parsing bugs" [
4040
TestQueryExpr ansi2011 {diOdbc=True}
4141
"select {fn CONVERT(cint,SQL_BIGINT)} from t;"
42-
$ makeSelect
43-
{qeSelectList = [(OdbcFunc (ap "CONVERT"
42+
$ toQueryExpr $ makeSelect
43+
{msSelectList = [(OdbcFunc (ap "CONVERT"
4444
[iden "cint"
4545
,iden "SQL_BIGINT"]), Nothing)]
46-
,qeFrom = [TRSimple [Name Nothing "t"]]}]
46+
,msFrom = [TRSimple [Name Nothing "t"]]}]
4747
]
4848
where
4949
e = TestScalarExpr ansi2011 {diOdbc = True}

tools/Language/SQL/SimpleSQL/QueryExprComponents.hs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,38 @@ duplicates = Group "duplicates" $ map (uncurry (TestQueryExpr ansi2011))
3737
,("select distinct a from t", ms Distinct)
3838
]
3939
where
40-
ms d = makeSelect
41-
{qeSetQuantifier = d
42-
,qeSelectList = [(Iden [Name Nothing "a"],Nothing)]
43-
,qeFrom = [TRSimple [Name Nothing "t"]]}
40+
ms d = toQueryExpr $ makeSelect
41+
{msSetQuantifier = d
42+
,msSelectList = [(Iden [Name Nothing "a"],Nothing)]
43+
,msFrom = [TRSimple [Name Nothing "t"]]}
4444

4545
selectLists :: TestItem
4646
selectLists = Group "selectLists" $ map (uncurry (TestQueryExpr ansi2011))
4747
[("select 1",
48-
makeSelect {qeSelectList = [(NumLit "1",Nothing)]})
48+
toQueryExpr $ makeSelect {msSelectList = [(NumLit "1",Nothing)]})
4949

5050
,("select a"
51-
,makeSelect {qeSelectList = [(Iden [Name Nothing "a"],Nothing)]})
51+
,toQueryExpr $ makeSelect {msSelectList = [(Iden [Name Nothing "a"],Nothing)]})
5252

5353
,("select a,b"
54-
,makeSelect {qeSelectList = [(Iden [Name Nothing "a"],Nothing)
54+
,toQueryExpr $ makeSelect {msSelectList = [(Iden [Name Nothing "a"],Nothing)
5555
,(Iden [Name Nothing "b"],Nothing)]})
5656

5757
,("select 1+2,3+4"
58-
,makeSelect {qeSelectList =
58+
,toQueryExpr $ makeSelect {msSelectList =
5959
[(BinOp (NumLit "1") [Name Nothing "+"] (NumLit "2"),Nothing)
6060
,(BinOp (NumLit "3") [Name Nothing "+"] (NumLit "4"),Nothing)]})
6161

6262
,("select a as a, /*comment*/ b as b"
63-
,makeSelect {qeSelectList = [(Iden [Name Nothing "a"], Just $ Name Nothing "a")
63+
,toQueryExpr $ makeSelect {msSelectList = [(Iden [Name Nothing "a"], Just $ Name Nothing "a")
6464
,(Iden [Name Nothing "b"], Just $ Name Nothing "b")]})
6565

6666
,("select a a, b b"
67-
,makeSelect {qeSelectList = [(Iden [Name Nothing "a"], Just $ Name Nothing "a")
67+
,toQueryExpr $ makeSelect {msSelectList = [(Iden [Name Nothing "a"], Just $ Name Nothing "a")
6868
,(Iden [Name Nothing "b"], Just $ Name Nothing "b")]})
6969

7070
,("select a + b * c"
71-
,makeSelect {qeSelectList =
71+
,toQueryExpr $ makeSelect {msSelectList =
7272
[(BinOp (Iden [Name Nothing "a"]) [Name Nothing "+"]
7373
(BinOp (Iden [Name Nothing "b"]) [Name Nothing "*"] (Iden [Name Nothing "c"]))
7474
,Nothing)]})
@@ -78,19 +78,19 @@ selectLists = Group "selectLists" $ map (uncurry (TestQueryExpr ansi2011))
7878
whereClause :: TestItem
7979
whereClause = Group "whereClause" $ map (uncurry (TestQueryExpr ansi2011))
8080
[("select a from t where a = 5"
81-
,makeSelect {qeSelectList = [(Iden [Name Nothing "a"],Nothing)]
82-
,qeFrom = [TRSimple [Name Nothing "t"]]
83-
,qeWhere = Just $ BinOp (Iden [Name Nothing "a"]) [Name Nothing "="] (NumLit "5")})
81+
,toQueryExpr $ makeSelect {msSelectList = [(Iden [Name Nothing "a"],Nothing)]
82+
,msFrom = [TRSimple [Name Nothing "t"]]
83+
,msWhere = Just $ BinOp (Iden [Name Nothing "a"]) [Name Nothing "="] (NumLit "5")})
8484
]
8585

8686
having :: TestItem
8787
having = Group "having" $ map (uncurry (TestQueryExpr ansi2011))
8888
[("select a,sum(b) from t group by a having sum(b) > 5"
89-
,makeSelect {qeSelectList = [(Iden [Name Nothing "a"],Nothing)
89+
,toQueryExpr $ makeSelect {msSelectList = [(Iden [Name Nothing "a"],Nothing)
9090
,(App [Name Nothing "sum"] [Iden [Name Nothing "b"]],Nothing)]
91-
,qeFrom = [TRSimple [Name Nothing "t"]]
92-
,qeGroupBy = [SimpleGroup $ Iden [Name Nothing "a"]]
93-
,qeHaving = Just $ BinOp (App [Name Nothing "sum"] [Iden [Name Nothing "b"]])
91+
,msFrom = [TRSimple [Name Nothing "t"]]
92+
,msGroupBy = [SimpleGroup $ Iden [Name Nothing "a"]]
93+
,msHaving = Just $ BinOp (App [Name Nothing "sum"] [Iden [Name Nothing "b"]])
9494
[Name Nothing ">"] (NumLit "5")
9595
})
9696
]
@@ -117,9 +117,9 @@ orderBy = Group "orderBy" $ map (uncurry (TestQueryExpr ansi2011))
117117

118118
]
119119
where
120-
ms o = makeSelect {qeSelectList = [(Iden [Name Nothing "a"],Nothing)]
121-
,qeFrom = [TRSimple [Name Nothing "t"]]
122-
,qeOrderBy = o}
120+
ms o = toQueryExpr $ makeSelect {msSelectList = [(Iden [Name Nothing "a"],Nothing)]
121+
,msFrom = [TRSimple [Name Nothing "t"]]
122+
,msOrderBy = o}
123123

124124
offsetFetch :: TestItem
125125
offsetFetch = Group "offsetFetch" $ map (uncurry (TestQueryExpr ansi2011))
@@ -138,11 +138,11 @@ offsetFetch = Group "offsetFetch" $ map (uncurry (TestQueryExpr ansi2011))
138138
-- ,ms (Just $ NumLit "5") (Just $ NumLit "10"))
139139
]
140140
where
141-
ms o l = makeSelect
142-
{qeSelectList = [(Iden [Name Nothing "a"],Nothing)]
143-
,qeFrom = [TRSimple [Name Nothing "t"]]
144-
,qeOffset = o
145-
,qeFetchFirst = l}
141+
ms o l = toQueryExpr $ makeSelect
142+
{msSelectList = [(Iden [Name Nothing "a"],Nothing)]
143+
,msFrom = [TRSimple [Name Nothing "t"]]
144+
,msOffset = o
145+
,msFetchFirst = l}
146146

147147
combos :: TestItem
148148
combos = Group "combos" $ map (uncurry (TestQueryExpr ansi2011))
@@ -164,12 +164,12 @@ combos = Group "combos" $ map (uncurry (TestQueryExpr ansi2011))
164164
Union SQDefault Respectively mst)
165165
]
166166
where
167-
mst = makeSelect
168-
{qeSelectList = [(Iden [Name Nothing "a"],Nothing)]
169-
,qeFrom = [TRSimple [Name Nothing "t"]]}
170-
msu = makeSelect
171-
{qeSelectList = [(Iden [Name Nothing "b"],Nothing)]
172-
,qeFrom = [TRSimple [Name Nothing "u"]]}
167+
mst = toQueryExpr $ makeSelect
168+
{msSelectList = [(Iden [Name Nothing "a"],Nothing)]
169+
,msFrom = [TRSimple [Name Nothing "t"]]}
170+
msu = toQueryExpr $ makeSelect
171+
{msSelectList = [(Iden [Name Nothing "b"],Nothing)]
172+
,msFrom = [TRSimple [Name Nothing "u"]]}
173173

174174

175175
withQueries :: TestItem
@@ -189,9 +189,9 @@ withQueries = Group "with queries" $ map (uncurry (TestQueryExpr ansi2011))
189189
,With True [(Alias (Name Nothing "u") Nothing, ms1)] ms2)
190190
]
191191
where
192-
ms c t = makeSelect
193-
{qeSelectList = [(Iden [Name Nothing c],Nothing)]
194-
,qeFrom = [TRSimple [Name Nothing t]]}
192+
ms c t = toQueryExpr $ makeSelect
193+
{msSelectList = [(Iden [Name Nothing c],Nothing)]
194+
,msFrom = [TRSimple [Name Nothing t]]}
195195
ms1 = ms "a" "t"
196196
ms2 = ms "a" "u"
197197
ms3 = ms "a" "x"

0 commit comments

Comments
 (0)