@@ -11,21 +11,15 @@ Squeal expressions are the atoms used to build statements.
11{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
11{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
12{-# LANGUAGE
12{-# LANGUAGE
13 AllowAmbiguousTypes
13 AllowAmbiguousTypes
15 , DeriveGeneric
14 , DeriveGeneric
17 , FlexibleContexts
15 , FlexibleContexts
18 , FlexibleInstances
16 , FlexibleInstances
19 , FunctionalDependencies
17 , FunctionalDependencies
21 , GeneralizedNewtypeDeriving
18 , GeneralizedNewtypeDeriving
22 , LambdaCase
19 , LambdaCase
23 , MagicHash
20 , MagicHash
24 , OverloadedStrings
21 , OverloadedStrings
27 , ScopedTypeVariables
22 , ScopedTypeVariables
29 , TypeApplications
23 , TypeApplications
30 , TypeFamilies
24 , TypeFamilies
31 , TypeInType
25 , TypeInType
@@ -33,21 +27,23 @@ Squeal expressions are the atoms used to build statements.
33 , UndecidableInstances
27 , UndecidableInstances
36−module Squeal.PostgreSQL.Expression
30+module Squeal.PostgreSQL.Expression
37 ( -- * Expression
31 ( -- * Expression
38 Expression (UnsafeExpression, renderExpression)
32 Expression (UnsafeExpression, renderExpression)
39− , HasParameter (param)
33+ , HasParameter (parameter)
40 -- ** Null
35 -- ** Null
42− , unNull
37+ , notNull
43 , coalesce
38 , coalesce
44 , fromNull
39 , fromNull
46− , isn'tNull
41+ , isNotNull
47 , matchNull
42 , matchNull
49− -- ** Arrays
44+ -- ** Collections
51 -- ** Functions
47 -- ** Functions
52 , unsafeBinaryOp
48 , unsafeBinaryOp
53 , unsafeUnaryOp
49 , unsafeUnaryOp
@@ -97,8 +93,6 @@ module Squeal.PostgreSQL.Expression
97 , count, countDistinct
93 , count, countDistinct
98 , every, everyDistinct
94 , every, everyDistinct
99 , max_, maxDistinct, min_, minDistinct
95 , max_, maxDistinct, min_, minDistinct
101− , Table (UnsafeTable, renderTable)
102 -- * Types
96 -- * Types
103 , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
97 , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
104 , PGTyped (pgtype)
98 , PGTyped (pgtype)
@@ -115,12 +109,6 @@ module Squeal.PostgreSQL.Expression
117 , doublePrecision
111 , doublePrecision
126 , character
114 , character
@@ -139,8 +127,6 @@ module Squeal.PostgreSQL.Expression
140 , vararray
128 , vararray
141 , fixarray
129 , fixarray
144 -- * Re-export
130 -- * Re-export
146 , NP ((:*), Nil)
132 , NP ((:*), Nil)
@@ -150,7 +136,7 @@ import Control.Category
150import Control.DeepSeq
136import Control.DeepSeq
151import Data.ByteString (ByteString)
137import Data.ByteString (ByteString)
152import Data.Function ((&))
138import Data.Function ((&))
153−import Data.Monoid hiding (All)
139+import Data.Semigroup
154import Data.Ratio
140import Data.Ratio
155import Data.String
141import Data.String
156import Generics.SOP hiding (from)
142import Generics.SOP hiding (from)
@@ -178,6 +164,7 @@ values from primitive expression using arithmetic, logical,
178and other operations.
164and other operations.
180newtype Expression
166newtype Expression
167+ (schema :: SchemaType)
181 (relations :: RelationsType)
168 (relations :: RelationsType)
182 (grouping :: Grouping)
169 (grouping :: Grouping)
183 (params :: [NullityType])
170 (params :: [NullityType])
@@ -185,6 +172,9 @@ newtype Expression
185 = UnsafeExpression { renderExpression :: ByteString }
172 = UnsafeExpression { renderExpression :: ByteString }
186 deriving (GHC.Generic,Show,Eq,Ord,NFData)
173 deriving (GHC.Generic,Show,Eq,Ord,NFData)
175+instance RenderSQL (Expression schema relations grouping params ty) where
176+ renderSQL = renderExpression
188{- | A `HasParameter` constraint is used to indicate a value that is
178{- | A `HasParameter` constraint is used to indicate a value that is
189supplied externally to a SQL statement.
179supplied externally to a SQL statement.
190`Squeal.PostgreSQL.PQ.manipulateParams`,
180`Squeal.PostgreSQL.PQ.manipulateParams`,
@@ -193,203 +183,302 @@ supplied externally to a SQL statement.
193separately from the SQL command string, in which case `param`s are used to
183separately from the SQL command string, in which case `param`s are used to
194refer to the out-of-line data values.
184refer to the out-of-line data values.
196−class (PGTyped (PGTypeOf ty), KnownNat n) => HasParameter
186+class KnownNat n => HasParameter
197 (n :: Nat)
187 (n :: Nat)
188+ (schema :: SchemaType)
198 (params :: [NullityType])
189 (params :: [NullityType])
199 (ty :: NullityType)
190 (ty :: NullityType)
200 | n params -> ty where
191 | n params -> ty where
192+ -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
194+ -- >>> let expr = parameter @1 int4 :: Expression sch rels grp '[ 'Null 'PGint4] ('Null 'PGint4)
195+ -- >>> printSQL expr
198+ :: TypeExpression schema (PGTypeOf ty)
201− param :: Expression relations grouping params ty
199+ -> Expression schema relations grouping params ty
202− param = UnsafeExpression $ parenthesized $
200+ parameter ty = UnsafeExpression $ parenthesized $
203 "$" <> renderNat (Proxy @n) <+> "::"
201 "$" <> renderNat (Proxy @n) <+> "::"
204− <+> renderTypeExpression (pgtype @(PGTypeOf ty))
202+ <+> renderTypeExpression ty
205−instance {-# OVERLAPPING #-} PGTyped (PGTypeOf ty1)
206− => HasParameter 1 (ty1:tys) ty1
203+instance {-# OVERLAPPING #-} HasParameter 1 schema (ty1:tys) ty1
207−instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
204+instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) schema params ty)
208− => HasParameter n (ty' : params) ty
205+ => HasParameter n schema (ty' : params) ty
207+-- | `param` takes a `Nat` using type application and for basic types,
208+-- infers a `TypeExpression`.
210+-- >>> let expr = param @1 :: Expression sch rels grp '[ 'Null 'PGint4] ('Null 'PGint4)
214+ :: forall n schema params relations grouping ty
215+ . (PGTyped schema (PGTypeOf ty), HasParameter n schema params ty)
216+ => Expression schema relations grouping params ty -- ^ param
217+param = parameter @n pgtype
210instance (HasUnique relation relations columns, Has column columns ty)
219instance (HasUnique relation relations columns, Has column columns ty)
211− => IsLabel column (Expression relations 'Ungrouped params ty) where
220+ => IsLabel column (Expression schema relations 'Ungrouped params ty) where
212 fromLabel = UnsafeExpression $ renderAlias (Alias @column)
221 fromLabel = UnsafeExpression $ renderAlias (Alias @column)
222+instance (HasUnique relation relations columns, Has column columns ty)
224+ (Aliased (Expression schema relations 'Ungrouped params) (column ::: ty)) where
225+ fromLabel = fromLabel @column `As` Alias @column
226+instance (HasUnique relation relations columns, Has column columns ty)
228+ (NP (Aliased (Expression schema relations 'Ungrouped params)) '[column ::: ty]) where
229+ fromLabel = fromLabel @column :* Nil
214instance (Has relation relations columns, Has column columns ty)
231instance (Has relation relations columns, Has column columns ty)
215− => IsQualified relation column (Expression relations 'Ungrouped params ty) where
232+ => IsQualified relation column (Expression schema relations 'Ungrouped params ty) where
216 relation ! column = UnsafeExpression $
233 relation ! column = UnsafeExpression $
217 renderAlias relation <> "." <> renderAlias column
234 renderAlias relation <> "." <> renderAlias column
218−
235+instance (Has relation relations columns, Has column columns ty)
236+ => IsQualified relation column
237+ (Aliased (Expression schema relations 'Ungrouped params) (column ::: ty)) where
238+ relation ! column = relation ! column `As` column
239+instance (Has relation relations columns, Has column columns ty)
240+ => IsQualified relation column
241+ (NP (Aliased (Expression schema relations 'Ungrouped params)) '[column ::: ty]) where
242+ relation ! column = relation ! column :* Nil
220 ( HasUnique relation relations columns
245 ( HasUnique relation relations columns
221 , Has column columns ty
246 , Has column columns ty
222 , GroupedBy relation column bys
247 , GroupedBy relation column bys
223 ) => IsLabel column
248 ) => IsLabel column
224− (Expression relations ('Grouped bys) params ty) where
249+ (Expression schema relations ('Grouped bys) params ty) where
225 fromLabel = UnsafeExpression $ renderAlias (Alias @column)
250 fromLabel = UnsafeExpression $ renderAlias (Alias @column)
252+ ( HasUnique relation relations columns
253+ , Has column columns ty
254+ , GroupedBy relation column bys
256+ ( Aliased (Expression schema relations ('Grouped bys) params)
257+ (column ::: ty) ) where
258+ fromLabel = fromLabel @column `As` Alias @column
260+ ( HasUnique relation relations columns
261+ , Has column columns ty
262+ , GroupedBy relation column bys
264+ ( NP (Aliased (Expression schema relations ('Grouped bys) params))
265+ '[column ::: ty] ) where
266+ fromLabel = fromLabel @column :* Nil
228 ( Has relation relations columns
269 ( Has relation relations columns
229 , Has column columns ty
270 , Has column columns ty
230 , GroupedBy relation column bys
271 , GroupedBy relation column bys
231 ) => IsQualified relation column
272 ) => IsQualified relation column
232− (Expression relations ('Grouped bys) params ty) where
273+ (Expression schema relations ('Grouped bys) params ty) where
233 relation ! column = UnsafeExpression $
274 relation ! column = UnsafeExpression $
234 renderAlias relation <> "." <> renderAlias column
275 renderAlias relation <> "." <> renderAlias column
277+ ( Has relation relations columns
278+ , Has column columns ty
279+ , GroupedBy relation column bys
280+ ) => IsQualified relation column
281+ (Aliased (Expression schema relations ('Grouped bys) params)
282+ (column ::: ty)) where
283+ relation ! column = relation ! column `As` column
285+ ( Has relation relations columns
286+ , Has column columns ty
287+ , GroupedBy relation column bys
288+ ) => IsQualified relation column
289+ ( NP (Aliased (Expression schema relations ('Grouped bys) params))
290+ '[column ::: ty]) where
291+ relation ! column = relation ! column :* Nil
236-- | analagous to `Nothing`
293-- | analagous to `Nothing`
238−-- >>> renderExpression $ null_
295+-- >>> printSQL null_
240−null_ :: Expression relations grouping params ('Null ty)
297+null_ :: Expression schema rels grouping params ('Null ty)
241null_ = UnsafeExpression "NULL"
298null_ = UnsafeExpression "NULL"
243-- | analagous to `Just`
300-- | analagous to `Just`
245−-- >>> renderExpression $ unNull true
302+-- >>> printSQL $ notNull true
248− :: Expression relations grouping params ('NotNull ty)
305+ :: Expression schema rels grouping params ('NotNull ty)
250− -> Expression relations grouping params ('Null ty)
306+ -> Expression schema rels grouping params ('Null ty)
251−unNull = UnsafeExpression . renderExpression
307+notNull = UnsafeExpression . renderExpression
253-- | return the leftmost value which is not NULL
309-- | return the leftmost value which is not NULL
255−-- >>> renderExpression $ coalesce [null_, unNull true] false
311+-- >>> printSQL $ coalesce [null_, notNull true] false
256−-- "COALESCE(NULL, TRUE, FALSE)"
312+-- COALESCE(NULL, TRUE, FALSE)
258− :: [Expression relations grouping params ('Null ty)]
314+ :: [Expression schema relations grouping params ('Null ty)]
259 -- ^ @NULL@s may be present
315 -- ^ @NULL@s may be present
260− -> Expression relations grouping params ('NotNull ty)
316+ -> Expression schema relations grouping params ('NotNull ty)
261 -- ^ @NULL@ is absent
317 -- ^ @NULL@ is absent
262− -> Expression relations grouping params ('NotNull ty)
318+ -> Expression schema relations grouping params ('NotNull ty)
263coalesce nullxs notNullx = UnsafeExpression $
319coalesce nullxs notNullx = UnsafeExpression $
264 "COALESCE" <> parenthesized (commaSeparated
320 "COALESCE" <> parenthesized (commaSeparated
265 ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
321 ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
267-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
323-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
269−-- >>> renderExpression $ fromNull true null_
325+-- >>> printSQL $ fromNull true null_
270−-- "COALESCE(NULL, TRUE)"
326+-- COALESCE(NULL, TRUE)
272− :: Expression relations grouping params ('NotNull ty)
328+ :: Expression schema relations grouping params ('NotNull ty)
273 -- ^ what to convert @NULL@ to
329 -- ^ what to convert @NULL@ to
274− -> Expression relations grouping params ('Null ty)
330+ -> Expression schema relations grouping params ('Null ty)
275− -> Expression relations grouping params ('NotNull ty)
331+ -> Expression schema relations grouping params ('NotNull ty)
276fromNull notNullx nullx = coalesce [nullx] notNullx
332fromNull notNullx nullx = coalesce [nullx] notNullx
278−-- | >>> renderExpression $ null_ & isNull
334+-- | >>> printSQL $ null_ & isNull
279−-- "NULL IS NULL"
335+-- NULL IS NULL
281− :: Expression relations grouping params ('Null ty)
337+ :: Expression schema relations grouping params ('Null ty)
282 -- ^ possibly @NULL@
338 -- ^ possibly @NULL@
283− -> Condition relations grouping params
339+ -> Condition schema relations grouping params
284isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
340isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
286−-- | >>> renderExpression $ null_ & isn'tNull
342+-- | >>> printSQL $ null_ & isNotNull
287−-- "NULL IS NOT NULL"
343+-- NULL IS NOT NULL
288−isn'tNull
344+isNotNull
289− :: Expression relations grouping params ('Null ty)
345+ :: Expression schema relations grouping params ('Null ty)
290 -- ^ possibly @NULL@
346 -- ^ possibly @NULL@
291− -> Condition relations grouping params
347+ -> Condition schema relations grouping params
292−isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
348+isNotNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
294-- | analagous to `maybe` using @IS NULL@
350-- | analagous to `maybe` using @IS NULL@
296−-- >>> renderExpression $ matchNull true not_ null_
352+-- >>> printSQL $ matchNull true not_ null_
297−-- "CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END"
353+-- CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END
299− :: Expression relations grouping params (nullty)
355+ :: Expression schema relations grouping params (nullty)
300 -- ^ what to convert @NULL@ to
356 -- ^ what to convert @NULL@ to
301− -> ( Expression relations grouping params ('NotNull ty)
357+ -> ( Expression schema relations grouping params ('NotNull ty)
302− -> Expression relations grouping params (nullty) )
358+ -> Expression schema relations grouping params (nullty) )
303 -- ^ function to perform when @NULL@ is absent
359 -- ^ function to perform when @NULL@ is absent
304− -> Expression relations grouping params ('Null ty)
360+ -> Expression schema relations grouping params ('Null ty)
305− -> Expression relations grouping params (nullty)
361+ -> Expression schema relations grouping params (nullty)
306matchNull y f x = ifThenElse (isNull x) y
362matchNull y f x = ifThenElse (isNull x) y
307 (f (UnsafeExpression (renderExpression x)))
363 (f (UnsafeExpression (renderExpression x)))
309−-- | right inverse to `fromNull`, if its arguments are equal then
365+{-| right inverse to `fromNull`, if its arguments are equal then
310−-- `nullIf` gives @NULL@.
366+`nullIf` gives @NULL@.
312−-- >>> :set -XTypeApplications -XDataKinds
368+>>> :set -XTypeApplications -XDataKinds
313−-- >>> renderExpression @_ @_ @'[_] $ fromNull false (nullIf false (param @1))
369+>>> let expr = nullIf false (param @1) :: Expression schema rels grp '[ 'NotNull 'PGbool] ('Null 'PGbool)
314−-- "COALESCE(NULL IF (FALSE, ($1 :: bool)), FALSE)"
371+NULL IF (FALSE, ($1 :: bool))
316− :: Expression relations grouping params ('NotNull ty)
374+ :: Expression schema relations grouping params ('NotNull ty)
317 -- ^ @NULL@ is absent
375 -- ^ @NULL@ is absent
318− -> Expression relations grouping params ('NotNull ty)
376+ -> Expression schema relations grouping params ('NotNull ty)
319 -- ^ @NULL@ is absent
377 -- ^ @NULL@ is absent
320− -> Expression relations grouping params ('Null ty)
378+ -> Expression schema relations grouping params ('Null ty)
321nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
379nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
322 (renderExpression x <> ", " <> renderExpression y)
380 (renderExpression x <> ", " <> renderExpression y)
324−-- | >>> renderExpression $ array [null_, unNull false, unNull true]
382+-- | >>> printSQL $ array [null_, notNull false, notNull true]
325−-- "ARRAY[NULL, FALSE, TRUE]"
383+-- ARRAY[NULL, FALSE, TRUE]
327− :: [Expression relations grouping params ('Null ty)]
385+ :: [Expression schema relations grouping params ('Null ty)]
328 -- ^ array elements
386 -- ^ array elements
329− -> Expression relations grouping params (nullity ('PGvararray ty))
387+ -> Expression schema relations grouping params (nullity ('PGvararray ty))
330array xs = UnsafeExpression $
388array xs = UnsafeExpression $
331 "ARRAY[" <> commaSeparated (renderExpression <$> xs) <> "]"
389 "ARRAY[" <> commaSeparated (renderExpression <$> xs) <> "]"
391+instance (KnownSymbol label, label `In` labels) => IsPGlabel label
392+ (Expression schema relations grouping params (nullity ('PGenum labels))) where
393+ label = UnsafeExpression $ renderLabel (PGlabel @label)
395+-- | A row constructor is an expression that builds a row value
396+-- (also called a composite value) using values for its member fields.
398+-- >>> type Complex = PGcomposite '["real" ::: 'PGfloat8, "imaginary" ::: 'PGfloat8]
399+-- >>> let i = row (0 `As` #real :* 1 `As` #imaginary :* Nil) :: Expression '[] '[] 'Ungrouped '[] ('NotNull Complex)
403+ :: SListI (Nulls fields)
404+ => NP (Aliased (Expression schema relations grouping params)) (Nulls fields)
405+ -- ^ zero or more expressions for the row field values
406+ -> Expression schema relations grouping params (nullity ('PGcomposite fields))
407+row exprs = UnsafeExpression $ "ROW" <> parenthesized
408+ (renderCommaSeparated (\ (expr `As` _) -> renderExpression expr) exprs)
410+instance Has field fields ty => IsLabel field
411+ ( Expression schema relation grouping params (nullity ('PGcomposite fields))
412+ -> Expression schema relation grouping params ('Null ty) ) where
413+ fromLabel expr = UnsafeExpression $
414+ parenthesized (renderExpression expr) <> "." <>
415+ fromString (symbolVal (Proxy @field))
418+ (Expression schema relations grouping params (nullity ('PGvararray ty))) where
419+ (<>) = unsafeBinaryOp "||"
333instance Monoid
421instance Monoid
334− (Expression relations grouping params (nullity ('PGvararray ty))) where
422+ (Expression schema relations grouping params (nullity ('PGvararray ty))) where
335 mempty = array []
423 mempty = array []
336− mappend = unsafeBinaryOp "||"
424+ mappend = (<>)
338−-- | >>> renderExpression @_ @_ @'[_] $ greatest currentTimestamp [param @1]
426+-- | >>> let expr = greatest currentTimestamp [param @1] :: Expression sch rels grp '[ 'NotNull 'PGtimestamptz] ('NotNull 'PGtimestamptz)
339−-- "GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))"
428+-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
341− :: Expression relations grouping params (nullty)
430+ :: Expression schema relations grouping params (nullty)
342 -- ^ needs at least 1 argument
431 -- ^ needs at least 1 argument
343− -> [Expression relations grouping params (nullty)]
432+ -> [Expression schema relations grouping params (nullty)]
344 -- ^ or more
433 -- ^ or more
345− -> Expression relations grouping params (nullty)
434+ -> Expression schema relations grouping params (nullty)
346greatest x xs = UnsafeExpression $ "GREATEST("
435greatest x xs = UnsafeExpression $ "GREATEST("
347 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
436 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
349−-- | >>> renderExpression $ least currentTimestamp [null_]
438+-- | >>> printSQL $ least currentTimestamp [null_]
350−-- "LEAST(CURRENT_TIMESTAMP, NULL)"
439+-- LEAST(CURRENT_TIMESTAMP, NULL)
352− :: Expression relations grouping params (nullty)
441+ :: Expression schema relations grouping params (nullty)
353 -- ^ needs at least 1 argument
442 -- ^ needs at least 1 argument
354− -> [Expression relations grouping params (nullty)]
443+ -> [Expression schema relations grouping params (nullty)]
355 -- ^ or more
444 -- ^ or more
356− -> Expression relations grouping params (nullty)
445+ -> Expression schema relations grouping params (nullty)
357least x xs = UnsafeExpression $ "LEAST("
446least x xs = UnsafeExpression $ "LEAST("
358 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
447 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
360−-- | >>> renderExpression $ unsafeBinaryOp "OR" true false
449+-- | >>> printSQL $ unsafeBinaryOp "OR" true false
361−-- "(TRUE OR FALSE)"
450+-- (TRUE OR FALSE)
362unsafeBinaryOp
451unsafeBinaryOp
363 :: ByteString
452 :: ByteString
364 -- ^ operator
453 -- ^ operator
365− -> Expression relations grouping params (ty0)
454+ -> Expression schema relations grouping params (ty0)
366− -> Expression relations grouping params (ty1)
455+ -> Expression schema relations grouping params (ty1)
367− -> Expression relations grouping params (ty2)
456+ -> Expression schema relations grouping params (ty2)
368unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
457unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
369 renderExpression x <+> op <+> renderExpression y
458 renderExpression x <+> op <+> renderExpression y
371−-- | >>> renderExpression $ unsafeUnaryOp "NOT" true
460+-- | >>> printSQL $ unsafeUnaryOp "NOT" true
372−-- "(NOT TRUE)"
461+-- (NOT TRUE)
373unsafeUnaryOp
462unsafeUnaryOp
374 :: ByteString
463 :: ByteString
375 -- ^ operator
464 -- ^ operator
376− -> Expression relations grouping params (ty0)
465+ -> Expression schema relations grouping params (ty0)
377− -> Expression relations grouping params (ty1)
466+ -> Expression schema relations grouping params (ty1)
378unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
467unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
379 op <+> renderExpression x
468 op <+> renderExpression x
381−-- | >>> renderExpression $ unsafeFunction "f" true
470+-- | >>> printSQL $ unsafeFunction "f" true
382−-- "f(TRUE)"
471+-- f(TRUE)
383unsafeFunction
472unsafeFunction
384 :: ByteString
473 :: ByteString
385 -- ^ function
474 -- ^ function
386− -> Expression relations grouping params (xty)
475+ -> Expression schema relations grouping params (xty)
387− -> Expression relations grouping params (yty)
476+ -> Expression schema relations grouping params (yty)
388unsafeFunction fun x = UnsafeExpression $
477unsafeFunction fun x = UnsafeExpression $
389 fun <> parenthesized (renderExpression x)
478 fun <> parenthesized (renderExpression x)
391instance PGNum ty
480instance PGNum ty
392− => Num (Expression relations grouping params (nullity ty)) where
481+ => Num (Expression schema relations grouping params (nullity ty)) where
393 (+) = unsafeBinaryOp "+"
482 (+) = unsafeBinaryOp "+"
394 (-) = unsafeBinaryOp "-"
483 (-) = unsafeBinaryOp "-"
395 (*) = unsafeBinaryOp "*"
484 (*) = unsafeBinaryOp "*"
@@ -401,12 +490,12 @@ instance PGNum ty
403instance (PGNum ty, PGFloating ty) => Fractional
492instance (PGNum ty, PGFloating ty) => Fractional
404− (Expression relations grouping params (nullity ty)) where
493+ (Expression schema relations grouping params (nullity ty)) where
405 (/) = unsafeBinaryOp "/"
494 (/) = unsafeBinaryOp "/"
406 fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
495 fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
408instance (PGNum ty, PGFloating ty) => Floating
497instance (PGNum ty, PGFloating ty) => Floating
409− (Expression relations grouping params (nullity ty)) where
498+ (Expression schema relations grouping params (nullity ty)) where
410 pi = UnsafeExpression "pi()"
499 pi = UnsafeExpression "pi()"
411 exp = unsafeFunction "exp"
500 exp = unsafeFunction "exp"
412 log = unsafeFunction "ln"
501 log = unsafeFunction "ln"
@@ -429,18 +518,18 @@ instance (PGNum ty, PGFloating ty) => Floating
430-- | >>> :{
519-- | >>> :{
432−-- expression :: Expression relations grouping params (nullity 'PGfloat4)
521+-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
433-- expression = atan2_ pi 2
522-- expression = atan2_ pi 2
434−-- in renderExpression expression
523+-- in printSQL expression
436−-- "atan2(pi(), 2)"
525+-- atan2(pi(), 2)
438 :: PGFloating float
527 :: PGFloating float
439− => Expression relations grouping params (nullity float)
528+ => Expression schema relations grouping params (nullity float)
440 -- ^ numerator
529 -- ^ numerator
441− -> Expression relations grouping params (nullity float)
530+ -> Expression schema relations grouping params (nullity float)
442 -- ^ denominator
531 -- ^ denominator
443− -> Expression relations grouping params (nullity float)
532+ -> Expression schema relations grouping params (nullity float)
444atan2_ y x = UnsafeExpression $
533atan2_ y x = UnsafeExpression $
445 "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
534 "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
@@ -448,14 +537,14 @@ atan2_ y x = UnsafeExpression $
448-- represents a run-time type conversion. The cast will succeed only if a
537-- represents a run-time type conversion. The cast will succeed only if a
449-- suitable type conversion operation has been defined.
538-- suitable type conversion operation has been defined.
451−-- | >>> renderExpression $ true & cast int4
540+-- | >>> printSQL $ true & cast int4
452−-- "(TRUE :: int4)"
541+-- (TRUE :: int4)
454− :: TypeExpression ('NoDef :=> 'Null ty1)
543+ :: TypeExpression schema ty1
455 -- ^ type to cast as
544 -- ^ type to cast as
456− -> Expression relations grouping params (nullity ty0)
545+ -> Expression schema relations grouping params (nullity ty0)
457 -- ^ value to convert
546 -- ^ value to convert
458− -> Expression relations grouping params (nullity ty1)
547+ -> Expression schema relations grouping params (nullity ty1)
459cast ty x = UnsafeExpression $ parenthesized $
548cast ty x = UnsafeExpression $ parenthesized $
460 renderExpression x <+> "::" <+> renderTypeExpression ty
549 renderExpression x <+> "::" <+> renderTypeExpression ty
@@ -463,135 +552,135 @@ cast ty x = UnsafeExpression $ parenthesized $
466−-- expression :: Expression relations grouping params (nullity 'PGint2)
555+-- expression :: Expression schema relations grouping params (nullity 'PGint2)
467-- expression = 5 `quot_` 2
556-- expression = 5 `quot_` 2
468−-- in renderExpression expression
557+-- in printSQL expression
470−-- "(5 / 2)"
559+-- (5 / 2)
472 :: PGIntegral int
561 :: PGIntegral int
473− => Expression relations grouping params (nullity int)
562+ => Expression schema relations grouping params (nullity int)
474 -- ^ numerator
563 -- ^ numerator
475− -> Expression relations grouping params (nullity int)
564+ -> Expression schema relations grouping params (nullity int)
476 -- ^ denominator
565 -- ^ denominator
477− -> Expression relations grouping params (nullity int)
566+ -> Expression schema relations grouping params (nullity int)
478quot_ = unsafeBinaryOp "/"
567quot_ = unsafeBinaryOp "/"
480-- | remainder upon integer division
569-- | remainder upon integer division
484−-- expression :: Expression relations grouping params (nullity 'PGint2)
573+-- expression :: Expression schema relations grouping params (nullity 'PGint2)
485-- expression = 5 `rem_` 2
574-- expression = 5 `rem_` 2
486−-- in renderExpression expression
575+-- in printSQL expression
488−-- "(5 % 2)"
577+-- (5 % 2)
490 :: PGIntegral int
579 :: PGIntegral int
491− => Expression relations grouping params (nullity int)
580+ => Expression schema relations grouping params (nullity int)
492 -- ^ numerator
581 -- ^ numerator
493− -> Expression relations grouping params (nullity int)
582+ -> Expression schema relations grouping params (nullity int)
494 -- ^ denominator
583 -- ^ denominator
495− -> Expression relations grouping params (nullity int)
584+ -> Expression schema relations grouping params (nullity int)
496rem_ = unsafeBinaryOp "%"
585rem_ = unsafeBinaryOp "%"
498-- | >>> :{
587-- | >>> :{
500−-- expression :: Expression relations grouping params (nullity 'PGfloat4)
589+-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
501-- expression = trunc pi
590-- expression = trunc pi
502−-- in renderExpression expression
591+-- in printSQL expression
504−-- "trunc(pi())"
593+-- trunc(pi())
506 :: PGFloating frac
595 :: PGFloating frac
507− => Expression relations grouping params (nullity frac)
596+ => Expression schema relations grouping params (nullity frac)
508 -- ^ fractional number
597 -- ^ fractional number
509− -> Expression relations grouping params (nullity frac)
598+ -> Expression schema relations grouping params (nullity frac)
510trunc = unsafeFunction "trunc"
599trunc = unsafeFunction "trunc"
512-- | >>> :{
601-- | >>> :{
514−-- expression :: Expression relations grouping params (nullity 'PGfloat4)
603+-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
515-- expression = round_ pi
604-- expression = round_ pi
516−-- in renderExpression expression
605+-- in printSQL expression
518−-- "round(pi())"
607+-- round(pi())
520 :: PGFloating frac
609 :: PGFloating frac
521− => Expression relations grouping params (nullity frac)
610+ => Expression schema relations grouping params (nullity frac)
522 -- ^ fractional number
611 -- ^ fractional number
523− -> Expression relations grouping params (nullity frac)
612+ -> Expression schema relations grouping params (nullity frac)
524round_ = unsafeFunction "round"
613round_ = unsafeFunction "round"
526-- | >>> :{
615-- | >>> :{
528−-- expression :: Expression relations grouping params (nullity 'PGfloat4)
617+-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
529-- expression = ceiling_ pi
618-- expression = ceiling_ pi
530−-- in renderExpression expression
619+-- in printSQL expression
532−-- "ceiling(pi())"
621+-- ceiling(pi())
534 :: PGFloating frac
623 :: PGFloating frac
535− => Expression relations grouping params (nullity frac)
624+ => Expression schema relations grouping params (nullity frac)
536 -- ^ fractional number
625 -- ^ fractional number
537− -> Expression relations grouping params (nullity frac)
626+ -> Expression schema relations grouping params (nullity frac)
538ceiling_ = unsafeFunction "ceiling"
627ceiling_ = unsafeFunction "ceiling"
540-- | A `Condition` is a boolean valued `Expression`. While SQL allows
629-- | A `Condition` is a boolean valued `Expression`. While SQL allows
541-- conditions to have @NULL@, Squeal instead chooses to disallow @NULL@,
630-- conditions to have @NULL@, Squeal instead chooses to disallow @NULL@,
542-- forcing one to handle the case of @NULL@ explicitly to produce
631-- forcing one to handle the case of @NULL@ explicitly to produce
543-- a `Condition`.
632-- a `Condition`.
544−type Condition relations grouping params =
633+type Condition schema relations grouping params =
545− Expression relations grouping params ('NotNull 'PGbool)
634+ Expression schema relations grouping params ('NotNull 'PGbool)
547−-- | >>> renderExpression true
636+-- | >>> printSQL true
549−true :: Condition relations grouping params
638+true :: Condition schema relations grouping params
550true = UnsafeExpression "TRUE"
639true = UnsafeExpression "TRUE"
552−-- | >>> renderExpression false
641+-- | >>> printSQL false
553−-- "FALSE"
642+-- FALSE
554−false :: Condition relations grouping params
643+false :: Condition schema relations grouping params
555false = UnsafeExpression "FALSE"
644false = UnsafeExpression "FALSE"
557−-- | >>> renderExpression $ not_ true
646+-- | >>> printSQL $ not_ true
558−-- "(NOT TRUE)"
647+-- (NOT TRUE)
560− :: Condition relations grouping params
649+ :: Condition schema relations grouping params
561− -> Condition relations grouping params
650+ -> Condition schema relations grouping params
562not_ = unsafeUnaryOp "NOT"
651not_ = unsafeUnaryOp "NOT"
564−-- | >>> renderExpression $ true .&& false
653+-- | >>> printSQL $ true .&& false
565−-- "(TRUE AND FALSE)"
654+-- (TRUE AND FALSE)
567− :: Condition relations grouping params
656+ :: Condition schema relations grouping params
568− -> Condition relations grouping params
657+ -> Condition schema relations grouping params
569− -> Condition relations grouping params
658+ -> Condition schema relations grouping params
570(.&&) = unsafeBinaryOp "AND"
659(.&&) = unsafeBinaryOp "AND"
572−-- | >>> renderExpression $ true .|| false
661+-- | >>> printSQL $ true .|| false
573−-- "(TRUE OR FALSE)"
662+-- (TRUE OR FALSE)
575− :: Condition relations grouping params
664+ :: Condition schema relations grouping params
576− -> Condition relations grouping params
665+ -> Condition schema relations grouping params
577− -> Condition relations grouping params
666+ -> Condition schema relations grouping params
578(.||) = unsafeBinaryOp "OR"
667(.||) = unsafeBinaryOp "OR"
580-- | >>> :{
669-- | >>> :{
582−-- expression :: Expression relations grouping params (nullity 'PGint2)
671+-- expression :: Expression schema relations grouping params (nullity 'PGint2)
583-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
672-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
584−-- in renderExpression expression
673+-- in printSQL expression
586−-- "CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END"
675+-- CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END
587caseWhenThenElse
676caseWhenThenElse
588− :: [ ( Condition relations grouping params
677+ :: [ ( Condition schema relations grouping params
589− , Expression relations grouping params (ty)
678+ , Expression schema relations grouping params (ty)
591 -- ^ whens and thens
680 -- ^ whens and thens
592− -> Expression relations grouping params (ty)
681+ -> Expression schema relations grouping params (ty)
593 -- ^ else
682 -- ^ else
594− -> Expression relations grouping params (ty)
683+ -> Expression schema relations grouping params (ty)
595caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
684caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
597 , mconcat
686 , mconcat
@@ -607,103 +696,103 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
608-- | >>> :{
697-- | >>> :{
610−-- expression :: Expression relations grouping params (nullity 'PGint2)
699+-- expression :: Expression schema relations grouping params (nullity 'PGint2)
611-- expression = ifThenElse true 1 0
700-- expression = ifThenElse true 1 0
612−-- in renderExpression expression
701+-- in printSQL expression
614−-- "CASE WHEN TRUE THEN 1 ELSE 0 END"
703+-- CASE WHEN TRUE THEN 1 ELSE 0 END
615ifThenElse
704ifThenElse
616− :: Condition relations grouping params
705+ :: Condition schema relations grouping params
617− -> Expression relations grouping params (ty) -- ^ then
706+ -> Expression schema relations grouping params (ty) -- ^ then
618− -> Expression relations grouping params (ty) -- ^ else
707+ -> Expression schema relations grouping params (ty) -- ^ else
619− -> Expression relations grouping params (ty)
708+ -> Expression schema relations grouping params (ty)
620ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
709ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
622-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
711-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
623-- will produce @NULL@s if one of their arguments is @NULL@.
712-- will produce @NULL@s if one of their arguments is @NULL@.
625−-- >>> renderExpression $ unNull true .== null_
714+-- >>> printSQL $ notNull true .== null_
626−-- "(TRUE = NULL)"
715+-- (TRUE = NULL)
628− :: Expression relations grouping params (nullity ty) -- ^ lhs
717+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
629− -> Expression relations grouping params (nullity ty) -- ^ rhs
718+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
630− -> Expression relations grouping params (nullity 'PGbool)
719+ -> Expression schema relations grouping params (nullity 'PGbool)
631(.==) = unsafeBinaryOp "="
720(.==) = unsafeBinaryOp "="
632infix 4 .==
721infix 4 .==
634−-- | >>> renderExpression $ unNull true ./= null_
723+-- | >>> printSQL $ notNull true ./= null_
635−-- "(TRUE <> NULL)"
724+-- (TRUE <> NULL)
637− :: Expression relations grouping params (nullity ty) -- ^ lhs
726+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
638− -> Expression relations grouping params (nullity ty) -- ^ rhs
727+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
639− -> Expression relations grouping params (nullity 'PGbool)
728+ -> Expression schema relations grouping params (nullity 'PGbool)
640(./=) = unsafeBinaryOp "<>"
729(./=) = unsafeBinaryOp "<>"
641infix 4 ./=
730infix 4 ./=
643−-- | >>> renderExpression $ unNull true .>= null_
732+-- | >>> printSQL $ notNull true .>= null_
644−-- "(TRUE >= NULL)"
733+-- (TRUE >= NULL)
646− :: Expression relations grouping params (nullity ty) -- ^ lhs
735+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
647− -> Expression relations grouping params (nullity ty) -- ^ rhs
736+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
648− -> Expression relations grouping params (nullity 'PGbool)
737+ -> Expression schema relations grouping params (nullity 'PGbool)
649(.>=) = unsafeBinaryOp ">="
738(.>=) = unsafeBinaryOp ">="
650infix 4 .>=
739infix 4 .>=
652−-- | >>> renderExpression $ unNull true .< null_
741+-- | >>> printSQL $ notNull true .< null_
653−-- "(TRUE < NULL)"
742+-- (TRUE < NULL)
655− :: Expression relations grouping params (nullity ty) -- ^ lhs
744+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
656− -> Expression relations grouping params (nullity ty) -- ^ rhs
745+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
657− -> Expression relations grouping params (nullity 'PGbool)
746+ -> Expression schema relations grouping params (nullity 'PGbool)
658(.<) = unsafeBinaryOp "<"
747(.<) = unsafeBinaryOp "<"
659infix 4 .<
748infix 4 .<
661−-- | >>> renderExpression $ unNull true .<= null_
750+-- | >>> printSQL $ notNull true .<= null_
662−-- "(TRUE <= NULL)"
751+-- (TRUE <= NULL)
664− :: Expression relations grouping params (nullity ty) -- ^ lhs
753+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
665− -> Expression relations grouping params (nullity ty) -- ^ rhs
754+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
666− -> Expression relations grouping params (nullity 'PGbool)
755+ -> Expression schema relations grouping params (nullity 'PGbool)
667(.<=) = unsafeBinaryOp "<="
756(.<=) = unsafeBinaryOp "<="
668infix 4 .<=
757infix 4 .<=
670−-- | >>> renderExpression $ unNull true .> null_
759+-- | >>> printSQL $ notNull true .> null_
671−-- "(TRUE > NULL)"
760+-- (TRUE > NULL)
673− :: Expression relations grouping params (nullity ty) -- ^ lhs
762+ :: Expression schema relations grouping params (nullity ty) -- ^ lhs
674− -> Expression relations grouping params (nullity ty) -- ^ rhs
763+ -> Expression schema relations grouping params (nullity ty) -- ^ rhs
675− -> Expression relations grouping params (nullity 'PGbool)
764+ -> Expression schema relations grouping params (nullity 'PGbool)
676(.>) = unsafeBinaryOp ">"
765(.>) = unsafeBinaryOp ">"
677infix 4 .>
766infix 4 .>
679−-- | >>> renderExpression currentDate
768+-- | >>> printSQL currentDate
680−-- "CURRENT_DATE"
769+-- CURRENT_DATE
681currentDate
770currentDate
682− :: Expression relations grouping params (nullity 'PGdate)
771+ :: Expression schema relations grouping params (nullity 'PGdate)
683currentDate = UnsafeExpression "CURRENT_DATE"
772currentDate = UnsafeExpression "CURRENT_DATE"
685−-- | >>> renderExpression currentTime
774+-- | >>> printSQL currentTime
686−-- "CURRENT_TIME"
775+-- CURRENT_TIME
687currentTime
776currentTime
688− :: Expression relations grouping params (nullity 'PGtimetz)
777+ :: Expression schema relations grouping params (nullity 'PGtimetz)
689currentTime = UnsafeExpression "CURRENT_TIME"
778currentTime = UnsafeExpression "CURRENT_TIME"
691−-- | >>> renderExpression currentTimestamp
780+-- | >>> printSQL currentTimestamp
692−-- "CURRENT_TIMESTAMP"
781+-- CURRENT_TIMESTAMP
693currentTimestamp
782currentTimestamp
694− :: Expression relations grouping params (nullity 'PGtimestamptz)
783+ :: Expression schema relations grouping params (nullity 'PGtimestamptz)
695currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
784currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
697−-- | >>> renderExpression localTime
786+-- | >>> printSQL localTime
698−-- "LOCALTIME"
787+-- LOCALTIME
700− :: Expression relations grouping params (nullity 'PGtime)
789+ :: Expression schema relations grouping params (nullity 'PGtime)
701localTime = UnsafeExpression "LOCALTIME"
790localTime = UnsafeExpression "LOCALTIME"
703−-- | >>> renderExpression localTimestamp
792+-- | >>> printSQL localTimestamp
704−-- "LOCALTIMESTAMP"
793+-- LOCALTIMESTAMP
705localTimestamp
794localTimestamp
706− :: Expression relations grouping params (nullity 'PGtimestamp)
795+ :: Expression schema relations grouping params (nullity 'PGtimestamp)
707localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
796localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
709{-----------------------------------------
798{-----------------------------------------
@@ -711,7 +800,7 @@ text
711-----------------------------------------}
800-----------------------------------------}
713instance IsString
802instance IsString
714− (Expression relations grouping params (nullity 'PGtext)) where
803+ (Expression schema relations grouping params (nullity 'PGtext)) where
715 fromString str = UnsafeExpression $
804 fromString str = UnsafeExpression $
716 "E\'" <> fromString (escape =<< str) <> "\'"
805 "E\'" <> fromString (escape =<< str) <> "\'"
@@ -726,33 +815,37 @@ instance IsString
726 '\\' -> "\\\\"
815 '\\' -> "\\\\"
819+ (Expression schema relations grouping params (nullity 'PGtext)) where
820+ (<>) = unsafeBinaryOp "||"
729instance Monoid
822instance Monoid
730− (Expression relations grouping params (nullity 'PGtext)) where
823+ (Expression schema relations grouping params (nullity 'PGtext)) where
731 mempty = fromString ""
824 mempty = fromString ""
732− mappend = unsafeBinaryOp "||"
825+ mappend = (<>)
734−-- | >>> renderExpression $ lower "ARRRGGG"
827+-- | >>> printSQL $ lower "ARRRGGG"
735−-- "lower(E'ARRRGGG')"
828+-- lower(E'ARRRGGG')
737− :: Expression relations grouping params (nullity 'PGtext)
830+ :: Expression schema relations grouping params (nullity 'PGtext)
738 -- ^ string to lower case
831 -- ^ string to lower case
739− -> Expression relations grouping params (nullity 'PGtext)
832+ -> Expression schema relations grouping params (nullity 'PGtext)
740lower = unsafeFunction "lower"
833lower = unsafeFunction "lower"
742−-- | >>> renderExpression $ upper "eeee"
835+-- | >>> printSQL $ upper "eeee"
743−-- "upper(E'eeee')"
836+-- upper(E'eeee')
745− :: Expression relations grouping params (nullity 'PGtext)
838+ :: Expression schema relations grouping params (nullity 'PGtext)
746 -- ^ string to upper case
839 -- ^ string to upper case
747− -> Expression relations grouping params (nullity 'PGtext)
840+ -> Expression schema relations grouping params (nullity 'PGtext)
748upper = unsafeFunction "upper"
841upper = unsafeFunction "upper"
750−-- | >>> renderExpression $ charLength "four"
843+-- | >>> printSQL $ charLength "four"
751−-- "char_length(E'four')"
844+-- char_length(E'four')
752charLength
845charLength
753− :: Expression relations grouping params (nullity 'PGtext)
846+ :: Expression schema relations grouping params (nullity 'PGtext)
754 -- ^ string to measure
847 -- ^ string to measure
755− -> Expression relations grouping params (nullity 'PGint4)
848+ -> Expression schema relations grouping params (nullity 'PGint4)
756charLength = unsafeFunction "char_length"
849charLength = unsafeFunction "char_length"
758-- | The `like` expression returns true if the @string@ matches
851-- | The `like` expression returns true if the @string@ matches
@@ -762,14 +855,14 @@ charLength = unsafeFunction "char_length"
762-- in pattern stands for (matches) any single character; a percent sign (%)
855-- in pattern stands for (matches) any single character; a percent sign (%)
763-- matches any sequence of zero or more characters.
856-- matches any sequence of zero or more characters.
765−-- >>> renderExpression $ "abc" `like` "a%"
858+-- >>> printSQL $ "abc" `like` "a%"
766−-- "(E'abc' LIKE E'a%')"
859+-- (E'abc' LIKE E'a%')
768− :: Expression relations grouping params (nullity 'PGtext)
861+ :: Expression schema relations grouping params (nullity 'PGtext)
769 -- ^ string
862 -- ^ string
770− -> Expression relations grouping params (nullity 'PGtext)
863+ -> Expression schema relations grouping params (nullity 'PGtext)
771 -- ^ pattern
864 -- ^ pattern
772− -> Expression relations grouping params (nullity 'PGbool)
865+ -> Expression schema relations grouping params (nullity 'PGbool)
773like = unsafeBinaryOp "LIKE"
866like = unsafeBinaryOp "LIKE"
775{-----------------------------------------
868{-----------------------------------------
@@ -779,54 +872,54 @@ aggregation
779-- | escape hatch to define aggregate functions
872-- | escape hatch to define aggregate functions
780unsafeAggregate
873unsafeAggregate
781 :: ByteString -- ^ aggregate function
874 :: ByteString -- ^ aggregate function
782− -> Expression relations 'Ungrouped params (xty)
875+ -> Expression schema relations 'Ungrouped params (xty)
783− -> Expression relations ('Grouped bys) params (yty)
876+ -> Expression schema relations ('Grouped bys) params (yty)
784unsafeAggregate fun x = UnsafeExpression $ mconcat
877unsafeAggregate fun x = UnsafeExpression $ mconcat
785 [fun, "(", renderExpression x, ")"]
878 [fun, "(", renderExpression x, ")"]
787-- | escape hatch to define aggregate functions over distinct values
880-- | escape hatch to define aggregate functions over distinct values
788unsafeAggregateDistinct
881unsafeAggregateDistinct
789 :: ByteString -- ^ aggregate function
882 :: ByteString -- ^ aggregate function
790− -> Expression relations 'Ungrouped params (xty)
883+ -> Expression schema relations 'Ungrouped params (xty)
791− -> Expression relations ('Grouped bys) params (yty)
884+ -> Expression schema relations ('Grouped bys) params (yty)
792unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
885unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
793 [fun, "(DISTINCT ", renderExpression x, ")"]
886 [fun, "(DISTINCT ", renderExpression x, ")"]
795-- | >>> :{
888-- | >>> :{
797−-- expression :: Expression '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Grouped bys) params ('Null 'PGnumeric)
890+-- expression :: Expression schema '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Grouped bys) params ('Null 'PGnumeric)
798-- expression = sum_ #col
891-- expression = sum_ #col
799−-- in renderExpression expression
892+-- in printSQL expression
801−-- "sum(\"col\")"
894+-- sum("col")
803 :: PGNum ty
896 :: PGNum ty
804− => Expression relations 'Ungrouped params (nullity ty)
897+ => Expression schema relations 'Ungrouped params (nullity ty)
805 -- ^ what to sum
898 -- ^ what to sum
806− -> Expression relations ('Grouped bys) params (nullity ty)
899+ -> Expression schema relations ('Grouped bys) params (nullity ty)
807sum_ = unsafeAggregate "sum"
900sum_ = unsafeAggregate "sum"
809-- | >>> :{
902-- | >>> :{
811−-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGnumeric]] ('Grouped bys) params (nullity 'PGnumeric)
904+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGnumeric]] ('Grouped bys) params (nullity 'PGnumeric)
812-- expression = sumDistinct #col
905-- expression = sumDistinct #col
813−-- in renderExpression expression
906+-- in printSQL expression
815−-- "sum(DISTINCT \"col\")"
908+-- sum(DISTINCT "col")
816sumDistinct
909sumDistinct
817 :: PGNum ty
910 :: PGNum ty
818− => Expression relations 'Ungrouped params (nullity ty)
911+ => Expression schema relations 'Ungrouped params (nullity ty)
819 -- ^ what to sum
912 -- ^ what to sum
820− -> Expression relations ('Grouped bys) params (nullity ty)
913+ -> Expression schema relations ('Grouped bys) params (nullity ty)
821sumDistinct = unsafeAggregateDistinct "sum"
914sumDistinct = unsafeAggregateDistinct "sum"
823-- | A constraint for `PGType`s that you can take averages of and the resulting
916-- | A constraint for `PGType`s that you can take averages of and the resulting
824-- `PGType`.
917-- `PGType`.
825class PGAvg ty avg | ty -> avg where
918class PGAvg ty avg | ty -> avg where
826 avg, avgDistinct
919 avg, avgDistinct
827− :: Expression relations 'Ungrouped params (nullity ty)
920+ :: Expression schema relations 'Ungrouped params (nullity ty)
828 -- ^ what to average
921 -- ^ what to average
829− -> Expression relations ('Grouped bys) params (nullity avg)
922+ -> Expression schema relations ('Grouped bys) params (nullity avg)
830 avg = unsafeAggregate "avg"
923 avg = unsafeAggregate "avg"
831 avgDistinct = unsafeAggregateDistinct "avg"
924 avgDistinct = unsafeAggregateDistinct "avg"
832instance PGAvg 'PGint2 'PGnumeric
925instance PGAvg 'PGint2 'PGnumeric
@@ -839,312 +932,281 @@ instance PGAvg 'PGinterval 'PGinterval
840-- | >>> :{
933-- | >>> :{
842−-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
935+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
843-- expression = bitAnd #col
936-- expression = bitAnd #col
844−-- in renderExpression expression
937+-- in printSQL expression
846−-- "bit_and(\"col\")"
939+-- bit_and("col")
848 :: PGIntegral int
941 :: PGIntegral int
849− => Expression relations 'Ungrouped params (nullity int)
942+ => Expression schema relations 'Ungrouped params (nullity int)
850 -- ^ what to aggregate
943 -- ^ what to aggregate
851− -> Expression relations ('Grouped bys) params (nullity int)
944+ -> Expression schema relations ('Grouped bys) params (nullity int)
852bitAnd = unsafeAggregate "bit_and"
945bitAnd = unsafeAggregate "bit_and"
854-- | >>> :{
947-- | >>> :{
856−-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
949+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
857-- expression = bitOr #col
950-- expression = bitOr #col
858−-- in renderExpression expression
951+-- in printSQL expression
860−-- "bit_or(\"col\")"
953+-- bit_or("col")
862 :: PGIntegral int
955 :: PGIntegral int
863− => Expression relations 'Ungrouped params (nullity int)
956+ => Expression schema relations 'Ungrouped params (nullity int)
864 -- ^ what to aggregate
957 -- ^ what to aggregate
865− -> Expression relations ('Grouped bys) params (nullity int)
958+ -> Expression schema relations ('Grouped bys) params (nullity int)
866bitOr = unsafeAggregate "bit_or"
959bitOr = unsafeAggregate "bit_or"
868-- | >>> :{
961-- | >>> :{
870−-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
963+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
871-- expression = bitAndDistinct #col
964-- expression = bitAndDistinct #col
872−-- in renderExpression expression
965+-- in printSQL expression
874−-- "bit_and(DISTINCT \"col\")"
967+-- bit_and(DISTINCT "col")
875bitAndDistinct
968bitAndDistinct
876 :: PGIntegral int
969 :: PGIntegral int
877− => Expression relations 'Ungrouped params (nullity int)
970+ => Expression schema relations 'Ungrouped params (nullity int)
878 -- ^ what to aggregate
971 -- ^ what to aggregate
879− -> Expression relations ('Grouped bys) params (nullity int)
972+ -> Expression schema relations ('Grouped bys) params (nullity int)
880bitAndDistinct = unsafeAggregateDistinct "bit_and"
973bitAndDistinct = unsafeAggregateDistinct "bit_and"
882-- | >>> :{
975-- | >>> :{
884−-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
977+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
885-- expression = bitOrDistinct #col
978-- expression = bitOrDistinct #col
886−-- in renderExpression expression
979+-- in printSQL expression
888−-- "bit_or(DISTINCT \"col\")"
981+-- bit_or(DISTINCT "col")
889bitOrDistinct
982bitOrDistinct
890 :: PGIntegral int
983 :: PGIntegral int
891− => Expression relations 'Ungrouped params (nullity int)
984+ => Expression schema relations 'Ungrouped params (nullity int)
892 -- ^ what to aggregate
985 -- ^ what to aggregate
893− -> Expression relations ('Grouped bys) params (nullity int)
986+ -> Expression schema relations ('Grouped bys) params (nullity int)
894bitOrDistinct = unsafeAggregateDistinct "bit_or"
987bitOrDistinct = unsafeAggregateDistinct "bit_or"
896-- | >>> :{
989-- | >>> :{
898−-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
991+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
899-- expression = boolAnd #col
992-- expression = boolAnd #col
900−-- in renderExpression expression
993+-- in printSQL expression
902−-- "bool_and(\"col\")"
995+-- bool_and("col")
904− :: Expression relations 'Ungrouped params (nullity 'PGbool)
997+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
905 -- ^ what to aggregate
998 -- ^ what to aggregate
906− -> Expression relations ('Grouped bys) params (nullity 'PGbool)
999+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
907boolAnd = unsafeAggregate "bool_and"
1000boolAnd = unsafeAggregate "bool_and"
909-- | >>> :{
1002-- | >>> :{
911−-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1004+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
912-- expression = boolOr #col
1005-- expression = boolOr #col
913−-- in renderExpression expression
1006+-- in printSQL expression
915−-- "bool_or(\"col\")"
1008+-- bool_or("col")
917− :: Expression relations 'Ungrouped params (nullity 'PGbool)
1010+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
918 -- ^ what to aggregate
1011 -- ^ what to aggregate
919− -> Expression relations ('Grouped bys) params (nullity 'PGbool)
1012+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
920boolOr = unsafeAggregate "bool_or"
1013boolOr = unsafeAggregate "bool_or"
922-- | >>> :{
1015-- | >>> :{
924−-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1017+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
925-- expression = boolAndDistinct #col
1018-- expression = boolAndDistinct #col
926−-- in renderExpression expression
1019+-- in printSQL expression
928−-- "bool_and(DISTINCT \"col\")"
1021+-- bool_and(DISTINCT "col")
929boolAndDistinct
1022boolAndDistinct
930− :: Expression relations 'Ungrouped params (nullity 'PGbool)
1023+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
931 -- ^ what to aggregate
1024 -- ^ what to aggregate
932− -> Expression relations ('Grouped bys) params (nullity 'PGbool)
1025+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
933boolAndDistinct = unsafeAggregateDistinct "bool_and"
1026boolAndDistinct = unsafeAggregateDistinct "bool_and"
935-- | >>> :{
1028-- | >>> :{
937−-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1030+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
938-- expression = boolOrDistinct #col
1031-- expression = boolOrDistinct #col
939−-- in renderExpression expression
1032+-- in printSQL expression
941−-- "bool_or(DISTINCT \"col\")"
1034+-- bool_or(DISTINCT "col")
942boolOrDistinct
1035boolOrDistinct
943− :: Expression relations 'Ungrouped params (nullity 'PGbool)
1036+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
944 -- ^ what to aggregate
1037 -- ^ what to aggregate
945− -> Expression relations ('Grouped bys) params (nullity 'PGbool)
1038+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
946boolOrDistinct = unsafeAggregateDistinct "bool_or"
1039boolOrDistinct = unsafeAggregateDistinct "bool_or"
948-- | A special aggregation that does not require an input
1041-- | A special aggregation that does not require an input
950−-- >>> renderExpression countStar
1043+-- >>> printSQL countStar
951−-- "count(*)"
1044+-- count(*)
952countStar
1045countStar
953− :: Expression relations ('Grouped bys) params ('NotNull 'PGint8)
1046+ :: Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
954countStar = UnsafeExpression $ "count(*)"
1047countStar = UnsafeExpression $ "count(*)"
956-- | >>> :{
1049-- | >>> :{
958−-- expression :: Expression '[tab ::: '["col" ::: nullity ty]] (Grouped bys) params ('NotNull 'PGint8)
1051+-- expression :: Expression schema '[tab ::: '["col" ::: nullity ty]] (Grouped bys) params ('NotNull 'PGint8)
959-- expression = count #col
1052-- expression = count #col
960−-- in renderExpression expression
1053+-- in printSQL expression
962−-- "count(\"col\")"
1055+-- count("col")
964− :: Expression relations 'Ungrouped params ty
1057+ :: Expression schema relations 'Ungrouped params ty
965 -- ^ what to count
1058 -- ^ what to count
966− -> Expression relations ('Grouped bys) params ('NotNull 'PGint8)
1059+ -> Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
967count = unsafeAggregate "count"
1060count = unsafeAggregate "count"
969-- | >>> :{
1062-- | >>> :{
971−-- expression :: Expression '[tab ::: '["col" ::: nullity ty]] (Grouped bys) params ('NotNull 'PGint8)
1064+-- expression :: Expression schema '[tab ::: '["col" ::: nullity ty]] (Grouped bys) params ('NotNull 'PGint8)
972-- expression = countDistinct #col
1065-- expression = countDistinct #col
973−-- in renderExpression expression
1066+-- in printSQL expression
975−-- "count(DISTINCT \"col\")"
1068+-- count(DISTINCT "col")
976countDistinct
1069countDistinct
977− :: Expression relations 'Ungrouped params ty
1070+ :: Expression schema relations 'Ungrouped params ty
978 -- ^ what to count
1071 -- ^ what to count
979− -> Expression relations ('Grouped bys) params ('NotNull 'PGint8)
1072+ -> Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
980countDistinct = unsafeAggregateDistinct "count"
1073countDistinct = unsafeAggregateDistinct "count"
982-- | synonym for `boolAnd`
1075-- | synonym for `boolAnd`
984-- >>> :{
1077-- >>> :{
986−-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1079+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
987-- expression = every #col
1080-- expression = every #col
988−-- in renderExpression expression
1081+-- in printSQL expression
990−-- "every(\"col\")"
1083+-- every("col")
992− :: Expression relations 'Ungrouped params (nullity 'PGbool)
1085+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
993 -- ^ what to aggregate
1086 -- ^ what to aggregate
994− -> Expression relations ('Grouped bys) params (nullity 'PGbool)
1087+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
995every = unsafeAggregate "every"
1088every = unsafeAggregate "every"
997-- | synonym for `boolAndDistinct`
1090-- | synonym for `boolAndDistinct`
999-- >>> :{
1092-- >>> :{
1001−-- expression :: Expression '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1094+-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1002-- expression = everyDistinct #col
1095-- expression = everyDistinct #col
1003−-- in renderExpression expression
1096+-- in printSQL expression
1005−-- "every(DISTINCT \"col\")"
1098+-- every(DISTINCT "col")
1006everyDistinct
1099everyDistinct
1007− :: Expression relations 'Ungrouped params (nullity 'PGbool)
1100+ :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
1008 -- ^ what to aggregate
1101 -- ^ what to aggregate
1009− -> Expression relations ('Grouped bys) params (nullity 'PGbool)
1102+ -> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
1010everyDistinct = unsafeAggregateDistinct "every"
1103everyDistinct = unsafeAggregateDistinct "every"
1012-- | minimum and maximum aggregation
1105-- | minimum and maximum aggregation
1013max_, min_, maxDistinct, minDistinct
1106max_, min_, maxDistinct, minDistinct
1014− :: Expression relations 'Ungrouped params (nullity ty)
1107+ :: Expression schema relations 'Ungrouped params (nullity ty)
1015 -- ^ what to aggregate
1108 -- ^ what to aggregate
1016− -> Expression relations ('Grouped bys) params (nullity ty)
1109+ -> Expression schema relations ('Grouped bys) params (nullity ty)
1017max_ = unsafeAggregate "max"
1110max_ = unsafeAggregate "max"
1018min_ = unsafeAggregate "min"
1111min_ = unsafeAggregate "min"
1019maxDistinct = unsafeAggregateDistinct "max"
1112maxDistinct = unsafeAggregateDistinct "max"
1020minDistinct = unsafeAggregateDistinct "min"
1113minDistinct = unsafeAggregateDistinct "min"
1022−{-----------------------------------------
1024−-----------------------------------------}
1026−-- | A `Table` from a table expression is a way
1027−-- to call a table reference by its alias.
1029− (schema :: TablesType)
1030− (columns :: RelationType)
1031− = UnsafeTable { renderTable :: ByteString }
1032− deriving (GHC.Generic,Show,Eq,Ord,NFData)
1034− ( Has alias schema table
1035− , relation ~ ColumnsToRelation (TableToColumns table)
1036− ) => IsLabel alias (Table schema relation) where
1037− fromLabel = UnsafeTable $ renderAlias (Alias @alias)
1039{-----------------------------------------
1115{-----------------------------------------
1040type expressions
1116type expressions
1041-----------------------------------------}
1117-----------------------------------------}
1043-- | `TypeExpression`s are used in `cast`s and `createTable` commands.
1119-- | `TypeExpression`s are used in `cast`s and `createTable` commands.
1044−newtype TypeExpression (ty :: ColumnType)
1120+newtype TypeExpression (schema :: SchemaType) (ty :: PGType)
1045 = UnsafeTypeExpression { renderTypeExpression :: ByteString }
1121 = UnsafeTypeExpression { renderTypeExpression :: ByteString }
1046 deriving (GHC.Generic,Show,Eq,Ord,NFData)
1122 deriving (GHC.Generic,Show,Eq,Ord,NFData)
1124+instance (Has alias schema ('Typedef ty))
1125+ => IsLabel alias (TypeExpression schema ty) where
1126+ fromLabel = UnsafeTypeExpression (renderAlias (fromLabel @alias))
1048-- | logical Boolean (true/false)
1128-- | logical Boolean (true/false)
1049−bool :: TypeExpression ('NoDef :=> 'Null 'PGbool)
1129+bool :: TypeExpression schema 'PGbool
1050bool = UnsafeTypeExpression "bool"
1130bool = UnsafeTypeExpression "bool"
1051-- | signed two-byte integer
1131-- | signed two-byte integer
1052−int2, smallint :: TypeExpression ('NoDef :=> 'Null 'PGint2)
1132+int2, smallint :: TypeExpression schema 'PGint2
1053int2 = UnsafeTypeExpression "int2"
1133int2 = UnsafeTypeExpression "int2"
1054smallint = UnsafeTypeExpression "smallint"
1134smallint = UnsafeTypeExpression "smallint"
1055-- | signed four-byte integer
1135-- | signed four-byte integer
1056−int4, int, integer :: TypeExpression ('NoDef :=> 'Null 'PGint4)
1136+int4, int, integer :: TypeExpression schema 'PGint4
1057int4 = UnsafeTypeExpression "int4"
1137int4 = UnsafeTypeExpression "int4"
1058int = UnsafeTypeExpression "int"
1138int = UnsafeTypeExpression "int"
1059integer = UnsafeTypeExpression "integer"
1139integer = UnsafeTypeExpression "integer"
1060-- | signed eight-byte integer
1140-- | signed eight-byte integer
1061−int8, bigint :: TypeExpression ('NoDef :=> 'Null 'PGint8)
1141+int8, bigint :: TypeExpression schema 'PGint8
1062int8 = UnsafeTypeExpression "int8"
1142int8 = UnsafeTypeExpression "int8"
1063bigint = UnsafeTypeExpression "bigint"
1143bigint = UnsafeTypeExpression "bigint"
1064-- | arbitrary precision numeric type
1144-- | arbitrary precision numeric type
1065−numeric :: TypeExpression ('NoDef :=> 'Null 'PGnumeric)
1145+numeric :: TypeExpression schema 'PGnumeric
1066numeric = UnsafeTypeExpression "numeric"
1146numeric = UnsafeTypeExpression "numeric"
1067-- | single precision floating-point number (4 bytes)
1147-- | single precision floating-point number (4 bytes)
1068−float4, real :: TypeExpression ('NoDef :=> 'Null 'PGfloat4)
1148+float4, real :: TypeExpression schema 'PGfloat4
1069float4 = UnsafeTypeExpression "float4"
1149float4 = UnsafeTypeExpression "float4"
1070real = UnsafeTypeExpression "real"
1150real = UnsafeTypeExpression "real"
1071-- | double precision floating-point number (8 bytes)
1151-- | double precision floating-point number (8 bytes)
1072−float8, doublePrecision :: TypeExpression ('NoDef :=> 'Null 'PGfloat8)
1152+float8, doublePrecision :: TypeExpression schema 'PGfloat8
1073float8 = UnsafeTypeExpression "float8"
1153float8 = UnsafeTypeExpression "float8"
1074doublePrecision = UnsafeTypeExpression "double precision"
1154doublePrecision = UnsafeTypeExpression "double precision"
1075−-- | not a true type, but merely a notational convenience for creating
1076−-- unique identifier columns with type `'PGint2`
1077−serial2, smallserial
1078− :: TypeExpression ('Def :=> 'NotNull 'PGint2)
1079−serial2 = UnsafeTypeExpression "serial2"
1080−smallserial = UnsafeTypeExpression "smallserial"
1081−-- | not a true type, but merely a notational convenience for creating
1082−-- unique identifier columns with type `'PGint4`
1084− :: TypeExpression ('Def :=> 'NotNull 'PGint4)
1085−serial4 = UnsafeTypeExpression "serial4"
1086−serial = UnsafeTypeExpression "serial"
1087−-- | not a true type, but merely a notational convenience for creating
1088−-- unique identifier columns with type `'PGint8`
1090− :: TypeExpression ('Def :=> 'NotNull 'PGint8)
1091−serial8 = UnsafeTypeExpression "serial8"
1092−bigserial = UnsafeTypeExpression "bigserial"
1093-- | variable-length character string
1155-- | variable-length character string
1094−text :: TypeExpression ('NoDef :=> 'Null 'PGtext)
1156+text :: TypeExpression schema 'PGtext
1095text = UnsafeTypeExpression "text"
1157text = UnsafeTypeExpression "text"
1096-- | fixed-length character string
1158-- | fixed-length character string
1097char, character
1159char, character
1098 :: (KnownNat n, 1 <= n)
1160 :: (KnownNat n, 1 <= n)
1099 => proxy n
1161 => proxy n
1100− -> TypeExpression ('NoDef :=> 'Null ('PGchar n))
1162+ -> TypeExpression schema ('PGchar n)
1101char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
1163char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
1102character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
1164character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
1103-- | variable-length character string
1165-- | variable-length character string
1104varchar, characterVarying
1166varchar, characterVarying
1105 :: (KnownNat n, 1 <= n)
1167 :: (KnownNat n, 1 <= n)
1106 => proxy n
1168 => proxy n
1107− -> TypeExpression ('NoDef :=> 'Null ('PGvarchar n))
1169+ -> TypeExpression schema ('PGvarchar n)
1108varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
1170varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
1109characterVarying p = UnsafeTypeExpression $
1171characterVarying p = UnsafeTypeExpression $
1110 "character varying(" <> renderNat p <> ")"
1172 "character varying(" <> renderNat p <> ")"
1111-- | binary data ("byte array")
1173-- | binary data ("byte array")
1112−bytea :: TypeExpression ('NoDef :=> 'Null 'PGbytea)
1174+bytea :: TypeExpression schema 'PGbytea
1113bytea = UnsafeTypeExpression "bytea"
1175bytea = UnsafeTypeExpression "bytea"
1114-- | date and time (no time zone)
1176-- | date and time (no time zone)
1115−timestamp :: TypeExpression ('NoDef :=> 'Null 'PGtimestamp)
1177+timestamp :: TypeExpression schema 'PGtimestamp
1116timestamp = UnsafeTypeExpression "timestamp"
1178timestamp = UnsafeTypeExpression "timestamp"
1117-- | date and time, including time zone
1179-- | date and time, including time zone
1118−timestampWithTimeZone :: TypeExpression ('NoDef :=> 'Null 'PGtimestamptz)
1180+timestampWithTimeZone :: TypeExpression schema 'PGtimestamptz
1119timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
1181timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
1120-- | calendar date (year, month, day)
1182-- | calendar date (year, month, day)
1121−date :: TypeExpression ('NoDef :=> 'Null 'PGdate)
1183+date :: TypeExpression schema 'PGdate
1122date = UnsafeTypeExpression "date"
1184date = UnsafeTypeExpression "date"
1123-- | time of day (no time zone)
1185-- | time of day (no time zone)
1124−time :: TypeExpression ('NoDef :=> 'Null 'PGtime)
1186+time :: TypeExpression schema 'PGtime
1125time = UnsafeTypeExpression "time"
1187time = UnsafeTypeExpression "time"
1126-- | time of day, including time zone
1188-- | time of day, including time zone
1127−timeWithTimeZone :: TypeExpression ('NoDef :=> 'Null 'PGtimetz)
1189+timeWithTimeZone :: TypeExpression schema 'PGtimetz
1128timeWithTimeZone = UnsafeTypeExpression "time with time zone"
1190timeWithTimeZone = UnsafeTypeExpression "time with time zone"
1129-- | time span
1191-- | time span
1130−interval :: TypeExpression ('NoDef :=> 'Null 'PGinterval)
1192+interval :: TypeExpression schema 'PGinterval
1131interval = UnsafeTypeExpression "interval"
1193interval = UnsafeTypeExpression "interval"
1132-- | universally unique identifier
1194-- | universally unique identifier
1133−uuid :: TypeExpression ('NoDef :=> 'Null 'PGuuid)
1195+uuid :: TypeExpression schema 'PGuuid
1134uuid = UnsafeTypeExpression "uuid"
1196uuid = UnsafeTypeExpression "uuid"
1135-- | IPv4 or IPv6 host address
1197-- | IPv4 or IPv6 host address
1136−inet :: TypeExpression ('NoDef :=> 'Null 'PGinet)
1198+inet :: TypeExpression schema 'PGinet
1137inet = UnsafeTypeExpression "inet"
1199inet = UnsafeTypeExpression "inet"
1138-- | textual JSON data
1200-- | textual JSON data
1139−json :: TypeExpression ('NoDef :=> 'Null 'PGjson)
1201+json :: TypeExpression schema 'PGjson
1140json = UnsafeTypeExpression "json"
1202json = UnsafeTypeExpression "json"
1141-- | binary JSON data, decomposed
1203-- | binary JSON data, decomposed
1142−jsonb :: TypeExpression ('NoDef :=> 'Null 'PGjsonb)
1204+jsonb :: TypeExpression schema 'PGjsonb
1143jsonb = UnsafeTypeExpression "jsonb"
1205jsonb = UnsafeTypeExpression "jsonb"
1144-- | variable length array
1206-- | variable length array
1146− :: TypeExpression ('NoDef :=> 'Null pg)
1208+ :: TypeExpression schema pg
1147− -> TypeExpression ('NoDef :=> 'Null ('PGvararray pg))
1209+ -> TypeExpression schema ('PGvararray pg)
1148vararray ty = UnsafeTypeExpression $ renderTypeExpression ty <> "[]"
1210vararray ty = UnsafeTypeExpression $ renderTypeExpression ty <> "[]"
1149-- | fixed length array
1211-- | fixed length array
@@ -1153,52 +1215,36 @@ vararray ty = UnsafeTypeExpression $ renderTypeExpression ty <> "[]"
1154 :: KnownNat n
1216 :: KnownNat n
1155 => proxy n
1217 => proxy n
1156− -> TypeExpression ('NoDef :=> 'Null pg)
1218+ -> TypeExpression schema pg
1157− -> TypeExpression ('NoDef :=> 'Null ('PGfixarray n pg))
1219+ -> TypeExpression schema ('PGfixarray n pg)
1158fixarray p ty = UnsafeTypeExpression $
1220fixarray p ty = UnsafeTypeExpression $
1159 renderTypeExpression ty <> "[" <> renderNat p <> "]"
1221 renderTypeExpression ty <> "[" <> renderNat p <> "]"
1161−-- | used in `createTable` commands as a column constraint to ensure
1162−-- @NULL@ is not present
1164− :: TypeExpression (def :=> 'Null ty)
1165− -> TypeExpression (def :=> 'NotNull ty)
1166−notNull ty = UnsafeTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
1168−-- | used in `createTable` commands as a column constraint to give a default
1170− :: Expression '[] 'Ungrouped '[] ty
1171− -> TypeExpression ('NoDef :=> ty)
1172− -> TypeExpression ('Def :=> ty)
1173−default_ x ty = UnsafeTypeExpression $
1174− renderTypeExpression ty <+> "DEFAULT" <+> renderExpression x
1176-- | `pgtype` is a demoted version of a `PGType`
1223-- | `pgtype` is a demoted version of a `PGType`
1177−class PGTyped (ty :: PGType) where
1224+class PGTyped schema (ty :: PGType) where pgtype :: TypeExpression schema ty
1178− pgtype :: TypeExpression ('NoDef :=> 'Null ty)
1179−instance PGTyped 'PGbool where pgtype = bool
1225+instance PGTyped schema 'PGbool where pgtype = bool
1180−instance PGTyped 'PGint2 where pgtype = int2
1226+instance PGTyped schema 'PGint2 where pgtype = int2
1181−instance PGTyped 'PGint4 where pgtype = int4
1227+instance PGTyped schema 'PGint4 where pgtype = int4
1182−instance PGTyped 'PGint8 where pgtype = int8
1228+instance PGTyped schema 'PGint8 where pgtype = int8
1183−instance PGTyped 'PGnumeric where pgtype = numeric
1229+instance PGTyped schema 'PGnumeric where pgtype = numeric
1184−instance PGTyped 'PGfloat4 where pgtype = float4
1230+instance PGTyped schema 'PGfloat4 where pgtype = float4
1185−instance PGTyped 'PGfloat8 where pgtype = float8
1231+instance PGTyped schema 'PGfloat8 where pgtype = float8
1186−instance PGTyped 'PGtext where pgtype = text
1232+instance PGTyped schema 'PGtext where pgtype = text
1187instance (KnownNat n, 1 <= n)
1233instance (KnownNat n, 1 <= n)
1188− => PGTyped ('PGchar n) where pgtype = char (Proxy @n)
1234+ => PGTyped schema ('PGchar n) where pgtype = char (Proxy @n)
1189instance (KnownNat n, 1 <= n)
1235instance (KnownNat n, 1 <= n)
1190− => PGTyped ('PGvarchar n) where pgtype = varchar (Proxy @n)
1236+ => PGTyped schema ('PGvarchar n) where pgtype = varchar (Proxy @n)
1191−instance PGTyped 'PGbytea where pgtype = bytea
1237+instance PGTyped schema 'PGbytea where pgtype = bytea
1192−instance PGTyped 'PGtimestamp where pgtype = timestamp
1238+instance PGTyped schema 'PGtimestamp where pgtype = timestamp
1193−instance PGTyped 'PGtimestamptz where pgtype = timestampWithTimeZone
1239+instance PGTyped schema 'PGtimestamptz where pgtype = timestampWithTimeZone
1194−instance PGTyped 'PGdate where pgtype = date
1240+instance PGTyped schema 'PGdate where pgtype = date
1195−instance PGTyped 'PGtime where pgtype = time
1241+instance PGTyped schema 'PGtime where pgtype = time
1196−instance PGTyped 'PGtimetz where pgtype = timeWithTimeZone
1242+instance PGTyped schema 'PGtimetz where pgtype = timeWithTimeZone
1197−instance PGTyped 'PGinterval where pgtype = interval
1243+instance PGTyped schema 'PGinterval where pgtype = interval
1198−instance PGTyped 'PGuuid where pgtype = uuid
1244+instance PGTyped schema 'PGuuid where pgtype = uuid
1199−instance PGTyped 'PGjson where pgtype = json
1245+instance PGTyped schema 'PGjson where pgtype = json
1200−instance PGTyped 'PGjsonb where pgtype = jsonb
1246+instance PGTyped schema 'PGjsonb where pgtype = jsonb
1201−instance PGTyped ty => PGTyped ('PGvararray ty) where
1247+instance PGTyped schema ty => PGTyped schema ('PGvararray ty) where
1202− pgtype = vararray (pgtype @ty)
1248+ pgtype = vararray (pgtype @schema @ty)
1203−instance (KnownNat n, PGTyped ty) => PGTyped ('PGfixarray n ty) where
1249+instance (KnownNat n, PGTyped schema ty) => PGTyped schema ('PGfixarray n ty) where
1204− pgtype = fixarray (Proxy @n) (pgtype @ty)
1250+ pgtype = fixarray (Proxy @n) (pgtype @schema @ty)