on dev help sign in
Merge pull request #97 from morphismtech/dev-window-stats

Dev window stats

commit7caa3a2ff01c66581f34ce7262532a096cbc74e7 authorEitan Chatav <eitan.chatav@gmail.com> dateFri 15 Feb 2019, 23:37 (8 years ago) parent26457ddc1102b2
4 files changed+1111−737
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+3−3 squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+897−497 squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+36−36 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+175−201
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+3−3
@@ -298,7 +298,7 @@ check
298298 , HasAll aliases (TableToRow table) subcolumns )
299299 => NP Alias aliases
300300 -- ^ specify the subcolumns which are getting checked
301− -> (forall t. Condition '[] schemas '[] 'Ungrouped '[t ::: subcolumns])
301+ -> (forall t. Condition 'Ungrouped '[] schemas '[] '[t ::: subcolumns])
302302 -- ^ a closed `Condition` on those subcolumns
303303 -> TableConstraintExpression sch tab schemas ('Check aliases)
304304check _cols condition = UnsafeTableConstraintExpression $
@@ -759,7 +759,7 @@ newtype AlterColumn (schemas :: SchemasType) (ty0 :: ColumnType) (ty1 :: ColumnT
759759-- :}
760760-- ALTER TABLE "tab" ALTER COLUMN "col" SET DEFAULT 5;
761761setDefault
762− :: Expression '[] schemas '[] 'Ungrouped '[] ty -- ^ default value to set
762+ :: Expression 'Ungrouped '[] schemas '[] '[] ty -- ^ default value to set
763763 -> AlterColumn schemas (constraint :=> ty) ('Def :=> ty)
764764setDefault expression = UnsafeAlterColumn $
765765 "SET DEFAULT" <+> renderExpression expression
@@ -1010,7 +1010,7 @@ notNullable ty = UnsafeColumnTypeExpression $ renderSQL ty <+> "NOT NULL"
10101010
10111011-- | used in `createTable` commands as a column constraint to give a default
10121012default_
1013− :: Expression '[] schemas '[] 'Ungrouped '[] ty
1013+ :: Expression 'Ungrouped '[] schemas '[] '[] ty
10141014 -> ColumnTypeExpression schemas ('NoDef :=> ty)
10151015 -> ColumnTypeExpression schemas ('Def :=> ty)
10161016default_ x ty = UnsafeColumnTypeExpression $
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+897−497
@@ -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])
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+36−36
@@ -263,13 +263,13 @@ insertInto_ tab qry =
263263data QueryClause commons schemas params columns where
264264 Values
265265 :: SOP.SListI columns
266− => NP (ColumnExpression schemas params 'Ungrouped '[]) columns
267− -> [NP (ColumnExpression schemas params 'Ungrouped '[]) columns]
266+ => NP (ColumnExpression 'Ungrouped schemas params '[]) columns
267+ -> [NP (ColumnExpression 'Ungrouped schemas params '[]) columns]
268268 -> QueryClause commons schemas params columns
269269 Select
270270 :: SOP.SListI columns
271− => NP (ColumnExpression schemas params grp from) columns
272− -> TableExpression commons schemas params grp from
271+ => NP (ColumnExpression grp schemas params from) columns
272+ -> TableExpression grp commons schemas params from
273273 -> QueryClause commons schemas params columns
274274 Subquery
275275 :: ColumnsToRow columns ~ row
@@ -292,7 +292,7 @@ instance RenderSQL (QueryClause commons schemas params columns) where
292292 Subquery qry -> renderQuery qry
293293 where
294294 renderSQLPartMaybe, renderValuePartMaybe
295− :: ColumnExpression schemas params grp from column -> Maybe ByteString
295+ :: ColumnExpression grp schemas params from column -> Maybe ByteString
296296 renderSQLPartMaybe = \case
297297 DefaultAs _ -> Nothing
298298 Specific (_ `As` name) -> Just $ renderSQL name
@@ -300,7 +300,7 @@ instance RenderSQL (QueryClause commons schemas params columns) where
300300 DefaultAs _ -> Nothing
301301 Specific (value `As` _) -> Just $ renderExpression value
302302 renderSQLPart, renderValuePart
303− :: ColumnExpression schemas params grp from column -> ByteString
303+ :: ColumnExpression grp schemas params from column -> ByteString
304304 renderSQLPart = \case
305305 DefaultAs name -> renderSQL name
306306 Specific (_ `As` name) -> renderSQL name
@@ -310,49 +310,49 @@ instance RenderSQL (QueryClause commons schemas params columns) where
310310
311311pattern Values_
312312 :: SOP.SListI columns
313− => NP (ColumnExpression schemas params 'Ungrouped '[]) columns
313+ => NP (ColumnExpression 'Ungrouped schemas params '[]) columns
314314 -> QueryClause commons schemas params columns
315315pattern Values_ vals = Values vals []
316316
317−data ColumnExpression schemas params grp from column where
317+data ColumnExpression grp schemas params from column where
318318 DefaultAs
319319 :: KnownSymbol col
320320 => Alias col
321− -> ColumnExpression schemas params grp from (col ::: 'Def :=> ty)
321+ -> ColumnExpression grp schemas params from (col ::: 'Def :=> ty)
322322 Specific
323− :: Aliased (Expression '[] schemas params grp from) (col ::: ty)
324− -> ColumnExpression schemas params grp from (col ::: defness :=> ty)
323+ :: Aliased (Expression grp '[] schemas params from) (col ::: ty)
324+ -> ColumnExpression grp schemas params from (col ::: defness :=> ty)
325325
326326instance (KnownSymbol col, column ~ (col ::: defness :=> ty))
327327 => Aliasable col
328− (Expression '[] schemas params grp from ty)
329− (ColumnExpression schemas params grp from column) where
328+ (Expression grp '[] schemas params from ty)
329+ (ColumnExpression grp schemas params from column) where
330330 expression `as` col = Specific (expression `As` col)
331331instance (KnownSymbol col, columns ~ '[col ::: defness :=> ty])
332332 => Aliasable col
333− (Expression '[] schemas params grp from ty)
334− (NP (ColumnExpression schemas params grp from) columns) where
333+ (Expression grp '[] schemas params from ty)
334+ (NP (ColumnExpression grp schemas params from) columns) where
335335 expression `as` col = expression `as` col :* Nil
336336instance (KnownSymbol col, column ~ (col ::: 'Def :=> ty))
337− => DefaultAliasable col (ColumnExpression schemas params grp from column) where
337+ => DefaultAliasable col (ColumnExpression grp schemas params from column) where
338338 defaultAs = DefaultAs
339339instance (KnownSymbol col, columns ~ '[col ::: 'Def :=> ty])
340− => DefaultAliasable col (NP (ColumnExpression schemas params grp from) columns) where
340+ => DefaultAliasable col (NP (ColumnExpression grp schemas params from) columns) where
341341 defaultAs col = defaultAs col :* Nil
342342
343343instance (HasUnique tab from row, Has col row ty, column ~ (col ::: defness :=> ty))
344− => IsLabel col (ColumnExpression schemas params 'Ungrouped from column) where
344+ => IsLabel col (ColumnExpression 'Ungrouped schemas params from column) where
345345 fromLabel = fromLabel @col `as` Alias
346346instance (HasUnique tab from row, Has col row ty, columns ~ '[col ::: defness :=> ty])
347347 => IsLabel col
348− (NP (ColumnExpression schemas params 'Ungrouped from) columns) where
348+ (NP (ColumnExpression 'Ungrouped schemas params from) columns) where
349349 fromLabel = fromLabel @col `as` Alias
350350
351351instance (Has tab from row, Has col row ty, column ~ (col ::: defness :=> ty))
352− => IsQualified tab col (ColumnExpression schemas params 'Ungrouped from column) where
352+ => IsQualified tab col (ColumnExpression 'Ungrouped schemas params from column) where
353353 tab ! col = tab ! col `as` col
354354instance (Has tab from row, Has col row ty, columns ~ '[col ::: defness :=> ty])
355− => IsQualified tab col (NP (ColumnExpression schemas params 'Ungrouped from) columns) where
355+ => IsQualified tab col (NP (ColumnExpression 'Ungrouped schemas params from) columns) where
356356 tab ! col = tab ! col `as` col
357357
358358instance
@@ -361,7 +361,7 @@ instance
361361 , GroupedBy tab col bys
362362 , column ~ (col ::: defness :=> ty)
363363 ) => IsLabel col
364− (ColumnExpression schemas params ('Grouped bys) from column) where
364+ (ColumnExpression ('Grouped bys) schemas params from column) where
365365 fromLabel = fromLabel @col `as` Alias
366366instance
367367 ( HasUnique tab from row
@@ -369,7 +369,7 @@ instance
369369 , GroupedBy tab col bys
370370 , columns ~ '[col ::: defness :=> ty]
371371 ) => IsLabel col
372− (NP (ColumnExpression schemas params ('Grouped bys) from) columns) where
372+ (NP (ColumnExpression ('Grouped bys) schemas params from) columns) where
373373 fromLabel = fromLabel @col `as` Alias
374374
375375instance
@@ -378,7 +378,7 @@ instance
378378 , GroupedBy tab col bys
379379 , column ~ (col ::: defness :=> ty)
380380 ) => IsQualified tab col
381− (ColumnExpression schemas params ('Grouped bys) from column) where
381+ (ColumnExpression ('Grouped bys) schemas params from column) where
382382 tab ! col = tab ! col `as` col
383383instance
384384 ( Has tab from row
@@ -386,10 +386,10 @@ instance
386386 , GroupedBy tab col bys
387387 , columns ~ '[col ::: defness :=> ty]
388388 ) => IsQualified tab col
389− (NP (ColumnExpression schemas params ('Grouped bys) from) columns) where
389+ (NP (ColumnExpression ('Grouped bys) schemas params from) columns) where
390390 tab ! col = tab ! col :* Nil
391391
392−instance RenderSQL (ColumnExpression schemas params grp from column) where
392+instance RenderSQL (ColumnExpression grp schemas params from column) where
393393 renderSQL = \case
394394 DefaultAs col ->
395395 renderSQL col <+> "=" <+> "DEFAULT"
@@ -407,7 +407,7 @@ instance RenderSQL (ColumnExpression schemas params grp from column) where
407407-- in the row. Use @Returning Nil@ in the common case where no return
408408-- values are desired.
409409newtype ReturningClause commons schemas params from row =
410− Returning (Selection commons schemas params 'Ungrouped from row)
410+ Returning (Selection 'Ungrouped commons schemas params from row)
411411
412412instance RenderSQL (ReturningClause commons schemas params from row) where
413413 renderSQL = \case
@@ -416,7 +416,7 @@ instance RenderSQL (ReturningClause commons schemas params from row) where
416416
417417pattern Returning_
418418 :: SOP.SListI row
419− => NP (Aliased (Expression commons schemas params 'Ungrouped from)) row
419+ => NP (Aliased (Expression 'Ungrouped commons schemas params from)) row
420420 -> ReturningClause commons schemas params from row
421421pattern Returning_ list = Returning (List list)
422422
@@ -448,8 +448,8 @@ data ConflictAction tab commons schemas params columns where
448448 , columns ~ (col0 ': cols)
449449 , SOP.All (HasIn columns) subcolumns
450450 , AllUnique subcolumns )
451− => NP (ColumnExpression schemas params 'Ungrouped '[tab ::: row, "excluded" ::: row]) subcolumns
452− -> [Condition commons schemas params 'Ungrouped '[tab ::: row, "excluded" ::: row]]
451+ => NP (ColumnExpression 'Ungrouped schemas params '[tab ::: row, "excluded" ::: row]) subcolumns
452+ -> [Condition 'Ungrouped commons schemas params '[tab ::: row, "excluded" ::: row]]
453453 -> ConflictAction tab commons schemas params columns
454454
455455instance RenderSQL (ConflictAction tab commons schemas params columns) where
@@ -492,9 +492,9 @@ update
492492 , SOP.All (HasIn columns) subcolumns
493493 , AllUnique subcolumns )
494494 => QualifiedAlias sch tab -- ^ table to update
495− -> NP (ColumnExpression schemas params 'Ungrouped '[tab ::: row0]) subcolumns
495+ -> NP (ColumnExpression 'Ungrouped schemas params '[tab ::: row0]) subcolumns
496496 -- ^ modified values to replace old values
497− -> Condition commons schemas params 'Ungrouped '[tab ::: row0]
497+ -> Condition 'Ungrouped commons schemas params '[tab ::: row0]
498498 -- ^ condition under which to perform update on a row
499499 -> ReturningClause commons schemas params '[tab ::: row0] row1 -- ^ results to return
500500 -> Manipulation commons schemas params row1
@@ -517,9 +517,9 @@ update_
517517 , SOP.All (HasIn columns) subcolumns
518518 , AllUnique subcolumns )
519519 => QualifiedAlias sch tab -- ^ table to update
520− -> NP (ColumnExpression schemas params 'Ungrouped '[tab ::: row]) subcolumns
520+ -> NP (ColumnExpression 'Ungrouped schemas params '[tab ::: row]) subcolumns
521521 -- ^ modified values to replace old values
522− -> Condition commons schemas params 'Ungrouped '[tab ::: row]
522+ -> Condition 'Ungrouped commons schemas params '[tab ::: row]
523523 -- ^ condition under which to perform update on a row
524524 -> Manipulation commons schemas params '[]
525525update_ tab columns wh = update tab columns wh (Returning_ Nil)
@@ -544,7 +544,7 @@ deleteFrom
544544 , columns ~ TableToColumns table )
545545 => QualifiedAlias sch tab -- ^ table to delete from
546546 -> UsingClause commons schemas params from
547− -> Condition commons schemas params 'Ungrouped (tab ::: row0 ': from)
547+ -> Condition 'Ungrouped commons schemas params (tab ::: row0 ': from)
548548 -- ^ condition under which to delete a row
549549 -> ReturningClause commons schemas params '[tab ::: row0] row1 -- ^ results to return
550550 -> Manipulation commons schemas params row1
@@ -566,7 +566,7 @@ deleteFrom_
566566 , columns ~ TableToColumns table )
567567 => QualifiedAlias sch tab -- ^ table to delete from
568568 -> UsingClause commons schemas params from
569− -> Condition commons schemas params 'Ungrouped (tab ::: row ': from)
569+ -> Condition 'Ungrouped commons schemas params (tab ::: row ': from)
570570 -- ^ condition under which to delete a row
571571 -> Manipulation commons schemas params '[]
572572deleteFrom_ tab uses wh = deleteFrom tab uses wh (Returning_ Nil)
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+175−201
@@ -74,7 +74,6 @@ module Squeal.PostgreSQL.Query
7474 , where_
7575 , groupBy
7676 , having
77− , orderBy
7877 , limit
7978 , offset
8079 -- * From Clauses
@@ -92,8 +91,6 @@ module Squeal.PostgreSQL.Query
9291 , By (..)
9392 , GroupByClause (..)
9493 , HavingClause (..)
95− -- * Sorting
96− , SortExpression (..)
9794 -- * Subquery Expressions
9895 , in_
9996 , rowIn
@@ -203,13 +200,13 @@ aggregation query:
203200let
204201 query :: Query '[] (Public Schema) '[] '["sum" ::: 'NotNull 'PGint4, "col1" ::: 'NotNull 'PGint4 ]
205202 query =
206− select_ (sum_ #col2 `as` #sum :* #col1)
203+ select_ (sum_ (All #col2) `as` #sum :* #col1)
207204 ( from (table (#tab `as` #table1))
208205 & groupBy #col1
209− & having (#col1 + sum_ #col2 .> 1) )
206+ & having (#col1 + sum_ (Distinct #col2) .> 1) )
210207in printSQL query
211208:}
212−SELECT sum("col2") AS "sum", "col1" AS "col1" FROM "tab" AS "table1" GROUP BY "col1" HAVING (("col1" + sum("col2")) > 1)
209+SELECT sum(ALL "col2") AS "sum", "col1" AS "col1" FROM "tab" AS "table1" GROUP BY "col1" HAVING (("col1" + sum(DISTINCT "col2")) > 1)
213210
214211sorted query:
215212
@@ -313,6 +310,18 @@ let
313310in printSQL query
314311:}
315312WITH "cte1" AS (SELECT * FROM "tab" AS "tab"), "cte2" AS (SELECT * FROM "cte1" AS "cte1") SELECT * FROM "cte2" AS "cte2"
313+
314+window function queries
315+
316+>>> :{
317+let
318+ query :: Query '[] (Public Schema) '[] ["col1" ::: 'NotNull 'PGint4, "rank" ::: 'NotNull 'PGint8]
319+ query = select
320+ (#col1 & Also (rank `as` #rank `Over` (partitionBy #col1 & orderBy [#col2 & Asc])))
321+ (from (table #tab))
322+in printSQL query
323+:}
324+SELECT "col1" AS "col1", rank() OVER (PARTITION BY "col1" ORDER BY "col2" ASC) AS "rank" FROM "tab" AS "tab"
316325-}
317326newtype Query
318327 (commons :: FromType)
@@ -396,54 +405,69 @@ q1 `exceptAll` q2 = UnsafeQuery $
396405SELECT queries
397406-----------------------------------------}
398407
399−data Selection commons schemas params grp from row where
408+data Selection grp commons schemas params from row where
400409 List
401410 :: SListI row
402− => NP (Aliased (Expression commons schemas params grp from)) row
403− -> Selection commons schemas params grp from row
411+ => NP (Aliased (Expression grp commons schemas params from)) row
412+ -> Selection grp commons schemas params from row
404413 Star
405414 :: HasUnique tab from row
406− => Selection commons schemas params 'Ungrouped from row
415+ => Selection 'Ungrouped commons schemas params from row
407416 DotStar
408417 :: Has tab from row
409418 => Alias tab
410− -> Selection commons schemas params 'Ungrouped from row
419+ -> Selection 'Ungrouped commons schemas params from row
411420 Also
412− :: Selection commons schemas params grp from right
413− -> Selection commons schemas params grp from left
414− -> Selection commons schemas params grp from (Join left right)
421+ :: Selection grp commons schemas params from right
422+ -> Selection grp commons schemas params from left
423+ -> Selection grp commons schemas params from (Join left right)
424+ Over
425+ :: SListI row
426+ => NP (Aliased (WindowFunction grp commons schemas params from)) row
427+ -> WindowDefinition grp commons schemas params from
428+ -> Selection grp commons schemas params from row
415429instance (KnownSymbol col, row ~ '[col ::: ty])
416430 => Aliasable col
417− (Expression commons schemas params grp from ty)
418− (Selection commons schemas params grp from row) where
431+ (Expression grp commons schemas params from ty)
432+ (Selection grp commons schemas params from row) where
419433 expr `as` col = List (expr `as` col)
420434instance (Has tab from row0, Has col row0 ty, row1 ~ '[col ::: ty])
421− => IsQualified tab col (Selection commons schemas params 'Ungrouped from row1) where
435+ => IsQualified tab col (Selection 'Ungrouped commons schemas params from row1) where
422436 tab ! col = tab ! col `as` col
423437instance
424438 ( Has tab from row0
425439 , Has col row0 ty
426440 , row1 ~ '[col ::: ty]
427441 , GroupedBy tab col bys )
428− => IsQualified tab col (Selection commons schemas params ('Grouped bys) from row1) where
442+ => IsQualified tab col (Selection ('Grouped bys) commons schemas params from row1) where
429443 tab ! col = tab ! col `as` col
430444instance (HasUnique tab from row0, Has col row0 ty, row1 ~ '[col ::: ty])
431− => IsLabel col (Selection commons schemas params 'Ungrouped from row1) where
445+ => IsLabel col (Selection 'Ungrouped commons schemas params from row1) where
432446 fromLabel = fromLabel @col `as` Alias
433447instance
434448 ( HasUnique tab from row0
435449 , Has col row0 ty
436450 , row1 ~ '[col ::: ty]
437451 , GroupedBy tab col bys )
438− => IsLabel col (Selection commons schemas params ('Grouped bys) from row1) where
452+ => IsLabel col (Selection ('Grouped bys) commons schemas params from row1) where
439453 fromLabel = fromLabel @col `as` Alias
440454
441−instance RenderSQL (Selection commons schemas params grp from row) where
455+instance RenderSQL (Selection grp commons schemas params from row) where
442456 renderSQL = \case
443457 List list -> renderCommaSeparated (renderAliased renderSQL) list
444458 Star -> "*"
445459 DotStar tab -> renderSQL tab <> ".*"
446460 Also right left -> renderSQL left <> ", " <> renderSQL right
461+ Over winFns winDef ->
462+ let
463+ renderOver
464+ :: Aliased (WindowFunction grp commons schemas params from) field
465+ -> ByteString
466+ renderOver (winFn `As` col) = renderSQL winFn
467+ <+> "OVER" <+> parenthesized (renderSQL winDef)
468+ <+> "AS" <+> renderSQL col
469+ in
470+ renderCommaSeparated renderOver winFns
447471
448472-- | the `TableExpression` in the `select` command constructs an intermediate
449473-- virtual table by possibly combining tables, views, eliminating rows,
@@ -452,9 +476,9 @@ instance RenderSQL (Selection commons schemas params grp from row) where
452476-- the intermediate table are actually output.
453477select
454478 :: (SListI row, row ~ (x ': xs))
455− => Selection commons schemas params grp from row
479+ => Selection grp commons schemas params from row
456480 -- ^ select list
457− -> TableExpression commons schemas params grp from
481+ -> TableExpression grp commons schemas params from
458482 -- ^ intermediate virtual table
459483 -> Query commons schemas params row
460484select selection tabexpr = UnsafeQuery $
@@ -464,8 +488,8 @@ select selection tabexpr = UnsafeQuery $
464488
465489select_
466490 :: (SListI row, row ~ (x ': xs))
467− => NP (Aliased (Expression commons schemas params grp from)) row
468− -> TableExpression commons schemas params grp from
491+ => NP (Aliased (Expression grp commons schemas params from)) row
492+ -> TableExpression grp commons schemas params from
469493 -> Query commons schemas params row
470494select_ list = select (List list)
471495
@@ -473,9 +497,9 @@ select_ list = select (List list)
473497-- be subject to the elimination of duplicate rows using `selectDistinct`.
474498selectDistinct
475499 :: (SListI columns, columns ~ (col ': cols))
476− => Selection commons schemas params 'Ungrouped from columns
500+ => Selection 'Ungrouped commons schemas params from columns
477501 -- ^ select list
478− -> TableExpression commons schemas params 'Ungrouped from
502+ -> TableExpression 'Ungrouped commons schemas params from
479503 -- ^ intermediate virtual table
480504 -> Query commons schemas params columns
481505selectDistinct selection tabexpr = UnsafeQuery $
@@ -485,9 +509,9 @@ selectDistinct selection tabexpr = UnsafeQuery $
485509
486510selectDistinct_
487511 :: (SListI columns, columns ~ (col ': cols))
488− => NP (Aliased (Expression commons schemas params 'Ungrouped from)) columns
512+ => NP (Aliased (Expression 'Ungrouped commons schemas params from)) columns
489513 -- ^ select list
490− -> TableExpression commons schemas params 'Ungrouped from
514+ -> TableExpression 'Ungrouped commons schemas params from
491515 -- ^ intermediate virtual table
492516 -> Query commons schemas params columns
493517selectDistinct_ list = select (List list)
@@ -503,8 +527,8 @@ selectDistinct_ list = select (List list)
503527-- SELECT * FROM (VALUES (1, E'one')) AS t ("a", "b")
504528values
505529 :: SListI cols
506− => NP (Aliased (Expression commons schemas params 'Ungrouped '[] )) cols
507− -> [NP (Aliased (Expression commons schemas params 'Ungrouped '[] )) cols]
530+ => NP (Aliased (Expression 'Ungrouped commons schemas params '[] )) cols
531+ -> [NP (Aliased (Expression 'Ungrouped commons schemas params '[] )) cols]
508532 -- ^ When more than one row is specified, all the rows must
509533 -- must have the same number of elements
510534 -> Query commons schemas params cols
@@ -518,7 +542,7 @@ values rw rws = UnsafeQuery $ "SELECT * FROM"
518542 <+> parenthesized (renderCommaSeparated renderAliasPart rw)
519543 where
520544 renderAliasPart, renderValuePart
521− :: Aliased (Expression commons schemas params 'Ungrouped '[] ) ty -> ByteString
545+ :: Aliased (Expression 'Ungrouped commons schemas params '[] ) ty -> ByteString
522546 renderAliasPart (_ `As` name) = renderSQL name
523547 renderValuePart (value `As` _) = renderSQL value
524548
@@ -526,7 +550,7 @@ values rw rws = UnsafeQuery $ "SELECT * FROM"
526550-- specified by value expressions.
527551values_
528552 :: SListI cols
529− => NP (Aliased (Expression commons schemas params 'Ungrouped '[] )) cols
553+ => NP (Aliased (Expression 'Ungrouped commons schemas params '[] )) cols
530554 -- ^ one row of values
531555 -> Query commons schemas params cols
532556values_ rw = values rw []
@@ -542,16 +566,16 @@ Table Expressions
542566-- to a table on disk, a so-called base table, but more complex expressions
543567-- can be used to modify or combine base tables in various ways.
544568data TableExpression
569+ (grp :: Grouping)
545570 (commons :: FromType)
546571 (schemas :: SchemasType)
547572 (params :: [NullityType])
548− (grp :: Grouping)
549573 (from :: FromType)
550574 = TableExpression
551575 { fromClause :: FromClause commons schemas params from
552576 -- ^ A table reference that can be a table name, or a derived table such
553577 -- as a subquery, a @JOIN@ construct, or complex combinations of these.
554− , whereClause :: [Condition commons schemas params 'Ungrouped from]
578+ , whereClause :: [Condition 'Ungrouped commons schemas params from]
555579 -- ^ optional search coditions, combined with `.&&`. After the processing
556580 -- of the `fromClause` is done, each row of the derived virtual table
557581 -- is checked against the search condition. If the result of the
@@ -567,13 +591,13 @@ data TableExpression
567591 -- set of rows having common values into one group row that represents all
568592 -- rows in the group. This is done to eliminate redundancy in the output
569593 -- and/or compute aggregates that apply to these groups.
570− , havingClause :: HavingClause commons schemas params grp from
594+ , havingClause :: HavingClause grp commons schemas params from
571595 -- ^ If a table has been grouped using `groupBy`, but only certain groups
572596 -- are of interest, the `havingClause` can be used, much like a
573597 -- `whereClause`, to eliminate groups from the result. Expressions in the
574598 -- `havingClause` can refer both to grouped expressions and to ungrouped
575599 -- expressions (which necessarily involve an aggregate function).
576− , orderByClause :: [SortExpression commons schemas params grp from]
600+ , orderByClause :: [SortExpression grp commons schemas params from]
577601 -- ^ The `orderByClause` is for optional sorting. When more than one
578602 -- `SortExpression` is specified, the later (right) values are used to sort
579603 -- rows that are equal according to the earlier (left) values.
@@ -590,7 +614,7 @@ data TableExpression
590614 }
591615
592616-- | Render a `TableExpression`
593−instance RenderSQL (TableExpression commons schemas params grp from) where
617+instance RenderSQL (TableExpression grp commons schemas params from) where
594618 renderSQL
595619 (TableExpression frm' whs' grps' hvs' srts' lims' offs') = mconcat
596620 [ "FROM ", renderSQL frm'
@@ -624,15 +648,15 @@ instance RenderSQL (TableExpression commons schemas params grp from) where
624648-- to match the left-to-right sequencing of their placement in SQL.
625649from
626650 :: FromClause commons schemas params from -- ^ table reference
627− -> TableExpression commons schemas params 'Ungrouped from
651+ -> TableExpression 'Ungrouped commons schemas params from
628652from rels = TableExpression rels [] NoGroups NoHaving [] [] []
629653
630654-- | A `where_` is an endomorphism of `TableExpression`s which adds a
631655-- search condition to the `whereClause`.
632656where_
633− :: Condition commons schemas params 'Ungrouped from -- ^ filtering condition
634− -> TableExpression commons schemas params grp from
635− -> TableExpression commons schemas params grp from
657+ :: Condition 'Ungrouped commons schemas params from -- ^ filtering condition
658+ -> TableExpression grp commons schemas params from
659+ -> TableExpression grp commons schemas params from
636660where_ wh rels = rels {whereClause = wh : whereClause rels}
637661
638662-- | A `groupBy` is a transformation of `TableExpression`s which switches
@@ -641,8 +665,8 @@ where_ wh rels = rels {whereClause = wh : whereClause rels}
641665groupBy
642666 :: SListI bys
643667 => NP (By from) bys -- ^ grouped columns
644− -> TableExpression commons schemas params 'Ungrouped from
645− -> TableExpression commons schemas params ('Grouped bys) from
668+ -> TableExpression 'Ungrouped commons schemas params from
669+ -> TableExpression ('Grouped bys) commons schemas params from
646670groupBy bys rels = TableExpression
647671 { fromClause = fromClause rels
648672 , whereClause = whereClause rels
@@ -656,34 +680,29 @@ groupBy bys rels = TableExpression
656680-- | A `having` is an endomorphism of `TableExpression`s which adds a
657681-- search condition to the `havingClause`.
658682having
659− :: Condition commons schemas params ('Grouped bys) from -- ^ having condition
660− -> TableExpression commons schemas params ('Grouped bys) from
661− -> TableExpression commons schemas params ('Grouped bys) from
683+ :: Condition ('Grouped bys) commons schemas params from -- ^ having condition
684+ -> TableExpression ('Grouped bys) commons schemas params from
685+ -> TableExpression ('Grouped bys) commons schemas params from
662686having hv rels = rels
663687 { havingClause = case havingClause rels of Having hvs -> Having (hv:hvs) }
664688
665−-- | An `orderBy` is an endomorphism of `TableExpression`s which appends an
666−-- ordering to the right of the `orderByClause`.
667−orderBy
668− :: [SortExpression commons schemas params grp from] -- ^ sort expressions
669− -> TableExpression commons schemas params grp from
670− -> TableExpression commons schemas params grp from
671−orderBy srts rels = rels {orderByClause = orderByClause rels ++ srts}
689+instance OrderBy TableExpression where
690+ orderBy srts rels = rels {orderByClause = orderByClause rels ++ srts}
672691
673692-- | A `limit` is an endomorphism of `TableExpression`s which adds to the
674693-- `limitClause`.
675694limit
676695 :: Word64 -- ^ limit parameter
677− -> TableExpression commons schemas params grp from
678− -> TableExpression commons schemas params grp from
696+ -> TableExpression grp commons schemas params from
697+ -> TableExpression grp commons schemas params from
679698limit lim rels = rels {limitClause = lim : limitClause rels}
680699
681700-- | An `offset` is an endomorphism of `TableExpression`s which adds to the
682701-- `offsetClause`.
683702offset
684703 :: Word64 -- ^ offset parameter
685− -> TableExpression commons schemas params grp from
686− -> TableExpression commons schemas params grp from
704+ -> TableExpression grp commons schemas params from
705+ -> TableExpression grp commons schemas params from
687706offset off rels = rels {offsetClause = off : offsetClause rels}
688707
689708{-----------------------------------------
@@ -692,55 +711,55 @@ JSON stuff
692711
693712unsafeSetOfFunction
694713 :: ByteString
695− -> Expression commons schemas params 'Ungrouped '[] ty
714+ -> Expression 'Ungrouped commons schemas params '[] ty
696715 -> Query commons schemas params row
697716unsafeSetOfFunction fun expr = UnsafeQuery $
698717 "SELECT * FROM " <> fun <> "(" <> renderSQL expr <> ")"
699718
700719-- | Expands the outermost JSON object into a set of key/value pairs.
701720jsonEach
702− :: Expression commons schemas params 'Ungrouped '[] (nullity 'PGjson) -- ^ json object
721+ :: Expression 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
703722 -> Query commons schemas params
704723 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]
705724jsonEach = unsafeSetOfFunction "json_each"
706725
707726-- | Expands the outermost binary JSON object into a set of key/value pairs.
708727jsonbEach
709− :: Expression commons schemas params 'Ungrouped '[] (nullity 'PGjsonb) -- ^ jsonb object
728+ :: Expression 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
710729 -> Query commons schemas params
711730 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjsonb]
712731jsonbEach = unsafeSetOfFunction "jsonb_each"
713732
714733-- | Expands the outermost JSON object into a set of key/value pairs.
715734jsonEachAsText
716− :: Expression commons schemas params 'Ungrouped '[] (nullity 'PGjson) -- ^ json object
735+ :: Expression 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
717736 -> Query commons schemas params
718737 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]
719738jsonEachAsText = unsafeSetOfFunction "json_each_text"
720739
721740-- | Expands the outermost binary JSON object into a set of key/value pairs.
722741jsonbEachAsText
723− :: Expression commons schemas params 'Ungrouped '[] (nullity 'PGjsonb) -- ^ jsonb object
742+ :: Expression 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
724743 -> Query commons schemas params
725744 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]
726745jsonbEachAsText = unsafeSetOfFunction "jsonb_each_text"
727746
728747-- | Returns set of keys in the outermost JSON object.
729748jsonObjectKeys
730− :: Expression commons schemas params 'Ungrouped '[] (nullity 'PGjson) -- ^ json object
749+ :: Expression 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
731750 -> Query commons schemas params '["json_object_keys" ::: 'NotNull 'PGtext]
732751jsonObjectKeys = unsafeSetOfFunction "json_object_keys"
733752
734753-- | Returns set of keys in the outermost JSON object.
735754jsonbObjectKeys
736− :: Expression commons schemas params 'Ungrouped '[] (nullity 'PGjsonb) -- ^ jsonb object
755+ :: Expression 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
737756 -> Query commons schemas params '["jsonb_object_keys" ::: 'NotNull 'PGtext]
738757jsonbObjectKeys = unsafeSetOfFunction "jsonb_object_keys"
739758
740759unsafePopulateFunction
741760 :: ByteString
742761 -> TypeExpression schemas (nullity ('PGcomposite row))
743− -> Expression commons schemas params 'Ungrouped '[] ty
762+ -> Expression 'Ungrouped commons schemas params '[] ty
744763 -> Query commons schemas params row
745764unsafePopulateFunction fun ty expr = UnsafeQuery $
746765 "SELECT * FROM " <> fun <> "("
@@ -751,7 +770,7 @@ unsafePopulateFunction fun ty expr = UnsafeQuery $
751770-- type defined by the given table.
752771jsonPopulateRecord
753772 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
754− -> Expression commons schemas params 'Ungrouped '[] (nullity 'PGjson) -- ^ json object
773+ -> Expression 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
755774 -> Query commons schemas params row
756775jsonPopulateRecord = unsafePopulateFunction "json_populate_record"
757776
@@ -759,7 +778,7 @@ jsonPopulateRecord = unsafePopulateFunction "json_populate_record"
759778-- type defined by the given table.
760779jsonbPopulateRecord
761780 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
762− -> Expression commons schemas params 'Ungrouped '[] (nullity 'PGjsonb) -- ^ jsonb object
781+ -> Expression 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
763782 -> Query commons schemas params row
764783jsonbPopulateRecord = unsafePopulateFunction "jsonb_populate_record"
765784
@@ -767,7 +786,7 @@ jsonbPopulateRecord = unsafePopulateFunction "jsonb_populate_record"
767786-- set of rows whose columns match the record type defined by the given table.
768787jsonPopulateRecordSet
769788 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
770− -> Expression commons schemas params 'Ungrouped '[] (nullity 'PGjson) -- ^ json array
789+ -> Expression 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json array
771790 -> Query commons schemas params row
772791jsonPopulateRecordSet = unsafePopulateFunction "json_populate_record_set"
773792
@@ -776,14 +795,14 @@ jsonPopulateRecordSet = unsafePopulateFunction "json_populate_record_set"
776795-- table.
777796jsonbPopulateRecordSet
778797 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
779− -> Expression commons schemas params 'Ungrouped '[] (nullity 'PGjsonb) -- ^ jsonb array
798+ -> Expression 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb array
780799 -> Query commons schemas params row
781800jsonbPopulateRecordSet = unsafePopulateFunction "jsonb_populate_record_set"
782801
783802unsafeRecordFunction
784803 :: (SListI record, json `In` PGJsonType)
785804 => ByteString
786− -> Expression commons schemas params 'Ungrouped '[] (nullity json)
805+ -> Expression 'Ungrouped commons schemas params '[] (nullity json)
787806 -> NP (Aliased (TypeExpression schemas)) record
788807 -> Query commons schemas params record
789808unsafeRecordFunction fun expr types = UnsafeQuery $
@@ -798,7 +817,7 @@ unsafeRecordFunction fun expr types = UnsafeQuery $
798817-- | Builds an arbitrary record from a JSON object.
799818jsonToRecord
800819 :: SListI record
801− => Expression commons schemas params 'Ungrouped '[] (nullity 'PGjson) -- ^ json object
820+ => Expression 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
802821 -> NP (Aliased (TypeExpression schemas)) record -- ^ record types
803822 -> Query commons schemas params record
804823jsonToRecord = unsafeRecordFunction "json_to_record"
@@ -806,7 +825,7 @@ jsonToRecord = unsafeRecordFunction "json_to_record"
806825-- | Builds an arbitrary record from a binary JSON object.
807826jsonbToRecord
808827 :: SListI record
809− => Expression commons schemas params 'Ungrouped '[] (nullity 'PGjsonb) -- ^ jsonb object
828+ => Expression 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
810829 -> NP (Aliased (TypeExpression schemas)) record -- ^ record types
811830 -> Query commons schemas params record
812831jsonbToRecord = unsafeRecordFunction "jsonb_to_record"
@@ -814,7 +833,7 @@ jsonbToRecord = unsafeRecordFunction "jsonb_to_record"
814833-- | Builds an arbitrary set of records from a JSON array of objects.
815834jsonToRecordSet
816835 :: SListI record
817− => Expression commons schemas params 'Ungrouped '[] (nullity 'PGjson) -- ^ json array
836+ => Expression 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json array
818837 -> NP (Aliased (TypeExpression schemas)) record -- ^ record types
819838 -> Query commons schemas params record
820839jsonToRecordSet = unsafeRecordFunction "json_to_record_set"
@@ -822,7 +841,7 @@ jsonToRecordSet = unsafeRecordFunction "json_to_record_set"
822841-- | Builds an arbitrary set of records from a binary JSON array of objects.
823842jsonbToRecordSet
824843 :: SListI record
825− => Expression commons schemas params 'Ungrouped '[] (nullity 'PGjsonb) -- ^ jsonb array
844+ => Expression 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb array
826845 -> NP (Aliased (TypeExpression schemas)) record -- ^ record types
827846 -> Query commons schemas params record
828847jsonbToRecordSet = unsafeRecordFunction "jsonb_to_record_set"
@@ -891,7 +910,7 @@ the @on@ condition.
891910innerJoin
892911 :: FromClause commons schemas params right
893912 -- ^ right
894− -> Condition commons schemas params 'Ungrouped (Join left right)
913+ -> Condition 'Ungrouped commons schemas params (Join left right)
895914 -- ^ @on@ condition
896915 -> FromClause commons schemas params left
897916 -- ^ left
@@ -908,7 +927,7 @@ innerJoin right on left = UnsafeFromClause $
908927leftOuterJoin
909928 :: FromClause commons schemas params right
910929 -- ^ right
911− -> Condition commons schemas params 'Ungrouped (Join left right)
930+ -> Condition 'Ungrouped commons schemas params (Join left right)
912931 -- ^ @on@ condition
913932 -> FromClause commons schemas params left
914933 -- ^ left
@@ -926,7 +945,7 @@ leftOuterJoin right on left = UnsafeFromClause $
926945rightOuterJoin
927946 :: FromClause commons schemas params right
928947 -- ^ right
929− -> Condition commons schemas params 'Ungrouped (Join left right)
948+ -> Condition 'Ungrouped commons schemas params (Join left right)
930949 -- ^ @on@ condition
931950 -> FromClause commons schemas params left
932951 -- ^ left
@@ -945,7 +964,7 @@ rightOuterJoin right on left = UnsafeFromClause $
945964fullOuterJoin
946965 :: FromClause commons schemas params right
947966 -- ^ right
948− -> Condition commons schemas params 'Ungrouped (Join left right)
967+ -> Condition 'Ungrouped commons schemas params (Join left right)
949968 -- ^ @on@ condition
950969 -> FromClause commons schemas params left
951970 -- ^ left
@@ -1018,82 +1037,37 @@ instance RenderSQL (GroupByClause grp from) where
10181037-- An `Ungrouped` `TableExpression` may only use `NoHaving` while a `Grouped`
10191038-- `TableExpression` must use `Having` whose conditions are combined with
10201039-- `.&&`.
1021−data HavingClause commons schemas params grp from where
1022− NoHaving :: HavingClause commons schemas params 'Ungrouped from
1040+data HavingClause grp commons schemas params from where
1041+ NoHaving :: HavingClause 'Ungrouped commons schemas params from
10231042 Having
1024− :: [Condition commons schemas params ('Grouped bys) from]
1025− -> HavingClause commons schemas params ('Grouped bys) from
1026−deriving instance Show (HavingClause commons schemas params grp from)
1027−deriving instance Eq (HavingClause commons schemas params grp from)
1028−deriving instance Ord (HavingClause commons schemas params grp from)
1043+ :: [Condition ('Grouped bys) commons schemas params from]
1044+ -> HavingClause ('Grouped bys) commons schemas params from
1045+deriving instance Show (HavingClause grp commons schemas params from)
1046+deriving instance Eq (HavingClause grp commons schemas params from)
1047+deriving instance Ord (HavingClause grp commons schemas params from)
10291048
10301049-- | Render a `HavingClause`.
1031−instance RenderSQL (HavingClause commons schemas params grp from) where
1050+instance RenderSQL (HavingClause grp commons schemas params from) where
10321051 renderSQL = \case
10331052 NoHaving -> ""
10341053 Having [] -> ""
10351054 Having conditions ->
10361055 " HAVING" <+> commaSeparated (renderSQL <$> conditions)
10371056
1038−{-----------------------------------------
1039−Sorting
1040−-----------------------------------------}
1041−
1042−-- | `SortExpression`s are used by `sortBy` to optionally sort the results
1043−-- of a `Query`. `Asc` or `Desc` set the sort direction of a `NotNull` result
1044−-- column to ascending or descending. Ascending order puts smaller values
1045−-- first, where "smaller" is defined in terms of the `.<` operator. Similarly,
1046−-- descending order is determined with the `.>` operator. `AscNullsFirst`,
1047−-- `AscNullsLast`, `DescNullsFirst` and `DescNullsLast` options are used to
1048−-- determine whether nulls appear before or after non-null values in the sort
1049−-- ordering of a `Null` result column.
1050−data SortExpression commons schemas params grp from where
1051− Asc
1052− :: Expression commons schemas params grp from ('NotNull ty)
1053− -> SortExpression commons schemas params grp from
1054− Desc
1055− :: Expression commons schemas params grp from ('NotNull ty)
1056− -> SortExpression commons schemas params grp from
1057− AscNullsFirst
1058− :: Expression commons schemas params grp from ('Null ty)
1059− -> SortExpression commons schemas params grp from
1060− AscNullsLast
1061− :: Expression commons schemas params grp from ('Null ty)
1062− -> SortExpression commons schemas params grp from
1063− DescNullsFirst
1064− :: Expression commons schemas params grp from ('Null ty)
1065− -> SortExpression commons schemas params grp from
1066− DescNullsLast
1067− :: Expression commons schemas params grp from ('Null ty)
1068− -> SortExpression commons schemas params grp from
1069−deriving instance Show (SortExpression commons schemas params grp from)
1070−
1071−-- | Render a `SortExpression`.
1072−instance RenderSQL (SortExpression commons schemas params grp from) where
1073− renderSQL = \case
1074− Asc expression -> renderSQL expression <+> "ASC"
1075− Desc expression -> renderSQL expression <+> "DESC"
1076− AscNullsFirst expression -> renderSQL expression
1077− <+> "ASC NULLS FIRST"
1078− DescNullsFirst expression -> renderSQL expression
1079− <+> "DESC NULLS FIRST"
1080− AscNullsLast expression -> renderSQL expression <+> "ASC NULLS LAST"
1081− DescNullsLast expression -> renderSQL expression <+> "DESC NULLS LAST"
1082−
10831057unsafeSubqueryExpression
10841058 :: ByteString
1085− -> Expression commons schemas params grp from ty
1059+ -> Expression grp commons schemas params from ty
10861060 -> Query commons schemas params '[alias ::: ty]
1087− -> Expression commons schemas params grp from (nullity 'PGbool)
1061+ -> Expression grp commons schemas params from (nullity 'PGbool)
10881062unsafeSubqueryExpression op x q = UnsafeExpression $
10891063 renderSQL x <+> op <+> parenthesized (renderSQL q)
10901064
10911065unsafeRowSubqueryExpression
10921066 :: SListI row
10931067 => ByteString
1094− -> NP (Aliased (Expression commons schemas params grp from)) row
1068+ -> NP (Aliased (Expression grp commons schemas params from)) row
10951069 -> Query commons schemas params row
1096− -> Expression commons schemas params grp from (nullity 'PGbool)
1070+ -> Expression grp commons schemas params from (nullity 'PGbool)
10971071unsafeRowSubqueryExpression op xs q = UnsafeExpression $
10981072 renderSQL (row xs) <+> op <+> parenthesized (renderSQL q)
10991073
@@ -1106,9 +1080,9 @@ unsafeRowSubqueryExpression op xs q = UnsafeExpression $
11061080-- >>> printSQL $ true `in_` values_ (true `as` #foo)
11071081-- TRUE IN (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
11081082in_
1109− :: Expression commons schemas params grp from ty -- ^ expression
1083+ :: Expression grp commons schemas params from ty -- ^ expression
11101084 -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1111− -> Expression commons schemas params grp from (nullity 'PGbool)
1085+ -> Expression grp commons schemas params from (nullity 'PGbool)
11121086in_ = unsafeSubqueryExpression "IN"
11131087
11141088{- | The left-hand side of this form of `rowIn` is a row constructor.
@@ -1121,231 +1095,231 @@ is `true` if any equal subquery row is found.
11211095The result is `false` if no equal row is found
11221096(including the case where the subquery returns no rows).
11231097
1124−>>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1098+>>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression grp commons schemas params from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
11251099>>> printSQL $ myRow `rowIn` values_ myRow
11261100ROW(1, FALSE) IN (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
11271101-}
11281102rowIn
11291103 :: SListI row
1130− => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1104+ => NP (Aliased (Expression grp commons schemas params from)) row -- ^ row constructor
11311105 -> Query commons schemas params row -- ^ subquery
1132− -> Expression commons schemas params grp from (nullity 'PGbool)
1106+ -> Expression grp commons schemas params from (nullity 'PGbool)
11331107rowIn = unsafeRowSubqueryExpression "IN"
11341108
11351109-- | >>> printSQL $ true `eqAll` values_ (true `as` #foo)
11361110-- TRUE = ALL (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
11371111eqAll
1138− :: Expression commons schemas params grp from ty -- ^ expression
1112+ :: Expression grp commons schemas params from ty -- ^ expression
11391113 -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1140− -> Expression commons schemas params grp from (nullity 'PGbool)
1114+ -> Expression grp commons schemas params from (nullity 'PGbool)
11411115eqAll = unsafeSubqueryExpression "= ALL"
11421116
1143−-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1117+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression grp commons schemas params from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
11441118-- >>> printSQL $ myRow `rowEqAll` values_ myRow
11451119-- ROW(1, FALSE) = ALL (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
11461120rowEqAll
11471121 :: SListI row
1148− => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1122+ => NP (Aliased (Expression grp commons schemas params from)) row -- ^ row constructor
11491123 -> Query commons schemas params row -- ^ subquery
1150− -> Expression commons schemas params grp from (nullity 'PGbool)
1124+ -> Expression grp commons schemas params from (nullity 'PGbool)
11511125rowEqAll = unsafeRowSubqueryExpression "= ALL"
11521126
11531127-- | >>> printSQL $ true `eqAny` values_ (true `as` #foo)
11541128-- TRUE = ANY (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
11551129eqAny
1156− :: Expression commons schemas params grp from ty -- ^ expression
1130+ :: Expression grp commons schemas params from ty -- ^ expression
11571131 -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1158− -> Expression commons schemas params grp from (nullity 'PGbool)
1132+ -> Expression grp commons schemas params from (nullity 'PGbool)
11591133eqAny = unsafeSubqueryExpression "= ANY"
11601134
1161−-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1135+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression grp commons schemas params from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
11621136-- >>> printSQL $ myRow `rowEqAny` values_ myRow
11631137-- ROW(1, FALSE) = ANY (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
11641138rowEqAny
11651139 :: SListI row
1166− => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1140+ => NP (Aliased (Expression grp commons schemas params from)) row -- ^ row constructor
11671141 -> Query commons schemas params row -- ^ subquery
1168− -> Expression commons schemas params grp from (nullity 'PGbool)
1142+ -> Expression grp commons schemas params from (nullity 'PGbool)
11691143rowEqAny = unsafeRowSubqueryExpression "= ANY"
11701144
11711145-- | >>> printSQL $ true `neqAll` values_ (true `as` #foo)
11721146-- TRUE <> ALL (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
11731147neqAll
1174− :: Expression commons schemas params grp from ty -- ^ expression
1148+ :: Expression grp commons schemas params from ty -- ^ expression
11751149 -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1176− -> Expression commons schemas params grp from (nullity 'PGbool)
1150+ -> Expression grp commons schemas params from (nullity 'PGbool)
11771151neqAll = unsafeSubqueryExpression "<> ALL"
11781152
1179−-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1153+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression grp commons schemas params from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
11801154-- >>> printSQL $ myRow `rowNeqAll` values_ myRow
11811155-- ROW(1, FALSE) <> ALL (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
11821156rowNeqAll
11831157 :: SListI row
1184− => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1158+ => NP (Aliased (Expression grp commons schemas params from)) row -- ^ row constructor
11851159 -> Query commons schemas params row -- ^ subquery
1186− -> Expression commons schemas params grp from (nullity 'PGbool)
1160+ -> Expression grp commons schemas params from (nullity 'PGbool)
11871161rowNeqAll = unsafeRowSubqueryExpression "<> ALL"
11881162
11891163-- | >>> printSQL $ true `neqAny` values_ (true `as` #foo)
11901164-- TRUE <> ANY (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
11911165neqAny
1192− :: Expression commons schemas params grp from ty -- ^ expression
1166+ :: Expression grp commons schemas params from ty -- ^ expression
11931167 -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1194− -> Expression commons schemas params grp from (nullity 'PGbool)
1168+ -> Expression grp commons schemas params from (nullity 'PGbool)
11951169neqAny = unsafeSubqueryExpression "<> ANY"
11961170
1197−-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1171+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression grp commons schemas params from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
11981172-- >>> printSQL $ myRow `rowNeqAny` values_ myRow
11991173-- ROW(1, FALSE) <> ANY (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
12001174rowNeqAny
12011175 :: SListI row
1202− => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1176+ => NP (Aliased (Expression grp commons schemas params from)) row -- ^ row constructor
12031177 -> Query commons schemas params row -- ^ subquery
1204− -> Expression commons schemas params grp from (nullity 'PGbool)
1178+ -> Expression grp commons schemas params from (nullity 'PGbool)
12051179rowNeqAny = unsafeRowSubqueryExpression "<> ANY"
12061180
12071181-- | >>> printSQL $ true `allLt` values_ (true `as` #foo)
12081182-- TRUE ALL < (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
12091183allLt
1210− :: Expression commons schemas params grp from ty -- ^ expression
1184+ :: Expression grp commons schemas params from ty -- ^ expression
12111185 -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1212− -> Expression commons schemas params grp from (nullity 'PGbool)
1186+ -> Expression grp commons schemas params from (nullity 'PGbool)
12131187allLt = unsafeSubqueryExpression "ALL <"
12141188
1215−-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1189+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression grp commons schemas params from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
12161190-- >>> printSQL $ myRow `rowLtAll` values_ myRow
12171191-- ROW(1, FALSE) ALL < (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
12181192rowLtAll
12191193 :: SListI row
1220− => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1194+ => NP (Aliased (Expression grp commons schemas params from)) row -- ^ row constructor
12211195 -> Query commons schemas params row -- ^ subquery
1222− -> Expression commons schemas params grp from (nullity 'PGbool)
1196+ -> Expression grp commons schemas params from (nullity 'PGbool)
12231197rowLtAll = unsafeRowSubqueryExpression "ALL <"
12241198
12251199-- | >>> printSQL $ true `ltAny` values_ (true `as` #foo)
12261200-- TRUE ANY < (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
12271201ltAny
1228− :: Expression commons schemas params grp from ty -- ^ expression
1202+ :: Expression grp commons schemas params from ty -- ^ expression
12291203 -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1230− -> Expression commons schemas params grp from (nullity 'PGbool)
1204+ -> Expression grp commons schemas params from (nullity 'PGbool)
12311205ltAny = unsafeSubqueryExpression "ANY <"
12321206
1233−-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1207+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression grp commons schemas params from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
12341208-- >>> printSQL $ myRow `rowLtAll` values_ myRow
12351209-- ROW(1, FALSE) ALL < (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
12361210rowLtAny
12371211 :: SListI row
1238− => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1212+ => NP (Aliased (Expression grp commons schemas params from)) row -- ^ row constructor
12391213 -> Query commons schemas params row -- ^ subquery
1240− -> Expression commons schemas params grp from (nullity 'PGbool)
1214+ -> Expression grp commons schemas params from (nullity 'PGbool)
12411215rowLtAny = unsafeRowSubqueryExpression "ANY <"
12421216
12431217-- | >>> printSQL $ true `lteAll` values_ (true `as` #foo)
12441218-- TRUE <= ALL (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
12451219lteAll
1246− :: Expression commons schemas params grp from ty -- ^ expression
1220+ :: Expression grp commons schemas params from ty -- ^ expression
12471221 -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1248− -> Expression commons schemas params grp from (nullity 'PGbool)
1222+ -> Expression grp commons schemas params from (nullity 'PGbool)
12491223lteAll = unsafeSubqueryExpression "<= ALL"
12501224
1251−-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1225+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression grp commons schemas params from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
12521226-- >>> printSQL $ myRow `rowLteAll` values_ myRow
12531227-- ROW(1, FALSE) <= ALL (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
12541228rowLteAll
12551229 :: SListI row
1256− => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1230+ => NP (Aliased (Expression grp commons schemas params from)) row -- ^ row constructor
12571231 -> Query commons schemas params row -- ^ subquery
1258− -> Expression commons schemas params grp from (nullity 'PGbool)
1232+ -> Expression grp commons schemas params from (nullity 'PGbool)
12591233rowLteAll = unsafeRowSubqueryExpression "<= ALL"
12601234
12611235-- | >>> printSQL $ true `lteAny` values_ (true `as` #foo)
12621236-- TRUE <= ANY (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
12631237lteAny
1264− :: Expression commons schemas params grp from ty -- ^ expression
1238+ :: Expression grp commons schemas params from ty -- ^ expression
12651239 -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1266− -> Expression commons schemas params grp from (nullity 'PGbool)
1240+ -> Expression grp commons schemas params from (nullity 'PGbool)
12671241lteAny = unsafeSubqueryExpression "<= ANY"
12681242
1269−-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1243+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression grp commons schemas params from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
12701244-- >>> printSQL $ myRow `rowLteAny` values_ myRow
12711245-- ROW(1, FALSE) <= ANY (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
12721246rowLteAny
12731247 :: SListI row
1274− => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1248+ => NP (Aliased (Expression grp commons schemas params from)) row -- ^ row constructor
12751249 -> Query commons schemas params row -- ^ subquery
1276− -> Expression commons schemas params grp from (nullity 'PGbool)
1250+ -> Expression grp commons schemas params from (nullity 'PGbool)
12771251rowLteAny = unsafeRowSubqueryExpression "<= ANY"
12781252
12791253-- | >>> printSQL $ true `gtAll` values_ (true `as` #foo)
12801254-- TRUE > ALL (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
12811255gtAll
1282− :: Expression commons schemas params grp from ty -- ^ expression
1256+ :: Expression grp commons schemas params from ty -- ^ expression
12831257 -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1284− -> Expression commons schemas params grp from (nullity 'PGbool)
1258+ -> Expression grp commons schemas params from (nullity 'PGbool)
12851259gtAll = unsafeSubqueryExpression "> ALL"
12861260
1287−-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1261+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression grp commons schemas params from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
12881262-- >>> printSQL $ myRow `rowGtAll` values_ myRow
12891263-- ROW(1, FALSE) > ALL (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
12901264rowGtAll
12911265 :: SListI row
1292− => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1266+ => NP (Aliased (Expression grp commons schemas params from)) row -- ^ row constructor
12931267 -> Query commons schemas params row -- ^ subquery
1294− -> Expression commons schemas params grp from (nullity 'PGbool)
1268+ -> Expression grp commons schemas params from (nullity 'PGbool)
12951269rowGtAll = unsafeRowSubqueryExpression "> ALL"
12961270
12971271-- | >>> printSQL $ true `gtAny` values_ (true `as` #foo)
12981272-- TRUE > ANY (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
12991273gtAny
1300− :: Expression commons schemas params grp from ty -- ^ expression
1274+ :: Expression grp commons schemas params from ty -- ^ expression
13011275 -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1302− -> Expression commons schemas params grp from (nullity 'PGbool)
1276+ -> Expression grp commons schemas params from (nullity 'PGbool)
13031277gtAny = unsafeSubqueryExpression "> ANY"
13041278
1305−-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1279+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression grp commons schemas params from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
13061280-- >>> printSQL $ myRow `rowGtAny` values_ myRow
13071281-- ROW(1, FALSE) > ANY (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
13081282rowGtAny
13091283 :: SListI row
1310− => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1284+ => NP (Aliased (Expression grp commons schemas params from)) row -- ^ row constructor
13111285 -> Query commons schemas params row -- ^ subquery
1312− -> Expression commons schemas params grp from (nullity 'PGbool)
1286+ -> Expression grp commons schemas params from (nullity 'PGbool)
13131287rowGtAny = unsafeRowSubqueryExpression "> ANY"
13141288
13151289-- | >>> printSQL $ true `gteAll` values_ (true `as` #foo)
13161290-- TRUE >= ALL (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
13171291gteAll
1318− :: Expression commons schemas params grp from ty -- ^ expression
1292+ :: Expression grp commons schemas params from ty -- ^ expression
13191293 -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1320− -> Expression commons schemas params grp from (nullity 'PGbool)
1294+ -> Expression grp commons schemas params from (nullity 'PGbool)
13211295gteAll = unsafeSubqueryExpression ">= ALL"
13221296
1323−-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1297+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression grp commons schemas params from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
13241298-- >>> printSQL $ myRow `rowGteAll` values_ myRow
13251299-- ROW(1, FALSE) >= ALL (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
13261300rowGteAll
13271301 :: SListI row
1328− => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1302+ => NP (Aliased (Expression grp commons schemas params from)) row -- ^ row constructor
13291303 -> Query commons schemas params row -- ^ subquery
1330− -> Expression commons schemas params grp from (nullity 'PGbool)
1304+ -> Expression grp commons schemas params from (nullity 'PGbool)
13311305rowGteAll = unsafeRowSubqueryExpression ">= ALL"
13321306
13331307-- | >>> printSQL $ true `gteAny` values_ (true `as` #foo)
13341308-- TRUE >= ANY (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
13351309gteAny
1336− :: Expression commons schemas params grp from ty -- ^ expression
1310+ :: Expression grp commons schemas params from ty -- ^ expression
13371311 -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1338− -> Expression commons schemas params grp from (nullity 'PGbool)
1312+ -> Expression grp commons schemas params from (nullity 'PGbool)
13391313gteAny = unsafeSubqueryExpression ">= ANY"
13401314
1341−-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1315+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression grp commons schemas params from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
13421316-- >>> printSQL $ myRow `rowGteAny` values_ myRow
13431317-- ROW(1, FALSE) >= ANY (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
13441318rowGteAny
13451319 :: SListI row
1346− => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1320+ => NP (Aliased (Expression grp commons schemas params from)) row -- ^ row constructor
13471321 -> Query commons schemas params row -- ^ subquery
1348− -> Expression commons schemas params grp from (nullity 'PGbool)
1322+ -> Expression grp commons schemas params from (nullity 'PGbool)
13491323rowGteAny = unsafeRowSubqueryExpression ">= ANY"
13501324
13511325-- | A `CommonTableExpression` is an auxiliary statement in a `with` clause.