on dev help sign in
remove old WITH
commitebaae8b4bd2de5771a207372958a89e668486550 authorEitan Chatav <eitan.chatav@gmail.com> dateSat 18 Aug 2018, 11:10 (8 years ago) parent27adc49
3 files changed+31−57
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+21−41 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+10−6 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs−10
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+21−41
@@ -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)
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+10−6
@@ -118,7 +118,7 @@ module Squeal.PostgreSQL.Query
118118 , rowAllGte
119119 , anyGte
120120 , rowAnyGte
121− , With' (with')
121+ , With (with)
122122 , CommonTableExpression (..)
123123 , renderCommonTableExpression
124124 , renderCommonTableExpressions
@@ -340,7 +340,7 @@ let
340340 '[]
341341 '[ "c1" ::: 'NotNull 'PGtext
342342 , "c2" ::: 'NotNull 'PGtext ]
343− query = with' (
343+ query = with (
344344 selectStar (from (view #t1)) `as` #t2 :>>
345345 selectStar (from (view #t2)) `as` #t3
346346 ) (selectStar (from (view #t3)))
@@ -1347,13 +1347,17 @@ renderCommonTableExpressions renderStatement cte ctes =
13471347 cte' :>> ctes' -> "," <+>
13481348 renderCommonTableExpressions renderStatement cte' ctes'
13491349
1350−class With' statement where
13511350 with'
1351+-- | `with` provides a way to write auxiliary statements for use in a larger statement.
1352+-- These statements, which are often referred to as Common Table Expressions or CTEs,
1353+-- can be thought of as defining temporary tables that exist just for one statement.
1354+class With statement where
1355+ with
13521356 :: AlignedList (CommonTableExpression statement params) schema0 schema1
13531357 -> statement schema1 params row
13541358 -> statement schema0 params row
1355−instance With' Query where
1356− with' Done query = query
1357− with' (cte :>> ctes) query = UnsafeQuery $
1359+instance With Query where
1360+ with Done query = query
1361+ with (cte :>> ctes) query = UnsafeQuery $
13581362 "WITH" <+> renderCommonTableExpressions renderQuery cte ctes
13591363 <+> renderQuery query
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs−10
@@ -81,7 +81,6 @@ module Squeal.PostgreSQL.Schema
8181 , GroupedBy
8282 -- * Type Families
8383 , Join
84− , With
8584 , Create
8685 , Drop
8786 , Alter
@@ -667,15 +666,6 @@ type family SchemumToType (schemum :: SchemumType) :: PGType where
667666-- user-defined `SchemumType`s.
668667type SchemaType = [(Symbol,SchemumType)]
669668
670−-- | Used in `Squeal.Postgresql.Manipulation.with`.
671−type family With
672− (from :: FromType)
673− (schema :: SchemaType)
674− :: SchemaType where
675− With '[] schema = schema
676− With (alias ::: rel ': rels) schema =
677− alias ::: 'View rel ': With rels schema
678−
679669-- | `IsPGlabel` looks very much like the `IsLabel` class. Whereas
680670-- the overloaded label, `fromLabel` is used for column references,
681671-- `label`s are used for enum terms. A `label` is called with