on dev help sign in
fix tests
commit74587afb29a41e9533256c56576c4c1545459adf authorEitan Chatav <eitan.chatav@gmail.com> dateTue 4 Dec 2018, 10:57 (8 years ago) parent42b84ff
3 files changed+26−34
squeal-postgresql/src/Squeal/PostgreSQL.hs+9−10 squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+15−22 squeal-postgresql/test/Specs/ExceptionHandling.hs+2−2
squeal-postgresql/src/Squeal/PostgreSQL.hs+9−10
@@ -118,25 +118,24 @@ of our inserts.
118118>>> :{
119119let
120120 insertUser :: Manipulation Schema '[ 'NotNull 'PGtext ] '[ "fromOnly" ::: 'NotNull 'PGint4 ]
121− insertUser = insertRow #users
122− (Default `as` #id :* Set (param @1) `as` #name)
123− OnConflictDoNothing (Returning (#id `as` #fromOnly))
121+ insertUser = insertInto #users
122+ (Values_ (defaultAs #id :* param @1 `as` #name))
123+ (OnConflict (OnConstraint #pk_users) DoNothing) (Returning (#id `as` #fromOnly))
124124:}
125125
126126>>> :{
127127let
128128 insertEmail :: Manipulation Schema '[ 'NotNull 'PGint4, 'Null 'PGtext] '[]
129− insertEmail = insertRow #emails
130− ( Default `as` #id :*
131− Set (param @1) `as` #user_id :*
132− Set (param @2) `as` #email )
133− OnConflictDoNothing (Returning Nil)
129+ insertEmail = insertInto #emails
130+ (Values_ (defaultAs #id :* param @1 `as` #user_id :* param @2 `as` #email))
131+ (OnConflict (OnConstraint #pk_emails) DoNothing) (Returning Nil)
134132:}
135133
136134>>> printSQL insertUser
137−INSERT INTO "users" ("id", "name") VALUES (DEFAULT, ($1 :: text)) ON CONFLICT DO NOTHING RETURNING "id" AS "fromOnly"
135+INSERT INTO "users" ("id", "name") VALUES (DEFAULT, ($1 :: text)) ON CONFLICT ON CONSTRAINT "pk_users" DO NOTHING RETURNING "id" AS "fromOnly"
136+
138137>>> printSQL insertEmail
139−INSERT INTO "emails" ("id", "user_id", "email") VALUES (DEFAULT, ($1 :: int4), ($2 :: text)) ON CONFLICT DO NOTHING
138+INSERT INTO "emails" ("id", "user_id", "email") VALUES (DEFAULT, ($1 :: int4), ($2 :: text)) ON CONFLICT ON CONSTRAINT "pk_emails" DO NOTHING
140139
141140Next we write a `Query` to retrieve users from the database. We're not
142141interested in the ids here, just the usernames and email addresses. We
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+15−22
@@ -82,7 +82,7 @@ let
8282 '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
8383 , "col2" ::: 'Def :=> 'NotNull 'PGint4 ])] '[] '[]
8484 manipulation =
85− insertRow_ #tab (Set 2 `as` #col1 :* Default `as` #col2)
85+ insertInto_ #tab (Values_ (2 `as` #col1 :* defaultAs #col2))
8686in printSQL manipulation
8787:}
8888INSERT INTO "tab" ("col1", "col2") VALUES (2, DEFAULT)
@@ -97,8 +97,7 @@ let
9797 , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ])]
9898 '[ 'NotNull 'PGint4, 'NotNull 'PGint4 ] '[]
9999 manipulation =
100− insertRow_ #tab
101− (Set (param @1) `as` #col1 :* Set (param @2) `as` #col2)
100+ insertInto_ #tab (Values_ (param @1 `as` #col1 :* param @2 `as` #col2))
102101in printSQL manipulation
103102:}
104103INSERT INTO "tab" ("col1", "col2") VALUES (($1 :: int4), ($2 :: int4))
@@ -113,7 +112,7 @@ let
113112 , "col2" ::: 'Def :=> 'NotNull 'PGint4 ])] '[]
114113 '["fromOnly" ::: 'NotNull 'PGint4]
115114 manipulation =
116− insertRow #tab (Set 2 `as` #col1 :* Default `as` #col2)
115+ insertInto #tab (Values_ (2 `as` #col1 :* defaultAs #col2))
117116 OnConflictDoRaise (Returning (#col1 `as` #fromOnly))
118117in printSQL manipulation
119118:}
@@ -124,22 +123,18 @@ upsert:
124123>>> :{
125124let
126125 manipulation :: Manipulation
127− '[ "tab" ::: 'Table ('[] :=>
126+ '[ "tab" ::: 'Table ('["uq" ::: 'Unique '["col1"]] :=>
128127 '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
129128 , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ])]
130129 '[] '[ "sum" ::: 'NotNull 'PGint4]
131130 manipulation =
132− insertRows #tab
133− (Set 2 `as` #col1 :* Set 4 `as` #col2)
134− [Set 6 `as` #col1 :* Set 8 `as` #col2]
135− (OnConflictDoUpdate
136− (UniqueIndexOn #col1)
137− (Set 2 `as` #col1)
138− [#existing ! #col1 .== #excluded ! #col2])
139− (Returning $ (#col1 + #col2) `as` #sum)
131+ insertInto #tab
132+ (Values (2 `as` #col1 :* 4 `as` #col2) [6 `as` #col1 :* 8 `as` #col2])
133+ (OnConflict (OnConstraint #uq) (DoUpdate (2 `as` #col1) [#col1 .== #col2]))
134+ (Returning ((#col1 + #col2) `as` #sum))
140135in printSQL manipulation
141136:}
142−INSERT INTO "tab" AS existing ("col1", "col2") VALUES (2, 4), (6, 8) ON CONFLICT ("col1") DO UPDATE SET "col1" = 2 WHERE ("existing"."col1" = "excluded"."col2") RETURNING ("col1" + "col2") AS "sum"
137+INSERT INTO "tab" ("col1", "col2") VALUES (2, 4), (6, 8) ON CONFLICT ON CONSTRAINT "uq" DO UPDATE SET "col1" = 2 WHERE ("col1" = "col2") RETURNING ("col1" + "col2") AS "sum"
143138
144139query insert:
145140
@@ -156,8 +151,8 @@ let
156151 ])
157152 ] '[] '[]
158153 manipulation =
159− insertQuery_ #tab
160− (selectStar (from (table (#other_tab `as` #t))))
154+ insertInto_ #tab
155+ (Subquery (selectStar (from (table (#other_tab `as` #t)))))
161156in printSQL manipulation
162157:}
163158INSERT INTO "tab" SELECT * FROM "other_tab" AS "t"
@@ -170,9 +165,7 @@ let
170165 '[ "tab" ::: 'Table ('[] :=>
171166 '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
172167 , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ])] '[] '[]
173− manipulation =
174− update_ #tab (Set 2 `as` #col1)
175− (#col1 ./= #col2)
168+ manipulation = update_ #tab (2 `as` #col1) (#col1 ./= #col2)
176169in printSQL manipulation
177170:}
178171UPDATE "tab" SET "col1" = 2 WHERE ("col1" <> "col2")
@@ -230,11 +223,11 @@ let
230223 , "products_deleted" ::: 'Table ProductsTable
231224 ] '[ 'NotNull 'PGdate] '[]
232225 manipulation = with
233− (deleteFrom #products NoUsing (#date .< param @1) ReturningStar `as` #deleted_rows)
234− (insertQuery_ #products_deleted (selectStar (from (view (#deleted_rows `as` #t)))))
226+ (deleteFrom #products NoUsing (#date .< param @1) ReturningStar `as` #del)
227+ (insertInto_ #products_deleted (Subquery (selectStar (from (view #del)))))
235228in printSQL manipulation
236229:}
237−WITH "deleted_rows" AS (DELETE FROM "products" WHERE ("date" < ($1 :: date)) RETURNING *) INSERT INTO "products_deleted" SELECT * FROM "deleted_rows" AS "t"
230+WITH "del" AS (DELETE FROM "products" WHERE ("date" < ($1 :: date)) RETURNING *) INSERT INTO "products_deleted" SELECT * FROM "del" AS "del"
238231-}
239232
240233newtype Manipulation
squeal-postgresql/test/Specs/ExceptionHandling.hs+2−2
@@ -55,8 +55,8 @@ instance SOP.HasDatatypeInfo User
5555
5656insertUser :: Manipulation Schema '[ 'NotNull 'PGtext, 'NotNull ('PGvararray ('Null 'PGint2))]
5757 '[ "fromOnly" ::: 'NotNull 'PGint4 ]
58−insertUser = insertRows #users
59− (Default `as` #id :* Set (param @1) `as` #name :* Set (param @2) `as` #vec) []
58+insertUser = insertInto #users
59+ (Values_ (defaultAs #id :* param @1 `as` #name :* param @2 `as` #vec))
6060 OnConflictDoRaise (Returning (#id `as` #fromOnly))
6161
6262setup :: Definition '[] Schema