@@ -30,7 +30,8 @@ Squeal expressions are the atoms used to build statements.
3030module Squeal.PostgreSQL.Expression
3131 ( -- * Expression
3232 Expression (UnsafeExpression, renderExpression)
33− , HasParameter (param)
33+ , HasParameter (parameter)
34+ , param
3435 -- ** Null
3536 , null_
3637 , notNull
@@ -163,6 +164,7 @@ values from primitive expression using arithmetic, logical,
163164and other operations.
164165-}
165166newtype Expression
167+ (schema :: SchemaType)
166168 (relations :: RelationsType)
167169 (grouping :: Grouping)
168170 (params :: [NullityType])
@@ -178,43 +180,51 @@ supplied externally to a SQL statement.
178180separately from the SQL command string, in which case `param`s are used to
179181refer to the out-of-line data values.
180182-}
181−class (PGTyped (PGTypeOf ty), KnownNat n) => HasParameter
183+class KnownNat n => HasParameter
182184 (n :: Nat)
185+ (schema :: SchemaType)
183186 (params :: [NullityType])
184187 (ty :: NullityType)
185188 | n params -> ty where
186− param :: Expression relations grouping params ty
187− param = UnsafeExpression $ parenthesized $
189+ parameter
190+ :: TypeExpression schema (PGTypeOf ty)
191+ -> Expression schema relations grouping params ty
192+ parameter ty = UnsafeExpression $ parenthesized $
188193 "$" <> renderNat (Proxy @n) <+> "::"
189− <+> renderTypeExpression (pgtype @(PGTypeOf ty))
190−instance {-# OVERLAPPING #-} PGTyped (PGTypeOf ty1)
191− => HasParameter 1 (ty1:tys) ty1
192−instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
193− => HasParameter n (ty' : params) ty
194+ <+> renderTypeExpression ty
195+instance {-# OVERLAPPING #-} HasParameter 1 schema (ty1:tys) ty1
196+instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) schema params ty)
197+ => HasParameter n schema (ty' : params) ty
198+
199+param
200+ :: forall n schema params relations grouping ty
201+ . (PGTyped schema (PGTypeOf ty), HasParameter n schema params ty)
202+ => Expression schema relations grouping params ty
203+param = parameter @n pgtype
194204
195205instance (HasUnique relation relations columns, Has column columns ty)
196− => IsLabel column (Expression relations 'Ungrouped params ty) where
206+ => IsLabel column (Expression schema relations 'Ungrouped params ty) where
197207 fromLabel = UnsafeExpression $ renderAlias (Alias @column)
198208instance (HasUnique relation relations columns, Has column columns ty)
199209 => IsLabel column
200− (Aliased (Expression relations 'Ungrouped params) (column ::: ty)) where
210+ (Aliased (Expression schema relations 'Ungrouped params) (column ::: ty)) where
201211 fromLabel = fromLabel @column `As` Alias @column
202212instance (HasUnique relation relations columns, Has column columns ty)
203213 => IsLabel column
204− (NP (Aliased (Expression relations 'Ungrouped params)) '[column ::: ty]) where
214+ (NP (Aliased (Expression schema relations 'Ungrouped params)) '[column ::: ty]) where
205215 fromLabel = fromLabel @column :* Nil
206216
207217instance (Has relation relations columns, Has column columns ty)
208− => IsQualified relation column (Expression relations 'Ungrouped params ty) where
218+ => IsQualified relation column (Expression schema relations 'Ungrouped params ty) where
209219 relation ! column = UnsafeExpression $
210220 renderAlias relation <> "." <> renderAlias column
211221instance (Has relation relations columns, Has column columns ty)
212222 => IsQualified relation column
213− (Aliased (Expression relations 'Ungrouped params) (column ::: ty)) where
223+ (Aliased (Expression schema relations 'Ungrouped params) (column ::: ty)) where
214224 relation ! column = relation ! column `As` column
215225instance (Has relation relations columns, Has column columns ty)
216226 => IsQualified relation column
217− (NP (Aliased (Expression relations 'Ungrouped params)) '[column ::: ty]) where
227+ (NP (Aliased (Expression schema relations 'Ungrouped params)) '[column ::: ty]) where
218228 relation ! column = relation ! column :* Nil
219229
220230instance
@@ -222,14 +232,14 @@ instance
222232 , Has column columns ty
223233 , GroupedBy relation column bys
224234 ) => IsLabel column
225− (Expression relations ('Grouped bys) params ty) where
235+ (Expression schema relations ('Grouped bys) params ty) where
226236 fromLabel = UnsafeExpression $ renderAlias (Alias @column)
227237instance
228238 ( HasUnique relation relations columns
229239 , Has column columns ty
230240 , GroupedBy relation column bys
231241 ) => IsLabel column
232− ( Aliased (Expression relations ('Grouped bys) params)
242+ ( Aliased (Expression schema relations ('Grouped bys) params)
233243 (column ::: ty) ) where
234244 fromLabel = fromLabel @column `As` Alias @column
235245instance
@@ -237,7 +247,7 @@ instance
237247 , Has column columns ty
238248 , GroupedBy relation column bys
239249 ) => IsLabel column
240− ( NP (Aliased (Expression relations ('Grouped bys) params))
250+ ( NP (Aliased (Expression schema relations ('Grouped bys) params))
241251 '[column ::: ty] ) where
242252 fromLabel = fromLabel @column :* Nil
243253
@@ -246,7 +256,7 @@ instance
246256 , Has column columns ty
247257 , GroupedBy relation column bys
248258 ) => IsQualified relation column
249− (Expression relations ('Grouped bys) params ty) where
259+ (Expression schema relations ('Grouped bys) params ty) where
250260 relation ! column = UnsafeExpression $
251261 renderAlias relation <> "." <> renderAlias column
252262instance
@@ -254,7 +264,7 @@ instance
254264 , Has column columns ty
255265 , GroupedBy relation column bys
256266 ) => IsQualified relation column
257− (Aliased (Expression relations ('Grouped bys) params)
267+ (Aliased (Expression schema relations ('Grouped bys) params)
258268 (column ::: ty)) where
259269 relation ! column = relation ! column `As` column
260270instance
@@ -262,7 +272,7 @@ instance
262272 , Has column columns ty
263273 , GroupedBy relation column bys
264274 ) => IsQualified relation column
265− ( NP (Aliased (Expression relations ('Grouped bys) params))
275+ ( NP (Aliased (Expression schema relations ('Grouped bys) params))
266276 '[column ::: ty]) where
267277 relation ! column = relation ! column :* Nil
268278
@@ -270,7 +280,7 @@ instance
270280--
271281-- >>> renderExpression $ null_
272282-- "NULL"
273−null_ :: Expression rels grouping params ('Null ty)
283+null_ :: Expression schema rels grouping params ('Null ty)
274284null_ = UnsafeExpression "NULL"
275285
276286-- | analagous to `Just`
@@ -278,8 +288,8 @@ null_ = UnsafeExpression "NULL"
278288-- >>> renderExpression $ notNull true
279289-- "TRUE"
280290notNull
281− :: Expression rels grouping params ('NotNull ty)
282− -> Expression rels grouping params ('Null ty)
291+ :: Expression schema rels grouping params ('NotNull ty)
292+ -> Expression schema rels grouping params ('Null ty)
283293notNull = UnsafeExpression . renderExpression
284294
285295-- | return the leftmost value which is not NULL
@@ -287,11 +297,11 @@ notNull = UnsafeExpression . renderExpression
287297-- >>> renderExpression $ coalesce [null_, notNull true] false
288298-- "COALESCE(NULL, TRUE, FALSE)"
289299coalesce
290− :: [Expression relations grouping params ('Null ty)]
300+ :: [Expression schema relations grouping params ('Null ty)]
291301 -- ^ @NULL@s may be present
292− -> Expression relations grouping params ('NotNull ty)
302+ -> Expression schema relations grouping params ('NotNull ty)
293303 -- ^ @NULL@ is absent
294− -> Expression relations grouping params ('NotNull ty)
304+ -> Expression schema relations grouping params ('NotNull ty)
295305coalesce nullxs notNullx = UnsafeExpression $
296306 "COALESCE" <> parenthesized (commaSeparated
297307 ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
@@ -301,26 +311,26 @@ coalesce nullxs notNullx = UnsafeExpression $
301311-- >>> renderExpression $ fromNull true null_
302312-- "COALESCE(NULL, TRUE)"
303313fromNull
304− :: Expression relations grouping params ('NotNull ty)
314+ :: Expression schema relations grouping params ('NotNull ty)
305315 -- ^ what to convert @NULL@ to
306− -> Expression relations grouping params ('Null ty)
307− -> Expression relations grouping params ('NotNull ty)
316+ -> Expression schema relations grouping params ('Null ty)
317+ -> Expression schema relations grouping params ('NotNull ty)
308318fromNull notNullx nullx = coalesce [nullx] notNullx
309319
310320-- | >>> renderExpression $ null_ & isNull
311321-- "NULL IS NULL"
312322isNull
313− :: Expression relations grouping params ('Null ty)
323+ :: Expression schema relations grouping params ('Null ty)
314324 -- ^ possibly @NULL@
315− -> Condition relations grouping params
325+ -> Condition schema relations grouping params
316326isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
317327
318328-- | >>> renderExpression $ null_ & isNotNull
319329-- "NULL IS NOT NULL"
320330isNotNull
321− :: Expression relations grouping params ('Null ty)
331+ :: Expression schema relations grouping params ('Null ty)
322332 -- ^ possibly @NULL@
323− -> Condition relations grouping params
333+ -> Condition schema relations grouping params
324334isNotNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
325335
326336-- | analagous to `maybe` using @IS NULL@
@@ -328,13 +338,13 @@ isNotNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
328338-- >>> renderExpression $ matchNull true not_ null_
329339-- "CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END"
330340matchNull
331− :: Expression relations grouping params (nullty)
341+ :: Expression schema relations grouping params (nullty)
332342 -- ^ what to convert @NULL@ to
333− -> ( Expression relations grouping params ('NotNull ty)
334− -> Expression relations grouping params (nullty) )
343+ -> ( Expression schema relations grouping params ('NotNull ty)
344+ -> Expression schema relations grouping params (nullty) )
335345 -- ^ function to perform when @NULL@ is absent
336− -> Expression relations grouping params ('Null ty)
337− -> Expression relations grouping params (nullty)
346+ -> Expression schema relations grouping params ('Null ty)
347+ -> Expression schema relations grouping params (nullty)
338348matchNull y f x = ifThenElse (isNull x) y
339349 (f (UnsafeExpression (renderExpression x)))
340350
@@ -345,25 +355,25 @@ matchNull y f x = ifThenElse (isNull x) y
345355-- >>> renderExpression @_ @_ @'[_] $ fromNull false (nullIf false (param @1))
346356-- "COALESCE(NULL IF (FALSE, ($1 :: bool)), FALSE)"
347357nullIf
348− :: Expression relations grouping params ('NotNull ty)
358+ :: Expression schema relations grouping params ('NotNull ty)
349359 -- ^ @NULL@ is absent
350− -> Expression relations grouping params ('NotNull ty)
360+ -> Expression schema relations grouping params ('NotNull ty)
351361 -- ^ @NULL@ is absent
352− -> Expression relations grouping params ('Null ty)
362+ -> Expression schema relations grouping params ('Null ty)
353363nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
354364 (renderExpression x <> ", " <> renderExpression y)
355365
356366-- | >>> renderExpression $ array [null_, notNull false, notNull true]
357367-- "ARRAY[NULL, FALSE, TRUE]"
358368array
359− :: [Expression relations grouping params ('Null ty)]
369+ :: [Expression schema relations grouping params ('Null ty)]
360370 -- ^ array elements
361− -> Expression relations grouping params (nullity ('PGvararray ty))
371+ -> Expression schema relations grouping params (nullity ('PGvararray ty))
362372array xs = UnsafeExpression $
363373 "ARRAY[" <> commaSeparated (renderExpression <$> xs) <> "]"
364374
365375instance (KnownSymbol label, label `In` labels) => IsPGlabel label
366− (Expression relations grouping params (nullity ('PGenum labels))) where
376+ (Expression schema relations grouping params (nullity ('PGenum labels))) where
367377 label = UnsafeExpression $ renderLabel (PGlabel @label)
368378
369379-- | A row constructor is an expression that builds a row value
@@ -375,47 +385,47 @@ instance (KnownSymbol label, label `In` labels) => IsPGlabel label
375385-- "ROW(0, 1)"
376386row
377387 :: SListI (Nulls fields)
378− => NP (Aliased (Expression relation grouping params)) (Nulls fields)
388+ => NP (Aliased (Expression schema relations grouping params)) (Nulls fields)
379389 -- ^ zero or more expressions for the row field values
380− -> Expression relation grouping params (nullity ('PGcomposite fields))
390+ -> Expression schema relations grouping params (nullity ('PGcomposite fields))
381391row exprs = UnsafeExpression $ "ROW" <> parenthesized
382392 (renderCommaSeparated (\ (expr `As` _) -> renderExpression expr) exprs)
383393
384394instance Has field fields ty => IsLabel field
385− ( Expression relation grouping params (nullity ('PGcomposite fields))
386− -> Expression relation grouping params ('Null ty) ) where
395+ ( Expression schema relation grouping params (nullity ('PGcomposite fields))
396+ -> Expression schema relation grouping params ('Null ty) ) where
387397 fromLabel expr = UnsafeExpression $
388398 parenthesized (renderExpression expr) <> "." <>
389399 fromString (symbolVal (Proxy @field))
390400
391401instance Semigroup
392− (Expression relations grouping params (nullity ('PGvararray ty))) where
402+ (Expression schema relations grouping params (nullity ('PGvararray ty))) where
393403 (<>) = unsafeBinaryOp "||"
394404
395405instance Monoid
396− (Expression relations grouping params (nullity ('PGvararray ty))) where
406+ (Expression schema relations grouping params (nullity ('PGvararray ty))) where
397407 mempty = array []
398408 mappend = (<>)
399409
400410-- | >>> renderExpression @_ @_ @'[_] $ greatest currentTimestamp [param @1]
401411-- "GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))"
402412greatest
403− :: Expression relations grouping params (nullty)
413+ :: Expression schema relations grouping params (nullty)
404414 -- ^ needs at least 1 argument
405− -> [Expression relations grouping params (nullty)]
415+ -> [Expression schema relations grouping params (nullty)]
406416 -- ^ or more
407− -> Expression relations grouping params (nullty)
417+ -> Expression schema relations grouping params (nullty)
408418greatest x xs = UnsafeExpression $ "GREATEST("
409419 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
410420
411421-- | >>> renderExpression $ least currentTimestamp [null_]
412422-- "LEAST(CURRENT_TIMESTAMP, NULL)"
413423least
414− :: Expression relations grouping params (nullty)
424+ :: Expression schema relations grouping params (nullty)
415425 -- ^ needs at least 1 argument
416− -> [Expression relations grouping params (nullty)]
426+ -> [Expression schema relations grouping params (nullty)]
417427 -- ^ or more
418− -> Expression relations grouping params (nullty)
428+ -> Expression schema relations grouping params (nullty)
419429least x xs = UnsafeExpression $ "LEAST("
420430 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
421431
@@ -424,9 +434,9 @@ least x xs = UnsafeExpression $ "LEAST("
424434unsafeBinaryOp
425435 :: ByteString
426436 -- ^ operator
427− -> Expression relations grouping params (ty0)
428− -> Expression relations grouping params (ty1)
429− -> Expression relations grouping params (ty2)
437+ -> Expression schema relations grouping params (ty0)
438+ -> Expression schema relations grouping params (ty1)
439+ -> Expression schema relations grouping params (ty2)
430440unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
431441 renderExpression x <+> op <+> renderExpression y
432442
@@ -435,8 +445,8 @@ unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
435445unsafeUnaryOp
436446 :: ByteString
437447 -- ^ operator
438− -> Expression relations grouping params (ty0)
439− -> Expression relations grouping params (ty1)
448+ -> Expression schema relations grouping params (ty0)
449+ -> Expression schema relations grouping params (ty1)
440450unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
441451 op <+> renderExpression x
442452
@@ -445,13 +455,13 @@ unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
445455unsafeFunction
446456 :: ByteString
447457 -- ^ function
448− -> Expression relations grouping params (xty)
449− -> Expression relations grouping params (yty)
458+ -> Expression schema relations grouping params (xty)
459+ -> Expression schema relations grouping params (yty)
450460unsafeFunction fun x = UnsafeExpression $
451461 fun <> parenthesized (renderExpression x)
452462
453463instance PGNum ty
454− => Num (Expression relations grouping params (nullity ty)) where
464+ => Num (Expression schema relations grouping params (nullity ty)) where
455465 (+) = unsafeBinaryOp "+"
456466 (-) = unsafeBinaryOp "-"
457467 (*) = unsafeBinaryOp "*"
@@ -463,12 +473,12 @@ instance PGNum ty
463473 . show
464474
465475instance (PGNum ty, PGFloating ty) => Fractional
466− (Expression relations grouping params (nullity ty)) where
476+ (Expression schema relations grouping params (nullity ty)) where
467477 (/) = unsafeBinaryOp "/"
468478 fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
469479
470480instance (PGNum ty, PGFloating ty) => Floating
471− (Expression relations grouping params (nullity ty)) where
481+ (Expression schema relations grouping params (nullity ty)) where
472482 pi = UnsafeExpression "pi()"
473483 exp = unsafeFunction "exp"
474484 log = unsafeFunction "ln"
@@ -491,18 +501,18 @@ instance (PGNum ty, PGFloating ty) => Floating
491501
492502-- | >>> :{
493503-- let
494−-- expression :: Expression relations grouping params (nullity 'PGfloat4)
504+-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
495505-- expression = atan2_ pi 2
496506-- in renderExpression expression
497507-- :}
498508-- "atan2(pi(), 2)"
499509atan2_
500510 :: PGFloating float
501− => Expression relations grouping params (nullity float)
511+ => Expression schema relations grouping params (nullity float)
502512 -- ^ numerator
503− -> Expression relations grouping params (nullity float)
513+ -> Expression schema relations grouping params (nullity float)
504514 -- ^ denominator
505− -> Expression relations grouping params (nullity float)
515+ -> Expression schema relations grouping params (nullity float)
506516atan2_ y x = UnsafeExpression $
507517 "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
508518
@@ -513,11 +523,11 @@ atan2_ y x = UnsafeExpression $
513523-- | >>> renderExpression $ true & cast int4
514524-- "(TRUE :: int4)"
515525cast
516− :: TypeExpression ty1
526+ :: TypeExpression schema ty1
517527 -- ^ type to cast as
518− -> Expression relations grouping params (nullity ty0)
528+ -> Expression schema relations grouping params (nullity ty0)
519529 -- ^ value to convert
520− -> Expression relations grouping params (nullity ty1)
530+ -> Expression schema relations grouping params (nullity ty1)
521531cast ty x = UnsafeExpression $ parenthesized $
522532 renderExpression x <+> "::" <+> renderTypeExpression ty
523533
@@ -525,135 +535,135 @@ cast ty x = UnsafeExpression $ parenthesized $
525535--
526536-- >>> :{
527537-- let
528−-- expression :: Expression relations grouping params (nullity 'PGint2)
538+-- expression :: Expression schema relations grouping params (nullity 'PGint2)
529539-- expression = 5 `quot_` 2
530540-- in renderExpression expression
531541-- :}
532542-- "(5 / 2)"
533543quot_
534544 :: PGIntegral int
535− => Expression relations grouping params (nullity int)
545+ => Expression schema relations grouping params (nullity int)
536546 -- ^ numerator
537− -> Expression relations grouping params (nullity int)
547+ -> Expression schema relations grouping params (nullity int)
538548 -- ^ denominator
539− -> Expression relations grouping params (nullity int)
549+ -> Expression schema relations grouping params (nullity int)
540550quot_ = unsafeBinaryOp "/"
541551
542552-- | remainder upon integer division
543553--
544554-- >>> :{
545555-- let
546−-- expression :: Expression relations grouping params (nullity 'PGint2)
556+-- expression :: Expression schema relations grouping params (nullity 'PGint2)
547557-- expression = 5 `rem_` 2
548558-- in renderExpression expression
549559-- :}
550560-- "(5 % 2)"
551561rem_
552562 :: PGIntegral int
553− => Expression relations grouping params (nullity int)
563+ => Expression schema relations grouping params (nullity int)
554564 -- ^ numerator
555− -> Expression relations grouping params (nullity int)
565+ -> Expression schema relations grouping params (nullity int)
556566 -- ^ denominator
557− -> Expression relations grouping params (nullity int)
567+ -> Expression schema relations grouping params (nullity int)
558568rem_ = unsafeBinaryOp "%"
559569
560570-- | >>> :{
561571-- let
562−-- expression :: Expression relations grouping params (nullity 'PGfloat4)
572+-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
563573-- expression = trunc pi
564574-- in renderExpression expression
565575-- :}
566576-- "trunc(pi())"
567577trunc
568578 :: PGFloating frac
569− => Expression relations grouping params (nullity frac)
579+ => Expression schema relations grouping params (nullity frac)
570580 -- ^ fractional number
571− -> Expression relations grouping params (nullity frac)
581+ -> Expression schema relations grouping params (nullity frac)
572582trunc = unsafeFunction "trunc"
573583
574584-- | >>> :{
575585-- let
576−-- expression :: Expression relations grouping params (nullity 'PGfloat4)
586+-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
577587-- expression = round_ pi
578588-- in renderExpression expression
579589-- :}
580590-- "round(pi())"
581591round_
582592 :: PGFloating frac
583− => Expression relations grouping params (nullity frac)
593+ => Expression schema relations grouping params (nullity frac)
584594 -- ^ fractional number
585− -> Expression relations grouping params (nullity frac)
595+ -> Expression schema relations grouping params (nullity frac)
586596round_ = unsafeFunction "round"
587597
588598-- | >>> :{
589599-- let
590−-- expression :: Expression relations grouping params (nullity 'PGfloat4)
600+-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
591601-- expression = ceiling_ pi
592602-- in renderExpression expression
593603-- :}
594604-- "ceiling(pi())"
595605ceiling_
596606 :: PGFloating frac
597− => Expression relations grouping params (nullity frac)
607+ => Expression schema relations grouping params (nullity frac)
598608 -- ^ fractional number
599− -> Expression relations grouping params (nullity frac)
609+ -> Expression schema relations grouping params (nullity frac)
600610ceiling_ = unsafeFunction "ceiling"
601611
602612-- | A `Condition` is a boolean valued `Expression`. While SQL allows
603613-- conditions to have @NULL@, Squeal instead chooses to disallow @NULL@,
604614-- forcing one to handle the case of @NULL@ explicitly to produce
605615-- a `Condition`.
606−type Condition relations grouping params =
607− Expression relations grouping params ('NotNull 'PGbool)
616+type Condition schema relations grouping params =
617+ Expression schema relations grouping params ('NotNull 'PGbool)
608618
609619-- | >>> renderExpression true
610620-- "TRUE"
611−true :: Condition relations grouping params
621+true :: Condition schema relations grouping params
612622true = UnsafeExpression "TRUE"
613623
614624-- | >>> renderExpression false
615625-- "FALSE"
616−false :: Condition relations grouping params
626+false :: Condition schema relations grouping params
617627false = UnsafeExpression "FALSE"
618628
619629-- | >>> renderExpression $ not_ true
620630-- "(NOT TRUE)"
621631not_
622− :: Condition relations grouping params
623− -> Condition relations grouping params
632+ :: Condition schema relations grouping params
633+ -> Condition schema relations grouping params
624634not_ = unsafeUnaryOp "NOT"
625635
626636-- | >>> renderExpression $ true .&& false
627637-- "(TRUE AND FALSE)"
628638(.&&)
629− :: Condition relations grouping params
630− -> Condition relations grouping params
631− -> Condition relations grouping params
639+ :: Condition schema relations grouping params
640+ -> Condition schema relations grouping params
641+ -> Condition schema relations grouping params
632642(.&&) = unsafeBinaryOp "AND"
633643
634644-- | >>> renderExpression $ true .|| false
635645-- "(TRUE OR FALSE)"
636646(.||)
637− :: Condition relations grouping params
638− -> Condition relations grouping params
639− -> Condition relations grouping params
647+ :: Condition schema relations grouping params
648+ -> Condition schema relations grouping params
649+ -> Condition schema relations grouping params
640650(.||) = unsafeBinaryOp "OR"
641651
642652-- | >>> :{
643653-- let
644−-- expression :: Expression relations grouping params (nullity 'PGint2)
654+-- expression :: Expression schema relations grouping params (nullity 'PGint2)
645655-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
646656-- in renderExpression expression
647657-- :}
648658-- "CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END"
649659caseWhenThenElse
650− :: [ ( Condition relations grouping params
651− , Expression relations grouping params (ty)
660+ :: [ ( Condition schema relations grouping params
661+ , Expression schema relations grouping params (ty)
652662 ) ]
653663 -- ^ whens and thens
654− -> Expression relations grouping params (ty)
664+ -> Expression schema relations grouping params (ty)
655665 -- ^ else
656− -> Expression relations grouping params (ty)
666+ -> Expression schema relations grouping params (ty)
657667caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
658668 [ "CASE"
659669 , mconcat
@@ -669,16 +679,16 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
669679
670680-- | >>> :{
671681-- let
672−-- expression :: Expression relations grouping params (nullity 'PGint2)
682+-- expression :: Expression schema relations grouping params (nullity 'PGint2)
673683-- expression = ifThenElse true 1 0
674684-- in renderExpression expression
675685-- :}
676686-- "CASE WHEN TRUE THEN 1 ELSE 0 END"
677687ifThenElse
678− :: Condition relations grouping params
679− -> Expression relations grouping params (ty) -- ^ then
680− -> Expression relations grouping params (ty) -- ^ else
681− -> Expression relations grouping params (ty)
688+ :: Condition schema relations grouping params
689+ -> Expression schema relations grouping params (ty) -- ^ then
690+ -> Expression schema relations grouping params (ty) -- ^ else
691+ -> Expression schema relations grouping params (ty)
682692ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
683693
684694-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
@@ -687,85 +697,85 @@ ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
687697-- >>> renderExpression $ notNull true .== null_
688698-- "(TRUE = NULL)"
689699(.==)
690− :: Expression relations grouping params (nullity ty) -- ^ lhs
691− -> Expression relations grouping params (nullity ty) -- ^ rhs
692− -> Expression relations grouping params (nullity 'PGbool)
700+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
701+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
702+ -> Expression schema relations grouping params (nullity 'PGbool)
693703(.==) = unsafeBinaryOp "="
694704infix 4 .==
695705
696706-- | >>> renderExpression $ notNull true ./= null_
697707-- "(TRUE <> NULL)"
698708(./=)
699− :: Expression relations grouping params (nullity ty) -- ^ lhs
700− -> Expression relations grouping params (nullity ty) -- ^ rhs
701− -> Expression relations grouping params (nullity 'PGbool)
709+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
710+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
711+ -> Expression schema relations grouping params (nullity 'PGbool)
702712(./=) = unsafeBinaryOp "<>"
703713infix 4 ./=
704714
705715-- | >>> renderExpression $ notNull true .>= null_
706716-- "(TRUE >= NULL)"
707717(.>=)
708− :: Expression relations grouping params (nullity ty) -- ^ lhs
709− -> Expression relations grouping params (nullity ty) -- ^ rhs
710− -> Expression relations grouping params (nullity 'PGbool)
718+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
719+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
720+ -> Expression schema relations grouping params (nullity 'PGbool)
711721(.>=) = unsafeBinaryOp ">="
712722infix 4 .>=
713723
714724-- | >>> renderExpression $ notNull true .< null_
715725-- "(TRUE < NULL)"
716726(.<)
717− :: Expression relations grouping params (nullity ty) -- ^ lhs
718− -> Expression relations grouping params (nullity ty) -- ^ rhs
719− -> Expression relations grouping params (nullity 'PGbool)
727+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
728+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
729+ -> Expression schema relations grouping params (nullity 'PGbool)
720730(.<) = unsafeBinaryOp "<"
721731infix 4 .<
722732
723733-- | >>> renderExpression $ notNull true .<= null_
724734-- "(TRUE <= NULL)"
725735(.<=)
726− :: Expression relations grouping params (nullity ty) -- ^ lhs
727− -> Expression relations grouping params (nullity ty) -- ^ rhs
728− -> Expression relations grouping params (nullity 'PGbool)
736+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
737+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
738+ -> Expression schema relations grouping params (nullity 'PGbool)
729739(.<=) = unsafeBinaryOp "<="
730740infix 4 .<=
731741
732742-- | >>> renderExpression $ notNull true .> null_
733743-- "(TRUE > NULL)"
734744(.>)
735− :: Expression relations grouping params (nullity ty) -- ^ lhs
736− -> Expression relations grouping params (nullity ty) -- ^ rhs
737− -> Expression relations grouping params (nullity 'PGbool)
745+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
746+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
747+ -> Expression schema relations grouping params (nullity 'PGbool)
738748(.>) = unsafeBinaryOp ">"
739749infix 4 .>
740750
741751-- | >>> renderExpression currentDate
742752-- "CURRENT_DATE"
743753currentDate
744− :: Expression relations grouping params (nullity 'PGdate)
754+ :: Expression schema relations grouping params (nullity 'PGdate)
745755currentDate = UnsafeExpression "CURRENT_DATE"
746756
747757-- | >>> renderExpression currentTime
748758-- "CURRENT_TIME"
749759currentTime
750− :: Expression relations grouping params (nullity 'PGtimetz)
760+ :: Expression schema relations grouping params (nullity 'PGtimetz)
751761currentTime = UnsafeExpression "CURRENT_TIME"
752762
753763-- | >>> renderExpression currentTimestamp
754764-- "CURRENT_TIMESTAMP"
755765currentTimestamp
756− :: Expression relations grouping params (nullity 'PGtimestamptz)
766+ :: Expression schema relations grouping params (nullity 'PGtimestamptz)
757767currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
758768
759769-- | >>> renderExpression localTime
760770-- "LOCALTIME"
761771localTime
762− :: Expression relations grouping params (nullity 'PGtime)
772+ :: Expression schema relations grouping params (nullity 'PGtime)
763773localTime = UnsafeExpression "LOCALTIME"
764774
765775-- | >>> renderExpression localTimestamp
766776-- "LOCALTIMESTAMP"
767777localTimestamp
768− :: Expression relations grouping params (nullity 'PGtimestamp)
778+ :: Expression schema relations grouping params (nullity 'PGtimestamp)
769779localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
770780
771781{-----------------------------------------
@@ -773,7 +783,7 @@ text
773783-----------------------------------------}
774784
775785instance IsString
776− (Expression relations grouping params (nullity 'PGtext)) where
786+ (Expression schema relations grouping params (nullity 'PGtext)) where
777787 fromString str = UnsafeExpression $
778788 "E\'" <> fromString (escape =<< str) <> "\'"
779789 where
@@ -789,36 +799,36 @@ instance IsString
789799 c -> [c]
790800
791801instance Semigroup
792− (Expression relations grouping params (nullity 'PGtext)) where
802+ (Expression schema relations grouping params (nullity 'PGtext)) where
793803 (<>) = unsafeBinaryOp "||"
794804
795805instance Monoid
796− (Expression relations grouping params (nullity 'PGtext)) where
806+ (Expression schema relations grouping params (nullity 'PGtext)) where
797807 mempty = fromString ""
798808 mappend = (<>)
799809
800810-- | >>> renderExpression $ lower "ARRRGGG"
801811-- "lower(E'ARRRGGG')"
802812lower
803− :: Expression relations grouping params (nullity 'PGtext)
813+ :: Expression schema relations grouping params (nullity 'PGtext)
804814 -- ^ string to lower case
805− -> Expression relations grouping params (nullity 'PGtext)
815+ -> Expression schema relations grouping params (nullity 'PGtext)
806816lower = unsafeFunction "lower"
807817
808818-- | >>> renderExpression $ upper "eeee"
809819-- "upper(E'eeee')"
810820upper
811− :: Expression relations grouping params (nullity 'PGtext)
821+ :: Expression schema relations grouping params (nullity 'PGtext)
812822 -- ^ string to upper case
813− -> Expression relations grouping params (nullity 'PGtext)
823+ -> Expression schema relations grouping params (nullity 'PGtext)
814824upper = unsafeFunction "upper"
815825
816826-- | >>> renderExpression $ charLength "four"
817827-- "char_length(E'four')"
818828charLength
819− :: Expression relations grouping params (nullity 'PGtext)
829+ :: Expression schema relations grouping params (nullity 'PGtext)
820830 -- ^ string to measure
821− -> Expression relations grouping params (nullity 'PGint4)
831+ -> Expression schema relations grouping params (nullity 'PGint4)
822832charLength = unsafeFunction "char_length"
823833
824834-- | The `like` expression returns true if the @string@ matches
@@ -831,11 +841,11 @@ charLength = unsafeFunction "char_length"
831841-- >>> renderExpression $ "abc" `like` "a%"
832842-- "(E'abc' LIKE E'a%')"
833843like
834− :: Expression relations grouping params (nullity 'PGtext)
844+ :: Expression schema relations grouping params (nullity 'PGtext)
835845 -- ^ string
836− -> Expression relations grouping params (nullity 'PGtext)
846+ -> Expression schema relations grouping params (nullity 'PGtext)
837847 -- ^ pattern
838− -> Expression relations grouping params (nullity 'PGbool)
848+ -> Expression schema relations grouping params (nullity 'PGbool)
839849like = unsafeBinaryOp "LIKE"
840850
841851{-----------------------------------------
@@ -845,16 +855,16 @@ aggregation
845855-- | escape hatch to define aggregate functions
846856unsafeAggregate
847857 :: ByteString -- ^ aggregate function
848− -> Expression relations 'Ungrouped params (xty)
849− -> Expression relations ('Grouped bys) params (yty)
858+ -> Expression schema relations 'Ungrouped params (xty)
859+ -> Expression schema relations ('Grouped bys) params (yty)
850860unsafeAggregate fun x = UnsafeExpression $ mconcat
851861 [fun, "(", renderExpression x, ")"]
852862
853863-- | escape hatch to define aggregate functions over distinct values
854864unsafeAggregateDistinct
855865 :: ByteString -- ^ aggregate function
856− -> Expression relations 'Ungrouped params (xty)
857− -> Expression relations ('Grouped bys) params (yty)
866+ -> Expression schema relations 'Ungrouped params (xty)
867+ -> Expression schema relations ('Grouped bys) params (yty)
858868unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
859869 [fun, "(DISTINCT ", renderExpression x, ")"]
860870
@@ -867,9 +877,9 @@ unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
867877-- "sum(\"col\")"
868878sum_
869879 :: PGNum ty
870− => Expression relations 'Ungrouped params (nullity ty)
880+ => Expression schema relations 'Ungrouped params (nullity ty)
871881 -- ^ what to sum
872− -> Expression relations ('Grouped bys) params (nullity ty)
882+ -> Expression schema relations ('Grouped bys) params (nullity ty)
873883sum_ = unsafeAggregate "sum"
874884
875885-- | >>> :{
@@ -881,18 +891,18 @@ sum_ = unsafeAggregate "sum"
881891-- "sum(DISTINCT \"col\")"
882892sumDistinct
883893 :: PGNum ty
884− => Expression relations 'Ungrouped params (nullity ty)
894+ => Expression schema relations 'Ungrouped params (nullity ty)
885895 -- ^ what to sum
886− -> Expression relations ('Grouped bys) params (nullity ty)
896+ -> Expression schema relations ('Grouped bys) params (nullity ty)
887897sumDistinct = unsafeAggregateDistinct "sum"
888898
889899-- | A constraint for `PGType`s that you can take averages of and the resulting
890900-- `PGType`.
891901class PGAvg ty avg | ty -> avg where
892902 avg, avgDistinct
893− :: Expression relations 'Ungrouped params (nullity ty)
903+ :: Expression schema relations 'Ungrouped params (nullity ty)
894904 -- ^ what to average
895− -> Expression relations ('Grouped bys) params (nullity avg)
905+ -> Expression schema relations ('Grouped bys) params (nullity avg)
896906 avg = unsafeAggregate "avg"
897907 avgDistinct = unsafeAggregateDistinct "avg"
898908instance PGAvg 'PGint2 'PGnumeric
@@ -912,9 +922,9 @@ instance PGAvg 'PGinterval 'PGinterval
912922-- "bit_and(\"col\")"
913923bitAnd
914924 :: PGIntegral int
915− => Expression relations 'Ungrouped params (nullity int)
925+ => Expression schema relations 'Ungrouped params (nullity int)
916926 -- ^ what to aggregate
917− -> Expression relations ('Grouped bys) params (nullity int)
927+ -> Expression schema relations ('Grouped bys) params (nullity int)
918928bitAnd = unsafeAggregate "bit_and"
919929
920930-- | >>> :{
@@ -926,9 +936,9 @@ bitAnd = unsafeAggregate "bit_and"
926936-- "bit_or(\"col\")"
927937bitOr
928938 :: PGIntegral int
929− => Expression relations 'Ungrouped params (nullity int)
939+ => Expression schema relations 'Ungrouped params (nullity int)
930940 -- ^ what to aggregate
931− -> Expression relations ('Grouped bys) params (nullity int)
941+ -> Expression schema relations ('Grouped bys) params (nullity int)
932942bitOr = unsafeAggregate "bit_or"
933943
934944-- | >>> :{
@@ -940,9 +950,9 @@ bitOr = unsafeAggregate "bit_or"
940950-- "bit_and(DISTINCT \"col\")"
941951bitAndDistinct
942952 :: PGIntegral int
943− => Expression relations 'Ungrouped params (nullity int)
953+ => Expression schema relations 'Ungrouped params (nullity int)
944954 -- ^ what to aggregate
945− -> Expression relations ('Grouped bys) params (nullity int)
955+ -> Expression schema relations ('Grouped bys) params (nullity int)
946956bitAndDistinct = unsafeAggregateDistinct "bit_and"
947957
948958-- | >>> :{
@@ -954,9 +964,9 @@ bitAndDistinct = unsafeAggregateDistinct "bit_and"
954964-- "bit_or(DISTINCT \"col\")"
955965bitOrDistinct
956966 :: PGIntegral int
957− => Expression relations 'Ungrouped params (nullity int)
967+ => Expression schema relations 'Ungrouped params (nullity int)
958968 -- ^ what to aggregate
959− -> Expression relations ('Grouped bys) params (nullity int)
969+ -> Expression schema relations ('Grouped bys) params (nullity int)
960970bitOrDistinct = unsafeAggregateDistinct "bit_or"
961971
962972-- | >>> :{
@@ -967,9 +977,9 @@ bitOrDistinct = unsafeAggregateDistinct "bit_or"
967977-- :}
968978-- "bool_and(\"col\")"
969979boolAnd
970− :: Expression relations 'Ungrouped params (nullity 'PGbool)
980+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
971981 -- ^ what to aggregate
972− -> Expression relations ('Grouped bys) params (nullity 'PGbool)
982+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
973983boolAnd = unsafeAggregate "bool_and"
974984
975985-- | >>> :{
@@ -980,9 +990,9 @@ boolAnd = unsafeAggregate "bool_and"
980990-- :}
981991-- "bool_or(\"col\")"
982992boolOr
983− :: Expression relations 'Ungrouped params (nullity 'PGbool)
993+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
984994 -- ^ what to aggregate
985− -> Expression relations ('Grouped bys) params (nullity 'PGbool)
995+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
986996boolOr = unsafeAggregate "bool_or"
987997
988998-- | >>> :{
@@ -993,9 +1003,9 @@ boolOr = unsafeAggregate "bool_or"
9931003-- :}
9941004-- "bool_and(DISTINCT \"col\")"
9951005boolAndDistinct
996− :: Expression relations 'Ungrouped params (nullity 'PGbool)
1006+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
9971007 -- ^ what to aggregate
998− -> Expression relations ('Grouped bys) params (nullity 'PGbool)
1008+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
9991009boolAndDistinct = unsafeAggregateDistinct "bool_and"
10001010
10011011-- | >>> :{
@@ -1006,9 +1016,9 @@ boolAndDistinct = unsafeAggregateDistinct "bool_and"
10061016-- :}
10071017-- "bool_or(DISTINCT \"col\")"
10081018boolOrDistinct
1009− :: Expression relations 'Ungrouped params (nullity 'PGbool)
1019+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
10101020 -- ^ what to aggregate
1011− -> Expression relations ('Grouped bys) params (nullity 'PGbool)
1021+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
10121022boolOrDistinct = unsafeAggregateDistinct "bool_or"
10131023
10141024-- | A special aggregation that does not require an input
@@ -1016,7 +1026,7 @@ boolOrDistinct = unsafeAggregateDistinct "bool_or"
10161026-- >>> renderExpression countStar
10171027-- "count(*)"
10181028countStar
1019− :: Expression relations ('Grouped bys) params ('NotNull 'PGint8)
1029+ :: Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
10201030countStar = UnsafeExpression $ "count(*)"
10211031
10221032-- | >>> :{
@@ -1027,9 +1037,9 @@ countStar = UnsafeExpression $ "count(*)"
10271037-- :}
10281038-- "count(\"col\")"
10291039count
1030− :: Expression relations 'Ungrouped params ty
1040+ :: Expression schema relations 'Ungrouped params ty
10311041 -- ^ what to count
1032− -> Expression relations ('Grouped bys) params ('NotNull 'PGint8)
1042+ -> Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
10331043count = unsafeAggregate "count"
10341044
10351045-- | >>> :{
@@ -1040,9 +1050,9 @@ count = unsafeAggregate "count"
10401050-- :}
10411051-- "count(DISTINCT \"col\")"
10421052countDistinct
1043− :: Expression relations 'Ungrouped params ty
1053+ :: Expression schema relations 'Ungrouped params ty
10441054 -- ^ what to count
1045− -> Expression relations ('Grouped bys) params ('NotNull 'PGint8)
1055+ -> Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
10461056countDistinct = unsafeAggregateDistinct "count"
10471057
10481058-- | synonym for `boolAnd`
@@ -1055,9 +1065,9 @@ countDistinct = unsafeAggregateDistinct "count"
10551065-- :}
10561066-- "every(\"col\")"
10571067every
1058− :: Expression relations 'Ungrouped params (nullity 'PGbool)
1068+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
10591069 -- ^ what to aggregate
1060− -> Expression relations ('Grouped bys) params (nullity 'PGbool)
1070+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
10611071every = unsafeAggregate "every"
10621072
10631073-- | synonym for `boolAndDistinct`
@@ -1070,16 +1080,16 @@ every = unsafeAggregate "every"
10701080-- :}
10711081-- "every(DISTINCT \"col\")"
10721082everyDistinct
1073− :: Expression relations 'Ungrouped params (nullity 'PGbool)
1083+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
10741084 -- ^ what to aggregate
1075− -> Expression relations ('Grouped bys) params (nullity 'PGbool)
1085+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
10761086everyDistinct = unsafeAggregateDistinct "every"
10771087
10781088-- | minimum and maximum aggregation
10791089max_, min_, maxDistinct, minDistinct
1080− :: Expression relations 'Ungrouped params (nullity ty)
1090+ :: Expression schema relations 'Ungrouped params (nullity ty)
10811091 -- ^ what to aggregate
1082− -> Expression relations ('Grouped bys) params (nullity ty)
1092+ -> Expression schema relations ('Grouped bys) params (nullity ty)
10831093max_ = unsafeAggregate "max"
10841094min_ = unsafeAggregate "min"
10851095maxDistinct = unsafeAggregateDistinct "max"
@@ -1090,92 +1100,96 @@ type expressions
10901100-----------------------------------------}
10911101
10921102-- | `TypeExpression`s are used in `cast`s and `createTable` commands.
1093−newtype TypeExpression (ty :: PGType)
1103+newtype TypeExpression (schema :: SchemaType) (ty :: PGType)
10941104 = UnsafeTypeExpression { renderTypeExpression :: ByteString }
10951105 deriving (GHC.Generic,Show,Eq,Ord,NFData)
10961106
1107+instance (Has alias schema ('Typedef ty))
1108+ => IsLabel alias (TypeExpression schema ty) where
1109+ fromLabel = UnsafeTypeExpression (renderAlias (fromLabel @alias))
1110+
10971111-- | logical Boolean (true/false)
1098−bool :: TypeExpression 'PGbool
1112+bool :: TypeExpression schema 'PGbool
10991113bool = UnsafeTypeExpression "bool"
11001114-- | signed two-byte integer
1101−int2, smallint :: TypeExpression 'PGint2
1115+int2, smallint :: TypeExpression schema 'PGint2
11021116int2 = UnsafeTypeExpression "int2"
11031117smallint = UnsafeTypeExpression "smallint"
11041118-- | signed four-byte integer
1105−int4, int, integer :: TypeExpression 'PGint4
1119+int4, int, integer :: TypeExpression schema 'PGint4
11061120int4 = UnsafeTypeExpression "int4"
11071121int = UnsafeTypeExpression "int"
11081122integer = UnsafeTypeExpression "integer"
11091123-- | signed eight-byte integer
1110−int8, bigint :: TypeExpression 'PGint8
1124+int8, bigint :: TypeExpression schema 'PGint8
11111125int8 = UnsafeTypeExpression "int8"
11121126bigint = UnsafeTypeExpression "bigint"
11131127-- | arbitrary precision numeric type
1114−numeric :: TypeExpression 'PGnumeric
1128+numeric :: TypeExpression schema 'PGnumeric
11151129numeric = UnsafeTypeExpression "numeric"
11161130-- | single precision floating-point number (4 bytes)
1117−float4, real :: TypeExpression 'PGfloat4
1131+float4, real :: TypeExpression schema 'PGfloat4
11181132float4 = UnsafeTypeExpression "float4"
11191133real = UnsafeTypeExpression "real"
11201134-- | double precision floating-point number (8 bytes)
1121−float8, doublePrecision :: TypeExpression 'PGfloat8
1135+float8, doublePrecision :: TypeExpression schema 'PGfloat8
11221136float8 = UnsafeTypeExpression "float8"
11231137doublePrecision = UnsafeTypeExpression "double precision"
11241138-- | variable-length character string
1125−text :: TypeExpression 'PGtext
1139+text :: TypeExpression schema 'PGtext
11261140text = UnsafeTypeExpression "text"
11271141-- | fixed-length character string
11281142char, character
11291143 :: (KnownNat n, 1 <= n)
11301144 => proxy n
1131− -> TypeExpression ('PGchar n)
1145+ -> TypeExpression schema ('PGchar n)
11321146char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
11331147character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
11341148-- | variable-length character string
11351149varchar, characterVarying
11361150 :: (KnownNat n, 1 <= n)
11371151 => proxy n
1138− -> TypeExpression ('PGvarchar n)
1152+ -> TypeExpression schema ('PGvarchar n)
11391153varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
11401154characterVarying p = UnsafeTypeExpression $
11411155 "character varying(" <> renderNat p <> ")"
11421156-- | binary data ("byte array")
1143−bytea :: TypeExpression 'PGbytea
1157+bytea :: TypeExpression schema 'PGbytea
11441158bytea = UnsafeTypeExpression "bytea"
11451159-- | date and time (no time zone)
1146−timestamp :: TypeExpression 'PGtimestamp
1160+timestamp :: TypeExpression schema 'PGtimestamp
11471161timestamp = UnsafeTypeExpression "timestamp"
11481162-- | date and time, including time zone
1149−timestampWithTimeZone :: TypeExpression 'PGtimestamptz
1163+timestampWithTimeZone :: TypeExpression schema 'PGtimestamptz
11501164timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
11511165-- | calendar date (year, month, day)
1152−date :: TypeExpression 'PGdate
1166+date :: TypeExpression schema 'PGdate
11531167date = UnsafeTypeExpression "date"
11541168-- | time of day (no time zone)
1155−time :: TypeExpression 'PGtime
1169+time :: TypeExpression schema 'PGtime
11561170time = UnsafeTypeExpression "time"
11571171-- | time of day, including time zone
1158−timeWithTimeZone :: TypeExpression 'PGtimetz
1172+timeWithTimeZone :: TypeExpression schema 'PGtimetz
11591173timeWithTimeZone = UnsafeTypeExpression "time with time zone"
11601174-- | time span
1161−interval :: TypeExpression 'PGinterval
1175+interval :: TypeExpression schema 'PGinterval
11621176interval = UnsafeTypeExpression "interval"
11631177-- | universally unique identifier
1164−uuid :: TypeExpression 'PGuuid
1178+uuid :: TypeExpression schema 'PGuuid
11651179uuid = UnsafeTypeExpression "uuid"
11661180-- | IPv4 or IPv6 host address
1167−inet :: TypeExpression 'PGinet
1181+inet :: TypeExpression schema 'PGinet
11681182inet = UnsafeTypeExpression "inet"
11691183-- | textual JSON data
1170−json :: TypeExpression 'PGjson
1184+json :: TypeExpression schema 'PGjson
11711185json = UnsafeTypeExpression "json"
11721186-- | binary JSON data, decomposed
1173−jsonb :: TypeExpression 'PGjsonb
1187+jsonb :: TypeExpression schema 'PGjsonb
11741188jsonb = UnsafeTypeExpression "jsonb"
11751189-- | variable length array
11761190vararray
1177− :: TypeExpression pg
1178− -> TypeExpression ('PGvararray pg)
1191+ :: TypeExpression schema pg
1192+ -> TypeExpression schema ('PGvararray pg)
11791193vararray ty = UnsafeTypeExpression $ renderTypeExpression ty <> "[]"
11801194-- | fixed length array
11811195--
@@ -1184,36 +1198,36 @@ vararray ty = UnsafeTypeExpression $ renderTypeExpression ty <> "[]"
11841198fixarray
11851199 :: KnownNat n
11861200 => proxy n
1187− -> TypeExpression pg
1188− -> TypeExpression ('PGfixarray n pg)
1201+ -> TypeExpression schema pg
1202+ -> TypeExpression schema ('PGfixarray n pg)
11891203fixarray p ty = UnsafeTypeExpression $
11901204 renderTypeExpression ty <> "[" <> renderNat p <> "]"
11911205
11921206-- | `pgtype` is a demoted version of a `PGType`
1193−class PGTyped (ty :: PGType) where pgtype :: TypeExpression ty
1194−instance PGTyped 'PGbool where pgtype = bool
1195−instance PGTyped 'PGint2 where pgtype = int2
1196−instance PGTyped 'PGint4 where pgtype = int4
1197−instance PGTyped 'PGint8 where pgtype = int8
1198−instance PGTyped 'PGnumeric where pgtype = numeric
1199−instance PGTyped 'PGfloat4 where pgtype = float4
1200−instance PGTyped 'PGfloat8 where pgtype = float8
1201−instance PGTyped 'PGtext where pgtype = text
1207+class PGTyped schema (ty :: PGType) where pgtype :: TypeExpression schema ty
1208+instance PGTyped schema 'PGbool where pgtype = bool
1209+instance PGTyped schema 'PGint2 where pgtype = int2
1210+instance PGTyped schema 'PGint4 where pgtype = int4
1211+instance PGTyped schema 'PGint8 where pgtype = int8
1212+instance PGTyped schema 'PGnumeric where pgtype = numeric
1213+instance PGTyped schema 'PGfloat4 where pgtype = float4
1214+instance PGTyped schema 'PGfloat8 where pgtype = float8
1215+instance PGTyped schema 'PGtext where pgtype = text
12021216instance (KnownNat n, 1 <= n)
1203− => PGTyped ('PGchar n) where pgtype = char (Proxy @n)
1217+ => PGTyped schema ('PGchar n) where pgtype = char (Proxy @n)
12041218instance (KnownNat n, 1 <= n)
1205− => PGTyped ('PGvarchar n) where pgtype = varchar (Proxy @n)
1206−instance PGTyped 'PGbytea where pgtype = bytea
1207−instance PGTyped 'PGtimestamp where pgtype = timestamp
1208−instance PGTyped 'PGtimestamptz where pgtype = timestampWithTimeZone
1209−instance PGTyped 'PGdate where pgtype = date
1210−instance PGTyped 'PGtime where pgtype = time
1211−instance PGTyped 'PGtimetz where pgtype = timeWithTimeZone
1212−instance PGTyped 'PGinterval where pgtype = interval
1213−instance PGTyped 'PGuuid where pgtype = uuid
1214−instance PGTyped 'PGjson where pgtype = json
1215−instance PGTyped 'PGjsonb where pgtype = jsonb
1216−instance PGTyped ty => PGTyped ('PGvararray ty) where
1217− pgtype = vararray (pgtype @ty)
1218−instance (KnownNat n, PGTyped ty) => PGTyped ('PGfixarray n ty) where
1219− pgtype = fixarray (Proxy @n) (pgtype @ty)
1219+ => PGTyped schema ('PGvarchar n) where pgtype = varchar (Proxy @n)
1220+instance PGTyped schema 'PGbytea where pgtype = bytea
1221+instance PGTyped schema 'PGtimestamp where pgtype = timestamp
1222+instance PGTyped schema 'PGtimestamptz where pgtype = timestampWithTimeZone
1223+instance PGTyped schema 'PGdate where pgtype = date
1224+instance PGTyped schema 'PGtime where pgtype = time
1225+instance PGTyped schema 'PGtimetz where pgtype = timeWithTimeZone
1226+instance PGTyped schema 'PGinterval where pgtype = interval
1227+instance PGTyped schema 'PGuuid where pgtype = uuid
1228+instance PGTyped schema 'PGjson where pgtype = json
1229+instance PGTyped schema 'PGjsonb where pgtype = jsonb
1230+instance PGTyped schema ty => PGTyped schema ('PGvararray ty) where
1231+ pgtype = vararray (pgtype @schema @ty)
1232+instance (KnownNat n, PGTyped schema ty) => PGTyped schema ('PGfixarray n ty) where
1233+ pgtype = fixarray (Proxy @n) (pgtype @schema @ty)