on dev help sign in
scrap your dones for CTEs
27adc49Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+1−1
@@ -192,7 +192,7 @@ instance RenderSQL (Manipulation schema params columns) where
192instance With' Manipulation where
192instance With' Manipulation where
193 with' Done manip = manip
193 with' Done manip = manip
194 with' (cte :>> ctes) manip = UnsafeManipulation $
194 with' (cte :>> ctes) manip = UnsafeManipulation $
195− "WITH" <+> renderCtes renderManipulation cte ctes
195+ "WITH" <+> renderCommonTableExpressions renderManipulation cte ctes
196 <+> renderManipulation manip
196 <+> renderManipulation manip
197
197
198-- | Convert a `Query` into a `Manipulation`.
198-- | Convert a `Query` into a `Manipulation`.
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+32−47
@@ -120,8 +120,8 @@ module Squeal.PostgreSQL.Query
120 , rowAnyGte
120 , rowAnyGte
121 , With' (with')
121 , With' (with')
122 , CommonTableExpression (..)
122 , CommonTableExpression (..)
123− , renderCte
123+ , renderCommonTableExpression
124− , renderCtes
124+ , renderCommonTableExpressions
125 ) where
125 ) where
126
126
127import Control.DeepSeq
127import Control.DeepSeq
@@ -334,37 +334,19 @@ with queries:
334>>> :{
334>>> :{
335let
335let
336 query :: Query
336 query :: Query
337− '[ "orders" ::: 'View
337+ '[ "t1" ::: 'View
338− '[ "region" ::: 'NotNull 'PGtext
338+ '[ "c1" ::: 'NotNull 'PGtext
339− , "product" ::: 'NotNull 'PGtext
339+ , "c2" ::: 'NotNull 'PGtext] ]
340− , "quantity" ::: 'NotNull 'PGint8
340+ '[]
341− , "amount" ::: 'NotNull 'PGnumeric ] ]
342− '[ 'NotNull 'PGint8 ]
343− '[ "region" ::: 'NotNull 'PGtext
341+ '[ "c1" ::: 'NotNull 'PGtext
344− , "product" ::: 'NotNull 'PGtext
342+ , "c2" ::: 'NotNull 'PGtext ]
345− , "product_units" ::: 'NotNull 'PGint8
346− , "product_sales" ::: 'NotNull 'PGnumeric ]
347 query = with' (
343 query = with' (
348− CommonTableExpression
344+ selectStar (from (view #t1)) `as` #t2 :>>
349− (((select (#region :* sum_ @'PGnumeric #amount `as` #total_sales)
345+ selectStar (from (view #t2)) `as` #t3
350− (from (view #orders) & groupBy #region))
346+ ) (selectStar (from (view #t3)))
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− )
365in printSQL query
347in printSQL query
366:}
348:}
367−
349+WITH "t2" AS (SELECT * FROM "t1" AS "t1"), "t3" AS (SELECT * FROM "t2" AS "t2") SELECT * FROM "t3" AS "t3"
368-}
350-}
369newtype Query
351newtype Query
370 (schema :: SchemaType)
352 (schema :: SchemaType)
@@ -1337,31 +1319,33 @@ data CommonTableExpression statement
1337 :: Aliased (statement schema params) (alias ::: cte)
1319 :: Aliased (statement schema params) (alias ::: cte)
1338 -> CommonTableExpression
1320 -> CommonTableExpression
1339 statement params schema (alias ::: 'View cte ': schema)
1321 statement params schema (alias ::: 'View cte ': schema)
1340−instance KnownSymbol alias => Aliasable alias (statement schema params cte)
1322+instance (KnownSymbol alias, schema1 ~ (alias ::: 'View cte ': schema))
1323+ => Aliasable alias
1324+ (Query schema params cte)
1341− (CommonTableExpression statement params schema
1325+ (CommonTableExpression Query params schema schema1) where
1342− (alias ::: 'View cte ': schema)) where
1343− statement `as` alias = CommonTableExpression (statement `As` alias)
1326+ query `as` alias = CommonTableExpression (query `as` alias)
1327+instance (KnownSymbol alias, schema1 ~ (alias ::: 'View cte ': schema))
1344−instance KnownSymbol alias => Aliasable alias (statement schema params cte)
1328+ => Aliasable alias (Query schema params cte)
1345− (AlignedList (CommonTableExpression statement params) schema
1329+ (AlignedList (CommonTableExpression Query params) schema schema1) where
1346− (alias ::: 'View cte ': schema)) where
1347− statement `as` alias = statement `as` alias :>> Done
1330+ query `as` alias = single (query `as` alias)
1348−
1331+
1349−renderCte
1332+renderCommonTableExpression
1350 :: (forall sch ps row. statement ps sch row -> ByteString)
1333 :: (forall sch ps row. statement ps sch row -> ByteString)
1351 -> CommonTableExpression statement params schema0 schema1 -> ByteString
1334 -> CommonTableExpression statement params schema0 schema1 -> ByteString
1335+renderCommonTableExpression renderStatement
1352−renderCte renderStatement (CommonTableExpression (statement `As` alias)) =
1336+ (CommonTableExpression (statement `As` alias)) =
1353− renderAlias alias <+> "AS" <+> parenthesized (renderStatement statement)
1337+ renderAlias alias <+> "AS" <+> parenthesized (renderStatement statement)
1354
1338
1355−renderCtes
1339+renderCommonTableExpressions
1356 :: (forall sch ps row. statement ps sch row -> ByteString)
1340 :: (forall sch ps row. statement ps sch row -> ByteString)
1357 -> CommonTableExpression statement params schema0 schema1
1341 -> CommonTableExpression statement params schema0 schema1
1358 -> AlignedList (CommonTableExpression statement params) schema1 schema2
1342 -> AlignedList (CommonTableExpression statement params) schema1 schema2
1359 -> ByteString
1343 -> ByteString
1360−renderCtes renderStatement cte ctes =
1344+renderCommonTableExpressions renderStatement cte ctes =
1361− renderCte renderStatement cte <+> case ctes of
1345+ renderCommonTableExpression renderStatement cte <> case ctes of
1362 Done -> ""
1346 Done -> ""
1363 cte' :>> ctes' -> "," <+>
1347 cte' :>> ctes' -> "," <+>
1364− renderCtes renderStatement cte' ctes'
1348+ renderCommonTableExpressions renderStatement cte' ctes'
1365
1349
1366class With' statement where
1350class With' statement where
1367 with'
1351 with'
@@ -1371,4 +1355,5 @@ class With' statement where
1371instance With' Query where
1355instance With' Query where
1372 with' Done query = query
1356 with' Done query = query
1373 with' (cte :>> ctes) query = UnsafeQuery $
1357 with' (cte :>> ctes) query = UnsafeQuery $
1358+ "WITH" <+> renderCommonTableExpressions renderQuery cte ctes
1374− "WITH" <+> renderCtes renderQuery cte ctes <+> renderQuery query
1359+ <+> renderQuery query
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+1−1
@@ -420,7 +420,7 @@ class KnownSymbol alias => Aliasable alias expression aliased
420 | aliased -> expression
420 | aliased -> expression
421 , aliased -> alias
421 , aliased -> alias
422 where as :: expression -> Alias alias -> aliased
422 where as :: expression -> Alias alias -> aliased
423−instance (alias ~ alias1, KnownSymbol alias) => Aliasable alias
423+instance (KnownSymbol alias, alias ~ alias1) => Aliasable alias
424 (expression ty)
424 (expression ty)
425 (Aliased expression (alias1 ::: ty))
425 (Aliased expression (alias1 ::: ty))
426 where
426 where