on dev help sign in
Merge pull request #112 from morphismtech/dev-textsearch-function
a36ffceEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+2−5
@@ -884,12 +884,9 @@ type family RowPG (hask :: Type) :: RowType where
884 RowPG (P (col ::: head)) = '[col ::: NullPG head]
884 RowPG (P (col ::: head)) = '[col ::: NullPG head]
885 RowPG hask = RowOf (RecordCodeOf hask)
885 RowPG hask = RowOf (RecordCodeOf hask)
886
886
887−type family RowOf (fields :: [(Symbol, Type)]) :: RowType where
887+type family RowOf (record :: [(Symbol, Type)]) :: RowType where
888 RowOf '[] = '[]
888 RowOf '[] = '[]
889− RowOf (field ': fields) = FieldPG field ': RowOf fields
889+ RowOf (col ::: ty ': record) = col ::: NullPG ty ': RowOf record
890−
891−type family FieldPG (field :: (Symbol, Type)) :: (Symbol, NullityType) where
892− FieldPG (field ::: hask) = field ::: NullPG hask
893
890
894{- | `NullPG` turns a Haskell type into a `NullityType`.
891{- | `NullPG` turns a Haskell type into a `NullityType`.
895
892
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+4−4
@@ -298,7 +298,7 @@ check
298 , HasAll aliases (TableToRow table) subcolumns )
298 , HasAll aliases (TableToRow table) subcolumns )
299 => NP Alias aliases
299 => NP Alias aliases
300 -- ^ specify the subcolumns which are getting checked
300 -- ^ specify the subcolumns which are getting checked
301− -> (forall t. Condition '[] 'Ungrouped '[] schemas '[] '[t ::: subcolumns])
301+ -> (forall t. Condition '[] '[] 'Ungrouped schemas '[] '[t ::: subcolumns])
302 -- ^ a closed `Condition` on those subcolumns
302 -- ^ a closed `Condition` on those subcolumns
303 -> TableConstraintExpression sch tab schemas ('Check aliases)
303 -> TableConstraintExpression sch tab schemas ('Check aliases)
304check _cols condition = UnsafeTableConstraintExpression $
304check _cols condition = UnsafeTableConstraintExpression $
@@ -773,7 +773,7 @@ newtype AlterColumn (schemas :: SchemasType) (ty0 :: ColumnType) (ty1 :: ColumnT
773-- :}
773-- :}
774-- ALTER TABLE "tab" ALTER COLUMN "col" SET DEFAULT 5;
774-- ALTER TABLE "tab" ALTER COLUMN "col" SET DEFAULT 5;
775setDefault
775setDefault
776− :: Expression '[] 'Ungrouped '[] schemas '[] '[] ty -- ^ default value to set
776+ :: Expression '[] '[] 'Ungrouped schemas '[] '[] ty -- ^ default value to set
777 -> AlterColumn schemas (constraint :=> ty) ('Def :=> ty)
777 -> AlterColumn schemas (constraint :=> ty) ('Def :=> ty)
778setDefault expression = UnsafeAlterColumn $
778setDefault expression = UnsafeAlterColumn $
779 "SET DEFAULT" <+> renderExpression expression
779 "SET DEFAULT" <+> renderExpression expression
@@ -1003,7 +1003,7 @@ createDomain
1003 :: (Has sch schemas schema, KnownSymbol dom)
1003 :: (Has sch schemas schema, KnownSymbol dom)
1004 => QualifiedAlias sch dom
1004 => QualifiedAlias sch dom
1005 -> (forall nullity. TypeExpression schemas (nullity ty))
1005 -> (forall nullity. TypeExpression schemas (nullity ty))
1006− -> (forall tab. Condition '[] 'Ungrouped '[] schemas '[] '[tab ::: '["value" ::: 'Null ty]])
1006+ -> (forall tab. Condition '[] '[] 'Ungrouped schemas '[] '[tab ::: '["value" ::: 'Null ty]])
1007 -> Definition schemas (Alter sch (Create alias ('Typedef ty) schema) schemas)
1007 -> Definition schemas (Alter sch (Create alias ('Typedef ty) schema) schemas)
1008createDomain dom ty condition =
1008createDomain dom ty condition =
1009 UnsafeDefinition $ "CREATE DOMAIN" <+> renderSQL dom
1009 UnsafeDefinition $ "CREATE DOMAIN" <+> renderSQL dom
@@ -1053,7 +1053,7 @@ notNullable ty = UnsafeColumnTypeExpression $ renderSQL ty <+> "NOT NULL"
1053
1053
1054-- | used in `createTable` commands as a column constraint to give a default
1054-- | used in `createTable` commands as a column constraint to give a default
1055default_
1055default_
1056− :: Expression '[] 'Ungrouped '[] schemas '[] '[] ty
1056+ :: Expression '[] '[] 'Ungrouped schemas '[] '[] ty
1057 -> ColumnTypeExpression schemas ('NoDef :=> ty)
1057 -> ColumnTypeExpression schemas ('NoDef :=> ty)
1058 -> ColumnTypeExpression schemas ('Def :=> ty)
1058 -> ColumnTypeExpression schemas ('Def :=> ty)
1059default_ x ty = UnsafeColumnTypeExpression $
1059default_ x ty = UnsafeColumnTypeExpression $
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+843−849
@@ -34,6 +34,17 @@ Squeal expressions are the atoms used to build statements.
34module Squeal.PostgreSQL.Expression
34module Squeal.PostgreSQL.Expression
35 ( -- * Expression
35 ( -- * Expression
36 Expression (..)
36 Expression (..)
37+ , Expr
38+ , Operator
39+ , unsafeBinaryOp
40+ , (:-->)
41+ , unsafeUnaryOpL
42+ , unsafeUnaryOpR
43+ , unsafeFunction
44+ , FunctionHom
45+ , unsafeFunctionHom
46+ , FunctionHet
47+ , unsafeFunctionHet
37 , HasParameter (parameter)
48 , HasParameter (parameter)
38 , param
49 , param
39 -- ** Null
50 -- ** Null
@@ -47,15 +58,14 @@ module Squeal.PostgreSQL.Expression
47 , nullIf
58 , nullIf
48 -- ** Collections
59 -- ** Collections
49 , array
60 , array
61+ , array1
62+ , array2
50 , cardinality
63 , cardinality
51 , index
64 , index
52 , row
65 , row
53 , field
66 , field
67+ , PGSubset (..)
54 -- ** Functions
68 -- ** Functions
55− , unsafeBinaryOp
56− , unsafeUnaryOp
57− , unsafeFunction
58− , unsafeVariadicFunction
59 , atan2_
69 , atan2_
60 , cast
70 , cast
61 , quot_
71 , quot_
@@ -65,6 +75,7 @@ module Squeal.PostgreSQL.Expression
65 , ceiling_
75 , ceiling_
66 , greatest
76 , greatest
67 , least
77 , least
78+ , litChar
68 -- ** Conditions
79 -- ** Conditions
69 , true
80 , true
70 , false
81 , false
@@ -93,16 +104,19 @@ module Squeal.PostgreSQL.Expression
93 , isUnknown
104 , isUnknown
94 , isNotUnknown
105 , isNotUnknown
95 -- ** Time
106 -- ** Time
107+ , TimeOp (..)
96 , currentDate
108 , currentDate
97 , currentTime
109 , currentTime
98 , currentTimestamp
110 , currentTimestamp
99 , localTime
111 , localTime
100 , localTimestamp
112 , localTimestamp
101− , TimeOp (..)
113+ , now
102 , makeDate
114 , makeDate
103 , makeTime
115 , makeTime
104 , makeTimestamp
116 , makeTimestamp
105 , makeTimestamptz
117 , makeTimestamptz
118+ , interval_
119+ , TimeUnit (..)
106 -- ** Text
120 -- ** Text
107 , lower
121 , lower
108 , upper
122 , upper
@@ -116,8 +130,6 @@ module Squeal.PostgreSQL.Expression
116 , (.#>)
130 , (.#>)
117 , (.#>>)
131 , (.#>>)
118 -- *** Jsonb operators
132 -- *** Jsonb operators
119− , (.@>)
120− , (.<@)
121 , (.?)
133 , (.?)
122 , (.?|)
134 , (.?|)
123 , (.?&)
135 , (.?&)
@@ -132,6 +144,7 @@ module Squeal.PostgreSQL.Expression
132 , rowToJson
144 , rowToJson
133 , jsonBuildArray
145 , jsonBuildArray
134 , jsonbBuildArray
146 , jsonbBuildArray
147+ , PGBuildObject
135 , jsonBuildObject
148 , jsonBuildObject
136 , jsonbBuildObject
149 , jsonbBuildObject
137 , jsonObject
150 , jsonObject
@@ -151,6 +164,28 @@ module Squeal.PostgreSQL.Expression
151 , jsonbSet
164 , jsonbSet
152 , jsonbInsert
165 , jsonbInsert
153 , jsonbPretty
166 , jsonbPretty
167+ -- ** Text Search
168+ , (@@)
169+ , (.&)
170+ , (.|)
171+ , (.!)
172+ , (<->)
173+ , arrayToTSvector
174+ , tsvectorLength
175+ , numnode
176+ , plainToTSquery
177+ , phraseToTSquery
178+ , websearchToTSquery
179+ , queryTree
180+ , toTSquery
181+ , toTSvector
182+ , setWeight
183+ , strip
184+ , jsonToTSvector
185+ , jsonbToTSvector
186+ , tsDelete
187+ , tsFilter
188+ , tsHeadline
154 -- ** Aggregation
189 -- ** Aggregation
155 , Aggregate (..)
190 , Aggregate (..)
156 , Distinction (..)
191 , Distinction (..)
@@ -211,10 +246,11 @@ module Squeal.PostgreSQL.Expression
211 , jsonb
246 , jsonb
212 , vararray
247 , vararray
213 , fixarray
248 , fixarray
249+ , tsvector
250+ , tsquery
214 -- * Re-export
251 -- * Re-export
215 , (&)
252 , (&)
216 , NP (..)
253 , NP (..)
217− , (:*:) (..)
218 , K (..)
254 , K (..)
219 ) where
255 ) where
220
256
@@ -223,12 +259,12 @@ import Control.DeepSeq
223import Data.ByteString (ByteString)
259import Data.ByteString (ByteString)
224import Data.ByteString.Lazy (toStrict)
260import Data.ByteString.Lazy (toStrict)
225import Data.Function ((&))
261import Data.Function ((&))
262+import Data.Kind
226import Data.Semigroup hiding (All)
263import Data.Semigroup hiding (All)
227import qualified Data.Aeson as JSON
264import qualified Data.Aeson as JSON
228import Data.String
265import Data.String
229import Data.Word
266import Data.Word
230import Generics.SOP hiding (All, from)
267import Generics.SOP hiding (All, from)
231−import GHC.Generics ((:*:) (..))
232import GHC.OverloadedLabels
268import GHC.OverloadedLabels
233import GHC.TypeLits
269import GHC.TypeLits
234import Numeric
270import Numeric
@@ -257,18 +293,82 @@ and other operations.
257-}
293-}
258newtype Expression
294newtype Expression
259 (outer :: FromType)
295 (outer :: FromType)
260− (grp :: Grouping)
261 (commons :: FromType)
296 (commons :: FromType)
297+ (grp :: Grouping)
262 (schemas :: SchemasType)
298 (schemas :: SchemasType)
263 (params :: [NullityType])
299 (params :: [NullityType])
264 (from :: FromType)
300 (from :: FromType)
265 (ty :: NullityType)
301 (ty :: NullityType)
266 = UnsafeExpression { renderExpression :: ByteString }
302 = UnsafeExpression { renderExpression :: ByteString }
267 deriving (GHC.Generic,Show,Eq,Ord,NFData)
303 deriving (GHC.Generic,Show,Eq,Ord,NFData)
268−
269−instance RenderSQL (Expression outer grp commons schemas params from ty) where
304+instance RenderSQL (Expression outer commons grp schemas params from ty) where
270 renderSQL = renderExpression
305 renderSQL = renderExpression
271
306
307+-- | An `Expr` is a closed `Expression`.
308+-- It is a F`RankNType` but don't be scared.
309+-- Think of it as an expression which sees no
310+-- namespaces, so you can't use parameters
311+-- or alias references. It can be used as
312+-- a simple piece of more complex `Expression`s.
313+type Expr x
314+ = forall outer commons grp schemas params from
315+ . Expression outer commons grp schemas params from x
316+ -- ^ cannot reference aliases
317+
318+-- | A `RankNType` for binary operators.
319+type Operator x1 x2 y
320+ = forall outer commons grp schemas params from
321+ . Expression outer commons grp schemas params from x1
322+ -- ^ left input
323+ -> Expression outer commons grp schemas params from x2
324+ -- ^ right input
325+ -> Expression outer commons grp schemas params from y
326+ -- ^ output
327+
328+-- | A `RankNType` for functions with a single argument.
329+-- These could be either function calls or unary operators.
330+-- This is a subtype of the usual Haskell function type `Prelude.->`,
331+-- indeed a subcategory as it is closed under the usual
332+-- `Prelude..` and `Prelude.id`.
333+type (:-->) x y
334+ = forall outer commons grp schemas params from
335+ . Expression outer commons grp schemas params from x
336+ -- ^ input
337+ -> Expression outer commons grp schemas params from y
338+ -- ^ output
339+
340+{- | A `RankNType` for functions with a fixed-length list of heterogeneous arguments.
341+Use the `*:` operator to end your argument lists, like so.
342+
343+>>> printSQL (unsafeFunctionHet "fun" (true :* false :* localTime *: true))
344+fun(TRUE, FALSE, LOCALTIME, TRUE)
345+-}
346+type FunctionHet xs y
347+ = forall outer commons grp schemas params from
348+ . NP (Expression outer commons grp schemas params from) xs
349+ -- ^ inputs
350+ -> Expression outer commons grp schemas params from y
351+ -- ^ output
352+
353+{- | A `RankNType` for functions with a variable-length list of
354+homogeneous arguments and at least 1 more argument.
355+-}
356+type FunctionHom x0 x1 y
357+ = forall outer commons grp schemas params from
358+ . [Expression outer commons grp schemas params from x0]
359+ -- ^ inputs
360+ -> Expression outer commons grp schemas params from x1
361+ -- ^ must have at least 1 input
362+ -> Expression outer commons grp schemas params from y
363+ -- ^ output
364+
365+{- | >>> printSQL (unsafeFunctionHom "greatest" [true, null_] false)
366+greatest(TRUE, NULL, FALSE)
367+-}
368+unsafeFunctionHom :: ByteString -> FunctionHom x0 x1 y
369+unsafeFunctionHom fun xs x = UnsafeExpression $ fun <> parenthesized
370+ (commaSeparated (renderSQL <$> xs) <> ", " <> renderSQL x)
371+
272{- | A `HasParameter` constraint is used to indicate a value that is
372{- | A `HasParameter` constraint is used to indicate a value that is
273supplied externally to a SQL statement.
373supplied externally to a SQL statement.
274`Squeal.PostgreSQL.PQ.manipulateParams`,
374`Squeal.PostgreSQL.PQ.manipulateParams`,
@@ -284,12 +384,12 @@ class KnownNat n => HasParameter
284 | n params -> ty where
384 | n params -> ty where
285 -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
385 -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
286 --
386 --
287− -- >>> let expr = parameter @1 int4 :: Expression outer grp '[] schemas '[ 'Null 'PGint4] from ('Null 'PGint4)
387+ -- >>> let expr = parameter @1 int4 :: Expression outer '[] grp schemas '[ 'Null 'PGint4] from ('Null 'PGint4)
288 -- >>> printSQL expr
388 -- >>> printSQL expr
289 -- ($1 :: int4)
389 -- ($1 :: int4)
290 parameter
390 parameter
291 :: TypeExpression schemas ty
391 :: TypeExpression schemas ty
292− -> Expression outer grp commons schemas params from ty
392+ -> Expression outer commons grp schemas params from ty
293 parameter ty = UnsafeExpression $ parenthesized $
393 parameter ty = UnsafeExpression $ parenthesized $
294 "$" <> renderNat @n <+> "::"
394 "$" <> renderNat @n <+> "::"
295 <+> renderSQL ty
395 <+> renderSQL ty
@@ -300,44 +400,44 @@ instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
300-- | `param` takes a `Nat` using type application and for basic types,
400-- | `param` takes a `Nat` using type application and for basic types,
301-- infers a `TypeExpression`.
401-- infers a `TypeExpression`.
302--
402--
303−-- >>> let expr = param @1 :: Expression outer grp commons schemas '[ 'Null 'PGint4] from ('Null 'PGint4)
403+-- >>> let expr = param @1 :: Expression outer commons grp schemas '[ 'Null 'PGint4] from ('Null 'PGint4)
304-- >>> printSQL expr
404-- >>> printSQL expr
305-- ($1 :: int4)
405-- ($1 :: int4)
306param
406param
307 :: forall n outer commons schemas params from grp ty
407 :: forall n outer commons schemas params from grp ty
308 . (PGTyped schemas ty, HasParameter n params ty)
408 . (PGTyped schemas ty, HasParameter n params ty)
309− => Expression outer grp commons schemas params from ty -- ^ param
409+ => Expression outer commons grp schemas params from ty -- ^ param
310param = parameter @n (pgtype @schemas)
410param = parameter @n (pgtype @schemas)
311
411
312instance (HasUnique tab (Join outer from) row, Has col row ty)
412instance (HasUnique tab (Join outer from) row, Has col row ty)
313− => IsLabel col (Expression outer 'Ungrouped commons schemas params from ty) where
413+ => IsLabel col (Expression outer commons 'Ungrouped schemas params from ty) where
314 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
414 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
315instance (HasUnique tab (Join outer from) row, Has col row ty, tys ~ '[ty])
415instance (HasUnique tab (Join outer from) row, Has col row ty, tys ~ '[ty])
316− => IsLabel col (NP (Expression outer 'Ungrouped commons schemas params from) tys) where
416+ => IsLabel col (NP (Expression outer commons 'Ungrouped schemas params from) tys) where
317 fromLabel = fromLabel @col :* Nil
417 fromLabel = fromLabel @col :* Nil
318instance (HasUnique tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
418instance (HasUnique tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
319 => IsLabel col
419 => IsLabel col
320− (Aliased (Expression outer 'Ungrouped commons schemas params from) column) where
420+ (Aliased (Expression outer commons 'Ungrouped schemas params from) column) where
321 fromLabel = fromLabel @col `As` Alias
421 fromLabel = fromLabel @col `As` Alias
322instance (HasUnique tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
422instance (HasUnique tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
323 => IsLabel col
423 => IsLabel col
324− (NP (Aliased (Expression outer 'Ungrouped commons schemas params from)) columns) where
424+ (NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns) where
325 fromLabel = fromLabel @col :* Nil
425 fromLabel = fromLabel @col :* Nil
326
426
327instance (Has tab (Join outer from) row, Has col row ty)
427instance (Has tab (Join outer from) row, Has col row ty)
328− => IsQualified tab col (Expression outer 'Ungrouped commons schemas params from ty) where
428+ => IsQualified tab col (Expression outer commons 'Ungrouped schemas params from ty) where
329 tab ! col = UnsafeExpression $
429 tab ! col = UnsafeExpression $
330 renderSQL tab <> "." <> renderSQL col
430 renderSQL tab <> "." <> renderSQL col
331instance (Has tab (Join outer from) row, Has col row ty, tys ~ '[ty])
431instance (Has tab (Join outer from) row, Has col row ty, tys ~ '[ty])
332− => IsQualified tab col (NP (Expression outer 'Ungrouped commons schemas params from) tys) where
432+ => IsQualified tab col (NP (Expression outer commons 'Ungrouped schemas params from) tys) where
333 tab ! col = tab ! col :* Nil
433 tab ! col = tab ! col :* Nil
334instance (Has tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
434instance (Has tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
335 => IsQualified tab col
435 => IsQualified tab col
336− (Aliased (Expression outer 'Ungrouped commons schemas params from) column) where
436+ (Aliased (Expression outer commons 'Ungrouped schemas params from) column) where
337 tab ! col = tab ! col `As` col
437 tab ! col = tab ! col `As` col
338instance (Has tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
438instance (Has tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
339 => IsQualified tab col
439 => IsQualified tab col
340− (NP (Aliased (Expression outer 'Ungrouped commons schemas params from)) columns) where
440+ (NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns) where
341 tab ! col = tab ! col :* Nil
441 tab ! col = tab ! col :* Nil
342
442
343instance
443instance
@@ -345,7 +445,7 @@ instance
345 , Has col row ty
445 , Has col row ty
346 , GroupedBy tab col bys
446 , GroupedBy tab col bys
347 ) => IsLabel col
447 ) => IsLabel col
348− (Expression outer ('Grouped bys) commons schemas params from ty) where
448+ (Expression outer commons ('Grouped bys) schemas params from ty) where
349 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
449 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
350instance
450instance
351 ( HasUnique tab (Join outer from) row
451 ( HasUnique tab (Join outer from) row
@@ -353,7 +453,7 @@ instance
353 , GroupedBy tab col bys
453 , GroupedBy tab col bys
354 , tys ~ '[ty]
454 , tys ~ '[ty]
355 ) => IsLabel col
455 ) => IsLabel col
356− (NP (Expression outer ('Grouped bys) commons schemas params from) tys) where
456+ (NP (Expression outer commons ('Grouped bys) schemas params from) tys) where
357 fromLabel = fromLabel @col :* Nil
457 fromLabel = fromLabel @col :* Nil
358instance
458instance
359 ( HasUnique tab (Join outer from) row
459 ( HasUnique tab (Join outer from) row
@@ -361,7 +461,7 @@ instance
361 , GroupedBy tab col bys
461 , GroupedBy tab col bys
362 , column ~ (col ::: ty)
462 , column ~ (col ::: ty)
363 ) => IsLabel col
463 ) => IsLabel col
364− (Aliased (Expression outer ('Grouped bys) commons schemas params from) column) where
464+ (Aliased (Expression outer commons ('Grouped bys) schemas params from) column) where
365 fromLabel = fromLabel @col `As` Alias
465 fromLabel = fromLabel @col `As` Alias
366instance
466instance
367 ( HasUnique tab (Join outer from) row
467 ( HasUnique tab (Join outer from) row
@@ -369,7 +469,7 @@ instance
369 , GroupedBy tab col bys
469 , GroupedBy tab col bys
370 , columns ~ '[col ::: ty]
470 , columns ~ '[col ::: ty]
371 ) => IsLabel col
471 ) => IsLabel col
372− (NP (Aliased (Expression outer ('Grouped bys) commons schemas params from)) columns) where
472+ (NP (Aliased (Expression outer commons ('Grouped bys) schemas params from)) columns) where
373 fromLabel = fromLabel @col :* Nil
473 fromLabel = fromLabel @col :* Nil
374
474
375instance
475instance
@@ -377,7 +477,7 @@ instance
377 , Has col row ty
477 , Has col row ty
378 , GroupedBy tab col bys
478 , GroupedBy tab col bys
379 ) => IsQualified tab col
479 ) => IsQualified tab col
380− (Expression outer ('Grouped bys) commons schemas params from ty) where
480+ (Expression outer commons ('Grouped bys) schemas params from ty) where
381 tab ! col = UnsafeExpression $
481 tab ! col = UnsafeExpression $
382 renderSQL tab <> "." <> renderSQL col
482 renderSQL tab <> "." <> renderSQL col
383instance
483instance
@@ -386,7 +486,7 @@ instance
386 , GroupedBy tab col bys
486 , GroupedBy tab col bys
387 , tys ~ '[ty]
487 , tys ~ '[ty]
388 ) => IsQualified tab col
488 ) => IsQualified tab col
389− (NP (Expression outer ('Grouped bys) commons schemas params from) tys) where
489+ (NP (Expression outer commons ('Grouped bys) schemas params from) tys) where
390 tab ! col = tab ! col :* Nil
490 tab ! col = tab ! col :* Nil
391instance
491instance
392 ( Has tab (Join outer from) row
492 ( Has tab (Join outer from) row
@@ -394,7 +494,7 @@ instance
394 , GroupedBy tab col bys
494 , GroupedBy tab col bys
395 , column ~ (col ::: ty)
495 , column ~ (col ::: ty)
396 ) => IsQualified tab col
496 ) => IsQualified tab col
397− (Aliased (Expression outer ('Grouped bys) commons schemas params from) column) where
497+ (Aliased (Expression outer commons ('Grouped bys) schemas params from) column) where
398 tab ! col = tab ! col `As` col
498 tab ! col = tab ! col `As` col
399instance
499instance
400 ( Has tab (Join outer from) row
500 ( Has tab (Join outer from) row
@@ -402,35 +502,28 @@ instance
402 , GroupedBy tab col bys
502 , GroupedBy tab col bys
403 , columns ~ '[col ::: ty]
503 , columns ~ '[col ::: ty]
404 ) => IsQualified tab col
504 ) => IsQualified tab col
405− (NP (Aliased (Expression outer ('Grouped bys) commons schemas params from)) columns) where
505+ (NP (Aliased (Expression outer commons ('Grouped bys) schemas params from)) columns) where
406 tab ! col = tab ! col :* Nil
506 tab ! col = tab ! col :* Nil
407
507
408-- | analagous to `Nothing`
508-- | analagous to `Nothing`
409--
509--
410-- >>> printSQL null_
510-- >>> printSQL null_
411-- NULL
511-- NULL
412−null_ :: Expression outer grp commons schemas params from ('Null ty)
512+null_ :: Expr ('Null ty)
413null_ = UnsafeExpression "NULL"
513null_ = UnsafeExpression "NULL"
414
514
415-- | analagous to `Just`
515-- | analagous to `Just`
416--
516--
417-- >>> printSQL $ notNull true
517-- >>> printSQL $ notNull true
418-- TRUE
518-- TRUE
419−notNull
519+notNull :: 'NotNull ty :--> 'Null ty
420− :: Expression outer grp commons schemas params from ('NotNull ty)
421− -> Expression outer grp commons schemas params from ('Null ty)
422notNull = UnsafeExpression . renderSQL
520notNull = UnsafeExpression . renderSQL
423
521
424-- | return the leftmost value which is not NULL
522-- | return the leftmost value which is not NULL
425--
523--
426-- >>> printSQL $ coalesce [null_, true] false
524-- >>> printSQL $ coalesce [null_, true] false
427-- COALESCE(NULL, TRUE, FALSE)
525-- COALESCE(NULL, TRUE, FALSE)
428−coalesce
526+coalesce :: FunctionHom ('Null ty) ('NotNull ty) ('NotNull ty)
429− :: [Expression outer grp commons schemas params from ('Null ty)]
430− -- ^ @NULL@s may be present
431− -> Expression outer grp commons schemas params from ('NotNull ty)
432− -- ^ @NULL@ is absent
433− -> Expression outer grp commons schemas params from ('NotNull ty)
434coalesce nullxs notNullx = UnsafeExpression $
527coalesce nullxs notNullx = UnsafeExpression $
435 "COALESCE" <> parenthesized (commaSeparated
528 "COALESCE" <> parenthesized (commaSeparated
436 ((renderSQL <$> nullxs) <> [renderSQL notNullx]))
529 ((renderSQL <$> nullxs) <> [renderSQL notNullx]))
@@ -440,26 +533,20 @@ coalesce nullxs notNullx = UnsafeExpression $
440-- >>> printSQL $ fromNull true null_
533-- >>> printSQL $ fromNull true null_
441-- COALESCE(NULL, TRUE)
534-- COALESCE(NULL, TRUE)
442fromNull
535fromNull
443− :: Expression outer grp commons schemas params from ('NotNull ty)
536+ :: Expression outer commons grp schemas params from ('NotNull ty)
444 -- ^ what to convert @NULL@ to
537 -- ^ what to convert @NULL@ to
445− -> Expression outer grp commons schemas params from ('Null ty)
538+ -> Expression outer commons grp schemas params from ('Null ty)
446− -> Expression outer grp commons schemas params from ('NotNull ty)
539+ -> Expression outer commons grp schemas params from ('NotNull ty)
447fromNull notNullx nullx = coalesce [nullx] notNullx
540fromNull notNullx nullx = coalesce [nullx] notNullx
448
541
449-- | >>> printSQL $ null_ & isNull
542-- | >>> printSQL $ null_ & isNull
450-- NULL IS NULL
543-- NULL IS NULL
451−isNull
544+isNull :: 'Null ty :--> null 'PGbool
452− :: Expression outer grp commons schemas params from ('Null ty)
453− -- ^ possibly @NULL@
454− -> Condition outer grp commons schemas params from
455isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
545isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
456
546
457-- | >>> printSQL $ null_ & isNotNull
547-- | >>> printSQL $ null_ & isNotNull
458-- NULL IS NOT NULL
548-- NULL IS NOT NULL
459−isNotNull
549+isNotNull :: 'Null ty :--> null 'PGbool
460− :: Expression outer grp commons schemas params from ('Null ty)
461− -- ^ possibly @NULL@
462− -> Condition outer grp commons schemas params from
463isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
550isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
464
551
465-- | analagous to `maybe` using @IS NULL@
552-- | analagous to `maybe` using @IS NULL@
@@ -467,13 +554,13 @@ isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
467-- >>> printSQL $ matchNull true not_ null_
554-- >>> printSQL $ matchNull true not_ null_
468-- CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END
555-- CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END
469matchNull
556matchNull
470− :: Expression outer grp commons schemas params from (nullty)
557+ :: Expression outer commons grp schemas params from (nullty)
471 -- ^ what to convert @NULL@ to
558 -- ^ what to convert @NULL@ to
472− -> ( Expression outer grp commons schemas params from ('NotNull ty)
559+ -> ( Expression outer commons grp schemas params from ('NotNull ty)
473− -> Expression outer grp commons schemas params from (nullty) )
560+ -> Expression outer commons grp schemas params from (nullty) )
474 -- ^ function to perform when @NULL@ is absent
561 -- ^ function to perform when @NULL@ is absent
475− -> Expression outer grp commons schemas params from ('Null ty)
562+ -> Expression outer commons grp schemas params from ('Null ty)
476− -> Expression outer grp commons schemas params from (nullty)
563+ -> Expression outer commons grp schemas params from (nullty)
477matchNull y f x = ifThenElse (isNull x) y
564matchNull y f x = ifThenElse (isNull x) y
478 (f (UnsafeExpression (renderSQL x)))
565 (f (UnsafeExpression (renderSQL x)))
479
566
@@ -481,46 +568,87 @@ matchNull y f x = ifThenElse (isNull x) y
481`nullIf` gives @NULL@.
568`nullIf` gives @NULL@.
482
569
483>>> :set -XTypeApplications -XDataKinds
570>>> :set -XTypeApplications -XDataKinds
484−>>> let expr = nullIf false (param @1) :: Expression outer grp commons schemas '[ 'NotNull 'PGbool] from ('Null 'PGbool)
571+>>> let expr = nullIf (false *: param @1) :: Expression outer commons grp schemas '[ 'NotNull 'PGbool] from ('Null 'PGbool)
485>>> printSQL expr
572>>> printSQL expr
486−NULL IF (FALSE, ($1 :: bool))
573+NULLIF(FALSE, ($1 :: bool))
487-}
574-}
488−nullIf
575+nullIf :: FunctionHet '[ 'NotNull ty, 'NotNull ty] ('Null ty)
489− :: Expression outer grp commons schemas params from ('NotNull ty)
576+nullIf = unsafeFunctionHet "NULLIF"
490− -- ^ @NULL@ is absent
491− -> Expression outer grp commons schemas params from ('NotNull ty)
492− -- ^ @NULL@ is absent
493− -> Expression outer grp commons schemas params from ('Null ty)
494−nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
495− (renderSQL x <> ", " <> renderSQL y)
496
577
497-- | >>> printSQL $ array [null_, false, true]
578-- | >>> printSQL $ array [null_, false, true]
498-- ARRAY[NULL, FALSE, TRUE]
579-- ARRAY[NULL, FALSE, TRUE]
499array
580array
500− :: [Expression outer grp commons schemas params from ty]
581+ :: [Expression outer commons grp schemas params from ty]
501 -- ^ array elements
582 -- ^ array elements
502− -> Expression outer grp commons schemas params from (nullity ('PGvararray ty))
583+ -> Expression outer commons grp schemas params from (null ('PGvararray ty))
503−array xs = UnsafeExpression $
584+array xs = UnsafeExpression $ "ARRAY" <>
504− "ARRAY[" <> commaSeparated (renderSQL <$> xs) <> "]"
585+ bracketed (commaSeparated (renderSQL <$> xs))
586+
587+{- | construct a 1-dimensional fixed length array
588+
589+>>> printSQL $ array1 (null_ :* false *: true)
590+ARRAY[NULL, FALSE, TRUE]
591+
592+>>> :type array1 (null_ :* false *: true)
593+array1 (null_ :* false *: true)
594+ :: Expression
595+ outer
596+ commons
597+ grp
598+ schemas
599+ params
600+ from
601+ (null ('PGfixarray '[3] ('Null 'PGbool)))
602+-}
603+array1
604+ :: (n ~ Length tys, SOP.All ((~) ty) tys)
605+ => NP (Expression outer commons grp schemas params from) tys
606+ -> Expression outer commons grp schemas params from (null ('PGfixarray '[n] ty))
607+array1 xs = UnsafeExpression $ "ARRAY" <>
608+ bracketed (renderCommaSeparated renderSQL xs)
609+
610+{- | construct a 2-dimensional fixed length array
611+
612+>>> printSQL $ array2 ((null_ :* false *: true) *: (false :* null_ *: true))
613+ARRAY[[NULL, FALSE, TRUE], [FALSE, NULL, TRUE]]
614+
615+>>> :type array2 ((null_ :* false *: true) *: (false :* null_ *: true))
616+array2 ((null_ :* false *: true) *: (false :* null_ *: true))
617+ :: Expression
618+ outer
619+ commons
620+ grp
621+ schemas
622+ params
623+ from
624+ (null ('PGfixarray '[2, 3] ('Null 'PGbool)))
625+-}
626+array2
627+ :: ( SOP.All ((~) tys) tyss
628+ , SOP.All SOP.SListI tyss
629+ , Length tyss ~ n1
630+ , SOP.All ((~) ty) tys
631+ , Length tys ~ n2 )
632+ => NP (NP (Expression outer commons grp schemas params from)) tyss
633+ -> Expression outer commons grp schemas params from (null ('PGfixarray '[n1,n2] ty))
634+array2 xss = UnsafeExpression $ "ARRAY" <>
635+ bracketed (renderCommaSeparatedConstraint @SOP.SListI (bracketed . renderCommaSeparated renderSQL) xss)
505
636
506-- | >>> printSQL $ cardinality (array [null_, false, true])
637-- | >>> printSQL $ cardinality (array [null_, false, true])
507-- cardinality(ARRAY[NULL, FALSE, TRUE])
638-- cardinality(ARRAY[NULL, FALSE, TRUE])
508−cardinality
639+cardinality :: null ('PGvararray ty) :--> null 'PGint8
509− :: Expression outer grp commons schemas params from (nullity ('PGvararray ty))
510− -> Expression outer grp commons schemas params from (nullity 'PGint8)
511cardinality = unsafeFunction "cardinality"
640cardinality = unsafeFunction "cardinality"
512
641
513-- | >>> printSQL $ array [null_, false, true] & index 2
642-- | >>> printSQL $ array [null_, false, true] & index 2
514-- (ARRAY[NULL, FALSE, TRUE])[2]
643-- (ARRAY[NULL, FALSE, TRUE])[2]
515index
644index
516 :: Word64 -- ^ index
645 :: Word64 -- ^ index
517− -> Expression outer grp commons schemas params from (nullity ('PGvararray ty)) -- ^ array
646+ -> null ('PGvararray ty) :--> NullifyType ty
518− -> Expression outer grp commons schemas params from (NullifyType ty)
519index n expr = UnsafeExpression $
647index n expr = UnsafeExpression $
520 parenthesized (renderSQL expr) <> "[" <> fromString (show n) <> "]"
648 parenthesized (renderSQL expr) <> "[" <> fromString (show n) <> "]"
521
649
522instance (KnownSymbol label, label `In` labels) => IsPGlabel label
650instance (KnownSymbol label, label `In` labels) => IsPGlabel label
523− (Expression outer grp commons schemas params from (nullity ('PGenum labels))) where
651+ (Expression outer commons grp schemas params from (null ('PGenum labels))) where
524 label = UnsafeExpression $ renderSQL (PGlabel @label)
652 label = UnsafeExpression $ renderSQL (PGlabel @label)
525
653
526-- | A row constructor is an expression that builds a row value
654-- | A row constructor is an expression that builds a row value
@@ -532,14 +660,14 @@ instance (KnownSymbol label, label `In` labels) => IsPGlabel label
532-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
660-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
533-- :}
661-- :}
534--
662--
535−-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression outer grp commons schemas params from ('NotNull Complex)
663+-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression outer commons grp schemas params from ('NotNull Complex)
536-- >>> printSQL i
664-- >>> printSQL i
537-- ROW(0, 1)
665-- ROW(0, 1)
538row
666row
539 :: SListI row
667 :: SListI row
540− => NP (Aliased (Expression outer grp commons schemas params from)) row
668+ => NP (Aliased (Expression outer commons grp schemas params from)) row
541 -- ^ zero or more expressions for the row field values
669 -- ^ zero or more expressions for the row field values
542− -> Expression outer grp commons schemas params from (nullity ('PGcomposite row))
670+ -> Expression outer commons grp schemas params from (null ('PGcomposite row))
543row exprs = UnsafeExpression $ "ROW" <> parenthesized
671row exprs = UnsafeExpression $ "ROW" <> parenthesized
544 (renderCommaSeparated (\ (expr `As` _) -> renderSQL expr) exprs)
672 (renderCommaSeparated (\ (expr `As` _) -> renderSQL expr) exprs)
545
673
@@ -550,7 +678,7 @@ row exprs = UnsafeExpression $ "ROW" <> parenthesized
550-- type Schema = '["complex" ::: 'Typedef Complex]
678-- type Schema = '["complex" ::: 'Typedef Complex]
551-- :}
679-- :}
552--
680--
553−-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression outer grp '[] (Public Schema) from params ('NotNull Complex)
681+-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression outer '[] grp (Public Schema) from params ('NotNull Complex)
554-- >>> printSQL $ i & field #complex #imaginary
682-- >>> printSQL $ i & field #complex #imaginary
555-- (ROW(0, 1)::"complex")."imaginary"
683-- (ROW(0, 1)::"complex")."imaginary"
556field
684field
@@ -559,87 +687,65 @@ field
559 , Has field row ty)
687 , Has field row ty)
560 => QualifiedAlias sch tydef -- ^ row type
688 => QualifiedAlias sch tydef -- ^ row type
561 -> Alias field -- ^ field name
689 -> Alias field -- ^ field name
562− -> Expression outer grp commons schemas params from ('NotNull ('PGcomposite row))
690+ -> Expression outer commons grp schemas params from ('NotNull ('PGcomposite row))
563− -> Expression outer grp commons schemas params from ty
691+ -> Expression outer commons grp schemas params from ty
564field td fld expr = UnsafeExpression $
692field td fld expr = UnsafeExpression $
565 parenthesized (renderSQL expr <> "::" <> renderSQL td)
693 parenthesized (renderSQL expr <> "::" <> renderSQL td)
566 <> "." <> renderSQL fld
694 <> "." <> renderSQL fld
567
695
568instance Semigroup
696instance Semigroup
569− (Expression outer grp commons schemas params from (nullity ('PGvararray ty))) where
697+ (Expression outer commons grp schemas params from (null ('PGvararray ty))) where
570 (<>) = unsafeBinaryOp "||"
698 (<>) = unsafeBinaryOp "||"
571
699
572instance Monoid
700instance Monoid
573− (Expression outer grp commons schemas params from (nullity ('PGvararray ty))) where
701+ (Expression outer commons grp schemas params from (null ('PGvararray ty))) where
574 mempty = array []
702 mempty = array []
575 mappend = (<>)
703 mappend = (<>)
576
704
577−-- | >>> let expr = greatest currentTimestamp [param @1] :: Expression outer grp commons schemas '[ 'NotNull 'PGtimestamptz] from ('NotNull 'PGtimestamptz)
705+-- | >>> let expr = greatest [param @1] currentTimestamp :: Expression outer commons grp schemas '[ 'NotNull 'PGtimestamptz] from ('NotNull 'PGtimestamptz)
578-- >>> printSQL expr
706-- >>> printSQL expr
579−-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
707+-- GREATEST(($1 :: timestamp with time zone), CURRENT_TIMESTAMP)
580−greatest
708+greatest :: FunctionHom ty ty ty
581− :: Expression outer grp commons schemas params from (nullty)
582− -- ^ needs at least 1 argument
583− -> [Expression outer grp commons schemas params from (nullty)]
584− -- ^ or more
585− -> Expression outer grp commons schemas params from (nullty)
586−greatest x xs = UnsafeExpression $ "GREATEST("
709+greatest = unsafeFunctionHom "GREATEST"
587− <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
710+
588−
589−-- | >>> printSQL $ least currentTimestamp [null_]
711+-- | >>> printSQL $ least [null_] currentTimestamp
590−-- LEAST(CURRENT_TIMESTAMP, NULL)
712+-- LEAST(NULL, CURRENT_TIMESTAMP)
591−least
713+least :: FunctionHom ty ty ty
592− :: Expression outer grp commons schemas params from (nullty)
714+least = unsafeFunctionHom "LEAST"
593− -- ^ needs at least 1 argument
594− -> [Expression outer grp commons schemas params from (nullty)]
595− -- ^ or more
596− -> Expression outer grp commons schemas params from (nullty)
597−least x xs = UnsafeExpression $ "LEAST("
598− <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
599
715
600-- | >>> printSQL $ unsafeBinaryOp "OR" true false
716-- | >>> printSQL $ unsafeBinaryOp "OR" true false
601-- (TRUE OR FALSE)
717-- (TRUE OR FALSE)
602−unsafeBinaryOp
718+unsafeBinaryOp :: ByteString {- ^ operator -} -> Operator ty0 ty1 ty2
603− :: ByteString
604− -- ^ operator
605− -> Expression outer grp commons schemas params from (ty0)
606− -> Expression outer grp commons schemas params from (ty1)
607− -> Expression outer grp commons schemas params from (ty2)
608unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
719unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
609 renderSQL x <+> op <+> renderSQL y
720 renderSQL x <+> op <+> renderSQL y
610
721
611−-- | >>> printSQL $ unsafeUnaryOp "NOT" true
722+-- | >>> printSQL $ unsafeUnaryOpL "NOT" true
612-- (NOT TRUE)
723-- (NOT TRUE)
613−unsafeUnaryOp
614− :: ByteString
724+unsafeUnaryOpL :: ByteString {- ^ operator -} -> x :--> y
615− -- ^ operator
725+unsafeUnaryOpL op x = UnsafeExpression $ parenthesized $ op <+> renderSQL x
616− -> Expression outer grp commons schemas params from (ty0)
726+
617− -> Expression outer grp commons schemas params from (ty1)
727+-- | >>> printSQL $ true & unsafeUnaryOpR "IS NOT TRUE"
728+-- (TRUE IS NOT TRUE)
729+unsafeUnaryOpR :: ByteString {- ^ operator -} -> x :--> y
618−unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
730+unsafeUnaryOpR op x = UnsafeExpression $ parenthesized $ renderSQL x <+> op
619− op <+> renderSQL x
620
731
621-- | >>> printSQL $ unsafeFunction "f" true
732-- | >>> printSQL $ unsafeFunction "f" true
622-- f(TRUE)
733-- f(TRUE)
623−unsafeFunction
734+unsafeFunction :: ByteString {- ^ function -} -> x :--> y
624− :: ByteString
625− -- ^ function
626− -> Expression outer grp commons schemas params from (xty)
627− -> Expression outer grp commons schemas params from (yty)
628unsafeFunction fun x = UnsafeExpression $
735unsafeFunction fun x = UnsafeExpression $
629 fun <> parenthesized (renderSQL x)
736 fun <> parenthesized (renderSQL x)
630
737
631−-- | Helper for defining variadic functions.
738+-- | >>> printSQL $ unsafeFunctionHet "f" (currentTime :* localTimestamp :* false *: litChar 'a')
632−unsafeVariadicFunction
739+-- f(CURRENT_TIME, LOCALTIMESTAMP, FALSE, E'a')
740+unsafeFunctionHet
633− :: SListI elems
741+ :: SListI xs
634− => ByteString
742+ => ByteString {- ^ function -}
635− -- ^ function
743+ -> FunctionHet xs y
636− -> NP (Expression outer grp commons schemas params from) elems
637− -> Expression outer grp commons schemas params from ret
638−unsafeVariadicFunction fun x = UnsafeExpression $
744+unsafeFunctionHet fun xs = UnsafeExpression $
639− fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderSQL) x)))
745+ fun <> parenthesized (renderCommaSeparated renderSQL xs)
640
746
641instance ty `In` PGNum
747instance ty `In` PGNum
642− => Num (Expression outer grp commons schemas params from (nullity ty)) where
748+ => Num (Expression outer commons grp schemas params from (null ty)) where
643 (+) = unsafeBinaryOp "+"
749 (+) = unsafeBinaryOp "+"
644 (-) = unsafeBinaryOp "-"
750 (-) = unsafeBinaryOp "-"
645 (*) = unsafeBinaryOp "*"
751 (*) = unsafeBinaryOp "*"
@@ -651,7 +757,7 @@ instance ty `In` PGNum
651 . show
757 . show
652
758
653instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
759instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
654− (Expression outer grp commons schemas params from (nullity ty)) where
760+ (Expression outer commons grp schemas params from (null ty)) where
655 (/) = unsafeBinaryOp "/"
761 (/) = unsafeBinaryOp "/"
656 fromRational
762 fromRational
657 = UnsafeExpression
763 = UnsafeExpression
@@ -661,7 +767,7 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
661 . fromRat @Double
767 . fromRat @Double
662
768
663instance (ty `In` PGNum, ty `In` PGFloating) => Floating
769instance (ty `In` PGNum, ty `In` PGFloating) => Floating
664− (Expression outer grp commons schemas params from (nullity ty)) where
770+ (Expression outer commons grp schemas params from (null ty)) where
665 pi = UnsafeExpression "pi()"
771 pi = UnsafeExpression "pi()"
666 exp = unsafeFunction "exp"
772 exp = unsafeFunction "exp"
667 log = unsafeFunction "ln"
773 log = unsafeFunction "ln"
@@ -684,20 +790,15 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Floating
684
790
685-- | >>> :{
791-- | >>> :{
686-- let
792-- let
687−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
793+-- expression :: Expr (null 'PGfloat4)
688−-- expression = atan2_ pi 2
794+-- expression = atan2_ (pi *: 2)
689-- in printSQL expression
795-- in printSQL expression
690-- :}
796-- :}
691-- atan2(pi(), 2)
797-- atan2(pi(), 2)
692atan2_
798atan2_
693 :: float `In` PGFloating
799 :: float `In` PGFloating
694− => Expression outer grp commons schemas params from (nullity float)
800+ => FunctionHet '[ null float, null float] (null float)
695− -- ^ numerator
801+atan2_ = unsafeFunctionHet "atan2"
696− -> Expression outer grp commons schemas params from (nullity float)
697− -- ^ denominator
698− -> Expression outer grp commons schemas params from (nullity float)
699−atan2_ y x = UnsafeExpression $
700− "atan2(" <> renderSQL y <> ", " <> renderSQL x <> ")"
701
802
702-- When a `cast` is applied to an `Expression` of a known type, it
803-- When a `cast` is applied to an `Expression` of a known type, it
703-- represents a run-time type conversion. The cast will succeed only if a
804-- represents a run-time type conversion. The cast will succeed only if a
@@ -708,9 +809,9 @@ atan2_ y x = UnsafeExpression $
708cast
809cast
709 :: TypeExpression schemas ty1
810 :: TypeExpression schemas ty1
710 -- ^ type to cast as
811 -- ^ type to cast as
711− -> Expression outer grp commons schemas params from ty0
812+ -> Expression outer commons grp schemas params from ty0
712 -- ^ value to convert
813 -- ^ value to convert
713− -> Expression outer grp commons schemas params from ty1
814+ -> Expression outer commons grp schemas params from ty1
714cast ty x = UnsafeExpression $ parenthesized $
815cast ty x = UnsafeExpression $ parenthesized $
715 renderSQL x <+> "::" <+> renderSQL ty
816 renderSQL x <+> "::" <+> renderSQL ty
716
817
@@ -718,136 +819,108 @@ cast ty x = UnsafeExpression $ parenthesized $
718--
819--
719-- >>> :{
820-- >>> :{
720-- let
821-- let
721−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
822+-- expression :: Expression outer commons grp schemas params from (null 'PGint2)
722-- expression = 5 `quot_` 2
823-- expression = 5 `quot_` 2
723-- in printSQL expression
824-- in printSQL expression
724-- :}
825-- :}
725-- (5 / 2)
826-- (5 / 2)
726quot_
827quot_
727 :: int `In` PGIntegral
828 :: int `In` PGIntegral
728− => Expression outer grp commons schemas params from (nullity int)
829+ => Operator (null int) (null int) (null int)
729− -- ^ numerator
730− -> Expression outer grp commons schemas params from (nullity int)
731− -- ^ denominator
732− -> Expression outer grp commons schemas params from (nullity int)
733quot_ = unsafeBinaryOp "/"
830quot_ = unsafeBinaryOp "/"
734
831
735-- | remainder upon integer division
832-- | remainder upon integer division
736--
833--
737-- >>> :{
834-- >>> :{
738-- let
835-- let
739−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
836+-- expression :: Expression outer commons grp schemas params from (null 'PGint2)
740-- expression = 5 `rem_` 2
837-- expression = 5 `rem_` 2
741-- in printSQL expression
838-- in printSQL expression
742-- :}
839-- :}
743-- (5 % 2)
840-- (5 % 2)
744rem_
841rem_
745 :: int `In` PGIntegral
842 :: int `In` PGIntegral
746− => Expression outer grp commons schemas params from (nullity int)
843+ => Operator (null int) (null int) (null int)
747− -- ^ numerator
748− -> Expression outer grp commons schemas params from (nullity int)
749− -- ^ denominator
750− -> Expression outer grp commons schemas params from (nullity int)
751rem_ = unsafeBinaryOp "%"
844rem_ = unsafeBinaryOp "%"
752
845
753-- | >>> :{
846-- | >>> :{
754-- let
847-- let
755−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
848+-- expression :: Expression outer commons grp schemas params from (null 'PGfloat4)
756-- expression = trunc pi
849-- expression = trunc pi
757-- in printSQL expression
850-- in printSQL expression
758-- :}
851-- :}
759-- trunc(pi())
852-- trunc(pi())
760−trunc
761− :: frac `In` PGFloating
853+trunc :: frac `In` PGFloating => null frac :--> null frac
762− => Expression outer grp commons schemas params from (nullity frac)
763− -- ^ fractional number
764− -> Expression outer grp commons schemas params from (nullity frac)
765trunc = unsafeFunction "trunc"
854trunc = unsafeFunction "trunc"
766
855
767-- | >>> :{
856-- | >>> :{
768-- let
857-- let
769−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
858+-- expression :: Expression outer commons grp schemas params from (null 'PGfloat4)
770-- expression = round_ pi
859-- expression = round_ pi
771-- in printSQL expression
860-- in printSQL expression
772-- :}
861-- :}
773-- round(pi())
862-- round(pi())
774−round_
775− :: frac `In` PGFloating
863+round_ :: frac `In` PGFloating => null frac :--> null frac
776− => Expression outer grp commons schemas params from (nullity frac)
777− -- ^ fractional number
778− -> Expression outer grp commons schemas params from (nullity frac)
779round_ = unsafeFunction "round"
864round_ = unsafeFunction "round"
780
865
781-- | >>> :{
866-- | >>> :{
782-- let
867-- let
783−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
868+-- expression :: Expression outer commons grp schemas params from (null 'PGfloat4)
784-- expression = ceiling_ pi
869-- expression = ceiling_ pi
785-- in printSQL expression
870-- in printSQL expression
786-- :}
871-- :}
787-- ceiling(pi())
872-- ceiling(pi())
788−ceiling_
789− :: frac `In` PGFloating
873+ceiling_ :: frac `In` PGFloating => null frac :--> null frac
790− => Expression outer grp commons schemas params from (nullity frac)
791− -- ^ fractional number
792− -> Expression outer grp commons schemas params from (nullity frac)
793ceiling_ = unsafeFunction "ceiling"
874ceiling_ = unsafeFunction "ceiling"
794
875
795-- | A `Condition` is an `Expression`, which can evaluate
876-- | A `Condition` is an `Expression`, which can evaluate
796-- to `true`, `false` or `null_`. This is because SQL uses
877-- to `true`, `false` or `null_`. This is because SQL uses
797-- a three valued logic.
878-- a three valued logic.
798−type Condition outer grp commons schemas params from =
879+type Condition outer commons grp schemas params from =
799− Expression outer grp commons schemas params from ('Null 'PGbool)
880+ Expression outer commons grp schemas params from ('Null 'PGbool)
800
881
801-- | >>> printSQL true
882-- | >>> printSQL true
802-- TRUE
883-- TRUE
803−true :: Expression outer grp commons schemas params from (nullity 'PGbool)
884+true :: Expr (null 'PGbool)
804true = UnsafeExpression "TRUE"
885true = UnsafeExpression "TRUE"
805
886
806-- | >>> printSQL false
887-- | >>> printSQL false
807-- FALSE
888-- FALSE
808−false :: Expression outer grp commons schemas params from (nullity 'PGbool)
889+false :: Expr (null 'PGbool)
809false = UnsafeExpression "FALSE"
890false = UnsafeExpression "FALSE"
810
891
811-- | >>> printSQL $ not_ true
892-- | >>> printSQL $ not_ true
812-- (NOT TRUE)
893-- (NOT TRUE)
813−not_
894+not_ :: null 'PGbool :--> null 'PGbool
814− :: Expression outer grp commons schemas params from (nullity 'PGbool)
895+not_ = unsafeUnaryOpL "NOT"
815− -> Expression outer grp commons schemas params from (nullity 'PGbool)
816−not_ = unsafeUnaryOp "NOT"
817
896
818-- | >>> printSQL $ true .&& false
897-- | >>> printSQL $ true .&& false
819-- (TRUE AND FALSE)
898-- (TRUE AND FALSE)
820−(.&&)
899+(.&&) :: Operator (null 'PGbool) (null 'PGbool) (null 'PGbool)
821− :: Expression outer grp commons schemas params from (nullity 'PGbool)
822− -> Expression outer grp commons schemas params from (nullity 'PGbool)
823− -> Expression outer grp commons schemas params from (nullity 'PGbool)
824infixr 3 .&&
900infixr 3 .&&
825(.&&) = unsafeBinaryOp "AND"
901(.&&) = unsafeBinaryOp "AND"
826
902
827-- | >>> printSQL $ true .|| false
903-- | >>> printSQL $ true .|| false
828-- (TRUE OR FALSE)
904-- (TRUE OR FALSE)
829−(.||)
905+(.||) :: Operator (null 'PGbool) (null 'PGbool) (null 'PGbool)
830− :: Expression outer grp commons schemas params from (nullity 'PGbool)
831− -> Expression outer grp commons schemas params from (nullity 'PGbool)
832− -> Expression outer grp commons schemas params from (nullity 'PGbool)
833infixr 2 .||
906infixr 2 .||
834(.||) = unsafeBinaryOp "OR"
907(.||) = unsafeBinaryOp "OR"
835
908
836-- | >>> :{
909-- | >>> :{
837-- let
910-- let
838−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
911+-- expression :: Expression outer commons grp schemas params from (null 'PGint2)
839-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
912-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
840-- in printSQL expression
913-- in printSQL expression
841-- :}
914-- :}
842-- CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END
915-- CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END
843caseWhenThenElse
916caseWhenThenElse
844− :: [ ( Condition outer grp commons schemas params from
917+ :: [ ( Condition outer commons grp schemas params from
845− , Expression outer grp commons schemas params from ty
918+ , Expression outer commons grp schemas params from ty
846 ) ]
919 ) ]
847 -- ^ whens and thens
920 -- ^ whens and thens
848− -> Expression outer grp commons schemas params from ty
921+ -> Expression outer commons grp schemas params from ty
849 -- ^ else
922 -- ^ else
850− -> Expression outer grp commons schemas params from ty
923+ -> Expression outer commons grp schemas params from ty
851caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
924caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
852 [ "CASE"
925 [ "CASE"
853 , mconcat
926 , mconcat
@@ -863,16 +936,16 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
863
936
864-- | >>> :{
937-- | >>> :{
865-- let
938-- let
866−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
939+-- expression :: Expression outer commons grp schemas params from (null 'PGint2)
867-- expression = ifThenElse true 1 0
940-- expression = ifThenElse true 1 0
868-- in printSQL expression
941-- in printSQL expression
869-- :}
942-- :}
870-- CASE WHEN TRUE THEN 1 ELSE 0 END
943-- CASE WHEN TRUE THEN 1 ELSE 0 END
871ifThenElse
944ifThenElse
872− :: Condition outer grp commons schemas params from
945+ :: Condition outer commons grp schemas params from
873− -> Expression outer grp commons schemas params from ty -- ^ then
946+ -> Expression outer commons grp schemas params from ty -- ^ then
874− -> Expression outer grp commons schemas params from ty -- ^ else
947+ -> Expression outer commons grp schemas params from ty -- ^ else
875− -> Expression outer grp commons schemas params from ty
948+ -> Expression outer commons grp schemas params from ty
876ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
949ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
877
950
878-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
951-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
@@ -880,261 +953,242 @@ ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
880--
953--
881-- >>> printSQL $ true .== null_
954-- >>> printSQL $ true .== null_
882-- (TRUE = NULL)
955-- (TRUE = NULL)
883−(.==)
956+(.==) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
884− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
885− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
886− -> Condition outer grp commons schemas params from
887(.==) = unsafeBinaryOp "="
957(.==) = unsafeBinaryOp "="
888infix 4 .==
958infix 4 .==
889
959
890-- | >>> printSQL $ true ./= null_
960-- | >>> printSQL $ true ./= null_
891-- (TRUE <> NULL)
961-- (TRUE <> NULL)
892−(./=)
962+(./=) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
893− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
894− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
895− -> Condition outer grp commons schemas params from
896(./=) = unsafeBinaryOp "<>"
963(./=) = unsafeBinaryOp "<>"
897infix 4 ./=
964infix 4 ./=
898
965
899-- | >>> printSQL $ true .>= null_
966-- | >>> printSQL $ true .>= null_
900-- (TRUE >= NULL)
967-- (TRUE >= NULL)
901−(.>=)
968+(.>=) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
902− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
903− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
904− -> Condition outer grp commons schemas params from
905(.>=) = unsafeBinaryOp ">="
969(.>=) = unsafeBinaryOp ">="
906infix 4 .>=
970infix 4 .>=
907
971
908-- | >>> printSQL $ true .< null_
972-- | >>> printSQL $ true .< null_
909-- (TRUE < NULL)
973-- (TRUE < NULL)
910−(.<)
974+(.<) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
911− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
912− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
913− -> Condition outer grp commons schemas params from
914(.<) = unsafeBinaryOp "<"
975(.<) = unsafeBinaryOp "<"
915infix 4 .<
976infix 4 .<
916
977
917-- | >>> printSQL $ true .<= null_
978-- | >>> printSQL $ true .<= null_
918-- (TRUE <= NULL)
979-- (TRUE <= NULL)
919−(.<=)
980+(.<=) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
920− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
921− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
922− -> Condition outer grp commons schemas params from
923(.<=) = unsafeBinaryOp "<="
981(.<=) = unsafeBinaryOp "<="
924infix 4 .<=
982infix 4 .<=
925
983
926-- | >>> printSQL $ true .> null_
984-- | >>> printSQL $ true .> null_
927-- (TRUE > NULL)
985-- (TRUE > NULL)
928−(.>)
986+(.>) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
929− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
930− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
931− -> Condition outer grp commons schemas params from
932(.>) = unsafeBinaryOp ">"
987(.>) = unsafeBinaryOp ">"
933infix 4 .>
988infix 4 .>
934
989
935−{- | between
936−>>> printSQL $ true `between` (null_, false)
990+{- | >>> printSQL $ true `between` (null_, false)
937TRUE BETWEEN NULL AND FALSE
991TRUE BETWEEN NULL AND FALSE
938-}
992-}
939between
993between
940− :: Expression outer grp commons schemas params from (nullity ty)
994+ :: Expression outer commons grp schemas params from (null ty)
941− -> ( Expression outer grp commons schemas params from (nullity ty)
995+ -> ( Expression outer commons grp schemas params from (null ty)
942− , Expression outer grp commons schemas params from (nullity ty) ) -- ^ bounds
996+ , Expression outer commons grp schemas params from (null ty) ) -- ^ bounds
943− -> Condition outer grp commons schemas params from
997+ -> Condition outer commons grp schemas params from
944between a (x,y) = UnsafeExpression $ renderSQL a <+> "BETWEEN"
998between a (x,y) = UnsafeExpression $ renderSQL a <+> "BETWEEN"
945 <+> renderSQL x <+> "AND" <+> renderSQL y
999 <+> renderSQL x <+> "AND" <+> renderSQL y
946
1000
947−{- | not between
948−>>> printSQL $ true `notBetween` (null_, false)
1001+{- | >>> printSQL $ true `notBetween` (null_, false)
949TRUE NOT BETWEEN NULL AND FALSE
1002TRUE NOT BETWEEN NULL AND FALSE
950-}
1003-}
951notBetween
1004notBetween
952− :: Expression outer grp commons schemas params from (nullity ty)
1005+ :: Expression outer commons grp schemas params from (null ty)
953− -> ( Expression outer grp commons schemas params from (nullity ty)
1006+ -> ( Expression outer commons grp schemas params from (null ty)
954− , Expression outer grp commons schemas params from (nullity ty) ) -- ^ bounds
1007+ , Expression outer commons grp schemas params from (null ty) ) -- ^ bounds
955− -> Condition outer grp commons schemas params from
1008+ -> Condition outer commons grp schemas params from
956notBetween a (x,y) = UnsafeExpression $ renderSQL a <+> "NOT BETWEEN"
1009notBetween a (x,y) = UnsafeExpression $ renderSQL a <+> "NOT BETWEEN"
957 <+> renderSQL x <+> "AND" <+> renderSQL y
1010 <+> renderSQL x <+> "AND" <+> renderSQL y
958
1011
959{- | between, after sorting the comparison values
1012{- | between, after sorting the comparison values
1013+
960>>> printSQL $ true `betweenSymmetric` (null_, false)
1014>>> printSQL $ true `betweenSymmetric` (null_, false)
961TRUE BETWEEN SYMMETRIC NULL AND FALSE
1015TRUE BETWEEN SYMMETRIC NULL AND FALSE
962-}
1016-}
963betweenSymmetric
1017betweenSymmetric
964− :: Expression outer grp commons schemas params from (nullity ty)
1018+ :: Expression outer commons grp schemas params from (null ty)
965− -> ( Expression outer grp commons schemas params from (nullity ty)
1019+ -> ( Expression outer commons grp schemas params from (null ty)
966− , Expression outer grp commons schemas params from (nullity ty) ) -- ^ bounds
1020+ , Expression outer commons grp schemas params from (null ty) ) -- ^ bounds
967− -> Condition outer grp commons schemas params from
1021+ -> Condition outer commons grp schemas params from
968betweenSymmetric a (x,y) = UnsafeExpression $ renderSQL a
1022betweenSymmetric a (x,y) = UnsafeExpression $ renderSQL a
969 <+> "BETWEEN SYMMETRIC" <+> renderSQL x <+> "AND" <+> renderSQL y
1023 <+> "BETWEEN SYMMETRIC" <+> renderSQL x <+> "AND" <+> renderSQL y
970
1024
971{- | not between, after sorting the comparison values
1025{- | not between, after sorting the comparison values
1026+
972>>> printSQL $ true `notBetweenSymmetric` (null_, false)
1027>>> printSQL $ true `notBetweenSymmetric` (null_, false)
973TRUE NOT BETWEEN SYMMETRIC NULL AND FALSE
1028TRUE NOT BETWEEN SYMMETRIC NULL AND FALSE
974-}
1029-}
975notBetweenSymmetric
1030notBetweenSymmetric
976− :: Expression outer grp commons schemas params from (nullity ty)
1031+ :: Expression outer commons grp schemas params from (null ty)
977− -> ( Expression outer grp commons schemas params from (nullity ty)
1032+ -> ( Expression outer commons grp schemas params from (null ty)
978− , Expression outer grp commons schemas params from (nullity ty) ) -- ^ bounds
1033+ , Expression outer commons grp schemas params from (null ty) ) -- ^ bounds
979− -> Condition outer grp commons schemas params from
1034+ -> Condition outer commons grp schemas params from
980notBetweenSymmetric a (x,y) = UnsafeExpression $ renderSQL a
1035notBetweenSymmetric a (x,y) = UnsafeExpression $ renderSQL a
981 <+> "NOT BETWEEN SYMMETRIC" <+> renderSQL x <+> "AND" <+> renderSQL y
1036 <+> "NOT BETWEEN SYMMETRIC" <+> renderSQL x <+> "AND" <+> renderSQL y
982
1037
983{- | not equal, treating null like an ordinary value
1038{- | not equal, treating null like an ordinary value
1039+
984>>> printSQL $ true `isDistinctFrom` null_
1040>>> printSQL $ true `isDistinctFrom` null_
985(TRUE IS DISTINCT FROM NULL)
1041(TRUE IS DISTINCT FROM NULL)
986-}
1042-}
987−isDistinctFrom
1043+isDistinctFrom :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
988− :: Expression outer grp commons schemas params from (nullity ty)
989− -> Expression outer grp commons schemas params from (nullity ty)
990− -> Condition outer grp commons schemas params from
991isDistinctFrom = unsafeBinaryOp "IS DISTINCT FROM"
1044isDistinctFrom = unsafeBinaryOp "IS DISTINCT FROM"
992
1045
993{- | equal, treating null like an ordinary value
1046{- | equal, treating null like an ordinary value
1047+
994>>> printSQL $ true `isNotDistinctFrom` null_
1048>>> printSQL $ true `isNotDistinctFrom` null_
995(TRUE IS NOT DISTINCT FROM NULL)
1049(TRUE IS NOT DISTINCT FROM NULL)
996-}
1050-}
997−isNotDistinctFrom
1051+isNotDistinctFrom :: Operator (null0 ty) (null1 ty) ('NotNull 'PGbool)
998− :: Expression outer grp commons schemas params from (nullity ty)
999− -> Expression outer grp commons schemas params from (nullity ty)
1000− -> Condition outer grp commons schemas params from
1001isNotDistinctFrom = unsafeBinaryOp "IS NOT DISTINCT FROM"
1052isNotDistinctFrom = unsafeBinaryOp "IS NOT DISTINCT FROM"
1002
1053
1003{- | is true
1054{- | is true
1055+
1004>>> printSQL $ true & isTrue
1056>>> printSQL $ true & isTrue
1005−TRUE IS TRUE
1057+(TRUE IS TRUE)
1006-}
1058-}
1007−isTrue
1059+isTrue :: null0 'PGbool :--> null1 'PGbool
1008− :: Expression outer grp commons schemas params from (nullity 'PGbool)
1060+isTrue = unsafeUnaryOpR "IS TRUE"
1009− -> Condition outer grp commons schemas params from
1010−isTrue b = UnsafeExpression $ renderSQL b <+> "IS TRUE"
1011
1061
1012{- | is false or unknown
1062{- | is false or unknown
1063+
1013>>> printSQL $ true & isNotTrue
1064>>> printSQL $ true & isNotTrue
1014−TRUE IS NOT TRUE
1065+(TRUE IS NOT TRUE)
1015-}
1066-}
1016−isNotTrue
1067+isNotTrue :: null0 'PGbool :--> null1 'PGbool
1017− :: Expression outer grp commons schemas params from (nullity 'PGbool)
1018− -> Condition outer grp commons schemas params from
1019−isNotTrue b = UnsafeExpression $ renderSQL b <+> "IS NOT TRUE"
1068+isNotTrue = unsafeUnaryOpR "IS NOT TRUE"
1020
1069
1021{- | is false
1070{- | is false
1071+
1022>>> printSQL $ true & isFalse
1072>>> printSQL $ true & isFalse
1023−TRUE IS FALSE
1073+(TRUE IS FALSE)
1024-}
1074-}
1025−isFalse
1075+isFalse :: null0 'PGbool :--> null1 'PGbool
1026− :: Expression outer grp commons schemas params from (nullity 'PGbool)
1027− -> Condition outer grp commons schemas params from
1028−isFalse b = UnsafeExpression $ renderSQL b <+> "IS FALSE"
1076+isFalse = unsafeUnaryOpR "IS FALSE"
1029
1077
1030{- | is true or unknown
1078{- | is true or unknown
1079+
1031>>> printSQL $ true & isNotFalse
1080>>> printSQL $ true & isNotFalse
1032−TRUE IS NOT FALSE
1081+(TRUE IS NOT FALSE)
1033-}
1082-}
1034−isNotFalse
1083+isNotFalse :: null0 'PGbool :--> null1 'PGbool
1035− :: Expression outer grp commons schemas params from (nullity 'PGbool)
1036− -> Condition outer grp commons schemas params from
1037−isNotFalse b = UnsafeExpression $ renderSQL b <+> "IS NOT FALSE"
1084+isNotFalse = unsafeUnaryOpR "IS NOT FALSE"
1038
1085
1039{- | is unknown
1086{- | is unknown
1087+
1040>>> printSQL $ true & isUnknown
1088>>> printSQL $ true & isUnknown
1041−TRUE IS UNKNOWN
1089+(TRUE IS UNKNOWN)
1042-}
1090-}
1043−isUnknown
1091+isUnknown :: null0 'PGbool :--> null1 'PGbool
1044− :: Expression outer grp commons schemas params from (nullity 'PGbool)
1045− -> Condition outer grp commons schemas params from
1046−isUnknown b = UnsafeExpression $ renderSQL b <+> "IS UNKNOWN"
1092+isUnknown = unsafeUnaryOpR "IS UNKNOWN"
1047
1093
1048{- | is true or false
1094{- | is true or false
1095+
1049>>> printSQL $ true & isNotUnknown
1096>>> printSQL $ true & isNotUnknown
1050−TRUE IS NOT UNKNOWN
1097+(TRUE IS NOT UNKNOWN)
1051-}
1098-}
1052−isNotUnknown
1099+isNotUnknown :: null0 'PGbool :--> null1 'PGbool
1053− :: Expression outer grp commons schemas params from (nullity 'PGbool)
1054− -> Condition outer grp commons schemas params from
1055−isNotUnknown b = UnsafeExpression $ renderSQL b <+> "IS NOT UNKNOWN"
1100+isNotUnknown = unsafeUnaryOpR "IS NOT UNKNOWN"
1056
1101
1057-- | >>> printSQL currentDate
1102-- | >>> printSQL currentDate
1058-- CURRENT_DATE
1103-- CURRENT_DATE
1059−currentDate
1104+currentDate :: Expr (null 'PGdate)
1060− :: Expression outer grp commons schemas params from (nullity 'PGdate)
1061currentDate = UnsafeExpression "CURRENT_DATE"
1105currentDate = UnsafeExpression "CURRENT_DATE"
1062
1106
1063-- | >>> printSQL currentTime
1107-- | >>> printSQL currentTime
1064-- CURRENT_TIME
1108-- CURRENT_TIME
1065−currentTime
1109+currentTime :: Expr (null 'PGtimetz)
1066− :: Expression outer grp commons schemas params from (nullity 'PGtimetz)
1067currentTime = UnsafeExpression "CURRENT_TIME"
1110currentTime = UnsafeExpression "CURRENT_TIME"
1068
1111
1069-- | >>> printSQL currentTimestamp
1112-- | >>> printSQL currentTimestamp
1070-- CURRENT_TIMESTAMP
1113-- CURRENT_TIMESTAMP
1071−currentTimestamp
1114+currentTimestamp :: Expr (null 'PGtimestamptz)
1072− :: Expression outer grp commons schemas params from (nullity 'PGtimestamptz)
1073currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
1115currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
1074
1116
1075-- | >>> printSQL localTime
1117-- | >>> printSQL localTime
1076-- LOCALTIME
1118-- LOCALTIME
1077−localTime
1119+localTime :: Expr (null 'PGtime)
1078− :: Expression outer grp commons schemas params from (nullity 'PGtime)
1079localTime = UnsafeExpression "LOCALTIME"
1120localTime = UnsafeExpression "LOCALTIME"
1080
1121
1081-- | >>> printSQL localTimestamp
1122-- | >>> printSQL localTimestamp
1082-- LOCALTIMESTAMP
1123-- LOCALTIMESTAMP
1083−localTimestamp
1124+localTimestamp :: Expr (null 'PGtimestamp)
1084− :: Expression outer grp commons schemas params from (nullity 'PGtimestamp)
1085localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
1125localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
1086
1126
1127+-- | Current date and time (equivalent to `currentTimestamp`)
1128+--
1129+-- >>> printSQL now
1130+-- now()
1131+now :: Expr (null 'PGtimestamptz)
1132+now = UnsafeExpression "now()"
1133+
1087{-----------------------------------------
1134{-----------------------------------------
1088text
1135text
1089-----------------------------------------}
1136-----------------------------------------}
1090
1137
1138+escape :: Char -> String
1139+escape = \case
1140+ '\NUL' -> "\\0"
1141+ '\'' -> "''"
1142+ '"' -> "\\\""
1143+ '\b' -> "\\b"
1144+ '\n' -> "\\n"
1145+ '\r' -> "\\r"
1146+ '\t' -> "\\t"
1147+ '\\' -> "\\\\"
1148+ c -> [c]
1149+
1091instance IsString
1150instance IsString
1092− (Expression outer grp commons schemas params from (nullity 'PGtext)) where
1151+ (Expression outer commons grp schemas params from (null 'PGtext)) where
1093 fromString str = UnsafeExpression $
1152 fromString str = UnsafeExpression $
1094 "E\'" <> fromString (escape =<< str) <> "\'"
1153 "E\'" <> fromString (escape =<< str) <> "\'"
1095− where
1154+instance IsString
1096− escape = \case
1155+ (Expression outer commons grp schemas params from (null 'PGtsvector)) where
1097− '\NUL' -> "\\0"
1156+ fromString str = cast tsvector . UnsafeExpression $
1098− '\'' -> "''"
1157+ "E\'" <> fromString (escape =<< str) <> "\'"
1099− '"' -> "\\\""
1158+instance IsString
1100− '\b' -> "\\b"
1159+ (Expression outer commons grp schemas params from (null 'PGtsquery)) where
1101− '\n' -> "\\n"
1160+ fromString str = cast tsquery . UnsafeExpression $
1102− '\r' -> "\\r"
1161+ "E\'" <> fromString (escape =<< str) <> "\'"
1103− '\t' -> "\\t"
1104− '\\' -> "\\\\"
1105− c -> [c]
1106
1162
1107instance Semigroup
1163instance Semigroup
1108− (Expression outer grp commons schemas params from (nullity 'PGtext)) where
1164+ (Expression outer commons grp schemas params from (null 'PGtext)) where
1165+ (<>) = unsafeBinaryOp "||"
1166+instance Semigroup
1167+ (Expression outer commons grp schemas params from (null 'PGtsvector)) where
1109 (<>) = unsafeBinaryOp "||"
1168 (<>) = unsafeBinaryOp "||"
1110
1169
1111instance Monoid
1170instance Monoid
1112− (Expression outer grp commons schemas params from (nullity 'PGtext)) where
1171+ (Expression outer commons grp schemas params from (null 'PGtext)) where
1172+ mempty = fromString ""
1173+ mappend = (<>)
1174+instance Monoid
1175+ (Expression outer commons grp schemas params from (null 'PGtsvector)) where
1113 mempty = fromString ""
1176 mempty = fromString ""
1114 mappend = (<>)
1177 mappend = (<>)
1115
1178
1116-- | >>> printSQL $ lower "ARRRGGG"
1179-- | >>> printSQL $ lower "ARRRGGG"
1117-- lower(E'ARRRGGG')
1180-- lower(E'ARRRGGG')
1118−lower
1181+lower :: null 'PGtext :--> null 'PGtext
1119− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1120− -- ^ string to lower case
1121− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1122lower = unsafeFunction "lower"
1182lower = unsafeFunction "lower"
1123
1183
1124-- | >>> printSQL $ upper "eeee"
1184-- | >>> printSQL $ upper "eeee"
1125-- upper(E'eeee')
1185-- upper(E'eeee')
1126−upper
1186+upper :: null 'PGtext :--> null 'PGtext
1127− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1128− -- ^ string to upper case
1129− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1130upper = unsafeFunction "upper"
1187upper = unsafeFunction "upper"
1131
1188
1132-- | >>> printSQL $ charLength "four"
1189-- | >>> printSQL $ charLength "four"
1133-- char_length(E'four')
1190-- char_length(E'four')
1134−charLength
1191+charLength :: null 'PGtext :--> null 'PGint4
1135− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1136− -- ^ string to measure
1137− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1138charLength = unsafeFunction "char_length"
1192charLength = unsafeFunction "char_length"
1139
1193
1140-- | The `like` expression returns true if the @string@ matches
1194-- | The `like` expression returns true if the @string@ matches
@@ -1146,12 +1200,7 @@ charLength = unsafeFunction "char_length"
1146--
1200--
1147-- >>> printSQL $ "abc" `like` "a%"
1201-- >>> printSQL $ "abc" `like` "a%"
1148-- (E'abc' LIKE E'a%')
1202-- (E'abc' LIKE E'a%')
1149−like
1203+like :: Operator (null 'PGtext) (null 'PGtext) ('Null 'PGbool)
1150− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1151− -- ^ string
1152− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1153− -- ^ pattern
1154− -> Expression outer grp commons schemas params from (nullity 'PGbool)
1155like = unsafeBinaryOp "LIKE"
1204like = unsafeBinaryOp "LIKE"
1156
1205
1157-- | The key word ILIKE can be used instead of LIKE to make the
1206-- | The key word ILIKE can be used instead of LIKE to make the
@@ -1159,12 +1208,7 @@ like = unsafeBinaryOp "LIKE"
1159--
1208--
1160-- >>> printSQL $ "abc" `ilike` "a%"
1209-- >>> printSQL $ "abc" `ilike` "a%"
1161-- (E'abc' ILIKE E'a%')
1210-- (E'abc' ILIKE E'a%')
1162−ilike
1211+ilike :: Operator (null 'PGtext) (null 'PGtext) ('Null 'PGbool)
1163− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1164− -- ^ string
1165− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1166− -- ^ pattern
1167− -> Expression outer grp commons schemas params from (nullity 'PGbool)
1168ilike = unsafeBinaryOp "ILIKE"
1212ilike = unsafeBinaryOp "ILIKE"
1169
1213
1170{-----------------------------------------
1214{-----------------------------------------
@@ -1179,86 +1223,66 @@ Table 9.44: json and jsonb operators
1179-- | Get JSON value (object field or array element) at a key.
1223-- | Get JSON value (object field or array element) at a key.
1180(.->)
1224(.->)
1181 :: (json `In` PGJsonType, key `In` PGJsonKey)
1225 :: (json `In` PGJsonType, key `In` PGJsonKey)
1182− => Expression outer grp commons schemas params from (nullity json)
1226+ => Operator (null json) (null key) ('Null json)
1183− -> Expression outer grp commons schemas params from (nullity key)
1184− -> Expression outer grp commons schemas params from ('Null json)
1185infixl 8 .->
1227infixl 8 .->
1186(.->) = unsafeBinaryOp "->"
1228(.->) = unsafeBinaryOp "->"
1187
1229
1188-- | Get JSON value (object field or array element) at a key, as text.
1230-- | Get JSON value (object field or array element) at a key, as text.
1189(.->>)
1231(.->>)
1190 :: (json `In` PGJsonType, key `In` PGJsonKey)
1232 :: (json `In` PGJsonType, key `In` PGJsonKey)
1191− => Expression outer grp commons schemas params from (nullity json)
1233+ => Operator (null json) (null key) ('Null 'PGtext)
1192− -> Expression outer grp commons schemas params from (nullity key)
1193− -> Expression outer grp commons schemas params from ('Null 'PGtext)
1194infixl 8 .->>
1234infixl 8 .->>
1195(.->>) = unsafeBinaryOp "->>"
1235(.->>) = unsafeBinaryOp "->>"
1196
1236
1197-- | Get JSON value at a specified path.
1237-- | Get JSON value at a specified path.
1198(.#>)
1238(.#>)
1199− :: (json `In` PGJsonType, PGTextArray "(.#>)" path)
1239+ :: json `In` PGJsonType
1200− => Expression outer grp commons schemas params from (nullity json)
1240+ => Operator (null json) (null ('PGvararray ('NotNull 'PGtext))) ('Null json)
1201− -> Expression outer grp commons schemas params from (nullity path)
1202− -> Expression outer grp commons schemas params from ('Null json)
1203infixl 8 .#>
1241infixl 8 .#>
1204(.#>) = unsafeBinaryOp "#>"
1242(.#>) = unsafeBinaryOp "#>"
1205
1243
1206-- | Get JSON value at a specified path as text.
1244-- | Get JSON value at a specified path as text.
1207(.#>>)
1245(.#>>)
1208− :: (json `In` PGJsonType, PGTextArray "(.#>>)" path)
1246+ :: json `In` PGJsonType
1209− => Expression outer grp commons schemas params from (nullity json)
1247+ => Operator (null json) (null ('PGvararray ('NotNull 'PGtext))) ('Null 'PGtext)
1210− -> Expression outer grp commons schemas params from (nullity path)
1211− -> Expression outer grp commons schemas params from ('Null 'PGtext)
1212infixl 8 .#>>
1248infixl 8 .#>>
1213(.#>>) = unsafeBinaryOp "#>>"
1249(.#>>) = unsafeBinaryOp "#>>"
1214
1250
1215-- Additional jsonb operators
1251-- Additional jsonb operators
1216
1252
1217−-- | Does the left JSON value contain the right JSON path/value entries at the
1253+class PGSubset container where
1218−-- top level?
1254+ (@>) :: Operator (null0 container) (null1 container) ('Null 'PGbool)
1219−(.@>)
1220− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1221− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1222− -> Condition outer grp commons schemas params from
1223−infixl 9 .@>
1224−(.@>) = unsafeBinaryOp "@>"
1255+ (@>) = unsafeBinaryOp "@>"
1225−
1256+ (<@) :: Operator (null0 container) (null1 container) ('Null 'PGbool)
1226−-- | Are the left JSON path/value entries contained at the top level within the
1257+ (<@) = unsafeBinaryOp "<@"
1227−-- right JSON value?
1258+instance PGSubset 'PGjsonb
1228−(.<@)
1259+instance PGSubset 'PGtsquery
1229− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1260+instance PGSubset ('PGvararray ty)
1230− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1231− -> Condition outer grp commons schemas params from
1232−infixl 9 .<@
1233−(.<@) = unsafeBinaryOp "<@"
1234
1261
1235-- | Does the string exist as a top-level key within the JSON value?
1262-- | Does the string exist as a top-level key within the JSON value?
1236−(.?)
1263+(.?) :: Operator (null 'PGjsonb) (null 'PGtext) ('Null 'PGbool)
1237− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1238− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1239− -> Condition outer grp commons schemas params from
1240infixl 9 .?
1264infixl 9 .?
1241(.?) = unsafeBinaryOp "?"
1265(.?) = unsafeBinaryOp "?"
1242
1266
1243-- | Do any of these array strings exist as top-level keys?
1267-- | Do any of these array strings exist as top-level keys?
1244−(.?|)
1268+(.?|) :: Operator
1245− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1269+ (null 'PGjsonb)
1246− -> Expression outer grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1270+ (null ('PGvararray ('NotNull 'PGtext)))
1247− -> Condition outer grp commons schemas params from
1271+ ('Null 'PGbool)
1248infixl 9 .?|
1272infixl 9 .?|
1249(.?|) = unsafeBinaryOp "?|"
1273(.?|) = unsafeBinaryOp "?|"
1250
1274
1251-- | Do all of these array strings exist as top-level keys?
1275-- | Do all of these array strings exist as top-level keys?
1252−(.?&)
1276+(.?&) :: Operator
1253− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1277+ (null 'PGjsonb)
1254− -> Expression outer grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1278+ (null ('PGvararray ('NotNull 'PGtext)))
1255− -> Condition outer grp commons schemas params from
1279+ ('Null 'PGbool)
1256infixl 9 .?&
1280infixl 9 .?&
1257(.?&) = unsafeBinaryOp "?&"
1281(.?&) = unsafeBinaryOp "?&"
1258
1282
1259-- | Concatenate two jsonb values into a new jsonb value.
1283-- | Concatenate two jsonb values into a new jsonb value.
1260instance Semigroup
1284instance Semigroup
1261− (Expression outer grp commons schemas params from (nullity 'PGjsonb)) where
1285+ (Expression outer commons grp schemas params from (null 'PGjsonb)) where
1262 (<>) = unsafeBinaryOp "||"
1286 (<>) = unsafeBinaryOp "||"
1263
1287
1264-- | Delete a key or keys from a JSON object, or remove an array element.
1288-- | Delete a key or keys from a JSON object, or remove an array element.
@@ -1275,19 +1299,13 @@ instance Semigroup
1275-- count from the end). Throws an error if top level container is not an array.
1299-- count from the end). Throws an error if top level container is not an array.
1276(.-.)
1300(.-.)
1277 :: (key `In` '[ 'PGtext, 'PGvararray ('NotNull 'PGtext), 'PGint4, 'PGint2 ]) -- hlint error without parens here
1301 :: (key `In` '[ 'PGtext, 'PGvararray ('NotNull 'PGtext), 'PGint4, 'PGint2 ]) -- hlint error without parens here
1278− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1302+ => Operator (null 'PGjsonb) (null key) (null 'PGjsonb)
1279− -> Expression outer grp commons schemas params from (nullity key)
1280− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1281infixl 6 .-.
1303infixl 6 .-.
1282(.-.) = unsafeBinaryOp "-"
1304(.-.) = unsafeBinaryOp "-"
1283
1305
1284-- | Delete the field or element with specified path (for JSON arrays, negative
1306-- | Delete the field or element with specified path (for JSON arrays, negative
1285-- integers count from the end)
1307-- integers count from the end)
1286−(#-.)
1308+(#-.) :: Operator (null 'PGjsonb) (null ('PGvararray ('NotNull 'PGtext))) (null 'PGjsonb)
1287− :: PGTextArray "(#-.)" arrayty
1288− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1289− -> Expression outer grp commons schemas params from (nullity arrayty)
1290− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1291infixl 6 #-.
1309infixl 6 #-.
1292(#-.) = unsafeBinaryOp "#-"
1310(#-.) = unsafeBinaryOp "#-"
1293
1311
@@ -1296,16 +1314,12 @@ Table 9.45: JSON creation functions
1296-----------------------------------------}
1314-----------------------------------------}
1297
1315
1298-- | Literal binary JSON
1316-- | Literal binary JSON
1299−jsonbLit
1300− :: JSON.ToJSON x
1317+jsonbLit :: JSON.ToJSON x => x -> Expr (null 'PGjsonb)
1301− => x -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1302jsonbLit = cast jsonb . UnsafeExpression
1318jsonbLit = cast jsonb . UnsafeExpression
1303 . singleQuotedUtf8 . toStrict . JSON.encode
1319 . singleQuotedUtf8 . toStrict . JSON.encode
1304
1320
1305-- | Literal JSON
1321-- | Literal JSON
1306−jsonLit
1307− :: JSON.ToJSON x
1322+jsonLit :: JSON.ToJSON x => x -> Expr (null 'PGjson)
1308− => x -> Expression outer grp commons schemas params from (nullity 'PGjson)
1309jsonLit = cast json . UnsafeExpression
1323jsonLit = cast json . UnsafeExpression
1310 . singleQuotedUtf8 . toStrict . JSON.encode
1324 . singleQuotedUtf8 . toStrict . JSON.encode
1311
1325
@@ -1315,9 +1329,7 @@ jsonLit = cast json . UnsafeExpression
1315-- otherwise, a scalar value is produced. For any scalar type other than a
1329-- otherwise, a scalar value is produced. For any scalar type other than a
1316-- number, a Boolean, or a null value, the text representation will be used, in
1330-- number, a Boolean, or a null value, the text representation will be used, in
1317-- such a fashion that it is a valid json value.
1331-- such a fashion that it is a valid json value.
1318−toJson
1332+toJson :: null ty :--> null 'PGjson
1319− :: Expression outer grp commons schemas params from (nullity ty)
1320− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1321toJson = unsafeFunction "to_json"
1333toJson = unsafeFunction "to_json"
1322
1334
1323-- | Returns the value as jsonb. Arrays and composites are converted
1335-- | Returns the value as jsonb. Arrays and composites are converted
@@ -1326,204 +1338,154 @@ toJson = unsafeFunction "to_json"
1326-- otherwise, a scalar value is produced. For any scalar type other than a
1338-- otherwise, a scalar value is produced. For any scalar type other than a
1327-- number, a Boolean, or a null value, the text representation will be used, in
1339-- number, a Boolean, or a null value, the text representation will be used, in
1328-- such a fashion that it is a valid jsonb value.
1340-- such a fashion that it is a valid jsonb value.
1329−toJsonb
1341+toJsonb :: null ty :--> null 'PGjsonb
1330− :: Expression outer grp commons schemas params from (nullity ty)
1331− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1332toJsonb = unsafeFunction "to_jsonb"
1342toJsonb = unsafeFunction "to_jsonb"
1333
1343
1334-- | Returns the array as a JSON array. A PostgreSQL multidimensional array
1344-- | Returns the array as a JSON array. A PostgreSQL multidimensional array
1335-- becomes a JSON array of arrays.
1345-- becomes a JSON array of arrays.
1336−arrayToJson
1346+arrayToJson :: null ('PGvararray ty) :--> null 'PGjson
1337− :: PGArray "arrayToJson" arr
1338− => Expression outer grp commons schemas params from (nullity arr)
1339− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1340arrayToJson = unsafeFunction "array_to_json"
1347arrayToJson = unsafeFunction "array_to_json"
1341
1348
1342-- | Returns the row as a JSON object.
1349-- | Returns the row as a JSON object.
1343−rowToJson
1350+rowToJson :: null ('PGcomposite ty) :--> null 'PGjson
1344− :: Expression outer grp commons schemas params from (nullity ('PGcomposite ty))
1345− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1346rowToJson = unsafeFunction "row_to_json"
1351rowToJson = unsafeFunction "row_to_json"
1347
1352
1348-- | Builds a possibly-heterogeneously-typed JSON array out of a variadic
1353-- | Builds a possibly-heterogeneously-typed JSON array out of a variadic
1349-- argument list.
1354-- argument list.
1350−jsonBuildArray
1355+jsonBuildArray :: SListI tuple => FunctionHet tuple (null 'PGjson)
1351− :: SListI elems
1352− => NP (Expression outer grp commons schemas params from) elems
1353− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1354−jsonBuildArray = unsafeVariadicFunction "json_build_array"
1356+jsonBuildArray = unsafeFunctionHet "json_build_array"
1355
1357
1356-- | Builds a possibly-heterogeneously-typed (binary) JSON array out of a
1358-- | Builds a possibly-heterogeneously-typed (binary) JSON array out of a
1357-- variadic argument list.
1359-- variadic argument list.
1358−jsonbBuildArray
1360+jsonbBuildArray :: SListI tuple => FunctionHet tuple (null 'PGjsonb)
1359− :: SListI elems
1360− => NP (Expression outer grp commons schemas params from) elems
1361− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1362−jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
1361+jsonbBuildArray = unsafeFunctionHet "jsonb_build_array"
1363−
1362+
1364−unsafeRowFunction
1363+class PGBuildObject tys where
1365− :: SOP.SListI elems
1364+instance PGBuildObject '[]
1366− => NP (Aliased (Expression outer grp commons schemas params from)) elems
1365+instance (PGBuildObject tys, key `In` PGJsonKey)
1367− -> [ByteString]
1366+ => PGBuildObject ('NotNull key ': value ': tys)
1368−unsafeRowFunction =
1369− (`appEndo` []) . hcfoldMap (Proxy :: SOP.Proxy SOP.Top)
1370− (\(col `As` name) -> Endo $ \xs ->
1371− renderAliasString name : renderSQL col : xs)
1372− where
1373− renderAliasString :: KnownSymbol alias => Alias alias -> ByteString
1374− renderAliasString = singleQuotedText . fromString . symbolVal
1375
1367
1376-- | Builds a possibly-heterogeneously-typed JSON object out of a variadic
1368-- | Builds a possibly-heterogeneously-typed JSON object out of a variadic
1377-- argument list. The elements of the argument list must alternate between text
1369-- argument list. The elements of the argument list must alternate between text
1378-- and values.
1370-- and values.
1379jsonBuildObject
1371jsonBuildObject
1380− :: SOP.SListI elems
1372+ :: (SListI elems, PGBuildObject elems)
1381− => NP (Aliased (Expression outer grp commons schemas params from)) elems
1373+ => FunctionHet elems (null 'PGjson)
1382− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1383−jsonBuildObject
1374+jsonBuildObject = unsafeFunctionHet "json_build_object"
1384− = unsafeFunction "json_build_object"
1385− . UnsafeExpression
1386− . commaSeparated
1387− . unsafeRowFunction
1388
1375
1389-- | Builds a possibly-heterogeneously-typed (binary) JSON object out of a
1376-- | Builds a possibly-heterogeneously-typed (binary) JSON object out of a
1390-- variadic argument list. The elements of the argument list must alternate
1377-- variadic argument list. The elements of the argument list must alternate
1391−-- between text and values.
1378+-- between keys and values.
1392−jsonbBuildObject
1393− :: SOP.SListI elems
1394− => NP (Aliased (Expression outer grp commons schemas params from)) elems
1395− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1396jsonbBuildObject
1379jsonbBuildObject
1380+ :: (SListI elems, PGBuildObject elems)
1381+ => FunctionHet elems (null 'PGjsonb)
1397− = unsafeFunction "jsonb_build_object"
1382+jsonbBuildObject = unsafeFunctionHet "jsonb_build_object"
1398− . UnsafeExpression
1383+
1399− . commaSeparated
1400− . unsafeRowFunction
1401−
1402−-- | Builds a JSON object out of a text array. The array must have either
1384+-- | Builds a JSON object out of a text array.
1403−-- exactly one dimension with an even number of members, in which case they are
1385+-- The array must have two dimensions
1404−-- taken as alternating key/value pairs, or two dimensions such that each inner
1386+-- such that each inner array has exactly two elements,
1405−-- array has exactly two elements, which are taken as a key/value pair.
1387+-- which are taken as a key/value pair.
1406jsonObject
1388jsonObject
1407− :: PGArrayOf "jsonObject" arr ('NotNull 'PGtext)
1389+ :: null ('PGfixarray '[n,2] ('NotNull 'PGtext))
1408− => Expression outer grp commons schemas params from (nullity arr)
1390+ :--> null 'PGjson
1409− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1410jsonObject = unsafeFunction "json_object"
1391jsonObject = unsafeFunction "json_object"
1411
1392
1412−-- | Builds a binary JSON object out of a text array. The array must have either
1393+-- | Builds a binary JSON object out of a text array.
1413−-- exactly one dimension with an even number of members, in which case they are
1394+-- The array must have two dimensions
1414−-- taken as alternating key/value pairs, or two dimensions such that each inner
1395+-- such that each inner array has exactly two elements,
1415−-- array has exactly two elements, which are taken as a key/value pair.
1396+-- which are taken as a key/value pair.
1416jsonbObject
1397jsonbObject
1417− :: PGArrayOf "jsonbObject" arr ('NotNull 'PGtext)
1398+ :: null ('PGfixarray '[n,2] ('NotNull 'PGtext))
1418− => Expression outer grp commons schemas params from (nullity arr)
1399+ :--> null 'PGjsonb
1419− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1420jsonbObject = unsafeFunction "jsonb_object"
1400jsonbObject = unsafeFunction "jsonb_object"
1421
1401
1422-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
1402-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
1423-- keys and one for values, that are zipped pairwise to create a JSON object.
1403-- keys and one for values, that are zipped pairwise to create a JSON object.
1424−jsonZipObject
1404+jsonZipObject :: FunctionHet
1425− :: ( PGArrayOf "jsonZipObject" keysArray ('NotNull 'PGtext)
1405+ '[ null ('PGvararray ('NotNull 'PGtext))
1426− , PGArrayOf "jsonZipObject" valuesArray ('NotNull 'PGtext))
1406+ , null ('PGvararray ('NotNull 'PGtext)) ]
1427− => Expression outer grp commons schemas params from (nullity keysArray)
1407+ ( null 'PGjson )
1428− -> Expression outer grp commons schemas params from (nullity valuesArray)
1429− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1430−jsonZipObject ks vs =
1408+jsonZipObject = unsafeFunctionHet "json_object"
1431− unsafeVariadicFunction "json_object" (ks :* vs :* Nil)
1432
1409
1433−-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
1410+-- | This is an alternate form of 'jsonbObject' that takes two arrays; one for
1434-- keys and one for values, that are zipped pairwise to create a binary JSON
1411-- keys and one for values, that are zipped pairwise to create a binary JSON
1435-- object.
1412-- object.
1436−jsonbZipObject
1413+jsonbZipObject :: FunctionHet
1437− :: ( PGArrayOf "jsonbZipObject" keysArray ('NotNull 'PGtext)
1414+ '[ null ('PGvararray ('NotNull 'PGtext))
1438− , PGArrayOf "jsonbZipObject" valuesArray ('NotNull 'PGtext))
1415+ , null ('PGvararray ('NotNull 'PGtext)) ]
1439− => Expression outer grp commons schemas params from (nullity keysArray)
1416+ ( null 'PGjsonb )
1440− -> Expression outer grp commons schemas params from (nullity valuesArray)
1441− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1442−jsonbZipObject ks vs =
1417+jsonbZipObject = unsafeFunctionHet "jsonb_object"
1443− unsafeVariadicFunction "jsonb_object" (ks :* vs :* Nil)
1444
1418
1445{-----------------------------------------
1419{-----------------------------------------
1446Table 9.46: JSON processing functions
1420Table 9.46: JSON processing functions
1447-----------------------------------------}
1421-----------------------------------------}
1448
1422
1449-- | Returns the number of elements in the outermost JSON array.
1423-- | Returns the number of elements in the outermost JSON array.
1450−jsonArrayLength
1424+jsonArrayLength :: null 'PGjson :--> null 'PGint4
1451− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1452− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1453jsonArrayLength = unsafeFunction "json_array_length"
1425jsonArrayLength = unsafeFunction "json_array_length"
1454
1426
1455-- | Returns the number of elements in the outermost binary JSON array.
1427-- | Returns the number of elements in the outermost binary JSON array.
1456−jsonbArrayLength
1428+jsonbArrayLength :: null 'PGjsonb :--> null 'PGint4
1457− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1458− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1459jsonbArrayLength = unsafeFunction "jsonb_array_length"
1429jsonbArrayLength = unsafeFunction "jsonb_array_length"
1460
1430
1461-- | Returns JSON value pointed to by the given path (equivalent to #>
1431-- | Returns JSON value pointed to by the given path (equivalent to #>
1462-- operator).
1432-- operator).
1463jsonExtractPath
1433jsonExtractPath
1464 :: SListI elems
1434 :: SListI elems
1465− => Expression outer grp commons schemas params from (nullity 'PGjson)
1435+ => Expression outer commons grp schemas params from (null 'PGjson)
1466− -> NP (Expression outer grp commons schemas params from) elems
1436+ -> NP (Expression outer commons grp schemas params from) elems
1467− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1437+ -> Expression outer commons grp schemas params from (null 'PGjsonb)
1468jsonExtractPath x xs =
1438jsonExtractPath x xs =
1469− unsafeVariadicFunction "json_extract_path" (x :* xs)
1439+ unsafeFunctionHet "json_extract_path" (x :* xs)
1470
1440
1471-- | Returns JSON value pointed to by the given path (equivalent to #>
1441-- | Returns JSON value pointed to by the given path (equivalent to #>
1472-- operator).
1442-- operator).
1473jsonbExtractPath
1443jsonbExtractPath
1474 :: SListI elems
1444 :: SListI elems
1475− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1445+ => Expression outer commons grp schemas params from (null 'PGjsonb)
1476− -> NP (Expression outer grp commons schemas params from) elems
1446+ -> NP (Expression outer commons grp schemas params from) elems
1477− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1447+ -> Expression outer commons grp schemas params from (null 'PGjsonb)
1478jsonbExtractPath x xs =
1448jsonbExtractPath x xs =
1479− unsafeVariadicFunction "jsonb_extract_path" (x :* xs)
1449+ unsafeFunctionHet "jsonb_extract_path" (x :* xs)
1480
1450
1481-- | Returns JSON value pointed to by the given path (equivalent to #>
1451-- | Returns JSON value pointed to by the given path (equivalent to #>
1482-- operator), as text.
1452-- operator), as text.
1483jsonExtractPathAsText
1453jsonExtractPathAsText
1484 :: SListI elems
1454 :: SListI elems
1485− => Expression outer grp commons schemas params from (nullity 'PGjson)
1455+ => Expression outer commons grp schemas params from (null 'PGjson)
1486− -> NP (Expression outer grp commons schemas params from) elems
1456+ -> NP (Expression outer commons grp schemas params from) elems
1487− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1457+ -> Expression outer commons grp schemas params from (null 'PGjson)
1488jsonExtractPathAsText x xs =
1458jsonExtractPathAsText x xs =
1489− unsafeVariadicFunction "json_extract_path_text" (x :* xs)
1459+ unsafeFunctionHet "json_extract_path_text" (x :* xs)
1490
1460
1491-- | Returns JSON value pointed to by the given path (equivalent to #>
1461-- | Returns JSON value pointed to by the given path (equivalent to #>
1492-- operator), as text.
1462-- operator), as text.
1493jsonbExtractPathAsText
1463jsonbExtractPathAsText
1494 :: SListI elems
1464 :: SListI elems
1495− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1465+ => Expression outer commons grp schemas params from (null 'PGjsonb)
1496− -> NP (Expression outer grp commons schemas params from) elems
1466+ -> NP (Expression outer commons grp schemas params from) elems
1497− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1467+ -> Expression outer commons grp schemas params from (null 'PGjsonb)
1498jsonbExtractPathAsText x xs =
1468jsonbExtractPathAsText x xs =
1499− unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
1469+ unsafeFunctionHet "jsonb_extract_path_text" (x :* xs)
1500
1470
1501-- | Returns the type of the outermost JSON value as a text string. Possible
1471-- | Returns the type of the outermost JSON value as a text string. Possible
1502-- types are object, array, string, number, boolean, and null.
1472-- types are object, array, string, number, boolean, and null.
1503−jsonTypeof
1473+jsonTypeof :: null 'PGjson :--> null 'PGtext
1504− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1505− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1506jsonTypeof = unsafeFunction "json_typeof"
1474jsonTypeof = unsafeFunction "json_typeof"
1507
1475
1508-- | Returns the type of the outermost binary JSON value as a text string.
1476-- | Returns the type of the outermost binary JSON value as a text string.
1509-- Possible types are object, array, string, number, boolean, and null.
1477-- Possible types are object, array, string, number, boolean, and null.
1510−jsonbTypeof
1478+jsonbTypeof :: null 'PGjsonb :--> null 'PGtext
1511− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1512− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1513jsonbTypeof = unsafeFunction "jsonb_typeof"
1479jsonbTypeof = unsafeFunction "jsonb_typeof"
1514
1480
1515-- | Returns its argument with all object fields that have null values omitted.
1481-- | Returns its argument with all object fields that have null values omitted.
1516-- Other null values are untouched.
1482-- Other null values are untouched.
1517−jsonStripNulls
1483+jsonStripNulls :: null 'PGjson :--> null 'PGjson
1518− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1519− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1520jsonStripNulls = unsafeFunction "json_strip_nulls"
1484jsonStripNulls = unsafeFunction "json_strip_nulls"
1521
1485
1522-- | Returns its argument with all object fields that have null values omitted.
1486-- | Returns its argument with all object fields that have null values omitted.
1523-- Other null values are untouched.
1487-- Other null values are untouched.
1524−jsonbStripNulls
1488+jsonbStripNulls :: null 'PGjsonb :--> null 'PGjsonb
1525− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1526− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1527jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
1489jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
1528
1490
1529-- | @ jsonbSet target path new_value create_missing @
1491-- | @ jsonbSet target path new_value create_missing @
@@ -1534,15 +1496,14 @@ jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
1534-- operators, negative integers that appear in path count from the end of JSON
1496-- operators, negative integers that appear in path count from the end of JSON
1535-- arrays.
1497-- arrays.
1536jsonbSet
1498jsonbSet
1537− :: PGTextArray "jsonbSet" arr
1538− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1499+ :: Expression outer commons grp schemas params from (null 'PGjsonb)
1539− -> Expression outer grp commons schemas params from (nullity arr)
1500+ -> Expression outer commons grp schemas params from (null ('PGvararray ('NotNull 'PGtext)))
1540− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1501+ -> Expression outer commons grp schemas params from (null 'PGjsonb)
1541− -> Maybe (Expression outer grp commons schemas params from (nullity 'PGbool))
1502+ -> Maybe (Expression outer commons grp schemas params from (null 'PGbool))
1542− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1503+ -> Expression outer commons grp schemas params from (null 'PGjsonb)
1543jsonbSet tgt path val createMissing = case createMissing of
1504jsonbSet tgt path val createMissing = case createMissing of
1544− Just m -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* m :* Nil)
1505+ Just m -> unsafeFunctionHet "jsonb_set" (tgt :* path :* val :* m :* Nil)
1545− Nothing -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* Nil)
1506+ Nothing -> unsafeFunctionHet "jsonb_set" (tgt :* path :* val :* Nil)
1546
1507
1547-- | @ jsonbInsert target path new_value insert_after @
1508-- | @ jsonbInsert target path new_value insert_after @
1548--
1509--
@@ -1553,39 +1514,120 @@ jsonbSet tgt path val createMissing = case createMissing of
1553-- exist. As with the path orientated operators, negative integers that appear
1514-- exist. As with the path orientated operators, negative integers that appear
1554-- in path count from the end of JSON arrays.
1515-- in path count from the end of JSON arrays.
1555jsonbInsert
1516jsonbInsert
1556− :: PGTextArray "jsonbInsert" arr
1557− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1517+ :: Expression outer commons grp schemas params from (null 'PGjsonb)
1558− -> Expression outer grp commons schemas params from (nullity arr)
1518+ -> Expression outer commons grp schemas params from (null ('PGvararray ('NotNull 'PGtext)))
1559− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1519+ -> Expression outer commons grp schemas params from (null 'PGjsonb)
1560− -> Maybe (Expression outer grp commons schemas params from (nullity 'PGbool))
1520+ -> Maybe (Expression outer commons grp schemas params from (null 'PGbool))
1561− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1521+ -> Expression outer commons grp schemas params from (null 'PGjsonb)
1562jsonbInsert tgt path val insertAfter = case insertAfter of
1522jsonbInsert tgt path val insertAfter = case insertAfter of
1563− Just i -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* i :* Nil)
1523+ Just i -> unsafeFunctionHet "jsonb_insert" (tgt :* path :* val :* i :* Nil)
1564− Nothing -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* Nil)
1524+ Nothing -> unsafeFunctionHet "jsonb_insert" (tgt :* path :* val :* Nil)
1565
1525
1566-- | Returns its argument as indented JSON text.
1526-- | Returns its argument as indented JSON text.
1567−jsonbPretty
1527+jsonbPretty :: null 'PGjsonb :--> null 'PGtext
1568− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1569− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1570jsonbPretty = unsafeFunction "jsonb_pretty"
1528jsonbPretty = unsafeFunction "jsonb_pretty"
1571
1529
1530+(@@) :: Operator (null 'PGtsvector) (null 'PGtsquery) ('Null 'PGbool)
1531+(@@) = unsafeBinaryOp "@@"
1532+
1533+(.&) :: Operator (null 'PGtsquery) (null 'PGtsquery) (null 'PGtsquery)
1534+(.&) = unsafeBinaryOp "&&"
1535+
1536+(.|) :: Operator (null 'PGtsquery) (null 'PGtsquery) (null 'PGtsquery)
1537+(.|) = unsafeBinaryOp "||"
1538+
1539+(.!) :: null 'PGtsquery :--> null 'PGtsquery
1540+(.!) = unsafeUnaryOpL "!!"
1541+
1542+(<->) :: Operator (null 'PGtsquery) (null 'PGtsquery) (null 'PGtsquery)
1543+(<->) = unsafeBinaryOp "<->"
1544+
1545+arrayToTSvector
1546+ :: null ('PGvararray ('NotNull 'PGtext))
1547+ :--> null 'PGtsvector
1548+arrayToTSvector = unsafeFunction "array_to_tsvector"
1549+
1550+tsvectorLength :: null 'PGtsvector :--> null 'PGint4
1551+tsvectorLength = unsafeFunction "length"
1552+
1553+numnode :: null 'PGtsquery :--> null 'PGint4
1554+numnode = unsafeFunction "numnode"
1555+
1556+plainToTSquery :: null 'PGtext :--> null 'PGtsquery
1557+plainToTSquery = unsafeFunction "plainto_tsquery"
1558+
1559+phraseToTSquery :: null 'PGtext :--> null 'PGtsquery
1560+phraseToTSquery = unsafeFunction "phraseto_tsquery"
1561+
1562+websearchToTSquery :: null 'PGtext :--> null 'PGtsquery
1563+websearchToTSquery = unsafeFunction "websearch_to_tsquery"
1564+
1565+queryTree :: null 'PGtsquery :--> null 'PGtext
1566+queryTree = unsafeFunction "query_tree"
1567+
1568+toTSquery :: null 'PGtext :--> null 'PGtsquery
1569+toTSquery = unsafeFunction "to_tsquery"
1570+
1571+toTSvector
1572+ :: ty `In` '[ 'PGtext, 'PGjson, 'PGjsonb]
1573+ => null ty :--> null 'PGtsquery
1574+toTSvector = unsafeFunction "to_tsvector"
1575+
1576+setWeight
1577+ :: FunctionHet '[null 'PGtsvector, null ('PGchar 1)] (null 'PGtsvector)
1578+setWeight = unsafeFunctionHet "set_weight"
1579+
1580+litChar :: Char -> Expr (null ('PGchar 1))
1581+litChar chr = UnsafeExpression $
1582+ "E\'" <> fromString (escape =<< [chr]) <> "\'"
1583+
1584+strip :: null 'PGtsvector :--> null 'PGtsvector
1585+strip = unsafeFunction "strip"
1586+
1587+jsonToTSvector :: FunctionHet '[null 'PGjson, null 'PGjson] (null 'PGtsvector)
1588+jsonToTSvector = unsafeFunctionHet "json_to_tsvector"
1589+
1590+jsonbToTSvector :: FunctionHet '[null 'PGjsonb, null 'PGjsonb] (null 'PGtsvector)
1591+jsonbToTSvector = unsafeFunctionHet "jsonb_to_tsvector"
1592+
1593+tsDelete :: FunctionHet
1594+ '[null 'PGtsvector, null ('PGvararray ('NotNull 'PGtext))]
1595+ (null 'PGtsvector)
1596+tsDelete = unsafeFunctionHet "ts_delete"
1597+
1598+tsFilter :: FunctionHet
1599+ '[null 'PGtsvector, null ('PGvararray ('NotNull ('PGchar 1)))]
1600+ (null 'PGtsvector)
1601+tsFilter = unsafeFunctionHet "ts_filter"
1602+
1603+tsHeadline
1604+ :: document `In` '[ 'PGtext, 'PGjson, 'PGjsonb]
1605+ => FunctionHet '[null document, null 'PGtsquery] (null 'PGtext)
1606+tsHeadline = unsafeFunctionHet "ts_headline"
1572{-----------------------------------------
1607{-----------------------------------------
1573aggregation
1608aggregation
1574-----------------------------------------}
1609-----------------------------------------}
1575
1610
1611+unsafeAggregateHet
1612+ :: SOP.SListI xs
1613+ => ByteString -- ^ function
1614+ -> Distinction (NP (Expression outer commons 'Ungrouped schemas params from)) xs
1615+ -> Expression outer commons ('Grouped bys) schemas params from y
1616+unsafeAggregateHet fun xs = UnsafeExpression $ fun <> parenthesized (renderSQL xs)
1617+
1576{- |
1618{- |
1577`Aggregate` functions compute a single result from a set of input values.
1619`Aggregate` functions compute a single result from a set of input values.
1578`Aggregate` functions can be used as `GroupedBy` `Expression`s as well
1620`Aggregate` functions can be used as `GroupedBy` `Expression`s as well
1579as `WindowFunction`s.
1621as `WindowFunction`s.
1580-}
1622-}
1581−class Aggregate expr1 expr2 aggr
1623+class Aggregate expr1 exprHet aggr
1582− | aggr -> expr1, aggr -> expr2 where
1624+ | aggr -> expr1, aggr -> exprHet where
1583
1625
1584 -- | A special aggregation that does not require an input
1626 -- | A special aggregation that does not require an input
1585 --
1627 --
1586 -- >>> :{
1628 -- >>> :{
1587 -- let
1629 -- let
1588− -- expression :: Expression '[] ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
1630+ -- expression :: Expression '[] commons ('Grouped bys) schemas params from ('NotNull 'PGint8)
1589 -- expression = countStar
1631 -- expression = countStar
1590 -- in printSQL expression
1632 -- in printSQL expression
1591 -- :}
1633 -- :}
@@ -1594,7 +1636,7 @@ class Aggregate expr1 expr2 aggr
1594
1636
1595 -- | >>> :{
1637 -- | >>> :{
1596 -- let
1638 -- let
1597− -- expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1639+ -- expression :: Expression '[] commons ('Grouped bys) schemas params '[tab ::: '["col" ::: null ty]] ('NotNull 'PGint8)
1598 -- expression = count (All #col)
1640 -- expression = count (All #col)
1599 -- in printSQL expression
1641 -- in printSQL expression
1600 -- :}
1642 -- :}
@@ -1606,14 +1648,14 @@ class Aggregate expr1 expr2 aggr
1606
1648
1607 -- | >>> :{
1649 -- | >>> :{
1608 -- let
1650 -- let
1609− -- expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('NotNull 'PGnumeric)
1651+ -- expression :: Expression '[] commons ('Grouped bys) schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('NotNull 'PGnumeric)
1610 -- expression = sum_ (Distinct #col)
1652 -- expression = sum_ (Distinct #col)
1611 -- in printSQL expression
1653 -- in printSQL expression
1612 -- :}
1654 -- :}
1613 -- sum(DISTINCT "col")
1655 -- sum(DISTINCT "col")
1614 sum_
1656 sum_
1615 :: ty `In` PGNum
1657 :: ty `In` PGNum
1616− => expr1 (nullity ty)
1658+ => expr1 (null ty)
1617 -> aggr ('NotNull ty)
1659 -> aggr ('NotNull ty)
1618
1660
1619 -- | input values, including nulls, concatenated into an array
1661 -- | input values, including nulls, concatenated into an array
@@ -1635,7 +1677,7 @@ class Aggregate expr1 expr2 aggr
1635 the bitwise AND of all non-null input values, or null if none
1677 the bitwise AND of all non-null input values, or null if none
1636 >>> :{
1678 >>> :{
1637 let
1679 let
1638− expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] ('Null 'PGint4)
1680+ expression :: Expression '[] commons ('Grouped bys) schemas params '[tab ::: '["col" ::: null 'PGint4]] ('Null 'PGint4)
1639 expression = bitAnd (Distinct #col)
1681 expression = bitAnd (Distinct #col)
1640 in printSQL expression
1682 in printSQL expression
1641 :}
1683 :}
@@ -1643,7 +1685,7 @@ class Aggregate expr1 expr2 aggr
1643 -}
1685 -}
1644 bitAnd
1686 bitAnd
1645 :: int `In` PGIntegral
1687 :: int `In` PGIntegral
1646− => expr1 (nullity int)
1688+ => expr1 (null int)
1647 -- ^ what to aggregate
1689 -- ^ what to aggregate
1648 -> aggr ('Null int)
1690 -> aggr ('Null int)
1649
1691
@@ -1652,7 +1694,7 @@ class Aggregate expr1 expr2 aggr
1652
1694
1653 >>> :{
1695 >>> :{
1654 let
1696 let
1655− expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] ('Null 'PGint4)
1697+ expression :: Expression '[] commons ('Grouped bys) schemas params '[tab ::: '["col" ::: null 'PGint4]] ('Null 'PGint4)
1656 expression = bitOr (All #col)
1698 expression = bitOr (All #col)
1657 in printSQL expression
1699 in printSQL expression
1658 :}
1700 :}
@@ -1660,7 +1702,7 @@ class Aggregate expr1 expr2 aggr
1660 -}
1702 -}
1661 bitOr
1703 bitOr
1662 :: int `In` PGIntegral
1704 :: int `In` PGIntegral
1663− => expr1 (nullity int)
1705+ => expr1 (null int)
1664 -- ^ what to aggregate
1706 -- ^ what to aggregate
1665 -> aggr ('Null int)
1707 -> aggr ('Null int)
1666
1708
@@ -1669,14 +1711,14 @@ class Aggregate expr1 expr2 aggr
1669
1711
1670 >>> :{
1712 >>> :{
1671 let
1713 let
1672− winFun :: WindowFunction '[] 'Ungrouped commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] ('Null 'PGbool)
1714+ winFun :: WindowFunction '[] commons 'Ungrouped schemas params '[tab ::: '["col" ::: null 'PGbool]] ('Null 'PGbool)
1673 winFun = boolAnd #col
1715 winFun = boolAnd #col
1674 in printSQL winFun
1716 in printSQL winFun
1675 :}
1717 :}
1676 bool_and("col")
1718 bool_and("col")
1677 -}
1719 -}
1678 boolAnd
1720 boolAnd
1679− :: expr1 (nullity 'PGbool)
1721+ :: expr1 (null 'PGbool)
1680 -- ^ what to aggregate
1722 -- ^ what to aggregate
1681 -> aggr ('Null 'PGbool)
1723 -> aggr ('Null 'PGbool)
1682
1724
@@ -1685,14 +1727,14 @@ class Aggregate expr1 expr2 aggr
1685
1727
1686 >>> :{
1728 >>> :{
1687 let
1729 let
1688− expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] ('Null 'PGbool)
1730+ expression :: Expression '[] commons ('Grouped bys) schemas params '[tab ::: '["col" ::: null 'PGbool]] ('Null 'PGbool)
1689 expression = boolOr (All #col)
1731 expression = boolOr (All #col)
1690 in printSQL expression
1732 in printSQL expression
1691 :}
1733 :}
1692 bool_or(ALL "col")
1734 bool_or(ALL "col")
1693 -}
1735 -}
1694 boolOr
1736 boolOr
1695− :: expr1(nullity 'PGbool)
1737+ :: expr1 (null 'PGbool)
1696 -- ^ what to aggregate
1738 -- ^ what to aggregate
1697 -> aggr ('Null 'PGbool)
1739 -> aggr ('Null 'PGbool)
1698
1740
@@ -1701,185 +1743,185 @@ class Aggregate expr1 expr2 aggr
1701
1743
1702 >>> :{
1744 >>> :{
1703 let
1745 let
1704− expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] ('Null 'PGbool)
1746+ expression :: Expression '[] commons ('Grouped bys) schemas params '[tab ::: '["col" ::: null 'PGbool]] ('Null 'PGbool)
1705 expression = every (Distinct #col)
1747 expression = every (Distinct #col)
1706 in printSQL expression
1748 in printSQL expression
1707 :}
1749 :}
1708 every(DISTINCT "col")
1750 every(DISTINCT "col")
1709 -}
1751 -}
1710 every
1752 every
1711− :: expr1 (nullity 'PGbool)
1753+ :: expr1 (null 'PGbool)
1712 -- ^ what to aggregate
1754 -- ^ what to aggregate
1713 -> aggr ('Null 'PGbool)
1755 -> aggr ('Null 'PGbool)
1714
1756
1715 {- |maximum value of expression across all input values-}
1757 {- |maximum value of expression across all input values-}
1716 max_
1758 max_
1717− :: expr1 (nullity ty)
1759+ :: expr1 (null ty)
1718 -- ^ what to maximize
1760 -- ^ what to maximize
1719 -> aggr ('Null ty)
1761 -> aggr ('Null ty)
1720
1762
1721 -- | minimum value of expression across all input values
1763 -- | minimum value of expression across all input values
1722 min_
1764 min_
1723− :: expr1 (nullity ty)
1765+ :: expr1 (null ty)
1724 -- ^ what to minimize
1766 -- ^ what to minimize
1725 -> aggr ('Null ty)
1767 -> aggr ('Null ty)
1726
1768
1727 -- | the average (arithmetic mean) of all input values
1769 -- | the average (arithmetic mean) of all input values
1728 avg
1770 avg
1729− :: expr1 (nullity ty)
1771+ :: expr1 (null ty)
1730 -- ^ what to average
1772 -- ^ what to average
1731 -> aggr ('Null (PGAvg ty))
1773 -> aggr ('Null (PGAvg ty))
1732
1774
1733 {- | correlation coefficient
1775 {- | correlation coefficient
1734 >>> :{
1776 >>> :{
1735 let
1777 let
1736− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1778+ expression :: Expression '[] c ('Grouped g) s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1737− expression = corr (All (#y :*: #x))
1779+ expression = corr (All (#y *: #x))
1738 in printSQL expression
1780 in printSQL expression
1739 :}
1781 :}
1740 corr(ALL "y", "x")
1782 corr(ALL "y", "x")
1741 -}
1783 -}
1742 corr
1784 corr
1743− :: expr2 (nullity 'PGfloat8)
1785+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1744 -> aggr ('Null 'PGfloat8)
1786 -> aggr ('Null 'PGfloat8)
1745
1787
1746 {- | population covariance
1788 {- | population covariance
1747 >>> :{
1789 >>> :{
1748 let
1790 let
1749− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1791+ expression :: Expression '[] c ('Grouped g) s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1750− expression = covarPop (All (#y :*: #x))
1792+ expression = covarPop (All (#y *: #x))
1751 in printSQL expression
1793 in printSQL expression
1752 :}
1794 :}
1753 covar_pop(ALL "y", "x")
1795 covar_pop(ALL "y", "x")
1754 -}
1796 -}
1755 covarPop
1797 covarPop
1756− :: expr2 (nullity 'PGfloat8)
1798+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1757 -> aggr ('Null 'PGfloat8)
1799 -> aggr ('Null 'PGfloat8)
1758
1800
1759 {- | sample covariance
1801 {- | sample covariance
1760 >>> :{
1802 >>> :{
1761 let
1803 let
1762− winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1804+ winFun :: WindowFunction '[] c 'Ungrouped s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1763− winFun = covarSamp (#y :*: #x)
1805+ winFun = covarSamp (#y *: #x)
1764 in printSQL winFun
1806 in printSQL winFun
1765 :}
1807 :}
1766 covar_samp("y", "x")
1808 covar_samp("y", "x")
1767 -}
1809 -}
1768 covarSamp
1810 covarSamp
1769− :: expr2 (nullity 'PGfloat8)
1811+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1770 -> aggr ('Null 'PGfloat8)
1812 -> aggr ('Null 'PGfloat8)
1771
1813
1772 {- | average of the independent variable (sum(X)/N)
1814 {- | average of the independent variable (sum(X)/N)
1773 >>> :{
1815 >>> :{
1774 let
1816 let
1775− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1817+ expression :: Expression '[] c ('Grouped g) s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1776− expression = regrAvgX (All (#y :*: #x))
1818+ expression = regrAvgX (All (#y *: #x))
1777 in printSQL expression
1819 in printSQL expression
1778 :}
1820 :}
1779 regr_avgx(ALL "y", "x")
1821 regr_avgx(ALL "y", "x")
1780 -}
1822 -}
1781 regrAvgX
1823 regrAvgX
1782− :: expr2 (nullity 'PGfloat8)
1824+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1783 -> aggr ('Null 'PGfloat8)
1825 -> aggr ('Null 'PGfloat8)
1784
1826
1785 {- | average of the dependent variable (sum(Y)/N)
1827 {- | average of the dependent variable (sum(Y)/N)
1786 >>> :{
1828 >>> :{
1787 let
1829 let
1788− winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1830+ winFun :: WindowFunction '[] c 'Ungrouped s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1789− winFun = regrAvgY (#y :*: #x)
1831+ winFun = regrAvgY (#y *: #x)
1790 in printSQL winFun
1832 in printSQL winFun
1791 :}
1833 :}
1792 regr_avgy("y", "x")
1834 regr_avgy("y", "x")
1793 -}
1835 -}
1794 regrAvgY
1836 regrAvgY
1795− :: expr2 (nullity 'PGfloat8)
1837+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1796 -> aggr ('Null 'PGfloat8)
1838 -> aggr ('Null 'PGfloat8)
1797
1839
1798 {- | number of input rows in which both expressions are nonnull
1840 {- | number of input rows in which both expressions are nonnull
1799 >>> :{
1841 >>> :{
1800 let
1842 let
1801− winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGint8)
1843+ winFun :: WindowFunction '[] c 'Ungrouped s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGint8)
1802− winFun = regrCount (#y :*: #x)
1844+ winFun = regrCount (#y *: #x)
1803 in printSQL winFun
1845 in printSQL winFun
1804 :}
1846 :}
1805 regr_count("y", "x")
1847 regr_count("y", "x")
1806 -}
1848 -}
1807 regrCount
1849 regrCount
1808− :: expr2 (nullity 'PGfloat8)
1850+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1809 -> aggr ('Null 'PGint8)
1851 -> aggr ('Null 'PGint8)
1810
1852
1811 {- | y-intercept of the least-squares-fit linear equation determined by the (X, Y) pairs
1853 {- | y-intercept of the least-squares-fit linear equation determined by the (X, Y) pairs
1812 >>> :{
1854 >>> :{
1813 let
1855 let
1814− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1856+ expression :: Expression '[] c ('Grouped g) s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1815− expression = regrIntercept (All (#y :*: #x))
1857+ expression = regrIntercept (All (#y *: #x))
1816 in printSQL expression
1858 in printSQL expression
1817 :}
1859 :}
1818 regr_intercept(ALL "y", "x")
1860 regr_intercept(ALL "y", "x")
1819 -}
1861 -}
1820 regrIntercept
1862 regrIntercept
1821− :: expr2 (nullity 'PGfloat8)
1863+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1822 -> aggr ('Null 'PGfloat8)
1864 -> aggr ('Null 'PGfloat8)
1823
1865
1824 -- | @regr_r2(Y, X)@, square of the correlation coefficient
1866 -- | @regr_r2(Y, X)@, square of the correlation coefficient
1825 regrR2
1867 regrR2
1826− :: expr2 (nullity 'PGfloat8)
1868+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1827 -> aggr ('Null 'PGfloat8)
1869 -> aggr ('Null 'PGfloat8)
1828
1870
1829 -- | @regr_slope(Y, X)@, slope of the least-squares-fit linear equation
1871 -- | @regr_slope(Y, X)@, slope of the least-squares-fit linear equation
1830 -- determined by the (X, Y) pairs
1872 -- determined by the (X, Y) pairs
1831 regrSlope
1873 regrSlope
1832− :: expr2 (nullity 'PGfloat8)
1874+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1833 -> aggr ('Null 'PGfloat8)
1875 -> aggr ('Null 'PGfloat8)
1834
1876
1835 -- | @regr_sxx(Y, X)@, sum(X^2) - sum(X)^2/N
1877 -- | @regr_sxx(Y, X)@, sum(X^2) - sum(X)^2/N
1836 -- (“sum of squares” of the independent variable)
1878 -- (“sum of squares” of the independent variable)
1837 regrSxx
1879 regrSxx
1838− :: expr2 (nullity 'PGfloat8)
1880+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1839 -> aggr ('Null 'PGfloat8)
1881 -> aggr ('Null 'PGfloat8)
1840
1882
1841 -- | @regr_sxy(Y, X)@, sum(X*Y) - sum(X) * sum(Y)/N
1883 -- | @regr_sxy(Y, X)@, sum(X*Y) - sum(X) * sum(Y)/N
1842 -- (“sum of products” of independent times dependent variable)
1884 -- (“sum of products” of independent times dependent variable)
1843 regrSxy
1885 regrSxy
1844− :: expr2 (nullity 'PGfloat8)
1886+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1845 -> aggr ('Null 'PGfloat8)
1887 -> aggr ('Null 'PGfloat8)
1846
1888
1847 -- | @regr_syy(Y, X)@, sum(Y^2) - sum(Y)^2/N
1889 -- | @regr_syy(Y, X)@, sum(Y^2) - sum(Y)^2/N
1848 -- (“sum of squares” of the dependent variable)
1890 -- (“sum of squares” of the dependent variable)
1849 regrSyy
1891 regrSyy
1850− :: expr2 (nullity 'PGfloat8)
1892+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1851 -> aggr ('Null 'PGfloat8)
1893 -> aggr ('Null 'PGfloat8)
1852
1894
1853 -- | historical alias for `stddevSamp`
1895 -- | historical alias for `stddevSamp`
1854 stddev
1896 stddev
1855− :: expr1 (nullity ty)
1897+ :: expr1 (null ty)
1856 -> aggr ('Null (PGAvg ty))
1898 -> aggr ('Null (PGAvg ty))
1857
1899
1858 -- | population standard deviation of the input values
1900 -- | population standard deviation of the input values
1859 stddevPop
1901 stddevPop
1860− :: expr1 (nullity ty)
1902+ :: expr1 (null ty)
1861 -> aggr ('Null (PGAvg ty))
1903 -> aggr ('Null (PGAvg ty))
1862
1904
1863 -- | sample standard deviation of the input values
1905 -- | sample standard deviation of the input values
1864 stddevSamp
1906 stddevSamp
1865− :: expr1 (nullity ty)
1907+ :: expr1 (null ty)
1866 -> aggr ('Null (PGAvg ty))
1908 -> aggr ('Null (PGAvg ty))
1867
1909
1868 -- | historical alias for `varSamp`
1910 -- | historical alias for `varSamp`
1869 variance
1911 variance
1870− :: expr1 (nullity ty)
1912+ :: expr1 (null ty)
1871 -> aggr ('Null (PGAvg ty))
1913 -> aggr ('Null (PGAvg ty))
1872
1914
1873 -- | population variance of the input values
1915 -- | population variance of the input values
1874 -- (square of the population standard deviation)
1916 -- (square of the population standard deviation)
1875 varPop
1917 varPop
1876− :: expr1 (nullity ty)
1918+ :: expr1 (null ty)
1877 -> aggr ('Null (PGAvg ty))
1919 -> aggr ('Null (PGAvg ty))
1878
1920
1879 -- | sample variance of the input values
1921 -- | sample variance of the input values
1880 -- (square of the sample standard deviation)
1922 -- (square of the sample standard deviation)
1881 varSamp
1923 varSamp
1882− :: expr1 (nullity ty)
1924+ :: expr1 (null ty)
1883 -> aggr ('Null (PGAvg ty))
1925 -> aggr ('Null (PGAvg ty))
1884
1926
1885{- |
1927{- |
@@ -1888,30 +1930,25 @@ class Aggregate expr1 expr2 aggr
1888`Distinct` invokes the aggregate once for each distinct value of the expression
1930`Distinct` invokes the aggregate once for each distinct value of the expression
1889(or distinct set of values, for multiple expressions) found in the input
1931(or distinct set of values, for multiple expressions) found in the input
1890-}
1932-}
1891−data Distinction expr ty
1933+data Distinction (expr :: kind -> Type) (ty :: kind)
1892 = All (expr ty)
1934 = All (expr ty)
1893 | Distinct (expr ty)
1935 | Distinct (expr ty)
1894 deriving (GHC.Generic,Show,Eq,Ord)
1936 deriving (GHC.Generic,Show,Eq,Ord)
1895−instance NFData (Distinction (Expression outer grp commons schemas params from) ty)
1937+instance NFData (Distinction (Expression outer commons grp schemas params from) ty)
1896−instance RenderSQL (Distinction (Expression outer grp commons schemas params from) ty) where
1938+instance RenderSQL (Distinction (Expression outer commons grp schemas params from) ty) where
1897 renderSQL = \case
1939 renderSQL = \case
1898 All x -> "ALL" <+> renderSQL x
1940 All x -> "ALL" <+> renderSQL x
1899 Distinct x -> "DISTINCT" <+> renderSQL x
1941 Distinct x -> "DISTINCT" <+> renderSQL x
1900−instance RenderSQL
1942+instance SOP.SListI tys => RenderSQL
1901− (Distinction
1943+ (Distinction (NP (Expression outer commons grp schemas params from)) tys) where
1902− ( Expression outer grp commons schemas params from
1903− :*:
1904− Expression outer grp commons schemas params from) xty ) where
1905− renderSQL = \case
1944+ renderSQL = \case
1906− All (x :*: x') ->
1945+ All xs -> "ALL" <+> renderCommaSeparated renderSQL xs
1907− "ALL" <+> renderSQL x <> ", " <> renderSQL x'
1908− Distinct (x :*: x') ->
1909− "DISTINCT" <+> renderSQL x <> ", " <> renderSQL x'
1946+ Distinct xs -> "DISTINCT" <+> renderCommaSeparated renderSQL xs
1910
1947
1911instance Aggregate
1948instance Aggregate
1912− (Distinction (Expression outer 'Ungrouped commons schemas params from))
1949+ (Distinction (Expression outer commons 'Ungrouped schemas params from))
1913− (Distinction (Expression outer 'Ungrouped commons schemas params from :*: Expression outer 'Ungrouped commons schemas params from))
1950+ (Distinction (NP (Expression outer commons 'Ungrouped schemas params from)))
1914− (Expression outer ('Grouped bys) commons schemas params from) where
1951+ (Expression outer commons ('Grouped bys) schemas params from) where
1915 countStar = UnsafeExpression "count(*)"
1952 countStar = UnsafeExpression "count(*)"
1916 count = unsafeAggregate1 "count"
1953 count = unsafeAggregate1 "count"
1917 sum_ = unsafeAggregate1 "sum"
1954 sum_ = unsafeAggregate1 "sum"
@@ -1926,18 +1963,18 @@ instance Aggregate
1926 max_ = unsafeAggregate1 "max"
1963 max_ = unsafeAggregate1 "max"
1927 min_ = unsafeAggregate1 "min"
1964 min_ = unsafeAggregate1 "min"
1928 avg = unsafeAggregate1 "avg"
1965 avg = unsafeAggregate1 "avg"
1929− corr = unsafeAggregate2 "corr"
1966+ corr = unsafeAggregateHet "corr"
1930− covarPop = unsafeAggregate2 "covar_pop"
1967+ covarPop = unsafeAggregateHet "covar_pop"
1931− covarSamp = unsafeAggregate2 "covar_samp"
1968+ covarSamp = unsafeAggregateHet "covar_samp"
1932− regrAvgX = unsafeAggregate2 "regr_avgx"
1969+ regrAvgX = unsafeAggregateHet "regr_avgx"
1933− regrAvgY = unsafeAggregate2 "regr_avgy"
1970+ regrAvgY = unsafeAggregateHet "regr_avgy"
1934− regrCount = unsafeAggregate2 "regr_count"
1971+ regrCount = unsafeAggregateHet "regr_count"
1935− regrIntercept = unsafeAggregate2 "regr_intercept"
1972+ regrIntercept = unsafeAggregateHet "regr_intercept"
1936− regrR2 = unsafeAggregate2 "regr_r2"
1973+ regrR2 = unsafeAggregateHet "regr_r2"
1937− regrSlope = unsafeAggregate2 "regr_slope"
1974+ regrSlope = unsafeAggregateHet "regr_slope"
1938− regrSxx = unsafeAggregate2 "regr_sxx"
1975+ regrSxx = unsafeAggregateHet "regr_sxx"
1939− regrSxy = unsafeAggregate2 "regr_sxy"
1976+ regrSxy = unsafeAggregateHet "regr_sxy"
1940− regrSyy = unsafeAggregate2 "regr_syy"
1977+ regrSyy = unsafeAggregateHet "regr_syy"
1941 stddev = unsafeAggregate1 "stddev"
1978 stddev = unsafeAggregate1 "stddev"
1942 stddevPop = unsafeAggregate1 "stddev_pop"
1979 stddevPop = unsafeAggregate1 "stddev_pop"
1943 stddevSamp = unsafeAggregate1 "stddev_samp"
1980 stddevSamp = unsafeAggregate1 "stddev_samp"
@@ -1945,9 +1982,9 @@ instance Aggregate
1945 varPop = unsafeAggregate1 "var_pop"
1982 varPop = unsafeAggregate1 "var_pop"
1946 varSamp = unsafeAggregate1 "var_samp"
1983 varSamp = unsafeAggregate1 "var_samp"
1947instance Aggregate
1984instance Aggregate
1948− (Expression outer grp commons schemas params from)
1985+ (Expression outer commons grp schemas params from)
1949− (Expression outer grp commons schemas params from :*: Expression outer grp commons schemas params from)
1986+ (NP (Expression outer commons grp schemas params from))
1950− (WindowFunction outer grp commons schemas params from) where
1987+ (WindowFunction outer commons grp schemas params from) where
1951 countStar = UnsafeWindowFunction "count(*)"
1988 countStar = UnsafeWindowFunction "count(*)"
1952 count = unsafeWindowFunction1 "count"
1989 count = unsafeWindowFunction1 "count"
1953 sum_ = unsafeWindowFunction1 "sum"
1990 sum_ = unsafeWindowFunction1 "sum"
@@ -1962,18 +1999,18 @@ instance Aggregate
1962 max_ = unsafeWindowFunction1 "max"
1999 max_ = unsafeWindowFunction1 "max"
1963 min_ = unsafeWindowFunction1 "min"
2000 min_ = unsafeWindowFunction1 "min"
1964 avg = unsafeWindowFunction1 "avg"
2001 avg = unsafeWindowFunction1 "avg"
1965− corr = unsafeWindowFunction2 "corr"
2002+ corr = unsafeWindowFunctionHet "corr"
1966− covarPop = unsafeWindowFunction2 "covar_pop"
2003+ covarPop = unsafeWindowFunctionHet "covar_pop"
1967− covarSamp = unsafeWindowFunction2 "covar_samp"
2004+ covarSamp = unsafeWindowFunctionHet "covar_samp"
1968− regrAvgX = unsafeWindowFunction2 "regr_avgx"
2005+ regrAvgX = unsafeWindowFunctionHet "regr_avgx"
1969− regrAvgY = unsafeWindowFunction2 "regr_avgy"
2006+ regrAvgY = unsafeWindowFunctionHet "regr_avgy"
1970− regrCount = unsafeWindowFunction2 "regr_count"
2007+ regrCount = unsafeWindowFunctionHet "regr_count"
1971− regrIntercept = unsafeWindowFunction2 "regr_intercept"
2008+ regrIntercept = unsafeWindowFunctionHet "regr_intercept"
1972− regrR2 = unsafeWindowFunction2 "regr_r2"
2009+ regrR2 = unsafeWindowFunctionHet "regr_r2"
1973− regrSlope = unsafeWindowFunction2 "regr_slope"
2010+ regrSlope = unsafeWindowFunctionHet "regr_slope"
1974− regrSxx = unsafeWindowFunction2 "regr_sxx"
2011+ regrSxx = unsafeWindowFunctionHet "regr_sxx"
1975− regrSxy = unsafeWindowFunction2 "regr_sxy"
2012+ regrSxy = unsafeWindowFunctionHet "regr_sxy"
1976− regrSyy = unsafeWindowFunction2 "regr_syy"
2013+ regrSyy = unsafeWindowFunctionHet "regr_syy"
1977 stddev = unsafeWindowFunction1 "stddev"
2014 stddev = unsafeWindowFunction1 "stddev"
1978 stddevPop = unsafeWindowFunction1 "stddev_pop"
2015 stddevPop = unsafeWindowFunction1 "stddev_pop"
1979 stddevSamp = unsafeWindowFunction1 "stddev_samp"
2016 stddevSamp = unsafeWindowFunction1 "stddev_samp"
@@ -1984,18 +2021,9 @@ instance Aggregate
1984-- | escape hatch to define aggregate functions
2021-- | escape hatch to define aggregate functions
1985unsafeAggregate1
2022unsafeAggregate1
1986 :: ByteString -- ^ aggregate function
2023 :: ByteString -- ^ aggregate function
1987− -> Distinction (Expression outer 'Ungrouped commons schemas params from) xty
2024+ -> Distinction (Expression outer commons 'Ungrouped schemas params from) x
1988− -> Expression outer ('Grouped bys) commons schemas params from yty
2025+ -> Expression outer commons ('Grouped bys) schemas params from y
1989−unsafeAggregate1 fun x = UnsafeExpression $ mconcat
2026+unsafeAggregate1 fun x = UnsafeExpression $ fun <> parenthesized (renderSQL x)
1990− [fun, "(", renderSQL x, ")"]
1991−
1992−-- | escape hatch to define aggregate binary functions
1993−unsafeAggregate2
1994− :: ByteString -- ^ aggregate function
1995− -> Distinction (Expression outer 'Ungrouped commons schemas params from :*: Expression outer 'Ungrouped commons schemas params from) xty
1996− -> Expression outer ('Grouped bys) commons schemas params from yty
1997−unsafeAggregate2 fun xx = UnsafeExpression $ mconcat
1998− [fun, "(", renderSQL xx, ")"]
1999
2027
2000-- | A type family that calculates `PGAvg` type of a `PGType`.
2028-- | A type family that calculates `PGAvg` type of a `PGType`.
2001type family PGAvg ty where
2029type family PGAvg ty where
@@ -2025,7 +2053,7 @@ instance RenderSQL (TypeExpression schemas ty) where
2025typedef
2053typedef
2026 :: (Has sch schemas schema, Has td schema ('Typedef ty))
2054 :: (Has sch schemas schema, Has td schema ('Typedef ty))
2027 => QualifiedAlias sch td
2055 => QualifiedAlias sch td
2028− -> TypeExpression schemas (nullity ty)
2056+ -> TypeExpression schemas (null ty)
2029typedef = UnsafeTypeExpression . renderSQL
2057typedef = UnsafeTypeExpression . renderSQL
2030
2058
2031-- | The composite type corresponding to a `Table` definition can be expressed
2059-- | The composite type corresponding to a `Table` definition can be expressed
@@ -2033,7 +2061,7 @@ typedef = UnsafeTypeExpression . renderSQL
2033typetable
2061typetable
2034 :: (Has sch schemas schema, Has tab schema ('Table table))
2062 :: (Has sch schemas schema, Has tab schema ('Table table))
2035 => QualifiedAlias sch tab
2063 => QualifiedAlias sch tab
2036− -> TypeExpression schemas (nullity ('PGcomposite (TableToRow table)))
2064+ -> TypeExpression schemas (null ('PGcomposite (TableToRow table)))
2037typetable = UnsafeTypeExpression . renderSQL
2065typetable = UnsafeTypeExpression . renderSQL
2038
2066
2039-- | The composite type corresponding to a `View` definition can be expressed
2067-- | The composite type corresponding to a `View` definition can be expressed
@@ -2041,101 +2069,101 @@ typetable = UnsafeTypeExpression . renderSQL
2041typeview
2069typeview
2042 :: (Has sch schemas schema, Has vw schema ('View view))
2070 :: (Has sch schemas schema, Has vw schema ('View view))
2043 => QualifiedAlias sch vw
2071 => QualifiedAlias sch vw
2044− -> TypeExpression schemas (nullity ('PGcomposite view))
2072+ -> TypeExpression schemas (null ('PGcomposite view))
2045typeview = UnsafeTypeExpression . renderSQL
2073typeview = UnsafeTypeExpression . renderSQL
2046
2074
2047-- | logical Boolean (true/false)
2075-- | logical Boolean (true/false)
2048−bool :: TypeExpression schemas (nullity 'PGbool)
2076+bool :: TypeExpression schemas (null 'PGbool)
2049bool = UnsafeTypeExpression "bool"
2077bool = UnsafeTypeExpression "bool"
2050-- | signed two-byte integer
2078-- | signed two-byte integer
2051−int2, smallint :: TypeExpression schemas (nullity 'PGint2)
2079+int2, smallint :: TypeExpression schemas (null 'PGint2)
2052int2 = UnsafeTypeExpression "int2"
2080int2 = UnsafeTypeExpression "int2"
2053smallint = UnsafeTypeExpression "smallint"
2081smallint = UnsafeTypeExpression "smallint"
2054-- | signed four-byte integer
2082-- | signed four-byte integer
2055−int4, int, integer :: TypeExpression schemas (nullity 'PGint4)
2083+int4, int, integer :: TypeExpression schemas (null 'PGint4)
2056int4 = UnsafeTypeExpression "int4"
2084int4 = UnsafeTypeExpression "int4"
2057int = UnsafeTypeExpression "int"
2085int = UnsafeTypeExpression "int"
2058integer = UnsafeTypeExpression "integer"
2086integer = UnsafeTypeExpression "integer"
2059-- | signed eight-byte integer
2087-- | signed eight-byte integer
2060−int8, bigint :: TypeExpression schemas (nullity 'PGint8)
2088+int8, bigint :: TypeExpression schemas (null 'PGint8)
2061int8 = UnsafeTypeExpression "int8"
2089int8 = UnsafeTypeExpression "int8"
2062bigint = UnsafeTypeExpression "bigint"
2090bigint = UnsafeTypeExpression "bigint"
2063-- | arbitrary precision numeric type
2091-- | arbitrary precision numeric type
2064−numeric :: TypeExpression schemas (nullity 'PGnumeric)
2092+numeric :: TypeExpression schemas (null 'PGnumeric)
2065numeric = UnsafeTypeExpression "numeric"
2093numeric = UnsafeTypeExpression "numeric"
2066-- | single precision floating-point number (4 bytes)
2094-- | single precision floating-point number (4 bytes)
2067−float4, real :: TypeExpression schemas (nullity 'PGfloat4)
2095+float4, real :: TypeExpression schemas (null 'PGfloat4)
2068float4 = UnsafeTypeExpression "float4"
2096float4 = UnsafeTypeExpression "float4"
2069real = UnsafeTypeExpression "real"
2097real = UnsafeTypeExpression "real"
2070-- | double precision floating-point number (8 bytes)
2098-- | double precision floating-point number (8 bytes)
2071−float8, doublePrecision :: TypeExpression schemas (nullity 'PGfloat8)
2099+float8, doublePrecision :: TypeExpression schemas (null 'PGfloat8)
2072float8 = UnsafeTypeExpression "float8"
2100float8 = UnsafeTypeExpression "float8"
2073doublePrecision = UnsafeTypeExpression "double precision"
2101doublePrecision = UnsafeTypeExpression "double precision"
2074-- | currency amount
2102-- | currency amount
2075−money :: TypeExpression schema (nullity 'PGmoney)
2103+money :: TypeExpression schema (null 'PGmoney)
2076money = UnsafeTypeExpression "money"
2104money = UnsafeTypeExpression "money"
2077-- | variable-length character string
2105-- | variable-length character string
2078−text :: TypeExpression schemas (nullity 'PGtext)
2106+text :: TypeExpression schemas (null 'PGtext)
2079text = UnsafeTypeExpression "text"
2107text = UnsafeTypeExpression "text"
2080-- | fixed-length character string
2108-- | fixed-length character string
2081char, character
2109char, character
2082− :: forall n schemas nullity. (KnownNat n, 1 <= n)
2110+ :: forall n schemas null. (KnownNat n, 1 <= n)
2083− => TypeExpression schemas (nullity ('PGchar n))
2111+ => TypeExpression schemas (null ('PGchar n))
2084char = UnsafeTypeExpression $ "char(" <> renderNat @n <> ")"
2112char = UnsafeTypeExpression $ "char(" <> renderNat @n <> ")"
2085character = UnsafeTypeExpression $ "character(" <> renderNat @n <> ")"
2113character = UnsafeTypeExpression $ "character(" <> renderNat @n <> ")"
2086-- | variable-length character string
2114-- | variable-length character string
2087varchar, characterVarying
2115varchar, characterVarying
2088− :: forall n schemas nullity. (KnownNat n, 1 <= n)
2116+ :: forall n schemas null. (KnownNat n, 1 <= n)
2089− => TypeExpression schemas (nullity ('PGvarchar n))
2117+ => TypeExpression schemas (null ('PGvarchar n))
2090varchar = UnsafeTypeExpression $ "varchar(" <> renderNat @n <> ")"
2118varchar = UnsafeTypeExpression $ "varchar(" <> renderNat @n <> ")"
2091characterVarying = UnsafeTypeExpression $
2119characterVarying = UnsafeTypeExpression $
2092 "character varying(" <> renderNat @n <> ")"
2120 "character varying(" <> renderNat @n <> ")"
2093-- | binary data ("byte array")
2121-- | binary data ("byte array")
2094−bytea :: TypeExpression schemas (nullity 'PGbytea)
2122+bytea :: TypeExpression schemas (null 'PGbytea)
2095bytea = UnsafeTypeExpression "bytea"
2123bytea = UnsafeTypeExpression "bytea"
2096-- | date and time (no time zone)
2124-- | date and time (no time zone)
2097−timestamp :: TypeExpression schemas (nullity 'PGtimestamp)
2125+timestamp :: TypeExpression schemas (null 'PGtimestamp)
2098timestamp = UnsafeTypeExpression "timestamp"
2126timestamp = UnsafeTypeExpression "timestamp"
2099-- | date and time, including time zone
2127-- | date and time, including time zone
2100−timestampWithTimeZone :: TypeExpression schemas (nullity 'PGtimestamptz)
2128+timestampWithTimeZone :: TypeExpression schemas (null 'PGtimestamptz)
2101timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
2129timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
2102-- | calendar date (year, month, day)
2130-- | calendar date (year, month, day)
2103−date :: TypeExpression schemas (nullity 'PGdate)
2131+date :: TypeExpression schemas (null 'PGdate)
2104date = UnsafeTypeExpression "date"
2132date = UnsafeTypeExpression "date"
2105-- | time of day (no time zone)
2133-- | time of day (no time zone)
2106−time :: TypeExpression schemas (nullity 'PGtime)
2134+time :: TypeExpression schemas (null 'PGtime)
2107time = UnsafeTypeExpression "time"
2135time = UnsafeTypeExpression "time"
2108-- | time of day, including time zone
2136-- | time of day, including time zone
2109−timeWithTimeZone :: TypeExpression schemas (nullity 'PGtimetz)
2137+timeWithTimeZone :: TypeExpression schemas (null 'PGtimetz)
2110timeWithTimeZone = UnsafeTypeExpression "time with time zone"
2138timeWithTimeZone = UnsafeTypeExpression "time with time zone"
2111-- | time span
2139-- | time span
2112−interval :: TypeExpression schemas (nullity 'PGinterval)
2140+interval :: TypeExpression schemas (null 'PGinterval)
2113interval = UnsafeTypeExpression "interval"
2141interval = UnsafeTypeExpression "interval"
2114-- | universally unique identifier
2142-- | universally unique identifier
2115−uuid :: TypeExpression schemas (nullity 'PGuuid)
2143+uuid :: TypeExpression schemas (null 'PGuuid)
2116uuid = UnsafeTypeExpression "uuid"
2144uuid = UnsafeTypeExpression "uuid"
2117-- | IPv4 or IPv6 host address
2145-- | IPv4 or IPv6 host address
2118−inet :: TypeExpression schemas (nullity 'PGinet)
2146+inet :: TypeExpression schemas (null 'PGinet)
2119inet = UnsafeTypeExpression "inet"
2147inet = UnsafeTypeExpression "inet"
2120-- | textual JSON data
2148-- | textual JSON data
2121−json :: TypeExpression schemas (nullity 'PGjson)
2149+json :: TypeExpression schemas (null 'PGjson)
2122json = UnsafeTypeExpression "json"
2150json = UnsafeTypeExpression "json"
2123-- | binary JSON data, decomposed
2151-- | binary JSON data, decomposed
2124−jsonb :: TypeExpression schemas (nullity 'PGjsonb)
2152+jsonb :: TypeExpression schemas (null 'PGjsonb)
2125jsonb = UnsafeTypeExpression "jsonb"
2153jsonb = UnsafeTypeExpression "jsonb"
2126-- | variable length array
2154-- | variable length array
2127vararray
2155vararray
2128 :: TypeExpression schemas pg
2156 :: TypeExpression schemas pg
2129− -> TypeExpression schemas (nullity ('PGvararray pg))
2157+ -> TypeExpression schemas (null ('PGvararray pg))
2130vararray ty = UnsafeTypeExpression $ renderSQL ty <> "[]"
2158vararray ty = UnsafeTypeExpression $ renderSQL ty <> "[]"
2131-- | fixed length array
2159-- | fixed length array
2132--
2160--
2133-- >>> renderSQL (fixarray @'[2] json)
2161-- >>> renderSQL (fixarray @'[2] json)
2134-- "json[2]"
2162-- "json[2]"
2135fixarray
2163fixarray
2136− :: forall dims schemas nullity pg. SOP.All KnownNat dims
2164+ :: forall dims schemas null pg. SOP.All KnownNat dims
2137 => TypeExpression schemas pg
2165 => TypeExpression schemas pg
2138− -> TypeExpression schemas (nullity ('PGfixarray dims pg))
2166+ -> TypeExpression schemas (null ('PGfixarray dims pg))
2139fixarray ty = UnsafeTypeExpression $
2167fixarray ty = UnsafeTypeExpression $
2140 renderSQL ty <> renderDims @dims
2168 renderSQL ty <> renderDims @dims
2141 where
2169 where
@@ -2148,38 +2176,45 @@ fixarray ty = UnsafeTypeExpression $
2148 $ hcmap (SOP.Proxy @KnownNat)
2176 $ hcmap (SOP.Proxy @KnownNat)
2149 (K . fromString . show . natVal)
2177 (K . fromString . show . natVal)
2150 (hpure SOP.Proxy :: NP SOP.Proxy ns)
2178 (hpure SOP.Proxy :: NP SOP.Proxy ns)
2179+tsvector :: TypeExpression schemas (null 'PGtsvector)
2180+tsvector = UnsafeTypeExpression "tsvector"
2181+tsquery :: TypeExpression schemas (null 'PGtsquery)
2182+tsquery = UnsafeTypeExpression "tsquery"
2151
2183
2152-- | `pgtype` is a demoted version of a `PGType`
2184-- | `pgtype` is a demoted version of a `PGType`
2153class PGTyped schemas (ty :: NullityType) where
2185class PGTyped schemas (ty :: NullityType) where
2154 pgtype :: TypeExpression schemas ty
2186 pgtype :: TypeExpression schemas ty
2155−instance PGTyped schemas (nullity 'PGbool) where pgtype = bool
2187+instance PGTyped schemas (null 'PGbool) where pgtype = bool
2156−instance PGTyped schemas (nullity 'PGint2) where pgtype = int2
2188+instance PGTyped schemas (null 'PGint2) where pgtype = int2
2157−instance PGTyped schemas (nullity 'PGint4) where pgtype = int4
2189+instance PGTyped schemas (null 'PGint4) where pgtype = int4
2158−instance PGTyped schemas (nullity 'PGint8) where pgtype = int8
2190+instance PGTyped schemas (null 'PGint8) where pgtype = int8
2159−instance PGTyped schemas (nullity 'PGnumeric) where pgtype = numeric
2191+instance PGTyped schemas (null 'PGnumeric) where pgtype = numeric
2160−instance PGTyped schemas (nullity 'PGfloat4) where pgtype = float4
2192+instance PGTyped schemas (null 'PGfloat4) where pgtype = float4
2161−instance PGTyped schemas (nullity 'PGfloat8) where pgtype = float8
2193+instance PGTyped schemas (null 'PGfloat8) where pgtype = float8
2162−instance PGTyped schemas (nullity 'PGtext) where pgtype = text
2194+instance PGTyped schemas (null 'PGmoney) where pgtype = money
2195+instance PGTyped schemas (null 'PGtext) where pgtype = text
2163instance (KnownNat n, 1 <= n)
2196instance (KnownNat n, 1 <= n)
2164− => PGTyped schemas (nullity ('PGchar n)) where pgtype = char @n
2197+ => PGTyped schemas (null ('PGchar n)) where pgtype = char @n
2165instance (KnownNat n, 1 <= n)
2198instance (KnownNat n, 1 <= n)
2166− => PGTyped schemas (nullity ('PGvarchar n)) where pgtype = varchar @n
2199+ => PGTyped schemas (null ('PGvarchar n)) where pgtype = varchar @n
2167−instance PGTyped schemas (nullity 'PGbytea) where pgtype = bytea
2200+instance PGTyped schemas (null 'PGbytea) where pgtype = bytea
2168−instance PGTyped schemas (nullity 'PGtimestamp) where pgtype = timestamp
2201+instance PGTyped schemas (null 'PGtimestamp) where pgtype = timestamp
2169−instance PGTyped schemas (nullity 'PGtimestamptz) where pgtype = timestampWithTimeZone
2202+instance PGTyped schemas (null 'PGtimestamptz) where pgtype = timestampWithTimeZone
2170−instance PGTyped schemas (nullity 'PGdate) where pgtype = date
2203+instance PGTyped schemas (null 'PGdate) where pgtype = date
2171−instance PGTyped schemas (nullity 'PGtime) where pgtype = time
2204+instance PGTyped schemas (null 'PGtime) where pgtype = time
2172−instance PGTyped schemas (nullity 'PGtimetz) where pgtype = timeWithTimeZone
2205+instance PGTyped schemas (null 'PGtimetz) where pgtype = timeWithTimeZone
2173−instance PGTyped schemas (nullity 'PGinterval) where pgtype = interval
2206+instance PGTyped schemas (null 'PGinterval) where pgtype = interval
2174−instance PGTyped schemas (nullity 'PGuuid) where pgtype = uuid
2207+instance PGTyped schemas (null 'PGuuid) where pgtype = uuid
2175−instance PGTyped schemas (nullity 'PGjson) where pgtype = json
2208+instance PGTyped schemas (null 'PGjson) where pgtype = json
2176−instance PGTyped schemas (nullity 'PGjsonb) where pgtype = jsonb
2209+instance PGTyped schemas (null 'PGjsonb) where pgtype = jsonb
2177instance PGTyped schemas ty
2210instance PGTyped schemas ty
2178− => PGTyped schemas (nullity ('PGvararray ty)) where
2211+ => PGTyped schemas (null ('PGvararray ty)) where
2179 pgtype = vararray (pgtype @schemas @ty)
2212 pgtype = vararray (pgtype @schemas @ty)
2180instance (SOP.All KnownNat dims, PGTyped schemas ty)
2213instance (SOP.All KnownNat dims, PGTyped schemas ty)
2181− => PGTyped schemas (nullity ('PGfixarray dims ty)) where
2214+ => PGTyped schemas (null ('PGfixarray dims ty)) where
2182 pgtype = fixarray @dims (pgtype @schemas @ty)
2215 pgtype = fixarray @dims (pgtype @schemas @ty)
2216+instance PGTyped schemas (null 'PGtsvector) where pgtype = tsvector
2217+instance PGTyped schemas (null 'PGtsquery) where pgtype = tsquery
2183
2218
2184{-----------------------------------------
2219{-----------------------------------------
2185Sorting
2220Sorting
@@ -2194,26 +2229,26 @@ Sorting
2194-- `AscNullsLast`, `DescNullsFirst` and `DescNullsLast` options are used to
2229-- `AscNullsLast`, `DescNullsFirst` and `DescNullsLast` options are used to
2195-- determine whether nulls appear before or after non-null values in the sort
2230-- determine whether nulls appear before or after non-null values in the sort
2196-- ordering of a `Null` result column.
2231-- ordering of a `Null` result column.
2197−data SortExpression outer grp commons schemas params from where
2232+data SortExpression outer commons grp schemas params from where
2198 Asc
2233 Asc
2199− :: Expression outer grp commons schemas params from ('NotNull ty)
2234+ :: Expression outer commons grp schemas params from ('NotNull ty)
2200− -> SortExpression outer grp commons schemas params from
2235+ -> SortExpression outer commons grp schemas params from
2201 Desc
2236 Desc
2202− :: Expression outer grp commons schemas params from ('NotNull ty)
2237+ :: Expression outer commons grp schemas params from ('NotNull ty)
2203− -> SortExpression outer grp commons schemas params from
2238+ -> SortExpression outer commons grp schemas params from
2204 AscNullsFirst
2239 AscNullsFirst
2205− :: Expression outer grp commons schemas params from ('Null ty)
2240+ :: Expression outer commons grp schemas params from ('Null ty)
2206− -> SortExpression outer grp commons schemas params from
2241+ -> SortExpression outer commons grp schemas params from
2207 AscNullsLast
2242 AscNullsLast
2208− :: Expression outer grp commons schemas params from ('Null ty)
2243+ :: Expression outer commons grp schemas params from ('Null ty)
2209− -> SortExpression outer grp commons schemas params from
2244+ -> SortExpression outer commons grp schemas params from
2210 DescNullsFirst
2245 DescNullsFirst
2211− :: Expression outer grp commons schemas params from ('Null ty)
2246+ :: Expression outer commons grp schemas params from ('Null ty)
2212− -> SortExpression outer grp commons schemas params from
2247+ -> SortExpression outer commons grp schemas params from
2213 DescNullsLast
2248 DescNullsLast
2214− :: Expression outer grp commons schemas params from ('Null ty)
2249+ :: Expression outer commons grp schemas params from ('Null ty)
2215− -> SortExpression outer grp commons schemas params from
2250+ -> SortExpression outer commons grp schemas params from
2216−deriving instance Show (SortExpression outer grp commons schemas params from)
2251+deriving instance Show (SortExpression outer commons grp schemas params from)
2217
2252
2218{- |
2253{- |
2219The `orderBy` clause causes the result rows of a `Squeal.PostgreSQL.Query.TableExpression`
2254The `orderBy` clause causes the result rows of a `Squeal.PostgreSQL.Query.TableExpression`
@@ -2228,12 +2263,12 @@ using `orderBy` within `Squeal.PostgreSQL.Query.Over`.
2228-}
2263-}
2229class OrderBy expr where
2264class OrderBy expr where
2230 orderBy
2265 orderBy
2231− :: [SortExpression outer grp commons schemas params from]
2266+ :: [SortExpression outer commons grp schemas params from]
2232− -> expr outer grp commons schemas params from
2267+ -> expr outer commons grp schemas params from
2233− -> expr outer grp commons schemas params from
2268+ -> expr outer commons grp schemas params from
2234
2269
2235-- | Render a `SortExpression`.
2270-- | Render a `SortExpression`.
2236−instance RenderSQL (SortExpression outer grp commons schemas params from) where
2271+instance RenderSQL (SortExpression outer commons grp schemas params from) where
2237 renderSQL = \case
2272 renderSQL = \case
2238 Asc expression -> renderSQL expression <+> "ASC"
2273 Asc expression -> renderSQL expression <+> "ASC"
2239 Desc expression -> renderSQL expression <+> "DESC"
2274 Desc expression -> renderSQL expression <+> "DESC"
@@ -2246,12 +2281,12 @@ instance RenderSQL (SortExpression outer grp commons schemas params from) where
2246
2281
2247-- | A `WindowDefinition` is a set of table rows that are somehow related
2282-- | A `WindowDefinition` is a set of table rows that are somehow related
2248-- to the current row
2283-- to the current row
2249−data WindowDefinition outer grp commons schemas params from where
2284+data WindowDefinition outer commons grp schemas params from where
2250 WindowDefinition
2285 WindowDefinition
2251 :: SListI bys
2286 :: SListI bys
2252− => NP (Expression outer grp commons schemas params from) bys
2287+ => NP (Expression outer commons grp schemas params from) bys
2253− -> [SortExpression outer grp commons schemas params from]
2288+ -> [SortExpression outer commons grp schemas params from]
2254− -> WindowDefinition outer grp commons schemas params from
2289+ -> WindowDefinition outer commons grp schemas params from
2255
2290
2256instance OrderBy WindowDefinition where
2291instance OrderBy WindowDefinition where
2257 orderBy sortsR (WindowDefinition parts sortsL)
2292 orderBy sortsR (WindowDefinition parts sortsL)
@@ -2277,8 +2312,8 @@ the same partition as the current row.
2277-}
2312-}
2278partitionBy
2313partitionBy
2279 :: SListI bys
2314 :: SListI bys
2280− => NP (Expression outer grp commons schemas params from) bys
2315+ => NP (Expression outer commons grp schemas params from) bys
2281− -> WindowDefinition outer grp commons schemas params from
2316+ -> WindowDefinition outer commons grp schemas params from
2282partitionBy bys = WindowDefinition bys []
2317partitionBy bys = WindowDefinition bys []
2283
2318
2284{- |
2319{- |
@@ -2293,8 +2328,8 @@ just the current row of the query result.
2293-}
2328-}
2294newtype WindowFunction
2329newtype WindowFunction
2295 (outer :: FromType)
2330 (outer :: FromType)
2296− (grp :: Grouping)
2297 (commons :: FromType)
2331 (commons :: FromType)
2332+ (grp :: Grouping)
2298 (schemas :: SchemasType)
2333 (schemas :: SchemasType)
2299 (params :: [NullityType])
2334 (params :: [NullityType])
2300 (from :: FromType)
2335 (from :: FromType)
@@ -2302,49 +2337,62 @@ newtype WindowFunction
2302 = UnsafeWindowFunction { renderWindowFunction :: ByteString }
2337 = UnsafeWindowFunction { renderWindowFunction :: ByteString }
2303 deriving (GHC.Generic,Show,Eq,Ord,NFData)
2338 deriving (GHC.Generic,Show,Eq,Ord,NFData)
2304
2339
2305−instance RenderSQL (WindowFunction outer grp commons schemas params from ty) where
2340+instance RenderSQL (WindowFunction outer commons grp schemas params from ty) where
2306 renderSQL = renderWindowFunction
2341 renderSQL = renderWindowFunction
2307
2342
2343+type WinFun0 x
2344+ = forall outer commons grp schemas params from
2345+ . WindowFunction outer commons grp schemas params from x
2346+ -- ^ cannot reference aliases
2347+
2348+type WinFun1 x y
2349+ = forall outer commons grp schemas params from
2350+ . Expression outer commons grp schemas params from x
2351+ -- ^ input
2352+ -> WindowFunction outer commons grp schemas params from y
2353+ -- ^ output
2354+
2355+type WinFunHet xs y
2356+ = forall outer commons grp schemas params from
2357+ . NP (Expression outer commons grp schemas params from) xs
2358+ -- ^ inputs
2359+ -> WindowFunction outer commons grp schemas params from y
2360+ -- ^ output
2361+
2308−unsafeWindowFunction1
2362+unsafeWindowFunction1 :: ByteString -> WinFun1 x y
2309− :: ByteString
2310− -> Expression outer grp commons schemas params from ty0
2311− -> WindowFunction outer grp commons schemas params from ty1
2312unsafeWindowFunction1 fun x
2363unsafeWindowFunction1 fun x
2313 = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x)
2364 = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x)
2314
2365
2315−unsafeWindowFunction2
2366+unsafeWindowFunctionHet :: SOP.SListI xs => ByteString -> WinFunHet xs y
2316− :: ByteString
2367+unsafeWindowFunctionHet fun xs = UnsafeWindowFunction $ fun <>
2317− -> (Expression outer grp commons schemas params from :*: Expression outer grp commons schemas params from) ty0
2318− -> WindowFunction outer grp commons schemas params from ty1
2319−unsafeWindowFunction2 fun (x :*: x')
2320− = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x <> ", " <> renderSQL x')
2368+ parenthesized (renderCommaSeparated renderSQL xs)
2321
2369
2322{- | rank of the current row with gaps; same as `rowNumber` of its first peer
2370{- | rank of the current row with gaps; same as `rowNumber` of its first peer
2323>>> printSQL rank
2371>>> printSQL rank
2324rank()
2372rank()
2325-}
2373-}
2326−rank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2374+rank :: WinFun0 ('NotNull 'PGint8)
2327rank = UnsafeWindowFunction "rank()"
2375rank = UnsafeWindowFunction "rank()"
2328
2376
2329{- | number of the current row within its partition, counting from 1
2377{- | number of the current row within its partition, counting from 1
2330>>> printSQL rowNumber
2378>>> printSQL rowNumber
2331row_number()
2379row_number()
2332-}
2380-}
2333−rowNumber :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2381+rowNumber :: WinFun0 ('NotNull 'PGint8)
2334rowNumber = UnsafeWindowFunction "row_number()"
2382rowNumber = UnsafeWindowFunction "row_number()"
2335
2383
2336{- | rank of the current row without gaps; this function counts peer groups
2384{- | rank of the current row without gaps; this function counts peer groups
2337>>> printSQL denseRank
2385>>> printSQL denseRank
2338dense_rank()
2386dense_rank()
2339-}
2387-}
2340−denseRank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2388+denseRank :: WinFun0 ('NotNull 'PGint8)
2341denseRank = UnsafeWindowFunction "dense_rank()"
2389denseRank = UnsafeWindowFunction "dense_rank()"
2342
2390
2343{- | relative rank of the current row: (rank - 1) / (total partition rows - 1)
2391{- | relative rank of the current row: (rank - 1) / (total partition rows - 1)
2344>>> printSQL percentRank
2392>>> printSQL percentRank
2345percent_rank()
2393percent_rank()
2346-}
2394-}
2347−percentRank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGfloat8)
2395+percentRank :: WinFun0 ('NotNull 'PGfloat8)
2348percentRank = UnsafeWindowFunction "percent_rank()"
2396percentRank = UnsafeWindowFunction "percent_rank()"
2349
2397
2350{- | cumulative distribution: (number of partition rows
2398{- | cumulative distribution: (number of partition rows
@@ -2352,16 +2400,13 @@ preceding or peer with current row) / total partition rows
2352>>> printSQL cumeDist
2400>>> printSQL cumeDist
2353cume_dist()
2401cume_dist()
2354-}
2402-}
2355−cumeDist :: WindowFunction outer grp commons schemas params from ('NotNull 'PGfloat8)
2403+cumeDist :: WinFun0 ('NotNull 'PGfloat8)
2356cumeDist = UnsafeWindowFunction "cume_dist()"
2404cumeDist = UnsafeWindowFunction "cume_dist()"
2357
2405
2358{- | integer ranging from 1 to the argument value,
2406{- | integer ranging from 1 to the argument value,
2359dividing the partition as equally as possible
2407dividing the partition as equally as possible
2360-}
2408-}
2361−ntile
2409+ntile :: WinFun1 ('NotNull 'PGint4) ('NotNull 'PGint4)
2362− :: Expression outer grp commons schemas params from ('NotNull 'PGint4)
2363− -- ^ num buckets
2364− -> WindowFunction outer grp commons schemas params from ('NotNull 'PGint4)
2365ntile = unsafeWindowFunction1 "ntile"
2410ntile = unsafeWindowFunction1 "ntile"
2366
2411
2367{- | returns value evaluated at the row that is offset rows before the current
2412{- | returns value evaluated at the row that is offset rows before the current
@@ -2369,188 +2414,110 @@ row within the partition; if there is no such row, instead return default
2369(which must be of the same type as value). Both offset and default are
2414(which must be of the same type as value). Both offset and default are
2370evaluated with respect to the current row.
2415evaluated with respect to the current row.
2371-}
2416-}
2372−lag
2417+lag :: WinFunHet '[ty, 'NotNull 'PGint4, ty] ty
2373− :: Expression outer grp commons schemas params from ty
2418+lag = unsafeWindowFunctionHet "lag"
2374− -- ^ value
2375− -> Expression outer grp commons schemas params from ('NotNull 'PGint4)
2376− -- ^ offset
2377− -> Expression outer grp commons schemas params from ty
2378− -- ^ default
2379− -> WindowFunction outer grp commons schemas params from ty
2380−lag value offset def = UnsafeWindowFunction $ "lag"
2381− <> parenthesized
2382− (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
2383
2419
2384{- | returns value evaluated at the row that is offset rows after the current
2420{- | returns value evaluated at the row that is offset rows after the current
2385row within the partition; if there is no such row, instead return default
2421row within the partition; if there is no such row, instead return default
2386(which must be of the same type as value). Both offset and default are
2422(which must be of the same type as value). Both offset and default are
2387evaluated with respect to the current row.
2423evaluated with respect to the current row.
2388-}
2424-}
2389−lead
2425+lead :: WinFunHet '[ty, 'NotNull 'PGint4, ty] ty
2390− :: Expression outer grp commons schemas params from ty
2426+lead = unsafeWindowFunctionHet "lag"
2391− -- ^ value
2392− -> Expression outer grp commons schemas params from ('NotNull 'PGint4)
2393− -- ^ offset
2394− -> Expression outer grp commons schemas params from ty
2395− -- ^ default
2396− -> WindowFunction outer grp commons schemas params from ty
2397−lead value offset def = UnsafeWindowFunction $ "lag"
2398− <> parenthesized
2399− (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
2400
2427
2401{- | returns value evaluated at the row that is the
2428{- | returns value evaluated at the row that is the
2402first row of the window frame
2429first row of the window frame
2403-}
2430-}
2404−firstValue
2431+firstValue :: WinFun1 ty ty
2405− :: Expression outer grp commons schemas params from ty
2406− -- ^ value
2407− -> WindowFunction outer grp commons schemas params from ty
2408firstValue = unsafeWindowFunction1 "first_value"
2432firstValue = unsafeWindowFunction1 "first_value"
2409
2433
2410{- | returns value evaluated at the row that is the
2434{- | returns value evaluated at the row that is the
2411last row of the window frame
2435last row of the window frame
2412-}
2436-}
2413−lastValue
2437+lastValue :: WinFun1 ty ty
2414− :: Expression outer grp commons schemas params from ty
2415− -- ^ value
2416− -> WindowFunction outer grp commons schemas params from ty
2417lastValue = unsafeWindowFunction1 "last_value"
2438lastValue = unsafeWindowFunction1 "last_value"
2418
2439
2419{- | returns value evaluated at the row that is the nth
2440{- | returns value evaluated at the row that is the nth
2420row of the window frame (counting from 1); null if no such row
2441row of the window frame (counting from 1); null if no such row
2421-}
2442-}
2422−nthValue
2443+nthValue :: WinFunHet '[null ty, 'NotNull 'PGint4] ('Null ty)
2423− :: Expression outer grp commons schemas params from (nullity ty)
2424− -- ^ value
2425− -> Expression outer grp commons schemas params from ('NotNull 'PGint4)
2426− -- ^ nth
2427− -> WindowFunction outer grp commons schemas params from ('Null ty)
2428−nthValue value nth = UnsafeWindowFunction $ "nth_value"
2444+nthValue = unsafeWindowFunctionHet "nth_value"
2429− <> parenthesized (commaSeparated [renderSQL value, renderSQL nth])
2430
2445
2431{-|
2446{-|
2432Create date from year, month and day fields
2447Create date from year, month and day fields
2433
2448
2434−>>> printSQL (makeDate 1984 7 3)
2449+>>> printSQL (makeDate (1984 :* 7 *: 3))
2435make_date(1984, 7, 3)
2450make_date(1984, 7, 3)
2436-}
2451-}
2437−makeDate
2452+makeDate :: FunctionHet
2438− :: Expression outer grp commons schemas params from (nullity 'PGint4)
2453+ '[ null 'PGint4, null 'PGint4, null 'PGint4 ]
2439− -- ^ year
2454+ ( null 'PGdate )
2440− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2441− -- ^ month
2442− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2443− -- ^ day
2444− -> Expression outer grp commons schemas params from (nullity 'PGdate)
2445−makeDate y m d = UnsafeExpression $ "make_date" <>
2455+makeDate = unsafeFunctionHet "make_date"
2446− parenthesized (renderCommaSeparated renderSQL (y :* m :* d :* Nil))
2447
2456
2448{-|
2457{-|
2449Create time from hour, minute and seconds fields
2458Create time from hour, minute and seconds fields
2450
2459
2451−>>> printSQL (makeTime 8 15 23.5)
2460+>>> printSQL (makeTime (8 :* 15 *: 23.5))
2452make_time(8, 15, 23.5)
2461make_time(8, 15, 23.5)
2453-}
2462-}
2454−makeTime
2463+makeTime :: FunctionHet
2455− :: Expression outer grp commons schemas params from (nullity 'PGint4)
2464+ '[ null 'PGint4, null 'PGint4, null 'PGfloat8 ]
2456− -- ^ hour
2465+ ( null 'PGtime )
2457− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2458− -- ^ min
2459− -> Expression outer grp commons schemas params from (nullity 'PGfloat8)
2460− -- ^ sec
2461− -> Expression outer grp commons schemas params from (nullity 'PGtime)
2462−makeTime h m s = UnsafeExpression $ "make_time" <>
2466+makeTime = unsafeFunctionHet "make_time"
2463− parenthesized (renderCommaSeparated renderSQL (h :* m :* s :* Nil))
2464
2467
2465{-|
2468{-|
2466Create timestamp from year, month, day, hour, minute and seconds fields
2469Create timestamp from year, month, day, hour, minute and seconds fields
2467
2470
2468−>>> printSQL (makeTimestamp 2013 7 15 8 15 23.5)
2471+>>> printSQL (makeTimestamp (2013 :* 7 :* 15 :* 8 :* 15 *: 23.5))
2469make_timestamp(2013, 7, 15, 8, 15, 23.5)
2472make_timestamp(2013, 7, 15, 8, 15, 23.5)
2470-}
2473-}
2471−makeTimestamp
2474+makeTimestamp :: FunctionHet
2472− :: Expression outer grp commons schemas params from (nullity 'PGint4)
2475+ '[ null 'PGint4, null 'PGint4, null 'PGint4
2473− -- ^ year
2476+ , null 'PGint4, null 'PGint4, null 'PGfloat8 ]
2474− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2477+ ( null 'PGtimestamp )
2475− -- ^ month
2476− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2477− -- ^ day
2478− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2479− -- ^ hour
2480− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2481− -- ^ min
2482− -> Expression outer grp commons schemas params from (nullity 'PGfloat8)
2483− -- ^ sec
2484− -> Expression outer grp commons schemas params from (nullity 'PGtimestamp)
2485−makeTimestamp y mon d h m s =
2478+makeTimestamp = unsafeFunctionHet "make_timestamp"
2486− UnsafeExpression $ "make_timestamp" <> parenthesized
2487− (renderCommaSeparated renderSQL (y :* mon :* d :* h :* m :* s :* Nil))
2488
2479
2489{-|
2480{-|
2490Create timestamp with time zone from
2481Create timestamp with time zone from
2491year, month, day, hour, minute and seconds fields;
2482year, month, day, hour, minute and seconds fields;
2492the current time zone is used
2483the current time zone is used
2493
2484
2494−>>> printSQL (makeTimestamptz 2013 7 15 8 15 23.5)
2485+>>> printSQL (makeTimestamptz (2013 :* 7 :* 15 :* 8 :* 15 *: 23.5))
2495make_timestamptz(2013, 7, 15, 8, 15, 23.5)
2486make_timestamptz(2013, 7, 15, 8, 15, 23.5)
2496-}
2487-}
2497−makeTimestamptz
2488+makeTimestamptz :: FunctionHet
2498− :: Expression outer grp commons schemas params from (nullity 'PGint4)
2489+ '[ null 'PGint4, null 'PGint4, null 'PGint4
2499− -- ^ year
2490+ , null 'PGint4, null 'PGint4, null 'PGfloat8 ]
2500− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2491+ ( null 'PGtimestamptz )
2501− -- ^ month
2502− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2503− -- ^ day
2504− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2505− -- ^ hour
2506− -> Expression outer grp commons schemas params from (nullity 'PGint4)
2507− -- ^ min
2508− -> Expression outer grp commons schemas params from (nullity 'PGfloat8)
2509− -- ^ sec
2510− -> Expression outer grp commons schemas params from (nullity 'PGtimestamptz)
2511−makeTimestamptz y mon d h m s =
2492+makeTimestamptz = unsafeFunctionHet "make_timestamptz"
2512− UnsafeExpression $ "make_timestamptz" <> parenthesized
2513− (renderCommaSeparated renderSQL (y :* mon :* d :* h :* m :* s :* Nil))
2514
2493
2515{-|
2494{-|
2516Affine space operations on time types.
2495Affine space operations on time types.
2517-}
2496-}
2518class TimeOp time diff | time -> diff where
2497class TimeOp time diff | time -> diff where
2519 {-|
2498 {-|
2520− >>> printSQL (makeDate 1984 7 3 !+ 365)
2499+ >>> printSQL (makeDate (1984 :* 7 *: 3) !+ 365)
2521 (make_date(1984, 7, 3) + 365)
2500 (make_date(1984, 7, 3) + 365)
2522 -}
2501 -}
2523− (!+)
2502+ (!+) :: Operator (null time) (null diff) (null time)
2524− :: Expression outer grp commons schemas params from (nullity time)
2525− -> Expression outer grp commons schemas params from (nullity diff)
2526− -> Expression outer grp commons schemas params from (nullity time)
2527 (!+) = unsafeBinaryOp "+"
2503 (!+) = unsafeBinaryOp "+"
2528 {-|
2504 {-|
2529− >>> printSQL (365 +! makeDate 1984 7 3)
2505+ >>> printSQL (365 +! makeDate (1984 :* 7 *: 3))
2530 (365 + make_date(1984, 7, 3))
2506 (365 + make_date(1984, 7, 3))
2531 -}
2507 -}
2532− (+!)
2508+ (+!) :: Operator (null diff) (null time) (null time)
2533− :: Expression outer grp commons schemas params from (nullity diff)
2534− -> Expression outer grp commons schemas params from (nullity time)
2535− -> Expression outer grp commons schemas params from (nullity time)
2536 (+!) = unsafeBinaryOp "+"
2509 (+!) = unsafeBinaryOp "+"
2537 {-|
2510 {-|
2538− >>> printSQL (makeDate 1984 7 3 !- 365)
2511+ >>> printSQL (makeDate (1984 :* 7 *: 3) !- 365)
2539 (make_date(1984, 7, 3) - 365)
2512 (make_date(1984, 7, 3) - 365)
2540 -}
2513 -}
2541− (!-)
2514+ (!-) :: Operator (null time) (null diff) (null time)
2542− :: Expression outer grp commons schemas params from (nullity time)
2543− -> Expression outer grp commons schemas params from (nullity diff)
2544− -> Expression outer grp commons schemas params from (nullity time)
2545 (!-) = unsafeBinaryOp "-"
2515 (!-) = unsafeBinaryOp "-"
2546 {-|
2516 {-|
2547− >>> printSQL (makeDate 1984 7 3 !-! currentDate)
2517+ >>> printSQL (makeDate (1984 :* 7 *: 3) !-! currentDate)
2548 (make_date(1984, 7, 3) - CURRENT_DATE)
2518 (make_date(1984, 7, 3) - CURRENT_DATE)
2549 -}
2519 -}
2550− (!-!)
2520+ (!-!) :: Operator (null time) (null time) (null diff)
2551− :: Expression outer grp commons schemas params from (nullity time)
2552− -> Expression outer grp commons schemas params from (nullity time)
2553− -> Expression outer grp commons schemas params from (nullity diff)
2554 (!-!) = unsafeBinaryOp "-"
2521 (!-!) = unsafeBinaryOp "-"
2555instance TimeOp 'PGtimestamp 'PGinterval
2522instance TimeOp 'PGtimestamp 'PGinterval
2556instance TimeOp 'PGtimestamptz 'PGinterval
2523instance TimeOp 'PGtimestamptz 'PGinterval
@@ -2562,3 +2529,30 @@ infixl 6 !+
2562infixl 6 +!
2529infixl 6 +!
2563infixl 6 !-
2530infixl 6 !-
2564infixl 6 !-!
2531infixl 6 !-!
2532+
2533+data TimeUnit
2534+ = Years | Months | Weeks | Days
2535+ | Hours | Minutes | Seconds
2536+ | Microseconds | Milliseconds
2537+ | Decades | Centuries | Millennia
2538+ deriving (Eq, Ord, Show, Read, Enum, GHC.Generic)
2539+instance RenderSQL TimeUnit where
2540+ renderSQL = \case
2541+ Years -> "years"
2542+ Months -> "months"
2543+ Weeks -> "weeks"
2544+ Days -> "days"
2545+ Hours -> "hours"
2546+ Minutes -> "minutes"
2547+ Seconds -> "seconds"
2548+ Microseconds -> "microseconds"
2549+ Milliseconds -> "milliseconds"
2550+ Decades -> "decades"
2551+ Centuries -> "centuries"
2552+ Millennia -> "millennia"
2553+
2554+-- | >>> printSQL $ interval_ 7 Days
2555+-- (INTERVAL '7.0 days')
2556+interval_ :: Double -> TimeUnit -> Expr (null 'PGinterval)
2557+interval_ num unit = UnsafeExpression . parenthesized $ "INTERVAL" <+>
2558+ "'" <> fromString (show num) <+> renderSQL unit <> "'"
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+11−11
@@ -269,7 +269,7 @@ data QueryClause commons schemas params columns where
269 Select
269 Select
270 :: SOP.SListI columns
270 :: SOP.SListI columns
271 => NP (ColumnExpression grp schemas params from) columns
271 => NP (ColumnExpression grp schemas params from) columns
272− -> TableExpression '[] grp commons schemas params from
272+ -> TableExpression '[] commons grp schemas params from
273 -> QueryClause commons schemas params columns
273 -> QueryClause commons schemas params columns
274 Subquery
274 Subquery
275 :: ColumnsToRow columns ~ row
275 :: ColumnsToRow columns ~ row
@@ -320,17 +320,17 @@ data ColumnExpression grp schemas params from column where
320 => Alias col
320 => Alias col
321 -> ColumnExpression grp schemas params from (col ::: 'Def :=> ty)
321 -> ColumnExpression grp schemas params from (col ::: 'Def :=> ty)
322 Specific
322 Specific
323− :: Aliased (Expression '[] grp '[] schemas params from) (col ::: ty)
323+ :: Aliased (Expression '[] '[] grp schemas params from) (col ::: ty)
324 -> ColumnExpression grp schemas params from (col ::: defness :=> ty)
324 -> ColumnExpression grp schemas params from (col ::: defness :=> ty)
325
325
326instance (KnownSymbol col, column ~ (col ::: defness :=> ty))
326instance (KnownSymbol col, column ~ (col ::: defness :=> ty))
327 => Aliasable col
327 => Aliasable col
328− (Expression '[] grp '[] schemas params from ty)
328+ (Expression '[] '[] grp schemas params from ty)
329 (ColumnExpression grp schemas params from column) where
329 (ColumnExpression grp schemas params from column) where
330 expression `as` col = Specific (expression `As` col)
330 expression `as` col = Specific (expression `As` col)
331instance (KnownSymbol col, columns ~ '[col ::: defness :=> ty])
331instance (KnownSymbol col, columns ~ '[col ::: defness :=> ty])
332 => Aliasable col
332 => Aliasable col
333− (Expression '[] grp '[] schemas params from ty)
333+ (Expression '[] '[] grp schemas params from ty)
334 (NP (ColumnExpression grp schemas params from) columns) where
334 (NP (ColumnExpression grp schemas params from) columns) where
335 expression `as` col = expression `as` col :* Nil
335 expression `as` col = expression `as` col :* Nil
336instance (KnownSymbol col, column ~ (col ::: 'Def :=> ty))
336instance (KnownSymbol col, column ~ (col ::: 'Def :=> ty))
@@ -407,7 +407,7 @@ instance RenderSQL (ColumnExpression grp schemas params from column) where
407-- in the row. Use @Returning Nil@ in the common case where no return
407-- in the row. Use @Returning Nil@ in the common case where no return
408-- values are desired.
408-- values are desired.
409newtype ReturningClause commons schemas params from row =
409newtype ReturningClause commons schemas params from row =
410− Returning (Selection '[] 'Ungrouped commons schemas params from row)
410+ Returning (Selection '[] commons 'Ungrouped schemas params from row)
411
411
412instance RenderSQL (ReturningClause commons schemas params from row) where
412instance RenderSQL (ReturningClause commons schemas params from row) where
413 renderSQL = \case
413 renderSQL = \case
@@ -416,7 +416,7 @@ instance RenderSQL (ReturningClause commons schemas params from row) where
416
416
417pattern Returning_
417pattern Returning_
418 :: SOP.SListI row
418 :: SOP.SListI row
419− => NP (Aliased (Expression '[] 'Ungrouped commons schemas params from)) row
419+ => NP (Aliased (Expression '[] commons 'Ungrouped schemas params from)) row
420 -> ReturningClause commons schemas params from row
420 -> ReturningClause commons schemas params from row
421pattern Returning_ list = Returning (List list)
421pattern Returning_ list = Returning (List list)
422
422
@@ -449,7 +449,7 @@ data ConflictAction tab commons schemas params columns where
449 , SOP.All (HasIn columns) subcolumns
449 , SOP.All (HasIn columns) subcolumns
450 , AllUnique subcolumns )
450 , AllUnique subcolumns )
451 => NP (ColumnExpression 'Ungrouped schemas params '[tab ::: row, "excluded" ::: row]) subcolumns
451 => NP (ColumnExpression 'Ungrouped schemas params '[tab ::: row, "excluded" ::: row]) subcolumns
452− -> [Condition '[] 'Ungrouped commons schemas params '[tab ::: row, "excluded" ::: row]]
452+ -> [Condition '[] commons 'Ungrouped schemas params '[tab ::: row, "excluded" ::: row]]
453 -> ConflictAction tab commons schemas params columns
453 -> ConflictAction tab commons schemas params columns
454
454
455instance RenderSQL (ConflictAction tab commons schemas params columns) where
455instance RenderSQL (ConflictAction tab commons schemas params columns) where
@@ -494,7 +494,7 @@ update
494 => QualifiedAlias sch tab -- ^ table to update
494 => QualifiedAlias sch tab -- ^ table to update
495 -> NP (ColumnExpression 'Ungrouped schemas params '[tab ::: row0]) subcolumns
495 -> NP (ColumnExpression 'Ungrouped schemas params '[tab ::: row0]) subcolumns
496 -- ^ modified values to replace old values
496 -- ^ modified values to replace old values
497− -> Condition '[] 'Ungrouped commons schemas params '[tab ::: row0]
497+ -> Condition '[] commons 'Ungrouped schemas params '[tab ::: row0]
498 -- ^ condition under which to perform update on a row
498 -- ^ condition under which to perform update on a row
499 -> ReturningClause commons schemas params '[tab ::: row0] row1 -- ^ results to return
499 -> ReturningClause commons schemas params '[tab ::: row0] row1 -- ^ results to return
500 -> Manipulation commons schemas params row1
500 -> Manipulation commons schemas params row1
@@ -519,7 +519,7 @@ update_
519 => QualifiedAlias sch tab -- ^ table to update
519 => QualifiedAlias sch tab -- ^ table to update
520 -> NP (ColumnExpression 'Ungrouped schemas params '[tab ::: row]) subcolumns
520 -> NP (ColumnExpression 'Ungrouped schemas params '[tab ::: row]) subcolumns
521 -- ^ modified values to replace old values
521 -- ^ modified values to replace old values
522− -> Condition '[] 'Ungrouped commons schemas params '[tab ::: row]
522+ -> Condition '[] commons 'Ungrouped schemas params '[tab ::: row]
523 -- ^ condition under which to perform update on a row
523 -- ^ condition under which to perform update on a row
524 -> Manipulation commons schemas params '[]
524 -> Manipulation commons schemas params '[]
525update_ tab columns wh = update tab columns wh (Returning_ Nil)
525update_ tab columns wh = update tab columns wh (Returning_ Nil)
@@ -544,7 +544,7 @@ deleteFrom
544 , columns ~ TableToColumns table )
544 , columns ~ TableToColumns table )
545 => QualifiedAlias sch tab -- ^ table to delete from
545 => QualifiedAlias sch tab -- ^ table to delete from
546 -> UsingClause commons schemas params from
546 -> UsingClause commons schemas params from
547− -> Condition '[] 'Ungrouped commons schemas params (tab ::: row0 ': from)
547+ -> Condition '[] commons 'Ungrouped schemas params (tab ::: row0 ': from)
548 -- ^ condition under which to delete a row
548 -- ^ condition under which to delete a row
549 -> ReturningClause commons schemas params '[tab ::: row0] row1 -- ^ results to return
549 -> ReturningClause commons schemas params '[tab ::: row0] row1 -- ^ results to return
550 -> Manipulation commons schemas params row1
550 -> Manipulation commons schemas params row1
@@ -565,7 +565,7 @@ deleteFrom_
565 , row ~ TableToRow table
565 , row ~ TableToRow table
566 , columns ~ TableToColumns table )
566 , columns ~ TableToColumns table )
567 => QualifiedAlias sch tab -- ^ table to delete from
567 => QualifiedAlias sch tab -- ^ table to delete from
568− -> Condition '[] 'Ungrouped commons schemas params '[tab ::: row]
568+ -> Condition '[] commons 'Ungrouped schemas params '[tab ::: row]
569 -- ^ condition under which to delete a row
569 -- ^ condition under which to delete a row
570 -> Manipulation commons schemas params '[]
570 -> Manipulation commons schemas params '[]
571deleteFrom_ tab wh = deleteFrom tab NoUsing wh (Returning_ Nil)
571deleteFrom_ tab wh = deleteFrom tab NoUsing wh (Returning_ Nil)
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+91−97
@@ -403,35 +403,35 @@ q1 `exceptAll` q2 = UnsafeQuery $
403SELECT queries
403SELECT queries
404-----------------------------------------}
404-----------------------------------------}
405
405
406−data Selection outer grp commons schemas params from row where
406+data Selection outer commons grp schemas params from row where
407 List
407 List
408 :: SListI row
408 :: SListI row
409− => NP (Aliased (Expression outer grp commons schemas params from)) row
409+ => NP (Aliased (Expression outer commons grp schemas params from)) row
410− -> Selection outer grp commons schemas params from row
410+ -> Selection outer commons grp schemas params from row
411 Star
411 Star
412 :: HasUnique tab from row
412 :: HasUnique tab from row
413− => Selection outer 'Ungrouped commons schemas params from row
413+ => Selection outer commons 'Ungrouped schemas params from row
414 DotStar
414 DotStar
415 :: Has tab from row
415 :: Has tab from row
416 => Alias tab
416 => Alias tab
417− -> Selection outer 'Ungrouped commons schemas params from row
417+ -> Selection outer commons 'Ungrouped schemas params from row
418 Also
418 Also
419− :: Selection outer grp commons schemas params from right
419+ :: Selection outer commons grp schemas params from right
420− -> Selection outer grp commons schemas params from left
420+ -> Selection outer commons grp schemas params from left
421− -> Selection outer grp commons schemas params from (Join left right)
421+ -> Selection outer commons grp schemas params from (Join left right)
422 Over
422 Over
423 :: SListI row
423 :: SListI row
424− => NP (Aliased (WindowFunction outer grp commons schemas params from)) row
424+ => NP (Aliased (WindowFunction outer commons grp schemas params from)) row
425− -> WindowDefinition outer grp commons schemas params from
425+ -> WindowDefinition outer commons grp schemas params from
426− -> Selection outer grp commons schemas params from row
426+ -> Selection outer commons grp schemas params from row
427instance (KnownSymbol col, row ~ '[col ::: ty])
427instance (KnownSymbol col, row ~ '[col ::: ty])
428 => Aliasable col
428 => Aliasable col
429− (Expression outer grp commons schemas params from ty)
429+ (Expression outer commons grp schemas params from ty)
430− (Selection outer grp commons schemas params from row) where
430+ (Selection outer commons grp schemas params from row) where
431 expr `as` col = List (expr `as` col)
431 expr `as` col = List (expr `as` col)
432instance (Has tab (Join outer from) row0, Has col row0 ty, row1 ~ '[col ::: ty])
432instance (Has tab (Join outer from) row0, Has col row0 ty, row1 ~ '[col ::: ty])
433 => IsQualified tab col
433 => IsQualified tab col
434− (Selection outer 'Ungrouped commons schemas params from row1) where
434+ (Selection outer commons 'Ungrouped schemas params from row1) where
435 tab ! col = tab ! col `as` col
435 tab ! col = tab ! col `as` col
436instance
436instance
437 ( Has tab (Join outer from) row0
437 ( Has tab (Join outer from) row0
@@ -439,11 +439,11 @@ instance
439 , row1 ~ '[col ::: ty]
439 , row1 ~ '[col ::: ty]
440 , GroupedBy tab col bys )
440 , GroupedBy tab col bys )
441 => IsQualified tab col
441 => IsQualified tab col
442− (Selection outer ('Grouped bys) commons schemas params from row1) where
442+ (Selection outer commons ('Grouped bys) schemas params from row1) where
443 tab ! col = tab ! col `as` col
443 tab ! col = tab ! col `as` col
444instance (HasUnique tab (Join outer from) row0, Has col row0 ty, row1 ~ '[col ::: ty])
444instance (HasUnique tab (Join outer from) row0, Has col row0 ty, row1 ~ '[col ::: ty])
445 => IsLabel col
445 => IsLabel col
446− (Selection outer 'Ungrouped commons schemas params from row1) where
446+ (Selection outer commons 'Ungrouped schemas params from row1) where
447 fromLabel = fromLabel @col `as` Alias
447 fromLabel = fromLabel @col `as` Alias
448instance
448instance
449 ( HasUnique tab (Join outer from) row0
449 ( HasUnique tab (Join outer from) row0
@@ -451,10 +451,10 @@ instance
451 , row1 ~ '[col ::: ty]
451 , row1 ~ '[col ::: ty]
452 , GroupedBy tab col bys )
452 , GroupedBy tab col bys )
453 => IsLabel col
453 => IsLabel col
454− (Selection outer ('Grouped bys) commons schemas params from row1) where
454+ (Selection outer commons ('Grouped bys) schemas params from row1) where
455 fromLabel = fromLabel @col `as` Alias
455 fromLabel = fromLabel @col `as` Alias
456
456
457−instance RenderSQL (Selection outer grp commons schemas params from row) where
457+instance RenderSQL (Selection outer commons grp schemas params from row) where
458 renderSQL = \case
458 renderSQL = \case
459 List list -> renderCommaSeparated (renderAliased renderSQL) list
459 List list -> renderCommaSeparated (renderAliased renderSQL) list
460 Star -> "*"
460 Star -> "*"
@@ -463,7 +463,7 @@ instance RenderSQL (Selection outer grp commons schemas params from row) where
463 Over winFns winDef ->
463 Over winFns winDef ->
464 let
464 let
465 renderOver
465 renderOver
466− :: Aliased (WindowFunction outer grp commons schemas params from) field
466+ :: Aliased (WindowFunction outer commons grp schemas params from) field
467 -> ByteString
467 -> ByteString
468 renderOver (winFn `As` col) = renderSQL winFn
468 renderOver (winFn `As` col) = renderSQL winFn
469 <+> "OVER" <+> parenthesized (renderSQL winDef)
469 <+> "OVER" <+> parenthesized (renderSQL winDef)
@@ -472,7 +472,7 @@ instance RenderSQL (Selection outer grp commons schemas params from row) where
472 renderCommaSeparated renderOver winFns
472 renderCommaSeparated renderOver winFns
473
473
474instance IsString
474instance IsString
475− (Selection outer grp commons schemas params from '["fromOnly" ::: 'NotNull 'PGtext]) where
475+ (Selection outer commons grp schemas params from '["fromOnly" ::: 'NotNull 'PGtext]) where
476 fromString str = fromString str `as` Alias
476 fromString str = fromString str `as` Alias
477
477
478-- | the `TableExpression` in the `select` command constructs an intermediate
478-- | the `TableExpression` in the `select` command constructs an intermediate
@@ -482,9 +482,9 @@ instance IsString
482-- the intermediate table are actually output.
482-- the intermediate table are actually output.
483select
483select
484 :: (SListI row, row ~ (x ': xs))
484 :: (SListI row, row ~ (x ': xs))
485− => Selection outer grp commons schemas params from row
485+ => Selection outer commons grp schemas params from row
486 -- ^ select list
486 -- ^ select list
487− -> TableExpression outer grp commons schemas params from
487+ -> TableExpression outer commons grp schemas params from
488 -- ^ intermediate virtual table
488 -- ^ intermediate virtual table
489 -> Query outer commons schemas params row
489 -> Query outer commons schemas params row
490select selection tabexpr = UnsafeQuery $
490select selection tabexpr = UnsafeQuery $
@@ -494,8 +494,8 @@ select selection tabexpr = UnsafeQuery $
494
494
495select_
495select_
496 :: (SListI row, row ~ (x ': xs))
496 :: (SListI row, row ~ (x ': xs))
497− => NP (Aliased (Expression outer grp commons schemas params from)) row
497+ => NP (Aliased (Expression outer commons grp schemas params from)) row
498− -> TableExpression outer grp commons schemas params from
498+ -> TableExpression outer commons grp schemas params from
499 -> Query outer commons schemas params row
499 -> Query outer commons schemas params row
500select_ list = select (List list)
500select_ list = select (List list)
501
501
@@ -503,9 +503,9 @@ select_ list = select (List list)
503-- be subject to the elimination of duplicate rows using `selectDistinct`.
503-- be subject to the elimination of duplicate rows using `selectDistinct`.
504selectDistinct
504selectDistinct
505 :: (SListI columns, columns ~ (col ': cols))
505 :: (SListI columns, columns ~ (col ': cols))
506− => Selection outer 'Ungrouped commons schemas params from columns
506+ => Selection outer commons 'Ungrouped schemas params from columns
507 -- ^ select list
507 -- ^ select list
508− -> TableExpression outer 'Ungrouped commons schemas params from
508+ -> TableExpression outer commons 'Ungrouped schemas params from
509 -- ^ intermediate virtual table
509 -- ^ intermediate virtual table
510 -> Query outer commons schemas params columns
510 -> Query outer commons schemas params columns
511selectDistinct selection tabexpr = UnsafeQuery $
511selectDistinct selection tabexpr = UnsafeQuery $
@@ -515,9 +515,9 @@ selectDistinct selection tabexpr = UnsafeQuery $
515
515
516selectDistinct_
516selectDistinct_
517 :: (SListI columns, columns ~ (col ': cols))
517 :: (SListI columns, columns ~ (col ': cols))
518− => NP (Aliased (Expression outer 'Ungrouped commons schemas params from)) columns
518+ => NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns
519 -- ^ select list
519 -- ^ select list
520− -> TableExpression outer 'Ungrouped commons schemas params from
520+ -> TableExpression outer commons 'Ungrouped schemas params from
521 -- ^ intermediate virtual table
521 -- ^ intermediate virtual table
522 -> Query outer commons schemas params columns
522 -> Query outer commons schemas params columns
523selectDistinct_ list = select (List list)
523selectDistinct_ list = select (List list)
@@ -533,8 +533,8 @@ selectDistinct_ list = select (List list)
533-- SELECT * FROM (VALUES (1, E'one')) AS t ("a", "b")
533-- SELECT * FROM (VALUES (1, E'one')) AS t ("a", "b")
534values
534values
535 :: SListI cols
535 :: SListI cols
536− => NP (Aliased (Expression outer 'Ungrouped commons schemas params '[] )) cols
536+ => NP (Aliased (Expression outer commons 'Ungrouped schemas params '[] )) cols
537− -> [NP (Aliased (Expression outer 'Ungrouped commons schemas params '[] )) cols]
537+ -> [NP (Aliased (Expression outer commons 'Ungrouped schemas params '[] )) cols]
538 -- ^ When more than one row is specified, all the rows must
538 -- ^ When more than one row is specified, all the rows must
539 -- must have the same number of elements
539 -- must have the same number of elements
540 -> Query outer commons schemas params cols
540 -> Query outer commons schemas params cols
@@ -548,7 +548,7 @@ values rw rws = UnsafeQuery $ "SELECT * FROM"
548 <+> parenthesized (renderCommaSeparated renderAliasPart rw)
548 <+> parenthesized (renderCommaSeparated renderAliasPart rw)
549 where
549 where
550 renderAliasPart, renderValuePart
550 renderAliasPart, renderValuePart
551− :: Aliased (Expression outer 'Ungrouped commons schemas params '[] ) ty -> ByteString
551+ :: Aliased (Expression outer commons 'Ungrouped schemas params '[] ) ty -> ByteString
552 renderAliasPart (_ `As` name) = renderSQL name
552 renderAliasPart (_ `As` name) = renderSQL name
553 renderValuePart (value `As` _) = renderSQL value
553 renderValuePart (value `As` _) = renderSQL value
554
554
@@ -556,7 +556,7 @@ values rw rws = UnsafeQuery $ "SELECT * FROM"
556-- specified by value expressions.
556-- specified by value expressions.
557values_
557values_
558 :: SListI cols
558 :: SListI cols
559− => NP (Aliased (Expression outer 'Ungrouped commons schemas params '[] )) cols
559+ => NP (Aliased (Expression outer commons 'Ungrouped schemas params '[] )) cols
560 -- ^ one row of values
560 -- ^ one row of values
561 -> Query outer commons schemas params cols
561 -> Query outer commons schemas params cols
562values_ rw = values rw []
562values_ rw = values rw []
@@ -573,8 +573,8 @@ Table Expressions
573-- can be used to modify or combine base tables in various ways.
573-- can be used to modify or combine base tables in various ways.
574data TableExpression
574data TableExpression
575 (outer :: FromType)
575 (outer :: FromType)
576− (grp :: Grouping)
577 (commons :: FromType)
576 (commons :: FromType)
577+ (grp :: Grouping)
578 (schemas :: SchemasType)
578 (schemas :: SchemasType)
579 (params :: [NullityType])
579 (params :: [NullityType])
580 (from :: FromType)
580 (from :: FromType)
@@ -582,7 +582,7 @@ data TableExpression
582 { fromClause :: FromClause outer commons schemas params from
582 { fromClause :: FromClause outer commons schemas params from
583 -- ^ A table reference that can be a table name, or a derived table such
583 -- ^ A table reference that can be a table name, or a derived table such
584 -- as a subquery, a @JOIN@ construct, or complex combinations of these.
584 -- as a subquery, a @JOIN@ construct, or complex combinations of these.
585− , whereClause :: [Condition outer 'Ungrouped commons schemas params from]
585+ , whereClause :: [Condition outer commons 'Ungrouped schemas params from]
586 -- ^ optional search coditions, combined with `.&&`. After the processing
586 -- ^ optional search coditions, combined with `.&&`. After the processing
587 -- of the `fromClause` is done, each row of the derived virtual table
587 -- of the `fromClause` is done, each row of the derived virtual table
588 -- is checked against the search condition. If the result of the
588 -- is checked against the search condition. If the result of the
@@ -598,13 +598,13 @@ data TableExpression
598 -- set of rows having common values into one group row that represents all
598 -- set of rows having common values into one group row that represents all
599 -- rows in the group. This is done to eliminate redundancy in the output
599 -- rows in the group. This is done to eliminate redundancy in the output
600 -- and/or compute aggregates that apply to these groups.
600 -- and/or compute aggregates that apply to these groups.
601− , havingClause :: HavingClause outer grp commons schemas params from
601+ , havingClause :: HavingClause outer commons grp schemas params from
602 -- ^ If a table has been grouped using `groupBy`, but only certain groups
602 -- ^ If a table has been grouped using `groupBy`, but only certain groups
603 -- are of interest, the `havingClause` can be used, much like a
603 -- are of interest, the `havingClause` can be used, much like a
604 -- `whereClause`, to eliminate groups from the result. Expressions in the
604 -- `whereClause`, to eliminate groups from the result. Expressions in the
605 -- `havingClause` can refer both to grouped expressions and to ungrouped
605 -- `havingClause` can refer both to grouped expressions and to ungrouped
606 -- expressions (which necessarily involve an aggregate function).
606 -- expressions (which necessarily involve an aggregate function).
607− , orderByClause :: [SortExpression outer grp commons schemas params from]
607+ , orderByClause :: [SortExpression outer commons grp schemas params from]
608 -- ^ The `orderByClause` is for optional sorting. When more than one
608 -- ^ The `orderByClause` is for optional sorting. When more than one
609 -- `SortExpression` is specified, the later (right) values are used to sort
609 -- `SortExpression` is specified, the later (right) values are used to sort
610 -- rows that are equal according to the earlier (left) values.
610 -- rows that are equal according to the earlier (left) values.
@@ -621,7 +621,7 @@ data TableExpression
621 } deriving (GHC.Generic)
621 } deriving (GHC.Generic)
622
622
623-- | Render a `TableExpression`
623-- | Render a `TableExpression`
624−instance RenderSQL (TableExpression outer grp commons schemas params from) where
624+instance RenderSQL (TableExpression outer commons grp schemas params from) where
625 renderSQL
625 renderSQL
626 (TableExpression frm' whs' grps' hvs' srts' lims' offs') = mconcat
626 (TableExpression frm' whs' grps' hvs' srts' lims' offs') = mconcat
627 [ "FROM ", renderSQL frm'
627 [ "FROM ", renderSQL frm'
@@ -654,15 +654,15 @@ instance RenderSQL (TableExpression outer grp commons schemas params from) where
654-- to match the left-to-right sequencing of their placement in SQL.
654-- to match the left-to-right sequencing of their placement in SQL.
655from
655from
656 :: FromClause outer commons schemas params from -- ^ table reference
656 :: FromClause outer commons schemas params from -- ^ table reference
657− -> TableExpression outer 'Ungrouped commons schemas params from
657+ -> TableExpression outer commons 'Ungrouped schemas params from
658from tab = TableExpression tab [] NoGroups NoHaving [] [] []
658from tab = TableExpression tab [] NoGroups NoHaving [] [] []
659
659
660-- | A `where_` is an endomorphism of `TableExpression`s which adds a
660-- | A `where_` is an endomorphism of `TableExpression`s which adds a
661-- search condition to the `whereClause`.
661-- search condition to the `whereClause`.
662where_
662where_
663− :: Condition outer 'Ungrouped commons schemas params from -- ^ filtering condition
663+ :: Condition outer commons 'Ungrouped schemas params from -- ^ filtering condition
664− -> TableExpression outer grp commons schemas params from
664+ -> TableExpression outer commons grp schemas params from
665− -> TableExpression outer grp commons schemas params from
665+ -> TableExpression outer commons grp schemas params from
666where_ wh rels = rels {whereClause = wh : whereClause rels}
666where_ wh rels = rels {whereClause = wh : whereClause rels}
667
667
668-- | A `groupBy` is a transformation of `TableExpression`s which switches
668-- | A `groupBy` is a transformation of `TableExpression`s which switches
@@ -671,8 +671,8 @@ where_ wh rels = rels {whereClause = wh : whereClause rels}
671groupBy
671groupBy
672 :: SListI bys
672 :: SListI bys
673 => NP (By from) bys -- ^ grouped columns
673 => NP (By from) bys -- ^ grouped columns
674− -> TableExpression outer 'Ungrouped commons schemas params from
674+ -> TableExpression outer commons 'Ungrouped schemas params from
675− -> TableExpression outer ('Grouped bys) commons schemas params from
675+ -> TableExpression outer commons ('Grouped bys) schemas params from
676groupBy bys rels = TableExpression
676groupBy bys rels = TableExpression
677 { fromClause = fromClause rels
677 { fromClause = fromClause rels
678 , whereClause = whereClause rels
678 , whereClause = whereClause rels
@@ -686,9 +686,9 @@ groupBy bys rels = TableExpression
686-- | A `having` is an endomorphism of `TableExpression`s which adds a
686-- | A `having` is an endomorphism of `TableExpression`s which adds a
687-- search condition to the `havingClause`.
687-- search condition to the `havingClause`.
688having
688having
689− :: Condition outer ('Grouped bys) commons schemas params from -- ^ having condition
689+ :: Condition outer commons ('Grouped bys) schemas params from -- ^ having condition
690− -> TableExpression outer ('Grouped bys) commons schemas params from
690+ -> TableExpression outer commons ('Grouped bys) schemas params from
691− -> TableExpression outer ('Grouped bys) commons schemas params from
691+ -> TableExpression outer commons ('Grouped bys) schemas params from
692having hv rels = rels
692having hv rels = rels
693 { havingClause = case havingClause rels of Having hvs -> Having (hv:hvs) }
693 { havingClause = case havingClause rels of Having hvs -> Having (hv:hvs) }
694
694
@@ -699,16 +699,16 @@ instance OrderBy TableExpression where
699-- `limitClause`.
699-- `limitClause`.
700limit
700limit
701 :: Word64 -- ^ limit parameter
701 :: Word64 -- ^ limit parameter
702− -> TableExpression outer grp commons schemas params from
702+ -> TableExpression outer commons grp schemas params from
703− -> TableExpression outer grp commons schemas params from
703+ -> TableExpression outer commons grp schemas params from
704limit lim rels = rels {limitClause = lim : limitClause rels}
704limit lim rels = rels {limitClause = lim : limitClause rels}
705
705
706-- | An `offset` is an endomorphism of `TableExpression`s which adds to the
706-- | An `offset` is an endomorphism of `TableExpression`s which adds to the
707-- `offsetClause`.
707-- `offsetClause`.
708offset
708offset
709 :: Word64 -- ^ offset parameter
709 :: Word64 -- ^ offset parameter
710− -> TableExpression outer grp commons schemas params from
710+ -> TableExpression outer commons grp schemas params from
711− -> TableExpression outer grp commons schemas params from
711+ -> TableExpression outer commons grp schemas params from
712offset off rels = rels {offsetClause = off : offsetClause rels}
712offset off rels = rels {offsetClause = off : offsetClause rels}
713
713
714{-----------------------------------------
714{-----------------------------------------
@@ -717,14 +717,14 @@ JSON stuff
717
717
718unsafeSetOfFunction
718unsafeSetOfFunction
719 :: ByteString
719 :: ByteString
720− -> Expression outer 'Ungrouped commons schemas params '[] ty
720+ -> Expression outer commons 'Ungrouped schemas params '[] ty
721 -> FromClause outer commons schemas params from
721 -> FromClause outer commons schemas params from
722unsafeSetOfFunction fun expr = UnsafeFromClause $
722unsafeSetOfFunction fun expr = UnsafeFromClause $
723 fun <> "(" <> renderSQL expr <> ")"
723 fun <> "(" <> renderSQL expr <> ")"
724
724
725-- | Expand an array to a set of rows
725-- | Expand an array to a set of rows
726unnest
726unnest
727− :: Expression outer 'Ungrouped commons schemas params '[] (nullity ('PGvararray ty))
727+ :: Expression outer commons 'Ungrouped schemas params '[] (nullity ('PGvararray ty))
728 -> FromClause outer commons schemas params '["unnest" ::: '["unnest" ::: ty]]
728 -> FromClause outer commons schemas params '["unnest" ::: '["unnest" ::: ty]]
729unnest = unsafeSetOfFunction "unnest"
729unnest = unsafeSetOfFunction "unnest"
730
730
@@ -733,7 +733,7 @@ unnest = unsafeSetOfFunction "unnest"
733-- >>> printSQL (select Star (from (jsonEach (jsonLit (object ["a" .= "foo", "b" .= "bar"])))))
733-- >>> printSQL (select Star (from (jsonEach (jsonLit (object ["a" .= "foo", "b" .= "bar"])))))
734-- SELECT * FROM json_each(('{"a":"foo","b":"bar"}' :: json))
734-- SELECT * FROM json_each(('{"a":"foo","b":"bar"}' :: json))
735jsonEach
735jsonEach
736− :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
736+ :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json object
737 -> FromClause outer commons schemas params
737 -> FromClause outer commons schemas params
738 '["json_each" ::: '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]]
738 '["json_each" ::: '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]]
739jsonEach = unsafeSetOfFunction "json_each"
739jsonEach = unsafeSetOfFunction "json_each"
@@ -743,7 +743,7 @@ jsonEach = unsafeSetOfFunction "json_each"
743-- >>> printSQL (select Star (from (jsonbEach (jsonbLit (object ["a" .= "foo", "b" .= "bar"])))))
743-- >>> printSQL (select Star (from (jsonbEach (jsonbLit (object ["a" .= "foo", "b" .= "bar"])))))
744-- SELECT * FROM jsonb_each(('{"a":"foo","b":"bar"}' :: jsonb))
744-- SELECT * FROM jsonb_each(('{"a":"foo","b":"bar"}' :: jsonb))
745jsonbEach
745jsonbEach
746− :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
746+ :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
747 -> FromClause outer commons schemas params
747 -> FromClause outer commons schemas params
748 '["jsonb_each" ::: '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]]
748 '["jsonb_each" ::: '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]]
749jsonbEach = unsafeSetOfFunction "jsonb_each"
749jsonbEach = unsafeSetOfFunction "jsonb_each"
@@ -753,7 +753,7 @@ jsonbEach = unsafeSetOfFunction "jsonb_each"
753-- >>> printSQL (select Star (from (jsonEachText (jsonLit (object ["a" .= "foo", "b" .= "bar"])))))
753-- >>> printSQL (select Star (from (jsonEachText (jsonLit (object ["a" .= "foo", "b" .= "bar"])))))
754-- SELECT * FROM json_each_text(('{"a":"foo","b":"bar"}' :: json))
754-- SELECT * FROM json_each_text(('{"a":"foo","b":"bar"}' :: json))
755jsonEachText
755jsonEachText
756− :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
756+ :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json object
757 -> FromClause outer commons schemas params
757 -> FromClause outer commons schemas params
758 '["json_each" ::: '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]]
758 '["json_each" ::: '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]]
759jsonEachText = unsafeSetOfFunction "json_each_text"
759jsonEachText = unsafeSetOfFunction "json_each_text"
@@ -763,7 +763,7 @@ jsonEachText = unsafeSetOfFunction "json_each_text"
763-- >>> printSQL (select Star (from (jsonbEachText (jsonbLit (object ["a" .= "foo", "b" .= "bar"])))))
763-- >>> printSQL (select Star (from (jsonbEachText (jsonbLit (object ["a" .= "foo", "b" .= "bar"])))))
764-- SELECT * FROM jsonb_each_text(('{"a":"foo","b":"bar"}' :: jsonb))
764-- SELECT * FROM jsonb_each_text(('{"a":"foo","b":"bar"}' :: jsonb))
765jsonbEachText
765jsonbEachText
766− :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
766+ :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
767 -> FromClause outer commons schemas params
767 -> FromClause outer commons schemas params
768 '["jsonb_each" ::: '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]]
768 '["jsonb_each" ::: '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]]
769jsonbEachText = unsafeSetOfFunction "jsonb_each_text"
769jsonbEachText = unsafeSetOfFunction "jsonb_each_text"
@@ -773,7 +773,7 @@ jsonbEachText = unsafeSetOfFunction "jsonb_each_text"
773-- >>> printSQL (jsonObjectKeys (jsonLit (object ["a" .= "foo", "b" .= "bar"])))
773-- >>> printSQL (jsonObjectKeys (jsonLit (object ["a" .= "foo", "b" .= "bar"])))
774-- json_object_keys(('{"a":"foo","b":"bar"}' :: json))
774-- json_object_keys(('{"a":"foo","b":"bar"}' :: json))
775jsonObjectKeys
775jsonObjectKeys
776− :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
776+ :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json object
777 -> FromClause outer commons schemas params
777 -> FromClause outer commons schemas params
778 '["json_object_keys" ::: '["json_object_keys" ::: 'NotNull 'PGtext]]
778 '["json_object_keys" ::: '["json_object_keys" ::: 'NotNull 'PGtext]]
779jsonObjectKeys = unsafeSetOfFunction "json_object_keys"
779jsonObjectKeys = unsafeSetOfFunction "json_object_keys"
@@ -783,7 +783,7 @@ jsonObjectKeys = unsafeSetOfFunction "json_object_keys"
783-- >>> printSQL (jsonbObjectKeys (jsonbLit (object ["a" .= "foo", "b" .= "bar"])))
783-- >>> printSQL (jsonbObjectKeys (jsonbLit (object ["a" .= "foo", "b" .= "bar"])))
784-- jsonb_object_keys(('{"a":"foo","b":"bar"}' :: jsonb))
784-- jsonb_object_keys(('{"a":"foo","b":"bar"}' :: jsonb))
785jsonbObjectKeys
785jsonbObjectKeys
786− :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
786+ :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
787 -> FromClause outer commons schemas params
787 -> FromClause outer commons schemas params
788 '["jsonb_object_keys" ::: '["jsonb_object_keys" ::: 'NotNull 'PGtext]]
788 '["jsonb_object_keys" ::: '["jsonb_object_keys" ::: 'NotNull 'PGtext]]
789jsonbObjectKeys = unsafeSetOfFunction "jsonb_object_keys"
789jsonbObjectKeys = unsafeSetOfFunction "jsonb_object_keys"
@@ -792,7 +792,7 @@ unsafePopulateFunction
792 :: KnownSymbol fun
792 :: KnownSymbol fun
793 => Alias fun
793 => Alias fun
794 -> TypeExpression schemas (nullity ('PGcomposite row))
794 -> TypeExpression schemas (nullity ('PGcomposite row))
795− -> Expression outer 'Ungrouped commons schemas params '[] ty
795+ -> Expression outer commons 'Ungrouped schemas params '[] ty
796 -> FromClause outer commons schemas params '[fun ::: row]
796 -> FromClause outer commons schemas params '[fun ::: row]
797unsafePopulateFunction fun ty expr = UnsafeFromClause $ renderSQL fun
797unsafePopulateFunction fun ty expr = UnsafeFromClause $ renderSQL fun
798 <> parenthesized ("null::" <> renderSQL ty <> ", " <> renderSQL expr)
798 <> parenthesized ("null::" <> renderSQL ty <> ", " <> renderSQL expr)
@@ -801,7 +801,7 @@ unsafePopulateFunction fun ty expr = UnsafeFromClause $ renderSQL fun
801-- type defined by the given table.
801-- type defined by the given table.
802jsonPopulateRecord
802jsonPopulateRecord
803 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
803 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
804− -> Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
804+ -> Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json object
805 -> FromClause outer commons schemas params '["json_populate_record" ::: row]
805 -> FromClause outer commons schemas params '["json_populate_record" ::: row]
806jsonPopulateRecord = unsafePopulateFunction #json_populate_record
806jsonPopulateRecord = unsafePopulateFunction #json_populate_record
807
807
@@ -809,7 +809,7 @@ jsonPopulateRecord = unsafePopulateFunction #json_populate_record
809-- type defined by the given table.
809-- type defined by the given table.
810jsonbPopulateRecord
810jsonbPopulateRecord
811 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
811 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
812− -> Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
812+ -> Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
813 -> FromClause outer commons schemas params '["jsonb_populate_record" ::: row]
813 -> FromClause outer commons schemas params '["jsonb_populate_record" ::: row]
814jsonbPopulateRecord = unsafePopulateFunction #jsonb_populate_record
814jsonbPopulateRecord = unsafePopulateFunction #jsonb_populate_record
815
815
@@ -817,7 +817,7 @@ jsonbPopulateRecord = unsafePopulateFunction #jsonb_populate_record
817-- set of rows whose columns match the record type defined by the given table.
817-- set of rows whose columns match the record type defined by the given table.
818jsonPopulateRecordSet
818jsonPopulateRecordSet
819 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
819 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
820− -> Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json array
820+ -> Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json array
821 -> FromClause outer commons schemas params '["json_populate_record_set" ::: row]
821 -> FromClause outer commons schemas params '["json_populate_record_set" ::: row]
822jsonPopulateRecordSet = unsafePopulateFunction #json_populate_record_set
822jsonPopulateRecordSet = unsafePopulateFunction #json_populate_record_set
823
823
@@ -826,14 +826,14 @@ jsonPopulateRecordSet = unsafePopulateFunction #json_populate_record_set
826-- table.
826-- table.
827jsonbPopulateRecordSet
827jsonbPopulateRecordSet
828 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
828 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
829− -> Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb array
829+ -> Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb array
830 -> FromClause outer commons schemas params '["jsonb_populate_record_set" ::: row]
830 -> FromClause outer commons schemas params '["jsonb_populate_record_set" ::: row]
831jsonbPopulateRecordSet = unsafePopulateFunction #jsonb_populate_record_set
831jsonbPopulateRecordSet = unsafePopulateFunction #jsonb_populate_record_set
832
832
833unsafeRecordFunction
833unsafeRecordFunction
834 :: (SListI record, json `In` PGJsonType)
834 :: (SListI record, json `In` PGJsonType)
835 => ByteString
835 => ByteString
836− -> Expression outer 'Ungrouped commons schemas params '[] (nullity json)
836+ -> Expression outer commons 'Ungrouped schemas params '[] (nullity json)
837 -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
837 -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
838 -> FromClause outer commons schemas params '[tab ::: record]
838 -> FromClause outer commons schemas params '[tab ::: record]
839unsafeRecordFunction fun expr (types `As` tab) = UnsafeFromClause $
839unsafeRecordFunction fun expr (types `As` tab) = UnsafeFromClause $
@@ -846,7 +846,7 @@ unsafeRecordFunction fun expr (types `As` tab) = UnsafeFromClause $
846-- | Builds an arbitrary record from a JSON object.
846-- | Builds an arbitrary record from a JSON object.
847jsonToRecord
847jsonToRecord
848 :: SListI record
848 :: SListI record
849− => Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
849+ => Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json object
850 -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
850 -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
851 -> FromClause outer commons schemas params '[tab ::: record]
851 -> FromClause outer commons schemas params '[tab ::: record]
852jsonToRecord = unsafeRecordFunction "json_to_record"
852jsonToRecord = unsafeRecordFunction "json_to_record"
@@ -854,7 +854,7 @@ jsonToRecord = unsafeRecordFunction "json_to_record"
854-- | Builds an arbitrary record from a binary JSON object.
854-- | Builds an arbitrary record from a binary JSON object.
855jsonbToRecord
855jsonbToRecord
856 :: SListI record
856 :: SListI record
857− => Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
857+ => Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
858 -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
858 -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
859 -> FromClause outer commons schemas params '[tab ::: record]
859 -> FromClause outer commons schemas params '[tab ::: record]
860jsonbToRecord = unsafeRecordFunction "jsonb_to_record"
860jsonbToRecord = unsafeRecordFunction "jsonb_to_record"
@@ -862,7 +862,7 @@ jsonbToRecord = unsafeRecordFunction "jsonb_to_record"
862-- | Builds an arbitrary set of records from a JSON array of objects.
862-- | Builds an arbitrary set of records from a JSON array of objects.
863jsonToRecordSet
863jsonToRecordSet
864 :: SListI record
864 :: SListI record
865− => Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json array
865+ => Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json array
866 -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
866 -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
867 -> FromClause outer commons schemas params '[tab ::: record]
867 -> FromClause outer commons schemas params '[tab ::: record]
868jsonToRecordSet = unsafeRecordFunction "json_to_record_set"
868jsonToRecordSet = unsafeRecordFunction "json_to_record_set"
@@ -870,7 +870,7 @@ jsonToRecordSet = unsafeRecordFunction "json_to_record_set"
870-- | Builds an arbitrary set of records from a binary JSON array of objects.
870-- | Builds an arbitrary set of records from a binary JSON array of objects.
871jsonbToRecordSet
871jsonbToRecordSet
872 :: SListI record
872 :: SListI record
873− => Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb array
873+ => Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb array
874 -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
874 -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
875 -> FromClause outer commons schemas params '[tab ::: record]
875 -> FromClause outer commons schemas params '[tab ::: record]
876jsonbToRecordSet = unsafeRecordFunction "jsonb_to_record_set"
876jsonbToRecordSet = unsafeRecordFunction "jsonb_to_record_set"
@@ -940,7 +940,7 @@ the @on@ condition.
940innerJoin
940innerJoin
941 :: FromClause outer commons schemas params right
941 :: FromClause outer commons schemas params right
942 -- ^ right
942 -- ^ right
943− -> Condition outer 'Ungrouped commons schemas params (Join left right)
943+ -> Condition outer commons 'Ungrouped schemas params (Join left right)
944 -- ^ @on@ condition
944 -- ^ @on@ condition
945 -> FromClause outer commons schemas params left
945 -> FromClause outer commons schemas params left
946 -- ^ left
946 -- ^ left
@@ -957,7 +957,7 @@ innerJoin right on left = UnsafeFromClause $
957leftOuterJoin
957leftOuterJoin
958 :: FromClause outer commons schemas params right
958 :: FromClause outer commons schemas params right
959 -- ^ right
959 -- ^ right
960− -> Condition outer 'Ungrouped commons schemas params (Join left right)
960+ -> Condition outer commons 'Ungrouped schemas params (Join left right)
961 -- ^ @on@ condition
961 -- ^ @on@ condition
962 -> FromClause outer commons schemas params left
962 -> FromClause outer commons schemas params left
963 -- ^ left
963 -- ^ left
@@ -975,7 +975,7 @@ leftOuterJoin right on left = UnsafeFromClause $
975rightOuterJoin
975rightOuterJoin
976 :: FromClause outer commons schemas params right
976 :: FromClause outer commons schemas params right
977 -- ^ right
977 -- ^ right
978− -> Condition outer 'Ungrouped commons schemas params (Join left right)
978+ -> Condition outer commons 'Ungrouped schemas params (Join left right)
979 -- ^ @on@ condition
979 -- ^ @on@ condition
980 -> FromClause outer commons schemas params left
980 -> FromClause outer commons schemas params left
981 -- ^ left
981 -- ^ left
@@ -994,7 +994,7 @@ rightOuterJoin right on left = UnsafeFromClause $
994fullOuterJoin
994fullOuterJoin
995 :: FromClause outer commons schemas params right
995 :: FromClause outer commons schemas params right
996 -- ^ right
996 -- ^ right
997− -> Condition outer 'Ungrouped commons schemas params (Join left right)
997+ -> Condition outer commons 'Ungrouped schemas params (Join left right)
998 -- ^ @on@ condition
998 -- ^ @on@ condition
999 -> FromClause outer commons schemas params left
999 -> FromClause outer commons schemas params left
1000 -- ^ left
1000 -- ^ left
@@ -1067,17 +1067,17 @@ instance RenderSQL (GroupByClause grp from) where
1067-- An `Ungrouped` `TableExpression` may only use `NoHaving` while a `Grouped`
1067-- An `Ungrouped` `TableExpression` may only use `NoHaving` while a `Grouped`
1068-- `TableExpression` must use `Having` whose conditions are combined with
1068-- `TableExpression` must use `Having` whose conditions are combined with
1069-- `.&&`.
1069-- `.&&`.
1070−data HavingClause outer grp commons schemas params from where
1070+data HavingClause outer commons grp schemas params from where
1071− NoHaving :: HavingClause outer 'Ungrouped commons schemas params from
1071+ NoHaving :: HavingClause outer commons 'Ungrouped schemas params from
1072 Having
1072 Having
1073− :: [Condition outer ('Grouped bys) commons schemas params from]
1073+ :: [Condition outer commons ('Grouped bys) schemas params from]
1074− -> HavingClause outer ('Grouped bys) commons schemas params from
1074+ -> HavingClause outer commons ('Grouped bys) schemas params from
1075−deriving instance Show (HavingClause outer grp commons schemas params from)
1075+deriving instance Show (HavingClause outer commons grp schemas params from)
1076−deriving instance Eq (HavingClause outer grp commons schemas params from)
1076+deriving instance Eq (HavingClause outer commons grp schemas params from)
1077−deriving instance Ord (HavingClause outer grp commons schemas params from)
1077+deriving instance Ord (HavingClause outer commons grp schemas params from)
1078
1078
1079-- | Render a `HavingClause`.
1079-- | Render a `HavingClause`.
1080−instance RenderSQL (HavingClause outer grp commons schemas params from) where
1080+instance RenderSQL (HavingClause outer commons grp schemas params from) where
1081 renderSQL = \case
1081 renderSQL = \case
1082 NoHaving -> ""
1082 NoHaving -> ""
1083 Having [] -> ""
1083 Having [] -> ""
@@ -1098,15 +1098,9 @@ at least one row is returned, not all the way to completion.
1098-}
1098-}
1099exists
1099exists
1100 :: Query (Join outer from) commons schemas params row
1100 :: Query (Join outer from) commons schemas params row
1101− -> Condition outer grp commons schemas params from
1101+ -> Condition outer commons grp schemas params from
1102exists query = UnsafeExpression $ "EXISTS" <+> parenthesized (renderSQL query)
1102exists query = UnsafeExpression $ "EXISTS" <+> parenthesized (renderSQL query)
1103
1103
1104−type Operator ty1 ty2 ty3
1105− = forall outer grp commons schemas params from
1106− . Expression outer grp commons schemas params from ty1
1107− -> Expression outer grp commons schemas params from ty2
1108− -> Expression outer grp commons schemas params from ty3
1109−
1110{- |
1104{- |
1111The right-hand side is a parenthesized subquery, which must return
1105The right-hand side is a parenthesized subquery, which must return
1112exactly one column. The left-hand expression is evaluated and compared to each
1106exactly one column. The left-hand expression is evaluated and compared to each
@@ -1121,10 +1115,10 @@ and at least one comparison returns `null_`.
1121(TRUE = ALL (SELECT * FROM (VALUES (TRUE)) AS t ("foo")))
1115(TRUE = ALL (SELECT * FROM (VALUES (TRUE)) AS t ("foo")))
1122-}
1116-}
1123subAll
1117subAll
1124− :: Expression outer grp commons schemas params from ty1 -- ^ expression
1118+ :: Expression outer commons grp schemas params from ty1 -- ^ expression
1125 -> Operator ty1 ty2 ('Null 'PGbool) -- ^ operator
1119 -> Operator ty1 ty2 ('Null 'PGbool) -- ^ operator
1126 -> Query (Join outer from) commons schemas params '[col ::: ty2] -- ^ subquery
1120 -> Query (Join outer from) commons schemas params '[col ::: ty2] -- ^ subquery
1127− -> Condition outer grp commons schemas params from
1121+ -> Condition outer commons grp schemas params from
1128subAll expr (?) qry = expr ?
1122subAll expr (?) qry = expr ?
1129 (UnsafeExpression $ "ALL" <+> parenthesized (renderSQL qry))
1123 (UnsafeExpression $ "ALL" <+> parenthesized (renderSQL qry))
1130
1124
@@ -1139,10 +1133,10 @@ if any `true` result is obtained. The result is `false` if no true result is fou
1139(E'foo' LIKE ALL (SELECT * FROM (VALUES (E'foobar')) AS t ("foo")))
1133(E'foo' LIKE ALL (SELECT * FROM (VALUES (E'foobar')) AS t ("foo")))
1140-}
1134-}
1141subAny
1135subAny
1142− :: Expression outer grp commons schemas params from ty1 -- ^ expression
1136+ :: Expression outer commons grp schemas params from ty1 -- ^ expression
1143 -> Operator ty1 ty2 ('Null 'PGbool) -- ^ operator
1137 -> Operator ty1 ty2 ('Null 'PGbool) -- ^ operator
1144 -> Query (Join outer from) commons schemas params '[col ::: ty2] -- ^ subquery
1138 -> Query (Join outer from) commons schemas params '[col ::: ty2] -- ^ subquery
1145− -> Condition outer grp commons schemas params from
1139+ -> Condition outer commons grp schemas params from
1146subAny expr (?) qry = expr ?
1140subAny expr (?) qry = expr ?
1147 (UnsafeExpression $ "ANY" <+> parenthesized (renderSQL qry))
1141 (UnsafeExpression $ "ANY" <+> parenthesized (renderSQL qry))
1148
1142
@@ -1154,9 +1148,9 @@ to any of the right-hand expressions.
1154TRUE IN (TRUE, FALSE, NULL)
1148TRUE IN (TRUE, FALSE, NULL)
1155-}
1149-}
1156in_
1150in_
1157− :: Expression outer grp commons schemas params from ty -- ^ expression
1151+ :: Expression outer commons grp schemas params from ty -- ^ expression
1158− -> [Expression outer grp commons schemas params from ty]
1152+ -> [Expression outer commons grp schemas params from ty]
1159− -> Condition outer grp commons schemas params from
1153+ -> Condition outer commons grp schemas params from
1160expr `in_` exprs = UnsafeExpression $ renderSQL expr <+> "IN"
1154expr `in_` exprs = UnsafeExpression $ renderSQL expr <+> "IN"
1161 <+> parenthesized (commaSeparated (renderSQL <$> exprs))
1155 <+> parenthesized (commaSeparated (renderSQL <$> exprs))
1162
1156
@@ -1168,9 +1162,9 @@ to any of the right-hand expressions.
1168TRUE NOT IN (FALSE, NULL)
1162TRUE NOT IN (FALSE, NULL)
1169-}
1163-}
1170notIn
1164notIn
1171− :: Expression outer grp commons schemas params from ty -- ^ expression
1165+ :: Expression outer commons grp schemas params from ty -- ^ expression
1172− -> [Expression outer grp commons schemas params from ty]
1166+ -> [Expression outer commons grp schemas params from ty]
1173− -> Condition outer grp commons schemas params from
1167+ -> Condition outer commons grp schemas params from
1174expr `notIn` exprs = UnsafeExpression $ renderSQL expr <+> "NOT IN"
1168expr `notIn` exprs = UnsafeExpression $ renderSQL expr <+> "NOT IN"
1175 <+> parenthesized (commaSeparated (renderSQL <$> exprs))
1169 <+> parenthesized (commaSeparated (renderSQL <$> exprs))
1176
1170
squeal-postgresql/src/Squeal/PostgreSQL/Render.hs+18
@@ -10,23 +10,27 @@ Rendering helper functions.
10
10
11{-# LANGUAGE
11{-# LANGUAGE
12 AllowAmbiguousTypes
12 AllowAmbiguousTypes
13+ , ConstraintKinds
13 , FlexibleContexts
14 , FlexibleContexts
14 , MagicHash
15 , MagicHash
15 , OverloadedStrings
16 , OverloadedStrings
16 , PolyKinds
17 , PolyKinds
17 , RankNTypes
18 , RankNTypes
18 , ScopedTypeVariables
19 , ScopedTypeVariables
20+ , TypeApplications
19#-}
21#-}
20
22
21module Squeal.PostgreSQL.Render
23module Squeal.PostgreSQL.Render
22 ( -- * Render
24 ( -- * Render
23 parenthesized
25 parenthesized
26+ , bracketed
24 , (<+>)
27 , (<+>)
25 , commaSeparated
28 , commaSeparated
26 , doubleQuoted
29 , doubleQuoted
27 , singleQuotedText
30 , singleQuotedText
28 , singleQuotedUtf8
31 , singleQuotedUtf8
29 , renderCommaSeparated
32 , renderCommaSeparated
33+ , renderCommaSeparatedConstraint
30 , renderCommaSeparatedMaybe
34 , renderCommaSeparatedMaybe
31 , renderNat
35 , renderNat
32 , renderSymbol
36 , renderSymbol
@@ -52,6 +56,10 @@ import qualified Data.ByteString.Char8 as Char8
52parenthesized :: ByteString -> ByteString
56parenthesized :: ByteString -> ByteString
53parenthesized str = "(" <> str <> ")"
57parenthesized str = "(" <> str <> ")"
54
58
59+-- | Square bracket a `ByteString`
60+bracketed :: ByteString -> ByteString
61+bracketed str = "[" <> str <> "]"
62+
55-- | Concatenate two `ByteString`s with a space between.
63-- | Concatenate two `ByteString`s with a space between.
56(<+>) :: ByteString -> ByteString -> ByteString
64(<+>) :: ByteString -> ByteString -> ByteString
57infixr 7 <+>
65infixr 7 <+>
@@ -84,6 +92,16 @@ renderCommaSeparated render
84 . hcollapse
92 . hcollapse
85 . hmap (K . render)
93 . hmap (K . render)
86
94
95+-- | Comma separate the renderings of a heterogeneous list.
96+renderCommaSeparatedConstraint
97+ :: forall c xs expression. (All c xs, SListI xs)
98+ => (forall x. c x => expression x -> ByteString)
99+ -> NP expression xs -> ByteString
100+renderCommaSeparatedConstraint render
101+ = commaSeparated
102+ . hcollapse
103+ . hcmap (Proxy @c) (K . render)
104+
87-- | Comma separate the `Maybe` renderings of a heterogeneous list, dropping
105-- | Comma separate the `Maybe` renderings of a heterogeneous list, dropping
88-- `Nothing`s.
106-- `Nothing`s.
89renderCommaSeparatedMaybe
107renderCommaSeparatedMaybe
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+7−34
@@ -91,15 +91,13 @@ module Squeal.PostgreSQL.Schema
91 , Elem
91 , Elem
92 , In
92 , In
93 , Length
93 , Length
94+ , (*:)
94 -- * Type Classifications
95 -- * Type Classifications
95 , HasOid (..)
96 , HasOid (..)
96 , PGNum
97 , PGNum
97 , PGIntegral
98 , PGIntegral
98 , PGFloating
99 , PGFloating
99 , PGTypeOf
100 , PGTypeOf
100− , PGArrayOf
101− , PGArray
102− , PGTextArray
103 , PGJsonType
101 , PGJsonType
104 , PGJsonKey
102 , PGJsonKey
105 , SamePGType
103 , SamePGType
@@ -164,6 +162,8 @@ data PGType
164 | PGfixarray [Nat] NullityType -- ^ fixed length array
162 | PGfixarray [Nat] NullityType -- ^ fixed length array
165 | PGenum [Symbol] -- ^ enumerated (enum) types are data types that comprise a static, ordered set of values.
163 | PGenum [Symbol] -- ^ enumerated (enum) types are data types that comprise a static, ordered set of values.
166 | PGcomposite RowType -- ^ a composite type represents the structure of a row or record; it is essentially just a list of field names and their data types.
164 | PGcomposite RowType -- ^ a composite type represents the structure of a row or record; it is essentially just a list of field names and their data types.
165+ | PGtsvector -- ^ A tsvector value is a sorted list of distinct lexemes, which are words that have been normalized to merge different variants of the same word.
166+ | PGtsquery -- ^ A tsquery value stores lexemes that are to be searched for
167 | UnsafePGType Symbol -- ^ an escape hatch for unsupported PostgreSQL types
167 | UnsafePGType Symbol -- ^ an escape hatch for unsupported PostgreSQL types
168
168
169-- | The object identifier of a `PGType`.
169-- | The object identifier of a `PGType`.
@@ -539,37 +539,6 @@ type PGFloating = '[ 'PGfloat4, 'PGfloat8, 'PGnumeric]
539-- | Integral Postgres types.
539-- | Integral Postgres types.
540type PGIntegral = '[ 'PGint2, 'PGint4, 'PGint8]
540type PGIntegral = '[ 'PGint2, 'PGint4, 'PGint8]
541
541
542−-- | Error message helper for displaying unavailable\/unknown\/placeholder type
543−-- variables whose kind is known.
544−type Placeholder k = 'Text "(_::" :<>: 'ShowType k :<>: 'Text ")"
545−
546−type ErrArrayOf arr ty = arr :<>: 'Text " " :<>: ty
547−type ErrPGfixarrayOf t = ErrArrayOf ('ShowType 'PGfixarray :<>: 'Text " " :<>: Placeholder Nat) t
548−type ErrPGvararrayOf t = ErrArrayOf ('ShowType 'PGvararray) t
549−
550−-- | Ensure a type is a valid array type.
551−type family PGArray name arr :: Constraint where
552− PGArray name ('PGvararray x) = ()
553− PGArray name ('PGfixarray n x) = ()
554− PGArray name val = TypeError
555− ('Text name :<>: 'Text ": Unsatisfied PGArray constraint. Expected either: "
556− :$$: 'Text " • " :<>: ErrPGvararrayOf (Placeholder PGType)
557− :$$: 'Text " • " :<>: ErrPGfixarrayOf (Placeholder PGType)
558− :$$: 'Text "But got: " :<>: 'ShowType val)
559−
560−-- | Ensure a type is a valid array type with a specific element type.
561−type family PGArrayOf name arr ty :: Constraint where
562− PGArrayOf name ('PGvararray x) ty = x ~ ty
563− PGArrayOf name ('PGfixarray n x) ty = x ~ ty
564− PGArrayOf name val ty = TypeError
565− ( 'Text name :<>: 'Text "Unsatisfied PGArrayOf constraint. Expected either: "
566− :$$: 'Text " • " :<>: ErrPGvararrayOf ( 'ShowType ty )
567− :$$: 'Text " • " :<>: ErrPGfixarrayOf ( 'ShowType ty )
568− :$$: 'Text "But got: " :<>: 'ShowType val)
569−
570−-- | Ensure a type is a valid array type whose elements are text.
571−type PGTextArray name arr = PGArrayOf name arr ('NotNull 'PGtext)
572−
573-- | `PGTypeOf` forgets about @NULL@ and any column constraints.
542-- | `PGTypeOf` forgets about @NULL@ and any column constraints.
574type family PGTypeOf (ty :: NullityType) :: PGType where
543type family PGTypeOf (ty :: NullityType) :: PGType where
575 PGTypeOf (nullity pg) = pg
544 PGTypeOf (nullity pg) = pg
@@ -784,3 +753,7 @@ instance (forall t0 t1. RenderSQL (p t0 t1))
784-- | A `single` step.
753-- | A `single` step.
785single :: p x0 x1 -> AlignedList p x0 x1
754single :: p x0 x1 -> AlignedList p x0 x1
786single step = step :>> Done
755single step = step :>> Done
756+
757+-- | A useful operator for ending an `NP` list of length at least 2 without `Nil`.
758+(*:) :: f x -> f y -> NP f '[x,y]
759+x *: y = x :* y :* Nil