on dev help sign in
manipulation docs
0a30cfeEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+19−16
@@ -73,9 +73,9 @@ let
73 , "col2" ::: 'Def :=> 'NotNull 'PGint4 ])] '[] '[]
73 , "col2" ::: 'Def :=> 'NotNull 'PGint4 ])] '[] '[]
74 manipulation =
74 manipulation =
75 insertRow_ #tab (Set 2 `As` #col1 :* Default `As` #col2 :* Nil)
75 insertRow_ #tab (Set 2 `As` #col1 :* Default `As` #col2 :* Nil)
76−in renderManipulation manipulation
76+in printSQL manipulation
77:}
77:}
78−"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (2, DEFAULT)"
78+INSERT INTO "tab" ("col1", "col2") VALUES (2, DEFAULT)
79
79
80parameterized insert:
80parameterized insert:
81
81
@@ -89,9 +89,9 @@ let
89 manipulation =
89 manipulation =
90 insertRow_ #tab
90 insertRow_ #tab
91 (Set (param @1) `As` #col1 :* Set (param @2) `As` #col2 :* Nil)
91 (Set (param @1) `As` #col1 :* Set (param @2) `As` #col2 :* Nil)
92−in renderManipulation manipulation
92+in printSQL manipulation
93:}
93:}
94−"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (($1 :: int4), ($2 :: int4))"
94+INSERT INTO "tab" ("col1", "col2") VALUES (($1 :: int4), ($2 :: int4))
95
95
96returning insert:
96returning insert:
97
97
@@ -105,9 +105,9 @@ let
105 manipulation =
105 manipulation =
106 insertRow #tab (Set 2 `As` #col1 :* Default `As` #col2 :* Nil)
106 insertRow #tab (Set 2 `As` #col1 :* Default `As` #col2 :* Nil)
107 OnConflictDoRaise (Returning (#col1 `As` #fromOnly :* Nil))
107 OnConflictDoRaise (Returning (#col1 `As` #fromOnly :* Nil))
108−in renderManipulation manipulation
108+in printSQL manipulation
109:}
109:}
110−"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (2, DEFAULT) RETURNING \"col1\" AS \"fromOnly\""
110+INSERT INTO "tab" ("col1", "col2") VALUES (2, DEFAULT) RETURNING "col1" AS "fromOnly"
111
111
112upsert:
112upsert:
113
113
@@ -126,9 +126,9 @@ let
126 (Set 2 `As` #col1 :* Same `As` #col2 :* Nil)
126 (Set 2 `As` #col1 :* Same `As` #col2 :* Nil)
127 [#col1 .== #col2])
127 [#col1 .== #col2])
128 (Returning $ (#col1 + #col2) `As` #sum :* Nil)
128 (Returning $ (#col1 + #col2) `As` #sum :* Nil)
129−in renderManipulation manipulation
129+in printSQL manipulation
130:}
130:}
131−"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (2, 4), (6, 8) ON CONFLICT DO UPDATE SET \"col1\" = 2 WHERE (\"col1\" = \"col2\") RETURNING (\"col1\" + \"col2\") AS \"sum\""
131+INSERT INTO "tab" ("col1", "col2") VALUES (2, 4), (6, 8) ON CONFLICT DO UPDATE SET "col1" = 2 WHERE ("col1" = "col2") RETURNING ("col1" + "col2") AS "sum"
132
132
133query insert:
133query insert:
134
134
@@ -147,9 +147,9 @@ let
147 manipulation =
147 manipulation =
148 insertQuery_ #tab
148 insertQuery_ #tab
149 (selectStar (from (table (#other_tab `As` #t))))
149 (selectStar (from (table (#other_tab `As` #t))))
150−in renderManipulation manipulation
150+in printSQL manipulation
151:}
151:}
152−"INSERT INTO \"tab\" SELECT * FROM \"other_tab\" AS \"t\""
152+INSERT INTO "tab" SELECT * FROM "other_tab" AS "t"
153
153
154update:
154update:
155
155
@@ -162,9 +162,9 @@ let
162 manipulation =
162 manipulation =
163 update_ #tab (Set 2 `As` #col1 :* Same `As` #col2 :* Nil)
163 update_ #tab (Set 2 `As` #col1 :* Same `As` #col2 :* Nil)
164 (#col1 ./= #col2)
164 (#col1 ./= #col2)
165−in renderManipulation manipulation
165+in printSQL manipulation
166:}
166:}
167−"UPDATE \"tab\" SET \"col1\" = 2 WHERE (\"col1\" <> \"col2\")"
167+UPDATE "tab" SET "col1" = 2 WHERE ("col1" <> "col2")
168
168
169delete:
169delete:
170
170
@@ -177,9 +177,9 @@ let
177 '[ "col1" ::: 'NotNull 'PGint4
177 '[ "col1" ::: 'NotNull 'PGint4
178 , "col2" ::: 'NotNull 'PGint4 ]
178 , "col2" ::: 'NotNull 'PGint4 ]
179 manipulation = deleteFrom #tab (#col1 .== #col2) ReturningStar
179 manipulation = deleteFrom #tab (#col1 .== #col2) ReturningStar
180−in renderManipulation manipulation
180+in printSQL manipulation
181:}
181:}
182−"DELETE FROM \"tab\" WHERE (\"col1\" = \"col2\") RETURNING *"
182+DELETE FROM "tab" WHERE ("col1" = "col2") RETURNING *
183-}
183-}
184newtype Manipulation
184newtype Manipulation
185 (schema :: SchemaType)
185 (schema :: SchemaType)
@@ -188,6 +188,9 @@ newtype Manipulation
188 = UnsafeManipulation { renderManipulation :: ByteString }
188 = UnsafeManipulation { renderManipulation :: ByteString }
189 deriving (GHC.Generic,Show,Eq,Ord,NFData)
189 deriving (GHC.Generic,Show,Eq,Ord,NFData)
190
190
191+instance RenderSQL (Manipulation schema params columns) where
192+ renderSQL = renderManipulation
193+
191-- | Convert a `Query` into a `Manipulation`.
194-- | Convert a `Query` into a `Manipulation`.
192queryStatement
195queryStatement
193 :: Query schema params columns
196 :: Query schema params columns
@@ -483,9 +486,9 @@ WITH statements
483-- manipulation = with
486-- manipulation = with
484-- (deleteFrom #products (#date .< param @1) ReturningStar `As` #deleted_rows :* Nil)
487-- (deleteFrom #products (#date .< param @1) ReturningStar `As` #deleted_rows :* Nil)
485-- (insertQuery_ #products_deleted (selectStar (from (view (#deleted_rows `As` #t)))))
488-- (insertQuery_ #products_deleted (selectStar (from (view (#deleted_rows `As` #t)))))
486−-- in renderManipulation manipulation
489+-- in printSQL manipulation
487-- :}
490-- :}
488−-- "WITH \"deleted_rows\" AS (DELETE FROM \"products\" WHERE (\"date\" < ($1 :: date)) RETURNING *) INSERT INTO \"products_deleted\" SELECT * FROM \"deleted_rows\" AS \"t\""
491+-- WITH "deleted_rows" AS (DELETE FROM "products" WHERE ("date" < ($1 :: date)) RETURNING *) INSERT INTO "products_deleted" SELECT * FROM "deleted_rows" AS "t"
489with
492with
490 :: SOP.SListI commons
493 :: SOP.SListI commons
491 => NP (Aliased (Manipulation schema params)) commons
494 => NP (Aliased (Manipulation schema params)) commons