on dev help sign in
Merge pull request #12 from echatav/monoid-morphisms

monoid morphisms

commit2fc4957aa6b89ceb30243b92028d61e8f6efe15b authorEitan Chatav <eitan.chatav@gmail.com> dateMon 1 May 2017, 12:48 (9 years ago) parent954354f092ae45
2 files changed+20−16
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+12−9 squeel-postgresql/test/Squeel/PostgreSQL/QuerySpec.hs+8−7
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+12−9
@@ -214,31 +214,34 @@ renderRelation (UnsafeRelation rel wh lim off) = rel
214214 <> maybe "" ((" LIMIT " <>) . renderExpression) lim
215215 <> maybe "" ((" OFFSET " <>) . renderExpression) off
216216
217+-- | where_ is a morphism of monoids `PGBool -> End Relation` under (&&*), true
217218where_
218− :: Relation ps xss xs
219− -> Expression ps xs 'PGBool
219+ :: Expression ps xs 'PGBool
220220 -> Relation ps xss xs
221−ys `where_` condition = ys
221+ -> Relation ps xss xs
222+where_ condition ys = ys
222223 { restriction = case restriction ys of
223224 Nothing -> Just condition
224225 Just conditions -> Just (conditions &&* condition)
225226 }
226227
228+-- | limit is a morphism of semigroups `PGInt8 -> End Relation` under minB
227229limit
228− :: Relation ps xss xs
229− -> Expression ps '[] 'PGInt8
230+ :: Expression ps '[] 'PGInt8
231+ -> Relation ps xss xs
230232 -> Relation ps xss xs
231−ys `limit` n = ys
233+limit n ys = ys
232234 { limitation = case limitation ys of
233235 Nothing -> Just n
234236 Just n' -> Just (n' `minB` n)
235237 }
236238
239+-- | offset is a morphism of monoids `PGInt8 -> End Relation` under (+), 0
237240offset
238− :: Relation ps xss xs
239− -> Expression ps '[] 'PGInt8
241+ :: Expression ps '[] 'PGInt8
242+ -> Relation ps xss xs
240243 -> Relation ps xss xs
241−ys `offset` n = ys
244+offset n ys = ys
242245 { offsetting = case offsetting ys of
243246 Nothing -> Just n
244247 Just n' -> Just (n' + n)
squeel-postgresql/test/Squeel/PostgreSQL/QuerySpec.hs+8−7
@@ -8,6 +8,7 @@
88module Squeel.PostgreSQL.QuerySpec where
99
1010import Data.Boolean
11+import Data.Function
1112import Data.Proxy
1213import Data.Vinyl
1314import Test.Hspec
@@ -18,11 +19,11 @@ import Squeel.PostgreSQL.Type
1819spec :: Spec
1920spec = do
2021 it "correctly renders a simple SELECT query" $ do
21− renderQuery (select (sumAndCol1 `from` (table1 `where_` true)))
22+ renderQuery (select (sumAndCol1 `from` (table1 & where_ true)))
2223 `shouldBe`
2324 "SELECT col1+col2 AS sum, col1 AS col1 FROM table1 WHERE TRUE;"
2425 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)))
2627 `shouldBe`
2728 "SELECT col1+col2 AS sum, col1 AS col1 FROM table1 WHERE TRUE AND FALSE;"
2829 it "performs subSELECTs" $ do
@@ -30,23 +31,23 @@ spec = do
3031 `shouldBe`
3132 "SELECT * FROM SELECT col1+col2 AS sum, col1 AS col1 FROM table1;"
3233 it "does LIMIT clauses" $ do
33− renderQuery (select (star `from` (table1 `limit` 1)))
34+ renderQuery (select (star `from` (table1 & limit 1)))
3435 `shouldBe`
3536 "SELECT * FROM table1 LIMIT 1;"
3637 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)))
3839 `shouldBe`
3940 "SELECT * FROM table1 LIMIT CASE WHEN 1<=2 THEN 1 ELSE 2 END;"
4041 it "should render parameters using $ signs" $ do
41− renderQuery (select (star `from` (parameterizedTable1 `limit` param1)))
42+ renderQuery (select (star `from` (parameterizedTable1 & limit param1)))
4243 `shouldBe`
4344 "SELECT * FROM table1 LIMIT $1;"
4445 it "does OFFSET clauses" $ do
45− renderQuery (select (star `from` (table1 `offset` 1)))
46+ renderQuery (select (star `from` (table1 & offset 1)))
4647 `shouldBe`
4748 "SELECT * FROM table1 OFFSET 1;"
4849 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)))
5051 `shouldBe`
5152 "SELECT * FROM table1 OFFSET 1+2;"
5253 it "correctly render simple INSERTs" $ do