on dev help sign in
finish boolean removal
9cc9139Eitan Chatav
squeel-postgresql/squeel-postgresql.cabal−2
@@ -23,7 +23,6 @@ library
23 build-depends:
23 build-depends:
24 aeson
24 aeson
25 , base
25 , base
26− , Boolean
27 , bytestring
26 , bytestring
28 , generics-sop
27 , generics-sop
29 , lifted-base
28 , lifted-base
@@ -48,7 +47,6 @@ test-suite squeel-postgresql-test
48 Squeel.PostgreSQL.StatementSpec
47 Squeel.PostgreSQL.StatementSpec
49 build-depends:
48 build-depends:
50 base
49 base
51− , Boolean
52 , generics-sop
50 , generics-sop
53 , hspec
51 , hspec
54 , squeel-postgresql
52 , squeel-postgresql
squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+85−73
@@ -1,6 +1,7 @@
1{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
1{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
2{-# LANGUAGE
2{-# LANGUAGE
3 DataKinds
3 DataKinds
4+ , FlexibleContexts
4 , FlexibleInstances
5 , FlexibleInstances
5 , FunctionalDependencies
6 , FunctionalDependencies
6 , GADTs
7 , GADTs
@@ -180,6 +181,14 @@ instance (PGFloating ty, PGCast 'PGNumeric ty, PGTyped ty)
180 acosh x = log (x + sqrt (x*x - 1))
181 acosh x = log (x + sqrt (x*x - 1))
181 atanh x = log ((1 + x) / (1 - x)) / 2
182 atanh x = log ((1 + x) / (1 - x)) / 2
182
183
184+atan2
185+ :: PGFloating float
186+ => Expression params tables ('Required (nullity float))
187+ -> Expression params tables ('Required (nullity float))
188+ -> Expression params tables ('Required (nullity float))
189+atan2 y x = UnsafeExpression $
190+ "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
191+
183class PGCast (ty0 :: PGType) (ty1 :: PGType) where
192class PGCast (ty0 :: PGType) (ty1 :: PGType) where
184 cast
193 cast
185 :: TypeExpression ('Required ('Null ty1))
194 :: TypeExpression ('Required ('Null ty1))
@@ -200,52 +209,52 @@ instance PGCast 'PGInt8 'PGFloat4
200instance PGCast 'PGInt8 'PGFloat8
209instance PGCast 'PGInt8 'PGFloat8
201instance PGCast 'PGInt8 'PGNumeric
210instance PGCast 'PGInt8 'PGNumeric
202
211
212+div
203−-- instance (PGNum ty, PGTyped ty, PGCast 'PGInt8 ty)
213+ :: (PGNum ty, PGTyped ty, PGCast 'PGInt8 ty, PGCast ty 'PGInt8)
204−-- => NumB (Expression params tables ('Required (nullity ty))) where
214+ => Expression params tables ('Required (nullity ty))
205−-- type IntegerOf (Expression params tables ('Required (nullity ty)))
215+ -> Expression params tables ('Required (nullity ty))
206−-- = (Expression params tables ('Required (nullity 'PGInt8)))
216+ -> Expression params tables ('Required (nullity ty))
207−-- fromIntegerB = cast pgtype
217+div = unsafeBinaryOp "/"
208−
218+
219+mod
209−-- instance (PGNum ty, PGTyped ty, PGCast 'PGInt8 ty, PGCast ty 'PGInt8)
220+ :: (PGNum ty, PGTyped ty, PGCast 'PGInt8 ty, PGCast ty 'PGInt8)
210−-- => IntegralB (Expression params tables ('Required ('NotNull ty))) where
221+ => Expression params tables ('Required (nullity ty))
211−-- quot = unsafeBinaryOp "/"
222+ -> Expression params tables ('Required (nullity ty))
212−-- rem = unsafeBinaryOp "%"
223+ -> Expression params tables ('Required (nullity ty))
213−-- div = unsafeBinaryOp "/"
214−-- mod = unsafeBinaryOp "%"
224+mod = unsafeBinaryOp "%"
215−-- toIntegerB = cast int8
225+
216−
226+truncate
217−-- instance
227+ :: (PGFractional frac, PGCast 'PGInt4 int, PGTyped int)
218−-- ( IntegerOf (Expression params tables ('Required ('NotNull ty)))
228+ => Expression params tables ('Required (nullity frac))
219−-- ~ (Expression params tables ('Required ('NotNull 'PGInt8)))
229+ -> Expression params tables ('Required (nullity int))
220−-- , PGNum ty
230+truncate = cast pgtype . truncate'
221−-- , PGTyped ty
231+ where
222−-- , PGCast 'PGInt8 ty
232+ truncate'
223−-- , PGFractional ty
224−-- )
225−-- => RealFracB (Expression params tables ('Required ('NotNull ty))) where
233+ :: Expression params tables ('Required (nullity frac))
234+ -> Expression params tables ('Required (nullity 'PGInt4))
226−-- properFraction x = (truncate x, x - unsafeFunction "trunc" x)
235+ truncate' = unsafeFunction "trunc"
227−-- truncate = fromIntegerB . unsafeFunction "trunc"
236+
228−-- round = fromIntegerB . unsafeFunction "round"
237+round
229−-- ceiling = fromIntegerB . unsafeFunction "ceiling"
238+ :: (PGFractional frac, PGCast 'PGInt4 int, PGTyped int)
230−-- floor = fromIntegerB . unsafeFunction "floor"
239+ => Expression params tables ('Required (nullity frac))
231−
240+ -> Expression params tables ('Required (nullity int))
232−-- instance
241+round = cast pgtype . round'
242+ where
243+ round'
233−-- ( IntegerOf (Expression params tables ('Required ('NotNull ty)))
244+ :: Expression params tables ('Required (nullity frac))
234−-- ~ (Expression params tables ('Required ('NotNull 'PGInt8)))
245+ -> Expression params tables ('Required (nullity 'PGInt4))
235−-- , PGNum ty
246+ round' = unsafeFunction "round"
236−-- , PGTyped ty
247+
237−-- , PGCast 'PGInt8 ty
248+ceiling
238−-- , PGCast 'PGNumeric ty
249+ :: (PGFractional frac, PGCast 'PGInt4 int, PGTyped int)
239−-- , PGFloating ty
240−-- )
241−-- => RealFloatB (Expression params tables ('Required ('NotNull ty))) where
250+ => Expression params tables ('Required (nullity frac))
242−-- isNaN x = x ==* UnsafeExpression "\'NaN\'"
251+ -> Expression params tables ('Required (nullity int))
243−-- isInfinite x = x ==* UnsafeExpression "\'Infinity\'"
252+ceiling = cast pgtype . ceiling'
244−-- ||* x ==* UnsafeExpression "\'-Infinity\'"
253+ where
245−-- isNegativeZero x = x ==* UnsafeExpression "-0"
254+ ceiling'
246−-- isIEEE _ = true
255+ :: Expression params tables ('Required (nullity frac))
247−-- atan2 y x = UnsafeExpression $
256+ -> Expression params tables ('Required (nullity 'PGInt4))
248−-- "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
257+ ceiling' = unsafeFunction "ceiling"
249
258
250instance IsString (Expression params tables ('Required (nullity 'PGText))) where
259instance IsString (Expression params tables ('Required (nullity 'PGText))) where
251 fromString str = UnsafeExpression $
260 fromString str = UnsafeExpression $
@@ -262,31 +271,33 @@ instance IsString (Expression params tables ('Required (nullity 'PGText))) where
262 '\\' -> "\\\\"
271 '\\' -> "\\\\"
263 c -> [c]
272 c -> [c]
264
273
265−-- instance Boolean (Expression params tables ('Required ('NotNull 'PGBool))) where
274+type Predicate params tables = Expression params tables ('Required ('NotNull 'PGBool))
275+
266−true :: Expression params tables ('Required ('NotNull 'PGBool))
276+true :: Predicate params tables
267true = UnsafeExpression "TRUE"
277true = UnsafeExpression "TRUE"
278+
268−false :: Expression params tables ('Required ('NotNull 'PGBool))
279+false :: Predicate params tables
269false = UnsafeExpression "FALSE"
280false = UnsafeExpression "FALSE"
281+
270notB
282notB
271− :: Expression params tables ('Required ('NotNull 'PGBool))
283+ :: Predicate params tables
272− -> Expression params tables ('Required ('NotNull 'PGBool))
284+ -> Predicate params tables
273notB = unsafeUnaryOp "NOT"
285notB = unsafeUnaryOp "NOT"
286+
274(&&*)
287(&&*)
275− :: Expression params tables ('Required ('NotNull 'PGBool))
288+ :: Predicate params tables
276− -> Expression params tables ('Required ('NotNull 'PGBool))
289+ -> Predicate params tables
277− -> Expression params tables ('Required ('NotNull 'PGBool))
290+ -> Predicate params tables
278(&&*) = unsafeBinaryOp "AND"
291(&&*) = unsafeBinaryOp "AND"
292+
279(||*)
293(||*)
280− :: Expression params tables ('Required ('NotNull 'PGBool))
294+ :: Predicate params tables
281− -> Expression params tables ('Required ('NotNull 'PGBool))
295+ -> Predicate params tables
282− -> Expression params tables ('Required ('NotNull 'PGBool))
296+ -> Predicate params tables
283(||*) = unsafeBinaryOp "OR"
297(||*) = unsafeBinaryOp "OR"
284
298
285−-- type instance BooleanOf (Expression params tables ty) =
286−-- Expression params tables ('Required ('NotNull 'PGBool))
287−
288caseWhenThenElse
299caseWhenThenElse
289− :: [(Expression params tables ('Required ('NotNull 'PGBool)), Expression params tables ty)]
300+ :: [(Predicate params tables, Expression params tables ty)]
290 -> Expression params tables ty
301 -> Expression params tables ty
291 -> Expression params tables ty
302 -> Expression params tables ty
292caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
303caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
@@ -302,46 +313,47 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
302 , " END"
313 , " END"
303 ]
314 ]
304
315
305−-- instance IfB (Expression params tables ty) where
306ifThenElse
316ifThenElse
307− :: Expression params tables ('Required ('NotNull 'PGBool))
317+ :: Predicate params tables
308 -> Expression params tables ty
318 -> Expression params tables ty
309 -> Expression params tables ty
319 -> Expression params tables ty
310 -> Expression params tables ty
320 -> Expression params tables ty
311ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
321ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
312
322
313−-- instance EqB (Expression params tables (optionality ('NotNull ty))) where
314(==*)
323(==*)
315 :: Expression params tables ty
324 :: Expression params tables ty
316 -> Expression params tables ty
325 -> Expression params tables ty
317− -> Expression params tables ('Required ('NotNull 'PGBool))
326+ -> Predicate params tables
318(==*) = unsafeBinaryOp "="
327(==*) = unsafeBinaryOp "="
328+
319(/=*)
329(/=*)
320 :: Expression params tables ty
330 :: Expression params tables ty
321 -> Expression params tables ty
331 -> Expression params tables ty
322− -> Expression params tables ('Required ('NotNull 'PGBool))
332+ -> Predicate params tables
323(/=*) = unsafeBinaryOp "<>"
333(/=*) = unsafeBinaryOp "<>"
324
334
325−-- instance OrdB (Expression params tables (optionality ('NotNull ty))) where
326(>=*)
335(>=*)
327 :: Expression params tables ty
336 :: Expression params tables ty
328 -> Expression params tables ty
337 -> Expression params tables ty
329− -> Expression params tables ('Required ('NotNull 'PGBool))
338+ -> Predicate params tables
330(>=*) = unsafeBinaryOp ">="
339(>=*) = unsafeBinaryOp ">="
340+
331(<*)
341(<*)
332 :: Expression params tables ty
342 :: Expression params tables ty
333 -> Expression params tables ty
343 -> Expression params tables ty
334− -> Expression params tables ('Required ('NotNull 'PGBool))
344+ -> Predicate params tables
335(<*) = unsafeBinaryOp "<"
345(<*) = unsafeBinaryOp "<"
346+
336(<=*)
347(<=*)
337 :: Expression params tables ty
348 :: Expression params tables ty
338 -> Expression params tables ty
349 -> Expression params tables ty
339− -> Expression params tables ('Required ('NotNull 'PGBool))
350+ -> Predicate params tables
340(<=*) = unsafeBinaryOp "<="
351(<=*) = unsafeBinaryOp "<="
352+
341(>*)
353(>*)
342 :: Expression params tables ty
354 :: Expression params tables ty
343 -> Expression params tables ty
355 -> Expression params tables ty
344− -> Expression params tables ('Required ('NotNull 'PGBool))
356+ -> Predicate params tables
345(>*) = unsafeBinaryOp ">"
357(>*) = unsafeBinaryOp ">"
346
358
347{-----------------------------------------
359{-----------------------------------------
@@ -474,7 +486,7 @@ data TableExpression
474 (tables :: [(Symbol,[(Symbol,ColumnType)])])
486 (tables :: [(Symbol,[(Symbol,ColumnType)])])
475 = TableExpression
487 = TableExpression
476 { joinClause :: Join params schema tables
488 { joinClause :: Join params schema tables
477− , whereClause :: [Expression params tables ('Required ('NotNull 'PGBool))]
489+ , whereClause :: [Predicate params tables]
478 , limitClause :: [Expression params '[] ('Required ('NotNull 'PGInt8))]
490 , limitClause :: [Expression params '[] ('Required ('NotNull 'PGInt8))]
479 , offsetClause :: [Expression params '[] ('Required ('NotNull 'PGInt8))]
491 , offsetClause :: [Expression params '[] ('Required ('NotNull 'PGInt8))]
480 }
492 }
@@ -513,7 +525,7 @@ join
513join tables = TableExpression tables [] [] []
525join tables = TableExpression tables [] [] []
514
526
515where_
527where_
516− :: Expression params tables ('Required ('NotNull 'PGBool))
528+ :: Predicate params tables
517 -> TableExpression params schema tables
529 -> TableExpression params schema tables
518 -> TableExpression params schema tables
530 -> TableExpression params schema tables
519where_ wh (TableExpression tabs whs lims offs) = TableExpression tabs (wh:whs) lims offs
531where_ wh (TableExpression tabs whs lims offs) = TableExpression tabs (wh:whs) lims offs