@@ -1,7 +1,7 @@
2Module: Squeal.PostgreSQL.Query
2Module: Squeal.PostgreSQL.Query
3Description: Squeal queries
3Description: Squeal queries
4−Copyright: (c) Eitan Chatav, 2019
4+Copyright: (c) Eitan Chatav, 2017
5Maintainer: eitan@morphism.tech
5Maintainer: eitan@morphism.tech
6Stability: experimental
6Stability: experimental
@@ -17,9 +17,7 @@ Squeal queries.
17 , GeneralizedNewtypeDeriving
17 , GeneralizedNewtypeDeriving
18 , LambdaCase
18 , LambdaCase
19 , MultiParamTypeClasses
19 , MultiParamTypeClasses
21 , OverloadedStrings
20 , OverloadedStrings
22− , QuantifiedConstraints
23 , ScopedTypeVariables
21 , ScopedTypeVariables
24 , StandaloneDeriving
22 , StandaloneDeriving
25 , TypeApplications
23 , TypeApplications
@@ -32,15 +30,13 @@ Squeal queries.
33module Squeal.PostgreSQL.Query
31module Squeal.PostgreSQL.Query
34 ( -- * Queries
32 ( -- * Queries
36− , Query (..)
34+ , Query_
37 -- ** Select
35 -- ** Select
40 , selectDistinct
38 , selectDistinct
41 , selectDistinct_
39 , selectDistinct_
44 , Selection (..)
40 , Selection (..)
45 -- ** Values
41 -- ** Values
@@ -55,13 +51,30 @@ module Squeal.PostgreSQL.Query
55 -- ** With
51 -- ** With
56 , With (..)
52 , With (..)
57 , CommonTableExpression (..)
53 , CommonTableExpression (..)
58− , withRecursive
54+ , renderCommonTableExpression
55+ , renderCommonTableExpressions
64+ , jsonbPopulateRecord
65+ , jsonPopulateRecordSet
66+ , jsonbPopulateRecordSet
59 -- * Table Expressions
71 -- * Table Expressions
60 , TableExpression (..)
72 , TableExpression (..)
67 -- * From Clauses
80 -- * From Clauses
@@ -79,6 +92,35 @@ module Squeal.PostgreSQL.Query
80 , GroupByClause (..)
93 , GroupByClause (..)
81 , HavingClause (..)
94 , HavingClause (..)
96+ , SortExpression (..)
97+ -- * Subquery Expressions
84import Control.DeepSeq
126import Control.DeepSeq
@@ -91,67 +133,13 @@ import GHC.TypeLits
92import qualified GHC.Generics as GHC
134import qualified GHC.Generics as GHC
94−import Squeal.PostgreSQL.Alias
95import Squeal.PostgreSQL.Expression
136import Squeal.PostgreSQL.Expression
96−import Squeal.PostgreSQL.Expression.Logic
97−import Squeal.PostgreSQL.Expression.Sort
98−import Squeal.PostgreSQL.Expression.Window
99−import Squeal.PostgreSQL.List
100−import Squeal.PostgreSQL.PG
101import Squeal.PostgreSQL.Render
137import Squeal.PostgreSQL.Render
102import Squeal.PostgreSQL.Schema
138import Squeal.PostgreSQL.Schema
105−-- >>> import Squeal.PostgreSQL
106−-- >>> import Data.Int (Int32, Int64)
107−-- >>> import Data.Monoid (Sum (..))
108−-- >>> import Data.Text (Text)
109−-- >>> import qualified Generics.SOP as SOP
112−The process of retrieving or the command to retrieve data from
113−a database is called a `Query`.
115−The general `Query` type is parameterized by
117−* @outer :: FromType@ - outer scope for a correlated subquery,
118−* @commons :: FromType@ - scope for all `common` table expressions,
119−* @schemas :: SchemasType@ - scope for all `table`s and `view`s,
120−* @params :: [NullityType]@ - scope for all `Squeal.Expression.Parameter.parameter`s,
121−* @row :: RowType@ - return type of the `Query`.
125− (commons :: FromType)
126− (schemas :: SchemasType)
127− (params :: [NullityType])
129− = UnsafeQuery { renderQuery :: ByteString }
130− deriving (GHC.Generic,Show,Eq,Ord,NFData)
131−instance RenderSQL (Query outer commons schemas params row) where renderSQL = renderQuery
134−The top level `Query_` type is parameterized by a @schemas@ `SchemasType`,
141+The process of retrieving or the command to retrieve data from a database
135−against which the query is type-checked, an input @parameters@ Haskell `Type`,
136−and an ouput row Haskell `Type`.
138−A top-level query can be run
139−using `Squeal.PostgreSQL.PQ.runQueryParams`, or if @parameters = ()@
140−using `Squeal.PostgreSQL.PQ.runQuery`.
142−Generally, @parameters@ will be a Haskell tuple or record whose entries
143−may be referenced using positional
144−`Squeal.PostgreSQL.Expression.Parameter.parameter`s and @row@ will be a
145−Haskell record, whose entries will be targeted using overloaded labels.
147−Let's see some examples of queries.
142+is called a `Query`. Let's see some examples of queries.
149−>>> :set -XDeriveAnyClass -XDerivingStrategies
151−data Row a b = Row { col1 :: a, col2 :: b }
152− deriving stock (GHC.Generic)
153− deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
156simple query:
144simple query:
@@ -159,7 +147,7 @@ simple query:
159>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
147>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
162− query :: Query_ (Public Schema) () (Row Int32 Int32)
150+ query :: Query '[] (Public Schema) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
163 query = select Star (from (table #tab))
151 query = select Star (from (table #tab))
164in printSQL query
152in printSQL query
@@ -169,21 +157,21 @@ restricted query:
172− query :: Query_ (Public Schema) () (Row Int32 Int32)
160+ query :: Query '[] (Public Schema) '[] '["sum" ::: 'NotNull 'PGint4, "col1" ::: 'NotNull 'PGint4]
174− select_ ((#col1 + #col2) `as` #col1 :* #col1 `as` #col2)
162+ select_ ((#col1 + #col2) `as` #sum :* #col1)
175 ( from (table #tab)
163 ( from (table #tab)
176 & where_ (#col1 .> #col2)
164 & where_ (#col1 .> #col2)
177 & where_ (#col2 .> 0) )
165 & where_ (#col2 .> 0) )
178in printSQL query
166in printSQL query
180−SELECT ("col1" + "col2") AS "col1", "col1" AS "col2" FROM "tab" AS "tab" WHERE (("col1" > "col2") AND ("col2" > 0))
168+SELECT ("col1" + "col2") AS "sum", "col1" AS "col1" FROM "tab" AS "tab" WHERE (("col1" > "col2") AND ("col2" > 0))
186− query :: Query_ (Public Schema) () (Row Int32 Int32)
174+ query :: Query '[] (Public Schema) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
187 query = select Star (from (subquery (select Star (from (table #tab)) `as` #sub)))
175 query = select Star (from (subquery (select Star (from (table #tab)) `as` #sub)))
188in printSQL query
176in printSQL query
@@ -193,7 +181,7 @@ limits and offsets:
196− query :: Query_ (Public Schema) () (Row Int32 Int32)
184+ query :: Query '[] (Public Schema) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
197 query = select Star (from (table #tab) & limit 100 & offset 2 & limit 50 & offset 2)
185 query = select Star (from (table #tab) & limit 100 & offset 2 & limit 50 & offset 2)
198in printSQL query
186in printSQL query
@@ -203,7 +191,7 @@ parameterized query:
206− query :: Query_ (Public Schema) (Only Int32) (Row Int32 Int32)
194+ query :: Query '[] (Public Schema) '[ 'NotNull 'PGint4] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
207 query = select Star (from (table #tab) & where_ (#col1 .> param @1))
195 query = select Star (from (table #tab) & where_ (#col1 .> param @1))
208in printSQL query
196in printSQL query
@@ -213,21 +201,21 @@ aggregation query:
216− query :: Query_ (Public Schema) () (Row Int64 Int32)
204+ query :: Query '[] (Public Schema) '[] '["sum" ::: 'NotNull 'PGint4, "col1" ::: 'NotNull 'PGint4 ]
218− select_ ((fromNull 0 (sum_ (All #col2))) `as` #col1 :* #col1 `as` #col2)
206+ select_ (sum_ #col2 `as` #sum :* #col1)
219 ( from (table (#tab `as` #table1))
207 ( from (table (#tab `as` #table1))
220 & groupBy #col1
208 & groupBy #col1
221− & having (sum_ (Distinct #col2) .> 1) )
209+ & having (#col1 + sum_ #col2 .> 1) )
222in printSQL query
210in printSQL query
224−SELECT COALESCE(sum(ALL "col2"), 0) AS "col1", "col1" AS "col2" FROM "tab" AS "table1" GROUP BY "col1" HAVING (sum(DISTINCT "col2") > 1)
212+SELECT sum("col2") AS "sum", "col1" AS "col1" FROM "tab" AS "table1" GROUP BY "col1" HAVING (("col1" + sum("col2")) > 1)
226sorted query:
214sorted query:
230− query :: Query_ (Public Schema) () (Row Int32 Int32)
218+ query :: Query '[] (Public Schema) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
231 query = select Star (from (table #tab) & orderBy [#col1 & Asc])
219 query = select Star (from (table #tab) & orderBy [#col1 & Asc])
232in printSQL query
220in printSQL query
@@ -235,12 +223,14 @@ SELECT * FROM "tab" AS "tab" ORDER BY "col1" ASC
226+>>> :set -XFlexibleContexts
239type OrdersColumns =
228type OrdersColumns =
240 '[ "id" ::: 'NoDef :=> 'NotNull 'PGint4
229 '[ "id" ::: 'NoDef :=> 'NotNull 'PGint4
241− , "price" ::: 'NoDef :=> 'NotNull 'PGfloat4
230+ , "price" ::: 'NoDef :=> 'NotNull 'PGfloat4
242− , "customer_id" ::: 'NoDef :=> 'NotNull 'PGint4
231+ , "customer_id" ::: 'NoDef :=> 'NotNull 'PGint4
243− , "shipper_id" ::: 'NoDef :=> 'NotNull 'PGint4 ]
232+ , "shipper_id" ::: 'NoDef :=> 'NotNull 'PGint4
@@ -256,27 +246,22 @@ type OrdersConstraints =
257type OrdersSchema =
247type OrdersSchema =
258 '[ "orders" ::: 'Table (OrdersConstraints :=> OrdersColumns)
248 '[ "orders" ::: 'Table (OrdersConstraints :=> OrdersColumns)
259− , "customers" ::: 'Table (CustomersConstraints :=> NamesColumns)
249+ , "customers" ::: 'Table (CustomersConstraints :=> NamesColumns)
260− , "shippers" ::: 'Table (ShippersConstraints :=> NamesColumns) ]
250+ , "shippers" ::: 'Table (ShippersConstraints :=> NamesColumns) ]
266− , customerName :: Text
267− , shipperName :: Text
268− } deriving GHC.Generic
269−instance SOP.Generic Order
270−instance SOP.HasDatatypeInfo Order
275− query :: Query_ (Public OrdersSchema) () Order
255+ query :: Query '[] (Public OrdersSchema)
257+ '[ "order_price" ::: 'NotNull 'PGfloat4
258+ , "customer_name" ::: 'NotNull 'PGtext
259+ , "shipper_name" ::: 'NotNull 'PGtext
276 query = select_
261 query = select_
277− ( #o ! #price `as` #price :*
262+ ( #o ! #price `as` #order_price :*
278− #c ! #name `as` #customerName :*
263+ #c ! #name `as` #customer_name :*
279− #s ! #name `as` #shipperName )
264+ #s ! #name `as` #shipper_name )
280 ( from (table (#orders `as` #o)
265 ( from (table (#orders `as` #o)
281 & innerJoin (table (#customers `as` #c))
266 & innerJoin (table (#customers `as` #c))
282 (#o ! #customer_id .== #c ! #id)
267 (#o ! #customer_id .== #c ! #id)
@@ -284,16 +269,14 @@ let
284 (#o ! #shipper_id .== #s ! #id)) )
269 (#o ! #shipper_id .== #s ! #id)) )
285in printSQL query
270in printSQL query
287−SELECT "o"."price" AS "price", "c"."name" AS "customerName", "s"."name" AS "shipperName" FROM "orders" AS "o" INNER JOIN "customers" AS "c" ON ("o"."customer_id" = "c"."id") INNER JOIN "shippers" AS "s" ON ("o"."shipper_id" = "s"."id")
272+SELECT "o"."price" AS "order_price", "c"."name" AS "customer_name", "s"."name" AS "shipper_name" FROM "orders" AS "o" INNER JOIN "customers" AS "c" ON ("o"."customer_id" = "c"."id") INNER JOIN "shippers" AS "s" ON ("o"."shipper_id" = "s"."id")
289self-join:
274self-join:
293− query :: Query_ (Public Schema) () (Row Int32 Int32)
278+ query :: Query '[] (Public Schema) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
296− (from (table (#tab `as` #t1) & crossJoin (table (#tab `as` #t2))))
279+ query = select (#t1 & DotStar) (from (table (#tab `as` #t1) & crossJoin (table (#tab `as` #t2))))
297in printSQL query
280in printSQL query
299SELECT "t1".* FROM "tab" AS "t1" CROSS JOIN "tab" AS "t2"
282SELECT "t1".* FROM "tab" AS "t1" CROSS JOIN "tab" AS "t2"
@@ -302,19 +285,17 @@ value queries:
305− query :: Query_ schemas () (Row String Bool)
288+ query :: Query commons schemas '[] '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
306− query = values
289+ query = values (1 `as` #foo :* true `as` #bar) [2 `as` #foo :* false `as` #bar]
307− ("true" `as` #col1 :* true `as` #col2)
308− ["false" `as` #col1 :* false `as` #col2]
309in printSQL query
290in printSQL query
311−SELECT * FROM (VALUES (E'true', TRUE), (E'false', FALSE)) AS t ("col1", "col2")
292+SELECT * FROM (VALUES (1, TRUE), (2, FALSE)) AS t ("foo", "bar")
313set operations:
294set operations:
317− query :: Query_ (Public Schema) () (Row Int32 Int32)
298+ query :: Query '[] (Public Schema) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
318 query = select Star (from (table #tab)) `unionAll` select Star (from (table #tab))
299 query = select Star (from (table #tab)) `unionAll` select Star (from (table #tab))
319in printSQL query
300in printSQL query
@@ -324,7 +305,7 @@ with queries:
327− query :: Query_ (Public Schema) () (Row Int32 Int32)
308+ query :: Query '[] (Public Schema) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
328 query = with (
309 query = with (
329 select Star (from (table #tab)) `as` #cte1 :>>
310 select Star (from (table #tab)) `as` #cte1 :>>
330 select Star (from (common #cte1)) `as` #cte2
311 select Star (from (common #cte1)) `as` #cte2
@@ -332,47 +313,25 @@ let
332in printSQL query
313in printSQL query
334WITH "cte1" AS (SELECT * FROM "tab" AS "tab"), "cte2" AS (SELECT * FROM "cte1" AS "cte1") SELECT * FROM "cte2" AS "cte2"
315WITH "cte1" AS (SELECT * FROM "tab" AS "tab"), "cte2" AS (SELECT * FROM "cte1" AS "cte1") SELECT * FROM "cte2" AS "cte2"
336−window function queries
340− query :: Query_ (Public Schema) () (Row Int32 Int64)
342− (#col1 & Also (rank `as` #col2 `Over` (partitionBy #col1 & orderBy [#col2 & Asc])))
346−SELECT "col1" AS "col1", rank() OVER (PARTITION BY "col1" ORDER BY "col2" ASC) AS "col2" FROM "tab" AS "tab"
348−correlated subqueries
352− query :: Query_ (Public Schema) () (Only Int32)
354− select (#col1 `as` #fromOnly) (from (table (#tab `as` #t1))
356− select Star (from (table (#tab `as` #t2))
357− & where_ (#t2 ! #col2 .== #t1 ! #col1)))))
360−SELECT "col1" AS "fromOnly" FROM "tab" AS "t1" WHERE EXISTS (SELECT * FROM "tab" AS "t2" WHERE ("t2"."col2" = "t1"."col1"))
363−type family Query_
317+newtype Query
318+ (commons :: FromType)
364 (schemas :: SchemasType)
319 (schemas :: SchemasType)
365− (parameters :: Type)
320+ (params :: [NullityType])
366− (row :: Type) where
321+ (row :: RowType)
367− Query_ schemas params row =
322+ = UnsafeQuery { renderQuery :: ByteString }
323+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
324+instance RenderSQL (Query commons schemas params row) where renderSQL = renderQuery
326+type family Query_ (schemas :: SchemasType) (params :: Type) (row :: Type) where
368− Query '[] '[] schemas (TuplePG params) (RowPG row)
327+ Query_ schemas params row = Query '[] schemas (TuplePG params) (RowPG row)
370-- | The results of two queries can be combined using the set operation
329-- | The results of two queries can be combined using the set operation
371-- `union`. Duplicate rows are eliminated.
330-- `union`. Duplicate rows are eliminated.
373− :: Query outer commons schemas params columns
332+ :: Query commons schemas params columns
374− -> Query outer commons schemas params columns
333+ -> Query commons schemas params columns
375− -> Query outer commons schemas params columns
334+ -> Query commons schemas params columns
376q1 `union` q2 = UnsafeQuery $
335q1 `union` q2 = UnsafeQuery $
377 parenthesized (renderSQL q1)
336 parenthesized (renderSQL q1)
378 <+> "UNION"
337 <+> "UNION"
@@ -381,9 +340,9 @@ q1 `union` q2 = UnsafeQuery $
381-- | The results of two queries can be combined using the set operation
340-- | The results of two queries can be combined using the set operation
382-- `unionAll`, the disjoint union. Duplicate rows are retained.
341-- `unionAll`, the disjoint union. Duplicate rows are retained.
384− :: Query outer commons schemas params columns
343+ :: Query commons schemas params columns
385− -> Query outer commons schemas params columns
344+ -> Query commons schemas params columns
386− -> Query outer commons schemas params columns
345+ -> Query commons schemas params columns
387q1 `unionAll` q2 = UnsafeQuery $
346q1 `unionAll` q2 = UnsafeQuery $
388 parenthesized (renderSQL q1)
347 parenthesized (renderSQL q1)
389 <+> "UNION" <+> "ALL"
348 <+> "UNION" <+> "ALL"
@@ -392,9 +351,9 @@ q1 `unionAll` q2 = UnsafeQuery $
392-- | The results of two queries can be combined using the set operation
351-- | The results of two queries can be combined using the set operation
393-- `intersect`, the intersection. Duplicate rows are eliminated.
352-- `intersect`, the intersection. Duplicate rows are eliminated.
395− :: Query outer commons schemas params columns
354+ :: Query commons schemas params columns
396− -> Query outer commons schemas params columns
355+ -> Query commons schemas params columns
397− -> Query outer commons schemas params columns
356+ -> Query commons schemas params columns
398q1 `intersect` q2 = UnsafeQuery $
357q1 `intersect` q2 = UnsafeQuery $
399 parenthesized (renderSQL q1)
358 parenthesized (renderSQL q1)
400 <+> "INTERSECT"
359 <+> "INTERSECT"
@@ -403,9 +362,9 @@ q1 `intersect` q2 = UnsafeQuery $
403-- | The results of two queries can be combined using the set operation
362-- | The results of two queries can be combined using the set operation
404-- `intersectAll`, the intersection. Duplicate rows are retained.
363-- `intersectAll`, the intersection. Duplicate rows are retained.
405intersectAll
364intersectAll
406− :: Query outer commons schemas params columns
365+ :: Query commons schemas params columns
407− -> Query outer commons schemas params columns
366+ -> Query commons schemas params columns
408− -> Query outer commons schemas params columns
367+ -> Query commons schemas params columns
409q1 `intersectAll` q2 = UnsafeQuery $
368q1 `intersectAll` q2 = UnsafeQuery $
410 parenthesized (renderSQL q1)
369 parenthesized (renderSQL q1)
411 <+> "INTERSECT" <+> "ALL"
370 <+> "INTERSECT" <+> "ALL"
@@ -414,9 +373,9 @@ q1 `intersectAll` q2 = UnsafeQuery $
414-- | The results of two queries can be combined using the set operation
373-- | The results of two queries can be combined using the set operation
415-- `except`, the set difference. Duplicate rows are eliminated.
374-- `except`, the set difference. Duplicate rows are eliminated.
417− :: Query outer commons schemas params columns
376+ :: Query commons schemas params columns
418− -> Query outer commons schemas params columns
377+ -> Query commons schemas params columns
419− -> Query outer commons schemas params columns
378+ -> Query commons schemas params columns
420q1 `except` q2 = UnsafeQuery $
379q1 `except` q2 = UnsafeQuery $
421 parenthesized (renderSQL q1)
380 parenthesized (renderSQL q1)
422 <+> "EXCEPT"
381 <+> "EXCEPT"
@@ -425,9 +384,9 @@ q1 `except` q2 = UnsafeQuery $
425-- | The results of two queries can be combined using the set operation
384-- | The results of two queries can be combined using the set operation
426-- `exceptAll`, the set difference. Duplicate rows are retained.
385-- `exceptAll`, the set difference. Duplicate rows are retained.
428− :: Query outer commons schemas params columns
387+ :: Query commons schemas params columns
429− -> Query outer commons schemas params columns
388+ -> Query commons schemas params columns
430− -> Query outer commons schemas params columns
389+ -> Query commons schemas params columns
431q1 `exceptAll` q2 = UnsafeQuery $
390q1 `exceptAll` q2 = UnsafeQuery $
432 parenthesized (renderSQL q1)
391 parenthesized (renderSQL q1)
433 <+> "EXCEPT" <+> "ALL"
392 <+> "EXCEPT" <+> "ALL"
@@ -437,90 +396,54 @@ q1 `exceptAll` q2 = UnsafeQuery $
437SELECT queries
396SELECT queries
438-----------------------------------------}
397-----------------------------------------}
440−{- | The simplest kinds of `Selection` are `Star` and `DotStar` which
399+data Selection commons schemas params grp from row where
441−emits all columns that a `TableExpression` produces. A select `List`
400+ List
442−is a list of `Expression`s. A `Selection` could be a list of
401+ :: SListI row
443−`WindowFunction`s `Over` `WindowDefinition`. `Additional` `Selection`s can
402+ => NP (Aliased (Expression commons schemas params grp from)) row
444−be selected with `Also`.
446−data Selection outer commons grp schemas params from row where
403+ -> Selection commons schemas params grp from row
448 :: HasUnique tab from row
405 :: HasUnique tab from row
449− => Selection outer commons 'Ungrouped schemas params from row
406+ => Selection commons schemas params 'Ungrouped from row
450− -- ^ `HasUnique` table in the `FromClause`
452 :: Has tab from row
408 :: Has tab from row
453 => Alias tab
409 => Alias tab
454− -- ^ `Has` table with `Alias`
410+ -> Selection commons schemas params 'Ungrouped from row
455− -> Selection outer commons 'Ungrouped schemas params from row
458− => NP (Aliased (Expression outer commons grp schemas params from)) row
459− -- ^ `NP` list of `Aliased` `Expression`s
460− -> Selection outer commons grp schemas params from row
463− => NP (Aliased (WindowFunction outer commons grp schemas params from)) row
464− -- ^ `NP` list of `Aliased` `WindowFunction`s
465− -> WindowDefinition outer commons grp schemas params from
466− -> Selection outer commons grp schemas params from row
468− :: Selection outer commons grp schemas params from right
412+ :: Selection commons schemas params grp from right
469− -- ^ `Additional` `Selection`
470− -> Selection outer commons grp schemas params from left
413+ -> Selection commons schemas params grp from left
471− -> Selection outer commons grp schemas params from (Join left right)
414+ -> Selection commons schemas params grp from (Join left right)
472−instance Additional (Selection outer commons grp schemas params from) where
474instance (KnownSymbol col, row ~ '[col ::: ty])
415instance (KnownSymbol col, row ~ '[col ::: ty])
475 => Aliasable col
416 => Aliasable col
476− (Expression outer commons grp schemas params from ty)
417+ (Expression commons schemas params grp from ty)
477− (Selection outer commons grp schemas params from row) where
418+ (Selection commons schemas params grp from row) where
478 expr `as` col = List (expr `as` col)
419 expr `as` col = List (expr `as` col)
479−instance (Has tab (Join outer from) row0, Has col row0 ty, row1 ~ '[col ::: ty])
420+instance (Has tab from row0, Has col row0 ty, row1 ~ '[col ::: ty])
480− => IsQualified tab col
481− (Selection outer commons 'Ungrouped schemas params from row1) where
421+ => IsQualified tab col (Selection commons schemas params 'Ungrouped from row1) where
482− tab ! col = tab ! col `as` col
422+ tab ! col = tab ! col `as` col
484− ( Has tab (Join outer from) row0
424+ ( Has tab from row0
485 , Has col row0 ty
425 , Has col row0 ty
486 , row1 ~ '[col ::: ty]
426 , row1 ~ '[col ::: ty]
487 , GroupedBy tab col bys )
427 , GroupedBy tab col bys )
488− => IsQualified tab col
489− (Selection outer commons ('Grouped bys) schemas params from row1) where
428+ => IsQualified tab col (Selection commons schemas params ('Grouped bys) from row1) where
490− tab ! col = tab ! col `as` col
429+ tab ! col = tab ! col `as` col
491−instance (HasUnique tab (Join outer from) row0, Has col row0 ty, row1 ~ '[col ::: ty])
430+instance (HasUnique tab from row0, Has col row0 ty, row1 ~ '[col ::: ty])
493− (Selection outer commons 'Ungrouped schemas params from row1) where
431+ => IsLabel col (Selection commons schemas params 'Ungrouped from row1) where
494− fromLabel = fromLabel @col `as` Alias
432+ fromLabel = fromLabel @col `as` Alias
496− ( HasUnique tab (Join outer from) row0
434+ ( HasUnique tab from row0
497 , Has col row0 ty
435 , Has col row0 ty
498 , row1 ~ '[col ::: ty]
436 , row1 ~ '[col ::: ty]
499 , GroupedBy tab col bys )
437 , GroupedBy tab col bys )
501− (Selection outer commons ('Grouped bys) schemas params from row1) where
438+ => IsLabel col (Selection commons schemas params ('Grouped bys) from row1) where
502− fromLabel = fromLabel @col `as` Alias
439+ fromLabel = fromLabel @col `as` Alias
504−instance RenderSQL (Selection outer commons grp schemas params from row) where
441+instance RenderSQL (Selection commons schemas params grp from row) where
505 renderSQL = \case
442 renderSQL = \case
506 List list -> renderCommaSeparated (renderAliased renderSQL) list
443 List list -> renderCommaSeparated (renderAliased renderSQL) list
507 Star -> "*"
444 Star -> "*"
508 DotStar tab -> renderSQL tab <> ".*"
445 DotStar tab -> renderSQL tab <> ".*"
509 Also right left -> renderSQL left <> ", " <> renderSQL right
446 Also right left -> renderSQL left <> ", " <> renderSQL right
510− Over winFns winDef ->
513− :: Aliased (WindowFunction outer commons grp schemas params from) field
515− renderOver (winFn `As` col) = renderSQL winFn
516− <+> "OVER" <+> parenthesized (renderSQL winDef)
517− <+> "AS" <+> renderSQL col
519− renderCommaSeparated renderOver winFns
522− (Selection outer commons grp schemas params from '["fromOnly" ::: 'NotNull 'PGtext]) where
523− fromString str = fromString str `as` Alias
525-- | the `TableExpression` in the `select` command constructs an intermediate
448-- | the `TableExpression` in the `select` command constructs an intermediate
526-- virtual table by possibly combining tables, views, eliminating rows,
449-- virtual table by possibly combining tables, views, eliminating rows,
@@ -529,100 +452,45 @@ instance IsString
529-- the intermediate table are actually output.
452-- the intermediate table are actually output.
531 :: (SListI row, row ~ (x ': xs))
454 :: (SListI row, row ~ (x ': xs))
532− => Selection outer commons grp schemas params from row
455+ => Selection commons schemas params grp from row
533− -- ^ selection
456+ -- ^ select list
534− -> TableExpression outer commons grp schemas params from
457+ -> TableExpression commons schemas params grp from
535 -- ^ intermediate virtual table
458 -- ^ intermediate virtual table
536− -> Query outer commons schemas params row
459+ -> Query commons schemas params row
537select selection tabexpr = UnsafeQuery $
460select selection tabexpr = UnsafeQuery $
539 <+> renderSQL selection
462 <+> renderSQL selection
540 <+> renderSQL tabexpr
463 <+> renderSQL tabexpr
542−-- | Like `select` but takes an `NP` list of `Expression`s instead
543−-- of a general `Selection`.
545 :: (SListI row, row ~ (x ': xs))
466 :: (SListI row, row ~ (x ': xs))
546− => NP (Aliased (Expression outer commons grp schemas params from)) row
467+ => NP (Aliased (Expression commons schemas params grp from)) row
548− -> TableExpression outer commons grp schemas params from
468+ -> TableExpression commons schemas params grp from
549− -- ^ intermediate virtual table
550− -> Query outer commons schemas params row
469+ -> Query commons schemas params row
551−select_ = select . List
470+select_ list = select (List list)
553-- | After the select list has been processed, the result table can
472-- | After the select list has been processed, the result table can
554-- be subject to the elimination of duplicate rows using `selectDistinct`.
473-- be subject to the elimination of duplicate rows using `selectDistinct`.
555selectDistinct
474selectDistinct
556 :: (SListI columns, columns ~ (col ': cols))
475 :: (SListI columns, columns ~ (col ': cols))
557− => Selection outer commons grp schemas params from columns
476+ => Selection commons schemas params 'Ungrouped from columns
558− -- ^ selection
477+ -- ^ select list
559− -> TableExpression outer commons grp schemas params from
478+ -> TableExpression commons schemas params 'Ungrouped from
560 -- ^ intermediate virtual table
479 -- ^ intermediate virtual table
561− -> Query outer commons schemas params columns
480+ -> Query commons schemas params columns
562selectDistinct selection tabexpr = UnsafeQuery $
481selectDistinct selection tabexpr = UnsafeQuery $
563 "SELECT DISTINCT"
482 "SELECT DISTINCT"
564 <+> renderSQL selection
483 <+> renderSQL selection
565 <+> renderSQL tabexpr
484 <+> renderSQL tabexpr
567−-- | Like `selectDistinct` but takes an `NP` list of `Expression`s instead
568−-- of a general `Selection`.
569selectDistinct_
486selectDistinct_
570 :: (SListI columns, columns ~ (col ': cols))
487 :: (SListI columns, columns ~ (col ': cols))
571− => NP (Aliased (Expression outer commons grp schemas params from)) columns
488+ => NP (Aliased (Expression commons schemas params 'Ungrouped from)) columns
572 -- ^ select list
489 -- ^ select list
573− -> TableExpression outer commons grp schemas params from
574− -- ^ intermediate virtual table
575− -> Query outer commons schemas params columns
490+ -> TableExpression commons schemas params 'Ungrouped from
576−selectDistinct_ = selectDistinct . List
579−`selectDistinctOn` keeps only the first row of each set of rows where
580−the given expressions evaluate to equal. The DISTINCT ON expressions are
581−interpreted using the same rules as for ORDER BY. ORDER BY is used to
582−ensure that the desired row appears first.
584−The DISTINCT ON expression(s) must match the leftmost ORDER BY expression(s).
585−The ORDER BY clause will normally contain additional expression(s) that
586−determine the desired precedence of rows within each DISTINCT ON group.
588−In order to guarantee they match and reduce redundancy, this function
589−will prepend the The DISTINCT ON expressions to the ORDER BY clause.
592− :: (SListI columns, columns ~ (col ': cols))
593− => [SortExpression outer commons grp schemas params from]
594− -- ^ DISTINCT ON expression(s) and prepended to ORDER BY clause
595− -> Selection outer commons grp schemas params from columns
597− -> TableExpression outer commons grp schemas params from
598 -- ^ intermediate virtual table
491 -- ^ intermediate virtual table
599− -> Query outer commons schemas params columns
492+ -> Query commons schemas params columns
600−selectDistinctOn distincts selection tab = UnsafeQuery $
493+selectDistinct_ list = select (List list)
601− "SELECT DISTINCT ON"
602− <+> parenthesized (commaSeparated (renderDistinctOn <$> distincts))
603− <+> renderSQL selection
604− <+> renderSQL (tab {orderByClause = distincts <> orderByClause tab})
606− renderDistinctOn = \case
607− Asc expression -> renderSQL expression
608− Desc expression -> renderSQL expression
609− AscNullsFirst expression -> renderSQL expression
610− DescNullsFirst expression -> renderSQL expression
611− AscNullsLast expression -> renderSQL expression
612− DescNullsLast expression -> renderSQL expression
614−-- | Like `selectDistinctOn` but takes an `NP` list of `Expression`s instead
615−-- of a general `Selection`.
617− :: (SListI columns, columns ~ (col ': cols))
618− => [SortExpression outer commons grp schemas params from]
619− -- ^ distinct on and return the first row in ordering
620− -> NP (Aliased (Expression outer commons grp schemas params from)) columns
622− -> TableExpression outer commons grp schemas params from
623− -- ^ intermediate virtual table
624− -> Query outer commons schemas params columns
625−selectDistinctOn_ distincts = selectDistinctOn distincts . List
627-- | `values` computes a row value or set of row values
495-- | `values` computes a row value or set of row values
628-- specified by value expressions. It is most commonly used
496-- specified by value expressions. It is most commonly used
@@ -630,16 +498,16 @@ selectDistinctOn_ distincts = selectDistinctOn distincts . List
630-- but it can be used on its own.
498-- but it can be used on its own.
632-- >>> type Row = '["a" ::: 'NotNull 'PGint4, "b" ::: 'NotNull 'PGtext]
500-- >>> type Row = '["a" ::: 'NotNull 'PGint4, "b" ::: 'NotNull 'PGtext]
633−-- >>> let query = values (1 `as` #a :* "one" `as` #b) [] :: Query outer commons schemas '[] Row
501+-- >>> let query = values (1 `as` #a :* "one" `as` #b) [] :: Query commons schemas '[] Row
634-- >>> printSQL query
502-- >>> printSQL query
635-- SELECT * FROM (VALUES (1, E'one')) AS t ("a", "b")
503-- SELECT * FROM (VALUES (1, E'one')) AS t ("a", "b")
637 :: SListI cols
505 :: SListI cols
638− => NP (Aliased (Expression outer commons 'Ungrouped schemas params '[] )) cols
506+ => NP (Aliased (Expression commons schemas params 'Ungrouped '[] )) cols
639− -> [NP (Aliased (Expression outer commons 'Ungrouped schemas params '[] )) cols]
507+ -> [NP (Aliased (Expression commons schemas params 'Ungrouped '[] )) cols]
640 -- ^ When more than one row is specified, all the rows must
508 -- ^ When more than one row is specified, all the rows must
641 -- must have the same number of elements
509 -- must have the same number of elements
642− -> Query outer commons schemas params cols
510+ -> Query commons schemas params cols
643values rw rws = UnsafeQuery $ "SELECT * FROM"
511values rw rws = UnsafeQuery $ "SELECT * FROM"
644 <+> parenthesized (
512 <+> parenthesized (
@@ -650,7 +518,7 @@ values rw rws = UnsafeQuery $ "SELECT * FROM"
650 <+> parenthesized (renderCommaSeparated renderAliasPart rw)
518 <+> parenthesized (renderCommaSeparated renderAliasPart rw)
652 renderAliasPart, renderValuePart
520 renderAliasPart, renderValuePart
653− :: Aliased (Expression outer commons 'Ungrouped schemas params '[] ) ty -> ByteString
521+ :: Aliased (Expression commons schemas params 'Ungrouped '[] ) ty -> ByteString
654 renderAliasPart (_ `As` name) = renderSQL name
522 renderAliasPart (_ `As` name) = renderSQL name
655 renderValuePart (value `As` _) = renderSQL value
523 renderValuePart (value `As` _) = renderSQL value
@@ -658,9 +526,9 @@ values rw rws = UnsafeQuery $ "SELECT * FROM"
658-- specified by value expressions.
526-- specified by value expressions.
660 :: SListI cols
528 :: SListI cols
661− => NP (Aliased (Expression outer commons 'Ungrouped schemas params '[] )) cols
529+ => NP (Aliased (Expression commons schemas params 'Ungrouped '[] )) cols
662 -- ^ one row of values
530 -- ^ one row of values
663− -> Query outer commons schemas params cols
531+ -> Query commons schemas params cols
664values_ rw = values rw []
532values_ rw = values rw []
666{-----------------------------------------
534{-----------------------------------------
@@ -674,17 +542,16 @@ Table Expressions
674-- to a table on disk, a so-called base table, but more complex expressions
542-- to a table on disk, a so-called base table, but more complex expressions
675-- can be used to modify or combine base tables in various ways.
543-- can be used to modify or combine base tables in various ways.
676data TableExpression
544data TableExpression
678 (commons :: FromType)
545 (commons :: FromType)
680 (schemas :: SchemasType)
546 (schemas :: SchemasType)
681 (params :: [NullityType])
547 (params :: [NullityType])
682 (from :: FromType)
549 (from :: FromType)
683 = TableExpression
550 = TableExpression
684− { fromClause :: FromClause outer commons schemas params from
551+ { fromClause :: FromClause commons schemas params from
685 -- ^ A table reference that can be a table name, or a derived table such
552 -- ^ A table reference that can be a table name, or a derived table such
686 -- as a subquery, a @JOIN@ construct, or complex combinations of these.
553 -- as a subquery, a @JOIN@ construct, or complex combinations of these.
687− , whereClause :: [Condition outer commons 'Ungrouped schemas params from]
554+ , whereClause :: [Condition commons schemas params 'Ungrouped from]
688 -- ^ optional search coditions, combined with `.&&`. After the processing
555 -- ^ optional search coditions, combined with `.&&`. After the processing
689 -- of the `fromClause` is done, each row of the derived virtual table
556 -- of the `fromClause` is done, each row of the derived virtual table
690 -- is checked against the search condition. If the result of the
557 -- is checked against the search condition. If the result of the
@@ -700,13 +567,13 @@ data TableExpression
700 -- set of rows having common values into one group row that represents all
567 -- set of rows having common values into one group row that represents all
701 -- rows in the group. This is done to eliminate redundancy in the output
568 -- rows in the group. This is done to eliminate redundancy in the output
702 -- and/or compute aggregates that apply to these groups.
569 -- and/or compute aggregates that apply to these groups.
703− , havingClause :: HavingClause outer commons grp schemas params from
570+ , havingClause :: HavingClause commons schemas params grp from
704 -- ^ If a table has been grouped using `groupBy`, but only certain groups
571 -- ^ If a table has been grouped using `groupBy`, but only certain groups
705 -- are of interest, the `havingClause` can be used, much like a
572 -- are of interest, the `havingClause` can be used, much like a
706 -- `whereClause`, to eliminate groups from the result. Expressions in the
573 -- `whereClause`, to eliminate groups from the result. Expressions in the
707 -- `havingClause` can refer both to grouped expressions and to ungrouped
574 -- `havingClause` can refer both to grouped expressions and to ungrouped
708 -- expressions (which necessarily involve an aggregate function).
575 -- expressions (which necessarily involve an aggregate function).
709− , orderByClause :: [SortExpression outer commons grp schemas params from]
576+ , orderByClause :: [SortExpression commons schemas params grp from]
710 -- ^ The `orderByClause` is for optional sorting. When more than one
577 -- ^ The `orderByClause` is for optional sorting. When more than one
711 -- `SortExpression` is specified, the later (right) values are used to sort
578 -- `SortExpression` is specified, the later (right) values are used to sort
712 -- rows that are equal according to the earlier (left) values.
579 -- rows that are equal according to the earlier (left) values.
@@ -716,14 +583,14 @@ data TableExpression
716 -- will be returned (but possibly fewer, if the query itself yields
583 -- will be returned (but possibly fewer, if the query itself yields
717 -- fewer rows).
584 -- fewer rows).
718 , offsetClause :: [Word64]
585 , offsetClause :: [Word64]
719− -- ^ The `offsetClause` is combined with `Prelude.+` to give an offset count
586+ -- ^ The `offsetClause` is combined with `+` to give an offset count
720 -- if nonempty. The offset count says to skip that many rows before
587 -- if nonempty. The offset count says to skip that many rows before
721 -- beginning to return rows. The rows are skipped before the limit count
588 -- beginning to return rows. The rows are skipped before the limit count
722 -- is applied.
589 -- is applied.
723− } deriving (GHC.Generic)
590+ }
725-- | Render a `TableExpression`
592-- | Render a `TableExpression`
726−instance RenderSQL (TableExpression outer commons grp schemas params from) where
593+instance RenderSQL (TableExpression commons schemas params grp from) where
727 renderSQL
594 renderSQL
728 (TableExpression frm' whs' grps' hvs' srts' lims' offs') = mconcat
595 (TableExpression frm' whs' grps' hvs' srts' lims' offs') = mconcat
729 [ "FROM ", renderSQL frm'
596 [ "FROM ", renderSQL frm'
@@ -732,7 +599,8 @@ instance RenderSQL (TableExpression outer commons grp schemas params from) where
732 , renderSQL hvs'
599 , renderSQL hvs'
733 , renderOrderByClause srts'
600 , renderOrderByClause srts'
734 , renderLimits lims'
601 , renderLimits lims'
735− , renderOffsets offs' ]
602+ , renderOffsets offs'
737 renderWheres = \case
605 renderWheres = \case
@@ -752,33 +620,33 @@ instance RenderSQL (TableExpression outer commons grp schemas params from) where
752-- | A `from` generates a `TableExpression` from a table reference that can be
620-- | A `from` generates a `TableExpression` from a table reference that can be
753-- a table name, or a derived table such as a subquery, a JOIN construct,
621-- a table name, or a derived table such as a subquery, a JOIN construct,
754-- or complex combinations of these. A `from` may be transformed by `where_`,
622-- or complex combinations of these. A `from` may be transformed by `where_`,
755−-- `groupBy`, `having`, `orderBy`, `limit` and `offset`, using the `&` operator
623+-- `group`, `having`, `orderBy`, `limit` and `offset`, using the `&` operator
756-- to match the left-to-right sequencing of their placement in SQL.
624-- to match the left-to-right sequencing of their placement in SQL.
758− :: FromClause outer commons schemas params from -- ^ table reference
626+ :: FromClause commons schemas params from -- ^ table reference
759− -> TableExpression outer commons 'Ungrouped schemas params from
627+ -> TableExpression commons schemas params 'Ungrouped from
760−from tab = TableExpression tab [] noGroups NoHaving [] [] []
628+from rels = TableExpression rels [] NoGroups NoHaving [] [] []
762-- | A `where_` is an endomorphism of `TableExpression`s which adds a
630-- | A `where_` is an endomorphism of `TableExpression`s which adds a
763-- search condition to the `whereClause`.
631-- search condition to the `whereClause`.
765− :: Condition outer commons 'Ungrouped schemas params from -- ^ filtering condition
633+ :: Condition commons schemas params 'Ungrouped from -- ^ filtering condition
766− -> TableExpression outer commons grp schemas params from
634+ -> TableExpression commons schemas params grp from
767− -> TableExpression outer commons grp schemas params from
635+ -> TableExpression commons schemas params grp from
768where_ wh rels = rels {whereClause = wh : whereClause rels}
636where_ wh rels = rels {whereClause = wh : whereClause rels}
770-- | A `groupBy` is a transformation of `TableExpression`s which switches
638-- | A `groupBy` is a transformation of `TableExpression`s which switches
771−-- its `Grouping` from `Ungrouped` to `Grouped`. Use @groupBy Nil@ to perform
639+-- its `Grouping` from `Ungrouped` to `Grouped`. Use @group Nil@ to perform
772-- a "grand total" aggregation query.
640-- a "grand total" aggregation query.
774 :: SListI bys
642 :: SListI bys
775 => NP (By from) bys -- ^ grouped columns
643 => NP (By from) bys -- ^ grouped columns
776− -> TableExpression outer commons 'Ungrouped schemas params from
644+ -> TableExpression commons schemas params 'Ungrouped from
777− -> TableExpression outer commons ('Grouped bys) schemas params from
645+ -> TableExpression commons schemas params ('Grouped bys) from
778groupBy bys rels = TableExpression
646groupBy bys rels = TableExpression
779 { fromClause = fromClause rels
647 { fromClause = fromClause rels
780 , whereClause = whereClause rels
648 , whereClause = whereClause rels
781− , groupByClause = group bys
649+ , groupByClause = Group bys
782 , havingClause = Having []
650 , havingClause = Having []
783 , orderByClause = []
651 , orderByClause = []
784 , limitClause = limitClause rels
652 , limitClause = limitClause rels
@@ -788,31 +656,177 @@ groupBy bys rels = TableExpression
788-- | A `having` is an endomorphism of `TableExpression`s which adds a
656-- | A `having` is an endomorphism of `TableExpression`s which adds a
789-- search condition to the `havingClause`.
657-- search condition to the `havingClause`.
791− :: Condition outer commons ('Grouped bys) schemas params from -- ^ having condition
659+ :: Condition commons schemas params ('Grouped bys) from -- ^ having condition
792− -> TableExpression outer commons ('Grouped bys) schemas params from
660+ -> TableExpression commons schemas params ('Grouped bys) from
793− -> TableExpression outer commons ('Grouped bys) schemas params from
661+ -> TableExpression commons schemas params ('Grouped bys) from
794having hv rels = rels
662having hv rels = rels
795 { havingClause = case havingClause rels of Having hvs -> Having (hv:hvs) }
663 { havingClause = case havingClause rels of Having hvs -> Having (hv:hvs) }
797−instance OrderBy TableExpression where
665+-- | An `orderBy` is an endomorphism of `TableExpression`s which appends an
666+-- ordering to the right of the `orderByClause`.
668+ :: [SortExpression commons schemas params grp from] -- ^ sort expressions
669+ -> TableExpression commons schemas params grp from
670+ -> TableExpression commons schemas params grp from
798− orderBy srts rels = rels {orderByClause = orderByClause rels ++ srts}
671+orderBy srts rels = rels {orderByClause = orderByClause rels ++ srts}
800-- | A `limit` is an endomorphism of `TableExpression`s which adds to the
673-- | A `limit` is an endomorphism of `TableExpression`s which adds to the
801-- `limitClause`.
674-- `limitClause`.
803 :: Word64 -- ^ limit parameter
676 :: Word64 -- ^ limit parameter
804− -> TableExpression outer commons grp schemas params from
677+ -> TableExpression commons schemas params grp from
805− -> TableExpression outer commons grp schemas params from
678+ -> TableExpression commons schemas params grp from
806limit lim rels = rels {limitClause = lim : limitClause rels}
679limit lim rels = rels {limitClause = lim : limitClause rels}
808-- | An `offset` is an endomorphism of `TableExpression`s which adds to the
681-- | An `offset` is an endomorphism of `TableExpression`s which adds to the
809-- `offsetClause`.
682-- `offsetClause`.
811 :: Word64 -- ^ offset parameter
684 :: Word64 -- ^ offset parameter
812− -> TableExpression outer commons grp schemas params from
685+ -> TableExpression commons schemas params grp from
813− -> TableExpression outer commons grp schemas params from
686+ -> TableExpression commons schemas params grp from
814offset off rels = rels {offsetClause = off : offsetClause rels}
687offset off rels = rels {offsetClause = off : offsetClause rels}
689+{-----------------------------------------
691+-----------------------------------------}
695+ -> Expression commons schemas params 'Ungrouped '[] ty
696+ -> Query commons schemas params row
697+unsafeSetOfFunction fun expr = UnsafeQuery $
698+ "SELECT * FROM " <> fun <> "(" <> renderSQL expr <> ")"
700+-- | Expands the outermost JSON object into a set of key/value pairs.
702+ :: Expression commons schemas params 'Ungrouped '[] (nullity 'PGjson) -- ^ json object
703+ -> Query commons schemas params
704+ '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]
705+jsonEach = unsafeSetOfFunction "json_each"
707+-- | Expands the outermost binary JSON object into a set of key/value pairs.
709+ :: Expression commons schemas params 'Ungrouped '[] (nullity 'PGjsonb) -- ^ jsonb object
710+ -> Query commons schemas params
711+ '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjsonb]
712+jsonbEach = unsafeSetOfFunction "jsonb_each"
714+-- | Expands the outermost JSON object into a set of key/value pairs.
716+ :: Expression commons schemas params 'Ungrouped '[] (nullity 'PGjson) -- ^ json object
717+ -> Query commons schemas params
718+ '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]
719+jsonEachAsText = unsafeSetOfFunction "json_each_text"
721+-- | Expands the outermost binary JSON object into a set of key/value pairs.
723+ :: Expression commons schemas params 'Ungrouped '[] (nullity 'PGjsonb) -- ^ jsonb object
724+ -> Query commons schemas params
725+ '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]
726+jsonbEachAsText = unsafeSetOfFunction "jsonb_each_text"
728+-- | Returns set of keys in the outermost JSON object.
730+ :: Expression commons schemas params 'Ungrouped '[] (nullity 'PGjson) -- ^ json object
731+ -> Query commons schemas params '["json_object_keys" ::: 'NotNull 'PGtext]
732+jsonObjectKeys = unsafeSetOfFunction "json_object_keys"
734+-- | Returns set of keys in the outermost JSON object.
736+ :: Expression commons schemas params 'Ungrouped '[] (nullity 'PGjsonb) -- ^ jsonb object
737+ -> Query commons schemas params '["jsonb_object_keys" ::: 'NotNull 'PGtext]
738+jsonbObjectKeys = unsafeSetOfFunction "jsonb_object_keys"
740+unsafePopulateFunction
742+ -> TypeExpression schemas (nullity ('PGcomposite row))
743+ -> Expression commons schemas params 'Ungrouped '[] ty
744+ -> Query commons schemas params row
745+unsafePopulateFunction fun ty expr = UnsafeQuery $
746+ "SELECT * FROM " <> fun <> "("
747+ <> "null::" <> renderSQL ty <> ", "
748+ <> renderSQL expr <> ")"
750+-- | Expands the JSON expression to a row whose columns match the record
751+-- type defined by the given table.
753+ :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
754+ -> Expression commons schemas params 'Ungrouped '[] (nullity 'PGjson) -- ^ json object
755+ -> Query commons schemas params row
756+jsonPopulateRecord = unsafePopulateFunction "json_populate_record"
758+-- | Expands the binary JSON expression to a row whose columns match the record
759+-- type defined by the given table.
761+ :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
762+ -> Expression commons schemas params 'Ungrouped '[] (nullity 'PGjsonb) -- ^ jsonb object
763+ -> Query commons schemas params row
764+jsonbPopulateRecord = unsafePopulateFunction "jsonb_populate_record"
766+-- | Expands the outermost array of objects in the given JSON expression to a
767+-- set of rows whose columns match the record type defined by the given table.
768+jsonPopulateRecordSet
769+ :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
770+ -> Expression commons schemas params 'Ungrouped '[] (nullity 'PGjson) -- ^ json array
771+ -> Query commons schemas params row
772+jsonPopulateRecordSet = unsafePopulateFunction "json_populate_record_set"
774+-- | Expands the outermost array of objects in the given binary JSON expression
775+-- to a set of rows whose columns match the record type defined by the given
777+jsonbPopulateRecordSet
778+ :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
779+ -> Expression commons schemas params 'Ungrouped '[] (nullity 'PGjsonb) -- ^ jsonb array
780+ -> Query commons schemas params row
781+jsonbPopulateRecordSet = unsafePopulateFunction "jsonb_populate_record_set"
784+ :: (SListI record, json `In` PGJsonType)
786+ -> Expression commons schemas params 'Ungrouped '[] (nullity json)
787+ -> NP (Aliased (TypeExpression schemas)) record
788+ -> Query commons schemas params record
789+unsafeRecordFunction fun expr types = UnsafeQuery $
790+ "SELECT * FROM " <> fun <> "("
791+ <> renderSQL expr <> ")"
792+ <+> "AS" <+> "x" <> parenthesized (renderCommaSeparated renderTy types)
794+ renderTy :: Aliased (TypeExpression schemas) ty -> ByteString
795+ renderTy (ty `As` alias) =
796+ renderSQL alias <+> renderSQL ty
798+-- | Builds an arbitrary record from a JSON object.
801+ => Expression commons schemas params 'Ungrouped '[] (nullity 'PGjson) -- ^ json object
802+ -> NP (Aliased (TypeExpression schemas)) record -- ^ record types
803+ -> Query commons schemas params record
804+jsonToRecord = unsafeRecordFunction "json_to_record"
806+-- | Builds an arbitrary record from a binary JSON object.
809+ => Expression commons schemas params 'Ungrouped '[] (nullity 'PGjsonb) -- ^ jsonb object
810+ -> NP (Aliased (TypeExpression schemas)) record -- ^ record types
811+ -> Query commons schemas params record
812+jsonbToRecord = unsafeRecordFunction "jsonb_to_record"
814+-- | Builds an arbitrary set of records from a JSON array of objects.
817+ => Expression commons schemas params 'Ungrouped '[] (nullity 'PGjson) -- ^ json array
818+ -> NP (Aliased (TypeExpression schemas)) record -- ^ record types
819+ -> Query commons schemas params record
820+jsonToRecordSet = unsafeRecordFunction "json_to_record_set"
822+-- | Builds an arbitrary set of records from a binary JSON array of objects.
825+ => Expression commons schemas params 'Ungrouped '[] (nullity 'PGjsonb) -- ^ jsonb array
826+ -> NP (Aliased (TypeExpression schemas)) record -- ^ record types
827+ -> Query commons schemas params record
828+jsonbToRecordSet = unsafeRecordFunction "jsonb_to_record_set"
816{-----------------------------------------
830{-----------------------------------------
817FROM clauses
831FROM clauses
818-----------------------------------------}
832-----------------------------------------}
@@ -821,59 +835,53 @@ FROM clauses
821A `FromClause` can be a table name, or a derived table such
835A `FromClause` can be a table name, or a derived table such
822as a subquery, a @JOIN@ construct, or complex combinations of these.
836as a subquery, a @JOIN@ construct, or complex combinations of these.
824−newtype FromClause outer commons schemas params from
838+newtype FromClause commons schemas params from
825 = UnsafeFromClause { renderFromClause :: ByteString }
839 = UnsafeFromClause { renderFromClause :: ByteString }
826 deriving (GHC.Generic,Show,Eq,Ord,NFData)
840 deriving (GHC.Generic,Show,Eq,Ord,NFData)
827−instance RenderSQL (FromClause outer commons schemas params from) where
841+instance RenderSQL (FromClause commons schemas params from) where
828 renderSQL = renderFromClause
842 renderSQL = renderFromClause
830-- | A real `table` is a table from the database.
844-- | A real `table` is a table from the database.
832 :: (Has sch schemas schema, Has tab schema ('Table table))
846 :: (Has sch schemas schema, Has tab schema ('Table table))
833 => Aliased (QualifiedAlias sch) (alias ::: tab)
847 => Aliased (QualifiedAlias sch) (alias ::: tab)
834− -> FromClause outer commons schemas params '[alias ::: TableToRow table]
848+ -> FromClause commons schemas params '[alias ::: TableToRow table]
835table (tab `As` alias) = UnsafeFromClause $
849table (tab `As` alias) = UnsafeFromClause $
836 renderSQL tab <+> "AS" <+> renderSQL alias
850 renderSQL tab <+> "AS" <+> renderSQL alias
838-- | `subquery` derives a table from a `Query`.
852-- | `subquery` derives a table from a `Query`.
840− :: Aliased (Query outer commons schemas params) query
854+ :: Aliased (Query commons schemas params) query
841− -> FromClause outer commons schemas params '[query]
855+ -> FromClause commons schemas params '[query]
842−subquery = UnsafeFromClause . renderAliased (parenthesized . renderSQL)
856+subquery = UnsafeFromClause . renderAliased (parenthesized . renderQuery)
844-- | `view` derives a table from a `View`.
858-- | `view` derives a table from a `View`.
846 :: (Has sch schemas schema, Has vw schema ('View view))
860 :: (Has sch schemas schema, Has vw schema ('View view))
847 => Aliased (QualifiedAlias sch) (alias ::: vw)
861 => Aliased (QualifiedAlias sch) (alias ::: vw)
848− -> FromClause outer commons schemas params '[alias ::: view]
862+ -> FromClause commons schemas params '[alias ::: view]
849view (vw `As` alias) = UnsafeFromClause $
863view (vw `As` alias) = UnsafeFromClause $
850 renderSQL vw <+> "AS" <+> renderSQL alias
864 renderSQL vw <+> "AS" <+> renderSQL alias
852−-- | `common` derives a table from a common table expression.
854 :: Has cte commons common
867 :: Has cte commons common
855 => Aliased Alias (alias ::: cte)
868 => Aliased Alias (alias ::: cte)
856− -> FromClause outer commons schemas params '[alias ::: common]
869+ -> FromClause commons schemas params '[alias ::: common]
857common (cte `As` alias) = UnsafeFromClause $
870common (cte `As` alias) = UnsafeFromClause $
858 renderSQL cte <+> "AS" <+> renderSQL alias
871 renderSQL cte <+> "AS" <+> renderSQL alias
860−instance Additional (FromClause outer commons schemas params) where
861− also right left = UnsafeFromClause $
862− renderSQL left <> ", " <> renderSQL right
865−@left & crossJoin right@. For every possible combination of rows from
873+{- | @left & crossJoin right@. For every possible combination of rows from
866−@left@ and @right@ (i.e., a Cartesian product), the joined table will contain
874+ @left@ and @right@ (i.e., a Cartesian product), the joined table will contain
867−a row consisting of all columns in @left@ followed by all columns in @right@.
875+ a row consisting of all columns in @left@ followed by all columns in @right@.
868−If the tables have @n@ and @m@ rows respectively, the joined table will
876+ If the tables have @n@ and @m@ rows respectively, the joined table will
869−have @n * m@ rows.
877+ have @n * m@ rows.
872− :: FromClause outer commons schemas params right
880+ :: FromClause commons schemas params right
873 -- ^ right
881 -- ^ right
874− -> FromClause outer commons schemas params left
882+ -> FromClause commons schemas params left
875 -- ^ left
883 -- ^ left
876− -> FromClause outer commons schemas params (Join left right)
884+ -> FromClause commons schemas params (Join left right)
877crossJoin right left = UnsafeFromClause $
885crossJoin right left = UnsafeFromClause $
878 renderSQL left <+> "CROSS JOIN" <+> renderSQL right
886 renderSQL left <+> "CROSS JOIN" <+> renderSQL right
@@ -881,13 +889,13 @@ crossJoin right left = UnsafeFromClause $
881the @on@ condition.
889the @on@ condition.
884− :: FromClause outer commons schemas params right
892+ :: FromClause commons schemas params right
885 -- ^ right
893 -- ^ right
886− -> Condition outer commons 'Ungrouped schemas params (Join left right)
894+ -> Condition commons schemas params 'Ungrouped (Join left right)
887 -- ^ @on@ condition
895 -- ^ @on@ condition
888− -> FromClause outer commons schemas params left
896+ -> FromClause commons schemas params left
889 -- ^ left
897 -- ^ left
890− -> FromClause outer commons schemas params (Join left right)
898+ -> FromClause commons schemas params (Join left right)
891innerJoin right on left = UnsafeFromClause $
899innerJoin right on left = UnsafeFromClause $
892 renderSQL left <+> "INNER JOIN" <+> renderSQL right
900 renderSQL left <+> "INNER JOIN" <+> renderSQL right
893 <+> "ON" <+> renderSQL on
901 <+> "ON" <+> renderSQL on
@@ -898,13 +906,13 @@ innerJoin right on left = UnsafeFromClause $
898 Thus, the joined table always has at least one row for each row in @left@.
906 Thus, the joined table always has at least one row for each row in @left@.
900leftOuterJoin
908leftOuterJoin
901− :: FromClause outer commons schemas params right
909+ :: FromClause commons schemas params right
902 -- ^ right
910 -- ^ right
903− -> Condition outer commons 'Ungrouped schemas params (Join left right)
911+ -> Condition commons schemas params 'Ungrouped (Join left right)
904 -- ^ @on@ condition
912 -- ^ @on@ condition
905− -> FromClause outer commons schemas params left
913+ -> FromClause commons schemas params left
906 -- ^ left
914 -- ^ left
907− -> FromClause outer commons schemas params (Join left (NullifyFrom right))
915+ -> FromClause commons schemas params (Join left (NullifyFrom right))
908leftOuterJoin right on left = UnsafeFromClause $
916leftOuterJoin right on left = UnsafeFromClause $
909 renderSQL left <+> "LEFT OUTER JOIN" <+> renderSQL right
917 renderSQL left <+> "LEFT OUTER JOIN" <+> renderSQL right
910 <+> "ON" <+> renderSQL on
918 <+> "ON" <+> renderSQL on
@@ -916,13 +924,13 @@ leftOuterJoin right on left = UnsafeFromClause $
916 have a row for each row in @right@.
924 have a row for each row in @right@.
918rightOuterJoin
926rightOuterJoin
919− :: FromClause outer commons schemas params right
927+ :: FromClause commons schemas params right
920 -- ^ right
928 -- ^ right
921− -> Condition outer commons 'Ungrouped schemas params (Join left right)
929+ -> Condition commons schemas params 'Ungrouped (Join left right)
922 -- ^ @on@ condition
930 -- ^ @on@ condition
923− -> FromClause outer commons schemas params left
931+ -> FromClause commons schemas params left
924 -- ^ left
932 -- ^ left
925− -> FromClause outer commons schemas params (Join (NullifyFrom left) right)
933+ -> FromClause commons schemas params (Join (NullifyFrom left) right)
926rightOuterJoin right on left = UnsafeFromClause $
934rightOuterJoin right on left = UnsafeFromClause $
927 renderSQL left <+> "RIGHT OUTER JOIN" <+> renderSQL right
935 renderSQL left <+> "RIGHT OUTER JOIN" <+> renderSQL right
928 <+> "ON" <+> renderSQL on
936 <+> "ON" <+> renderSQL on
@@ -935,13 +943,13 @@ rightOuterJoin right on left = UnsafeFromClause $
935 is added.
943 is added.
937fullOuterJoin
945fullOuterJoin
938− :: FromClause outer commons schemas params right
946+ :: FromClause commons schemas params right
939 -- ^ right
947 -- ^ right
940− -> Condition outer commons 'Ungrouped schemas params (Join left right)
948+ -> Condition commons schemas params 'Ungrouped (Join left right)
941 -- ^ @on@ condition
949 -- ^ @on@ condition
942− -> FromClause outer commons schemas params left
950+ -> FromClause commons schemas params left
943 -- ^ left
951 -- ^ left
944− -> FromClause outer commons schemas params
952+ -> FromClause commons schemas params
945 (Join (NullifyFrom left) (NullifyFrom right))
953 (Join (NullifyFrom left) (NullifyFrom right))
946fullOuterJoin right on left = UnsafeFromClause $
954fullOuterJoin right on left = UnsafeFromClause $
947 renderSQL left <+> "FULL OUTER JOIN" <+> renderSQL right
955 renderSQL left <+> "FULL OUTER JOIN" <+> renderSQL right
@@ -951,7 +959,7 @@ fullOuterJoin right on left = UnsafeFromClause $
952-----------------------------------------}
960-----------------------------------------}
954−-- | `By`s are used in `groupBy` to reference a list of columns which are then
962+-- | `By`s are used in `group` to reference a list of columns which are then
955-- used to group together those rows in a table that have the same values
963-- used to group together those rows in a table that have the same values
956-- in all the columns listed. @By \#col@ will reference an unambiguous
964-- in all the columns listed. @By \#col@ will reference an unambiguous
957-- column @col@; otherwise @By2 (\#tab \! \#col)@ will reference a table
965-- column @col@; otherwise @By2 (\#tab \! \#col)@ will reference a table
@@ -992,42 +1000,354 @@ instance (Has rel rels cols, Has col cols ty, bys ~ '[ '(rel, col)])
992-- done on @NoGroups@ while all output `Expression`s must be aggregated
1000-- done on @NoGroups@ while all output `Expression`s must be aggregated
993-- in @Group Nil@. In general, all output `Expression`s in the
1001-- in @Group Nil@. In general, all output `Expression`s in the
994-- complement of @bys@ must be aggregated in @Group bys@.
1002-- complement of @bys@ must be aggregated in @Group bys@.
995−newtype GroupByClause grp from = UnsafeGroupByClause
1003+data GroupByClause grp from where
996− { renderGroupByClause :: ByteString }
1004+ NoGroups :: GroupByClause 'Ungrouped from
997− deriving (GHC.Generic,Show,Eq,Ord,NFData)
1005+ Group
1007+ => NP (By from) bys
1008+ -> GroupByClause ('Grouped bys) from
1010+-- | Renders a `GroupByClause`.
998instance RenderSQL (GroupByClause grp from) where
1011instance RenderSQL (GroupByClause grp from) where
999− renderSQL = renderGroupByClause
1012+ renderSQL = \case
1000−noGroups :: GroupByClause 'Ungrouped from
1013+ NoGroups -> ""
1001−noGroups = UnsafeGroupByClause ""
1004− => NP (By from) bys
1005− -> GroupByClause ('Grouped bys) from
1006−group bys = UnsafeGroupByClause $ case bys of
1007− Nil -> ""
1014+ Group Nil -> ""
1008− bys' -> " GROUP BY" <+> renderCommaSeparated renderSQL bys'
1015+ Group bys -> " GROUP BY" <+> renderCommaSeparated renderSQL bys
1010-- | A `HavingClause` is used to eliminate groups that are not of interest.
1017-- | A `HavingClause` is used to eliminate groups that are not of interest.
1011-- An `Ungrouped` `TableExpression` may only use `NoHaving` while a `Grouped`
1018-- An `Ungrouped` `TableExpression` may only use `NoHaving` while a `Grouped`
1012-- `TableExpression` must use `Having` whose conditions are combined with
1019-- `TableExpression` must use `Having` whose conditions are combined with
1013-- `.&&`.
1020-- `.&&`.
1014−data HavingClause outer commons grp schemas params from where
1021+data HavingClause commons schemas params grp from where
1015− NoHaving :: HavingClause outer commons 'Ungrouped schemas params from
1022+ NoHaving :: HavingClause commons schemas params 'Ungrouped from
1017− :: [Condition outer commons ('Grouped bys) schemas params from]
1024+ :: [Condition commons schemas params ('Grouped bys) from]
1018− -> HavingClause outer commons ('Grouped bys) schemas params from
1025+ -> HavingClause commons schemas params ('Grouped bys) from
1019−deriving instance Show (HavingClause outer commons grp schemas params from)
1026+deriving instance Show (HavingClause commons schemas params grp from)
1020−deriving instance Eq (HavingClause outer commons grp schemas params from)
1027+deriving instance Eq (HavingClause commons schemas params grp from)
1021−deriving instance Ord (HavingClause outer commons grp schemas params from)
1028+deriving instance Ord (HavingClause commons schemas params grp from)
1023-- | Render a `HavingClause`.
1030-- | Render a `HavingClause`.
1024−instance RenderSQL (HavingClause outer commons grp schemas params from) where
1031+instance RenderSQL (HavingClause commons schemas params grp from) where
1025 renderSQL = \case
1032 renderSQL = \case
1026 NoHaving -> ""
1033 NoHaving -> ""
1027 Having [] -> ""
1034 Having [] -> ""
1028 Having conditions ->
1035 Having conditions ->
1029 " HAVING" <+> commaSeparated (renderSQL <$> conditions)
1036 " HAVING" <+> commaSeparated (renderSQL <$> conditions)
1038+{-----------------------------------------
1040+-----------------------------------------}
1042+-- | `SortExpression`s are used by `sortBy` to optionally sort the results
1043+-- of a `Query`. `Asc` or `Desc` set the sort direction of a `NotNull` result
1044+-- column to ascending or descending. Ascending order puts smaller values
1045+-- first, where "smaller" is defined in terms of the `.<` operator. Similarly,
1046+-- descending order is determined with the `.>` operator. `AscNullsFirst`,
1047+-- `AscNullsLast`, `DescNullsFirst` and `DescNullsLast` options are used to
1048+-- determine whether nulls appear before or after non-null values in the sort
1049+-- ordering of a `Null` result column.
1050+data SortExpression commons schemas params grp from where
1052+ :: Expression commons schemas params grp from ('NotNull ty)
1053+ -> SortExpression commons schemas params grp from
1055+ :: Expression commons schemas params grp from ('NotNull ty)
1056+ -> SortExpression commons schemas params grp from
1058+ :: Expression commons schemas params grp from ('Null ty)
1059+ -> SortExpression commons schemas params grp from
1061+ :: Expression commons schemas params grp from ('Null ty)
1062+ -> SortExpression commons schemas params grp from
1064+ :: Expression commons schemas params grp from ('Null ty)
1065+ -> SortExpression commons schemas params grp from
1067+ :: Expression commons schemas params grp from ('Null ty)
1068+ -> SortExpression commons schemas params grp from
1069+deriving instance Show (SortExpression commons schemas params grp from)
1071+-- | Render a `SortExpression`.
1072+instance RenderSQL (SortExpression commons schemas params grp from) where
1074+ Asc expression -> renderSQL expression <+> "ASC"
1075+ Desc expression -> renderSQL expression <+> "DESC"
1076+ AscNullsFirst expression -> renderSQL expression
1077+ <+> "ASC NULLS FIRST"
1078+ DescNullsFirst expression -> renderSQL expression
1079+ <+> "DESC NULLS FIRST"
1080+ AscNullsLast expression -> renderSQL expression <+> "ASC NULLS LAST"
1081+ DescNullsLast expression -> renderSQL expression <+> "DESC NULLS LAST"
1083+unsafeSubqueryExpression
1085+ -> Expression commons schemas params grp from ty
1086+ -> Query commons schemas params '[alias ::: ty]
1087+ -> Expression commons schemas params grp from (nullity 'PGbool)
1088+unsafeSubqueryExpression op x q = UnsafeExpression $
1089+ renderSQL x <+> op <+> parenthesized (renderSQL q)
1091+unsafeRowSubqueryExpression
1094+ -> NP (Aliased (Expression commons schemas params grp from)) row
1095+ -> Query commons schemas params row
1096+ -> Expression commons schemas params grp from (nullity 'PGbool)
1097+unsafeRowSubqueryExpression op xs q = UnsafeExpression $
1098+ renderSQL (row xs) <+> op <+> parenthesized (renderSQL q)
1100+-- | The right-hand side is a sub`Query`, which must return exactly one column.
1101+-- The left-hand expression is evaluated and compared to each row of the
1102+-- sub`Query` result. The result of `in_` is `true` if any equal subquery row is found.
1103+-- The result is `false` if no equal row is found
1104+-- (including the case where the subquery returns no rows).
1106+-- >>> printSQL $ true `in_` values_ (true `as` #foo)
1107+-- TRUE IN (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
1109+ :: Expression commons schemas params grp from ty -- ^ expression
1110+ -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1111+ -> Expression commons schemas params grp from (nullity 'PGbool)
1112+in_ = unsafeSubqueryExpression "IN"
1114+{- | The left-hand side of this form of `rowIn` is a row constructor.
1115+The right-hand side is a sub`Query`,
1116+which must return exactly as many columns as
1117+there are expressions in the left-hand row.
1118+The left-hand expressions are evaluated and compared row-wise to each row
1119+of the subquery result. The result of `rowIn`
1120+is `true` if any equal subquery row is found.
1121+The result is `false` if no equal row is found
1122+(including the case where the subquery returns no rows).
1124+>>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1125+>>> printSQL $ myRow `rowIn` values_ myRow
1126+ROW(1, FALSE) IN (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
1130+ => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1131+ -> Query commons schemas params row -- ^ subquery
1132+ -> Expression commons schemas params grp from (nullity 'PGbool)
1133+rowIn = unsafeRowSubqueryExpression "IN"
1135+-- | >>> printSQL $ true `eqAll` values_ (true `as` #foo)
1136+-- TRUE = ALL (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
1138+ :: Expression commons schemas params grp from ty -- ^ expression
1139+ -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1140+ -> Expression commons schemas params grp from (nullity 'PGbool)
1141+eqAll = unsafeSubqueryExpression "= ALL"
1143+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1144+-- >>> printSQL $ myRow `rowEqAll` values_ myRow
1145+-- ROW(1, FALSE) = ALL (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
1148+ => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1149+ -> Query commons schemas params row -- ^ subquery
1150+ -> Expression commons schemas params grp from (nullity 'PGbool)
1151+rowEqAll = unsafeRowSubqueryExpression "= ALL"
1153+-- | >>> printSQL $ true `eqAny` values_ (true `as` #foo)
1154+-- TRUE = ANY (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
1156+ :: Expression commons schemas params grp from ty -- ^ expression
1157+ -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1158+ -> Expression commons schemas params grp from (nullity 'PGbool)
1159+eqAny = unsafeSubqueryExpression "= ANY"
1161+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1162+-- >>> printSQL $ myRow `rowEqAny` values_ myRow
1163+-- ROW(1, FALSE) = ANY (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
1166+ => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1167+ -> Query commons schemas params row -- ^ subquery
1168+ -> Expression commons schemas params grp from (nullity 'PGbool)
1169+rowEqAny = unsafeRowSubqueryExpression "= ANY"
1171+-- | >>> printSQL $ true `neqAll` values_ (true `as` #foo)
1172+-- TRUE <> ALL (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
1174+ :: Expression commons schemas params grp from ty -- ^ expression
1175+ -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1176+ -> Expression commons schemas params grp from (nullity 'PGbool)
1177+neqAll = unsafeSubqueryExpression "<> ALL"
1179+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1180+-- >>> printSQL $ myRow `rowNeqAll` values_ myRow
1181+-- ROW(1, FALSE) <> ALL (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
1184+ => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1185+ -> Query commons schemas params row -- ^ subquery
1186+ -> Expression commons schemas params grp from (nullity 'PGbool)
1187+rowNeqAll = unsafeRowSubqueryExpression "<> ALL"
1189+-- | >>> printSQL $ true `neqAny` values_ (true `as` #foo)
1190+-- TRUE <> ANY (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
1192+ :: Expression commons schemas params grp from ty -- ^ expression
1193+ -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1194+ -> Expression commons schemas params grp from (nullity 'PGbool)
1195+neqAny = unsafeSubqueryExpression "<> ANY"
1197+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1198+-- >>> printSQL $ myRow `rowNeqAny` values_ myRow
1199+-- ROW(1, FALSE) <> ANY (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
1202+ => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1203+ -> Query commons schemas params row -- ^ subquery
1204+ -> Expression commons schemas params grp from (nullity 'PGbool)
1205+rowNeqAny = unsafeRowSubqueryExpression "<> ANY"
1207+-- | >>> printSQL $ true `allLt` values_ (true `as` #foo)
1208+-- TRUE ALL < (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
1210+ :: Expression commons schemas params grp from ty -- ^ expression
1211+ -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1212+ -> Expression commons schemas params grp from (nullity 'PGbool)
1213+allLt = unsafeSubqueryExpression "ALL <"
1215+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1216+-- >>> printSQL $ myRow `rowLtAll` values_ myRow
1217+-- ROW(1, FALSE) ALL < (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
1220+ => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1221+ -> Query commons schemas params row -- ^ subquery
1222+ -> Expression commons schemas params grp from (nullity 'PGbool)
1223+rowLtAll = unsafeRowSubqueryExpression "ALL <"
1225+-- | >>> printSQL $ true `ltAny` values_ (true `as` #foo)
1226+-- TRUE ANY < (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
1228+ :: Expression commons schemas params grp from ty -- ^ expression
1229+ -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1230+ -> Expression commons schemas params grp from (nullity 'PGbool)
1231+ltAny = unsafeSubqueryExpression "ANY <"
1233+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1234+-- >>> printSQL $ myRow `rowLtAll` values_ myRow
1235+-- ROW(1, FALSE) ALL < (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
1238+ => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1239+ -> Query commons schemas params row -- ^ subquery
1240+ -> Expression commons schemas params grp from (nullity 'PGbool)
1241+rowLtAny = unsafeRowSubqueryExpression "ANY <"
1243+-- | >>> printSQL $ true `lteAll` values_ (true `as` #foo)
1244+-- TRUE <= ALL (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
1246+ :: Expression commons schemas params grp from ty -- ^ expression
1247+ -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1248+ -> Expression commons schemas params grp from (nullity 'PGbool)
1249+lteAll = unsafeSubqueryExpression "<= ALL"
1251+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1252+-- >>> printSQL $ myRow `rowLteAll` values_ myRow
1253+-- ROW(1, FALSE) <= ALL (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
1256+ => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1257+ -> Query commons schemas params row -- ^ subquery
1258+ -> Expression commons schemas params grp from (nullity 'PGbool)
1259+rowLteAll = unsafeRowSubqueryExpression "<= ALL"
1261+-- | >>> printSQL $ true `lteAny` values_ (true `as` #foo)
1262+-- TRUE <= ANY (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
1264+ :: Expression commons schemas params grp from ty -- ^ expression
1265+ -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1266+ -> Expression commons schemas params grp from (nullity 'PGbool)
1267+lteAny = unsafeSubqueryExpression "<= ANY"
1269+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1270+-- >>> printSQL $ myRow `rowLteAny` values_ myRow
1271+-- ROW(1, FALSE) <= ANY (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
1274+ => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1275+ -> Query commons schemas params row -- ^ subquery
1276+ -> Expression commons schemas params grp from (nullity 'PGbool)
1277+rowLteAny = unsafeRowSubqueryExpression "<= ANY"
1279+-- | >>> printSQL $ true `gtAll` values_ (true `as` #foo)
1280+-- TRUE > ALL (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
1282+ :: Expression commons schemas params grp from ty -- ^ expression
1283+ -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1284+ -> Expression commons schemas params grp from (nullity 'PGbool)
1285+gtAll = unsafeSubqueryExpression "> ALL"
1287+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1288+-- >>> printSQL $ myRow `rowGtAll` values_ myRow
1289+-- ROW(1, FALSE) > ALL (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
1292+ => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1293+ -> Query commons schemas params row -- ^ subquery
1294+ -> Expression commons schemas params grp from (nullity 'PGbool)
1295+rowGtAll = unsafeRowSubqueryExpression "> ALL"
1297+-- | >>> printSQL $ true `gtAny` values_ (true `as` #foo)
1298+-- TRUE > ANY (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
1300+ :: Expression commons schemas params grp from ty -- ^ expression
1301+ -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1302+ -> Expression commons schemas params grp from (nullity 'PGbool)
1303+gtAny = unsafeSubqueryExpression "> ANY"
1305+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1306+-- >>> printSQL $ myRow `rowGtAny` values_ myRow
1307+-- ROW(1, FALSE) > ANY (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
1310+ => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1311+ -> Query commons schemas params row -- ^ subquery
1312+ -> Expression commons schemas params grp from (nullity 'PGbool)
1313+rowGtAny = unsafeRowSubqueryExpression "> ANY"
1315+-- | >>> printSQL $ true `gteAll` values_ (true `as` #foo)
1316+-- TRUE >= ALL (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
1318+ :: Expression commons schemas params grp from ty -- ^ expression
1319+ -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1320+ -> Expression commons schemas params grp from (nullity 'PGbool)
1321+gteAll = unsafeSubqueryExpression ">= ALL"
1323+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1324+-- >>> printSQL $ myRow `rowGteAll` values_ myRow
1325+-- ROW(1, FALSE) >= ALL (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
1328+ => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1329+ -> Query commons schemas params row -- ^ subquery
1330+ -> Expression commons schemas params grp from (nullity 'PGbool)
1331+rowGteAll = unsafeRowSubqueryExpression ">= ALL"
1333+-- | >>> printSQL $ true `gteAny` values_ (true `as` #foo)
1334+-- TRUE >= ANY (SELECT * FROM (VALUES (TRUE)) AS t ("foo"))
1336+ :: Expression commons schemas params grp from ty -- ^ expression
1337+ -> Query commons schemas params '[alias ::: ty] -- ^ subquery
1338+ -> Expression commons schemas params grp from (nullity 'PGbool)
1339+gteAny = unsafeSubqueryExpression ">= ANY"
1341+-- | >>> let myRow = 1 `as` #foo :* false `as` #bar :: NP (Aliased (Expression commons schemas params grp from)) '["foo" ::: 'NotNull 'PGint2, "bar" ::: 'NotNull 'PGbool]
1342+-- >>> printSQL $ myRow `rowGteAny` values_ myRow
1343+-- ROW(1, FALSE) >= ANY (SELECT * FROM (VALUES (1, FALSE)) AS t ("foo", "bar"))
1346+ => NP (Aliased (Expression commons schemas params grp from)) row -- ^ row constructor
1347+ -> Query commons schemas params row -- ^ subquery
1348+ -> Expression commons schemas params grp from (nullity 'PGbool)
1349+rowGteAny = unsafeRowSubqueryExpression ">= ANY"
1031-- | A `CommonTableExpression` is an auxiliary statement in a `with` clause.
1351-- | A `CommonTableExpression` is an auxiliary statement in a `with` clause.
1032data CommonTableExpression statement
1352data CommonTableExpression statement
1033 (schemas :: SchemasType)
1353 (schemas :: SchemasType)
@@ -1049,50 +1369,41 @@ instance
1049 , commons1 ~ (cte ::: common ': commons)
1369 , commons1 ~ (cte ::: common ': commons)
1050 ) => Aliasable cte
1370 ) => Aliasable cte
1051 (statement commons schemas params common)
1371 (statement commons schemas params common)
1052− (Path (CommonTableExpression statement schemas params) commons commons1) where
1372+ (AlignedList (CommonTableExpression statement schemas params) commons commons1) where
1053− statement `as` cte = csingleton (statement `as` cte)
1373+ statement `as` cte = single (statement `as` cte)
1055−instance (forall c s p r. RenderSQL (statement c s p r)) => RenderSQL
1375+-- | render a `CommonTableExpression`.
1376+renderCommonTableExpression
1377+ :: (forall cs ss ps row. statement cs ss ps row -> ByteString) -- ^ render statement
1056− (CommonTableExpression statement schemas params commons0 commons1) where
1378+ -> CommonTableExpression statement schemas params commons0 commons1 -> ByteString
1379+renderCommonTableExpression renderStatement
1057− renderSQL (CommonTableExpression (statement `As` cte)) =
1380+ (CommonTableExpression (statement `As` alias)) =
1058− renderSQL cte <+> "AS" <+> parenthesized (renderSQL statement)
1381+ renderSQL alias <+> "AS" <+> parenthesized (renderStatement statement)
1383+-- | render a non-empty `AlignedList` of `CommonTableExpression`s.
1384+renderCommonTableExpressions
1385+ :: (forall cs ss ps row. statement cs ss ps row -> ByteString) -- ^ render statement
1386+ -> CommonTableExpression statement schemas params commons0 commons1
1387+ -> AlignedList (CommonTableExpression statement schemas params) commons1 commons2
1389+renderCommonTableExpressions renderStatement cte ctes =
1390+ renderCommonTableExpression renderStatement cte <> case ctes of
1392+ cte' :>> ctes' -> "," <+>
1393+ renderCommonTableExpressions renderStatement cte' ctes'
1060-- | `with` provides a way to write auxiliary statements for use in a larger query.
1395-- | `with` provides a way to write auxiliary statements for use in a larger query.
1061-- These statements, referred to as `CommonTableExpression`s, can be thought of as
1396-- These statements, referred to as `CommonTableExpression`s, can be thought of as
1062-- defining temporary tables that exist just for one query.
1397-- defining temporary tables that exist just for one query.
1063class With statement where
1398class With statement where
1065− :: Path (CommonTableExpression statement schemas params) commons0 commons1
1400+ :: AlignedList (CommonTableExpression statement schemas params) commons0 commons1
1066 -- ^ common table expressions
1401 -- ^ common table expressions
1067 -> statement commons1 schemas params row
1402 -> statement commons1 schemas params row
1068 -- ^ larger query
1403 -- ^ larger query
1069 -> statement commons0 schemas params row
1404 -> statement commons0 schemas params row
1070−instance With (Query outer) where
1405+instance With Query where
1071 with Done query = query
1406 with Done query = query
1072− with ctes query = UnsafeQuery $
1407+ with (cte :>> ctes) query = UnsafeQuery $
1073− "WITH" <+> commaSeparated (ctoList renderSQL ctes) <+> renderSQL query
1076−>>> import Data.Monoid (Sum (..))
1077−>>> import Data.Int (Int64)
1080− query :: Query_ schema () (Sum Int64)
1081− query = withRecursive
1082− ( values_ ((1 & astype int) `as` #n)
1084− select_ ((#n + 1) `as` #n)
1085− (from (common #t) & where_ (#n .< 100)) `as` #t )
1086− ( select_ (fromNull 0 (sum_ (All #n)) `as` #getSum) (from (common #t) & groupBy Nil))
1089−WITH RECURSIVE "t" AS ((SELECT * FROM (VALUES ((1 :: int))) AS t ("n")) UNION ALL (SELECT ("n" + 1) AS "n" FROM "t" AS "t" WHERE ("n" < 100))) SELECT COALESCE(sum(ALL "n"), 0) AS "getSum" FROM "t" AS "t"
1092− :: Aliased (Query outer (recursive ': commons) schemas params) recursive
1093− -> Query outer (recursive ': commons) schemas params row
1094− -> Query outer commons schemas params row
1095−withRecursive (recursive `As` cte) query = UnsafeQuery $
1096− "WITH RECURSIVE" <+> renderSQL cte
1408+ "WITH" <+> renderCommonTableExpressions renderSQL cte ctes
1097− <+> "AS" <+> parenthesized (renderSQL recursive)
1098− <+> renderSQL query
1409+ <+> renderSQL query