@@ -120,8 +120,8 @@ module Squeal.PostgreSQL.Query
120120 , rowAnyGte
121121 , With' (with')
122122 , CommonTableExpression (..)
123− , renderCte
124− , renderCtes
123+ , renderCommonTableExpression
124+ , renderCommonTableExpressions
125125 ) where
126126
127127import Control.DeepSeq
@@ -334,37 +334,19 @@ with queries:
334334>>> :{
335335let
336336 query :: Query
337− '[ "orders" ::: 'View
338− '[ "region" ::: 'NotNull 'PGtext
339− , "product" ::: 'NotNull 'PGtext
340− , "quantity" ::: 'NotNull 'PGint8
341− , "amount" ::: 'NotNull 'PGnumeric ] ]
342− '[ 'NotNull 'PGint8 ]
343− '[ "region" ::: 'NotNull 'PGtext
344− , "product" ::: 'NotNull 'PGtext
345− , "product_units" ::: 'NotNull 'PGint8
346− , "product_sales" ::: 'NotNull 'PGnumeric ]
337+ '[ "t1" ::: 'View
338+ '[ "c1" ::: 'NotNull 'PGtext
339+ , "c2" ::: 'NotNull 'PGtext] ]
340+ '[]
341+ '[ "c1" ::: 'NotNull 'PGtext
342+ , "c2" ::: 'NotNull 'PGtext ]
347343 query = with' (
348− CommonTableExpression
349− (((select (#region :* sum_ @'PGnumeric #amount `as` #total_sales)
350− (from (view #orders) & groupBy #region))
351− `as` #regional_sales)) :>>
352− CommonTableExpression
353− (((select (#region `as` #region)
354− (from (view #regional_sales) & where_ (#total_sales .> param @1)))
355− `as` #top_regions)) :>> Done
356− ) (select (
357− #region :*
358− #product :*
359− sum_ #quantity `as` #product_units :*
360− sum_ #amount `as` #product_sales
361− ) (from (view #orders)
362− & where_ (#region `in_` select #region (from (view #top_regions)))
363− & groupBy (#region :* #product))
364− )
344+ selectStar (from (view #t1)) `as` #t2 :>>
345+ selectStar (from (view #t2)) `as` #t3
346+ ) (selectStar (from (view #t3)))
365347in printSQL query
366348:}
367−
349+WITH "t2" AS (SELECT * FROM "t1" AS "t1"), "t3" AS (SELECT * FROM "t2" AS "t2") SELECT * FROM "t3" AS "t3"
368350-}
369351newtype Query
370352 (schema :: SchemaType)
@@ -1337,31 +1319,33 @@ data CommonTableExpression statement
13371319 :: Aliased (statement schema params) (alias ::: cte)
13381320 -> CommonTableExpression
13391321 statement params schema (alias ::: 'View cte ': schema)
1340−instance KnownSymbol alias => Aliasable alias (statement schema params cte)
1341− (CommonTableExpression statement params schema
1342− (alias ::: 'View cte ': schema)) where
1343− statement `as` alias = CommonTableExpression (statement `As` alias)
1344−instance KnownSymbol alias => Aliasable alias (statement schema params cte)
1345− (AlignedList (CommonTableExpression statement params) schema
1346− (alias ::: 'View cte ': schema)) where
1347− statement `as` alias = statement `as` alias :>> Done
1348−
1349−renderCte
1322+instance (KnownSymbol alias, schema1 ~ (alias ::: 'View cte ': schema))
1323+ => Aliasable alias
1324+ (Query schema params cte)
1325+ (CommonTableExpression Query params schema schema1) where
1326+ query `as` alias = CommonTableExpression (query `as` alias)
1327+instance (KnownSymbol alias, schema1 ~ (alias ::: 'View cte ': schema))
1328+ => Aliasable alias (Query schema params cte)
1329+ (AlignedList (CommonTableExpression Query params) schema schema1) where
1330+ query `as` alias = single (query `as` alias)
1331+
1332+renderCommonTableExpression
13501333 :: (forall sch ps row. statement ps sch row -> ByteString)
13511334 -> CommonTableExpression statement params schema0 schema1 -> ByteString
1352−renderCte renderStatement (CommonTableExpression (statement `As` alias)) =
1353− renderAlias alias <+> "AS" <+> parenthesized (renderStatement statement)
1335+renderCommonTableExpression renderStatement
1336+ (CommonTableExpression (statement `As` alias)) =
1337+ renderAlias alias <+> "AS" <+> parenthesized (renderStatement statement)
13541338
1355−renderCtes
1339+renderCommonTableExpressions
13561340 :: (forall sch ps row. statement ps sch row -> ByteString)
13571341 -> CommonTableExpression statement params schema0 schema1
13581342 -> AlignedList (CommonTableExpression statement params) schema1 schema2
13591343 -> ByteString
1360−renderCtes renderStatement cte ctes =
1361− renderCte renderStatement cte <+> case ctes of
1344+renderCommonTableExpressions renderStatement cte ctes =
1345+ renderCommonTableExpression renderStatement cte <> case ctes of
13621346 Done -> ""
13631347 cte' :>> ctes' -> "," <+>
1364− renderCtes renderStatement cte' ctes'
1348+ renderCommonTableExpressions renderStatement cte' ctes'
13651349
13661350class With' statement where
13671351 with'
@@ -1371,4 +1355,5 @@ class With' statement where
13711355instance With' Query where
13721356 with' Done query = query
13731357 with' (cte :>> ctes) query = UnsafeQuery $
1374− "WITH" <+> renderCtes renderQuery cte ctes <+> renderQuery query
1358+ "WITH" <+> renderCommonTableExpressions renderQuery cte ctes
1359+ <+> renderQuery query