on dev help sign in
Merge pull request #12 from echatav/monoid-morphisms
2fc4957Eitan Chatav
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+12−9
@@ -214,31 +214,34 @@ renderRelation (UnsafeRelation rel wh lim off) = rel
214 <> maybe "" ((" LIMIT " <>) . renderExpression) lim
214 <> maybe "" ((" LIMIT " <>) . renderExpression) lim
215 <> maybe "" ((" OFFSET " <>) . renderExpression) off
215 <> maybe "" ((" OFFSET " <>) . renderExpression) off
216
216
217+-- | where_ is a morphism of monoids `PGBool -> End Relation` under (&&*), true
217where_
218where_
218− :: Relation ps xss xs
219− -> Expression ps xs 'PGBool
219+ :: Expression ps xs 'PGBool
220 -> Relation ps xss xs
220 -> Relation ps xss xs
221+ -> Relation ps xss xs
221−ys `where_` condition = ys
222+where_ condition ys = ys
222 { restriction = case restriction ys of
223 { restriction = case restriction ys of
223 Nothing -> Just condition
224 Nothing -> Just condition
224 Just conditions -> Just (conditions &&* condition)
225 Just conditions -> Just (conditions &&* condition)
225 }
226 }
226
227
228+-- | limit is a morphism of semigroups `PGInt8 -> End Relation` under minB
227limit
229limit
228− :: Relation ps xss xs
229− -> Expression ps '[] 'PGInt8
230+ :: Expression ps '[] 'PGInt8
231+ -> Relation ps xss xs
230 -> Relation ps xss xs
232 -> Relation ps xss xs
231−ys `limit` n = ys
233+limit n ys = ys
232 { limitation = case limitation ys of
234 { limitation = case limitation ys of
233 Nothing -> Just n
235 Nothing -> Just n
234 Just n' -> Just (n' `minB` n)
236 Just n' -> Just (n' `minB` n)
235 }
237 }
236
238
239+-- | offset is a morphism of monoids `PGInt8 -> End Relation` under (+), 0
237offset
240offset
238− :: Relation ps xss xs
239− -> Expression ps '[] 'PGInt8
241+ :: Expression ps '[] 'PGInt8
242+ -> Relation ps xss xs
240 -> Relation ps xss xs
243 -> Relation ps xss xs
241−ys `offset` n = ys
244+offset n ys = ys
242 { offsetting = case offsetting ys of
245 { offsetting = case offsetting ys of
243 Nothing -> Just n
246 Nothing -> Just n
244 Just n' -> Just (n' + n)
247 Just n' -> Just (n' + n)
squeel-postgresql/test/Squeel/PostgreSQL/QuerySpec.hs+8−7
@@ -8,6 +8,7 @@
8module Squeel.PostgreSQL.QuerySpec where
8module Squeel.PostgreSQL.QuerySpec where
9
9
10import Data.Boolean
10import Data.Boolean
11+import Data.Function
11import Data.Proxy
12import Data.Proxy
12import Data.Vinyl
13import Data.Vinyl
13import Test.Hspec
14import Test.Hspec
@@ -18,11 +19,11 @@ import Squeel.PostgreSQL.Type
18spec :: Spec
19spec :: Spec
19spec = do
20spec = do
20 it "correctly renders a simple SELECT query" $ do
21 it "correctly renders a simple SELECT query" $ do
21− renderQuery (select (sumAndCol1 `from` (table1 `where_` true)))
22+ renderQuery (select (sumAndCol1 `from` (table1 & where_ true)))
22 `shouldBe`
23 `shouldBe`
23 "SELECT col1+col2 AS sum, col1 AS col1 FROM table1 WHERE TRUE;"
24 "SELECT col1+col2 AS sum, col1 AS col1 FROM table1 WHERE TRUE;"
24 it "combines WHEREs using AND" $ do
25 it "combines WHEREs using AND" $ do
25− renderQuery (select (sumAndCol1 `from` (table1 `where_` true `where_` false)))
26+ renderQuery (select (sumAndCol1 `from` (table1 & where_ true & where_ false)))
26 `shouldBe`
27 `shouldBe`
27 "SELECT col1+col2 AS sum, col1 AS col1 FROM table1 WHERE TRUE AND FALSE;"
28 "SELECT col1+col2 AS sum, col1 AS col1 FROM table1 WHERE TRUE AND FALSE;"
28 it "performs subSELECTs" $ do
29 it "performs subSELECTs" $ do
@@ -30,23 +31,23 @@ spec = do
30 `shouldBe`
31 `shouldBe`
31 "SELECT * FROM SELECT col1+col2 AS sum, col1 AS col1 FROM table1;"
32 "SELECT * FROM SELECT col1+col2 AS sum, col1 AS col1 FROM table1;"
32 it "does LIMIT clauses" $ do
33 it "does LIMIT clauses" $ do
33− renderQuery (select (star `from` (table1 `limit` 1)))
34+ renderQuery (select (star `from` (table1 & limit 1)))
34 `shouldBe`
35 `shouldBe`
35 "SELECT * FROM table1 LIMIT 1;"
36 "SELECT * FROM table1 LIMIT 1;"
36 it "should use the minimum of given LIMITs" $ do
37 it "should use the minimum of given LIMITs" $ do
37− renderQuery (select (star `from` (table1 `limit` 1 `limit` 2)))
38+ renderQuery (select (star `from` (table1 & limit 1 & limit 2)))
38 `shouldBe`
39 `shouldBe`
39 "SELECT * FROM table1 LIMIT CASE WHEN 1<=2 THEN 1 ELSE 2 END;"
40 "SELECT * FROM table1 LIMIT CASE WHEN 1<=2 THEN 1 ELSE 2 END;"
40 it "should render parameters using $ signs" $ do
41 it "should render parameters using $ signs" $ do
41− renderQuery (select (star `from` (parameterizedTable1 `limit` param1)))
42+ renderQuery (select (star `from` (parameterizedTable1 & limit param1)))
42 `shouldBe`
43 `shouldBe`
43 "SELECT * FROM table1 LIMIT $1;"
44 "SELECT * FROM table1 LIMIT $1;"
44 it "does OFFSET clauses" $ do
45 it "does OFFSET clauses" $ do
45− renderQuery (select (star `from` (table1 `offset` 1)))
46+ renderQuery (select (star `from` (table1 & offset 1)))
46 `shouldBe`
47 `shouldBe`
47 "SELECT * FROM table1 OFFSET 1;"
48 "SELECT * FROM table1 OFFSET 1;"
48 it "should use the sum of given OFFSETs" $ do
49 it "should use the sum of given OFFSETs" $ do
49− renderQuery (select (star `from` (table1 `offset` 1 `offset` 2)))
50+ renderQuery (select (star `from` (table1 & offset 1 & offset 2)))
50 `shouldBe`
51 `shouldBe`
51 "SELECT * FROM table1 OFFSET 1+2;"
52 "SELECT * FROM table1 OFFSET 1+2;"
52 it "correctly render simple INSERTs" $ do
53 it "correctly render simple INSERTs" $ do