@@ -17,11 +17,205 @@
1717 , StandaloneDeriving
1818 , TypeApplications
1919 , TypeFamilies
20+ , TypeInType
2021 , TypeOperators
2122 , UndecidableInstances
2223#-}
2324
24−module Squeal.PostgreSQL.Statement where
25+module Squeal.PostgreSQL.Statement
26+ ( -- * Column Expressions
27+ Expression (UnsafeExpression, renderExpression)
28+ , HasParameter (param)
29+ , HasColumn (getColumn)
30+ , Column (Column)
31+ , renderColumn
32+ -- ** Default
33+ , def
34+ , unDef
35+ -- ** Null
36+ , null_
37+ , unNull
38+ , coalesce
39+ , fromNull
40+ , isNull
41+ , isn'tNull
42+ , matchNull
43+ , nullIf
44+ -- ** Functions
45+ , unsafeBinaryOp
46+ , unsafeUnaryOp
47+ , unsafeFunction
48+ , atan2_
49+ , cast
50+ , div_
51+ , mod_
52+ , trunc
53+ , round_
54+ , ceiling_
55+ , greatest
56+ , least
57+ -- ** Conditions
58+ , Condition
59+ , true
60+ , false
61+ , not_
62+ , (&&*)
63+ , (||*)
64+ , caseWhenThenElse
65+ , ifThenElse
66+ , (==*)
67+ , (/=*)
68+ , (>=*)
69+ , (*<)
70+ , (*<=)
71+ , (>*)
72+ -- ** Time
73+ , currentDate
74+ , currentTime
75+ , currentTimestamp
76+ , localTime
77+ , localTimestamp
78+ -- ** Aggregation
79+ , unsafeAggregate
80+ , unsafeAggregateDistinct
81+ , sum_
82+ , sumDistinct
83+ , PGAvg (avg, avgDistinct)
84+ , bitAnd
85+ , bitOr
86+ , boolAnd
87+ , boolOr
88+ , countStar
89+ , countDistinctStar
90+ , count
91+ , every
92+ , jsonAgg
93+ , jsonbAgg
94+ , jsonObjectAgg
95+ , jsonbObjectAgg
96+ , max_
97+ , min_
98+ -- * Tables
99+ , Table (UnsafeTable, renderTable)
100+ , HasTable (getTable)
101+ -- ** Froms and Joins
102+ , FromClause (..)
103+ , renderFromClause
104+ -- ** Table Expressions
105+ , TableExpression (..)
106+ , renderTableExpression
107+ , from
108+ , where_
109+ , group
110+ , having
111+ , orderBy
112+ , limit
113+ , offset
114+ , By (By, By2)
115+ , renderBy
116+ , GroupByClause (NoGroups, Group)
117+ , renderGroupByClause
118+ , HavingClause (NoHaving, Having)
119+ , renderHavingClause
120+ , GroupedBy (getGroup1, getGroup2)
121+ , SortExpression (..)
122+ , renderSortExpression
123+ -- * TypeExpression
124+ , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
125+ , PGTyped (pgtype)
126+ , bool
127+ , int2
128+ , smallint
129+ , int4
130+ , int
131+ , integer
132+ , int8
133+ , bigint
134+ , numeric
135+ , float4
136+ , real
137+ , float8
138+ , doublePrecision
139+ , serial2
140+ , smallserial
141+ , serial4
142+ , serial
143+ , serial8
144+ , bigserial
145+ , text
146+ , char
147+ , character
148+ , varchar
149+ , characterVarying
150+ , bytea
151+ , timestamp
152+ , timestampWithTimeZone
153+ , date
154+ , time
155+ , timeWithTimeZone
156+ , interval
157+ , uuid
158+ , json
159+ , jsonb
160+ , notNull
161+ , default_
162+ -- * Statements
163+ -- ** Queries
164+ , Query (UnsafeQuery, renderQuery)
165+ , select
166+ , selectDistinct
167+ , selectStar
168+ , selectDistinctStar
169+ , selectDotStar
170+ , selectDistinctDotStar
171+ , union
172+ , unionAll
173+ , intersect
174+ , intersectAll
175+ , except
176+ , exceptAll
177+ -- ** Data Definition Language
178+ , Definition (UnsafeDefinition, renderDefinition)
179+ , createTable
180+ , TableConstraint (UnsafeTableConstraint, renderTableConstraint)
181+ , check
182+ , unique
183+ , primaryKey
184+ , foreignKey
185+ , OnDelete (OnDeleteNoAction, OnDeleteRestrict, OnDeleteCascade)
186+ , renderOnDelete
187+ , OnUpdate (OnUpdateNoAction, OnUpdateRestrict, OnUpdateCascade)
188+ , renderOnUpdate
189+ , dropTable
190+ , alterTable
191+ , alterTableRename
192+ , AlterTable (UnsafeAlterTable, renderAlterTable)
193+ , addColumnDefault
194+ , addColumnNull
195+ , dropColumn
196+ , renameColumn
197+ , alterColumn
198+ , AlterColumn (UnsafeAlterColumn, renderAlterColumn)
199+ , setDefault
200+ , dropDefault
201+ , setNotNull
202+ , dropNotNull
203+ , alterType
204+ -- ** Data Manipulation Language
205+ , Manipulation (UnsafeManipulation, renderManipulation)
206+ , queryStatement
207+ , insertInto
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
216+ , update
217+ , deleteFrom
218+ ) where
25219
26220import Control.Category
27221import Control.DeepSeq
@@ -33,11 +227,11 @@ import Data.Monoid hiding (All)
33227import Data.Ratio
34228import Data.String
35229import Data.Word
36−import Generics.SOP
230+import Generics.SOP hiding (from)
37231import GHC.Exts
38232import GHC.OverloadedLabels
39233import GHC.TypeLits
40−import Prelude hiding (RealFrac(..), id, (.))
234+import Prelude hiding (id, (.))
41235
42236import qualified GHC.Generics as GHC
43237import qualified Data.ByteString as ByteString
@@ -48,14 +242,31 @@ import Squeal.PostgreSQL.Schema
48242column expressions
49243-----------------------------------------}
50244
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.
249+
250+The expression syntax allows the calculation of
251+values from primitive expression using arithmetic, logical,
252+and other operations.
253+-}
51254newtype Expression
52255 (params :: [ColumnType])
53− (tables :: [(Symbol,[(Symbol,ColumnType)])])
256+ (tables :: TablesType)
54257 (grouping :: Grouping)
55258 (ty :: ColumnType)
56259 = UnsafeExpression { renderExpression :: ByteString }
57260 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
58261
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.
269+-}
59270class (PGTyped (BaseType ty), KnownNat n)
60271 => HasParameter (n :: Nat) params ty | n params -> ty where
61272 param :: proxy n -> Expression params tables grouping ty
@@ -67,18 +278,25 @@ instance {-# OVERLAPPING #-} PGTyped (BaseType ty1)
67278instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
68279 => HasParameter n (ty' : params) ty
69280
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.
286+-}
70287class KnownSymbol column => HasColumn column columns ty
71288 | column columns -> ty where
72289 getColumn
73− :: Alias column
74− -> Expression params '[table ::: columns] 'Ungrouped ty
290+ :: HasUnique table tables columns
291+ => Alias column
292+ -> Expression params tables 'Ungrouped ty
75293 getColumn column = UnsafeExpression $ renderAlias column
76294instance {-# OVERLAPPING #-} KnownSymbol column
77295 => HasColumn column ((column ::: optionality ty) ': tys) ('Required ty)
78296instance {-# OVERLAPPABLE #-} (KnownSymbol column, HasColumn column table ty)
79297 => HasColumn column (ty' ': table) ty
80298
81−instance (HasColumn column columns ty, tables ~ '[table ::: columns])
299+instance (HasColumn column columns ty, HasUnique table tables columns)
82300 => IsLabel column (Expression params tables 'Ungrouped ty) where
83301 fromLabel = getColumn (Alias @column)
84302
@@ -88,7 +306,7 @@ instance (HasTable table tables columns, HasColumn column columns ty)
88306 renderAlias table <> "." <> renderAlias column
89307
90308data Column
91− (columns :: [(Symbol,ColumnType)])
309+ (columns :: ColumnsType)
92310 (columnty :: (Symbol,ColumnType))
93311 where
94312 Column
@@ -99,22 +317,38 @@ deriving instance Show (Column columns columnty)
99317deriving instance Eq (Column columns columnty)
100318deriving instance Ord (Column columns columnty)
101319
320+-- | >>> renderExpression def
321+-- "DEFAULT"
102322def :: Expression params '[] 'Ungrouped ('Optional (nullity ty))
103323def = UnsafeExpression "DEFAULT"
104324
325+-- | >>> renderExpression $ unDef false
326+-- "FALSE"
105327unDef
106328 :: Expression params '[] 'Ungrouped ('Required (nullity ty))
107329 -> Expression params '[] 'Ungrouped ('Optional (nullity ty))
108330unDef = UnsafeExpression . renderExpression
109331
110−null :: Expression params tables grouping (optionality ('Null ty))
111−null = UnsafeExpression "NULL"
112−
332+-- | analagous to `Nothing`
333+--
334+-- >>> renderExpression $ null_
335+-- "NULL"
336+null_ :: Expression params tables grouping (optionality ('Null ty))
337+null_ = UnsafeExpression "NULL"
338+
339+-- | analagous to `Just`
340+--
341+-- >>> renderExpression $ unNull true
342+-- "TRUE"
113343unNull
114344 :: Expression params tables grouping (optionality ('NotNull ty))
115345 -> Expression params tables grouping (optionality ('Null ty))
116346unNull = UnsafeExpression . renderExpression
117347
348+-- | return the leftmost value which is not NULL
349+--
350+-- >>> renderExpression $ coalesce [null_, unNull true] false
351+-- "COALESCE(NULL, TRUE, FALSE)"
118352coalesce
119353 :: [Expression params tables grouping ('Required ('Null ty))]
120354 -> Expression params tables grouping ('Required ('NotNull ty))
@@ -123,22 +357,34 @@ coalesce nullxs notNullx = UnsafeExpression $
123357 "COALESCE" <> parenthesized (commaSeparated
124358 ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
125359
360+-- | analagous to `fromMaybe` using @COALESCE@
361+--
362+-- >>> renderExpression $ fromNull true null_
363+-- "COALESCE(NULL, TRUE)"
126364fromNull
127365 :: Expression params tables grouping ('Required ('NotNull ty))
128366 -> Expression params tables grouping ('Required ('Null ty))
129367 -> Expression params tables grouping ('Required ('NotNull ty))
130368fromNull notNullx nullx = coalesce [nullx] notNullx
131369
370+-- | >>> renderExpression $ null_ & isNull
371+-- "NULL IS NULL"
132372isNull
133373 :: Expression params tables grouping ('Required ('Null ty))
134374 -> Condition params tables grouping
135375isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
136376
377+-- | >>> renderExpression $ null_ & isn'tNull
378+-- "NULL IS NOT NULL"
137379isn'tNull
138380 :: Expression params tables grouping ('Required ('Null ty))
139381 -> Condition params tables grouping
140382isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
141383
384+-- | analagous to `maybe` using @IS NULL@
385+--
386+-- >>> renderExpression $ matchNull true not null_
387+-- "CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT TRUE) END"
142388matchNull
143389 :: Expression params tables grouping ('Required nullty)
144390 -> ( Expression params tables grouping ('Required ('NotNull ty))
@@ -148,24 +394,34 @@ matchNull
148394matchNull y f x = ifThenElse (isNull x) y
149395 (f (UnsafeExpression (renderExpression y)))
150396
397+-- | left inverse to `fromNull`
398+--
399+-- >>> renderExpression $ nullIf false (fromNull false null_)
400+-- "NULL IF (FALSE, COALESCE(NULL, FALSE))"
151401nullIf
152402 :: Expression params tables grouping ('Required ('NotNull ty))
153403 -> Expression params tables grouping ('Required ('NotNull ty))
154404 -> Expression params tables grouping ('Required ('Null ty))
155−nullIf x y = UnsafeExpression $ parenthesized $
156− renderExpression x <> ", " <> renderExpression y
405+nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
406+ (renderExpression x <> ", " <> renderExpression y)
157407
408+-- | >>> renderExpression $ greatest @'[_] currentTimestamp [param (Proxy @1)]
409+-- "GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))"
158410greatest
159− :: [Expression params tables grouping ('Required nullty)]
411+ :: Expression params tables grouping ('Required nullty)
412+ -> [Expression params tables grouping ('Required nullty)]
160413 -> Expression params table grouping ('Required nullty)
161−greatest xs = UnsafeExpression $ "GREATEST("
162− <> commaSeparated (renderExpression <$> xs) <> ")"
414+greatest x xs = UnsafeExpression $ "GREATEST("
415+ <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
163416
417+-- | >>> renderExpression $ least currentTimestamp [null_]
418+-- "LEAST(CURRENT_TIMESTAMP, NULL)"
164419least
165− :: [Expression params tables grouping ('Required nullty)]
420+ :: Expression params tables grouping ('Required nullty)
421+ -> [Expression params tables grouping ('Required nullty)]
166422 -> Expression params table grouping ('Required nullty)
167−least xs = UnsafeExpression $ "LEAST("
168− <> commaSeparated (renderExpression <$> xs) <> ")"
423+least x xs = UnsafeExpression $ "LEAST("
424+ <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
169425
170426unsafeBinaryOp
171427 :: ByteString
@@ -198,16 +454,15 @@ instance PGNum ty
198454 signum = unsafeFunction "sign"
199455 fromInteger
200456 = UnsafeExpression
201− . (<> if decimal (Proxy :: Proxy ty) then "." else "")
202457 . fromString
203458 . show
204459
205−instance PGFloating ty => Fractional
460+instance (PGNum ty, PGFloating ty) => Fractional
206461 (Expression params tables grouping ('Required (nullity ty))) where
207462 (/) = unsafeBinaryOp "/"
208463 fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
209464
210−instance (PGFloating ty, PGTyped ty) => Floating
465+instance (PGNum ty, PGFloating ty) => Floating
211466 (Expression params tables grouping ('Required (nullity ty))) where
212467 pi = UnsafeExpression "pi()"
213468 exp = unsafeFunction "exp"
@@ -215,19 +470,7 @@ instance (PGFloating ty, PGTyped ty) => Floating
215470 sqrt = unsafeFunction "sqrt"
216471 b ** x = UnsafeExpression $
217472 "power(" <> renderExpression b <> ", " <> renderExpression x <> ")"
218− logBase b y = cast pgtype $ logBaseNumeric b y
219− where
220− logBaseNumeric
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
225− [ "log("
226− , renderExpression (cast numeric b')
227− , ", "
228− , renderExpression (cast numeric y')
229− , ")"
230− ]
473+ logBase b y = log y / log b
231474 sin = unsafeFunction "sin"
232475 cos = unsafeFunction "cos"
233476 tan = unsafeFunction "tan"
@@ -241,12 +484,12 @@ instance (PGFloating ty, PGTyped ty) => Floating
241484 acosh x = log (x + sqrt (x*x - 1))
242485 atanh x = log ((1 + x) / (1 - x)) / 2
243486
244−atan2
487+atan2_
245488 :: PGFloating float
246489 => Expression params tables grouping ('Required (nullity float))
247490 -> Expression params tables grouping ('Required (nullity float))
248491 -> Expression params tables grouping ('Required (nullity float))
249−atan2 y x = UnsafeExpression $
492+atan2_ y x = UnsafeExpression $
250493 "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
251494
252495cast
@@ -256,19 +499,19 @@ cast
256499cast ty x = UnsafeExpression $ parenthesized $
257500 renderExpression x <+> "::" <+> renderTypeExpression ty
258501
259−div
502+div_
260503 :: PGIntegral int
261504 => Expression params tables grouping ('Required (nullity int))
262505 -> Expression params tables grouping ('Required (nullity int))
263506 -> Expression params tables grouping ('Required (nullity int))
264−div = unsafeBinaryOp "/"
507+div_ = unsafeBinaryOp "/"
265508
266−mod
509+mod_
267510 :: PGIntegral int
268511 => Expression params tables grouping ('Required (nullity int))
269512 -> Expression params tables grouping ('Required (nullity int))
270513 -> Expression params tables grouping ('Required (nullity int))
271−mod = unsafeBinaryOp "%"
514+mod_ = unsafeBinaryOp "%"
272515
273516trunc
274517 :: PGFloating frac
@@ -377,19 +620,19 @@ infix 4 /=*
377620(>=*) = unsafeBinaryOp ">="
378621infix 4 >=*
379622
380−(<*)
623+(*<)
381624 :: Expression params tables grouping ('Required (nullity ty))
382625 -> Expression params tables grouping ('Required (nullity ty))
383626 -> Expression params tables grouping ('Required (nullity 'PGbool))
384−(<*) = unsafeBinaryOp "<"
385−infix 4 <*
627+(*<) = unsafeBinaryOp "<"
628+infix 4 *<
386629
387−(<=*)
630+(*<=)
388631 :: Expression params tables grouping ('Required (nullity ty))
389632 -> Expression params tables grouping ('Required (nullity ty))
390633 -> Expression params tables grouping ('Required (nullity 'PGbool))
391−(<=*) = unsafeBinaryOp "<="
392−infix 4 <=*
634+(*<=) = unsafeBinaryOp "<="
635+infix 4 *<=
393636
394637(>*)
395638 :: Expression params tables grouping ('Required (nullity ty))
@@ -456,7 +699,7 @@ aggregation
456699-----------------------------------------}
457700
458701data By
459− (tables :: [(Symbol,[(Symbol,ColumnType)])])
702+ (tables :: TablesType)
460703 (by :: (Symbol,Symbol)) where
461704 By
462705 :: (tables ~ '[table ::: columns], HasColumn column columns ty)
@@ -527,7 +770,7 @@ instance {-# OVERLAPPABLE #-}
527770 ) => GroupedBy table column (tabcol ': bys)
528771
529772instance
530− ( tables ~ '[table ::: columns]
773+ ( HasUnique table tables columns
531774 , HasColumn column columns ty
532775 , GroupedBy table column bys
533776 ) => IsLabel column
@@ -541,97 +784,80 @@ instance
541784 ) => IsTableColumn table column
542785 (Expression params tables ('Grouped bys) ty) where (!) = getGroup2
543786
544−data AllOrDistinct = All | Distinct
545− deriving (GHC.Generic,Show,Eq,Ord,Data)
546−instance NFData AllOrDistinct
787+-- data AllOrDistinct = All | Distinct
788+-- deriving (GHC.Generic,Show,Eq,Ord,Data)
789+-- instance NFData AllOrDistinct
547790
548−renderAllOrDistinct :: AllOrDistinct -> ByteString
549−renderAllOrDistinct = \case
550− All -> "ALL"
551− Distinct -> "DISTINCT"
791+-- renderAllOrDistinct :: AllOrDistinct -> ByteString
792+-- renderAllOrDistinct = \case
793+-- All -> "ALL"
794+-- Distinct -> "DISTINCT"
552795
553−unsafeAggregate
796+unsafeAggregate, unsafeAggregateDistinct
554797 :: ByteString
555− -> AllOrDistinct
556798 -> Expression params tables 'Ungrouped ('Required xty)
557799 -> Expression params tables ('Grouped bys) ('Required yty)
558−unsafeAggregate fun allOrDistinct x = UnsafeExpression $ mconcat
559− [fun, "(", renderAllOrDistinct allOrDistinct <+> renderExpression x, ")"]
800+unsafeAggregate fun x = UnsafeExpression $ mconcat
801+ [fun, "(", renderExpression x, ")"]
802+unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
803+ [fun, "(DISTINCT ", renderExpression x, ")"]
560804
561−sum_
805+sum_, sumDistinct
562806 :: PGNum ty
563− => AllOrDistinct
564− -> Expression params tables 'Ungrouped ('Required (nullity ty))
807+ => Expression params tables 'Ungrouped ('Required (nullity ty))
565808 -> Expression params tables ('Grouped bys) ('Required (nullity ty))
566809sum_ = unsafeAggregate "sum"
567−
568−avgInt
569− :: PGIntegral int
570− => AllOrDistinct
571− -> Expression params tables 'Ungrouped ('Required (nullity int))
572− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGnumeric))
573−avgInt = unsafeAggregate "avg"
574−
575−avgFloat
576− :: PGFloating float
577− => AllOrDistinct
578− -> Expression params tables 'Ungrouped ('Required (nullity float))
579− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGfloat8))
580−avgFloat = unsafeAggregate "avg"
581−
582−bitAnd
810+sumDistinct = unsafeAggregateDistinct "sum"
811+
812+class PGAvg ty avg | ty -> avg where
813+ avg, avgDistinct
814+ :: Expression params tables 'Ungrouped ('Required (nullity ty))
815+ -> Expression params tables ('Grouped bys) ('Required (nullity avg))
816+ avg = unsafeAggregate "avg"
817+ avgDistinct = unsafeAggregateDistinct "avg"
818+instance PGAvg 'PGint2 'PGnumeric
819+instance PGAvg 'PGint4 'PGnumeric
820+instance PGAvg 'PGint8 'PGnumeric
821+instance PGAvg 'PGnumeric 'PGnumeric
822+instance PGAvg 'PGfloat4 'PGfloat8
823+instance PGAvg 'PGfloat8 'PGfloat8
824+instance PGAvg 'PGinterval 'PGinterval
825+
826+bitAnd, bitOr
583827 :: PGIntegral int
584− => AllOrDistinct
585− -> Expression params tables 'Ungrouped ('Required (nullity int))
828+ => Expression params tables 'Ungrouped ('Required (nullity int))
586829 -> Expression params tables ('Grouped bys) ('Required (nullity int))
587830bitAnd = unsafeAggregate "bit_and"
588−
589−bitOr
590− :: PGIntegral int
591− => AllOrDistinct
592− -> Expression params tables 'Ungrouped ('Required (nullity int))
593− -> Expression params tables ('Grouped bys) ('Required (nullity int))
594831bitOr = unsafeAggregate "bit_or"
595832
596−boolAnd
597− :: AllOrDistinct
598− -> Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
833+boolAnd, boolOr
834+ :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
599835 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
600836boolAnd = unsafeAggregate "bool_and"
601−
602−boolOr
603− :: AllOrDistinct
604− -> Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
605− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
606837boolOr = unsafeAggregate "bool_or"
607838
608−countStar
609− :: AllOrDistinct
610− -> Expression params table ('Grouped bys) ('Required ('NotNull 'PGint8))
611−countStar allOrDistinct = UnsafeExpression $
612− "count(" <> renderAllOrDistinct allOrDistinct <+> "*)"
839+countStar, countDistinctStar
840+ :: Expression params table ('Grouped bys) ('Required ('NotNull 'PGint8))
841+countStar = UnsafeExpression $ "count(*)"
842+countDistinctStar = UnsafeExpression $ "count(DISTINCT *)"
613843
614844count
615− :: AllOrDistinct
616− -> Expression params tables 'Ungrouped ('Required ty)
845+ :: Expression params tables 'Ungrouped ('Required ty)
617846 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGint8))
618847count = unsafeAggregate "count"
619848
620849every
621− :: AllOrDistinct
622− -> Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
850+ :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
623851 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
624852every = unsafeAggregate "every"
625853
626854jsonAgg
627− :: AllOrDistinct
628− -> Expression params tables 'Ungrouped ('Required ty)
855+ :: Expression params tables 'Ungrouped ('Required ty)
629856 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
630857jsonAgg = unsafeAggregate "json_agg"
631858
632859jsonbAgg
633− :: AllOrDistinct
634− -> Expression params tables 'Ungrouped ('Required ty)
860+ :: Expression params tables 'Ungrouped ('Required ty)
635861 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
636862jsonbAgg = unsafeAggregate "jsonb_agg"
637863
@@ -644,24 +870,17 @@ jsonObjectAgg k v = UnsafeExpression $
644870 <> ", " <> renderExpression v <> ")"
645871
646872jsonbObjectAgg
647− :: AllOrDistinct
648− -> Expression params tables 'Ungrouped ('Required ('NotNull keyty))
873+ :: Expression params tables 'Ungrouped ('Required ('NotNull keyty))
649874 -> Expression params tables 'Ungrouped ('Required valty)
650875 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
651−jsonbObjectAgg allOrDistinct k v = UnsafeExpression $
652− "jsonb_object_agg(" <> renderAllOrDistinct allOrDistinct
653− <+> renderExpression k <> ", " <> renderExpression v <> ")"
876+jsonbObjectAgg k v = UnsafeExpression $
877+ "jsonb_object_agg("
878+ <> renderExpression k <> ", " <> renderExpression v <> ")"
654879
655−max_
656− :: AllOrDistinct
657− -> Expression params tables 'Ungrouped ('Required (nullity ty))
880+max_, min_
881+ :: Expression params tables 'Ungrouped ('Required (nullity ty))
658882 -> Expression params tables ('Grouped bys) ('Required (nullity ty))
659883max_ = unsafeAggregate "max"
660−
661−min_
662− :: AllOrDistinct
663− -> Expression params tables 'Ungrouped ('Required (nullity ty))
664− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
665884min_ = unsafeAggregate "min"
666885
667886{-----------------------------------------
@@ -669,8 +888,8 @@ tables
669888-----------------------------------------}
670889
671890newtype Table
672− (schema :: [(Symbol,[(Symbol,ColumnType)])])
673− (columns :: [(Symbol,ColumnType)])
891+ (schema :: TablesType)
892+ (columns :: ColumnsType)
674893 = UnsafeTable { renderTable :: ByteString }
675894 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
676895
@@ -738,8 +957,8 @@ renderFromClause = \case
738957
739958data TableExpression
740959 (params :: [ColumnType])
741− (schema :: [(Symbol,[(Symbol,ColumnType)])])
742− (tables :: [(Symbol,[(Symbol,ColumnType)])])
960+ (schema :: TablesType)
961+ (tables :: TablesType)
743962 (grouping :: Grouping)
744963 = TableExpression
745964 { fromClause :: FromClause params schema tables
@@ -960,8 +1179,8 @@ statements
9601179-----------------------------------------}
9611180
9621181newtype Definition
963− (schema0 :: [(Symbol,[(Symbol,ColumnType)])])
964− (schema1 :: [(Symbol,[(Symbol,ColumnType)])])
1182+ (schema0 :: TablesType)
1183+ (schema1 :: TablesType)
9651184 = UnsafeDefinition { renderDefinition :: ByteString }
9661185 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
9671186
@@ -971,16 +1190,16 @@ instance Category Definition where
9711190 renderDefinition ddl0 <+> renderDefinition ddl1
9721191
9731192newtype Manipulation
974− (schema :: [(Symbol,[(Symbol,ColumnType)])])
1193+ (schema :: TablesType)
9751194 (params :: [ColumnType])
976− (columns :: [(Symbol,ColumnType)])
1195+ (columns :: ColumnsType)
9771196 = UnsafeManipulation { renderManipulation :: ByteString }
9781197 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
9791198
9801199newtype Query
981− (schema :: [(Symbol,[(Symbol,ColumnType)])])
1200+ (schema :: TablesType)
9821201 (params :: [ColumnType])
983− (columns :: [(Symbol,ColumnType)])
1202+ (columns :: ColumnsType)
9841203 = UnsafeQuery { renderQuery :: ByteString }
9851204 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
9861205
@@ -1098,8 +1317,8 @@ createTable table columns constraints = UnsafeDefinition $
10981317 _ -> ", " <> commaSeparated (renderTableConstraint <$> constraints)
10991318
11001319newtype TableConstraint
1101− (schema :: [(Symbol,[(Symbol,ColumnType)])])
1102− (columns :: [(Symbol,ColumnType)])
1320+ (schema :: TablesType)
1321+ (columns :: ColumnsType)
11031322 = UnsafeTableConstraint { renderTableConstraint :: ByteString }
11041323 deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
11051324
@@ -1224,7 +1443,7 @@ addColumnNull
12241443 :: KnownSymbol column
12251444 => Alias column
12261445 -> TypeExpression ('Required ('Null ty))
1227− -> AlterTable columns ((column ::: 'Required ('Null ty)) ': columns)
1446+ -> AlterTable columns (Create column ('Required ('Null ty)) columns)
12281447addColumnNull column ty = UnsafeAlterTable $
12291448 "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
12301449
@@ -1295,9 +1514,9 @@ insertInto table insert conflict returning = UnsafeManipulation $
12951514 <> renderReturningClause returning
12961515
12971516data ValuesClause
1298− (schema :: [(Symbol,[(Symbol,ColumnType)])])
1517+ (schema :: TablesType)
12991518 (params :: [ColumnType])
1300− (columns :: [(Symbol,ColumnType)])
1519+ (columns :: ColumnsType)
13011520 = Values
13021521 (NP (Aliased (Expression params '[] 'Ungrouped)) columns)
13031522 [NP (Aliased (Expression params '[] 'Ungrouped)) columns]
@@ -1322,8 +1541,8 @@ renderValuesClause = \case
13221541
13231542data ReturningClause
13241543 (params :: [ColumnType])
1325− (columns :: [(Symbol,ColumnType)])
1326− (results :: [(Symbol,ColumnType)])
1544+ (columns :: ColumnsType)
1545+ (results :: ColumnsType)
13271546 where
13281547 ReturningStar :: ReturningClause params columns columns
13291548 Returning