@@ -1,13 +1,14 @@
11{-|
2−Module: Squeal.PostgreSQL.Expression
2+Module: Squeal.PostgreSQL.Query
33Description: Squeal expressions
4−Copyright: (c) Eitan Chatav, 2019
4+Copyright: (c) Eitan Chatav, 2017
55Maintainer: eitan@morphism.tech
66Stability: experimental
77
88Squeal expressions are the atoms used to build statements.
99-}
1010
11+{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
1112{-# LANGUAGE
1213 AllowAmbiguousTypes
1314 , ConstraintKinds
@@ -15,13 +16,11 @@ Squeal expressions are the atoms used to build statements.
1516 , FlexibleContexts
1617 , FlexibleInstances
1718 , FunctionalDependencies
18− , GADTs
1919 , GeneralizedNewtypeDeriving
2020 , LambdaCase
2121 , MagicHash
2222 , OverloadedStrings
2323 , ScopedTypeVariables
24− , StandaloneDeriving
2524 , TypeApplications
2625 , TypeFamilies
2726 , TypeInType
@@ -32,351 +31,497 @@ Squeal expressions are the atoms used to build statements.
3231
3332module Squeal.PostgreSQL.Expression
3433 ( -- * Expression
35− Expression (..)
36− , Expr
37− , type (-->)
38− , FunctionDB
39− , unsafeFunction
40− , function
41− , unsafeLeftOp
42− , leftOp
43− , unsafeRightOp
44− , rightOp
45− , Operator
46− , OperatorDB
34+ Expression (UnsafeExpression, renderExpression)
35+ , HasParameter (parameter)
36+ , param
37+ -- ** Null
38+ , null_
39+ , notNull
40+ , coalesce
41+ , fromNull
42+ , isNull
43+ , isNotNull
44+ , matchNull
45+ , nullIf
46+ -- ** Collections
47+ , array
48+ , row
49+ -- ** Functions
4750 , unsafeBinaryOp
48− , binaryOp
49− , FunctionVar
50− , unsafeFunctionVar
51− , type (--->)
52− , FunctionNDB
53− , unsafeFunctionN
54− , functionN
55− , PGSubset (..)
56− , PGIntersect (..)
51+ , unsafeUnaryOp
52+ , unsafeFunction
53+ , unsafeVariadicFunction
54+ , atan2_
55+ , cast
56+ , quot_
57+ , rem_
58+ , trunc
59+ , round_
60+ , ceiling_
61+ , greatest
62+ , least
63+ -- ** Conditions
64+ , Condition
65+ , true
66+ , false
67+ , not_
68+ , (.&&)
69+ , (.||)
70+ , caseWhenThenElse
71+ , ifThenElse
72+ , (.==)
73+ , (./=)
74+ , (.>=)
75+ , (.<)
76+ , (.<=)
77+ , (.>)
78+ -- ** Time
79+ , currentDate
80+ , currentTime
81+ , currentTimestamp
82+ , localTime
83+ , localTimestamp
84+ -- ** Text
85+ , lower
86+ , upper
87+ , charLength
88+ , like
89+ -- ** json or jsonb operators
90+ , PGarray
91+ , PGarrayOf
92+ , PGjsonKey
93+ , PGjson_
94+ , PGobjectpairs
95+ , (.->)
96+ , (.->>)
97+ , (.#>)
98+ , (.#>>)
99+ -- *** jsonb only operators
100+ , (.@>)
101+ , (.<@)
102+ , (.?)
103+ , (.?|)
104+ , (.?&)
105+ , (.||.)
106+ , (.-.)
107+ , (#-.)
108+ -- *** Functions
109+ , toJson
110+ , toJsonb
111+ , arrayToJson
112+ , rowToJson
113+ , jsonBuildArray
114+ , jsonbBuildArray
115+ , jsonBuildObject
116+ , jsonbBuildObject
117+ , jsonObjectKeys
118+ , jsonbObjectKeys
119+ -- ** Aggregation
120+ , unsafeAggregate, unsafeAggregateDistinct
121+ , sum_, sumDistinct
122+ , PGAvg (avg, avgDistinct)
123+ , bitAnd, bitOr, boolAnd, boolOr
124+ , bitAndDistinct, bitOrDistinct, boolAndDistinct, boolOrDistinct
125+ , countStar
126+ , count, countDistinct
127+ , every, everyDistinct
128+ , max_, maxDistinct, min_, minDistinct
129+ -- * Types
130+ , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
131+ , PGTyped (pgtype)
132+ , bool
133+ , int2
134+ , smallint
135+ , int4
136+ , int
137+ , integer
138+ , int8
139+ , bigint
140+ , numeric
141+ , float4
142+ , real
143+ , float8
144+ , doublePrecision
145+ , text
146+ , char
147+ , character
148+ , varchar
149+ , characterVarying
150+ , bytea
151+ , timestamp
152+ , timestampWithTimeZone
153+ , date
154+ , time
155+ , timeWithTimeZone
156+ , interval
157+ , uuid
158+ , inet
159+ , json
160+ , jsonb
161+ , vararray
162+ , fixarray
57163 -- * Re-export
58164 , (&)
59− , K (..)
60− , unK
165+ , NP ((:*), Nil)
61166 ) where
62167
63168import Control.Category
64169import Control.DeepSeq
65170import Data.ByteString (ByteString)
66171import Data.Function ((&))
67−import Data.Semigroup hiding (All)
172+import Data.Semigroup
173+import Data.Ratio
68174import Data.String
69−import Generics.SOP hiding (All, from)
175+import Generics.SOP hiding (from)
176+import GHC.Exts (Constraint)
70177import GHC.OverloadedLabels
71178import GHC.TypeLits
72−import Numeric
73179import Prelude hiding (id, (.))
74180
75181import qualified GHC.Generics as GHC
76182
77−import Squeal.PostgreSQL.Alias
78−import Squeal.PostgreSQL.List
79183import Squeal.PostgreSQL.Render
80184import Squeal.PostgreSQL.Schema
81185
82−-- $setup
83−-- >>> import Squeal.PostgreSQL
84−
85186{-----------------------------------------
86187column expressions
87188-----------------------------------------}
88189
89190{- | `Expression`s are used in a variety of contexts,
90−such as in the target `Squeal.PostgreSQL.Query.List` of the
91−`Squeal.PostgreSQL.Query.select` command,
191+such as in the target list of the `Squeal.PostgreSQL.Query.select` command,
92192as new column values in `Squeal.PostgreSQL.Manipulation.insertRow` or
93193`Squeal.PostgreSQL.Manipulation.update`,
94−or in search `Squeal.PostgreSQL.Logic.Condition`s in a number of commands.
194+or in search `Condition`s in a number of commands.
95195
96196The expression syntax allows the calculation of
97197values from primitive expression using arithmetic, logical,
98198and other operations.
99−
100−The type parameters of `Expression` are
101−
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`.
113199-}
114200newtype Expression
115− (outer :: FromType)
116− (commons :: FromType)
117− (grp :: Grouping)
118− (schemas :: SchemasType)
201+ (schema :: SchemaType)
202+ (relations :: RelationsType)
203+ (grouping :: Grouping)
119204 (params :: [NullityType])
120− (from :: FromType)
121205 (ty :: NullityType)
122206 = UnsafeExpression { renderExpression :: ByteString }
123207 deriving (GHC.Generic,Show,Eq,Ord,NFData)
124−instance RenderSQL (Expression outer commons grp schemas params from ty) where
208+
209+instance RenderSQL (Expression schema relations grouping params ty) where
125210 renderSQL = renderExpression
126211
127−-- | An `Expr` is a closed `Expression`.
128−-- It is a F@RankNType@ but don't be scared.
129−-- Think of it as an expression which sees no
130−-- namespaces, so you can't use parameters
131−-- or alias references. It can be used as
132−-- a simple piece of more complex `Expression`s.
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
137−
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
142− -- ^ left input
143− -> Expression outer commons grp schemas params from x2
144− -- ^ right input
145− -> Expression outer commons grp schemas params from y
146− -- ^ output
147−
148−type OperatorDB schemas x1 x2 y
149− = forall outer commons grp params from
150− . Expression outer commons grp schemas params from x1
151− -- ^ left input
152− -> Expression outer commons grp schemas params from x2
153− -- ^ right input
154− -> Expression outer commons grp schemas params from y
155− -- ^ output
156−
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`.
162−type (-->) x y
163− = forall outer commons grp schemas params from
164− . Expression outer commons grp schemas params from x
165− -- ^ input
166− -> Expression outer commons grp schemas params from y
167− -- ^ output
168−
169−type FunctionDB schemas x y
170− = forall outer commons grp params from
171− . Expression outer commons grp schemas params from x
172− -- ^ input
173− -> Expression outer commons grp schemas params from y
174− -- ^ output
175−
176−{- | A @RankNType@ for functions with a fixed-length list of heterogeneous arguments.
177−Use the `*:` operator to end your argument lists, like so.
178−
179−>>> printSQL (unsafeFunctionN "fun" (true :* false :* localTime *: true))
180−fun(TRUE, FALSE, LOCALTIME, TRUE)
212+{- | A `HasParameter` constraint is used to indicate a value that is
213+supplied externally to a SQL statement.
214+`Squeal.PostgreSQL.PQ.manipulateParams`,
215+`Squeal.PostgreSQL.PQ.queryParams` and
216+`Squeal.PostgreSQL.PQ.traversePrepared` support specifying data values
217+separately from the SQL command string, in which case `param`s are used to
218+refer to the out-of-line data values.
181219-}
182−type (--->) xs y
183− = forall outer commons grp schemas params from
184− . NP (Expression outer commons grp schemas params from) xs
185− -- ^ inputs
186− -> Expression outer commons grp schemas params from y
187− -- ^ output
188−
189−type FunctionNDB schemas xs y
190− = forall outer commons grp params from
191− . NP (Expression outer commons grp schemas params from) xs
192− -- ^ inputs
193− -> Expression outer commons grp schemas params from y
194− -- ^ output
195−
196−{- | A @RankNType@ for functions with a variable-length list of
197−homogeneous arguments and at least 1 more argument.
198−-}
199−type FunctionVar x0 x1 y
200− = forall outer commons grp schemas params from
201− . [Expression outer commons grp schemas params from x0]
202− -- ^ inputs
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
206− -- ^ output
207−
208−{- | >>> printSQL (unsafeFunctionVar "greatest" [true, null_] false)
209−greatest(TRUE, NULL, FALSE)
210−-}
211−unsafeFunctionVar :: ByteString -> FunctionVar x0 x1 y
212−unsafeFunctionVar fun xs x = UnsafeExpression $ fun <> parenthesized
213− (commaSeparated (renderSQL <$> xs) <> ", " <> renderSQL x)
214−
215−instance (HasUnique tab (Join outer from) row, Has col row ty)
216− => IsLabel col (Expression outer commons 'Ungrouped schemas params from ty) where
217− fromLabel = UnsafeExpression $ renderSQL (Alias @col)
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))
222− => IsLabel col
223− (Aliased (Expression outer commons 'Ungrouped schemas params from) column) where
224− fromLabel = fromLabel @col `As` Alias
225−instance (HasUnique tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
226− => IsLabel col
227− (NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns) where
228− fromLabel = fromLabel @col :* Nil
229−
230−instance (Has tab (Join outer from) row, Has col row ty)
231− => IsQualified tab col (Expression outer commons 'Ungrouped schemas params from ty) where
232− tab ! col = UnsafeExpression $
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
237−instance (Has tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
238− => IsQualified tab col
239− (Aliased (Expression outer commons 'Ungrouped schemas params from) column) where
240− tab ! col = tab ! col `As` col
241−instance (Has tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
242− => IsQualified tab col
243− (NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns) where
244− tab ! col = tab ! col :* Nil
220+class KnownNat n => HasParameter
221+ (n :: Nat)
222+ (schema :: SchemaType)
223+ (params :: [NullityType])
224+ (ty :: NullityType)
225+ | n params -> ty where
226+ -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
227+ --
228+ -- >>> let expr = parameter @1 int4 :: Expression sch rels grp '[ 'Null 'PGint4] ('Null 'PGint4)
229+ -- >>> printSQL expr
230+ -- ($1 :: int4)
231+ parameter
232+ :: TypeExpression schema (PGTypeOf ty)
233+ -> Expression schema relations grouping params ty
234+ parameter ty = UnsafeExpression $ parenthesized $
235+ "$" <> renderNat (Proxy @n) <+> "::"
236+ <+> renderTypeExpression ty
237+instance {-# OVERLAPPING #-} HasParameter 1 schema (ty1:tys) ty1
238+instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) schema params ty)
239+ => HasParameter n schema (ty' : params) ty
240+
241+-- | `param` takes a `Nat` using type application and for basic types,
242+-- infers a `TypeExpression`.
243+--
244+-- >>> let expr = param @1 :: Expression sch rels grp '[ 'Null 'PGint4] ('Null 'PGint4)
245+-- >>> printSQL expr
246+-- ($1 :: int4)
247+param
248+ :: forall n schema params relations grouping ty
249+ . (PGTyped schema (PGTypeOf ty), HasParameter n schema params ty)
250+ => Expression schema relations grouping params ty -- ^ param
251+param = parameter @n pgtype
252+
253+instance (HasUnique relation relations columns, Has column columns ty)
254+ => IsLabel column (Expression schema relations 'Ungrouped params ty) where
255+ fromLabel = UnsafeExpression $ renderAlias (Alias @column)
256+instance (HasUnique relation relations columns, Has column columns ty)
257+ => IsLabel column
258+ (Aliased (Expression schema relations 'Ungrouped params) (column ::: ty)) where
259+ fromLabel = fromLabel @column `As` Alias @column
260+instance (HasUnique relation relations columns, Has column columns ty)
261+ => IsLabel column
262+ (NP (Aliased (Expression schema relations 'Ungrouped params)) '[column ::: ty]) where
263+ fromLabel = fromLabel @column :* Nil
264+
265+instance (Has relation relations columns, Has column columns ty)
266+ => IsQualified relation column (Expression schema relations 'Ungrouped params ty) where
267+ relation ! column = UnsafeExpression $
268+ renderAlias relation <> "." <> renderAlias column
269+instance (Has relation relations columns, Has column columns ty)
270+ => IsQualified relation column
271+ (Aliased (Expression schema relations 'Ungrouped params) (column ::: ty)) where
272+ relation ! column = relation ! column `As` column
273+instance (Has relation relations columns, Has column columns ty)
274+ => IsQualified relation column
275+ (NP (Aliased (Expression schema relations 'Ungrouped params)) '[column ::: ty]) where
276+ relation ! column = relation ! column :* Nil
245277
246278instance
247− ( HasUnique tab (Join outer from) row
248− , Has col row ty
249− , GroupedBy tab col bys
250− ) => IsLabel col
251− (Expression outer commons ('Grouped bys) schemas params from ty) where
252− fromLabel = UnsafeExpression $ renderSQL (Alias @col)
253−instance
254− ( HasUnique tab (Join outer from) row
255− , Has col row ty
256− , GroupedBy tab col bys
257− , tys ~ '[ty]
258− ) => IsLabel col
259− (NP (Expression outer commons ('Grouped bys) schemas params from) tys) where
260− fromLabel = fromLabel @col :* Nil
279+ ( HasUnique relation relations columns
280+ , Has column columns ty
281+ , GroupedBy relation column bys
282+ ) => IsLabel column
283+ (Expression schema relations ('Grouped bys) params ty) where
284+ fromLabel = UnsafeExpression $ renderAlias (Alias @column)
261285instance
262− ( HasUnique tab (Join outer from) row
263− , Has col row ty
264− , GroupedBy tab col bys
265− , column ~ (col ::: ty)
266− ) => IsLabel col
267− (Aliased (Expression outer commons ('Grouped bys) schemas params from) column) where
268− fromLabel = fromLabel @col `As` Alias
286+ ( HasUnique relation relations columns
287+ , Has column columns ty
288+ , GroupedBy relation column bys
289+ ) => IsLabel column
290+ ( Aliased (Expression schema relations ('Grouped bys) params)
291+ (column ::: ty) ) where
292+ fromLabel = fromLabel @column `As` Alias @column
269293instance
270− ( HasUnique tab (Join outer from) row
271− , Has col row ty
272− , GroupedBy tab col bys
273− , columns ~ '[col ::: ty]
274− ) => IsLabel col
275− (NP (Aliased (Expression outer commons ('Grouped bys) schemas params from)) columns) where
276− fromLabel = fromLabel @col :* Nil
294+ ( HasUnique relation relations columns
295+ , Has column columns ty
296+ , GroupedBy relation column bys
297+ ) => IsLabel column
298+ ( NP (Aliased (Expression schema relations ('Grouped bys) params))
299+ '[column ::: ty] ) where
300+ fromLabel = fromLabel @column :* Nil
277301
278302instance
279− ( Has tab (Join outer from) row
280− , Has col row ty
281− , GroupedBy tab col bys
282− ) => IsQualified tab col
283− (Expression outer commons ('Grouped bys) schemas params from ty) where
284− tab ! col = UnsafeExpression $
285− renderSQL tab <> "." <> renderSQL col
303+ ( Has relation relations columns
304+ , Has column columns ty
305+ , GroupedBy relation column bys
306+ ) => IsQualified relation column
307+ (Expression schema relations ('Grouped bys) params ty) where
308+ relation ! column = UnsafeExpression $
309+ renderAlias relation <> "." <> renderAlias column
286310instance
287− ( Has tab (Join outer from) row
288− , Has col row ty
289− , GroupedBy tab col bys
290− , tys ~ '[ty]
291− ) => IsQualified tab col
292− (NP (Expression outer commons ('Grouped bys) schemas params from) tys) where
293− tab ! col = tab ! col :* Nil
311+ ( Has relation relations columns
312+ , Has column columns ty
313+ , GroupedBy relation column bys
314+ ) => IsQualified relation column
315+ (Aliased (Expression schema relations ('Grouped bys) params)
316+ (column ::: ty)) where
317+ relation ! column = relation ! column `As` column
294318instance
295− ( Has tab (Join outer from) row
296− , Has col row ty
297− , GroupedBy tab col bys
298− , column ~ (col ::: ty)
299− ) => IsQualified tab col
300− (Aliased (Expression outer commons ('Grouped bys) schemas params from) column) where
301− tab ! col = tab ! col `As` col
302−instance
303− ( Has tab (Join outer from) row
304− , Has col row ty
305− , GroupedBy tab col bys
306− , columns ~ '[col ::: ty]
307− ) => IsQualified tab col
308− (NP (Aliased (Expression outer commons ('Grouped bys) schemas params from)) columns) where
309− tab ! col = tab ! col :* Nil
319+ ( Has relation relations columns
320+ , Has column columns ty
321+ , GroupedBy relation column bys
322+ ) => IsQualified relation column
323+ ( NP (Aliased (Expression schema relations ('Grouped bys) params))
324+ '[column ::: ty]) where
325+ relation ! column = relation ! column :* Nil
326+
327+-- | analagous to `Nothing`
328+--
329+-- >>> printSQL null_
330+-- NULL
331+null_ :: Expression schema rels grouping params ('Null ty)
332+null_ = UnsafeExpression "NULL"
333+
334+-- | analagous to `Just`
335+--
336+-- >>> printSQL $ notNull true
337+-- TRUE
338+notNull
339+ :: Expression schema rels grouping params ('NotNull ty)
340+ -> Expression schema rels grouping params ('Null ty)
341+notNull = UnsafeExpression . renderExpression
342+
343+-- | return the leftmost value which is not NULL
344+--
345+-- >>> printSQL $ coalesce [null_, notNull true] false
346+-- COALESCE(NULL, TRUE, FALSE)
347+coalesce
348+ :: [Expression schema relations grouping params ('Null ty)]
349+ -- ^ @NULL@s may be present
350+ -> Expression schema relations grouping params ('NotNull ty)
351+ -- ^ @NULL@ is absent
352+ -> Expression schema relations grouping params ('NotNull ty)
353+coalesce nullxs notNullx = UnsafeExpression $
354+ "COALESCE" <> parenthesized (commaSeparated
355+ ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
356+
357+-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
358+--
359+-- >>> printSQL $ fromNull true null_
360+-- COALESCE(NULL, TRUE)
361+fromNull
362+ :: Expression schema relations grouping params ('NotNull ty)
363+ -- ^ what to convert @NULL@ to
364+ -> Expression schema relations grouping params ('Null ty)
365+ -> Expression schema relations grouping params ('NotNull ty)
366+fromNull notNullx nullx = coalesce [nullx] notNullx
367+
368+-- | >>> printSQL $ null_ & isNull
369+-- NULL IS NULL
370+isNull
371+ :: Expression schema relations grouping params ('Null ty)
372+ -- ^ possibly @NULL@
373+ -> Condition schema relations grouping params
374+isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
375+
376+-- | >>> printSQL $ null_ & isNotNull
377+-- NULL IS NOT NULL
378+isNotNull
379+ :: Expression schema relations grouping params ('Null ty)
380+ -- ^ possibly @NULL@
381+ -> Condition schema relations grouping params
382+isNotNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
383+
384+-- | analagous to `maybe` using @IS NULL@
385+--
386+-- >>> printSQL $ matchNull true not_ null_
387+-- CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END
388+matchNull
389+ :: Expression schema relations grouping params (nullty)
390+ -- ^ what to convert @NULL@ to
391+ -> ( Expression schema relations grouping params ('NotNull ty)
392+ -> Expression schema relations grouping params (nullty) )
393+ -- ^ function to perform when @NULL@ is absent
394+ -> Expression schema relations grouping params ('Null ty)
395+ -> Expression schema relations grouping params (nullty)
396+matchNull y f x = ifThenElse (isNull x) y
397+ (f (UnsafeExpression (renderExpression x)))
398+
399+{-| right inverse to `fromNull`, if its arguments are equal then
400+`nullIf` gives @NULL@.
401+
402+>>> :set -XTypeApplications -XDataKinds
403+>>> let expr = nullIf false (param @1) :: Expression schema rels grp '[ 'NotNull 'PGbool] ('Null 'PGbool)
404+>>> printSQL expr
405+NULL IF (FALSE, ($1 :: bool))
406+-}
407+nullIf
408+ :: Expression schema relations grouping params ('NotNull ty)
409+ -- ^ @NULL@ is absent
410+ -> Expression schema relations grouping params ('NotNull ty)
411+ -- ^ @NULL@ is absent
412+ -> Expression schema relations grouping params ('Null ty)
413+nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
414+ (renderExpression x <> ", " <> renderExpression y)
415+
416+-- | >>> printSQL $ array [null_, notNull false, notNull true]
417+-- ARRAY[NULL, FALSE, TRUE]
418+array
419+ :: [Expression schema relations grouping params ('Null ty)]
420+ -- ^ array elements
421+ -> Expression schema relations grouping params (nullity ('PGvararray ty))
422+array xs = UnsafeExpression $
423+ "ARRAY[" <> commaSeparated (renderExpression <$> xs) <> "]"
310424
311425instance (KnownSymbol label, label `In` labels) => IsPGlabel label
312− (Expression outer commons grp schemas params from (null ('PGenum labels))) where
313− label = UnsafeExpression $ renderSQL (PGlabel @label)
426+ (Expression schema relations grouping params (nullity ('PGenum labels))) where
427+ label = UnsafeExpression $ renderLabel (PGlabel @label)
428+
429+-- | A row constructor is an expression that builds a row value
430+-- (also called a composite value) using values for its member fields.
431+--
432+-- >>> type Complex = PGcomposite '["real" ::: 'PGfloat8, "imaginary" ::: 'PGfloat8]
433+-- >>> let i = row (0 `As` #real :* 1 `As` #imaginary :* Nil) :: Expression '[] '[] 'Ungrouped '[] ('NotNull Complex)
434+-- >>> printSQL i
435+-- ROW(0, 1)
436+row
437+ :: SListI (Nulls fields)
438+ => NP (Aliased (Expression schema relations grouping params)) (Nulls fields)
439+ -- ^ zero or more expressions for the row field values
440+ -> Expression schema relations grouping params (nullity ('PGcomposite fields))
441+row exprs = UnsafeExpression $ "ROW" <> parenthesized
442+ (renderCommaSeparated (\ (expr `As` _) -> renderExpression expr) exprs)
443+
444+instance Has field fields ty => IsLabel field
445+ ( Expression schema relation grouping params (nullity ('PGcomposite fields))
446+ -> Expression schema relation grouping params ('Null ty) ) where
447+ fromLabel expr = UnsafeExpression $
448+ parenthesized (renderExpression expr) <> "." <>
449+ fromString (symbolVal (Proxy @field))
450+
451+instance Semigroup
452+ (Expression schema relations grouping params (nullity ('PGvararray ty))) where
453+ (<>) = unsafeBinaryOp "||"
454+
455+instance Monoid
456+ (Expression schema relations grouping params (nullity ('PGvararray ty))) where
457+ mempty = array []
458+ mappend = (<>)
459+
460+-- | >>> let expr = greatest currentTimestamp [param @1] :: Expression sch rels grp '[ 'NotNull 'PGtimestamptz] ('NotNull 'PGtimestamptz)
461+-- >>> printSQL expr
462+-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
463+greatest
464+ :: Expression schema relations grouping params (nullty)
465+ -- ^ needs at least 1 argument
466+ -> [Expression schema relations grouping params (nullty)]
467+ -- ^ or more
468+ -> Expression schema relations grouping params (nullty)
469+greatest x xs = UnsafeExpression $ "GREATEST("
470+ <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
471+
472+-- | >>> printSQL $ least currentTimestamp [null_]
473+-- LEAST(CURRENT_TIMESTAMP, NULL)
474+least
475+ :: Expression schema relations grouping params (nullty)
476+ -- ^ needs at least 1 argument
477+ -> [Expression schema relations grouping params (nullty)]
478+ -- ^ or more
479+ -> Expression schema relations grouping params (nullty)
480+least x xs = UnsafeExpression $ "LEAST("
481+ <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
314482
315483-- | >>> printSQL $ unsafeBinaryOp "OR" true false
316484-- (TRUE OR FALSE)
317−unsafeBinaryOp :: ByteString -> Operator ty0 ty1 ty2
485+unsafeBinaryOp
486+ :: ByteString
487+ -- ^ operator
488+ -> Expression schema relations grouping params (ty0)
489+ -> Expression schema relations grouping params (ty1)
490+ -> Expression schema relations grouping params (ty2)
318491unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
319− renderSQL x <+> op <+> renderSQL y
492+ renderExpression x <+> op <+> renderExpression y
320493
321−binaryOp
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
327−
328−-- | >>> printSQL $ unsafeLeftOp "NOT" true
494+-- | >>> printSQL $ unsafeUnaryOp "NOT" true
329495-- (NOT TRUE)
330−unsafeLeftOp :: ByteString -> x --> y
331−unsafeLeftOp op x = UnsafeExpression $ parenthesized $ op <+> renderSQL x
332−
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
339−
340−-- | >>> printSQL $ true & unsafeRightOp "IS NOT TRUE"
341−-- (TRUE IS NOT TRUE)
342−unsafeRightOp :: ByteString -> x --> y
343−unsafeRightOp op x = UnsafeExpression $ parenthesized $ renderSQL x <+> op
344−
345−rightOp
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
496+unsafeUnaryOp
497+ :: ByteString
498+ -- ^ operator
499+ -> Expression schema relations grouping params (ty0)
500+ -> Expression schema relations grouping params (ty1)
501+unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
502+ op <+> renderExpression x
351503
352504-- | >>> printSQL $ unsafeFunction "f" true
353505-- f(TRUE)
354−unsafeFunction :: ByteString -> x --> y
506+unsafeFunction
507+ :: ByteString
508+ -- ^ function
509+ -> Expression schema relations grouping params (xty)
510+ -> Expression schema relations grouping params (yty)
355511unsafeFunction fun x = UnsafeExpression $
356− fun <> parenthesized (renderSQL x)
357−
358−function
359− :: (Has sch schemas schema, Has fun schema ('Function ('[x] :=> 'Returns y)))
360− => QualifiedAlias sch fun
361− -> FunctionDB schemas x y
362−function = unsafeFunction . renderSQL
363−
364−-- | >>> printSQL $ unsafeFunctionN "f" (currentTime :* localTimestamp :* false *: literal 'a')
365−-- f(CURRENT_TIME, LOCALTIMESTAMP, FALSE, E'a')
366−unsafeFunctionN :: SListI xs => ByteString -> xs ---> y
367−unsafeFunctionN fun xs = UnsafeExpression $
368− fun <> parenthesized (renderCommaSeparated renderSQL xs)
369−
370−functionN
371− :: ( Has sch schemas schema
372− , Has fun schema ('Function (xs :=> 'Returns y))
373− , SListI xs )
374− => QualifiedAlias sch fun
375− -> FunctionNDB schemas xs y
376−functionN = unsafeFunctionN . renderSQL
377−
378−instance ty `In` PGNum
379− => Num (Expression outer commons grp schemas params from (null ty)) where
512+ fun <> parenthesized (renderExpression x)
513+
514+unsafeVariadicFunction
515+ :: SListI elems
516+ => ByteString
517+ -- ^ function
518+ -> NP (Expression schema relations grouping params) elems
519+ -> Expression schema relations grouping params ret
520+unsafeVariadicFunction fun x = UnsafeExpression $
521+ fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderExpression) x)))
522+
523+instance PGNum ty
524+ => Num (Expression schema relations grouping params (nullity ty)) where
380525 (+) = unsafeBinaryOp "+"
381526 (-) = unsafeBinaryOp "-"
382527 (*) = unsafeBinaryOp "*"
@@ -387,24 +532,19 @@ instance ty `In` PGNum
387532 . fromString
388533 . show
389534
390−instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
391− (Expression outer commons grp schemas params from (null ty)) where
535+instance (PGNum ty, PGFloating ty) => Fractional
536+ (Expression schema relations grouping params (nullity ty)) where
392537 (/) = unsafeBinaryOp "/"
393− fromRational
394− = UnsafeExpression
395− . fromString
396− . ($ "")
397− . showFFloat Nothing
398− . fromRat @Double
538+ fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
399539
400−instance (ty `In` PGNum, ty `In` PGFloating) => Floating
401− (Expression outer commons grp schemas params from (null ty)) where
540+instance (PGNum ty, PGFloating ty) => Floating
541+ (Expression schema relations grouping params (nullity ty)) where
402542 pi = UnsafeExpression "pi()"
403543 exp = unsafeFunction "exp"
404544 log = unsafeFunction "ln"
405545 sqrt = unsafeFunction "sqrt"
406546 b ** x = UnsafeExpression $
407− "power(" <> renderSQL b <> ", " <> renderSQL x <> ")"
547+ "power(" <> renderExpression b <> ", " <> renderExpression x <> ")"
408548 logBase b y = log y / log b
409549 sin = unsafeFunction "sin"
410550 cos = unsafeFunction "cos"
@@ -419,54 +559,995 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Floating
419559 acosh x = log (x + sqrt (x*x - 1))
420560 atanh x = log ((1 + x) / (1 - x)) / 2
421561
422−-- | Contained by operators
423−class PGSubset ty where
424− (@>) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
425− (@>) = unsafeBinaryOp "@>"
426− (<@) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
427− (<@) = unsafeBinaryOp "<@"
428−instance PGSubset 'PGjsonb
429−instance PGSubset 'PGtsquery
430−instance PGSubset ('PGvararray ty)
431−instance PGSubset ('PGrange ty)
432−
433−class PGIntersect ty where
434− (@&&) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
435− (@&&) = unsafeBinaryOp "&&"
436−instance PGIntersect ('PGvararray ty)
437−instance PGIntersect ('PGrange ty)
562+-- | >>> :{
563+-- let
564+-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
565+-- expression = atan2_ pi 2
566+-- in printSQL expression
567+-- :}
568+-- atan2(pi(), 2)
569+atan2_
570+ :: PGFloating float
571+ => Expression schema relations grouping params (nullity float)
572+ -- ^ numerator
573+ -> Expression schema relations grouping params (nullity float)
574+ -- ^ denominator
575+ -> Expression schema relations grouping params (nullity float)
576+atan2_ y x = UnsafeExpression $
577+ "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
578+
579+-- When a `cast` is applied to an `Expression` of a known type, it
580+-- represents a run-time type conversion. The cast will succeed only if a
581+-- suitable type conversion operation has been defined.
582+--
583+-- | >>> printSQL $ true & cast int4
584+-- (TRUE :: int4)
585+cast
586+ :: TypeExpression schema ty1
587+ -- ^ type to cast as
588+ -> Expression schema relations grouping params (nullity ty0)
589+ -- ^ value to convert
590+ -> Expression schema relations grouping params (nullity ty1)
591+cast ty x = UnsafeExpression $ parenthesized $
592+ renderExpression x <+> "::" <+> renderTypeExpression ty
593+
594+-- | integer division, truncates the result
595+--
596+-- >>> :{
597+-- let
598+-- expression :: Expression schema relations grouping params (nullity 'PGint2)
599+-- expression = 5 `quot_` 2
600+-- in printSQL expression
601+-- :}
602+-- (5 / 2)
603+quot_
604+ :: PGIntegral int
605+ => Expression schema relations grouping params (nullity int)
606+ -- ^ numerator
607+ -> Expression schema relations grouping params (nullity int)
608+ -- ^ denominator
609+ -> Expression schema relations grouping params (nullity int)
610+quot_ = unsafeBinaryOp "/"
611+
612+-- | remainder upon integer division
613+--
614+-- >>> :{
615+-- let
616+-- expression :: Expression schema relations grouping params (nullity 'PGint2)
617+-- expression = 5 `rem_` 2
618+-- in printSQL expression
619+-- :}
620+-- (5 % 2)
621+rem_
622+ :: PGIntegral int
623+ => Expression schema relations grouping params (nullity int)
624+ -- ^ numerator
625+ -> Expression schema relations grouping params (nullity int)
626+ -- ^ denominator
627+ -> Expression schema relations grouping params (nullity int)
628+rem_ = unsafeBinaryOp "%"
629+
630+-- | >>> :{
631+-- let
632+-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
633+-- expression = trunc pi
634+-- in printSQL expression
635+-- :}
636+-- trunc(pi())
637+trunc
638+ :: PGFloating frac
639+ => Expression schema relations grouping params (nullity frac)
640+ -- ^ fractional number
641+ -> Expression schema relations grouping params (nullity frac)
642+trunc = unsafeFunction "trunc"
643+
644+-- | >>> :{
645+-- let
646+-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
647+-- expression = round_ pi
648+-- in printSQL expression
649+-- :}
650+-- round(pi())
651+round_
652+ :: PGFloating frac
653+ => Expression schema relations grouping params (nullity frac)
654+ -- ^ fractional number
655+ -> Expression schema relations grouping params (nullity frac)
656+round_ = unsafeFunction "round"
657+
658+-- | >>> :{
659+-- let
660+-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
661+-- expression = ceiling_ pi
662+-- in printSQL expression
663+-- :}
664+-- ceiling(pi())
665+ceiling_
666+ :: PGFloating frac
667+ => Expression schema relations grouping params (nullity frac)
668+ -- ^ fractional number
669+ -> Expression schema relations grouping params (nullity frac)
670+ceiling_ = unsafeFunction "ceiling"
671+
672+-- | A `Condition` is a boolean valued `Expression`. While SQL allows
673+-- conditions to have @NULL@, Squeal instead chooses to disallow @NULL@,
674+-- forcing one to handle the case of @NULL@ explicitly to produce
675+-- a `Condition`.
676+type Condition schema relations grouping params =
677+ Expression schema relations grouping params ('NotNull 'PGbool)
678+
679+-- | >>> printSQL true
680+-- TRUE
681+true :: Condition schema relations grouping params
682+true = UnsafeExpression "TRUE"
683+
684+-- | >>> printSQL false
685+-- FALSE
686+false :: Condition schema relations grouping params
687+false = UnsafeExpression "FALSE"
688+
689+-- | >>> printSQL $ not_ true
690+-- (NOT TRUE)
691+not_
692+ :: Condition schema relations grouping params
693+ -> Condition schema relations grouping params
694+not_ = unsafeUnaryOp "NOT"
695+
696+-- | >>> printSQL $ true .&& false
697+-- (TRUE AND FALSE)
698+(.&&)
699+ :: Condition schema relations grouping params
700+ -> Condition schema relations grouping params
701+ -> Condition schema relations grouping params
702+(.&&) = unsafeBinaryOp "AND"
703+
704+-- | >>> printSQL $ true .|| false
705+-- (TRUE OR FALSE)
706+(.||)
707+ :: Condition schema relations grouping params
708+ -> Condition schema relations grouping params
709+ -> Condition schema relations grouping params
710+(.||) = unsafeBinaryOp "OR"
711+
712+-- | >>> :{
713+-- let
714+-- expression :: Expression schema relations grouping params (nullity 'PGint2)
715+-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
716+-- in printSQL expression
717+-- :}
718+-- CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END
719+caseWhenThenElse
720+ :: [ ( Condition schema relations grouping params
721+ , Expression schema relations grouping params (ty)
722+ ) ]
723+ -- ^ whens and thens
724+ -> Expression schema relations grouping params (ty)
725+ -- ^ else
726+ -> Expression schema relations grouping params (ty)
727+caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
728+ [ "CASE"
729+ , mconcat
730+ [ mconcat
731+ [ " WHEN ", renderExpression when_
732+ , " THEN ", renderExpression then_
733+ ]
734+ | (when_,then_) <- whenThens
735+ ]
736+ , " ELSE ", renderExpression else_
737+ , " END"
738+ ]
739+
740+-- | >>> :{
741+-- let
742+-- expression :: Expression schema relations grouping params (nullity 'PGint2)
743+-- expression = ifThenElse true 1 0
744+-- in printSQL expression
745+-- :}
746+-- CASE WHEN TRUE THEN 1 ELSE 0 END
747+ifThenElse
748+ :: Condition schema relations grouping params
749+ -> Expression schema relations grouping params (ty) -- ^ then
750+ -> Expression schema relations grouping params (ty) -- ^ else
751+ -> Expression schema relations grouping params (ty)
752+ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
753+
754+-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
755+-- will produce @NULL@s if one of their arguments is @NULL@.
756+--
757+-- >>> printSQL $ notNull true .== null_
758+-- (TRUE = NULL)
759+(.==)
760+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
761+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
762+ -> Expression schema relations grouping params (nullity 'PGbool)
763+(.==) = unsafeBinaryOp "="
764+infix 4 .==
765+
766+-- | >>> printSQL $ notNull true ./= null_
767+-- (TRUE <> NULL)
768+(./=)
769+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
770+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
771+ -> Expression schema relations grouping params (nullity 'PGbool)
772+(./=) = unsafeBinaryOp "<>"
773+infix 4 ./=
774+
775+-- | >>> printSQL $ notNull true .>= null_
776+-- (TRUE >= NULL)
777+(.>=)
778+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
779+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
780+ -> Expression schema relations grouping params (nullity 'PGbool)
781+(.>=) = unsafeBinaryOp ">="
782+infix 4 .>=
783+
784+-- | >>> printSQL $ notNull true .< null_
785+-- (TRUE < NULL)
786+(.<)
787+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
788+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
789+ -> Expression schema relations grouping params (nullity 'PGbool)
790+(.<) = unsafeBinaryOp "<"
791+infix 4 .<
792+
793+-- | >>> printSQL $ notNull true .<= null_
794+-- (TRUE <= NULL)
795+(.<=)
796+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
797+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
798+ -> Expression schema relations grouping params (nullity 'PGbool)
799+(.<=) = unsafeBinaryOp "<="
800+infix 4 .<=
801+
802+-- | >>> printSQL $ notNull true .> null_
803+-- (TRUE > NULL)
804+(.>)
805+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
806+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
807+ -> Expression schema relations grouping params (nullity 'PGbool)
808+(.>) = unsafeBinaryOp ">"
809+infix 4 .>
810+
811+-- | >>> printSQL currentDate
812+-- CURRENT_DATE
813+currentDate
814+ :: Expression schema relations grouping params (nullity 'PGdate)
815+currentDate = UnsafeExpression "CURRENT_DATE"
816+
817+-- | >>> printSQL currentTime
818+-- CURRENT_TIME
819+currentTime
820+ :: Expression schema relations grouping params (nullity 'PGtimetz)
821+currentTime = UnsafeExpression "CURRENT_TIME"
822+
823+-- | >>> printSQL currentTimestamp
824+-- CURRENT_TIMESTAMP
825+currentTimestamp
826+ :: Expression schema relations grouping params (nullity 'PGtimestamptz)
827+currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
828+
829+-- | >>> printSQL localTime
830+-- LOCALTIME
831+localTime
832+ :: Expression schema relations grouping params (nullity 'PGtime)
833+localTime = UnsafeExpression "LOCALTIME"
834+
835+-- | >>> printSQL localTimestamp
836+-- LOCALTIMESTAMP
837+localTimestamp
838+ :: Expression schema relations grouping params (nullity 'PGtimestamp)
839+localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
840+
841+{-----------------------------------------
842+text
843+-----------------------------------------}
438844
439845instance IsString
440− (Expression outer commons grp schemas params from (null 'PGtext)) where
846+ (Expression schema relations grouping params (nullity 'PGtext)) where
441847 fromString str = UnsafeExpression $
442848 "E\'" <> fromString (escape =<< str) <> "\'"
443−instance IsString
444− (Expression outer commons grp schemas params from (null 'PGtsvector)) where
445− fromString str = UnsafeExpression . parenthesized . (<> " :: tsvector") $
446− "E\'" <> fromString (escape =<< str) <> "\'"
447−instance IsString
448− (Expression outer commons grp schemas params from (null 'PGtsquery)) where
449− fromString str = UnsafeExpression . parenthesized . (<> " :: tsquery") $
450− "E\'" <> fromString (escape =<< str) <> "\'"
849+ where
850+ escape = \case
851+ '\NUL' -> "\\0"
852+ '\'' -> "''"
853+ '"' -> "\\\""
854+ '\b' -> "\\b"
855+ '\n' -> "\\n"
856+ '\r' -> "\\r"
857+ '\t' -> "\\t"
858+ '\\' -> "\\\\"
859+ c -> [c]
451860
452861instance Semigroup
453− (Expression outer commons grp schemas params from (null ('PGvararray ty))) where
454− (<>) = unsafeBinaryOp "||"
455−instance Semigroup
456− (Expression outer commons grp schemas params from (null 'PGjsonb)) where
457− (<>) = unsafeBinaryOp "||"
458−instance Semigroup
459− (Expression outer commons grp schemas params from (null 'PGtext)) where
460− (<>) = unsafeBinaryOp "||"
461−instance Semigroup
462− (Expression outer commons grp schemas params from (null 'PGtsvector)) where
862+ (Expression schema relations grouping params (nullity 'PGtext)) where
463863 (<>) = unsafeBinaryOp "||"
464864
465865instance Monoid
466− (Expression outer commons grp schemas params from (null 'PGtext)) where
467− mempty = fromString ""
468− mappend = (<>)
469−instance Monoid
470− (Expression outer commons grp schemas params from (null 'PGtsvector)) where
866+ (Expression schema relations grouping params (nullity 'PGtext)) where
471867 mempty = fromString ""
472868 mappend = (<>)
869+
870+-- | >>> printSQL $ lower "ARRRGGG"
871+-- lower(E'ARRRGGG')
872+lower
873+ :: Expression schema relations grouping params (nullity 'PGtext)
874+ -- ^ string to lower case
875+ -> Expression schema relations grouping params (nullity 'PGtext)
876+lower = unsafeFunction "lower"
877+
878+-- | >>> printSQL $ upper "eeee"
879+-- upper(E'eeee')
880+upper
881+ :: Expression schema relations grouping params (nullity 'PGtext)
882+ -- ^ string to upper case
883+ -> Expression schema relations grouping params (nullity 'PGtext)
884+upper = unsafeFunction "upper"
885+
886+-- | >>> printSQL $ charLength "four"
887+-- char_length(E'four')
888+charLength
889+ :: Expression schema relations grouping params (nullity 'PGtext)
890+ -- ^ string to measure
891+ -> Expression schema relations grouping params (nullity 'PGint4)
892+charLength = unsafeFunction "char_length"
893+
894+-- | The `like` expression returns true if the @string@ matches
895+-- the supplied @pattern@. If @pattern@ does not contain percent signs
896+-- or underscores, then the pattern only represents the string itself;
897+-- in that case `like` acts like the equals operator. An underscore (_)
898+-- in pattern stands for (matches) any single character; a percent sign (%)
899+-- matches any sequence of zero or more characters.
900+--
901+-- >>> printSQL $ "abc" `like` "a%"
902+-- (E'abc' LIKE E'a%')
903+like
904+ :: Expression schema relations grouping params (nullity 'PGtext)
905+ -- ^ string
906+ -> Expression schema relations grouping params (nullity 'PGtext)
907+ -- ^ pattern
908+ -> Expression schema relations grouping params (nullity 'PGbool)
909+like = unsafeBinaryOp "LIKE"
910+
911+{-----------------------------------------
912+ -- json and jsonb support
913+
914+See https://www.postgresql.org/docs/10/static/functions-json.html -- most
915+comments lifted directly from this page.
916+
917+Table 9.44: json and jsonb operators
918+-----------------------------------------}
919+
920+-- | Get JSON value (object field or array element) at a key.
921+(.->)
922+ :: (PGjson_ json, PGjsonKey key)
923+ => Expression schema relations grouping params (nullity json)
924+ -> Expression schema relations grouping params (nullity key)
925+ -> Expression schema relations grouping params ('Null json)
926+(.->) = unsafeBinaryOp "->"
927+
928+-- | Get JSON value (object field or array element) at a key, as text.
929+(.->>)
930+ :: (PGjson_ json, PGjsonKey key)
931+ => Expression schema relations grouping params (nullity json)
932+ -> Expression schema relations grouping params (nullity key)
933+ -> Expression schema relations grouping params ('Null 'PGtext)
934+(.->>) = unsafeBinaryOp "->>"
935+
936+-- | Get JSON value at a specified path.
937+(.#>)
938+ :: (PGjson_ json, PGtextArray "(.#>)" path)
939+ => Expression schema relations grouping params (nullity json)
940+ -> Expression schema relations grouping params (nullity path)
941+ -> Expression schema relations grouping params ('Null json)
942+(.#>) = unsafeBinaryOp "#>"
943+
944+-- | Get JSON value at a specified path as text.
945+(.#>>)
946+ :: (PGjson_ json, PGtextArray "(.#>>)" path)
947+ => Expression schema relations grouping params (nullity json)
948+ -> Expression schema relations grouping params (nullity path)
949+ -> Expression schema relations grouping params ('Null 'PGtext)
950+(.#>>) = unsafeBinaryOp "#>>"
951+
952+-- Additional jsonb operators
953+
954+-- | Does the left JSON value contain the right JSON path/value entries at the
955+-- top level?
956+(.@>)
957+ :: Expression schema relations grouping params (nullity 'PGjsonb)
958+ -> Expression schema relations grouping params (nullity 'PGjsonb)
959+ -> Expression schema relations grouping params ('NotNull 'PGbool)
960+(.@>) = unsafeBinaryOp "@>"
961+
962+-- | Are the left JSON path/value entries contained at the top level within the
963+-- right JSON value?
964+(.<@)
965+ :: Expression schema relations grouping params (nullity 'PGjsonb)
966+ -> Expression schema relations grouping params (nullity 'PGjsonb)
967+ -> Expression schema relations grouping params ('NotNull 'PGbool)
968+(.<@) = unsafeBinaryOp "<@"
969+
970+-- | Does the string exist as a top-level key within the JSON value?
971+(.?)
972+ :: Expression schema relations grouping params (nullity 'PGjsonb)
973+ -> Expression schema relations grouping params (nullity 'PGtext)
974+ -> Expression schema relations grouping params ('NotNull 'PGbool)
975+(.?) = unsafeBinaryOp "?"
976+
977+-- | Do any of these array strings exist as top-level keys?
978+(.?|)
979+ :: Expression schema relations grouping params (nullity 'PGjsonb)
980+ -> Expression schema relations grouping params (nullity ('PGvararray 'PGtext))
981+ -> Expression schema relations grouping params ('NotNull 'PGbool)
982+(.?|) = unsafeBinaryOp "?|"
983+
984+-- | Do all of these array strings exist as top-level keys?
985+(.?&)
986+ :: Expression schema relations grouping params (nullity 'PGjsonb)
987+ -> Expression schema relations grouping params (nullity ('PGvararray 'PGtext))
988+ -> Expression schema relations grouping params ('NotNull 'PGbool)
989+(.?&) = unsafeBinaryOp "?&"
990+
991+-- | Concatenate two jsonb values into a new jsonb value.
992+(.||.)
993+ :: Expression schema relations grouping params (nullity 'PGjsonb)
994+ -> Expression schema relations grouping params (nullity 'PGjsonb)
995+ -> Expression schema relations grouping params (nullity 'PGjsonb)
996+(.||.) = unsafeBinaryOp "||"
997+
998+-- | Delete a key or keys from a JSON object, or remove an array element.
999+--
1000+-- If the right operand is..
1001+--
1002+-- @ text @: Delete key/value pair or string element from left operand. Key/value pairs
1003+-- are matched based on their key value.
1004+--
1005+-- @ text[] @: Delete multiple key/value pairs or string elements
1006+-- from left operand. Key/value pairs are matched based on their key value.
1007+--
1008+-- @ integer @: Delete the array element with specified index (Negative integers
1009+-- count from the end). Throws an error if top level container is not an array.
1010+(.-.)
1011+ :: (key `In` '[ 'PGtext, 'PGvararray 'PGtext, 'PGint4, 'PGint2 ]) -- hlint error without parens here
1012+ => Expression schema relations grouping params (nullity 'PGjsonb)
1013+ -> Expression schema relations grouping params (nullity key)
1014+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1015+(.-.) = unsafeBinaryOp "-"
1016+
1017+-- | Delete the field or element with specified path (for JSON arrays, negative
1018+-- integers count from the end)
1019+(#-.)
1020+ :: PGtextArray "(#-.)" arrayty
1021+ => Expression schema relations grouping params (nullity 'PGjsonb)
1022+ -> Expression schema relations grouping params (nullity arrayty)
1023+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1024+(#-.) = unsafeBinaryOp "#-"
1025+
1026+{-----------------------------------------
1027+Table 9.45: JSON creation functions
1028+-----------------------------------------}
1029+
1030+-- | Returns the value as json. Arrays and composites are converted
1031+-- (recursively) to arrays and objects; otherwise, if there is a cast from the
1032+-- type to json, the cast function will be used to perform the conversion;
1033+-- otherwise, a scalar value is produced. For any scalar type other than a
1034+-- number, a Boolean, or a null value, the text representation will be used, in
1035+-- such a fashion that it is a valid json value.
1036+toJson
1037+ :: Expression schema relations grouping params (nullity ty)
1038+ -> Expression schema relations grouping params (nullity 'PGjson)
1039+toJson = unsafeFunction "to_json"
1040+
1041+-- | Returns the value as jsonb. Arrays and composites are converted
1042+-- (recursively) to arrays and objects; otherwise, if there is a cast from the
1043+-- type to json, the cast function will be used to perform the conversion;
1044+-- otherwise, a scalar value is produced. For any scalar type other than a
1045+-- number, a Boolean, or a null value, the text representation will be used, in
1046+-- such a fashion that it is a valid jsonb value.
1047+toJsonb
1048+ :: Expression schema relations grouping params (nullity ty)
1049+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1050+toJsonb = unsafeUnaryOp "to_jsonb"
1051+
1052+-- | Returns the array as a JSON array. A PostgreSQL multidimensional array
1053+-- becomes a JSON array of arrays. Line feeds will be added between dimension-1
1054+-- elements if pretty_bool is true.
1055+arrayToJson
1056+ :: PGarray "arrayToJson" arr
1057+ => Expression schema relations grouping params (nullity arr)
1058+ -> Expression schema relations grouping params (nullity 'PGjson)
1059+arrayToJson = unsafeFunction "array_to_json"
1060+
1061+-- | Returns the row as a JSON object. Line feeds will be added between level-1
1062+-- elements if pretty_bool is true.
1063+rowToJson
1064+ :: Expression schema relations grouping params (nullity ('PGcomposite ty))
1065+ -> Expression schema relations grouping params (nullity 'PGjson)
1066+rowToJson = unsafeFunction "row_to_json"
1067+
1068+-- | Builds a possibly-heterogeneously-typed JSON array out of a variadic
1069+-- argument list.
1070+jsonBuildArray
1071+ :: SListI elems
1072+ => NP (Expression schema relations grouping params) elems
1073+ -> Expression schema relations grouping params (nullity 'PGjson)
1074+jsonBuildArray = unsafeVariadicFunction "json_build_array"
1075+
1076+-- | Builds a possibly-heterogeneously-typed (binary) JSON array out of a
1077+-- variadic argument list.
1078+jsonbBuildArray
1079+ :: SListI elems
1080+ => NP (Expression schema relations grouping params) elems
1081+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1082+jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
1083+
1084+-- | Builds a possibly-heterogeneously-typed JSON object out of a variadic
1085+-- argument list. The elements of the argument list must alternate between text
1086+-- and values.
1087+jsonBuildObject
1088+ :: (PGobjectpairs "jsonBuildObject" elems, SListI elems)
1089+ => NP (Expression schema relations grouping params) elems
1090+ -> Expression schema relations grouping params (nullity 'PGjson)
1091+jsonBuildObject = unsafeVariadicFunction "json_build_object"
1092+
1093+-- | Builds a possibly-heterogeneously-typed (binary) JSON object out of a
1094+-- variadic argument list. The elements of the argument list must alternate
1095+-- between text and values.
1096+jsonbBuildObject
1097+ :: (PGobjectpairs "jsonbBuildObject" elems, SListI elems)
1098+ => NP (Expression schema relations grouping params) elems
1099+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1100+jsonbBuildObject = unsafeVariadicFunction "jsonb_build_object"
1101+
1102+-- | Builds a JSON object out of a text array. The array must have either
1103+-- exactly one dimension with an even number of members, in which case they are
1104+-- taken as alternating key/value pairs, or two dimensions such that each inner
1105+-- array has exactly two elements, which are taken as a key/value pair.
1106+jsonObject
1107+ :: PGarrayOf "jsonObject" arr 'PGtext
1108+ => Expression schema relations grouping params (nullity arr)
1109+ -> Expression schema relations grouping params (nullity 'PGjson)
1110+jsonObject = unsafeFunction "json_object"
1111+
1112+-- | Builds a binary JSON object out of a text array. The array must have either
1113+-- exactly one dimension with an even number of members, in which case they are
1114+-- taken as alternating key/value pairs, or two dimensions such that each inner
1115+-- array has exactly two elements, which are taken as a key/value pair.
1116+jsonbObject
1117+ :: PGarrayOf "jsonbObject" arr 'PGtext
1118+ => Expression schema relations grouping params (nullity arr)
1119+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1120+jsonbObject = unsafeFunction "jsonb_object"
1121+
1122+-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
1123+-- keys and one for values, that are zipped pairwise to create a JSON object.
1124+jsonZipObject
1125+ :: ( PGarrayOf "jsonZipObject" keysArray 'PGtext
1126+ , PGarrayOf "jsonZipObject" valuesArray 'PGtext)
1127+ => Expression schema relations grouping params (nullity keysArray)
1128+ -> Expression schema relations grouping params (nullity valuesArray)
1129+ -> Expression schema relations grouping params (nullity 'PGjson)
1130+jsonZipObject ks vs =
1131+ unsafeVariadicFunction "json_object" (ks :* vs :* Nil)
1132+
1133+-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
1134+-- keys and one for values, that are zipped pairwise to create a binary JSON
1135+-- object.
1136+jsonbZipObject
1137+ :: ( PGarrayOf "jsonbZipObject" keysArray 'PGtext
1138+ , PGarrayOf "jsonbZipObject" valuesArray 'PGtext)
1139+ => Expression schema relations grouping params (nullity keysArray)
1140+ -> Expression schema relations grouping params (nullity valuesArray)
1141+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1142+jsonbZipObject ks vs =
1143+ unsafeVariadicFunction "jsonb_object" (ks :* vs :* Nil)
1144+
1145+{-----------------------------------------
1146+Table 9.46: JSON processing functions
1147+-----------------------------------------}
1148+
1149+-- | Returns the number of elements in the outermost JSON array.
1150+jsonArrayLength
1151+ :: Expression schema relations grouping params (nullity 'PGjson)
1152+ -> Expression schema relations grouping params (nullity 'PGint4)
1153+jsonArrayLength = unsafeFunction "json_array_length"
1154+
1155+-- | Returns the number of elements in the outermost binary JSON array.
1156+jsonbArrayLength
1157+ :: Expression schema relations grouping params (nullity 'PGjsonb)
1158+ -> Expression schema relations grouping params (nullity 'PGint4)
1159+jsonbArrayLength = unsafeFunction "jsonb_array_length"
1160+
1161+jsonObjectKeys
1162+ :: Expression schema relations grouping params (nullity 'PGjson)
1163+ -> Expression schema relations grouping params (nullity 'PGtext)
1164+jsonObjectKeys = unsafeFunction "json_object_keys"
1165+
1166+jsonbObjectKeys
1167+ :: Expression schema relations grouping params (nullity 'PGjsonb)
1168+ -> Expression schema relations grouping params (nullity 'PGtext)
1169+jsonbObjectKeys = unsafeFunction "jsonb_object_keys"
1170+
1171+{-----------------------------------------
1172+aggregation
1173+-----------------------------------------}
1174+
1175+-- | escape hatch to define aggregate functions
1176+unsafeAggregate
1177+ :: ByteString -- ^ aggregate function
1178+ -> Expression schema relations 'Ungrouped params (xty)
1179+ -> Expression schema relations ('Grouped bys) params (yty)
1180+unsafeAggregate fun x = UnsafeExpression $ mconcat
1181+ [fun, "(", renderExpression x, ")"]
1182+
1183+-- | escape hatch to define aggregate functions over distinct values
1184+unsafeAggregateDistinct
1185+ :: ByteString -- ^ aggregate function
1186+ -> Expression schema relations 'Ungrouped params (xty)
1187+ -> Expression schema relations ('Grouped bys) params (yty)
1188+unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
1189+ [fun, "(DISTINCT ", renderExpression x, ")"]
1190+
1191+-- | >>> :{
1192+-- let
1193+-- expression :: Expression schema '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Grouped bys) params ('Null 'PGnumeric)
1194+-- expression = sum_ #col
1195+-- in printSQL expression
1196+-- :}
1197+-- sum("col")
1198+sum_
1199+ :: PGNum ty
1200+ => Expression schema relations 'Ungrouped params (nullity ty)
1201+ -- ^ what to sum
1202+ -> Expression schema relations ('Grouped bys) params (nullity ty)
1203+sum_ = unsafeAggregate "sum"
1204+
1205+-- | >>> :{
1206+-- let
1207+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGnumeric]] ('Grouped bys) params (nullity 'PGnumeric)
1208+-- expression = sumDistinct #col
1209+-- in printSQL expression
1210+-- :}
1211+-- sum(DISTINCT "col")
1212+sumDistinct
1213+ :: PGNum ty
1214+ => Expression schema relations 'Ungrouped params (nullity ty)
1215+ -- ^ what to sum
1216+ -> Expression schema relations ('Grouped bys) params (nullity ty)
1217+sumDistinct = unsafeAggregateDistinct "sum"
1218+
1219+-- | A constraint for `PGType`s that you can take averages of and the resulting
1220+-- `PGType`.
1221+class PGAvg ty avg | ty -> avg where
1222+ avg, avgDistinct
1223+ :: Expression schema relations 'Ungrouped params (nullity ty)
1224+ -- ^ what to average
1225+ -> Expression schema relations ('Grouped bys) params (nullity avg)
1226+ avg = unsafeAggregate "avg"
1227+ avgDistinct = unsafeAggregateDistinct "avg"
1228+instance PGAvg 'PGint2 'PGnumeric
1229+instance PGAvg 'PGint4 'PGnumeric
1230+instance PGAvg 'PGint8 'PGnumeric
1231+instance PGAvg 'PGnumeric 'PGnumeric
1232+instance PGAvg 'PGfloat4 'PGfloat8
1233+instance PGAvg 'PGfloat8 'PGfloat8
1234+instance PGAvg 'PGinterval 'PGinterval
1235+
1236+-- | >>> :{
1237+-- let
1238+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
1239+-- expression = bitAnd #col
1240+-- in printSQL expression
1241+-- :}
1242+-- bit_and("col")
1243+bitAnd
1244+ :: PGIntegral int
1245+ => Expression schema relations 'Ungrouped params (nullity int)
1246+ -- ^ what to aggregate
1247+ -> Expression schema relations ('Grouped bys) params (nullity int)
1248+bitAnd = unsafeAggregate "bit_and"
1249+
1250+-- | >>> :{
1251+-- let
1252+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
1253+-- expression = bitOr #col
1254+-- in printSQL expression
1255+-- :}
1256+-- bit_or("col")
1257+bitOr
1258+ :: PGIntegral int
1259+ => Expression schema relations 'Ungrouped params (nullity int)
1260+ -- ^ what to aggregate
1261+ -> Expression schema relations ('Grouped bys) params (nullity int)
1262+bitOr = unsafeAggregate "bit_or"
1263+
1264+-- | >>> :{
1265+-- let
1266+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
1267+-- expression = bitAndDistinct #col
1268+-- in printSQL expression
1269+-- :}
1270+-- bit_and(DISTINCT "col")
1271+bitAndDistinct
1272+ :: PGIntegral int
1273+ => Expression schema relations 'Ungrouped params (nullity int)
1274+ -- ^ what to aggregate
1275+ -> Expression schema relations ('Grouped bys) params (nullity int)
1276+bitAndDistinct = unsafeAggregateDistinct "bit_and"
1277+
1278+-- | >>> :{
1279+-- let
1280+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
1281+-- expression = bitOrDistinct #col
1282+-- in printSQL expression
1283+-- :}
1284+-- bit_or(DISTINCT "col")
1285+bitOrDistinct
1286+ :: PGIntegral int
1287+ => Expression schema relations 'Ungrouped params (nullity int)
1288+ -- ^ what to aggregate
1289+ -> Expression schema relations ('Grouped bys) params (nullity int)
1290+bitOrDistinct = unsafeAggregateDistinct "bit_or"
1291+
1292+-- | >>> :{
1293+-- let
1294+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1295+-- expression = boolAnd #col
1296+-- in printSQL expression
1297+-- :}
1298+-- bool_and("col")
1299+boolAnd
1300+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
1301+ -- ^ what to aggregate
1302+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
1303+boolAnd = unsafeAggregate "bool_and"
1304+
1305+-- | >>> :{
1306+-- let
1307+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1308+-- expression = boolOr #col
1309+-- in printSQL expression
1310+-- :}
1311+-- bool_or("col")
1312+boolOr
1313+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
1314+ -- ^ what to aggregate
1315+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
1316+boolOr = unsafeAggregate "bool_or"
1317+
1318+-- | >>> :{
1319+-- let
1320+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1321+-- expression = boolAndDistinct #col
1322+-- in printSQL expression
1323+-- :}
1324+-- bool_and(DISTINCT "col")
1325+boolAndDistinct
1326+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
1327+ -- ^ what to aggregate
1328+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
1329+boolAndDistinct = unsafeAggregateDistinct "bool_and"
1330+
1331+-- | >>> :{
1332+-- let
1333+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1334+-- expression = boolOrDistinct #col
1335+-- in printSQL expression
1336+-- :}
1337+-- bool_or(DISTINCT "col")
1338+boolOrDistinct
1339+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
1340+ -- ^ what to aggregate
1341+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
1342+boolOrDistinct = unsafeAggregateDistinct "bool_or"
1343+
1344+-- | A special aggregation that does not require an input
1345+--
1346+-- >>> printSQL countStar
1347+-- count(*)
1348+countStar
1349+ :: Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
1350+countStar = UnsafeExpression $ "count(*)"
1351+
1352+-- | >>> :{
1353+-- let
1354+-- expression :: Expression schema '[tab ::: '["col" ::: nullity ty]] (Grouped bys) params ('NotNull 'PGint8)
1355+-- expression = count #col
1356+-- in printSQL expression
1357+-- :}
1358+-- count("col")
1359+count
1360+ :: Expression schema relations 'Ungrouped params ty
1361+ -- ^ what to count
1362+ -> Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
1363+count = unsafeAggregate "count"
1364+
1365+-- | >>> :{
1366+-- let
1367+-- expression :: Expression schema '[tab ::: '["col" ::: nullity ty]] (Grouped bys) params ('NotNull 'PGint8)
1368+-- expression = countDistinct #col
1369+-- in printSQL expression
1370+-- :}
1371+-- count(DISTINCT "col")
1372+countDistinct
1373+ :: Expression schema relations 'Ungrouped params ty
1374+ -- ^ what to count
1375+ -> Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
1376+countDistinct = unsafeAggregateDistinct "count"
1377+
1378+-- | synonym for `boolAnd`
1379+--
1380+-- >>> :{
1381+-- let
1382+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1383+-- expression = every #col
1384+-- in printSQL expression
1385+-- :}
1386+-- every("col")
1387+every
1388+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
1389+ -- ^ what to aggregate
1390+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
1391+every = unsafeAggregate "every"
1392+
1393+-- | synonym for `boolAndDistinct`
1394+--
1395+-- >>> :{
1396+-- let
1397+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1398+-- expression = everyDistinct #col
1399+-- in printSQL expression
1400+-- :}
1401+-- every(DISTINCT "col")
1402+everyDistinct
1403+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
1404+ -- ^ what to aggregate
1405+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
1406+everyDistinct = unsafeAggregateDistinct "every"
1407+
1408+-- | minimum and maximum aggregation
1409+max_, min_, maxDistinct, minDistinct
1410+ :: Expression schema relations 'Ungrouped params (nullity ty)
1411+ -- ^ what to aggregate
1412+ -> Expression schema relations ('Grouped bys) params (nullity ty)
1413+max_ = unsafeAggregate "max"
1414+min_ = unsafeAggregate "min"
1415+maxDistinct = unsafeAggregateDistinct "max"
1416+minDistinct = unsafeAggregateDistinct "min"
1417+
1418+{-----------------------------------------
1419+type expressions
1420+-----------------------------------------}
1421+
1422+-- | `TypeExpression`s are used in `cast`s and `createTable` commands.
1423+newtype TypeExpression (schema :: SchemaType) (ty :: PGType)
1424+ = UnsafeTypeExpression { renderTypeExpression :: ByteString }
1425+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
1426+
1427+instance (Has alias schema ('Typedef ty))
1428+ => IsLabel alias (TypeExpression schema ty) where
1429+ fromLabel = UnsafeTypeExpression (renderAlias (fromLabel @alias))
1430+
1431+-- | logical Boolean (true/false)
1432+bool :: TypeExpression schema 'PGbool
1433+bool = UnsafeTypeExpression "bool"
1434+-- | signed two-byte integer
1435+int2, smallint :: TypeExpression schema 'PGint2
1436+int2 = UnsafeTypeExpression "int2"
1437+smallint = UnsafeTypeExpression "smallint"
1438+-- | signed four-byte integer
1439+int4, int, integer :: TypeExpression schema 'PGint4
1440+int4 = UnsafeTypeExpression "int4"
1441+int = UnsafeTypeExpression "int"
1442+integer = UnsafeTypeExpression "integer"
1443+-- | signed eight-byte integer
1444+int8, bigint :: TypeExpression schema 'PGint8
1445+int8 = UnsafeTypeExpression "int8"
1446+bigint = UnsafeTypeExpression "bigint"
1447+-- | arbitrary precision numeric type
1448+numeric :: TypeExpression schema 'PGnumeric
1449+numeric = UnsafeTypeExpression "numeric"
1450+-- | single precision floating-point number (4 bytes)
1451+float4, real :: TypeExpression schema 'PGfloat4
1452+float4 = UnsafeTypeExpression "float4"
1453+real = UnsafeTypeExpression "real"
1454+-- | double precision floating-point number (8 bytes)
1455+float8, doublePrecision :: TypeExpression schema 'PGfloat8
1456+float8 = UnsafeTypeExpression "float8"
1457+doublePrecision = UnsafeTypeExpression "double precision"
1458+-- | variable-length character string
1459+text :: TypeExpression schema 'PGtext
1460+text = UnsafeTypeExpression "text"
1461+-- | fixed-length character string
1462+char, character
1463+ :: (KnownNat n, 1 <= n)
1464+ => proxy n
1465+ -> TypeExpression schema ('PGchar n)
1466+char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
1467+character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
1468+-- | variable-length character string
1469+varchar, characterVarying
1470+ :: (KnownNat n, 1 <= n)
1471+ => proxy n
1472+ -> TypeExpression schema ('PGvarchar n)
1473+varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
1474+characterVarying p = UnsafeTypeExpression $
1475+ "character varying(" <> renderNat p <> ")"
1476+-- | binary data ("byte array")
1477+bytea :: TypeExpression schema 'PGbytea
1478+bytea = UnsafeTypeExpression "bytea"
1479+-- | date and time (no time zone)
1480+timestamp :: TypeExpression schema 'PGtimestamp
1481+timestamp = UnsafeTypeExpression "timestamp"
1482+-- | date and time, including time zone
1483+timestampWithTimeZone :: TypeExpression schema 'PGtimestamptz
1484+timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
1485+-- | calendar date (year, month, day)
1486+date :: TypeExpression schema 'PGdate
1487+date = UnsafeTypeExpression "date"
1488+-- | time of day (no time zone)
1489+time :: TypeExpression schema 'PGtime
1490+time = UnsafeTypeExpression "time"
1491+-- | time of day, including time zone
1492+timeWithTimeZone :: TypeExpression schema 'PGtimetz
1493+timeWithTimeZone = UnsafeTypeExpression "time with time zone"
1494+-- | time span
1495+interval :: TypeExpression schema 'PGinterval
1496+interval = UnsafeTypeExpression "interval"
1497+-- | universally unique identifier
1498+uuid :: TypeExpression schema 'PGuuid
1499+uuid = UnsafeTypeExpression "uuid"
1500+-- | IPv4 or IPv6 host address
1501+inet :: TypeExpression schema 'PGinet
1502+inet = UnsafeTypeExpression "inet"
1503+-- | textual JSON data
1504+json :: TypeExpression schema 'PGjson
1505+json = UnsafeTypeExpression "json"
1506+-- | binary JSON data, decomposed
1507+jsonb :: TypeExpression schema 'PGjsonb
1508+jsonb = UnsafeTypeExpression "jsonb"
1509+-- | variable length array
1510+vararray
1511+ :: TypeExpression schema pg
1512+ -> TypeExpression schema ('PGvararray pg)
1513+vararray ty = UnsafeTypeExpression $ renderTypeExpression ty <> "[]"
1514+-- | fixed length array
1515+--
1516+-- >>> renderTypeExpression (fixarray (Proxy @2) json)
1517+-- "json[2]"
1518+fixarray
1519+ :: KnownNat n
1520+ => proxy n
1521+ -> TypeExpression schema pg
1522+ -> TypeExpression schema ('PGfixarray n pg)
1523+fixarray p ty = UnsafeTypeExpression $
1524+ renderTypeExpression ty <> "[" <> renderNat p <> "]"
1525+
1526+-- | `pgtype` is a demoted version of a `PGType`
1527+class PGTyped schema (ty :: PGType) where pgtype :: TypeExpression schema ty
1528+instance PGTyped schema 'PGbool where pgtype = bool
1529+instance PGTyped schema 'PGint2 where pgtype = int2
1530+instance PGTyped schema 'PGint4 where pgtype = int4
1531+instance PGTyped schema 'PGint8 where pgtype = int8
1532+instance PGTyped schema 'PGnumeric where pgtype = numeric
1533+instance PGTyped schema 'PGfloat4 where pgtype = float4
1534+instance PGTyped schema 'PGfloat8 where pgtype = float8
1535+instance PGTyped schema 'PGtext where pgtype = text
1536+instance (KnownNat n, 1 <= n)
1537+ => PGTyped schema ('PGchar n) where pgtype = char (Proxy @n)
1538+instance (KnownNat n, 1 <= n)
1539+ => PGTyped schema ('PGvarchar n) where pgtype = varchar (Proxy @n)
1540+instance PGTyped schema 'PGbytea where pgtype = bytea
1541+instance PGTyped schema 'PGtimestamp where pgtype = timestamp
1542+instance PGTyped schema 'PGtimestamptz where pgtype = timestampWithTimeZone
1543+instance PGTyped schema 'PGdate where pgtype = date
1544+instance PGTyped schema 'PGtime where pgtype = time
1545+instance PGTyped schema 'PGtimetz where pgtype = timeWithTimeZone
1546+instance PGTyped schema 'PGinterval where pgtype = interval
1547+instance PGTyped schema 'PGuuid where pgtype = uuid
1548+instance PGTyped schema 'PGjson where pgtype = json
1549+instance PGTyped schema 'PGjsonb where pgtype = jsonb
1550+instance PGTyped schema ty => PGTyped schema ('PGvararray ty) where
1551+ pgtype = vararray (pgtype @schema @ty)
1552+instance (KnownNat n, PGTyped schema ty) => PGTyped schema ('PGfixarray n ty) where
1553+ pgtype = fixarray (Proxy @n) (pgtype @schema @ty)