on dev help sign in
remove Projection type
18f8fe7Eitan Chatav
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+41−34
@@ -127,13 +127,25 @@ instance Boolean (Expression params tables 'PGBool) where
127type instance BooleanOf (Expression params tables ty) =
127type instance BooleanOf (Expression params tables ty) =
128 Expression params tables 'PGBool
128 Expression params tables 'PGBool
129
129
130+caseWhenThenElse
130−instance IfB (Expression params tables ty) where
131+ :: [(Expression params tables 'PGBool, Expression params tables ty)]
132+ -> Expression params tables ty
133+ -> Expression params tables ty
131− ifB if_ then_ else_ = UnsafeExpression $ mconcat
134+caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
135+ [ "CASE"
136+ , mconcat
137+ [ mconcat
132− [ "CASE WHEN ",renderExpression if_
138+ [ " WHEN ", renderExpression when_
133− , " THEN ",renderExpression then_
139+ , " THEN ", renderExpression then_
134− , " ELSE ",renderExpression else_
140+ ]
135− , " END"
141+ | (when_,then_) <- whenThens
136 ]
142 ]
143+ , " ELSE ", renderExpression else_
144+ , " END"
145+ ]
146+
147+instance IfB (Expression params tables ty) where
148+ ifB if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
137
149
138instance EqB (Expression params tables ty) where
150instance EqB (Expression params tables ty) where
139 (==*) = unsafeBinaryOp "="
151 (==*) = unsafeBinaryOp "="
@@ -180,30 +192,6 @@ innerJoin tab on tabs = UnsafeTableRef $
180 <> " ON " <>
192 <> " ON " <>
181 renderExpression on
193 renderExpression on
182
194
183−newtype Projection
184− (params :: [PGType])
185− (tables :: [(Symbol,[(Symbol,PGType)])])
186− (columns :: [(Symbol,PGType)]) =
187− UnsafeProjection { renderProjection :: ByteString }
188−
189−star :: Projection params '[table ::: columns] columns
190−star = UnsafeProjection "*"
191−
192−tableStar
193− :: HasTable table tables columns
194− => Alias table -> Projection params tables columns
195−tableStar (Alias table) = UnsafeProjection $
196− fromString (symbolVal' table) <> ".*"
197−
198−project
199− :: Rec (Aliased (Expression params tables)) columns
200− -> Projection params tables columns
201−project
202− = UnsafeProjection
203− . ByteString.intercalate ", "
204− . recordToList
205− . rmap (Const . renderAliased renderExpression)
206−
207data Clauses params tables = Clauses
195data Clauses params tables = Clauses
208 { whereClause :: Maybe (Expression params tables 'PGBool)
196 { whereClause :: Maybe (Expression params tables 'PGBool)
209 , limitClause :: Maybe (Expression params '[] 'PGInt8)
197 , limitClause :: Maybe (Expression params '[] 'PGInt8)
@@ -288,12 +276,31 @@ newtype Selection
288 = UnsafeSelection
276 = UnsafeSelection
289 { renderSelection :: ByteString }
277 { renderSelection :: ByteString }
290
278
279+starFrom
280+ :: tables ~ '[table ::: columns]
281+ => TableExpression params schema tables
282+ -> Selection params schema columns
283+starFrom tabs = UnsafeSelection $ "* FROM " <> renderTabulation tabs
284+
285+tableStarFrom
286+ :: HasTable table tables columns
287+ => Alias table
288+ -> TableExpression params schema tables
289+ -> Selection params schema columns
290+Alias tab `tableStarFrom` tabs = UnsafeSelection $
291+ fromString (symbolVal' tab) <> ".* FROM" <> renderTabulation tabs
292+
291from
293from
294+ :: Rec (Aliased (Expression params tables)) columns
292− :: TableExpression params schema tables
295+ -> TableExpression params schema tables
293− -> Projection params tables columns
294 -> Selection params schema columns
296 -> Selection params schema columns
295−from tabs projection = UnsafeSelection $
297+list `from` tabs = UnsafeSelection $
296− renderProjection projection <> " FROM " <> renderTabulation tabs
298+ renderList list <> " FROM " <> renderTabulation tabs
299+ where
300+ renderList
301+ = ByteString.intercalate ", "
302+ . recordToList
303+ . rmap (Const . renderAliased renderExpression)
297
304
298newtype Query
305newtype Query
299 (params :: [PGType])
306 (params :: [PGType])
squeel-postgresql/test/Squeel/PostgreSQL/QuerySpec.hs+14−14
@@ -24,50 +24,50 @@ spec = do
24 let
24 let
25 statement :: Query '[] Tables Tables SumAndCol1
25 statement :: Query '[] Tables Tables SumAndCol1
26 statement = select $
26 statement = select $
27− project ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
27+ ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
28− & from (#table1 & where_ true)
28+ `from` (#table1 & where_ true)
29 statement `shouldRenderAs`
29 statement `shouldRenderAs`
30 "SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 WHERE TRUE;"
30 "SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 WHERE TRUE;"
31 it "combines WHEREs using AND" $ do
31 it "combines WHEREs using AND" $ do
32 let
32 let
33 statement :: Query '[] Tables Tables SumAndCol1
33 statement :: Query '[] Tables Tables SumAndCol1
34 statement = select $
34 statement = select $
35− project ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
35+ ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
36− & from (#table1 & where_ true & where_ false)
36+ `from` (#table1 & where_ true & where_ false)
37 statement `shouldRenderAs`
37 statement `shouldRenderAs`
38 "SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 WHERE (TRUE AND FALSE);"
38 "SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 WHERE (TRUE AND FALSE);"
39 it "performs sub SELECTs" $ do
39 it "performs sub SELECTs" $ do
40 let
40 let
41− selection = project ((#col1 + #col2) `As` #sum :& #col1 :& RNil) & from #table1
41+ selection = ((#col1 + #col2) `As` #sum :& #col1 :& RNil) `from` #table1
42 statement :: Query '[] Tables Tables SumAndCol1
42 statement :: Query '[] Tables Tables SumAndCol1
43− statement = select $ star & from (subselect (selection `As` #sub))
43+ statement = select $ starFrom (subselect (selection `As` #sub))
44 statement `shouldRenderAs`
44 statement `shouldRenderAs`
45 "SELECT * FROM SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 AS sub;"
45 "SELECT * FROM SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 AS sub;"
46 it "does LIMIT clauses" $ do
46 it "does LIMIT clauses" $ do
47 let
47 let
48 statement :: Query '[] Tables Tables Columns
48 statement :: Query '[] Tables Tables Columns
49− statement = select $ star & from (#table1 & limit 1)
49+ statement = select $ starFrom (#table1 & limit 1)
50 statement `shouldRenderAs` "SELECT * FROM table1 LIMIT 1;"
50 statement `shouldRenderAs` "SELECT * FROM table1 LIMIT 1;"
51 it "should use the minimum of given LIMITs" $ do
51 it "should use the minimum of given LIMITs" $ do
52 let
52 let
53 statement :: Query '[] Tables Tables Columns
53 statement :: Query '[] Tables Tables Columns
54− statement = select $ star & from (#table1 & limit 1 & limit 2)
54+ statement = select $ starFrom (#table1 & limit 1 & limit 2)
55 statement `shouldRenderAs`
55 statement `shouldRenderAs`
56 "SELECT * FROM table1 LIMIT CASE WHEN (1 <= 2) THEN 1 ELSE 2 END;"
56 "SELECT * FROM table1 LIMIT CASE WHEN (1 <= 2) THEN 1 ELSE 2 END;"
57 it "should render parameters using $ signs" $ do
57 it "should render parameters using $ signs" $ do
58 let
58 let
59 statement :: Query '[ 'PGInt8] Tables Tables Columns
59 statement :: Query '[ 'PGInt8] Tables Tables Columns
60− statement = select $ star & from (#table1 & limit param1)
60+ statement = select $ starFrom (#table1 & limit param1)
61 statement `shouldRenderAs` "SELECT * FROM table1 LIMIT $1;"
61 statement `shouldRenderAs` "SELECT * FROM table1 LIMIT $1;"
62 it "does OFFSET clauses" $ do
62 it "does OFFSET clauses" $ do
63 let
63 let
64 statement :: Query '[] Tables Tables Columns
64 statement :: Query '[] Tables Tables Columns
65− statement = select $ star & from (#table1 & offset 1)
65+ statement = select $ starFrom (#table1 & offset 1)
66 statement `shouldRenderAs` "SELECT * FROM table1 OFFSET 1;"
66 statement `shouldRenderAs` "SELECT * FROM table1 OFFSET 1;"
67 it "should use the sum of given OFFSETs" $ do
67 it "should use the sum of given OFFSETs" $ do
68 let
68 let
69 statement :: Query '[] Tables Tables Columns
69 statement :: Query '[] Tables Tables Columns
70− statement = select $ star & from (#table1 & offset 1 & offset 2)
70+ statement = select $ starFrom (#table1 & offset 1 & offset 2)
71 statement `shouldRenderAs` "SELECT * FROM table1 OFFSET (1 + 2);"
71 statement `shouldRenderAs` "SELECT * FROM table1 OFFSET (1 + 2);"
72 it "correctly render simple INSERTs" $ do
72 it "correctly render simple INSERTs" $ do
73 let
73 let
@@ -84,14 +84,14 @@ spec = do
84 "INSERT INTO students (name) VALUES (E'Robert''); DROP TABLE students;');"
84 "INSERT INTO students (name) VALUES (E'Robert''); DROP TABLE students;');"
85 describe "JOINs" $ do
85 describe "JOINs" $ do
86 let
86 let
87− vals = project $
87+ vals =
88 #orders .&. #orderVal `As` #orderVal
88 #orders .&. #orderVal `As` #orderVal
89 :& #customers .&. #customerVal `As` #customerVal
89 :& #customers .&. #customerVal `As` #customerVal
90 :& #shippers .&. #shipperVal `As` #shipperVal :& RNil
90 :& #shippers .&. #shipperVal `As` #shipperVal :& RNil
91 it "should render CROSS JOINs" $ do
91 it "should render CROSS JOINs" $ do
92 let
92 let
93 statement :: Query '[] JoinTables JoinTables ValueColumns
93 statement :: Query '[] JoinTables JoinTables ValueColumns
94− statement = select $ vals & from
94+ statement = select $ vals `from`
95 (tables (#orders & crossJoin #customers & crossJoin #shippers))
95 (tables (#orders & crossJoin #customers & crossJoin #shippers))
96 statement `shouldRenderAs`
96 statement `shouldRenderAs`
97 "SELECT\
97 "SELECT\
@@ -111,7 +111,7 @@ spec = do
111 & innerJoin #shippers
111 & innerJoin #shippers
112 (#orders .&. #shipperID ==* #shippers .&. #shipperID)
112 (#orders .&. #shipperID ==* #shippers .&. #shipperID)
113 selection :: Query '[] JoinTables JoinTables ValueColumns
113 selection :: Query '[] JoinTables JoinTables ValueColumns
114− selection = select $ vals & from innerJoins
114+ selection = select $ vals `from` innerJoins
115 selection `shouldRenderAs`
115 selection `shouldRenderAs`
116 "SELECT\
116 "SELECT\
117 \ orders.orderVal AS orderVal,\
117 \ orders.orderVal AS orderVal,\