@@ -42,8 +42,6 @@ module Squeal.PostgreSQL.Manipulation
42 -- * Delete
42 -- * Delete
43 , deleteFrom
43 , deleteFrom
44 , deleteFrom_
44 , deleteFrom_
49import Control.DeepSeq
47import Control.DeepSeq
@@ -180,7 +178,25 @@ let
180in printSQL manipulation
178in printSQL manipulation
182DELETE FROM "tab" WHERE ("col1" = "col2") RETURNING *
180DELETE FROM "tab" WHERE ("col1" = "col2") RETURNING *
184+>>> type ProductsTable = '[] :=> '["product" ::: 'NoDef :=> 'NotNull 'PGtext, "date" ::: 'Def :=> 'NotNull 'PGdate]
188+ manipulation :: Manipulation
189+ '[ "products" ::: 'Table ProductsTable
190+ , "products_deleted" ::: 'Table ProductsTable
191+ ] '[ 'NotNull 'PGdate] '[]
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
197+WITH "deleted_rows" AS (DELETE FROM "products" WHERE ("date" < ($1 :: date)) RETURNING *) INSERT INTO "products_deleted" SELECT * FROM "deleted_rows" AS "t"
184newtype Manipulation
200newtype Manipulation
185 (schema :: SchemaType)
201 (schema :: SchemaType)
186 (params :: [NullityType])
202 (params :: [NullityType])
@@ -189,9 +205,9 @@ newtype Manipulation
189 deriving (GHC.Generic,Show,Eq,Ord,NFData)
205 deriving (GHC.Generic,Show,Eq,Ord,NFData)
190instance RenderSQL (Manipulation schema params columns) where
206instance RenderSQL (Manipulation schema params columns) where
191 renderSQL = renderManipulation
207 renderSQL = renderManipulation
192−instance With' Manipulation where
208+instance With Manipulation where
193− with' Done manip = manip
209+ with Done manip = manip
194− with' (cte :>> ctes) manip = UnsafeManipulation $
210+ with (cte :>> ctes) manip = UnsafeManipulation $
195 "WITH" <+> renderCommonTableExpressions renderManipulation cte ctes
211 "WITH" <+> renderCommonTableExpressions renderManipulation cte ctes
196 <+> renderManipulation manip
212 <+> renderManipulation manip
@@ -473,39 +489,3 @@ deleteFrom_
473 -- ^ condition under which to delete a row
489 -- ^ condition under which to delete a row
474 -> Manipulation schema params '[]
490 -> Manipulation schema params '[]
475deleteFrom_ tab wh = deleteFrom tab wh (Returning Nil)
491deleteFrom_ tab wh = deleteFrom tab wh (Returning Nil)
477−{-----------------------------------------
479−-----------------------------------------}
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.
485−-- >>> type ProductsTable = '[] :=> '["product" ::: 'NoDef :=> 'NotNull 'PGtext, "date" ::: 'Def :=> 'NotNull 'PGdate]
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
495−-- WITH "deleted_rows" AS (DELETE FROM "products" WHERE ("date" < ($1 :: date)) RETURNING *) INSERT INTO "products_deleted" SELECT * FROM "deleted_rows" AS "t"
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
507− :: Aliased (Manipulation schema params) common
509− renderCommon (common `As` alias) =
510− renderAlias alias <+> "AS" <+>
511− parenthesized (renderManipulation common)