@@ -1,14 +1,13 @@
2−Module: Squeal.PostgreSQL.Query
2+Module: 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,565 +33,350 @@ 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− , HasParameter (parameter)
36+ , Expr
38− , param
37+ , type (-->)
39− -- ** Null
38+ , FunctionDB
56 , unsafeFunction
39 , unsafeFunction
57− , unsafeVariadicFunction
40+ , function
58− , atan2_
41+ , unsafeLeftOp
60− , quot_
43+ , unsafeRightOp
62− , trunc
45+ , Operator
63− , round_
46+ , OperatorDB
64− , ceiling_
47+ , unsafeBinaryOp
65− , greatest
48+ , binaryOp
66− , least
49+ , FunctionVar
67− -- ** Conditions
50+ , unsafeFunctionVar
68− , true
51+ , type (--->)
69− , false
52+ , FunctionNDB
70− , not_
53+ , unsafeFunctionN
71− , (.&&)
54+ , functionN
72− , (.||)
55+ , PGSubset (..)
73− , Condition
56+ , PGIntersect (..)
95− -- *** Json and Jsonb operators
100− -- *** Jsonb operators
127− , jsonExtractPathAsText
128− , jsonbExtractPathAsText
137− , unsafeAggregate, unsafeAggregateDistinct
139− , PGAvg (avg, avgDistinct)
140− , bitAnd, bitOr, boolAnd, boolOr
141− , bitAndDistinct, bitOrDistinct, boolAndDistinct, boolOrDistinct
143− , count, countDistinct
144− , every, everyDistinct
145− , max_, maxDistinct, min_, minDistinct
146− -- * Window Functions
147− , WindowDefinition (..)
149− , WindowFunction (..)
162− , SortExpression (..)
165− , TypeExpression (..)
190− , timestampWithTimeZone
201 -- * Re-export
57 -- * Re-export
203− , NP ((:*), Nil)
59+ , K (..)
206import Control.Category
63import Control.Category
207import Control.DeepSeq
64import Control.DeepSeq
208import Data.ByteString (ByteString)
65import Data.ByteString (ByteString)
209−import Data.ByteString.Lazy (toStrict)
210import Data.Function ((&))
66import Data.Function ((&))
211import Data.Semigroup hiding (All)
67import Data.Semigroup hiding (All)
212−import qualified Data.Aeson as JSON
214import Data.String
68import Data.String
216−import Generics.SOP hiding (from)
69+import Generics.SOP hiding (All, from)
217import GHC.OverloadedLabels
70import GHC.OverloadedLabels
218import GHC.TypeLits
71import GHC.TypeLits
219import Prelude hiding (id, (.))
73import Prelude hiding (id, (.))
221−import qualified Data.ByteString as ByteString
222import qualified GHC.Generics as GHC
75import qualified GHC.Generics as GHC
77+import Squeal.PostgreSQL.Alias
78+import Squeal.PostgreSQL.List
224import Squeal.PostgreSQL.Render
79import Squeal.PostgreSQL.Render
225import Squeal.PostgreSQL.Schema
80import Squeal.PostgreSQL.Schema
83+-- >>> import Squeal.PostgreSQL
227{-----------------------------------------
85{-----------------------------------------
228column expressions
86column expressions
229-----------------------------------------}
87-----------------------------------------}
231{- | `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
232−such as in the target list of the `Squeal.PostgreSQL.Query.select` command,
91+`Squeal.PostgreSQL.Query.select` command,
233as new column values in `Squeal.PostgreSQL.Manipulation.insertRow` or
92as new column values in `Squeal.PostgreSQL.Manipulation.insertRow` or
234`Squeal.PostgreSQL.Manipulation.update`,
93`Squeal.PostgreSQL.Manipulation.update`,
235−or in search `Condition`s in a number of commands.
94+or in search `Squeal.PostgreSQL.Logic.Condition`s in a number of commands.
237The expression syntax allows the calculation of
96The expression syntax allows the calculation of
238values from primitive expression using arithmetic, logical,
97values from primitive expression using arithmetic, logical,
239and 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`.
241newtype Expression
114newtype Expression
242− (grp :: Grouping)
115+ (outer :: FromType)
243 (commons :: FromType)
116 (commons :: FromType)
244 (schemas :: SchemasType)
118 (schemas :: SchemasType)
245 (params :: [NullityType])
119 (params :: [NullityType])
246 (from :: FromType)
120 (from :: FromType)
247 (ty :: NullityType)
121 (ty :: NullityType)
248 = UnsafeExpression { renderExpression :: ByteString }
122 = UnsafeExpression { renderExpression :: ByteString }
249 deriving (GHC.Generic,Show,Eq,Ord,NFData)
123 deriving (GHC.Generic,Show,Eq,Ord,NFData)
251−instance RenderSQL (Expression grp commons schemas params from ty) where
124+instance RenderSQL (Expression outer commons grp schemas params from ty) where
252 renderSQL = renderExpression
125 renderSQL = renderExpression
254−{- | A `HasParameter` constraint is used to indicate a value that is
127+-- | An `Expr` is a closed `Expression`.
255−supplied externally to a SQL statement.
128+-- It is a F@RankNType@ but don't be scared.
256−`Squeal.PostgreSQL.PQ.manipulateParams`,
129+-- Think of it as an expression which sees no
257−`Squeal.PostgreSQL.PQ.queryParams` and
130+-- namespaces, so you can't use parameters
258−`Squeal.PostgreSQL.PQ.traversePrepared` support specifying data values
131+-- or alias references. It can be used as
259−separately from the SQL command string, in which case `param`s are used to
132+-- a simple piece of more complex `Expression`s.
260−refer to the out-of-line data values.
133+type Expr x
134+ = forall outer commons grp schemas params from
135+ . Expression outer commons grp schemas params from x
136+ -- ^ cannot reference aliases
138+-- | A @RankNType@ for binary operators.
139+type Operator x1 x2 y
140+ = forall outer commons grp schemas params from
141+ . Expression outer commons grp schemas params from x1
143+ -> Expression outer commons grp schemas params from x2
145+ -> Expression outer commons grp schemas params from y
148+type OperatorDB schemas x1 x2 y
149+ = forall outer commons grp params from
150+ . Expression outer commons grp schemas params from x1
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)
262−class KnownNat n => HasParameter
182+type (--->) xs y
263− (n :: Nat)
183+ = forall outer commons grp schemas params from
264− (params :: [NullityType])
184+ . NP (Expression outer commons grp schemas params from) xs
265− (ty :: NullityType)
185+ -- ^ inputs
266− | n params -> ty where
186+ -> Expression outer commons grp schemas params from y
267− -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
187+ -- ^ output
269− -- >>> let expr = parameter @1 int4 :: Expression '[] schemas '[ 'Null 'PGint4] grp from ('Null 'PGint4)
189+type FunctionNDB schemas xs y
270− -- >>> printSQL expr
190+ = forall outer commons grp params from
271− -- ($1 :: int4)
191+ . NP (Expression outer commons grp schemas params from) xs
272− parameter
192+ -- ^ inputs
273− :: TypeExpression schemas ty
193+ -> Expression outer commons grp schemas params from y
274− -> Expression grp commons schemas params from ty
194+ -- ^ output
196+{- | A @RankNType@ for functions with a variable-length list of
197+homogeneous arguments and at least 1 more argument.
199+type FunctionVar x0 x1 y
200+ = forall outer commons grp schemas params from
201+ . [Expression outer commons grp schemas params from x0]
203+ -> Expression outer commons grp schemas params from x1
204+ -- ^ must have at least 1 input
205+ -> Expression outer commons grp schemas params from y
208+{- | >>> printSQL (unsafeFunctionVar "greatest" [true, null_] false)
209+greatest(TRUE, NULL, FALSE)
211+unsafeFunctionVar :: ByteString -> FunctionVar x0 x1 y
275− parameter ty = UnsafeExpression $ parenthesized $
212+unsafeFunctionVar fun xs x = UnsafeExpression $ fun <> parenthesized
276− "$" <> renderNat @n <+> "::"
213+ (commaSeparated (renderSQL <$> xs) <> ", " <> renderSQL x)
278−instance {-# OVERLAPPING #-} HasParameter 1 (ty1:tys) ty1
279−instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
280− => HasParameter n (ty' : params) ty
282−-- | `param` takes a `Nat` using type application and for basic types,
283−-- infers a `TypeExpression`.
285−-- >>> let expr = param @1 :: Expression commons schemas '[ 'Null 'PGint4] grp from ('Null 'PGint4)
289− :: forall n commons schemas params from grp ty
290− . (PGTyped schemas ty, HasParameter n params ty)
291− => Expression grp commons schemas params from ty -- ^ param
292−param = parameter @n (pgtype @schemas)
294−instance (HasUnique tab from row, Has col row ty)
215+instance (HasUnique tab (Join outer from) row, Has col row ty)
295− => IsLabel col (Expression 'Ungrouped commons schemas params from ty) where
216+ => IsLabel col (Expression outer commons 'Ungrouped schemas params from ty) where
296 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
217 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
297−instance (HasUnique tab from row, Has col row ty, column ~ (col ::: ty))
218+instance (HasUnique tab (Join outer from) row, Has col row ty, tys ~ '[ty])
219+ => IsLabel col (NP (Expression outer commons 'Ungrouped schemas params from) tys) where
220+ fromLabel = fromLabel @col :* Nil
221+instance (HasUnique tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
298 => IsLabel col
222 => IsLabel col
299− (Aliased (Expression 'Ungrouped commons schemas params from) column) where
223+ (Aliased (Expression outer commons 'Ungrouped schemas params from) column) where
300 fromLabel = fromLabel @col `As` Alias
224 fromLabel = fromLabel @col `As` Alias
301−instance (HasUnique tab from row, Has col row ty, columns ~ '[col ::: ty])
225+instance (HasUnique tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
302 => IsLabel col
226 => IsLabel col
303− (NP (Aliased (Expression 'Ungrouped commons schemas params from)) columns) where
227+ (NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns) where
304 fromLabel = fromLabel @col :* Nil
228 fromLabel = fromLabel @col :* Nil
306−instance (Has tab from row, Has col row ty)
230+instance (Has tab (Join outer from) row, Has col row ty)
307− => IsQualified tab col (Expression 'Ungrouped commons schemas params from ty) where
231+ => IsQualified tab col (Expression outer commons 'Ungrouped schemas params from ty) where
308 tab ! col = UnsafeExpression $
232 tab ! col = UnsafeExpression $
309 renderSQL tab <> "." <> renderSQL col
233 renderSQL tab <> "." <> renderSQL col
234+instance (Has tab (Join outer from) row, Has col row ty, tys ~ '[ty])
235+ => IsQualified tab col (NP (Expression outer commons 'Ungrouped schemas params from) tys) where
236+ tab ! col = tab ! col :* Nil
310−instance (Has tab from row, Has col row ty, column ~ (col ::: ty))
237+instance (Has tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
311 => IsQualified tab col
238 => IsQualified tab col
312− (Aliased (Expression 'Ungrouped commons schemas params from) column) where
239+ (Aliased (Expression outer commons 'Ungrouped schemas params from) column) where
313 tab ! col = tab ! col `As` col
240 tab ! col = tab ! col `As` col
314−instance (Has tab from row, Has col row ty, columns ~ '[col ::: ty])
241+instance (Has tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
315 => IsQualified tab col
242 => IsQualified tab col
316− (NP (Aliased (Expression 'Ungrouped commons schemas params from)) columns) where
243+ (NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns) where
317 tab ! col = tab ! col :* Nil
244 tab ! col = tab ! col :* Nil
320− ( HasUnique tab from row
247+ ( HasUnique tab (Join outer from) row
321 , Has col row ty
248 , Has col row ty
322 , GroupedBy tab col bys
249 , GroupedBy tab col bys
323 ) => IsLabel col
250 ) => IsLabel col
324− (Expression ('Grouped bys) commons schemas params from ty) where
251+ (Expression outer commons ('Grouped bys) schemas params from ty) where
325 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
252 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
327− ( HasUnique tab from row
254+ ( HasUnique tab (Join outer from) row
256+ , GroupedBy tab col bys
259+ (NP (Expression outer commons ('Grouped bys) schemas params from) tys) where
260+ fromLabel = fromLabel @col :* Nil
262+ ( HasUnique tab (Join outer from) row
328 , Has col row ty
263 , Has col row ty
329 , GroupedBy tab col bys
264 , GroupedBy tab col bys
330 , column ~ (col ::: ty)
265 , column ~ (col ::: ty)
331 ) => IsLabel col
266 ) => IsLabel col
332− (Aliased (Expression ('Grouped bys) commons schemas params from) column) where
267+ (Aliased (Expression outer commons ('Grouped bys) schemas params from) column) where
333 fromLabel = fromLabel @col `As` Alias
268 fromLabel = fromLabel @col `As` Alias
335− ( HasUnique tab from row
270+ ( HasUnique tab (Join outer from) row
336 , Has col row ty
271 , Has col row ty
337 , GroupedBy tab col bys
272 , GroupedBy tab col bys
338 , columns ~ '[col ::: ty]
273 , columns ~ '[col ::: ty]
339 ) => IsLabel col
274 ) => IsLabel col
340− (NP (Aliased (Expression ('Grouped bys) commons schemas params from)) columns) where
275+ (NP (Aliased (Expression outer commons ('Grouped bys) schemas params from)) columns) where
341 fromLabel = fromLabel @col :* Nil
276 fromLabel = fromLabel @col :* Nil
344− ( Has tab from row
279+ ( Has tab (Join outer from) row
345 , Has col row ty
280 , Has col row ty
346 , GroupedBy tab col bys
281 , GroupedBy tab col bys
347 ) => IsQualified tab col
282 ) => IsQualified tab col
348− (Expression ('Grouped bys) commons schemas params from ty) where
283+ (Expression outer commons ('Grouped bys) schemas params from ty) where
349 tab ! col = UnsafeExpression $
284 tab ! col = UnsafeExpression $
350 renderSQL tab <> "." <> renderSQL col
285 renderSQL tab <> "." <> renderSQL col
352− ( Has tab from row
287+ ( Has tab (Join outer from) row
289+ , GroupedBy tab col bys
291+ ) => IsQualified tab col
292+ (NP (Expression outer commons ('Grouped bys) schemas params from) tys) where
293+ tab ! col = tab ! col :* Nil
295+ ( Has tab (Join outer from) row
353 , Has col row ty
296 , Has col row ty
354 , GroupedBy tab col bys
297 , GroupedBy tab col bys
355 , column ~ (col ::: ty)
298 , column ~ (col ::: ty)
356 ) => IsQualified tab col
299 ) => IsQualified tab col
357− (Aliased (Expression ('Grouped bys) commons schemas params from) column) where
300+ (Aliased (Expression outer commons ('Grouped bys) schemas params from) column) where
358 tab ! col = tab ! col `As` col
301 tab ! col = tab ! col `As` col
360− ( Has tab from row
303+ ( Has tab (Join outer from) row
361 , Has col row ty
304 , Has col row ty
362 , GroupedBy tab col bys
305 , GroupedBy tab col bys
363 , columns ~ '[col ::: ty]
306 , columns ~ '[col ::: ty]
364 ) => IsQualified tab col
307 ) => IsQualified tab col
365− (NP (Aliased (Expression ('Grouped bys) commons schemas params from)) columns) where
308+ (NP (Aliased (Expression outer commons ('Grouped bys) schemas params from)) columns) where
366 tab ! col = tab ! col :* Nil
309 tab ! col = tab ! col :* Nil
368−-- | analagous to `Nothing`
370−-- >>> printSQL null_
372−null_ :: Expression commons schemas rels grouping params ('Null ty)
373−null_ = UnsafeExpression "NULL"
375−-- | analagous to `Just`
377−-- >>> printSQL $ notNull true
380− :: Expression commons schemas rels grouping params ('NotNull ty)
381− -> Expression commons schemas rels grouping params ('Null ty)
382−notNull = UnsafeExpression . renderSQL
384−-- | return the leftmost value which is not NULL
386−-- >>> printSQL $ coalesce [null_, true] false
387−-- COALESCE(NULL, TRUE, FALSE)
389− :: [Expression grp commons schemas params from ('Null ty)]
390− -- ^ @NULL@s may be present
391− -> Expression grp commons schemas params from ('NotNull ty)
392− -- ^ @NULL@ is absent
393− -> Expression grp commons schemas params from ('NotNull ty)
394−coalesce nullxs notNullx = UnsafeExpression $
395− "COALESCE" <> parenthesized (commaSeparated
396− ((renderSQL <$> nullxs) <> [renderSQL notNullx]))
398−-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
400−-- >>> printSQL $ fromNull true null_
401−-- COALESCE(NULL, TRUE)
403− :: Expression grp commons schemas params from ('NotNull ty)
404− -- ^ what to convert @NULL@ to
405− -> Expression grp commons schemas params from ('Null ty)
406− -> Expression grp commons schemas params from ('NotNull ty)
407−fromNull notNullx nullx = coalesce [nullx] notNullx
409−-- | >>> printSQL $ null_ & isNull
412− :: Expression grp commons schemas params from ('Null ty)
413− -- ^ possibly @NULL@
414− -> Condition grp commons schemas params from
415−isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
417−-- | >>> printSQL $ null_ & isNotNull
420− :: Expression grp commons schemas params from ('Null ty)
421− -- ^ possibly @NULL@
422− -> Condition grp commons schemas params from
423−isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
425−-- | analagous to `maybe` using @IS NULL@
427−-- >>> printSQL $ matchNull true not_ null_
428−-- CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END
430− :: Expression grp commons schemas params from (nullty)
431− -- ^ what to convert @NULL@ to
432− -> ( Expression grp commons schemas params from ('NotNull ty)
433− -> Expression grp commons schemas params from (nullty) )
434− -- ^ function to perform when @NULL@ is absent
435− -> Expression grp commons schemas params from ('Null ty)
436− -> Expression grp commons schemas params from (nullty)
437−matchNull y f x = ifThenElse (isNull x) y
438− (f (UnsafeExpression (renderSQL x)))
440−{-| right inverse to `fromNull`, if its arguments are equal then
441−`nullIf` gives @NULL@.
443−>>> :set -XTypeApplications -XDataKinds
444−>>> let expr = nullIf false (param @1) :: Expression commons schemas '[ 'NotNull 'PGbool] grp from ('Null 'PGbool)
446−NULL IF (FALSE, ($1 :: bool))
449− :: Expression grp commons schemas params from ('NotNull ty)
450− -- ^ @NULL@ is absent
451− -> Expression grp commons schemas params from ('NotNull ty)
452− -- ^ @NULL@ is absent
453− -> Expression grp commons schemas params from ('Null ty)
454−nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
455− (renderSQL x <> ", " <> renderSQL y)
457−-- | >>> printSQL $ array [null_, false, true]
458−-- ARRAY[NULL, FALSE, TRUE]
460− :: [Expression grp commons schemas params from ty]
462− -> Expression grp commons schemas params from (nullity ('PGvararray ty))
463−array xs = UnsafeExpression $
464− "ARRAY[" <> commaSeparated (renderSQL <$> xs) <> "]"
466−-- | >>> printSQL $ array [null_, false, true] & index 2
467−-- (ARRAY[NULL, FALSE, TRUE])[2]
469− :: Word64 -- ^ index
470− -> Expression grp commons schemas params from (nullity ('PGvararray ty)) -- ^ array
471− -> Expression grp commons schemas params from (NullifyType ty)
472−index n expr = UnsafeExpression $
473− parenthesized (renderSQL expr) <> "[" <> fromString (show n) <> "]"
475instance (KnownSymbol label, label `In` labels) => IsPGlabel label
311instance (KnownSymbol label, label `In` labels) => IsPGlabel label
476− (Expression grp commons schemas params from (nullity ('PGenum labels))) where
312+ (Expression outer commons grp schemas params from (null ('PGenum labels))) where
477 label = UnsafeExpression $ renderSQL (PGlabel @label)
313 label = UnsafeExpression $ renderSQL (PGlabel @label)
479−-- | A row constructor is an expression that builds a row value
480−-- (also called a composite value) using values for its member fields.
483−-- type Complex = 'PGcomposite
484−-- '[ "real" ::: 'NotNull 'PGfloat8
485−-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
488−-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression grp commons schemas params from ('NotNull Complex)
493− => NP (Aliased (Expression grp commons schemas params from)) row
494− -- ^ zero or more expressions for the row field values
495− -> Expression grp commons schemas params from (nullity ('PGcomposite row))
496−row exprs = UnsafeExpression $ "ROW" <> parenthesized
497− (renderCommaSeparated (\ (expr `As` _) -> renderSQL expr) exprs)
500−-- type Complex = 'PGcomposite
501−-- '[ "real" ::: 'NotNull 'PGfloat8
502−-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
503−-- type Schema = '["complex" ::: 'Typedef Complex]
506−-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression '[] (Public Schema) from grp params ('NotNull Complex)
507−-- >>> printSQL $ i & field #complex #imaginary
508−-- (ROW(0, 1)::"complex")."imaginary"
510− :: ( Has sch schemas schema
511− , Has tydef schema ('Typedef ('PGcomposite row))
513− => QualifiedAlias sch tydef -- ^ row type
514− -> Alias field -- ^ field name
515− -> Expression grp commons schemas params from ('NotNull ('PGcomposite row))
516− -> Expression grp commons schemas params from ty
517−field td fld expr = UnsafeExpression $
518− parenthesized (renderSQL expr <> "::" <> renderSQL td)
519− <> "." <> renderSQL fld
522− (Expression grp commons schemas params from (nullity ('PGvararray ty))) where
523− (<>) = unsafeBinaryOp "||"
526− (Expression grp commons schemas params from (nullity ('PGvararray ty))) where
530−-- | >>> let expr = greatest currentTimestamp [param @1] :: Expression commons schemas '[ 'NotNull 'PGtimestamptz] grp from ('NotNull 'PGtimestamptz)
532−-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
534− :: Expression grp commons schemas params from (nullty)
535− -- ^ needs at least 1 argument
536− -> [Expression grp commons schemas params from (nullty)]
538− -> Expression grp commons schemas params from (nullty)
539−greatest x xs = UnsafeExpression $ "GREATEST("
540− <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
542−-- | >>> printSQL $ least currentTimestamp [null_]
543−-- LEAST(CURRENT_TIMESTAMP, NULL)
545− :: Expression grp commons schemas params from (nullty)
546− -- ^ needs at least 1 argument
547− -> [Expression grp commons schemas params from (nullty)]
549− -> Expression grp commons schemas params from (nullty)
550−least x xs = UnsafeExpression $ "LEAST("
551− <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
553-- | >>> printSQL $ unsafeBinaryOp "OR" true false
315-- | >>> printSQL $ unsafeBinaryOp "OR" true false
554-- (TRUE OR FALSE)
316-- (TRUE OR FALSE)
555−unsafeBinaryOp
317+unsafeBinaryOp :: ByteString -> Operator ty0 ty1 ty2
558− -> Expression grp commons schemas params from (ty0)
559− -> Expression grp commons schemas params from (ty1)
560− -> Expression grp commons schemas params from (ty2)
561unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
318unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
562 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
564−-- | >>> printSQL $ unsafeUnaryOp "NOT" true
328+-- | >>> printSQL $ unsafeLeftOp "NOT" true
565-- (NOT TRUE)
329-- (NOT TRUE)
567− :: ByteString
330+unsafeLeftOp :: ByteString -> x --> y
568− -- ^ operator
331+unsafeLeftOp op x = UnsafeExpression $ parenthesized $ op <+> renderSQL x
569− -> Expression grp commons schemas params from (ty0)
332+
570− -> Expression 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
571−unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
343+unsafeRightOp op x = UnsafeExpression $ parenthesized $ renderSQL x <+> op
572− 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
574-- | >>> printSQL $ unsafeFunction "f" true
352-- | >>> printSQL $ unsafeFunction "f" true
575-- f(TRUE)
353-- f(TRUE)
576−unsafeFunction
354+unsafeFunction :: ByteString -> x --> y
579− -> Expression grp commons schemas params from (xty)
580− -> Expression grp commons schemas params from (yty)
581unsafeFunction fun x = UnsafeExpression $
355unsafeFunction fun x = UnsafeExpression $
582 fun <> parenthesized (renderSQL x)
356 fun <> parenthesized (renderSQL x)
584−-- | Helper for defining variadic functions.
358+function
585−unsafeVariadicFunction
359+ :: (Has sch schemas schema, Has fun schema ('Function ('[x] :=> 'Returns y)))
586− :: 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')
587− => ByteString
366+unsafeFunctionN :: SListI xs => ByteString -> xs ---> y
589− -> NP (Expression grp commons schemas params from) elems
590− -> Expression grp commons schemas params from ret
591−unsafeVariadicFunction fun x = UnsafeExpression $
367+unsafeFunctionN fun xs = UnsafeExpression $
592− 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
594instance ty `In` PGNum
378instance ty `In` PGNum
595− => Num (Expression grp commons schemas params from (nullity ty)) where
379+ => Num (Expression outer commons grp schemas params from (null ty)) where
596 (+) = unsafeBinaryOp "+"
380 (+) = unsafeBinaryOp "+"
597 (-) = unsafeBinaryOp "-"
381 (-) = unsafeBinaryOp "-"
598 (*) = unsafeBinaryOp "*"
382 (*) = unsafeBinaryOp "*"
@@ -604,12 +388,17 @@ instance ty `In` PGNum
606instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
390instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
607− (Expression grp commons schemas params from (nullity ty)) where
391+ (Expression outer commons grp schemas params from (null ty)) where
608 (/) = unsafeBinaryOp "/"
392 (/) = unsafeBinaryOp "/"
609− fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
393+ fromRational
397+ . showFFloat Nothing
611instance (ty `In` PGNum, ty `In` PGFloating) => Floating
400instance (ty `In` PGNum, ty `In` PGFloating) => Floating
612− (Expression grp commons schemas params from (nullity ty)) where
401+ (Expression outer commons grp schemas params from (null ty)) where
613 pi = UnsafeExpression "pi()"
402 pi = UnsafeExpression "pi()"
614 exp = unsafeFunction "exp"
403 exp = unsafeFunction "exp"
615 log = unsafeFunction "ln"
404 log = unsafeFunction "ln"
@@ -630,1387 +419,54 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Floating
630 acosh x = log (x + sqrt (x*x - 1))
419 acosh x = log (x + sqrt (x*x - 1))
631 atanh x = log ((1 + x) / (1 - x)) / 2
420 atanh x = log ((1 + x) / (1 - x)) / 2
633−-- | >>> :{
422+-- | Contained by operators
634−-- let
423+class PGSubset ty where
635−-- expression :: Expression grp commons schemas params from (nullity 'PGfloat4)
424+ (@>) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
636−-- expression = atan2_ pi 2
425+ (@>) = unsafeBinaryOp "@>"
637−-- in printSQL expression
426+ (<@) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
638−-- :}
427+ (<@) = unsafeBinaryOp "<@"
639−-- atan2(pi(), 2)
428+instance PGSubset 'PGjsonb
640−atan2_
429+instance PGSubset 'PGtsquery
641− :: float `In` PGFloating
430+instance PGSubset ('PGvararray ty)
642− => Expression grp commons schemas params from (nullity float)
431+instance PGSubset ('PGrange ty)
644− -> Expression grp commons schemas params from (nullity float)
433+class PGIntersect ty where
645− -- ^ denominator
434+ (@&&) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
646− -> Expression grp commons schemas params from (nullity float)
435+ (@&&) = unsafeBinaryOp "&&"
647−atan2_ y x = UnsafeExpression $
436+instance PGIntersect ('PGvararray ty)
648− "atan2(" <> renderSQL y <> ", " <> renderSQL x <> ")"
437+instance PGIntersect ('PGrange ty)
650−-- When a `cast` is applied to an `Expression` of a known type, it
651−-- represents a run-time type conversion. The cast will succeed only if a
652−-- suitable type conversion operation has been defined.
654−-- | >>> printSQL $ true & cast int4
657− :: TypeExpression schemas ty1
658− -- ^ type to cast as
659− -> Expression grp commons schemas params from ty0
660− -- ^ value to convert
661− -> Expression grp commons schemas params from ty1
662−cast ty x = UnsafeExpression $ parenthesized $
663− renderSQL x <+> "::" <+> renderSQL ty
665−-- | integer division, truncates the result
669−-- expression :: Expression grp commons schemas params from (nullity 'PGint2)
670−-- expression = 5 `quot_` 2
671−-- in printSQL expression
675− :: int `In` PGIntegral
676− => Expression grp commons schemas params from (nullity int)
678− -> Expression grp commons schemas params from (nullity int)
680− -> Expression grp commons schemas params from (nullity int)
681−quot_ = unsafeBinaryOp "/"
683−-- | remainder upon integer division
687−-- expression :: Expression grp commons schemas params from (nullity 'PGint2)
688−-- expression = 5 `rem_` 2
689−-- in printSQL expression
693− :: int `In` PGIntegral
694− => Expression grp commons schemas params from (nullity int)
696− -> Expression grp commons schemas params from (nullity int)
698− -> Expression grp commons schemas params from (nullity int)
699−rem_ = unsafeBinaryOp "%"
703−-- expression :: Expression grp commons schemas params from (nullity 'PGfloat4)
704−-- expression = trunc pi
705−-- in printSQL expression
709− :: frac `In` PGFloating
710− => Expression grp commons schemas params from (nullity frac)
711− -- ^ fractional number
712− -> Expression grp commons schemas params from (nullity frac)
713−trunc = unsafeFunction "trunc"
717−-- expression :: Expression grp commons schemas params from (nullity 'PGfloat4)
718−-- expression = round_ pi
719−-- in printSQL expression
723− :: frac `In` PGFloating
724− => Expression grp commons schemas params from (nullity frac)
725− -- ^ fractional number
726− -> Expression grp commons schemas params from (nullity frac)
727−round_ = unsafeFunction "round"
731−-- expression :: Expression grp commons schemas params from (nullity 'PGfloat4)
732−-- expression = ceiling_ pi
733−-- in printSQL expression
737− :: frac `In` PGFloating
738− => Expression grp commons schemas params from (nullity frac)
739− -- ^ fractional number
740− -> Expression grp commons schemas params from (nullity frac)
741−ceiling_ = unsafeFunction "ceiling"
743−-- | A `Condition` is an `Expression`, which can evaluate
744−-- to `true`, `false` or `null_`. This is because SQL uses
745−-- a three valued logic.
746−type Condition grp commons schemas params from =
747− Expression grp commons schemas params from ('Null 'PGbool)
749−-- | >>> printSQL true
751−true :: Expression grp commons schemas params from (nullity 'PGbool)
752−true = UnsafeExpression "TRUE"
754−-- | >>> printSQL false
756−false :: Expression grp commons schemas params from (nullity 'PGbool)
757−false = UnsafeExpression "FALSE"
759−-- | >>> printSQL $ not_ true
762− :: Expression grp commons schemas params from (nullity 'PGbool)
763− -> Expression grp commons schemas params from (nullity 'PGbool)
764−not_ = unsafeUnaryOp "NOT"
766−-- | >>> printSQL $ true .&& false
769− :: Expression grp commons schemas params from (nullity 'PGbool)
770− -> Expression grp commons schemas params from (nullity 'PGbool)
771− -> Expression grp commons schemas params from (nullity 'PGbool)
773−(.&&) = unsafeBinaryOp "AND"
775−-- | >>> printSQL $ true .|| false
778− :: Expression grp commons schemas params from (nullity 'PGbool)
779− -> Expression grp commons schemas params from (nullity 'PGbool)
780− -> Expression grp commons schemas params from (nullity 'PGbool)
782−(.||) = unsafeBinaryOp "OR"
786−-- expression :: Expression grp commons schemas params from (nullity 'PGint2)
787−-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
788−-- in printSQL expression
790−-- CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END
792− :: [ ( Condition grp commons schemas params from
793− , Expression grp commons schemas params from ty
795− -- ^ whens and thens
796− -> Expression grp commons schemas params from ty
798− -> Expression grp commons schemas params from ty
799−caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
803− [ " WHEN ", renderSQL when_
804− , " THEN ", renderSQL then_
806− | (when_,then_) <- whenThens
808− , " ELSE ", renderSQL else_
814−-- expression :: Expression grp commons schemas params from (nullity 'PGint2)
815−-- expression = ifThenElse true 1 0
816−-- in printSQL expression
818−-- CASE WHEN TRUE THEN 1 ELSE 0 END
820− :: Condition grp commons schemas params from
821− -> Expression grp commons schemas params from ty -- ^ then
822− -> Expression grp commons schemas params from ty -- ^ else
823− -> Expression grp commons schemas params from ty
824−ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
826−-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
827−-- will produce @NULL@s if one of their arguments is @NULL@.
829−-- >>> printSQL $ true .== null_
832− :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
833− -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
834− -> Condition grp commons schemas params from
835−(.==) = unsafeBinaryOp "="
838−-- | >>> printSQL $ true ./= null_
841− :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
842− -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
843− -> Condition grp commons schemas params from
844−(./=) = unsafeBinaryOp "<>"
847−-- | >>> printSQL $ true .>= null_
850− :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
851− -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
852− -> Condition grp commons schemas params from
853−(.>=) = unsafeBinaryOp ">="
856−-- | >>> printSQL $ true .< null_
859− :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
860− -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
861− -> Condition grp commons schemas params from
862−(.<) = unsafeBinaryOp "<"
865−-- | >>> printSQL $ true .<= null_
868− :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
869− -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
870− -> Condition grp commons schemas params from
871−(.<=) = unsafeBinaryOp "<="
874−-- | >>> printSQL $ true .> null_
877− :: Expression grp commons schemas params from (nullity0 ty) -- ^ lhs
878− -> Expression grp commons schemas params from (nullity1 ty) -- ^ rhs
879− -> Condition grp commons schemas params from
880−(.>) = unsafeBinaryOp ">"
883−-- | >>> printSQL currentDate
886− :: Expression grp commons schemas params from (nullity 'PGdate)
887−currentDate = UnsafeExpression "CURRENT_DATE"
889−-- | >>> printSQL currentTime
892− :: Expression grp commons schemas params from (nullity 'PGtimetz)
893−currentTime = UnsafeExpression "CURRENT_TIME"
895−-- | >>> printSQL currentTimestamp
898− :: Expression grp commons schemas params from (nullity 'PGtimestamptz)
899−currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
901−-- | >>> printSQL localTime
904− :: Expression grp commons schemas params from (nullity 'PGtime)
905−localTime = UnsafeExpression "LOCALTIME"
907−-- | >>> printSQL localTimestamp
910− :: Expression grp commons schemas params from (nullity 'PGtimestamp)
911−localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
913−{-----------------------------------------
915−-----------------------------------------}
917instance IsString
439instance IsString
918− (Expression grp commons schemas params from (nullity 'PGtext)) where
440+ (Expression outer commons grp schemas params from (null 'PGtext)) where
919 fromString str = UnsafeExpression $
441 fromString str = UnsafeExpression $
920 "E\'" <> fromString (escape =<< str) <> "\'"
442 "E\'" <> fromString (escape =<< str) <> "\'"
921− where
443+instance IsString
922− escape = \case
444+ (Expression outer commons grp schemas params from (null 'PGtsvector)) where
923− '\NUL' -> "\\0"
445+ fromString str = UnsafeExpression . parenthesized . (<> " :: tsvector") $
924− '\'' -> "''"
446+ "E\'" <> fromString (escape =<< str) <> "\'"
925− '"' -> "\\\""
447+instance IsString
926− '\b' -> "\\b"
448+ (Expression outer commons grp schemas params from (null 'PGtsquery)) where
927− '\n' -> "\\n"
449+ fromString str = UnsafeExpression . parenthesized . (<> " :: tsquery") $
928− '\r' -> "\\r"
450+ "E\'" <> fromString (escape =<< str) <> "\'"
933instance Semigroup
452instance Semigroup
453+ (Expression outer commons grp schemas params from (null ('PGvararray ty))) where
454+ (<>) = unsafeBinaryOp "||"
456+ (Expression outer commons grp schemas params from (null 'PGjsonb)) where
457+ (<>) = unsafeBinaryOp "||"
934− (Expression grp commons schemas params from (nullity 'PGtext)) where
459+ (Expression outer commons grp schemas params from (null 'PGtext)) where
460+ (<>) = unsafeBinaryOp "||"
462+ (Expression outer commons grp schemas params from (null 'PGtsvector)) where
935 (<>) = unsafeBinaryOp "||"
463 (<>) = unsafeBinaryOp "||"
937instance Monoid
465instance Monoid
938− (Expression grp commons schemas params from (nullity 'PGtext)) where
466+ (Expression outer commons grp schemas params from (null 'PGtext)) where
467+ mempty = fromString ""
470+ (Expression outer commons grp schemas params from (null 'PGtsvector)) where
939 mempty = fromString ""
471 mempty = fromString ""
940 mappend = (<>)
472 mappend = (<>)
942−-- | >>> printSQL $ lower "ARRRGGG"
945− :: Expression grp commons schemas params from (nullity 'PGtext)
946− -- ^ string to lower case
947− -> Expression grp commons schemas params from (nullity 'PGtext)
948−lower = unsafeFunction "lower"
950−-- | >>> printSQL $ upper "eeee"
953− :: Expression grp commons schemas params from (nullity 'PGtext)
954− -- ^ string to upper case
955− -> Expression grp commons schemas params from (nullity 'PGtext)
956−upper = unsafeFunction "upper"
958−-- | >>> printSQL $ charLength "four"
959−-- char_length(E'four')
961− :: Expression grp commons schemas params from (nullity 'PGtext)
962− -- ^ string to measure
963− -> Expression grp commons schemas params from (nullity 'PGint4)
964−charLength = unsafeFunction "char_length"
966−-- | The `like` expression returns true if the @string@ matches
967−-- the supplied @pattern@. If @pattern@ does not contain percent signs
968−-- or underscores, then the pattern only represents the string itself;
969−-- in that case `like` acts like the equals operator. An underscore (_)
970−-- in pattern stands for (matches) any single character; a percent sign (%)
971−-- matches any sequence of zero or more characters.
973−-- >>> printSQL $ "abc" `like` "a%"
974−-- (E'abc' LIKE E'a%')
976− :: Expression grp commons schemas params from (nullity 'PGtext)
978− -> Expression grp commons schemas params from (nullity 'PGtext)
980− -> Expression grp commons schemas params from (nullity 'PGbool)
981−like = unsafeBinaryOp "LIKE"
983−-- | The key word ILIKE can be used instead of LIKE to make the
984−-- match case-insensitive according to the active locale.
986−-- >>> printSQL $ "abc" `ilike` "a%"
987−-- (E'abc' ILIKE E'a%')
989− :: Expression grp commons schemas params from (nullity 'PGtext)
991− -> Expression grp commons schemas params from (nullity 'PGtext)
993− -> Expression grp commons schemas params from (nullity 'PGbool)
994−ilike = unsafeBinaryOp "ILIKE"
996−{-----------------------------------------
997− -- json and jsonb support
999−See https://www.postgresql.org/docs/10/static/functions-json.html -- most
1000−comments lifted directly from this page.
1002−Table 9.44: json and jsonb operators
1003−-----------------------------------------}
1005−-- | Get JSON value (object field or array element) at a key.
1007− :: (json `In` PGJsonType, key `In` PGJsonKey)
1008− => Expression grp commons schemas params from (nullity json)
1009− -> Expression grp commons schemas params from (nullity key)
1010− -> Expression grp commons schemas params from ('Null json)
1012−(.->) = unsafeBinaryOp "->"
1014−-- | Get JSON value (object field or array element) at a key, as text.
1016− :: (json `In` PGJsonType, key `In` PGJsonKey)
1017− => Expression grp commons schemas params from (nullity json)
1018− -> Expression grp commons schemas params from (nullity key)
1019− -> Expression grp commons schemas params from ('Null 'PGtext)
1021−(.->>) = unsafeBinaryOp "->>"
1023−-- | Get JSON value at a specified path.
1025− :: (json `In` PGJsonType, PGTextArray "(.#>)" path)
1026− => Expression grp commons schemas params from (nullity json)
1027− -> Expression grp commons schemas params from (nullity path)
1028− -> Expression grp commons schemas params from ('Null json)
1030−(.#>) = unsafeBinaryOp "#>"
1032−-- | Get JSON value at a specified path as text.
1034− :: (json `In` PGJsonType, PGTextArray "(.#>>)" path)
1035− => Expression grp commons schemas params from (nullity json)
1036− -> Expression grp commons schemas params from (nullity path)
1037− -> Expression grp commons schemas params from ('Null 'PGtext)
1039−(.#>>) = unsafeBinaryOp "#>>"
1041−-- Additional jsonb operators
1043−-- | Does the left JSON value contain the right JSON path/value entries at the
1046− :: Expression grp commons schemas params from (nullity 'PGjsonb)
1047− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1048− -> Condition grp commons schemas params from
1050−(.@>) = unsafeBinaryOp "@>"
1052−-- | Are the left JSON path/value entries contained at the top level within the
1053−-- right JSON value?
1055− :: Expression grp commons schemas params from (nullity 'PGjsonb)
1056− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1057− -> Condition grp commons schemas params from
1059−(.<@) = unsafeBinaryOp "<@"
1061−-- | Does the string exist as a top-level key within the JSON value?
1063− :: Expression grp commons schemas params from (nullity 'PGjsonb)
1064− -> Expression grp commons schemas params from (nullity 'PGtext)
1065− -> Condition grp commons schemas params from
1067−(.?) = unsafeBinaryOp "?"
1069−-- | Do any of these array strings exist as top-level keys?
1071− :: Expression grp commons schemas params from (nullity 'PGjsonb)
1072− -> Expression grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1073− -> Condition grp commons schemas params from
1075−(.?|) = unsafeBinaryOp "?|"
1077−-- | Do all of these array strings exist as top-level keys?
1079− :: Expression grp commons schemas params from (nullity 'PGjsonb)
1080− -> Expression grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1081− -> Condition grp commons schemas params from
1083−(.?&) = unsafeBinaryOp "?&"
1085−-- | Concatenate two jsonb values into a new jsonb value.
1087− Semigroup (Expression commons schemas from grouping param (nullity 'PGjsonb)) where
1088− (<>) = unsafeBinaryOp "||"
1090−-- | Delete a key or keys from a JSON object, or remove an array element.
1092−-- If the right operand is..
1094−-- @ text @: Delete key/value pair or string element from left operand. Key/value pairs
1095−-- are matched based on their key value.
1097−-- @ text[] @: Delete multiple key/value pairs or string elements
1098−-- from left operand. Key/value pairs are matched based on their key value.
1100−-- @ integer @: Delete the array element with specified index (Negative integers
1101−-- count from the end). Throws an error if top level container is not an array.
1103− :: (key `In` '[ 'PGtext, 'PGvararray ('NotNull 'PGtext), 'PGint4, 'PGint2 ]) -- hlint error without parens here
1104− => Expression grp commons schemas params from (nullity 'PGjsonb)
1105− -> Expression grp commons schemas params from (nullity key)
1106− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1108−(.-.) = unsafeBinaryOp "-"
1110−-- | Delete the field or element with specified path (for JSON arrays, negative
1111−-- integers count from the end)
1113− :: PGTextArray "(#-.)" arrayty
1114− => Expression grp commons schemas params from (nullity 'PGjsonb)
1115− -> Expression grp commons schemas params from (nullity arrayty)
1116− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1118−(#-.) = unsafeBinaryOp "#-"
1120−{-----------------------------------------
1121−Table 9.45: JSON creation functions
1122−-----------------------------------------}
1124−-- | Literal binary JSON
1127− => x -> Expression grp commons schemas params from (nullity 'PGjsonb)
1128−jsonbLit = cast jsonb . UnsafeExpression
1129− . singleQuotedUtf8 . toStrict . JSON.encode
1134− => x -> Expression grp commons schemas params from (nullity 'PGjson)
1135−jsonLit = cast json . UnsafeExpression
1136− . singleQuotedUtf8 . toStrict . JSON.encode
1138−-- | Returns the value as json. Arrays and composites are converted
1139−-- (recursively) to arrays and objects; otherwise, if there is a cast from the
1140−-- type to json, the cast function will be used to perform the conversion;
1141−-- otherwise, a scalar value is produced. For any scalar type other than a
1142−-- number, a Boolean, or a null value, the text representation will be used, in
1143−-- such a fashion that it is a valid json value.
1145− :: Expression grp commons schemas params from (nullity ty)
1146− -> Expression grp commons schemas params from (nullity 'PGjson)
1147−toJson = unsafeFunction "to_json"
1149−-- | Returns the value as jsonb. Arrays and composites are converted
1150−-- (recursively) to arrays and objects; otherwise, if there is a cast from the
1151−-- type to json, the cast function will be used to perform the conversion;
1152−-- otherwise, a scalar value is produced. For any scalar type other than a
1153−-- number, a Boolean, or a null value, the text representation will be used, in
1154−-- such a fashion that it is a valid jsonb value.
1156− :: Expression grp commons schemas params from (nullity ty)
1157− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1158−toJsonb = unsafeFunction "to_jsonb"
1160−-- | Returns the array as a JSON array. A PostgreSQL multidimensional array
1161−-- becomes a JSON array of arrays.
1163− :: PGArray "arrayToJson" arr
1164− => Expression grp commons schemas params from (nullity arr)
1165− -> Expression grp commons schemas params from (nullity 'PGjson)
1166−arrayToJson = unsafeFunction "array_to_json"
1168−-- | Returns the row as a JSON object.
1170− :: Expression grp commons schemas params from (nullity ('PGcomposite ty))
1171− -> Expression grp commons schemas params from (nullity 'PGjson)
1172−rowToJson = unsafeFunction "row_to_json"
1174−-- | Builds a possibly-heterogeneously-typed JSON array out of a variadic
1178− => NP (Expression grp commons schemas params from) elems
1179− -> Expression grp commons schemas params from (nullity 'PGjson)
1180−jsonBuildArray = unsafeVariadicFunction "json_build_array"
1182−-- | Builds a possibly-heterogeneously-typed (binary) JSON array out of a
1183−-- variadic argument list.
1186− => NP (Expression grp commons schemas params from) elems
1187− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1188−jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
1192− => NP (Aliased (Expression grp commons schemas params from)) elems
1195− (`appEndo` []) . hcfoldMap (Proxy :: Proxy Top)
1196− (\(col `As` name) -> Endo $ \xs ->
1197− renderAliasString name : renderSQL col : xs)
1199− renderAliasString :: KnownSymbol alias => Alias alias -> ByteString
1200− renderAliasString = singleQuotedText . fromString . symbolVal
1202−-- | Builds a possibly-heterogeneously-typed JSON object out of a variadic
1203−-- argument list. The elements of the argument list must alternate between text
1207− => NP (Aliased (Expression grp commons schemas params from)) elems
1208− -> Expression grp commons schemas params from (nullity 'PGjson)
1210− = unsafeFunction "json_build_object"
1213− . unsafeRowFunction
1215−-- | Builds a possibly-heterogeneously-typed (binary) JSON object out of a
1216−-- variadic argument list. The elements of the argument list must alternate
1217−-- between text and values.
1220− => NP (Aliased (Expression grp commons schemas params from)) elems
1221− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1223− = unsafeFunction "jsonb_build_object"
1226− . unsafeRowFunction
1228−-- | Builds a JSON object out of a text array. The array must have either
1229−-- exactly one dimension with an even number of members, in which case they are
1230−-- taken as alternating key/value pairs, or two dimensions such that each inner
1231−-- array has exactly two elements, which are taken as a key/value pair.
1233− :: PGArrayOf "jsonObject" arr ('NotNull 'PGtext)
1234− => Expression grp commons schemas params from (nullity arr)
1235− -> Expression grp commons schemas params from (nullity 'PGjson)
1236−jsonObject = unsafeFunction "json_object"
1238−-- | Builds a binary JSON object out of a text array. The array must have either
1239−-- exactly one dimension with an even number of members, in which case they are
1240−-- taken as alternating key/value pairs, or two dimensions such that each inner
1241−-- array has exactly two elements, which are taken as a key/value pair.
1243− :: PGArrayOf "jsonbObject" arr ('NotNull 'PGtext)
1244− => Expression grp commons schemas params from (nullity arr)
1245− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1246−jsonbObject = unsafeFunction "jsonb_object"
1248−-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
1249−-- keys and one for values, that are zipped pairwise to create a JSON object.
1251− :: ( PGArrayOf "jsonZipObject" keysArray ('NotNull 'PGtext)
1252− , PGArrayOf "jsonZipObject" valuesArray ('NotNull 'PGtext))
1253− => Expression grp commons schemas params from (nullity keysArray)
1254− -> Expression grp commons schemas params from (nullity valuesArray)
1255− -> Expression grp commons schemas params from (nullity 'PGjson)
1256−jsonZipObject ks vs =
1257− unsafeVariadicFunction "json_object" (ks :* vs :* Nil)
1259−-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
1260−-- keys and one for values, that are zipped pairwise to create a binary JSON
1263− :: ( PGArrayOf "jsonbZipObject" keysArray ('NotNull 'PGtext)
1264− , PGArrayOf "jsonbZipObject" valuesArray ('NotNull 'PGtext))
1265− => Expression grp commons schemas params from (nullity keysArray)
1266− -> Expression grp commons schemas params from (nullity valuesArray)
1267− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1268−jsonbZipObject ks vs =
1269− unsafeVariadicFunction "jsonb_object" (ks :* vs :* Nil)
1271−{-----------------------------------------
1272−Table 9.46: JSON processing functions
1273−-----------------------------------------}
1275−-- | Returns the number of elements in the outermost JSON array.
1277− :: Expression grp commons schemas params from (nullity 'PGjson)
1278− -> Expression grp commons schemas params from (nullity 'PGint4)
1279−jsonArrayLength = unsafeFunction "json_array_length"
1281−-- | Returns the number of elements in the outermost binary JSON array.
1283− :: Expression grp commons schemas params from (nullity 'PGjsonb)
1284− -> Expression grp commons schemas params from (nullity 'PGint4)
1285−jsonbArrayLength = unsafeFunction "jsonb_array_length"
1287−-- | Returns JSON value pointed to by the given path (equivalent to #>
1291− => Expression grp commons schemas params from (nullity 'PGjson)
1292− -> NP (Expression grp commons schemas params from) elems
1293− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1294−jsonExtractPath x xs =
1295− unsafeVariadicFunction "json_extract_path" (x :* xs)
1297−-- | Returns JSON value pointed to by the given path (equivalent to #>
1301− => Expression grp commons schemas params from (nullity 'PGjsonb)
1302− -> NP (Expression grp commons schemas params from) elems
1303− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1304−jsonbExtractPath x xs =
1305− unsafeVariadicFunction "jsonb_extract_path" (x :* xs)
1307−-- | Returns JSON value pointed to by the given path (equivalent to #>
1308−-- operator), as text.
1309−jsonExtractPathAsText
1311− => Expression grp commons schemas params from (nullity 'PGjson)
1312− -> NP (Expression grp commons schemas params from) elems
1313− -> Expression grp commons schemas params from (nullity 'PGjson)
1314−jsonExtractPathAsText x xs =
1315− unsafeVariadicFunction "json_extract_path_text" (x :* xs)
1317−-- | Returns JSON value pointed to by the given path (equivalent to #>
1318−-- operator), as text.
1319−jsonbExtractPathAsText
1321− => Expression grp commons schemas params from (nullity 'PGjsonb)
1322− -> NP (Expression grp commons schemas params from) elems
1323− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1324−jsonbExtractPathAsText x xs =
1325− unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
1327−-- | Returns the type of the outermost JSON value as a text string. Possible
1328−-- types are object, array, string, number, boolean, and null.
1330− :: Expression grp commons schemas params from (nullity 'PGjson)
1331− -> Expression grp commons schemas params from (nullity 'PGtext)
1332−jsonTypeof = unsafeFunction "json_typeof"
1334−-- | Returns the type of the outermost binary JSON value as a text string.
1335−-- Possible types are object, array, string, number, boolean, and null.
1337− :: Expression grp commons schemas params from (nullity 'PGjsonb)
1338− -> Expression grp commons schemas params from (nullity 'PGtext)
1339−jsonbTypeof = unsafeFunction "jsonb_typeof"
1341−-- | Returns its argument with all object fields that have null values omitted.
1342−-- Other null values are untouched.
1344− :: Expression grp commons schemas params from (nullity 'PGjson)
1345− -> Expression grp commons schemas params from (nullity 'PGjson)
1346−jsonStripNulls = unsafeFunction "json_strip_nulls"
1348−-- | Returns its argument with all object fields that have null values omitted.
1349−-- Other null values are untouched.
1351− :: Expression grp commons schemas params from (nullity 'PGjsonb)
1352− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1353−jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
1355−-- | @ jsonbSet target path new_value create_missing @
1357−-- Returns target with the section designated by path replaced by new_value,
1358−-- or with new_value added if create_missing is true ( default is true) and the
1359−-- item designated by path does not exist. As with the path orientated
1360−-- operators, negative integers that appear in path count from the end of JSON
1363− :: PGTextArray "jsonbSet" arr
1364− => Expression grp commons schemas params from (nullity 'PGjsonb)
1365− -> Expression grp commons schemas params from (nullity arr)
1366− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1367− -> Maybe (Expression grp commons schemas params from (nullity 'PGbool))
1368− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1369−jsonbSet tgt path val createMissing = case createMissing of
1370− Just m -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* m :* Nil)
1371− Nothing -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* Nil)
1373−-- | @ jsonbInsert target path new_value insert_after @
1375−-- Returns target with new_value inserted. If target section designated by
1376−-- path is in a JSONB array, new_value will be inserted before target or after
1377−-- if insert_after is true (default is false). If target section designated by
1378−-- path is in JSONB object, new_value will be inserted only if target does not
1379−-- exist. As with the path orientated operators, negative integers that appear
1380−-- in path count from the end of JSON arrays.
1382− :: PGTextArray "jsonbInsert" arr
1383− => Expression grp commons schemas params from (nullity 'PGjsonb)
1384− -> Expression grp commons schemas params from (nullity arr)
1385− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1386− -> Maybe (Expression grp commons schemas params from (nullity 'PGbool))
1387− -> Expression grp commons schemas params from (nullity 'PGjsonb)
1388−jsonbInsert tgt path val insertAfter = case insertAfter of
1389− Just i -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* i :* Nil)
1390− Nothing -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* Nil)
1392−-- | Returns its argument as indented JSON text.
1394− :: Expression grp commons schemas params from (nullity 'PGjsonb)
1395− -> Expression grp commons schemas params from (nullity 'PGtext)
1396−jsonbPretty = unsafeFunction "jsonb_pretty"
1398−{-----------------------------------------
1400−-----------------------------------------}
1402−-- | escape hatch to define aggregate functions
1404− :: ByteString -- ^ aggregate function
1405− -> Expression 'Ungrouped commons schemas params from xty
1406− -> Expression ('Grouped bys) commons schemas params from yty
1407−unsafeAggregate fun x = UnsafeExpression $ mconcat
1408− [fun, "(", renderSQL x, ")"]
1410−-- | escape hatch to define aggregate functions over distinct values
1411−unsafeAggregateDistinct
1412− :: ByteString -- ^ aggregate function
1413− -> Expression 'Ungrouped commons schemas params from xty
1414− -> Expression ('Grouped bys) commons schemas params from yty
1415−unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
1416− [fun, "(DISTINCT ", renderSQL x, ")"]
1420−-- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1421−-- expression = sum_ #col
1422−-- in printSQL expression
1427− => Expression 'Ungrouped commons schemas params from (nullity ty)
1429− -> Expression ('Grouped bys) commons schemas params from (nullity ty)
1430−sum_ = unsafeAggregate "sum"
1434−-- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGnumeric]] (nullity 'PGnumeric)
1435−-- expression = sumDistinct #col
1436−-- in printSQL expression
1438−-- sum(DISTINCT "col")
1441− => Expression 'Ungrouped commons schemas params from (nullity ty)
1443− -> Expression ('Grouped bys) commons schemas params from (nullity ty)
1444−sumDistinct = unsafeAggregateDistinct "sum"
1446−-- | A constraint for `PGType`s that you can take averages of and the resulting
1448−class PGAvg ty avg | ty -> avg where
1450− :: Expression 'Ungrouped commons schemas params from (nullity ty)
1451− -- ^ what to average
1452− -> Expression ('Grouped bys) commons schemas params from (nullity avg)
1453− avg = unsafeAggregate "avg"
1454− avgDistinct = unsafeAggregateDistinct "avg"
1455−instance PGAvg 'PGint2 'PGnumeric
1456−instance PGAvg 'PGint4 'PGnumeric
1457−instance PGAvg 'PGint8 'PGnumeric
1458−instance PGAvg 'PGnumeric 'PGnumeric
1459−instance PGAvg 'PGfloat4 'PGfloat8
1460−instance PGAvg 'PGfloat8 'PGfloat8
1461−instance PGAvg 'PGinterval 'PGinterval
1465−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1466−-- expression = bitAnd #col
1467−-- in printSQL expression
1471− :: int `In` PGIntegral
1472− => Expression 'Ungrouped commons schemas params from (nullity int)
1473− -- ^ what to aggregate
1474− -> Expression ('Grouped bys) commons schemas params from (nullity int)
1475−bitAnd = unsafeAggregate "bit_and"
1479−-- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1480−-- expression = bitOr #col
1481−-- in printSQL expression
1485− :: int `In` PGIntegral
1486− => Expression 'Ungrouped commons schemas params from (nullity int)
1487− -- ^ what to aggregate
1488− -> Expression ('Grouped bys) commons schemas params from (nullity int)
1489−bitOr = unsafeAggregate "bit_or"
1493−-- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1494−-- expression = bitAndDistinct #col
1495−-- in printSQL expression
1497−-- bit_and(DISTINCT "col")
1499− :: int `In` PGIntegral
1500− => Expression 'Ungrouped commons schemas params from (nullity int)
1501− -- ^ what to aggregate
1502− -> Expression ('Grouped bys) commons schemas params from (nullity int)
1503−bitAndDistinct = unsafeAggregateDistinct "bit_and"
1507−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1508−-- expression = bitOrDistinct #col
1509−-- in printSQL expression
1511−-- bit_or(DISTINCT "col")
1513− :: int `In` PGIntegral
1514− => Expression 'Ungrouped commons schemas params from (nullity int)
1515− -- ^ what to aggregate
1516− -> Expression ('Grouped bys) commons schemas params from (nullity int)
1517−bitOrDistinct = unsafeAggregateDistinct "bit_or"
1521−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1522−-- expression = boolAnd #col
1523−-- in printSQL expression
1527− :: Expression 'Ungrouped commons schemas params from (nullity 'PGbool)
1528− -- ^ what to aggregate
1529− -> Expression ('Grouped bys) commons schemas params from (nullity 'PGbool)
1530−boolAnd = unsafeAggregate "bool_and"
1534−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1535−-- expression = boolOr #col
1536−-- in printSQL expression
1540− :: Expression 'Ungrouped commons schemas params from (nullity 'PGbool)
1541− -- ^ what to aggregate
1542− -> Expression ('Grouped bys) commons schemas params from (nullity 'PGbool)
1543−boolOr = unsafeAggregate "bool_or"
1547−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1548−-- expression = boolAndDistinct #col
1549−-- in printSQL expression
1551−-- bool_and(DISTINCT "col")
1553− :: Expression 'Ungrouped commons schemas params from (nullity 'PGbool)
1554− -- ^ what to aggregate
1555− -> Expression ('Grouped bys) commons schemas params from (nullity 'PGbool)
1556−boolAndDistinct = unsafeAggregateDistinct "bool_and"
1560−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1561−-- expression = boolOrDistinct #col
1562−-- in printSQL expression
1564−-- bool_or(DISTINCT "col")
1566− :: Expression 'Ungrouped commons schemas params from (nullity 'PGbool)
1567− -- ^ what to aggregate
1568− -> Expression ('Grouped bys) commons schemas params from (nullity 'PGbool)
1569−boolOrDistinct = unsafeAggregateDistinct "bool_or"
1571−-- | A special aggregation that does not require an input
1573−-- >>> printSQL countStar
1576− :: Expression ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
1577−countStar = UnsafeExpression $ "count(*)"
1581−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1582−-- expression = count #col
1583−-- in printSQL expression
1587− :: Expression 'Ungrouped commons schemas params from ty
1589− -> Expression ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
1590−count = unsafeAggregate "count"
1594−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1595−-- expression = countDistinct #col
1596−-- in printSQL expression
1598−-- count(DISTINCT "col")
1600− :: Expression 'Ungrouped commons schemas params from ty
1602− -> Expression ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
1603−countDistinct = unsafeAggregateDistinct "count"
1605−-- | synonym for `boolAnd`
1609−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1610−-- expression = every #col
1611−-- in printSQL expression
1615− :: Expression 'Ungrouped commons schemas params from (nullity 'PGbool)
1616− -- ^ what to aggregate
1617− -> Expression ('Grouped bys) commons schemas params from (nullity 'PGbool)
1618−every = unsafeAggregate "every"
1620−-- | synonym for `boolAndDistinct`
1624−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1625−-- expression = everyDistinct #col
1626−-- in printSQL expression
1628−-- every(DISTINCT "col")
1630− :: Expression 'Ungrouped commons schemas params from (nullity 'PGbool)
1631− -- ^ what to aggregate
1632− -> Expression ('Grouped bys) commons schemas params from (nullity 'PGbool)
1633−everyDistinct = unsafeAggregateDistinct "every"
1635−-- | minimum and maximum aggregation
1636−max_, min_, maxDistinct, minDistinct
1637− :: Expression 'Ungrouped commons schemas params from (nullity ty)
1638− -- ^ what to aggregate
1639− -> Expression ('Grouped bys) commons schemas params from (nullity ty)
1640−max_ = unsafeAggregate "max"
1641−min_ = unsafeAggregate "min"
1642−maxDistinct = unsafeAggregateDistinct "max"
1643−minDistinct = unsafeAggregateDistinct "min"
1645−{-----------------------------------------
1647−-----------------------------------------}
1649−-- | `TypeExpression`s are used in `cast`s and `createTable` commands.
1650−newtype TypeExpression (schemas :: SchemasType) (ty :: NullityType)
1651− = UnsafeTypeExpression { renderTypeExpression :: ByteString }
1652− deriving (GHC.Generic,Show,Eq,Ord,NFData)
1653−instance RenderSQL (TypeExpression schemas ty) where
1654− renderSQL = renderTypeExpression
1656−-- | The enum or composite type in a `Typedef` can be expressed by its alias.
1658− :: (Has sch schemas schema, Has td schema ('Typedef ty))
1659− => QualifiedAlias sch td
1660− -> TypeExpression schemas (nullity ty)
1661−typedef = UnsafeTypeExpression . renderSQL
1663−-- | The composite type corresponding to a `Table` definition can be expressed
1666− :: (Has sch schemas schema, Has tab schema ('Table table))
1667− => QualifiedAlias sch tab
1668− -> TypeExpression schemas (nullity ('PGcomposite (TableToRow table)))
1669−typetable = UnsafeTypeExpression . renderSQL
1671−-- | The composite type corresponding to a `View` definition can be expressed
1674− :: (Has sch schemas schema, Has vw schema ('View view))
1675− => QualifiedAlias sch vw
1676− -> TypeExpression schemas (nullity ('PGcomposite view))
1677−typeview = UnsafeTypeExpression . renderSQL
1679−-- | logical Boolean (true/false)
1680−bool :: TypeExpression schemas (nullity 'PGbool)
1681−bool = UnsafeTypeExpression "bool"
1682−-- | signed two-byte integer
1683−int2, smallint :: TypeExpression schemas (nullity 'PGint2)
1684−int2 = UnsafeTypeExpression "int2"
1685−smallint = UnsafeTypeExpression "smallint"
1686−-- | signed four-byte integer
1687−int4, int, integer :: TypeExpression schemas (nullity 'PGint4)
1688−int4 = UnsafeTypeExpression "int4"
1689−int = UnsafeTypeExpression "int"
1690−integer = UnsafeTypeExpression "integer"
1691−-- | signed eight-byte integer
1692−int8, bigint :: TypeExpression schemas (nullity 'PGint8)
1693−int8 = UnsafeTypeExpression "int8"
1694−bigint = UnsafeTypeExpression "bigint"
1695−-- | arbitrary precision numeric type
1696−numeric :: TypeExpression schemas (nullity 'PGnumeric)
1697−numeric = UnsafeTypeExpression "numeric"
1698−-- | single precision floating-point number (4 bytes)
1699−float4, real :: TypeExpression schemas (nullity 'PGfloat4)
1700−float4 = UnsafeTypeExpression "float4"
1701−real = UnsafeTypeExpression "real"
1702−-- | double precision floating-point number (8 bytes)
1703−float8, doublePrecision :: TypeExpression schemas (nullity 'PGfloat8)
1704−float8 = UnsafeTypeExpression "float8"
1705−doublePrecision = UnsafeTypeExpression "double precision"
1706−-- | variable-length character string
1707−text :: TypeExpression schemas (nullity 'PGtext)
1708−text = UnsafeTypeExpression "text"
1709−-- | fixed-length character string
1711− :: forall n schemas nullity. (KnownNat n, 1 <= n)
1712− => TypeExpression schemas (nullity ('PGchar n))
1713−char = UnsafeTypeExpression $ "char(" <> renderNat @n <> ")"
1714−character = UnsafeTypeExpression $ "character(" <> renderNat @n <> ")"
1715−-- | variable-length character string
1716−varchar, characterVarying
1717− :: forall n schemas nullity. (KnownNat n, 1 <= n)
1718− => TypeExpression schemas (nullity ('PGvarchar n))
1719−varchar = UnsafeTypeExpression $ "varchar(" <> renderNat @n <> ")"
1720−characterVarying = UnsafeTypeExpression $
1721− "character varying(" <> renderNat @n <> ")"
1722−-- | binary data ("byte array")
1723−bytea :: TypeExpression schemas (nullity 'PGbytea)
1724−bytea = UnsafeTypeExpression "bytea"
1725−-- | date and time (no time zone)
1726−timestamp :: TypeExpression schemas (nullity 'PGtimestamp)
1727−timestamp = UnsafeTypeExpression "timestamp"
1728−-- | date and time, including time zone
1729−timestampWithTimeZone :: TypeExpression schemas (nullity 'PGtimestamptz)
1730−timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
1731−-- | calendar date (year, month, day)
1732−date :: TypeExpression schemas (nullity 'PGdate)
1733−date = UnsafeTypeExpression "date"
1734−-- | time of day (no time zone)
1735−time :: TypeExpression schemas (nullity 'PGtime)
1736−time = UnsafeTypeExpression "time"
1737−-- | time of day, including time zone
1738−timeWithTimeZone :: TypeExpression schemas (nullity 'PGtimetz)
1739−timeWithTimeZone = UnsafeTypeExpression "time with time zone"
1741−interval :: TypeExpression schemas (nullity 'PGinterval)
1742−interval = UnsafeTypeExpression "interval"
1743−-- | universally unique identifier
1744−uuid :: TypeExpression schemas (nullity 'PGuuid)
1745−uuid = UnsafeTypeExpression "uuid"
1746−-- | IPv4 or IPv6 host address
1747−inet :: TypeExpression schemas (nullity 'PGinet)
1748−inet = UnsafeTypeExpression "inet"
1749−-- | textual JSON data
1750−json :: TypeExpression schemas (nullity 'PGjson)
1751−json = UnsafeTypeExpression "json"
1752−-- | binary JSON data, decomposed
1753−jsonb :: TypeExpression schemas (nullity 'PGjsonb)
1754−jsonb = UnsafeTypeExpression "jsonb"
1755−-- | variable length array
1757− :: TypeExpression schemas pg
1758− -> TypeExpression schemas (nullity ('PGvararray pg))
1759−vararray ty = UnsafeTypeExpression $ renderSQL ty <> "[]"
1760−-- | fixed length array
1762−-- >>> renderSQL (fixarray @'[2] json)
1765− :: forall dims schemas nullity pg. All KnownNat dims
1766− => TypeExpression schemas pg
1767− -> TypeExpression schemas (nullity ('PGfixarray dims pg))
1768−fixarray ty = UnsafeTypeExpression $
1769− renderSQL ty <> renderDims @dims
1771− renderDims :: forall ns. All KnownNat ns => ByteString
1775− . ByteString.intercalate "]["
1777− $ hcmap (Proxy @KnownNat)
1778− (K . fromString . show . natVal)
1779− (hpure Proxy :: NP Proxy ns)
1781−-- | `pgtype` is a demoted version of a `PGType`
1782−class PGTyped schemas (ty :: NullityType) where
1783− pgtype :: TypeExpression schemas ty
1784−instance PGTyped schemas (nullity 'PGbool) where pgtype = bool
1785−instance PGTyped schemas (nullity 'PGint2) where pgtype = int2
1786−instance PGTyped schemas (nullity 'PGint4) where pgtype = int4
1787−instance PGTyped schemas (nullity 'PGint8) where pgtype = int8
1788−instance PGTyped schemas (nullity 'PGnumeric) where pgtype = numeric
1789−instance PGTyped schemas (nullity 'PGfloat4) where pgtype = float4
1790−instance PGTyped schemas (nullity 'PGfloat8) where pgtype = float8
1791−instance PGTyped schemas (nullity 'PGtext) where pgtype = text
1792−instance (KnownNat n, 1 <= n)
1793− => PGTyped schemas (nullity ('PGchar n)) where pgtype = char @n
1794−instance (KnownNat n, 1 <= n)
1795− => PGTyped schemas (nullity ('PGvarchar n)) where pgtype = varchar @n
1796−instance PGTyped schemas (nullity 'PGbytea) where pgtype = bytea
1797−instance PGTyped schemas (nullity 'PGtimestamp) where pgtype = timestamp
1798−instance PGTyped schemas (nullity 'PGtimestamptz) where pgtype = timestampWithTimeZone
1799−instance PGTyped schemas (nullity 'PGdate) where pgtype = date
1800−instance PGTyped schemas (nullity 'PGtime) where pgtype = time
1801−instance PGTyped schemas (nullity 'PGtimetz) where pgtype = timeWithTimeZone
1802−instance PGTyped schemas (nullity 'PGinterval) where pgtype = interval
1803−instance PGTyped schemas (nullity 'PGuuid) where pgtype = uuid
1804−instance PGTyped schemas (nullity 'PGjson) where pgtype = json
1805−instance PGTyped schemas (nullity 'PGjsonb) where pgtype = jsonb
1806−instance PGTyped schemas ty
1807− => PGTyped schemas (nullity ('PGvararray ty)) where
1808− pgtype = vararray (pgtype @schemas @ty)
1809−instance (All KnownNat dims, PGTyped schemas ty)
1810− => PGTyped schemas (nullity ('PGfixarray dims ty)) where
1811− pgtype = fixarray @dims (pgtype @schemas @ty)
1813−{-----------------------------------------
1815−-----------------------------------------}
1817−-- | `SortExpression`s are used by `sortBy` to optionally sort the results
1818−-- of a `Query`. `Asc` or `Desc` set the sort direction of a `NotNull` result
1819−-- column to ascending or descending. Ascending order puts smaller values
1820−-- first, where "smaller" is defined in terms of the `.<` operator. Similarly,
1821−-- descending order is determined with the `.>` operator. `AscNullsFirst`,
1822−-- `AscNullsLast`, `DescNullsFirst` and `DescNullsLast` options are used to
1823−-- determine whether nulls appear before or after non-null values in the sort
1824−-- ordering of a `Null` result column.
1825−data SortExpression grp commons schemas params from where
1827− :: Expression grp commons schemas params from ('NotNull ty)
1828− -> SortExpression grp commons schemas params from
1830− :: Expression grp commons schemas params from ('NotNull ty)
1831− -> SortExpression grp commons schemas params from
1833− :: Expression grp commons schemas params from ('Null ty)
1834− -> SortExpression grp commons schemas params from
1836− :: Expression grp commons schemas params from ('Null ty)
1837− -> SortExpression grp commons schemas params from
1839− :: Expression grp commons schemas params from ('Null ty)
1840− -> SortExpression grp commons schemas params from
1842− :: Expression grp commons schemas params from ('Null ty)
1843− -> SortExpression grp commons schemas params from
1844−deriving instance Show (SortExpression grp commons schemas params from)
1846−class OrderBy expr where
1848− :: [SortExpression grp commons schemas params from]
1849− -> expr grp commons schemas params from
1850− -> expr grp commons schemas params from
1852−-- | Render a `SortExpression`.
1853−instance RenderSQL (SortExpression grp commons schemas params from) where
1855− Asc expression -> renderSQL expression <+> "ASC"
1856− Desc expression -> renderSQL expression <+> "DESC"
1857− AscNullsFirst expression -> renderSQL expression
1858− <+> "ASC NULLS FIRST"
1859− DescNullsFirst expression -> renderSQL expression
1860− <+> "DESC NULLS FIRST"
1861− AscNullsLast expression -> renderSQL expression <+> "ASC NULLS LAST"
1862− DescNullsLast expression -> renderSQL expression <+> "DESC NULLS LAST"
1864−data WindowDefinition grp commons schemas params from where
1867− => NP (Expression grp commons schemas params from) bys
1868− -> [SortExpression grp commons schemas params from]
1869− -> WindowDefinition grp commons schemas params from
1871−instance OrderBy WindowDefinition where
1872− orderBy sortsR (WindowDefinition parts sortsL)
1873− = WindowDefinition parts (sortsL ++ sortsR)
1875−instance RenderSQL (WindowDefinition commons schemas from grp params) where
1876− renderSQL (WindowDefinition part ord) =
1877− renderPartitionByClause part <> renderOrderByClause ord
1879− renderPartitionByClause = \case
1881− parts -> "PARTITION" <+> "BY" <+> renderCommaSeparated renderExpression parts
1882− renderOrderByClause = \case
1884− srts -> " ORDER" <+> "BY"
1885− <+> commaSeparated (renderSQL <$> srts)
1889− => NP (Expression grp commons schemas params from) bys
1890− -> WindowDefinition grp commons schemas params from
1891−partitionBy bys = WindowDefinition bys []
1893−newtype WindowFunction
1895− (commons :: FromType)
1896− (schemas :: SchemasType)
1897− (params :: [NullityType])
1899− (ty :: NullityType)
1900− = UnsafeWindowFunction { renderWindowFunction :: ByteString }
1901− deriving (GHC.Generic,Show,Eq,Ord,NFData)
1903−instance RenderSQL (WindowFunction grp commons schemas params from ty) where
1904− renderSQL = renderWindowFunction
1906−{- | rank of the current row with gaps; same as `rowNumber` of its first peer
1910−rank :: WindowFunction grp commons schemas params from ('NotNull 'PGint8)
1911−rank = UnsafeWindowFunction "rank()"
1913−{- | number of the current row within its partition, counting from 1
1914−>>> printSQL rowNumber
1917−rowNumber :: WindowFunction grp commons schemas params from ('NotNull 'PGint8)
1918−rowNumber = UnsafeWindowFunction "row_number()"
1920−{- | rank of the current row without gaps; this function counts peer groups
1921−>>> printSQL denseRank
1924−denseRank :: WindowFunction grp commons schemas params from ('NotNull 'PGint8)
1925−denseRank = UnsafeWindowFunction "dense_rank()"
1927−{- | relative rank of the current row: (rank - 1) / (total partition rows - 1)
1928−>>> printSQL percentRank
1931−percentRank :: WindowFunction grp commons schemas params from ('NotNull 'PGfloat8)
1932−percentRank = UnsafeWindowFunction "percent_rank()"
1934−{- | cumulative distribution: (number of partition rows
1935−preceding or peer with current row) / total partition rows
1936−>>> printSQL cumeDist
1939−cumeDist :: WindowFunction grp commons schemas params from ('NotNull 'PGfloat8)
1940−cumeDist = UnsafeWindowFunction "cume_dist()"
1942−{- | integer ranging from 1 to the argument value,
1943−dividing the partition as equally as possible
1946− :: Expression grp commons schemas params from ('NotNull 'PGint4)
1948− -> WindowFunction grp commons schemas params from ('NotNull 'PGint4)
1949−ntile numBuckets = UnsafeWindowFunction $ "ntile"
1950− <> parenthesized (renderSQL numBuckets)
1952−{- | returns value evaluated at the row that is offset rows before the current
1953−row within the partition; if there is no such row, instead return default
1954−(which must be of the same type as value). Both offset and default are
1955−evaluated with respect to the current row.
1958− :: Expression grp commons schemas params from ty
1960− -> Expression grp commons schemas params from ('NotNull 'PGint4)
1962− -> Expression grp commons schemas params from ty
1964− -> WindowFunction grp commons schemas params from ty
1965−lag value offset def = UnsafeWindowFunction $ "lag"
1967− (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
1969−{- | returns value evaluated at the row that is offset rows after the current
1970−row within the partition; if there is no such row, instead return default
1971−(which must be of the same type as value). Both offset and default are
1972−evaluated with respect to the current row.
1975− :: Expression grp commons schemas params from ty
1977− -> Expression grp commons schemas params from ('NotNull 'PGint4)
1979− -> Expression grp commons schemas params from ty
1981− -> WindowFunction grp commons schemas params from ty
1982−lead value offset def = UnsafeWindowFunction $ "lag"
1984− (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
1986−{- | returns value evaluated at the row that is the
1987−first row of the window frame
1990− :: Expression grp commons schemas params from ty
1992− -> WindowFunction grp commons schemas params from ty
1993−firstValue value = UnsafeWindowFunction $ "first_value"
1994− <> parenthesized (renderSQL value)
1996−{- | returns value evaluated at the row that is the
1997−last row of the window frame
2000− :: Expression grp commons schemas params from ty
2002− -> WindowFunction grp commons schemas params from ty
2003−lastValue value = UnsafeWindowFunction $ "last_value"
2004− <> parenthesized (renderSQL value)
2006−{- | returns value evaluated at the row that is the nth
2007−row of the window frame (counting from 1); null if no such row
2010− :: Expression grp commons schemas params from (nullity ty)
2012− -> Expression grp commons schemas params from ('NotNull 'PGint4)
2014− -> WindowFunction grp commons schemas params from ('Null ty)
2015−nthValue value nth = UnsafeWindowFunction $ "nth_value"
2016− <> parenthesized (commaSeparated [renderSQL value, renderSQL nth])