@@ -145,13 +145,20 @@ records, whose entries will be targeted using overloaded labels.
146Let's see some examples of queries.
146Let's see some examples of queries.
148+>>> :set -XDeriveAnyClass -XDerivingStrategies
150+data Row a b = Row { col1 :: a, col2 :: b }
151+ deriving stock (GHC.Generic)
152+ deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
148simple query:
155simple query:
150>>> type Columns = '["col1" ::: 'NoDef :=> 'NotNull 'PGint4, "col2" ::: 'NoDef :=> 'NotNull 'PGint4]
157>>> type Columns = '["col1" ::: 'NoDef :=> 'NotNull 'PGint4, "col2" ::: 'NoDef :=> 'NotNull 'PGint4]
151>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
158>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
154− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("col2" ::: Int32))
161+ query :: Query_ (Public Schema) () (Row Int32 Int32)
155 query = select Star (from (table #tab))
162 query = select Star (from (table #tab))
156in printSQL query
163in printSQL query
@@ -161,21 +168,21 @@ restricted query:
164− query :: Query_ (Public Schema) () (Sum Int32, P ("col1" ::: Int32))
171+ query :: Query_ (Public Schema) () (Row Int32 Int32)
166− select_ ((#col1 + #col2) `as` #getSum :* #col1)
173+ select_ ((#col1 + #col2) `as` #col1 :* #col1 `as` #col2)
167 ( from (table #tab)
174 ( from (table #tab)
168 & where_ (#col1 .> #col2)
175 & where_ (#col1 .> #col2)
169 & where_ (#col2 .> 0) )
176 & where_ (#col2 .> 0) )
170in printSQL query
177in printSQL query
172−SELECT ("col1" + "col2") AS "getSum", "col1" AS "col1" FROM "tab" AS "tab" WHERE (("col1" > "col2") AND ("col2" > 0))
179+SELECT ("col1" + "col2") AS "col1", "col1" AS "col2" FROM "tab" AS "tab" WHERE (("col1" > "col2") AND ("col2" > 0))
178− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("col2" ::: Int32))
185+ query :: Query_ (Public Schema) () (Row Int32 Int32)
179 query = select Star (from (subquery (select Star (from (table #tab)) `as` #sub)))
186 query = select Star (from (subquery (select Star (from (table #tab)) `as` #sub)))
180in printSQL query
187in printSQL query
@@ -185,7 +192,7 @@ limits and offsets:
188− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("col2" ::: Int32))
195+ query :: Query_ (Public Schema) () (Row Int32 Int32)
189 query = select Star (from (table #tab) & limit 100 & offset 2 & limit 50 & offset 2)
196 query = select Star (from (table #tab) & limit 100 & offset 2 & limit 50 & offset 2)
190in printSQL query
197in printSQL query
@@ -195,7 +202,7 @@ parameterized query:
198− query :: Query_ (Public Schema) (Only Int32) (P ("col1" ::: Int32), P ("col2" ::: Int32))
205+ query :: Query_ (Public Schema) (Only Int32) (Row Int32 Int32)
199 query = select Star (from (table #tab) & where_ (#col1 .> param @1))
206 query = select Star (from (table #tab) & where_ (#col1 .> param @1))
200in printSQL query
207in printSQL query
@@ -205,21 +212,21 @@ aggregation query:
208− query :: Query_ (Public Schema) () (Sum Int32, P ("col1" ::: Int32))
215+ query :: Query_ (Public Schema) () (Row Int32 Int32)
210− select_ ((fromNull 0 (sum_ (All #col2))) `as` #getSum :* #col1)
217+ select_ ((fromNull 0 (sum_ (All #col2))) `as` #col1 :* #col1 `as` #col2)
211 ( from (table (#tab `as` #table1))
218 ( from (table (#tab `as` #table1))
212 & groupBy #col1
219 & groupBy #col1
213 & having (#col1 + (fromNull 0 (sum_ (Distinct #col2))) .> 1) )
220 & having (#col1 + (fromNull 0 (sum_ (Distinct #col2))) .> 1) )
214in printSQL query
221in printSQL query
216−SELECT COALESCE(sum(ALL "col2"), 0) AS "getSum", "col1" AS "col1" FROM "tab" AS "table1" GROUP BY "col1" HAVING (("col1" + COALESCE(sum(DISTINCT "col2"), 0)) > 1)
223+SELECT COALESCE(sum(ALL "col2"), 0) AS "col1", "col1" AS "col2" FROM "tab" AS "table1" GROUP BY "col1" HAVING (("col1" + COALESCE(sum(DISTINCT "col2"), 0)) > 1)
218sorted query:
225sorted query:
222− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("col2" ::: Int32))
229+ query :: Query_ (Public Schema) () (Row Int32 Int32)
223 query = select Star (from (table #tab) & orderBy [#col1 & Asc])
230 query = select Star (from (table #tab) & orderBy [#col1 & Asc])
224in printSQL query
231in printSQL query
@@ -282,8 +289,10 @@ self-join:
285− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("col2" ::: Int32))
292+ query :: Query_ (Public Schema) () (Row Int32 Int32)
286− query = select (#t1 & DotStar) (from (table (#tab `as` #t1) & crossJoin (table (#tab `as` #t2))))
295+ (from (table (#tab `as` #t1) & crossJoin (table (#tab `as` #t2))))
287in printSQL query
296in printSQL query
289SELECT "t1".* FROM "tab" AS "t1" CROSS JOIN "tab" AS "t2"
298SELECT "t1".* FROM "tab" AS "t1" CROSS JOIN "tab" AS "t2"
@@ -292,17 +301,19 @@ value queries:
295− query :: Query_ schemas () (P ("foo" ::: Int32), P ("bar" ::: Bool))
304+ query :: Query_ schemas () (Row String Bool)
296− query = values (1 `as` #foo :* true `as` #bar) [2 `as` #foo :* false `as` #bar]
305+ query = values
306+ ("true" `as` #col1 :* true `as` #col2)
307+ ["false" `as` #col1 :* false `as` #col2]
297in printSQL query
308in printSQL query
299−SELECT * FROM (VALUES (1, TRUE), (2, FALSE)) AS t ("foo", "bar")
310+SELECT * FROM (VALUES (E'true', TRUE), (E'false', FALSE)) AS t ("col1", "col2")
301set operations:
312set operations:
305− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("col2" ::: Int32))
316+ query :: Query_ (Public Schema) () (Row Int32 Int32)
306 query = select Star (from (table #tab)) `unionAll` select Star (from (table #tab))
317 query = select Star (from (table #tab)) `unionAll` select Star (from (table #tab))
307in printSQL query
318in printSQL query
@@ -312,7 +323,7 @@ with queries:
315− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("col2" ::: Int32))
326+ query :: Query_ (Public Schema) () (Row Int32 Int32)
316 query = with (
327 query = with (
317 select Star (from (table #tab)) `as` #cte1 :>>
328 select Star (from (table #tab)) `as` #cte1 :>>
318 select Star (from (common #cte1)) `as` #cte2
329 select Star (from (common #cte1)) `as` #cte2
@@ -325,27 +336,27 @@ window function queries
328− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("rank" ::: Int64))
339+ query :: Query_ (Public Schema) () (Row Int32 Int64)
329 query = select
340 query = select
330− (#col1 & Also (rank `as` #rank `Over` (partitionBy #col1 & orderBy [#col2 & Asc])))
341+ (#col1 & Also (rank `as` #col2 `Over` (partitionBy #col1 & orderBy [#col2 & Asc])))
331 (from (table #tab))
342 (from (table #tab))
332in printSQL query
343in printSQL query
334−SELECT "col1" AS "col1", rank() OVER (PARTITION BY "col1" ORDER BY "col2" ASC) AS "rank" FROM "tab" AS "tab"
345+SELECT "col1" AS "col1", rank() OVER (PARTITION BY "col1" ORDER BY "col2" ASC) AS "col2" FROM "tab" AS "tab"
336correlated subqueries
347correlated subqueries
340− query :: Query_ (Public Schema) () (P ("col1" ::: Int32))
351+ query :: Query_ (Public Schema) () (Only Int32)
342− select #col1 (from (table (#tab `as` #t1))
353+ select (#col1 `as` #fromOnly) (from (table (#tab `as` #t1))
343 & where_ (exists (
354 & where_ (exists (
344 select Star (from (table (#tab `as` #t2))
355 select Star (from (table (#tab `as` #t2))
345 & where_ (#t2 ! #col2 .== #t1 ! #col1)))))
356 & where_ (#t2 ! #col2 .== #t1 ! #col1)))))
346in printSQL query
357in printSQL query
348−SELECT "col1" AS "col1" FROM "tab" AS "t1" WHERE EXISTS (SELECT * FROM "tab" AS "t2" WHERE ("t2"."col2" = "t1"."col1"))
359+SELECT "col1" AS "fromOnly" FROM "tab" AS "t1" WHERE EXISTS (SELECT * FROM "tab" AS "t2" WHERE ("t2"."col2" = "t1"."col1"))
351type family Query_
362type family Query_