on dev help sign in
remove `;` from manipulations

for composability of manipulations e.g. in WITH statements

commit6d0dd7eb86cf624548cdcefb00fc40cb90db8371 authorEitan Chatav <eitan.chatav@gmail.com> dateTue 29 May 2018, 10:37 (8 years ago) parentbd505af
3 files changed+17−22
squeal-postgresql/src/Squeal/PostgreSQL.hs+2−2 squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+13−19 squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+2−1
squeal-postgresql/src/Squeal/PostgreSQL.hs+2−2
@@ -127,9 +127,9 @@
127127-- :}
128128--
129129-- >>> renderManipulation insertUser
130−-- "INSERT INTO \"users\" (\"id\", \"name\") VALUES (DEFAULT, ($1 :: text)) ON CONFLICT DO NOTHING RETURNING \"id\" AS \"fromOnly\";"
130+-- "INSERT INTO \"users\" (\"id\", \"name\") VALUES (DEFAULT, ($1 :: text)) ON CONFLICT DO NOTHING RETURNING \"id\" AS \"fromOnly\""
131131-- >>> renderManipulation insertEmail
132−-- "INSERT INTO \"emails\" (\"id\", \"user_id\", \"email\") VALUES (DEFAULT, ($1 :: int4), ($2 :: text)) ON CONFLICT DO NOTHING;"
132+-- "INSERT INTO \"emails\" (\"id\", \"user_id\", \"email\") VALUES (DEFAULT, ($1 :: int4), ($2 :: text)) ON CONFLICT DO NOTHING"
133133--
134134-- Next we write a `Query` to retrieve users from the database. We're not
135135-- interested in the ids here, just the usernames and email addresses. We
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+13−19
@@ -50,7 +50,6 @@ import Control.DeepSeq
5050import Data.ByteString hiding (foldr)
5151import Data.Monoid
5252
53−import qualified Data.ByteString as ByteString
5453import qualified Generics.SOP as SOP
5554import qualified GHC.Generics as GHC
5655
@@ -76,7 +75,7 @@ let
7675 insertRow_ #tab (Set 2 `As` #col1 :* Default `As` #col2 :* Nil)
7776in renderManipulation manipulation
7877:}
79−"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (2, DEFAULT);"
78+"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (2, DEFAULT)"
8079
8180parameterized insert:
8281
@@ -92,7 +91,7 @@ let
9291 (Set (param @1) `As` #col1 :* Set (param @2) `As` #col2 :* Nil)
9392in renderManipulation manipulation
9493:}
95−"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (($1 :: int4), ($2 :: int4));"
94+"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (($1 :: int4), ($2 :: int4))"
9695
9796returning insert:
9897
@@ -108,7 +107,7 @@ let
108107 OnConflictDoRaise (Returning (#col1 `As` #fromOnly :* Nil))
109108in renderManipulation manipulation
110109:}
111−"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\""
112111
113112upsert:
114113
@@ -129,7 +128,7 @@ let
129128 (Returning $ (#col1 + #col2) `As` #sum :* Nil)
130129in renderManipulation manipulation
131130:}
132−"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\""
133132
134133query insert:
135134
@@ -150,7 +149,7 @@ let
150149 (selectStar (from (table (#other_tab `As` #t))))
151150in renderManipulation manipulation
152151:}
153−"INSERT INTO \"tab\" SELECT * FROM \"other_tab\" AS \"t\";"
152+"INSERT INTO \"tab\" SELECT * FROM \"other_tab\" AS \"t\""
154153
155154update:
156155
@@ -165,7 +164,7 @@ let
165164 (#col1 ./= #col2)
166165in renderManipulation manipulation
167166:}
168−"UPDATE \"tab\" SET \"col1\" = 2 WHERE (\"col1\" <> \"col2\");"
167+"UPDATE \"tab\" SET \"col1\" = 2 WHERE (\"col1\" <> \"col2\")"
169168
170169delete:
171170
@@ -180,7 +179,7 @@ let
180179 manipulation = deleteFrom #tab (#col1 .== #col2) ReturningStar
181180in renderManipulation manipulation
182181:}
183−"DELETE FROM \"tab\" WHERE (\"col1\" = \"col2\") RETURNING *;"
182+"DELETE FROM \"tab\" WHERE (\"col1\" = \"col2\") RETURNING *"
184183-}
185184newtype Manipulation
186185 (schema :: SchemaType)
@@ -193,7 +192,7 @@ newtype Manipulation
193192queryStatement
194193 :: Query schema params columns
195194 -> Manipulation schema params columns
196−queryStatement q = UnsafeManipulation $ renderQuery q <> ";"
195+queryStatement q = UnsafeManipulation $ renderQuery q
197196
198197{-----------------------------------------
199198INSERT statements
@@ -345,10 +344,10 @@ renderReturningClause
345344 => ReturningClause params columns results
346345 -> ByteString
347346renderReturningClause = \case
348− ReturningStar -> " RETURNING *;"
349− Returning Nil -> ";"
347+ ReturningStar -> " RETURNING *"
348+ Returning Nil -> ""
350349 Returning results -> " RETURNING"
351− <+> renderCommaSeparated (renderAliasedAs renderExpression) results <> ";"
350+ <+> renderCommaSeparated (renderAliasedAs renderExpression) results
352351
353352-- | A `ConflictClause` specifies an action to perform upon a constraint
354353-- violation. `OnConflictDoRaise` will raise an error.
@@ -484,7 +483,7 @@ WITH statements
484483-- (insertQuery_ #products_deleted (selectStar (from (view (#deleted_rows `As` #t)))))
485484-- in renderManipulation manipulation
486485-- :}
487−-- "WITH \"deleted_rows\" AS (DELETE FROM \"products\" WHERE (\"date\" < ($1 :: date)) RETURNING *) INSERT INTO \"products_deleted\" SELECT * FROM \"deleted_rows\" AS \"t\";"
486+-- "WITH \"deleted_rows\" AS (DELETE FROM \"products\" WHERE (\"date\" < ($1 :: date)) RETURNING *) INSERT INTO \"products_deleted\" SELECT * FROM \"deleted_rows\" AS \"t\""
488487with
489488 :: SOP.SListI commons
490489 => NP (Aliased (Manipulation schema params)) commons
@@ -500,9 +499,4 @@ with commons manipulation = UnsafeManipulation $
500499 -> ByteString
501500 renderCommon (common `As` alias) =
502501 renderAlias alias <+> "AS" <+>
503− let
504− str = renderManipulation common
505− len = ByteString.length str
506− str' = ByteString.take (len - 1) str -- remove ';'
507− in
508− parenthesized str'
502+ parenthesized (renderManipulation common)
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+2−1
@@ -395,7 +395,8 @@ instance (MonadBase IO io, schema0 ~ schema, schema1 ~ schema)
395395 let
396396 toParam' bytes = (LibPQ.invalidOid,bytes,LibPQ.Binary)
397397 params' = fmap (fmap toParam') (hcollapse (toParams @x @ps params))
398− resultMaybe <- liftBase $ LibPQ.execParams conn q params' LibPQ.Binary
398+ q' = q <> ";"
399+ resultMaybe <- liftBase $ LibPQ.execParams conn q' params' LibPQ.Binary
399400 case resultMaybe of
400401 Nothing -> error
401402 "manipulateParams: LibPQ.execParams returned no results"