@@ -17,11 +17,205 @@
17 , StandaloneDeriving
17 , StandaloneDeriving
18 , TypeApplications
18 , TypeApplications
19 , TypeFamilies
19 , TypeFamilies
20 , TypeOperators
21 , TypeOperators
21 , UndecidableInstances
22 , UndecidableInstances
24−module Squeal.PostgreSQL.Statement where
25+module Squeal.PostgreSQL.Statement
26+ ( -- * Column Expressions
27+ Expression (UnsafeExpression, renderExpression)
28+ , HasParameter (param)
29+ , HasColumn (getColumn)
80+ , unsafeAggregateDistinct
83+ , PGAvg (avg, avgDistinct)
99+ , Table (UnsafeTable, renderTable)
100+ , HasTable (getTable)
101+ -- ** Froms and Joins
104+ -- ** Table Expressions
105+ , TableExpression (..)
106+ , renderTableExpression
116+ , GroupByClause (NoGroups, Group)
117+ , renderGroupByClause
118+ , HavingClause (NoHaving, Having)
119+ , renderHavingClause
120+ , GroupedBy (getGroup1, getGroup2)
121+ , SortExpression (..)
122+ , renderSortExpression
124+ , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
152+ , timestampWithTimeZone
164+ , Query (UnsafeQuery, renderQuery)
168+ , selectDistinctStar
170+ , selectDistinctDotStar
177+ -- ** Data Definition Language
178+ , Definition (UnsafeDefinition, renderDefinition)
180+ , TableConstraint (UnsafeTableConstraint, renderTableConstraint)
185+ , OnDelete (OnDeleteNoAction, OnDeleteRestrict, OnDeleteCascade)
187+ , OnUpdate (OnUpdateNoAction, OnUpdateRestrict, OnUpdateCascade)
192+ , AlterTable (UnsafeAlterTable, renderAlterTable)
198+ , AlterColumn (UnsafeAlterColumn, renderAlterColumn)
204+ -- ** Data Manipulation Language
205+ , Manipulation (UnsafeManipulation, renderManipulation)
208+ , ValuesClause (Values, ValuesQuery)
209+ , renderValuesClause
210+ , ReturningClause (ReturningStar, Returning)
211+ , renderReturningClause
212+ , ConflictClause (Conflict, OnConflictDoNothing, OnConflictDoUpdate)
213+ , renderConflictClause
214+ , UpdateExpression (Same, Set)
215+ , renderUpdateExpression
26import Control.Category
220import Control.Category
27import Control.DeepSeq
221import Control.DeepSeq
@@ -33,11 +227,11 @@ import Data.Monoid hiding (All)
33import Data.Ratio
227import Data.Ratio
34import Data.String
228import Data.String
35import Data.Word
229import Data.Word
36−import Generics.SOP
230+import Generics.SOP hiding (from)
37import GHC.Exts
231import GHC.Exts
38import GHC.OverloadedLabels
232import GHC.OverloadedLabels
39import GHC.TypeLits
233import GHC.TypeLits
40−import Prelude hiding (RealFrac(..), id, (.))
234+import Prelude hiding (id, (.))
42import qualified GHC.Generics as GHC
236import qualified GHC.Generics as GHC
43import qualified Data.ByteString as ByteString
237import qualified Data.ByteString as ByteString
@@ -48,14 +242,31 @@ import Squeal.PostgreSQL.Schema
48column expressions
242column expressions
49-----------------------------------------}
243-----------------------------------------}
245+{- | `Expression`s are used in a variety of contexts,
246+such as in the target list of the `select` command,
247+as new column values in `insertInto` or `update`,
248+or in search `Condition`s in a number of commands.
250+The expression syntax allows the calculation of
251+values from primitive expression using arithmetic, logical,
252+and other operations.
51newtype Expression
254newtype Expression
52 (params :: [ColumnType])
255 (params :: [ColumnType])
53− (tables :: [(Symbol,[(Symbol,ColumnType)])])
256+ (tables :: TablesType)
54 (grouping :: Grouping)
257 (grouping :: Grouping)
55 (ty :: ColumnType)
258 (ty :: ColumnType)
56 = UnsafeExpression { renderExpression :: ByteString }
259 = UnsafeExpression { renderExpression :: ByteString }
57 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
260 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
262+{- | A `HasParameter` constraint is used to indicate a value that is
263+supplied externally to a SQL statement.
264+`Squeal.PostgreSQL.PQ.manipulateParams`,
265+`Squeal.PostgreSQL.PQ.queryParams` and
266+`Squeal.PostgreSQL.PQ.traversePrepared` support specifying data values
267+separately from the SQL command string, in which case `param`s are used to
268+refer to the out-of-line data values.
59class (PGTyped (BaseType ty), KnownNat n)
270class (PGTyped (BaseType ty), KnownNat n)
60 => HasParameter (n :: Nat) params ty | n params -> ty where
271 => HasParameter (n :: Nat) params ty | n params -> ty where
61 param :: proxy n -> Expression params tables grouping ty
272 param :: proxy n -> Expression params tables grouping ty
@@ -67,18 +278,25 @@ instance {-# OVERLAPPING #-} PGTyped (BaseType ty1)
67instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
278instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
68 => HasParameter n (ty' : params) ty
279 => HasParameter n (ty' : params) ty
281+{- | A `HasColumn` constraint indicates an unqualified column reference.
282+`getColumn` can only be unambiguous when the `TableExpression` the column
283+references is unique, in which case the column may be referenced using
284+@-XOverloadedLabels@. Otherwise, combined with a `HasTable` constraint, the
285+qualified column reference operator `!` may be used.
70class KnownSymbol column => HasColumn column columns ty
287class KnownSymbol column => HasColumn column columns ty
71 | column columns -> ty where
288 | column columns -> ty where
72 getColumn
289 getColumn
290+ :: HasUnique table tables columns
73− :: Alias column
291+ => Alias column
74− -> Expression params '[table ::: columns] 'Ungrouped ty
292+ -> Expression params tables 'Ungrouped ty
75 getColumn column = UnsafeExpression $ renderAlias column
293 getColumn column = UnsafeExpression $ renderAlias column
76instance {-# OVERLAPPING #-} KnownSymbol column
294instance {-# OVERLAPPING #-} KnownSymbol column
77 => HasColumn column ((column ::: optionality ty) ': tys) ('Required ty)
295 => HasColumn column ((column ::: optionality ty) ': tys) ('Required ty)
78instance {-# OVERLAPPABLE #-} (KnownSymbol column, HasColumn column table ty)
296instance {-# OVERLAPPABLE #-} (KnownSymbol column, HasColumn column table ty)
79 => HasColumn column (ty' ': table) ty
297 => HasColumn column (ty' ': table) ty
81−instance (HasColumn column columns ty, tables ~ '[table ::: columns])
299+instance (HasColumn column columns ty, HasUnique table tables columns)
82 => IsLabel column (Expression params tables 'Ungrouped ty) where
300 => IsLabel column (Expression params tables 'Ungrouped ty) where
83 fromLabel = getColumn (Alias @column)
301 fromLabel = getColumn (Alias @column)
@@ -88,7 +306,7 @@ instance (HasTable table tables columns, HasColumn column columns ty)
88 renderAlias table <> "." <> renderAlias column
306 renderAlias table <> "." <> renderAlias column
90data Column
308data Column
91− (columns :: [(Symbol,ColumnType)])
309+ (columns :: ColumnsType)
92 (columnty :: (Symbol,ColumnType))
310 (columnty :: (Symbol,ColumnType))
@@ -99,22 +317,38 @@ deriving instance Show (Column columns columnty)
99deriving instance Eq (Column columns columnty)
317deriving instance Eq (Column columns columnty)
100deriving instance Ord (Column columns columnty)
318deriving instance Ord (Column columns columnty)
320+-- | >>> renderExpression def
102def :: Expression params '[] 'Ungrouped ('Optional (nullity ty))
322def :: Expression params '[] 'Ungrouped ('Optional (nullity ty))
103def = UnsafeExpression "DEFAULT"
323def = UnsafeExpression "DEFAULT"
325+-- | >>> renderExpression $ unDef false
106 :: Expression params '[] 'Ungrouped ('Required (nullity ty))
328 :: Expression params '[] 'Ungrouped ('Required (nullity ty))
107 -> Expression params '[] 'Ungrouped ('Optional (nullity ty))
329 -> Expression params '[] 'Ungrouped ('Optional (nullity ty))
108unDef = UnsafeExpression . renderExpression
330unDef = UnsafeExpression . renderExpression
332+-- | analagous to `Nothing`
334+-- >>> renderExpression $ null_
110−null :: Expression params tables grouping (optionality ('Null ty))
336+null_ :: Expression params tables grouping (optionality ('Null ty))
111−null = UnsafeExpression "NULL"
337+null_ = UnsafeExpression "NULL"
339+-- | analagous to `Just`
341+-- >>> renderExpression $ unNull true
114 :: Expression params tables grouping (optionality ('NotNull ty))
344 :: Expression params tables grouping (optionality ('NotNull ty))
115 -> Expression params tables grouping (optionality ('Null ty))
345 -> Expression params tables grouping (optionality ('Null ty))
116unNull = UnsafeExpression . renderExpression
346unNull = UnsafeExpression . renderExpression
348+-- | return the leftmost value which is not NULL
350+-- >>> renderExpression $ coalesce [null_, unNull true] false
351+-- "COALESCE(NULL, TRUE, FALSE)"
119 :: [Expression params tables grouping ('Required ('Null ty))]
353 :: [Expression params tables grouping ('Required ('Null ty))]
120 -> Expression params tables grouping ('Required ('NotNull ty))
354 -> Expression params tables grouping ('Required ('NotNull ty))
@@ -123,22 +357,34 @@ coalesce nullxs notNullx = UnsafeExpression $
123 "COALESCE" <> parenthesized (commaSeparated
357 "COALESCE" <> parenthesized (commaSeparated
124 ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
358 ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
360+-- | analagous to `fromMaybe` using @COALESCE@
362+-- >>> renderExpression $ fromNull true null_
363+-- "COALESCE(NULL, TRUE)"
127 :: Expression params tables grouping ('Required ('NotNull ty))
365 :: Expression params tables grouping ('Required ('NotNull ty))
128 -> Expression params tables grouping ('Required ('Null ty))
366 -> Expression params tables grouping ('Required ('Null ty))
129 -> Expression params tables grouping ('Required ('NotNull ty))
367 -> Expression params tables grouping ('Required ('NotNull ty))
130fromNull notNullx nullx = coalesce [nullx] notNullx
368fromNull notNullx nullx = coalesce [nullx] notNullx
370+-- | >>> renderExpression $ null_ & isNull
133 :: Expression params tables grouping ('Required ('Null ty))
373 :: Expression params tables grouping ('Required ('Null ty))
134 -> Condition params tables grouping
374 -> Condition params tables grouping
135isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
375isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
377+-- | >>> renderExpression $ null_ & isn'tNull
378+-- "NULL IS NOT NULL"
138 :: Expression params tables grouping ('Required ('Null ty))
380 :: Expression params tables grouping ('Required ('Null ty))
139 -> Condition params tables grouping
381 -> Condition params tables grouping
140isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
382isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
384+-- | analagous to `maybe` using @IS NULL@
386+-- >>> renderExpression $ matchNull true not null_
387+-- "CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT TRUE) END"
143 :: Expression params tables grouping ('Required nullty)
389 :: Expression params tables grouping ('Required nullty)
144 -> ( Expression params tables grouping ('Required ('NotNull ty))
390 -> ( Expression params tables grouping ('Required ('NotNull ty))
@@ -148,24 +394,34 @@ matchNull
148matchNull y f x = ifThenElse (isNull x) y
394matchNull y f x = ifThenElse (isNull x) y
149 (f (UnsafeExpression (renderExpression y)))
395 (f (UnsafeExpression (renderExpression y)))
397+-- | left inverse to `fromNull`
399+-- >>> renderExpression $ nullIf false (fromNull false null_)
400+-- "NULL IF (FALSE, COALESCE(NULL, FALSE))"
152 :: Expression params tables grouping ('Required ('NotNull ty))
402 :: Expression params tables grouping ('Required ('NotNull ty))
153 -> Expression params tables grouping ('Required ('NotNull ty))
403 -> Expression params tables grouping ('Required ('NotNull ty))
154 -> Expression params tables grouping ('Required ('Null ty))
404 -> Expression params tables grouping ('Required ('Null ty))
155−nullIf x y = UnsafeExpression $ parenthesized $
405+nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
156− renderExpression x <> ", " <> renderExpression y
406+ (renderExpression x <> ", " <> renderExpression y)
408+-- | >>> renderExpression $ greatest @'[_] currentTimestamp [param (Proxy @1)]
409+-- "GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))"
411+ :: Expression params tables grouping ('Required nullty)
159− :: [Expression params tables grouping ('Required nullty)]
412+ -> [Expression params tables grouping ('Required nullty)]
160 -> Expression params table grouping ('Required nullty)
413 -> Expression params table grouping ('Required nullty)
161−greatest xs = UnsafeExpression $ "GREATEST("
414+greatest x xs = UnsafeExpression $ "GREATEST("
162− <> commaSeparated (renderExpression <$> xs) <> ")"
415+ <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
417+-- | >>> renderExpression $ least currentTimestamp [null_]
418+-- "LEAST(CURRENT_TIMESTAMP, NULL)"
420+ :: Expression params tables grouping ('Required nullty)
165− :: [Expression params tables grouping ('Required nullty)]
421+ -> [Expression params tables grouping ('Required nullty)]
166 -> Expression params table grouping ('Required nullty)
422 -> Expression params table grouping ('Required nullty)
167−least xs = UnsafeExpression $ "LEAST("
423+least x xs = UnsafeExpression $ "LEAST("
168− <> commaSeparated (renderExpression <$> xs) <> ")"
424+ <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
170unsafeBinaryOp
426unsafeBinaryOp
171 :: ByteString
427 :: ByteString
@@ -198,16 +454,15 @@ instance PGNum ty
198 signum = unsafeFunction "sign"
454 signum = unsafeFunction "sign"
199 fromInteger
455 fromInteger
200 = UnsafeExpression
456 = UnsafeExpression
201− . (<> if decimal (Proxy :: Proxy ty) then "." else "")
202 . fromString
457 . fromString
205−instance PGFloating ty => Fractional
460+instance (PGNum ty, PGFloating ty) => Fractional
206 (Expression params tables grouping ('Required (nullity ty))) where
461 (Expression params tables grouping ('Required (nullity ty))) where
207 (/) = unsafeBinaryOp "/"
462 (/) = unsafeBinaryOp "/"
208 fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
463 fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
210−instance (PGFloating ty, PGTyped ty) => Floating
465+instance (PGNum ty, PGFloating ty) => Floating
211 (Expression params tables grouping ('Required (nullity ty))) where
466 (Expression params tables grouping ('Required (nullity ty))) where
212 pi = UnsafeExpression "pi()"
467 pi = UnsafeExpression "pi()"
213 exp = unsafeFunction "exp"
468 exp = unsafeFunction "exp"
@@ -215,19 +470,7 @@ instance (PGFloating ty, PGTyped ty) => Floating
215 sqrt = unsafeFunction "sqrt"
470 sqrt = unsafeFunction "sqrt"
216 b ** x = UnsafeExpression $
471 b ** x = UnsafeExpression $
217 "power(" <> renderExpression b <> ", " <> renderExpression x <> ")"
472 "power(" <> renderExpression b <> ", " <> renderExpression x <> ")"
218− logBase b y = cast pgtype $ logBaseNumeric b y
473+ logBase b y = log y / log b
221− :: Expression params tables grouping ('Required (nullity ty))
222− -> Expression params tables grouping ('Required (nullity ty))
223− -> Expression params tables grouping ('Required (nullity 'PGnumeric))
224− logBaseNumeric b' y' = UnsafeExpression $ mconcat
226− , renderExpression (cast numeric b')
228− , renderExpression (cast numeric y')
231 sin = unsafeFunction "sin"
474 sin = unsafeFunction "sin"
232 cos = unsafeFunction "cos"
475 cos = unsafeFunction "cos"
233 tan = unsafeFunction "tan"
476 tan = unsafeFunction "tan"
@@ -241,12 +484,12 @@ instance (PGFloating ty, PGTyped ty) => Floating
241 acosh x = log (x + sqrt (x*x - 1))
484 acosh x = log (x + sqrt (x*x - 1))
242 atanh x = log ((1 + x) / (1 - x)) / 2
485 atanh x = log ((1 + x) / (1 - x)) / 2
245 :: PGFloating float
488 :: PGFloating float
246 => Expression params tables grouping ('Required (nullity float))
489 => Expression params tables grouping ('Required (nullity float))
247 -> Expression params tables grouping ('Required (nullity float))
490 -> Expression params tables grouping ('Required (nullity float))
248 -> Expression params tables grouping ('Required (nullity float))
491 -> Expression params tables grouping ('Required (nullity float))
249−atan2 y x = UnsafeExpression $
492+atan2_ y x = UnsafeExpression $
250 "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
493 "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
@@ -256,19 +499,19 @@ cast
256cast ty x = UnsafeExpression $ parenthesized $
499cast ty x = UnsafeExpression $ parenthesized $
257 renderExpression x <+> "::" <+> renderTypeExpression ty
500 renderExpression x <+> "::" <+> renderTypeExpression ty
260 :: PGIntegral int
503 :: PGIntegral int
261 => Expression params tables grouping ('Required (nullity int))
504 => Expression params tables grouping ('Required (nullity int))
262 -> Expression params tables grouping ('Required (nullity int))
505 -> Expression params tables grouping ('Required (nullity int))
263 -> Expression params tables grouping ('Required (nullity int))
506 -> Expression params tables grouping ('Required (nullity int))
264−div = unsafeBinaryOp "/"
507+div_ = unsafeBinaryOp "/"
267 :: PGIntegral int
510 :: PGIntegral int
268 => Expression params tables grouping ('Required (nullity int))
511 => Expression params tables grouping ('Required (nullity int))
269 -> Expression params tables grouping ('Required (nullity int))
512 -> Expression params tables grouping ('Required (nullity int))
270 -> Expression params tables grouping ('Required (nullity int))
513 -> Expression params tables grouping ('Required (nullity int))
271−mod = unsafeBinaryOp "%"
514+mod_ = unsafeBinaryOp "%"
274 :: PGFloating frac
517 :: PGFloating frac
@@ -377,19 +620,19 @@ infix 4 /=*
377(>=*) = unsafeBinaryOp ">="
620(>=*) = unsafeBinaryOp ">="
378infix 4 >=*
621infix 4 >=*
381 :: Expression params tables grouping ('Required (nullity ty))
624 :: Expression params tables grouping ('Required (nullity ty))
382 -> Expression params tables grouping ('Required (nullity ty))
625 -> Expression params tables grouping ('Required (nullity ty))
383 -> Expression params tables grouping ('Required (nullity 'PGbool))
626 -> Expression params tables grouping ('Required (nullity 'PGbool))
384−(<*) = unsafeBinaryOp "<"
627+(*<) = unsafeBinaryOp "<"
385−infix 4 <*
628+infix 4 *<
388 :: Expression params tables grouping ('Required (nullity ty))
631 :: Expression params tables grouping ('Required (nullity ty))
389 -> Expression params tables grouping ('Required (nullity ty))
632 -> Expression params tables grouping ('Required (nullity ty))
390 -> Expression params tables grouping ('Required (nullity 'PGbool))
633 -> Expression params tables grouping ('Required (nullity 'PGbool))
391−(<=*) = unsafeBinaryOp "<="
634+(*<=) = unsafeBinaryOp "<="
392−infix 4 <=*
635+infix 4 *<=
395 :: Expression params tables grouping ('Required (nullity ty))
638 :: Expression params tables grouping ('Required (nullity ty))
@@ -456,7 +699,7 @@ aggregation
456-----------------------------------------}
699-----------------------------------------}
459− (tables :: [(Symbol,[(Symbol,ColumnType)])])
702+ (tables :: TablesType)
460 (by :: (Symbol,Symbol)) where
703 (by :: (Symbol,Symbol)) where
462 :: (tables ~ '[table ::: columns], HasColumn column columns ty)
705 :: (tables ~ '[table ::: columns], HasColumn column columns ty)
@@ -527,7 +770,7 @@ instance {-# OVERLAPPABLE #-}
527 ) => GroupedBy table column (tabcol ': bys)
770 ) => GroupedBy table column (tabcol ': bys)
530− ( tables ~ '[table ::: columns]
773+ ( HasUnique table tables columns
531 , HasColumn column columns ty
774 , HasColumn column columns ty
532 , GroupedBy table column bys
775 , GroupedBy table column bys
533 ) => IsLabel column
776 ) => IsLabel column
@@ -541,97 +784,80 @@ instance
541 ) => IsTableColumn table column
784 ) => IsTableColumn table column
542 (Expression params tables ('Grouped bys) ty) where (!) = getGroup2
785 (Expression params tables ('Grouped bys) ty) where (!) = getGroup2
544−data AllOrDistinct = All | Distinct
787+-- data AllOrDistinct = All | Distinct
545− deriving (GHC.Generic,Show,Eq,Ord,Data)
788+-- deriving (GHC.Generic,Show,Eq,Ord,Data)
546−instance NFData AllOrDistinct
789+-- instance NFData AllOrDistinct
548−renderAllOrDistinct :: AllOrDistinct -> ByteString
791+-- renderAllOrDistinct :: AllOrDistinct -> ByteString
549−renderAllOrDistinct = \case
792+-- renderAllOrDistinct = \case
550− All -> "ALL"
793+-- All -> "ALL"
551− Distinct -> "DISTINCT"
794+-- Distinct -> "DISTINCT"
553−unsafeAggregate
796+unsafeAggregate, unsafeAggregateDistinct
554 :: ByteString
797 :: ByteString
556 -> Expression params tables 'Ungrouped ('Required xty)
798 -> Expression params tables 'Ungrouped ('Required xty)
557 -> Expression params tables ('Grouped bys) ('Required yty)
799 -> Expression params tables ('Grouped bys) ('Required yty)
558−unsafeAggregate fun allOrDistinct x = UnsafeExpression $ mconcat
800+unsafeAggregate fun x = UnsafeExpression $ mconcat
801+ [fun, "(", renderExpression x, ")"]
802+unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
559− [fun, "(", renderAllOrDistinct allOrDistinct <+> renderExpression x, ")"]
803+ [fun, "(DISTINCT ", renderExpression x, ")"]
561−sum_
805+sum_, sumDistinct
562 :: PGNum ty
806 :: PGNum ty
564− -> Expression params tables 'Ungrouped ('Required (nullity ty))
807+ => Expression params tables 'Ungrouped ('Required (nullity ty))
565 -> Expression params tables ('Grouped bys) ('Required (nullity ty))
808 -> Expression params tables ('Grouped bys) ('Required (nullity ty))
566sum_ = unsafeAggregate "sum"
809sum_ = unsafeAggregate "sum"
567−
810+sumDistinct = unsafeAggregateDistinct "sum"
569− :: PGIntegral int
812+class PGAvg ty avg | ty -> avg where
570− => AllOrDistinct
813+ avg, avgDistinct
571− -> Expression params tables 'Ungrouped ('Required (nullity int))
814+ :: Expression params tables 'Ungrouped ('Required (nullity ty))
572− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGnumeric))
815+ -> Expression params tables ('Grouped bys) ('Required (nullity avg))
573−avgInt = unsafeAggregate "avg"
816+ avg = unsafeAggregate "avg"
574−
817+ avgDistinct = unsafeAggregateDistinct "avg"
575−avgFloat
818+instance PGAvg 'PGint2 'PGnumeric
576− :: PGFloating float
819+instance PGAvg 'PGint4 'PGnumeric
577− => AllOrDistinct
820+instance PGAvg 'PGint8 'PGnumeric
578− -> Expression params tables 'Ungrouped ('Required (nullity float))
821+instance PGAvg 'PGnumeric 'PGnumeric
579− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGfloat8))
822+instance PGAvg 'PGfloat4 'PGfloat8
580−avgFloat = unsafeAggregate "avg"
823+instance PGAvg 'PGfloat8 'PGfloat8
581−
824+instance PGAvg 'PGinterval 'PGinterval
582−bitAnd
826+bitAnd, bitOr
583 :: PGIntegral int
827 :: PGIntegral int
585− -> Expression params tables 'Ungrouped ('Required (nullity int))
828+ => Expression params tables 'Ungrouped ('Required (nullity int))
586 -> Expression params tables ('Grouped bys) ('Required (nullity int))
829 -> Expression params tables ('Grouped bys) ('Required (nullity int))
587bitAnd = unsafeAggregate "bit_and"
830bitAnd = unsafeAggregate "bit_and"
592− -> Expression params tables 'Ungrouped ('Required (nullity int))
593− -> Expression params tables ('Grouped bys) ('Required (nullity int))
594bitOr = unsafeAggregate "bit_or"
831bitOr = unsafeAggregate "bit_or"
596−boolAnd
833+boolAnd, boolOr
598− -> Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
834+ :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
599 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
835 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
600boolAnd = unsafeAggregate "bool_and"
836boolAnd = unsafeAggregate "bool_and"
604− -> Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
605− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
606boolOr = unsafeAggregate "bool_or"
837boolOr = unsafeAggregate "bool_or"
608−countStar
839+countStar, countDistinctStar
610− -> Expression params table ('Grouped bys) ('Required ('NotNull 'PGint8))
840+ :: Expression params table ('Grouped bys) ('Required ('NotNull 'PGint8))
611−countStar allOrDistinct = UnsafeExpression $
841+countStar = UnsafeExpression $ "count(*)"
612− "count(" <> renderAllOrDistinct allOrDistinct <+> "*)"
842+countDistinctStar = UnsafeExpression $ "count(DISTINCT *)"
616− -> Expression params tables 'Ungrouped ('Required ty)
845+ :: Expression params tables 'Ungrouped ('Required ty)
617 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGint8))
846 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGint8))
618count = unsafeAggregate "count"
847count = unsafeAggregate "count"
622− -> Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
850+ :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
623 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
851 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
624every = unsafeAggregate "every"
852every = unsafeAggregate "every"
628− -> Expression params tables 'Ungrouped ('Required ty)
855+ :: Expression params tables 'Ungrouped ('Required ty)
629 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
856 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
630jsonAgg = unsafeAggregate "json_agg"
857jsonAgg = unsafeAggregate "json_agg"
634− -> Expression params tables 'Ungrouped ('Required ty)
860+ :: Expression params tables 'Ungrouped ('Required ty)
635 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
861 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
636jsonbAgg = unsafeAggregate "jsonb_agg"
862jsonbAgg = unsafeAggregate "jsonb_agg"
@@ -644,24 +870,17 @@ jsonObjectAgg k v = UnsafeExpression $
644 <> ", " <> renderExpression v <> ")"
870 <> ", " <> renderExpression v <> ")"
646jsonbObjectAgg
872jsonbObjectAgg
648− -> Expression params tables 'Ungrouped ('Required ('NotNull keyty))
873+ :: Expression params tables 'Ungrouped ('Required ('NotNull keyty))
649 -> Expression params tables 'Ungrouped ('Required valty)
874 -> Expression params tables 'Ungrouped ('Required valty)
650 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
875 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
651−jsonbObjectAgg allOrDistinct k v = UnsafeExpression $
876+jsonbObjectAgg k v = UnsafeExpression $
652− "jsonb_object_agg(" <> renderAllOrDistinct allOrDistinct
877+ "jsonb_object_agg("
653− <+> renderExpression k <> ", " <> renderExpression v <> ")"
878+ <> renderExpression k <> ", " <> renderExpression v <> ")"
657− -> Expression params tables 'Ungrouped ('Required (nullity ty))
881+ :: Expression params tables 'Ungrouped ('Required (nullity ty))
658 -> Expression params tables ('Grouped bys) ('Required (nullity ty))
882 -> Expression params tables ('Grouped bys) ('Required (nullity ty))
659max_ = unsafeAggregate "max"
883max_ = unsafeAggregate "max"
663− -> Expression params tables 'Ungrouped ('Required (nullity ty))
664− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
665min_ = unsafeAggregate "min"
884min_ = unsafeAggregate "min"
667{-----------------------------------------
886{-----------------------------------------
@@ -669,8 +888,8 @@ tables
669-----------------------------------------}
888-----------------------------------------}
671newtype Table
890newtype Table
672− (schema :: [(Symbol,[(Symbol,ColumnType)])])
891+ (schema :: TablesType)
673− (columns :: [(Symbol,ColumnType)])
892+ (columns :: ColumnsType)
674 = UnsafeTable { renderTable :: ByteString }
893 = UnsafeTable { renderTable :: ByteString }
675 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
894 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
@@ -738,8 +957,8 @@ renderFromClause = \case
739data TableExpression
958data TableExpression
740 (params :: [ColumnType])
959 (params :: [ColumnType])
741− (schema :: [(Symbol,[(Symbol,ColumnType)])])
960+ (schema :: TablesType)
742− (tables :: [(Symbol,[(Symbol,ColumnType)])])
961+ (tables :: TablesType)
743 (grouping :: Grouping)
962 (grouping :: Grouping)
744 = TableExpression
963 = TableExpression
745 { fromClause :: FromClause params schema tables
964 { fromClause :: FromClause params schema tables
@@ -960,8 +1179,8 @@ statements
960-----------------------------------------}
1179-----------------------------------------}
962newtype Definition
1181newtype Definition
963− (schema0 :: [(Symbol,[(Symbol,ColumnType)])])
1182+ (schema0 :: TablesType)
964− (schema1 :: [(Symbol,[(Symbol,ColumnType)])])
1183+ (schema1 :: TablesType)
965 = UnsafeDefinition { renderDefinition :: ByteString }
1184 = UnsafeDefinition { renderDefinition :: ByteString }
966 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1185 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
@@ -971,16 +1190,16 @@ instance Category Definition where
971 renderDefinition ddl0 <+> renderDefinition ddl1
1190 renderDefinition ddl0 <+> renderDefinition ddl1
973newtype Manipulation
1192newtype Manipulation
974− (schema :: [(Symbol,[(Symbol,ColumnType)])])
1193+ (schema :: TablesType)
975 (params :: [ColumnType])
1194 (params :: [ColumnType])
976− (columns :: [(Symbol,ColumnType)])
1195+ (columns :: ColumnsType)
977 = UnsafeManipulation { renderManipulation :: ByteString }
1196 = UnsafeManipulation { renderManipulation :: ByteString }
978 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1197 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
980newtype Query
1199newtype Query
981− (schema :: [(Symbol,[(Symbol,ColumnType)])])
1200+ (schema :: TablesType)
982 (params :: [ColumnType])
1201 (params :: [ColumnType])
983− (columns :: [(Symbol,ColumnType)])
1202+ (columns :: ColumnsType)
984 = UnsafeQuery { renderQuery :: ByteString }
1203 = UnsafeQuery { renderQuery :: ByteString }
985 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1204 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
@@ -1098,8 +1317,8 @@ createTable table columns constraints = UnsafeDefinition $
1098 _ -> ", " <> commaSeparated (renderTableConstraint <$> constraints)
1317 _ -> ", " <> commaSeparated (renderTableConstraint <$> constraints)
1100newtype TableConstraint
1319newtype TableConstraint
1101− (schema :: [(Symbol,[(Symbol,ColumnType)])])
1320+ (schema :: TablesType)
1102− (columns :: [(Symbol,ColumnType)])
1321+ (columns :: ColumnsType)
1103 = UnsafeTableConstraint { renderTableConstraint :: ByteString }
1322 = UnsafeTableConstraint { renderTableConstraint :: ByteString }
1104 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1323 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
@@ -1224,7 +1443,7 @@ addColumnNull
1224 :: KnownSymbol column
1443 :: KnownSymbol column
1225 => Alias column
1444 => Alias column
1226 -> TypeExpression ('Required ('Null ty))
1445 -> TypeExpression ('Required ('Null ty))
1227− -> AlterTable columns ((column ::: 'Required ('Null ty)) ': columns)
1446+ -> AlterTable columns (Create column ('Required ('Null ty)) columns)
1228addColumnNull column ty = UnsafeAlterTable $
1447addColumnNull column ty = UnsafeAlterTable $
1229 "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
1448 "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
@@ -1295,9 +1514,9 @@ insertInto table insert conflict returning = UnsafeManipulation $
1295 <> renderReturningClause returning
1514 <> renderReturningClause returning
1297data ValuesClause
1516data ValuesClause
1298− (schema :: [(Symbol,[(Symbol,ColumnType)])])
1517+ (schema :: TablesType)
1299 (params :: [ColumnType])
1518 (params :: [ColumnType])
1300− (columns :: [(Symbol,ColumnType)])
1519+ (columns :: ColumnsType)
1301 = Values
1520 = Values
1302 (NP (Aliased (Expression params '[] 'Ungrouped)) columns)
1521 (NP (Aliased (Expression params '[] 'Ungrouped)) columns)
1303 [NP (Aliased (Expression params '[] 'Ungrouped)) columns]
1522 [NP (Aliased (Expression params '[] 'Ungrouped)) columns]
@@ -1322,8 +1541,8 @@ renderValuesClause = \case
1323data ReturningClause
1542data ReturningClause
1324 (params :: [ColumnType])
1543 (params :: [ColumnType])
1325− (columns :: [(Symbol,ColumnType)])
1544+ (columns :: ColumnsType)
1326− (results :: [(Symbol,ColumnType)])
1545+ (results :: ColumnsType)
1328 ReturningStar :: ReturningClause params columns columns
1547 ReturningStar :: ReturningClause params columns columns
1329 Returning
1548 Returning