on dev help sign in
remove `;` from manipulations
6d0dd7eEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL.hs+2−2
@@ -127,9 +127,9 @@
127-- :}
127-- :}
128--
128--
129-- >>> renderManipulation insertUser
129-- >>> 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\""
131-- >>> renderManipulation insertEmail
131-- >>> 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"
133--
133--
134-- Next we write a `Query` to retrieve users from the database. We're not
134-- Next we write a `Query` to retrieve users from the database. We're not
135-- interested in the ids here, just the usernames and email addresses. We
135-- 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
50import Data.ByteString hiding (foldr)
50import Data.ByteString hiding (foldr)
51import Data.Monoid
51import Data.Monoid
52
52
53−import qualified Data.ByteString as ByteString
54import qualified Generics.SOP as SOP
53import qualified Generics.SOP as SOP
55import qualified GHC.Generics as GHC
54import qualified GHC.Generics as GHC
56
55
@@ -76,7 +75,7 @@ let
76 insertRow_ #tab (Set 2 `As` #col1 :* Default `As` #col2 :* Nil)
75 insertRow_ #tab (Set 2 `As` #col1 :* Default `As` #col2 :* Nil)
77in renderManipulation manipulation
76in renderManipulation manipulation
78:}
77:}
79−"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (2, DEFAULT);"
78+"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (2, DEFAULT)"
80
79
81parameterized insert:
80parameterized insert:
82
81
@@ -92,7 +91,7 @@ let
92 (Set (param @1) `As` #col1 :* Set (param @2) `As` #col2 :* Nil)
91 (Set (param @1) `As` #col1 :* Set (param @2) `As` #col2 :* Nil)
93in renderManipulation manipulation
92in renderManipulation manipulation
94:}
93:}
95−"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (($1 :: int4), ($2 :: int4));"
94+"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (($1 :: int4), ($2 :: int4))"
96
95
97returning insert:
96returning insert:
98
97
@@ -108,7 +107,7 @@ let
108 OnConflictDoRaise (Returning (#col1 `As` #fromOnly :* Nil))
107 OnConflictDoRaise (Returning (#col1 `As` #fromOnly :* Nil))
109in renderManipulation manipulation
108in renderManipulation manipulation
110:}
109:}
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\""
112
111
113upsert:
112upsert:
114
113
@@ -129,7 +128,7 @@ let
129 (Returning $ (#col1 + #col2) `As` #sum :* Nil)
128 (Returning $ (#col1 + #col2) `As` #sum :* Nil)
130in renderManipulation manipulation
129in renderManipulation manipulation
131:}
130:}
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\""
133
132
134query insert:
133query insert:
135
134
@@ -150,7 +149,7 @@ let
150 (selectStar (from (table (#other_tab `As` #t))))
149 (selectStar (from (table (#other_tab `As` #t))))
151in renderManipulation manipulation
150in renderManipulation manipulation
152:}
151:}
153−"INSERT INTO \"tab\" SELECT * FROM \"other_tab\" AS \"t\";"
152+"INSERT INTO \"tab\" SELECT * FROM \"other_tab\" AS \"t\""
154
153
155update:
154update:
156
155
@@ -165,7 +164,7 @@ let
165 (#col1 ./= #col2)
164 (#col1 ./= #col2)
166in renderManipulation manipulation
165in renderManipulation manipulation
167:}
166:}
168−"UPDATE \"tab\" SET \"col1\" = 2 WHERE (\"col1\" <> \"col2\");"
167+"UPDATE \"tab\" SET \"col1\" = 2 WHERE (\"col1\" <> \"col2\")"
169
168
170delete:
169delete:
171
170
@@ -180,7 +179,7 @@ let
180 manipulation = deleteFrom #tab (#col1 .== #col2) ReturningStar
179 manipulation = deleteFrom #tab (#col1 .== #col2) ReturningStar
181in renderManipulation manipulation
180in renderManipulation manipulation
182:}
181:}
183−"DELETE FROM \"tab\" WHERE (\"col1\" = \"col2\") RETURNING *;"
182+"DELETE FROM \"tab\" WHERE (\"col1\" = \"col2\") RETURNING *"
184-}
183-}
185newtype Manipulation
184newtype Manipulation
186 (schema :: SchemaType)
185 (schema :: SchemaType)
@@ -193,7 +192,7 @@ newtype Manipulation
193queryStatement
192queryStatement
194 :: Query schema params columns
193 :: Query schema params columns
195 -> Manipulation schema params columns
194 -> Manipulation schema params columns
196−queryStatement q = UnsafeManipulation $ renderQuery q <> ";"
195+queryStatement q = UnsafeManipulation $ renderQuery q
197
196
198{-----------------------------------------
197{-----------------------------------------
199INSERT statements
198INSERT statements
@@ -345,10 +344,10 @@ renderReturningClause
345 => ReturningClause params columns results
344 => ReturningClause params columns results
346 -> ByteString
345 -> ByteString
347renderReturningClause = \case
346renderReturningClause = \case
348− ReturningStar -> " RETURNING *;"
347+ ReturningStar -> " RETURNING *"
349− Returning Nil -> ";"
348+ Returning Nil -> ""
350 Returning results -> " RETURNING"
349 Returning results -> " RETURNING"
351− <+> renderCommaSeparated (renderAliasedAs renderExpression) results <> ";"
350+ <+> renderCommaSeparated (renderAliasedAs renderExpression) results
352
351
353-- | A `ConflictClause` specifies an action to perform upon a constraint
352-- | A `ConflictClause` specifies an action to perform upon a constraint
354-- violation. `OnConflictDoRaise` will raise an error.
353-- violation. `OnConflictDoRaise` will raise an error.
@@ -484,7 +483,7 @@ WITH statements
484-- (insertQuery_ #products_deleted (selectStar (from (view (#deleted_rows `As` #t)))))
483-- (insertQuery_ #products_deleted (selectStar (from (view (#deleted_rows `As` #t)))))
485-- in renderManipulation manipulation
484-- in renderManipulation manipulation
486-- :}
485-- :}
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\""
488with
487with
489 :: SOP.SListI commons
488 :: SOP.SListI commons
490 => NP (Aliased (Manipulation schema params)) commons
489 => NP (Aliased (Manipulation schema params)) commons
@@ -500,9 +499,4 @@ with commons manipulation = UnsafeManipulation $
500 -> ByteString
499 -> ByteString
501 renderCommon (common `As` alias) =
500 renderCommon (common `As` alias) =
502 renderAlias alias <+> "AS" <+>
501 renderAlias alias <+> "AS" <+>
503− let
504− str = renderManipulation common
502+ parenthesized (renderManipulation common)
505− len = ByteString.length str
506− str' = ByteString.take (len - 1) str -- remove ';'
507− in
508− parenthesized str'
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+2−1
@@ -395,7 +395,8 @@ instance (MonadBase IO io, schema0 ~ schema, schema1 ~ schema)
395 let
395 let
396 toParam' bytes = (LibPQ.invalidOid,bytes,LibPQ.Binary)
396 toParam' bytes = (LibPQ.invalidOid,bytes,LibPQ.Binary)
397 params' = fmap (fmap toParam') (hcollapse (toParams @x @ps params))
397 params' = fmap (fmap toParam') (hcollapse (toParams @x @ps params))
398+ q' = q <> ";"
398− resultMaybe <- liftBase $ LibPQ.execParams conn q params' LibPQ.Binary
399+ resultMaybe <- liftBase $ LibPQ.execParams conn q' params' LibPQ.Binary
399 case resultMaybe of
400 case resultMaybe of
400 Nothing -> error
401 Nothing -> error
401 "manipulateParams: LibPQ.execParams returned no results"
402 "manipulateParams: LibPQ.execParams returned no results"