@@ -16,11 +16,13 @@ Squeal expressions are the atoms used to build statements.
1616 , FlexibleContexts
1717 , FlexibleInstances
1818 , FunctionalDependencies
19+ , GADTs
1920 , GeneralizedNewtypeDeriving
2021 , LambdaCase
2122 , MagicHash
2223 , OverloadedStrings
2324 , ScopedTypeVariables
25+ , StandaloneDeriving
2426 , TypeApplications
2527 , TypeFamilies
2628 , TypeInType
@@ -132,15 +134,27 @@ module Squeal.PostgreSQL.Expression
132134 , jsonbInsert
133135 , jsonbPretty
134136 -- ** Aggregation
135− , unsafeAggregate, unsafeAggregateDistinct
136− , sum_, sumDistinct
137− , PGAvg (avg, avgDistinct)
138− , bitAnd, bitOr, boolAnd, boolOr
139− , bitAndDistinct, bitOrDistinct, boolAndDistinct, boolOrDistinct
140− , countStar
141− , count, countDistinct
142− , every, everyDistinct
143− , max_, maxDistinct, min_, minDistinct
137+ , Aggregate (..)
138+ , Distinction (..)
139+ , PGAvg
140+ -- * Window Functions
141+ , WindowDefinition (..)
142+ , partitionBy
143+ , WindowFunction (..)
144+ , rank
145+ , rowNumber
146+ , denseRank
147+ , percentRank
148+ , cumeDist
149+ , ntile
150+ , lag
151+ , lead
152+ , firstValue
153+ , lastValue
154+ , nthValue
155+ -- * Sorting
156+ , SortExpression (..)
157+ , OrderBy (..)
144158 -- * Types
145159 , TypeExpression (..)
146160 , PGTyped (..)
@@ -180,7 +194,10 @@ module Squeal.PostgreSQL.Expression
180194 , fixarray
181195 -- * Re-export
182196 , (&)
183− , NP ((:*), Nil)
197+ , NP (..)
198+ , (:*:) (..)
199+ , I (..)
200+ , K (..)
184201 ) where
185202
186203import Control.Category
@@ -194,6 +211,7 @@ import Data.Ratio
194211import Data.String
195212import Data.Word
196213import Generics.SOP hiding (from)
214+import GHC.Generics ((:*:) (..))
197215import GHC.OverloadedLabels
198216import GHC.TypeLits
199217import Prelude hiding (id, (.))
@@ -219,16 +237,16 @@ values from primitive expression using arithmetic, logical,
219237and other operations.
220238-}
221239newtype Expression
240+ (grp :: Grouping)
222241 (commons :: FromType)
223242 (schemas :: SchemasType)
224243 (params :: [NullityType])
225− (grp :: Grouping)
226244 (from :: FromType)
227245 (ty :: NullityType)
228246 = UnsafeExpression { renderExpression :: ByteString }
229247 deriving (GHC.Generic,Show,Eq,Ord,NFData)
230248
231−instance RenderSQL (Expression commons schemas params grp from ty) where
249+instance RenderSQL (Expression grp commons schemas params from ty) where
232250 renderSQL = renderExpression
233251
234252{- | A `HasParameter` constraint is used to indicate a value that is
@@ -246,12 +264,12 @@ class KnownNat n => HasParameter
246264 | n params -> ty where
247265 -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
248266 --
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)
250268 -- >>> printSQL expr
251269 -- ($1 :: int4)
252270 parameter
253271 :: TypeExpression schemas ty
254− -> Expression commons schemas params grp from ty
272+ -> Expression grp commons schemas params from ty
255273 parameter ty = UnsafeExpression $ parenthesized $
256274 "$" <> renderNat @n <+> "::"
257275 <+> renderSQL ty
@@ -262,38 +280,44 @@ instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
262280-- | `param` takes a `Nat` using type application and for basic types,
263281-- infers a `TypeExpression`.
264282--
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)
266284-- >>> printSQL expr
267285-- ($1 :: int4)
268286param
269287 :: forall n commons schemas params from grp ty
270288 . (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
272290param = parameter @n (pgtype @schemas)
273291
274292instance (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
276294 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
277298instance (HasUnique tab from row, Has col row ty, column ~ (col ::: ty))
278299 => IsLabel col
279− (Aliased (Expression commons schemas params 'Ungrouped from) column) where
300+ (Aliased (Expression 'Ungrouped commons schemas params from) column) where
280301 fromLabel = fromLabel @col `As` Alias
281302instance (HasUnique tab from row, Has col row ty, columns ~ '[col ::: ty])
282303 => IsLabel col
283− (NP (Aliased (Expression commons schemas params 'Ungrouped from)) columns) where
304+ (NP (Aliased (Expression 'Ungrouped commons schemas params from)) columns) where
284305 fromLabel = fromLabel @col :* Nil
285306
286307instance (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
288309 tab ! col = UnsafeExpression $
289310 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
290314instance (Has tab from row, Has col row ty, column ~ (col ::: ty))
291315 => IsQualified tab col
292− (Aliased (Expression commons schemas params 'Ungrouped from) column) where
316+ (Aliased (Expression 'Ungrouped commons schemas params from) column) where
293317 tab ! col = tab ! col `As` col
294318instance (Has tab from row, Has col row ty, columns ~ '[col ::: ty])
295319 => 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
297321 tab ! col = tab ! col :* Nil
298322
299323instance
@@ -301,15 +325,23 @@ instance
301325 , Has col row ty
302326 , GroupedBy tab col bys
303327 ) => IsLabel col
304− (Expression commons schemas params ('Grouped bys) from ty) where
328+ (Expression ('Grouped bys) commons schemas params from ty) where
305329 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
330+instance
331+ ( HasUnique tab from row
332+ , Has col row ty
333+ , GroupedBy tab col bys
334+ , tys ~ '[ty]
335+ ) => IsLabel col
336+ (NP (Expression ('Grouped bys) commons schemas params from) tys) where
337+ fromLabel = fromLabel @col :* Nil
306338instance
307339 ( HasUnique tab from row
308340 , Has col row ty
309341 , GroupedBy tab col bys
310342 , column ~ (col ::: ty)
311343 ) => IsLabel col
312− (Aliased (Expression commons schemas params ('Grouped bys) from) column) where
344+ (Aliased (Expression ('Grouped bys) commons schemas params from) column) where
313345 fromLabel = fromLabel @col `As` Alias
314346instance
315347 ( HasUnique tab from row
@@ -317,7 +349,7 @@ instance
317349 , GroupedBy tab col bys
318350 , columns ~ '[col ::: ty]
319351 ) => 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
321353 fromLabel = fromLabel @col :* Nil
322354
323355instance
@@ -325,16 +357,24 @@ instance
325357 , Has col row ty
326358 , GroupedBy tab col bys
327359 ) => IsQualified tab col
328− (Expression commons schemas params ('Grouped bys) from ty) where
360+ (Expression ('Grouped bys) commons schemas params from ty) where
329361 tab ! col = UnsafeExpression $
330362 renderSQL tab <> "." <> renderSQL col
363+instance
364+ ( Has tab from row
365+ , Has col row ty
366+ , GroupedBy tab col bys
367+ , tys ~ '[ty]
368+ ) => IsQualified tab col
369+ (NP (Expression ('Grouped bys) commons schemas params from) tys) where
370+ tab ! col = tab ! col :* Nil
331371instance
332372 ( Has tab from row
333373 , Has col row ty
334374 , GroupedBy tab col bys
335375 , column ~ (col ::: ty)
336376 ) => 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
338378 tab ! col = tab ! col `As` col
339379instance
340380 ( Has tab from row
@@ -342,7 +382,7 @@ instance
342382 , GroupedBy tab col bys
343383 , columns ~ '[col ::: ty]
344384 ) => 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
346386 tab ! col = tab ! col :* Nil
347387
348388-- | analagous to `Nothing`
@@ -366,11 +406,11 @@ notNull = UnsafeExpression . renderSQL
366406-- >>> printSQL $ coalesce [null_, true] false
367407-- COALESCE(NULL, TRUE, FALSE)
368408coalesce
369− :: [Expression commons schemas params grp from ('Null ty)]
409+ :: [Expression grp commons schemas params from ('Null ty)]
370410 -- ^ @NULL@s may be present
371− -> Expression commons schemas params grp from ('NotNull ty)
411+ -> Expression grp commons schemas params from ('NotNull ty)
372412 -- ^ @NULL@ is absent
373− -> Expression commons schemas params grp from ('NotNull ty)
413+ -> Expression grp commons schemas params from ('NotNull ty)
374414coalesce nullxs notNullx = UnsafeExpression $
375415 "COALESCE" <> parenthesized (commaSeparated
376416 ((renderSQL <$> nullxs) <> [renderSQL notNullx]))
@@ -380,26 +420,26 @@ coalesce nullxs notNullx = UnsafeExpression $
380420-- >>> printSQL $ fromNull true null_
381421-- COALESCE(NULL, TRUE)
382422fromNull
383− :: Expression commons schemas params grp from ('NotNull ty)
423+ :: Expression grp commons schemas params from ('NotNull ty)
384424 -- ^ what to convert @NULL@ to
385− -> Expression commons schemas params grp from ('Null ty)
386− -> Expression commons schemas params grp from ('NotNull ty)
425+ -> Expression grp commons schemas params from ('Null ty)
426+ -> Expression grp commons schemas params from ('NotNull ty)
387427fromNull notNullx nullx = coalesce [nullx] notNullx
388428
389429-- | >>> printSQL $ null_ & isNull
390430-- NULL IS NULL
391431isNull
392− :: Expression commons schemas params grp from ('Null ty)
432+ :: Expression grp commons schemas params from ('Null ty)
393433 -- ^ possibly @NULL@
394− -> Condition commons schemas params grp from
434+ -> Condition grp commons schemas params from
395435isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
396436
397437-- | >>> printSQL $ null_ & isNotNull
398438-- NULL IS NOT NULL
399439isNotNull
400− :: Expression commons schemas params grp from ('Null ty)
440+ :: Expression grp commons schemas params from ('Null ty)
401441 -- ^ possibly @NULL@
402− -> Condition commons schemas params grp from
442+ -> Condition grp commons schemas params from
403443isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
404444
405445-- | analagous to `maybe` using @IS NULL@
@@ -407,13 +447,13 @@ isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
407447-- >>> printSQL $ matchNull true not_ null_
408448-- CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END
409449matchNull
410− :: Expression commons schemas params grp from (nullty)
450+ :: Expression grp commons schemas params from (nullty)
411451 -- ^ what to convert @NULL@ to
412− -> ( Expression commons schemas params grp from ('NotNull ty)
413− -> Expression commons schemas params grp from (nullty) )
452+ -> ( Expression grp commons schemas params from ('NotNull ty)
453+ -> Expression grp commons schemas params from (nullty) )
414454 -- ^ function to perform when @NULL@ is absent
415− -> Expression commons schemas params grp from ('Null ty)
416− -> Expression commons schemas params grp from (nullty)
455+ -> Expression grp commons schemas params from ('Null ty)
456+ -> Expression grp commons schemas params from (nullty)
417457matchNull y f x = ifThenElse (isNull x) y
418458 (f (UnsafeExpression (renderSQL x)))
419459
@@ -421,25 +461,25 @@ matchNull y f x = ifThenElse (isNull x) y
421461`nullIf` gives @NULL@.
422462
423463>>> :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)
425465>>> printSQL expr
426466NULL IF (FALSE, ($1 :: bool))
427467-}
428468nullIf
429− :: Expression commons schemas params grp from ('NotNull ty)
469+ :: Expression grp commons schemas params from ('NotNull ty)
430470 -- ^ @NULL@ is absent
431− -> Expression commons schemas params grp from ('NotNull ty)
471+ -> Expression grp commons schemas params from ('NotNull ty)
432472 -- ^ @NULL@ is absent
433− -> Expression commons schemas params grp from ('Null ty)
473+ -> Expression grp commons schemas params from ('Null ty)
434474nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
435475 (renderSQL x <> ", " <> renderSQL y)
436476
437477-- | >>> printSQL $ array [null_, false, true]
438478-- ARRAY[NULL, FALSE, TRUE]
439479array
440− :: [Expression commons schemas params grp from ty]
480+ :: [Expression grp commons schemas params from ty]
441481 -- ^ array elements
442− -> Expression commons schemas params grp from (nullity ('PGvararray ty))
482+ -> Expression grp commons schemas params from (nullity ('PGvararray ty))
443483array xs = UnsafeExpression $
444484 "ARRAY[" <> commaSeparated (renderSQL <$> xs) <> "]"
445485
@@ -447,13 +487,13 @@ array xs = UnsafeExpression $
447487-- (ARRAY[NULL, FALSE, TRUE])[2]
448488index
449489 :: Word64 -- ^ index
450− -> Expression commons schemas params grp from (nullity ('PGvararray ty)) -- ^ array
451− -> Expression commons schemas params grp from (NullifyType ty)
490+ -> Expression grp commons schemas params from (nullity ('PGvararray ty)) -- ^ array
491+ -> Expression grp commons schemas params from (NullifyType ty)
452492index n expr = UnsafeExpression $
453493 parenthesized (renderSQL expr) <> "[" <> fromString (show n) <> "]"
454494
455495instance (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
457497 label = UnsafeExpression $ renderSQL (PGlabel @label)
458498
459499-- | A row constructor is an expression that builds a row value
@@ -465,14 +505,14 @@ instance (KnownSymbol label, label `In` labels) => IsPGlabel label
465505-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
466506-- :}
467507--
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)
469509-- >>> printSQL i
470510-- ROW(0, 1)
471511row
472512 :: SListI row
473− => NP (Aliased (Expression commons schemas params grp from)) row
513+ => NP (Aliased (Expression grp commons schemas params from)) row
474514 -- ^ 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))
476516row exprs = UnsafeExpression $ "ROW" <> parenthesized
477517 (renderCommaSeparated (\ (expr `As` _) -> renderSQL expr) exprs)
478518
@@ -483,7 +523,7 @@ row exprs = UnsafeExpression $ "ROW" <> parenthesized
483523-- type Schema = '["complex" ::: 'Typedef Complex]
484524-- :}
485525--
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)
487527-- >>> printSQL $ i & field #complex #imaginary
488528-- (ROW(0, 1)::"complex")."imaginary"
489529field
@@ -492,41 +532,41 @@ field
492532 , Has field row ty)
493533 => QualifiedAlias sch tydef -- ^ row type
494534 -> Alias field -- ^ field name
495− -> Expression commons schemas params grp from ('NotNull ('PGcomposite row))
496− -> Expression commons schemas params grp from ty
535+ -> Expression grp commons schemas params from ('NotNull ('PGcomposite row))
536+ -> Expression grp commons schemas params from ty
497537field td fld expr = UnsafeExpression $
498538 parenthesized (renderSQL expr <> "::" <> renderSQL td)
499539 <> "." <> renderSQL fld
500540
501541instance Semigroup
502− (Expression commons schemas params grp from (nullity ('PGvararray ty))) where
542+ (Expression grp commons schemas params from (nullity ('PGvararray ty))) where
503543 (<>) = unsafeBinaryOp "||"
504544
505545instance Monoid
506− (Expression commons schemas params grp from (nullity ('PGvararray ty))) where
546+ (Expression grp commons schemas params from (nullity ('PGvararray ty))) where
507547 mempty = array []
508548 mappend = (<>)
509549
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)
511551-- >>> printSQL expr
512552-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
513553greatest
514− :: Expression commons schemas params grp from (nullty)
554+ :: Expression grp commons schemas params from (nullty)
515555 -- ^ needs at least 1 argument
516− -> [Expression commons schemas params grp from (nullty)]
556+ -> [Expression grp commons schemas params from (nullty)]
517557 -- ^ or more
518− -> Expression commons schemas params grp from (nullty)
558+ -> Expression grp commons schemas params from (nullty)
519559greatest x xs = UnsafeExpression $ "GREATEST("
520560 <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
521561
522562-- | >>> printSQL $ least currentTimestamp [null_]
523563-- LEAST(CURRENT_TIMESTAMP, NULL)
524564least
525− :: Expression commons schemas params grp from (nullty)
565+ :: Expression grp commons schemas params from (nullty)
526566 -- ^ needs at least 1 argument
527− -> [Expression commons schemas params grp from (nullty)]
567+ -> [Expression grp commons schemas params from (nullty)]
528568 -- ^ or more
529− -> Expression commons schemas params grp from (nullty)
569+ -> Expression grp commons schemas params from (nullty)
530570least x xs = UnsafeExpression $ "LEAST("
531571 <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
532572
@@ -535,9 +575,9 @@ least x xs = UnsafeExpression $ "LEAST("
535575unsafeBinaryOp
536576 :: ByteString
537577 -- ^ operator
538− -> Expression commons schemas params grp from (ty0)
539− -> Expression commons schemas params grp from (ty1)
540− -> Expression commons schemas params grp from (ty2)
578+ -> Expression grp commons schemas params from (ty0)
579+ -> Expression grp commons schemas params from (ty1)
580+ -> Expression grp commons schemas params from (ty2)
541581unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
542582 renderSQL x <+> op <+> renderSQL y
543583
@@ -546,8 +586,8 @@ unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
546586unsafeUnaryOp
547587 :: ByteString
548588 -- ^ operator
549− -> Expression commons schemas params grp from (ty0)
550− -> Expression commons schemas params grp from (ty1)
589+ -> Expression grp commons schemas params from (ty0)
590+ -> Expression grp commons schemas params from (ty1)
551591unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
552592 op <+> renderSQL x
553593
@@ -556,8 +596,8 @@ unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
556596unsafeFunction
557597 :: ByteString
558598 -- ^ function
559− -> Expression commons schemas params grp from (xty)
560− -> Expression commons schemas params grp from (yty)
599+ -> Expression grp commons schemas params from (xty)
600+ -> Expression grp commons schemas params from (yty)
561601unsafeFunction fun x = UnsafeExpression $
562602 fun <> parenthesized (renderSQL x)
563603
@@ -566,13 +606,13 @@ unsafeVariadicFunction
566606 :: SListI elems
567607 => ByteString
568608 -- ^ function
569− -> NP (Expression commons schemas params grp from) elems
570− -> Expression commons schemas params grp from ret
609+ -> NP (Expression grp commons schemas params from) elems
610+ -> Expression grp commons schemas params from ret
571611unsafeVariadicFunction fun x = UnsafeExpression $
572612 fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderSQL) x)))
573613
574614instance 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
576616 (+) = unsafeBinaryOp "+"
577617 (-) = unsafeBinaryOp "-"
578618 (*) = unsafeBinaryOp "*"
@@ -584,12 +624,12 @@ instance ty `In` PGNum
584624 . show
585625
586626instance (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
588628 (/) = unsafeBinaryOp "/"
589629 fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
590630
591631instance (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
593633 pi = UnsafeExpression "pi()"
594634 exp = unsafeFunction "exp"
595635 log = unsafeFunction "ln"
@@ -612,18 +652,18 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Floating
612652
613653-- | >>> :{
614654-- let
615−-- expression :: Expression commons schemas params grp from (nullity 'PGfloat4)
655+-- expression :: Expression grp commons schemas params from (nullity 'PGfloat4)
616656-- expression = atan2_ pi 2
617657-- in printSQL expression
618658-- :}
619659-- atan2(pi(), 2)
620660atan2_
621661 :: float `In` PGFloating
622− => Expression commons schemas params grp from (nullity float)
662+ => Expression grp commons schemas params from (nullity float)
623663 -- ^ numerator
624− -> Expression commons schemas params grp from (nullity float)
664+ -> Expression grp commons schemas params from (nullity float)
625665 -- ^ denominator
626− -> Expression commons schemas params grp from (nullity float)
666+ -> Expression grp commons schemas params from (nullity float)
627667atan2_ y x = UnsafeExpression $
628668 "atan2(" <> renderSQL y <> ", " <> renderSQL x <> ")"
629669
@@ -636,9 +676,9 @@ atan2_ y x = UnsafeExpression $
636676cast
637677 :: TypeExpression schemas ty1
638678 -- ^ type to cast as
639− -> Expression commons schemas params grp from ty0
679+ -> Expression grp commons schemas params from ty0
640680 -- ^ value to convert
641− -> Expression commons schemas params grp from ty1
681+ -> Expression grp commons schemas params from ty1
642682cast ty x = UnsafeExpression $ parenthesized $
643683 renderSQL x <+> "::" <+> renderSQL ty
644684
@@ -646,136 +686,136 @@ cast ty x = UnsafeExpression $ parenthesized $
646686--
647687-- >>> :{
648688-- let
649−-- expression :: Expression commons schemas params grp from (nullity 'PGint2)
689+-- expression :: Expression grp commons schemas params from (nullity 'PGint2)
650690-- expression = 5 `quot_` 2
651691-- in printSQL expression
652692-- :}
653693-- (5 / 2)
654694quot_
655695 :: int `In` PGIntegral
656− => Expression commons schemas params grp from (nullity int)
696+ => Expression grp commons schemas params from (nullity int)
657697 -- ^ numerator
658− -> Expression commons schemas params grp from (nullity int)
698+ -> Expression grp commons schemas params from (nullity int)
659699 -- ^ denominator
660− -> Expression commons schemas params grp from (nullity int)
700+ -> Expression grp commons schemas params from (nullity int)
661701quot_ = unsafeBinaryOp "/"
662702
663703-- | remainder upon integer division
664704--
665705-- >>> :{
666706-- let
667−-- expression :: Expression commons schemas params grp from (nullity 'PGint2)
707+-- expression :: Expression grp commons schemas params from (nullity 'PGint2)
668708-- expression = 5 `rem_` 2
669709-- in printSQL expression
670710-- :}
671711-- (5 % 2)
672712rem_
673713 :: int `In` PGIntegral
674− => Expression commons schemas params grp from (nullity int)
714+ => Expression grp commons schemas params from (nullity int)
675715 -- ^ numerator
676− -> Expression commons schemas params grp from (nullity int)
716+ -> Expression grp commons schemas params from (nullity int)
677717 -- ^ denominator
678− -> Expression commons schemas params grp from (nullity int)
718+ -> Expression grp commons schemas params from (nullity int)
679719rem_ = unsafeBinaryOp "%"
680720
681721-- | >>> :{
682722-- let
683−-- expression :: Expression commons schemas params grp from (nullity 'PGfloat4)
723+-- expression :: Expression grp commons schemas params from (nullity 'PGfloat4)
684724-- expression = trunc pi
685725-- in printSQL expression
686726-- :}
687727-- trunc(pi())
688728trunc
689729 :: frac `In` PGFloating
690− => Expression commons schemas params grp from (nullity frac)
730+ => Expression grp commons schemas params from (nullity frac)
691731 -- ^ fractional number
692− -> Expression commons schemas params grp from (nullity frac)
732+ -> Expression grp commons schemas params from (nullity frac)
693733trunc = unsafeFunction "trunc"
694734
695735-- | >>> :{
696736-- let
697−-- expression :: Expression commons schemas params grp from (nullity 'PGfloat4)
737+-- expression :: Expression grp commons schemas params from (nullity 'PGfloat4)
698738-- expression = round_ pi
699739-- in printSQL expression
700740-- :}
701741-- round(pi())
702742round_
703743 :: frac `In` PGFloating
704− => Expression commons schemas params grp from (nullity frac)
744+ => Expression grp commons schemas params from (nullity frac)
705745 -- ^ fractional number
706− -> Expression commons schemas params grp from (nullity frac)
746+ -> Expression grp commons schemas params from (nullity frac)
707747round_ = unsafeFunction "round"
708748
709749-- | >>> :{
710750-- let
711−-- expression :: Expression commons schemas params grp from (nullity 'PGfloat4)
751+-- expression :: Expression grp commons schemas params from (nullity 'PGfloat4)
712752-- expression = ceiling_ pi
713753-- in printSQL expression
714754-- :}
715755-- ceiling(pi())
716756ceiling_
717757 :: frac `In` PGFloating
718− => Expression commons schemas params grp from (nullity frac)
758+ => Expression grp commons schemas params from (nullity frac)
719759 -- ^ fractional number
720− -> Expression commons schemas params grp from (nullity frac)
760+ -> Expression grp commons schemas params from (nullity frac)
721761ceiling_ = unsafeFunction "ceiling"
722762
723763-- | A `Condition` is an `Expression`, which can evaluate
724764-- to `true`, `false` or `null_`. This is because SQL uses
725765-- a three valued logic.
726−type Condition commons schemas params grp from =
727− Expression commons schemas params grp from ('Null 'PGbool)
766+type Condition grp commons schemas params from =
767+ Expression grp commons schemas params from ('Null 'PGbool)
728768
729769-- | >>> printSQL true
730770-- TRUE
731−true :: Expression commons schemas params grp from (nullity 'PGbool)
771+true :: Expression grp commons schemas params from (nullity 'PGbool)
732772true = UnsafeExpression "TRUE"
733773
734774-- | >>> printSQL false
735775-- FALSE
736−false :: Expression commons schemas params grp from (nullity 'PGbool)
776+false :: Expression grp commons schemas params from (nullity 'PGbool)
737777false = UnsafeExpression "FALSE"
738778
739779-- | >>> printSQL $ not_ true
740780-- (NOT TRUE)
741781not_
742− :: Expression commons schemas params grp from (nullity 'PGbool)
743− -> Expression commons schemas params grp from (nullity 'PGbool)
782+ :: Expression grp commons schemas params from (nullity 'PGbool)
783+ -> Expression grp commons schemas params from (nullity 'PGbool)
744784not_ = unsafeUnaryOp "NOT"
745785
746786-- | >>> printSQL $ true .&& false
747787-- (TRUE AND FALSE)
748788(.&&)
749− :: Expression commons schemas params grp from (nullity 'PGbool)
750− -> Expression commons schemas params grp from (nullity 'PGbool)
751− -> Expression commons schemas params grp from (nullity 'PGbool)
789+ :: Expression grp commons schemas params from (nullity 'PGbool)
790+ -> Expression grp commons schemas params from (nullity 'PGbool)
791+ -> Expression grp commons schemas params from (nullity 'PGbool)
752792infixr 3 .&&
753793(.&&) = unsafeBinaryOp "AND"
754794
755795-- | >>> printSQL $ true .|| false
756796-- (TRUE OR FALSE)
757797(.||)
758− :: Expression commons schemas params grp from (nullity 'PGbool)
759− -> Expression commons schemas params grp from (nullity 'PGbool)
760− -> Expression commons schemas params grp from (nullity 'PGbool)
798+ :: Expression grp commons schemas params from (nullity 'PGbool)
799+ -> Expression grp commons schemas params from (nullity 'PGbool)
800+ -> Expression grp commons schemas params from (nullity 'PGbool)
761801infixr 2 .||
762802(.||) = unsafeBinaryOp "OR"
763803
764804-- | >>> :{
765805-- let
766−-- expression :: Expression commons schemas params grp from (nullity 'PGint2)
806+-- expression :: Expression grp commons schemas params from (nullity 'PGint2)
767807-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
768808-- in printSQL expression
769809-- :}
770810-- CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END
771811caseWhenThenElse
772− :: [ ( Condition commons schemas params grp from
773− , Expression commons schemas params grp from ty
812+ :: [ ( Condition grp commons schemas params from
813+ , Expression grp commons schemas params from ty
774814 ) ]
775815 -- ^ whens and thens
776− -> Expression commons schemas params grp from ty
816+ -> Expression grp commons schemas params from ty
777817 -- ^ else
778− -> Expression commons schemas params grp from ty
818+ -> Expression grp commons schemas params from ty
779819caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
780820 [ "CASE"
781821 , mconcat
@@ -791,16 +831,16 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
791831
792832-- | >>> :{
793833-- let
794−-- expression :: Expression commons schemas params grp from (nullity 'PGint2)
834+-- expression :: Expression grp commons schemas params from (nullity 'PGint2)
795835-- expression = ifThenElse true 1 0
796836-- in printSQL expression
797837-- :}
798838-- CASE WHEN TRUE THEN 1 ELSE 0 END
799839ifThenElse
800− :: Condition commons schemas params grp from
801− -> Expression commons schemas params grp from ty -- ^ then
802− -> Expression commons schemas params grp from ty -- ^ else
803− -> Expression commons schemas params grp from ty
840+ :: Condition grp commons schemas params from
841+ -> Expression grp commons schemas params from ty -- ^ then
842+ -> Expression grp commons schemas params from ty -- ^ else
843+ -> Expression grp commons schemas params from ty
804844ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
805845
806846-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
@@ -809,85 +849,85 @@ ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
809849-- >>> printSQL $ true .== null_
810850-- (TRUE = NULL)
811851(.==)
812− :: Expression commons schemas params grp from (nullity0 ty) -- ^ lhs
813− -> Expression commons schemas params grp from (nullity1 ty) -- ^ rhs
814− -> Condition commons schemas params grp from
852+ :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
853+ -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
854+ -> Condition grp commons schemas params from
815855(.==) = unsafeBinaryOp "="
816856infix 4 .==
817857
818858-- | >>> printSQL $ true ./= null_
819859-- (TRUE <> NULL)
820860(./=)
821− :: Expression commons schemas params grp from (nullity0 ty) -- ^ lhs
822− -> Expression commons schemas params grp from (nullity1 ty) -- ^ rhs
823− -> Condition commons schemas params grp from
861+ :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
862+ -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
863+ -> Condition grp commons schemas params from
824864(./=) = unsafeBinaryOp "<>"
825865infix 4 ./=
826866
827867-- | >>> printSQL $ true .>= null_
828868-- (TRUE >= NULL)
829869(.>=)
830− :: Expression commons schemas params grp from (nullity0 ty) -- ^ lhs
831− -> Expression commons schemas params grp from (nullity1 ty) -- ^ rhs
832− -> Condition commons schemas params grp from
870+ :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
871+ -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
872+ -> Condition grp commons schemas params from
833873(.>=) = unsafeBinaryOp ">="
834874infix 4 .>=
835875
836876-- | >>> printSQL $ true .< null_
837877-- (TRUE < NULL)
838878(.<)
839− :: Expression commons schemas params grp from (nullity0 ty) -- ^ lhs
840− -> Expression commons schemas params grp from (nullity1 ty) -- ^ rhs
841− -> Condition commons schemas params grp from
879+ :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
880+ -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
881+ -> Condition grp commons schemas params from
842882(.<) = unsafeBinaryOp "<"
843883infix 4 .<
844884
845885-- | >>> printSQL $ true .<= null_
846886-- (TRUE <= NULL)
847887(.<=)
848− :: Expression commons schemas params grp from (nullity0 ty) -- ^ lhs
849− -> Expression commons schemas params grp from (nullity1 ty) -- ^ rhs
850− -> Condition commons schemas params grp from
888+ :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
889+ -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
890+ -> Condition grp commons schemas params from
851891(.<=) = unsafeBinaryOp "<="
852892infix 4 .<=
853893
854894-- | >>> printSQL $ true .> null_
855895-- (TRUE > NULL)
856896(.>)
857− :: Expression commons schemas params grp from (nullity0 ty) -- ^ lhs
858− -> Expression commons schemas params grp from (nullity1 ty) -- ^ rhs
859− -> Condition commons schemas params grp from
897+ :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
898+ -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
899+ -> Condition grp commons schemas params from
860900(.>) = unsafeBinaryOp ">"
861901infix 4 .>
862902
863903-- | >>> printSQL currentDate
864904-- CURRENT_DATE
865905currentDate
866− :: Expression commons schemas params grp from (nullity 'PGdate)
906+ :: Expression grp commons schemas params from (nullity 'PGdate)
867907currentDate = UnsafeExpression "CURRENT_DATE"
868908
869909-- | >>> printSQL currentTime
870910-- CURRENT_TIME
871911currentTime
872− :: Expression commons schemas params grp from (nullity 'PGtimetz)
912+ :: Expression grp commons schemas params from (nullity 'PGtimetz)
873913currentTime = UnsafeExpression "CURRENT_TIME"
874914
875915-- | >>> printSQL currentTimestamp
876916-- CURRENT_TIMESTAMP
877917currentTimestamp
878− :: Expression commons schemas params grp from (nullity 'PGtimestamptz)
918+ :: Expression grp commons schemas params from (nullity 'PGtimestamptz)
879919currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
880920
881921-- | >>> printSQL localTime
882922-- LOCALTIME
883923localTime
884− :: Expression commons schemas params grp from (nullity 'PGtime)
924+ :: Expression grp commons schemas params from (nullity 'PGtime)
885925localTime = UnsafeExpression "LOCALTIME"
886926
887927-- | >>> printSQL localTimestamp
888928-- LOCALTIMESTAMP
889929localTimestamp
890− :: Expression commons schemas params grp from (nullity 'PGtimestamp)
930+ :: Expression grp commons schemas params from (nullity 'PGtimestamp)
891931localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
892932
893933{-----------------------------------------
@@ -895,7 +935,7 @@ text
895935-----------------------------------------}
896936
897937instance IsString
898− (Expression commons schemas params grp from (nullity 'PGtext)) where
938+ (Expression grp commons schemas params from (nullity 'PGtext)) where
899939 fromString str = UnsafeExpression $
900940 "E\'" <> fromString (escape =<< str) <> "\'"
901941 where
@@ -911,36 +951,36 @@ instance IsString
911951 c -> [c]
912952
913953instance Semigroup
914− (Expression commons schemas params grp from (nullity 'PGtext)) where
954+ (Expression grp commons schemas params from (nullity 'PGtext)) where
915955 (<>) = unsafeBinaryOp "||"
916956
917957instance Monoid
918− (Expression commons schemas params grp from (nullity 'PGtext)) where
958+ (Expression grp commons schemas params from (nullity 'PGtext)) where
919959 mempty = fromString ""
920960 mappend = (<>)
921961
922962-- | >>> printSQL $ lower "ARRRGGG"
923963-- lower(E'ARRRGGG')
924964lower
925− :: Expression commons schemas params grp from (nullity 'PGtext)
965+ :: Expression grp commons schemas params from (nullity 'PGtext)
926966 -- ^ string to lower case
927− -> Expression commons schemas params grp from (nullity 'PGtext)
967+ -> Expression grp commons schemas params from (nullity 'PGtext)
928968lower = unsafeFunction "lower"
929969
930970-- | >>> printSQL $ upper "eeee"
931971-- upper(E'eeee')
932972upper
933− :: Expression commons schemas params grp from (nullity 'PGtext)
973+ :: Expression grp commons schemas params from (nullity 'PGtext)
934974 -- ^ string to upper case
935− -> Expression commons schemas params grp from (nullity 'PGtext)
975+ -> Expression grp commons schemas params from (nullity 'PGtext)
936976upper = unsafeFunction "upper"
937977
938978-- | >>> printSQL $ charLength "four"
939979-- char_length(E'four')
940980charLength
941− :: Expression commons schemas params grp from (nullity 'PGtext)
981+ :: Expression grp commons schemas params from (nullity 'PGtext)
942982 -- ^ string to measure
943− -> Expression commons schemas params grp from (nullity 'PGint4)
983+ -> Expression grp commons schemas params from (nullity 'PGint4)
944984charLength = unsafeFunction "char_length"
945985
946986-- | The `like` expression returns true if the @string@ matches
@@ -953,11 +993,11 @@ charLength = unsafeFunction "char_length"
953993-- >>> printSQL $ "abc" `like` "a%"
954994-- (E'abc' LIKE E'a%')
955995like
956− :: Expression commons schemas params grp from (nullity 'PGtext)
996+ :: Expression grp commons schemas params from (nullity 'PGtext)
957997 -- ^ string
958− -> Expression commons schemas params grp from (nullity 'PGtext)
998+ -> Expression grp commons schemas params from (nullity 'PGtext)
959999 -- ^ pattern
960− -> Expression commons schemas params grp from (nullity 'PGbool)
1000+ -> Expression grp commons schemas params from (nullity 'PGbool)
9611001like = unsafeBinaryOp "LIKE"
9621002
9631003-- | The key word ILIKE can be used instead of LIKE to make the
@@ -966,11 +1006,11 @@ like = unsafeBinaryOp "LIKE"
9661006-- >>> printSQL $ "abc" `ilike` "a%"
9671007-- (E'abc' ILIKE E'a%')
9681008ilike
969− :: Expression commons schemas params grp from (nullity 'PGtext)
1009+ :: Expression grp commons schemas params from (nullity 'PGtext)
9701010 -- ^ string
971− -> Expression commons schemas params grp from (nullity 'PGtext)
1011+ -> Expression grp commons schemas params from (nullity 'PGtext)
9721012 -- ^ pattern
973− -> Expression commons schemas params grp from (nullity 'PGbool)
1013+ -> Expression grp commons schemas params from (nullity 'PGbool)
9741014ilike = unsafeBinaryOp "ILIKE"
9751015
9761016{-----------------------------------------
@@ -985,36 +1025,36 @@ Table 9.44: json and jsonb operators
9851025-- | Get JSON value (object field or array element) at a key.
9861026(.->)
9871027 :: (json `In` PGJsonType, key `In` PGJsonKey)
988− => Expression commons schemas params grp from (nullity json)
989− -> Expression commons schemas params grp from (nullity key)
990− -> Expression commons schemas params grp from ('Null json)
1028+ => Expression grp commons schemas params from (nullity json)
1029+ -> Expression grp commons schemas params from (nullity key)
1030+ -> Expression grp commons schemas params from ('Null json)
9911031infixl 8 .->
9921032(.->) = unsafeBinaryOp "->"
9931033
9941034-- | Get JSON value (object field or array element) at a key, as text.
9951035(.->>)
9961036 :: (json `In` PGJsonType, key `In` PGJsonKey)
997− => Expression commons schemas params grp from (nullity json)
998− -> Expression commons schemas params grp from (nullity key)
999− -> Expression commons schemas params grp from ('Null 'PGtext)
1037+ => Expression grp commons schemas params from (nullity json)
1038+ -> Expression grp commons schemas params from (nullity key)
1039+ -> Expression grp commons schemas params from ('Null 'PGtext)
10001040infixl 8 .->>
10011041(.->>) = unsafeBinaryOp "->>"
10021042
10031043-- | Get JSON value at a specified path.
10041044(.#>)
10051045 :: (json `In` PGJsonType, PGTextArray "(.#>)" path)
1006− => Expression commons schemas params grp from (nullity json)
1007− -> Expression commons schemas params grp from (nullity path)
1008− -> Expression commons schemas params grp from ('Null json)
1046+ => Expression grp commons schemas params from (nullity json)
1047+ -> Expression grp commons schemas params from (nullity path)
1048+ -> Expression grp commons schemas params from ('Null json)
10091049infixl 8 .#>
10101050(.#>) = unsafeBinaryOp "#>"
10111051
10121052-- | Get JSON value at a specified path as text.
10131053(.#>>)
10141054 :: (json `In` PGJsonType, PGTextArray "(.#>>)" path)
1015− => Expression commons schemas params grp from (nullity json)
1016− -> Expression commons schemas params grp from (nullity path)
1017− -> Expression commons schemas params grp from ('Null 'PGtext)
1055+ => Expression grp commons schemas params from (nullity json)
1056+ -> Expression grp commons schemas params from (nullity path)
1057+ -> Expression grp commons schemas params from ('Null 'PGtext)
10181058infixl 8 .#>>
10191059(.#>>) = unsafeBinaryOp "#>>"
10201060
@@ -1023,42 +1063,42 @@ infixl 8 .#>>
10231063-- | Does the left JSON value contain the right JSON path/value entries at the
10241064-- top level?
10251065(.@>)
1026− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1027− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1028− -> Condition commons schemas params grp from
1066+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1067+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1068+ -> Condition grp commons schemas params from
10291069infixl 9 .@>
10301070(.@>) = unsafeBinaryOp "@>"
10311071
10321072-- | Are the left JSON path/value entries contained at the top level within the
10331073-- right JSON value?
10341074(.<@)
1035− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1036− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1037− -> Condition commons schemas params grp from
1075+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1076+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1077+ -> Condition grp commons schemas params from
10381078infixl 9 .<@
10391079(.<@) = unsafeBinaryOp "<@"
10401080
10411081-- | Does the string exist as a top-level key within the JSON value?
10421082(.?)
1043− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1044− -> Expression commons schemas params grp from (nullity 'PGtext)
1045− -> Condition commons schemas params grp from
1083+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1084+ -> Expression grp commons schemas params from (nullity 'PGtext)
1085+ -> Condition grp commons schemas params from
10461086infixl 9 .?
10471087(.?) = unsafeBinaryOp "?"
10481088
10491089-- | Do any of these array strings exist as top-level keys?
10501090(.?|)
1051− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1052− -> Expression commons schemas params grp from (nullity ('PGvararray ('NotNull 'PGtext)))
1053− -> Condition commons schemas params grp from
1091+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1092+ -> Expression grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1093+ -> Condition grp commons schemas params from
10541094infixl 9 .?|
10551095(.?|) = unsafeBinaryOp "?|"
10561096
10571097-- | Do all of these array strings exist as top-level keys?
10581098(.?&)
1059− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1060− -> Expression commons schemas params grp from (nullity ('PGvararray ('NotNull 'PGtext)))
1061− -> Condition commons schemas params grp from
1099+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1100+ -> Expression grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1101+ -> Condition grp commons schemas params from
10621102infixl 9 .?&
10631103(.?&) = unsafeBinaryOp "?&"
10641104
@@ -1081,9 +1121,9 @@ instance
10811121-- count from the end). Throws an error if top level container is not an array.
10821122(.-.)
10831123 :: (key `In` '[ 'PGtext, 'PGvararray ('NotNull 'PGtext), 'PGint4, 'PGint2 ]) -- hlint error without parens here
1084− => Expression commons schemas params grp from (nullity 'PGjsonb)
1085− -> Expression commons schemas params grp from (nullity key)
1086− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1124+ => Expression grp commons schemas params from (nullity 'PGjsonb)
1125+ -> Expression grp commons schemas params from (nullity key)
1126+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
10871127infixl 6 .-.
10881128(.-.) = unsafeBinaryOp "-"
10891129
@@ -1091,9 +1131,9 @@ infixl 6 .-.
10911131-- integers count from the end)
10921132(#-.)
10931133 :: PGTextArray "(#-.)" arrayty
1094− => Expression commons schemas params grp from (nullity 'PGjsonb)
1095− -> Expression commons schemas params grp from (nullity arrayty)
1096− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1134+ => Expression grp commons schemas params from (nullity 'PGjsonb)
1135+ -> Expression grp commons schemas params from (nullity arrayty)
1136+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
10971137infixl 6 #-.
10981138(#-.) = unsafeBinaryOp "#-"
10991139
@@ -1104,14 +1144,14 @@ Table 9.45: JSON creation functions
11041144-- | Literal binary JSON
11051145jsonbLit
11061146 :: JSON.ToJSON x
1107− => x -> Expression commons schemas params grp from (nullity 'PGjsonb)
1147+ => x -> Expression grp commons schemas params from (nullity 'PGjsonb)
11081148jsonbLit = cast jsonb . UnsafeExpression
11091149 . singleQuotedUtf8 . toStrict . JSON.encode
11101150
11111151-- | Literal JSON
11121152jsonLit
11131153 :: JSON.ToJSON x
1114− => x -> Expression commons schemas params grp from (nullity 'PGjson)
1154+ => x -> Expression grp commons schemas params from (nullity 'PGjson)
11151155jsonLit = cast json . UnsafeExpression
11161156 . singleQuotedUtf8 . toStrict . JSON.encode
11171157
@@ -1122,8 +1162,8 @@ jsonLit = cast json . UnsafeExpression
11221162-- number, a Boolean, or a null value, the text representation will be used, in
11231163-- such a fashion that it is a valid json value.
11241164toJson
1125− :: Expression commons schemas params grp from (nullity ty)
1126− -> Expression commons schemas params grp from (nullity 'PGjson)
1165+ :: Expression grp commons schemas params from (nullity ty)
1166+ -> Expression grp commons schemas params from (nullity 'PGjson)
11271167toJson = unsafeFunction "to_json"
11281168
11291169-- | Returns the value as jsonb. Arrays and composites are converted
@@ -1133,43 +1173,43 @@ toJson = unsafeFunction "to_json"
11331173-- number, a Boolean, or a null value, the text representation will be used, in
11341174-- such a fashion that it is a valid jsonb value.
11351175toJsonb
1136− :: Expression commons schemas params grp from (nullity ty)
1137− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1176+ :: Expression grp commons schemas params from (nullity ty)
1177+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
11381178toJsonb = unsafeFunction "to_jsonb"
11391179
11401180-- | Returns the array as a JSON array. A PostgreSQL multidimensional array
11411181-- becomes a JSON array of arrays.
11421182arrayToJson
11431183 :: PGArray "arrayToJson" arr
1144− => Expression commons schemas params grp from (nullity arr)
1145− -> Expression commons schemas params grp from (nullity 'PGjson)
1184+ => Expression grp commons schemas params from (nullity arr)
1185+ -> Expression grp commons schemas params from (nullity 'PGjson)
11461186arrayToJson = unsafeFunction "array_to_json"
11471187
11481188-- | Returns the row as a JSON object.
11491189rowToJson
1150− :: Expression commons schemas params grp from (nullity ('PGcomposite ty))
1151− -> Expression commons schemas params grp from (nullity 'PGjson)
1190+ :: Expression grp commons schemas params from (nullity ('PGcomposite ty))
1191+ -> Expression grp commons schemas params from (nullity 'PGjson)
11521192rowToJson = unsafeFunction "row_to_json"
11531193
11541194-- | Builds a possibly-heterogeneously-typed JSON array out of a variadic
11551195-- argument list.
11561196jsonBuildArray
11571197 :: SListI elems
1158− => NP (Expression commons schemas params grp from) elems
1159− -> Expression commons schemas params grp from (nullity 'PGjson)
1198+ => NP (Expression grp commons schemas params from) elems
1199+ -> Expression grp commons schemas params from (nullity 'PGjson)
11601200jsonBuildArray = unsafeVariadicFunction "json_build_array"
11611201
11621202-- | Builds a possibly-heterogeneously-typed (binary) JSON array out of a
11631203-- variadic argument list.
11641204jsonbBuildArray
11651205 :: SListI elems
1166− => NP (Expression commons schemas params grp from) elems
1167− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1206+ => NP (Expression grp commons schemas params from) elems
1207+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
11681208jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
11691209
11701210unsafeRowFunction
11711211 :: All Top elems
1172− => NP (Aliased (Expression commons schemas params grp from)) elems
1212+ => NP (Aliased (Expression grp commons schemas params from)) elems
11731213 -> [ByteString]
11741214unsafeRowFunction =
11751215 (`appEndo` []) . hcfoldMap (Proxy :: Proxy Top)
@@ -1184,8 +1224,8 @@ unsafeRowFunction =
11841224-- and values.
11851225jsonBuildObject
11861226 :: All Top elems
1187− => NP (Aliased (Expression commons schemas params grp from)) elems
1188− -> Expression commons schemas params grp from (nullity 'PGjson)
1227+ => NP (Aliased (Expression grp commons schemas params from)) elems
1228+ -> Expression grp commons schemas params from (nullity 'PGjson)
11891229jsonBuildObject
11901230 = unsafeFunction "json_build_object"
11911231 . UnsafeExpression
@@ -1197,8 +1237,8 @@ jsonBuildObject
11971237-- between text and values.
11981238jsonbBuildObject
11991239 :: All Top elems
1200− => NP (Aliased (Expression commons schemas params grp from)) elems
1201− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1240+ => NP (Aliased (Expression grp commons schemas params from)) elems
1241+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
12021242jsonbBuildObject
12031243 = unsafeFunction "jsonb_build_object"
12041244 . UnsafeExpression
@@ -1211,8 +1251,8 @@ jsonbBuildObject
12111251-- array has exactly two elements, which are taken as a key/value pair.
12121252jsonObject
12131253 :: PGArrayOf "jsonObject" arr ('NotNull 'PGtext)
1214− => Expression commons schemas params grp from (nullity arr)
1215− -> Expression commons schemas params grp from (nullity 'PGjson)
1254+ => Expression grp commons schemas params from (nullity arr)
1255+ -> Expression grp commons schemas params from (nullity 'PGjson)
12161256jsonObject = unsafeFunction "json_object"
12171257
12181258-- | Builds a binary JSON object out of a text array. The array must have either
@@ -1221,8 +1261,8 @@ jsonObject = unsafeFunction "json_object"
12211261-- array has exactly two elements, which are taken as a key/value pair.
12221262jsonbObject
12231263 :: PGArrayOf "jsonbObject" arr ('NotNull 'PGtext)
1224− => Expression commons schemas params grp from (nullity arr)
1225− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1264+ => Expression grp commons schemas params from (nullity arr)
1265+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
12261266jsonbObject = unsafeFunction "jsonb_object"
12271267
12281268-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
@@ -1230,9 +1270,9 @@ jsonbObject = unsafeFunction "jsonb_object"
12301270jsonZipObject
12311271 :: ( PGArrayOf "jsonZipObject" keysArray ('NotNull 'PGtext)
12321272 , PGArrayOf "jsonZipObject" valuesArray ('NotNull 'PGtext))
1233− => Expression commons schemas params grp from (nullity keysArray)
1234− -> Expression commons schemas params grp from (nullity valuesArray)
1235− -> Expression commons schemas params grp from (nullity 'PGjson)
1273+ => Expression grp commons schemas params from (nullity keysArray)
1274+ -> Expression grp commons schemas params from (nullity valuesArray)
1275+ -> Expression grp commons schemas params from (nullity 'PGjson)
12361276jsonZipObject ks vs =
12371277 unsafeVariadicFunction "json_object" (ks :* vs :* Nil)
12381278
@@ -1242,9 +1282,9 @@ jsonZipObject ks vs =
12421282jsonbZipObject
12431283 :: ( PGArrayOf "jsonbZipObject" keysArray ('NotNull 'PGtext)
12441284 , PGArrayOf "jsonbZipObject" valuesArray ('NotNull 'PGtext))
1245− => Expression commons schemas params grp from (nullity keysArray)
1246− -> Expression commons schemas params grp from (nullity valuesArray)
1247− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1285+ => Expression grp commons schemas params from (nullity keysArray)
1286+ -> Expression grp commons schemas params from (nullity valuesArray)
1287+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
12481288jsonbZipObject ks vs =
12491289 unsafeVariadicFunction "jsonb_object" (ks :* vs :* Nil)
12501290
@@ -1254,23 +1294,23 @@ Table 9.46: JSON processing functions
12541294
12551295-- | Returns the number of elements in the outermost JSON array.
12561296jsonArrayLength
1257− :: Expression commons schemas params grp from (nullity 'PGjson)
1258− -> Expression commons schemas params grp from (nullity 'PGint4)
1297+ :: Expression grp commons schemas params from (nullity 'PGjson)
1298+ -> Expression grp commons schemas params from (nullity 'PGint4)
12591299jsonArrayLength = unsafeFunction "json_array_length"
12601300
12611301-- | Returns the number of elements in the outermost binary JSON array.
12621302jsonbArrayLength
1263− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1264− -> Expression commons schemas params grp from (nullity 'PGint4)
1303+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1304+ -> Expression grp commons schemas params from (nullity 'PGint4)
12651305jsonbArrayLength = unsafeFunction "jsonb_array_length"
12661306
12671307-- | Returns JSON value pointed to by the given path (equivalent to #>
12681308-- operator).
12691309jsonExtractPath
12701310 :: SListI elems
1271− => Expression commons schemas params grp from (nullity 'PGjson)
1272− -> NP (Expression commons schemas params grp from) elems
1273− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1311+ => Expression grp commons schemas params from (nullity 'PGjson)
1312+ -> NP (Expression grp commons schemas params from) elems
1313+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
12741314jsonExtractPath x xs =
12751315 unsafeVariadicFunction "json_extract_path" (x :* xs)
12761316
@@ -1278,9 +1318,9 @@ jsonExtractPath x xs =
12781318-- operator).
12791319jsonbExtractPath
12801320 :: SListI elems
1281− => Expression commons schemas params grp from (nullity 'PGjsonb)
1282− -> NP (Expression commons schemas params grp from) elems
1283− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1321+ => Expression grp commons schemas params from (nullity 'PGjsonb)
1322+ -> NP (Expression grp commons schemas params from) elems
1323+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
12841324jsonbExtractPath x xs =
12851325 unsafeVariadicFunction "jsonb_extract_path" (x :* xs)
12861326
@@ -1288,9 +1328,9 @@ jsonbExtractPath x xs =
12881328-- operator), as text.
12891329jsonExtractPathAsText
12901330 :: SListI elems
1291− => Expression commons schemas params grp from (nullity 'PGjson)
1292− -> NP (Expression commons schemas params grp from) elems
1293− -> Expression commons schemas params grp from (nullity 'PGjson)
1331+ => Expression grp commons schemas params from (nullity 'PGjson)
1332+ -> NP (Expression grp commons schemas params from) elems
1333+ -> Expression grp commons schemas params from (nullity 'PGjson)
12941334jsonExtractPathAsText x xs =
12951335 unsafeVariadicFunction "json_extract_path_text" (x :* xs)
12961336
@@ -1298,38 +1338,38 @@ jsonExtractPathAsText x xs =
12981338-- operator), as text.
12991339jsonbExtractPathAsText
13001340 :: SListI elems
1301− => Expression commons schemas params grp from (nullity 'PGjsonb)
1302− -> NP (Expression commons schemas params grp from) elems
1303− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1341+ => Expression grp commons schemas params from (nullity 'PGjsonb)
1342+ -> NP (Expression grp commons schemas params from) elems
1343+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
13041344jsonbExtractPathAsText x xs =
13051345 unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
13061346
13071347-- | Returns the type of the outermost JSON value as a text string. Possible
13081348-- types are object, array, string, number, boolean, and null.
13091349jsonTypeof
1310− :: Expression commons schemas params grp from (nullity 'PGjson)
1311− -> Expression commons schemas params grp from (nullity 'PGtext)
1350+ :: Expression grp commons schemas params from (nullity 'PGjson)
1351+ -> Expression grp commons schemas params from (nullity 'PGtext)
13121352jsonTypeof = unsafeFunction "json_typeof"
13131353
13141354-- | Returns the type of the outermost binary JSON value as a text string.
13151355-- Possible types are object, array, string, number, boolean, and null.
13161356jsonbTypeof
1317− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1318− -> Expression commons schemas params grp from (nullity 'PGtext)
1357+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1358+ -> Expression grp commons schemas params from (nullity 'PGtext)
13191359jsonbTypeof = unsafeFunction "jsonb_typeof"
13201360
13211361-- | Returns its argument with all object fields that have null values omitted.
13221362-- Other null values are untouched.
13231363jsonStripNulls
1324− :: Expression commons schemas params grp from (nullity 'PGjson)
1325− -> Expression commons schemas params grp from (nullity 'PGjson)
1364+ :: Expression grp commons schemas params from (nullity 'PGjson)
1365+ -> Expression grp commons schemas params from (nullity 'PGjson)
13261366jsonStripNulls = unsafeFunction "json_strip_nulls"
13271367
13281368-- | Returns its argument with all object fields that have null values omitted.
13291369-- Other null values are untouched.
13301370jsonbStripNulls
1331− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1332− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1371+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1372+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
13331373jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
13341374
13351375-- | @ jsonbSet target path new_value create_missing @
@@ -1341,11 +1381,11 @@ jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
13411381-- arrays.
13421382jsonbSet
13431383 :: PGTextArray "jsonbSet" arr
1344− => Expression commons schemas params grp from (nullity 'PGjsonb)
1345− -> Expression commons schemas params grp from (nullity arr)
1346− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1347− -> Maybe (Expression commons schemas params grp from (nullity 'PGbool))
1348− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1384+ => Expression grp commons schemas params from (nullity 'PGjsonb)
1385+ -> Expression grp commons schemas params from (nullity arr)
1386+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1387+ -> Maybe (Expression grp commons schemas params from (nullity 'PGbool))
1388+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
13491389jsonbSet tgt path val createMissing = case createMissing of
13501390 Just m -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* m :* Nil)
13511391 Nothing -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* Nil)
@@ -1360,267 +1400,411 @@ jsonbSet tgt path val createMissing = case createMissing of
13601400-- in path count from the end of JSON arrays.
13611401jsonbInsert
13621402 :: PGTextArray "jsonbInsert" arr
1363− => Expression commons schemas params grp from (nullity 'PGjsonb)
1364− -> Expression commons schemas params grp from (nullity arr)
1365− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1366− -> Maybe (Expression commons schemas params grp from (nullity 'PGbool))
1367− -> Expression commons schemas params grp from (nullity 'PGjsonb)
1403+ => Expression grp commons schemas params from (nullity 'PGjsonb)
1404+ -> Expression grp commons schemas params from (nullity arr)
1405+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
1406+ -> Maybe (Expression grp commons schemas params from (nullity 'PGbool))
1407+ -> Expression grp commons schemas params from (nullity 'PGjsonb)
13681408jsonbInsert tgt path val insertAfter = case insertAfter of
13691409 Just i -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* i :* Nil)
13701410 Nothing -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* Nil)
13711411
13721412-- | Returns its argument as indented JSON text.
13731413jsonbPretty
1374− :: Expression commons schemas params grp from (nullity 'PGjsonb)
1375− -> Expression commons schemas params grp from (nullity 'PGtext)
1414+ :: Expression grp commons schemas params from (nullity 'PGjsonb)
1415+ -> Expression grp commons schemas params from (nullity 'PGtext)
13761416jsonbPretty = unsafeFunction "jsonb_pretty"
13771417
13781418{-----------------------------------------
13791419aggregation
13801420-----------------------------------------}
13811421
1422+class Aggregate expr1 expr2 aggr
1423+ | aggr -> expr1, aggr -> expr2 where
1424+
1425+ -- | A special aggregation that does not require an input
1426+ --
1427+ -- >>> :{
1428+ -- let
1429+ -- expression :: Expression (Grouped bys) commons schemas params from ('NotNull 'PGint8)
1430+ -- expression = countStar
1431+ -- in printSQL expression
1432+ -- :}
1433+ -- count(*)
1434+ countStar :: aggr ('NotNull 'PGint8)
1435+
1436+ -- | >>> :{
1437+ -- let
1438+ -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1439+ -- expression = count (All #col)
1440+ -- in printSQL expression
1441+ -- :}
1442+ -- count(ALL "col")
1443+ count
1444+ :: expr1 ty
1445+ -- ^ what to count
1446+ -> aggr ('NotNull 'PGint8)
1447+
1448+ -- | >>> :{
1449+ -- let
1450+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1451+ -- expression = sum_ (Distinct #col)
1452+ -- in printSQL expression
1453+ -- :}
1454+ -- sum(DISTINCT "col")
1455+ sum_
1456+ :: ty `In` PGNum
1457+ => expr1 (nullity ty)
1458+ -> aggr (nullity ty)
1459+
1460+ -- | >>> :{
1461+ -- let
1462+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1463+ -- expression = bitAnd (Distinct #col)
1464+ -- in printSQL expression
1465+ -- :}
1466+ -- bit_and(DISTINCT "col")
1467+ bitAnd
1468+ :: int `In` PGIntegral
1469+ => expr1 (nullity int)
1470+ -- ^ what to aggregate
1471+ -> aggr (nullity int)
1472+
1473+ -- | >>> :{
1474+ -- let
1475+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1476+ -- expression = bitOr (All #col)
1477+ -- in printSQL expression
1478+ -- :}
1479+ -- bit_or(ALL "col")
1480+ bitOr
1481+ :: int `In` PGIntegral
1482+ => expr1 (nullity int)
1483+ -- ^ what to aggregate
1484+ -> aggr (nullity int)
1485+
1486+ -- | >>> :{
1487+ -- let
1488+ -- winFun :: WindowFunction 'Ungrouped commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1489+ -- winFun = boolAnd #col
1490+ -- in printSQL winFun
1491+ -- :}
1492+ -- bool_and("col")
1493+ boolAnd
1494+ :: expr1 (nullity 'PGbool)
1495+ -- ^ what to aggregate
1496+ -> aggr (nullity 'PGbool)
1497+
1498+ -- | >>> :{
1499+ -- let
1500+ -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1501+ -- expression = boolOr (All #col)
1502+ -- in printSQL expression
1503+ -- :}
1504+ -- bool_or(ALL "col")
1505+ boolOr
1506+ :: expr1(nullity 'PGbool)
1507+ -- ^ what to aggregate
1508+ -> aggr (nullity 'PGbool)
1509+
1510+ -- | synonym for `boolAnd`
1511+ --
1512+ -- >>> :{
1513+ -- let
1514+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1515+ -- expression = every (Distinct #col)
1516+ -- in printSQL expression
1517+ -- :}
1518+ -- every(DISTINCT "col")
1519+ every
1520+ :: expr1 (nullity 'PGbool)
1521+ -- ^ what to aggregate
1522+ -> aggr (nullity 'PGbool)
1523+
1524+ -- | maximum value of expression across all input values
1525+ max_
1526+ :: expr1 (nullity ty)
1527+ -- ^ what to maximize
1528+ -> aggr (nullity ty)
1529+
1530+ -- | minimum value of expression across all input values
1531+ min_
1532+ :: expr1 (nullity ty)
1533+ -- ^ what to minimize
1534+ -> aggr (nullity ty)
1535+
1536+ -- | average aggregation
1537+ avg
1538+ :: expr1 (nullity ty)
1539+ -- ^ what to average
1540+ -> aggr (nullity (PGAvg ty))
1541+
1542+ {- | correlation coefficient
1543+ >>> :{
1544+ let
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
1548+ :}
1549+ corr(ALL "y", "x")
1550+ -}
1551+ corr
1552+ :: expr2 (nullity 'PGfloat8)
1553+ -> aggr (nullity 'PGfloat8)
1554+
1555+ {- | population covariance
1556+ >>> :{
1557+ let
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
1561+ :}
1562+ covar_pop(ALL "y", "x")
1563+ -}
1564+ covarPop
1565+ :: expr2 (nullity 'PGfloat8)
1566+ -> aggr (nullity 'PGfloat8)
1567+
1568+ {- | sample covariance
1569+ >>> :{
1570+ let
1571+ winFun :: WindowFunction 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1572+ winFun = covarSamp (#y :*: #x)
1573+ in printSQL winFun
1574+ :}
1575+ covar_samp("y", "x")
1576+ -}
1577+ covarSamp
1578+ :: expr2 (nullity 'PGfloat8)
1579+ -> aggr (nullity 'PGfloat8)
1580+
1581+ {- | average of the independent variable (sum(X)/N)
1582+ >>> :{
1583+ let
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
1587+ :}
1588+ regr_avgx(ALL "y", "x")
1589+ -}
1590+ regrAvgX
1591+ :: expr2 (nullity 'PGfloat8)
1592+ -> aggr (nullity 'PGfloat8)
1593+
1594+ {- | average of the dependent variable (sum(Y)/N)
1595+ >>> :{
1596+ let
1597+ winFun :: WindowFunction 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1598+ winFun = regrAvgY (#y :*: #x)
1599+ in printSQL winFun
1600+ :}
1601+ regr_avgy("y", "x")
1602+ -}
1603+ regrAvgY
1604+ :: expr2 (nullity 'PGfloat8)
1605+ -> aggr (nullity 'PGfloat8)
1606+
1607+ {- | number of input rows in which both expressions are nonnull
1608+ >>> :{
1609+ let
1610+ winFun :: WindowFunction 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGint8)
1611+ winFun = regrCount (#y :*: #x)
1612+ in printSQL winFun
1613+ :}
1614+ regr_count("y", "x")
1615+ -}
1616+ regrCount
1617+ :: expr2 (nullity 'PGfloat8)
1618+ -> aggr (nullity 'PGint8)
1619+
1620+ {- | y-intercept of the least-squares-fit linear equation determined by the (X, Y) pairs
1621+ >>> :{
1622+ let
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
1626+ :}
1627+ regr_intercept(ALL "y", "x")
1628+ -}
1629+ regrIntercept
1630+ :: expr2 (nullity 'PGfloat8)
1631+ -> aggr (nullity 'PGfloat8)
1632+
1633+ -- | @regr_r2(Y, X)@, square of the correlation coefficient
1634+ regrR2
1635+ :: expr2 (nullity 'PGfloat8)
1636+ -> aggr (nullity 'PGfloat8)
1637+
1638+ -- | @regr_slope(Y, X)@, slope of the least-squares-fit linear equation
1639+ -- determined by the (X, Y) pairs
1640+ regrSlope
1641+ :: expr2 (nullity 'PGfloat8)
1642+ -> aggr (nullity 'PGfloat8)
1643+
1644+ -- | @regr_sxx(Y, X)@, sum(X^2) - sum(X)^2/N
1645+ -- (“sum of squares” of the independent variable)
1646+ regrSxx
1647+ :: expr2 (nullity 'PGfloat8)
1648+ -> aggr (nullity 'PGfloat8)
1649+
1650+ -- | @regr_sxy(Y, X)@, sum(X*Y) - sum(X) * sum(Y)/N
1651+ -- (“sum of products” of independent times dependent variable)
1652+ regrSxy
1653+ :: expr2 (nullity 'PGfloat8)
1654+ -> aggr (nullity 'PGfloat8)
1655+
1656+ -- | @regr_syy(Y, X)@, sum(Y^2) - sum(Y)^2/N
1657+ -- (“sum of squares” of the dependent variable)
1658+ regrSyy
1659+ :: expr2 (nullity 'PGfloat8)
1660+ -> aggr (nullity 'PGfloat8)
1661+
1662+ -- | historical alias for `stddevSamp`
1663+ stddev
1664+ :: expr1 (nullity ty)
1665+ -> aggr (nullity (PGAvg ty))
1666+
1667+ -- | population standard deviation of the input values
1668+ stddevPop
1669+ :: expr1 (nullity ty)
1670+ -> aggr (nullity (PGAvg ty))
1671+
1672+ -- | sample standard deviation of the input values
1673+ stddevSamp
1674+ :: expr1 (nullity ty)
1675+ -> aggr (nullity (PGAvg ty))
1676+
1677+ -- | historical alias for `varSamp`
1678+ variance
1679+ :: expr1 (nullity ty)
1680+ -> aggr (nullity (PGAvg ty))
1681+
1682+ -- | population variance of the input values
1683+ -- (square of the population standard deviation)
1684+ varPop
1685+ :: expr1 (nullity ty)
1686+ -> aggr (nullity (PGAvg ty))
1687+
1688+ -- | sample variance of the input values
1689+ -- (square of the sample standard deviation)
1690+ varSamp
1691+ :: expr1 (nullity ty)
1692+ -> aggr (nullity (PGAvg ty))
1693+
1694+data Distinction expr ty
1695+ = All (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
1700+ renderSQL = \case
1701+ All x -> "ALL" <+> renderSQL x
1702+ Distinct x -> "DISTINCT" <+> renderSQL x
1703+instance RenderSQL
1704+ (Distinction
1705+ ( Expression grp commons schemas params from
1706+ :*:
1707+ Expression grp commons schemas params from) xty ) where
1708+ renderSQL = \case
1709+ All (x :*: x') ->
1710+ "ALL" <+> renderSQL x <> ", " <> renderSQL x'
1711+ Distinct (x :*: x') ->
1712+ "DISTINCT" <+> renderSQL x <> ", " <> renderSQL x'
1713+
1714+instance Aggregate
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"
1747+instance Aggregate
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"
1780+
13821781-- | escape hatch to define aggregate functions
1383−unsafeAggregate
1782+unsafeAggregate1
13841783 :: ByteString -- ^ aggregate function
1385− -> Expression commons schemas from 'Ungrouped params (xty)
1386− -> Expression commons schemas from ('Grouped bys) params (yty)
1387−unsafeAggregate fun x = UnsafeExpression $ mconcat
1784+ -> Distinction (Expression 'Ungrouped commons schemas params from) xty
1785+ -> Expression ('Grouped bys) commons schemas params from yty
1786+unsafeAggregate1 fun x = UnsafeExpression $ mconcat
13881787 [fun, "(", renderSQL x, ")"]
13891788
1390−-- | escape hatch to define aggregate functions over distinct values
1391−unsafeAggregateDistinct
1789+-- | escape hatch to define aggregate binary functions
1790+unsafeAggregate2
13921791 :: ByteString -- ^ aggregate function
1393− -> Expression commons schemas from 'Ungrouped params (xty)
1394− -> Expression commons schemas from ('Grouped bys) params (yty)
1395−unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
1396− [fun, "(DISTINCT ", renderSQL x, ")"]
1397−
1398−-- | >>> :{
1399−-- let
1400−-- expression :: Expression commons schemas params ('Grouped bys) '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1401−-- expression = sum_ #col
1402−-- in printSQL expression
1403−-- :}
1404−-- sum("col")
1405−sum_
1406− :: ty `In` PGNum
1407− => Expression commons schemas from 'Ungrouped params (nullity ty)
1408− -- ^ what to sum
1409− -> Expression commons schemas from ('Grouped bys) params (nullity ty)
1410−sum_ = unsafeAggregate "sum"
1411−
1412−-- | >>> :{
1413−-- let
1414−-- expression :: Expression commons schemas params ('Grouped bys) '[tab ::: '["col" ::: nullity 'PGnumeric]] (nullity 'PGnumeric)
1415−-- expression = sumDistinct #col
1416−-- in printSQL expression
1417−-- :}
1418−-- sum(DISTINCT "col")
1419−sumDistinct
1420− :: ty `In` PGNum
1421− => Expression commons schemas from 'Ungrouped params (nullity ty)
1422− -- ^ what to sum
1423− -> Expression commons schemas from ('Grouped bys) params (nullity ty)
1424−sumDistinct = unsafeAggregateDistinct "sum"
1425−
1426−-- | A constraint for `PGType`s that you can take averages of and the resulting
1427−-- `PGType`.
1428−class PGAvg ty avg | ty -> avg where
1429− avg, avgDistinct
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
1436−instance PGAvg 'PGint4 'PGnumeric
1437−instance PGAvg 'PGint8 'PGnumeric
1438−instance PGAvg 'PGnumeric 'PGnumeric
1439−instance PGAvg 'PGfloat4 'PGfloat8
1440−instance PGAvg 'PGfloat8 'PGfloat8
1441−instance PGAvg 'PGinterval 'PGinterval
1442−
1443−-- | >>> :{
1444−-- let
1445−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1446−-- expression = bitAnd #col
1447−-- in printSQL expression
1448−-- :}
1449−-- bit_and("col")
1450−bitAnd
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"
1456−
1457−-- | >>> :{
1458−-- let
1459−-- expression :: Expression commons schemas params ('Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1460−-- expression = bitOr #col
1461−-- in printSQL expression
1462−-- :}
1463−-- bit_or("col")
1464−bitOr
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"
1470−
1471−-- | >>> :{
1472−-- let
1473−-- expression :: Expression commons schemas params ('Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1474−-- expression = bitAndDistinct #col
1475−-- in printSQL expression
1476−-- :}
1477−-- bit_and(DISTINCT "col")
1478−bitAndDistinct
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"
1484−
1485−-- | >>> :{
1486−-- let
1487−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1488−-- expression = bitOrDistinct #col
1489−-- in printSQL expression
1490−-- :}
1491−-- bit_or(DISTINCT "col")
1492−bitOrDistinct
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"
1498−
1499−-- | >>> :{
1500−-- let
1501−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1502−-- expression = boolAnd #col
1503−-- in printSQL expression
1504−-- :}
1505−-- bool_and("col")
1506−boolAnd
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"
1511−
1512−-- | >>> :{
1513−-- let
1514−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1515−-- expression = boolOr #col
1516−-- in printSQL expression
1517−-- :}
1518−-- bool_or("col")
1519−boolOr
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"
1524−
1525−-- | >>> :{
1526−-- let
1527−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1528−-- expression = boolAndDistinct #col
1529−-- in printSQL expression
1530−-- :}
1531−-- bool_and(DISTINCT "col")
1532−boolAndDistinct
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"
1537−
1538−-- | >>> :{
1539−-- let
1540−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1541−-- expression = boolOrDistinct #col
1542−-- in printSQL expression
1543−-- :}
1544−-- bool_or(DISTINCT "col")
1545−boolOrDistinct
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"
1550−
1551−-- | A special aggregation that does not require an input
1552−--
1553−-- >>> printSQL countStar
1554−-- count(*)
1555−countStar
1556− :: Expression commons schemas from ('Grouped bys) params ('NotNull 'PGint8)
1557−countStar = UnsafeExpression $ "count(*)"
1558−
1559−-- | >>> :{
1560−-- let
1561−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1562−-- expression = count #col
1563−-- in printSQL expression
1564−-- :}
1565−-- count("col")
1566−count
1567− :: Expression commons schemas from 'Ungrouped params ty
1568− -- ^ what to count
1569− -> Expression commons schemas from ('Grouped bys) params ('NotNull 'PGint8)
1570−count = unsafeAggregate "count"
1571−
1572−-- | >>> :{
1573−-- let
1574−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1575−-- expression = countDistinct #col
1576−-- in printSQL expression
1577−-- :}
1578−-- count(DISTINCT "col")
1579−countDistinct
1580− :: Expression commons schemas from 'Ungrouped params ty
1581− -- ^ what to count
1582− -> Expression commons schemas from ('Grouped bys) params ('NotNull 'PGint8)
1583−countDistinct = unsafeAggregateDistinct "count"
1584−
1585−-- | synonym for `boolAnd`
1586−--
1587−-- >>> :{
1588−-- let
1589−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1590−-- expression = every #col
1591−-- in printSQL expression
1592−-- :}
1593−-- every("col")
1594−every
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"
1599−
1600−-- | synonym for `boolAndDistinct`
1601−--
1602−-- >>> :{
1603−-- let
1604−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1605−-- expression = everyDistinct #col
1606−-- in printSQL expression
1607−-- :}
1608−-- every(DISTINCT "col")
1609−everyDistinct
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"
1614−
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"
1792+ -> Distinction (Expression 'Ungrouped commons schemas params from :*: Expression 'Ungrouped commons schemas params from) xty
1793+ -> Expression ('Grouped bys) commons schemas params from yty
1794+unsafeAggregate2 fun xx = UnsafeExpression $ mconcat
1795+ [fun, "(", renderSQL xx, ")"]
1796+
1797+-- | A type family that calculates `PGAvg` type of a `PGType`.
1798+type family PGAvg ty where
1799+ PGAvg 'PGint2 = 'PGnumeric
1800+ PGAvg 'PGint4 = 'PGnumeric
1801+ PGAvg 'PGint8 = 'PGnumeric
1802+ PGAvg 'PGnumeric = 'PGnumeric
1803+ PGAvg 'PGfloat4 = 'PGfloat8
1804+ PGAvg 'PGfloat8 = 'PGfloat8
1805+ PGAvg 'PGinterval = 'PGinterval
1806+ PGAvg pg = TypeError
1807+ ('Text "Squeal type error: No average for " ':<>: 'ShowType pg)
16241808
16251809{-----------------------------------------
16261810type expressions
@@ -1789,3 +1973,219 @@ instance PGTyped schemas ty
17891973instance (All KnownNat dims, PGTyped schemas ty)
17901974 => PGTyped schemas (nullity ('PGfixarray dims ty)) where
17911975 pgtype = fixarray @dims (pgtype @schemas @ty)
1976+
1977+{-----------------------------------------
1978+Sorting
1979+-----------------------------------------}
1980+
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
1990+ Asc
1991+ :: Expression grp commons schemas params from ('NotNull ty)
1992+ -> SortExpression grp commons schemas params from
1993+ Desc
1994+ :: Expression grp commons schemas params from ('NotNull ty)
1995+ -> SortExpression grp commons schemas params from
1996+ AscNullsFirst
1997+ :: Expression grp commons schemas params from ('Null ty)
1998+ -> SortExpression grp commons schemas params from
1999+ AscNullsLast
2000+ :: Expression grp commons schemas params from ('Null ty)
2001+ -> SortExpression grp commons schemas params from
2002+ DescNullsFirst
2003+ :: Expression grp commons schemas params from ('Null ty)
2004+ -> SortExpression grp commons schemas params from
2005+ DescNullsLast
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)
2009+
2010+class OrderBy expr where
2011+ orderBy
2012+ :: [SortExpression grp commons schemas params from]
2013+ -> expr grp commons schemas params from
2014+ -> expr grp commons schemas params from
2015+
2016+-- | Render a `SortExpression`.
2017+instance RenderSQL (SortExpression grp commons schemas params from) where
2018+ renderSQL = \case
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"
2027+
2028+data WindowDefinition grp commons schemas params from where
2029+ WindowDefinition
2030+ :: SListI bys
2031+ => NP (Expression grp commons schemas params from) bys
2032+ -> [SortExpression grp commons schemas params from]
2033+ -> WindowDefinition grp commons schemas params from
2034+
2035+instance OrderBy WindowDefinition where
2036+ orderBy sortsR (WindowDefinition parts sortsL)
2037+ = WindowDefinition parts (sortsL ++ sortsR)
2038+
2039+instance RenderSQL (WindowDefinition commons schemas from grp params) where
2040+ renderSQL (WindowDefinition part ord) =
2041+ renderPartitionByClause part <> renderOrderByClause ord
2042+ where
2043+ renderPartitionByClause = \case
2044+ Nil -> ""
2045+ parts -> "PARTITION" <+> "BY" <+> renderCommaSeparated renderExpression parts
2046+ renderOrderByClause = \case
2047+ [] -> ""
2048+ srts -> " ORDER" <+> "BY"
2049+ <+> commaSeparated (renderSQL <$> srts)
2050+
2051+partitionBy
2052+ :: SListI bys
2053+ => NP (Expression grp commons schemas params from) bys
2054+ -> WindowDefinition grp commons schemas params from
2055+partitionBy bys = WindowDefinition bys []
2056+
2057+newtype WindowFunction
2058+ (grp :: Grouping)
2059+ (commons :: FromType)
2060+ (schemas :: SchemasType)
2061+ (params :: [NullityType])
2062+ (from :: FromType)
2063+ (ty :: NullityType)
2064+ = UnsafeWindowFunction { renderWindowFunction :: ByteString }
2065+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
2066+
2067+instance RenderSQL (WindowFunction grp commons schemas params from ty) where
2068+ renderSQL = renderWindowFunction
2069+
2070+unsafeWindowFunction1
2071+ :: ByteString
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)
2076+
2077+unsafeWindowFunction2
2078+ :: ByteString
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')
2083+
2084+{- | rank of the current row with gaps; same as `rowNumber` of its first peer
2085+>>> printSQL rank
2086+rank()
2087+-}
2088+rank :: WindowFunction grp commons schemas params from ('NotNull 'PGint8)
2089+rank = UnsafeWindowFunction "rank()"
2090+
2091+{- | number of the current row within its partition, counting from 1
2092+>>> printSQL rowNumber
2093+row_number()
2094+-}
2095+rowNumber :: WindowFunction grp commons schemas params from ('NotNull 'PGint8)
2096+rowNumber = UnsafeWindowFunction "row_number()"
2097+
2098+{- | rank of the current row without gaps; this function counts peer groups
2099+>>> printSQL denseRank
2100+dense_rank()
2101+-}
2102+denseRank :: WindowFunction grp commons schemas params from ('NotNull 'PGint8)
2103+denseRank = UnsafeWindowFunction "dense_rank()"
2104+
2105+{- | relative rank of the current row: (rank - 1) / (total partition rows - 1)
2106+>>> printSQL percentRank
2107+percent_rank()
2108+-}
2109+percentRank :: WindowFunction grp commons schemas params from ('NotNull 'PGfloat8)
2110+percentRank = UnsafeWindowFunction "percent_rank()"
2111+
2112+{- | cumulative distribution: (number of partition rows
2113+preceding or peer with current row) / total partition rows
2114+>>> printSQL cumeDist
2115+cume_dist()
2116+-}
2117+cumeDist :: WindowFunction grp commons schemas params from ('NotNull 'PGfloat8)
2118+cumeDist = UnsafeWindowFunction "cume_dist()"
2119+
2120+{- | integer ranging from 1 to the argument value,
2121+dividing the partition as equally as possible
2122+-}
2123+ntile
2124+ :: Expression grp commons schemas params from ('NotNull 'PGint4)
2125+ -- ^ num buckets
2126+ -> WindowFunction grp commons schemas params from ('NotNull 'PGint4)
2127+ntile = unsafeWindowFunction1 "ntile"
2128+
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.
2133+-}
2134+lag
2135+ :: Expression grp commons schemas params from ty
2136+ -- ^ value
2137+ -> Expression grp commons schemas params from ('NotNull 'PGint4)
2138+ -- ^ offset
2139+ -> Expression grp commons schemas params from ty
2140+ -- ^ default
2141+ -> WindowFunction grp commons schemas params from ty
2142+lag value offset def = UnsafeWindowFunction $ "lag"
2143+ <> parenthesized
2144+ (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
2145+
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.
2150+-}
2151+lead
2152+ :: Expression grp commons schemas params from ty
2153+ -- ^ value
2154+ -> Expression grp commons schemas params from ('NotNull 'PGint4)
2155+ -- ^ offset
2156+ -> Expression grp commons schemas params from ty
2157+ -- ^ default
2158+ -> WindowFunction grp commons schemas params from ty
2159+lead value offset def = UnsafeWindowFunction $ "lag"
2160+ <> parenthesized
2161+ (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
2162+
2163+{- | returns value evaluated at the row that is the
2164+first row of the window frame
2165+-}
2166+firstValue
2167+ :: Expression grp commons schemas params from ty
2168+ -- ^ value
2169+ -> WindowFunction grp commons schemas params from ty
2170+firstValue = unsafeWindowFunction1 "first_value"
2171+
2172+{- | returns value evaluated at the row that is the
2173+last row of the window frame
2174+-}
2175+lastValue
2176+ :: Expression grp commons schemas params from ty
2177+ -- ^ value
2178+ -> WindowFunction grp commons schemas params from ty
2179+lastValue = unsafeWindowFunction1 "last_value"
2180+
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
2183+-}
2184+nthValue
2185+ :: Expression grp commons schemas params from (nullity ty)
2186+ -- ^ value
2187+ -> Expression grp commons schemas params from ('NotNull 'PGint4)
2188+ -- ^ nth
2189+ -> WindowFunction grp commons schemas params from ('Null ty)
2190+nthValue value nth = UnsafeWindowFunction $ "nth_value"
2191+ <> parenthesized (commaSeparated [renderSQL value, renderSQL nth])