@@ -42,8 +42,6 @@ module Squeal.PostgreSQL.Manipulation
4242 -- * Delete
4343 , deleteFrom
4444 , deleteFrom_
45− -- * With
46− , with
4745 ) where
4846
4947import Control.DeepSeq
@@ -180,7 +178,25 @@ let
180178in printSQL manipulation
181179:}
182180DELETE FROM "tab" WHERE ("col1" = "col2") RETURNING *
181+
182+with manipulation:
183+
184+>>> type ProductsTable = '[] :=> '["product" ::: 'NoDef :=> 'NotNull 'PGtext, "date" ::: 'Def :=> 'NotNull 'PGdate]
185+
186+>>> :{
187+let
188+ manipulation :: Manipulation
189+ '[ "products" ::: 'Table ProductsTable
190+ , "products_deleted" ::: 'Table ProductsTable
191+ ] '[ 'NotNull 'PGdate] '[]
192+ manipulation = with
193+ (deleteFrom #products (#date .< param @1) ReturningStar `as` #deleted_rows)
194+ (insertQuery_ #products_deleted (selectStar (from (view (#deleted_rows `as` #t)))))
195+in printSQL manipulation
196+:}
197+WITH "deleted_rows" AS (DELETE FROM "products" WHERE ("date" < ($1 :: date)) RETURNING *) INSERT INTO "products_deleted" SELECT * FROM "deleted_rows" AS "t"
183198-}
199+
184200newtype Manipulation
185201 (schema :: SchemaType)
186202 (params :: [NullityType])
@@ -189,9 +205,9 @@ newtype Manipulation
189205 deriving (GHC.Generic,Show,Eq,Ord,NFData)
190206instance RenderSQL (Manipulation schema params columns) where
191207 renderSQL = renderManipulation
192−instance With' Manipulation where
193− with' Done manip = manip
194− with' (cte :>> ctes) manip = UnsafeManipulation $
208+instance With Manipulation where
209+ with Done manip = manip
210+ with (cte :>> ctes) manip = UnsafeManipulation $
195211 "WITH" <+> renderCommonTableExpressions renderManipulation cte ctes
196212 <+> renderManipulation manip
197213
@@ -473,39 +489,3 @@ deleteFrom_
473489 -- ^ condition under which to delete a row
474490 -> Manipulation schema params '[]
475491deleteFrom_ tab wh = deleteFrom tab wh (Returning Nil)
476−
477−{-----------------------------------------
478−WITH statements
479−-----------------------------------------}
480−
481−-- | `with` provides a way to write auxiliary statements for use in a larger statement.
482−-- These statements, which are often referred to as Common Table Expressions or CTEs,
483−-- can be thought of as defining temporary tables that exist just for one statement.
484−--
485−-- >>> type ProductsTable = '[] :=> '["product" ::: 'NoDef :=> 'NotNull 'PGtext, "date" ::: 'Def :=> 'NotNull 'PGdate]
486−--
487−-- >>> :{
488−-- let
489−-- manipulation :: Manipulation '["products" ::: 'Table ProductsTable, "products_deleted" ::: 'Table ProductsTable] '[ 'NotNull 'PGdate] '[]
490−-- manipulation = with
491−-- (deleteFrom #products (#date .< param @1) ReturningStar `as` #deleted_rows)
492−-- (insertQuery_ #products_deleted (selectStar (from (view (#deleted_rows `as` #t)))))
493−-- in printSQL manipulation
494−-- :}
495−-- WITH "deleted_rows" AS (DELETE FROM "products" WHERE ("date" < ($1 :: date)) RETURNING *) INSERT INTO "products_deleted" SELECT * FROM "deleted_rows" AS "t"
496−with
497− :: SOP.SListI commons
498− => NP (Aliased (Manipulation schema params)) (common ': commons)
499− -- ^ common table expressions
500− -> Manipulation (With (common ': commons) schema) params results
501− -> Manipulation schema params results
502−with commons manipulation = UnsafeManipulation $
503− "WITH" <+> renderCommaSeparated renderCommon commons
504− <+> renderManipulation manipulation
505− where
506− renderCommon
507− :: Aliased (Manipulation schema params) common
508− -> ByteString
509− renderCommon (common `As` alias) =
510− renderAlias alias <+> "AS" <+>
511− parenthesized (renderManipulation common)