@@ -45,7 +45,9 @@ module Squeal.PostgreSQL.Expression
4545 , nullIf
4646 -- ** Collections
4747 , array
48+ , index
4849 , row
50+ , field
4951 -- ** Functions
5052 , unsafeBinaryOp
5153 , unsafeUnaryOp
@@ -61,12 +63,12 @@ module Squeal.PostgreSQL.Expression
6163 , greatest
6264 , least
6365 -- ** Conditions
64− , Condition
6566 , true
6667 , false
6768 , not_
6869 , (.&&)
6970 , (.||)
71+ , Condition
7072 , caseWhenThenElse
7173 , ifThenElse
7274 , (.==)
@@ -86,16 +88,13 @@ module Squeal.PostgreSQL.Expression
8688 , upper
8789 , charLength
8890 , like
89− -- ** json or jsonb operators
90− , PGarray
91− , PGarrayOf
92− , PGjsonKey
93− , PGjson_
91+ -- ** Json
92+ -- *** Json and Jsonb operators
9493 , (.->)
9594 , (.->>)
9695 , (.#>)
9796 , (.#>>)
98− -- *** jsonb only operators
97+ -- *** Jsonb operators
9998 , (.@>)
10099 , (.<@)
101100 , (.?)
@@ -124,8 +123,6 @@ module Squeal.PostgreSQL.Expression
124123 , jsonbExtractPath
125124 , jsonExtractPathAsText
126125 , jsonbExtractPathAsText
127− , jsonObjectKeys
128− , jsonbObjectKeys
129126 , jsonTypeof
130127 , jsonbTypeof
131128 , jsonStripNulls
@@ -146,6 +143,9 @@ module Squeal.PostgreSQL.Expression
146143 -- * Types
147144 , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
148145 , PGTyped (pgtype)
146+ , typedef
147+ , typetable
148+ , typeview
149149 , bool
150150 , int2
151151 , smallint
@@ -191,6 +191,7 @@ import Data.Semigroup hiding (All)
191191import qualified Data.Aeson as JSON
192192import Data.Ratio
193193import Data.String
194+import Data.Word
194195import Generics.SOP hiding (from)
195196import GHC.OverloadedLabels
196197import GHC.TypeLits
@@ -217,14 +218,14 @@ and other operations.
217218-}
218219newtype Expression
219220 (schema :: SchemaType)
220− (relations :: RelationsType)
221+ (from :: FromType)
221222 (grouping :: Grouping)
222223 (params :: [NullityType])
223224 (ty :: NullityType)
224225 = UnsafeExpression { renderExpression :: ByteString }
225226 deriving (GHC.Generic,Show,Eq,Ord,NFData)
226227
227−instance RenderSQL (Expression schema relations grouping params ty) where
228+instance RenderSQL (Expression schema from grouping params ty) where
228229 renderSQL = renderExpression
229230
230231{- | A `HasParameter` constraint is used to indicate a value that is
@@ -247,10 +248,10 @@ class KnownNat n => HasParameter
247248 -- >>> printSQL expr
248249 -- ($1 :: int4)
249250 parameter
250− :: TypeExpression schema (PGTypeOf ty)
251− -> Expression schema relations grouping params ty
251+ :: TypeExpression schema ty
252+ -> Expression schema from grouping params ty
252253 parameter ty = UnsafeExpression $ parenthesized $
253− "$" <> renderNat (Proxy @n) <+> "::"
254+ "$" <> renderNat @n <+> "::"
254255 <+> renderTypeExpression ty
255256instance {-# OVERLAPPING #-} HasParameter 1 schema (ty1:tys) ty1
256257instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) schema params ty)
@@ -263,84 +264,84 @@ instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) schema params ty)
263264-- >>> printSQL expr
264265-- ($1 :: int4)
265266param
266− :: forall n schema params relations grouping ty
267− . (PGTyped schema (PGTypeOf ty), HasParameter n schema params ty)
268− => Expression schema relations grouping params ty -- ^ param
267+ :: forall n schema params from grouping ty
268+ . (PGTyped schema ty, HasParameter n schema params ty)
269+ => Expression schema from grouping params ty -- ^ param
269270param = parameter @n pgtype
270271
271−instance (HasUnique relation relations columns, Has column columns ty)
272− => IsLabel column (Expression schema relations 'Ungrouped params ty) where
272+instance (HasUnique table from columns, Has column columns ty)
273+ => IsLabel column (Expression schema from 'Ungrouped params ty) where
273274 fromLabel = UnsafeExpression $ renderAlias (Alias @column)
274−instance (HasUnique relation relations columns, Has column columns ty)
275+instance (HasUnique table from columns, Has column columns ty)
275276 => IsLabel column
276− (Aliased (Expression schema relations 'Ungrouped params) (column ::: ty)) where
277+ (Aliased (Expression schema from 'Ungrouped params) (column ::: ty)) where
277278 fromLabel = fromLabel @column `As` Alias @column
278−instance (HasUnique relation relations columns, Has column columns ty)
279+instance (HasUnique table from columns, Has column columns ty)
279280 => IsLabel column
280− (NP (Aliased (Expression schema relations 'Ungrouped params)) '[column ::: ty]) where
281+ (NP (Aliased (Expression schema from 'Ungrouped params)) '[column ::: ty]) where
281282 fromLabel = fromLabel @column :* Nil
282283
283−instance (Has relation relations columns, Has column columns ty)
284− => IsQualified relation column (Expression schema relations 'Ungrouped params ty) where
285− relation ! column = UnsafeExpression $
286− renderAlias relation <> "." <> renderAlias column
287−instance (Has relation relations columns, Has column columns ty)
288− => IsQualified relation column
289− (Aliased (Expression schema relations 'Ungrouped params) (column ::: ty)) where
290− relation ! column = relation ! column `As` column
291−instance (Has relation relations columns, Has column columns ty)
292− => IsQualified relation column
293− (NP (Aliased (Expression schema relations 'Ungrouped params)) '[column ::: ty]) where
294− relation ! column = relation ! column :* Nil
284+instance (Has table from columns, Has column columns ty)
285+ => IsQualified table column (Expression schema from 'Ungrouped params ty) where
286+ table ! column = UnsafeExpression $
287+ renderAlias table <> "." <> renderAlias column
288+instance (Has table from columns, Has column columns ty)
289+ => IsQualified table column
290+ (Aliased (Expression schema from 'Ungrouped params) (column ::: ty)) where
291+ table ! column = table ! column `As` column
292+instance (Has table from columns, Has column columns ty)
293+ => IsQualified table column
294+ (NP (Aliased (Expression schema from 'Ungrouped params)) '[column ::: ty]) where
295+ table ! column = table ! column :* Nil
295296
296297instance
297− ( HasUnique relation relations columns
298+ ( HasUnique table from columns
298299 , Has column columns ty
299− , GroupedBy relation column bys
300+ , GroupedBy table column bys
300301 ) => IsLabel column
301− (Expression schema relations ('Grouped bys) params ty) where
302+ (Expression schema from ('Grouped bys) params ty) where
302303 fromLabel = UnsafeExpression $ renderAlias (Alias @column)
303304instance
304− ( HasUnique relation relations columns
305+ ( HasUnique table from columns
305306 , Has column columns ty
306− , GroupedBy relation column bys
307+ , GroupedBy table column bys
307308 ) => IsLabel column
308− ( Aliased (Expression schema relations ('Grouped bys) params)
309+ ( Aliased (Expression schema from ('Grouped bys) params)
309310 (column ::: ty) ) where
310311 fromLabel = fromLabel @column `As` Alias @column
311312instance
312− ( HasUnique relation relations columns
313+ ( HasUnique table from columns
313314 , Has column columns ty
314− , GroupedBy relation column bys
315+ , GroupedBy table column bys
315316 ) => IsLabel column
316− ( NP (Aliased (Expression schema relations ('Grouped bys) params))
317+ ( NP (Aliased (Expression schema from ('Grouped bys) params))
317318 '[column ::: ty] ) where
318319 fromLabel = fromLabel @column :* Nil
319320
320321instance
321− ( Has relation relations columns
322+ ( Has table from columns
322323 , Has column columns ty
323− , GroupedBy relation column bys
324− ) => IsQualified relation column
325− (Expression schema relations ('Grouped bys) params ty) where
326− relation ! column = UnsafeExpression $
327− renderAlias relation <> "." <> renderAlias column
324+ , GroupedBy table column bys
325+ ) => IsQualified table column
326+ (Expression schema from ('Grouped bys) params ty) where
327+ table ! column = UnsafeExpression $
328+ renderAlias table <> "." <> renderAlias column
328329instance
329− ( Has relation relations columns
330+ ( Has table from columns
330331 , Has column columns ty
331− , GroupedBy relation column bys
332− ) => IsQualified relation column
333− (Aliased (Expression schema relations ('Grouped bys) params)
332+ , GroupedBy table column bys
333+ ) => IsQualified table column
334+ (Aliased (Expression schema from ('Grouped bys) params)
334335 (column ::: ty)) where
335− relation ! column = relation ! column `As` column
336+ table ! column = table ! column `As` column
336337instance
337− ( Has relation relations columns
338+ ( Has table from columns
338339 , Has column columns ty
339− , GroupedBy relation column bys
340− ) => IsQualified relation column
341− ( NP (Aliased (Expression schema relations ('Grouped bys) params))
340+ , GroupedBy table column bys
341+ ) => IsQualified table column
342+ ( NP (Aliased (Expression schema from ('Grouped bys) params))
342343 '[column ::: ty]) where
343− relation ! column = relation ! column :* Nil
344+ table ! column = table ! column :* Nil
344345
345346-- | analagous to `Nothing`
346347--
@@ -360,14 +361,14 @@ notNull = UnsafeExpression . renderExpression
360361
361362-- | return the leftmost value which is not NULL
362363--
363−-- >>> printSQL $ coalesce [null_, notNull true] false
364+-- >>> printSQL $ coalesce [null_, true] false
364365-- COALESCE(NULL, TRUE, FALSE)
365366coalesce
366− :: [Expression schema relations grouping params ('Null ty)]
367+ :: [Expression schema from grouping params ('Null ty)]
367368 -- ^ @NULL@s may be present
368− -> Expression schema relations grouping params ('NotNull ty)
369+ -> Expression schema from grouping params ('NotNull ty)
369370 -- ^ @NULL@ is absent
370− -> Expression schema relations grouping params ('NotNull ty)
371+ -> Expression schema from grouping params ('NotNull ty)
371372coalesce nullxs notNullx = UnsafeExpression $
372373 "COALESCE" <> parenthesized (commaSeparated
373374 ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
@@ -377,26 +378,26 @@ coalesce nullxs notNullx = UnsafeExpression $
377378-- >>> printSQL $ fromNull true null_
378379-- COALESCE(NULL, TRUE)
379380fromNull
380− :: Expression schema relations grouping params ('NotNull ty)
381+ :: Expression schema from grouping params ('NotNull ty)
381382 -- ^ what to convert @NULL@ to
382− -> Expression schema relations grouping params ('Null ty)
383− -> Expression schema relations grouping params ('NotNull ty)
383+ -> Expression schema from grouping params ('Null ty)
384+ -> Expression schema from grouping params ('NotNull ty)
384385fromNull notNullx nullx = coalesce [nullx] notNullx
385386
386387-- | >>> printSQL $ null_ & isNull
387388-- NULL IS NULL
388389isNull
389− :: Expression schema relations grouping params ('Null ty)
390+ :: Expression schema from grouping params ('Null ty)
390391 -- ^ possibly @NULL@
391− -> Condition schema relations grouping params
392+ -> Condition schema from grouping params
392393isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
393394
394395-- | >>> printSQL $ null_ & isNotNull
395396-- NULL IS NOT NULL
396397isNotNull
397− :: Expression schema relations grouping params ('Null ty)
398+ :: Expression schema from grouping params ('Null ty)
398399 -- ^ possibly @NULL@
399− -> Condition schema relations grouping params
400+ -> Condition schema from grouping params
400401isNotNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
401402
402403-- | analagous to `maybe` using @IS NULL@
@@ -404,13 +405,13 @@ isNotNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
404405-- >>> printSQL $ matchNull true not_ null_
405406-- CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END
406407matchNull
407− :: Expression schema relations grouping params (nullty)
408+ :: Expression schema from grouping params (nullty)
408409 -- ^ what to convert @NULL@ to
409− -> ( Expression schema relations grouping params ('NotNull ty)
410− -> Expression schema relations grouping params (nullty) )
410+ -> ( Expression schema from grouping params ('NotNull ty)
411+ -> Expression schema from grouping params (nullty) )
411412 -- ^ function to perform when @NULL@ is absent
412− -> Expression schema relations grouping params ('Null ty)
413− -> Expression schema relations grouping params (nullty)
413+ -> Expression schema from grouping params ('Null ty)
414+ -> Expression schema from grouping params (nullty)
414415matchNull y f x = ifThenElse (isNull x) y
415416 (f (UnsafeExpression (renderExpression x)))
416417
@@ -423,55 +424,81 @@ matchNull y f x = ifThenElse (isNull x) y
423424NULL IF (FALSE, ($1 :: bool))
424425-}
425426nullIf
426− :: Expression schema relations grouping params ('NotNull ty)
427+ :: Expression schema from grouping params ('NotNull ty)
427428 -- ^ @NULL@ is absent
428− -> Expression schema relations grouping params ('NotNull ty)
429+ -> Expression schema from grouping params ('NotNull ty)
429430 -- ^ @NULL@ is absent
430− -> Expression schema relations grouping params ('Null ty)
431+ -> Expression schema from grouping params ('Null ty)
431432nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
432433 (renderExpression x <> ", " <> renderExpression y)
433434
434−-- | >>> printSQL $ array [null_, notNull false, notNull true]
435+-- | >>> printSQL $ array [null_, false, true]
435436-- ARRAY[NULL, FALSE, TRUE]
436437array
437− :: [Expression schema relations grouping params ('Null ty)]
438+ :: [Expression schema from grouping params ty]
438439 -- ^ array elements
439− -> Expression schema relations grouping params (nullity ('PGvararray ty))
440+ -> Expression schema from grouping params (nullity ('PGvararray ty))
440441array xs = UnsafeExpression $
441442 "ARRAY[" <> commaSeparated (renderExpression <$> xs) <> "]"
442443
444+-- | >>> printSQL $ array [null_, false, true] & index 2
445+-- (ARRAY[NULL, FALSE, TRUE])[2]
446+index
447+ :: Word64 -- ^ index
448+ -> Expression schema from grouping params (nullity ('PGvararray ty)) -- ^ array
449+ -> Expression schema from grouping params (NullifyType ty)
450+index n expr = UnsafeExpression $
451+ parenthesized (renderExpression expr) <> "[" <> fromString (show n) <> "]"
452+
443453instance (KnownSymbol label, label `In` labels) => IsPGlabel label
444− (Expression schema relations grouping params (nullity ('PGenum labels))) where
454+ (Expression schema from grouping params (nullity ('PGenum labels))) where
445455 label = UnsafeExpression $ renderLabel (PGlabel @label)
446456
447457-- | A row constructor is an expression that builds a row value
448458-- (also called a composite value) using values for its member fields.
449459--
450−-- >>> type Complex = PGcomposite '["real" ::: 'PGfloat8, "imaginary" ::: 'PGfloat8]
460+-- >>> :{
461+-- type Complex = 'PGcomposite
462+-- '[ "real" ::: 'NotNull 'PGfloat8
463+-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
464+-- :}
465+--
451466-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression '[] '[] 'Ungrouped '[] ('NotNull Complex)
452467-- >>> printSQL i
453468-- ROW(0, 1)
454469row
455− :: SListI (Nulls fields)
456− => NP (Aliased (Expression schema relations grouping params)) (Nulls fields)
470+ :: SListI row
471+ => NP (Aliased (Expression schema from grouping params)) row
457472 -- ^ zero or more expressions for the row field values
458− -> Expression schema relations grouping params (nullity ('PGcomposite fields))
473+ -> Expression schema from grouping params (nullity ('PGcomposite row))
459474row exprs = UnsafeExpression $ "ROW" <> parenthesized
460475 (renderCommaSeparated (\ (expr `As` _) -> renderExpression expr) exprs)
461476
462−instance Has field fields ty => IsLabel field
463− ( Expression schema relation grouping params (nullity ('PGcomposite fields))
464− -> Expression schema relation grouping params ('Null ty) ) where
465− fromLabel expr = UnsafeExpression $
466− parenthesized (renderExpression expr) <> "." <>
467− fromString (symbolVal (Proxy @field))
477+-- | >>> :{
478+-- type Complex = 'PGcomposite
479+-- '[ "real" ::: 'NotNull 'PGfloat8
480+-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
481+-- :}
482+--
483+-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression '["complex" ::: 'Typedef Complex] '[] 'Ungrouped '[] ('NotNull Complex)
484+-- >>> printSQL $ i & field #complex #imaginary
485+-- (ROW(0, 1)::"complex")."imaginary"
486+field
487+ :: (Has tydef schema ('Typedef ('PGcomposite row)), Has field row ty)
488+ => Alias tydef -- ^ row type
489+ -> Alias field -- ^ field name
490+ -> Expression schema from grouping params ('NotNull ('PGcomposite row))
491+ -> Expression schema from grouping params ty
492+field td fld expr = UnsafeExpression $
493+ parenthesized (renderExpression expr <> "::" <> renderAlias td)
494+ <> "." <> renderAlias fld
468495
469496instance Semigroup
470− (Expression schema relations grouping params (nullity ('PGvararray ty))) where
497+ (Expression schema from grouping params (nullity ('PGvararray ty))) where
471498 (<>) = unsafeBinaryOp "||"
472499
473500instance Monoid
474− (Expression schema relations grouping params (nullity ('PGvararray ty))) where
501+ (Expression schema from grouping params (nullity ('PGvararray ty))) where
475502 mempty = array []
476503 mappend = (<>)
477504
@@ -479,22 +506,22 @@ instance Monoid
479506-- >>> printSQL expr
480507-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
481508greatest
482− :: Expression schema relations grouping params (nullty)
509+ :: Expression schema from grouping params (nullty)
483510 -- ^ needs at least 1 argument
484− -> [Expression schema relations grouping params (nullty)]
511+ -> [Expression schema from grouping params (nullty)]
485512 -- ^ or more
486− -> Expression schema relations grouping params (nullty)
513+ -> Expression schema from grouping params (nullty)
487514greatest x xs = UnsafeExpression $ "GREATEST("
488515 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
489516
490517-- | >>> printSQL $ least currentTimestamp [null_]
491518-- LEAST(CURRENT_TIMESTAMP, NULL)
492519least
493− :: Expression schema relations grouping params (nullty)
520+ :: Expression schema from grouping params (nullty)
494521 -- ^ needs at least 1 argument
495− -> [Expression schema relations grouping params (nullty)]
522+ -> [Expression schema from grouping params (nullty)]
496523 -- ^ or more
497− -> Expression schema relations grouping params (nullty)
524+ -> Expression schema from grouping params (nullty)
498525least x xs = UnsafeExpression $ "LEAST("
499526 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
500527
@@ -503,9 +530,9 @@ least x xs = UnsafeExpression $ "LEAST("
503530unsafeBinaryOp
504531 :: ByteString
505532 -- ^ operator
506− -> Expression schema relations grouping params (ty0)
507− -> Expression schema relations grouping params (ty1)
508− -> Expression schema relations grouping params (ty2)
533+ -> Expression schema from grouping params (ty0)
534+ -> Expression schema from grouping params (ty1)
535+ -> Expression schema from grouping params (ty2)
509536unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
510537 renderExpression x <+> op <+> renderExpression y
511538
@@ -514,8 +541,8 @@ unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
514541unsafeUnaryOp
515542 :: ByteString
516543 -- ^ operator
517− -> Expression schema relations grouping params (ty0)
518− -> Expression schema relations grouping params (ty1)
544+ -> Expression schema from grouping params (ty0)
545+ -> Expression schema from grouping params (ty1)
519546unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
520547 op <+> renderExpression x
521548
@@ -524,8 +551,8 @@ unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
524551unsafeFunction
525552 :: ByteString
526553 -- ^ function
527− -> Expression schema relations grouping params (xty)
528− -> Expression schema relations grouping params (yty)
554+ -> Expression schema from grouping params (xty)
555+ -> Expression schema from grouping params (yty)
529556unsafeFunction fun x = UnsafeExpression $
530557 fun <> parenthesized (renderExpression x)
531558
@@ -534,13 +561,13 @@ unsafeVariadicFunction
534561 :: SListI elems
535562 => ByteString
536563 -- ^ function
537− -> NP (Expression schema relations grouping params) elems
538− -> Expression schema relations grouping params ret
564+ -> NP (Expression schema from grouping params) elems
565+ -> Expression schema from grouping params ret
539566unsafeVariadicFunction fun x = UnsafeExpression $
540567 fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderExpression) x)))
541568
542−instance PGNum ty
543− => Num (Expression schema relations grouping params (nullity ty)) where
569+instance ty `In` PGNum
570+ => Num (Expression schema from grouping params (nullity ty)) where
544571 (+) = unsafeBinaryOp "+"
545572 (-) = unsafeBinaryOp "-"
546573 (*) = unsafeBinaryOp "*"
@@ -551,13 +578,13 @@ instance PGNum ty
551578 . fromString
552579 . show
553580
554−instance (PGNum ty, PGFloating ty) => Fractional
555− (Expression schema relations grouping params (nullity ty)) where
581+instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
582+ (Expression schema from grouping params (nullity ty)) where
556583 (/) = unsafeBinaryOp "/"
557584 fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
558585
559−instance (PGNum ty, PGFloating ty) => Floating
560− (Expression schema relations grouping params (nullity ty)) where
586+instance (ty `In` PGNum, ty `In` PGFloating) => Floating
587+ (Expression schema from grouping params (nullity ty)) where
561588 pi = UnsafeExpression "pi()"
562589 exp = unsafeFunction "exp"
563590 log = unsafeFunction "ln"
@@ -580,18 +607,18 @@ instance (PGNum ty, PGFloating ty) => Floating
580607
581608-- | >>> :{
582609-- let
583−-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
610+-- expression :: Expression schema from grouping params (nullity 'PGfloat4)
584611-- expression = atan2_ pi 2
585612-- in printSQL expression
586613-- :}
587614-- atan2(pi(), 2)
588615atan2_
589− :: PGFloating float
590− => Expression schema relations grouping params (nullity float)
616+ :: float `In` PGFloating
617+ => Expression schema from grouping params (nullity float)
591618 -- ^ numerator
592− -> Expression schema relations grouping params (nullity float)
619+ -> Expression schema from grouping params (nullity float)
593620 -- ^ denominator
594− -> Expression schema relations grouping params (nullity float)
621+ -> Expression schema from grouping params (nullity float)
595622atan2_ y x = UnsafeExpression $
596623 "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
597624
@@ -604,9 +631,9 @@ atan2_ y x = UnsafeExpression $
604631cast
605632 :: TypeExpression schema ty1
606633 -- ^ type to cast as
607− -> Expression schema relations grouping params (nullity ty0)
634+ -> Expression schema from grouping params ty0
608635 -- ^ value to convert
609− -> Expression schema relations grouping params (nullity ty1)
636+ -> Expression schema from grouping params ty1
610637cast ty x = UnsafeExpression $ parenthesized $
611638 renderExpression x <+> "::" <+> renderTypeExpression ty
612639
@@ -614,135 +641,136 @@ cast ty x = UnsafeExpression $ parenthesized $
614641--
615642-- >>> :{
616643-- let
617−-- expression :: Expression schema relations grouping params (nullity 'PGint2)
644+-- expression :: Expression schema from grouping params (nullity 'PGint2)
618645-- expression = 5 `quot_` 2
619646-- in printSQL expression
620647-- :}
621648-- (5 / 2)
622649quot_
623− :: PGIntegral int
624− => Expression schema relations grouping params (nullity int)
650+ :: int `In` PGIntegral
651+ => Expression schema from grouping params (nullity int)
625652 -- ^ numerator
626− -> Expression schema relations grouping params (nullity int)
653+ -> Expression schema from grouping params (nullity int)
627654 -- ^ denominator
628− -> Expression schema relations grouping params (nullity int)
655+ -> Expression schema from grouping params (nullity int)
629656quot_ = unsafeBinaryOp "/"
630657
631658-- | remainder upon integer division
632659--
633660-- >>> :{
634661-- let
635−-- expression :: Expression schema relations grouping params (nullity 'PGint2)
662+-- expression :: Expression schema from grouping params (nullity 'PGint2)
636663-- expression = 5 `rem_` 2
637664-- in printSQL expression
638665-- :}
639666-- (5 % 2)
640667rem_
641− :: PGIntegral int
642− => Expression schema relations grouping params (nullity int)
668+ :: int `In` PGIntegral
669+ => Expression schema from grouping params (nullity int)
643670 -- ^ numerator
644− -> Expression schema relations grouping params (nullity int)
671+ -> Expression schema from grouping params (nullity int)
645672 -- ^ denominator
646− -> Expression schema relations grouping params (nullity int)
673+ -> Expression schema from grouping params (nullity int)
647674rem_ = unsafeBinaryOp "%"
648675
649676-- | >>> :{
650677-- let
651−-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
678+-- expression :: Expression schema from grouping params (nullity 'PGfloat4)
652679-- expression = trunc pi
653680-- in printSQL expression
654681-- :}
655682-- trunc(pi())
656683trunc
657− :: PGFloating frac
658− => Expression schema relations grouping params (nullity frac)
684+ :: frac `In` PGFloating
685+ => Expression schema from grouping params (nullity frac)
659686 -- ^ fractional number
660− -> Expression schema relations grouping params (nullity frac)
687+ -> Expression schema from grouping params (nullity frac)
661688trunc = unsafeFunction "trunc"
662689
663690-- | >>> :{
664691-- let
665−-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
692+-- expression :: Expression schema from grouping params (nullity 'PGfloat4)
666693-- expression = round_ pi
667694-- in printSQL expression
668695-- :}
669696-- round(pi())
670697round_
671− :: PGFloating frac
672− => Expression schema relations grouping params (nullity frac)
698+ :: frac `In` PGFloating
699+ => Expression schema from grouping params (nullity frac)
673700 -- ^ fractional number
674− -> Expression schema relations grouping params (nullity frac)
701+ -> Expression schema from grouping params (nullity frac)
675702round_ = unsafeFunction "round"
676703
677704-- | >>> :{
678705-- let
679−-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
706+-- expression :: Expression schema from grouping params (nullity 'PGfloat4)
680707-- expression = ceiling_ pi
681708-- in printSQL expression
682709-- :}
683710-- ceiling(pi())
684711ceiling_
685− :: PGFloating frac
686− => Expression schema relations grouping params (nullity frac)
712+ :: frac `In` PGFloating
713+ => Expression schema from grouping params (nullity frac)
687714 -- ^ fractional number
688− -> Expression schema relations grouping params (nullity frac)
715+ -> Expression schema from grouping params (nullity frac)
689716ceiling_ = unsafeFunction "ceiling"
690717
691−-- | A `Condition` is a boolean valued `Expression`. While SQL allows
692−-- conditions to have @NULL@, Squeal instead chooses to disallow @NULL@,
693−-- forcing one to handle the case of @NULL@ explicitly to produce
694−-- a `Condition`.
695−type Condition schema relations grouping params =
696− Expression schema relations grouping params ('NotNull 'PGbool)
718+-- | A `Condition` is an `Expression`, which can evaluate
719+-- to `true`, `false` or `null_`. This is because SQL uses
720+-- a three valued logic.
721+type Condition schema from grouping params =
722+ Expression schema from grouping params ('Null 'PGbool)
697723
698724-- | >>> printSQL true
699725-- TRUE
700−true :: Condition schema relations grouping params
726+true :: Expression schema from grouping params (nullity 'PGbool)
701727true = UnsafeExpression "TRUE"
702728
703729-- | >>> printSQL false
704730-- FALSE
705−false :: Condition schema relations grouping params
731+false :: Expression schema from grouping params (nullity 'PGbool)
706732false = UnsafeExpression "FALSE"
707733
708734-- | >>> printSQL $ not_ true
709735-- (NOT TRUE)
710736not_
711− :: Condition schema relations grouping params
712− -> Condition schema relations grouping params
737+ :: Expression schema from grouping params (nullity 'PGbool)
738+ -> Expression schema from grouping params (nullity 'PGbool)
713739not_ = unsafeUnaryOp "NOT"
714740
715741-- | >>> printSQL $ true .&& false
716742-- (TRUE AND FALSE)
717743(.&&)
718− :: Condition schema relations grouping params
719− -> Condition schema relations grouping params
720− -> Condition schema relations grouping params
744+ :: Expression schema from grouping params (nullity 'PGbool)
745+ -> Expression schema from grouping params (nullity 'PGbool)
746+ -> Expression schema from grouping params (nullity 'PGbool)
747+infixr 3 .&&
721748(.&&) = unsafeBinaryOp "AND"
722749
723750-- | >>> printSQL $ true .|| false
724751-- (TRUE OR FALSE)
725752(.||)
726− :: Condition schema relations grouping params
727− -> Condition schema relations grouping params
728− -> Condition schema relations grouping params
753+ :: Expression schema from grouping params (nullity 'PGbool)
754+ -> Expression schema from grouping params (nullity 'PGbool)
755+ -> Expression schema from grouping params (nullity 'PGbool)
756+infixr 2 .||
729757(.||) = unsafeBinaryOp "OR"
730758
731759-- | >>> :{
732760-- let
733−-- expression :: Expression schema relations grouping params (nullity 'PGint2)
761+-- expression :: Expression schema from grouping params (nullity 'PGint2)
734762-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
735763-- in printSQL expression
736764-- :}
737765-- CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END
738766caseWhenThenElse
739− :: [ ( Condition schema relations grouping params
740− , Expression schema relations grouping params (ty)
767+ :: [ ( Condition schema from grouping params
768+ , Expression schema from grouping params ty
741769 ) ]
742770 -- ^ whens and thens
743− -> Expression schema relations grouping params (ty)
771+ -> Expression schema from grouping params ty
744772 -- ^ else
745− -> Expression schema relations grouping params (ty)
773+ -> Expression schema from grouping params ty
746774caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
747775 [ "CASE"
748776 , mconcat
@@ -758,103 +786,103 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
758786
759787-- | >>> :{
760788-- let
761−-- expression :: Expression schema relations grouping params (nullity 'PGint2)
789+-- expression :: Expression schema from grouping params (nullity 'PGint2)
762790-- expression = ifThenElse true 1 0
763791-- in printSQL expression
764792-- :}
765793-- CASE WHEN TRUE THEN 1 ELSE 0 END
766794ifThenElse
767− :: Condition schema relations grouping params
768− -> Expression schema relations grouping params (ty) -- ^ then
769− -> Expression schema relations grouping params (ty) -- ^ else
770− -> Expression schema relations grouping params (ty)
795+ :: Condition schema from grouping params
796+ -> Expression schema from grouping params ty -- ^ then
797+ -> Expression schema from grouping params ty -- ^ else
798+ -> Expression schema from grouping params ty
771799ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
772800
773801-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
774802-- will produce @NULL@s if one of their arguments is @NULL@.
775803--
776−-- >>> printSQL $ notNull true .== null_
804+-- >>> printSQL $ true .== null_
777805-- (TRUE = NULL)
778806(.==)
779− :: Expression schema relations grouping params (nullity ty) -- ^ lhs
780− -> Expression schema relations grouping params (nullity ty) -- ^ rhs
781− -> Expression schema relations grouping params (nullity 'PGbool)
807+ :: Expression schema from grouping params (nullity0 ty) -- ^ lhs
808+ -> Expression schema from grouping params (nullity1 ty) -- ^ rhs
809+ -> Condition schema from grouping params
782810(.==) = unsafeBinaryOp "="
783811infix 4 .==
784812
785−-- | >>> printSQL $ notNull true ./= null_
813+-- | >>> printSQL $ true ./= null_
786814-- (TRUE <> NULL)
787815(./=)
788− :: Expression schema relations grouping params (nullity ty) -- ^ lhs
789− -> Expression schema relations grouping params (nullity ty) -- ^ rhs
790− -> Expression schema relations grouping params (nullity 'PGbool)
816+ :: Expression schema from grouping params (nullity0 ty) -- ^ lhs
817+ -> Expression schema from grouping params (nullity1 ty) -- ^ rhs
818+ -> Condition schema from grouping params
791819(./=) = unsafeBinaryOp "<>"
792820infix 4 ./=
793821
794−-- | >>> printSQL $ notNull true .>= null_
822+-- | >>> printSQL $ true .>= null_
795823-- (TRUE >= NULL)
796824(.>=)
797− :: Expression schema relations grouping params (nullity ty) -- ^ lhs
798− -> Expression schema relations grouping params (nullity ty) -- ^ rhs
799− -> Expression schema relations grouping params (nullity 'PGbool)
825+ :: Expression schema from grouping params (nullity0 ty) -- ^ lhs
826+ -> Expression schema from grouping params (nullity1 ty) -- ^ rhs
827+ -> Condition schema from grouping params
800828(.>=) = unsafeBinaryOp ">="
801829infix 4 .>=
802830
803−-- | >>> printSQL $ notNull true .< null_
831+-- | >>> printSQL $ true .< null_
804832-- (TRUE < NULL)
805833(.<)
806− :: Expression schema relations grouping params (nullity ty) -- ^ lhs
807− -> Expression schema relations grouping params (nullity ty) -- ^ rhs
808− -> Expression schema relations grouping params (nullity 'PGbool)
834+ :: Expression schema from grouping params (nullity0 ty) -- ^ lhs
835+ -> Expression schema from grouping params (nullity1 ty) -- ^ rhs
836+ -> Condition schema from grouping params
809837(.<) = unsafeBinaryOp "<"
810838infix 4 .<
811839
812−-- | >>> printSQL $ notNull true .<= null_
840+-- | >>> printSQL $ true .<= null_
813841-- (TRUE <= NULL)
814842(.<=)
815− :: Expression schema relations grouping params (nullity ty) -- ^ lhs
816− -> Expression schema relations grouping params (nullity ty) -- ^ rhs
817− -> Expression schema relations grouping params (nullity 'PGbool)
843+ :: Expression schema from grouping params (nullity0 ty) -- ^ lhs
844+ -> Expression schema from grouping params (nullity1 ty) -- ^ rhs
845+ -> Condition schema from grouping params
818846(.<=) = unsafeBinaryOp "<="
819847infix 4 .<=
820848
821−-- | >>> printSQL $ notNull true .> null_
849+-- | >>> printSQL $ true .> null_
822850-- (TRUE > NULL)
823851(.>)
824− :: Expression schema relations grouping params (nullity ty) -- ^ lhs
825− -> Expression schema relations grouping params (nullity ty) -- ^ rhs
826− -> Expression schema relations grouping params (nullity 'PGbool)
852+ :: Expression schema from grouping params (nullity0 ty) -- ^ lhs
853+ -> Expression schema from grouping params (nullity1 ty) -- ^ rhs
854+ -> Condition schema from grouping params
827855(.>) = unsafeBinaryOp ">"
828856infix 4 .>
829857
830858-- | >>> printSQL currentDate
831859-- CURRENT_DATE
832860currentDate
833− :: Expression schema relations grouping params (nullity 'PGdate)
861+ :: Expression schema from grouping params (nullity 'PGdate)
834862currentDate = UnsafeExpression "CURRENT_DATE"
835863
836864-- | >>> printSQL currentTime
837865-- CURRENT_TIME
838866currentTime
839− :: Expression schema relations grouping params (nullity 'PGtimetz)
867+ :: Expression schema from grouping params (nullity 'PGtimetz)
840868currentTime = UnsafeExpression "CURRENT_TIME"
841869
842870-- | >>> printSQL currentTimestamp
843871-- CURRENT_TIMESTAMP
844872currentTimestamp
845− :: Expression schema relations grouping params (nullity 'PGtimestamptz)
873+ :: Expression schema from grouping params (nullity 'PGtimestamptz)
846874currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
847875
848876-- | >>> printSQL localTime
849877-- LOCALTIME
850878localTime
851− :: Expression schema relations grouping params (nullity 'PGtime)
879+ :: Expression schema from grouping params (nullity 'PGtime)
852880localTime = UnsafeExpression "LOCALTIME"
853881
854882-- | >>> printSQL localTimestamp
855883-- LOCALTIMESTAMP
856884localTimestamp
857− :: Expression schema relations grouping params (nullity 'PGtimestamp)
885+ :: Expression schema from grouping params (nullity 'PGtimestamp)
858886localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
859887
860888{-----------------------------------------
@@ -862,7 +890,7 @@ text
862890-----------------------------------------}
863891
864892instance IsString
865− (Expression schema relations grouping params (nullity 'PGtext)) where
893+ (Expression schema from grouping params (nullity 'PGtext)) where
866894 fromString str = UnsafeExpression $
867895 "E\'" <> fromString (escape =<< str) <> "\'"
868896 where
@@ -878,36 +906,36 @@ instance IsString
878906 c -> [c]
879907
880908instance Semigroup
881− (Expression schema relations grouping params (nullity 'PGtext)) where
909+ (Expression schema from grouping params (nullity 'PGtext)) where
882910 (<>) = unsafeBinaryOp "||"
883911
884912instance Monoid
885− (Expression schema relations grouping params (nullity 'PGtext)) where
913+ (Expression schema from grouping params (nullity 'PGtext)) where
886914 mempty = fromString ""
887915 mappend = (<>)
888916
889917-- | >>> printSQL $ lower "ARRRGGG"
890918-- lower(E'ARRRGGG')
891919lower
892− :: Expression schema relations grouping params (nullity 'PGtext)
920+ :: Expression schema from grouping params (nullity 'PGtext)
893921 -- ^ string to lower case
894− -> Expression schema relations grouping params (nullity 'PGtext)
922+ -> Expression schema from grouping params (nullity 'PGtext)
895923lower = unsafeFunction "lower"
896924
897925-- | >>> printSQL $ upper "eeee"
898926-- upper(E'eeee')
899927upper
900− :: Expression schema relations grouping params (nullity 'PGtext)
928+ :: Expression schema from grouping params (nullity 'PGtext)
901929 -- ^ string to upper case
902− -> Expression schema relations grouping params (nullity 'PGtext)
930+ -> Expression schema from grouping params (nullity 'PGtext)
903931upper = unsafeFunction "upper"
904932
905933-- | >>> printSQL $ charLength "four"
906934-- char_length(E'four')
907935charLength
908− :: Expression schema relations grouping params (nullity 'PGtext)
936+ :: Expression schema from grouping params (nullity 'PGtext)
909937 -- ^ string to measure
910− -> Expression schema relations grouping params (nullity 'PGint4)
938+ -> Expression schema from grouping params (nullity 'PGint4)
911939charLength = unsafeFunction "char_length"
912940
913941-- | The `like` expression returns true if the @string@ matches
@@ -920,11 +948,11 @@ charLength = unsafeFunction "char_length"
920948-- >>> printSQL $ "abc" `like` "a%"
921949-- (E'abc' LIKE E'a%')
922950like
923− :: Expression schema relations grouping params (nullity 'PGtext)
951+ :: Expression schema from grouping params (nullity 'PGtext)
924952 -- ^ string
925− -> Expression schema relations grouping params (nullity 'PGtext)
953+ -> Expression schema from grouping params (nullity 'PGtext)
926954 -- ^ pattern
927− -> Expression schema relations grouping params (nullity 'PGbool)
955+ -> Expression schema from grouping params (nullity 'PGbool)
928956like = unsafeBinaryOp "LIKE"
929957
930958{-----------------------------------------
@@ -938,34 +966,38 @@ Table 9.44: json and jsonb operators
938966
939967-- | Get JSON value (object field or array element) at a key.
940968(.->)
941− :: (PGjson_ json, PGjsonKey key)
942− => Expression schema relations grouping params (nullity json)
943− -> Expression schema relations grouping params (nullity key)
944− -> Expression schema relations grouping params ('Null json)
969+ :: (json `In` PGJsonType, key `In` PGJsonKey)
970+ => Expression schema from grouping params (nullity json)
971+ -> Expression schema from grouping params (nullity key)
972+ -> Expression schema from grouping params ('Null json)
973+infixl 8 .->
945974(.->) = unsafeBinaryOp "->"
946975
947976-- | Get JSON value (object field or array element) at a key, as text.
948977(.->>)
949− :: (PGjson_ json, PGjsonKey key)
950− => Expression schema relations grouping params (nullity json)
951− -> Expression schema relations grouping params (nullity key)
952− -> Expression schema relations grouping params ('Null 'PGtext)
978+ :: (json `In` PGJsonType, key `In` PGJsonKey)
979+ => Expression schema from grouping params (nullity json)
980+ -> Expression schema from grouping params (nullity key)
981+ -> Expression schema from grouping params ('Null 'PGtext)
982+infixl 8 .->>
953983(.->>) = unsafeBinaryOp "->>"
954984
955985-- | Get JSON value at a specified path.
956986(.#>)
957− :: (PGjson_ json, PGtextArray "(.#>)" path)
958− => Expression schema relations grouping params (nullity json)
959− -> Expression schema relations grouping params (nullity path)
960− -> Expression schema relations grouping params ('Null json)
987+ :: (json `In` PGJsonType, PGTextArray "(.#>)" path)
988+ => Expression schema from grouping params (nullity json)
989+ -> Expression schema from grouping params (nullity path)
990+ -> Expression schema from grouping params ('Null json)
991+infixl 8 .#>
961992(.#>) = unsafeBinaryOp "#>"
962993
963994-- | Get JSON value at a specified path as text.
964995(.#>>)
965− :: (PGjson_ json, PGtextArray "(.#>>)" path)
966− => Expression schema relations grouping params (nullity json)
967− -> Expression schema relations grouping params (nullity path)
968− -> Expression schema relations grouping params ('Null 'PGtext)
996+ :: (json `In` PGJsonType, PGTextArray "(.#>>)" path)
997+ => Expression schema from grouping params (nullity json)
998+ -> Expression schema from grouping params (nullity path)
999+ -> Expression schema from grouping params ('Null 'PGtext)
1000+infixl 8 .#>>
9691001(.#>>) = unsafeBinaryOp "#>>"
9701002
9711003-- Additional jsonb operators
@@ -973,43 +1005,48 @@ Table 9.44: json and jsonb operators
9731005-- | Does the left JSON value contain the right JSON path/value entries at the
9741006-- top level?
9751007(.@>)
976− :: Expression schema relations grouping params (nullity 'PGjsonb)
977− -> Expression schema relations grouping params (nullity 'PGjsonb)
978− -> Condition schema relations grouping params
1008+ :: Expression schema from grouping params (nullity 'PGjsonb)
1009+ -> Expression schema from grouping params (nullity 'PGjsonb)
1010+ -> Condition schema from grouping params
1011+infixl 9 .@>
9791012(.@>) = unsafeBinaryOp "@>"
9801013
9811014-- | Are the left JSON path/value entries contained at the top level within the
9821015-- right JSON value?
9831016(.<@)
984− :: Expression schema relations grouping params (nullity 'PGjsonb)
985− -> Expression schema relations grouping params (nullity 'PGjsonb)
986− -> Condition schema relations grouping params
1017+ :: Expression schema from grouping params (nullity 'PGjsonb)
1018+ -> Expression schema from grouping params (nullity 'PGjsonb)
1019+ -> Condition schema from grouping params
1020+infixl 9 .<@
9871021(.<@) = unsafeBinaryOp "<@"
9881022
9891023-- | Does the string exist as a top-level key within the JSON value?
9901024(.?)
991− :: Expression schema relations grouping params (nullity 'PGjsonb)
992− -> Expression schema relations grouping params (nullity 'PGtext)
993− -> Condition schema relations grouping params
1025+ :: Expression schema from grouping params (nullity 'PGjsonb)
1026+ -> Expression schema from grouping params (nullity 'PGtext)
1027+ -> Condition schema from grouping params
1028+infixl 9 .?
9941029(.?) = unsafeBinaryOp "?"
9951030
9961031-- | Do any of these array strings exist as top-level keys?
9971032(.?|)
998− :: Expression schema relations grouping params (nullity 'PGjsonb)
999− -> Expression schema relations grouping params (nullity ('PGvararray 'PGtext))
1000− -> Condition schema relations grouping params
1033+ :: Expression schema from grouping params (nullity 'PGjsonb)
1034+ -> Expression schema from grouping params (nullity ('PGvararray ('NotNull 'PGtext)))
1035+ -> Condition schema from grouping params
1036+infixl 9 .?|
10011037(.?|) = unsafeBinaryOp "?|"
10021038
10031039-- | Do all of these array strings exist as top-level keys?
10041040(.?&)
1005− :: Expression schema relations grouping params (nullity 'PGjsonb)
1006− -> Expression schema relations grouping params (nullity ('PGvararray 'PGtext))
1007− -> Condition schema relations grouping params
1041+ :: Expression schema from grouping params (nullity 'PGjsonb)
1042+ -> Expression schema from grouping params (nullity ('PGvararray ('NotNull 'PGtext)))
1043+ -> Condition schema from grouping params
1044+infixl 9 .?&
10081045(.?&) = unsafeBinaryOp "?&"
10091046
10101047-- | Concatenate two jsonb values into a new jsonb value.
10111048instance
1012− Semigroup (Expression schema relations grouping param (nullity 'PGjsonb)) where
1049+ Semigroup (Expression schema from grouping param (nullity 'PGjsonb)) where
10131050 (<>) = unsafeBinaryOp "||"
10141051
10151052-- | Delete a key or keys from a JSON object, or remove an array element.
@@ -1025,19 +1062,21 @@ instance
10251062-- @ integer @: Delete the array element with specified index (Negative integers
10261063-- count from the end). Throws an error if top level container is not an array.
10271064(.-.)
1028− :: (key `In` '[ 'PGtext, 'PGvararray 'PGtext, 'PGint4, 'PGint2 ]) -- hlint error without parens here
1029− => Expression schema relations grouping params (nullity 'PGjsonb)
1030− -> Expression schema relations grouping params (nullity key)
1031− -> Expression schema relations grouping params (nullity 'PGjsonb)
1065+ :: (key `In` '[ 'PGtext, 'PGvararray ('NotNull 'PGtext), 'PGint4, 'PGint2 ]) -- hlint error without parens here
1066+ => Expression schema from grouping params (nullity 'PGjsonb)
1067+ -> Expression schema from grouping params (nullity key)
1068+ -> Expression schema from grouping params (nullity 'PGjsonb)
1069+infixl 6 .-.
10321070(.-.) = unsafeBinaryOp "-"
10331071
10341072-- | Delete the field or element with specified path (for JSON arrays, negative
10351073-- integers count from the end)
10361074(#-.)
1037− :: PGtextArray "(#-.)" arrayty
1038− => Expression schema relations grouping params (nullity 'PGjsonb)
1039− -> Expression schema relations grouping params (nullity arrayty)
1040− -> Expression schema relations grouping params (nullity 'PGjsonb)
1075+ :: PGTextArray "(#-.)" arrayty
1076+ => Expression schema from grouping params (nullity 'PGjsonb)
1077+ -> Expression schema from grouping params (nullity arrayty)
1078+ -> Expression schema from grouping params (nullity 'PGjsonb)
1079+infixl 6 #-.
10411080(#-.) = unsafeBinaryOp "#-"
10421081
10431082{-----------------------------------------
@@ -1045,12 +1084,18 @@ Table 9.45: JSON creation functions
10451084-----------------------------------------}
10461085
10471086-- | Literal binary JSON
1048−jsonbLit :: JSON.Value -> Expression schema relations grouping params (nullity 'PGjsonb)
1049−jsonbLit = cast jsonb . UnsafeExpression . singleQuotedUtf8 . toStrict . JSON.encode
1087+jsonbLit
1088+ :: JSON.ToJSON x
1089+ => x -> Expression schema from grouping params (nullity 'PGjsonb)
1090+jsonbLit = cast jsonb . UnsafeExpression
1091+ . singleQuotedUtf8 . toStrict . JSON.encode
10501092
10511093-- | Literal JSON
1052−jsonLit :: JSON.Value -> Expression schema relations grouping params (nullity 'PGjson)
1053−jsonLit = cast json . UnsafeExpression . singleQuotedUtf8 . toStrict . JSON.encode
1094+jsonLit
1095+ :: JSON.ToJSON x
1096+ => x -> Expression schema from grouping params (nullity 'PGjson)
1097+jsonLit = cast json . UnsafeExpression
1098+ . singleQuotedUtf8 . toStrict . JSON.encode
10541099
10551100-- | Returns the value as json. Arrays and composites are converted
10561101-- (recursively) to arrays and objects; otherwise, if there is a cast from the
@@ -1059,8 +1104,8 @@ jsonLit = cast json . UnsafeExpression . singleQuotedUtf8 . toStrict . JSON.enco
10591104-- number, a Boolean, or a null value, the text representation will be used, in
10601105-- such a fashion that it is a valid json value.
10611106toJson
1062− :: Expression schema relations grouping params (nullity ty)
1063− -> Expression schema relations grouping params (nullity 'PGjson)
1107+ :: Expression schema from grouping params (nullity ty)
1108+ -> Expression schema from grouping params (nullity 'PGjson)
10641109toJson = unsafeFunction "to_json"
10651110
10661111-- | Returns the value as jsonb. Arrays and composites are converted
@@ -1070,43 +1115,43 @@ toJson = unsafeFunction "to_json"
10701115-- number, a Boolean, or a null value, the text representation will be used, in
10711116-- such a fashion that it is a valid jsonb value.
10721117toJsonb
1073− :: Expression schema relations grouping params (nullity ty)
1074− -> Expression schema relations grouping params (nullity 'PGjsonb)
1118+ :: Expression schema from grouping params (nullity ty)
1119+ -> Expression schema from grouping params (nullity 'PGjsonb)
10751120toJsonb = unsafeFunction "to_jsonb"
10761121
10771122-- | Returns the array as a JSON array. A PostgreSQL multidimensional array
10781123-- becomes a JSON array of arrays.
10791124arrayToJson
1080− :: PGarray "arrayToJson" arr
1081− => Expression schema relations grouping params (nullity arr)
1082− -> Expression schema relations grouping params (nullity 'PGjson)
1125+ :: PGArray "arrayToJson" arr
1126+ => Expression schema from grouping params (nullity arr)
1127+ -> Expression schema from grouping params (nullity 'PGjson)
10831128arrayToJson = unsafeFunction "array_to_json"
10841129
10851130-- | Returns the row as a JSON object.
10861131rowToJson
1087− :: Expression schema relations grouping params (nullity ('PGcomposite ty))
1088− -> Expression schema relations grouping params (nullity 'PGjson)
1132+ :: Expression schema from grouping params (nullity ('PGcomposite ty))
1133+ -> Expression schema from grouping params (nullity 'PGjson)
10891134rowToJson = unsafeFunction "row_to_json"
10901135
10911136-- | Builds a possibly-heterogeneously-typed JSON array out of a variadic
10921137-- argument list.
10931138jsonBuildArray
10941139 :: SListI elems
1095− => NP (Expression schema relations grouping params) elems
1096− -> Expression schema relations grouping params (nullity 'PGjson)
1140+ => NP (Expression schema from grouping params) elems
1141+ -> Expression schema from grouping params (nullity 'PGjson)
10971142jsonBuildArray = unsafeVariadicFunction "json_build_array"
10981143
10991144-- | Builds a possibly-heterogeneously-typed (binary) JSON array out of a
11001145-- variadic argument list.
11011146jsonbBuildArray
11021147 :: SListI elems
1103− => NP (Expression schema relations grouping params) elems
1104− -> Expression schema relations grouping params (nullity 'PGjsonb)
1148+ => NP (Expression schema from grouping params) elems
1149+ -> Expression schema from grouping params (nullity 'PGjsonb)
11051150jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
11061151
11071152unsafeRowFunction
11081153 :: All Top elems
1109− => NP (Aliased (Expression schema relations grouping params)) elems
1154+ => NP (Aliased (Expression schema from grouping params)) elems
11101155 -> [ByteString]
11111156unsafeRowFunction =
11121157 (`appEndo` []) . hcfoldMap (Proxy :: Proxy Top)
@@ -1118,8 +1163,8 @@ unsafeRowFunction =
11181163-- and values.
11191164jsonBuildObject
11201165 :: All Top elems
1121− => NP (Aliased (Expression schema relations grouping params)) elems
1122− -> Expression schema relations grouping params (nullity 'PGjson)
1166+ => NP (Aliased (Expression schema from grouping params)) elems
1167+ -> Expression schema from grouping params (nullity 'PGjson)
11231168jsonBuildObject
11241169 = unsafeFunction "json_build_object"
11251170 . UnsafeExpression
@@ -1131,8 +1176,8 @@ jsonBuildObject
11311176-- between text and values.
11321177jsonbBuildObject
11331178 :: All Top elems
1134− => NP (Aliased (Expression schema relations grouping params)) elems
1135− -> Expression schema relations grouping params (nullity 'PGjsonb)
1179+ => NP (Aliased (Expression schema from grouping params)) elems
1180+ -> Expression schema from grouping params (nullity 'PGjsonb)
11361181jsonbBuildObject
11371182 = unsafeFunction "jsonb_build_object"
11381183 . UnsafeExpression
@@ -1144,9 +1189,9 @@ jsonbBuildObject
11441189-- taken as alternating key/value pairs, or two dimensions such that each inner
11451190-- array has exactly two elements, which are taken as a key/value pair.
11461191jsonObject
1147− :: PGarrayOf "jsonObject" arr 'PGtext
1148− => Expression schema relations grouping params (nullity arr)
1149− -> Expression schema relations grouping params (nullity 'PGjson)
1192+ :: PGArrayOf "jsonObject" arr ('NotNull 'PGtext)
1193+ => Expression schema from grouping params (nullity arr)
1194+ -> Expression schema from grouping params (nullity 'PGjson)
11501195jsonObject = unsafeFunction "json_object"
11511196
11521197-- | Builds a binary JSON object out of a text array. The array must have either
@@ -1154,19 +1199,19 @@ jsonObject = unsafeFunction "json_object"
11541199-- taken as alternating key/value pairs, or two dimensions such that each inner
11551200-- array has exactly two elements, which are taken as a key/value pair.
11561201jsonbObject
1157− :: PGarrayOf "jsonbObject" arr 'PGtext
1158− => Expression schema relations grouping params (nullity arr)
1159− -> Expression schema relations grouping params (nullity 'PGjsonb)
1202+ :: PGArrayOf "jsonbObject" arr ('NotNull 'PGtext)
1203+ => Expression schema from grouping params (nullity arr)
1204+ -> Expression schema from grouping params (nullity 'PGjsonb)
11601205jsonbObject = unsafeFunction "jsonb_object"
11611206
11621207-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
11631208-- keys and one for values, that are zipped pairwise to create a JSON object.
11641209jsonZipObject
1165− :: ( PGarrayOf "jsonZipObject" keysArray 'PGtext
1166− , PGarrayOf "jsonZipObject" valuesArray 'PGtext)
1167− => Expression schema relations grouping params (nullity keysArray)
1168− -> Expression schema relations grouping params (nullity valuesArray)
1169− -> Expression schema relations grouping params (nullity 'PGjson)
1210+ :: ( PGArrayOf "jsonZipObject" keysArray ('NotNull 'PGtext)
1211+ , PGArrayOf "jsonZipObject" valuesArray ('NotNull 'PGtext))
1212+ => Expression schema from grouping params (nullity keysArray)
1213+ -> Expression schema from grouping params (nullity valuesArray)
1214+ -> Expression schema from grouping params (nullity 'PGjson)
11701215jsonZipObject ks vs =
11711216 unsafeVariadicFunction "json_object" (ks :* vs :* Nil)
11721217
@@ -1174,11 +1219,11 @@ jsonZipObject ks vs =
11741219-- keys and one for values, that are zipped pairwise to create a binary JSON
11751220-- object.
11761221jsonbZipObject
1177− :: ( PGarrayOf "jsonbZipObject" keysArray 'PGtext
1178− , PGarrayOf "jsonbZipObject" valuesArray 'PGtext)
1179− => Expression schema relations grouping params (nullity keysArray)
1180− -> Expression schema relations grouping params (nullity valuesArray)
1181− -> Expression schema relations grouping params (nullity 'PGjsonb)
1222+ :: ( PGArrayOf "jsonbZipObject" keysArray ('NotNull 'PGtext)
1223+ , PGArrayOf "jsonbZipObject" valuesArray ('NotNull 'PGtext))
1224+ => Expression schema from grouping params (nullity keysArray)
1225+ -> Expression schema from grouping params (nullity valuesArray)
1226+ -> Expression schema from grouping params (nullity 'PGjsonb)
11821227jsonbZipObject ks vs =
11831228 unsafeVariadicFunction "jsonb_object" (ks :* vs :* Nil)
11841229
@@ -1188,23 +1233,23 @@ Table 9.46: JSON processing functions
11881233
11891234-- | Returns the number of elements in the outermost JSON array.
11901235jsonArrayLength
1191− :: Expression schema relations grouping params (nullity 'PGjson)
1192− -> Expression schema relations grouping params (nullity 'PGint4)
1236+ :: Expression schema from grouping params (nullity 'PGjson)
1237+ -> Expression schema from grouping params (nullity 'PGint4)
11931238jsonArrayLength = unsafeFunction "json_array_length"
11941239
11951240-- | Returns the number of elements in the outermost binary JSON array.
11961241jsonbArrayLength
1197− :: Expression schema relations grouping params (nullity 'PGjsonb)
1198− -> Expression schema relations grouping params (nullity 'PGint4)
1242+ :: Expression schema from grouping params (nullity 'PGjsonb)
1243+ -> Expression schema from grouping params (nullity 'PGint4)
11991244jsonbArrayLength = unsafeFunction "jsonb_array_length"
12001245
12011246-- | Returns JSON value pointed to by the given path (equivalent to #>
12021247-- operator).
12031248jsonExtractPath
12041249 :: SListI elems
1205− => Expression schema relations grouping params (nullity 'PGjson)
1206− -> NP (Expression schema relations grouping params) elems
1207− -> Expression schema relations grouping params (nullity 'PGjsonb)
1250+ => Expression schema from grouping params (nullity 'PGjson)
1251+ -> NP (Expression schema from grouping params) elems
1252+ -> Expression schema from grouping params (nullity 'PGjsonb)
12081253jsonExtractPath x xs =
12091254 unsafeVariadicFunction "json_extract_path" (x :* xs)
12101255
@@ -1212,9 +1257,9 @@ jsonExtractPath x xs =
12121257-- operator).
12131258jsonbExtractPath
12141259 :: SListI elems
1215− => Expression schema relations grouping params (nullity 'PGjsonb)
1216− -> NP (Expression schema relations grouping params) elems
1217− -> Expression schema relations grouping params (nullity 'PGjsonb)
1260+ => Expression schema from grouping params (nullity 'PGjsonb)
1261+ -> NP (Expression schema from grouping params) elems
1262+ -> Expression schema from grouping params (nullity 'PGjsonb)
12181263jsonbExtractPath x xs =
12191264 unsafeVariadicFunction "jsonb_extract_path" (x :* xs)
12201265
@@ -1222,9 +1267,9 @@ jsonbExtractPath x xs =
12221267-- operator), as text.
12231268jsonExtractPathAsText
12241269 :: SListI elems
1225− => Expression schema relations grouping params (nullity 'PGjson)
1226− -> NP (Expression schema relations grouping params) elems
1227− -> Expression schema relations grouping params (nullity 'PGjson)
1270+ => Expression schema from grouping params (nullity 'PGjson)
1271+ -> NP (Expression schema from grouping params) elems
1272+ -> Expression schema from grouping params (nullity 'PGjson)
12281273jsonExtractPathAsText x xs =
12291274 unsafeVariadicFunction "json_extract_path_text" (x :* xs)
12301275
@@ -1232,50 +1277,38 @@ jsonExtractPathAsText x xs =
12321277-- operator), as text.
12331278jsonbExtractPathAsText
12341279 :: SListI elems
1235− => Expression schema relations grouping params (nullity 'PGjsonb)
1236− -> NP (Expression schema relations grouping params) elems
1237− -> Expression schema relations grouping params (nullity 'PGjsonb)
1280+ => Expression schema from grouping params (nullity 'PGjsonb)
1281+ -> NP (Expression schema from grouping params) elems
1282+ -> Expression schema from grouping params (nullity 'PGjsonb)
12381283jsonbExtractPathAsText x xs =
12391284 unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
12401285
1241−-- | Returns set of keys in the outermost JSON object.
1242−jsonObjectKeys
1243− :: Expression schema relations grouping params (nullity 'PGjson)
1244− -> Expression schema relations grouping params (nullity 'PGtext)
1245−jsonObjectKeys = unsafeFunction "json_object_keys"
1246−
1247−-- | Returns set of keys in the outermost JSON object.
1248−jsonbObjectKeys
1249− :: Expression schema relations grouping params (nullity 'PGjsonb)
1250− -> Expression schema relations grouping params (nullity 'PGtext)
1251−jsonbObjectKeys = unsafeFunction "jsonb_object_keys"
1252−
12531286-- | Returns the type of the outermost JSON value as a text string. Possible
12541287-- types are object, array, string, number, boolean, and null.
12551288jsonTypeof
1256− :: Expression schema relations grouping params (nullity 'PGjson)
1257− -> Expression schema relations grouping params (nullity 'PGtext)
1289+ :: Expression schema from grouping params (nullity 'PGjson)
1290+ -> Expression schema from grouping params (nullity 'PGtext)
12581291jsonTypeof = unsafeFunction "json_typeof"
12591292
12601293-- | Returns the type of the outermost binary JSON value as a text string.
12611294-- Possible types are object, array, string, number, boolean, and null.
12621295jsonbTypeof
1263− :: Expression schema relations grouping params (nullity 'PGjsonb)
1264− -> Expression schema relations grouping params (nullity 'PGtext)
1296+ :: Expression schema from grouping params (nullity 'PGjsonb)
1297+ -> Expression schema from grouping params (nullity 'PGtext)
12651298jsonbTypeof = unsafeFunction "jsonb_typeof"
12661299
12671300-- | Returns its argument with all object fields that have null values omitted.
12681301-- Other null values are untouched.
12691302jsonStripNulls
1270− :: Expression schema relations grouping params (nullity 'PGjson)
1271− -> Expression schema relations grouping params (nullity 'PGjson)
1303+ :: Expression schema from grouping params (nullity 'PGjson)
1304+ -> Expression schema from grouping params (nullity 'PGjson)
12721305jsonStripNulls = unsafeFunction "json_strip_nulls"
12731306
12741307-- | Returns its argument with all object fields that have null values omitted.
12751308-- Other null values are untouched.
12761309jsonbStripNulls
1277− :: Expression schema relations grouping params (nullity 'PGjsonb)
1278− -> Expression schema relations grouping params (nullity 'PGjsonb)
1310+ :: Expression schema from grouping params (nullity 'PGjsonb)
1311+ -> Expression schema from grouping params (nullity 'PGjsonb)
12791312jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
12801313
12811314-- | @ jsonbSet target path new_value create_missing @
@@ -1286,12 +1319,12 @@ jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
12861319-- operators, negative integers that appear in path count from the end of JSON
12871320-- arrays.
12881321jsonbSet
1289− :: PGtextArray "jsonbSet" arr
1290− => Expression schema relations grouping params (nullity 'PGjsonb)
1291− -> Expression schema relations grouping params (nullity arr)
1292− -> Expression schema relations grouping params (nullity 'PGjsonb)
1293− -> Maybe (Expression schema relations grouping params (nullity 'PGbool))
1294− -> Expression schema relations grouping params (nullity 'PGjsonb)
1322+ :: PGTextArray "jsonbSet" arr
1323+ => Expression schema from grouping params (nullity 'PGjsonb)
1324+ -> Expression schema from grouping params (nullity arr)
1325+ -> Expression schema from grouping params (nullity 'PGjsonb)
1326+ -> Maybe (Expression schema from grouping params (nullity 'PGbool))
1327+ -> Expression schema from grouping params (nullity 'PGjsonb)
12951328jsonbSet tgt path val createMissing = case createMissing of
12961329 Just m -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* m :* Nil)
12971330 Nothing -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* Nil)
@@ -1305,20 +1338,20 @@ jsonbSet tgt path val createMissing = case createMissing of
13051338-- exist. As with the path orientated operators, negative integers that appear
13061339-- in path count from the end of JSON arrays.
13071340jsonbInsert
1308− :: PGtextArray "jsonbInsert" arr
1309− => Expression schema relations grouping params (nullity 'PGjsonb)
1310− -> Expression schema relations grouping params (nullity arr)
1311− -> Expression schema relations grouping params (nullity 'PGjsonb)
1312− -> Maybe (Expression schema relations grouping params (nullity 'PGbool))
1313− -> Expression schema relations grouping params (nullity 'PGjsonb)
1341+ :: PGTextArray "jsonbInsert" arr
1342+ => Expression schema from grouping params (nullity 'PGjsonb)
1343+ -> Expression schema from grouping params (nullity arr)
1344+ -> Expression schema from grouping params (nullity 'PGjsonb)
1345+ -> Maybe (Expression schema from grouping params (nullity 'PGbool))
1346+ -> Expression schema from grouping params (nullity 'PGjsonb)
13141347jsonbInsert tgt path val insertAfter = case insertAfter of
13151348 Just i -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* i :* Nil)
13161349 Nothing -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* Nil)
13171350
13181351-- | Returns its argument as indented JSON text.
13191352jsonbPretty
1320− :: Expression schema relations grouping params (nullity 'PGjsonb)
1321− -> Expression schema relations grouping params (nullity 'PGtext)
1353+ :: Expression schema from grouping params (nullity 'PGjsonb)
1354+ -> Expression schema from grouping params (nullity 'PGtext)
13221355jsonbPretty = unsafeFunction "jsonb_pretty"
13231356
13241357{-----------------------------------------
@@ -1328,16 +1361,16 @@ aggregation
13281361-- | escape hatch to define aggregate functions
13291362unsafeAggregate
13301363 :: ByteString -- ^ aggregate function
1331− -> Expression schema relations 'Ungrouped params (xty)
1332− -> Expression schema relations ('Grouped bys) params (yty)
1364+ -> Expression schema from 'Ungrouped params (xty)
1365+ -> Expression schema from ('Grouped bys) params (yty)
13331366unsafeAggregate fun x = UnsafeExpression $ mconcat
13341367 [fun, "(", renderExpression x, ")"]
13351368
13361369-- | escape hatch to define aggregate functions over distinct values
13371370unsafeAggregateDistinct
13381371 :: ByteString -- ^ aggregate function
1339− -> Expression schema relations 'Ungrouped params (xty)
1340− -> Expression schema relations ('Grouped bys) params (yty)
1372+ -> Expression schema from 'Ungrouped params (xty)
1373+ -> Expression schema from ('Grouped bys) params (yty)
13411374unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
13421375 [fun, "(DISTINCT ", renderExpression x, ")"]
13431376
@@ -1349,10 +1382,10 @@ unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
13491382-- :}
13501383-- sum("col")
13511384sum_
1352− :: PGNum ty
1353− => Expression schema relations 'Ungrouped params (nullity ty)
1385+ :: ty `In` PGNum
1386+ => Expression schema from 'Ungrouped params (nullity ty)
13541387 -- ^ what to sum
1355− -> Expression schema relations ('Grouped bys) params (nullity ty)
1388+ -> Expression schema from ('Grouped bys) params (nullity ty)
13561389sum_ = unsafeAggregate "sum"
13571390
13581391-- | >>> :{
@@ -1363,19 +1396,19 @@ sum_ = unsafeAggregate "sum"
13631396-- :}
13641397-- sum(DISTINCT "col")
13651398sumDistinct
1366− :: PGNum ty
1367− => Expression schema relations 'Ungrouped params (nullity ty)
1399+ :: ty `In` PGNum
1400+ => Expression schema from 'Ungrouped params (nullity ty)
13681401 -- ^ what to sum
1369− -> Expression schema relations ('Grouped bys) params (nullity ty)
1402+ -> Expression schema from ('Grouped bys) params (nullity ty)
13701403sumDistinct = unsafeAggregateDistinct "sum"
13711404
13721405-- | A constraint for `PGType`s that you can take averages of and the resulting
13731406-- `PGType`.
13741407class PGAvg ty avg | ty -> avg where
13751408 avg, avgDistinct
1376− :: Expression schema relations 'Ungrouped params (nullity ty)
1409+ :: Expression schema from 'Ungrouped params (nullity ty)
13771410 -- ^ what to average
1378− -> Expression schema relations ('Grouped bys) params (nullity avg)
1411+ -> Expression schema from ('Grouped bys) params (nullity avg)
13791412 avg = unsafeAggregate "avg"
13801413 avgDistinct = unsafeAggregateDistinct "avg"
13811414instance PGAvg 'PGint2 'PGnumeric
@@ -1394,10 +1427,10 @@ instance PGAvg 'PGinterval 'PGinterval
13941427-- :}
13951428-- bit_and("col")
13961429bitAnd
1397− :: PGIntegral int
1398− => Expression schema relations 'Ungrouped params (nullity int)
1430+ :: int `In` PGIntegral
1431+ => Expression schema from 'Ungrouped params (nullity int)
13991432 -- ^ what to aggregate
1400− -> Expression schema relations ('Grouped bys) params (nullity int)
1433+ -> Expression schema from ('Grouped bys) params (nullity int)
14011434bitAnd = unsafeAggregate "bit_and"
14021435
14031436-- | >>> :{
@@ -1408,10 +1441,10 @@ bitAnd = unsafeAggregate "bit_and"
14081441-- :}
14091442-- bit_or("col")
14101443bitOr
1411− :: PGIntegral int
1412− => Expression schema relations 'Ungrouped params (nullity int)
1444+ :: int `In` PGIntegral
1445+ => Expression schema from 'Ungrouped params (nullity int)
14131446 -- ^ what to aggregate
1414− -> Expression schema relations ('Grouped bys) params (nullity int)
1447+ -> Expression schema from ('Grouped bys) params (nullity int)
14151448bitOr = unsafeAggregate "bit_or"
14161449
14171450-- | >>> :{
@@ -1422,10 +1455,10 @@ bitOr = unsafeAggregate "bit_or"
14221455-- :}
14231456-- bit_and(DISTINCT "col")
14241457bitAndDistinct
1425− :: PGIntegral int
1426− => Expression schema relations 'Ungrouped params (nullity int)
1458+ :: int `In` PGIntegral
1459+ => Expression schema from 'Ungrouped params (nullity int)
14271460 -- ^ what to aggregate
1428− -> Expression schema relations ('Grouped bys) params (nullity int)
1461+ -> Expression schema from ('Grouped bys) params (nullity int)
14291462bitAndDistinct = unsafeAggregateDistinct "bit_and"
14301463
14311464-- | >>> :{
@@ -1436,10 +1469,10 @@ bitAndDistinct = unsafeAggregateDistinct "bit_and"
14361469-- :}
14371470-- bit_or(DISTINCT "col")
14381471bitOrDistinct
1439− :: PGIntegral int
1440− => Expression schema relations 'Ungrouped params (nullity int)
1472+ :: int `In` PGIntegral
1473+ => Expression schema from 'Ungrouped params (nullity int)
14411474 -- ^ what to aggregate
1442− -> Expression schema relations ('Grouped bys) params (nullity int)
1475+ -> Expression schema from ('Grouped bys) params (nullity int)
14431476bitOrDistinct = unsafeAggregateDistinct "bit_or"
14441477
14451478-- | >>> :{
@@ -1450,9 +1483,9 @@ bitOrDistinct = unsafeAggregateDistinct "bit_or"
14501483-- :}
14511484-- bool_and("col")
14521485boolAnd
1453− :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
1486+ :: Expression schema from 'Ungrouped params (nullity 'PGbool)
14541487 -- ^ what to aggregate
1455− -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
1488+ -> Expression schema from ('Grouped bys) params (nullity 'PGbool)
14561489boolAnd = unsafeAggregate "bool_and"
14571490
14581491-- | >>> :{
@@ -1463,9 +1496,9 @@ boolAnd = unsafeAggregate "bool_and"
14631496-- :}
14641497-- bool_or("col")
14651498boolOr
1466− :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
1499+ :: Expression schema from 'Ungrouped params (nullity 'PGbool)
14671500 -- ^ what to aggregate
1468− -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
1501+ -> Expression schema from ('Grouped bys) params (nullity 'PGbool)
14691502boolOr = unsafeAggregate "bool_or"
14701503
14711504-- | >>> :{
@@ -1476,9 +1509,9 @@ boolOr = unsafeAggregate "bool_or"
14761509-- :}
14771510-- bool_and(DISTINCT "col")
14781511boolAndDistinct
1479− :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
1512+ :: Expression schema from 'Ungrouped params (nullity 'PGbool)
14801513 -- ^ what to aggregate
1481− -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
1514+ -> Expression schema from ('Grouped bys) params (nullity 'PGbool)
14821515boolAndDistinct = unsafeAggregateDistinct "bool_and"
14831516
14841517-- | >>> :{
@@ -1489,9 +1522,9 @@ boolAndDistinct = unsafeAggregateDistinct "bool_and"
14891522-- :}
14901523-- bool_or(DISTINCT "col")
14911524boolOrDistinct
1492− :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
1525+ :: Expression schema from 'Ungrouped params (nullity 'PGbool)
14931526 -- ^ what to aggregate
1494− -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
1527+ -> Expression schema from ('Grouped bys) params (nullity 'PGbool)
14951528boolOrDistinct = unsafeAggregateDistinct "bool_or"
14961529
14971530-- | A special aggregation that does not require an input
@@ -1499,7 +1532,7 @@ boolOrDistinct = unsafeAggregateDistinct "bool_or"
14991532-- >>> printSQL countStar
15001533-- count(*)
15011534countStar
1502− :: Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
1535+ :: Expression schema from ('Grouped bys) params ('NotNull 'PGint8)
15031536countStar = UnsafeExpression $ "count(*)"
15041537
15051538-- | >>> :{
@@ -1510,9 +1543,9 @@ countStar = UnsafeExpression $ "count(*)"
15101543-- :}
15111544-- count("col")
15121545count
1513− :: Expression schema relations 'Ungrouped params ty
1546+ :: Expression schema from 'Ungrouped params ty
15141547 -- ^ what to count
1515− -> Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
1548+ -> Expression schema from ('Grouped bys) params ('NotNull 'PGint8)
15161549count = unsafeAggregate "count"
15171550
15181551-- | >>> :{
@@ -1523,9 +1556,9 @@ count = unsafeAggregate "count"
15231556-- :}
15241557-- count(DISTINCT "col")
15251558countDistinct
1526− :: Expression schema relations 'Ungrouped params ty
1559+ :: Expression schema from 'Ungrouped params ty
15271560 -- ^ what to count
1528− -> Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
1561+ -> Expression schema from ('Grouped bys) params ('NotNull 'PGint8)
15291562countDistinct = unsafeAggregateDistinct "count"
15301563
15311564-- | synonym for `boolAnd`
@@ -1538,9 +1571,9 @@ countDistinct = unsafeAggregateDistinct "count"
15381571-- :}
15391572-- every("col")
15401573every
1541− :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
1574+ :: Expression schema from 'Ungrouped params (nullity 'PGbool)
15421575 -- ^ what to aggregate
1543− -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
1576+ -> Expression schema from ('Grouped bys) params (nullity 'PGbool)
15441577every = unsafeAggregate "every"
15451578
15461579-- | synonym for `boolAndDistinct`
@@ -1553,16 +1586,16 @@ every = unsafeAggregate "every"
15531586-- :}
15541587-- every(DISTINCT "col")
15551588everyDistinct
1556− :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
1589+ :: Expression schema from 'Ungrouped params (nullity 'PGbool)
15571590 -- ^ what to aggregate
1558− -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
1591+ -> Expression schema from ('Grouped bys) params (nullity 'PGbool)
15591592everyDistinct = unsafeAggregateDistinct "every"
15601593
15611594-- | minimum and maximum aggregation
15621595max_, min_, maxDistinct, minDistinct
1563− :: Expression schema relations 'Ungrouped params (nullity ty)
1596+ :: Expression schema from 'Ungrouped params (nullity ty)
15641597 -- ^ what to aggregate
1565− -> Expression schema relations ('Grouped bys) params (nullity ty)
1598+ -> Expression schema from ('Grouped bys) params (nullity ty)
15661599max_ = unsafeAggregate "max"
15671600min_ = unsafeAggregate "min"
15681601maxDistinct = unsafeAggregateDistinct "max"
@@ -1573,134 +1606,153 @@ type expressions
15731606-----------------------------------------}
15741607
15751608-- | `TypeExpression`s are used in `cast`s and `createTable` commands.
1576−newtype TypeExpression (schema :: SchemaType) (ty :: PGType)
1609+newtype TypeExpression (schema :: SchemaType) (ty :: NullityType)
15771610 = UnsafeTypeExpression { renderTypeExpression :: ByteString }
15781611 deriving (GHC.Generic,Show,Eq,Ord,NFData)
15791612
1580−instance (Has alias schema ('Typedef ty))
1581− => IsLabel alias (TypeExpression schema ty) where
1582− fromLabel = UnsafeTypeExpression (renderAlias (fromLabel @alias))
1613+-- | The enum or composite type in a `Typedef` can be expressed by its alias.
1614+typedef
1615+ :: Has alias schema ('Typedef ty)
1616+ => Alias alias
1617+ -> TypeExpression schema (nullity ty)
1618+typedef = UnsafeTypeExpression . renderAlias
1619+
1620+-- | The composite type corresponding to a `Table` definition can be expressed
1621+-- by its alias.
1622+typetable
1623+ :: Has alias schema ('Table tab)
1624+ => Alias alias
1625+ -> TypeExpression schema (nullity ('PGcomposite (TableToRow tab)))
1626+typetable = UnsafeTypeExpression . renderAlias
1627+
1628+-- | The composite type corresponding to a `View` definition can be expressed
1629+-- by its alias.
1630+typeview
1631+ :: Has alias schema ('View view)
1632+ => Alias alias
1633+ -> TypeExpression schema (nullity ('PGcomposite view))
1634+typeview = UnsafeTypeExpression . renderAlias
15831635
15841636-- | logical Boolean (true/false)
1585−bool :: TypeExpression schema 'PGbool
1637+bool :: TypeExpression schema (nullity 'PGbool)
15861638bool = UnsafeTypeExpression "bool"
15871639-- | signed two-byte integer
1588−int2, smallint :: TypeExpression schema 'PGint2
1640+int2, smallint :: TypeExpression schema (nullity 'PGint2)
15891641int2 = UnsafeTypeExpression "int2"
15901642smallint = UnsafeTypeExpression "smallint"
15911643-- | signed four-byte integer
1592−int4, int, integer :: TypeExpression schema 'PGint4
1644+int4, int, integer :: TypeExpression schema (nullity 'PGint4)
15931645int4 = UnsafeTypeExpression "int4"
15941646int = UnsafeTypeExpression "int"
15951647integer = UnsafeTypeExpression "integer"
15961648-- | signed eight-byte integer
1597−int8, bigint :: TypeExpression schema 'PGint8
1649+int8, bigint :: TypeExpression schema (nullity 'PGint8)
15981650int8 = UnsafeTypeExpression "int8"
15991651bigint = UnsafeTypeExpression "bigint"
16001652-- | arbitrary precision numeric type
1601−numeric :: TypeExpression schema 'PGnumeric
1653+numeric :: TypeExpression schema (nullity 'PGnumeric)
16021654numeric = UnsafeTypeExpression "numeric"
16031655-- | single precision floating-point number (4 bytes)
1604−float4, real :: TypeExpression schema 'PGfloat4
1656+float4, real :: TypeExpression schema (nullity 'PGfloat4)
16051657float4 = UnsafeTypeExpression "float4"
16061658real = UnsafeTypeExpression "real"
16071659-- | double precision floating-point number (8 bytes)
1608−float8, doublePrecision :: TypeExpression schema 'PGfloat8
1660+float8, doublePrecision :: TypeExpression schema (nullity 'PGfloat8)
16091661float8 = UnsafeTypeExpression "float8"
16101662doublePrecision = UnsafeTypeExpression "double precision"
16111663-- | variable-length character string
1612−text :: TypeExpression schema 'PGtext
1664+text :: TypeExpression schema (nullity 'PGtext)
16131665text = UnsafeTypeExpression "text"
16141666-- | fixed-length character string
16151667char, character
1616− :: (KnownNat n, 1 <= n)
1617− => proxy n
1618− -> TypeExpression schema ('PGchar n)
1619−char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
1620−character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
1668+ :: forall n schema nullity. (KnownNat n, 1 <= n)
1669+ => TypeExpression schema (nullity ('PGchar n))
1670+char = UnsafeTypeExpression $ "char(" <> renderNat @n <> ")"
1671+character = UnsafeTypeExpression $ "character(" <> renderNat @n <> ")"
16211672-- | variable-length character string
16221673varchar, characterVarying
1623− :: (KnownNat n, 1 <= n)
1624− => proxy n
1625− -> TypeExpression schema ('PGvarchar n)
1626−varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
1627−characterVarying p = UnsafeTypeExpression $
1628− "character varying(" <> renderNat p <> ")"
1674+ :: forall n schema nullity. (KnownNat n, 1 <= n)
1675+ => TypeExpression schema (nullity ('PGvarchar n))
1676+varchar = UnsafeTypeExpression $ "varchar(" <> renderNat @n <> ")"
1677+characterVarying = UnsafeTypeExpression $
1678+ "character varying(" <> renderNat @n <> ")"
16291679-- | binary data ("byte array")
1630−bytea :: TypeExpression schema 'PGbytea
1680+bytea :: TypeExpression schema (nullity 'PGbytea)
16311681bytea = UnsafeTypeExpression "bytea"
16321682-- | date and time (no time zone)
1633−timestamp :: TypeExpression schema 'PGtimestamp
1683+timestamp :: TypeExpression schema (nullity 'PGtimestamp)
16341684timestamp = UnsafeTypeExpression "timestamp"
16351685-- | date and time, including time zone
1636−timestampWithTimeZone :: TypeExpression schema 'PGtimestamptz
1686+timestampWithTimeZone :: TypeExpression schema (nullity 'PGtimestamptz)
16371687timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
16381688-- | calendar date (year, month, day)
1639−date :: TypeExpression schema 'PGdate
1689+date :: TypeExpression schema (nullity 'PGdate)
16401690date = UnsafeTypeExpression "date"
16411691-- | time of day (no time zone)
1642−time :: TypeExpression schema 'PGtime
1692+time :: TypeExpression schema (nullity 'PGtime)
16431693time = UnsafeTypeExpression "time"
16441694-- | time of day, including time zone
1645−timeWithTimeZone :: TypeExpression schema 'PGtimetz
1695+timeWithTimeZone :: TypeExpression schema (nullity 'PGtimetz)
16461696timeWithTimeZone = UnsafeTypeExpression "time with time zone"
16471697-- | time span
1648−interval :: TypeExpression schema 'PGinterval
1698+interval :: TypeExpression schema (nullity 'PGinterval)
16491699interval = UnsafeTypeExpression "interval"
16501700-- | universally unique identifier
1651−uuid :: TypeExpression schema 'PGuuid
1701+uuid :: TypeExpression schema (nullity 'PGuuid)
16521702uuid = UnsafeTypeExpression "uuid"
16531703-- | IPv4 or IPv6 host address
1654−inet :: TypeExpression schema 'PGinet
1704+inet :: TypeExpression schema (nullity 'PGinet)
16551705inet = UnsafeTypeExpression "inet"
16561706-- | textual JSON data
1657−json :: TypeExpression schema 'PGjson
1707+json :: TypeExpression schema (nullity 'PGjson)
16581708json = UnsafeTypeExpression "json"
16591709-- | binary JSON data, decomposed
1660−jsonb :: TypeExpression schema 'PGjsonb
1710+jsonb :: TypeExpression schema (nullity 'PGjsonb)
16611711jsonb = UnsafeTypeExpression "jsonb"
16621712-- | variable length array
16631713vararray
16641714 :: TypeExpression schema pg
1665− -> TypeExpression schema ('PGvararray pg)
1715+ -> TypeExpression schema (nullity ('PGvararray pg))
16661716vararray ty = UnsafeTypeExpression $ renderTypeExpression ty <> "[]"
16671717-- | fixed length array
16681718--
1669−-- >>> renderTypeExpression (fixarray (Proxy @2) json)
1719+-- >>> renderTypeExpression (fixarray @2 json)
16701720-- "json[2]"
16711721fixarray
1672− :: KnownNat n
1673− => proxy n
1674− -> TypeExpression schema pg
1675− -> TypeExpression schema ('PGfixarray n pg)
1676−fixarray p ty = UnsafeTypeExpression $
1677− renderTypeExpression ty <> "[" <> renderNat p <> "]"
1722+ :: forall n schema nullity pg. KnownNat n
1723+ => TypeExpression schema pg
1724+ -> TypeExpression schema (nullity ('PGfixarray n pg))
1725+fixarray ty = UnsafeTypeExpression $
1726+ renderTypeExpression ty <> "[" <> renderNat @n <> "]"
16781727
16791728-- | `pgtype` is a demoted version of a `PGType`
1680−class PGTyped schema (ty :: PGType) where pgtype :: TypeExpression schema ty
1681−instance PGTyped schema 'PGbool where pgtype = bool
1682−instance PGTyped schema 'PGint2 where pgtype = int2
1683−instance PGTyped schema 'PGint4 where pgtype = int4
1684−instance PGTyped schema 'PGint8 where pgtype = int8
1685−instance PGTyped schema 'PGnumeric where pgtype = numeric
1686−instance PGTyped schema 'PGfloat4 where pgtype = float4
1687−instance PGTyped schema 'PGfloat8 where pgtype = float8
1688−instance PGTyped schema 'PGtext where pgtype = text
1729+class PGTyped schema (ty :: NullityType) where
1730+ pgtype :: TypeExpression schema ty
1731+instance PGTyped schema (nullity 'PGbool) where pgtype = bool
1732+instance PGTyped schema (nullity 'PGint2) where pgtype = int2
1733+instance PGTyped schema (nullity 'PGint4) where pgtype = int4
1734+instance PGTyped schema (nullity 'PGint8) where pgtype = int8
1735+instance PGTyped schema (nullity 'PGnumeric) where pgtype = numeric
1736+instance PGTyped schema (nullity 'PGfloat4) where pgtype = float4
1737+instance PGTyped schema (nullity 'PGfloat8) where pgtype = float8
1738+instance PGTyped schema (nullity 'PGtext) where pgtype = text
16891739instance (KnownNat n, 1 <= n)
1690− => PGTyped schema ('PGchar n) where pgtype = char (Proxy @n)
1740+ => PGTyped schema (nullity ('PGchar n)) where pgtype = char @n
16911741instance (KnownNat n, 1 <= n)
1692− => PGTyped schema ('PGvarchar n) where pgtype = varchar (Proxy @n)
1693−instance PGTyped schema 'PGbytea where pgtype = bytea
1694−instance PGTyped schema 'PGtimestamp where pgtype = timestamp
1695−instance PGTyped schema 'PGtimestamptz where pgtype = timestampWithTimeZone
1696−instance PGTyped schema 'PGdate where pgtype = date
1697−instance PGTyped schema 'PGtime where pgtype = time
1698−instance PGTyped schema 'PGtimetz where pgtype = timeWithTimeZone
1699−instance PGTyped schema 'PGinterval where pgtype = interval
1700−instance PGTyped schema 'PGuuid where pgtype = uuid
1701−instance PGTyped schema 'PGjson where pgtype = json
1702−instance PGTyped schema 'PGjsonb where pgtype = jsonb
1703−instance PGTyped schema ty => PGTyped schema ('PGvararray ty) where
1704− pgtype = vararray (pgtype @schema @ty)
1705−instance (KnownNat n, PGTyped schema ty) => PGTyped schema ('PGfixarray n ty) where
1706− pgtype = fixarray (Proxy @n) (pgtype @schema @ty)
1742+ => PGTyped schema (nullity ('PGvarchar n)) where pgtype = varchar @n
1743+instance PGTyped schema (nullity 'PGbytea) where pgtype = bytea
1744+instance PGTyped schema (nullity 'PGtimestamp) where pgtype = timestamp
1745+instance PGTyped schema (nullity 'PGtimestamptz) where pgtype = timestampWithTimeZone
1746+instance PGTyped schema (nullity 'PGdate) where pgtype = date
1747+instance PGTyped schema (nullity 'PGtime) where pgtype = time
1748+instance PGTyped schema (nullity 'PGtimetz) where pgtype = timeWithTimeZone
1749+instance PGTyped schema (nullity 'PGinterval) where pgtype = interval
1750+instance PGTyped schema (nullity 'PGuuid) where pgtype = uuid
1751+instance PGTyped schema (nullity 'PGjson) where pgtype = json
1752+instance PGTyped schema (nullity 'PGjsonb) where pgtype = jsonb
1753+instance PGTyped schema ty
1754+ => PGTyped schema (nullity ('PGvararray ty)) where
1755+ pgtype = vararray (pgtype @schema @ty)
1756+instance (KnownNat n, PGTyped schema ty)
1757+ => PGTyped schema (nullity ('PGfixarray n ty)) where
1758+ pgtype = fixarray @n (pgtype @schema @ty)