@@ -37,13 +37,6 @@ module Squeal.PostgreSQL.Expression
37 ( -- * Expression
37 ( -- * Expression
38 Expression (UnsafeExpression, renderExpression)
38 Expression (UnsafeExpression, renderExpression)
39 , HasParameter (param)
39 , HasParameter (param)
40− , HasColumn (getColumn)
43− , GroupedBy (getGroup1, getGroup2)
47 -- ** Null
40 -- ** Null
@@ -53,7 +46,9 @@ module Squeal.PostgreSQL.Expression
53 , isn'tNull
46 , isn'tNull
54 , matchNull
47 , matchNull
56− -- ** Functions
51+ -- ** Functions
57 , unsafeBinaryOp
52 , unsafeBinaryOp
58 , unsafeUnaryOp
53 , unsafeUnaryOp
59 , unsafeFunction
54 , unsafeFunction
@@ -104,8 +99,7 @@ module Squeal.PostgreSQL.Expression
104 , max_, maxDistinct, min_, minDistinct
99 , max_, maxDistinct, min_, minDistinct
105 -- * Tables
100 -- * Tables
106 , Table (UnsafeTable, renderTable)
101 , Table (UnsafeTable, renderTable)
107− , HasTable (getTable)
102+ -- * Types
109 , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
103 , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
110 , PGTyped (pgtype)
104 , PGTyped (pgtype)
@@ -143,6 +137,8 @@ module Squeal.PostgreSQL.Expression
146 , notNull
142 , notNull
147 , default_
143 , default_
148 -- * Re-export
144 -- * Re-export
@@ -164,7 +160,7 @@ import Prelude hiding (id, (.))
165import qualified GHC.Generics as GHC
161import qualified GHC.Generics as GHC
167−import Squeal.PostgreSQL.Prettyprint
163+import Squeal.PostgreSQL.Render
168import Squeal.PostgreSQL.Schema
164import Squeal.PostgreSQL.Schema
170{-----------------------------------------
166{-----------------------------------------
@@ -181,10 +177,10 @@ values from primitive expression using arithmetic, logical,
181and other operations.
177and other operations.
183newtype Expression
179newtype Expression
184− (tables :: TablesType)
180+ (relations :: RelationsType)
185 (grouping :: Grouping)
181 (grouping :: Grouping)
186− (params :: [ColumnType])
182+ (params :: [NullityType])
187− (ty :: ColumnType)
183+ (ty :: NullityType)
188 = UnsafeExpression { renderExpression :: ByteString }
184 = UnsafeExpression { renderExpression :: ByteString }
189 deriving (GHC.Generic,Show,Eq,Ord,NFData)
185 deriving (GHC.Generic,Show,Eq,Ord,NFData)
@@ -196,123 +192,51 @@ supplied externally to a SQL statement.
196separately from the SQL command string, in which case `param`s are used to
192separately from the SQL command string, in which case `param`s are used to
197refer to the out-of-line data values.
193refer to the out-of-line data values.
199−class (PGTyped (BaseType ty), KnownNat n)
195+class (PGTyped (PGTypeOf ty), KnownNat n) => HasParameter
197+ (params :: [NullityType])
200− => HasParameter (n :: Nat) params ty | n params -> ty where
199+ | n params -> ty where
201− param :: Expression tables grouping params ty
200+ param :: Expression relations grouping params ty
202 param = UnsafeExpression $ parenthesized $
201 param = UnsafeExpression $ parenthesized $
203 "$" <> renderNat (Proxy @n) <+> "::"
202 "$" <> renderNat (Proxy @n) <+> "::"
204− <+> renderTypeExpression (pgtype @(BaseType ty))
203+ <+> renderTypeExpression (pgtype @(PGTypeOf ty))
205−instance {-# OVERLAPPING #-} PGTyped (BaseType ty1)
204+instance {-# OVERLAPPING #-} PGTyped (PGTypeOf ty1)
206 => HasParameter 1 (ty1:tys) ty1
205 => HasParameter 1 (ty1:tys) ty1
207instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
206instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
208 => HasParameter n (ty' : params) ty
207 => HasParameter n (ty' : params) ty
210−{- | A `HasColumn` constraint indicates an unqualified column reference.
209+instance (HasUnique relation relations columns, Has column columns ty)
211−`getColumn` can only be unambiguous when the `TableExpression` the column
212−references is unique, in which case the column may be referenced using
213−@-XOverloadedLabels@. Otherwise, combined with a `HasTable` constraint, the
214−qualified column reference operator `!` may be used.
216−class KnownSymbol column => HasColumn column columns ty
217− | column columns -> ty where
219− :: HasUnique table tables columns
221− -> Expression tables 'Ungrouped params ty
222− getColumn column = UnsafeExpression $ renderAlias column
223−instance {-# OVERLAPPING #-} KnownSymbol column
224− => HasColumn column ((column ::: optionality ty) ': tys) ('Required ty)
225−instance {-# OVERLAPPABLE #-} (KnownSymbol column, HasColumn column table ty)
226− => HasColumn column (ty' ': table) ty
228−instance (HasColumn column columns ty, HasUnique table tables columns)
229− => IsLabel column (Expression tables 'Ungrouped params ty) where
210+ => IsLabel column (Expression relations 'Ungrouped params ty) where
230− fromLabel = getColumn (Alias @column)
211+ fromLabel = UnsafeExpression $ renderAlias (Alias @column)
232−instance (HasTable table tables columns, HasColumn column columns ty)
213+instance (Has relation relations columns, Has column columns ty)
233− => IsTableColumn table column (Expression tables 'Ungrouped params ty) where
214+ => IsQualified relation column (Expression relations 'Ungrouped params ty) where
234− table ! column = UnsafeExpression $
215+ relation ! column = UnsafeExpression $
235− renderAlias table <> "." <> renderAlias column
216+ renderAlias relation <> "." <> renderAlias column
237−-- | A `Column` is a witness to a `HasColumn` constraint. It's used
238−-- in `Squeel.PostgreSQL.Definition.unique` and other
239−-- `Squeel.PostgreSQL.Definition.TableConstraint`s to witness a
240−-- subcolumns relationship.
242− (columns :: ColumnsType)
243− (columnty :: (Symbol,ColumnType))
246− :: HasColumn column columns ty
248− -> Column columns (column ::: ty)
249−deriving instance Show (Column columns columnty)
250−deriving instance Eq (Column columns columnty)
251−deriving instance Ord (Column columns columnty)
253−-- | Render a `Column`.
254−renderColumn :: Column columns columnty -> ByteString
255−renderColumn (Column column) = renderAlias column
257−{- | A `GroupedBy` constraint indicates that a table qualified column is
258−a member of the auxiliary namespace created by @GROUP BY@ clauses and thus,
259−may be called in an output `Expression` without aggregating.
261−class (KnownSymbol table, KnownSymbol column)
262− => GroupedBy table column bys where
264− :: (HasUnique table tables columns, HasColumn column columns ty)
266− -> Expression tables ('Grouped bys) params ty
267− getGroup1 column = UnsafeExpression $ renderAlias column
269− :: (HasTable table tables columns, HasColumn column columns ty)
272− -> Expression tables ('Grouped bys) params ty
273− getGroup2 table column = UnsafeExpression $
274− renderAlias table <> "." <> renderAlias column
275−instance {-# OVERLAPPING #-} (KnownSymbol table, KnownSymbol column)
276− => GroupedBy table column ('(table,column) ': bys)
277−instance {-# OVERLAPPABLE #-}
279− , KnownSymbol column
280− , GroupedBy table column bys
281− ) => GroupedBy table column (tabcol ': bys)
284− ( HasUnique table tables columns
219+ ( HasUnique relation relations columns
285− , HasColumn column columns ty
220+ , Has column columns ty
286− , GroupedBy table column bys
221+ , GroupedBy relation column bys
287 ) => IsLabel column
222 ) => IsLabel column
288− (Expression tables ('Grouped bys) params ty) where
223+ (Expression relations ('Grouped bys) params ty) where
289− fromLabel = getGroup1 (Alias @column)
224+ fromLabel = UnsafeExpression $ renderAlias (Alias @column)
292− ( HasTable table tables columns
227+ ( Has relation relations columns
293− , HasColumn column columns ty
228+ , Has column columns ty
294− , GroupedBy table column bys
229+ , GroupedBy relation column bys
295− ) => IsTableColumn table column
230+ ) => IsQualified relation column
296− (Expression tables ('Grouped bys) params ty) where (!) = getGroup2
231+ (Expression relations ('Grouped bys) params ty) where
298−-- | >>> renderExpression def
300−def :: Expression '[] 'Ungrouped params ('Optional (nullity ty))
301−def = UnsafeExpression "DEFAULT"
232+ relation ! column = UnsafeExpression $
302−
233+ renderAlias relation <> "." <> renderAlias column
303−-- | >>> renderExpression $ unDef false
306− :: Expression '[] 'Ungrouped params ('Required (nullity ty))
308− -> Expression '[] 'Ungrouped params ('Optional (nullity ty))
309−unDef = UnsafeExpression . renderExpression
311-- | analagous to `Nothing`
235-- | analagous to `Nothing`
313-- >>> renderExpression $ null_
237-- >>> renderExpression $ null_
315−null_ :: Expression tables grouping params (optionality ('Null ty))
239+null_ :: Expression relations grouping params ('Null ty)
316null_ = UnsafeExpression "NULL"
240null_ = UnsafeExpression "NULL"
318-- | analagous to `Just`
242-- | analagous to `Just`
@@ -320,9 +244,9 @@ null_ = UnsafeExpression "NULL"
320-- >>> renderExpression $ unNull true
244-- >>> renderExpression $ unNull true
323− :: Expression tables grouping params (optionality ('NotNull ty))
247+ :: Expression relations grouping params ('NotNull ty)
324 -- ^ not @NULL@
248 -- ^ not @NULL@
325− -> Expression tables grouping params (optionality ('Null ty))
249+ -> Expression relations grouping params ('Null ty)
326unNull = UnsafeExpression . renderExpression
250unNull = UnsafeExpression . renderExpression
328-- | return the leftmost value which is not NULL
252-- | return the leftmost value which is not NULL
@@ -330,40 +254,40 @@ unNull = UnsafeExpression . renderExpression
330-- >>> renderExpression $ coalesce [null_, unNull true] false
254-- >>> renderExpression $ coalesce [null_, unNull true] false
331-- "COALESCE(NULL, TRUE, FALSE)"
255-- "COALESCE(NULL, TRUE, FALSE)"
333− :: [Expression tables grouping params ('Required ('Null ty))]
257+ :: [Expression relations grouping params ('Null ty)]
334 -- ^ @NULL@s may be present
258 -- ^ @NULL@s may be present
335− -> Expression tables grouping params ('Required ('NotNull ty))
259+ -> Expression relations grouping params ('NotNull ty)
336 -- ^ @NULL@ is absent
260 -- ^ @NULL@ is absent
337− -> Expression tables grouping params ('Required ('NotNull ty))
261+ -> Expression relations grouping params ('NotNull ty)
338coalesce nullxs notNullx = UnsafeExpression $
262coalesce nullxs notNullx = UnsafeExpression $
339 "COALESCE" <> parenthesized (commaSeparated
263 "COALESCE" <> parenthesized (commaSeparated
340 ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
264 ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
342−-- | analagous to `fromMaybe` using @COALESCE@
266+-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
344-- >>> renderExpression $ fromNull true null_
268-- >>> renderExpression $ fromNull true null_
345-- "COALESCE(NULL, TRUE)"
269-- "COALESCE(NULL, TRUE)"
347− :: Expression tables grouping params ('Required ('NotNull ty))
271+ :: Expression relations grouping params ('NotNull ty)
348 -- ^ what to convert @NULL@ to
272 -- ^ what to convert @NULL@ to
349− -> Expression tables grouping params ('Required ('Null ty))
273+ -> Expression relations grouping params ('Null ty)
350− -> Expression tables grouping params ('Required ('NotNull ty))
274+ -> Expression relations grouping params ('NotNull ty)
351fromNull notNullx nullx = coalesce [nullx] notNullx
275fromNull notNullx nullx = coalesce [nullx] notNullx
353-- | >>> renderExpression $ null_ & isNull
277-- | >>> renderExpression $ null_ & isNull
354-- "NULL IS NULL"
278-- "NULL IS NULL"
356− :: Expression tables grouping params ('Required ('Null ty))
280+ :: Expression relations grouping params ('Null ty)
357 -- ^ possibly @NULL@
281 -- ^ possibly @NULL@
358− -> Condition tables grouping params
282+ -> Condition relations grouping params
359isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
283isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
361-- | >>> renderExpression $ null_ & isn'tNull
285-- | >>> renderExpression $ null_ & isn'tNull
362-- "NULL IS NOT NULL"
286-- "NULL IS NOT NULL"
364− :: Expression tables grouping params ('Required ('Null ty))
288+ :: Expression relations grouping params ('Null ty)
365 -- ^ possibly @NULL@
289 -- ^ possibly @NULL@
366− -> Condition tables grouping params
290+ -> Condition relations grouping params
367isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
291isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
369-- | analagous to `maybe` using @IS NULL@
293-- | analagous to `maybe` using @IS NULL@
@@ -371,13 +295,13 @@ isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
371-- >>> renderExpression $ matchNull true not_ null_
295-- >>> renderExpression $ matchNull true not_ null_
372-- "CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END"
296-- "CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END"
374− :: Expression tables grouping params ('Required nullty)
298+ :: Expression relations grouping params (nullty)
375 -- ^ what to convert @NULL@ to
299 -- ^ what to convert @NULL@ to
376− -> ( Expression tables grouping params ('Required ('NotNull ty))
300+ -> ( Expression relations grouping params ('NotNull ty)
377− -> Expression tables grouping params ('Required nullty) )
301+ -> Expression relations grouping params (nullty) )
378 -- ^ function to perform when @NULL@ is absent
302 -- ^ function to perform when @NULL@ is absent
379− -> Expression tables grouping params ('Required ('Null ty))
303+ -> Expression relations grouping params ('Null ty)
380− -> Expression tables grouping params ('Required nullty)
304+ -> Expression relations grouping params (nullty)
381matchNull y f x = ifThenElse (isNull x) y
305matchNull y f x = ifThenElse (isNull x) y
382 (f (UnsafeExpression (renderExpression x)))
306 (f (UnsafeExpression (renderExpression x)))
@@ -388,33 +312,47 @@ matchNull y f x = ifThenElse (isNull x) y
388-- >>> renderExpression @_ @_ @'[_] $ fromNull false (nullIf false (param @1))
312-- >>> renderExpression @_ @_ @'[_] $ fromNull false (nullIf false (param @1))
389-- "COALESCE(NULL IF (FALSE, ($1 :: bool)), FALSE)"
313-- "COALESCE(NULL IF (FALSE, ($1 :: bool)), FALSE)"
391− :: Expression tables grouping params ('Required ('NotNull ty))
315+ :: Expression relations grouping params ('NotNull ty)
392 -- ^ @NULL@ is absent
316 -- ^ @NULL@ is absent
393− -> Expression tables grouping params ('Required ('NotNull ty))
317+ -> Expression relations grouping params ('NotNull ty)
394 -- ^ @NULL@ is absent
318 -- ^ @NULL@ is absent
395− -> Expression tables grouping params ('Required ('Null ty))
319+ -> Expression relations grouping params ('Null ty)
396nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
320nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
397 (renderExpression x <> ", " <> renderExpression y)
321 (renderExpression x <> ", " <> renderExpression y)
323+-- | >>> renderExpression $ array [null_, unNull false, unNull true]
324+-- "ARRAY[NULL, FALSE, TRUE]"
326+ :: [Expression relations grouping params ('Null ty)]
328+ -> Expression relations grouping params (nullity ('PGvararray ty))
329+array xs = UnsafeExpression $
330+ "ARRAY[" <> commaSeparated (renderExpression <$> xs) <> "]"
333+ (Expression relations grouping params (nullity ('PGvararray ty))) where
335+ mappend = unsafeBinaryOp "||"
399-- | >>> renderExpression @_ @_ @'[_] $ greatest currentTimestamp [param @1]
337-- | >>> renderExpression @_ @_ @'[_] $ greatest currentTimestamp [param @1]
400-- "GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))"
338-- "GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))"
402− :: Expression tables grouping params ('Required nullty)
340+ :: Expression relations grouping params (nullty)
403 -- ^ needs at least 1 argument
341 -- ^ needs at least 1 argument
404− -> [Expression tables grouping params ('Required nullty)]
342+ -> [Expression relations grouping params (nullty)]
405 -- ^ or more
343 -- ^ or more
406− -> Expression tables grouping params ('Required nullty)
344+ -> Expression relations grouping params (nullty)
407greatest x xs = UnsafeExpression $ "GREATEST("
345greatest x xs = UnsafeExpression $ "GREATEST("
408 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
346 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
410-- | >>> renderExpression $ least currentTimestamp [null_]
348-- | >>> renderExpression $ least currentTimestamp [null_]
411-- "LEAST(CURRENT_TIMESTAMP, NULL)"
349-- "LEAST(CURRENT_TIMESTAMP, NULL)"
413− :: Expression tables grouping params ('Required nullty)
351+ :: Expression relations grouping params (nullty)
414 -- ^ needs at least 1 argument
352 -- ^ needs at least 1 argument
415− -> [Expression tables grouping params ('Required nullty)]
353+ -> [Expression relations grouping params (nullty)]
416 -- ^ or more
354 -- ^ or more
417− -> Expression tables grouping params ('Required nullty)
355+ -> Expression relations grouping params (nullty)
418least x xs = UnsafeExpression $ "LEAST("
356least x xs = UnsafeExpression $ "LEAST("
419 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
357 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
@@ -423,9 +361,9 @@ least x xs = UnsafeExpression $ "LEAST("
423unsafeBinaryOp
361unsafeBinaryOp
424 :: ByteString
362 :: ByteString
425 -- ^ operator
363 -- ^ operator
426− -> Expression tables grouping params ('Required ty0)
364+ -> Expression relations grouping params (ty0)
427− -> Expression tables grouping params ('Required ty1)
365+ -> Expression relations grouping params (ty1)
428− -> Expression tables grouping params ('Required ty2)
366+ -> Expression relations grouping params (ty2)
429unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
367unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
430 renderExpression x <+> op <+> renderExpression y
368 renderExpression x <+> op <+> renderExpression y
@@ -434,8 +372,8 @@ unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
434unsafeUnaryOp
372unsafeUnaryOp
435 :: ByteString
373 :: ByteString
436 -- ^ operator
374 -- ^ operator
437− -> Expression tables grouping params ('Required ty0)
375+ -> Expression relations grouping params (ty0)
438− -> Expression tables grouping params ('Required ty1)
376+ -> Expression relations grouping params (ty1)
439unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
377unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
440 op <+> renderExpression x
378 op <+> renderExpression x
@@ -444,13 +382,13 @@ unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
444unsafeFunction
382unsafeFunction
445 :: ByteString
383 :: ByteString
446 -- ^ function
384 -- ^ function
447− -> Expression tables grouping params ('Required xty)
385+ -> Expression relations grouping params (xty)
448− -> Expression tables grouping params ('Required yty)
386+ -> Expression relations grouping params (yty)
449unsafeFunction fun x = UnsafeExpression $
387unsafeFunction fun x = UnsafeExpression $
450 fun <> parenthesized (renderExpression x)
388 fun <> parenthesized (renderExpression x)
452instance PGNum ty
390instance PGNum ty
453− => Num (Expression tables grouping params ('Required (nullity ty))) where
391+ => Num (Expression relations grouping params (nullity ty)) where
454 (+) = unsafeBinaryOp "+"
392 (+) = unsafeBinaryOp "+"
455 (-) = unsafeBinaryOp "-"
393 (-) = unsafeBinaryOp "-"
456 (*) = unsafeBinaryOp "*"
394 (*) = unsafeBinaryOp "*"
@@ -462,12 +400,12 @@ instance PGNum ty
464instance (PGNum ty, PGFloating ty) => Fractional
402instance (PGNum ty, PGFloating ty) => Fractional
465− (Expression tables grouping params ('Required (nullity ty))) where
403+ (Expression relations grouping params (nullity ty)) where
466 (/) = unsafeBinaryOp "/"
404 (/) = unsafeBinaryOp "/"
467 fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
405 fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
469instance (PGNum ty, PGFloating ty) => Floating
407instance (PGNum ty, PGFloating ty) => Floating
470− (Expression tables grouping params ('Required (nullity ty))) where
408+ (Expression relations grouping params (nullity ty)) where
471 pi = UnsafeExpression "pi()"
409 pi = UnsafeExpression "pi()"
472 exp = unsafeFunction "exp"
410 exp = unsafeFunction "exp"
473 log = unsafeFunction "ln"
411 log = unsafeFunction "ln"
@@ -488,15 +426,20 @@ instance (PGNum ty, PGFloating ty) => Floating
488 acosh x = log (x + sqrt (x*x - 1))
426 acosh x = log (x + sqrt (x*x - 1))
489 atanh x = log ((1 + x) / (1 - x)) / 2
427 atanh x = log ((1 + x) / (1 - x)) / 2
491−-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGfloat4)) $ atan2_ pi 2
429+-- | >>> :{
431+-- expression :: Expression relations grouping params (nullity 'PGfloat4)
432+-- expression = atan2_ pi 2
433+-- in renderExpression expression
492-- "atan2(pi(), 2)"
435-- "atan2(pi(), 2)"
494 :: PGFloating float
437 :: PGFloating float
495− => Expression tables grouping params ('Required (nullity float))
438+ => Expression relations grouping params (nullity float)
496 -- ^ numerator
439 -- ^ numerator
497− -> Expression tables grouping params ('Required (nullity float))
440+ -> Expression relations grouping params (nullity float)
498 -- ^ denominator
441 -- ^ denominator
499− -> Expression tables grouping params ('Required (nullity float))
442+ -> Expression relations grouping params (nullity float)
500atan2_ y x = UnsafeExpression $
443atan2_ y x = UnsafeExpression $
501 "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
444 "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
@@ -507,115 +450,147 @@ atan2_ y x = UnsafeExpression $
507-- | >>> renderExpression $ true & cast int4
450-- | >>> renderExpression $ true & cast int4
508-- "(TRUE :: int4)"
451-- "(TRUE :: int4)"
510− :: TypeExpression ('Required ('Null ty1))
453+ :: TypeExpression ('NoDef :=> 'Null ty1)
511 -- ^ type to cast as
454 -- ^ type to cast as
512− -> Expression tables grouping params ('Required (nullity ty0))
455+ -> Expression relations grouping params (nullity ty0)
513 -- ^ value to convert
456 -- ^ value to convert
514− -> Expression tables grouping params ('Required (nullity ty1))
457+ -> Expression relations grouping params (nullity ty1)
515cast ty x = UnsafeExpression $ parenthesized $
458cast ty x = UnsafeExpression $ parenthesized $
516 renderExpression x <+> "::" <+> renderTypeExpression ty
459 renderExpression x <+> "::" <+> renderTypeExpression ty
518-- | integer division, truncates the result
461-- | integer division, truncates the result
520−-- >>> renderExpression @_ @_ @_ @(_(_ 'PGint2)) $ 5 `quot_` 2
463+-- >>> :{
465+-- expression :: Expression relations grouping params (nullity 'PGint2)
466+-- expression = 5 `quot_` 2
467+-- in renderExpression expression
521-- "(5 / 2)"
469-- "(5 / 2)"
523 :: PGIntegral int
471 :: PGIntegral int
524− => Expression tables grouping params ('Required (nullity int))
472+ => Expression relations grouping params (nullity int)
525 -- ^ numerator
473 -- ^ numerator
526− -> Expression tables grouping params ('Required (nullity int))
474+ -> Expression relations grouping params (nullity int)
527 -- ^ denominator
475 -- ^ denominator
528− -> Expression tables grouping params ('Required (nullity int))
476+ -> Expression relations grouping params (nullity int)
529quot_ = unsafeBinaryOp "/"
477quot_ = unsafeBinaryOp "/"
531-- | remainder upon integer division
479-- | remainder upon integer division
533−-- >>> renderExpression @_ @_ @_ @(_ (_ 'PGint2)) $ 5 `rem_` 2
481+-- >>> :{
483+-- expression :: Expression relations grouping params (nullity 'PGint2)
484+-- expression = 5 `rem_` 2
485+-- in renderExpression expression
534-- "(5 % 2)"
487-- "(5 % 2)"
536 :: PGIntegral int
489 :: PGIntegral int
537− => Expression tables grouping params ('Required (nullity int))
490+ => Expression relations grouping params (nullity int)
538 -- ^ numerator
491 -- ^ numerator
539− -> Expression tables grouping params ('Required (nullity int))
492+ -> Expression relations grouping params (nullity int)
540 -- ^ denominator
493 -- ^ denominator
541− -> Expression tables grouping params ('Required (nullity int))
494+ -> Expression relations grouping params (nullity int)
542rem_ = unsafeBinaryOp "%"
495rem_ = unsafeBinaryOp "%"
544−-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGfloat4)) $ trunc pi
497+-- | >>> :{
499+-- expression :: Expression relations grouping params (nullity 'PGfloat4)
500+-- expression = trunc pi
501+-- in renderExpression expression
545-- "trunc(pi())"
503-- "trunc(pi())"
547 :: PGFloating frac
505 :: PGFloating frac
548− => Expression tables grouping params ('Required (nullity frac))
506+ => Expression relations grouping params (nullity frac)
549 -- ^ fractional number
507 -- ^ fractional number
550− -> Expression tables grouping params ('Required (nullity frac))
508+ -> Expression relations grouping params (nullity frac)
551trunc = unsafeFunction "trunc"
509trunc = unsafeFunction "trunc"
553−-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGfloat4)) $ round_ pi
511+-- | >>> :{
513+-- expression :: Expression relations grouping params (nullity 'PGfloat4)
514+-- expression = round_ pi
515+-- in renderExpression expression
554-- "round(pi())"
517-- "round(pi())"
556 :: PGFloating frac
519 :: PGFloating frac
557− => Expression tables grouping params ('Required (nullity frac))
520+ => Expression relations grouping params (nullity frac)
558 -- ^ fractional number
521 -- ^ fractional number
559− -> Expression tables grouping params ('Required (nullity frac))
522+ -> Expression relations grouping params (nullity frac)
560round_ = unsafeFunction "round"
523round_ = unsafeFunction "round"
562−-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGfloat4)) $ ceiling_ pi
525+-- | >>> :{
527+-- expression :: Expression relations grouping params (nullity 'PGfloat4)
528+-- expression = ceiling_ pi
529+-- in renderExpression expression
563-- "ceiling(pi())"
531-- "ceiling(pi())"
565 :: PGFloating frac
533 :: PGFloating frac
566− => Expression tables grouping params ('Required (nullity frac))
534+ => Expression relations grouping params (nullity frac)
567 -- ^ fractional number
535 -- ^ fractional number
568− -> Expression tables grouping params ('Required (nullity frac))
536+ -> Expression relations grouping params (nullity frac)
569ceiling_ = unsafeFunction "ceiling"
537ceiling_ = unsafeFunction "ceiling"
571-- | A `Condition` is a boolean valued `Expression`. While SQL allows
539-- | A `Condition` is a boolean valued `Expression`. While SQL allows
572−-- conditions to have @NULL@, squeal instead chooses to disallow @NULL@,
540+-- conditions to have @NULL@, Squeal instead chooses to disallow @NULL@,
573-- forcing one to handle the case of @NULL@ explicitly to produce
541-- forcing one to handle the case of @NULL@ explicitly to produce
574-- a `Condition`.
542-- a `Condition`.
575−type Condition tables grouping params =
543+type Condition relations grouping params =
576− Expression tables grouping params ('Required ('NotNull 'PGbool))
544+ Expression relations grouping params ('NotNull 'PGbool)
578-- | >>> renderExpression true
546-- | >>> renderExpression true
580−true :: Condition tables grouping params
548+true :: Condition relations grouping params
581true = UnsafeExpression "TRUE"
549true = UnsafeExpression "TRUE"
583-- | >>> renderExpression false
551-- | >>> renderExpression false
584-- "FALSE"
552-- "FALSE"
585−false :: Condition tables grouping params
553+false :: Condition relations grouping params
586false = UnsafeExpression "FALSE"
554false = UnsafeExpression "FALSE"
588-- | >>> renderExpression $ not_ true
556-- | >>> renderExpression $ not_ true
589-- "(NOT TRUE)"
557-- "(NOT TRUE)"
591− :: Condition tables grouping params
559+ :: Condition relations grouping params
592− -> Condition tables grouping params
560+ -> Condition relations grouping params
593not_ = unsafeUnaryOp "NOT"
561not_ = unsafeUnaryOp "NOT"
595-- | >>> renderExpression $ true .&& false
563-- | >>> renderExpression $ true .&& false
596-- "(TRUE AND FALSE)"
564-- "(TRUE AND FALSE)"
598− :: Condition tables grouping params
566+ :: Condition relations grouping params
599− -> Condition tables grouping params
567+ -> Condition relations grouping params
600− -> Condition tables grouping params
568+ -> Condition relations grouping params
601(.&&) = unsafeBinaryOp "AND"
569(.&&) = unsafeBinaryOp "AND"
603-- | >>> renderExpression $ true .|| false
571-- | >>> renderExpression $ true .|| false
604-- "(TRUE OR FALSE)"
572-- "(TRUE OR FALSE)"
606− :: Condition tables grouping params
574+ :: Condition relations grouping params
607− -> Condition tables grouping params
575+ -> Condition relations grouping params
608− -> Condition tables grouping params
576+ -> Condition relations grouping params
609(.||) = unsafeBinaryOp "OR"
577(.||) = unsafeBinaryOp "OR"
581+-- expression :: Expression relations grouping params (nullity 'PGint2)
611−-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGint2)) $ caseWhenThenElse [(true, 1), (false, 2)] 3
582+-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
583+-- in renderExpression expression
612-- "CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END"
585-- "CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END"
613caseWhenThenElse
586caseWhenThenElse
614− :: [ ( Condition tables grouping params
587+ :: [ ( Condition relations grouping params
615− , Expression tables grouping params ('Required ty)
588+ , Expression relations grouping params (ty)
590+ -- ^ whens and thens
617− -> Expression tables grouping params ('Required ty)
591+ -> Expression relations grouping params (ty)
618− -> Expression tables grouping params ('Required ty)
593+ -> Expression relations grouping params (ty)
619caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
594caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
621 , mconcat
596 , mconcat
@@ -629,13 +604,18 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
609+-- expression :: Expression relations grouping params (nullity 'PGint2)
632−-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGint2)) $ ifThenElse true 1 0
610+-- expression = ifThenElse true 1 0
611+-- in renderExpression expression
633-- "CASE WHEN TRUE THEN 1 ELSE 0 END"
613-- "CASE WHEN TRUE THEN 1 ELSE 0 END"
634ifThenElse
614ifThenElse
635− :: Condition tables grouping params
615+ :: Condition relations grouping params
636− -> Expression tables grouping params ('Required ty)
616+ -> Expression relations grouping params (ty) -- ^ then
637− -> Expression tables grouping params ('Required ty)
617+ -> Expression relations grouping params (ty) -- ^ else
638− -> Expression tables grouping params ('Required ty)
618+ -> Expression relations grouping params (ty)
639ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
619ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
641-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
621-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
@@ -644,85 +624,85 @@ ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
644-- >>> renderExpression $ unNull true .== null_
624-- >>> renderExpression $ unNull true .== null_
645-- "(TRUE = NULL)"
625-- "(TRUE = NULL)"
647− :: Expression tables grouping params ('Required (nullity ty)) -- ^ lhs
627+ :: Expression relations grouping params (nullity ty) -- ^ lhs
648− -> Expression tables grouping params ('Required (nullity ty)) -- ^ rhs
628+ -> Expression relations grouping params (nullity ty) -- ^ rhs
649− -> Expression tables grouping params ('Required (nullity 'PGbool))
629+ -> Expression relations grouping params (nullity 'PGbool)
650(.==) = unsafeBinaryOp "="
630(.==) = unsafeBinaryOp "="
651infix 4 .==
631infix 4 .==
653-- | >>> renderExpression $ unNull true ./= null_
633-- | >>> renderExpression $ unNull true ./= null_
654-- "(TRUE <> NULL)"
634-- "(TRUE <> NULL)"
656− :: Expression tables grouping params ('Required (nullity ty)) -- ^ lhs
636+ :: Expression relations grouping params (nullity ty) -- ^ lhs
657− -> Expression tables grouping params ('Required (nullity ty)) -- ^ rhs
637+ -> Expression relations grouping params (nullity ty) -- ^ rhs
658− -> Expression tables grouping params ('Required (nullity 'PGbool))
638+ -> Expression relations grouping params (nullity 'PGbool)
659(./=) = unsafeBinaryOp "<>"
639(./=) = unsafeBinaryOp "<>"
660infix 4 ./=
640infix 4 ./=
662-- | >>> renderExpression $ unNull true .>= null_
642-- | >>> renderExpression $ unNull true .>= null_
663-- "(TRUE >= NULL)"
643-- "(TRUE >= NULL)"
665− :: Expression tables grouping params ('Required (nullity ty)) -- ^ lhs
645+ :: Expression relations grouping params (nullity ty) -- ^ lhs
666− -> Expression tables grouping params ('Required (nullity ty)) -- ^ rhs
646+ -> Expression relations grouping params (nullity ty) -- ^ rhs
667− -> Expression tables grouping params ('Required (nullity 'PGbool))
647+ -> Expression relations grouping params (nullity 'PGbool)
668(.>=) = unsafeBinaryOp ">="
648(.>=) = unsafeBinaryOp ">="
669infix 4 .>=
649infix 4 .>=
671-- | >>> renderExpression $ unNull true .< null_
651-- | >>> renderExpression $ unNull true .< null_
672-- "(TRUE < NULL)"
652-- "(TRUE < NULL)"
674− :: Expression tables grouping params ('Required (nullity ty)) -- ^ lhs
654+ :: Expression relations grouping params (nullity ty) -- ^ lhs
675− -> Expression tables grouping params ('Required (nullity ty)) -- ^ rhs
655+ -> Expression relations grouping params (nullity ty) -- ^ rhs
676− -> Expression tables grouping params ('Required (nullity 'PGbool))
656+ -> Expression relations grouping params (nullity 'PGbool)
677(.<) = unsafeBinaryOp "<"
657(.<) = unsafeBinaryOp "<"
678infix 4 .<
658infix 4 .<
680-- | >>> renderExpression $ unNull true .<= null_
660-- | >>> renderExpression $ unNull true .<= null_
681-- "(TRUE <= NULL)"
661-- "(TRUE <= NULL)"
683− :: Expression tables grouping params ('Required (nullity ty)) -- ^ lhs
663+ :: Expression relations grouping params (nullity ty) -- ^ lhs
684− -> Expression tables grouping params ('Required (nullity ty)) -- ^ rhs
664+ -> Expression relations grouping params (nullity ty) -- ^ rhs
685− -> Expression tables grouping params ('Required (nullity 'PGbool))
665+ -> Expression relations grouping params (nullity 'PGbool)
686(.<=) = unsafeBinaryOp "<="
666(.<=) = unsafeBinaryOp "<="
687infix 4 .<=
667infix 4 .<=
689-- | >>> renderExpression $ unNull true .> null_
669-- | >>> renderExpression $ unNull true .> null_
690-- "(TRUE > NULL)"
670-- "(TRUE > NULL)"
692− :: Expression tables grouping params ('Required (nullity ty)) -- ^ lhs
672+ :: Expression relations grouping params (nullity ty) -- ^ lhs
693− -> Expression tables grouping params ('Required (nullity ty)) -- ^ rhs
673+ -> Expression relations grouping params (nullity ty) -- ^ rhs
694− -> Expression tables grouping params ('Required (nullity 'PGbool))
674+ -> Expression relations grouping params (nullity 'PGbool)
695(.>) = unsafeBinaryOp ">"
675(.>) = unsafeBinaryOp ">"
696infix 4 .>
676infix 4 .>
698−-- | >>> renderExpression $ currentDate
678+-- | >>> renderExpression currentDate
699-- "CURRENT_DATE"
679-- "CURRENT_DATE"
700currentDate
680currentDate
701− :: Expression tables grouping params ('Required (nullity 'PGdate))
681+ :: Expression relations grouping params (nullity 'PGdate)
702currentDate = UnsafeExpression "CURRENT_DATE"
682currentDate = UnsafeExpression "CURRENT_DATE"
704−-- | >>> renderExpression $ currentTime
684+-- | >>> renderExpression currentTime
705-- "CURRENT_TIME"
685-- "CURRENT_TIME"
706currentTime
686currentTime
707− :: Expression tables grouping params ('Required (nullity 'PGtimetz))
687+ :: Expression relations grouping params (nullity 'PGtimetz)
708currentTime = UnsafeExpression "CURRENT_TIME"
688currentTime = UnsafeExpression "CURRENT_TIME"
710−-- | >>> renderExpression $ currentTimestamp
690+-- | >>> renderExpression currentTimestamp
711-- "CURRENT_TIMESTAMP"
691-- "CURRENT_TIMESTAMP"
712currentTimestamp
692currentTimestamp
713− :: Expression tables grouping params ('Required (nullity 'PGtimestamptz))
693+ :: Expression relations grouping params (nullity 'PGtimestamptz)
714currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
694currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
716−-- | >>> renderExpression $ localTime
696+-- | >>> renderExpression localTime
717-- "LOCALTIME"
697-- "LOCALTIME"
719− :: Expression tables grouping params ('Required (nullity 'PGtime))
699+ :: Expression relations grouping params (nullity 'PGtime)
720localTime = UnsafeExpression "LOCALTIME"
700localTime = UnsafeExpression "LOCALTIME"
722−-- | >>> renderExpression $ localTimestamp
702+-- | >>> renderExpression localTimestamp
723-- "LOCALTIMESTAMP"
703-- "LOCALTIMESTAMP"
724localTimestamp
704localTimestamp
725− :: Expression tables grouping params ('Required (nullity 'PGtimestamp))
705+ :: Expression relations grouping params (nullity 'PGtimestamp)
726localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
706localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
728{-----------------------------------------
708{-----------------------------------------
@@ -730,7 +710,7 @@ text
730-----------------------------------------}
710-----------------------------------------}
732instance IsString
712instance IsString
733− (Expression tables grouping params ('Required (nullity 'PGtext))) where
713+ (Expression relations grouping params (nullity 'PGtext)) where
734 fromString str = UnsafeExpression $
714 fromString str = UnsafeExpression $
735 "E\'" <> fromString (escape =<< str) <> "\'"
715 "E\'" <> fromString (escape =<< str) <> "\'"
@@ -746,32 +726,32 @@ instance IsString
748instance Monoid
728instance Monoid
749− (Expression tables grouping params ('Required (nullity 'PGtext))) where
729+ (Expression relations grouping params (nullity 'PGtext)) where
750 mempty = fromString ""
730 mempty = fromString ""
751 mappend = unsafeBinaryOp "||"
731 mappend = unsafeBinaryOp "||"
753-- | >>> renderExpression $ lower "ARRRGGG"
733-- | >>> renderExpression $ lower "ARRRGGG"
754-- "lower(E'ARRRGGG')"
734-- "lower(E'ARRRGGG')"
756− :: Expression tables grouping params ('Required (nullity 'PGtext))
736+ :: Expression relations grouping params (nullity 'PGtext)
757 -- ^ string to lower case
737 -- ^ string to lower case
758− -> Expression tables grouping params ('Required (nullity 'PGtext))
738+ -> Expression relations grouping params (nullity 'PGtext)
759lower = unsafeFunction "lower"
739lower = unsafeFunction "lower"
761-- | >>> renderExpression $ upper "eeee"
741-- | >>> renderExpression $ upper "eeee"
762-- "upper(E'eeee')"
742-- "upper(E'eeee')"
764− :: Expression tables grouping params ('Required (nullity 'PGtext))
744+ :: Expression relations grouping params (nullity 'PGtext)
765 -- ^ string to upper case
745 -- ^ string to upper case
766− -> Expression tables grouping params ('Required (nullity 'PGtext))
746+ -> Expression relations grouping params (nullity 'PGtext)
767upper = unsafeFunction "upper"
747upper = unsafeFunction "upper"
769-- | >>> renderExpression $ charLength "four"
749-- | >>> renderExpression $ charLength "four"
770-- "char_length(E'four')"
750-- "char_length(E'four')"
771charLength
751charLength
772− :: Expression tables grouping params ('Required (nullity 'PGtext))
752+ :: Expression relations grouping params (nullity 'PGtext)
773 -- ^ string to measure
753 -- ^ string to measure
774− -> Expression tables grouping params ('Required (nullity 'PGint4))
754+ -> Expression relations grouping params (nullity 'PGint4)
775charLength = unsafeFunction "char_length"
755charLength = unsafeFunction "char_length"
777-- | The `like` expression returns true if the @string@ matches
757-- | The `like` expression returns true if the @string@ matches
@@ -784,11 +764,11 @@ charLength = unsafeFunction "char_length"
784-- >>> renderExpression $ "abc" `like` "a%"
764-- >>> renderExpression $ "abc" `like` "a%"
785-- "(E'abc' LIKE E'a%')"
765-- "(E'abc' LIKE E'a%')"
787− :: Expression tables grouping params ('Required (nullity 'PGtext))
767+ :: Expression relations grouping params (nullity 'PGtext)
788 -- ^ string
768 -- ^ string
789− -> Expression tables grouping params ('Required (nullity 'PGtext))
769+ -> Expression relations grouping params (nullity 'PGtext)
790 -- ^ pattern
770 -- ^ pattern
791− -> Expression tables grouping params ('Required (nullity 'PGbool))
771+ -> Expression relations grouping params (nullity 'PGbool)
792like = unsafeBinaryOp "LIKE"
772like = unsafeBinaryOp "LIKE"
794{-----------------------------------------
774{-----------------------------------------
@@ -798,44 +778,54 @@ aggregation
798-- | escape hatch to define aggregate functions
778-- | escape hatch to define aggregate functions
799unsafeAggregate
779unsafeAggregate
800 :: ByteString -- ^ aggregate function
780 :: ByteString -- ^ aggregate function
801− -> Expression tables 'Ungrouped params ('Required xty)
781+ -> Expression relations 'Ungrouped params (xty)
802− -> Expression tables ('Grouped bys) params ('Required yty)
782+ -> Expression relations ('Grouped bys) params (yty)
803unsafeAggregate fun x = UnsafeExpression $ mconcat
783unsafeAggregate fun x = UnsafeExpression $ mconcat
804 [fun, "(", renderExpression x, ")"]
784 [fun, "(", renderExpression x, ")"]
806-- | escape hatch to define aggregate functions over distinct values
786-- | escape hatch to define aggregate functions over distinct values
807unsafeAggregateDistinct
787unsafeAggregateDistinct
808 :: ByteString -- ^ aggregate function
788 :: ByteString -- ^ aggregate function
809− -> Expression tables 'Ungrouped params ('Required xty)
789+ -> Expression relations 'Ungrouped params (xty)
810− -> Expression tables ('Grouped bys) params ('Required yty)
790+ -> Expression relations ('Grouped bys) params (yty)
811unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
791unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
812 [fun, "(DISTINCT ", renderExpression x, ")"]
792 [fun, "(DISTINCT ", renderExpression x, ")"]
814−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGnumeric)]] $ sum_ #col
794+-- | >>> :{
796+-- expression :: Expression '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Grouped bys) params ('Null 'PGnumeric)
797+-- expression = sum_ #col
798+-- in renderExpression expression
815-- "sum(col)"
800-- "sum(col)"
817 :: PGNum ty
802 :: PGNum ty
818− => Expression tables 'Ungrouped params ('Required (nullity ty))
803+ => Expression relations 'Ungrouped params (nullity ty)
819 -- ^ what to sum
804 -- ^ what to sum
820− -> Expression tables ('Grouped bys) params ('Required (nullity ty))
805+ -> Expression relations ('Grouped bys) params (nullity ty)
821sum_ = unsafeAggregate "sum"
806sum_ = unsafeAggregate "sum"
823−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGnumeric)]] $ sumDistinct #col
808+-- | >>> :{
810+-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGnumeric]] ('Grouped bys) params (nullity 'PGnumeric)
811+-- expression = sumDistinct #col
812+-- in renderExpression expression
824-- "sum(DISTINCT col)"
814-- "sum(DISTINCT col)"
825sumDistinct
815sumDistinct
826 :: PGNum ty
816 :: PGNum ty
827− => Expression tables 'Ungrouped params ('Required (nullity ty))
817+ => Expression relations 'Ungrouped params (nullity ty)
828 -- ^ what to sum
818 -- ^ what to sum
829− -> Expression tables ('Grouped bys) params ('Required (nullity ty))
819+ -> Expression relations ('Grouped bys) params (nullity ty)
830sumDistinct = unsafeAggregateDistinct "sum"
820sumDistinct = unsafeAggregateDistinct "sum"
832-- | A constraint for `PGType`s that you can take averages of and the resulting
822-- | A constraint for `PGType`s that you can take averages of and the resulting
833-- `PGType`.
823-- `PGType`.
834class PGAvg ty avg | ty -> avg where
824class PGAvg ty avg | ty -> avg where
835 avg, avgDistinct
825 avg, avgDistinct
836− :: Expression tables 'Ungrouped params ('Required (nullity ty))
826+ :: Expression relations 'Ungrouped params (nullity ty)
837 -- ^ what to average
827 -- ^ what to average
838− -> Expression tables ('Grouped bys) params ('Required (nullity avg))
828+ -> Expression relations ('Grouped bys) params (nullity avg)
839 avg = unsafeAggregate "avg"
829 avg = unsafeAggregate "avg"
840 avgDistinct = unsafeAggregateDistinct "avg"
830 avgDistinct = unsafeAggregateDistinct "avg"
841instance PGAvg 'PGint2 'PGnumeric
831instance PGAvg 'PGint2 'PGnumeric
@@ -846,72 +836,112 @@ instance PGAvg 'PGfloat4 'PGfloat8
846instance PGAvg 'PGfloat8 'PGfloat8
836instance PGAvg 'PGfloat8 'PGfloat8
847instance PGAvg 'PGinterval 'PGinterval
837instance PGAvg 'PGinterval 'PGinterval
849−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGint4)]] $ bitAnd #col
839+-- | >>> :{
841+-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
842+-- expression = bitAnd #col
843+-- in renderExpression expression
850-- "bit_and(col)"
845-- "bit_and(col)"
852 :: PGIntegral int
847 :: PGIntegral int
853− => Expression tables 'Ungrouped params ('Required (nullity int))
848+ => Expression relations 'Ungrouped params (nullity int)
854 -- ^ what to aggregate
849 -- ^ what to aggregate
855− -> Expression tables ('Grouped bys) params ('Required (nullity int))
850+ -> Expression relations ('Grouped bys) params (nullity int)
856bitAnd = unsafeAggregate "bit_and"
851bitAnd = unsafeAggregate "bit_and"
858−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGint4)]] $ bitOr #col
853+-- | >>> :{
855+-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
856+-- expression = bitOr #col
857+-- in renderExpression expression
859-- "bit_or(col)"
859-- "bit_or(col)"
861 :: PGIntegral int
861 :: PGIntegral int
862− => Expression tables 'Ungrouped params ('Required (nullity int))
862+ => Expression relations 'Ungrouped params (nullity int)
863 -- ^ what to aggregate
863 -- ^ what to aggregate
864− -> Expression tables ('Grouped bys) params ('Required (nullity int))
864+ -> Expression relations ('Grouped bys) params (nullity int)
865bitOr = unsafeAggregate "bit_or"
865bitOr = unsafeAggregate "bit_or"
869+-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
867−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGint4)]] $ bitAndDistinct #col
870+-- expression = bitAndDistinct #col
871+-- in renderExpression expression
868-- "bit_and(DISTINCT col)"
873-- "bit_and(DISTINCT col)"
869bitAndDistinct
874bitAndDistinct
870 :: PGIntegral int
875 :: PGIntegral int
871− => Expression tables 'Ungrouped params ('Required (nullity int))
876+ => Expression relations 'Ungrouped params (nullity int)
872 -- ^ what to aggregate
877 -- ^ what to aggregate
873− -> Expression tables ('Grouped bys) params ('Required (nullity int))
878+ -> Expression relations ('Grouped bys) params (nullity int)
874bitAndDistinct = unsafeAggregateDistinct "bit_and"
879bitAndDistinct = unsafeAggregateDistinct "bit_and"
883+-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
876−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGint4)]] $ bitOrDistinct #col
884+-- expression = bitOrDistinct #col
885+-- in renderExpression expression
877-- "bit_or(DISTINCT col)"
887-- "bit_or(DISTINCT col)"
878bitOrDistinct
888bitOrDistinct
879 :: PGIntegral int
889 :: PGIntegral int
880− => Expression tables 'Ungrouped params ('Required (nullity int))
890+ => Expression relations 'Ungrouped params (nullity int)
881 -- ^ what to aggregate
891 -- ^ what to aggregate
882− -> Expression tables ('Grouped bys) params ('Required (nullity int))
892+ -> Expression relations ('Grouped bys) params (nullity int)
883bitOrDistinct = unsafeAggregateDistinct "bit_or"
893bitOrDistinct = unsafeAggregateDistinct "bit_or"
885−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ boolAnd #col
895+-- | >>> :{
897+-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
898+-- expression = boolAnd #col
899+-- in renderExpression expression
886-- "bool_and(col)"
901-- "bool_and(col)"
888− :: Expression tables 'Ungrouped params ('Required (nullity 'PGbool))
903+ :: Expression relations 'Ungrouped params (nullity 'PGbool)
889 -- ^ what to aggregate
904 -- ^ what to aggregate
890− -> Expression tables ('Grouped bys) params ('Required (nullity 'PGbool))
905+ -> Expression relations ('Grouped bys) params (nullity 'PGbool)
891boolAnd = unsafeAggregate "bool_and"
906boolAnd = unsafeAggregate "bool_and"
893−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ boolOr #col
908+-- | >>> :{
910+-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
911+-- expression = boolOr #col
912+-- in renderExpression expression
894-- "bool_or(col)"
914-- "bool_or(col)"
896− :: Expression tables 'Ungrouped params ('Required (nullity 'PGbool))
916+ :: Expression relations 'Ungrouped params (nullity 'PGbool)
897 -- ^ what to aggregate
917 -- ^ what to aggregate
898− -> Expression tables ('Grouped bys) params ('Required (nullity 'PGbool))
918+ -> Expression relations ('Grouped bys) params (nullity 'PGbool)
899boolOr = unsafeAggregate "bool_or"
919boolOr = unsafeAggregate "bool_or"
923+-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
901−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ boolAndDistinct #col
924+-- expression = boolAndDistinct #col
925+-- in renderExpression expression
902-- "bool_and(DISTINCT col)"
927-- "bool_and(DISTINCT col)"
903boolAndDistinct
928boolAndDistinct
904− :: Expression tables 'Ungrouped params ('Required (nullity 'PGbool))
929+ :: Expression relations 'Ungrouped params (nullity 'PGbool)
905 -- ^ what to aggregate
930 -- ^ what to aggregate
906− -> Expression tables ('Grouped bys) params ('Required (nullity 'PGbool))
931+ -> Expression relations ('Grouped bys) params (nullity 'PGbool)
907boolAndDistinct = unsafeAggregateDistinct "bool_and"
932boolAndDistinct = unsafeAggregateDistinct "bool_and"
936+-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
909−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ boolOrDistinct #col
937+-- expression = boolOrDistinct #col
938+-- in renderExpression expression
910-- "bool_or(DISTINCT col)"
940-- "bool_or(DISTINCT col)"
911boolOrDistinct
941boolOrDistinct
912− :: Expression tables 'Ungrouped params ('Required (nullity 'PGbool))
942+ :: Expression relations 'Ungrouped params (nullity 'PGbool)
913 -- ^ what to aggregate
943 -- ^ what to aggregate
914− -> Expression tables ('Grouped bys) params ('Required (nullity 'PGbool))
944+ -> Expression relations ('Grouped bys) params (nullity 'PGbool)
915boolOrDistinct = unsafeAggregateDistinct "bool_or"
945boolOrDistinct = unsafeAggregateDistinct "bool_or"
917-- | A special aggregation that does not require an input
947-- | A special aggregation that does not require an input
@@ -919,50 +949,70 @@ boolOrDistinct = unsafeAggregateDistinct "bool_or"
919-- >>> renderExpression countStar
949-- >>> renderExpression countStar
920-- "count(*)"
950-- "count(*)"
922− :: Expression tables ('Grouped bys) params ('Required ('NotNull 'PGint8))
952+ :: Expression relations ('Grouped bys) params ('NotNull 'PGint8)
923countStar = UnsafeExpression $ "count(*)"
953countStar = UnsafeExpression $ "count(*)"
925−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Optional _]] $ count #col
955+-- | >>> :{
957+-- expression :: Expression '[tab ::: '["col" ::: nullity ty]] (Grouped bys) params ('NotNull 'PGint8)
958+-- expression = count #col
959+-- in renderExpression expression
926-- "count(col)"
961-- "count(col)"
928− :: Expression tables 'Ungrouped params ('Required ty)
963+ :: Expression relations 'Ungrouped params ty
929 -- ^ what to count
964 -- ^ what to count
930− -> Expression tables ('Grouped bys) params ('Required ('NotNull 'PGint8))
965+ -> Expression relations ('Grouped bys) params ('NotNull 'PGint8)
931count = unsafeAggregate "count"
966count = unsafeAggregate "count"
970+-- expression :: Expression '[tab ::: '["col" ::: nullity ty]] (Grouped bys) params ('NotNull 'PGint8)
933−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required _]] $ countDistinct #col
971+-- expression = countDistinct #col
972+-- in renderExpression expression
934-- "count(DISTINCT col)"
974-- "count(DISTINCT col)"
935countDistinct
975countDistinct
936− :: Expression tables 'Ungrouped params ('Required ty)
976+ :: Expression relations 'Ungrouped params ty
937 -- ^ what to count
977 -- ^ what to count
938− -> Expression tables ('Grouped bys) params ('Required ('NotNull 'PGint8))
978+ -> Expression relations ('Grouped bys) params ('NotNull 'PGint8)
939countDistinct = unsafeAggregateDistinct "count"
979countDistinct = unsafeAggregateDistinct "count"
941-- | synonym for `boolAnd`
981-- | synonym for `boolAnd`
943−-- >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ every #col
983+-- >>> :{
985+-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
986+-- expression = every #col
987+-- in renderExpression expression
944-- "every(col)"
989-- "every(col)"
946− :: Expression tables 'Ungrouped params ('Required (nullity 'PGbool))
991+ :: Expression relations 'Ungrouped params (nullity 'PGbool)
947 -- ^ what to aggregate
992 -- ^ what to aggregate
948− -> Expression tables ('Grouped bys) params ('Required (nullity 'PGbool))
993+ -> Expression relations ('Grouped bys) params (nullity 'PGbool)
949every = unsafeAggregate "every"
994every = unsafeAggregate "every"
951-- | synonym for `boolAndDistinct`
996-- | synonym for `boolAndDistinct`
1000+-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
953−-- >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ everyDistinct #col
1001+-- expression = everyDistinct #col
1002+-- in renderExpression expression
954-- "every(DISTINCT col)"
1004-- "every(DISTINCT col)"
955everyDistinct
1005everyDistinct
956− :: Expression tables 'Ungrouped params ('Required (nullity 'PGbool))
1006+ :: Expression relations 'Ungrouped params (nullity 'PGbool)
957 -- ^ what to aggregate
1007 -- ^ what to aggregate
958− -> Expression tables ('Grouped bys) params ('Required (nullity 'PGbool))
1008+ -> Expression relations ('Grouped bys) params (nullity 'PGbool)
959everyDistinct = unsafeAggregateDistinct "every"
1009everyDistinct = unsafeAggregateDistinct "every"
961-- | minimum and maximum aggregation
1011-- | minimum and maximum aggregation
962max_, min_, maxDistinct, minDistinct
1012max_, min_, maxDistinct, minDistinct
963− :: Expression tables 'Ungrouped params ('Required (nullity ty))
1013+ :: Expression relations 'Ungrouped params (nullity ty)
964 -- ^ what to aggregate
1014 -- ^ what to aggregate
965− -> Expression tables ('Grouped bys) params ('Required (nullity ty))
1015+ -> Expression relations ('Grouped bys) params (nullity ty)
966max_ = unsafeAggregate "max"
1016max_ = unsafeAggregate "max"
967min_ = unsafeAggregate "min"
1017min_ = unsafeAggregate "min"
968maxDistinct = unsafeAggregateDistinct "max"
1018maxDistinct = unsafeAggregateDistinct "max"
@@ -972,28 +1022,18 @@ minDistinct = unsafeAggregateDistinct "min"
973-----------------------------------------}
1023-----------------------------------------}
975−-- | A `Table` from a schema without its alias with an `IsLabel` instance
1025+-- | A `Table` from a table expression is a way
976-- to call a table reference by its alias.
1026-- to call a table reference by its alias.
977newtype Table
1027newtype Table
978 (schema :: TablesType)
1028 (schema :: TablesType)
979− (columns :: ColumnsType)
1029+ (columns :: RelationType)
980 = UnsafeTable { renderTable :: ByteString }
1030 = UnsafeTable { renderTable :: ByteString }
981 deriving (GHC.Generic,Show,Eq,Ord,NFData)
1031 deriving (GHC.Generic,Show,Eq,Ord,NFData)
983−-- | A `HasTable` constraint indicates a table reference.
1033+ ( Has alias schema table
984−class KnownSymbol table => HasTable table tables columns
1034+ , relation ~ ColumnsToRelation (TableToColumns table)
985− | table tables -> columns where
986− getTable :: Alias table -> Table tables columns
987− getTable table = UnsafeTable $ renderAlias table
988−instance {-# OVERLAPPING #-} KnownSymbol table
989− => HasTable table ((table ::: columns) ': tables) columns
990−instance {-# OVERLAPPABLE #-}
991− (KnownSymbol table, HasTable table schema columns)
992− => HasTable table (table' ': schema) columns
994−instance HasTable table schema columns
995− => IsLabel table (Table schema columns) where
1035+ ) => IsLabel alias (Table schema relation) where
996− fromLabel = getTable (Alias @table)
1036+ fromLabel = UnsafeTable $ renderAlias (Alias @alias)
998{-----------------------------------------
1038{-----------------------------------------
999type expressions
1039type expressions
@@ -1005,117 +1045,136 @@ newtype TypeExpression (ty :: ColumnType)
1005 deriving (GHC.Generic,Show,Eq,Ord,NFData)
1045 deriving (GHC.Generic,Show,Eq,Ord,NFData)
1007-- | logical Boolean (true/false)
1047-- | logical Boolean (true/false)
1008−bool :: TypeExpression ('Required ('Null 'PGbool))
1048+bool :: TypeExpression ('NoDef :=> 'Null 'PGbool)
1009bool = UnsafeTypeExpression "bool"
1049bool = UnsafeTypeExpression "bool"
1010-- | signed two-byte integer
1050-- | signed two-byte integer
1011−int2, smallint :: TypeExpression ('Required ('Null 'PGint2))
1051+int2, smallint :: TypeExpression ('NoDef :=> 'Null 'PGint2)
1012int2 = UnsafeTypeExpression "int2"
1052int2 = UnsafeTypeExpression "int2"
1013smallint = UnsafeTypeExpression "smallint"
1053smallint = UnsafeTypeExpression "smallint"
1014-- | signed four-byte integer
1054-- | signed four-byte integer
1015−int4, int, integer :: TypeExpression ('Required ('Null 'PGint4))
1055+int4, int, integer :: TypeExpression ('NoDef :=> 'Null 'PGint4)
1016int4 = UnsafeTypeExpression "int4"
1056int4 = UnsafeTypeExpression "int4"
1017int = UnsafeTypeExpression "int"
1057int = UnsafeTypeExpression "int"
1018integer = UnsafeTypeExpression "integer"
1058integer = UnsafeTypeExpression "integer"
1019-- | signed eight-byte integer
1059-- | signed eight-byte integer
1020−int8, bigint :: TypeExpression ('Required ('Null 'PGint8))
1060+int8, bigint :: TypeExpression ('NoDef :=> 'Null 'PGint8)
1021int8 = UnsafeTypeExpression "int8"
1061int8 = UnsafeTypeExpression "int8"
1022bigint = UnsafeTypeExpression "bigint"
1062bigint = UnsafeTypeExpression "bigint"
1023-- | arbitrary precision numeric type
1063-- | arbitrary precision numeric type
1024−numeric :: TypeExpression ('Required ('Null 'PGnumeric))
1064+numeric :: TypeExpression ('NoDef :=> 'Null 'PGnumeric)
1025numeric = UnsafeTypeExpression "numeric"
1065numeric = UnsafeTypeExpression "numeric"
1026-- | single precision floating-point number (4 bytes)
1066-- | single precision floating-point number (4 bytes)
1027−float4, real :: TypeExpression ('Required ('Null 'PGfloat4))
1067+float4, real :: TypeExpression ('NoDef :=> 'Null 'PGfloat4)
1028float4 = UnsafeTypeExpression "float4"
1068float4 = UnsafeTypeExpression "float4"
1029real = UnsafeTypeExpression "real"
1069real = UnsafeTypeExpression "real"
1030-- | double precision floating-point number (8 bytes)
1070-- | double precision floating-point number (8 bytes)
1031−float8, doublePrecision :: TypeExpression ('Required ('Null 'PGfloat8))
1071+float8, doublePrecision :: TypeExpression ('NoDef :=> 'Null 'PGfloat8)
1032float8 = UnsafeTypeExpression "float8"
1072float8 = UnsafeTypeExpression "float8"
1033doublePrecision = UnsafeTypeExpression "double precision"
1073doublePrecision = UnsafeTypeExpression "double precision"
1034-- | not a true type, but merely a notational convenience for creating
1074-- | not a true type, but merely a notational convenience for creating
1035-- unique identifier columns with type `'PGint2`
1075-- unique identifier columns with type `'PGint2`
1076+serial2, smallserial
1036−serial2, smallserial :: TypeExpression ('Optional ('NotNull 'PGint2))
1077+ :: TypeExpression ('Def :=> 'NotNull 'PGint2)
1037serial2 = UnsafeTypeExpression "serial2"
1078serial2 = UnsafeTypeExpression "serial2"
1038smallserial = UnsafeTypeExpression "smallserial"
1079smallserial = UnsafeTypeExpression "smallserial"
1039-- | not a true type, but merely a notational convenience for creating
1080-- | not a true type, but merely a notational convenience for creating
1040-- unique identifier columns with type `'PGint4`
1081-- unique identifier columns with type `'PGint4`
1041−serial4, serial :: TypeExpression ('Optional ('NotNull 'PGint4))
1083+ :: TypeExpression ('Def :=> 'NotNull 'PGint4)
1042serial4 = UnsafeTypeExpression "serial4"
1084serial4 = UnsafeTypeExpression "serial4"
1043serial = UnsafeTypeExpression "serial"
1085serial = UnsafeTypeExpression "serial"
1044-- | not a true type, but merely a notational convenience for creating
1086-- | not a true type, but merely a notational convenience for creating
1045-- unique identifier columns with type `'PGint8`
1087-- unique identifier columns with type `'PGint8`
1046−serial8, bigserial :: TypeExpression ('Optional ('NotNull 'PGint8))
1089+ :: TypeExpression ('Def :=> 'NotNull 'PGint8)
1047serial8 = UnsafeTypeExpression "serial8"
1090serial8 = UnsafeTypeExpression "serial8"
1048bigserial = UnsafeTypeExpression "bigserial"
1091bigserial = UnsafeTypeExpression "bigserial"
1049-- | variable-length character string
1092-- | variable-length character string
1050−text :: TypeExpression ('Required ('Null 'PGtext))
1093+text :: TypeExpression ('NoDef :=> 'Null 'PGtext)
1051text = UnsafeTypeExpression "text"
1094text = UnsafeTypeExpression "text"
1052-- | fixed-length character string
1095-- | fixed-length character string
1053char, character
1096char, character
1054 :: (KnownNat n, 1 <= n)
1097 :: (KnownNat n, 1 <= n)
1055 => proxy n
1098 => proxy n
1056− -> TypeExpression ('Required ('Null ('PGchar n)))
1099+ -> TypeExpression ('NoDef :=> 'Null ('PGchar n))
1057char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
1100char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
1058character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
1101character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
1059-- | variable-length character string
1102-- | variable-length character string
1060varchar, characterVarying
1103varchar, characterVarying
1061 :: (KnownNat n, 1 <= n)
1104 :: (KnownNat n, 1 <= n)
1062 => proxy n
1105 => proxy n
1063− -> TypeExpression ('Required ('Null ('PGvarchar n)))
1106+ -> TypeExpression ('NoDef :=> 'Null ('PGvarchar n))
1064varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
1107varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
1065characterVarying p = UnsafeTypeExpression $
1108characterVarying p = UnsafeTypeExpression $
1066 "character varying(" <> renderNat p <> ")"
1109 "character varying(" <> renderNat p <> ")"
1067-- | binary data ("byte array")
1110-- | binary data ("byte array")
1068−bytea :: TypeExpression ('Required ('Null 'PGbytea))
1111+bytea :: TypeExpression ('NoDef :=> 'Null 'PGbytea)
1069bytea = UnsafeTypeExpression "bytea"
1112bytea = UnsafeTypeExpression "bytea"
1070-- | date and time (no time zone)
1113-- | date and time (no time zone)
1071−timestamp :: TypeExpression ('Required ('Null 'PGtimestamp))
1114+timestamp :: TypeExpression ('NoDef :=> 'Null 'PGtimestamp)
1072timestamp = UnsafeTypeExpression "timestamp"
1115timestamp = UnsafeTypeExpression "timestamp"
1073-- | date and time, including time zone
1116-- | date and time, including time zone
1074−timestampWithTimeZone :: TypeExpression ('Required ('Null 'PGtimestamptz))
1117+timestampWithTimeZone :: TypeExpression ('NoDef :=> 'Null 'PGtimestamptz)
1075timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
1118timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
1076-- | calendar date (year, month, day)
1119-- | calendar date (year, month, day)
1077−date :: TypeExpression ('Required ('Null 'PGdate))
1120+date :: TypeExpression ('NoDef :=> 'Null 'PGdate)
1078date = UnsafeTypeExpression "date"
1121date = UnsafeTypeExpression "date"
1079-- | time of day (no time zone)
1122-- | time of day (no time zone)
1080−time :: TypeExpression ('Required ('Null 'PGtime))
1123+time :: TypeExpression ('NoDef :=> 'Null 'PGtime)
1081time = UnsafeTypeExpression "time"
1124time = UnsafeTypeExpression "time"
1082-- | time of day, including time zone
1125-- | time of day, including time zone
1083−timeWithTimeZone :: TypeExpression ('Required ('Null 'PGtimetz))
1126+timeWithTimeZone :: TypeExpression ('NoDef :=> 'Null 'PGtimetz)
1084timeWithTimeZone = UnsafeTypeExpression "time with time zone"
1127timeWithTimeZone = UnsafeTypeExpression "time with time zone"
1085-- | time span
1128-- | time span
1086−interval :: TypeExpression ('Required ('Null 'PGinterval))
1129+interval :: TypeExpression ('NoDef :=> 'Null 'PGinterval)
1087interval = UnsafeTypeExpression "interval"
1130interval = UnsafeTypeExpression "interval"
1088-- | universally unique identifier
1131-- | universally unique identifier
1089−uuid :: TypeExpression ('Required ('Null 'PGuuid))
1132+uuid :: TypeExpression ('NoDef :=> 'Null 'PGuuid)
1090uuid = UnsafeTypeExpression "uuid"
1133uuid = UnsafeTypeExpression "uuid"
1091-- | IPv4 or IPv6 host address
1134-- | IPv4 or IPv6 host address
1092−inet :: TypeExpression ('Required ('Null 'PGinet))
1135+inet :: TypeExpression ('NoDef :=> 'Null 'PGinet)
1093inet = UnsafeTypeExpression "inet"
1136inet = UnsafeTypeExpression "inet"
1094-- | textual JSON data
1137-- | textual JSON data
1095−json :: TypeExpression ('Required ('Null 'PGjson))
1138+json :: TypeExpression ('NoDef :=> 'Null 'PGjson)
1096json = UnsafeTypeExpression "json"
1139json = UnsafeTypeExpression "json"
1097-- | binary JSON data, decomposed
1140-- | binary JSON data, decomposed
1098−jsonb :: TypeExpression ('Required ('Null 'PGjsonb))
1141+jsonb :: TypeExpression ('NoDef :=> 'Null 'PGjsonb)
1099jsonb = UnsafeTypeExpression "jsonb"
1142jsonb = UnsafeTypeExpression "jsonb"
1143+-- | variable length array
1145+ :: TypeExpression ('NoDef :=> 'Null pg)
1146+ -> TypeExpression ('NoDef :=> 'Null ('PGvararray pg))
1147+vararray ty = UnsafeTypeExpression $ renderTypeExpression ty <> "[]"
1148+-- | fixed length array
1150+-- >>> renderTypeExpression (fixarray (Proxy @2) json)
1155+ -> TypeExpression ('NoDef :=> 'Null pg)
1156+ -> TypeExpression ('NoDef :=> 'Null ('PGfixarray n pg))
1157+fixarray p ty = UnsafeTypeExpression $
1158+ renderTypeExpression ty <> "[" <> renderNat p <> "]"
1101-- | used in `createTable` commands as a column constraint to ensure
1160-- | used in `createTable` commands as a column constraint to ensure
1102-- @NULL@ is not present
1161-- @NULL@ is not present
1104− :: TypeExpression (optionality ('Null ty))
1163+ :: TypeExpression (def :=> 'Null ty)
1105− -> TypeExpression (optionality ('NotNull ty))
1164+ -> TypeExpression (def :=> 'NotNull ty)
1106notNull ty = UnsafeTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
1165notNull ty = UnsafeTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
1108-- | used in `createTable` commands as a column constraint to give a default
1167-- | used in `createTable` commands as a column constraint to give a default
1110− :: Expression '[] 'Ungrouped '[] ('Required ty)
1169+ :: Expression '[] 'Ungrouped '[] ty
1111− -> TypeExpression ('Required ty)
1170+ -> TypeExpression ('NoDef :=> ty)
1112− -> TypeExpression ('Optional ty)
1171+ -> TypeExpression ('Def :=> ty)
1113default_ x ty = UnsafeTypeExpression $
1172default_ x ty = UnsafeTypeExpression $
1114 renderTypeExpression ty <+> "DEFAULT" <+> renderExpression x
1173 renderTypeExpression ty <+> "DEFAULT" <+> renderExpression x
1116-- | `pgtype` is a demoted version of a `PGType`
1175-- | `pgtype` is a demoted version of a `PGType`
1117class PGTyped (ty :: PGType) where
1176class PGTyped (ty :: PGType) where
1118− pgtype :: TypeExpression ('Required ('Null ty))
1177+ pgtype :: TypeExpression ('NoDef :=> 'Null ty)
1119instance PGTyped 'PGbool where pgtype = bool
1178instance PGTyped 'PGbool where pgtype = bool
1120instance PGTyped 'PGint2 where pgtype = int2
1179instance PGTyped 'PGint2 where pgtype = int2
1121instance PGTyped 'PGint4 where pgtype = int4
1180instance PGTyped 'PGint4 where pgtype = int4
@@ -1138,3 +1197,7 @@ instance PGTyped 'PGinterval where pgtype = interval
1138instance PGTyped 'PGuuid where pgtype = uuid
1197instance PGTyped 'PGuuid where pgtype = uuid
1139instance PGTyped 'PGjson where pgtype = json
1198instance PGTyped 'PGjson where pgtype = json
1140instance PGTyped 'PGjsonb where pgtype = jsonb
1199instance PGTyped 'PGjsonb where pgtype = jsonb
1200+instance PGTyped ty => PGTyped ('PGvararray ty) where
1201+ pgtype = vararray (pgtype @ty)
1202+instance (KnownNat n, PGTyped ty) => PGTyped ('PGfixarray n ty) where
1203+ pgtype = fixarray (Proxy @n) (pgtype @ty)