@@ -1,13 +1,14 @@
2Module: Squeal.PostgreSQL.Expression
2Module: Squeal.PostgreSQL.Expression
3Description: Squeal expressions
3Description: Squeal expressions
4−Copyright: (c) Eitan Chatav, 2019
4+Copyright: (c) Eitan Chatav, 2017
5Maintainer: eitan@morphism.tech
5Maintainer: eitan@morphism.tech
6Stability: experimental
6Stability: experimental
8Squeal expressions are the atoms used to build statements.
8Squeal expressions are the atoms used to build statements.
11+{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
11{-# LANGUAGE
12{-# LANGUAGE
12 AllowAmbiguousTypes
13 AllowAmbiguousTypes
13 , ConstraintKinds
14 , ConstraintKinds
@@ -34,213 +35,377 @@ module Squeal.PostgreSQL.Expression
34 ( -- * Expression
35 ( -- * Expression
35 Expression (..)
36 Expression (..)
45 , Operator
38 , Operator
46− , OperatorDB
39+ , (:-->)
47− , unsafeBinaryOp
40+ , HasParameter (parameter)
48− , binaryOp
41+ , param
49− , FunctionVar
42+ -- ** Null
50− , unsafeFunctionVar
43+ , null_
51− , type (--->)
44+ , notNull
52− , FunctionNDB
45+ , coalesce
53− , unsafeFunctionN
46+ , fromNull
54− , functionN
47+ , isNull
55 , PGSubset (..)
57 , PGSubset (..)
56− , PGIntersect (..)
58+ -- ** Functions
63+ , unsafeVariadicFunction
92+ , notBetweenSymmetric
119+ -- *** Json and Jsonb operators
124+ -- *** Jsonb operators
149+ , jsonExtractPathAsText
150+ , jsonbExtractPathAsText
169+ , websearchToTSquery
184+ -- * Window Functions
185+ , WindowDefinition (..)
187+ , WindowFunction (..)
200+ , SortExpression (..)
203+ , TypeExpression (..)
229+ , timestampWithTimeZone
57 -- * Re-export
242 -- * Re-export
63import Control.Category
248import Control.Category
64import Control.DeepSeq
249import Control.DeepSeq
65import Data.ByteString (ByteString)
250import Data.ByteString (ByteString)
251+import Data.ByteString.Lazy (toStrict)
66import Data.Function ((&))
252import Data.Function ((&))
67import Data.Semigroup hiding (All)
254import Data.Semigroup hiding (All)
255+import qualified Data.Aeson as JSON
68import Data.String
256import Data.String
69import Generics.SOP hiding (All, from)
258import Generics.SOP hiding (All, from)
70import GHC.OverloadedLabels
259import GHC.OverloadedLabels
71import GHC.TypeLits
260import GHC.TypeLits
72import Numeric
261import Numeric
73import Prelude hiding (id, (.))
262import Prelude hiding (id, (.))
264+import qualified Data.ByteString as ByteString
75import qualified GHC.Generics as GHC
265import qualified GHC.Generics as GHC
266+import qualified Generics.SOP as SOP
77−import Squeal.PostgreSQL.Alias
78−import Squeal.PostgreSQL.List
79import Squeal.PostgreSQL.Render
268import Squeal.PostgreSQL.Render
80import Squeal.PostgreSQL.Schema
269import Squeal.PostgreSQL.Schema
83−-- >>> import Squeal.PostgreSQL
85{-----------------------------------------
271{-----------------------------------------
86column expressions
272column expressions
87-----------------------------------------}
273-----------------------------------------}
89{- | `Expression`s are used in a variety of contexts,
275{- | `Expression`s are used in a variety of contexts,
90−such as in the target `Squeal.PostgreSQL.Query.List` of the
91−`Squeal.PostgreSQL.Query.select` command,
276+such as in the target list of the `Squeal.PostgreSQL.Query.select` command,
92as new column values in `Squeal.PostgreSQL.Manipulation.insertRow` or
277as new column values in `Squeal.PostgreSQL.Manipulation.insertRow` or
93`Squeal.PostgreSQL.Manipulation.update`,
278`Squeal.PostgreSQL.Manipulation.update`,
94−or in search `Squeal.PostgreSQL.Logic.Condition`s in a number of commands.
279+or in search `Condition`s in a number of commands.
96The expression syntax allows the calculation of
281The expression syntax allows the calculation of
97values from primitive expression using arithmetic, logical,
282values from primitive expression using arithmetic, logical,
98and other operations.
283and other operations.
100−The type parameters of `Expression` are
102−* @outer :: @ `FromType`, the @from@ clauses of any outer queries in which
103− the `Expression` is a correlated subquery expression;
104−* @commons :: @ `FromType`, the `Squeal.PostgreSQL.Query.CommonTableExpression`s
105− that are in scope for the `Expression`;
106−* @grp :: @ `Grouping`, the `Grouping` of the @from@ clause which may limit
107− which columns may be referenced by alias;
108−* @schemas :: @ `SchemasType`, the schemas of your database that are in
109− scope for the `Expression`;
110−* @from :: @ `FromType`, the @from@ clause which the `Expression` may use
111− to reference columns by alias;
112−* @ty :: @ `NullityType`, the type of the `Expression`.
114newtype Expression
285newtype Expression
115 (outer :: FromType)
286 (outer :: FromType)
116− (commons :: FromType)
117 (grp :: Grouping)
287 (grp :: Grouping)
288+ (commons :: FromType)
118 (schemas :: SchemasType)
289 (schemas :: SchemasType)
119 (params :: [NullityType])
290 (params :: [NullityType])
120 (from :: FromType)
291 (from :: FromType)
121 (ty :: NullityType)
292 (ty :: NullityType)
122 = UnsafeExpression { renderExpression :: ByteString }
293 = UnsafeExpression { renderExpression :: ByteString }
123 deriving (GHC.Generic,Show,Eq,Ord,NFData)
294 deriving (GHC.Generic,Show,Eq,Ord,NFData)
124−instance RenderSQL (Expression outer commons grp schemas params from ty) where
295+instance RenderSQL (Expression outer grp commons schemas params from ty) where
125 renderSQL = renderExpression
296 renderSQL = renderExpression
127-- | An `Expr` is a closed `Expression`.
298-- | An `Expr` is a closed `Expression`.
128−-- It is a F@RankNType@ but don't be scared.
129−-- Think of it as an expression which sees no
130−-- namespaces, so you can't use parameters
131−-- or alias references. It can be used as
132−-- a simple piece of more complex `Expression`s.
133type Expr x
299type Expr x
134− = forall outer commons grp schemas params from
300+ = forall outer grp commons schemas params from
135− . Expression outer commons grp schemas params from x
301+ . Expression outer grp commons schemas params from x
136 -- ^ cannot reference aliases
302 -- ^ cannot reference aliases
138−-- | A @RankNType@ for binary operators.
139type Operator x1 x2 y
304type Operator x1 x2 y
140− = forall outer commons grp schemas params from
141− . Expression outer commons grp schemas params from x1
305+ = forall outer grp commons schemas params from
143− -> Expression outer commons grp schemas params from x2
145− -> Expression outer commons grp schemas params from y
148−type OperatorDB schemas x1 x2 y
149− = forall outer commons grp params from
150− . Expression outer commons grp schemas params from x1
306+ . Expression outer grp commons schemas params from x1
151 -- ^ left input
307 -- ^ left input
152− -> Expression outer commons grp schemas params from x2
308+ -> Expression outer grp commons schemas params from x2
153 -- ^ right input
309 -- ^ right input
154− -> Expression outer commons grp schemas params from y
157−-- | A @RankNType@ for functions with a single argument.
158−-- These could be either function calls or unary operators.
159−-- This is a subtype of the usual Haskell function type `Prelude.->`,
160−-- indeed a subcategory as it is closed under the usual
161−-- `Prelude..` and `Prelude.id`.
163− = forall outer commons grp schemas params from
164− . Expression outer commons grp schemas params from x
166− -> Expression outer commons grp schemas params from y
310+ -> Expression outer grp commons schemas params from y
167 -- ^ output
311 -- ^ output
169−type FunctionDB schemas x y
313+type (:-->) x y
170− = forall outer commons grp params from
314+ = forall outer grp commons schemas params from
171− . Expression outer commons grp schemas params from x
315+ . Expression outer grp commons schemas params from x
172 -- ^ input
316 -- ^ input
173− -> Expression outer commons grp schemas params from y
317+ -> Expression outer grp commons schemas params from y
174 -- ^ output
318 -- ^ output
176−{- | A @RankNType@ for functions with a fixed-length list of heterogeneous arguments.
177−Use the `*:` operator to end your argument lists, like so.
179−>>> printSQL (unsafeFunctionN "fun" (true :* false :* localTime *: true))
180−fun(TRUE, FALSE, LOCALTIME, TRUE)
182−type (--->) xs y
320+type FunctionHet xs y
183− = forall outer commons grp schemas params from
321+ = forall outer grp commons schemas params from
184− . NP (Expression outer commons grp schemas params from) xs
322+ . NP (Expression outer grp commons schemas params from) xs
185 -- ^ inputs
323 -- ^ inputs
186− -> Expression outer commons grp schemas params from y
324+ -> Expression outer grp commons schemas params from y
187 -- ^ output
325 -- ^ output
189−type FunctionNDB schemas xs y
327+type FunctionHom x0 x1 y
190− = forall outer commons grp params from
191− . NP (Expression outer commons grp schemas params from) xs
193− -> Expression outer commons grp schemas params from y
196−{- | A @RankNType@ for functions with a variable-length list of
197−homogeneous arguments and at least 1 more argument.
199−type FunctionVar x0 x1 y
200− = forall outer commons grp schemas params from
328+ = forall outer grp commons schemas params from
201− . [Expression outer commons grp schemas params from x0]
329+ . [Expression outer grp commons schemas params from x0]
202 -- ^ inputs
330 -- ^ inputs
203− -> Expression outer commons grp schemas params from x1
331+ -> Expression outer grp commons schemas params from x1
204 -- ^ must have at least 1 input
332 -- ^ must have at least 1 input
205− -> Expression outer commons grp schemas params from y
333+ -> Expression outer grp commons schemas params from y
206 -- ^ output
334 -- ^ output
208−{- | >>> printSQL (unsafeFunctionVar "greatest" [true, null_] false)
336+unsafeFunctionHom :: ByteString -> FunctionHom x0 x1 y
209−greatest(TRUE, NULL, FALSE)
211−unsafeFunctionVar :: ByteString -> FunctionVar x0 x1 y
212−unsafeFunctionVar fun xs x = UnsafeExpression $ fun <> parenthesized
337+unsafeFunctionHom fun xs x = UnsafeExpression $ fun <> parenthesized
213 (commaSeparated (renderSQL <$> xs) <> ", " <> renderSQL x)
338 (commaSeparated (renderSQL <$> xs) <> ", " <> renderSQL x)
340+{- | A `HasParameter` constraint is used to indicate a value that is
341+supplied externally to a SQL statement.
342+`Squeal.PostgreSQL.PQ.manipulateParams`,
343+`Squeal.PostgreSQL.PQ.queryParams` and
344+`Squeal.PostgreSQL.PQ.traversePrepared` support specifying data values
345+separately from the SQL command string, in which case `param`s are used to
346+refer to the out-of-line data values.
348+class KnownNat n => HasParameter
350+ (params :: [NullityType])
352+ | n params -> ty where
353+ -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
355+ -- >>> let expr = parameter @1 int4 :: Expression outer grp '[] schemas '[ 'Null 'PGint4] from ('Null 'PGint4)
356+ -- >>> printSQL expr
359+ :: TypeExpression schemas ty
360+ -> Expression outer grp commons schemas params from ty
361+ parameter ty = UnsafeExpression $ parenthesized $
362+ "$" <> renderNat @n <+> "::"
364+instance {-# OVERLAPPING #-} HasParameter 1 (ty1:tys) ty1
365+instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
366+ => HasParameter n (ty' : params) ty
368+-- | `param` takes a `Nat` using type application and for basic types,
369+-- infers a `TypeExpression`.
371+-- >>> let expr = param @1 :: Expression outer grp commons schemas '[ 'Null 'PGint4] from ('Null 'PGint4)
375+ :: forall n outer commons schemas params from grp ty
376+ . (PGTyped schemas ty, HasParameter n params ty)
377+ => Expression outer grp commons schemas params from ty -- ^ param
378+param = parameter @n (pgtype @schemas)
215instance (HasUnique tab (Join outer from) row, Has col row ty)
380instance (HasUnique tab (Join outer from) row, Has col row ty)
216− => IsLabel col (Expression outer commons 'Ungrouped schemas params from ty) where
381+ => IsLabel col (Expression outer 'Ungrouped commons schemas params from ty) where
217 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
382 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
218instance (HasUnique tab (Join outer from) row, Has col row ty, tys ~ '[ty])
383instance (HasUnique tab (Join outer from) row, Has col row ty, tys ~ '[ty])
219− => IsLabel col (NP (Expression outer commons 'Ungrouped schemas params from) tys) where
384+ => IsLabel col (NP (Expression outer 'Ungrouped commons schemas params from) tys) where
220 fromLabel = fromLabel @col :* Nil
385 fromLabel = fromLabel @col :* Nil
221instance (HasUnique tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
386instance (HasUnique tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
222 => IsLabel col
387 => IsLabel col
223− (Aliased (Expression outer commons 'Ungrouped schemas params from) column) where
388+ (Aliased (Expression outer 'Ungrouped commons schemas params from) column) where
224 fromLabel = fromLabel @col `As` Alias
389 fromLabel = fromLabel @col `As` Alias
225instance (HasUnique tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
390instance (HasUnique tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
226 => IsLabel col
391 => IsLabel col
227− (NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns) where
392+ (NP (Aliased (Expression outer 'Ungrouped commons schemas params from)) columns) where
228 fromLabel = fromLabel @col :* Nil
393 fromLabel = fromLabel @col :* Nil
230instance (Has tab (Join outer from) row, Has col row ty)
395instance (Has tab (Join outer from) row, Has col row ty)
231− => IsQualified tab col (Expression outer commons 'Ungrouped schemas params from ty) where
396+ => IsQualified tab col (Expression outer 'Ungrouped commons schemas params from ty) where
232 tab ! col = UnsafeExpression $
397 tab ! col = UnsafeExpression $
233 renderSQL tab <> "." <> renderSQL col
398 renderSQL tab <> "." <> renderSQL col
234instance (Has tab (Join outer from) row, Has col row ty, tys ~ '[ty])
399instance (Has tab (Join outer from) row, Has col row ty, tys ~ '[ty])
235− => IsQualified tab col (NP (Expression outer commons 'Ungrouped schemas params from) tys) where
400+ => IsQualified tab col (NP (Expression outer 'Ungrouped commons schemas params from) tys) where
236 tab ! col = tab ! col :* Nil
401 tab ! col = tab ! col :* Nil
237instance (Has tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
402instance (Has tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
238 => IsQualified tab col
403 => IsQualified tab col
239− (Aliased (Expression outer commons 'Ungrouped schemas params from) column) where
404+ (Aliased (Expression outer 'Ungrouped commons schemas params from) column) where
240 tab ! col = tab ! col `As` col
405 tab ! col = tab ! col `As` col
241instance (Has tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
406instance (Has tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
242 => IsQualified tab col
407 => IsQualified tab col
243− (NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns) where
408+ (NP (Aliased (Expression outer 'Ungrouped commons schemas params from)) columns) where
244 tab ! col = tab ! col :* Nil
409 tab ! col = tab ! col :* Nil
@@ -248,7 +413,7 @@ instance
248 , Has col row ty
413 , Has col row ty
249 , GroupedBy tab col bys
414 , GroupedBy tab col bys
250 ) => IsLabel col
415 ) => IsLabel col
251− (Expression outer commons ('Grouped bys) schemas params from ty) where
416+ (Expression outer ('Grouped bys) commons schemas params from ty) where
252 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
417 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
254 ( HasUnique tab (Join outer from) row
419 ( HasUnique tab (Join outer from) row
@@ -256,7 +421,7 @@ instance
256 , GroupedBy tab col bys
421 , GroupedBy tab col bys
257 , tys ~ '[ty]
422 , tys ~ '[ty]
258 ) => IsLabel col
423 ) => IsLabel col
259− (NP (Expression outer commons ('Grouped bys) schemas params from) tys) where
424+ (NP (Expression outer ('Grouped bys) commons schemas params from) tys) where
260 fromLabel = fromLabel @col :* Nil
425 fromLabel = fromLabel @col :* Nil
262 ( HasUnique tab (Join outer from) row
427 ( HasUnique tab (Join outer from) row
@@ -264,7 +429,7 @@ instance
264 , GroupedBy tab col bys
429 , GroupedBy tab col bys
265 , column ~ (col ::: ty)
430 , column ~ (col ::: ty)
266 ) => IsLabel col
431 ) => IsLabel col
267− (Aliased (Expression outer commons ('Grouped bys) schemas params from) column) where
432+ (Aliased (Expression outer ('Grouped bys) commons schemas params from) column) where
268 fromLabel = fromLabel @col `As` Alias
433 fromLabel = fromLabel @col `As` Alias
270 ( HasUnique tab (Join outer from) row
435 ( HasUnique tab (Join outer from) row
@@ -272,7 +437,7 @@ instance
272 , GroupedBy tab col bys
437 , GroupedBy tab col bys
273 , columns ~ '[col ::: ty]
438 , columns ~ '[col ::: ty]
274 ) => IsLabel col
439 ) => IsLabel col
275− (NP (Aliased (Expression outer commons ('Grouped bys) schemas params from)) columns) where
440+ (NP (Aliased (Expression outer ('Grouped bys) commons schemas params from)) columns) where
276 fromLabel = fromLabel @col :* Nil
441 fromLabel = fromLabel @col :* Nil
@@ -280,7 +445,7 @@ instance
280 , Has col row ty
445 , Has col row ty
281 , GroupedBy tab col bys
446 , GroupedBy tab col bys
282 ) => IsQualified tab col
447 ) => IsQualified tab col
283− (Expression outer commons ('Grouped bys) schemas params from ty) where
448+ (Expression outer ('Grouped bys) commons schemas params from ty) where
284 tab ! col = UnsafeExpression $
449 tab ! col = UnsafeExpression $
285 renderSQL tab <> "." <> renderSQL col
450 renderSQL tab <> "." <> renderSQL col
@@ -289,7 +454,7 @@ instance
289 , GroupedBy tab col bys
454 , GroupedBy tab col bys
290 , tys ~ '[ty]
455 , tys ~ '[ty]
291 ) => IsQualified tab col
456 ) => IsQualified tab col
292− (NP (Expression outer commons ('Grouped bys) schemas params from) tys) where
457+ (NP (Expression outer ('Grouped bys) commons schemas params from) tys) where
293 tab ! col = tab ! col :* Nil
458 tab ! col = tab ! col :* Nil
295 ( Has tab (Join outer from) row
460 ( Has tab (Join outer from) row
@@ -297,7 +462,7 @@ instance
297 , GroupedBy tab col bys
462 , GroupedBy tab col bys
298 , column ~ (col ::: ty)
463 , column ~ (col ::: ty)
299 ) => IsQualified tab col
464 ) => IsQualified tab col
300− (Aliased (Expression outer commons ('Grouped bys) schemas params from) column) where
465+ (Aliased (Expression outer ('Grouped bys) commons schemas params from) column) where
301 tab ! col = tab ! col `As` col
466 tab ! col = tab ! col `As` col
303 ( Has tab (Join outer from) row
468 ( Has tab (Join outer from) row
@@ -305,78 +470,206 @@ instance
305 , GroupedBy tab col bys
470 , GroupedBy tab col bys
306 , columns ~ '[col ::: ty]
471 , columns ~ '[col ::: ty]
307 ) => IsQualified tab col
472 ) => IsQualified tab col
308− (NP (Aliased (Expression outer commons ('Grouped bys) schemas params from)) columns) where
473+ (NP (Aliased (Expression outer ('Grouped bys) commons schemas params from)) columns) where
309 tab ! col = tab ! col :* Nil
474 tab ! col = tab ! col :* Nil
476+-- | analagous to `Nothing`
478+-- >>> printSQL null_
480+null_ :: Expr ('Null ty)
481+null_ = UnsafeExpression "NULL"
483+-- | analagous to `Just`
485+-- >>> printSQL $ notNull true
487+notNull :: 'NotNull ty :--> 'Null ty
488+notNull = UnsafeExpression . renderSQL
490+-- | return the leftmost value which is not NULL
492+-- >>> printSQL $ coalesce [null_, true] false
493+-- COALESCE(NULL, TRUE, FALSE)
494+coalesce :: FunctionHom ('Null ty) ('NotNull ty) ('NotNull ty)
495+coalesce nullxs notNullx = UnsafeExpression $
496+ "COALESCE" <> parenthesized (commaSeparated
497+ ((renderSQL <$> nullxs) <> [renderSQL notNullx]))
499+-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
501+-- >>> printSQL $ fromNull true null_
502+-- COALESCE(NULL, TRUE)
504+ :: Expression outer grp commons schemas params from ('NotNull ty)
505+ -- ^ what to convert @NULL@ to
506+ -> Expression outer grp commons schemas params from ('Null ty)
507+ -> Expression outer grp commons schemas params from ('NotNull ty)
508+fromNull notNullx nullx = coalesce [nullx] notNullx
510+-- | >>> printSQL $ null_ & isNull
512+isNull :: 'Null ty :--> null 'PGbool
513+isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
515+-- | >>> printSQL $ null_ & isNotNull
517+isNotNull :: 'Null ty :--> null 'PGbool
518+isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
520+-- | analagous to `maybe` using @IS NULL@
522+-- >>> printSQL $ matchNull true not_ null_
523+-- CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END
525+ :: Expression outer grp commons schemas params from (nullty)
526+ -- ^ what to convert @NULL@ to
527+ -> ( Expression outer grp commons schemas params from ('NotNull ty)
528+ -> Expression outer grp commons schemas params from (nullty) )
529+ -- ^ function to perform when @NULL@ is absent
530+ -> Expression outer grp commons schemas params from ('Null ty)
531+ -> Expression outer grp commons schemas params from (nullty)
532+matchNull y f x = ifThenElse (isNull x) y
533+ (f (UnsafeExpression (renderSQL x)))
535+{-| right inverse to `fromNull`, if its arguments are equal then
536+`nullIf` gives @NULL@.
538+>>> :set -XTypeApplications -XDataKinds
539+>>> let expr = nullIf (false *: param @1) :: Expression outer grp commons schemas '[ 'NotNull 'PGbool] from ('Null 'PGbool)
541+NULLIF(FALSE, ($1 :: bool))
543+nullIf :: FunctionHet '[ 'NotNull ty, 'NotNull ty] ('Null ty)
544+nullIf = unsafeFunctionHet "NULLIF"
546+-- | >>> printSQL $ array [null_, false, true]
547+-- ARRAY[NULL, FALSE, TRUE]
549+ :: [Expression outer grp commons schemas params from ty]
551+ -> Expression outer grp commons schemas params from (null ('PGvararray ty))
552+array xs = UnsafeExpression $
553+ "ARRAY[" <> commaSeparated (renderSQL <$> xs) <> "]"
555+-- | >>> printSQL $ cardinality (array [null_, false, true])
556+-- cardinality(ARRAY[NULL, FALSE, TRUE])
557+cardinality :: null ('PGvararray ty) :--> null 'PGint8
558+cardinality = unsafeFunction "cardinality"
560+-- | >>> printSQL $ array [null_, false, true] & index 2
561+-- (ARRAY[NULL, FALSE, TRUE])[2]
563+ :: Word64 -- ^ index
564+ -> null ('PGvararray ty) :--> NullifyType ty
565+index n expr = UnsafeExpression $
566+ parenthesized (renderSQL expr) <> "[" <> fromString (show n) <> "]"
311instance (KnownSymbol label, label `In` labels) => IsPGlabel label
568instance (KnownSymbol label, label `In` labels) => IsPGlabel label
312− (Expression outer commons grp schemas params from (null ('PGenum labels))) where
569+ (Expression outer grp commons schemas params from (null ('PGenum labels))) where
313 label = UnsafeExpression $ renderSQL (PGlabel @label)
570 label = UnsafeExpression $ renderSQL (PGlabel @label)
572+-- | A row constructor is an expression that builds a row value
573+-- (also called a composite value) using values for its member fields.
576+-- type Complex = 'PGcomposite
577+-- '[ "real" ::: 'NotNull 'PGfloat8
578+-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
581+-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression outer grp commons schemas params from ('NotNull Complex)
586+ => NP (Aliased (Expression outer grp commons schemas params from)) row
587+ -- ^ zero or more expressions for the row field values
588+ -> Expression outer grp commons schemas params from (null ('PGcomposite row))
589+row exprs = UnsafeExpression $ "ROW" <> parenthesized
590+ (renderCommaSeparated (\ (expr `As` _) -> renderSQL expr) exprs)
593+-- type Complex = 'PGcomposite
594+-- '[ "real" ::: 'NotNull 'PGfloat8
595+-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
596+-- type Schema = '["complex" ::: 'Typedef Complex]
599+-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression outer grp '[] (Public Schema) from params ('NotNull Complex)
600+-- >>> printSQL $ i & field #complex #imaginary
601+-- (ROW(0, 1)::"complex")."imaginary"
603+ :: ( Has sch schemas schema
604+ , Has tydef schema ('Typedef ('PGcomposite row))
606+ => QualifiedAlias sch tydef -- ^ row type
607+ -> Alias field -- ^ field name
608+ -> Expression outer grp commons schemas params from ('NotNull ('PGcomposite row))
609+ -> Expression outer grp commons schemas params from ty
610+field td fld expr = UnsafeExpression $
611+ parenthesized (renderSQL expr <> "::" <> renderSQL td)
612+ <> "." <> renderSQL fld
615+ (Expression outer grp commons schemas params from (null ('PGvararray ty))) where
616+ (<>) = unsafeBinaryOp "||"
619+ (Expression outer grp commons schemas params from (null ('PGvararray ty))) where
623+-- | >>> let expr = greatest [param @1] currentTimestamp :: Expression outer grp commons schemas '[ 'NotNull 'PGtimestamptz] from ('NotNull 'PGtimestamptz)
625+-- GREATEST(($1 :: timestamp with time zone), CURRENT_TIMESTAMP)
626+greatest :: FunctionHom ty ty ty
627+greatest = unsafeFunctionHom "GREATEST"
629+-- | >>> printSQL $ least [null_] currentTimestamp
630+-- LEAST(NULL, CURRENT_TIMESTAMP)
631+least :: FunctionHom ty ty ty
632+least = unsafeFunctionHom "LEAST"
315-- | >>> printSQL $ unsafeBinaryOp "OR" true false
634-- | >>> printSQL $ unsafeBinaryOp "OR" true false
316-- (TRUE OR FALSE)
635-- (TRUE OR FALSE)
317−unsafeBinaryOp :: ByteString -> Operator ty0 ty1 ty2
636+unsafeBinaryOp :: ByteString {- ^ operator -} -> Operator ty0 ty1 ty2
318unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
637unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
319 renderSQL x <+> op <+> renderSQL y
638 renderSQL x <+> op <+> renderSQL y
322− :: forall op sch schemas schema x y z.
323− ( Has sch schemas schema
324− , Has op schema ('Operator ('BinaryOp x y z)) )
325− => OperatorDB schemas x y z
326−binaryOp = unsafeBinaryOp $ renderSymbol @op
328−-- | >>> printSQL $ unsafeLeftOp "NOT" true
640+-- | >>> printSQL $ unsafeUnaryOpL "NOT" true
329-- (NOT TRUE)
641-- (NOT TRUE)
330−unsafeLeftOp :: ByteString -> x --> y
642+unsafeUnaryOpL :: ByteString {- ^ operator -} -> x :--> y
331−unsafeLeftOp op x = UnsafeExpression $ parenthesized $ op <+> renderSQL x
643+unsafeUnaryOpL op x = UnsafeExpression $ parenthesized $ op <+> renderSQL x
333−leftOp
645+unsafeUnaryOpR :: ByteString {- ^ operator -} -> x :--> y
334− :: forall op sch schemas schema x y.
335− ( Has sch schemas schema
336− , Has op schema ('Operator ('LeftOp x y)) )
337− => FunctionDB schemas x y
338−leftOp = unsafeLeftOp $ renderSymbol @op
340−-- | >>> printSQL $ true & unsafeRightOp "IS NOT TRUE"
341−-- (TRUE IS NOT TRUE)
342−unsafeRightOp :: ByteString -> x --> y
343−unsafeRightOp op x = UnsafeExpression $ parenthesized $ renderSQL x <+> op
646+unsafeUnaryOpR op x = UnsafeExpression $ parenthesized $ renderSQL x <+> op
346− :: forall op sch schemas schema x y.
347− ( Has sch schemas schema
348− , Has op schema ('Operator ('RightOp x y)) )
349− => FunctionDB schemas x y
350−rightOp = unsafeRightOp $ renderSymbol @op
352-- | >>> printSQL $ unsafeFunction "f" true
648-- | >>> printSQL $ unsafeFunction "f" true
353-- f(TRUE)
649-- f(TRUE)
354−unsafeFunction :: ByteString -> x --> y
650+unsafeFunction :: ByteString {- ^ function -} -> x :--> y
355unsafeFunction fun x = UnsafeExpression $
651unsafeFunction fun x = UnsafeExpression $
356 fun <> parenthesized (renderSQL x)
652 fun <> parenthesized (renderSQL x)
358−function
654+unsafeFunctionHet
359− :: (Has sch schemas schema, Has fun schema ('Function ('[x] :=> 'Returns y)))
655+ :: SListI xs
360− => QualifiedAlias sch fun
656+ => ByteString {- ^ function -}
361− -> FunctionDB schemas x y
657+ -> FunctionHet xs y
362−function = unsafeFunction . renderSQL
364−-- | >>> printSQL $ unsafeFunctionN "f" (currentTime :* localTimestamp :* false *: literal 'a')
365−-- f(CURRENT_TIME, LOCALTIMESTAMP, FALSE, E'a')
366−unsafeFunctionN :: SListI xs => ByteString -> xs ---> y
367−unsafeFunctionN fun xs = UnsafeExpression $
658+unsafeFunctionHet fun xs = UnsafeExpression $
368 fun <> parenthesized (renderCommaSeparated renderSQL xs)
659 fun <> parenthesized (renderCommaSeparated renderSQL xs)
370−functionN
661+-- | Helper for defining variadic functions.
371− :: ( Has sch schemas schema
662+unsafeVariadicFunction
372− , Has fun schema ('Function (xs :=> 'Returns y))
373− , SListI xs )
663+ :: SListI elems
374− => QualifiedAlias sch fun
664+ => ByteString
375− -> FunctionNDB schemas xs y
665+ -- ^ function
376−functionN = unsafeFunctionN . renderSQL
666+ -> NP (Expression outer grp commons schemas params from) elems
667+ -> Expression outer grp commons schemas params from ret
668+unsafeVariadicFunction fun x = UnsafeExpression $
669+ fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderSQL) x)))
378instance ty `In` PGNum
671instance ty `In` PGNum
379− => Num (Expression outer commons grp schemas params from (null ty)) where
672+ => Num (Expression outer grp commons schemas params from (null ty)) where
380 (+) = unsafeBinaryOp "+"
673 (+) = unsafeBinaryOp "+"
381 (-) = unsafeBinaryOp "-"
674 (-) = unsafeBinaryOp "-"
382 (*) = unsafeBinaryOp "*"
675 (*) = unsafeBinaryOp "*"
@@ -388,7 +681,7 @@ instance ty `In` PGNum
390instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
683instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
391− (Expression outer commons grp schemas params from (null ty)) where
684+ (Expression outer grp commons schemas params from (null ty)) where
392 (/) = unsafeBinaryOp "/"
685 (/) = unsafeBinaryOp "/"
393 fromRational
686 fromRational
394 = UnsafeExpression
687 = UnsafeExpression
@@ -398,7 +691,7 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
398 . fromRat @Double
691 . fromRat @Double
400instance (ty `In` PGNum, ty `In` PGFloating) => Floating
693instance (ty `In` PGNum, ty `In` PGFloating) => Floating
401− (Expression outer commons grp schemas params from (null ty)) where
694+ (Expression outer grp commons schemas params from (null ty)) where
402 pi = UnsafeExpression "pi()"
695 pi = UnsafeExpression "pi()"
403 exp = unsafeFunction "exp"
696 exp = unsafeFunction "exp"
404 log = unsafeFunction "ln"
697 log = unsafeFunction "ln"
@@ -419,54 +712,1775 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Floating
419 acosh x = log (x + sqrt (x*x - 1))
712 acosh x = log (x + sqrt (x*x - 1))
420 atanh x = log ((1 + x) / (1 - x)) / 2
713 atanh x = log ((1 + x) / (1 - x)) / 2
422−-- | Contained by operators
715+-- | >>> :{
423−class PGSubset ty where
716+-- let
717+-- expression :: Expr (null 'PGfloat4)
718+-- expression = atan2_ (pi *: 2)
719+-- in printSQL expression
723+ :: float `In` PGFloating
724+ => FunctionHet '[ null float, null float] (null float)
725+atan2_ = unsafeFunctionHet "atan2"
727+-- When a `cast` is applied to an `Expression` of a known type, it
728+-- represents a run-time type conversion. The cast will succeed only if a
729+-- suitable type conversion operation has been defined.
731+-- | >>> printSQL $ true & cast int4
734+ :: TypeExpression schemas ty1
735+ -- ^ type to cast as
736+ -> Expression outer grp commons schemas params from ty0
737+ -- ^ value to convert
738+ -> Expression outer grp commons schemas params from ty1
739+cast ty x = UnsafeExpression $ parenthesized $
740+ renderSQL x <+> "::" <+> renderSQL ty
742+-- | integer division, truncates the result
746+-- expression :: Expression outer grp commons schemas params from (null 'PGint2)
747+-- expression = 5 `quot_` 2
748+-- in printSQL expression
752+ :: int `In` PGIntegral
753+ => Operator (null int) (null int) (null int)
754+quot_ = unsafeBinaryOp "/"
756+-- | remainder upon integer division
760+-- expression :: Expression outer grp commons schemas params from (null 'PGint2)
761+-- expression = 5 `rem_` 2
762+-- in printSQL expression
766+ :: int `In` PGIntegral
767+ => Operator (null int) (null int) (null int)
768+rem_ = unsafeBinaryOp "%"
772+-- expression :: Expression outer grp commons schemas params from (null 'PGfloat4)
773+-- expression = trunc pi
774+-- in printSQL expression
777+trunc :: frac `In` PGFloating => null frac :--> null frac
778+trunc = unsafeFunction "trunc"
782+-- expression :: Expression outer grp commons schemas params from (null 'PGfloat4)
783+-- expression = round_ pi
784+-- in printSQL expression
787+round_ :: frac `In` PGFloating => null frac :--> null frac
788+round_ = unsafeFunction "round"
792+-- expression :: Expression outer grp commons schemas params from (null 'PGfloat4)
793+-- expression = ceiling_ pi
794+-- in printSQL expression
797+ceiling_ :: frac `In` PGFloating => null frac :--> null frac
798+ceiling_ = unsafeFunction "ceiling"
800+-- | A `Condition` is an `Expression`, which can evaluate
801+-- to `true`, `false` or `null_`. This is because SQL uses
802+-- a three valued logic.
803+type Condition outer grp commons schemas params from =
804+ Expression outer grp commons schemas params from ('Null 'PGbool)
806+-- | >>> printSQL true
808+true :: Expr (null 'PGbool)
809+true = UnsafeExpression "TRUE"
811+-- | >>> printSQL false
813+false :: Expr (null 'PGbool)
814+false = UnsafeExpression "FALSE"
816+-- | >>> printSQL $ not_ true
818+not_ :: null 'PGbool :--> null 'PGbool
819+not_ = unsafeUnaryOpL "NOT"
821+-- | >>> printSQL $ true .&& false
823+(.&&) :: Operator (null 'PGbool) (null 'PGbool) (null 'PGbool)
825+(.&&) = unsafeBinaryOp "AND"
827+-- | >>> printSQL $ true .|| false
829+(.||) :: Operator (null 'PGbool) (null 'PGbool) (null 'PGbool)
831+(.||) = unsafeBinaryOp "OR"
835+-- expression :: Expression outer grp commons schemas params from (null 'PGint2)
836+-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
837+-- in printSQL expression
839+-- CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END
841+ :: [ ( Condition outer grp commons schemas params from
842+ , Expression outer grp commons schemas params from ty
844+ -- ^ whens and thens
845+ -> Expression outer grp commons schemas params from ty
847+ -> Expression outer grp commons schemas params from ty
848+caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
852+ [ " WHEN ", renderSQL when_
853+ , " THEN ", renderSQL then_
855+ | (when_,then_) <- whenThens
857+ , " ELSE ", renderSQL else_
863+-- expression :: Expression outer grp commons schemas params from (null 'PGint2)
864+-- expression = ifThenElse true 1 0
865+-- in printSQL expression
867+-- CASE WHEN TRUE THEN 1 ELSE 0 END
869+ :: Condition outer grp commons schemas params from
870+ -> Expression outer grp commons schemas params from ty -- ^ then
871+ -> Expression outer grp commons schemas params from ty -- ^ else
872+ -> Expression outer grp commons schemas params from ty
873+ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
875+-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
876+-- will produce @NULL@s if one of their arguments is @NULL@.
878+-- >>> printSQL $ true .== null_
880+(.==) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
881+(.==) = unsafeBinaryOp "="
884+-- | >>> printSQL $ true ./= null_
886+(./=) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
887+(./=) = unsafeBinaryOp "<>"
890+-- | >>> printSQL $ true .>= null_
892+(.>=) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
893+(.>=) = unsafeBinaryOp ">="
896+-- | >>> printSQL $ true .< null_
898+(.<) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
899+(.<) = unsafeBinaryOp "<"
902+-- | >>> printSQL $ true .<= null_
424− (@>) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
904+(.<=) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
425− (@>) = unsafeBinaryOp "@>"
905+(.<=) = unsafeBinaryOp "<="
908+-- | >>> printSQL $ true .> null_
426− (<@) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
910+(.>) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
427− (<@) = unsafeBinaryOp "<@"
911+(.>) = unsafeBinaryOp ">"
428−instance PGSubset 'PGjsonb
912+infix 4 .>
429−instance PGSubset 'PGtsquery
913+
430−instance PGSubset ('PGvararray ty)
914+{- | between
431−instance PGSubset ('PGrange ty)
915+>>> printSQL $ true `between` (null_, false)
916+TRUE BETWEEN NULL AND FALSE
919+ :: Expression outer grp commons schemas params from (null ty)
920+ -> ( Expression outer grp commons schemas params from (null ty)
921+ , Expression outer grp commons schemas params from (null ty) ) -- ^ bounds
922+ -> Condition outer grp commons schemas params from
923+between a (x,y) = UnsafeExpression $ renderSQL a <+> "BETWEEN"
924+ <+> renderSQL x <+> "AND" <+> renderSQL y
433−class PGIntersect ty where
926+{- | not between
927+>>> printSQL $ true `notBetween` (null_, false)
928+TRUE NOT BETWEEN NULL AND FALSE
931+ :: Expression outer grp commons schemas params from (null ty)
932+ -> ( Expression outer grp commons schemas params from (null ty)
933+ , Expression outer grp commons schemas params from (null ty) ) -- ^ bounds
934+ -> Condition outer grp commons schemas params from
935+notBetween a (x,y) = UnsafeExpression $ renderSQL a <+> "NOT BETWEEN"
936+ <+> renderSQL x <+> "AND" <+> renderSQL y
938+{- | between, after sorting the comparison values
939+>>> printSQL $ true `betweenSymmetric` (null_, false)
940+TRUE BETWEEN SYMMETRIC NULL AND FALSE
943+ :: Expression outer grp commons schemas params from (null ty)
944+ -> ( Expression outer grp commons schemas params from (null ty)
945+ , Expression outer grp commons schemas params from (null ty) ) -- ^ bounds
946+ -> Condition outer grp commons schemas params from
947+betweenSymmetric a (x,y) = UnsafeExpression $ renderSQL a
948+ <+> "BETWEEN SYMMETRIC" <+> renderSQL x <+> "AND" <+> renderSQL y
950+{- | not between, after sorting the comparison values
951+>>> printSQL $ true `notBetweenSymmetric` (null_, false)
952+TRUE NOT BETWEEN SYMMETRIC NULL AND FALSE
955+ :: Expression outer grp commons schemas params from (null ty)
956+ -> ( Expression outer grp commons schemas params from (null ty)
957+ , Expression outer grp commons schemas params from (null ty) ) -- ^ bounds
958+ -> Condition outer grp commons schemas params from
959+notBetweenSymmetric a (x,y) = UnsafeExpression $ renderSQL a
960+ <+> "NOT BETWEEN SYMMETRIC" <+> renderSQL x <+> "AND" <+> renderSQL y
962+{- | not equal, treating null like an ordinary value
963+>>> printSQL $ true `isDistinctFrom` null_
964+(TRUE IS DISTINCT FROM NULL)
434− (@&&) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
966+isDistinctFrom :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
967+isDistinctFrom = unsafeBinaryOp "IS DISTINCT FROM"
969+{- | equal, treating null like an ordinary value
970+>>> printSQL $ true `isNotDistinctFrom` null_
971+(TRUE IS NOT DISTINCT FROM NULL)
973+isNotDistinctFrom :: Operator (null0 ty) (null1 ty) ('NotNull 'PGbool)
435− (@&&) = unsafeBinaryOp "&&"
974+isNotDistinctFrom = unsafeBinaryOp "IS NOT DISTINCT FROM"
436−instance PGIntersect ('PGvararray ty)
975+
437−instance PGIntersect ('PGrange ty)
976+{- | is true
977+>>> printSQL $ true & isTrue
980+isTrue :: null0 'PGbool :--> null1 'PGbool
981+isTrue = unsafeUnaryOpR "IS TRUE"
983+{- | is false or unknown
984+>>> printSQL $ true & isNotTrue
987+isNotTrue :: null0 'PGbool :--> null1 'PGbool
988+isNotTrue = unsafeUnaryOpR "IS NOT TRUE"
991+>>> printSQL $ true & isFalse
994+isFalse :: null0 'PGbool :--> null1 'PGbool
995+isFalse = unsafeUnaryOpR "IS FALSE"
997+{- | is true or unknown
998+>>> printSQL $ true & isNotFalse
1001+isNotFalse :: null0 'PGbool :--> null1 'PGbool
1002+isNotFalse = unsafeUnaryOpR "IS NOT FALSE"
1005+>>> printSQL $ true & isUnknown
1008+isUnknown :: null0 'PGbool :--> null1 'PGbool
1009+isUnknown = unsafeUnaryOpR "IS UNKNOWN"
1011+{- | is true or false
1012+>>> printSQL $ true & isNotUnknown
1013+(TRUE IS NOT UNKNOWN)
1015+isNotUnknown :: null0 'PGbool :--> null1 'PGbool
1016+isNotUnknown = unsafeUnaryOpR "IS NOT UNKNOWN"
1018+-- | >>> printSQL currentDate
1020+currentDate :: Expr (null 'PGdate)
1021+currentDate = UnsafeExpression "CURRENT_DATE"
1023+-- | >>> printSQL currentTime
1025+currentTime :: Expr (null 'PGtimetz)
1026+currentTime = UnsafeExpression "CURRENT_TIME"
1028+-- | >>> printSQL currentTimestamp
1029+-- CURRENT_TIMESTAMP
1030+currentTimestamp :: Expr (null 'PGtimestamptz)
1031+currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
1033+-- | >>> printSQL localTime
1035+localTime :: Expr (null 'PGtime)
1036+localTime = UnsafeExpression "LOCALTIME"
1038+-- | >>> printSQL localTimestamp
1040+localTimestamp :: Expr (null 'PGtimestamp)
1041+localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
1043+{-----------------------------------------
1045+-----------------------------------------}
1047+escape :: Char -> String
439instance IsString
1059instance IsString
440− (Expression outer commons grp schemas params from (null 'PGtext)) where
1060+ (Expression outer grp commons schemas params from (null 'PGtext)) where
441 fromString str = UnsafeExpression $
1061 fromString str = UnsafeExpression $
442 "E\'" <> fromString (escape =<< str) <> "\'"
1062 "E\'" <> fromString (escape =<< str) <> "\'"
443instance IsString
1063instance IsString
444− (Expression outer commons grp schemas params from (null 'PGtsvector)) where
1064+ (Expression outer grp commons schemas params from (null 'PGtsvector)) where
445− fromString str = UnsafeExpression . parenthesized . (<> " :: tsvector") $
1065+ fromString str = cast tsvector . UnsafeExpression $
446 "E\'" <> fromString (escape =<< str) <> "\'"
1066 "E\'" <> fromString (escape =<< str) <> "\'"
447instance IsString
1067instance IsString
448− (Expression outer commons grp schemas params from (null 'PGtsquery)) where
1068+ (Expression outer grp commons schemas params from (null 'PGtsquery)) where
449− fromString str = UnsafeExpression . parenthesized . (<> " :: tsquery") $
1069+ fromString str = cast tsquery . UnsafeExpression $
450 "E\'" <> fromString (escape =<< str) <> "\'"
1070 "E\'" <> fromString (escape =<< str) <> "\'"
452instance Semigroup
1072instance Semigroup
453− (Expression outer commons grp schemas params from (null ('PGvararray ty))) where
1073+ (Expression outer grp commons schemas params from (null 'PGtext)) where
454 (<>) = unsafeBinaryOp "||"
1074 (<>) = unsafeBinaryOp "||"
455instance Semigroup
1075instance Semigroup
456− (Expression outer commons grp schemas params from (null 'PGjsonb)) where
457− (<>) = unsafeBinaryOp "||"
459− (Expression outer commons grp schemas params from (null 'PGtext)) where
460− (<>) = unsafeBinaryOp "||"
462− (Expression outer commons grp schemas params from (null 'PGtsvector)) where
1076+ (Expression outer grp commons schemas params from (null 'PGtsvector)) where
463 (<>) = unsafeBinaryOp "||"
1077 (<>) = unsafeBinaryOp "||"
465instance Monoid
1079instance Monoid
466− (Expression outer commons grp schemas params from (null 'PGtext)) where
1080+ (Expression outer grp commons schemas params from (null 'PGtext)) where
467 mempty = fromString ""
1081 mempty = fromString ""
468 mappend = (<>)
1082 mappend = (<>)
469instance Monoid
1083instance Monoid
470− (Expression outer commons grp schemas params from (null 'PGtsvector)) where
1084+ (Expression outer grp commons schemas params from (null 'PGtsvector)) where
471 mempty = fromString ""
1085 mempty = fromString ""
472 mappend = (<>)
1086 mappend = (<>)
1088+-- | >>> printSQL $ lower "ARRRGGG"
1089+-- lower(E'ARRRGGG')
1090+lower :: null 'PGtext :--> null 'PGtext
1091+lower = unsafeFunction "lower"
1093+-- | >>> printSQL $ upper "eeee"
1095+upper :: null 'PGtext :--> null 'PGtext
1096+upper = unsafeFunction "upper"
1098+-- | >>> printSQL $ charLength "four"
1099+-- char_length(E'four')
1100+charLength :: null 'PGtext :--> null 'PGint4
1101+charLength = unsafeFunction "char_length"
1103+-- | The `like` expression returns true if the @string@ matches
1104+-- the supplied @pattern@. If @pattern@ does not contain percent signs
1105+-- or underscores, then the pattern only represents the string itself;
1106+-- in that case `like` acts like the equals operator. An underscore (_)
1107+-- in pattern stands for (matches) any single character; a percent sign (%)
1108+-- matches any sequence of zero or more characters.
1110+-- >>> printSQL $ "abc" `like` "a%"
1111+-- (E'abc' LIKE E'a%')
1112+like :: Operator (null 'PGtext) (null 'PGtext) ('Null 'PGbool)
1113+like = unsafeBinaryOp "LIKE"
1115+-- | The key word ILIKE can be used instead of LIKE to make the
1116+-- match case-insensitive according to the active locale.
1118+-- >>> printSQL $ "abc" `ilike` "a%"
1119+-- (E'abc' ILIKE E'a%')
1120+ilike :: Operator (null 'PGtext) (null 'PGtext) ('Null 'PGbool)
1121+ilike = unsafeBinaryOp "ILIKE"
1123+{-----------------------------------------
1124+ -- json and jsonb support
1126+See https://www.postgresql.org/docs/10/static/functions-json.html -- most
1127+comments lifted directly from this page.
1129+Table 9.44: json and jsonb operators
1130+-----------------------------------------}
1132+-- | Get JSON value (object field or array element) at a key.
1134+ :: (json `In` PGJsonType, key `In` PGJsonKey)
1135+ => Operator (null json) (null key) ('Null json)
1137+(.->) = unsafeBinaryOp "->"
1139+-- | Get JSON value (object field or array element) at a key, as text.
1141+ :: (json `In` PGJsonType, key `In` PGJsonKey)
1142+ => Operator (null json) (null key) ('Null 'PGtext)
1144+(.->>) = unsafeBinaryOp "->>"
1146+-- | Get JSON value at a specified path.
1148+ :: (json `In` PGJsonType, PGTextArray "(.#>)" path)
1149+ => Operator (null json) (null path) ('Null json)
1151+(.#>) = unsafeBinaryOp "#>"
1153+-- | Get JSON value at a specified path as text.
1155+ :: (json `In` PGJsonType, PGTextArray "(.#>>)" path)
1156+ => Operator (null json) (null path) ('Null 'PGtext)
1158+(.#>>) = unsafeBinaryOp "#>>"
1160+-- Additional jsonb operators
1162+class PGSubset container where
1163+ (@>) :: Operator (null0 container) (null1 container) ('Null 'PGbool)
1164+ (@>) = unsafeBinaryOp "@>"
1165+ (<@) :: Operator (null0 container) (null1 container) ('Null 'PGbool)
1166+ (<@) = unsafeBinaryOp "<@"
1167+instance PGSubset 'PGjsonb
1168+instance PGSubset 'PGtsquery
1169+instance PGSubset ('PGvararray ty)
1171+-- | Does the string exist as a top-level key within the JSON value?
1172+(.?) :: Operator (null 'PGjsonb) (null 'PGtext) ('Null 'PGbool)
1174+(.?) = unsafeBinaryOp "?"
1176+-- | Do any of these array strings exist as top-level keys?
1179+ (null ('PGvararray ('NotNull 'PGtext)))
1182+(.?|) = unsafeBinaryOp "?|"
1184+-- | Do all of these array strings exist as top-level keys?
1187+ (null ('PGvararray ('NotNull 'PGtext)))
1190+(.?&) = unsafeBinaryOp "?&"
1192+-- | Concatenate two jsonb values into a new jsonb value.
1194+ (Expression outer grp commons schemas params from (null 'PGjsonb)) where
1195+ (<>) = unsafeBinaryOp "||"
1197+-- | Delete a key or keys from a JSON object, or remove an array element.
1199+-- If the right operand is..
1201+-- @ text @: Delete key/value pair or string element from left operand. Key/value pairs
1202+-- are matched based on their key value.
1204+-- @ text[] @: Delete multiple key/value pairs or string elements
1205+-- from left operand. Key/value pairs are matched based on their key value.
1207+-- @ integer @: Delete the array element with specified index (Negative integers
1208+-- count from the end). Throws an error if top level container is not an array.
1210+ :: (key `In` '[ 'PGtext, 'PGvararray ('NotNull 'PGtext), 'PGint4, 'PGint2 ]) -- hlint error without parens here
1211+ => Operator (null 'PGjsonb) (null key) (null 'PGjsonb)
1213+(.-.) = unsafeBinaryOp "-"
1215+-- | Delete the field or element with specified path (for JSON arrays, negative
1216+-- integers count from the end)
1218+ :: PGTextArray "(#-.)" arrayty
1219+ => Operator (null 'PGjsonb) (null arrayty) (null 'PGjsonb)
1221+(#-.) = unsafeBinaryOp "#-"
1223+{-----------------------------------------
1224+Table 9.45: JSON creation functions
1225+-----------------------------------------}
1227+-- | Literal binary JSON
1228+jsonbLit :: JSON.ToJSON x => x -> Expr (null 'PGjsonb)
1229+jsonbLit = cast jsonb . UnsafeExpression
1230+ . singleQuotedUtf8 . toStrict . JSON.encode
1233+jsonLit :: JSON.ToJSON x => x -> Expr (null 'PGjson)
1234+jsonLit = cast json . UnsafeExpression
1235+ . singleQuotedUtf8 . toStrict . JSON.encode
1237+-- | Returns the value as json. Arrays and composites are converted
1238+-- (recursively) to arrays and objects; otherwise, if there is a cast from the
1239+-- type to json, the cast function will be used to perform the conversion;
1240+-- otherwise, a scalar value is produced. For any scalar type other than a
1241+-- number, a Boolean, or a null value, the text representation will be used, in
1242+-- such a fashion that it is a valid json value.
1243+toJson :: null ty :--> null 'PGjson
1244+toJson = unsafeFunction "to_json"
1246+-- | Returns the value as jsonb. Arrays and composites are converted
1247+-- (recursively) to arrays and objects; otherwise, if there is a cast from the
1248+-- type to json, the cast function will be used to perform the conversion;
1249+-- otherwise, a scalar value is produced. For any scalar type other than a
1250+-- number, a Boolean, or a null value, the text representation will be used, in
1251+-- such a fashion that it is a valid jsonb value.
1252+toJsonb :: null ty :--> null 'PGjsonb
1253+toJsonb = unsafeFunction "to_jsonb"
1255+-- | Returns the array as a JSON array. A PostgreSQL multidimensional array
1256+-- becomes a JSON array of arrays.
1258+ :: PGArray "arrayToJson" arr
1259+ => null arr :--> null 'PGjson
1260+arrayToJson = unsafeFunction "array_to_json"
1262+-- | Returns the row as a JSON object.
1263+rowToJson :: null ('PGcomposite ty) :--> null 'PGjson
1264+rowToJson = unsafeFunction "row_to_json"
1266+-- | Builds a possibly-heterogeneously-typed JSON array out of a variadic
1270+ => NP (Expression outer grp commons schemas params from) elems
1271+ -> Expression outer grp commons schemas params from (null 'PGjson)
1272+jsonBuildArray = unsafeVariadicFunction "json_build_array"
1274+-- | Builds a possibly-heterogeneously-typed (binary) JSON array out of a
1275+-- variadic argument list.
1278+ => NP (Expression outer grp commons schemas params from) elems
1279+ -> Expression outer grp commons schemas params from (null 'PGjsonb)
1280+jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
1283+ :: SOP.SListI elems
1284+ => NP (Aliased (Expression outer grp commons schemas params from)) elems
1287+ (`appEndo` []) . hcfoldMap (Proxy :: SOP.Proxy SOP.Top)
1288+ (\(col `As` name) -> Endo $ \xs ->
1289+ renderAliasString name : renderSQL col : xs)
1291+ renderAliasString :: KnownSymbol alias => Alias alias -> ByteString
1292+ renderAliasString = singleQuotedText . fromString . symbolVal
1294+-- | Builds a possibly-heterogeneously-typed JSON object out of a variadic
1295+-- argument list. The elements of the argument list must alternate between text
1298+ :: SOP.SListI elems
1299+ => NP (Aliased (Expression outer grp commons schemas params from)) elems
1300+ -> Expression outer grp commons schemas params from (null 'PGjson)
1302+ = unsafeFunction "json_build_object"
1305+ . unsafeRowFunction
1307+-- | Builds a possibly-heterogeneously-typed (binary) JSON object out of a
1308+-- variadic argument list. The elements of the argument list must alternate
1309+-- between text and values.
1311+ :: SOP.SListI elems
1312+ => NP (Aliased (Expression outer grp commons schemas params from)) elems
1313+ -> Expression outer grp commons schemas params from (null 'PGjsonb)
1315+ = unsafeFunction "jsonb_build_object"
1318+ . unsafeRowFunction
1320+-- | Builds a JSON object out of a text array. The array must have either
1321+-- exactly one dimension with an even number of members, in which case they are
1322+-- taken as alternating key/value pairs, or two dimensions such that each inner
1323+-- array has exactly two elements, which are taken as a key/value pair.
1325+ :: PGArrayOf "jsonObject" arr ('NotNull 'PGtext)
1326+ => null arr :--> null 'PGjson
1327+jsonObject = unsafeFunction "json_object"
1329+-- | Builds a binary JSON object out of a text array. The array must have either
1330+-- exactly one dimension with an even number of members, in which case they are
1331+-- taken as alternating key/value pairs, or two dimensions such that each inner
1332+-- array has exactly two elements, which are taken as a key/value pair.
1334+ :: PGArrayOf "jsonbObject" arr ('NotNull 'PGtext)
1335+ => null arr :--> null 'PGjsonb
1336+jsonbObject = unsafeFunction "jsonb_object"
1338+-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
1339+-- keys and one for values, that are zipped pairwise to create a JSON object.
1341+ :: ( PGArrayOf "jsonZipObject" keysArray ('NotNull 'PGtext)
1342+ , PGArrayOf "jsonZipObject" valuesArray ('NotNull 'PGtext))
1343+ => Expression outer grp commons schemas params from (null keysArray)
1344+ -> Expression outer grp commons schemas params from (null valuesArray)
1345+ -> Expression outer grp commons schemas params from (null 'PGjson)
1346+jsonZipObject ks vs =
1347+ unsafeVariadicFunction "json_object" (ks :* vs :* Nil)
1349+-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
1350+-- keys and one for values, that are zipped pairwise to create a binary JSON
1353+ :: ( PGArrayOf "jsonbZipObject" keysArray ('NotNull 'PGtext)
1354+ , PGArrayOf "jsonbZipObject" valuesArray ('NotNull 'PGtext))
1355+ => Expression outer grp commons schemas params from (null keysArray)
1356+ -> Expression outer grp commons schemas params from (null valuesArray)
1357+ -> Expression outer grp commons schemas params from (null 'PGjsonb)
1358+jsonbZipObject ks vs =
1359+ unsafeVariadicFunction "jsonb_object" (ks :* vs :* Nil)
1361+{-----------------------------------------
1362+Table 9.46: JSON processing functions
1363+-----------------------------------------}
1365+-- | Returns the number of elements in the outermost JSON array.
1366+jsonArrayLength :: null 'PGjson :--> null 'PGint4
1367+jsonArrayLength = unsafeFunction "json_array_length"
1369+-- | Returns the number of elements in the outermost binary JSON array.
1370+jsonbArrayLength :: null 'PGjsonb :--> null 'PGint4
1371+jsonbArrayLength = unsafeFunction "jsonb_array_length"
1373+-- | Returns JSON value pointed to by the given path (equivalent to #>
1377+ => Expression outer grp commons schemas params from (null 'PGjson)
1378+ -> NP (Expression outer grp commons schemas params from) elems
1379+ -> Expression outer grp commons schemas params from (null 'PGjsonb)
1380+jsonExtractPath x xs =
1381+ unsafeVariadicFunction "json_extract_path" (x :* xs)
1383+-- | Returns JSON value pointed to by the given path (equivalent to #>
1387+ => Expression outer grp commons schemas params from (null 'PGjsonb)
1388+ -> NP (Expression outer grp commons schemas params from) elems
1389+ -> Expression outer grp commons schemas params from (null 'PGjsonb)
1390+jsonbExtractPath x xs =
1391+ unsafeVariadicFunction "jsonb_extract_path" (x :* xs)
1393+-- | Returns JSON value pointed to by the given path (equivalent to #>
1394+-- operator), as text.
1395+jsonExtractPathAsText
1397+ => Expression outer grp commons schemas params from (null 'PGjson)
1398+ -> NP (Expression outer grp commons schemas params from) elems
1399+ -> Expression outer grp commons schemas params from (null 'PGjson)
1400+jsonExtractPathAsText x xs =
1401+ unsafeVariadicFunction "json_extract_path_text" (x :* xs)
1403+-- | Returns JSON value pointed to by the given path (equivalent to #>
1404+-- operator), as text.
1405+jsonbExtractPathAsText
1407+ => Expression outer grp commons schemas params from (null 'PGjsonb)
1408+ -> NP (Expression outer grp commons schemas params from) elems
1409+ -> Expression outer grp commons schemas params from (null 'PGjsonb)
1410+jsonbExtractPathAsText x xs =
1411+ unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
1413+-- | Returns the type of the outermost JSON value as a text string. Possible
1414+-- types are object, array, string, number, boolean, and null.
1415+jsonTypeof :: null 'PGjson :--> null 'PGtext
1416+jsonTypeof = unsafeFunction "json_typeof"
1418+-- | Returns the type of the outermost binary JSON value as a text string.
1419+-- Possible types are object, array, string, number, boolean, and null.
1420+jsonbTypeof :: null 'PGjsonb :--> null 'PGtext
1421+jsonbTypeof = unsafeFunction "jsonb_typeof"
1423+-- | Returns its argument with all object fields that have null values omitted.
1424+-- Other null values are untouched.
1425+jsonStripNulls :: null 'PGjson :--> null 'PGjson
1426+jsonStripNulls = unsafeFunction "json_strip_nulls"
1428+-- | Returns its argument with all object fields that have null values omitted.
1429+-- Other null values are untouched.
1430+jsonbStripNulls :: null 'PGjsonb :--> null 'PGjsonb
1431+jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
1433+-- | @ jsonbSet target path new_value create_missing @
1435+-- Returns target with the section designated by path replaced by new_value,
1436+-- or with new_value added if create_missing is true ( default is true) and the
1437+-- item designated by path does not exist. As with the path orientated
1438+-- operators, negative integers that appear in path count from the end of JSON
1441+ :: PGTextArray "jsonbSet" arr
1442+ => Expression outer grp commons schemas params from (null 'PGjsonb)
1443+ -> Expression outer grp commons schemas params from (null arr)
1444+ -> Expression outer grp commons schemas params from (null 'PGjsonb)
1445+ -> Maybe (Expression outer grp commons schemas params from (null 'PGbool))
1446+ -> Expression outer grp commons schemas params from (null 'PGjsonb)
1447+jsonbSet tgt path val createMissing = case createMissing of
1448+ Just m -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* m :* Nil)
1449+ Nothing -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* Nil)
1451+-- | @ jsonbInsert target path new_value insert_after @
1453+-- Returns target with new_value inserted. If target section designated by
1454+-- path is in a JSONB array, new_value will be inserted before target or after
1455+-- if insert_after is true (default is false). If target section designated by
1456+-- path is in JSONB object, new_value will be inserted only if target does not
1457+-- exist. As with the path orientated operators, negative integers that appear
1458+-- in path count from the end of JSON arrays.
1460+ :: PGTextArray "jsonbInsert" arr
1461+ => Expression outer grp commons schemas params from (null 'PGjsonb)
1462+ -> Expression outer grp commons schemas params from (null arr)
1463+ -> Expression outer grp commons schemas params from (null 'PGjsonb)
1464+ -> Maybe (Expression outer grp commons schemas params from (null 'PGbool))
1465+ -> Expression outer grp commons schemas params from (null 'PGjsonb)
1466+jsonbInsert tgt path val insertAfter = case insertAfter of
1467+ Just i -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* i :* Nil)
1468+ Nothing -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* Nil)
1470+-- | Returns its argument as indented JSON text.
1471+jsonbPretty :: null 'PGjsonb :--> null 'PGtext
1472+jsonbPretty = unsafeFunction "jsonb_pretty"
1474+(@@) :: Operator (null 'PGtsvector) (null 'PGtsquery) ('Null 'PGbool)
1475+(@@) = unsafeBinaryOp "@@"
1477+(.&) :: Operator (null 'PGtsquery) (null 'PGtsquery) (null 'PGtsquery)
1478+(.&) = unsafeBinaryOp "&&"
1480+(.|) :: Operator (null 'PGtsquery) (null 'PGtsquery) (null 'PGtsquery)
1481+(.|) = unsafeBinaryOp "||"
1483+(.!) :: null 'PGtsquery :--> null 'PGtsquery
1484+(.!) = unsafeUnaryOpL "!!"
1486+(<->) :: Operator (null 'PGtsquery) (null 'PGtsquery) (null 'PGtsquery)
1487+(<->) = unsafeBinaryOp "<->"
1490+ :: null ('PGvararray ('NotNull 'PGtext))
1491+ :--> null 'PGtsvector
1492+arrayToTSvector = unsafeFunction "array_to_tsvector"
1494+tsvectorLength :: null 'PGtsvector :--> null 'PGint4
1495+tsvectorLength = unsafeFunction "length"
1497+numnode :: null 'PGtsquery :--> null 'PGint4
1498+numnode = unsafeFunction "numnode"
1500+plainToTSquery :: null 'PGtext :--> null 'PGtsquery
1501+plainToTSquery = unsafeFunction "plainto_tsquery"
1503+phraseToTSquery :: null 'PGtext :--> null 'PGtsquery
1504+phraseToTSquery = unsafeFunction "phraseto_tsquery"
1506+websearchToTSquery :: null 'PGtext :--> null 'PGtsquery
1507+websearchToTSquery = unsafeFunction "websearch_to_tsquery"
1509+queryTree :: null 'PGtsquery :--> null 'PGtext
1510+queryTree = unsafeFunction "query_tree"
1512+toTSquery :: null 'PGtext :--> null 'PGtsquery
1513+toTSquery = unsafeFunction "to_tsquery"
1516+ :: ty `In` '[ 'PGtext, 'PGjson, 'PGjsonb]
1517+ => null ty :--> null 'PGtsquery
1518+toTSvector = unsafeFunction "to_tsvector"
1521+ :: FunctionHet '[null 'PGtsvector, null ('PGchar 1)] (null 'PGtsvector)
1522+setWeight = unsafeFunctionHet "set_weight"
1524+litChar :: Char -> Expr (null ('PGchar 1))
1525+litChar chr = UnsafeExpression $
1526+ "E\'" <> fromString (escape =<< [chr]) <> "\'"
1528+strip :: null 'PGtsvector :--> null 'PGtsvector
1529+strip = unsafeFunction "strip"
1531+jsonToTSvector :: FunctionHet '[null 'PGjson, null 'PGjson] (null 'PGtsvector)
1532+jsonToTSvector = unsafeFunctionHet "json_to_tsvector"
1534+jsonbToTSvector :: FunctionHet '[null 'PGjsonb, null 'PGjsonb] (null 'PGtsvector)
1535+jsonbToTSvector = unsafeFunctionHet "jsonb_to_tsvector"
1537+tsDelete :: FunctionHet
1538+ '[null 'PGtsvector, null ('PGvararray ('NotNull 'PGtext))]
1540+tsDelete = unsafeFunctionHet "ts_delete"
1542+tsFilter :: FunctionHet
1543+ '[null 'PGtsvector, null ('PGvararray ('NotNull ('PGchar 1)))]
1545+tsFilter = unsafeFunctionHet "ts_filter"
1548+ :: document `In` '[ 'PGtext, 'PGjson, 'PGjsonb]
1549+ => FunctionHet '[null document, null 'PGtsquery] (null 'PGtext)
1550+tsHeadline = unsafeFunctionHet "ts_headline"
1551+{-----------------------------------------
1553+-----------------------------------------}
1556+ = forall outer bys commons schemas params from
1557+ . Distinction (Expression outer 'Ungrouped commons schemas params from) x
1559+ -> Expression outer ('Grouped bys) commons schemas params from y
1563+ = forall outer bys commons schemas params from
1564+ . Distinction (NP (Expression outer 'Ungrouped commons schemas params from)) xs
1566+ -> Expression outer ('Grouped bys) commons schemas params from y
1569+unsafeAggregateHet :: SOP.SListI xs => ByteString -> AggrHet xs y
1570+unsafeAggregateHet fun xs = UnsafeExpression $ mconcat
1571+ [fun, "(", renderSQL xs, ")"]
1574+`Aggregate` functions compute a single result from a set of input values.
1575+`Aggregate` functions can be used as `GroupedBy` `Expression`s as well
1576+as `WindowFunction`s.
1578+class Aggregate expr1 exprHet aggr
1579+ | aggr -> expr1, aggr -> exprHet where
1581+ -- | A special aggregation that does not require an input
1585+ -- expression :: Expression '[] ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
1586+ -- expression = countStar
1587+ -- in printSQL expression
1590+ countStar :: aggr ('NotNull 'PGint8)
1594+ -- expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: null ty]] ('NotNull 'PGint8)
1595+ -- expression = count (All #col)
1596+ -- in printSQL expression
1598+ -- count(ALL "col")
1602+ -> aggr ('NotNull 'PGint8)
1606+ -- expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('NotNull 'PGnumeric)
1607+ -- expression = sum_ (Distinct #col)
1608+ -- in printSQL expression
1610+ -- sum(DISTINCT "col")
1614+ -> aggr ('NotNull ty)
1616+ -- | input values, including nulls, concatenated into an array
1619+ -> aggr ('Null ('PGvararray ty))
1621+ -- | aggregates values as a JSON array
1624+ -> aggr ('Null 'PGjson)
1626+ -- | aggregates values as a JSON array
1629+ -> aggr ('Null 'PGjsonb)
1632+ the bitwise AND of all non-null input values, or null if none
1635+ expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: null 'PGint4]] ('Null 'PGint4)
1636+ expression = bitAnd (Distinct #col)
1637+ in printSQL expression
1639+ bit_and(DISTINCT "col")
1642+ :: int `In` PGIntegral
1643+ => expr1 (null int)
1644+ -- ^ what to aggregate
1645+ -> aggr ('Null int)
1648+ the bitwise OR of all non-null input values, or null if none
1652+ expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: null 'PGint4]] ('Null 'PGint4)
1653+ expression = bitOr (All #col)
1654+ in printSQL expression
1659+ :: int `In` PGIntegral
1660+ => expr1 (null int)
1661+ -- ^ what to aggregate
1662+ -> aggr ('Null int)
1665+ true if all input values are true, otherwise false
1669+ winFun :: WindowFunction '[] 'Ungrouped commons schemas params '[tab ::: '["col" ::: null 'PGbool]] ('Null 'PGbool)
1670+ winFun = boolAnd #col
1676+ :: expr1 (null 'PGbool)
1677+ -- ^ what to aggregate
1678+ -> aggr ('Null 'PGbool)
1681+ true if at least one input value is true, otherwise false
1685+ expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: null 'PGbool]] ('Null 'PGbool)
1686+ expression = boolOr (All #col)
1687+ in printSQL expression
1692+ :: expr1 (null 'PGbool)
1693+ -- ^ what to aggregate
1694+ -> aggr ('Null 'PGbool)
1697+ equivalent to `boolAnd`
1701+ expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: null 'PGbool]] ('Null 'PGbool)
1702+ expression = every (Distinct #col)
1703+ in printSQL expression
1705+ every(DISTINCT "col")
1708+ :: expr1 (null 'PGbool)
1709+ -- ^ what to aggregate
1710+ -> aggr ('Null 'PGbool)
1712+ {- |maximum value of expression across all input values-}
1715+ -- ^ what to maximize
1718+ -- | minimum value of expression across all input values
1721+ -- ^ what to minimize
1724+ -- | the average (arithmetic mean) of all input values
1727+ -- ^ what to average
1728+ -> aggr ('Null (PGAvg ty))
1730+ {- | correlation coefficient
1733+ expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1734+ expression = corr (All (#y *: #x))
1735+ in printSQL expression
1740+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1741+ -> aggr ('Null 'PGfloat8)
1743+ {- | population covariance
1746+ expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1747+ expression = covarPop (All (#y *: #x))
1748+ in printSQL expression
1750+ covar_pop(ALL "y", "x")
1753+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1754+ -> aggr ('Null 'PGfloat8)
1756+ {- | sample covariance
1759+ winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1760+ winFun = covarSamp (#y *: #x)
1763+ covar_samp("y", "x")
1766+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1767+ -> aggr ('Null 'PGfloat8)
1769+ {- | average of the independent variable (sum(X)/N)
1772+ expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1773+ expression = regrAvgX (All (#y *: #x))
1774+ in printSQL expression
1776+ regr_avgx(ALL "y", "x")
1779+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1780+ -> aggr ('Null 'PGfloat8)
1782+ {- | average of the dependent variable (sum(Y)/N)
1785+ winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1786+ winFun = regrAvgY (#y *: #x)
1789+ regr_avgy("y", "x")
1792+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1793+ -> aggr ('Null 'PGfloat8)
1795+ {- | number of input rows in which both expressions are nonnull
1798+ winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGint8)
1799+ winFun = regrCount (#y *: #x)
1802+ regr_count("y", "x")
1805+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1806+ -> aggr ('Null 'PGint8)
1808+ {- | y-intercept of the least-squares-fit linear equation determined by the (X, Y) pairs
1811+ expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1812+ expression = regrIntercept (All (#y *: #x))
1813+ in printSQL expression
1815+ regr_intercept(ALL "y", "x")
1818+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1819+ -> aggr ('Null 'PGfloat8)
1821+ -- | @regr_r2(Y, X)@, square of the correlation coefficient
1823+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1824+ -> aggr ('Null 'PGfloat8)
1826+ -- | @regr_slope(Y, X)@, slope of the least-squares-fit linear equation
1827+ -- determined by the (X, Y) pairs
1829+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1830+ -> aggr ('Null 'PGfloat8)
1832+ -- | @regr_sxx(Y, X)@, sum(X^2) - sum(X)^2/N
1833+ -- (“sum of squares” of the independent variable)
1835+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1836+ -> aggr ('Null 'PGfloat8)
1838+ -- | @regr_sxy(Y, X)@, sum(X*Y) - sum(X) * sum(Y)/N
1839+ -- (“sum of products” of independent times dependent variable)
1841+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1842+ -> aggr ('Null 'PGfloat8)
1844+ -- | @regr_syy(Y, X)@, sum(Y^2) - sum(Y)^2/N
1845+ -- (“sum of squares” of the dependent variable)
1847+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1848+ -> aggr ('Null 'PGfloat8)
1850+ -- | historical alias for `stddevSamp`
1853+ -> aggr ('Null (PGAvg ty))
1855+ -- | population standard deviation of the input values
1858+ -> aggr ('Null (PGAvg ty))
1860+ -- | sample standard deviation of the input values
1863+ -> aggr ('Null (PGAvg ty))
1865+ -- | historical alias for `varSamp`
1868+ -> aggr ('Null (PGAvg ty))
1870+ -- | population variance of the input values
1871+ -- (square of the population standard deviation)
1874+ -> aggr ('Null (PGAvg ty))
1876+ -- | sample variance of the input values
1877+ -- (square of the sample standard deviation)
1880+ -> aggr ('Null (PGAvg ty))
1883+`Distinction`s are used for the input of `Aggregate` `Expression`s.
1884+`All` invokes the aggregate once for each input row.
1885+`Distinct` invokes the aggregate once for each distinct value of the expression
1886+(or distinct set of values, for multiple expressions) found in the input
1888+data Distinction (expr :: kind -> Type) (ty :: kind)
1890+ | Distinct (expr ty)
1891+ deriving (GHC.Generic,Show,Eq,Ord)
1892+instance NFData (Distinction (Expression outer grp commons schemas params from) ty)
1893+instance RenderSQL (Distinction (Expression outer grp commons schemas params from) ty) where
1895+ All x -> "ALL" <+> renderSQL x
1896+ Distinct x -> "DISTINCT" <+> renderSQL x
1897+instance SOP.SListI tys => RenderSQL
1898+ (Distinction (NP (Expression outer grp commons schemas params from)) tys) where
1900+ All xs -> "ALL" <+> renderCommaSeparated renderSQL xs
1901+ Distinct xs -> "DISTINCT" <+> renderCommaSeparated renderSQL xs
1904+ (Distinction (Expression outer 'Ungrouped commons schemas params from))
1905+ (Distinction (NP (Expression outer 'Ungrouped commons schemas params from)))
1906+ (Expression outer ('Grouped bys) commons schemas params from) where
1907+ countStar = UnsafeExpression "count(*)"
1908+ count = unsafeAggregate1 "count"
1909+ sum_ = unsafeAggregate1 "sum"
1910+ arrayAgg = unsafeAggregate1 "array_agg"
1911+ jsonAgg = unsafeAggregate1 "json_agg"
1912+ jsonbAgg = unsafeAggregate1 "jsonb_agg"
1913+ bitAnd = unsafeAggregate1 "bit_and"
1914+ bitOr = unsafeAggregate1 "bit_or"
1915+ boolAnd = unsafeAggregate1 "bool_and"
1916+ boolOr = unsafeAggregate1 "bool_or"
1917+ every = unsafeAggregate1 "every"
1918+ max_ = unsafeAggregate1 "max"
1919+ min_ = unsafeAggregate1 "min"
1920+ avg = unsafeAggregate1 "avg"
1921+ corr = unsafeAggregateHet "corr"
1922+ covarPop = unsafeAggregateHet "covar_pop"
1923+ covarSamp = unsafeAggregateHet "covar_samp"
1924+ regrAvgX = unsafeAggregateHet "regr_avgx"
1925+ regrAvgY = unsafeAggregateHet "regr_avgy"
1926+ regrCount = unsafeAggregateHet "regr_count"
1927+ regrIntercept = unsafeAggregateHet "regr_intercept"
1928+ regrR2 = unsafeAggregateHet "regr_r2"
1929+ regrSlope = unsafeAggregateHet "regr_slope"
1930+ regrSxx = unsafeAggregateHet "regr_sxx"
1931+ regrSxy = unsafeAggregateHet "regr_sxy"
1932+ regrSyy = unsafeAggregateHet "regr_syy"
1933+ stddev = unsafeAggregate1 "stddev"
1934+ stddevPop = unsafeAggregate1 "stddev_pop"
1935+ stddevSamp = unsafeAggregate1 "stddev_samp"
1936+ variance = unsafeAggregate1 "variance"
1937+ varPop = unsafeAggregate1 "var_pop"
1938+ varSamp = unsafeAggregate1 "var_samp"
1940+ (Expression outer grp commons schemas params from)
1941+ (NP (Expression outer grp commons schemas params from))
1942+ (WindowFunction outer grp commons schemas params from) where
1943+ countStar = UnsafeWindowFunction "count(*)"
1944+ count = unsafeWindowFunction1 "count"
1945+ sum_ = unsafeWindowFunction1 "sum"
1946+ arrayAgg = unsafeWindowFunction1 "array_agg"
1947+ jsonAgg = unsafeWindowFunction1 "json_agg"
1948+ jsonbAgg = unsafeWindowFunction1 "jsonb_agg"
1949+ bitAnd = unsafeWindowFunction1 "bit_and"
1950+ bitOr = unsafeWindowFunction1 "bit_or"
1951+ boolAnd = unsafeWindowFunction1 "bool_and"
1952+ boolOr = unsafeWindowFunction1 "bool_or"
1953+ every = unsafeWindowFunction1 "every"
1954+ max_ = unsafeWindowFunction1 "max"
1955+ min_ = unsafeWindowFunction1 "min"
1956+ avg = unsafeWindowFunction1 "avg"
1957+ corr = unsafeWindowFunctionHet "corr"
1958+ covarPop = unsafeWindowFunctionHet "covar_pop"
1959+ covarSamp = unsafeWindowFunctionHet "covar_samp"
1960+ regrAvgX = unsafeWindowFunctionHet "regr_avgx"
1961+ regrAvgY = unsafeWindowFunctionHet "regr_avgy"
1962+ regrCount = unsafeWindowFunctionHet "regr_count"
1963+ regrIntercept = unsafeWindowFunctionHet "regr_intercept"
1964+ regrR2 = unsafeWindowFunctionHet "regr_r2"
1965+ regrSlope = unsafeWindowFunctionHet "regr_slope"
1966+ regrSxx = unsafeWindowFunctionHet "regr_sxx"
1967+ regrSxy = unsafeWindowFunctionHet "regr_sxy"
1968+ regrSyy = unsafeWindowFunctionHet "regr_syy"
1969+ stddev = unsafeWindowFunction1 "stddev"
1970+ stddevPop = unsafeWindowFunction1 "stddev_pop"
1971+ stddevSamp = unsafeWindowFunction1 "stddev_samp"
1972+ variance = unsafeWindowFunction1 "variance"
1973+ varPop = unsafeWindowFunction1 "var_pop"
1974+ varSamp = unsafeWindowFunction1 "var_samp"
1976+-- | escape hatch to define aggregate functions
1978+ :: ByteString -- ^ aggregate function
1980+unsafeAggregate1 fun x = UnsafeExpression $ mconcat
1981+ [fun, "(", renderSQL x, ")"]
1983+-- | A type family that calculates `PGAvg` type of a `PGType`.
1984+type family PGAvg ty where
1985+ PGAvg 'PGint2 = 'PGnumeric
1986+ PGAvg 'PGint4 = 'PGnumeric
1987+ PGAvg 'PGint8 = 'PGnumeric
1988+ PGAvg 'PGnumeric = 'PGnumeric
1989+ PGAvg 'PGfloat4 = 'PGfloat8
1990+ PGAvg 'PGfloat8 = 'PGfloat8
1991+ PGAvg 'PGinterval = 'PGinterval
1992+ PGAvg pg = TypeError
1993+ ('Text "Squeal type error: No average for " ':<>: 'ShowType pg)
1995+{-----------------------------------------
1997+-----------------------------------------}
1999+-- | `TypeExpression`s are used in `cast`s and
2000+-- `Squeal.PostgreSQL.Definition.createTable` commands.
2001+newtype TypeExpression (schemas :: SchemasType) (ty :: NullityType)
2002+ = UnsafeTypeExpression { renderTypeExpression :: ByteString }
2003+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
2004+instance RenderSQL (TypeExpression schemas ty) where
2005+ renderSQL = renderTypeExpression
2007+-- | The enum or composite type in a `Typedef` can be expressed by its alias.
2009+ :: (Has sch schemas schema, Has td schema ('Typedef ty))
2010+ => QualifiedAlias sch td
2011+ -> TypeExpression schemas (null ty)
2012+typedef = UnsafeTypeExpression . renderSQL
2014+-- | The composite type corresponding to a `Table` definition can be expressed
2017+ :: (Has sch schemas schema, Has tab schema ('Table table))
2018+ => QualifiedAlias sch tab
2019+ -> TypeExpression schemas (null ('PGcomposite (TableToRow table)))
2020+typetable = UnsafeTypeExpression . renderSQL
2022+-- | The composite type corresponding to a `View` definition can be expressed
2025+ :: (Has sch schemas schema, Has vw schema ('View view))
2026+ => QualifiedAlias sch vw
2027+ -> TypeExpression schemas (null ('PGcomposite view))
2028+typeview = UnsafeTypeExpression . renderSQL
2030+-- | logical Boolean (true/false)
2031+bool :: TypeExpression schemas (null 'PGbool)
2032+bool = UnsafeTypeExpression "bool"
2033+-- | signed two-byte integer
2034+int2, smallint :: TypeExpression schemas (null 'PGint2)
2035+int2 = UnsafeTypeExpression "int2"
2036+smallint = UnsafeTypeExpression "smallint"
2037+-- | signed four-byte integer
2038+int4, int, integer :: TypeExpression schemas (null 'PGint4)
2039+int4 = UnsafeTypeExpression "int4"
2040+int = UnsafeTypeExpression "int"
2041+integer = UnsafeTypeExpression "integer"
2042+-- | signed eight-byte integer
2043+int8, bigint :: TypeExpression schemas (null 'PGint8)
2044+int8 = UnsafeTypeExpression "int8"
2045+bigint = UnsafeTypeExpression "bigint"
2046+-- | arbitrary precision numeric type
2047+numeric :: TypeExpression schemas (null 'PGnumeric)
2048+numeric = UnsafeTypeExpression "numeric"
2049+-- | single precision floating-point number (4 bytes)
2050+float4, real :: TypeExpression schemas (null 'PGfloat4)
2051+float4 = UnsafeTypeExpression "float4"
2052+real = UnsafeTypeExpression "real"
2053+-- | double precision floating-point number (8 bytes)
2054+float8, doublePrecision :: TypeExpression schemas (null 'PGfloat8)
2055+float8 = UnsafeTypeExpression "float8"
2056+doublePrecision = UnsafeTypeExpression "double precision"
2057+-- | currency amount
2058+money :: TypeExpression schema (null 'PGmoney)
2059+money = UnsafeTypeExpression "money"
2060+-- | variable-length character string
2061+text :: TypeExpression schemas (null 'PGtext)
2062+text = UnsafeTypeExpression "text"
2063+-- | fixed-length character string
2065+ :: forall n schemas null. (KnownNat n, 1 <= n)
2066+ => TypeExpression schemas (null ('PGchar n))
2067+char = UnsafeTypeExpression $ "char(" <> renderNat @n <> ")"
2068+character = UnsafeTypeExpression $ "character(" <> renderNat @n <> ")"
2069+-- | variable-length character string
2070+varchar, characterVarying
2071+ :: forall n schemas null. (KnownNat n, 1 <= n)
2072+ => TypeExpression schemas (null ('PGvarchar n))
2073+varchar = UnsafeTypeExpression $ "varchar(" <> renderNat @n <> ")"
2074+characterVarying = UnsafeTypeExpression $
2075+ "character varying(" <> renderNat @n <> ")"
2076+-- | binary data ("byte array")
2077+bytea :: TypeExpression schemas (null 'PGbytea)
2078+bytea = UnsafeTypeExpression "bytea"
2079+-- | date and time (no time zone)
2080+timestamp :: TypeExpression schemas (null 'PGtimestamp)
2081+timestamp = UnsafeTypeExpression "timestamp"
2082+-- | date and time, including time zone
2083+timestampWithTimeZone :: TypeExpression schemas (null 'PGtimestamptz)
2084+timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
2085+-- | calendar date (year, month, day)
2086+date :: TypeExpression schemas (null 'PGdate)
2087+date = UnsafeTypeExpression "date"
2088+-- | time of day (no time zone)
2089+time :: TypeExpression schemas (null 'PGtime)
2090+time = UnsafeTypeExpression "time"
2091+-- | time of day, including time zone
2092+timeWithTimeZone :: TypeExpression schemas (null 'PGtimetz)
2093+timeWithTimeZone = UnsafeTypeExpression "time with time zone"
2095+interval :: TypeExpression schemas (null 'PGinterval)
2096+interval = UnsafeTypeExpression "interval"
2097+-- | universally unique identifier
2098+uuid :: TypeExpression schemas (null 'PGuuid)
2099+uuid = UnsafeTypeExpression "uuid"
2100+-- | IPv4 or IPv6 host address
2101+inet :: TypeExpression schemas (null 'PGinet)
2102+inet = UnsafeTypeExpression "inet"
2103+-- | textual JSON data
2104+json :: TypeExpression schemas (null 'PGjson)
2105+json = UnsafeTypeExpression "json"
2106+-- | binary JSON data, decomposed
2107+jsonb :: TypeExpression schemas (null 'PGjsonb)
2108+jsonb = UnsafeTypeExpression "jsonb"
2109+-- | variable length array
2111+ :: TypeExpression schemas pg
2112+ -> TypeExpression schemas (null ('PGvararray pg))
2113+vararray ty = UnsafeTypeExpression $ renderSQL ty <> "[]"
2114+-- | fixed length array
2116+-- >>> renderSQL (fixarray @'[2] json)
2119+ :: forall dims schemas null pg. SOP.All KnownNat dims
2120+ => TypeExpression schemas pg
2121+ -> TypeExpression schemas (null ('PGfixarray dims pg))
2122+fixarray ty = UnsafeTypeExpression $
2123+ renderSQL ty <> renderDims @dims
2125+ renderDims :: forall ns. SOP.All KnownNat ns => ByteString
2129+ . ByteString.intercalate "]["
2131+ $ hcmap (SOP.Proxy @KnownNat)
2132+ (K . fromString . show . natVal)
2133+ (hpure SOP.Proxy :: NP SOP.Proxy ns)
2134+tsvector :: TypeExpression schemas (null 'PGtsvector)
2135+tsvector = UnsafeTypeExpression "tsvector"
2136+tsquery :: TypeExpression schemas (null 'PGtsquery)
2137+tsquery = UnsafeTypeExpression "tsquery"
2139+-- | `pgtype` is a demoted version of a `PGType`
2140+class PGTyped schemas (ty :: NullityType) where
2141+ pgtype :: TypeExpression schemas ty
2142+instance PGTyped schemas (null 'PGbool) where pgtype = bool
2143+instance PGTyped schemas (null 'PGint2) where pgtype = int2
2144+instance PGTyped schemas (null 'PGint4) where pgtype = int4
2145+instance PGTyped schemas (null 'PGint8) where pgtype = int8
2146+instance PGTyped schemas (null 'PGnumeric) where pgtype = numeric
2147+instance PGTyped schemas (null 'PGfloat4) where pgtype = float4
2148+instance PGTyped schemas (null 'PGfloat8) where pgtype = float8
2149+instance PGTyped schemas (null 'PGmoney) where pgtype = money
2150+instance PGTyped schemas (null 'PGtext) where pgtype = text
2151+instance (KnownNat n, 1 <= n)
2152+ => PGTyped schemas (null ('PGchar n)) where pgtype = char @n
2153+instance (KnownNat n, 1 <= n)
2154+ => PGTyped schemas (null ('PGvarchar n)) where pgtype = varchar @n
2155+instance PGTyped schemas (null 'PGbytea) where pgtype = bytea
2156+instance PGTyped schemas (null 'PGtimestamp) where pgtype = timestamp
2157+instance PGTyped schemas (null 'PGtimestamptz) where pgtype = timestampWithTimeZone
2158+instance PGTyped schemas (null 'PGdate) where pgtype = date
2159+instance PGTyped schemas (null 'PGtime) where pgtype = time
2160+instance PGTyped schemas (null 'PGtimetz) where pgtype = timeWithTimeZone
2161+instance PGTyped schemas (null 'PGinterval) where pgtype = interval
2162+instance PGTyped schemas (null 'PGuuid) where pgtype = uuid
2163+instance PGTyped schemas (null 'PGjson) where pgtype = json
2164+instance PGTyped schemas (null 'PGjsonb) where pgtype = jsonb
2165+instance PGTyped schemas ty
2166+ => PGTyped schemas (null ('PGvararray ty)) where
2167+ pgtype = vararray (pgtype @schemas @ty)
2168+instance (SOP.All KnownNat dims, PGTyped schemas ty)
2169+ => PGTyped schemas (null ('PGfixarray dims ty)) where
2170+ pgtype = fixarray @dims (pgtype @schemas @ty)
2171+instance PGTyped schemas (null 'PGtsvector) where pgtype = tsvector
2172+instance PGTyped schemas (null 'PGtsquery) where pgtype = tsquery
2174+{-----------------------------------------
2176+-----------------------------------------}
2178+-- | `SortExpression`s are used by `orderBy` to optionally sort the results
2179+-- of a `Squeal.PostgreSQL.Query.Query`. `Asc` or `Desc`
2180+-- set the sort direction of a `NotNull` result
2181+-- column to ascending or descending. Ascending order puts smaller values
2182+-- first, where "smaller" is defined in terms of the `.<` operator. Similarly,
2183+-- descending order is determined with the `.>` operator. `AscNullsFirst`,
2184+-- `AscNullsLast`, `DescNullsFirst` and `DescNullsLast` options are used to
2185+-- determine whether nulls appear before or after non-null values in the sort
2186+-- ordering of a `Null` result column.
2187+data SortExpression outer grp commons schemas params from where
2189+ :: Expression outer grp commons schemas params from ('NotNull ty)
2190+ -> SortExpression outer grp commons schemas params from
2192+ :: Expression outer grp commons schemas params from ('NotNull ty)
2193+ -> SortExpression outer grp commons schemas params from
2195+ :: Expression outer grp commons schemas params from ('Null ty)
2196+ -> SortExpression outer grp commons schemas params from
2198+ :: Expression outer grp commons schemas params from ('Null ty)
2199+ -> SortExpression outer grp commons schemas params from
2201+ :: Expression outer grp commons schemas params from ('Null ty)
2202+ -> SortExpression outer grp commons schemas params from
2204+ :: Expression outer grp commons schemas params from ('Null ty)
2205+ -> SortExpression outer grp commons schemas params from
2206+deriving instance Show (SortExpression outer grp commons schemas params from)
2209+The `orderBy` clause causes the result rows of a `Squeal.PostgreSQL.Query.TableExpression`
2210+to be sorted according to the specified `SortExpression`(s).
2211+If two rows are equal according to the leftmost expression,
2212+they are compared according to the next expression and so on.
2213+If they are equal according to all specified expressions,
2214+they are returned in an implementation-dependent order.
2216+You can also control the order in which rows are processed by window functions
2217+using `orderBy` within `Squeal.PostgreSQL.Query.Over`.
2219+class OrderBy expr where
2221+ :: [SortExpression outer grp commons schemas params from]
2222+ -> expr outer grp commons schemas params from
2223+ -> expr outer grp commons schemas params from
2225+-- | Render a `SortExpression`.
2226+instance RenderSQL (SortExpression outer grp commons schemas params from) where
2228+ Asc expression -> renderSQL expression <+> "ASC"
2229+ Desc expression -> renderSQL expression <+> "DESC"
2230+ AscNullsFirst expression -> renderSQL expression
2231+ <+> "ASC NULLS FIRST"
2232+ DescNullsFirst expression -> renderSQL expression
2233+ <+> "DESC NULLS FIRST"
2234+ AscNullsLast expression -> renderSQL expression <+> "ASC NULLS LAST"
2235+ DescNullsLast expression -> renderSQL expression <+> "DESC NULLS LAST"
2237+-- | A `WindowDefinition` is a set of table rows that are somehow related
2238+-- to the current row
2239+data WindowDefinition outer grp commons schemas params from where
2242+ => NP (Expression outer grp commons schemas params from) bys
2243+ -> [SortExpression outer grp commons schemas params from]
2244+ -> WindowDefinition outer grp commons schemas params from
2246+instance OrderBy WindowDefinition where
2247+ orderBy sortsR (WindowDefinition parts sortsL)
2248+ = WindowDefinition parts (sortsL ++ sortsR)
2250+instance RenderSQL (WindowDefinition outer commons schemas from grp params) where
2251+ renderSQL (WindowDefinition part ord) =
2252+ renderPartitionByClause part <> renderOrderByClause ord
2254+ renderPartitionByClause = \case
2256+ parts -> "PARTITION" <+> "BY" <+> renderCommaSeparated renderExpression parts
2257+ renderOrderByClause = \case
2259+ srts -> " ORDER" <+> "BY"
2260+ <+> commaSeparated (renderSQL <$> srts)
2263+The `partitionBy` clause within `Squeal.PostgreSQL.Query.Over` divides the rows into groups,
2264+or partitions, that share the same values of the `partitionBy` `Expression`(s).
2265+For each row, the window function is computed across the rows that fall into
2266+the same partition as the current row.
2270+ => NP (Expression outer grp commons schemas params from) bys
2271+ -> WindowDefinition outer grp commons schemas params from
2272+partitionBy bys = WindowDefinition bys []
2275+A window function performs a calculation across a set of table rows
2276+that are somehow related to the current row. This is comparable to the type
2277+of calculation that can be done with an aggregate function.
2278+However, window functions do not cause rows to become grouped into a single
2279+output row like non-window aggregate calls would.
2280+Instead, the rows retain their separate identities.
2281+Behind the scenes, the window function is able to access more than
2282+just the current row of the query result.
2284+newtype WindowFunction
2285+ (outer :: FromType)
2287+ (commons :: FromType)
2288+ (schemas :: SchemasType)
2289+ (params :: [NullityType])
2291+ (ty :: NullityType)
2292+ = UnsafeWindowFunction { renderWindowFunction :: ByteString }
2293+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
2295+instance RenderSQL (WindowFunction outer grp commons schemas params from ty) where
2296+ renderSQL = renderWindowFunction
2299+ = forall outer grp commons schemas params from
2300+ . WindowFunction outer grp commons schemas params from x
2301+ -- ^ cannot reference aliases
2304+ = forall outer grp commons schemas params from
2305+ . Expression outer grp commons schemas params from x
2307+ -> WindowFunction outer grp commons schemas params from y
2311+ = forall outer grp commons schemas params from
2312+ . NP (Expression outer grp commons schemas params from) xs
2314+ -> WindowFunction outer grp commons schemas params from y
2317+unsafeWindowFunction1 :: ByteString -> WinFun1 x y
2318+unsafeWindowFunction1 fun x
2319+ = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x)
2321+unsafeWindowFunctionHet :: SOP.SListI xs => ByteString -> WinFunHet xs y
2322+unsafeWindowFunctionHet fun xs = UnsafeWindowFunction $ fun <>
2323+ parenthesized (renderCommaSeparated renderSQL xs)
2325+{- | rank of the current row with gaps; same as `rowNumber` of its first peer
2329+rank :: WinFun0 ('NotNull 'PGint8)
2330+rank = UnsafeWindowFunction "rank()"
2332+{- | number of the current row within its partition, counting from 1
2333+>>> printSQL rowNumber
2336+rowNumber :: WinFun0 ('NotNull 'PGint8)
2337+rowNumber = UnsafeWindowFunction "row_number()"
2339+{- | rank of the current row without gaps; this function counts peer groups
2340+>>> printSQL denseRank
2343+denseRank :: WinFun0 ('NotNull 'PGint8)
2344+denseRank = UnsafeWindowFunction "dense_rank()"
2346+{- | relative rank of the current row: (rank - 1) / (total partition rows - 1)
2347+>>> printSQL percentRank
2350+percentRank :: WinFun0 ('NotNull 'PGfloat8)
2351+percentRank = UnsafeWindowFunction "percent_rank()"
2353+{- | cumulative distribution: (number of partition rows
2354+preceding or peer with current row) / total partition rows
2355+>>> printSQL cumeDist
2358+cumeDist :: WinFun0 ('NotNull 'PGfloat8)
2359+cumeDist = UnsafeWindowFunction "cume_dist()"
2361+{- | integer ranging from 1 to the argument value,
2362+dividing the partition as equally as possible
2364+ntile :: WinFun1 ('NotNull 'PGint4) ('NotNull 'PGint4)
2365+ntile = unsafeWindowFunction1 "ntile"
2367+{- | returns value evaluated at the row that is offset rows before the current
2368+row within the partition; if there is no such row, instead return default
2369+(which must be of the same type as value). Both offset and default are
2370+evaluated with respect to the current row.
2372+lag :: WinFunHet '[ty, 'NotNull 'PGint4, ty] ty
2373+lag = unsafeWindowFunctionHet "lag"
2375+{- | returns value evaluated at the row that is offset rows after the current
2376+row within the partition; if there is no such row, instead return default
2377+(which must be of the same type as value). Both offset and default are
2378+evaluated with respect to the current row.
2380+lead :: WinFunHet '[ty, 'NotNull 'PGint4, ty] ty
2381+lead = unsafeWindowFunctionHet "lag"
2383+{- | returns value evaluated at the row that is the
2384+first row of the window frame
2386+firstValue :: WinFun1 ty ty
2387+firstValue = unsafeWindowFunction1 "first_value"
2389+{- | returns value evaluated at the row that is the
2390+last row of the window frame
2392+lastValue :: WinFun1 ty ty
2393+lastValue = unsafeWindowFunction1 "last_value"
2395+{- | returns value evaluated at the row that is the nth
2396+row of the window frame (counting from 1); null if no such row
2398+nthValue :: WinFunHet '[null ty, 'NotNull 'PGint4] ('Null ty)
2399+nthValue = unsafeWindowFunctionHet "nth_value"
2402+Create date from year, month and day fields
2404+>>> printSQL (makeDate (1984 :* 7 *: 3))
2405+make_date(1984, 7, 3)
2407+makeDate :: FunctionHet
2408+ '[ null 'PGint4, null 'PGint4, null 'PGint4 ]
2410+makeDate = unsafeFunctionHet "make_date"
2413+Create time from hour, minute and seconds fields
2415+>>> printSQL (makeTime (8 :* 15 *: 23.5))
2416+make_time(8, 15, 23.5)
2418+makeTime :: FunctionHet
2419+ '[ null 'PGint4, null 'PGint4, null 'PGfloat8 ]
2421+makeTime = unsafeFunctionHet "make_time"
2424+Create timestamp from year, month, day, hour, minute and seconds fields
2426+>>> printSQL (makeTimestamp (2013 :* 7 :* 15 :* 8 :* 15 *: 23.5))
2427+make_timestamp(2013, 7, 15, 8, 15, 23.5)
2429+makeTimestamp :: FunctionHet
2430+ '[ null 'PGint4, null 'PGint4, null 'PGint4
2431+ , null 'PGint4, null 'PGint4, null 'PGfloat8 ]
2432+ ( null 'PGtimestamp )
2433+makeTimestamp = unsafeFunctionHet "make_timestamp"
2436+Create timestamp with time zone from
2437+year, month, day, hour, minute and seconds fields;
2438+the current time zone is used
2440+>>> printSQL (makeTimestamptz (2013 :* 7 :* 15 :* 8 :* 15 *: 23.5))
2441+make_timestamptz(2013, 7, 15, 8, 15, 23.5)
2443+makeTimestamptz :: FunctionHet
2444+ '[ null 'PGint4, null 'PGint4, null 'PGint4
2445+ , null 'PGint4, null 'PGint4, null 'PGfloat8 ]
2446+ ( null 'PGtimestamptz )
2447+makeTimestamptz = unsafeFunctionHet "make_timestamptz"
2450+Affine space operations on time types.
2452+class TimeOp time diff | time -> diff where
2454+ >>> printSQL (makeDate (1984 :* 7 *: 3) !+ 365)
2455+ (make_date(1984, 7, 3) + 365)
2457+ (!+) :: Operator (null time) (null diff) (null time)
2458+ (!+) = unsafeBinaryOp "+"
2460+ >>> printSQL (365 +! makeDate (1984 :* 7 *: 3))
2461+ (365 + make_date(1984, 7, 3))
2463+ (+!) :: Operator (null diff) (null time) (null time)
2464+ (+!) = unsafeBinaryOp "+"
2466+ >>> printSQL (makeDate (1984 :* 7 *: 3) !- 365)
2467+ (make_date(1984, 7, 3) - 365)
2469+ (!-) :: Operator (null time) (null diff) (null time)
2470+ (!-) = unsafeBinaryOp "-"
2472+ >>> printSQL (makeDate (1984 :* 7 *: 3) !-! currentDate)
2473+ (make_date(1984, 7, 3) - CURRENT_DATE)
2475+ (!-!) :: Operator (null time) (null time) (null diff)
2476+ (!-!) = unsafeBinaryOp "-"
2477+instance TimeOp 'PGtimestamp 'PGinterval
2478+instance TimeOp 'PGtimestamptz 'PGinterval
2479+instance TimeOp 'PGtime 'PGinterval
2480+instance TimeOp 'PGtimetz 'PGinterval
2481+instance TimeOp 'PGinterval 'PGinterval
2482+instance TimeOp 'PGdate 'PGint4