@@ -53,6 +53,7 @@ module Squeal.PostgreSQL.Query
53 -- ** With
53 -- ** With
54 , With (..)
54 , With (..)
55 , CommonTableExpression (..)
55 , CommonTableExpression (..)
56 -- * Table Expressions
57 -- * Table Expressions
57 , TableExpression (..)
58 , TableExpression (..)
@@ -965,3 +966,32 @@ instance With (Query outer) where
965 with Done query = query
966 with Done query = query
966 with ctes query = UnsafeQuery $
967 with ctes query = UnsafeQuery $
967 "WITH" <+> renderSQL ctes <+> renderSQL query
968 "WITH" <+> renderSQL ctes <+> renderSQL query
971+>>> import Data.Monoid (Sum (..))
972+>>> import Data.Int (Int32)
975+ one :: Expr ('NotNull 'PGint4)
977+ query :: Query_ schema () (Sum Int32)
978+ query = withRecursive
979+ ( values_ (one `as` #n)
981+ select_ ((#n + 1) `as` #n)
982+ (from (common #t) & where_ (#n .< 100)) `as` #t )
983+ ( select_ (sum_ (All #n) `as` #getSum) (from (common #t) & groupBy Nil))
986+WITH RECURSIVE "t"("n") AS ((SELECT * FROM (VALUES (1)) AS t ("n")) UNION ALL (SELECT ("n" + 1) AS "n" FROM "t" AS "t" WHERE ("n" < 100))) SELECT sum(ALL "n") AS "getSum" FROM "t" AS "t"
989+ :: RenderFields recursive
990+ => Aliased (Query outer (cte ::: recursive ': commons) schemas params) (cte ::: recursive)
991+ -> Query outer (cte ::: recursive ': commons) schemas params row
992+ -> Query outer commons schemas params row
993+withRecursive (recursive `As` cte) query = UnsafeQuery $
994+ "WITH RECURSIVE" <+> renderSQL cte
995+ <> parenthesized (commaSeparated (renderFields recursive))
996+ <+> "AS" <+> parenthesized (renderSQL recursive)