on dev help sign in
remove Projection type

and add `caseWhenThenElse`

commit18f8fe7c045f60362ef47c80d39d2202bcf99dcc authorEitan Chatav <eitan.chatav@gmail.com> dateFri 9 Jun 2017, 08:43 (9 years ago) parentd7e5675
2 files changed+55−48
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+41−34 squeel-postgresql/test/Squeel/PostgreSQL/QuerySpec.hs+14−14
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+41−34
@@ -127,13 +127,25 @@ instance Boolean (Expression params tables 'PGBool) where
127127type instance BooleanOf (Expression params tables ty) =
128128 Expression params tables 'PGBool
129129
130−instance IfB (Expression params tables ty) where
131− ifB if_ then_ else_ = UnsafeExpression $ mconcat
132− [ "CASE WHEN ",renderExpression if_
133− , " THEN ",renderExpression then_
134− , " ELSE ",renderExpression else_
135− , " END"
130+caseWhenThenElse
131+ :: [(Expression params tables 'PGBool, Expression params tables ty)]
132+ -> Expression params tables ty
133+ -> Expression params tables ty
134+caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
135+ [ "CASE"
136+ , mconcat
137+ [ mconcat
138+ [ " WHEN ", renderExpression when_
139+ , " THEN ", renderExpression then_
140+ ]
141+ | (when_,then_) <- whenThens
136142 ]
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_
137149
138150instance EqB (Expression params tables ty) where
139151 (==*) = unsafeBinaryOp "="
@@ -180,30 +192,6 @@ innerJoin tab on tabs = UnsafeTableRef $
180192 <> " ON " <>
181193 renderExpression on
182194
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−
207195data Clauses params tables = Clauses
208196 { whereClause :: Maybe (Expression params tables 'PGBool)
209197 , limitClause :: Maybe (Expression params '[] 'PGInt8)
@@ -288,12 +276,31 @@ newtype Selection
288276 = UnsafeSelection
289277 { renderSelection :: ByteString }
290278
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+
291293from
292− :: TableExpression params schema tables
293− -> Projection params tables columns
294+ :: Rec (Aliased (Expression params tables)) columns
295+ -> TableExpression params schema tables
294296 -> Selection params schema columns
295−from tabs projection = UnsafeSelection $
296− renderProjection projection <> " FROM " <> renderTabulation tabs
297+list `from` tabs = UnsafeSelection $
298+ renderList list <> " FROM " <> renderTabulation tabs
299+ where
300+ renderList
301+ = ByteString.intercalate ", "
302+ . recordToList
303+ . rmap (Const . renderAliased renderExpression)
297304
298305newtype Query
299306 (params :: [PGType])
squeel-postgresql/test/Squeel/PostgreSQL/QuerySpec.hs+14−14
@@ -24,50 +24,50 @@ spec = do
2424 let
2525 statement :: Query '[] Tables Tables SumAndCol1
2626 statement = select $
27− project ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
28− & from (#table1 & where_ true)
27+ ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
28+ `from` (#table1 & where_ true)
2929 statement `shouldRenderAs`
3030 "SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 WHERE TRUE;"
3131 it "combines WHEREs using AND" $ do
3232 let
3333 statement :: Query '[] Tables Tables SumAndCol1
3434 statement = select $
35− project ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
36− & from (#table1 & where_ true & where_ false)
35+ ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
36+ `from` (#table1 & where_ true & where_ false)
3737 statement `shouldRenderAs`
3838 "SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 WHERE (TRUE AND FALSE);"
3939 it "performs sub SELECTs" $ do
4040 let
41− selection = project ((#col1 + #col2) `As` #sum :& #col1 :& RNil) & from #table1
41+ selection = ((#col1 + #col2) `As` #sum :& #col1 :& RNil) `from` #table1
4242 statement :: Query '[] Tables Tables SumAndCol1
43− statement = select $ star & from (subselect (selection `As` #sub))
43+ statement = select $ starFrom (subselect (selection `As` #sub))
4444 statement `shouldRenderAs`
4545 "SELECT * FROM SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 AS sub;"
4646 it "does LIMIT clauses" $ do
4747 let
4848 statement :: Query '[] Tables Tables Columns
49− statement = select $ star & from (#table1 & limit 1)
49+ statement = select $ starFrom (#table1 & limit 1)
5050 statement `shouldRenderAs` "SELECT * FROM table1 LIMIT 1;"
5151 it "should use the minimum of given LIMITs" $ do
5252 let
5353 statement :: Query '[] Tables Tables Columns
54− statement = select $ star & from (#table1 & limit 1 & limit 2)
54+ statement = select $ starFrom (#table1 & limit 1 & limit 2)
5555 statement `shouldRenderAs`
5656 "SELECT * FROM table1 LIMIT CASE WHEN (1 <= 2) THEN 1 ELSE 2 END;"
5757 it "should render parameters using $ signs" $ do
5858 let
5959 statement :: Query '[ 'PGInt8] Tables Tables Columns
60− statement = select $ star & from (#table1 & limit param1)
60+ statement = select $ starFrom (#table1 & limit param1)
6161 statement `shouldRenderAs` "SELECT * FROM table1 LIMIT $1;"
6262 it "does OFFSET clauses" $ do
6363 let
6464 statement :: Query '[] Tables Tables Columns
65− statement = select $ star & from (#table1 & offset 1)
65+ statement = select $ starFrom (#table1 & offset 1)
6666 statement `shouldRenderAs` "SELECT * FROM table1 OFFSET 1;"
6767 it "should use the sum of given OFFSETs" $ do
6868 let
6969 statement :: Query '[] Tables Tables Columns
70− statement = select $ star & from (#table1 & offset 1 & offset 2)
70+ statement = select $ starFrom (#table1 & offset 1 & offset 2)
7171 statement `shouldRenderAs` "SELECT * FROM table1 OFFSET (1 + 2);"
7272 it "correctly render simple INSERTs" $ do
7373 let
@@ -84,14 +84,14 @@ spec = do
8484 "INSERT INTO students (name) VALUES (E'Robert''); DROP TABLE students;');"
8585 describe "JOINs" $ do
8686 let
87− vals = project $
87+ vals =
8888 #orders .&. #orderVal `As` #orderVal
8989 :& #customers .&. #customerVal `As` #customerVal
9090 :& #shippers .&. #shipperVal `As` #shipperVal :& RNil
9191 it "should render CROSS JOINs" $ do
9292 let
9393 statement :: Query '[] JoinTables JoinTables ValueColumns
94− statement = select $ vals & from
94+ statement = select $ vals `from`
9595 (tables (#orders & crossJoin #customers & crossJoin #shippers))
9696 statement `shouldRenderAs`
9797 "SELECT\
@@ -111,7 +111,7 @@ spec = do
111111 & innerJoin #shippers
112112 (#orders .&. #shipperID ==* #shippers .&. #shipperID)
113113 selection :: Query '[] JoinTables JoinTables ValueColumns
114− selection = select $ vals & from innerJoins
114+ selection = select $ vals `from` innerJoins
115115 selection `shouldRenderAs`
116116 "SELECT\
117117 \ orders.orderVal AS orderVal,\