@@ -1,14 +1,13 @@
2Module: Squeal.PostgreSQL.Expression
2Module: Squeal.PostgreSQL.Expression
3Description: Squeal expressions
3Description: Squeal expressions
4−Copyright: (c) Eitan Chatav, 2017
4+Copyright: (c) Eitan Chatav, 2019
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 #-}
12{-# LANGUAGE
11{-# LANGUAGE
13 AllowAmbiguousTypes
12 AllowAmbiguousTypes
14 , ConstraintKinds
13 , ConstraintKinds
@@ -34,333 +33,214 @@ Squeal expressions are the atoms used to build statements.
34module Squeal.PostgreSQL.Expression
33module Squeal.PostgreSQL.Expression
35 ( -- * Expression
34 ( -- * Expression
36 Expression (..)
35 Expression (..)
37− , ClosedExpression
36+ , Expr
38 , Operator
45 , Operator
39− , ComparisonOperator
46+ , OperatorDB
40− , HasParameter (parameter)
58 , unsafeBinaryOp
47 , unsafeBinaryOp
59− , unsafeUnaryOp
48+ , binaryOp
60− , unsafeFunction
49+ , FunctionVar
61− , unsafeVariadicFunction
50+ , unsafeFunctionVar
62− , atan2_
51+ , type (--->)
63− , cast
52+ , FunctionNDB
64− , quot_
53+ , unsafeFunctionN
65− , rem_
54+ , functionN
66− , trunc
55+ , PGSubset (..)
67− , round_
56+ , PGIntersect (..)
89− , notBetweenSymmetric
116− -- *** Json and Jsonb operators
121− -- *** Jsonb operators
148− , jsonExtractPathAsText
149− , jsonbExtractPathAsText
167− -- * Window Functions
168− , WindowDefinition (..)
170− , WindowFunction (..)
183− , SortExpression (..)
186− , TypeExpression (..)
212− , timestampWithTimeZone
225 -- * Re-export
57 -- * Re-export
232import Control.Category
63import Control.Category
233import Control.DeepSeq
64import Control.DeepSeq
234import Data.ByteString (ByteString)
65import Data.ByteString (ByteString)
235−import Data.ByteString.Lazy (toStrict)
236import Data.Function ((&))
66import Data.Function ((&))
237import Data.Semigroup hiding (All)
67import Data.Semigroup hiding (All)
238−import qualified Data.Aeson as JSON
239import Data.String
68import Data.String
241import Generics.SOP hiding (All, from)
69import Generics.SOP hiding (All, from)
242−import GHC.Generics ((:*:) (..))
243import GHC.OverloadedLabels
70import GHC.OverloadedLabels
244import GHC.TypeLits
71import GHC.TypeLits
245import Numeric
72import Numeric
246import Prelude hiding (id, (.))
73import Prelude hiding (id, (.))
248−import qualified Data.ByteString as ByteString
249import qualified GHC.Generics as GHC
75import qualified GHC.Generics as GHC
250−import qualified Generics.SOP as SOP
77+import Squeal.PostgreSQL.Alias
78+import Squeal.PostgreSQL.List
252import Squeal.PostgreSQL.Render
79import Squeal.PostgreSQL.Render
253import Squeal.PostgreSQL.Schema
80import Squeal.PostgreSQL.Schema
83+-- >>> import Squeal.PostgreSQL
255{-----------------------------------------
85{-----------------------------------------
256column expressions
86column expressions
257-----------------------------------------}
87-----------------------------------------}
259{- | `Expression`s are used in a variety of contexts,
89{- | `Expression`s are used in a variety of contexts,
90+such as in the target `Squeal.PostgreSQL.Query.List` of the
260−such as in the target list of the `Squeal.PostgreSQL.Query.select` command,
91+`Squeal.PostgreSQL.Query.select` command,
261as new column values in `Squeal.PostgreSQL.Manipulation.insertRow` or
92as new column values in `Squeal.PostgreSQL.Manipulation.insertRow` or
262`Squeal.PostgreSQL.Manipulation.update`,
93`Squeal.PostgreSQL.Manipulation.update`,
263−or in search `Condition`s in a number of commands.
94+or in search `Squeal.PostgreSQL.Logic.Condition`s in a number of commands.
265The expression syntax allows the calculation of
96The expression syntax allows the calculation of
266values from primitive expression using arithmetic, logical,
97values from primitive expression using arithmetic, logical,
267and other operations.
98and 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`.
269newtype Expression
114newtype Expression
270 (outer :: FromType)
115 (outer :: FromType)
272 (commons :: FromType)
116 (commons :: FromType)
273 (schemas :: SchemasType)
118 (schemas :: SchemasType)
274 (params :: [NullityType])
119 (params :: [NullityType])
275 (from :: FromType)
120 (from :: FromType)
276 (ty :: NullityType)
121 (ty :: NullityType)
277 = UnsafeExpression { renderExpression :: ByteString }
122 = UnsafeExpression { renderExpression :: ByteString }
278 deriving (GHC.Generic,Show,Eq,Ord,NFData)
123 deriving (GHC.Generic,Show,Eq,Ord,NFData)
279−instance RenderSQL (Expression outer grp commons schemas params from ty) where
124+instance RenderSQL (Expression outer commons grp schemas params from ty) where
280 renderSQL = renderExpression
125 renderSQL = renderExpression
282−type ClosedExpression ty
127+-- | An `Expr` is a closed `Expression`.
283− = forall outer grp commons schemas params from
128+-- It is a F@RankNType@ but don't be scared.
284− . Expression outer grp commons schemas params from ty
129+-- Think of it as an expression which sees no
285−
130+-- namespaces, so you can't use parameters
286−type Operator ty1 ty2 ty3
131+-- or alias references. It can be used as
287− = forall outer grp commons schemas params from
132+-- a simple piece of more complex `Expression`s.
288− . Expression outer grp commons schemas params from ty1
133+type Expr x
289− -> Expression outer grp commons schemas params from ty2
134+ = forall outer commons grp schemas params from
290− -> Expression outer grp commons schemas params from ty3
135+ . Expression outer commons grp schemas params from x
291−
136+ -- ^ cannot reference aliases
292−type ComparisonOperator ty1 ty2
137+
293− = Operator ty1 ty2 ('Null 'PGbool)
138+-- | A @RankNType@ for binary operators.
294−
139+type Operator x1 x2 y
295−{- | A `HasParameter` constraint is used to indicate a value that is
140+ = forall outer commons grp schemas params from
296−supplied externally to a SQL statement.
141+ . Expression outer commons grp schemas params from x1
297−`Squeal.PostgreSQL.PQ.manipulateParams`,
142+ -- ^ left input
298−`Squeal.PostgreSQL.PQ.queryParams` and
143+ -> Expression outer commons grp schemas params from x2
299−`Squeal.PostgreSQL.PQ.traversePrepared` support specifying data values
144+ -- ^ right input
300−separately from the SQL command string, in which case `param`s are used to
145+ -> Expression outer commons grp schemas params from y
301−refer to the out-of-line data values.
146+ -- ^ output
148+type OperatorDB schemas x1 x2 y
149+ = forall outer commons grp params from
150+ . Expression outer commons grp schemas params from x1
152+ -> Expression outer commons grp schemas params from x2
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
169+type FunctionDB schemas x y
170+ = forall outer commons grp params from
171+ . Expression outer commons grp schemas params from x
173+ -> Expression outer commons grp schemas params from y
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)
183+ = forall outer commons grp schemas params from
184+ . NP (Expression outer commons grp schemas params from) xs
186+ -> Expression outer commons grp schemas params from y
189+type FunctionNDB schemas xs 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.
303−class KnownNat n => HasParameter
199+type FunctionVar x0 x1 y
304− (n :: Nat)
200+ = forall outer commons grp schemas params from
305− (params :: [NullityType])
201+ . [Expression outer commons grp schemas params from x0]
306− (ty :: NullityType)
202+ -- ^ inputs
307− | n params -> ty where
203+ -> Expression outer commons grp schemas params from x1
308− -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
204+ -- ^ must have at least 1 input
310− -- >>> let expr = parameter @1 int4 :: Expression outer grp '[] schemas '[ 'Null 'PGint4] from ('Null 'PGint4)
205+ -> Expression outer commons grp schemas params from y
311− -- >>> printSQL expr
206+ -- ^ output
313− parameter
208+{- | >>> printSQL (unsafeFunctionVar "greatest" [true, null_] false)
314− :: TypeExpression schemas ty
209+greatest(TRUE, NULL, FALSE)
315− -> Expression outer grp commons schemas params from ty
210+-}
211+unsafeFunctionVar :: ByteString -> FunctionVar x0 x1 y
316− parameter ty = UnsafeExpression $ parenthesized $
212+unsafeFunctionVar fun xs x = UnsafeExpression $ fun <> parenthesized
317− "$" <> renderNat @n <+> "::"
213+ (commaSeparated (renderSQL <$> xs) <> ", " <> renderSQL x)
319−instance {-# OVERLAPPING #-} HasParameter 1 (ty1:tys) ty1
320−instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
321− => HasParameter n (ty' : params) ty
323−-- | `param` takes a `Nat` using type application and for basic types,
324−-- infers a `TypeExpression`.
326−-- >>> let expr = param @1 :: Expression outer grp commons schemas '[ 'Null 'PGint4] from ('Null 'PGint4)
330− :: forall n outer commons schemas params from grp ty
331− . (PGTyped schemas ty, HasParameter n params ty)
332− => Expression outer grp commons schemas params from ty -- ^ param
333−param = parameter @n (pgtype @schemas)
335instance (HasUnique tab (Join outer from) row, Has col row ty)
215instance (HasUnique tab (Join outer from) row, Has col row ty)
336− => IsLabel col (Expression outer 'Ungrouped commons schemas params from ty) where
216+ => IsLabel col (Expression outer commons 'Ungrouped schemas params from ty) where
337 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
217 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
338instance (HasUnique tab (Join outer from) row, Has col row ty, tys ~ '[ty])
218instance (HasUnique tab (Join outer from) row, Has col row ty, tys ~ '[ty])
339− => IsLabel col (NP (Expression outer 'Ungrouped commons schemas params from) tys) where
219+ => IsLabel col (NP (Expression outer commons 'Ungrouped schemas params from) tys) where
340 fromLabel = fromLabel @col :* Nil
220 fromLabel = fromLabel @col :* Nil
341instance (HasUnique tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
221instance (HasUnique tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
342 => IsLabel col
222 => IsLabel col
343− (Aliased (Expression outer 'Ungrouped commons schemas params from) column) where
223+ (Aliased (Expression outer commons 'Ungrouped schemas params from) column) where
344 fromLabel = fromLabel @col `As` Alias
224 fromLabel = fromLabel @col `As` Alias
345instance (HasUnique tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
225instance (HasUnique tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
346 => IsLabel col
226 => IsLabel col
347− (NP (Aliased (Expression outer 'Ungrouped commons schemas params from)) columns) where
227+ (NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns) where
348 fromLabel = fromLabel @col :* Nil
228 fromLabel = fromLabel @col :* Nil
350instance (Has tab (Join outer from) row, Has col row ty)
230instance (Has tab (Join outer from) row, Has col row ty)
351− => IsQualified tab col (Expression outer 'Ungrouped commons schemas params from ty) where
231+ => IsQualified tab col (Expression outer commons 'Ungrouped schemas params from ty) where
352 tab ! col = UnsafeExpression $
232 tab ! col = UnsafeExpression $
353 renderSQL tab <> "." <> renderSQL col
233 renderSQL tab <> "." <> renderSQL col
354instance (Has tab (Join outer from) row, Has col row ty, tys ~ '[ty])
234instance (Has tab (Join outer from) row, Has col row ty, tys ~ '[ty])
355− => IsQualified tab col (NP (Expression outer 'Ungrouped commons schemas params from) tys) where
235+ => IsQualified tab col (NP (Expression outer commons 'Ungrouped schemas params from) tys) where
356 tab ! col = tab ! col :* Nil
236 tab ! col = tab ! col :* Nil
357instance (Has tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
237instance (Has tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
358 => IsQualified tab col
238 => IsQualified tab col
359− (Aliased (Expression outer 'Ungrouped commons schemas params from) column) where
239+ (Aliased (Expression outer commons 'Ungrouped schemas params from) column) where
360 tab ! col = tab ! col `As` col
240 tab ! col = tab ! col `As` col
361instance (Has tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
241instance (Has tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
362 => IsQualified tab col
242 => IsQualified tab col
363− (NP (Aliased (Expression outer 'Ungrouped commons schemas params from)) columns) where
243+ (NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns) where
364 tab ! col = tab ! col :* Nil
244 tab ! col = tab ! col :* Nil
@@ -368,7 +248,7 @@ instance
368 , Has col row ty
248 , Has col row ty
369 , GroupedBy tab col bys
249 , GroupedBy tab col bys
370 ) => IsLabel col
250 ) => IsLabel col
371− (Expression outer ('Grouped bys) commons schemas params from ty) where
251+ (Expression outer commons ('Grouped bys) schemas params from ty) where
372 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
252 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
374 ( HasUnique tab (Join outer from) row
254 ( HasUnique tab (Join outer from) row
@@ -376,7 +256,7 @@ instance
376 , GroupedBy tab col bys
256 , GroupedBy tab col bys
377 , tys ~ '[ty]
257 , tys ~ '[ty]
378 ) => IsLabel col
258 ) => IsLabel col
379− (NP (Expression outer ('Grouped bys) commons schemas params from) tys) where
259+ (NP (Expression outer commons ('Grouped bys) schemas params from) tys) where
380 fromLabel = fromLabel @col :* Nil
260 fromLabel = fromLabel @col :* Nil
382 ( HasUnique tab (Join outer from) row
262 ( HasUnique tab (Join outer from) row
@@ -384,7 +264,7 @@ instance
384 , GroupedBy tab col bys
264 , GroupedBy tab col bys
385 , column ~ (col ::: ty)
265 , column ~ (col ::: ty)
386 ) => IsLabel col
266 ) => IsLabel col
387− (Aliased (Expression outer ('Grouped bys) commons schemas params from) column) where
267+ (Aliased (Expression outer commons ('Grouped bys) schemas params from) column) where
388 fromLabel = fromLabel @col `As` Alias
268 fromLabel = fromLabel @col `As` Alias
390 ( HasUnique tab (Join outer from) row
270 ( HasUnique tab (Join outer from) row
@@ -392,7 +272,7 @@ instance
392 , GroupedBy tab col bys
272 , GroupedBy tab col bys
393 , columns ~ '[col ::: ty]
273 , columns ~ '[col ::: ty]
394 ) => IsLabel col
274 ) => IsLabel col
395− (NP (Aliased (Expression outer ('Grouped bys) commons schemas params from)) columns) where
275+ (NP (Aliased (Expression outer commons ('Grouped bys) schemas params from)) columns) where
396 fromLabel = fromLabel @col :* Nil
276 fromLabel = fromLabel @col :* Nil
@@ -400,7 +280,7 @@ instance
400 , Has col row ty
280 , Has col row ty
401 , GroupedBy tab col bys
281 , GroupedBy tab col bys
402 ) => IsQualified tab col
282 ) => IsQualified tab col
403− (Expression outer ('Grouped bys) commons schemas params from ty) where
283+ (Expression outer commons ('Grouped bys) schemas params from ty) where
404 tab ! col = UnsafeExpression $
284 tab ! col = UnsafeExpression $
405 renderSQL tab <> "." <> renderSQL col
285 renderSQL tab <> "." <> renderSQL col
@@ -409,7 +289,7 @@ instance
409 , GroupedBy tab col bys
289 , GroupedBy tab col bys
410 , tys ~ '[ty]
290 , tys ~ '[ty]
411 ) => IsQualified tab col
291 ) => IsQualified tab col
412− (NP (Expression outer ('Grouped bys) commons schemas params from) tys) where
292+ (NP (Expression outer commons ('Grouped bys) schemas params from) tys) where
413 tab ! col = tab ! col :* Nil
293 tab ! col = tab ! col :* Nil
415 ( Has tab (Join outer from) row
295 ( Has tab (Join outer from) row
@@ -417,7 +297,7 @@ instance
417 , GroupedBy tab col bys
297 , GroupedBy tab col bys
418 , column ~ (col ::: ty)
298 , column ~ (col ::: ty)
419 ) => IsQualified tab col
299 ) => IsQualified tab col
420− (Aliased (Expression outer ('Grouped bys) commons schemas params from) column) where
300+ (Aliased (Expression outer commons ('Grouped bys) schemas params from) column) where
421 tab ! col = tab ! col `As` col
301 tab ! col = tab ! col `As` col
423 ( Has tab (Join outer from) row
303 ( Has tab (Join outer from) row
@@ -425,244 +305,78 @@ instance
425 , GroupedBy tab col bys
305 , GroupedBy tab col bys
426 , columns ~ '[col ::: ty]
306 , columns ~ '[col ::: ty]
427 ) => IsQualified tab col
307 ) => IsQualified tab col
428− (NP (Aliased (Expression outer ('Grouped bys) commons schemas params from)) columns) where
308+ (NP (Aliased (Expression outer commons ('Grouped bys) schemas params from)) columns) where
429 tab ! col = tab ! col :* Nil
309 tab ! col = tab ! col :* Nil
431−-- | analagous to `Nothing`
433−-- >>> printSQL null_
435−null_ :: Expression outer grp commons schemas params from ('Null ty)
436−null_ = UnsafeExpression "NULL"
438−-- | analagous to `Just`
440−-- >>> printSQL $ notNull true
443− :: Expression outer grp commons schemas params from ('NotNull ty)
444− -> Expression outer grp commons schemas params from ('Null ty)
445−notNull = UnsafeExpression . renderSQL
447−-- | return the leftmost value which is not NULL
449−-- >>> printSQL $ coalesce [null_, true] false
450−-- COALESCE(NULL, TRUE, FALSE)
452− :: [Expression outer grp commons schemas params from ('Null ty)]
453− -- ^ @NULL@s may be present
454− -> Expression outer grp commons schemas params from ('NotNull ty)
455− -- ^ @NULL@ is absent
456− -> Expression outer grp commons schemas params from ('NotNull ty)
457−coalesce nullxs notNullx = UnsafeExpression $
458− "COALESCE" <> parenthesized (commaSeparated
459− ((renderSQL <$> nullxs) <> [renderSQL notNullx]))
461−-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
463−-- >>> printSQL $ fromNull true null_
464−-- COALESCE(NULL, TRUE)
466− :: Expression outer grp commons schemas params from ('NotNull ty)
467− -- ^ what to convert @NULL@ to
468− -> Expression outer grp commons schemas params from ('Null ty)
469− -> Expression outer grp commons schemas params from ('NotNull ty)
470−fromNull notNullx nullx = coalesce [nullx] notNullx
472−-- | >>> printSQL $ null_ & isNull
475− :: Expression outer grp commons schemas params from ('Null ty)
476− -- ^ possibly @NULL@
477− -> Condition outer grp commons schemas params from
478−isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
480−-- | >>> printSQL $ null_ & isNotNull
483− :: Expression outer grp commons schemas params from ('Null ty)
484− -- ^ possibly @NULL@
485− -> Condition outer grp commons schemas params from
486−isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
488−-- | analagous to `maybe` using @IS NULL@
490−-- >>> printSQL $ matchNull true not_ null_
491−-- CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END
493− :: Expression outer grp commons schemas params from (nullty)
494− -- ^ what to convert @NULL@ to
495− -> ( Expression outer grp commons schemas params from ('NotNull ty)
496− -> Expression outer grp commons schemas params from (nullty) )
497− -- ^ function to perform when @NULL@ is absent
498− -> Expression outer grp commons schemas params from ('Null ty)
499− -> Expression outer grp commons schemas params from (nullty)
500−matchNull y f x = ifThenElse (isNull x) y
501− (f (UnsafeExpression (renderSQL x)))
503−{-| right inverse to `fromNull`, if its arguments are equal then
504−`nullIf` gives @NULL@.
506−>>> :set -XTypeApplications -XDataKinds
507−>>> let expr = nullIf false (param @1) :: Expression outer grp commons schemas '[ 'NotNull 'PGbool] from ('Null 'PGbool)
509−NULL IF (FALSE, ($1 :: bool))
512− :: Expression outer grp commons schemas params from ('NotNull ty)
513− -- ^ @NULL@ is absent
514− -> Expression outer grp commons schemas params from ('NotNull ty)
515− -- ^ @NULL@ is absent
516− -> Expression outer grp commons schemas params from ('Null ty)
517−nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
518− (renderSQL x <> ", " <> renderSQL y)
520−-- | >>> printSQL $ array [null_, false, true]
521−-- ARRAY[NULL, FALSE, TRUE]
523− :: [Expression outer grp commons schemas params from ty]
525− -> Expression outer grp commons schemas params from (nullity ('PGvararray ty))
526−array xs = UnsafeExpression $
527− "ARRAY[" <> commaSeparated (renderSQL <$> xs) <> "]"
529−-- | >>> printSQL $ cardinality (array [null_, false, true])
530−-- cardinality(ARRAY[NULL, FALSE, TRUE])
532− :: Expression outer grp commons schemas params from (nullity ('PGvararray ty))
533− -> Expression outer grp commons schemas params from (nullity 'PGint8)
534−cardinality = unsafeFunction "cardinality"
536−-- | >>> printSQL $ array [null_, false, true] & index 2
537−-- (ARRAY[NULL, FALSE, TRUE])[2]
539− :: Word64 -- ^ index
540− -> Expression outer grp commons schemas params from (nullity ('PGvararray ty)) -- ^ array
541− -> Expression outer grp commons schemas params from (NullifyType ty)
542−index n expr = UnsafeExpression $
543− parenthesized (renderSQL expr) <> "[" <> fromString (show n) <> "]"
545instance (KnownSymbol label, label `In` labels) => IsPGlabel label
311instance (KnownSymbol label, label `In` labels) => IsPGlabel label
546− (Expression outer grp commons schemas params from (nullity ('PGenum labels))) where
312+ (Expression outer commons grp schemas params from (null ('PGenum labels))) where
547 label = UnsafeExpression $ renderSQL (PGlabel @label)
313 label = UnsafeExpression $ renderSQL (PGlabel @label)
549−-- | A row constructor is an expression that builds a row value
550−-- (also called a composite value) using values for its member fields.
553−-- type Complex = 'PGcomposite
554−-- '[ "real" ::: 'NotNull 'PGfloat8
555−-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
558−-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression outer grp commons schemas params from ('NotNull Complex)
563− => NP (Aliased (Expression outer grp commons schemas params from)) row
564− -- ^ zero or more expressions for the row field values
565− -> Expression outer grp commons schemas params from (nullity ('PGcomposite row))
566−row exprs = UnsafeExpression $ "ROW" <> parenthesized
567− (renderCommaSeparated (\ (expr `As` _) -> renderSQL expr) exprs)
570−-- type Complex = 'PGcomposite
571−-- '[ "real" ::: 'NotNull 'PGfloat8
572−-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
573−-- type Schema = '["complex" ::: 'Typedef Complex]
576−-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression outer grp '[] (Public Schema) from params ('NotNull Complex)
577−-- >>> printSQL $ i & field #complex #imaginary
578−-- (ROW(0, 1)::"complex")."imaginary"
580− :: ( Has sch schemas schema
581− , Has tydef schema ('Typedef ('PGcomposite row))
583− => QualifiedAlias sch tydef -- ^ row type
584− -> Alias field -- ^ field name
585− -> Expression outer grp commons schemas params from ('NotNull ('PGcomposite row))
586− -> Expression outer grp commons schemas params from ty
587−field td fld expr = UnsafeExpression $
588− parenthesized (renderSQL expr <> "::" <> renderSQL td)
589− <> "." <> renderSQL fld
592− (Expression outer grp commons schemas params from (nullity ('PGvararray ty))) where
593− (<>) = unsafeBinaryOp "||"
596− (Expression outer grp commons schemas params from (nullity ('PGvararray ty))) where
600−-- | >>> let expr = greatest currentTimestamp [param @1] :: Expression outer grp commons schemas '[ 'NotNull 'PGtimestamptz] from ('NotNull 'PGtimestamptz)
602−-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
604− :: Expression outer grp commons schemas params from (nullty)
605− -- ^ needs at least 1 argument
606− -> [Expression outer grp commons schemas params from (nullty)]
608− -> Expression outer grp commons schemas params from (nullty)
609−greatest x xs = UnsafeExpression $ "GREATEST("
610− <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
612−-- | >>> printSQL $ least currentTimestamp [null_]
613−-- LEAST(CURRENT_TIMESTAMP, NULL)
615− :: Expression outer grp commons schemas params from (nullty)
616− -- ^ needs at least 1 argument
617− -> [Expression outer grp commons schemas params from (nullty)]
619− -> Expression outer grp commons schemas params from (nullty)
620−least x xs = UnsafeExpression $ "LEAST("
621− <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
623-- | >>> printSQL $ unsafeBinaryOp "OR" true false
315-- | >>> printSQL $ unsafeBinaryOp "OR" true false
624-- (TRUE OR FALSE)
316-- (TRUE OR FALSE)
625−unsafeBinaryOp
317+unsafeBinaryOp :: ByteString -> Operator ty0 ty1 ty2
628− -> Expression outer grp commons schemas params from (ty0)
629− -> Expression outer grp commons schemas params from (ty1)
630− -> Expression outer grp commons schemas params from (ty2)
631unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
318unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
632 renderSQL x <+> op <+> renderSQL y
319 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
634−-- | >>> printSQL $ unsafeUnaryOp "NOT" true
328+-- | >>> printSQL $ unsafeLeftOp "NOT" true
635-- (NOT TRUE)
329-- (NOT TRUE)
637− :: ByteString
330+unsafeLeftOp :: ByteString -> x --> y
638− -- ^ operator
331+unsafeLeftOp op x = UnsafeExpression $ parenthesized $ op <+> renderSQL x
639− -> Expression outer grp commons schemas params from (ty0)
332+
640− -> Expression outer grp commons schemas params from (ty1)
333+leftOp
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
641−unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
343+unsafeRightOp op x = UnsafeExpression $ parenthesized $ renderSQL x <+> op
642− op <+> renderSQL x
344+
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
644-- | >>> printSQL $ unsafeFunction "f" true
352-- | >>> printSQL $ unsafeFunction "f" true
645-- f(TRUE)
353-- f(TRUE)
646−unsafeFunction
354+unsafeFunction :: ByteString -> x --> y
649− -> Expression outer grp commons schemas params from (xty)
650− -> Expression outer grp commons schemas params from (yty)
651unsafeFunction fun x = UnsafeExpression $
355unsafeFunction fun x = UnsafeExpression $
652 fun <> parenthesized (renderSQL x)
356 fun <> parenthesized (renderSQL x)
654−-- | Helper for defining variadic functions.
358+function
655−unsafeVariadicFunction
359+ :: (Has sch schemas schema, Has fun schema ('Function ('[x] :=> 'Returns y)))
656− :: SListI elems
360+ => QualifiedAlias sch fun
361+ -> FunctionDB schemas x y
362+function = unsafeFunction . renderSQL
364+-- | >>> printSQL $ unsafeFunctionN "f" (currentTime :* localTimestamp :* false *: literal 'a')
365+-- f(CURRENT_TIME, LOCALTIMESTAMP, FALSE, E'a')
657− => ByteString
366+unsafeFunctionN :: SListI xs => ByteString -> xs ---> y
659− -> NP (Expression outer grp commons schemas params from) elems
660− -> Expression outer grp commons schemas params from ret
661−unsafeVariadicFunction fun x = UnsafeExpression $
367+unsafeFunctionN fun xs = UnsafeExpression $
662− fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderSQL) x)))
368+ fun <> parenthesized (renderCommaSeparated renderSQL xs)
371+ :: ( Has sch schemas schema
372+ , Has fun schema ('Function (xs :=> 'Returns y))
374+ => QualifiedAlias sch fun
375+ -> FunctionNDB schemas xs y
376+functionN = unsafeFunctionN . renderSQL
664instance ty `In` PGNum
378instance ty `In` PGNum
665− => Num (Expression outer grp commons schemas params from (nullity ty)) where
379+ => Num (Expression outer commons grp schemas params from (null ty)) where
666 (+) = unsafeBinaryOp "+"
380 (+) = unsafeBinaryOp "+"
667 (-) = unsafeBinaryOp "-"
381 (-) = unsafeBinaryOp "-"
668 (*) = unsafeBinaryOp "*"
382 (*) = unsafeBinaryOp "*"
@@ -674,7 +388,7 @@ instance ty `In` PGNum
676instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
390instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
677− (Expression outer grp commons schemas params from (nullity ty)) where
391+ (Expression outer commons grp schemas params from (null ty)) where
678 (/) = unsafeBinaryOp "/"
392 (/) = unsafeBinaryOp "/"
679 fromRational
393 fromRational
680 = UnsafeExpression
394 = UnsafeExpression
@@ -684,7 +398,7 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
684 . fromRat @Double
398 . fromRat @Double
686instance (ty `In` PGNum, ty `In` PGFloating) => Floating
400instance (ty `In` PGNum, ty `In` PGFloating) => Floating
687− (Expression outer grp commons schemas params from (nullity ty)) where
401+ (Expression outer commons grp schemas params from (null ty)) where
688 pi = UnsafeExpression "pi()"
402 pi = UnsafeExpression "pi()"
689 exp = unsafeFunction "exp"
403 exp = unsafeFunction "exp"
690 log = unsafeFunction "ln"
404 log = unsafeFunction "ln"
@@ -705,1923 +419,54 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Floating
705 acosh x = log (x + sqrt (x*x - 1))
419 acosh x = log (x + sqrt (x*x - 1))
706 atanh x = log ((1 + x) / (1 - x)) / 2
420 atanh x = log ((1 + x) / (1 - x)) / 2
708−-- | >>> :{
422+-- | Contained by operators
709−-- let
423+class PGSubset ty where
710−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
424+ (@>) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
711−-- expression = atan2_ pi 2
425+ (@>) = unsafeBinaryOp "@>"
712−-- in printSQL expression
426+ (<@) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
713−-- :}
427+ (<@) = unsafeBinaryOp "<@"
714−-- atan2(pi(), 2)
428+instance PGSubset 'PGjsonb
715−atan2_
429+instance PGSubset 'PGtsquery
716− :: float `In` PGFloating
430+instance PGSubset ('PGvararray ty)
717− => Expression outer grp commons schemas params from (nullity float)
431+instance PGSubset ('PGrange ty)
719− -> Expression outer grp commons schemas params from (nullity float)
433+class PGIntersect ty where
720− -- ^ denominator
434+ (@&&) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
721− -> Expression outer grp commons schemas params from (nullity float)
435+ (@&&) = unsafeBinaryOp "&&"
722−atan2_ y x = UnsafeExpression $
436+instance PGIntersect ('PGvararray ty)
723− "atan2(" <> renderSQL y <> ", " <> renderSQL x <> ")"
437+instance PGIntersect ('PGrange ty)
725−-- When a `cast` is applied to an `Expression` of a known type, it
726−-- represents a run-time type conversion. The cast will succeed only if a
727−-- suitable type conversion operation has been defined.
729−-- | >>> printSQL $ true & cast int4
732− :: TypeExpression schemas ty1
733− -- ^ type to cast as
734− -> Expression outer grp commons schemas params from ty0
735− -- ^ value to convert
736− -> Expression outer grp commons schemas params from ty1
737−cast ty x = UnsafeExpression $ parenthesized $
738− renderSQL x <+> "::" <+> renderSQL ty
740−-- | integer division, truncates the result
744−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
745−-- expression = 5 `quot_` 2
746−-- in printSQL expression
750− :: int `In` PGIntegral
751− => Expression outer grp commons schemas params from (nullity int)
753− -> Expression outer grp commons schemas params from (nullity int)
755− -> Expression outer grp commons schemas params from (nullity int)
756−quot_ = unsafeBinaryOp "/"
758−-- | remainder upon integer division
762−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
763−-- expression = 5 `rem_` 2
764−-- in printSQL expression
768− :: int `In` PGIntegral
769− => Expression outer grp commons schemas params from (nullity int)
771− -> Expression outer grp commons schemas params from (nullity int)
773− -> Expression outer grp commons schemas params from (nullity int)
774−rem_ = unsafeBinaryOp "%"
778−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
779−-- expression = trunc pi
780−-- in printSQL expression
784− :: frac `In` PGFloating
785− => Expression outer grp commons schemas params from (nullity frac)
786− -- ^ fractional number
787− -> Expression outer grp commons schemas params from (nullity frac)
788−trunc = unsafeFunction "trunc"
792−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
793−-- expression = round_ pi
794−-- in printSQL expression
798− :: frac `In` PGFloating
799− => Expression outer grp commons schemas params from (nullity frac)
800− -- ^ fractional number
801− -> Expression outer grp commons schemas params from (nullity frac)
802−round_ = unsafeFunction "round"
806−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
807−-- expression = ceiling_ pi
808−-- in printSQL expression
812− :: frac `In` PGFloating
813− => Expression outer grp commons schemas params from (nullity frac)
814− -- ^ fractional number
815− -> Expression outer grp commons schemas params from (nullity frac)
816−ceiling_ = unsafeFunction "ceiling"
818−-- | A `Condition` is an `Expression`, which can evaluate
819−-- to `true`, `false` or `null_`. This is because SQL uses
820−-- a three valued logic.
821−type Condition outer grp commons schemas params from =
822− Expression outer grp commons schemas params from ('Null 'PGbool)
824−-- | >>> printSQL true
826−true :: Expression outer grp commons schemas params from (nullity 'PGbool)
827−true = UnsafeExpression "TRUE"
829−-- | >>> printSQL false
831−false :: Expression outer grp commons schemas params from (nullity 'PGbool)
832−false = UnsafeExpression "FALSE"
834−-- | >>> printSQL $ not_ true
837− :: Expression outer grp commons schemas params from (nullity 'PGbool)
838− -> Expression outer grp commons schemas params from (nullity 'PGbool)
839−not_ = unsafeUnaryOp "NOT"
841−-- | >>> printSQL $ true .&& false
844− :: Expression outer grp commons schemas params from (nullity 'PGbool)
845− -> Expression outer grp commons schemas params from (nullity 'PGbool)
846− -> Expression outer grp commons schemas params from (nullity 'PGbool)
848−(.&&) = unsafeBinaryOp "AND"
850−-- | >>> printSQL $ true .|| false
853− :: Expression outer grp commons schemas params from (nullity 'PGbool)
854− -> Expression outer grp commons schemas params from (nullity 'PGbool)
855− -> Expression outer grp commons schemas params from (nullity 'PGbool)
857−(.||) = unsafeBinaryOp "OR"
861−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
862−-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
863−-- in printSQL expression
865−-- CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END
867− :: [ ( Condition outer grp commons schemas params from
868− , Expression outer grp commons schemas params from ty
870− -- ^ whens and thens
871− -> Expression outer grp commons schemas params from ty
873− -> Expression outer grp commons schemas params from ty
874−caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
878− [ " WHEN ", renderSQL when_
879− , " THEN ", renderSQL then_
881− | (when_,then_) <- whenThens
883− , " ELSE ", renderSQL else_
889−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
890−-- expression = ifThenElse true 1 0
891−-- in printSQL expression
893−-- CASE WHEN TRUE THEN 1 ELSE 0 END
895− :: Condition outer grp commons schemas params from
896− -> Expression outer grp commons schemas params from ty -- ^ then
897− -> Expression outer grp commons schemas params from ty -- ^ else
898− -> Expression outer grp commons schemas params from ty
899−ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
901−-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
902−-- will produce @NULL@s if one of their arguments is @NULL@.
904−-- >>> printSQL $ true .== null_
907− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
908− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
909− -> Condition outer grp commons schemas params from
910−(.==) = unsafeBinaryOp "="
913−-- | >>> printSQL $ true ./= null_
916− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
917− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
918− -> Condition outer grp commons schemas params from
919−(./=) = unsafeBinaryOp "<>"
922−-- | >>> printSQL $ true .>= null_
925− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
926− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
927− -> Condition outer grp commons schemas params from
928−(.>=) = unsafeBinaryOp ">="
931−-- | >>> printSQL $ true .< null_
934− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
935− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
936− -> Condition outer grp commons schemas params from
937−(.<) = unsafeBinaryOp "<"
940−-- | >>> printSQL $ true .<= null_
943− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
944− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
945− -> Condition outer grp commons schemas params from
946−(.<=) = unsafeBinaryOp "<="
949−-- | >>> printSQL $ true .> null_
952− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
953− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
954− -> Condition outer grp commons schemas params from
955−(.>) = unsafeBinaryOp ">"
959−>>> printSQL $ true `between` (null_, false)
960−TRUE BETWEEN NULL AND FALSE
963− :: Expression outer grp commons schemas params from (nullity ty)
964− -> ( Expression outer grp commons schemas params from (nullity ty)
965− , Expression outer grp commons schemas params from (nullity ty) ) -- ^ bounds
966− -> Condition outer grp commons schemas params from
967−between a (x,y) = UnsafeExpression $ renderSQL a <+> "BETWEEN"
968− <+> renderSQL x <+> "AND" <+> renderSQL y
971−>>> printSQL $ true `notBetween` (null_, false)
972−TRUE NOT BETWEEN NULL AND FALSE
975− :: Expression outer grp commons schemas params from (nullity ty)
976− -> ( Expression outer grp commons schemas params from (nullity ty)
977− , Expression outer grp commons schemas params from (nullity ty) ) -- ^ bounds
978− -> Condition outer grp commons schemas params from
979−notBetween a (x,y) = UnsafeExpression $ renderSQL a <+> "NOT BETWEEN"
980− <+> renderSQL x <+> "AND" <+> renderSQL y
982−{- | between, after sorting the comparison values
983−>>> printSQL $ true `betweenSymmetric` (null_, false)
984−TRUE BETWEEN SYMMETRIC NULL AND FALSE
987− :: Expression outer grp commons schemas params from (nullity ty)
988− -> ( Expression outer grp commons schemas params from (nullity ty)
989− , Expression outer grp commons schemas params from (nullity ty) ) -- ^ bounds
990− -> Condition outer grp commons schemas params from
991−betweenSymmetric a (x,y) = UnsafeExpression $ renderSQL a
992− <+> "BETWEEN SYMMETRIC" <+> renderSQL x <+> "AND" <+> renderSQL y
994−{- | not between, after sorting the comparison values
995−>>> printSQL $ true `notBetweenSymmetric` (null_, false)
996−TRUE NOT BETWEEN SYMMETRIC NULL AND FALSE
999− :: Expression outer grp commons schemas params from (nullity ty)
1000− -> ( Expression outer grp commons schemas params from (nullity ty)
1001− , Expression outer grp commons schemas params from (nullity ty) ) -- ^ bounds
1002− -> Condition outer grp commons schemas params from
1003−notBetweenSymmetric a (x,y) = UnsafeExpression $ renderSQL a
1004− <+> "NOT BETWEEN SYMMETRIC" <+> renderSQL x <+> "AND" <+> renderSQL y
1006−{- | not equal, treating null like an ordinary value
1007−>>> printSQL $ true `isDistinctFrom` null_
1008−(TRUE IS DISTINCT FROM NULL)
1011− :: Expression outer grp commons schemas params from (nullity ty)
1012− -> Expression outer grp commons schemas params from (nullity ty)
1013− -> Condition outer grp commons schemas params from
1014−isDistinctFrom = unsafeBinaryOp "IS DISTINCT FROM"
1016−{- | equal, treating null like an ordinary value
1017−>>> printSQL $ true `isNotDistinctFrom` null_
1018−(TRUE IS NOT DISTINCT FROM NULL)
1021− :: Expression outer grp commons schemas params from (nullity ty)
1022− -> Expression outer grp commons schemas params from (nullity ty)
1023− -> Condition outer grp commons schemas params from
1024−isNotDistinctFrom = unsafeBinaryOp "IS NOT DISTINCT FROM"
1027−>>> printSQL $ true & isTrue
1031− :: Expression outer grp commons schemas params from (nullity 'PGbool)
1032− -> Condition outer grp commons schemas params from
1033−isTrue b = UnsafeExpression $ renderSQL b <+> "IS TRUE"
1035−{- | is false or unknown
1036−>>> printSQL $ true & isNotTrue
1040− :: Expression outer grp commons schemas params from (nullity 'PGbool)
1041− -> Condition outer grp commons schemas params from
1042−isNotTrue b = UnsafeExpression $ renderSQL b <+> "IS NOT TRUE"
1045−>>> printSQL $ true & isFalse
1049− :: Expression outer grp commons schemas params from (nullity 'PGbool)
1050− -> Condition outer grp commons schemas params from
1051−isFalse b = UnsafeExpression $ renderSQL b <+> "IS FALSE"
1053−{- | is true or unknown
1054−>>> printSQL $ true & isNotFalse
1058− :: Expression outer grp commons schemas params from (nullity 'PGbool)
1059− -> Condition outer grp commons schemas params from
1060−isNotFalse b = UnsafeExpression $ renderSQL b <+> "IS NOT FALSE"
1063−>>> printSQL $ true & isUnknown
1067− :: Expression outer grp commons schemas params from (nullity 'PGbool)
1068− -> Condition outer grp commons schemas params from
1069−isUnknown b = UnsafeExpression $ renderSQL b <+> "IS UNKNOWN"
1071−{- | is true or false
1072−>>> printSQL $ true & isNotUnknown
1076− :: Expression outer grp commons schemas params from (nullity 'PGbool)
1077− -> Condition outer grp commons schemas params from
1078−isNotUnknown b = UnsafeExpression $ renderSQL b <+> "IS NOT UNKNOWN"
1080−-- | >>> printSQL currentDate
1083− :: Expression outer grp commons schemas params from (nullity 'PGdate)
1084−currentDate = UnsafeExpression "CURRENT_DATE"
1086−-- | >>> printSQL currentTime
1089− :: Expression outer grp commons schemas params from (nullity 'PGtimetz)
1090−currentTime = UnsafeExpression "CURRENT_TIME"
1092−-- | >>> printSQL currentTimestamp
1093−-- CURRENT_TIMESTAMP
1095− :: Expression outer grp commons schemas params from (nullity 'PGtimestamptz)
1096−currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
1098−-- | >>> printSQL localTime
1101− :: Expression outer grp commons schemas params from (nullity 'PGtime)
1102−localTime = UnsafeExpression "LOCALTIME"
1104−-- | >>> printSQL localTimestamp
1107− :: Expression outer grp commons schemas params from (nullity 'PGtimestamp)
1108−localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
1110−{-----------------------------------------
1112−-----------------------------------------}
1114−escape :: Char -> String
1126instance IsString
439instance IsString
1127− (Expression outer grp commons schemas params from (nullity 'PGtext)) where
440+ (Expression outer commons grp schemas params from (null 'PGtext)) where
1128 fromString str = UnsafeExpression $
441 fromString str = UnsafeExpression $
1129 "E\'" <> fromString (escape =<< str) <> "\'"
442 "E\'" <> fromString (escape =<< str) <> "\'"
1130instance IsString
443instance IsString
1131− (Expression outer grp commons schemas params from (nullity 'PGtsvector)) where
444+ (Expression outer commons grp schemas params from (null 'PGtsvector)) where
1132− fromString str = cast tsvector . UnsafeExpression $
445+ fromString str = UnsafeExpression . parenthesized . (<> " :: tsvector") $
1133 "E\'" <> fromString (escape =<< str) <> "\'"
446 "E\'" <> fromString (escape =<< str) <> "\'"
1134instance IsString
447instance IsString
1135− (Expression outer grp commons schemas params from (nullity 'PGtsquery)) where
448+ (Expression outer commons grp schemas params from (null 'PGtsquery)) where
1136− fromString str = cast tsquery . UnsafeExpression $
449+ fromString str = UnsafeExpression . parenthesized . (<> " :: tsquery") $
1137 "E\'" <> fromString (escape =<< str) <> "\'"
450 "E\'" <> fromString (escape =<< str) <> "\'"
1139instance Semigroup
452instance Semigroup
453+ (Expression outer commons grp schemas params from (null ('PGvararray ty))) where
454+ (<>) = unsafeBinaryOp "||"
1140− (Expression outer grp commons schemas params from (nullity 'PGtext)) where
456+ (Expression outer commons grp schemas params from (null 'PGjsonb)) where
1141 (<>) = unsafeBinaryOp "||"
457 (<>) = unsafeBinaryOp "||"
1142instance Semigroup
458instance Semigroup
459+ (Expression outer commons grp schemas params from (null 'PGtext)) where
460+ (<>) = unsafeBinaryOp "||"
1143− (Expression outer grp commons schemas params from (nullity 'PGtsvector)) where
462+ (Expression outer commons grp schemas params from (null 'PGtsvector)) where
1144 (<>) = unsafeBinaryOp "||"
463 (<>) = unsafeBinaryOp "||"
1146instance Monoid
465instance Monoid
1147− (Expression outer grp commons schemas params from (nullity 'PGtext)) where
466+ (Expression outer commons grp schemas params from (null 'PGtext)) where
1148 mempty = fromString ""
467 mempty = fromString ""
1149 mappend = (<>)
468 mappend = (<>)
1150instance Monoid
469instance Monoid
1151− (Expression outer grp commons schemas params from (nullity 'PGtsvector)) where
470+ (Expression outer commons grp schemas params from (null 'PGtsvector)) where
1152 mempty = fromString ""
471 mempty = fromString ""
1153 mappend = (<>)
472 mappend = (<>)
1155−-- | >>> printSQL $ lower "ARRRGGG"
1156−-- lower(E'ARRRGGG')
1158− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1159− -- ^ string to lower case
1160− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1161−lower = unsafeFunction "lower"
1163−-- | >>> printSQL $ upper "eeee"
1166− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1167− -- ^ string to upper case
1168− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1169−upper = unsafeFunction "upper"
1171−-- | >>> printSQL $ charLength "four"
1172−-- char_length(E'four')
1174− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1175− -- ^ string to measure
1176− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1177−charLength = unsafeFunction "char_length"
1179−-- | The `like` expression returns true if the @string@ matches
1180−-- the supplied @pattern@. If @pattern@ does not contain percent signs
1181−-- or underscores, then the pattern only represents the string itself;
1182−-- in that case `like` acts like the equals operator. An underscore (_)
1183−-- in pattern stands for (matches) any single character; a percent sign (%)
1184−-- matches any sequence of zero or more characters.
1186−-- >>> printSQL $ "abc" `like` "a%"
1187−-- (E'abc' LIKE E'a%')
1189− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1191− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1193− -> Expression outer grp commons schemas params from (nullity 'PGbool)
1194−like = unsafeBinaryOp "LIKE"
1196−-- | The key word ILIKE can be used instead of LIKE to make the
1197−-- match case-insensitive according to the active locale.
1199−-- >>> printSQL $ "abc" `ilike` "a%"
1200−-- (E'abc' ILIKE E'a%')
1202− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1204− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1206− -> Expression outer grp commons schemas params from (nullity 'PGbool)
1207−ilike = unsafeBinaryOp "ILIKE"
1209−{-----------------------------------------
1210− -- json and jsonb support
1212−See https://www.postgresql.org/docs/10/static/functions-json.html -- most
1213−comments lifted directly from this page.
1215−Table 9.44: json and jsonb operators
1216−-----------------------------------------}
1218−-- | Get JSON value (object field or array element) at a key.
1220− :: (json `In` PGJsonType, key `In` PGJsonKey)
1221− => Expression outer grp commons schemas params from (nullity json)
1222− -> Expression outer grp commons schemas params from (nullity key)
1223− -> Expression outer grp commons schemas params from ('Null json)
1225−(.->) = unsafeBinaryOp "->"
1227−-- | Get JSON value (object field or array element) at a key, as text.
1229− :: (json `In` PGJsonType, key `In` PGJsonKey)
1230− => Expression outer grp commons schemas params from (nullity json)
1231− -> Expression outer grp commons schemas params from (nullity key)
1232− -> Expression outer grp commons schemas params from ('Null 'PGtext)
1234−(.->>) = unsafeBinaryOp "->>"
1236−-- | Get JSON value at a specified path.
1238− :: (json `In` PGJsonType, PGTextArray "(.#>)" path)
1239− => Expression outer grp commons schemas params from (nullity json)
1240− -> Expression outer grp commons schemas params from (nullity path)
1241− -> Expression outer grp commons schemas params from ('Null json)
1243−(.#>) = unsafeBinaryOp "#>"
1245−-- | Get JSON value at a specified path as text.
1247− :: (json `In` PGJsonType, PGTextArray "(.#>>)" path)
1248− => Expression outer grp commons schemas params from (nullity json)
1249− -> Expression outer grp commons schemas params from (nullity path)
1250− -> Expression outer grp commons schemas params from ('Null 'PGtext)
1252−(.#>>) = unsafeBinaryOp "#>>"
1254−-- Additional jsonb operators
1256−-- | Does the left JSON value contain the right JSON path/value entries at the
1259− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1260− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1261− -> Condition outer grp commons schemas params from
1263−(.@>) = unsafeBinaryOp "@>"
1265−-- | Are the left JSON path/value entries contained at the top level within the
1266−-- right JSON value?
1268− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1269− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1270− -> Condition outer grp commons schemas params from
1272−(.<@) = unsafeBinaryOp "<@"
1274−-- | Does the string exist as a top-level key within the JSON value?
1276− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1277− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1278− -> Condition outer grp commons schemas params from
1280−(.?) = unsafeBinaryOp "?"
1282−-- | Do any of these array strings exist as top-level keys?
1284− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1285− -> Expression outer grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1286− -> Condition outer grp commons schemas params from
1288−(.?|) = unsafeBinaryOp "?|"
1290−-- | Do all of these array strings exist as top-level keys?
1292− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1293− -> Expression outer grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1294− -> Condition outer grp commons schemas params from
1296−(.?&) = unsafeBinaryOp "?&"
1298−-- | Concatenate two jsonb values into a new jsonb value.
1300− (Expression outer grp commons schemas params from (nullity 'PGjsonb)) where
1301− (<>) = unsafeBinaryOp "||"
1303−-- | Delete a key or keys from a JSON object, or remove an array element.
1305−-- If the right operand is..
1307−-- @ text @: Delete key/value pair or string element from left operand. Key/value pairs
1308−-- are matched based on their key value.
1310−-- @ text[] @: Delete multiple key/value pairs or string elements
1311−-- from left operand. Key/value pairs are matched based on their key value.
1313−-- @ integer @: Delete the array element with specified index (Negative integers
1314−-- count from the end). Throws an error if top level container is not an array.
1316− :: (key `In` '[ 'PGtext, 'PGvararray ('NotNull 'PGtext), 'PGint4, 'PGint2 ]) -- hlint error without parens here
1317− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1318− -> Expression outer grp commons schemas params from (nullity key)
1319− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1321−(.-.) = unsafeBinaryOp "-"
1323−-- | Delete the field or element with specified path (for JSON arrays, negative
1324−-- integers count from the end)
1326− :: PGTextArray "(#-.)" arrayty
1327− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1328− -> Expression outer grp commons schemas params from (nullity arrayty)
1329− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1331−(#-.) = unsafeBinaryOp "#-"
1333−{-----------------------------------------
1334−Table 9.45: JSON creation functions
1335−-----------------------------------------}
1337−-- | Literal binary JSON
1340− => x -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1341−jsonbLit = cast jsonb . UnsafeExpression
1342− . singleQuotedUtf8 . toStrict . JSON.encode
1347− => x -> Expression outer grp commons schemas params from (nullity 'PGjson)
1348−jsonLit = cast json . UnsafeExpression
1349− . singleQuotedUtf8 . toStrict . JSON.encode
1351−-- | Returns the value as json. Arrays and composites are converted
1352−-- (recursively) to arrays and objects; otherwise, if there is a cast from the
1353−-- type to json, the cast function will be used to perform the conversion;
1354−-- otherwise, a scalar value is produced. For any scalar type other than a
1355−-- number, a Boolean, or a null value, the text representation will be used, in
1356−-- such a fashion that it is a valid json value.
1358− :: Expression outer grp commons schemas params from (nullity ty)
1359− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1360−toJson = unsafeFunction "to_json"
1362−-- | Returns the value as jsonb. Arrays and composites are converted
1363−-- (recursively) to arrays and objects; otherwise, if there is a cast from the
1364−-- type to json, the cast function will be used to perform the conversion;
1365−-- otherwise, a scalar value is produced. For any scalar type other than a
1366−-- number, a Boolean, or a null value, the text representation will be used, in
1367−-- such a fashion that it is a valid jsonb value.
1369− :: Expression outer grp commons schemas params from (nullity ty)
1370− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1371−toJsonb = unsafeFunction "to_jsonb"
1373−-- | Returns the array as a JSON array. A PostgreSQL multidimensional array
1374−-- becomes a JSON array of arrays.
1376− :: PGArray "arrayToJson" arr
1377− => Expression outer grp commons schemas params from (nullity arr)
1378− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1379−arrayToJson = unsafeFunction "array_to_json"
1381−-- | Returns the row as a JSON object.
1383− :: Expression outer grp commons schemas params from (nullity ('PGcomposite ty))
1384− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1385−rowToJson = unsafeFunction "row_to_json"
1387−-- | Builds a possibly-heterogeneously-typed JSON array out of a variadic
1391− => NP (Expression outer grp commons schemas params from) elems
1392− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1393−jsonBuildArray = unsafeVariadicFunction "json_build_array"
1395−-- | Builds a possibly-heterogeneously-typed (binary) JSON array out of a
1396−-- variadic argument list.
1399− => NP (Expression outer grp commons schemas params from) elems
1400− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1401−jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
1404− :: SOP.SListI elems
1405− => NP (Aliased (Expression outer grp commons schemas params from)) elems
1408− (`appEndo` []) . hcfoldMap (Proxy :: SOP.Proxy SOP.Top)
1409− (\(col `As` name) -> Endo $ \xs ->
1410− renderAliasString name : renderSQL col : xs)
1412− renderAliasString :: KnownSymbol alias => Alias alias -> ByteString
1413− renderAliasString = singleQuotedText . fromString . symbolVal
1415−-- | Builds a possibly-heterogeneously-typed JSON object out of a variadic
1416−-- argument list. The elements of the argument list must alternate between text
1419− :: SOP.SListI elems
1420− => NP (Aliased (Expression outer grp commons schemas params from)) elems
1421− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1423− = unsafeFunction "json_build_object"
1426− . unsafeRowFunction
1428−-- | Builds a possibly-heterogeneously-typed (binary) JSON object out of a
1429−-- variadic argument list. The elements of the argument list must alternate
1430−-- between text and values.
1432− :: SOP.SListI elems
1433− => NP (Aliased (Expression outer grp commons schemas params from)) elems
1434− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1436− = unsafeFunction "jsonb_build_object"
1439− . unsafeRowFunction
1441−-- | Builds a JSON object out of a text array. The array must have either
1442−-- exactly one dimension with an even number of members, in which case they are
1443−-- taken as alternating key/value pairs, or two dimensions such that each inner
1444−-- array has exactly two elements, which are taken as a key/value pair.
1446− :: PGArrayOf "jsonObject" arr ('NotNull 'PGtext)
1447− => Expression outer grp commons schemas params from (nullity arr)
1448− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1449−jsonObject = unsafeFunction "json_object"
1451−-- | Builds a binary JSON object out of a text array. The array must have either
1452−-- exactly one dimension with an even number of members, in which case they are
1453−-- taken as alternating key/value pairs, or two dimensions such that each inner
1454−-- array has exactly two elements, which are taken as a key/value pair.
1456− :: PGArrayOf "jsonbObject" arr ('NotNull 'PGtext)
1457− => Expression outer grp commons schemas params from (nullity arr)
1458− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1459−jsonbObject = unsafeFunction "jsonb_object"
1461−-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
1462−-- keys and one for values, that are zipped pairwise to create a JSON object.
1464− :: ( PGArrayOf "jsonZipObject" keysArray ('NotNull 'PGtext)
1465− , PGArrayOf "jsonZipObject" valuesArray ('NotNull 'PGtext))
1466− => Expression outer grp commons schemas params from (nullity keysArray)
1467− -> Expression outer grp commons schemas params from (nullity valuesArray)
1468− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1469−jsonZipObject ks vs =
1470− unsafeVariadicFunction "json_object" (ks :* vs :* Nil)
1472−-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
1473−-- keys and one for values, that are zipped pairwise to create a binary JSON
1476− :: ( PGArrayOf "jsonbZipObject" keysArray ('NotNull 'PGtext)
1477− , PGArrayOf "jsonbZipObject" valuesArray ('NotNull 'PGtext))
1478− => Expression outer grp commons schemas params from (nullity keysArray)
1479− -> Expression outer grp commons schemas params from (nullity valuesArray)
1480− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1481−jsonbZipObject ks vs =
1482− unsafeVariadicFunction "jsonb_object" (ks :* vs :* Nil)
1484−{-----------------------------------------
1485−Table 9.46: JSON processing functions
1486−-----------------------------------------}
1488−-- | Returns the number of elements in the outermost JSON array.
1490− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1491− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1492−jsonArrayLength = unsafeFunction "json_array_length"
1494−-- | Returns the number of elements in the outermost binary JSON array.
1496− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1497− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1498−jsonbArrayLength = unsafeFunction "jsonb_array_length"
1500−-- | Returns JSON value pointed to by the given path (equivalent to #>
1504− => Expression outer grp commons schemas params from (nullity 'PGjson)
1505− -> NP (Expression outer grp commons schemas params from) elems
1506− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1507−jsonExtractPath x xs =
1508− unsafeVariadicFunction "json_extract_path" (x :* xs)
1510−-- | Returns JSON value pointed to by the given path (equivalent to #>
1514− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1515− -> NP (Expression outer grp commons schemas params from) elems
1516− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1517−jsonbExtractPath x xs =
1518− unsafeVariadicFunction "jsonb_extract_path" (x :* xs)
1520−-- | Returns JSON value pointed to by the given path (equivalent to #>
1521−-- operator), as text.
1522−jsonExtractPathAsText
1524− => Expression outer grp commons schemas params from (nullity 'PGjson)
1525− -> NP (Expression outer grp commons schemas params from) elems
1526− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1527−jsonExtractPathAsText x xs =
1528− unsafeVariadicFunction "json_extract_path_text" (x :* xs)
1530−-- | Returns JSON value pointed to by the given path (equivalent to #>
1531−-- operator), as text.
1532−jsonbExtractPathAsText
1534− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1535− -> NP (Expression outer grp commons schemas params from) elems
1536− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1537−jsonbExtractPathAsText x xs =
1538− unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
1540−-- | Returns the type of the outermost JSON value as a text string. Possible
1541−-- types are object, array, string, number, boolean, and null.
1543− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1544− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1545−jsonTypeof = unsafeFunction "json_typeof"
1547−-- | Returns the type of the outermost binary JSON value as a text string.
1548−-- Possible types are object, array, string, number, boolean, and null.
1550− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1551− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1552−jsonbTypeof = unsafeFunction "jsonb_typeof"
1554−-- | Returns its argument with all object fields that have null values omitted.
1555−-- Other null values are untouched.
1557− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1558− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1559−jsonStripNulls = unsafeFunction "json_strip_nulls"
1561−-- | Returns its argument with all object fields that have null values omitted.
1562−-- Other null values are untouched.
1564− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1565− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1566−jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
1568−-- | @ jsonbSet target path new_value create_missing @
1570−-- Returns target with the section designated by path replaced by new_value,
1571−-- or with new_value added if create_missing is true ( default is true) and the
1572−-- item designated by path does not exist. As with the path orientated
1573−-- operators, negative integers that appear in path count from the end of JSON
1576− :: PGTextArray "jsonbSet" arr
1577− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1578− -> Expression outer grp commons schemas params from (nullity arr)
1579− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1580− -> Maybe (Expression outer grp commons schemas params from (nullity 'PGbool))
1581− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1582−jsonbSet tgt path val createMissing = case createMissing of
1583− Just m -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* m :* Nil)
1584− Nothing -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* Nil)
1586−-- | @ jsonbInsert target path new_value insert_after @
1588−-- Returns target with new_value inserted. If target section designated by
1589−-- path is in a JSONB array, new_value will be inserted before target or after
1590−-- if insert_after is true (default is false). If target section designated by
1591−-- path is in JSONB object, new_value will be inserted only if target does not
1592−-- exist. As with the path orientated operators, negative integers that appear
1593−-- in path count from the end of JSON arrays.
1595− :: PGTextArray "jsonbInsert" arr
1596− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1597− -> Expression outer grp commons schemas params from (nullity arr)
1598− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1599− -> Maybe (Expression outer grp commons schemas params from (nullity 'PGbool))
1600− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1601−jsonbInsert tgt path val insertAfter = case insertAfter of
1602− Just i -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* i :* Nil)
1603− Nothing -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* Nil)
1605−-- | Returns its argument as indented JSON text.
1607− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1608− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1609−jsonbPretty = unsafeFunction "jsonb_pretty"
1611−(@@) :: ComparisonOperator (nullity 'PGtsvector) (nullity 'PGtsquery)
1612−(@@) = unsafeBinaryOp "@@"
1614−(.&) :: Operator (nullity 'PGtsquery) (nullity 'PGtsquery) (nullity 'PGtsquery)
1615−(.&) = unsafeBinaryOp "&&"
1617−(.|) :: Operator (nullity 'PGtsquery) (nullity 'PGtsquery) (nullity 'PGtsquery)
1618−(.|) = unsafeBinaryOp "||"
1621− :: Expression outer grp commons schemas params from (nullity 'PGtsquery)
1622− -> Expression outer grp commons schemas params from (nullity 'PGtsquery)
1623−(.!) = unsafeUnaryOp "!!"
1625−(<->) :: Operator (nullity 'PGtsquery) (nullity 'PGtsquery) (nullity 'PGtsquery)
1626−(<->) = unsafeBinaryOp "<->"
1628−{-----------------------------------------
1630−-----------------------------------------}
1633−`Aggregate` functions compute a single result from a set of input values.
1634−`Aggregate` functions can be used as `GroupedBy` `Expression`s as well
1635−as `WindowFunction`s.
1637−class Aggregate expr1 expr2 aggr
1638− | aggr -> expr1, aggr -> expr2 where
1640− -- | A special aggregation that does not require an input
1644− -- expression :: Expression '[] ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
1645− -- expression = countStar
1646− -- in printSQL expression
1649− countStar :: aggr ('NotNull 'PGint8)
1653− -- expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1654− -- expression = count (All #col)
1655− -- in printSQL expression
1657− -- count(ALL "col")
1661− -> aggr ('NotNull 'PGint8)
1665− -- expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('NotNull 'PGnumeric)
1666− -- expression = sum_ (Distinct #col)
1667− -- in printSQL expression
1669− -- sum(DISTINCT "col")
1672− => expr1 (nullity ty)
1673− -> aggr ('NotNull ty)
1675− -- | input values, including nulls, concatenated into an array
1678− -> aggr ('Null ('PGvararray ty))
1680− -- | aggregates values as a JSON array
1683− -> aggr ('Null 'PGjson)
1685− -- | aggregates values as a JSON array
1688− -> aggr ('Null 'PGjsonb)
1691− the bitwise AND of all non-null input values, or null if none
1694− expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] ('Null 'PGint4)
1695− expression = bitAnd (Distinct #col)
1696− in printSQL expression
1698− bit_and(DISTINCT "col")
1701− :: int `In` PGIntegral
1702− => expr1 (nullity int)
1703− -- ^ what to aggregate
1704− -> aggr ('Null int)
1707− the bitwise OR of all non-null input values, or null if none
1711− expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] ('Null 'PGint4)
1712− expression = bitOr (All #col)
1713− in printSQL expression
1718− :: int `In` PGIntegral
1719− => expr1 (nullity int)
1720− -- ^ what to aggregate
1721− -> aggr ('Null int)
1724− true if all input values are true, otherwise false
1728− winFun :: WindowFunction '[] 'Ungrouped commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] ('Null 'PGbool)
1729− winFun = boolAnd #col
1735− :: expr1 (nullity 'PGbool)
1736− -- ^ what to aggregate
1737− -> aggr ('Null 'PGbool)
1740− true if at least one input value is true, otherwise false
1744− expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] ('Null 'PGbool)
1745− expression = boolOr (All #col)
1746− in printSQL expression
1751− :: expr1(nullity 'PGbool)
1752− -- ^ what to aggregate
1753− -> aggr ('Null 'PGbool)
1756− equivalent to `boolAnd`
1760− expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] ('Null 'PGbool)
1761− expression = every (Distinct #col)
1762− in printSQL expression
1764− every(DISTINCT "col")
1767− :: expr1 (nullity 'PGbool)
1768− -- ^ what to aggregate
1769− -> aggr ('Null 'PGbool)
1771− {- |maximum value of expression across all input values-}
1773− :: expr1 (nullity ty)
1774− -- ^ what to maximize
1777− -- | minimum value of expression across all input values
1779− :: expr1 (nullity ty)
1780− -- ^ what to minimize
1783− -- | the average (arithmetic mean) of all input values
1785− :: expr1 (nullity ty)
1786− -- ^ what to average
1787− -> aggr ('Null (PGAvg ty))
1789− {- | correlation coefficient
1792− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1793− expression = corr (All (#y :*: #x))
1794− in printSQL expression
1799− :: expr2 (nullity 'PGfloat8)
1800− -> aggr ('Null 'PGfloat8)
1802− {- | population covariance
1805− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1806− expression = covarPop (All (#y :*: #x))
1807− in printSQL expression
1809− covar_pop(ALL "y", "x")
1812− :: expr2 (nullity 'PGfloat8)
1813− -> aggr ('Null 'PGfloat8)
1815− {- | sample covariance
1818− winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1819− winFun = covarSamp (#y :*: #x)
1822− covar_samp("y", "x")
1825− :: expr2 (nullity 'PGfloat8)
1826− -> aggr ('Null 'PGfloat8)
1828− {- | average of the independent variable (sum(X)/N)
1831− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1832− expression = regrAvgX (All (#y :*: #x))
1833− in printSQL expression
1835− regr_avgx(ALL "y", "x")
1838− :: expr2 (nullity 'PGfloat8)
1839− -> aggr ('Null 'PGfloat8)
1841− {- | average of the dependent variable (sum(Y)/N)
1844− winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1845− winFun = regrAvgY (#y :*: #x)
1848− regr_avgy("y", "x")
1851− :: expr2 (nullity 'PGfloat8)
1852− -> aggr ('Null 'PGfloat8)
1854− {- | number of input rows in which both expressions are nonnull
1857− winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGint8)
1858− winFun = regrCount (#y :*: #x)
1861− regr_count("y", "x")
1864− :: expr2 (nullity 'PGfloat8)
1865− -> aggr ('Null 'PGint8)
1867− {- | y-intercept of the least-squares-fit linear equation determined by the (X, Y) pairs
1870− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1871− expression = regrIntercept (All (#y :*: #x))
1872− in printSQL expression
1874− regr_intercept(ALL "y", "x")
1877− :: expr2 (nullity 'PGfloat8)
1878− -> aggr ('Null 'PGfloat8)
1880− -- | @regr_r2(Y, X)@, square of the correlation coefficient
1882− :: expr2 (nullity 'PGfloat8)
1883− -> aggr ('Null 'PGfloat8)
1885− -- | @regr_slope(Y, X)@, slope of the least-squares-fit linear equation
1886− -- determined by the (X, Y) pairs
1888− :: expr2 (nullity 'PGfloat8)
1889− -> aggr ('Null 'PGfloat8)
1891− -- | @regr_sxx(Y, X)@, sum(X^2) - sum(X)^2/N
1892− -- (“sum of squares” of the independent variable)
1894− :: expr2 (nullity 'PGfloat8)
1895− -> aggr ('Null 'PGfloat8)
1897− -- | @regr_sxy(Y, X)@, sum(X*Y) - sum(X) * sum(Y)/N
1898− -- (“sum of products” of independent times dependent variable)
1900− :: expr2 (nullity 'PGfloat8)
1901− -> aggr ('Null 'PGfloat8)
1903− -- | @regr_syy(Y, X)@, sum(Y^2) - sum(Y)^2/N
1904− -- (“sum of squares” of the dependent variable)
1906− :: expr2 (nullity 'PGfloat8)
1907− -> aggr ('Null 'PGfloat8)
1909− -- | historical alias for `stddevSamp`
1911− :: expr1 (nullity ty)
1912− -> aggr ('Null (PGAvg ty))
1914− -- | population standard deviation of the input values
1916− :: expr1 (nullity ty)
1917− -> aggr ('Null (PGAvg ty))
1919− -- | sample standard deviation of the input values
1921− :: expr1 (nullity ty)
1922− -> aggr ('Null (PGAvg ty))
1924− -- | historical alias for `varSamp`
1926− :: expr1 (nullity ty)
1927− -> aggr ('Null (PGAvg ty))
1929− -- | population variance of the input values
1930− -- (square of the population standard deviation)
1932− :: expr1 (nullity ty)
1933− -> aggr ('Null (PGAvg ty))
1935− -- | sample variance of the input values
1936− -- (square of the sample standard deviation)
1938− :: expr1 (nullity ty)
1939− -> aggr ('Null (PGAvg ty))
1942−`Distinction`s are used for the input of `Aggregate` `Expression`s.
1943−`All` invokes the aggregate once for each input row.
1944−`Distinct` invokes the aggregate once for each distinct value of the expression
1945−(or distinct set of values, for multiple expressions) found in the input
1947−data Distinction expr ty
1949− | Distinct (expr ty)
1950− deriving (GHC.Generic,Show,Eq,Ord)
1951−instance NFData (Distinction (Expression outer grp commons schemas params from) ty)
1952−instance RenderSQL (Distinction (Expression outer grp commons schemas params from) ty) where
1954− All x -> "ALL" <+> renderSQL x
1955− Distinct x -> "DISTINCT" <+> renderSQL x
1958− ( Expression outer grp commons schemas params from
1960− Expression outer grp commons schemas params from) xty ) where
1963− "ALL" <+> renderSQL x <> ", " <> renderSQL x'
1964− Distinct (x :*: x') ->
1965− "DISTINCT" <+> renderSQL x <> ", " <> renderSQL x'
1968− (Distinction (Expression outer 'Ungrouped commons schemas params from))
1969− (Distinction (Expression outer 'Ungrouped commons schemas params from :*: Expression outer 'Ungrouped commons schemas params from))
1970− (Expression outer ('Grouped bys) commons schemas params from) where
1971− countStar = UnsafeExpression "count(*)"
1972− count = unsafeAggregate1 "count"
1973− sum_ = unsafeAggregate1 "sum"
1974− arrayAgg = unsafeAggregate1 "array_agg"
1975− jsonAgg = unsafeAggregate1 "json_agg"
1976− jsonbAgg = unsafeAggregate1 "jsonb_agg"
1977− bitAnd = unsafeAggregate1 "bit_and"
1978− bitOr = unsafeAggregate1 "bit_or"
1979− boolAnd = unsafeAggregate1 "bool_and"
1980− boolOr = unsafeAggregate1 "bool_or"
1981− every = unsafeAggregate1 "every"
1982− max_ = unsafeAggregate1 "max"
1983− min_ = unsafeAggregate1 "min"
1984− avg = unsafeAggregate1 "avg"
1985− corr = unsafeAggregate2 "corr"
1986− covarPop = unsafeAggregate2 "covar_pop"
1987− covarSamp = unsafeAggregate2 "covar_samp"
1988− regrAvgX = unsafeAggregate2 "regr_avgx"
1989− regrAvgY = unsafeAggregate2 "regr_avgy"
1990− regrCount = unsafeAggregate2 "regr_count"
1991− regrIntercept = unsafeAggregate2 "regr_intercept"
1992− regrR2 = unsafeAggregate2 "regr_r2"
1993− regrSlope = unsafeAggregate2 "regr_slope"
1994− regrSxx = unsafeAggregate2 "regr_sxx"
1995− regrSxy = unsafeAggregate2 "regr_sxy"
1996− regrSyy = unsafeAggregate2 "regr_syy"
1997− stddev = unsafeAggregate1 "stddev"
1998− stddevPop = unsafeAggregate1 "stddev_pop"
1999− stddevSamp = unsafeAggregate1 "stddev_samp"
2000− variance = unsafeAggregate1 "variance"
2001− varPop = unsafeAggregate1 "var_pop"
2002− varSamp = unsafeAggregate1 "var_samp"
2004− (Expression outer grp commons schemas params from)
2005− (Expression outer grp commons schemas params from :*: Expression outer grp commons schemas params from)
2006− (WindowFunction outer grp commons schemas params from) where
2007− countStar = UnsafeWindowFunction "count(*)"
2008− count = unsafeWindowFunction1 "count"
2009− sum_ = unsafeWindowFunction1 "sum"
2010− arrayAgg = unsafeWindowFunction1 "array_agg"
2011− jsonAgg = unsafeWindowFunction1 "json_agg"
2012− jsonbAgg = unsafeWindowFunction1 "jsonb_agg"
2013− bitAnd = unsafeWindowFunction1 "bit_and"
2014− bitOr = unsafeWindowFunction1 "bit_or"
2015− boolAnd = unsafeWindowFunction1 "bool_and"
2016− boolOr = unsafeWindowFunction1 "bool_or"
2017− every = unsafeWindowFunction1 "every"
2018− max_ = unsafeWindowFunction1 "max"
2019− min_ = unsafeWindowFunction1 "min"
2020− avg = unsafeWindowFunction1 "avg"
2021− corr = unsafeWindowFunction2 "corr"
2022− covarPop = unsafeWindowFunction2 "covar_pop"
2023− covarSamp = unsafeWindowFunction2 "covar_samp"
2024− regrAvgX = unsafeWindowFunction2 "regr_avgx"
2025− regrAvgY = unsafeWindowFunction2 "regr_avgy"
2026− regrCount = unsafeWindowFunction2 "regr_count"
2027− regrIntercept = unsafeWindowFunction2 "regr_intercept"
2028− regrR2 = unsafeWindowFunction2 "regr_r2"
2029− regrSlope = unsafeWindowFunction2 "regr_slope"
2030− regrSxx = unsafeWindowFunction2 "regr_sxx"
2031− regrSxy = unsafeWindowFunction2 "regr_sxy"
2032− regrSyy = unsafeWindowFunction2 "regr_syy"
2033− stddev = unsafeWindowFunction1 "stddev"
2034− stddevPop = unsafeWindowFunction1 "stddev_pop"
2035− stddevSamp = unsafeWindowFunction1 "stddev_samp"
2036− variance = unsafeWindowFunction1 "variance"
2037− varPop = unsafeWindowFunction1 "var_pop"
2038− varSamp = unsafeWindowFunction1 "var_samp"
2040−-- | escape hatch to define aggregate functions
2042− :: ByteString -- ^ aggregate function
2043− -> Distinction (Expression outer 'Ungrouped commons schemas params from) xty
2044− -> Expression outer ('Grouped bys) commons schemas params from yty
2045−unsafeAggregate1 fun x = UnsafeExpression $ mconcat
2046− [fun, "(", renderSQL x, ")"]
2048−-- | escape hatch to define aggregate binary functions
2050− :: ByteString -- ^ aggregate function
2051− -> Distinction (Expression outer 'Ungrouped commons schemas params from :*: Expression outer 'Ungrouped commons schemas params from) xty
2052− -> Expression outer ('Grouped bys) commons schemas params from yty
2053−unsafeAggregate2 fun xx = UnsafeExpression $ mconcat
2054− [fun, "(", renderSQL xx, ")"]
2056−-- | A type family that calculates `PGAvg` type of a `PGType`.
2057−type family PGAvg ty where
2058− PGAvg 'PGint2 = 'PGnumeric
2059− PGAvg 'PGint4 = 'PGnumeric
2060− PGAvg 'PGint8 = 'PGnumeric
2061− PGAvg 'PGnumeric = 'PGnumeric
2062− PGAvg 'PGfloat4 = 'PGfloat8
2063− PGAvg 'PGfloat8 = 'PGfloat8
2064− PGAvg 'PGinterval = 'PGinterval
2065− PGAvg pg = TypeError
2066− ('Text "Squeal type error: No average for " ':<>: 'ShowType pg)
2068−{-----------------------------------------
2070−-----------------------------------------}
2072−-- | `TypeExpression`s are used in `cast`s and
2073−-- `Squeal.PostgreSQL.Definition.createTable` commands.
2074−newtype TypeExpression (schemas :: SchemasType) (ty :: NullityType)
2075− = UnsafeTypeExpression { renderTypeExpression :: ByteString }
2076− deriving (GHC.Generic,Show,Eq,Ord,NFData)
2077−instance RenderSQL (TypeExpression schemas ty) where
2078− renderSQL = renderTypeExpression
2080−-- | The enum or composite type in a `Typedef` can be expressed by its alias.
2082− :: (Has sch schemas schema, Has td schema ('Typedef ty))
2083− => QualifiedAlias sch td
2084− -> TypeExpression schemas (nullity ty)
2085−typedef = UnsafeTypeExpression . renderSQL
2087−-- | The composite type corresponding to a `Table` definition can be expressed
2090− :: (Has sch schemas schema, Has tab schema ('Table table))
2091− => QualifiedAlias sch tab
2092− -> TypeExpression schemas (nullity ('PGcomposite (TableToRow table)))
2093−typetable = UnsafeTypeExpression . renderSQL
2095−-- | The composite type corresponding to a `View` definition can be expressed
2098− :: (Has sch schemas schema, Has vw schema ('View view))
2099− => QualifiedAlias sch vw
2100− -> TypeExpression schemas (nullity ('PGcomposite view))
2101−typeview = UnsafeTypeExpression . renderSQL
2103−-- | logical Boolean (true/false)
2104−bool :: TypeExpression schemas (nullity 'PGbool)
2105−bool = UnsafeTypeExpression "bool"
2106−-- | signed two-byte integer
2107−int2, smallint :: TypeExpression schemas (nullity 'PGint2)
2108−int2 = UnsafeTypeExpression "int2"
2109−smallint = UnsafeTypeExpression "smallint"
2110−-- | signed four-byte integer
2111−int4, int, integer :: TypeExpression schemas (nullity 'PGint4)
2112−int4 = UnsafeTypeExpression "int4"
2113−int = UnsafeTypeExpression "int"
2114−integer = UnsafeTypeExpression "integer"
2115−-- | signed eight-byte integer
2116−int8, bigint :: TypeExpression schemas (nullity 'PGint8)
2117−int8 = UnsafeTypeExpression "int8"
2118−bigint = UnsafeTypeExpression "bigint"
2119−-- | arbitrary precision numeric type
2120−numeric :: TypeExpression schemas (nullity 'PGnumeric)
2121−numeric = UnsafeTypeExpression "numeric"
2122−-- | single precision floating-point number (4 bytes)
2123−float4, real :: TypeExpression schemas (nullity 'PGfloat4)
2124−float4 = UnsafeTypeExpression "float4"
2125−real = UnsafeTypeExpression "real"
2126−-- | double precision floating-point number (8 bytes)
2127−float8, doublePrecision :: TypeExpression schemas (nullity 'PGfloat8)
2128−float8 = UnsafeTypeExpression "float8"
2129−doublePrecision = UnsafeTypeExpression "double precision"
2130−-- | currency amount
2131−money :: TypeExpression schema (nullity 'PGmoney)
2132−money = UnsafeTypeExpression "money"
2133−-- | variable-length character string
2134−text :: TypeExpression schemas (nullity 'PGtext)
2135−text = UnsafeTypeExpression "text"
2136−-- | fixed-length character string
2138− :: forall n schemas nullity. (KnownNat n, 1 <= n)
2139− => TypeExpression schemas (nullity ('PGchar n))
2140−char = UnsafeTypeExpression $ "char(" <> renderNat @n <> ")"
2141−character = UnsafeTypeExpression $ "character(" <> renderNat @n <> ")"
2142−-- | variable-length character string
2143−varchar, characterVarying
2144− :: forall n schemas nullity. (KnownNat n, 1 <= n)
2145− => TypeExpression schemas (nullity ('PGvarchar n))
2146−varchar = UnsafeTypeExpression $ "varchar(" <> renderNat @n <> ")"
2147−characterVarying = UnsafeTypeExpression $
2148− "character varying(" <> renderNat @n <> ")"
2149−-- | binary data ("byte array")
2150−bytea :: TypeExpression schemas (nullity 'PGbytea)
2151−bytea = UnsafeTypeExpression "bytea"
2152−-- | date and time (no time zone)
2153−timestamp :: TypeExpression schemas (nullity 'PGtimestamp)
2154−timestamp = UnsafeTypeExpression "timestamp"
2155−-- | date and time, including time zone
2156−timestampWithTimeZone :: TypeExpression schemas (nullity 'PGtimestamptz)
2157−timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
2158−-- | calendar date (year, month, day)
2159−date :: TypeExpression schemas (nullity 'PGdate)
2160−date = UnsafeTypeExpression "date"
2161−-- | time of day (no time zone)
2162−time :: TypeExpression schemas (nullity 'PGtime)
2163−time = UnsafeTypeExpression "time"
2164−-- | time of day, including time zone
2165−timeWithTimeZone :: TypeExpression schemas (nullity 'PGtimetz)
2166−timeWithTimeZone = UnsafeTypeExpression "time with time zone"
2168−interval :: TypeExpression schemas (nullity 'PGinterval)
2169−interval = UnsafeTypeExpression "interval"
2170−-- | universally unique identifier
2171−uuid :: TypeExpression schemas (nullity 'PGuuid)
2172−uuid = UnsafeTypeExpression "uuid"
2173−-- | IPv4 or IPv6 host address
2174−inet :: TypeExpression schemas (nullity 'PGinet)
2175−inet = UnsafeTypeExpression "inet"
2176−-- | textual JSON data
2177−json :: TypeExpression schemas (nullity 'PGjson)
2178−json = UnsafeTypeExpression "json"
2179−-- | binary JSON data, decomposed
2180−jsonb :: TypeExpression schemas (nullity 'PGjsonb)
2181−jsonb = UnsafeTypeExpression "jsonb"
2182−-- | variable length array
2184− :: TypeExpression schemas pg
2185− -> TypeExpression schemas (nullity ('PGvararray pg))
2186−vararray ty = UnsafeTypeExpression $ renderSQL ty <> "[]"
2187−-- | fixed length array
2189−-- >>> renderSQL (fixarray @'[2] json)
2192− :: forall dims schemas nullity pg. SOP.All KnownNat dims
2193− => TypeExpression schemas pg
2194− -> TypeExpression schemas (nullity ('PGfixarray dims pg))
2195−fixarray ty = UnsafeTypeExpression $
2196− renderSQL ty <> renderDims @dims
2198− renderDims :: forall ns. SOP.All KnownNat ns => ByteString
2202− . ByteString.intercalate "]["
2204− $ hcmap (SOP.Proxy @KnownNat)
2205− (K . fromString . show . natVal)
2206− (hpure SOP.Proxy :: NP SOP.Proxy ns)
2207−tsvector :: TypeExpression schemas (nullity 'PGtsvector)
2208−tsvector = UnsafeTypeExpression "tsvector"
2209−tsquery :: TypeExpression schemas (nullity 'PGtsquery)
2210−tsquery = UnsafeTypeExpression "tsquery"
2212−-- | `pgtype` is a demoted version of a `PGType`
2213−class PGTyped schemas (ty :: NullityType) where
2214− pgtype :: TypeExpression schemas ty
2215−instance PGTyped schemas (nullity 'PGbool) where pgtype = bool
2216−instance PGTyped schemas (nullity 'PGint2) where pgtype = int2
2217−instance PGTyped schemas (nullity 'PGint4) where pgtype = int4
2218−instance PGTyped schemas (nullity 'PGint8) where pgtype = int8
2219−instance PGTyped schemas (nullity 'PGnumeric) where pgtype = numeric
2220−instance PGTyped schemas (nullity 'PGfloat4) where pgtype = float4
2221−instance PGTyped schemas (nullity 'PGfloat8) where pgtype = float8
2222−instance PGTyped schemas (nullity 'PGmoney) where pgtype = money
2223−instance PGTyped schemas (nullity 'PGtext) where pgtype = text
2224−instance (KnownNat n, 1 <= n)
2225− => PGTyped schemas (nullity ('PGchar n)) where pgtype = char @n
2226−instance (KnownNat n, 1 <= n)
2227− => PGTyped schemas (nullity ('PGvarchar n)) where pgtype = varchar @n
2228−instance PGTyped schemas (nullity 'PGbytea) where pgtype = bytea
2229−instance PGTyped schemas (nullity 'PGtimestamp) where pgtype = timestamp
2230−instance PGTyped schemas (nullity 'PGtimestamptz) where pgtype = timestampWithTimeZone
2231−instance PGTyped schemas (nullity 'PGdate) where pgtype = date
2232−instance PGTyped schemas (nullity 'PGtime) where pgtype = time
2233−instance PGTyped schemas (nullity 'PGtimetz) where pgtype = timeWithTimeZone
2234−instance PGTyped schemas (nullity 'PGinterval) where pgtype = interval
2235−instance PGTyped schemas (nullity 'PGuuid) where pgtype = uuid
2236−instance PGTyped schemas (nullity 'PGjson) where pgtype = json
2237−instance PGTyped schemas (nullity 'PGjsonb) where pgtype = jsonb
2238−instance PGTyped schemas ty
2239− => PGTyped schemas (nullity ('PGvararray ty)) where
2240− pgtype = vararray (pgtype @schemas @ty)
2241−instance (SOP.All KnownNat dims, PGTyped schemas ty)
2242− => PGTyped schemas (nullity ('PGfixarray dims ty)) where
2243− pgtype = fixarray @dims (pgtype @schemas @ty)
2244−instance PGTyped schemas (nullity 'PGtsvector) where pgtype = tsvector
2245−instance PGTyped schemas (nullity 'PGtsquery) where pgtype = tsquery
2247−{-----------------------------------------
2249−-----------------------------------------}
2251−-- | `SortExpression`s are used by `orderBy` to optionally sort the results
2252−-- of a `Squeal.PostgreSQL.Query.Query`. `Asc` or `Desc`
2253−-- set the sort direction of a `NotNull` result
2254−-- column to ascending or descending. Ascending order puts smaller values
2255−-- first, where "smaller" is defined in terms of the `.<` operator. Similarly,
2256−-- descending order is determined with the `.>` operator. `AscNullsFirst`,
2257−-- `AscNullsLast`, `DescNullsFirst` and `DescNullsLast` options are used to
2258−-- determine whether nulls appear before or after non-null values in the sort
2259−-- ordering of a `Null` result column.
2260−data SortExpression outer grp commons schemas params from where
2262− :: Expression outer grp commons schemas params from ('NotNull ty)
2263− -> SortExpression outer grp commons schemas params from
2265− :: Expression outer grp commons schemas params from ('NotNull ty)
2266− -> SortExpression outer grp commons schemas params from
2268− :: Expression outer grp commons schemas params from ('Null ty)
2269− -> SortExpression outer grp commons schemas params from
2271− :: Expression outer grp commons schemas params from ('Null ty)
2272− -> SortExpression outer grp commons schemas params from
2274− :: Expression outer grp commons schemas params from ('Null ty)
2275− -> SortExpression outer grp commons schemas params from
2277− :: Expression outer grp commons schemas params from ('Null ty)
2278− -> SortExpression outer grp commons schemas params from
2279−deriving instance Show (SortExpression outer grp commons schemas params from)
2282−The `orderBy` clause causes the result rows of a `Squeal.PostgreSQL.Query.TableExpression`
2283−to be sorted according to the specified `SortExpression`(s).
2284−If two rows are equal according to the leftmost expression,
2285−they are compared according to the next expression and so on.
2286−If they are equal according to all specified expressions,
2287−they are returned in an implementation-dependent order.
2289−You can also control the order in which rows are processed by window functions
2290−using `orderBy` within `Squeal.PostgreSQL.Query.Over`.
2292−class OrderBy expr where
2294− :: [SortExpression outer grp commons schemas params from]
2295− -> expr outer grp commons schemas params from
2296− -> expr outer grp commons schemas params from
2298−-- | Render a `SortExpression`.
2299−instance RenderSQL (SortExpression outer grp commons schemas params from) where
2301− Asc expression -> renderSQL expression <+> "ASC"
2302− Desc expression -> renderSQL expression <+> "DESC"
2303− AscNullsFirst expression -> renderSQL expression
2304− <+> "ASC NULLS FIRST"
2305− DescNullsFirst expression -> renderSQL expression
2306− <+> "DESC NULLS FIRST"
2307− AscNullsLast expression -> renderSQL expression <+> "ASC NULLS LAST"
2308− DescNullsLast expression -> renderSQL expression <+> "DESC NULLS LAST"
2310−-- | A `WindowDefinition` is a set of table rows that are somehow related
2311−-- to the current row
2312−data WindowDefinition outer grp commons schemas params from where
2315− => NP (Expression outer grp commons schemas params from) bys
2316− -> [SortExpression outer grp commons schemas params from]
2317− -> WindowDefinition outer grp commons schemas params from
2319−instance OrderBy WindowDefinition where
2320− orderBy sortsR (WindowDefinition parts sortsL)
2321− = WindowDefinition parts (sortsL ++ sortsR)
2323−instance RenderSQL (WindowDefinition outer commons schemas from grp params) where
2324− renderSQL (WindowDefinition part ord) =
2325− renderPartitionByClause part <> renderOrderByClause ord
2327− renderPartitionByClause = \case
2329− parts -> "PARTITION" <+> "BY" <+> renderCommaSeparated renderExpression parts
2330− renderOrderByClause = \case
2332− srts -> " ORDER" <+> "BY"
2333− <+> commaSeparated (renderSQL <$> srts)
2336−The `partitionBy` clause within `Squeal.PostgreSQL.Query.Over` divides the rows into groups,
2337−or partitions, that share the same values of the `partitionBy` `Expression`(s).
2338−For each row, the window function is computed across the rows that fall into
2339−the same partition as the current row.
2343− => NP (Expression outer grp commons schemas params from) bys
2344− -> WindowDefinition outer grp commons schemas params from
2345−partitionBy bys = WindowDefinition bys []
2348−A window function performs a calculation across a set of table rows
2349−that are somehow related to the current row. This is comparable to the type
2350−of calculation that can be done with an aggregate function.
2351−However, window functions do not cause rows to become grouped into a single
2352−output row like non-window aggregate calls would.
2353−Instead, the rows retain their separate identities.
2354−Behind the scenes, the window function is able to access more than
2355−just the current row of the query result.
2357−newtype WindowFunction
2358− (outer :: FromType)
2360− (commons :: FromType)
2361− (schemas :: SchemasType)
2362− (params :: [NullityType])
2364− (ty :: NullityType)
2365− = UnsafeWindowFunction { renderWindowFunction :: ByteString }
2366− deriving (GHC.Generic,Show,Eq,Ord,NFData)
2368−instance RenderSQL (WindowFunction outer grp commons schemas params from ty) where
2369− renderSQL = renderWindowFunction
2371−unsafeWindowFunction1
2373− -> Expression outer grp commons schemas params from ty0
2374− -> WindowFunction outer grp commons schemas params from ty1
2375−unsafeWindowFunction1 fun x
2376− = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x)
2378−unsafeWindowFunction2
2380− -> (Expression outer grp commons schemas params from :*: Expression outer grp commons schemas params from) ty0
2381− -> WindowFunction outer grp commons schemas params from ty1
2382−unsafeWindowFunction2 fun (x :*: x')
2383− = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x <> ", " <> renderSQL x')
2385−{- | rank of the current row with gaps; same as `rowNumber` of its first peer
2389−rank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2390−rank = UnsafeWindowFunction "rank()"
2392−{- | number of the current row within its partition, counting from 1
2393−>>> printSQL rowNumber
2396−rowNumber :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2397−rowNumber = UnsafeWindowFunction "row_number()"
2399−{- | rank of the current row without gaps; this function counts peer groups
2400−>>> printSQL denseRank
2403−denseRank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2404−denseRank = UnsafeWindowFunction "dense_rank()"
2406−{- | relative rank of the current row: (rank - 1) / (total partition rows - 1)
2407−>>> printSQL percentRank
2410−percentRank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGfloat8)
2411−percentRank = UnsafeWindowFunction "percent_rank()"
2413−{- | cumulative distribution: (number of partition rows
2414−preceding or peer with current row) / total partition rows
2415−>>> printSQL cumeDist
2418−cumeDist :: WindowFunction outer grp commons schemas params from ('NotNull 'PGfloat8)
2419−cumeDist = UnsafeWindowFunction "cume_dist()"
2421−{- | integer ranging from 1 to the argument value,
2422−dividing the partition as equally as possible
2425− :: Expression outer grp commons schemas params from ('NotNull 'PGint4)
2427− -> WindowFunction outer grp commons schemas params from ('NotNull 'PGint4)
2428−ntile = unsafeWindowFunction1 "ntile"
2430−{- | returns value evaluated at the row that is offset rows before the current
2431−row within the partition; if there is no such row, instead return default
2432−(which must be of the same type as value). Both offset and default are
2433−evaluated with respect to the current row.
2436− :: Expression outer grp commons schemas params from ty
2438− -> Expression outer grp commons schemas params from ('NotNull 'PGint4)
2440− -> Expression outer grp commons schemas params from ty
2442− -> WindowFunction outer grp commons schemas params from ty
2443−lag value offset def = UnsafeWindowFunction $ "lag"
2445− (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
2447−{- | returns value evaluated at the row that is offset rows after the current
2448−row within the partition; if there is no such row, instead return default
2449−(which must be of the same type as value). Both offset and default are
2450−evaluated with respect to the current row.
2453− :: Expression outer grp commons schemas params from ty
2455− -> Expression outer grp commons schemas params from ('NotNull 'PGint4)
2457− -> Expression outer grp commons schemas params from ty
2459− -> WindowFunction outer grp commons schemas params from ty
2460−lead value offset def = UnsafeWindowFunction $ "lag"
2462− (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
2464−{- | returns value evaluated at the row that is the
2465−first row of the window frame
2468− :: Expression outer grp commons schemas params from ty
2470− -> WindowFunction outer grp commons schemas params from ty
2471−firstValue = unsafeWindowFunction1 "first_value"
2473−{- | returns value evaluated at the row that is the
2474−last row of the window frame
2477− :: Expression outer grp commons schemas params from ty
2479− -> WindowFunction outer grp commons schemas params from ty
2480−lastValue = unsafeWindowFunction1 "last_value"
2482−{- | returns value evaluated at the row that is the nth
2483−row of the window frame (counting from 1); null if no such row
2486− :: Expression outer grp commons schemas params from (nullity ty)
2488− -> Expression outer grp commons schemas params from ('NotNull 'PGint4)
2490− -> WindowFunction outer grp commons schemas params from ('Null ty)
2491−nthValue value nth = UnsafeWindowFunction $ "nth_value"
2492− <> parenthesized (commaSeparated [renderSQL value, renderSQL nth])
2495−Create date from year, month and day fields
2497−>>> printSQL (makeDate 1984 7 3)
2498−make_date(1984, 7, 3)
2501− :: Expression outer grp commons schemas params from (nullity 'PGint4)
2503− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2505− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2507− -> Expression outer grp commons schemas params from (nullity 'PGdate)
2508−makeDate y m d = UnsafeExpression $ "make_date" <>
2509− parenthesized (renderCommaSeparated renderSQL (y :* m :* d :* Nil))
2512−Create time from hour, minute and seconds fields
2514−>>> printSQL (makeTime 8 15 23.5)
2515−make_time(8, 15, 23.5)
2518− :: Expression outer grp commons schemas params from (nullity 'PGint4)
2520− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2522− -> Expression outer grp commons schemas params from (nullity 'PGfloat8)
2524− -> Expression outer grp commons schemas params from (nullity 'PGtime)
2525−makeTime h m s = UnsafeExpression $ "make_time" <>
2526− parenthesized (renderCommaSeparated renderSQL (h :* m :* s :* Nil))
2529−Create timestamp from year, month, day, hour, minute and seconds fields
2531−>>> printSQL (makeTimestamp 2013 7 15 8 15 23.5)
2532−make_timestamp(2013, 7, 15, 8, 15, 23.5)
2535− :: Expression outer grp commons schemas params from (nullity 'PGint4)
2537− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2539− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2541− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2543− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2545− -> Expression outer grp commons schemas params from (nullity 'PGfloat8)
2547− -> Expression outer grp commons schemas params from (nullity 'PGtimestamp)
2548−makeTimestamp y mon d h m s =
2549− UnsafeExpression $ "make_timestamp" <> parenthesized
2550− (renderCommaSeparated renderSQL (y :* mon :* d :* h :* m :* s :* Nil))
2553−Create timestamp with time zone from
2554−year, month, day, hour, minute and seconds fields;
2555−the current time zone is used
2557−>>> printSQL (makeTimestamptz 2013 7 15 8 15 23.5)
2558−make_timestamptz(2013, 7, 15, 8, 15, 23.5)
2561− :: Expression outer grp commons schemas params from (nullity 'PGint4)
2563− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2565− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2567− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2569− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2571− -> Expression outer grp commons schemas params from (nullity 'PGfloat8)
2573− -> Expression outer grp commons schemas params from (nullity 'PGtimestamptz)
2574−makeTimestamptz y mon d h m s =
2575− UnsafeExpression $ "make_timestamptz" <> parenthesized
2576− (renderCommaSeparated renderSQL (y :* mon :* d :* h :* m :* s :* Nil))
2579−Affine space operations on time types.
2581−class TimeOp time diff | time -> diff where
2583− >>> printSQL (makeDate 1984 7 3 !+ 365)
2584− (make_date(1984, 7, 3) + 365)
2587− :: Expression outer grp commons schemas params from (nullity time)
2588− -> Expression outer grp commons schemas params from (nullity diff)
2589− -> Expression outer grp commons schemas params from (nullity time)
2590− (!+) = unsafeBinaryOp "+"
2592− >>> printSQL (365 +! makeDate 1984 7 3)
2593− (365 + make_date(1984, 7, 3))
2596− :: Expression outer grp commons schemas params from (nullity diff)
2597− -> Expression outer grp commons schemas params from (nullity time)
2598− -> Expression outer grp commons schemas params from (nullity time)
2599− (+!) = unsafeBinaryOp "+"
2601− >>> printSQL (makeDate 1984 7 3 !- 365)
2602− (make_date(1984, 7, 3) - 365)
2605− :: Expression outer grp commons schemas params from (nullity time)
2606− -> Expression outer grp commons schemas params from (nullity diff)
2607− -> Expression outer grp commons schemas params from (nullity time)
2608− (!-) = unsafeBinaryOp "-"
2610− >>> printSQL (makeDate 1984 7 3 !-! currentDate)
2611− (make_date(1984, 7, 3) - CURRENT_DATE)
2614− :: Expression outer grp commons schemas params from (nullity time)
2615− -> Expression outer grp commons schemas params from (nullity time)
2616− -> Expression outer grp commons schemas params from (nullity diff)
2617− (!-!) = unsafeBinaryOp "-"
2618−instance TimeOp 'PGtimestamp 'PGinterval
2619−instance TimeOp 'PGtimestamptz 'PGinterval
2620−instance TimeOp 'PGtime 'PGinterval
2621−instance TimeOp 'PGtimetz 'PGinterval
2622−instance TimeOp 'PGinterval 'PGinterval
2623−instance TimeOp 'PGdate 'PGint4