@@ -34,9 +34,7 @@ Squeal expressions are the atoms used to build statements.
3434module Squeal.PostgreSQL.Expression
3535 ( -- * Expression
3636 Expression (..)
37− , ClosedExpression
3837 , Operator
39− , ComparisonOperator
4038 , HasParameter (parameter)
4139 , param
4240 -- ** Null
@@ -289,19 +287,12 @@ newtype Expression
289287instance RenderSQL (Expression outer grp commons schemas params from ty) where
290288 renderSQL = renderExpression
291289
292−type ClosedExpression ty
293− = forall outer grp commons schemas params from
294− . Expression outer grp commons schemas params from ty
295−
296290type Operator ty1 ty2 ty3
297291 = forall outer grp commons schemas params from
298292 . Expression outer grp commons schemas params from ty1
299293 -> Expression outer grp commons schemas params from ty2
300294 -> Expression outer grp commons schemas params from ty3
301295
302−type ComparisonOperator ty1 ty2
303− = Operator ty1 ty2 ('Null 'PGbool)
304−
305296{- | A `HasParameter` constraint is used to indicate a value that is
306297supplied externally to a SQL statement.
307298`Squeal.PostgreSQL.PQ.manipulateParams`,
@@ -635,9 +626,7 @@ least x xs = UnsafeExpression $ "LEAST("
635626unsafeBinaryOp
636627 :: ByteString
637628 -- ^ operator
638− -> Expression outer grp commons schemas params from (ty0)
639− -> Expression outer grp commons schemas params from (ty1)
640− -> Expression outer grp commons schemas params from (ty2)
629+ -> Operator ty0 ty1 ty2
641630unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
642631 renderSQL x <+> op <+> renderSQL y
643632
@@ -767,11 +756,7 @@ cast ty x = UnsafeExpression $ parenthesized $
767756-- (5 / 2)
768757quot_
769758 :: int `In` PGIntegral
770− => Expression outer grp commons schemas params from (nullity int)
771− -- ^ numerator
772− -> Expression outer grp commons schemas params from (nullity int)
773− -- ^ denominator
774− -> Expression outer grp commons schemas params from (nullity int)
759+ => Operator (nullity int) (nullity int) (nullity int)
775760quot_ = unsafeBinaryOp "/"
776761
777762-- | remainder upon integer division
@@ -785,11 +770,7 @@ quot_ = unsafeBinaryOp "/"
785770-- (5 % 2)
786771rem_
787772 :: int `In` PGIntegral
788− => Expression outer grp commons schemas params from (nullity int)
789− -- ^ numerator
790− -> Expression outer grp commons schemas params from (nullity int)
791− -- ^ denominator
792− -> Expression outer grp commons schemas params from (nullity int)
773+ => Operator (nullity int) (nullity int) (nullity int)
793774rem_ = unsafeBinaryOp "%"
794775
795776-- | >>> :{
@@ -859,19 +840,13 @@ not_ = unsafeUnaryOp "NOT"
859840
860841-- | >>> printSQL $ true .&& false
861842-- (TRUE AND FALSE)
862−(.&&)
863− :: Expression outer grp commons schemas params from (nullity 'PGbool)
864− -> Expression outer grp commons schemas params from (nullity 'PGbool)
865− -> Expression outer grp commons schemas params from (nullity 'PGbool)
843+(.&&) :: Operator (nullity 'PGbool) (nullity 'PGbool) (nullity 'PGbool)
866844infixr 3 .&&
867845(.&&) = unsafeBinaryOp "AND"
868846
869847-- | >>> printSQL $ true .|| false
870848-- (TRUE OR FALSE)
871−(.||)
872− :: Expression outer grp commons schemas params from (nullity 'PGbool)
873− -> Expression outer grp commons schemas params from (nullity 'PGbool)
874− -> Expression outer grp commons schemas params from (nullity 'PGbool)
849+(.||) :: Operator (nullity 'PGbool) (nullity 'PGbool) (nullity 'PGbool)
875850infixr 2 .||
876851(.||) = unsafeBinaryOp "OR"
877852
@@ -922,55 +897,37 @@ ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
922897--
923898-- >>> printSQL $ true .== null_
924899-- (TRUE = NULL)
925−(.==)
926− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
927− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
928− -> Condition outer grp commons schemas params from
900+(.==) :: Operator (nullity0 ty) (nullity1 ty) ('Null 'PGbool)
929901(.==) = unsafeBinaryOp "="
930902infix 4 .==
931903
932904-- | >>> printSQL $ true ./= null_
933905-- (TRUE <> NULL)
934−(./=)
935− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
936− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
937− -> Condition outer grp commons schemas params from
906+(./=) :: Operator (nullity0 ty) (nullity1 ty) ('Null 'PGbool)
938907(./=) = unsafeBinaryOp "<>"
939908infix 4 ./=
940909
941910-- | >>> printSQL $ true .>= null_
942911-- (TRUE >= NULL)
943−(.>=)
944− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
945− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
946− -> Condition outer grp commons schemas params from
912+(.>=) :: Operator (nullity0 ty) (nullity1 ty) ('Null 'PGbool)
947913(.>=) = unsafeBinaryOp ">="
948914infix 4 .>=
949915
950916-- | >>> printSQL $ true .< null_
951917-- (TRUE < NULL)
952−(.<)
953− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
954− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
955− -> Condition outer grp commons schemas params from
918+(.<) :: Operator (nullity0 ty) (nullity1 ty) ('Null 'PGbool)
956919(.<) = unsafeBinaryOp "<"
957920infix 4 .<
958921
959922-- | >>> printSQL $ true .<= null_
960923-- (TRUE <= NULL)
961−(.<=)
962− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
963− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
964− -> Condition outer grp commons schemas params from
924+(.<=) :: Operator (nullity0 ty) (nullity1 ty) ('Null 'PGbool)
965925(.<=) = unsafeBinaryOp "<="
966926infix 4 .<=
967927
968928-- | >>> printSQL $ true .> null_
969929-- (TRUE > NULL)
970−(.>)
971− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
972− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
973− -> Condition outer grp commons schemas params from
930+(.>) :: Operator (nullity0 ty) (nullity1 ty) ('Null 'PGbool)
974931(.>) = unsafeBinaryOp ">"
975932infix 4 .>
976933
@@ -1026,20 +983,14 @@ notBetweenSymmetric a (x,y) = UnsafeExpression $ renderSQL a
1026983>>> printSQL $ true `isDistinctFrom` null_
1027984(TRUE IS DISTINCT FROM NULL)
1028985-}
1029−isDistinctFrom
1030− :: Expression outer grp commons schemas params from (nullity ty)
1031− -> Expression outer grp commons schemas params from (nullity ty)
1032− -> Condition outer grp commons schemas params from
986+isDistinctFrom :: Operator (nullity0 ty) (nullity1 ty) ('Null 'PGbool)
1033987isDistinctFrom = unsafeBinaryOp "IS DISTINCT FROM"
1034988
1035989{- | equal, treating null like an ordinary value
1036990>>> printSQL $ true `isNotDistinctFrom` null_
1037991(TRUE IS NOT DISTINCT FROM NULL)
1038992-}
1039−isNotDistinctFrom
1040− :: Expression outer grp commons schemas params from (nullity ty)
1041− -> Expression outer grp commons schemas params from (nullity ty)
1042− -> Condition outer grp commons schemas params from
993+isNotDistinctFrom :: Operator (nullity0 ty) (nullity1 ty) ('Null 'PGbool)
1043994isNotDistinctFrom = unsafeBinaryOp "IS NOT DISTINCT FROM"
1044995
1045996{- | is true
@@ -1204,12 +1155,7 @@ charLength = unsafeFunction "char_length"
12041155--
12051156-- >>> printSQL $ "abc" `like` "a%"
12061157-- (E'abc' LIKE E'a%')
1207−like
1208− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1209− -- ^ string
1210− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1211− -- ^ pattern
1212− -> Expression outer grp commons schemas params from (nullity 'PGbool)
1158+like :: Operator (nullity 'PGtext) (nullity 'PGtext) ('Null 'PGbool)
12131159like = unsafeBinaryOp "LIKE"
12141160
12151161-- | The key word ILIKE can be used instead of LIKE to make the
@@ -1217,12 +1163,7 @@ like = unsafeBinaryOp "LIKE"
12171163--
12181164-- >>> printSQL $ "abc" `ilike` "a%"
12191165-- (E'abc' ILIKE E'a%')
1220−ilike
1221− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1222− -- ^ string
1223− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1224− -- ^ pattern
1225− -> Expression outer grp commons schemas params from (nullity 'PGbool)
1166+ilike :: Operator (nullity 'PGtext) (nullity 'PGtext) ('Null 'PGbool)
12261167ilike = unsafeBinaryOp "ILIKE"
12271168
12281169{-----------------------------------------
@@ -1237,36 +1178,28 @@ Table 9.44: json and jsonb operators
12371178-- | Get JSON value (object field or array element) at a key.
12381179(.->)
12391180 :: (json `In` PGJsonType, key `In` PGJsonKey)
1240− => Expression outer grp commons schemas params from (nullity json)
1241− -> Expression outer grp commons schemas params from (nullity key)
1242− -> Expression outer grp commons schemas params from ('Null json)
1181+ => Operator (nullity json) (nullity key) ('Null json)
12431182infixl 8 .->
12441183(.->) = unsafeBinaryOp "->"
12451184
12461185-- | Get JSON value (object field or array element) at a key, as text.
12471186(.->>)
12481187 :: (json `In` PGJsonType, key `In` PGJsonKey)
1249− => Expression outer grp commons schemas params from (nullity json)
1250− -> Expression outer grp commons schemas params from (nullity key)
1251− -> Expression outer grp commons schemas params from ('Null 'PGtext)
1188+ => Operator (nullity json) (nullity key) ('Null 'PGtext)
12521189infixl 8 .->>
12531190(.->>) = unsafeBinaryOp "->>"
12541191
12551192-- | Get JSON value at a specified path.
12561193(.#>)
12571194 :: (json `In` PGJsonType, PGTextArray "(.#>)" path)
1258− => Expression outer grp commons schemas params from (nullity json)
1259− -> Expression outer grp commons schemas params from (nullity path)
1260− -> Expression outer grp commons schemas params from ('Null json)
1195+ => Operator (nullity json) (nullity path) ('Null json)
12611196infixl 8 .#>
12621197(.#>) = unsafeBinaryOp "#>"
12631198
12641199-- | Get JSON value at a specified path as text.
12651200(.#>>)
12661201 :: (json `In` PGJsonType, PGTextArray "(.#>>)" path)
1267− => Expression outer grp commons schemas params from (nullity json)
1268− -> Expression outer grp commons schemas params from (nullity path)
1269− -> Expression outer grp commons schemas params from ('Null 'PGtext)
1202+ => Operator (nullity json) (nullity path) ('Null 'PGtext)
12701203infixl 8 .#>>
12711204(.#>>) = unsafeBinaryOp "#>>"
12721205
@@ -1275,42 +1208,34 @@ infixl 8 .#>>
12751208-- | Does the left JSON value contain the right JSON path/value entries at the
12761209-- top level?
12771210(.@>)
1278− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1279− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1280− -> Condition outer grp commons schemas params from
1211+ :: Operator (nullity 'PGjsonb) (nullity 'PGjsonb) ('Null 'PGbool)
12811212infixl 9 .@>
12821213(.@>) = unsafeBinaryOp "@>"
12831214
12841215-- | Are the left JSON path/value entries contained at the top level within the
12851216-- right JSON value?
1286−(.<@)
1287− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1288− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1289− -> Condition outer grp commons schemas params from
1217+(.<@) :: Operator (nullity 'PGjsonb) (nullity 'PGjsonb) ('Null 'PGbool)
12901218infixl 9 .<@
12911219(.<@) = unsafeBinaryOp "<@"
12921220
12931221-- | Does the string exist as a top-level key within the JSON value?
1294−(.?)
1295− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1296− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1297− -> Condition outer grp commons schemas params from
1222+(.?) :: Operator (nullity 'PGjsonb) (nullity 'PGtext) ('Null 'PGbool)
12981223infixl 9 .?
12991224(.?) = unsafeBinaryOp "?"
13001225
13011226-- | Do any of these array strings exist as top-level keys?
1302−(.?|)
1303− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1304− -> Expression outer grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1305− -> Condition outer grp commons schemas params from
1227+(.?|) :: Operator
1228+ (nullity 'PGjsonb)
1229+ (nullity ('PGvararray ('NotNull 'PGtext)))
1230+ ('Null 'PGbool)
13061231infixl 9 .?|
13071232(.?|) = unsafeBinaryOp "?|"
13081233
13091234-- | Do all of these array strings exist as top-level keys?
1310−(.?&)
1311− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1312− -> Expression outer grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1313− -> Condition outer grp commons schemas params from
1235+(.?&) :: Operator
1236+ (nullity 'PGjsonb)
1237+ (nullity ('PGvararray ('NotNull 'PGtext)))
1238+ ('Null 'PGbool)
13141239infixl 9 .?&
13151240(.?&) = unsafeBinaryOp "?&"
13161241
@@ -1333,9 +1258,7 @@ instance Semigroup
13331258-- count from the end). Throws an error if top level container is not an array.
13341259(.-.)
13351260 :: (key `In` '[ 'PGtext, 'PGvararray ('NotNull 'PGtext), 'PGint4, 'PGint2 ]) -- hlint error without parens here
1336− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1337− -> Expression outer grp commons schemas params from (nullity key)
1338− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1261+ => Operator (nullity 'PGjsonb) (nullity key) (nullity 'PGjsonb)
13391262infixl 6 .-.
13401263(.-.) = unsafeBinaryOp "-"
13411264
@@ -1343,9 +1266,7 @@ infixl 6 .-.
13431266-- integers count from the end)
13441267(#-.)
13451268 :: PGTextArray "(#-.)" arrayty
1346− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1347− -> Expression outer grp commons schemas params from (nullity arrayty)
1348− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1269+ => Operator (nullity 'PGjsonb) (nullity arrayty)(nullity 'PGjsonb)
13491270infixl 6 #-.
13501271(#-.) = unsafeBinaryOp "#-"
13511272
@@ -1627,7 +1548,7 @@ jsonbPretty
16271548 -> Expression outer grp commons schemas params from (nullity 'PGtext)
16281549jsonbPretty = unsafeFunction "jsonb_pretty"
16291550
1630−(@@) :: ComparisonOperator (nullity 'PGtsvector) (nullity 'PGtsquery)
1551+(@@) :: Operator (nullity 'PGtsvector) (nullity 'PGtsquery) ('Null 'PGbool)
16311552(@@) = unsafeBinaryOp "@@"
16321553
16331554(.&) :: Operator (nullity 'PGtsquery) (nullity 'PGtsquery) (nullity 'PGtsquery)
@@ -2653,37 +2574,25 @@ class TimeOp time diff | time -> diff where
26532574 >>> printSQL (makeDate 1984 7 3 !+ 365)
26542575 (make_date(1984, 7, 3) + 365)
26552576 -}
2656− (!+)
2657− :: Expression outer grp commons schemas params from (nullity time)
2658− -> Expression outer grp commons schemas params from (nullity diff)
2659− -> Expression outer grp commons schemas params from (nullity time)
2577+ (!+) :: Operator (nullity time) (nullity diff) (nullity time)
26602578 (!+) = unsafeBinaryOp "+"
26612579 {-|
26622580 >>> printSQL (365 +! makeDate 1984 7 3)
26632581 (365 + make_date(1984, 7, 3))
26642582 -}
2665− (+!)
2666− :: Expression outer grp commons schemas params from (nullity diff)
2667− -> Expression outer grp commons schemas params from (nullity time)
2668− -> Expression outer grp commons schemas params from (nullity time)
2583+ (+!) :: Operator (nullity diff) (nullity time) (nullity time)
26692584 (+!) = unsafeBinaryOp "+"
26702585 {-|
26712586 >>> printSQL (makeDate 1984 7 3 !- 365)
26722587 (make_date(1984, 7, 3) - 365)
26732588 -}
2674− (!-)
2675− :: Expression outer grp commons schemas params from (nullity time)
2676− -> Expression outer grp commons schemas params from (nullity diff)
2677− -> Expression outer grp commons schemas params from (nullity time)
2589+ (!-) :: Operator (nullity time) (nullity diff) (nullity time)
26782590 (!-) = unsafeBinaryOp "-"
26792591 {-|
26802592 >>> printSQL (makeDate 1984 7 3 !-! currentDate)
26812593 (make_date(1984, 7, 3) - CURRENT_DATE)
26822594 -}
2683− (!-!)
2684− :: Expression outer grp commons schemas params from (nullity time)
2685− -> Expression outer grp commons schemas params from (nullity time)
2686− -> Expression outer grp commons schemas params from (nullity diff)
2595+ (!-!) :: Operator (nullity time) (nullity time) (nullity diff)
26872596 (!-!) = unsafeBinaryOp "-"
26882597instance TimeOp 'PGtimestamp 'PGinterval
26892598instance TimeOp 'PGtimestamptz 'PGinterval