@@ -16,11 +16,13 @@ Squeal expressions are the atoms used to build statements.
16 , FlexibleContexts
16 , FlexibleContexts
17 , FlexibleInstances
17 , FlexibleInstances
18 , FunctionalDependencies
18 , FunctionalDependencies
19 , GeneralizedNewtypeDeriving
20 , GeneralizedNewtypeDeriving
20 , LambdaCase
21 , LambdaCase
21 , MagicHash
22 , MagicHash
22 , OverloadedStrings
23 , OverloadedStrings
23 , ScopedTypeVariables
24 , ScopedTypeVariables
24 , TypeApplications
26 , TypeApplications
25 , TypeFamilies
27 , TypeFamilies
26 , TypeInType
28 , TypeInType
@@ -132,15 +134,27 @@ module Squeal.PostgreSQL.Expression
132 , jsonbInsert
134 , jsonbInsert
133 , jsonbPretty
135 , jsonbPretty
134 -- ** Aggregation
136 -- ** Aggregation
135− , unsafeAggregate, unsafeAggregateDistinct
137+ , Aggregate (..)
136− , sum_, sumDistinct
138+ , Distinction (..)
137− , PGAvg (avg, avgDistinct)
139+ , PGAvg
138− , bitAnd, bitOr, boolAnd, boolOr
140+ -- * Window Functions
139− , bitAndDistinct, bitOrDistinct, boolAndDistinct, boolOrDistinct
141+ , WindowDefinition (..)
140− , countStar
142+ , partitionBy
141− , count, countDistinct
143+ , WindowFunction (..)
142− , every, everyDistinct
144+ , rank
143− , max_, maxDistinct, min_, minDistinct
145+ , rowNumber
156+ , SortExpression (..)
144 -- * Types
158 -- * Types
145 , TypeExpression (..)
159 , TypeExpression (..)
146 , PGTyped (..)
160 , PGTyped (..)
@@ -180,7 +194,10 @@ module Squeal.PostgreSQL.Expression
180 , fixarray
194 , fixarray
181 -- * Re-export
195 -- * Re-export
183− , NP ((:*), Nil)
197+ , NP (..)
186import Control.Category
203import Control.Category
@@ -194,6 +211,7 @@ import Data.Ratio
194import Data.String
211import Data.String
195import Data.Word
212import Data.Word
196import Generics.SOP hiding (from)
213import Generics.SOP hiding (from)
214+import GHC.Generics ((:*:) (..))
197import GHC.OverloadedLabels
215import GHC.OverloadedLabels
198import GHC.TypeLits
216import GHC.TypeLits
199import Prelude hiding (id, (.))
217import Prelude hiding (id, (.))
@@ -219,16 +237,16 @@ values from primitive expression using arithmetic, logical,
219and other operations.
237and other operations.
221newtype Expression
239newtype Expression
222 (commons :: FromType)
241 (commons :: FromType)
223 (schemas :: SchemasType)
242 (schemas :: SchemasType)
224 (params :: [NullityType])
243 (params :: [NullityType])
226 (from :: FromType)
244 (from :: FromType)
227 (ty :: NullityType)
245 (ty :: NullityType)
228 = UnsafeExpression { renderExpression :: ByteString }
246 = UnsafeExpression { renderExpression :: ByteString }
229 deriving (GHC.Generic,Show,Eq,Ord,NFData)
247 deriving (GHC.Generic,Show,Eq,Ord,NFData)
231−instance RenderSQL (Expression commons schemas params grp from ty) where
249+instance RenderSQL (Expression grp commons schemas params from ty) where
232 renderSQL = renderExpression
250 renderSQL = renderExpression
234{- | A `HasParameter` constraint is used to indicate a value that is
252{- | A `HasParameter` constraint is used to indicate a value that is
@@ -246,12 +264,12 @@ class KnownNat n => HasParameter
246 | n params -> ty where
264 | n params -> ty where
247 -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
265 -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
249− -- >>> let expr = parameter @1 int4 :: Expression '[] schemas '[ 'Null 'PGint4] grp from ('Null 'PGint4)
267+ -- >>> let expr = parameter @1 int4 :: Expression grp '[] schemas '[ 'Null 'PGint4] from ('Null 'PGint4)
250 -- >>> printSQL expr
268 -- >>> printSQL expr
251 -- ($1 :: int4)
269 -- ($1 :: int4)
252 parameter
270 parameter
253 :: TypeExpression schemas ty
271 :: TypeExpression schemas ty
254− -> Expression commons schemas params grp from ty
272+ -> Expression grp commons schemas params from ty
255 parameter ty = UnsafeExpression $ parenthesized $
273 parameter ty = UnsafeExpression $ parenthesized $
256 "$" <> renderNat @n <+> "::"
274 "$" <> renderNat @n <+> "::"
257 <+> renderSQL ty
275 <+> renderSQL ty
@@ -262,38 +280,44 @@ instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
262-- | `param` takes a `Nat` using type application and for basic types,
280-- | `param` takes a `Nat` using type application and for basic types,
263-- infers a `TypeExpression`.
281-- infers a `TypeExpression`.
265−-- >>> let expr = param @1 :: Expression commons schemas '[ 'Null 'PGint4] grp from ('Null 'PGint4)
283+-- >>> let expr = param @1 :: Expression grp commons schemas '[ 'Null 'PGint4] from ('Null 'PGint4)
266-- >>> printSQL expr
284-- >>> printSQL expr
267-- ($1 :: int4)
285-- ($1 :: int4)
269 :: forall n commons schemas params from grp ty
287 :: forall n commons schemas params from grp ty
270 . (PGTyped schemas ty, HasParameter n params ty)
288 . (PGTyped schemas ty, HasParameter n params ty)
271− => Expression commons schemas params grp from ty -- ^ param
289+ => Expression grp commons schemas params from ty -- ^ param
272param = parameter @n (pgtype @schemas)
290param = parameter @n (pgtype @schemas)
274instance (HasUnique tab from row, Has col row ty)
292instance (HasUnique tab from row, Has col row ty)
275− => IsLabel col (Expression commons schemas params 'Ungrouped from ty) where
293+ => IsLabel col (Expression 'Ungrouped commons schemas params from ty) where
276 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
294 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
295+instance (HasUnique tab from row, Has col row ty, tys ~ '[ty])
296+ => IsLabel col (NP (Expression 'Ungrouped commons schemas params from) tys) where
297+ fromLabel = fromLabel @col :* Nil
277instance (HasUnique tab from row, Has col row ty, column ~ (col ::: ty))
298instance (HasUnique tab from row, Has col row ty, column ~ (col ::: ty))
278 => IsLabel col
299 => IsLabel col
279− (Aliased (Expression commons schemas params 'Ungrouped from) column) where
300+ (Aliased (Expression 'Ungrouped commons schemas params from) column) where
280 fromLabel = fromLabel @col `As` Alias
301 fromLabel = fromLabel @col `As` Alias
281instance (HasUnique tab from row, Has col row ty, columns ~ '[col ::: ty])
302instance (HasUnique tab from row, Has col row ty, columns ~ '[col ::: ty])
282 => IsLabel col
303 => IsLabel col
283− (NP (Aliased (Expression commons schemas params 'Ungrouped from)) columns) where
304+ (NP (Aliased (Expression 'Ungrouped commons schemas params from)) columns) where
284 fromLabel = fromLabel @col :* Nil
305 fromLabel = fromLabel @col :* Nil
286instance (Has tab from row, Has col row ty)
307instance (Has tab from row, Has col row ty)
287− => IsQualified tab col (Expression commons schemas params 'Ungrouped from ty) where
308+ => IsQualified tab col (Expression 'Ungrouped commons schemas params from ty) where
288 tab ! col = UnsafeExpression $
309 tab ! col = UnsafeExpression $
289 renderSQL tab <> "." <> renderSQL col
310 renderSQL tab <> "." <> renderSQL col
311+instance (Has tab from row, Has col row ty, tys ~ '[ty])
312+ => IsQualified tab col (NP (Expression 'Ungrouped commons schemas params from) tys) where
313+ tab ! col = tab ! col :* Nil
290instance (Has tab from row, Has col row ty, column ~ (col ::: ty))
314instance (Has tab from row, Has col row ty, column ~ (col ::: ty))
291 => IsQualified tab col
315 => IsQualified tab col
292− (Aliased (Expression commons schemas params 'Ungrouped from) column) where
316+ (Aliased (Expression 'Ungrouped commons schemas params from) column) where
293 tab ! col = tab ! col `As` col
317 tab ! col = tab ! col `As` col
294instance (Has tab from row, Has col row ty, columns ~ '[col ::: ty])
318instance (Has tab from row, Has col row ty, columns ~ '[col ::: ty])
295 => IsQualified tab col
319 => IsQualified tab col
296− (NP (Aliased (Expression commons schemas params 'Ungrouped from)) columns) where
320+ (NP (Aliased (Expression 'Ungrouped commons schemas params from)) columns) where
297 tab ! col = tab ! col :* Nil
321 tab ! col = tab ! col :* Nil
@@ -301,15 +325,23 @@ instance
301 , Has col row ty
325 , Has col row ty
302 , GroupedBy tab col bys
326 , GroupedBy tab col bys
303 ) => IsLabel col
327 ) => IsLabel col
304− (Expression commons schemas params ('Grouped bys) from ty) where
328+ (Expression ('Grouped bys) commons schemas params from ty) where
305 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
329 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
331+ ( HasUnique tab from row
333+ , GroupedBy tab col bys
336+ (NP (Expression ('Grouped bys) commons schemas params from) tys) where
337+ fromLabel = fromLabel @col :* Nil
307 ( HasUnique tab from row
339 ( HasUnique tab from row
308 , Has col row ty
340 , Has col row ty
309 , GroupedBy tab col bys
341 , GroupedBy tab col bys
310 , column ~ (col ::: ty)
342 , column ~ (col ::: ty)
311 ) => IsLabel col
343 ) => IsLabel col
312− (Aliased (Expression commons schemas params ('Grouped bys) from) column) where
344+ (Aliased (Expression ('Grouped bys) commons schemas params from) column) where
313 fromLabel = fromLabel @col `As` Alias
345 fromLabel = fromLabel @col `As` Alias
315 ( HasUnique tab from row
347 ( HasUnique tab from row
@@ -317,7 +349,7 @@ instance
317 , GroupedBy tab col bys
349 , GroupedBy tab col bys
318 , columns ~ '[col ::: ty]
350 , columns ~ '[col ::: ty]
319 ) => IsLabel col
351 ) => IsLabel col
320− (NP (Aliased (Expression commons schemas params ('Grouped bys) from)) columns) where
352+ (NP (Aliased (Expression ('Grouped bys) commons schemas params from)) columns) where
321 fromLabel = fromLabel @col :* Nil
353 fromLabel = fromLabel @col :* Nil
@@ -325,16 +357,24 @@ instance
325 , Has col row ty
357 , Has col row ty
326 , GroupedBy tab col bys
358 , GroupedBy tab col bys
327 ) => IsQualified tab col
359 ) => IsQualified tab col
328− (Expression commons schemas params ('Grouped bys) from ty) where
360+ (Expression ('Grouped bys) commons schemas params from ty) where
329 tab ! col = UnsafeExpression $
361 tab ! col = UnsafeExpression $
330 renderSQL tab <> "." <> renderSQL col
362 renderSQL tab <> "." <> renderSQL col
366+ , GroupedBy tab col bys
368+ ) => IsQualified tab col
369+ (NP (Expression ('Grouped bys) commons schemas params from) tys) where
370+ tab ! col = tab ! col :* Nil
332 ( Has tab from row
372 ( Has tab from row
333 , Has col row ty
373 , Has col row ty
334 , GroupedBy tab col bys
374 , GroupedBy tab col bys
335 , column ~ (col ::: ty)
375 , column ~ (col ::: ty)
336 ) => IsQualified tab col
376 ) => IsQualified tab col
337− (Aliased (Expression commons schemas params ('Grouped bys) from) column) where
377+ (Aliased (Expression ('Grouped bys) commons schemas params from) column) where
338 tab ! col = tab ! col `As` col
378 tab ! col = tab ! col `As` col
340 ( Has tab from row
380 ( Has tab from row
@@ -342,7 +382,7 @@ instance
342 , GroupedBy tab col bys
382 , GroupedBy tab col bys
343 , columns ~ '[col ::: ty]
383 , columns ~ '[col ::: ty]
344 ) => IsQualified tab col
384 ) => IsQualified tab col
345− (NP (Aliased (Expression commons schemas params ('Grouped bys) from)) columns) where
385+ (NP (Aliased (Expression ('Grouped bys) commons schemas params from)) columns) where
346 tab ! col = tab ! col :* Nil
386 tab ! col = tab ! col :* Nil
348-- | analagous to `Nothing`
388-- | analagous to `Nothing`
@@ -366,11 +406,11 @@ notNull = UnsafeExpression . renderSQL
366-- >>> printSQL $ coalesce [null_, true] false
406-- >>> printSQL $ coalesce [null_, true] false
367-- COALESCE(NULL, TRUE, FALSE)
407-- COALESCE(NULL, TRUE, FALSE)
369− :: [Expression commons schemas params grp from ('Null ty)]
409+ :: [Expression grp commons schemas params from ('Null ty)]
370 -- ^ @NULL@s may be present
410 -- ^ @NULL@s may be present
371− -> Expression commons schemas params grp from ('NotNull ty)
411+ -> Expression grp commons schemas params from ('NotNull ty)
372 -- ^ @NULL@ is absent
412 -- ^ @NULL@ is absent
373− -> Expression commons schemas params grp from ('NotNull ty)
413+ -> Expression grp commons schemas params from ('NotNull ty)
374coalesce nullxs notNullx = UnsafeExpression $
414coalesce nullxs notNullx = UnsafeExpression $
375 "COALESCE" <> parenthesized (commaSeparated
415 "COALESCE" <> parenthesized (commaSeparated
376 ((renderSQL <$> nullxs) <> [renderSQL notNullx]))
416 ((renderSQL <$> nullxs) <> [renderSQL notNullx]))
@@ -380,26 +420,26 @@ coalesce nullxs notNullx = UnsafeExpression $
380-- >>> printSQL $ fromNull true null_
420-- >>> printSQL $ fromNull true null_
381-- COALESCE(NULL, TRUE)
421-- COALESCE(NULL, TRUE)
383− :: Expression commons schemas params grp from ('NotNull ty)
423+ :: Expression grp commons schemas params from ('NotNull ty)
384 -- ^ what to convert @NULL@ to
424 -- ^ what to convert @NULL@ to
385− -> Expression commons schemas params grp from ('Null ty)
425+ -> Expression grp commons schemas params from ('Null ty)
386− -> Expression commons schemas params grp from ('NotNull ty)
426+ -> Expression grp commons schemas params from ('NotNull ty)
387fromNull notNullx nullx = coalesce [nullx] notNullx
427fromNull notNullx nullx = coalesce [nullx] notNullx
389-- | >>> printSQL $ null_ & isNull
429-- | >>> printSQL $ null_ & isNull
390-- NULL IS NULL
430-- NULL IS NULL
392− :: Expression commons schemas params grp from ('Null ty)
432+ :: Expression grp commons schemas params from ('Null ty)
393 -- ^ possibly @NULL@
433 -- ^ possibly @NULL@
394− -> Condition commons schemas params grp from
434+ -> Condition grp commons schemas params from
395isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
435isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
397-- | >>> printSQL $ null_ & isNotNull
437-- | >>> printSQL $ null_ & isNotNull
398-- NULL IS NOT NULL
438-- NULL IS NOT NULL
400− :: Expression commons schemas params grp from ('Null ty)
440+ :: Expression grp commons schemas params from ('Null ty)
401 -- ^ possibly @NULL@
441 -- ^ possibly @NULL@
402− -> Condition commons schemas params grp from
442+ -> Condition grp commons schemas params from
403isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
443isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
405-- | analagous to `maybe` using @IS NULL@
445-- | analagous to `maybe` using @IS NULL@
@@ -407,13 +447,13 @@ isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
407-- >>> printSQL $ matchNull true not_ null_
447-- >>> printSQL $ matchNull true not_ null_
408-- CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END
448-- CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END
410− :: Expression commons schemas params grp from (nullty)
450+ :: Expression grp commons schemas params from (nullty)
411 -- ^ what to convert @NULL@ to
451 -- ^ what to convert @NULL@ to
412− -> ( Expression commons schemas params grp from ('NotNull ty)
452+ -> ( Expression grp commons schemas params from ('NotNull ty)
413− -> Expression commons schemas params grp from (nullty) )
453+ -> Expression grp commons schemas params from (nullty) )
414 -- ^ function to perform when @NULL@ is absent
454 -- ^ function to perform when @NULL@ is absent
415− -> Expression commons schemas params grp from ('Null ty)
455+ -> Expression grp commons schemas params from ('Null ty)
416− -> Expression commons schemas params grp from (nullty)
456+ -> Expression grp commons schemas params from (nullty)
417matchNull y f x = ifThenElse (isNull x) y
457matchNull y f x = ifThenElse (isNull x) y
418 (f (UnsafeExpression (renderSQL x)))
458 (f (UnsafeExpression (renderSQL x)))
@@ -421,25 +461,25 @@ matchNull y f x = ifThenElse (isNull x) y
421`nullIf` gives @NULL@.
461`nullIf` gives @NULL@.
423>>> :set -XTypeApplications -XDataKinds
463>>> :set -XTypeApplications -XDataKinds
424−>>> let expr = nullIf false (param @1) :: Expression commons schemas '[ 'NotNull 'PGbool] grp from ('Null 'PGbool)
464+>>> let expr = nullIf false (param @1) :: Expression grp commons schemas '[ 'NotNull 'PGbool] from ('Null 'PGbool)
425>>> printSQL expr
465>>> printSQL expr
426NULL IF (FALSE, ($1 :: bool))
466NULL IF (FALSE, ($1 :: bool))
429− :: Expression commons schemas params grp from ('NotNull ty)
469+ :: Expression grp commons schemas params from ('NotNull ty)
430 -- ^ @NULL@ is absent
470 -- ^ @NULL@ is absent
431− -> Expression commons schemas params grp from ('NotNull ty)
471+ -> Expression grp commons schemas params from ('NotNull ty)
432 -- ^ @NULL@ is absent
472 -- ^ @NULL@ is absent
433− -> Expression commons schemas params grp from ('Null ty)
473+ -> Expression grp commons schemas params from ('Null ty)
434nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
474nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
435 (renderSQL x <> ", " <> renderSQL y)
475 (renderSQL x <> ", " <> renderSQL y)
437-- | >>> printSQL $ array [null_, false, true]
477-- | >>> printSQL $ array [null_, false, true]
438-- ARRAY[NULL, FALSE, TRUE]
478-- ARRAY[NULL, FALSE, TRUE]
440− :: [Expression commons schemas params grp from ty]
480+ :: [Expression grp commons schemas params from ty]
441 -- ^ array elements
481 -- ^ array elements
442− -> Expression commons schemas params grp from (nullity ('PGvararray ty))
482+ -> Expression grp commons schemas params from (nullity ('PGvararray ty))
443array xs = UnsafeExpression $
483array xs = UnsafeExpression $
444 "ARRAY[" <> commaSeparated (renderSQL <$> xs) <> "]"
484 "ARRAY[" <> commaSeparated (renderSQL <$> xs) <> "]"
@@ -447,13 +487,13 @@ array xs = UnsafeExpression $
447-- (ARRAY[NULL, FALSE, TRUE])[2]
487-- (ARRAY[NULL, FALSE, TRUE])[2]
449 :: Word64 -- ^ index
489 :: Word64 -- ^ index
450− -> Expression commons schemas params grp from (nullity ('PGvararray ty)) -- ^ array
490+ -> Expression grp commons schemas params from (nullity ('PGvararray ty)) -- ^ array
451− -> Expression commons schemas params grp from (NullifyType ty)
491+ -> Expression grp commons schemas params from (NullifyType ty)
452index n expr = UnsafeExpression $
492index n expr = UnsafeExpression $
453 parenthesized (renderSQL expr) <> "[" <> fromString (show n) <> "]"
493 parenthesized (renderSQL expr) <> "[" <> fromString (show n) <> "]"
455instance (KnownSymbol label, label `In` labels) => IsPGlabel label
495instance (KnownSymbol label, label `In` labels) => IsPGlabel label
456− (Expression commons schemas params grp from (nullity ('PGenum labels))) where
496+ (Expression grp commons schemas params from (nullity ('PGenum labels))) where
457 label = UnsafeExpression $ renderSQL (PGlabel @label)
497 label = UnsafeExpression $ renderSQL (PGlabel @label)
459-- | A row constructor is an expression that builds a row value
499-- | A row constructor is an expression that builds a row value
@@ -465,14 +505,14 @@ instance (KnownSymbol label, label `In` labels) => IsPGlabel label
465-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
505-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
468−-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression commons schemas from grp params ('NotNull Complex)
508+-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression grp commons schemas params from ('NotNull Complex)
469-- >>> printSQL i
509-- >>> printSQL i
470-- ROW(0, 1)
510-- ROW(0, 1)
472 :: SListI row
512 :: SListI row
473− => NP (Aliased (Expression commons schemas params grp from)) row
513+ => NP (Aliased (Expression grp commons schemas params from)) row
474 -- ^ zero or more expressions for the row field values
514 -- ^ zero or more expressions for the row field values
475− -> Expression commons schemas params grp from (nullity ('PGcomposite row))
515+ -> Expression grp commons schemas params from (nullity ('PGcomposite row))
476row exprs = UnsafeExpression $ "ROW" <> parenthesized
516row exprs = UnsafeExpression $ "ROW" <> parenthesized
477 (renderCommaSeparated (\ (expr `As` _) -> renderSQL expr) exprs)
517 (renderCommaSeparated (\ (expr `As` _) -> renderSQL expr) exprs)
@@ -483,7 +523,7 @@ row exprs = UnsafeExpression $ "ROW" <> parenthesized
483-- type Schema = '["complex" ::: 'Typedef Complex]
523-- type Schema = '["complex" ::: 'Typedef Complex]
486−-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression '[] (Public Schema) from grp params ('NotNull Complex)
526+-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression grp '[] (Public Schema) from params ('NotNull Complex)
487-- >>> printSQL $ i & field #complex #imaginary
527-- >>> printSQL $ i & field #complex #imaginary
488-- (ROW(0, 1)::"complex")."imaginary"
528-- (ROW(0, 1)::"complex")."imaginary"
@@ -492,41 +532,41 @@ field
492 , Has field row ty)
532 , Has field row ty)
493 => QualifiedAlias sch tydef -- ^ row type
533 => QualifiedAlias sch tydef -- ^ row type
494 -> Alias field -- ^ field name
534 -> Alias field -- ^ field name
495− -> Expression commons schemas params grp from ('NotNull ('PGcomposite row))
535+ -> Expression grp commons schemas params from ('NotNull ('PGcomposite row))
496− -> Expression commons schemas params grp from ty
536+ -> Expression grp commons schemas params from ty
497field td fld expr = UnsafeExpression $
537field td fld expr = UnsafeExpression $
498 parenthesized (renderSQL expr <> "::" <> renderSQL td)
538 parenthesized (renderSQL expr <> "::" <> renderSQL td)
499 <> "." <> renderSQL fld
539 <> "." <> renderSQL fld
501instance Semigroup
541instance Semigroup
502− (Expression commons schemas params grp from (nullity ('PGvararray ty))) where
542+ (Expression grp commons schemas params from (nullity ('PGvararray ty))) where
503 (<>) = unsafeBinaryOp "||"
543 (<>) = unsafeBinaryOp "||"
505instance Monoid
545instance Monoid
506− (Expression commons schemas params grp from (nullity ('PGvararray ty))) where
546+ (Expression grp commons schemas params from (nullity ('PGvararray ty))) where
507 mempty = array []
547 mempty = array []
508 mappend = (<>)
548 mappend = (<>)
510−-- | >>> let expr = greatest currentTimestamp [param @1] :: Expression commons schemas '[ 'NotNull 'PGtimestamptz] grp from ('NotNull 'PGtimestamptz)
550+-- | >>> let expr = greatest currentTimestamp [param @1] :: Expression grp commons schemas '[ 'NotNull 'PGtimestamptz] from ('NotNull 'PGtimestamptz)
511-- >>> printSQL expr
551-- >>> printSQL expr
512-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
552-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
514− :: Expression commons schemas params grp from (nullty)
554+ :: Expression grp commons schemas params from (nullty)
515 -- ^ needs at least 1 argument
555 -- ^ needs at least 1 argument
516− -> [Expression commons schemas params grp from (nullty)]
556+ -> [Expression grp commons schemas params from (nullty)]
517 -- ^ or more
557 -- ^ or more
518− -> Expression commons schemas params grp from (nullty)
558+ -> Expression grp commons schemas params from (nullty)
519greatest x xs = UnsafeExpression $ "GREATEST("
559greatest x xs = UnsafeExpression $ "GREATEST("
520 <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
560 <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
522-- | >>> printSQL $ least currentTimestamp [null_]
562-- | >>> printSQL $ least currentTimestamp [null_]
523-- LEAST(CURRENT_TIMESTAMP, NULL)
563-- LEAST(CURRENT_TIMESTAMP, NULL)
525− :: Expression commons schemas params grp from (nullty)
565+ :: Expression grp commons schemas params from (nullty)
526 -- ^ needs at least 1 argument
566 -- ^ needs at least 1 argument
527− -> [Expression commons schemas params grp from (nullty)]
567+ -> [Expression grp commons schemas params from (nullty)]
528 -- ^ or more
568 -- ^ or more
529− -> Expression commons schemas params grp from (nullty)
569+ -> Expression grp commons schemas params from (nullty)
530least x xs = UnsafeExpression $ "LEAST("
570least x xs = UnsafeExpression $ "LEAST("
531 <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
571 <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
@@ -535,9 +575,9 @@ least x xs = UnsafeExpression $ "LEAST("
535unsafeBinaryOp
575unsafeBinaryOp
536 :: ByteString
576 :: ByteString
537 -- ^ operator
577 -- ^ operator
538− -> Expression commons schemas params grp from (ty0)
578+ -> Expression grp commons schemas params from (ty0)
539− -> Expression commons schemas params grp from (ty1)
579+ -> Expression grp commons schemas params from (ty1)
540− -> Expression commons schemas params grp from (ty2)
580+ -> Expression grp commons schemas params from (ty2)
541unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
581unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
542 renderSQL x <+> op <+> renderSQL y
582 renderSQL x <+> op <+> renderSQL y
@@ -546,8 +586,8 @@ unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
546unsafeUnaryOp
586unsafeUnaryOp
547 :: ByteString
587 :: ByteString
548 -- ^ operator
588 -- ^ operator
549− -> Expression commons schemas params grp from (ty0)
589+ -> Expression grp commons schemas params from (ty0)
550− -> Expression commons schemas params grp from (ty1)
590+ -> Expression grp commons schemas params from (ty1)
551unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
591unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
552 op <+> renderSQL x
592 op <+> renderSQL x
@@ -556,8 +596,8 @@ unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
556unsafeFunction
596unsafeFunction
557 :: ByteString
597 :: ByteString
558 -- ^ function
598 -- ^ function
559− -> Expression commons schemas params grp from (xty)
599+ -> Expression grp commons schemas params from (xty)
560− -> Expression commons schemas params grp from (yty)
600+ -> Expression grp commons schemas params from (yty)
561unsafeFunction fun x = UnsafeExpression $
601unsafeFunction fun x = UnsafeExpression $
562 fun <> parenthesized (renderSQL x)
602 fun <> parenthesized (renderSQL x)
@@ -566,13 +606,13 @@ unsafeVariadicFunction
566 :: SListI elems
606 :: SListI elems
567 => ByteString
607 => ByteString
568 -- ^ function
608 -- ^ function
569− -> NP (Expression commons schemas params grp from) elems
609+ -> NP (Expression grp commons schemas params from) elems
570− -> Expression commons schemas params grp from ret
610+ -> Expression grp commons schemas params from ret
571unsafeVariadicFunction fun x = UnsafeExpression $
611unsafeVariadicFunction fun x = UnsafeExpression $
572 fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderSQL) x)))
612 fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderSQL) x)))
574instance ty `In` PGNum
614instance ty `In` PGNum
575− => Num (Expression commons schemas params grp from (nullity ty)) where
615+ => Num (Expression grp commons schemas params from (nullity ty)) where
576 (+) = unsafeBinaryOp "+"
616 (+) = unsafeBinaryOp "+"
577 (-) = unsafeBinaryOp "-"
617 (-) = unsafeBinaryOp "-"
578 (*) = unsafeBinaryOp "*"
618 (*) = unsafeBinaryOp "*"
@@ -584,12 +624,12 @@ instance ty `In` PGNum
586instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
626instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
587− (Expression commons schemas params grp from (nullity ty)) where
627+ (Expression grp commons schemas params from (nullity ty)) where
588 (/) = unsafeBinaryOp "/"
628 (/) = unsafeBinaryOp "/"
589 fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
629 fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
591instance (ty `In` PGNum, ty `In` PGFloating) => Floating
631instance (ty `In` PGNum, ty `In` PGFloating) => Floating
592− (Expression commons schemas params grp from (nullity ty)) where
632+ (Expression grp commons schemas params from (nullity ty)) where
593 pi = UnsafeExpression "pi()"
633 pi = UnsafeExpression "pi()"
594 exp = unsafeFunction "exp"
634 exp = unsafeFunction "exp"
595 log = unsafeFunction "ln"
635 log = unsafeFunction "ln"
@@ -612,18 +652,18 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Floating
613-- | >>> :{
653-- | >>> :{
615−-- expression :: Expression commons schemas params grp from (nullity 'PGfloat4)
655+-- expression :: Expression grp commons schemas params from (nullity 'PGfloat4)
616-- expression = atan2_ pi 2
656-- expression = atan2_ pi 2
617-- in printSQL expression
657-- in printSQL expression
619-- atan2(pi(), 2)
659-- atan2(pi(), 2)
621 :: float `In` PGFloating
661 :: float `In` PGFloating
622− => Expression commons schemas params grp from (nullity float)
662+ => Expression grp commons schemas params from (nullity float)
623 -- ^ numerator
663 -- ^ numerator
624− -> Expression commons schemas params grp from (nullity float)
664+ -> Expression grp commons schemas params from (nullity float)
625 -- ^ denominator
665 -- ^ denominator
626− -> Expression commons schemas params grp from (nullity float)
666+ -> Expression grp commons schemas params from (nullity float)
627atan2_ y x = UnsafeExpression $
667atan2_ y x = UnsafeExpression $
628 "atan2(" <> renderSQL y <> ", " <> renderSQL x <> ")"
668 "atan2(" <> renderSQL y <> ", " <> renderSQL x <> ")"
@@ -636,9 +676,9 @@ atan2_ y x = UnsafeExpression $
637 :: TypeExpression schemas ty1
677 :: TypeExpression schemas ty1
638 -- ^ type to cast as
678 -- ^ type to cast as
639− -> Expression commons schemas params grp from ty0
679+ -> Expression grp commons schemas params from ty0
640 -- ^ value to convert
680 -- ^ value to convert
641− -> Expression commons schemas params grp from ty1
681+ -> Expression grp commons schemas params from ty1
642cast ty x = UnsafeExpression $ parenthesized $
682cast ty x = UnsafeExpression $ parenthesized $
643 renderSQL x <+> "::" <+> renderSQL ty
683 renderSQL x <+> "::" <+> renderSQL ty
@@ -646,136 +686,136 @@ cast ty x = UnsafeExpression $ parenthesized $
649−-- expression :: Expression commons schemas params grp from (nullity 'PGint2)
689+-- expression :: Expression grp commons schemas params from (nullity 'PGint2)
650-- expression = 5 `quot_` 2
690-- expression = 5 `quot_` 2
651-- in printSQL expression
691-- in printSQL expression
653-- (5 / 2)
693-- (5 / 2)
655 :: int `In` PGIntegral
695 :: int `In` PGIntegral
656− => Expression commons schemas params grp from (nullity int)
696+ => Expression grp commons schemas params from (nullity int)
657 -- ^ numerator
697 -- ^ numerator
658− -> Expression commons schemas params grp from (nullity int)
698+ -> Expression grp commons schemas params from (nullity int)
659 -- ^ denominator
699 -- ^ denominator
660− -> Expression commons schemas params grp from (nullity int)
700+ -> Expression grp commons schemas params from (nullity int)
661quot_ = unsafeBinaryOp "/"
701quot_ = unsafeBinaryOp "/"
663-- | remainder upon integer division
703-- | remainder upon integer division
667−-- expression :: Expression commons schemas params grp from (nullity 'PGint2)
707+-- expression :: Expression grp commons schemas params from (nullity 'PGint2)
668-- expression = 5 `rem_` 2
708-- expression = 5 `rem_` 2
669-- in printSQL expression
709-- in printSQL expression
671-- (5 % 2)
711-- (5 % 2)
673 :: int `In` PGIntegral
713 :: int `In` PGIntegral
674− => Expression commons schemas params grp from (nullity int)
714+ => Expression grp commons schemas params from (nullity int)
675 -- ^ numerator
715 -- ^ numerator
676− -> Expression commons schemas params grp from (nullity int)
716+ -> Expression grp commons schemas params from (nullity int)
677 -- ^ denominator
717 -- ^ denominator
678− -> Expression commons schemas params grp from (nullity int)
718+ -> Expression grp commons schemas params from (nullity int)
679rem_ = unsafeBinaryOp "%"
719rem_ = unsafeBinaryOp "%"
681-- | >>> :{
721-- | >>> :{
683−-- expression :: Expression commons schemas params grp from (nullity 'PGfloat4)
723+-- expression :: Expression grp commons schemas params from (nullity 'PGfloat4)
684-- expression = trunc pi
724-- expression = trunc pi
685-- in printSQL expression
725-- in printSQL expression
687-- trunc(pi())
727-- trunc(pi())
689 :: frac `In` PGFloating
729 :: frac `In` PGFloating
690− => Expression commons schemas params grp from (nullity frac)
730+ => Expression grp commons schemas params from (nullity frac)
691 -- ^ fractional number
731 -- ^ fractional number
692− -> Expression commons schemas params grp from (nullity frac)
732+ -> Expression grp commons schemas params from (nullity frac)
693trunc = unsafeFunction "trunc"
733trunc = unsafeFunction "trunc"
695-- | >>> :{
735-- | >>> :{
697−-- expression :: Expression commons schemas params grp from (nullity 'PGfloat4)
737+-- expression :: Expression grp commons schemas params from (nullity 'PGfloat4)
698-- expression = round_ pi
738-- expression = round_ pi
699-- in printSQL expression
739-- in printSQL expression
701-- round(pi())
741-- round(pi())
703 :: frac `In` PGFloating
743 :: frac `In` PGFloating
704− => Expression commons schemas params grp from (nullity frac)
744+ => Expression grp commons schemas params from (nullity frac)
705 -- ^ fractional number
745 -- ^ fractional number
706− -> Expression commons schemas params grp from (nullity frac)
746+ -> Expression grp commons schemas params from (nullity frac)
707round_ = unsafeFunction "round"
747round_ = unsafeFunction "round"
709-- | >>> :{
749-- | >>> :{
711−-- expression :: Expression commons schemas params grp from (nullity 'PGfloat4)
751+-- expression :: Expression grp commons schemas params from (nullity 'PGfloat4)
712-- expression = ceiling_ pi
752-- expression = ceiling_ pi
713-- in printSQL expression
753-- in printSQL expression
715-- ceiling(pi())
755-- ceiling(pi())
717 :: frac `In` PGFloating
757 :: frac `In` PGFloating
718− => Expression commons schemas params grp from (nullity frac)
758+ => Expression grp commons schemas params from (nullity frac)
719 -- ^ fractional number
759 -- ^ fractional number
720− -> Expression commons schemas params grp from (nullity frac)
760+ -> Expression grp commons schemas params from (nullity frac)
721ceiling_ = unsafeFunction "ceiling"
761ceiling_ = unsafeFunction "ceiling"
723-- | A `Condition` is an `Expression`, which can evaluate
763-- | A `Condition` is an `Expression`, which can evaluate
724-- to `true`, `false` or `null_`. This is because SQL uses
764-- to `true`, `false` or `null_`. This is because SQL uses
725-- a three valued logic.
765-- a three valued logic.
726−type Condition commons schemas params grp from =
766+type Condition grp commons schemas params from =
727− Expression commons schemas params grp from ('Null 'PGbool)
767+ Expression grp commons schemas params from ('Null 'PGbool)
729-- | >>> printSQL true
769-- | >>> printSQL true
731−true :: Expression commons schemas params grp from (nullity 'PGbool)
771+true :: Expression grp commons schemas params from (nullity 'PGbool)
732true = UnsafeExpression "TRUE"
772true = UnsafeExpression "TRUE"
734-- | >>> printSQL false
774-- | >>> printSQL false
736−false :: Expression commons schemas params grp from (nullity 'PGbool)
776+false :: Expression grp commons schemas params from (nullity 'PGbool)
737false = UnsafeExpression "FALSE"
777false = UnsafeExpression "FALSE"
739-- | >>> printSQL $ not_ true
779-- | >>> printSQL $ not_ true
740-- (NOT TRUE)
780-- (NOT TRUE)
742− :: Expression commons schemas params grp from (nullity 'PGbool)
782+ :: Expression grp commons schemas params from (nullity 'PGbool)
743− -> Expression commons schemas params grp from (nullity 'PGbool)
783+ -> Expression grp commons schemas params from (nullity 'PGbool)
744not_ = unsafeUnaryOp "NOT"
784not_ = unsafeUnaryOp "NOT"
746-- | >>> printSQL $ true .&& false
786-- | >>> printSQL $ true .&& false
747-- (TRUE AND FALSE)
787-- (TRUE AND FALSE)
749− :: Expression commons schemas params grp from (nullity 'PGbool)
789+ :: Expression grp commons schemas params from (nullity 'PGbool)
750− -> Expression commons schemas params grp from (nullity 'PGbool)
790+ -> Expression grp commons schemas params from (nullity 'PGbool)
751− -> Expression commons schemas params grp from (nullity 'PGbool)
791+ -> Expression grp commons schemas params from (nullity 'PGbool)
752infixr 3 .&&
792infixr 3 .&&
753(.&&) = unsafeBinaryOp "AND"
793(.&&) = unsafeBinaryOp "AND"
755-- | >>> printSQL $ true .|| false
795-- | >>> printSQL $ true .|| false
756-- (TRUE OR FALSE)
796-- (TRUE OR FALSE)
758− :: Expression commons schemas params grp from (nullity 'PGbool)
798+ :: Expression grp commons schemas params from (nullity 'PGbool)
759− -> Expression commons schemas params grp from (nullity 'PGbool)
799+ -> Expression grp commons schemas params from (nullity 'PGbool)
760− -> Expression commons schemas params grp from (nullity 'PGbool)
800+ -> Expression grp commons schemas params from (nullity 'PGbool)
761infixr 2 .||
801infixr 2 .||
762(.||) = unsafeBinaryOp "OR"
802(.||) = unsafeBinaryOp "OR"
764-- | >>> :{
804-- | >>> :{
766−-- expression :: Expression commons schemas params grp from (nullity 'PGint2)
806+-- expression :: Expression grp commons schemas params from (nullity 'PGint2)
767-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
807-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
768-- in printSQL expression
808-- in printSQL expression
770-- CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END
810-- CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END
771caseWhenThenElse
811caseWhenThenElse
772− :: [ ( Condition commons schemas params grp from
812+ :: [ ( Condition grp commons schemas params from
773− , Expression commons schemas params grp from ty
813+ , Expression grp commons schemas params from ty
775 -- ^ whens and thens
815 -- ^ whens and thens
776− -> Expression commons schemas params grp from ty
816+ -> Expression grp commons schemas params from ty
777 -- ^ else
817 -- ^ else
778− -> Expression commons schemas params grp from ty
818+ -> Expression grp commons schemas params from ty
779caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
819caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
781 , mconcat
821 , mconcat
@@ -791,16 +831,16 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
792-- | >>> :{
832-- | >>> :{
794−-- expression :: Expression commons schemas params grp from (nullity 'PGint2)
834+-- expression :: Expression grp commons schemas params from (nullity 'PGint2)
795-- expression = ifThenElse true 1 0
835-- expression = ifThenElse true 1 0
796-- in printSQL expression
836-- in printSQL expression
798-- CASE WHEN TRUE THEN 1 ELSE 0 END
838-- CASE WHEN TRUE THEN 1 ELSE 0 END
799ifThenElse
839ifThenElse
800− :: Condition commons schemas params grp from
840+ :: Condition grp commons schemas params from
801− -> Expression commons schemas params grp from ty -- ^ then
841+ -> Expression grp commons schemas params from ty -- ^ then
802− -> Expression commons schemas params grp from ty -- ^ else
842+ -> Expression grp commons schemas params from ty -- ^ else
803− -> Expression commons schemas params grp from ty
843+ -> Expression grp commons schemas params from ty
804ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
844ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
806-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
846-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
@@ -809,85 +849,85 @@ ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
809-- >>> printSQL $ true .== null_
849-- >>> printSQL $ true .== null_
810-- (TRUE = NULL)
850-- (TRUE = NULL)
812− :: Expression commons schemas params grp from (nullity0 ty) -- ^ lhs
852+ :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
813− -> Expression commons schemas params grp from (nullity1 ty) -- ^ rhs
853+ -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
814− -> Condition commons schemas params grp from
854+ -> Condition grp commons schemas params from
815(.==) = unsafeBinaryOp "="
855(.==) = unsafeBinaryOp "="
816infix 4 .==
856infix 4 .==
818-- | >>> printSQL $ true ./= null_
858-- | >>> printSQL $ true ./= null_
819-- (TRUE <> NULL)
859-- (TRUE <> NULL)
821− :: Expression commons schemas params grp from (nullity0 ty) -- ^ lhs
861+ :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
822− -> Expression commons schemas params grp from (nullity1 ty) -- ^ rhs
862+ -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
823− -> Condition commons schemas params grp from
863+ -> Condition grp commons schemas params from
824(./=) = unsafeBinaryOp "<>"
864(./=) = unsafeBinaryOp "<>"
825infix 4 ./=
865infix 4 ./=
827-- | >>> printSQL $ true .>= null_
867-- | >>> printSQL $ true .>= null_
828-- (TRUE >= NULL)
868-- (TRUE >= NULL)
830− :: Expression commons schemas params grp from (nullity0 ty) -- ^ lhs
870+ :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
831− -> Expression commons schemas params grp from (nullity1 ty) -- ^ rhs
871+ -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
832− -> Condition commons schemas params grp from
872+ -> Condition grp commons schemas params from
833(.>=) = unsafeBinaryOp ">="
873(.>=) = unsafeBinaryOp ">="
834infix 4 .>=
874infix 4 .>=
836-- | >>> printSQL $ true .< null_
876-- | >>> printSQL $ true .< null_
837-- (TRUE < NULL)
877-- (TRUE < NULL)
839− :: Expression commons schemas params grp from (nullity0 ty) -- ^ lhs
879+ :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
840− -> Expression commons schemas params grp from (nullity1 ty) -- ^ rhs
880+ -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
841− -> Condition commons schemas params grp from
881+ -> Condition grp commons schemas params from
842(.<) = unsafeBinaryOp "<"
882(.<) = unsafeBinaryOp "<"
843infix 4 .<
883infix 4 .<
845-- | >>> printSQL $ true .<= null_
885-- | >>> printSQL $ true .<= null_
846-- (TRUE <= NULL)
886-- (TRUE <= NULL)
848− :: Expression commons schemas params grp from (nullity0 ty) -- ^ lhs
888+ :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
849− -> Expression commons schemas params grp from (nullity1 ty) -- ^ rhs
889+ -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
850− -> Condition commons schemas params grp from
890+ -> Condition grp commons schemas params from
851(.<=) = unsafeBinaryOp "<="
891(.<=) = unsafeBinaryOp "<="
852infix 4 .<=
892infix 4 .<=
854-- | >>> printSQL $ true .> null_
894-- | >>> printSQL $ true .> null_
855-- (TRUE > NULL)
895-- (TRUE > NULL)
857− :: Expression commons schemas params grp from (nullity0 ty) -- ^ lhs
897+ :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
858− -> Expression commons schemas params grp from (nullity1 ty) -- ^ rhs
898+ -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
859− -> Condition commons schemas params grp from
899+ -> Condition grp commons schemas params from
860(.>) = unsafeBinaryOp ">"
900(.>) = unsafeBinaryOp ">"
861infix 4 .>
901infix 4 .>
863-- | >>> printSQL currentDate
903-- | >>> printSQL currentDate
864-- CURRENT_DATE
904-- CURRENT_DATE
865currentDate
905currentDate
866− :: Expression commons schemas params grp from (nullity 'PGdate)
906+ :: Expression grp commons schemas params from (nullity 'PGdate)
867currentDate = UnsafeExpression "CURRENT_DATE"
907currentDate = UnsafeExpression "CURRENT_DATE"
869-- | >>> printSQL currentTime
909-- | >>> printSQL currentTime
870-- CURRENT_TIME
910-- CURRENT_TIME
871currentTime
911currentTime
872− :: Expression commons schemas params grp from (nullity 'PGtimetz)
912+ :: Expression grp commons schemas params from (nullity 'PGtimetz)
873currentTime = UnsafeExpression "CURRENT_TIME"
913currentTime = UnsafeExpression "CURRENT_TIME"
875-- | >>> printSQL currentTimestamp
915-- | >>> printSQL currentTimestamp
876-- CURRENT_TIMESTAMP
916-- CURRENT_TIMESTAMP
877currentTimestamp
917currentTimestamp
878− :: Expression commons schemas params grp from (nullity 'PGtimestamptz)
918+ :: Expression grp commons schemas params from (nullity 'PGtimestamptz)
879currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
919currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
881-- | >>> printSQL localTime
921-- | >>> printSQL localTime
882-- LOCALTIME
922-- LOCALTIME
884− :: Expression commons schemas params grp from (nullity 'PGtime)
924+ :: Expression grp commons schemas params from (nullity 'PGtime)
885localTime = UnsafeExpression "LOCALTIME"
925localTime = UnsafeExpression "LOCALTIME"
887-- | >>> printSQL localTimestamp
927-- | >>> printSQL localTimestamp
888-- LOCALTIMESTAMP
928-- LOCALTIMESTAMP
889localTimestamp
929localTimestamp
890− :: Expression commons schemas params grp from (nullity 'PGtimestamp)
930+ :: Expression grp commons schemas params from (nullity 'PGtimestamp)
891localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
931localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
893{-----------------------------------------
933{-----------------------------------------
@@ -895,7 +935,7 @@ text
895-----------------------------------------}
935-----------------------------------------}
897instance IsString
937instance IsString
898− (Expression commons schemas params grp from (nullity 'PGtext)) where
938+ (Expression grp commons schemas params from (nullity 'PGtext)) where
899 fromString str = UnsafeExpression $
939 fromString str = UnsafeExpression $
900 "E\'" <> fromString (escape =<< str) <> "\'"
940 "E\'" <> fromString (escape =<< str) <> "\'"
@@ -911,36 +951,36 @@ instance IsString
913instance Semigroup
953instance Semigroup
914− (Expression commons schemas params grp from (nullity 'PGtext)) where
954+ (Expression grp commons schemas params from (nullity 'PGtext)) where
915 (<>) = unsafeBinaryOp "||"
955 (<>) = unsafeBinaryOp "||"
917instance Monoid
957instance Monoid
918− (Expression commons schemas params grp from (nullity 'PGtext)) where
958+ (Expression grp commons schemas params from (nullity 'PGtext)) where
919 mempty = fromString ""
959 mempty = fromString ""
920 mappend = (<>)
960 mappend = (<>)
922-- | >>> printSQL $ lower "ARRRGGG"
962-- | >>> printSQL $ lower "ARRRGGG"
923-- lower(E'ARRRGGG')
963-- lower(E'ARRRGGG')
925− :: Expression commons schemas params grp from (nullity 'PGtext)
965+ :: Expression grp commons schemas params from (nullity 'PGtext)
926 -- ^ string to lower case
966 -- ^ string to lower case
927− -> Expression commons schemas params grp from (nullity 'PGtext)
967+ -> Expression grp commons schemas params from (nullity 'PGtext)
928lower = unsafeFunction "lower"
968lower = unsafeFunction "lower"
930-- | >>> printSQL $ upper "eeee"
970-- | >>> printSQL $ upper "eeee"
931-- upper(E'eeee')
971-- upper(E'eeee')
933− :: Expression commons schemas params grp from (nullity 'PGtext)
973+ :: Expression grp commons schemas params from (nullity 'PGtext)
934 -- ^ string to upper case
974 -- ^ string to upper case
935− -> Expression commons schemas params grp from (nullity 'PGtext)
975+ -> Expression grp commons schemas params from (nullity 'PGtext)
936upper = unsafeFunction "upper"
976upper = unsafeFunction "upper"
938-- | >>> printSQL $ charLength "four"
978-- | >>> printSQL $ charLength "four"
939-- char_length(E'four')
979-- char_length(E'four')
940charLength
980charLength
941− :: Expression commons schemas params grp from (nullity 'PGtext)
981+ :: Expression grp commons schemas params from (nullity 'PGtext)
942 -- ^ string to measure
982 -- ^ string to measure
943− -> Expression commons schemas params grp from (nullity 'PGint4)
983+ -> Expression grp commons schemas params from (nullity 'PGint4)
944charLength = unsafeFunction "char_length"
984charLength = unsafeFunction "char_length"
946-- | The `like` expression returns true if the @string@ matches
986-- | The `like` expression returns true if the @string@ matches
@@ -953,11 +993,11 @@ charLength = unsafeFunction "char_length"
953-- >>> printSQL $ "abc" `like` "a%"
993-- >>> printSQL $ "abc" `like` "a%"
954-- (E'abc' LIKE E'a%')
994-- (E'abc' LIKE E'a%')
956− :: Expression commons schemas params grp from (nullity 'PGtext)
996+ :: Expression grp commons schemas params from (nullity 'PGtext)
957 -- ^ string
997 -- ^ string
958− -> Expression commons schemas params grp from (nullity 'PGtext)
998+ -> Expression grp commons schemas params from (nullity 'PGtext)
959 -- ^ pattern
999 -- ^ pattern
960− -> Expression commons schemas params grp from (nullity 'PGbool)
1000+ -> Expression grp commons schemas params from (nullity 'PGbool)
961like = unsafeBinaryOp "LIKE"
1001like = unsafeBinaryOp "LIKE"
963-- | The key word ILIKE can be used instead of LIKE to make the
1003-- | The key word ILIKE can be used instead of LIKE to make the
@@ -966,11 +1006,11 @@ like = unsafeBinaryOp "LIKE"
966-- >>> printSQL $ "abc" `ilike` "a%"
1006-- >>> printSQL $ "abc" `ilike` "a%"
967-- (E'abc' ILIKE E'a%')
1007-- (E'abc' ILIKE E'a%')
969− :: Expression commons schemas params grp from (nullity 'PGtext)
1009+ :: Expression grp commons schemas params from (nullity 'PGtext)
970 -- ^ string
1010 -- ^ string
971− -> Expression commons schemas params grp from (nullity 'PGtext)
1011+ -> Expression grp commons schemas params from (nullity 'PGtext)
972 -- ^ pattern
1012 -- ^ pattern
973− -> Expression commons schemas params grp from (nullity 'PGbool)
1013+ -> Expression grp commons schemas params from (nullity 'PGbool)
974ilike = unsafeBinaryOp "ILIKE"
1014ilike = unsafeBinaryOp "ILIKE"
976{-----------------------------------------
1016{-----------------------------------------
@@ -985,36 +1025,36 @@ Table 9.44: json and jsonb operators
985-- | Get JSON value (object field or array element) at a key.
1025-- | Get JSON value (object field or array element) at a key.
987 :: (json `In` PGJsonType, key `In` PGJsonKey)
1027 :: (json `In` PGJsonType, key `In` PGJsonKey)
988− => Expression commons schemas params grp from (nullity json)
1028+ => Expression grp commons schemas params from (nullity json)
989− -> Expression commons schemas params grp from (nullity key)
1029+ -> Expression grp commons schemas params from (nullity key)
990− -> Expression commons schemas params grp from ('Null json)
1030+ -> Expression grp commons schemas params from ('Null json)
991infixl 8 .->
1031infixl 8 .->
992(.->) = unsafeBinaryOp "->"
1032(.->) = unsafeBinaryOp "->"
994-- | Get JSON value (object field or array element) at a key, as text.
1034-- | Get JSON value (object field or array element) at a key, as text.
996 :: (json `In` PGJsonType, key `In` PGJsonKey)
1036 :: (json `In` PGJsonType, key `In` PGJsonKey)
997− => Expression commons schemas params grp from (nullity json)
1037+ => Expression grp commons schemas params from (nullity json)
998− -> Expression commons schemas params grp from (nullity key)
1038+ -> Expression grp commons schemas params from (nullity key)
999− -> Expression commons schemas params grp from ('Null 'PGtext)
1039+ -> Expression grp commons schemas params from ('Null 'PGtext)
1000infixl 8 .->>
1040infixl 8 .->>
1001(.->>) = unsafeBinaryOp "->>"
1041(.->>) = unsafeBinaryOp "->>"
1003-- | Get JSON value at a specified path.
1043-- | Get JSON value at a specified path.
1005 :: (json `In` PGJsonType, PGTextArray "(.#>)" path)
1045 :: (json `In` PGJsonType, PGTextArray "(.#>)" path)
1006− => Expression commons schemas params grp from (nullity json)
1046+ => Expression grp commons schemas params from (nullity json)
1007− -> Expression commons schemas params grp from (nullity path)
1047+ -> Expression grp commons schemas params from (nullity path)
1008− -> Expression commons schemas params grp from ('Null json)
1048+ -> Expression grp commons schemas params from ('Null json)
1009infixl 8 .#>
1049infixl 8 .#>
1010(.#>) = unsafeBinaryOp "#>"
1050(.#>) = unsafeBinaryOp "#>"
1012-- | Get JSON value at a specified path as text.
1052-- | Get JSON value at a specified path as text.
1014 :: (json `In` PGJsonType, PGTextArray "(.#>>)" path)
1054 :: (json `In` PGJsonType, PGTextArray "(.#>>)" path)
1015− => Expression commons schemas params grp from (nullity json)
1055+ => Expression grp commons schemas params from (nullity json)
1016− -> Expression commons schemas params grp from (nullity path)
1056+ -> Expression grp commons schemas params from (nullity path)
1017− -> Expression commons schemas params grp from ('Null 'PGtext)
1057+ -> Expression grp commons schemas params from ('Null 'PGtext)
1018infixl 8 .#>>
1058infixl 8 .#>>
1019(.#>>) = unsafeBinaryOp "#>>"
1059(.#>>) = unsafeBinaryOp "#>>"
@@ -1023,42 +1063,42 @@ infixl 8 .#>>
1023-- | Does the left JSON value contain the right JSON path/value entries at the
1063-- | Does the left JSON value contain the right JSON path/value entries at the
1024-- top level?
1064-- top level?
1026− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1066+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1027− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1067+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1028− -> Condition commons schemas params grp from
1068+ -> Condition grp commons schemas params from
1029infixl 9 .@>
1069infixl 9 .@>
1030(.@>) = unsafeBinaryOp "@>"
1070(.@>) = unsafeBinaryOp "@>"
1032-- | Are the left JSON path/value entries contained at the top level within the
1072-- | Are the left JSON path/value entries contained at the top level within the
1033-- right JSON value?
1073-- right JSON value?
1035− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1075+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1036− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1076+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1037− -> Condition commons schemas params grp from
1077+ -> Condition grp commons schemas params from
1038infixl 9 .<@
1078infixl 9 .<@
1039(.<@) = unsafeBinaryOp "<@"
1079(.<@) = unsafeBinaryOp "<@"
1041-- | Does the string exist as a top-level key within the JSON value?
1081-- | Does the string exist as a top-level key within the JSON value?
1043− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1083+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1044− -> Expression commons schemas params grp from (nullity 'PGtext)
1084+ -> Expression grp commons schemas params from (nullity 'PGtext)
1045− -> Condition commons schemas params grp from
1085+ -> Condition grp commons schemas params from
1046infixl 9 .?
1086infixl 9 .?
1047(.?) = unsafeBinaryOp "?"
1087(.?) = unsafeBinaryOp "?"
1049-- | Do any of these array strings exist as top-level keys?
1089-- | Do any of these array strings exist as top-level keys?
1051− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1091+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1052− -> Expression commons schemas params grp from (nullity ('PGvararray ('NotNull 'PGtext)))
1092+ -> Expression grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1053− -> Condition commons schemas params grp from
1093+ -> Condition grp commons schemas params from
1054infixl 9 .?|
1094infixl 9 .?|
1055(.?|) = unsafeBinaryOp "?|"
1095(.?|) = unsafeBinaryOp "?|"
1057-- | Do all of these array strings exist as top-level keys?
1097-- | Do all of these array strings exist as top-level keys?
1059− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1099+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1060− -> Expression commons schemas params grp from (nullity ('PGvararray ('NotNull 'PGtext)))
1100+ -> Expression grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1061− -> Condition commons schemas params grp from
1101+ -> Condition grp commons schemas params from
1062infixl 9 .?&
1102infixl 9 .?&
1063(.?&) = unsafeBinaryOp "?&"
1103(.?&) = unsafeBinaryOp "?&"
@@ -1081,9 +1121,9 @@ instance
1081-- count from the end). Throws an error if top level container is not an array.
1121-- count from the end). Throws an error if top level container is not an array.
1083 :: (key `In` '[ 'PGtext, 'PGvararray ('NotNull 'PGtext), 'PGint4, 'PGint2 ]) -- hlint error without parens here
1123 :: (key `In` '[ 'PGtext, 'PGvararray ('NotNull 'PGtext), 'PGint4, 'PGint2 ]) -- hlint error without parens here
1084− => Expression commons schemas params grp from (nullity 'PGjsonb)
1124+ => Expression grp commons schemas params from (nullity 'PGjsonb)
1085− -> Expression commons schemas params grp from (nullity key)
1125+ -> Expression grp commons schemas params from (nullity key)
1086− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1126+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1087infixl 6 .-.
1127infixl 6 .-.
1088(.-.) = unsafeBinaryOp "-"
1128(.-.) = unsafeBinaryOp "-"
@@ -1091,9 +1131,9 @@ infixl 6 .-.
1091-- integers count from the end)
1131-- integers count from the end)
1093 :: PGTextArray "(#-.)" arrayty
1133 :: PGTextArray "(#-.)" arrayty
1094− => Expression commons schemas params grp from (nullity 'PGjsonb)
1134+ => Expression grp commons schemas params from (nullity 'PGjsonb)
1095− -> Expression commons schemas params grp from (nullity arrayty)
1135+ -> Expression grp commons schemas params from (nullity arrayty)
1096− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1136+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1097infixl 6 #-.
1137infixl 6 #-.
1098(#-.) = unsafeBinaryOp "#-"
1138(#-.) = unsafeBinaryOp "#-"
@@ -1104,14 +1144,14 @@ Table 9.45: JSON creation functions
1104-- | Literal binary JSON
1144-- | Literal binary JSON
1106 :: JSON.ToJSON x
1146 :: JSON.ToJSON x
1107− => x -> Expression commons schemas params grp from (nullity 'PGjsonb)
1147+ => x -> Expression grp commons schemas params from (nullity 'PGjsonb)
1108jsonbLit = cast jsonb . UnsafeExpression
1148jsonbLit = cast jsonb . UnsafeExpression
1109 . singleQuotedUtf8 . toStrict . JSON.encode
1149 . singleQuotedUtf8 . toStrict . JSON.encode
1111-- | Literal JSON
1151-- | Literal JSON
1113 :: JSON.ToJSON x
1153 :: JSON.ToJSON x
1114− => x -> Expression commons schemas params grp from (nullity 'PGjson)
1154+ => x -> Expression grp commons schemas params from (nullity 'PGjson)
1115jsonLit = cast json . UnsafeExpression
1155jsonLit = cast json . UnsafeExpression
1116 . singleQuotedUtf8 . toStrict . JSON.encode
1156 . singleQuotedUtf8 . toStrict . JSON.encode
@@ -1122,8 +1162,8 @@ jsonLit = cast json . UnsafeExpression
1122-- number, a Boolean, or a null value, the text representation will be used, in
1162-- number, a Boolean, or a null value, the text representation will be used, in
1123-- such a fashion that it is a valid json value.
1163-- such a fashion that it is a valid json value.
1125− :: Expression commons schemas params grp from (nullity ty)
1165+ :: Expression grp commons schemas params from (nullity ty)
1126− -> Expression commons schemas params grp from (nullity 'PGjson)
1166+ -> Expression grp commons schemas params from (nullity 'PGjson)
1127toJson = unsafeFunction "to_json"
1167toJson = unsafeFunction "to_json"
1129-- | Returns the value as jsonb. Arrays and composites are converted
1169-- | Returns the value as jsonb. Arrays and composites are converted
@@ -1133,43 +1173,43 @@ toJson = unsafeFunction "to_json"
1133-- number, a Boolean, or a null value, the text representation will be used, in
1173-- number, a Boolean, or a null value, the text representation will be used, in
1134-- such a fashion that it is a valid jsonb value.
1174-- such a fashion that it is a valid jsonb value.
1136− :: Expression commons schemas params grp from (nullity ty)
1176+ :: Expression grp commons schemas params from (nullity ty)
1137− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1177+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1138toJsonb = unsafeFunction "to_jsonb"
1178toJsonb = unsafeFunction "to_jsonb"
1140-- | Returns the array as a JSON array. A PostgreSQL multidimensional array
1180-- | Returns the array as a JSON array. A PostgreSQL multidimensional array
1141-- becomes a JSON array of arrays.
1181-- becomes a JSON array of arrays.
1142arrayToJson
1182arrayToJson
1143 :: PGArray "arrayToJson" arr
1183 :: PGArray "arrayToJson" arr
1144− => Expression commons schemas params grp from (nullity arr)
1184+ => Expression grp commons schemas params from (nullity arr)
1145− -> Expression commons schemas params grp from (nullity 'PGjson)
1185+ -> Expression grp commons schemas params from (nullity 'PGjson)
1146arrayToJson = unsafeFunction "array_to_json"
1186arrayToJson = unsafeFunction "array_to_json"
1148-- | Returns the row as a JSON object.
1188-- | Returns the row as a JSON object.
1149rowToJson
1189rowToJson
1150− :: Expression commons schemas params grp from (nullity ('PGcomposite ty))
1190+ :: Expression grp commons schemas params from (nullity ('PGcomposite ty))
1151− -> Expression commons schemas params grp from (nullity 'PGjson)
1191+ -> Expression grp commons schemas params from (nullity 'PGjson)
1152rowToJson = unsafeFunction "row_to_json"
1192rowToJson = unsafeFunction "row_to_json"
1154-- | Builds a possibly-heterogeneously-typed JSON array out of a variadic
1194-- | Builds a possibly-heterogeneously-typed JSON array out of a variadic
1155-- argument list.
1195-- argument list.
1156jsonBuildArray
1196jsonBuildArray
1157 :: SListI elems
1197 :: SListI elems
1158− => NP (Expression commons schemas params grp from) elems
1198+ => NP (Expression grp commons schemas params from) elems
1159− -> Expression commons schemas params grp from (nullity 'PGjson)
1199+ -> Expression grp commons schemas params from (nullity 'PGjson)
1160jsonBuildArray = unsafeVariadicFunction "json_build_array"
1200jsonBuildArray = unsafeVariadicFunction "json_build_array"
1162-- | Builds a possibly-heterogeneously-typed (binary) JSON array out of a
1202-- | Builds a possibly-heterogeneously-typed (binary) JSON array out of a
1163-- variadic argument list.
1203-- variadic argument list.
1164jsonbBuildArray
1204jsonbBuildArray
1165 :: SListI elems
1205 :: SListI elems
1166− => NP (Expression commons schemas params grp from) elems
1206+ => NP (Expression grp commons schemas params from) elems
1167− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1207+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1168jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
1208jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
1170unsafeRowFunction
1210unsafeRowFunction
1171 :: All Top elems
1211 :: All Top elems
1172− => NP (Aliased (Expression commons schemas params grp from)) elems
1212+ => NP (Aliased (Expression grp commons schemas params from)) elems
1173 -> [ByteString]
1213 -> [ByteString]
1174unsafeRowFunction =
1214unsafeRowFunction =
1175 (`appEndo` []) . hcfoldMap (Proxy :: Proxy Top)
1215 (`appEndo` []) . hcfoldMap (Proxy :: Proxy Top)
@@ -1184,8 +1224,8 @@ unsafeRowFunction =
1184-- and values.
1224-- and values.
1185jsonBuildObject
1225jsonBuildObject
1186 :: All Top elems
1226 :: All Top elems
1187− => NP (Aliased (Expression commons schemas params grp from)) elems
1227+ => NP (Aliased (Expression grp commons schemas params from)) elems
1188− -> Expression commons schemas params grp from (nullity 'PGjson)
1228+ -> Expression grp commons schemas params from (nullity 'PGjson)
1189jsonBuildObject
1229jsonBuildObject
1190 = unsafeFunction "json_build_object"
1230 = unsafeFunction "json_build_object"
1191 . UnsafeExpression
1231 . UnsafeExpression
@@ -1197,8 +1237,8 @@ jsonBuildObject
1197-- between text and values.
1237-- between text and values.
1198jsonbBuildObject
1238jsonbBuildObject
1199 :: All Top elems
1239 :: All Top elems
1200− => NP (Aliased (Expression commons schemas params grp from)) elems
1240+ => NP (Aliased (Expression grp commons schemas params from)) elems
1201− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1241+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1202jsonbBuildObject
1242jsonbBuildObject
1203 = unsafeFunction "jsonb_build_object"
1243 = unsafeFunction "jsonb_build_object"
1204 . UnsafeExpression
1244 . UnsafeExpression
@@ -1211,8 +1251,8 @@ jsonbBuildObject
1211-- array has exactly two elements, which are taken as a key/value pair.
1251-- array has exactly two elements, which are taken as a key/value pair.
1212jsonObject
1252jsonObject
1213 :: PGArrayOf "jsonObject" arr ('NotNull 'PGtext)
1253 :: PGArrayOf "jsonObject" arr ('NotNull 'PGtext)
1214− => Expression commons schemas params grp from (nullity arr)
1254+ => Expression grp commons schemas params from (nullity arr)
1215− -> Expression commons schemas params grp from (nullity 'PGjson)
1255+ -> Expression grp commons schemas params from (nullity 'PGjson)
1216jsonObject = unsafeFunction "json_object"
1256jsonObject = unsafeFunction "json_object"
1218-- | Builds a binary JSON object out of a text array. The array must have either
1258-- | Builds a binary JSON object out of a text array. The array must have either
@@ -1221,8 +1261,8 @@ jsonObject = unsafeFunction "json_object"
1221-- array has exactly two elements, which are taken as a key/value pair.
1261-- array has exactly two elements, which are taken as a key/value pair.
1222jsonbObject
1262jsonbObject
1223 :: PGArrayOf "jsonbObject" arr ('NotNull 'PGtext)
1263 :: PGArrayOf "jsonbObject" arr ('NotNull 'PGtext)
1224− => Expression commons schemas params grp from (nullity arr)
1264+ => Expression grp commons schemas params from (nullity arr)
1225− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1265+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1226jsonbObject = unsafeFunction "jsonb_object"
1266jsonbObject = unsafeFunction "jsonb_object"
1228-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
1268-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
@@ -1230,9 +1270,9 @@ jsonbObject = unsafeFunction "jsonb_object"
1230jsonZipObject
1270jsonZipObject
1231 :: ( PGArrayOf "jsonZipObject" keysArray ('NotNull 'PGtext)
1271 :: ( PGArrayOf "jsonZipObject" keysArray ('NotNull 'PGtext)
1232 , PGArrayOf "jsonZipObject" valuesArray ('NotNull 'PGtext))
1272 , PGArrayOf "jsonZipObject" valuesArray ('NotNull 'PGtext))
1233− => Expression commons schemas params grp from (nullity keysArray)
1273+ => Expression grp commons schemas params from (nullity keysArray)
1234− -> Expression commons schemas params grp from (nullity valuesArray)
1274+ -> Expression grp commons schemas params from (nullity valuesArray)
1235− -> Expression commons schemas params grp from (nullity 'PGjson)
1275+ -> Expression grp commons schemas params from (nullity 'PGjson)
1236jsonZipObject ks vs =
1276jsonZipObject ks vs =
1237 unsafeVariadicFunction "json_object" (ks :* vs :* Nil)
1277 unsafeVariadicFunction "json_object" (ks :* vs :* Nil)
@@ -1242,9 +1282,9 @@ jsonZipObject ks vs =
1242jsonbZipObject
1282jsonbZipObject
1243 :: ( PGArrayOf "jsonbZipObject" keysArray ('NotNull 'PGtext)
1283 :: ( PGArrayOf "jsonbZipObject" keysArray ('NotNull 'PGtext)
1244 , PGArrayOf "jsonbZipObject" valuesArray ('NotNull 'PGtext))
1284 , PGArrayOf "jsonbZipObject" valuesArray ('NotNull 'PGtext))
1245− => Expression commons schemas params grp from (nullity keysArray)
1285+ => Expression grp commons schemas params from (nullity keysArray)
1246− -> Expression commons schemas params grp from (nullity valuesArray)
1286+ -> Expression grp commons schemas params from (nullity valuesArray)
1247− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1287+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1248jsonbZipObject ks vs =
1288jsonbZipObject ks vs =
1249 unsafeVariadicFunction "jsonb_object" (ks :* vs :* Nil)
1289 unsafeVariadicFunction "jsonb_object" (ks :* vs :* Nil)
@@ -1254,23 +1294,23 @@ Table 9.46: JSON processing functions
1255-- | Returns the number of elements in the outermost JSON array.
1295-- | Returns the number of elements in the outermost JSON array.
1256jsonArrayLength
1296jsonArrayLength
1257− :: Expression commons schemas params grp from (nullity 'PGjson)
1297+ :: Expression grp commons schemas params from (nullity 'PGjson)
1258− -> Expression commons schemas params grp from (nullity 'PGint4)
1298+ -> Expression grp commons schemas params from (nullity 'PGint4)
1259jsonArrayLength = unsafeFunction "json_array_length"
1299jsonArrayLength = unsafeFunction "json_array_length"
1261-- | Returns the number of elements in the outermost binary JSON array.
1301-- | Returns the number of elements in the outermost binary JSON array.
1262jsonbArrayLength
1302jsonbArrayLength
1263− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1303+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1264− -> Expression commons schemas params grp from (nullity 'PGint4)
1304+ -> Expression grp commons schemas params from (nullity 'PGint4)
1265jsonbArrayLength = unsafeFunction "jsonb_array_length"
1305jsonbArrayLength = unsafeFunction "jsonb_array_length"
1267-- | Returns JSON value pointed to by the given path (equivalent to #>
1307-- | Returns JSON value pointed to by the given path (equivalent to #>
1268-- operator).
1308-- operator).
1269jsonExtractPath
1309jsonExtractPath
1270 :: SListI elems
1310 :: SListI elems
1271− => Expression commons schemas params grp from (nullity 'PGjson)
1311+ => Expression grp commons schemas params from (nullity 'PGjson)
1272− -> NP (Expression commons schemas params grp from) elems
1312+ -> NP (Expression grp commons schemas params from) elems
1273− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1313+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1274jsonExtractPath x xs =
1314jsonExtractPath x xs =
1275 unsafeVariadicFunction "json_extract_path" (x :* xs)
1315 unsafeVariadicFunction "json_extract_path" (x :* xs)
@@ -1278,9 +1318,9 @@ jsonExtractPath x xs =
1278-- operator).
1318-- operator).
1279jsonbExtractPath
1319jsonbExtractPath
1280 :: SListI elems
1320 :: SListI elems
1281− => Expression commons schemas params grp from (nullity 'PGjsonb)
1321+ => Expression grp commons schemas params from (nullity 'PGjsonb)
1282− -> NP (Expression commons schemas params grp from) elems
1322+ -> NP (Expression grp commons schemas params from) elems
1283− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1323+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1284jsonbExtractPath x xs =
1324jsonbExtractPath x xs =
1285 unsafeVariadicFunction "jsonb_extract_path" (x :* xs)
1325 unsafeVariadicFunction "jsonb_extract_path" (x :* xs)
@@ -1288,9 +1328,9 @@ jsonbExtractPath x xs =
1288-- operator), as text.
1328-- operator), as text.
1289jsonExtractPathAsText
1329jsonExtractPathAsText
1290 :: SListI elems
1330 :: SListI elems
1291− => Expression commons schemas params grp from (nullity 'PGjson)
1331+ => Expression grp commons schemas params from (nullity 'PGjson)
1292− -> NP (Expression commons schemas params grp from) elems
1332+ -> NP (Expression grp commons schemas params from) elems
1293− -> Expression commons schemas params grp from (nullity 'PGjson)
1333+ -> Expression grp commons schemas params from (nullity 'PGjson)
1294jsonExtractPathAsText x xs =
1334jsonExtractPathAsText x xs =
1295 unsafeVariadicFunction "json_extract_path_text" (x :* xs)
1335 unsafeVariadicFunction "json_extract_path_text" (x :* xs)
@@ -1298,38 +1338,38 @@ jsonExtractPathAsText x xs =
1298-- operator), as text.
1338-- operator), as text.
1299jsonbExtractPathAsText
1339jsonbExtractPathAsText
1300 :: SListI elems
1340 :: SListI elems
1301− => Expression commons schemas params grp from (nullity 'PGjsonb)
1341+ => Expression grp commons schemas params from (nullity 'PGjsonb)
1302− -> NP (Expression commons schemas params grp from) elems
1342+ -> NP (Expression grp commons schemas params from) elems
1303− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1343+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1304jsonbExtractPathAsText x xs =
1344jsonbExtractPathAsText x xs =
1305 unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
1345 unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
1307-- | Returns the type of the outermost JSON value as a text string. Possible
1347-- | Returns the type of the outermost JSON value as a text string. Possible
1308-- types are object, array, string, number, boolean, and null.
1348-- types are object, array, string, number, boolean, and null.
1309jsonTypeof
1349jsonTypeof
1310− :: Expression commons schemas params grp from (nullity 'PGjson)
1350+ :: Expression grp commons schemas params from (nullity 'PGjson)
1311− -> Expression commons schemas params grp from (nullity 'PGtext)
1351+ -> Expression grp commons schemas params from (nullity 'PGtext)
1312jsonTypeof = unsafeFunction "json_typeof"
1352jsonTypeof = unsafeFunction "json_typeof"
1314-- | Returns the type of the outermost binary JSON value as a text string.
1354-- | Returns the type of the outermost binary JSON value as a text string.
1315-- Possible types are object, array, string, number, boolean, and null.
1355-- Possible types are object, array, string, number, boolean, and null.
1316jsonbTypeof
1356jsonbTypeof
1317− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1357+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1318− -> Expression commons schemas params grp from (nullity 'PGtext)
1358+ -> Expression grp commons schemas params from (nullity 'PGtext)
1319jsonbTypeof = unsafeFunction "jsonb_typeof"
1359jsonbTypeof = unsafeFunction "jsonb_typeof"
1321-- | Returns its argument with all object fields that have null values omitted.
1361-- | Returns its argument with all object fields that have null values omitted.
1322-- Other null values are untouched.
1362-- Other null values are untouched.
1323jsonStripNulls
1363jsonStripNulls
1324− :: Expression commons schemas params grp from (nullity 'PGjson)
1364+ :: Expression grp commons schemas params from (nullity 'PGjson)
1325− -> Expression commons schemas params grp from (nullity 'PGjson)
1365+ -> Expression grp commons schemas params from (nullity 'PGjson)
1326jsonStripNulls = unsafeFunction "json_strip_nulls"
1366jsonStripNulls = unsafeFunction "json_strip_nulls"
1328-- | Returns its argument with all object fields that have null values omitted.
1368-- | Returns its argument with all object fields that have null values omitted.
1329-- Other null values are untouched.
1369-- Other null values are untouched.
1330jsonbStripNulls
1370jsonbStripNulls
1331− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1371+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1332− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1372+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1333jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
1373jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
1335-- | @ jsonbSet target path new_value create_missing @
1375-- | @ jsonbSet target path new_value create_missing @
@@ -1341,11 +1381,11 @@ jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
1341-- arrays.
1381-- arrays.
1343 :: PGTextArray "jsonbSet" arr
1383 :: PGTextArray "jsonbSet" arr
1344− => Expression commons schemas params grp from (nullity 'PGjsonb)
1384+ => Expression grp commons schemas params from (nullity 'PGjsonb)
1345− -> Expression commons schemas params grp from (nullity arr)
1385+ -> Expression grp commons schemas params from (nullity arr)
1346− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1386+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1347− -> Maybe (Expression commons schemas params grp from (nullity 'PGbool))
1387+ -> Maybe (Expression grp commons schemas params from (nullity 'PGbool))
1348− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1388+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1349jsonbSet tgt path val createMissing = case createMissing of
1389jsonbSet tgt path val createMissing = case createMissing of
1350 Just m -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* m :* Nil)
1390 Just m -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* m :* Nil)
1351 Nothing -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* Nil)
1391 Nothing -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* Nil)
@@ -1360,267 +1400,411 @@ jsonbSet tgt path val createMissing = case createMissing of
1360-- in path count from the end of JSON arrays.
1400-- in path count from the end of JSON arrays.
1361jsonbInsert
1401jsonbInsert
1362 :: PGTextArray "jsonbInsert" arr
1402 :: PGTextArray "jsonbInsert" arr
1363− => Expression commons schemas params grp from (nullity 'PGjsonb)
1403+ => Expression grp commons schemas params from (nullity 'PGjsonb)
1364− -> Expression commons schemas params grp from (nullity arr)
1404+ -> Expression grp commons schemas params from (nullity arr)
1365− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1405+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1366− -> Maybe (Expression commons schemas params grp from (nullity 'PGbool))
1406+ -> Maybe (Expression grp commons schemas params from (nullity 'PGbool))
1367− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1407+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1368jsonbInsert tgt path val insertAfter = case insertAfter of
1408jsonbInsert tgt path val insertAfter = case insertAfter of
1369 Just i -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* i :* Nil)
1409 Just i -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* i :* Nil)
1370 Nothing -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* Nil)
1410 Nothing -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* Nil)
1372-- | Returns its argument as indented JSON text.
1412-- | Returns its argument as indented JSON text.
1373jsonbPretty
1413jsonbPretty
1374− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1414+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1375− -> Expression commons schemas params grp from (nullity 'PGtext)
1415+ -> Expression grp commons schemas params from (nullity 'PGtext)
1376jsonbPretty = unsafeFunction "jsonb_pretty"
1416jsonbPretty = unsafeFunction "jsonb_pretty"
1378{-----------------------------------------
1418{-----------------------------------------
1379aggregation
1419aggregation
1380-----------------------------------------}
1420-----------------------------------------}
1422+class Aggregate expr1 expr2 aggr
1423+ | aggr -> expr1, aggr -> expr2 where
1425+ -- | A special aggregation that does not require an input
1429+ -- expression :: Expression (Grouped bys) commons schemas params from ('NotNull 'PGint8)
1430+ -- expression = countStar
1431+ -- in printSQL expression
1434+ countStar :: aggr ('NotNull 'PGint8)
1438+ -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1439+ -- expression = count (All #col)
1440+ -- in printSQL expression
1442+ -- count(ALL "col")
1446+ -> aggr ('NotNull 'PGint8)
1450+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1451+ -- expression = sum_ (Distinct #col)
1452+ -- in printSQL expression
1454+ -- sum(DISTINCT "col")
1457+ => expr1 (nullity ty)
1458+ -> aggr (nullity ty)
1462+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1463+ -- expression = bitAnd (Distinct #col)
1464+ -- in printSQL expression
1466+ -- bit_and(DISTINCT "col")
1468+ :: int `In` PGIntegral
1469+ => expr1 (nullity int)
1470+ -- ^ what to aggregate
1471+ -> aggr (nullity int)
1475+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1476+ -- expression = bitOr (All #col)
1477+ -- in printSQL expression
1479+ -- bit_or(ALL "col")
1481+ :: int `In` PGIntegral
1482+ => expr1 (nullity int)
1483+ -- ^ what to aggregate
1484+ -> aggr (nullity int)
1488+ -- winFun :: WindowFunction 'Ungrouped commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1489+ -- winFun = boolAnd #col
1490+ -- in printSQL winFun
1494+ :: expr1 (nullity 'PGbool)
1495+ -- ^ what to aggregate
1496+ -> aggr (nullity 'PGbool)
1500+ -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1501+ -- expression = boolOr (All #col)
1502+ -- in printSQL expression
1504+ -- bool_or(ALL "col")
1506+ :: expr1(nullity 'PGbool)
1507+ -- ^ what to aggregate
1508+ -> aggr (nullity 'PGbool)
1510+ -- | synonym for `boolAnd`
1514+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1515+ -- expression = every (Distinct #col)
1516+ -- in printSQL expression
1518+ -- every(DISTINCT "col")
1520+ :: expr1 (nullity 'PGbool)
1521+ -- ^ what to aggregate
1522+ -> aggr (nullity 'PGbool)
1524+ -- | maximum value of expression across all input values
1526+ :: expr1 (nullity ty)
1527+ -- ^ what to maximize
1528+ -> aggr (nullity ty)
1530+ -- | minimum value of expression across all input values
1532+ :: expr1 (nullity ty)
1533+ -- ^ what to minimize
1534+ -> aggr (nullity ty)
1536+ -- | average aggregation
1538+ :: expr1 (nullity ty)
1539+ -- ^ what to average
1540+ -> aggr (nullity (PGAvg ty))
1542+ {- | correlation coefficient
1545+ expression :: Expression ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1546+ expression = corr (All (#y :*: #x))
1547+ in printSQL expression
1552+ :: expr2 (nullity 'PGfloat8)
1553+ -> aggr (nullity 'PGfloat8)
1555+ {- | population covariance
1558+ expression :: Expression ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1559+ expression = covarPop (All (#y :*: #x))
1560+ in printSQL expression
1562+ covar_pop(ALL "y", "x")
1565+ :: expr2 (nullity 'PGfloat8)
1566+ -> aggr (nullity 'PGfloat8)
1568+ {- | sample covariance
1571+ winFun :: WindowFunction 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1572+ winFun = covarSamp (#y :*: #x)
1575+ covar_samp("y", "x")
1578+ :: expr2 (nullity 'PGfloat8)
1579+ -> aggr (nullity 'PGfloat8)
1581+ {- | average of the independent variable (sum(X)/N)
1584+ expression :: Expression ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1585+ expression = regrAvgX (All (#y :*: #x))
1586+ in printSQL expression
1588+ regr_avgx(ALL "y", "x")
1591+ :: expr2 (nullity 'PGfloat8)
1592+ -> aggr (nullity 'PGfloat8)
1594+ {- | average of the dependent variable (sum(Y)/N)
1597+ winFun :: WindowFunction 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1598+ winFun = regrAvgY (#y :*: #x)
1601+ regr_avgy("y", "x")
1604+ :: expr2 (nullity 'PGfloat8)
1605+ -> aggr (nullity 'PGfloat8)
1607+ {- | number of input rows in which both expressions are nonnull
1610+ winFun :: WindowFunction 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGint8)
1611+ winFun = regrCount (#y :*: #x)
1614+ regr_count("y", "x")
1617+ :: expr2 (nullity 'PGfloat8)
1618+ -> aggr (nullity 'PGint8)
1620+ {- | y-intercept of the least-squares-fit linear equation determined by the (X, Y) pairs
1623+ expression :: Expression ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1624+ expression = regrIntercept (All (#y :*: #x))
1625+ in printSQL expression
1627+ regr_intercept(ALL "y", "x")
1630+ :: expr2 (nullity 'PGfloat8)
1631+ -> aggr (nullity 'PGfloat8)
1633+ -- | @regr_r2(Y, X)@, square of the correlation coefficient
1635+ :: expr2 (nullity 'PGfloat8)
1636+ -> aggr (nullity 'PGfloat8)
1638+ -- | @regr_slope(Y, X)@, slope of the least-squares-fit linear equation
1639+ -- determined by the (X, Y) pairs
1641+ :: expr2 (nullity 'PGfloat8)
1642+ -> aggr (nullity 'PGfloat8)
1644+ -- | @regr_sxx(Y, X)@, sum(X^2) - sum(X)^2/N
1645+ -- (“sum of squares” of the independent variable)
1647+ :: expr2 (nullity 'PGfloat8)
1648+ -> aggr (nullity 'PGfloat8)
1650+ -- | @regr_sxy(Y, X)@, sum(X*Y) - sum(X) * sum(Y)/N
1651+ -- (“sum of products” of independent times dependent variable)
1653+ :: expr2 (nullity 'PGfloat8)
1654+ -> aggr (nullity 'PGfloat8)
1656+ -- | @regr_syy(Y, X)@, sum(Y^2) - sum(Y)^2/N
1657+ -- (“sum of squares” of the dependent variable)
1659+ :: expr2 (nullity 'PGfloat8)
1660+ -> aggr (nullity 'PGfloat8)
1662+ -- | historical alias for `stddevSamp`
1664+ :: expr1 (nullity ty)
1665+ -> aggr (nullity (PGAvg ty))
1667+ -- | population standard deviation of the input values
1669+ :: expr1 (nullity ty)
1670+ -> aggr (nullity (PGAvg ty))
1672+ -- | sample standard deviation of the input values
1674+ :: expr1 (nullity ty)
1675+ -> aggr (nullity (PGAvg ty))
1677+ -- | historical alias for `varSamp`
1679+ :: expr1 (nullity ty)
1680+ -> aggr (nullity (PGAvg ty))
1682+ -- | population variance of the input values
1683+ -- (square of the population standard deviation)
1685+ :: expr1 (nullity ty)
1686+ -> aggr (nullity (PGAvg ty))
1688+ -- | sample variance of the input values
1689+ -- (square of the sample standard deviation)
1691+ :: expr1 (nullity ty)
1692+ -> aggr (nullity (PGAvg ty))
1694+data Distinction expr ty
1696+ | Distinct (expr ty)
1697+ deriving (GHC.Generic,Show,Eq,Ord)
1698+instance NFData (Distinction (Expression grp commons schemas params from) ty)
1699+instance RenderSQL (Distinction (Expression grp commons schemas params from) ty) where
1701+ All x -> "ALL" <+> renderSQL x
1702+ Distinct x -> "DISTINCT" <+> renderSQL x
1705+ ( Expression grp commons schemas params from
1707+ Expression grp commons schemas params from) xty ) where
1710+ "ALL" <+> renderSQL x <> ", " <> renderSQL x'
1711+ Distinct (x :*: x') ->
1712+ "DISTINCT" <+> renderSQL x <> ", " <> renderSQL x'
1715+ (Distinction (Expression 'Ungrouped commons schemas params from))
1716+ (Distinction (Expression 'Ungrouped commons schemas params from :*: Expression 'Ungrouped commons schemas params from))
1717+ (Expression ('Grouped bys) commons schemas params from) where
1718+ countStar = UnsafeExpression "count(*)"
1719+ count = unsafeAggregate1 "count"
1720+ sum_ = unsafeAggregate1 "sum"
1721+ bitAnd = unsafeAggregate1 "bit_and"
1722+ bitOr = unsafeAggregate1 "bit_or"
1723+ boolAnd = unsafeAggregate1 "bool_and"
1724+ boolOr = unsafeAggregate1 "bool_or"
1725+ every = unsafeAggregate1 "every"
1726+ max_ = unsafeAggregate1 "max"
1727+ min_ = unsafeAggregate1 "min"
1728+ avg = unsafeAggregate1 "avg"
1729+ corr = unsafeAggregate2 "corr"
1730+ covarPop = unsafeAggregate2 "covar_pop"
1731+ covarSamp = unsafeAggregate2 "covar_samp"
1732+ regrAvgX = unsafeAggregate2 "regr_avgx"
1733+ regrAvgY = unsafeAggregate2 "regr_avgy"
1734+ regrCount = unsafeAggregate2 "regr_count"
1735+ regrIntercept = unsafeAggregate2 "regr_intercept"
1736+ regrR2 = unsafeAggregate2 "regr_r2"
1737+ regrSlope = unsafeAggregate2 "regr_slope"
1738+ regrSxx = unsafeAggregate2 "regr_sxx"
1739+ regrSxy = unsafeAggregate2 "regr_sxy"
1740+ regrSyy = unsafeAggregate2 "regr_syy"
1741+ stddev = unsafeAggregate1 "stddev"
1742+ stddevPop = unsafeAggregate1 "stddev_pop"
1743+ stddevSamp = unsafeAggregate1 "stddev_samp"
1744+ variance = unsafeAggregate1 "variance"
1745+ varPop = unsafeAggregate1 "var_pop"
1746+ varSamp = unsafeAggregate1 "var_samp"
1748+ (Expression grp commons schemas params from)
1749+ (Expression grp commons schemas params from :*: Expression grp commons schemas params from)
1750+ (WindowFunction grp commons schemas params from) where
1751+ countStar = UnsafeWindowFunction "count(*)"
1752+ count = unsafeWindowFunction1 "count"
1753+ sum_ = unsafeWindowFunction1 "sum"
1754+ bitAnd = unsafeWindowFunction1 "bit_and"
1755+ bitOr = unsafeWindowFunction1 "bit_or"
1756+ boolAnd = unsafeWindowFunction1 "bool_and"
1757+ boolOr = unsafeWindowFunction1 "bool_or"
1758+ every = unsafeWindowFunction1 "every"
1759+ max_ = unsafeWindowFunction1 "max"
1760+ min_ = unsafeWindowFunction1 "min"
1761+ avg = unsafeWindowFunction1 "avg"
1762+ corr = unsafeWindowFunction2 "corr"
1763+ covarPop = unsafeWindowFunction2 "covar_pop"
1764+ covarSamp = unsafeWindowFunction2 "covar_samp"
1765+ regrAvgX = unsafeWindowFunction2 "regr_avgx"
1766+ regrAvgY = unsafeWindowFunction2 "regr_avgy"
1767+ regrCount = unsafeWindowFunction2 "regr_count"
1768+ regrIntercept = unsafeWindowFunction2 "regr_intercept"
1769+ regrR2 = unsafeWindowFunction2 "regr_r2"
1770+ regrSlope = unsafeWindowFunction2 "regr_slope"
1771+ regrSxx = unsafeWindowFunction2 "regr_sxx"
1772+ regrSxy = unsafeWindowFunction2 "regr_sxy"
1773+ regrSyy = unsafeWindowFunction2 "regr_syy"
1774+ stddev = unsafeWindowFunction1 "stddev"
1775+ stddevPop = unsafeWindowFunction1 "stddev_pop"
1776+ stddevSamp = unsafeWindowFunction1 "stddev_samp"
1777+ variance = unsafeWindowFunction1 "variance"
1778+ varPop = unsafeWindowFunction1 "var_pop"
1779+ varSamp = unsafeWindowFunction1 "var_samp"
1382-- | escape hatch to define aggregate functions
1781-- | escape hatch to define aggregate functions
1383−unsafeAggregate
1782+unsafeAggregate1
1384 :: ByteString -- ^ aggregate function
1783 :: ByteString -- ^ aggregate function
1385− -> Expression commons schemas from 'Ungrouped params (xty)
1784+ -> Distinction (Expression 'Ungrouped commons schemas params from) xty
1386− -> Expression commons schemas from ('Grouped bys) params (yty)
1785+ -> Expression ('Grouped bys) commons schemas params from yty
1387−unsafeAggregate fun x = UnsafeExpression $ mconcat
1786+unsafeAggregate1 fun x = UnsafeExpression $ mconcat
1388 [fun, "(", renderSQL x, ")"]
1787 [fun, "(", renderSQL x, ")"]
1390−-- | escape hatch to define aggregate functions over distinct values
1789+-- | escape hatch to define aggregate binary functions
1391−unsafeAggregateDistinct
1790+unsafeAggregate2
1392 :: ByteString -- ^ aggregate function
1791 :: ByteString -- ^ aggregate function
1393− -> Expression commons schemas from 'Ungrouped params (xty)
1792+ -> Distinction (Expression 'Ungrouped commons schemas params from :*: Expression 'Ungrouped commons schemas params from) xty
1394− -> Expression commons schemas from ('Grouped bys) params (yty)
1793+ -> Expression ('Grouped bys) commons schemas params from yty
1395−unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
1794+unsafeAggregate2 fun xx = UnsafeExpression $ mconcat
1396− [fun, "(DISTINCT ", renderSQL x, ")"]
1795+ [fun, "(", renderSQL xx, ")"]
1398−-- | >>> :{
1797+-- | A type family that calculates `PGAvg` type of a `PGType`.
1399−-- let
1798+type family PGAvg ty where
1400−-- expression :: Expression commons schemas params ('Grouped bys) '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1401−-- expression = sum_ #col
1402−-- in printSQL expression
1407− => Expression commons schemas from 'Ungrouped params (nullity ty)
1409− -> Expression commons schemas from ('Grouped bys) params (nullity ty)
1410−sum_ = unsafeAggregate "sum"
1414−-- expression :: Expression commons schemas params ('Grouped bys) '[tab ::: '["col" ::: nullity 'PGnumeric]] (nullity 'PGnumeric)
1415−-- expression = sumDistinct #col
1416−-- in printSQL expression
1418−-- sum(DISTINCT "col")
1421− => Expression commons schemas from 'Ungrouped params (nullity ty)
1423− -> Expression commons schemas from ('Grouped bys) params (nullity ty)
1424−sumDistinct = unsafeAggregateDistinct "sum"
1426−-- | A constraint for `PGType`s that you can take averages of and the resulting
1428−class PGAvg ty avg | ty -> avg where
1430− :: Expression commons schemas from 'Ungrouped params (nullity ty)
1431− -- ^ what to average
1432− -> Expression commons schemas from ('Grouped bys) params (nullity avg)
1433− avg = unsafeAggregate "avg"
1434− avgDistinct = unsafeAggregateDistinct "avg"
1435−instance PGAvg 'PGint2 'PGnumeric
1799+ PGAvg 'PGint2 = 'PGnumeric
1436−instance PGAvg 'PGint4 'PGnumeric
1800+ PGAvg 'PGint4 = 'PGnumeric
1437−instance PGAvg 'PGint8 'PGnumeric
1801+ PGAvg 'PGint8 = 'PGnumeric
1438−instance PGAvg 'PGnumeric 'PGnumeric
1802+ PGAvg 'PGnumeric = 'PGnumeric
1439−instance PGAvg 'PGfloat4 'PGfloat8
1803+ PGAvg 'PGfloat4 = 'PGfloat8
1440−instance PGAvg 'PGfloat8 'PGfloat8
1804+ PGAvg 'PGfloat8 = 'PGfloat8
1441−instance PGAvg 'PGinterval 'PGinterval
1805+ PGAvg 'PGinterval = 'PGinterval
1442−
1806+ PGAvg pg = TypeError
1443−-- | >>> :{
1807+ ('Text "Squeal type error: No average for " ':<>: 'ShowType pg)
1445−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1446−-- expression = bitAnd #col
1447−-- in printSQL expression
1451− :: int `In` PGIntegral
1452− => Expression commons schemas from 'Ungrouped params (nullity int)
1453− -- ^ what to aggregate
1454− -> Expression commons schemas from ('Grouped bys) params (nullity int)
1455−bitAnd = unsafeAggregate "bit_and"
1459−-- expression :: Expression commons schemas params ('Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1460−-- expression = bitOr #col
1461−-- in printSQL expression
1465− :: int `In` PGIntegral
1466− => Expression commons schemas from 'Ungrouped params (nullity int)
1467− -- ^ what to aggregate
1468− -> Expression commons schemas from ('Grouped bys) params (nullity int)
1469−bitOr = unsafeAggregate "bit_or"
1473−-- expression :: Expression commons schemas params ('Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1474−-- expression = bitAndDistinct #col
1475−-- in printSQL expression
1477−-- bit_and(DISTINCT "col")
1479− :: int `In` PGIntegral
1480− => Expression commons schemas from 'Ungrouped params (nullity int)
1481− -- ^ what to aggregate
1482− -> Expression commons schemas from ('Grouped bys) params (nullity int)
1483−bitAndDistinct = unsafeAggregateDistinct "bit_and"
1487−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1488−-- expression = bitOrDistinct #col
1489−-- in printSQL expression
1491−-- bit_or(DISTINCT "col")
1493− :: int `In` PGIntegral
1494− => Expression commons schemas from 'Ungrouped params (nullity int)
1495− -- ^ what to aggregate
1496− -> Expression commons schemas from ('Grouped bys) params (nullity int)
1497−bitOrDistinct = unsafeAggregateDistinct "bit_or"
1501−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1502−-- expression = boolAnd #col
1503−-- in printSQL expression
1507− :: Expression commons schemas from 'Ungrouped params (nullity 'PGbool)
1508− -- ^ what to aggregate
1509− -> Expression commons schemas from ('Grouped bys) params (nullity 'PGbool)
1510−boolAnd = unsafeAggregate "bool_and"
1514−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1515−-- expression = boolOr #col
1516−-- in printSQL expression
1520− :: Expression commons schemas from 'Ungrouped params (nullity 'PGbool)
1521− -- ^ what to aggregate
1522− -> Expression commons schemas from ('Grouped bys) params (nullity 'PGbool)
1523−boolOr = unsafeAggregate "bool_or"
1527−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1528−-- expression = boolAndDistinct #col
1529−-- in printSQL expression
1531−-- bool_and(DISTINCT "col")
1533− :: Expression commons schemas from 'Ungrouped params (nullity 'PGbool)
1534− -- ^ what to aggregate
1535− -> Expression commons schemas from ('Grouped bys) params (nullity 'PGbool)
1536−boolAndDistinct = unsafeAggregateDistinct "bool_and"
1540−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1541−-- expression = boolOrDistinct #col
1542−-- in printSQL expression
1544−-- bool_or(DISTINCT "col")
1546− :: Expression commons schemas from 'Ungrouped params (nullity 'PGbool)
1547− -- ^ what to aggregate
1548− -> Expression commons schemas from ('Grouped bys) params (nullity 'PGbool)
1549−boolOrDistinct = unsafeAggregateDistinct "bool_or"
1551−-- | A special aggregation that does not require an input
1553−-- >>> printSQL countStar
1556− :: Expression commons schemas from ('Grouped bys) params ('NotNull 'PGint8)
1557−countStar = UnsafeExpression $ "count(*)"
1561−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1562−-- expression = count #col
1563−-- in printSQL expression
1567− :: Expression commons schemas from 'Ungrouped params ty
1569− -> Expression commons schemas from ('Grouped bys) params ('NotNull 'PGint8)
1570−count = unsafeAggregate "count"
1574−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1575−-- expression = countDistinct #col
1576−-- in printSQL expression
1578−-- count(DISTINCT "col")
1580− :: Expression commons schemas from 'Ungrouped params ty
1582− -> Expression commons schemas from ('Grouped bys) params ('NotNull 'PGint8)
1583−countDistinct = unsafeAggregateDistinct "count"
1585−-- | synonym for `boolAnd`
1589−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1590−-- expression = every #col
1591−-- in printSQL expression
1595− :: Expression commons schemas from 'Ungrouped params (nullity 'PGbool)
1596− -- ^ what to aggregate
1597− -> Expression commons schemas from ('Grouped bys) params (nullity 'PGbool)
1598−every = unsafeAggregate "every"
1600−-- | synonym for `boolAndDistinct`
1604−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1605−-- expression = everyDistinct #col
1606−-- in printSQL expression
1608−-- every(DISTINCT "col")
1610− :: Expression commons schemas from 'Ungrouped params (nullity 'PGbool)
1611− -- ^ what to aggregate
1612− -> Expression commons schemas from ('Grouped bys) params (nullity 'PGbool)
1613−everyDistinct = unsafeAggregateDistinct "every"
1615−-- | minimum and maximum aggregation
1616−max_, min_, maxDistinct, minDistinct
1617− :: Expression commons schemas from 'Ungrouped params (nullity ty)
1618− -- ^ what to aggregate
1619− -> Expression commons schemas from ('Grouped bys) params (nullity ty)
1620−max_ = unsafeAggregate "max"
1621−min_ = unsafeAggregate "min"
1622−maxDistinct = unsafeAggregateDistinct "max"
1623−minDistinct = unsafeAggregateDistinct "min"
1625{-----------------------------------------
1809{-----------------------------------------
1626type expressions
1810type expressions
@@ -1789,3 +1973,219 @@ instance PGTyped schemas ty
1789instance (All KnownNat dims, PGTyped schemas ty)
1973instance (All KnownNat dims, PGTyped schemas ty)
1790 => PGTyped schemas (nullity ('PGfixarray dims ty)) where
1974 => PGTyped schemas (nullity ('PGfixarray dims ty)) where
1791 pgtype = fixarray @dims (pgtype @schemas @ty)
1975 pgtype = fixarray @dims (pgtype @schemas @ty)
1977+{-----------------------------------------
1979+-----------------------------------------}
1981+-- | `SortExpression`s are used by `sortBy` to optionally sort the results
1982+-- of a `Query`. `Asc` or `Desc` set the sort direction of a `NotNull` result
1983+-- column to ascending or descending. Ascending order puts smaller values
1984+-- first, where "smaller" is defined in terms of the `.<` operator. Similarly,
1985+-- descending order is determined with the `.>` operator. `AscNullsFirst`,
1986+-- `AscNullsLast`, `DescNullsFirst` and `DescNullsLast` options are used to
1987+-- determine whether nulls appear before or after non-null values in the sort
1988+-- ordering of a `Null` result column.
1989+data SortExpression grp commons schemas params from where
1991+ :: Expression grp commons schemas params from ('NotNull ty)
1992+ -> SortExpression grp commons schemas params from
1994+ :: Expression grp commons schemas params from ('NotNull ty)
1995+ -> SortExpression grp commons schemas params from
1997+ :: Expression grp commons schemas params from ('Null ty)
1998+ -> SortExpression grp commons schemas params from
2000+ :: Expression grp commons schemas params from ('Null ty)
2001+ -> SortExpression grp commons schemas params from
2003+ :: Expression grp commons schemas params from ('Null ty)
2004+ -> SortExpression grp commons schemas params from
2006+ :: Expression grp commons schemas params from ('Null ty)
2007+ -> SortExpression grp commons schemas params from
2008+deriving instance Show (SortExpression grp commons schemas params from)
2010+class OrderBy expr where
2012+ :: [SortExpression grp commons schemas params from]
2013+ -> expr grp commons schemas params from
2014+ -> expr grp commons schemas params from
2016+-- | Render a `SortExpression`.
2017+instance RenderSQL (SortExpression grp commons schemas params from) where
2019+ Asc expression -> renderSQL expression <+> "ASC"
2020+ Desc expression -> renderSQL expression <+> "DESC"
2021+ AscNullsFirst expression -> renderSQL expression
2022+ <+> "ASC NULLS FIRST"
2023+ DescNullsFirst expression -> renderSQL expression
2024+ <+> "DESC NULLS FIRST"
2025+ AscNullsLast expression -> renderSQL expression <+> "ASC NULLS LAST"
2026+ DescNullsLast expression -> renderSQL expression <+> "DESC NULLS LAST"
2028+data WindowDefinition grp commons schemas params from where
2031+ => NP (Expression grp commons schemas params from) bys
2032+ -> [SortExpression grp commons schemas params from]
2033+ -> WindowDefinition grp commons schemas params from
2035+instance OrderBy WindowDefinition where
2036+ orderBy sortsR (WindowDefinition parts sortsL)
2037+ = WindowDefinition parts (sortsL ++ sortsR)
2039+instance RenderSQL (WindowDefinition commons schemas from grp params) where
2040+ renderSQL (WindowDefinition part ord) =
2041+ renderPartitionByClause part <> renderOrderByClause ord
2043+ renderPartitionByClause = \case
2045+ parts -> "PARTITION" <+> "BY" <+> renderCommaSeparated renderExpression parts
2046+ renderOrderByClause = \case
2048+ srts -> " ORDER" <+> "BY"
2049+ <+> commaSeparated (renderSQL <$> srts)
2053+ => NP (Expression grp commons schemas params from) bys
2054+ -> WindowDefinition grp commons schemas params from
2055+partitionBy bys = WindowDefinition bys []
2057+newtype WindowFunction
2059+ (commons :: FromType)
2060+ (schemas :: SchemasType)
2061+ (params :: [NullityType])
2063+ (ty :: NullityType)
2064+ = UnsafeWindowFunction { renderWindowFunction :: ByteString }
2065+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
2067+instance RenderSQL (WindowFunction grp commons schemas params from ty) where
2068+ renderSQL = renderWindowFunction
2070+unsafeWindowFunction1
2072+ -> Expression grp commons schemas params from ty0
2073+ -> WindowFunction grp commons schemas params from ty1
2074+unsafeWindowFunction1 fun x
2075+ = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x)
2077+unsafeWindowFunction2
2079+ -> (Expression grp commons schemas params from :*: Expression grp commons schemas params from) ty0
2080+ -> WindowFunction grp commons schemas params from ty1
2081+unsafeWindowFunction2 fun (x :*: x')
2082+ = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x <> ", " <> renderSQL x')
2084+{- | rank of the current row with gaps; same as `rowNumber` of its first peer
2088+rank :: WindowFunction grp commons schemas params from ('NotNull 'PGint8)
2089+rank = UnsafeWindowFunction "rank()"
2091+{- | number of the current row within its partition, counting from 1
2092+>>> printSQL rowNumber
2095+rowNumber :: WindowFunction grp commons schemas params from ('NotNull 'PGint8)
2096+rowNumber = UnsafeWindowFunction "row_number()"
2098+{- | rank of the current row without gaps; this function counts peer groups
2099+>>> printSQL denseRank
2102+denseRank :: WindowFunction grp commons schemas params from ('NotNull 'PGint8)
2103+denseRank = UnsafeWindowFunction "dense_rank()"
2105+{- | relative rank of the current row: (rank - 1) / (total partition rows - 1)
2106+>>> printSQL percentRank
2109+percentRank :: WindowFunction grp commons schemas params from ('NotNull 'PGfloat8)
2110+percentRank = UnsafeWindowFunction "percent_rank()"
2112+{- | cumulative distribution: (number of partition rows
2113+preceding or peer with current row) / total partition rows
2114+>>> printSQL cumeDist
2117+cumeDist :: WindowFunction grp commons schemas params from ('NotNull 'PGfloat8)
2118+cumeDist = UnsafeWindowFunction "cume_dist()"
2120+{- | integer ranging from 1 to the argument value,
2121+dividing the partition as equally as possible
2124+ :: Expression grp commons schemas params from ('NotNull 'PGint4)
2126+ -> WindowFunction grp commons schemas params from ('NotNull 'PGint4)
2127+ntile = unsafeWindowFunction1 "ntile"
2129+{- | returns value evaluated at the row that is offset rows before the current
2130+row within the partition; if there is no such row, instead return default
2131+(which must be of the same type as value). Both offset and default are
2132+evaluated with respect to the current row.
2135+ :: Expression grp commons schemas params from ty
2137+ -> Expression grp commons schemas params from ('NotNull 'PGint4)
2139+ -> Expression grp commons schemas params from ty
2141+ -> WindowFunction grp commons schemas params from ty
2142+lag value offset def = UnsafeWindowFunction $ "lag"
2144+ (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
2146+{- | returns value evaluated at the row that is offset rows after the current
2147+row within the partition; if there is no such row, instead return default
2148+(which must be of the same type as value). Both offset and default are
2149+evaluated with respect to the current row.
2152+ :: Expression grp commons schemas params from ty
2154+ -> Expression grp commons schemas params from ('NotNull 'PGint4)
2156+ -> Expression grp commons schemas params from ty
2158+ -> WindowFunction grp commons schemas params from ty
2159+lead value offset def = UnsafeWindowFunction $ "lag"
2161+ (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
2163+{- | returns value evaluated at the row that is the
2164+first row of the window frame
2167+ :: Expression grp commons schemas params from ty
2169+ -> WindowFunction grp commons schemas params from ty
2170+firstValue = unsafeWindowFunction1 "first_value"
2172+{- | returns value evaluated at the row that is the
2173+last row of the window frame
2176+ :: Expression grp commons schemas params from ty
2178+ -> WindowFunction grp commons schemas params from ty
2179+lastValue = unsafeWindowFunction1 "last_value"
2181+{- | returns value evaluated at the row that is the nth
2182+row of the window frame (counting from 1); null if no such row
2185+ :: Expression grp commons schemas params from (nullity ty)
2187+ -> Expression grp commons schemas params from ('NotNull 'PGint4)
2189+ -> WindowFunction grp commons schemas params from ('Null ty)
2190+nthValue value nth = UnsafeWindowFunction $ "nth_value"
2191+ <> parenthesized (commaSeparated [renderSQL value, renderSQL nth])