@@ -34,6 +34,17 @@ Squeal expressions are the atoms used to build statements.
3434module Squeal.PostgreSQL.Expression
3535 ( -- * Expression
3636 Expression (..)
37+ , Expr
38+ , Operator
39+ , unsafeBinaryOp
40+ , (:-->)
41+ , unsafeUnaryOpL
42+ , unsafeUnaryOpR
43+ , unsafeFunction
44+ , FunctionHom
45+ , unsafeFunctionHom
46+ , FunctionHet
47+ , unsafeFunctionHet
3748 , HasParameter (parameter)
3849 , param
3950 -- ** Null
@@ -47,15 +58,14 @@ module Squeal.PostgreSQL.Expression
4758 , nullIf
4859 -- ** Collections
4960 , array
61+ , array1
62+ , array2
5063 , cardinality
5164 , index
5265 , row
5366 , field
67+ , PGSubset (..)
5468 -- ** Functions
55− , unsafeBinaryOp
56− , unsafeUnaryOp
57− , unsafeFunction
58− , unsafeVariadicFunction
5969 , atan2_
6070 , cast
6171 , quot_
@@ -65,6 +75,7 @@ module Squeal.PostgreSQL.Expression
6575 , ceiling_
6676 , greatest
6777 , least
78+ , litChar
6879 -- ** Conditions
6980 , true
7081 , false
@@ -93,16 +104,19 @@ module Squeal.PostgreSQL.Expression
93104 , isUnknown
94105 , isNotUnknown
95106 -- ** Time
107+ , TimeOp (..)
96108 , currentDate
97109 , currentTime
98110 , currentTimestamp
99111 , localTime
100112 , localTimestamp
101− , TimeOp (..)
113+ , now
102114 , makeDate
103115 , makeTime
104116 , makeTimestamp
105117 , makeTimestamptz
118+ , interval_
119+ , TimeUnit (..)
106120 -- ** Text
107121 , lower
108122 , upper
@@ -116,8 +130,6 @@ module Squeal.PostgreSQL.Expression
116130 , (.#>)
117131 , (.#>>)
118132 -- *** Jsonb operators
119− , (.@>)
120− , (.<@)
121133 , (.?)
122134 , (.?|)
123135 , (.?&)
@@ -132,6 +144,7 @@ module Squeal.PostgreSQL.Expression
132144 , rowToJson
133145 , jsonBuildArray
134146 , jsonbBuildArray
147+ , PGBuildObject
135148 , jsonBuildObject
136149 , jsonbBuildObject
137150 , jsonObject
@@ -151,6 +164,28 @@ module Squeal.PostgreSQL.Expression
151164 , jsonbSet
152165 , jsonbInsert
153166 , 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
154189 -- ** Aggregation
155190 , Aggregate (..)
156191 , Distinction (..)
@@ -211,10 +246,11 @@ module Squeal.PostgreSQL.Expression
211246 , jsonb
212247 , vararray
213248 , fixarray
249+ , tsvector
250+ , tsquery
214251 -- * Re-export
215252 , (&)
216253 , NP (..)
217− , (:*:) (..)
218254 , K (..)
219255 ) where
220256
@@ -223,12 +259,12 @@ import Control.DeepSeq
223259import Data.ByteString (ByteString)
224260import Data.ByteString.Lazy (toStrict)
225261import Data.Function ((&))
262+import Data.Kind
226263import Data.Semigroup hiding (All)
227264import qualified Data.Aeson as JSON
228265import Data.String
229266import Data.Word
230267import Generics.SOP hiding (All, from)
231−import GHC.Generics ((:*:) (..))
232268import GHC.OverloadedLabels
233269import GHC.TypeLits
234270import Numeric
@@ -257,18 +293,82 @@ and other operations.
257293-}
258294newtype Expression
259295 (outer :: FromType)
260− (grp :: Grouping)
261296 (commons :: FromType)
297+ (grp :: Grouping)
262298 (schemas :: SchemasType)
263299 (params :: [NullityType])
264300 (from :: FromType)
265301 (ty :: NullityType)
266302 = UnsafeExpression { renderExpression :: ByteString }
267303 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
270305 renderSQL = renderExpression
271306
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+
272372{- | A `HasParameter` constraint is used to indicate a value that is
273373supplied externally to a SQL statement.
274374`Squeal.PostgreSQL.PQ.manipulateParams`,
@@ -284,12 +384,12 @@ class KnownNat n => HasParameter
284384 | n params -> ty where
285385 -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
286386 --
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)
288388 -- >>> printSQL expr
289389 -- ($1 :: int4)
290390 parameter
291391 :: TypeExpression schemas ty
292− -> Expression outer grp commons schemas params from ty
392+ -> Expression outer commons grp schemas params from ty
293393 parameter ty = UnsafeExpression $ parenthesized $
294394 "$" <> renderNat @n <+> "::"
295395 <+> renderSQL ty
@@ -300,44 +400,44 @@ instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
300400-- | `param` takes a `Nat` using type application and for basic types,
301401-- infers a `TypeExpression`.
302402--
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)
304404-- >>> printSQL expr
305405-- ($1 :: int4)
306406param
307407 :: forall n outer commons schemas params from grp ty
308408 . (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
310410param = parameter @n (pgtype @schemas)
311411
312412instance (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
314414 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
315415instance (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
317417 fromLabel = fromLabel @col :* Nil
318418instance (HasUnique tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
319419 => IsLabel col
320− (Aliased (Expression outer 'Ungrouped commons schemas params from) column) where
420+ (Aliased (Expression outer commons 'Ungrouped schemas params from) column) where
321421 fromLabel = fromLabel @col `As` Alias
322422instance (HasUnique tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
323423 => 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
325425 fromLabel = fromLabel @col :* Nil
326426
327427instance (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
329429 tab ! col = UnsafeExpression $
330430 renderSQL tab <> "." <> renderSQL col
331431instance (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
333433 tab ! col = tab ! col :* Nil
334434instance (Has tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
335435 => 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
337437 tab ! col = tab ! col `As` col
338438instance (Has tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
339439 => 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
341441 tab ! col = tab ! col :* Nil
342442
343443instance
@@ -345,7 +445,7 @@ instance
345445 , Has col row ty
346446 , GroupedBy tab col bys
347447 ) => IsLabel col
348− (Expression outer ('Grouped bys) commons schemas params from ty) where
448+ (Expression outer commons ('Grouped bys) schemas params from ty) where
349449 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
350450instance
351451 ( HasUnique tab (Join outer from) row
@@ -353,7 +453,7 @@ instance
353453 , GroupedBy tab col bys
354454 , tys ~ '[ty]
355455 ) => 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
357457 fromLabel = fromLabel @col :* Nil
358458instance
359459 ( HasUnique tab (Join outer from) row
@@ -361,7 +461,7 @@ instance
361461 , GroupedBy tab col bys
362462 , column ~ (col ::: ty)
363463 ) => 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
365465 fromLabel = fromLabel @col `As` Alias
366466instance
367467 ( HasUnique tab (Join outer from) row
@@ -369,7 +469,7 @@ instance
369469 , GroupedBy tab col bys
370470 , columns ~ '[col ::: ty]
371471 ) => 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
373473 fromLabel = fromLabel @col :* Nil
374474
375475instance
@@ -377,7 +477,7 @@ instance
377477 , Has col row ty
378478 , GroupedBy tab col bys
379479 ) => 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
381481 tab ! col = UnsafeExpression $
382482 renderSQL tab <> "." <> renderSQL col
383483instance
@@ -386,7 +486,7 @@ instance
386486 , GroupedBy tab col bys
387487 , tys ~ '[ty]
388488 ) => 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
390490 tab ! col = tab ! col :* Nil
391491instance
392492 ( Has tab (Join outer from) row
@@ -394,7 +494,7 @@ instance
394494 , GroupedBy tab col bys
395495 , column ~ (col ::: ty)
396496 ) => 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
398498 tab ! col = tab ! col `As` col
399499instance
400500 ( Has tab (Join outer from) row
@@ -402,35 +502,28 @@ instance
402502 , GroupedBy tab col bys
403503 , columns ~ '[col ::: ty]
404504 ) => 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
406506 tab ! col = tab ! col :* Nil
407507
408508-- | analagous to `Nothing`
409509--
410510-- >>> printSQL null_
411511-- NULL
412−null_ :: Expression outer grp commons schemas params from ('Null ty)
512+null_ :: Expr ('Null ty)
413513null_ = UnsafeExpression "NULL"
414514
415515-- | analagous to `Just`
416516--
417517-- >>> printSQL $ notNull true
418518-- TRUE
419−notNull
420− :: Expression outer grp commons schemas params from ('NotNull ty)
421− -> Expression outer grp commons schemas params from ('Null ty)
519+notNull :: 'NotNull ty :--> 'Null ty
422520notNull = UnsafeExpression . renderSQL
423521
424522-- | return the leftmost value which is not NULL
425523--
426524-- >>> printSQL $ coalesce [null_, true] false
427525-- COALESCE(NULL, TRUE, FALSE)
428−coalesce
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)
526+coalesce :: FunctionHom ('Null ty) ('NotNull ty) ('NotNull ty)
434527coalesce nullxs notNullx = UnsafeExpression $
435528 "COALESCE" <> parenthesized (commaSeparated
436529 ((renderSQL <$> nullxs) <> [renderSQL notNullx]))
@@ -440,26 +533,20 @@ coalesce nullxs notNullx = UnsafeExpression $
440533-- >>> printSQL $ fromNull true null_
441534-- COALESCE(NULL, TRUE)
442535fromNull
443− :: Expression outer grp commons schemas params from ('NotNull ty)
536+ :: Expression outer commons grp schemas params from ('NotNull ty)
444537 -- ^ what to convert @NULL@ to
445− -> Expression outer grp commons schemas params from ('Null ty)
446− -> Expression outer grp commons schemas params from ('NotNull ty)
538+ -> Expression outer commons grp schemas params from ('Null ty)
539+ -> Expression outer commons grp schemas params from ('NotNull ty)
447540fromNull notNullx nullx = coalesce [nullx] notNullx
448541
449542-- | >>> printSQL $ null_ & isNull
450543-- NULL IS NULL
451−isNull
452− :: Expression outer grp commons schemas params from ('Null ty)
453− -- ^ possibly @NULL@
454− -> Condition outer grp commons schemas params from
544+isNull :: 'Null ty :--> null 'PGbool
455545isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
456546
457547-- | >>> printSQL $ null_ & isNotNull
458548-- NULL IS NOT NULL
459−isNotNull
460− :: Expression outer grp commons schemas params from ('Null ty)
461− -- ^ possibly @NULL@
462− -> Condition outer grp commons schemas params from
549+isNotNull :: 'Null ty :--> null 'PGbool
463550isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
464551
465552-- | analagous to `maybe` using @IS NULL@
@@ -467,13 +554,13 @@ isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
467554-- >>> printSQL $ matchNull true not_ null_
468555-- CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END
469556matchNull
470− :: Expression outer grp commons schemas params from (nullty)
557+ :: Expression outer commons grp schemas params from (nullty)
471558 -- ^ what to convert @NULL@ to
472− -> ( Expression outer grp commons schemas params from ('NotNull ty)
473− -> Expression outer grp commons schemas params from (nullty) )
559+ -> ( Expression outer commons grp schemas params from ('NotNull ty)
560+ -> Expression outer commons grp schemas params from (nullty) )
474561 -- ^ function to perform when @NULL@ is absent
475− -> Expression outer grp commons schemas params from ('Null ty)
476− -> Expression outer grp commons schemas params from (nullty)
562+ -> Expression outer commons grp schemas params from ('Null ty)
563+ -> Expression outer commons grp schemas params from (nullty)
477564matchNull y f x = ifThenElse (isNull x) y
478565 (f (UnsafeExpression (renderSQL x)))
479566
@@ -481,46 +568,87 @@ matchNull y f x = ifThenElse (isNull x) y
481568`nullIf` gives @NULL@.
482569
483570>>> :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)
485572>>> printSQL expr
486−NULL IF (FALSE, ($1 :: bool))
573+NULLIF(FALSE, ($1 :: bool))
487574-}
488−nullIf
489− :: Expression outer grp commons schemas params from ('NotNull ty)
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)
575+nullIf :: FunctionHet '[ 'NotNull ty, 'NotNull ty] ('Null ty)
576+nullIf = unsafeFunctionHet "NULLIF"
496577
497578-- | >>> printSQL $ array [null_, false, true]
498579-- ARRAY[NULL, FALSE, TRUE]
499580array
500− :: [Expression outer grp commons schemas params from ty]
581+ :: [Expression outer commons grp schemas params from ty]
501582 -- ^ array elements
502− -> Expression outer grp commons schemas params from (nullity ('PGvararray ty))
503−array xs = UnsafeExpression $
504− "ARRAY[" <> commaSeparated (renderSQL <$> xs) <> "]"
583+ -> Expression outer commons grp schemas params from (null ('PGvararray ty))
584+array xs = UnsafeExpression $ "ARRAY" <>
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)
505636
506637-- | >>> printSQL $ cardinality (array [null_, false, true])
507638-- cardinality(ARRAY[NULL, FALSE, TRUE])
508−cardinality
509− :: Expression outer grp commons schemas params from (nullity ('PGvararray ty))
510− -> Expression outer grp commons schemas params from (nullity 'PGint8)
639+cardinality :: null ('PGvararray ty) :--> null 'PGint8
511640cardinality = unsafeFunction "cardinality"
512641
513642-- | >>> printSQL $ array [null_, false, true] & index 2
514643-- (ARRAY[NULL, FALSE, TRUE])[2]
515644index
516645 :: Word64 -- ^ index
517− -> Expression outer grp commons schemas params from (nullity ('PGvararray ty)) -- ^ array
518− -> Expression outer grp commons schemas params from (NullifyType ty)
646+ -> null ('PGvararray ty) :--> NullifyType ty
519647index n expr = UnsafeExpression $
520648 parenthesized (renderSQL expr) <> "[" <> fromString (show n) <> "]"
521649
522650instance (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
524652 label = UnsafeExpression $ renderSQL (PGlabel @label)
525653
526654-- | A row constructor is an expression that builds a row value
@@ -532,14 +660,14 @@ instance (KnownSymbol label, label `In` labels) => IsPGlabel label
532660-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
533661-- :}
534662--
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)
536664-- >>> printSQL i
537665-- ROW(0, 1)
538666row
539667 :: SListI row
540− => NP (Aliased (Expression outer grp commons schemas params from)) row
668+ => NP (Aliased (Expression outer commons grp schemas params from)) row
541669 -- ^ 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))
543671row exprs = UnsafeExpression $ "ROW" <> parenthesized
544672 (renderCommaSeparated (\ (expr `As` _) -> renderSQL expr) exprs)
545673
@@ -550,7 +678,7 @@ row exprs = UnsafeExpression $ "ROW" <> parenthesized
550678-- type Schema = '["complex" ::: 'Typedef Complex]
551679-- :}
552680--
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)
554682-- >>> printSQL $ i & field #complex #imaginary
555683-- (ROW(0, 1)::"complex")."imaginary"
556684field
@@ -559,87 +687,65 @@ field
559687 , Has field row ty)
560688 => QualifiedAlias sch tydef -- ^ row type
561689 -> Alias field -- ^ field name
562− -> Expression outer grp commons schemas params from ('NotNull ('PGcomposite row))
563− -> Expression outer grp commons schemas params from ty
690+ -> Expression outer commons grp schemas params from ('NotNull ('PGcomposite row))
691+ -> Expression outer commons grp schemas params from ty
564692field td fld expr = UnsafeExpression $
565693 parenthesized (renderSQL expr <> "::" <> renderSQL td)
566694 <> "." <> renderSQL fld
567695
568696instance 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
570698 (<>) = unsafeBinaryOp "||"
571699
572700instance 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
574702 mempty = array []
575703 mappend = (<>)
576704
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)
578706-- >>> printSQL expr
579−-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
580−greatest
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("
587− <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
588−
589−-- | >>> printSQL $ least currentTimestamp [null_]
590−-- LEAST(CURRENT_TIMESTAMP, NULL)
591−least
592− :: Expression outer grp commons schemas params from (nullty)
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)) <> ")"
707+-- GREATEST(($1 :: timestamp with time zone), CURRENT_TIMESTAMP)
708+greatest :: FunctionHom ty ty ty
709+greatest = unsafeFunctionHom "GREATEST"
710+
711+-- | >>> printSQL $ least [null_] currentTimestamp
712+-- LEAST(NULL, CURRENT_TIMESTAMP)
713+least :: FunctionHom ty ty ty
714+least = unsafeFunctionHom "LEAST"
599715
600716-- | >>> printSQL $ unsafeBinaryOp "OR" true false
601717-- (TRUE OR FALSE)
602−unsafeBinaryOp
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)
718+unsafeBinaryOp :: ByteString {- ^ operator -} -> Operator ty0 ty1 ty2
608719unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
609720 renderSQL x <+> op <+> renderSQL y
610721
611−-- | >>> printSQL $ unsafeUnaryOp "NOT" true
722+-- | >>> printSQL $ unsafeUnaryOpL "NOT" true
612723-- (NOT TRUE)
613−unsafeUnaryOp
614− :: ByteString
615− -- ^ operator
616− -> Expression outer grp commons schemas params from (ty0)
617− -> Expression outer grp commons schemas params from (ty1)
618−unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
619− op <+> renderSQL x
724+unsafeUnaryOpL :: ByteString {- ^ operator -} -> x :--> y
725+unsafeUnaryOpL op x = UnsafeExpression $ parenthesized $ op <+> renderSQL x
726+
727+-- | >>> printSQL $ true & unsafeUnaryOpR "IS NOT TRUE"
728+-- (TRUE IS NOT TRUE)
729+unsafeUnaryOpR :: ByteString {- ^ operator -} -> x :--> y
730+unsafeUnaryOpR op x = UnsafeExpression $ parenthesized $ renderSQL x <+> op
620731
621732-- | >>> printSQL $ unsafeFunction "f" true
622733-- f(TRUE)
623−unsafeFunction
624− :: ByteString
625− -- ^ function
626− -> Expression outer grp commons schemas params from (xty)
627− -> Expression outer grp commons schemas params from (yty)
734+unsafeFunction :: ByteString {- ^ function -} -> x :--> y
628735unsafeFunction fun x = UnsafeExpression $
629736 fun <> parenthesized (renderSQL x)
630737
631−-- | Helper for defining variadic functions.
632−unsafeVariadicFunction
633− :: SListI elems
634− => ByteString
635− -- ^ function
636− -> NP (Expression outer grp commons schemas params from) elems
637− -> Expression outer grp commons schemas params from ret
638−unsafeVariadicFunction fun x = UnsafeExpression $
639− fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderSQL) x)))
738+-- | >>> printSQL $ unsafeFunctionHet "f" (currentTime :* localTimestamp :* false *: litChar 'a')
739+-- f(CURRENT_TIME, LOCALTIMESTAMP, FALSE, E'a')
740+unsafeFunctionHet
741+ :: SListI xs
742+ => ByteString {- ^ function -}
743+ -> FunctionHet xs y
744+unsafeFunctionHet fun xs = UnsafeExpression $
745+ fun <> parenthesized (renderCommaSeparated renderSQL xs)
640746
641747instance 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
643749 (+) = unsafeBinaryOp "+"
644750 (-) = unsafeBinaryOp "-"
645751 (*) = unsafeBinaryOp "*"
@@ -651,7 +757,7 @@ instance ty `In` PGNum
651757 . show
652758
653759instance (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
655761 (/) = unsafeBinaryOp "/"
656762 fromRational
657763 = UnsafeExpression
@@ -661,7 +767,7 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
661767 . fromRat @Double
662768
663769instance (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
665771 pi = UnsafeExpression "pi()"
666772 exp = unsafeFunction "exp"
667773 log = unsafeFunction "ln"
@@ -684,20 +790,15 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Floating
684790
685791-- | >>> :{
686792-- let
687−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
688−-- expression = atan2_ pi 2
793+-- expression :: Expr (null 'PGfloat4)
794+-- expression = atan2_ (pi *: 2)
689795-- in printSQL expression
690796-- :}
691797-- atan2(pi(), 2)
692798atan2_
693799 :: float `In` PGFloating
694− => Expression outer grp commons schemas params from (nullity float)
695− -- ^ numerator
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 <> ")"
800+ => FunctionHet '[ null float, null float] (null float)
801+atan2_ = unsafeFunctionHet "atan2"
701802
702803-- When a `cast` is applied to an `Expression` of a known type, it
703804-- represents a run-time type conversion. The cast will succeed only if a
@@ -708,9 +809,9 @@ atan2_ y x = UnsafeExpression $
708809cast
709810 :: TypeExpression schemas ty1
710811 -- ^ type to cast as
711− -> Expression outer grp commons schemas params from ty0
812+ -> Expression outer commons grp schemas params from ty0
712813 -- ^ value to convert
713− -> Expression outer grp commons schemas params from ty1
814+ -> Expression outer commons grp schemas params from ty1
714815cast ty x = UnsafeExpression $ parenthesized $
715816 renderSQL x <+> "::" <+> renderSQL ty
716817
@@ -718,136 +819,108 @@ cast ty x = UnsafeExpression $ parenthesized $
718819--
719820-- >>> :{
720821-- let
721−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
822+-- expression :: Expression outer commons grp schemas params from (null 'PGint2)
722823-- expression = 5 `quot_` 2
723824-- in printSQL expression
724825-- :}
725826-- (5 / 2)
726827quot_
727828 :: int `In` PGIntegral
728− => Expression outer grp commons schemas params from (nullity 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)
829+ => Operator (null int) (null int) (null int)
733830quot_ = unsafeBinaryOp "/"
734831
735832-- | remainder upon integer division
736833--
737834-- >>> :{
738835-- let
739−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
836+-- expression :: Expression outer commons grp schemas params from (null 'PGint2)
740837-- expression = 5 `rem_` 2
741838-- in printSQL expression
742839-- :}
743840-- (5 % 2)
744841rem_
745842 :: int `In` PGIntegral
746− => Expression outer grp commons schemas params from (nullity 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)
843+ => Operator (null int) (null int) (null int)
751844rem_ = unsafeBinaryOp "%"
752845
753846-- | >>> :{
754847-- let
755−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
848+-- expression :: Expression outer commons grp schemas params from (null 'PGfloat4)
756849-- expression = trunc pi
757850-- in printSQL expression
758851-- :}
759852-- trunc(pi())
760−trunc
761− :: frac `In` PGFloating
762− => Expression outer grp commons schemas params from (nullity frac)
763− -- ^ fractional number
764− -> Expression outer grp commons schemas params from (nullity frac)
853+trunc :: frac `In` PGFloating => null frac :--> null frac
765854trunc = unsafeFunction "trunc"
766855
767856-- | >>> :{
768857-- let
769−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
858+-- expression :: Expression outer commons grp schemas params from (null 'PGfloat4)
770859-- expression = round_ pi
771860-- in printSQL expression
772861-- :}
773862-- round(pi())
774−round_
775− :: frac `In` PGFloating
776− => Expression outer grp commons schemas params from (nullity frac)
777− -- ^ fractional number
778− -> Expression outer grp commons schemas params from (nullity frac)
863+round_ :: frac `In` PGFloating => null frac :--> null frac
779864round_ = unsafeFunction "round"
780865
781866-- | >>> :{
782867-- let
783−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
868+-- expression :: Expression outer commons grp schemas params from (null 'PGfloat4)
784869-- expression = ceiling_ pi
785870-- in printSQL expression
786871-- :}
787872-- ceiling(pi())
788−ceiling_
789− :: frac `In` PGFloating
790− => Expression outer grp commons schemas params from (nullity frac)
791− -- ^ fractional number
792− -> Expression outer grp commons schemas params from (nullity frac)
873+ceiling_ :: frac `In` PGFloating => null frac :--> null frac
793874ceiling_ = unsafeFunction "ceiling"
794875
795876-- | A `Condition` is an `Expression`, which can evaluate
796877-- to `true`, `false` or `null_`. This is because SQL uses
797878-- a three valued logic.
798−type Condition outer grp commons schemas params from =
799− Expression outer grp commons schemas params from ('Null 'PGbool)
879+type Condition outer commons grp schemas params from =
880+ Expression outer commons grp schemas params from ('Null 'PGbool)
800881
801882-- | >>> printSQL true
802883-- TRUE
803−true :: Expression outer grp commons schemas params from (nullity 'PGbool)
884+true :: Expr (null 'PGbool)
804885true = UnsafeExpression "TRUE"
805886
806887-- | >>> printSQL false
807888-- FALSE
808−false :: Expression outer grp commons schemas params from (nullity 'PGbool)
889+false :: Expr (null 'PGbool)
809890false = UnsafeExpression "FALSE"
810891
811892-- | >>> printSQL $ not_ true
812893-- (NOT TRUE)
813−not_
814− :: Expression outer grp commons schemas params from (nullity 'PGbool)
815− -> Expression outer grp commons schemas params from (nullity 'PGbool)
816−not_ = unsafeUnaryOp "NOT"
894+not_ :: null 'PGbool :--> null 'PGbool
895+not_ = unsafeUnaryOpL "NOT"
817896
818897-- | >>> printSQL $ true .&& false
819898-- (TRUE AND FALSE)
820−(.&&)
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)
899+(.&&) :: Operator (null 'PGbool) (null 'PGbool) (null 'PGbool)
824900infixr 3 .&&
825901(.&&) = unsafeBinaryOp "AND"
826902
827903-- | >>> printSQL $ true .|| false
828904-- (TRUE OR FALSE)
829−(.||)
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)
905+(.||) :: Operator (null 'PGbool) (null 'PGbool) (null 'PGbool)
833906infixr 2 .||
834907(.||) = unsafeBinaryOp "OR"
835908
836909-- | >>> :{
837910-- let
838−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
911+-- expression :: Expression outer commons grp schemas params from (null 'PGint2)
839912-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
840913-- in printSQL expression
841914-- :}
842915-- CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END
843916caseWhenThenElse
844− :: [ ( Condition outer grp commons schemas params from
845− , Expression outer grp commons schemas params from ty
917+ :: [ ( Condition outer commons grp schemas params from
918+ , Expression outer commons grp schemas params from ty
846919 ) ]
847920 -- ^ whens and thens
848− -> Expression outer grp commons schemas params from ty
921+ -> Expression outer commons grp schemas params from ty
849922 -- ^ else
850− -> Expression outer grp commons schemas params from ty
923+ -> Expression outer commons grp schemas params from ty
851924caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
852925 [ "CASE"
853926 , mconcat
@@ -863,16 +936,16 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
863936
864937-- | >>> :{
865938-- let
866−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
939+-- expression :: Expression outer commons grp schemas params from (null 'PGint2)
867940-- expression = ifThenElse true 1 0
868941-- in printSQL expression
869942-- :}
870943-- CASE WHEN TRUE THEN 1 ELSE 0 END
871944ifThenElse
872− :: Condition outer grp commons schemas params from
873− -> Expression outer grp commons schemas params from ty -- ^ then
874− -> Expression outer grp commons schemas params from ty -- ^ else
875− -> Expression outer grp commons schemas params from ty
945+ :: Condition outer commons grp schemas params from
946+ -> Expression outer commons grp schemas params from ty -- ^ then
947+ -> Expression outer commons grp schemas params from ty -- ^ else
948+ -> Expression outer commons grp schemas params from ty
876949ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
877950
878951-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
@@ -880,261 +953,242 @@ ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
880953--
881954-- >>> printSQL $ true .== null_
882955-- (TRUE = NULL)
883−(.==)
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
956+(.==) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
887957(.==) = unsafeBinaryOp "="
888958infix 4 .==
889959
890960-- | >>> printSQL $ true ./= null_
891961-- (TRUE <> NULL)
892−(./=)
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
962+(./=) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
896963(./=) = unsafeBinaryOp "<>"
897964infix 4 ./=
898965
899966-- | >>> printSQL $ true .>= null_
900967-- (TRUE >= NULL)
901−(.>=)
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
968+(.>=) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
905969(.>=) = unsafeBinaryOp ">="
906970infix 4 .>=
907971
908972-- | >>> printSQL $ true .< null_
909973-- (TRUE < NULL)
910−(.<)
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
974+(.<) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
914975(.<) = unsafeBinaryOp "<"
915976infix 4 .<
916977
917978-- | >>> printSQL $ true .<= null_
918979-- (TRUE <= NULL)
919−(.<=)
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
980+(.<=) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
923981(.<=) = unsafeBinaryOp "<="
924982infix 4 .<=
925983
926984-- | >>> printSQL $ true .> null_
927985-- (TRUE > NULL)
928−(.>)
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
986+(.>) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
932987(.>) = unsafeBinaryOp ">"
933988infix 4 .>
934989
935−{- | between
936−>>> printSQL $ true `between` (null_, false)
990+{- | >>> printSQL $ true `between` (null_, false)
937991TRUE BETWEEN NULL AND FALSE
938992-}
939993between
940− :: Expression outer grp commons schemas params from (nullity ty)
941− -> ( Expression outer grp commons schemas params from (nullity ty)
942− , Expression outer grp commons schemas params from (nullity ty) ) -- ^ bounds
943− -> Condition outer grp commons schemas params from
994+ :: Expression outer commons grp schemas params from (null ty)
995+ -> ( Expression outer commons grp schemas params from (null ty)
996+ , Expression outer commons grp schemas params from (null ty) ) -- ^ bounds
997+ -> Condition outer commons grp schemas params from
944998between a (x,y) = UnsafeExpression $ renderSQL a <+> "BETWEEN"
945999 <+> renderSQL x <+> "AND" <+> renderSQL y
9461000
947−{- | not between
948−>>> printSQL $ true `notBetween` (null_, false)
1001+{- | >>> printSQL $ true `notBetween` (null_, false)
9491002TRUE NOT BETWEEN NULL AND FALSE
9501003-}
9511004notBetween
952− :: Expression outer grp commons schemas params from (nullity ty)
953− -> ( Expression outer grp commons schemas params from (nullity ty)
954− , Expression outer grp commons schemas params from (nullity ty) ) -- ^ bounds
955− -> Condition outer grp commons schemas params from
1005+ :: Expression outer commons grp schemas params from (null ty)
1006+ -> ( Expression outer commons grp schemas params from (null ty)
1007+ , Expression outer commons grp schemas params from (null ty) ) -- ^ bounds
1008+ -> Condition outer commons grp schemas params from
9561009notBetween a (x,y) = UnsafeExpression $ renderSQL a <+> "NOT BETWEEN"
9571010 <+> renderSQL x <+> "AND" <+> renderSQL y
9581011
9591012{- | between, after sorting the comparison values
1013+
9601014>>> printSQL $ true `betweenSymmetric` (null_, false)
9611015TRUE BETWEEN SYMMETRIC NULL AND FALSE
9621016-}
9631017betweenSymmetric
964− :: Expression outer grp commons schemas params from (nullity ty)
965− -> ( Expression outer grp commons schemas params from (nullity ty)
966− , Expression outer grp commons schemas params from (nullity ty) ) -- ^ bounds
967− -> Condition outer grp commons schemas params from
1018+ :: Expression outer commons grp schemas params from (null ty)
1019+ -> ( Expression outer commons grp schemas params from (null ty)
1020+ , Expression outer commons grp schemas params from (null ty) ) -- ^ bounds
1021+ -> Condition outer commons grp schemas params from
9681022betweenSymmetric a (x,y) = UnsafeExpression $ renderSQL a
9691023 <+> "BETWEEN SYMMETRIC" <+> renderSQL x <+> "AND" <+> renderSQL y
9701024
9711025{- | not between, after sorting the comparison values
1026+
9721027>>> printSQL $ true `notBetweenSymmetric` (null_, false)
9731028TRUE NOT BETWEEN SYMMETRIC NULL AND FALSE
9741029-}
9751030notBetweenSymmetric
976− :: Expression outer grp commons schemas params from (nullity ty)
977− -> ( Expression outer grp commons schemas params from (nullity ty)
978− , Expression outer grp commons schemas params from (nullity ty) ) -- ^ bounds
979− -> Condition outer grp commons schemas params from
1031+ :: Expression outer commons grp schemas params from (null ty)
1032+ -> ( Expression outer commons grp schemas params from (null ty)
1033+ , Expression outer commons grp schemas params from (null ty) ) -- ^ bounds
1034+ -> Condition outer commons grp schemas params from
9801035notBetweenSymmetric a (x,y) = UnsafeExpression $ renderSQL a
9811036 <+> "NOT BETWEEN SYMMETRIC" <+> renderSQL x <+> "AND" <+> renderSQL y
9821037
9831038{- | not equal, treating null like an ordinary value
1039+
9841040>>> printSQL $ true `isDistinctFrom` null_
9851041(TRUE IS DISTINCT FROM NULL)
9861042-}
987−isDistinctFrom
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
1043+isDistinctFrom :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
9911044isDistinctFrom = unsafeBinaryOp "IS DISTINCT FROM"
9921045
9931046{- | equal, treating null like an ordinary value
1047+
9941048>>> printSQL $ true `isNotDistinctFrom` null_
9951049(TRUE IS NOT DISTINCT FROM NULL)
9961050-}
997−isNotDistinctFrom
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
1051+isNotDistinctFrom :: Operator (null0 ty) (null1 ty) ('NotNull 'PGbool)
10011052isNotDistinctFrom = unsafeBinaryOp "IS NOT DISTINCT FROM"
10021053
10031054{- | is true
1055+
10041056>>> printSQL $ true & isTrue
1005−TRUE IS TRUE
1057+(TRUE IS TRUE)
10061058-}
1007−isTrue
1008− :: Expression outer grp commons schemas params from (nullity 'PGbool)
1009− -> Condition outer grp commons schemas params from
1010−isTrue b = UnsafeExpression $ renderSQL b <+> "IS TRUE"
1059+isTrue :: null0 'PGbool :--> null1 'PGbool
1060+isTrue = unsafeUnaryOpR "IS TRUE"
10111061
10121062{- | is false or unknown
1063+
10131064>>> printSQL $ true & isNotTrue
1014−TRUE IS NOT TRUE
1065+(TRUE IS NOT TRUE)
10151066-}
1016−isNotTrue
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"
1067+isNotTrue :: null0 'PGbool :--> null1 'PGbool
1068+isNotTrue = unsafeUnaryOpR "IS NOT TRUE"
10201069
10211070{- | is false
1071+
10221072>>> printSQL $ true & isFalse
1023−TRUE IS FALSE
1073+(TRUE IS FALSE)
10241074-}
1025−isFalse
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"
1075+isFalse :: null0 'PGbool :--> null1 'PGbool
1076+isFalse = unsafeUnaryOpR "IS FALSE"
10291077
10301078{- | is true or unknown
1079+
10311080>>> printSQL $ true & isNotFalse
1032−TRUE IS NOT FALSE
1081+(TRUE IS NOT FALSE)
10331082-}
1034−isNotFalse
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"
1083+isNotFalse :: null0 'PGbool :--> null1 'PGbool
1084+isNotFalse = unsafeUnaryOpR "IS NOT FALSE"
10381085
10391086{- | is unknown
1087+
10401088>>> printSQL $ true & isUnknown
1041−TRUE IS UNKNOWN
1089+(TRUE IS UNKNOWN)
10421090-}
1043−isUnknown
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"
1091+isUnknown :: null0 'PGbool :--> null1 'PGbool
1092+isUnknown = unsafeUnaryOpR "IS UNKNOWN"
10471093
10481094{- | is true or false
1095+
10491096>>> printSQL $ true & isNotUnknown
1050−TRUE IS NOT UNKNOWN
1097+(TRUE IS NOT UNKNOWN)
10511098-}
1052−isNotUnknown
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"
1099+isNotUnknown :: null0 'PGbool :--> null1 'PGbool
1100+isNotUnknown = unsafeUnaryOpR "IS NOT UNKNOWN"
10561101
10571102-- | >>> printSQL currentDate
10581103-- CURRENT_DATE
1059−currentDate
1060− :: Expression outer grp commons schemas params from (nullity 'PGdate)
1104+currentDate :: Expr (null 'PGdate)
10611105currentDate = UnsafeExpression "CURRENT_DATE"
10621106
10631107-- | >>> printSQL currentTime
10641108-- CURRENT_TIME
1065−currentTime
1066− :: Expression outer grp commons schemas params from (nullity 'PGtimetz)
1109+currentTime :: Expr (null 'PGtimetz)
10671110currentTime = UnsafeExpression "CURRENT_TIME"
10681111
10691112-- | >>> printSQL currentTimestamp
10701113-- CURRENT_TIMESTAMP
1071−currentTimestamp
1072− :: Expression outer grp commons schemas params from (nullity 'PGtimestamptz)
1114+currentTimestamp :: Expr (null 'PGtimestamptz)
10731115currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
10741116
10751117-- | >>> printSQL localTime
10761118-- LOCALTIME
1077−localTime
1078− :: Expression outer grp commons schemas params from (nullity 'PGtime)
1119+localTime :: Expr (null 'PGtime)
10791120localTime = UnsafeExpression "LOCALTIME"
10801121
10811122-- | >>> printSQL localTimestamp
10821123-- LOCALTIMESTAMP
1083−localTimestamp
1084− :: Expression outer grp commons schemas params from (nullity 'PGtimestamp)
1124+localTimestamp :: Expr (null 'PGtimestamp)
10851125localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
10861126
1127+-- | Current date and time (equivalent to `currentTimestamp`)
1128+--
1129+-- >>> printSQL now
1130+-- now()
1131+now :: Expr (null 'PGtimestamptz)
1132+now = UnsafeExpression "now()"
1133+
10871134{-----------------------------------------
10881135text
10891136-----------------------------------------}
10901137
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+
10911150instance IsString
1092− (Expression outer grp commons schemas params from (nullity 'PGtext)) where
1151+ (Expression outer commons grp schemas params from (null 'PGtext)) where
10931152 fromString str = UnsafeExpression $
10941153 "E\'" <> fromString (escape =<< str) <> "\'"
1095− where
1096− escape = \case
1097− '\NUL' -> "\\0"
1098− '\'' -> "''"
1099− '"' -> "\\\""
1100− '\b' -> "\\b"
1101− '\n' -> "\\n"
1102− '\r' -> "\\r"
1103− '\t' -> "\\t"
1104− '\\' -> "\\\\"
1105− c -> [c]
1154+instance IsString
1155+ (Expression outer commons grp schemas params from (null 'PGtsvector)) where
1156+ fromString str = cast tsvector . UnsafeExpression $
1157+ "E\'" <> fromString (escape =<< str) <> "\'"
1158+instance IsString
1159+ (Expression outer commons grp schemas params from (null 'PGtsquery)) where
1160+ fromString str = cast tsquery . UnsafeExpression $
1161+ "E\'" <> fromString (escape =<< str) <> "\'"
11061162
11071163instance 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
11091168 (<>) = unsafeBinaryOp "||"
11101169
11111170instance 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
11131176 mempty = fromString ""
11141177 mappend = (<>)
11151178
11161179-- | >>> printSQL $ lower "ARRRGGG"
11171180-- lower(E'ARRRGGG')
1118−lower
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)
1181+lower :: null 'PGtext :--> null 'PGtext
11221182lower = unsafeFunction "lower"
11231183
11241184-- | >>> printSQL $ upper "eeee"
11251185-- upper(E'eeee')
1126−upper
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)
1186+upper :: null 'PGtext :--> null 'PGtext
11301187upper = unsafeFunction "upper"
11311188
11321189-- | >>> printSQL $ charLength "four"
11331190-- char_length(E'four')
1134−charLength
1135− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1136− -- ^ string to measure
1137− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1191+charLength :: null 'PGtext :--> null 'PGint4
11381192charLength = unsafeFunction "char_length"
11391193
11401194-- | The `like` expression returns true if the @string@ matches
@@ -1146,12 +1200,7 @@ charLength = unsafeFunction "char_length"
11461200--
11471201-- >>> printSQL $ "abc" `like` "a%"
11481202-- (E'abc' LIKE E'a%')
1149−like
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)
1203+like :: Operator (null 'PGtext) (null 'PGtext) ('Null 'PGbool)
11551204like = unsafeBinaryOp "LIKE"
11561205
11571206-- | The key word ILIKE can be used instead of LIKE to make the
@@ -1159,12 +1208,7 @@ like = unsafeBinaryOp "LIKE"
11591208--
11601209-- >>> printSQL $ "abc" `ilike` "a%"
11611210-- (E'abc' ILIKE E'a%')
1162−ilike
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)
1211+ilike :: Operator (null 'PGtext) (null 'PGtext) ('Null 'PGbool)
11681212ilike = unsafeBinaryOp "ILIKE"
11691213
11701214{-----------------------------------------
@@ -1179,86 +1223,66 @@ Table 9.44: json and jsonb operators
11791223-- | Get JSON value (object field or array element) at a key.
11801224(.->)
11811225 :: (json `In` PGJsonType, key `In` PGJsonKey)
1182− => Expression outer grp commons schemas params from (nullity json)
1183− -> Expression outer grp commons schemas params from (nullity key)
1184− -> Expression outer grp commons schemas params from ('Null json)
1226+ => Operator (null json) (null key) ('Null json)
11851227infixl 8 .->
11861228(.->) = unsafeBinaryOp "->"
11871229
11881230-- | Get JSON value (object field or array element) at a key, as text.
11891231(.->>)
11901232 :: (json `In` PGJsonType, key `In` PGJsonKey)
1191− => Expression outer grp commons schemas params from (nullity json)
1192− -> Expression outer grp commons schemas params from (nullity key)
1193− -> Expression outer grp commons schemas params from ('Null 'PGtext)
1233+ => Operator (null json) (null key) ('Null 'PGtext)
11941234infixl 8 .->>
11951235(.->>) = unsafeBinaryOp "->>"
11961236
11971237-- | Get JSON value at a specified path.
11981238(.#>)
1199− :: (json `In` PGJsonType, PGTextArray "(.#>)" path)
1200− => Expression outer grp commons schemas params from (nullity json)
1201− -> Expression outer grp commons schemas params from (nullity path)
1202− -> Expression outer grp commons schemas params from ('Null json)
1239+ :: json `In` PGJsonType
1240+ => Operator (null json) (null ('PGvararray ('NotNull 'PGtext))) ('Null json)
12031241infixl 8 .#>
12041242(.#>) = unsafeBinaryOp "#>"
12051243
12061244-- | Get JSON value at a specified path as text.
12071245(.#>>)
1208− :: (json `In` PGJsonType, PGTextArray "(.#>>)" path)
1209− => Expression outer grp commons schemas params from (nullity json)
1210− -> Expression outer grp commons schemas params from (nullity path)
1211− -> Expression outer grp commons schemas params from ('Null 'PGtext)
1246+ :: json `In` PGJsonType
1247+ => Operator (null json) (null ('PGvararray ('NotNull 'PGtext))) ('Null 'PGtext)
12121248infixl 8 .#>>
12131249(.#>>) = unsafeBinaryOp "#>>"
12141250
12151251-- Additional jsonb operators
12161252
1217−-- | Does the left JSON value contain the right JSON path/value entries at the
1218−-- top level?
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 "@>"
1225−
1226−-- | Are the left JSON path/value entries contained at the top level within the
1227−-- right JSON value?
1228−(.<@)
1229− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1230− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1231− -> Condition outer grp commons schemas params from
1232−infixl 9 .<@
1233−(.<@) = unsafeBinaryOp "<@"
1253+class PGSubset container where
1254+ (@>) :: Operator (null0 container) (null1 container) ('Null 'PGbool)
1255+ (@>) = unsafeBinaryOp "@>"
1256+ (<@) :: Operator (null0 container) (null1 container) ('Null 'PGbool)
1257+ (<@) = unsafeBinaryOp "<@"
1258+instance PGSubset 'PGjsonb
1259+instance PGSubset 'PGtsquery
1260+instance PGSubset ('PGvararray ty)
12341261
12351262-- | Does the string exist as a top-level key within the JSON value?
1236−(.?)
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
1263+(.?) :: Operator (null 'PGjsonb) (null 'PGtext) ('Null 'PGbool)
12401264infixl 9 .?
12411265(.?) = unsafeBinaryOp "?"
12421266
12431267-- | Do any of these array strings exist as top-level keys?
1244−(.?|)
1245− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1246− -> Expression outer grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1247− -> Condition outer grp commons schemas params from
1268+(.?|) :: Operator
1269+ (null 'PGjsonb)
1270+ (null ('PGvararray ('NotNull 'PGtext)))
1271+ ('Null 'PGbool)
12481272infixl 9 .?|
12491273(.?|) = unsafeBinaryOp "?|"
12501274
12511275-- | Do all of these array strings exist as top-level keys?
1252−(.?&)
1253− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1254− -> Expression outer grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1255− -> Condition outer grp commons schemas params from
1276+(.?&) :: Operator
1277+ (null 'PGjsonb)
1278+ (null ('PGvararray ('NotNull 'PGtext)))
1279+ ('Null 'PGbool)
12561280infixl 9 .?&
12571281(.?&) = unsafeBinaryOp "?&"
12581282
12591283-- | Concatenate two jsonb values into a new jsonb value.
12601284instance Semigroup
1261− (Expression outer grp commons schemas params from (nullity 'PGjsonb)) where
1285+ (Expression outer commons grp schemas params from (null 'PGjsonb)) where
12621286 (<>) = unsafeBinaryOp "||"
12631287
12641288-- | Delete a key or keys from a JSON object, or remove an array element.
@@ -1275,19 +1299,13 @@ instance Semigroup
12751299-- count from the end). Throws an error if top level container is not an array.
12761300(.-.)
12771301 :: (key `In` '[ 'PGtext, 'PGvararray ('NotNull 'PGtext), 'PGint4, 'PGint2 ]) -- hlint error without parens here
1278− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1279− -> Expression outer grp commons schemas params from (nullity key)
1280− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1302+ => Operator (null 'PGjsonb) (null key) (null 'PGjsonb)
12811303infixl 6 .-.
12821304(.-.) = unsafeBinaryOp "-"
12831305
12841306-- | Delete the field or element with specified path (for JSON arrays, negative
12851307-- integers count from the end)
1286−(#-.)
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)
1308+(#-.) :: Operator (null 'PGjsonb) (null ('PGvararray ('NotNull 'PGtext))) (null 'PGjsonb)
12911309infixl 6 #-.
12921310(#-.) = unsafeBinaryOp "#-"
12931311
@@ -1296,16 +1314,12 @@ Table 9.45: JSON creation functions
12961314-----------------------------------------}
12971315
12981316-- | Literal binary JSON
1299−jsonbLit
1300− :: JSON.ToJSON x
1301− => x -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1317+jsonbLit :: JSON.ToJSON x => x -> Expr (null 'PGjsonb)
13021318jsonbLit = cast jsonb . UnsafeExpression
13031319 . singleQuotedUtf8 . toStrict . JSON.encode
13041320
13051321-- | Literal JSON
1306−jsonLit
1307− :: JSON.ToJSON x
1308− => x -> Expression outer grp commons schemas params from (nullity 'PGjson)
1322+jsonLit :: JSON.ToJSON x => x -> Expr (null 'PGjson)
13091323jsonLit = cast json . UnsafeExpression
13101324 . singleQuotedUtf8 . toStrict . JSON.encode
13111325
@@ -1315,9 +1329,7 @@ jsonLit = cast json . UnsafeExpression
13151329-- otherwise, a scalar value is produced. For any scalar type other than a
13161330-- number, a Boolean, or a null value, the text representation will be used, in
13171331-- such a fashion that it is a valid json value.
1318−toJson
1319− :: Expression outer grp commons schemas params from (nullity ty)
1320− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1332+toJson :: null ty :--> null 'PGjson
13211333toJson = unsafeFunction "to_json"
13221334
13231335-- | Returns the value as jsonb. Arrays and composites are converted
@@ -1326,204 +1338,154 @@ toJson = unsafeFunction "to_json"
13261338-- otherwise, a scalar value is produced. For any scalar type other than a
13271339-- number, a Boolean, or a null value, the text representation will be used, in
13281340-- such a fashion that it is a valid jsonb value.
1329−toJsonb
1330− :: Expression outer grp commons schemas params from (nullity ty)
1331− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1341+toJsonb :: null ty :--> null 'PGjsonb
13321342toJsonb = unsafeFunction "to_jsonb"
13331343
13341344-- | Returns the array as a JSON array. A PostgreSQL multidimensional array
13351345-- becomes a JSON array of arrays.
1336−arrayToJson
1337− :: PGArray "arrayToJson" arr
1338− => Expression outer grp commons schemas params from (nullity arr)
1339− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1346+arrayToJson :: null ('PGvararray ty) :--> null 'PGjson
13401347arrayToJson = unsafeFunction "array_to_json"
13411348
13421349-- | Returns the row as a JSON object.
1343−rowToJson
1344− :: Expression outer grp commons schemas params from (nullity ('PGcomposite ty))
1345− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1350+rowToJson :: null ('PGcomposite ty) :--> null 'PGjson
13461351rowToJson = unsafeFunction "row_to_json"
13471352
13481353-- | Builds a possibly-heterogeneously-typed JSON array out of a variadic
13491354-- argument list.
1350−jsonBuildArray
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"
1355+jsonBuildArray :: SListI tuple => FunctionHet tuple (null 'PGjson)
1356+jsonBuildArray = unsafeFunctionHet "json_build_array"
13551357
13561358-- | Builds a possibly-heterogeneously-typed (binary) JSON array out of a
13571359-- variadic argument list.
1358−jsonbBuildArray
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"
1363−
1364−unsafeRowFunction
1365− :: SOP.SListI elems
1366− => NP (Aliased (Expression outer grp commons schemas params from)) elems
1367− -> [ByteString]
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
1360+jsonbBuildArray :: SListI tuple => FunctionHet tuple (null 'PGjsonb)
1361+jsonbBuildArray = unsafeFunctionHet "jsonb_build_array"
1362+
1363+class PGBuildObject tys where
1364+instance PGBuildObject '[]
1365+instance (PGBuildObject tys, key `In` PGJsonKey)
1366+ => PGBuildObject ('NotNull key ': value ': tys)
13751367
13761368-- | Builds a possibly-heterogeneously-typed JSON object out of a variadic
13771369-- argument list. The elements of the argument list must alternate between text
13781370-- and values.
13791371jsonBuildObject
1380− :: SOP.SListI elems
1381− => NP (Aliased (Expression outer grp commons schemas params from)) elems
1382− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1383−jsonBuildObject
1384− = unsafeFunction "json_build_object"
1385− . UnsafeExpression
1386− . commaSeparated
1387− . unsafeRowFunction
1372+ :: (SListI elems, PGBuildObject elems)
1373+ => FunctionHet elems (null 'PGjson)
1374+jsonBuildObject = unsafeFunctionHet "json_build_object"
13881375
13891376-- | Builds a possibly-heterogeneously-typed (binary) JSON object out of a
13901377-- variadic argument list. The elements of the argument list must alternate
1391−-- between text 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)
1378+-- between keys and values.
13961379jsonbBuildObject
1397− = unsafeFunction "jsonb_build_object"
1398− . UnsafeExpression
1399− . commaSeparated
1400− . unsafeRowFunction
1401−
1402−-- | Builds a JSON object out of a text array. The array must have either
1403−-- exactly one dimension with an even number of members, in which case they are
1404−-- taken as alternating key/value pairs, or two dimensions such that each inner
1405−-- array has exactly two elements, which are taken as a key/value pair.
1380+ :: (SListI elems, PGBuildObject elems)
1381+ => FunctionHet elems (null 'PGjsonb)
1382+jsonbBuildObject = unsafeFunctionHet "jsonb_build_object"
1383+
1384+-- | Builds a JSON object out of a text array.
1385+-- The array must have two dimensions
1386+-- such that each inner array has exactly two elements,
1387+-- which are taken as a key/value pair.
14061388jsonObject
1407− :: PGArrayOf "jsonObject" arr ('NotNull 'PGtext)
1408− => Expression outer grp commons schemas params from (nullity arr)
1409− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1389+ :: null ('PGfixarray '[n,2] ('NotNull 'PGtext))
1390+ :--> null 'PGjson
14101391jsonObject = unsafeFunction "json_object"
14111392
1412−-- | Builds a binary JSON object out of a text array. The array must have either
1413−-- exactly one dimension with an even number of members, in which case they are
1414−-- taken as alternating key/value pairs, or two dimensions such that each inner
1415−-- array has exactly two elements, which are taken as a key/value pair.
1393+-- | Builds a binary JSON object out of a text array.
1394+-- The array must have two dimensions
1395+-- such that each inner array has exactly two elements,
1396+-- which are taken as a key/value pair.
14161397jsonbObject
1417− :: PGArrayOf "jsonbObject" arr ('NotNull 'PGtext)
1418− => Expression outer grp commons schemas params from (nullity arr)
1419− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1398+ :: null ('PGfixarray '[n,2] ('NotNull 'PGtext))
1399+ :--> null 'PGjsonb
14201400jsonbObject = unsafeFunction "jsonb_object"
14211401
14221402-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
14231403-- keys and one for values, that are zipped pairwise to create a JSON object.
1424−jsonZipObject
1425− :: ( PGArrayOf "jsonZipObject" keysArray ('NotNull 'PGtext)
1426− , PGArrayOf "jsonZipObject" valuesArray ('NotNull 'PGtext))
1427− => Expression outer grp commons schemas params from (nullity keysArray)
1428− -> Expression outer grp commons schemas params from (nullity valuesArray)
1429− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1430−jsonZipObject ks vs =
1431− unsafeVariadicFunction "json_object" (ks :* vs :* Nil)
1404+jsonZipObject :: FunctionHet
1405+ '[ null ('PGvararray ('NotNull 'PGtext))
1406+ , null ('PGvararray ('NotNull 'PGtext)) ]
1407+ ( null 'PGjson )
1408+jsonZipObject = unsafeFunctionHet "json_object"
14321409
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
14341411-- keys and one for values, that are zipped pairwise to create a binary JSON
14351412-- object.
1436−jsonbZipObject
1437− :: ( PGArrayOf "jsonbZipObject" keysArray ('NotNull 'PGtext)
1438− , PGArrayOf "jsonbZipObject" valuesArray ('NotNull 'PGtext))
1439− => Expression outer grp commons schemas params from (nullity keysArray)
1440− -> Expression outer grp commons schemas params from (nullity valuesArray)
1441− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1442−jsonbZipObject ks vs =
1443− unsafeVariadicFunction "jsonb_object" (ks :* vs :* Nil)
1413+jsonbZipObject :: FunctionHet
1414+ '[ null ('PGvararray ('NotNull 'PGtext))
1415+ , null ('PGvararray ('NotNull 'PGtext)) ]
1416+ ( null 'PGjsonb )
1417+jsonbZipObject = unsafeFunctionHet "jsonb_object"
14441418
14451419{-----------------------------------------
14461420Table 9.46: JSON processing functions
14471421-----------------------------------------}
14481422
14491423-- | Returns the number of elements in the outermost JSON array.
1450−jsonArrayLength
1451− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1452− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1424+jsonArrayLength :: null 'PGjson :--> null 'PGint4
14531425jsonArrayLength = unsafeFunction "json_array_length"
14541426
14551427-- | Returns the number of elements in the outermost binary JSON array.
1456−jsonbArrayLength
1457− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1458− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1428+jsonbArrayLength :: null 'PGjsonb :--> null 'PGint4
14591429jsonbArrayLength = unsafeFunction "jsonb_array_length"
14601430
14611431-- | Returns JSON value pointed to by the given path (equivalent to #>
14621432-- operator).
14631433jsonExtractPath
14641434 :: SListI elems
1465− => Expression outer grp commons schemas params from (nullity 'PGjson)
1466− -> NP (Expression outer grp commons schemas params from) elems
1467− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1435+ => Expression outer commons grp schemas params from (null 'PGjson)
1436+ -> NP (Expression outer commons grp schemas params from) elems
1437+ -> Expression outer commons grp schemas params from (null 'PGjsonb)
14681438jsonExtractPath x xs =
1469− unsafeVariadicFunction "json_extract_path" (x :* xs)
1439+ unsafeFunctionHet "json_extract_path" (x :* xs)
14701440
14711441-- | Returns JSON value pointed to by the given path (equivalent to #>
14721442-- operator).
14731443jsonbExtractPath
14741444 :: SListI elems
1475− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1476− -> NP (Expression outer grp commons schemas params from) elems
1477− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1445+ => Expression outer commons grp schemas params from (null 'PGjsonb)
1446+ -> NP (Expression outer commons grp schemas params from) elems
1447+ -> Expression outer commons grp schemas params from (null 'PGjsonb)
14781448jsonbExtractPath x xs =
1479− unsafeVariadicFunction "jsonb_extract_path" (x :* xs)
1449+ unsafeFunctionHet "jsonb_extract_path" (x :* xs)
14801450
14811451-- | Returns JSON value pointed to by the given path (equivalent to #>
14821452-- operator), as text.
14831453jsonExtractPathAsText
14841454 :: SListI elems
1485− => Expression outer grp commons schemas params from (nullity 'PGjson)
1486− -> NP (Expression outer grp commons schemas params from) elems
1487− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1455+ => Expression outer commons grp schemas params from (null 'PGjson)
1456+ -> NP (Expression outer commons grp schemas params from) elems
1457+ -> Expression outer commons grp schemas params from (null 'PGjson)
14881458jsonExtractPathAsText x xs =
1489− unsafeVariadicFunction "json_extract_path_text" (x :* xs)
1459+ unsafeFunctionHet "json_extract_path_text" (x :* xs)
14901460
14911461-- | Returns JSON value pointed to by the given path (equivalent to #>
14921462-- operator), as text.
14931463jsonbExtractPathAsText
14941464 :: SListI elems
1495− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1496− -> NP (Expression outer grp commons schemas params from) elems
1497− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1465+ => Expression outer commons grp schemas params from (null 'PGjsonb)
1466+ -> NP (Expression outer commons grp schemas params from) elems
1467+ -> Expression outer commons grp schemas params from (null 'PGjsonb)
14981468jsonbExtractPathAsText x xs =
1499− unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
1469+ unsafeFunctionHet "jsonb_extract_path_text" (x :* xs)
15001470
15011471-- | Returns the type of the outermost JSON value as a text string. Possible
15021472-- types are object, array, string, number, boolean, and null.
1503−jsonTypeof
1504− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1505− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1473+jsonTypeof :: null 'PGjson :--> null 'PGtext
15061474jsonTypeof = unsafeFunction "json_typeof"
15071475
15081476-- | Returns the type of the outermost binary JSON value as a text string.
15091477-- Possible types are object, array, string, number, boolean, and null.
1510−jsonbTypeof
1511− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1512− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1478+jsonbTypeof :: null 'PGjsonb :--> null 'PGtext
15131479jsonbTypeof = unsafeFunction "jsonb_typeof"
15141480
15151481-- | Returns its argument with all object fields that have null values omitted.
15161482-- Other null values are untouched.
1517−jsonStripNulls
1518− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1519− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1483+jsonStripNulls :: null 'PGjson :--> null 'PGjson
15201484jsonStripNulls = unsafeFunction "json_strip_nulls"
15211485
15221486-- | Returns its argument with all object fields that have null values omitted.
15231487-- Other null values are untouched.
1524−jsonbStripNulls
1525− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1526− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1488+jsonbStripNulls :: null 'PGjsonb :--> null 'PGjsonb
15271489jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
15281490
15291491-- | @ jsonbSet target path new_value create_missing @
@@ -1534,15 +1496,14 @@ jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
15341496-- operators, negative integers that appear in path count from the end of JSON
15351497-- arrays.
15361498jsonbSet
1537− :: PGTextArray "jsonbSet" arr
1538− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1539− -> Expression outer grp commons schemas params from (nullity arr)
1540− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1541− -> Maybe (Expression outer grp commons schemas params from (nullity 'PGbool))
1542− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1499+ :: Expression outer commons grp schemas params from (null 'PGjsonb)
1500+ -> Expression outer commons grp schemas params from (null ('PGvararray ('NotNull 'PGtext)))
1501+ -> Expression outer commons grp schemas params from (null 'PGjsonb)
1502+ -> Maybe (Expression outer commons grp schemas params from (null 'PGbool))
1503+ -> Expression outer commons grp schemas params from (null 'PGjsonb)
15431504jsonbSet tgt path val createMissing = case createMissing of
1544− Just m -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* m :* Nil)
1545− Nothing -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* Nil)
1505+ Just m -> unsafeFunctionHet "jsonb_set" (tgt :* path :* val :* m :* Nil)
1506+ Nothing -> unsafeFunctionHet "jsonb_set" (tgt :* path :* val :* Nil)
15461507
15471508-- | @ jsonbInsert target path new_value insert_after @
15481509--
@@ -1553,39 +1514,120 @@ jsonbSet tgt path val createMissing = case createMissing of
15531514-- exist. As with the path orientated operators, negative integers that appear
15541515-- in path count from the end of JSON arrays.
15551516jsonbInsert
1556− :: PGTextArray "jsonbInsert" arr
1557− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1558− -> Expression outer grp commons schemas params from (nullity arr)
1559− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1560− -> Maybe (Expression outer grp commons schemas params from (nullity 'PGbool))
1561− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1517+ :: Expression outer commons grp schemas params from (null 'PGjsonb)
1518+ -> Expression outer commons grp schemas params from (null ('PGvararray ('NotNull 'PGtext)))
1519+ -> Expression outer commons grp schemas params from (null 'PGjsonb)
1520+ -> Maybe (Expression outer commons grp schemas params from (null 'PGbool))
1521+ -> Expression outer commons grp schemas params from (null 'PGjsonb)
15621522jsonbInsert tgt path val insertAfter = case insertAfter of
1563− Just i -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* i :* Nil)
1564− Nothing -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* Nil)
1523+ Just i -> unsafeFunctionHet "jsonb_insert" (tgt :* path :* val :* i :* Nil)
1524+ Nothing -> unsafeFunctionHet "jsonb_insert" (tgt :* path :* val :* Nil)
15651525
15661526-- | Returns its argument as indented JSON text.
1567−jsonbPretty
1568− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1569− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1527+jsonbPretty :: null 'PGjsonb :--> null 'PGtext
15701528jsonbPretty = unsafeFunction "jsonb_pretty"
15711529
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"
15721607{-----------------------------------------
15731608aggregation
15741609-----------------------------------------}
15751610
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+
15761618{- |
15771619`Aggregate` functions compute a single result from a set of input values.
15781620`Aggregate` functions can be used as `GroupedBy` `Expression`s as well
15791621as `WindowFunction`s.
15801622-}
1581−class Aggregate expr1 expr2 aggr
1582− | aggr -> expr1, aggr -> expr2 where
1623+class Aggregate expr1 exprHet aggr
1624+ | aggr -> expr1, aggr -> exprHet where
15831625
15841626 -- | A special aggregation that does not require an input
15851627 --
15861628 -- >>> :{
15871629 -- let
1588− -- expression :: Expression '[] ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
1630+ -- expression :: Expression '[] commons ('Grouped bys) schemas params from ('NotNull 'PGint8)
15891631 -- expression = countStar
15901632 -- in printSQL expression
15911633 -- :}
@@ -1594,7 +1636,7 @@ class Aggregate expr1 expr2 aggr
15941636
15951637 -- | >>> :{
15961638 -- 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)
15981640 -- expression = count (All #col)
15991641 -- in printSQL expression
16001642 -- :}
@@ -1606,14 +1648,14 @@ class Aggregate expr1 expr2 aggr
16061648
16071649 -- | >>> :{
16081650 -- 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)
16101652 -- expression = sum_ (Distinct #col)
16111653 -- in printSQL expression
16121654 -- :}
16131655 -- sum(DISTINCT "col")
16141656 sum_
16151657 :: ty `In` PGNum
1616− => expr1 (nullity ty)
1658+ => expr1 (null ty)
16171659 -> aggr ('NotNull ty)
16181660
16191661 -- | input values, including nulls, concatenated into an array
@@ -1635,7 +1677,7 @@ class Aggregate expr1 expr2 aggr
16351677 the bitwise AND of all non-null input values, or null if none
16361678 >>> :{
16371679 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)
16391681 expression = bitAnd (Distinct #col)
16401682 in printSQL expression
16411683 :}
@@ -1643,7 +1685,7 @@ class Aggregate expr1 expr2 aggr
16431685 -}
16441686 bitAnd
16451687 :: int `In` PGIntegral
1646− => expr1 (nullity int)
1688+ => expr1 (null int)
16471689 -- ^ what to aggregate
16481690 -> aggr ('Null int)
16491691
@@ -1652,7 +1694,7 @@ class Aggregate expr1 expr2 aggr
16521694
16531695 >>> :{
16541696 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)
16561698 expression = bitOr (All #col)
16571699 in printSQL expression
16581700 :}
@@ -1660,7 +1702,7 @@ class Aggregate expr1 expr2 aggr
16601702 -}
16611703 bitOr
16621704 :: int `In` PGIntegral
1663− => expr1 (nullity int)
1705+ => expr1 (null int)
16641706 -- ^ what to aggregate
16651707 -> aggr ('Null int)
16661708
@@ -1669,14 +1711,14 @@ class Aggregate expr1 expr2 aggr
16691711
16701712 >>> :{
16711713 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)
16731715 winFun = boolAnd #col
16741716 in printSQL winFun
16751717 :}
16761718 bool_and("col")
16771719 -}
16781720 boolAnd
1679− :: expr1 (nullity 'PGbool)
1721+ :: expr1 (null 'PGbool)
16801722 -- ^ what to aggregate
16811723 -> aggr ('Null 'PGbool)
16821724
@@ -1685,14 +1727,14 @@ class Aggregate expr1 expr2 aggr
16851727
16861728 >>> :{
16871729 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)
16891731 expression = boolOr (All #col)
16901732 in printSQL expression
16911733 :}
16921734 bool_or(ALL "col")
16931735 -}
16941736 boolOr
1695− :: expr1(nullity 'PGbool)
1737+ :: expr1 (null 'PGbool)
16961738 -- ^ what to aggregate
16971739 -> aggr ('Null 'PGbool)
16981740
@@ -1701,185 +1743,185 @@ class Aggregate expr1 expr2 aggr
17011743
17021744 >>> :{
17031745 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)
17051747 expression = every (Distinct #col)
17061748 in printSQL expression
17071749 :}
17081750 every(DISTINCT "col")
17091751 -}
17101752 every
1711− :: expr1 (nullity 'PGbool)
1753+ :: expr1 (null 'PGbool)
17121754 -- ^ what to aggregate
17131755 -> aggr ('Null 'PGbool)
17141756
17151757 {- |maximum value of expression across all input values-}
17161758 max_
1717− :: expr1 (nullity ty)
1759+ :: expr1 (null ty)
17181760 -- ^ what to maximize
17191761 -> aggr ('Null ty)
17201762
17211763 -- | minimum value of expression across all input values
17221764 min_
1723− :: expr1 (nullity ty)
1765+ :: expr1 (null ty)
17241766 -- ^ what to minimize
17251767 -> aggr ('Null ty)
17261768
17271769 -- | the average (arithmetic mean) of all input values
17281770 avg
1729− :: expr1 (nullity ty)
1771+ :: expr1 (null ty)
17301772 -- ^ what to average
17311773 -> aggr ('Null (PGAvg ty))
17321774
17331775 {- | correlation coefficient
17341776 >>> :{
17351777 let
1736− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1737− expression = corr (All (#y :*: #x))
1778+ expression :: Expression '[] c ('Grouped g) s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1779+ expression = corr (All (#y *: #x))
17381780 in printSQL expression
17391781 :}
17401782 corr(ALL "y", "x")
17411783 -}
17421784 corr
1743− :: expr2 (nullity 'PGfloat8)
1785+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
17441786 -> aggr ('Null 'PGfloat8)
17451787
17461788 {- | population covariance
17471789 >>> :{
17481790 let
1749− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1750− expression = covarPop (All (#y :*: #x))
1791+ expression :: Expression '[] c ('Grouped g) s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1792+ expression = covarPop (All (#y *: #x))
17511793 in printSQL expression
17521794 :}
17531795 covar_pop(ALL "y", "x")
17541796 -}
17551797 covarPop
1756− :: expr2 (nullity 'PGfloat8)
1798+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
17571799 -> aggr ('Null 'PGfloat8)
17581800
17591801 {- | sample covariance
17601802 >>> :{
17611803 let
1762− winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1763− winFun = covarSamp (#y :*: #x)
1804+ winFun :: WindowFunction '[] c 'Ungrouped s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1805+ winFun = covarSamp (#y *: #x)
17641806 in printSQL winFun
17651807 :}
17661808 covar_samp("y", "x")
17671809 -}
17681810 covarSamp
1769− :: expr2 (nullity 'PGfloat8)
1811+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
17701812 -> aggr ('Null 'PGfloat8)
17711813
17721814 {- | average of the independent variable (sum(X)/N)
17731815 >>> :{
17741816 let
1775− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1776− expression = regrAvgX (All (#y :*: #x))
1817+ expression :: Expression '[] c ('Grouped g) s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1818+ expression = regrAvgX (All (#y *: #x))
17771819 in printSQL expression
17781820 :}
17791821 regr_avgx(ALL "y", "x")
17801822 -}
17811823 regrAvgX
1782− :: expr2 (nullity 'PGfloat8)
1824+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
17831825 -> aggr ('Null 'PGfloat8)
17841826
17851827 {- | average of the dependent variable (sum(Y)/N)
17861828 >>> :{
17871829 let
1788− winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1789− winFun = regrAvgY (#y :*: #x)
1830+ winFun :: WindowFunction '[] c 'Ungrouped s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1831+ winFun = regrAvgY (#y *: #x)
17901832 in printSQL winFun
17911833 :}
17921834 regr_avgy("y", "x")
17931835 -}
17941836 regrAvgY
1795− :: expr2 (nullity 'PGfloat8)
1837+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
17961838 -> aggr ('Null 'PGfloat8)
17971839
17981840 {- | number of input rows in which both expressions are nonnull
17991841 >>> :{
18001842 let
1801− winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGint8)
1802− winFun = regrCount (#y :*: #x)
1843+ winFun :: WindowFunction '[] c 'Ungrouped s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGint8)
1844+ winFun = regrCount (#y *: #x)
18031845 in printSQL winFun
18041846 :}
18051847 regr_count("y", "x")
18061848 -}
18071849 regrCount
1808− :: expr2 (nullity 'PGfloat8)
1850+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
18091851 -> aggr ('Null 'PGint8)
18101852
18111853 {- | y-intercept of the least-squares-fit linear equation determined by the (X, Y) pairs
18121854 >>> :{
18131855 let
1814− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1815− expression = regrIntercept (All (#y :*: #x))
1856+ expression :: Expression '[] c ('Grouped g) s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1857+ expression = regrIntercept (All (#y *: #x))
18161858 in printSQL expression
18171859 :}
18181860 regr_intercept(ALL "y", "x")
18191861 -}
18201862 regrIntercept
1821− :: expr2 (nullity 'PGfloat8)
1863+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
18221864 -> aggr ('Null 'PGfloat8)
18231865
18241866 -- | @regr_r2(Y, X)@, square of the correlation coefficient
18251867 regrR2
1826− :: expr2 (nullity 'PGfloat8)
1868+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
18271869 -> aggr ('Null 'PGfloat8)
18281870
18291871 -- | @regr_slope(Y, X)@, slope of the least-squares-fit linear equation
18301872 -- determined by the (X, Y) pairs
18311873 regrSlope
1832− :: expr2 (nullity 'PGfloat8)
1874+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
18331875 -> aggr ('Null 'PGfloat8)
18341876
18351877 -- | @regr_sxx(Y, X)@, sum(X^2) - sum(X)^2/N
18361878 -- (“sum of squares” of the independent variable)
18371879 regrSxx
1838− :: expr2 (nullity 'PGfloat8)
1880+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
18391881 -> aggr ('Null 'PGfloat8)
18401882
18411883 -- | @regr_sxy(Y, X)@, sum(X*Y) - sum(X) * sum(Y)/N
18421884 -- (“sum of products” of independent times dependent variable)
18431885 regrSxy
1844− :: expr2 (nullity 'PGfloat8)
1886+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
18451887 -> aggr ('Null 'PGfloat8)
18461888
18471889 -- | @regr_syy(Y, X)@, sum(Y^2) - sum(Y)^2/N
18481890 -- (“sum of squares” of the dependent variable)
18491891 regrSyy
1850− :: expr2 (nullity 'PGfloat8)
1892+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
18511893 -> aggr ('Null 'PGfloat8)
18521894
18531895 -- | historical alias for `stddevSamp`
18541896 stddev
1855− :: expr1 (nullity ty)
1897+ :: expr1 (null ty)
18561898 -> aggr ('Null (PGAvg ty))
18571899
18581900 -- | population standard deviation of the input values
18591901 stddevPop
1860− :: expr1 (nullity ty)
1902+ :: expr1 (null ty)
18611903 -> aggr ('Null (PGAvg ty))
18621904
18631905 -- | sample standard deviation of the input values
18641906 stddevSamp
1865− :: expr1 (nullity ty)
1907+ :: expr1 (null ty)
18661908 -> aggr ('Null (PGAvg ty))
18671909
18681910 -- | historical alias for `varSamp`
18691911 variance
1870− :: expr1 (nullity ty)
1912+ :: expr1 (null ty)
18711913 -> aggr ('Null (PGAvg ty))
18721914
18731915 -- | population variance of the input values
18741916 -- (square of the population standard deviation)
18751917 varPop
1876− :: expr1 (nullity ty)
1918+ :: expr1 (null ty)
18771919 -> aggr ('Null (PGAvg ty))
18781920
18791921 -- | sample variance of the input values
18801922 -- (square of the sample standard deviation)
18811923 varSamp
1882− :: expr1 (nullity ty)
1924+ :: expr1 (null ty)
18831925 -> aggr ('Null (PGAvg ty))
18841926
18851927{- |
@@ -1888,30 +1930,25 @@ class Aggregate expr1 expr2 aggr
18881930`Distinct` invokes the aggregate once for each distinct value of the expression
18891931(or distinct set of values, for multiple expressions) found in the input
18901932-}
1891−data Distinction expr ty
1933+data Distinction (expr :: kind -> Type) (ty :: kind)
18921934 = All (expr ty)
18931935 | Distinct (expr ty)
18941936 deriving (GHC.Generic,Show,Eq,Ord)
1895−instance NFData (Distinction (Expression outer grp commons schemas params from) ty)
1896−instance RenderSQL (Distinction (Expression outer grp commons schemas params from) ty) where
1937+instance NFData (Distinction (Expression outer commons grp schemas params from) ty)
1938+instance RenderSQL (Distinction (Expression outer commons grp schemas params from) ty) where
18971939 renderSQL = \case
18981940 All x -> "ALL" <+> renderSQL x
18991941 Distinct x -> "DISTINCT" <+> renderSQL x
1900−instance RenderSQL
1901− (Distinction
1902− ( Expression outer grp commons schemas params from
1903− :*:
1904− Expression outer grp commons schemas params from) xty ) where
1905− renderSQL = \case
1906− All (x :*: x') ->
1907− "ALL" <+> renderSQL x <> ", " <> renderSQL x'
1908− Distinct (x :*: x') ->
1909− "DISTINCT" <+> renderSQL x <> ", " <> renderSQL x'
1942+instance SOP.SListI tys => RenderSQL
1943+ (Distinction (NP (Expression outer commons grp schemas params from)) tys) where
1944+ renderSQL = \case
1945+ All xs -> "ALL" <+> renderCommaSeparated renderSQL xs
1946+ Distinct xs -> "DISTINCT" <+> renderCommaSeparated renderSQL xs
19101947
19111948instance Aggregate
1912− (Distinction (Expression outer 'Ungrouped commons schemas params from))
1913− (Distinction (Expression outer 'Ungrouped commons schemas params from :*: Expression outer 'Ungrouped commons schemas params from))
1914− (Expression outer ('Grouped bys) commons schemas params from) where
1949+ (Distinction (Expression outer commons 'Ungrouped schemas params from))
1950+ (Distinction (NP (Expression outer commons 'Ungrouped schemas params from)))
1951+ (Expression outer commons ('Grouped bys) schemas params from) where
19151952 countStar = UnsafeExpression "count(*)"
19161953 count = unsafeAggregate1 "count"
19171954 sum_ = unsafeAggregate1 "sum"
@@ -1926,18 +1963,18 @@ instance Aggregate
19261963 max_ = unsafeAggregate1 "max"
19271964 min_ = unsafeAggregate1 "min"
19281965 avg = unsafeAggregate1 "avg"
1929− corr = unsafeAggregate2 "corr"
1930− covarPop = unsafeAggregate2 "covar_pop"
1931− covarSamp = unsafeAggregate2 "covar_samp"
1932− regrAvgX = unsafeAggregate2 "regr_avgx"
1933− regrAvgY = unsafeAggregate2 "regr_avgy"
1934− regrCount = unsafeAggregate2 "regr_count"
1935− regrIntercept = unsafeAggregate2 "regr_intercept"
1936− regrR2 = unsafeAggregate2 "regr_r2"
1937− regrSlope = unsafeAggregate2 "regr_slope"
1938− regrSxx = unsafeAggregate2 "regr_sxx"
1939− regrSxy = unsafeAggregate2 "regr_sxy"
1940− regrSyy = unsafeAggregate2 "regr_syy"
1966+ corr = unsafeAggregateHet "corr"
1967+ covarPop = unsafeAggregateHet "covar_pop"
1968+ covarSamp = unsafeAggregateHet "covar_samp"
1969+ regrAvgX = unsafeAggregateHet "regr_avgx"
1970+ regrAvgY = unsafeAggregateHet "regr_avgy"
1971+ regrCount = unsafeAggregateHet "regr_count"
1972+ regrIntercept = unsafeAggregateHet "regr_intercept"
1973+ regrR2 = unsafeAggregateHet "regr_r2"
1974+ regrSlope = unsafeAggregateHet "regr_slope"
1975+ regrSxx = unsafeAggregateHet "regr_sxx"
1976+ regrSxy = unsafeAggregateHet "regr_sxy"
1977+ regrSyy = unsafeAggregateHet "regr_syy"
19411978 stddev = unsafeAggregate1 "stddev"
19421979 stddevPop = unsafeAggregate1 "stddev_pop"
19431980 stddevSamp = unsafeAggregate1 "stddev_samp"
@@ -1945,9 +1982,9 @@ instance Aggregate
19451982 varPop = unsafeAggregate1 "var_pop"
19461983 varSamp = unsafeAggregate1 "var_samp"
19471984instance Aggregate
1948− (Expression outer grp commons schemas params from)
1949− (Expression outer grp commons schemas params from :*: Expression outer grp commons schemas params from)
1950− (WindowFunction outer grp commons schemas params from) where
1985+ (Expression outer commons grp schemas params from)
1986+ (NP (Expression outer commons grp schemas params from))
1987+ (WindowFunction outer commons grp schemas params from) where
19511988 countStar = UnsafeWindowFunction "count(*)"
19521989 count = unsafeWindowFunction1 "count"
19531990 sum_ = unsafeWindowFunction1 "sum"
@@ -1962,18 +1999,18 @@ instance Aggregate
19621999 max_ = unsafeWindowFunction1 "max"
19632000 min_ = unsafeWindowFunction1 "min"
19642001 avg = unsafeWindowFunction1 "avg"
1965− corr = unsafeWindowFunction2 "corr"
1966− covarPop = unsafeWindowFunction2 "covar_pop"
1967− covarSamp = unsafeWindowFunction2 "covar_samp"
1968− regrAvgX = unsafeWindowFunction2 "regr_avgx"
1969− regrAvgY = unsafeWindowFunction2 "regr_avgy"
1970− regrCount = unsafeWindowFunction2 "regr_count"
1971− regrIntercept = unsafeWindowFunction2 "regr_intercept"
1972− regrR2 = unsafeWindowFunction2 "regr_r2"
1973− regrSlope = unsafeWindowFunction2 "regr_slope"
1974− regrSxx = unsafeWindowFunction2 "regr_sxx"
1975− regrSxy = unsafeWindowFunction2 "regr_sxy"
1976− regrSyy = unsafeWindowFunction2 "regr_syy"
2002+ corr = unsafeWindowFunctionHet "corr"
2003+ covarPop = unsafeWindowFunctionHet "covar_pop"
2004+ covarSamp = unsafeWindowFunctionHet "covar_samp"
2005+ regrAvgX = unsafeWindowFunctionHet "regr_avgx"
2006+ regrAvgY = unsafeWindowFunctionHet "regr_avgy"
2007+ regrCount = unsafeWindowFunctionHet "regr_count"
2008+ regrIntercept = unsafeWindowFunctionHet "regr_intercept"
2009+ regrR2 = unsafeWindowFunctionHet "regr_r2"
2010+ regrSlope = unsafeWindowFunctionHet "regr_slope"
2011+ regrSxx = unsafeWindowFunctionHet "regr_sxx"
2012+ regrSxy = unsafeWindowFunctionHet "regr_sxy"
2013+ regrSyy = unsafeWindowFunctionHet "regr_syy"
19772014 stddev = unsafeWindowFunction1 "stddev"
19782015 stddevPop = unsafeWindowFunction1 "stddev_pop"
19792016 stddevSamp = unsafeWindowFunction1 "stddev_samp"
@@ -1984,18 +2021,9 @@ instance Aggregate
19842021-- | escape hatch to define aggregate functions
19852022unsafeAggregate1
19862023 :: ByteString -- ^ aggregate function
1987− -> Distinction (Expression outer 'Ungrouped commons schemas params from) xty
1988− -> Expression outer ('Grouped bys) commons schemas params from yty
1989−unsafeAggregate1 fun x = UnsafeExpression $ mconcat
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, ")"]
2024+ -> Distinction (Expression outer commons 'Ungrouped schemas params from) x
2025+ -> Expression outer commons ('Grouped bys) schemas params from y
2026+unsafeAggregate1 fun x = UnsafeExpression $ fun <> parenthesized (renderSQL x)
19992027
20002028-- | A type family that calculates `PGAvg` type of a `PGType`.
20012029type family PGAvg ty where
@@ -2025,7 +2053,7 @@ instance RenderSQL (TypeExpression schemas ty) where
20252053typedef
20262054 :: (Has sch schemas schema, Has td schema ('Typedef ty))
20272055 => QualifiedAlias sch td
2028− -> TypeExpression schemas (nullity ty)
2056+ -> TypeExpression schemas (null ty)
20292057typedef = UnsafeTypeExpression . renderSQL
20302058
20312059-- | The composite type corresponding to a `Table` definition can be expressed
@@ -2033,7 +2061,7 @@ typedef = UnsafeTypeExpression . renderSQL
20332061typetable
20342062 :: (Has sch schemas schema, Has tab schema ('Table table))
20352063 => QualifiedAlias sch tab
2036− -> TypeExpression schemas (nullity ('PGcomposite (TableToRow table)))
2064+ -> TypeExpression schemas (null ('PGcomposite (TableToRow table)))
20372065typetable = UnsafeTypeExpression . renderSQL
20382066
20392067-- | The composite type corresponding to a `View` definition can be expressed
@@ -2041,101 +2069,101 @@ typetable = UnsafeTypeExpression . renderSQL
20412069typeview
20422070 :: (Has sch schemas schema, Has vw schema ('View view))
20432071 => QualifiedAlias sch vw
2044− -> TypeExpression schemas (nullity ('PGcomposite view))
2072+ -> TypeExpression schemas (null ('PGcomposite view))
20452073typeview = UnsafeTypeExpression . renderSQL
20462074
20472075-- | logical Boolean (true/false)
2048−bool :: TypeExpression schemas (nullity 'PGbool)
2076+bool :: TypeExpression schemas (null 'PGbool)
20492077bool = UnsafeTypeExpression "bool"
20502078-- | signed two-byte integer
2051−int2, smallint :: TypeExpression schemas (nullity 'PGint2)
2079+int2, smallint :: TypeExpression schemas (null 'PGint2)
20522080int2 = UnsafeTypeExpression "int2"
20532081smallint = UnsafeTypeExpression "smallint"
20542082-- | signed four-byte integer
2055−int4, int, integer :: TypeExpression schemas (nullity 'PGint4)
2083+int4, int, integer :: TypeExpression schemas (null 'PGint4)
20562084int4 = UnsafeTypeExpression "int4"
20572085int = UnsafeTypeExpression "int"
20582086integer = UnsafeTypeExpression "integer"
20592087-- | signed eight-byte integer
2060−int8, bigint :: TypeExpression schemas (nullity 'PGint8)
2088+int8, bigint :: TypeExpression schemas (null 'PGint8)
20612089int8 = UnsafeTypeExpression "int8"
20622090bigint = UnsafeTypeExpression "bigint"
20632091-- | arbitrary precision numeric type
2064−numeric :: TypeExpression schemas (nullity 'PGnumeric)
2092+numeric :: TypeExpression schemas (null 'PGnumeric)
20652093numeric = UnsafeTypeExpression "numeric"
20662094-- | single precision floating-point number (4 bytes)
2067−float4, real :: TypeExpression schemas (nullity 'PGfloat4)
2095+float4, real :: TypeExpression schemas (null 'PGfloat4)
20682096float4 = UnsafeTypeExpression "float4"
20692097real = UnsafeTypeExpression "real"
20702098-- | double precision floating-point number (8 bytes)
2071−float8, doublePrecision :: TypeExpression schemas (nullity 'PGfloat8)
2099+float8, doublePrecision :: TypeExpression schemas (null 'PGfloat8)
20722100float8 = UnsafeTypeExpression "float8"
20732101doublePrecision = UnsafeTypeExpression "double precision"
20742102-- | currency amount
2075−money :: TypeExpression schema (nullity 'PGmoney)
2103+money :: TypeExpression schema (null 'PGmoney)
20762104money = UnsafeTypeExpression "money"
20772105-- | variable-length character string
2078−text :: TypeExpression schemas (nullity 'PGtext)
2106+text :: TypeExpression schemas (null 'PGtext)
20792107text = UnsafeTypeExpression "text"
20802108-- | fixed-length character string
20812109char, character
2082− :: forall n schemas nullity. (KnownNat n, 1 <= n)
2083− => TypeExpression schemas (nullity ('PGchar n))
2110+ :: forall n schemas null. (KnownNat n, 1 <= n)
2111+ => TypeExpression schemas (null ('PGchar n))
20842112char = UnsafeTypeExpression $ "char(" <> renderNat @n <> ")"
20852113character = UnsafeTypeExpression $ "character(" <> renderNat @n <> ")"
20862114-- | variable-length character string
20872115varchar, characterVarying
2088− :: forall n schemas nullity. (KnownNat n, 1 <= n)
2089− => TypeExpression schemas (nullity ('PGvarchar n))
2116+ :: forall n schemas null. (KnownNat n, 1 <= n)
2117+ => TypeExpression schemas (null ('PGvarchar n))
20902118varchar = UnsafeTypeExpression $ "varchar(" <> renderNat @n <> ")"
20912119characterVarying = UnsafeTypeExpression $
20922120 "character varying(" <> renderNat @n <> ")"
20932121-- | binary data ("byte array")
2094−bytea :: TypeExpression schemas (nullity 'PGbytea)
2122+bytea :: TypeExpression schemas (null 'PGbytea)
20952123bytea = UnsafeTypeExpression "bytea"
20962124-- | date and time (no time zone)
2097−timestamp :: TypeExpression schemas (nullity 'PGtimestamp)
2125+timestamp :: TypeExpression schemas (null 'PGtimestamp)
20982126timestamp = UnsafeTypeExpression "timestamp"
20992127-- | date and time, including time zone
2100−timestampWithTimeZone :: TypeExpression schemas (nullity 'PGtimestamptz)
2128+timestampWithTimeZone :: TypeExpression schemas (null 'PGtimestamptz)
21012129timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
21022130-- | calendar date (year, month, day)
2103−date :: TypeExpression schemas (nullity 'PGdate)
2131+date :: TypeExpression schemas (null 'PGdate)
21042132date = UnsafeTypeExpression "date"
21052133-- | time of day (no time zone)
2106−time :: TypeExpression schemas (nullity 'PGtime)
2134+time :: TypeExpression schemas (null 'PGtime)
21072135time = UnsafeTypeExpression "time"
21082136-- | time of day, including time zone
2109−timeWithTimeZone :: TypeExpression schemas (nullity 'PGtimetz)
2137+timeWithTimeZone :: TypeExpression schemas (null 'PGtimetz)
21102138timeWithTimeZone = UnsafeTypeExpression "time with time zone"
21112139-- | time span
2112−interval :: TypeExpression schemas (nullity 'PGinterval)
2140+interval :: TypeExpression schemas (null 'PGinterval)
21132141interval = UnsafeTypeExpression "interval"
21142142-- | universally unique identifier
2115−uuid :: TypeExpression schemas (nullity 'PGuuid)
2143+uuid :: TypeExpression schemas (null 'PGuuid)
21162144uuid = UnsafeTypeExpression "uuid"
21172145-- | IPv4 or IPv6 host address
2118−inet :: TypeExpression schemas (nullity 'PGinet)
2146+inet :: TypeExpression schemas (null 'PGinet)
21192147inet = UnsafeTypeExpression "inet"
21202148-- | textual JSON data
2121−json :: TypeExpression schemas (nullity 'PGjson)
2149+json :: TypeExpression schemas (null 'PGjson)
21222150json = UnsafeTypeExpression "json"
21232151-- | binary JSON data, decomposed
2124−jsonb :: TypeExpression schemas (nullity 'PGjsonb)
2152+jsonb :: TypeExpression schemas (null 'PGjsonb)
21252153jsonb = UnsafeTypeExpression "jsonb"
21262154-- | variable length array
21272155vararray
21282156 :: TypeExpression schemas pg
2129− -> TypeExpression schemas (nullity ('PGvararray pg))
2157+ -> TypeExpression schemas (null ('PGvararray pg))
21302158vararray ty = UnsafeTypeExpression $ renderSQL ty <> "[]"
21312159-- | fixed length array
21322160--
21332161-- >>> renderSQL (fixarray @'[2] json)
21342162-- "json[2]"
21352163fixarray
2136− :: forall dims schemas nullity pg. SOP.All KnownNat dims
2164+ :: forall dims schemas null pg. SOP.All KnownNat dims
21372165 => TypeExpression schemas pg
2138− -> TypeExpression schemas (nullity ('PGfixarray dims pg))
2166+ -> TypeExpression schemas (null ('PGfixarray dims pg))
21392167fixarray ty = UnsafeTypeExpression $
21402168 renderSQL ty <> renderDims @dims
21412169 where
@@ -2148,38 +2176,45 @@ fixarray ty = UnsafeTypeExpression $
21482176 $ hcmap (SOP.Proxy @KnownNat)
21492177 (K . fromString . show . natVal)
21502178 (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"
21512183
21522184-- | `pgtype` is a demoted version of a `PGType`
21532185class PGTyped schemas (ty :: NullityType) where
21542186 pgtype :: TypeExpression schemas ty
2155−instance PGTyped schemas (nullity 'PGbool) where pgtype = bool
2156−instance PGTyped schemas (nullity 'PGint2) where pgtype = int2
2157−instance PGTyped schemas (nullity 'PGint4) where pgtype = int4
2158−instance PGTyped schemas (nullity 'PGint8) where pgtype = int8
2159−instance PGTyped schemas (nullity 'PGnumeric) where pgtype = numeric
2160−instance PGTyped schemas (nullity 'PGfloat4) where pgtype = float4
2161−instance PGTyped schemas (nullity 'PGfloat8) where pgtype = float8
2162−instance PGTyped schemas (nullity 'PGtext) where pgtype = text
2187+instance PGTyped schemas (null 'PGbool) where pgtype = bool
2188+instance PGTyped schemas (null 'PGint2) where pgtype = int2
2189+instance PGTyped schemas (null 'PGint4) where pgtype = int4
2190+instance PGTyped schemas (null 'PGint8) where pgtype = int8
2191+instance PGTyped schemas (null 'PGnumeric) where pgtype = numeric
2192+instance PGTyped schemas (null 'PGfloat4) where pgtype = float4
2193+instance PGTyped schemas (null 'PGfloat8) where pgtype = float8
2194+instance PGTyped schemas (null 'PGmoney) where pgtype = money
2195+instance PGTyped schemas (null 'PGtext) where pgtype = text
21632196instance (KnownNat n, 1 <= n)
2164− => PGTyped schemas (nullity ('PGchar n)) where pgtype = char @n
2197+ => PGTyped schemas (null ('PGchar n)) where pgtype = char @n
21652198instance (KnownNat n, 1 <= n)
2166− => PGTyped schemas (nullity ('PGvarchar n)) where pgtype = varchar @n
2167−instance PGTyped schemas (nullity 'PGbytea) where pgtype = bytea
2168−instance PGTyped schemas (nullity 'PGtimestamp) where pgtype = timestamp
2169−instance PGTyped schemas (nullity 'PGtimestamptz) where pgtype = timestampWithTimeZone
2170−instance PGTyped schemas (nullity 'PGdate) where pgtype = date
2171−instance PGTyped schemas (nullity 'PGtime) where pgtype = time
2172−instance PGTyped schemas (nullity 'PGtimetz) where pgtype = timeWithTimeZone
2173−instance PGTyped schemas (nullity 'PGinterval) where pgtype = interval
2174−instance PGTyped schemas (nullity 'PGuuid) where pgtype = uuid
2175−instance PGTyped schemas (nullity 'PGjson) where pgtype = json
2176−instance PGTyped schemas (nullity 'PGjsonb) where pgtype = jsonb
2199+ => PGTyped schemas (null ('PGvarchar n)) where pgtype = varchar @n
2200+instance PGTyped schemas (null 'PGbytea) where pgtype = bytea
2201+instance PGTyped schemas (null 'PGtimestamp) where pgtype = timestamp
2202+instance PGTyped schemas (null 'PGtimestamptz) where pgtype = timestampWithTimeZone
2203+instance PGTyped schemas (null 'PGdate) where pgtype = date
2204+instance PGTyped schemas (null 'PGtime) where pgtype = time
2205+instance PGTyped schemas (null 'PGtimetz) where pgtype = timeWithTimeZone
2206+instance PGTyped schemas (null 'PGinterval) where pgtype = interval
2207+instance PGTyped schemas (null 'PGuuid) where pgtype = uuid
2208+instance PGTyped schemas (null 'PGjson) where pgtype = json
2209+instance PGTyped schemas (null 'PGjsonb) where pgtype = jsonb
21772210instance PGTyped schemas ty
2178− => PGTyped schemas (nullity ('PGvararray ty)) where
2211+ => PGTyped schemas (null ('PGvararray ty)) where
21792212 pgtype = vararray (pgtype @schemas @ty)
21802213instance (SOP.All KnownNat dims, PGTyped schemas ty)
2181− => PGTyped schemas (nullity ('PGfixarray dims ty)) where
2214+ => PGTyped schemas (null ('PGfixarray dims ty)) where
21822215 pgtype = fixarray @dims (pgtype @schemas @ty)
2216+instance PGTyped schemas (null 'PGtsvector) where pgtype = tsvector
2217+instance PGTyped schemas (null 'PGtsquery) where pgtype = tsquery
21832218
21842219{-----------------------------------------
21852220Sorting
@@ -2194,26 +2229,26 @@ Sorting
21942229-- `AscNullsLast`, `DescNullsFirst` and `DescNullsLast` options are used to
21952230-- determine whether nulls appear before or after non-null values in the sort
21962231-- 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
21982233 Asc
2199− :: Expression outer grp commons schemas params from ('NotNull ty)
2200− -> SortExpression outer grp commons schemas params from
2234+ :: Expression outer commons grp schemas params from ('NotNull ty)
2235+ -> SortExpression outer commons grp schemas params from
22012236 Desc
2202− :: Expression outer grp commons schemas params from ('NotNull ty)
2203− -> SortExpression outer grp commons schemas params from
2237+ :: Expression outer commons grp schemas params from ('NotNull ty)
2238+ -> SortExpression outer commons grp schemas params from
22042239 AscNullsFirst
2205− :: Expression outer grp commons schemas params from ('Null ty)
2206− -> SortExpression outer grp commons schemas params from
2240+ :: Expression outer commons grp schemas params from ('Null ty)
2241+ -> SortExpression outer commons grp schemas params from
22072242 AscNullsLast
2208− :: Expression outer grp commons schemas params from ('Null ty)
2209− -> SortExpression outer grp commons schemas params from
2243+ :: Expression outer commons grp schemas params from ('Null ty)
2244+ -> SortExpression outer commons grp schemas params from
22102245 DescNullsFirst
2211− :: Expression outer grp commons schemas params from ('Null ty)
2212− -> SortExpression outer grp commons schemas params from
2246+ :: Expression outer commons grp schemas params from ('Null ty)
2247+ -> SortExpression outer commons grp schemas params from
22132248 DescNullsLast
2214− :: Expression outer grp commons schemas params from ('Null ty)
2215− -> SortExpression outer grp commons schemas params from
2216−deriving instance Show (SortExpression outer grp commons schemas params from)
2249+ :: Expression outer commons grp schemas params from ('Null ty)
2250+ -> SortExpression outer commons grp schemas params from
2251+deriving instance Show (SortExpression outer commons grp schemas params from)
22172252
22182253{- |
22192254The `orderBy` clause causes the result rows of a `Squeal.PostgreSQL.Query.TableExpression`
@@ -2228,12 +2263,12 @@ using `orderBy` within `Squeal.PostgreSQL.Query.Over`.
22282263-}
22292264class OrderBy expr where
22302265 orderBy
2231− :: [SortExpression outer grp commons schemas params from]
2232− -> expr outer grp commons schemas params from
2233− -> expr outer grp commons schemas params from
2266+ :: [SortExpression outer commons grp schemas params from]
2267+ -> expr outer commons grp schemas params from
2268+ -> expr outer commons grp schemas params from
22342269
22352270-- | 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
22372272 renderSQL = \case
22382273 Asc expression -> renderSQL expression <+> "ASC"
22392274 Desc expression -> renderSQL expression <+> "DESC"
@@ -2246,12 +2281,12 @@ instance RenderSQL (SortExpression outer grp commons schemas params from) where
22462281
22472282-- | A `WindowDefinition` is a set of table rows that are somehow related
22482283-- to the current row
2249−data WindowDefinition outer grp commons schemas params from where
2284+data WindowDefinition outer commons grp schemas params from where
22502285 WindowDefinition
22512286 :: SListI bys
2252− => NP (Expression outer grp commons schemas params from) bys
2253− -> [SortExpression outer grp commons schemas params from]
2254− -> WindowDefinition outer grp commons schemas params from
2287+ => NP (Expression outer commons grp schemas params from) bys
2288+ -> [SortExpression outer commons grp schemas params from]
2289+ -> WindowDefinition outer commons grp schemas params from
22552290
22562291instance OrderBy WindowDefinition where
22572292 orderBy sortsR (WindowDefinition parts sortsL)
@@ -2277,8 +2312,8 @@ the same partition as the current row.
22772312-}
22782313partitionBy
22792314 :: SListI bys
2280− => NP (Expression outer grp commons schemas params from) bys
2281− -> WindowDefinition outer grp commons schemas params from
2315+ => NP (Expression outer commons grp schemas params from) bys
2316+ -> WindowDefinition outer commons grp schemas params from
22822317partitionBy bys = WindowDefinition bys []
22832318
22842319{- |
@@ -2293,8 +2328,8 @@ just the current row of the query result.
22932328-}
22942329newtype WindowFunction
22952330 (outer :: FromType)
2296− (grp :: Grouping)
22972331 (commons :: FromType)
2332+ (grp :: Grouping)
22982333 (schemas :: SchemasType)
22992334 (params :: [NullityType])
23002335 (from :: FromType)
@@ -2302,49 +2337,62 @@ newtype WindowFunction
23022337 = UnsafeWindowFunction { renderWindowFunction :: ByteString }
23032338 deriving (GHC.Generic,Show,Eq,Ord,NFData)
23042339
2305−instance RenderSQL (WindowFunction outer grp commons schemas params from ty) where
2340+instance RenderSQL (WindowFunction outer commons grp schemas params from ty) where
23062341 renderSQL = renderWindowFunction
23072342
2308−unsafeWindowFunction1
2309− :: ByteString
2310− -> Expression outer grp commons schemas params from ty0
2311− -> WindowFunction outer grp commons schemas params from ty1
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+
2362+unsafeWindowFunction1 :: ByteString -> WinFun1 x y
23122363unsafeWindowFunction1 fun x
23132364 = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x)
23142365
2315−unsafeWindowFunction2
2316− :: ByteString
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')
2366+unsafeWindowFunctionHet :: SOP.SListI xs => ByteString -> WinFunHet xs y
2367+unsafeWindowFunctionHet fun xs = UnsafeWindowFunction $ fun <>
2368+ parenthesized (renderCommaSeparated renderSQL xs)
23212369
23222370{- | rank of the current row with gaps; same as `rowNumber` of its first peer
23232371>>> printSQL rank
23242372rank()
23252373-}
2326−rank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2374+rank :: WinFun0 ('NotNull 'PGint8)
23272375rank = UnsafeWindowFunction "rank()"
23282376
23292377{- | number of the current row within its partition, counting from 1
23302378>>> printSQL rowNumber
23312379row_number()
23322380-}
2333−rowNumber :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2381+rowNumber :: WinFun0 ('NotNull 'PGint8)
23342382rowNumber = UnsafeWindowFunction "row_number()"
23352383
23362384{- | rank of the current row without gaps; this function counts peer groups
23372385>>> printSQL denseRank
23382386dense_rank()
23392387-}
2340−denseRank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2388+denseRank :: WinFun0 ('NotNull 'PGint8)
23412389denseRank = UnsafeWindowFunction "dense_rank()"
23422390
23432391{- | relative rank of the current row: (rank - 1) / (total partition rows - 1)
23442392>>> printSQL percentRank
23452393percent_rank()
23462394-}
2347−percentRank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGfloat8)
2395+percentRank :: WinFun0 ('NotNull 'PGfloat8)
23482396percentRank = UnsafeWindowFunction "percent_rank()"
23492397
23502398{- | cumulative distribution: (number of partition rows
@@ -2352,16 +2400,13 @@ preceding or peer with current row) / total partition rows
23522400>>> printSQL cumeDist
23532401cume_dist()
23542402-}
2355−cumeDist :: WindowFunction outer grp commons schemas params from ('NotNull 'PGfloat8)
2403+cumeDist :: WinFun0 ('NotNull 'PGfloat8)
23562404cumeDist = UnsafeWindowFunction "cume_dist()"
23572405
23582406{- | integer ranging from 1 to the argument value,
23592407dividing the partition as equally as possible
23602408-}
2361−ntile
2362− :: Expression outer grp commons schemas params from ('NotNull 'PGint4)
2363− -- ^ num buckets
2364− -> WindowFunction outer grp commons schemas params from ('NotNull 'PGint4)
2409+ntile :: WinFun1 ('NotNull 'PGint4) ('NotNull 'PGint4)
23652410ntile = unsafeWindowFunction1 "ntile"
23662411
23672412{- | 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
23692414(which must be of the same type as value). Both offset and default are
23702415evaluated with respect to the current row.
23712416-}
2372−lag
2373− :: Expression outer grp commons schemas params from ty
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]))
2417+lag :: WinFunHet '[ty, 'NotNull 'PGint4, ty] ty
2418+lag = unsafeWindowFunctionHet "lag"
23832419
23842420{- | returns value evaluated at the row that is offset rows after the current
23852421row within the partition; if there is no such row, instead return default
23862422(which must be of the same type as value). Both offset and default are
23872423evaluated with respect to the current row.
23882424-}
2389−lead
2390− :: Expression outer grp commons schemas params from ty
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]))
2425+lead :: WinFunHet '[ty, 'NotNull 'PGint4, ty] ty
2426+lead = unsafeWindowFunctionHet "lag"
24002427
24012428{- | returns value evaluated at the row that is the
24022429first row of the window frame
24032430-}
2404−firstValue
2405− :: Expression outer grp commons schemas params from ty
2406− -- ^ value
2407− -> WindowFunction outer grp commons schemas params from ty
2431+firstValue :: WinFun1 ty ty
24082432firstValue = unsafeWindowFunction1 "first_value"
24092433
24102434{- | returns value evaluated at the row that is the
24112435last row of the window frame
24122436-}
2413−lastValue
2414− :: Expression outer grp commons schemas params from ty
2415− -- ^ value
2416− -> WindowFunction outer grp commons schemas params from ty
2437+lastValue :: WinFun1 ty ty
24172438lastValue = unsafeWindowFunction1 "last_value"
24182439
24192440{- | returns value evaluated at the row that is the nth
24202441row of the window frame (counting from 1); null if no such row
24212442-}
2422−nthValue
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"
2429− <> parenthesized (commaSeparated [renderSQL value, renderSQL nth])
2443+nthValue :: WinFunHet '[null ty, 'NotNull 'PGint4] ('Null ty)
2444+nthValue = unsafeWindowFunctionHet "nth_value"
24302445
24312446{-|
24322447Create date from year, month and day fields
24332448
2434−>>> printSQL (makeDate 1984 7 3)
2449+>>> printSQL (makeDate (1984 :* 7 *: 3))
24352450make_date(1984, 7, 3)
24362451-}
2437−makeDate
2438− :: Expression outer grp commons schemas params from (nullity 'PGint4)
2439− -- ^ year
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" <>
2446− parenthesized (renderCommaSeparated renderSQL (y :* m :* d :* Nil))
2452+makeDate :: FunctionHet
2453+ '[ null 'PGint4, null 'PGint4, null 'PGint4 ]
2454+ ( null 'PGdate )
2455+makeDate = unsafeFunctionHet "make_date"
24472456
24482457{-|
24492458Create time from hour, minute and seconds fields
24502459
2451−>>> printSQL (makeTime 8 15 23.5)
2460+>>> printSQL (makeTime (8 :* 15 *: 23.5))
24522461make_time(8, 15, 23.5)
24532462-}
2454−makeTime
2455− :: Expression outer grp commons schemas params from (nullity 'PGint4)
2456− -- ^ hour
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" <>
2463− parenthesized (renderCommaSeparated renderSQL (h :* m :* s :* Nil))
2463+makeTime :: FunctionHet
2464+ '[ null 'PGint4, null 'PGint4, null 'PGfloat8 ]
2465+ ( null 'PGtime )
2466+makeTime = unsafeFunctionHet "make_time"
24642467
24652468{-|
24662469Create timestamp from year, month, day, hour, minute and seconds fields
24672470
2468−>>> printSQL (makeTimestamp 2013 7 15 8 15 23.5)
2471+>>> printSQL (makeTimestamp (2013 :* 7 :* 15 :* 8 :* 15 *: 23.5))
24692472make_timestamp(2013, 7, 15, 8, 15, 23.5)
24702473-}
2471−makeTimestamp
2472− :: Expression outer grp commons schemas params from (nullity 'PGint4)
2473− -- ^ year
2474− -> Expression outer grp commons schemas params from (nullity 'PGint4)
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 =
2486− UnsafeExpression $ "make_timestamp" <> parenthesized
2487− (renderCommaSeparated renderSQL (y :* mon :* d :* h :* m :* s :* Nil))
2474+makeTimestamp :: FunctionHet
2475+ '[ null 'PGint4, null 'PGint4, null 'PGint4
2476+ , null 'PGint4, null 'PGint4, null 'PGfloat8 ]
2477+ ( null 'PGtimestamp )
2478+makeTimestamp = unsafeFunctionHet "make_timestamp"
24882479
24892480{-|
24902481Create timestamp with time zone from
24912482year, month, day, hour, minute and seconds fields;
24922483the current time zone is used
24932484
2494−>>> printSQL (makeTimestamptz 2013 7 15 8 15 23.5)
2485+>>> printSQL (makeTimestamptz (2013 :* 7 :* 15 :* 8 :* 15 *: 23.5))
24952486make_timestamptz(2013, 7, 15, 8, 15, 23.5)
24962487-}
2497−makeTimestamptz
2498− :: Expression outer grp commons schemas params from (nullity 'PGint4)
2499− -- ^ year
2500− -> Expression outer grp commons schemas params from (nullity 'PGint4)
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 =
2512− UnsafeExpression $ "make_timestamptz" <> parenthesized
2513− (renderCommaSeparated renderSQL (y :* mon :* d :* h :* m :* s :* Nil))
2488+makeTimestamptz :: FunctionHet
2489+ '[ null 'PGint4, null 'PGint4, null 'PGint4
2490+ , null 'PGint4, null 'PGint4, null 'PGfloat8 ]
2491+ ( null 'PGtimestamptz )
2492+makeTimestamptz = unsafeFunctionHet "make_timestamptz"
25142493
25152494{-|
25162495Affine space operations on time types.
25172496-}
25182497class TimeOp time diff | time -> diff where
25192498 {-|
2520− >>> printSQL (makeDate 1984 7 3 !+ 365)
2499+ >>> printSQL (makeDate (1984 :* 7 *: 3) !+ 365)
25212500 (make_date(1984, 7, 3) + 365)
25222501 -}
2523− (!+)
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)
2502+ (!+) :: Operator (null time) (null diff) (null time)
25272503 (!+) = unsafeBinaryOp "+"
25282504 {-|
2529− >>> printSQL (365 +! makeDate 1984 7 3)
2505+ >>> printSQL (365 +! makeDate (1984 :* 7 *: 3))
25302506 (365 + make_date(1984, 7, 3))
25312507 -}
2532− (+!)
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)
2508+ (+!) :: Operator (null diff) (null time) (null time)
25362509 (+!) = unsafeBinaryOp "+"
25372510 {-|
2538− >>> printSQL (makeDate 1984 7 3 !- 365)
2511+ >>> printSQL (makeDate (1984 :* 7 *: 3) !- 365)
25392512 (make_date(1984, 7, 3) - 365)
25402513 -}
2541− (!-)
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)
2514+ (!-) :: Operator (null time) (null diff) (null time)
25452515 (!-) = unsafeBinaryOp "-"
25462516 {-|
2547− >>> printSQL (makeDate 1984 7 3 !-! currentDate)
2517+ >>> printSQL (makeDate (1984 :* 7 *: 3) !-! currentDate)
25482518 (make_date(1984, 7, 3) - CURRENT_DATE)
25492519 -}
2550− (!-!)
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)
2520+ (!-!) :: Operator (null time) (null time) (null diff)
25542521 (!-!) = unsafeBinaryOp "-"
25552522instance TimeOp 'PGtimestamp 'PGinterval
25562523instance TimeOp 'PGtimestamptz 'PGinterval
@@ -2562,3 +2529,30 @@ infixl 6 !+
25622529infixl 6 +!
25632530infixl 6 !-
25642531infixl 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 <> "'"