on dev help sign in
fix tests
74587afEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL.hs+9−10
@@ -118,25 +118,24 @@ of our inserts.
118>>> :{
118>>> :{
119let
119let
120 insertUser :: Manipulation Schema '[ 'NotNull 'PGtext ] '[ "fromOnly" ::: 'NotNull 'PGint4 ]
120 insertUser :: Manipulation Schema '[ 'NotNull 'PGtext ] '[ "fromOnly" ::: 'NotNull 'PGint4 ]
121− insertUser = insertRow #users
121+ insertUser = insertInto #users
122− (Default `as` #id :* Set (param @1) `as` #name)
122+ (Values_ (defaultAs #id :* param @1 `as` #name))
123− OnConflictDoNothing (Returning (#id `as` #fromOnly))
123+ (OnConflict (OnConstraint #pk_users) DoNothing) (Returning (#id `as` #fromOnly))
124:}
124:}
125
125
126>>> :{
126>>> :{
127let
127let
128 insertEmail :: Manipulation Schema '[ 'NotNull 'PGint4, 'Null 'PGtext] '[]
128 insertEmail :: Manipulation Schema '[ 'NotNull 'PGint4, 'Null 'PGtext] '[]
129− insertEmail = insertRow #emails
129+ insertEmail = insertInto #emails
130− ( Default `as` #id :*
131− Set (param @1) `as` #user_id :*
130+ (Values_ (defaultAs #id :* param @1 `as` #user_id :* param @2 `as` #email))
132− Set (param @2) `as` #email )
131+ (OnConflict (OnConstraint #pk_emails) DoNothing) (Returning Nil)
133− OnConflictDoNothing (Returning Nil)
134:}
132:}
135
133
136>>> printSQL insertUser
134>>> 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+
138>>> printSQL insertEmail
137>>> 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
140
139
141Next we write a `Query` to retrieve users from the database. We're not
140Next we write a `Query` to retrieve users from the database. We're not
142interested in the ids here, just the usernames and email addresses. We
141interested 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
82 '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
82 '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
83 , "col2" ::: 'Def :=> 'NotNull 'PGint4 ])] '[] '[]
83 , "col2" ::: 'Def :=> 'NotNull 'PGint4 ])] '[] '[]
84 manipulation =
84 manipulation =
85− insertRow_ #tab (Set 2 `as` #col1 :* Default `as` #col2)
85+ insertInto_ #tab (Values_ (2 `as` #col1 :* defaultAs #col2))
86in printSQL manipulation
86in printSQL manipulation
87:}
87:}
88INSERT INTO "tab" ("col1", "col2") VALUES (2, DEFAULT)
88INSERT INTO "tab" ("col1", "col2") VALUES (2, DEFAULT)
@@ -97,8 +97,7 @@ let
97 , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ])]
97 , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ])]
98 '[ 'NotNull 'PGint4, 'NotNull 'PGint4 ] '[]
98 '[ 'NotNull 'PGint4, 'NotNull 'PGint4 ] '[]
99 manipulation =
99 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))
102in printSQL manipulation
101in printSQL manipulation
103:}
102:}
104INSERT INTO "tab" ("col1", "col2") VALUES (($1 :: int4), ($2 :: int4))
103INSERT INTO "tab" ("col1", "col2") VALUES (($1 :: int4), ($2 :: int4))
@@ -113,7 +112,7 @@ let
113 , "col2" ::: 'Def :=> 'NotNull 'PGint4 ])] '[]
112 , "col2" ::: 'Def :=> 'NotNull 'PGint4 ])] '[]
114 '["fromOnly" ::: 'NotNull 'PGint4]
113 '["fromOnly" ::: 'NotNull 'PGint4]
115 manipulation =
114 manipulation =
116− insertRow #tab (Set 2 `as` #col1 :* Default `as` #col2)
115+ insertInto #tab (Values_ (2 `as` #col1 :* defaultAs #col2))
117 OnConflictDoRaise (Returning (#col1 `as` #fromOnly))
116 OnConflictDoRaise (Returning (#col1 `as` #fromOnly))
118in printSQL manipulation
117in printSQL manipulation
119:}
118:}
@@ -124,22 +123,18 @@ upsert:
124>>> :{
123>>> :{
125let
124let
126 manipulation :: Manipulation
125 manipulation :: Manipulation
127− '[ "tab" ::: 'Table ('[] :=>
126+ '[ "tab" ::: 'Table ('["uq" ::: 'Unique '["col1"]] :=>
128 '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
127 '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
129 , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ])]
128 , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ])]
130 '[] '[ "sum" ::: 'NotNull 'PGint4]
129 '[] '[ "sum" ::: 'NotNull 'PGint4]
131 manipulation =
130 manipulation =
132− insertRows #tab
131+ insertInto #tab
133− (Set 2 `as` #col1 :* Set 4 `as` #col2)
132+ (Values (2 `as` #col1 :* 4 `as` #col2) [6 `as` #col1 :* 8 `as` #col2])
134− [Set 6 `as` #col1 :* Set 8 `as` #col2]
133+ (OnConflict (OnConstraint #uq) (DoUpdate (2 `as` #col1) [#col1 .== #col2]))
135− (OnConflictDoUpdate
136− (UniqueIndexOn #col1)
137− (Set 2 `as` #col1)
138− [#existing ! #col1 .== #excluded ! #col2])
139− (Returning $ (#col1 + #col2) `as` #sum)
134+ (Returning ((#col1 + #col2) `as` #sum))
140in printSQL manipulation
135in printSQL manipulation
141:}
136:}
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"
143
138
144query insert:
139query insert:
145
140
@@ -156,8 +151,8 @@ let
156 ])
151 ])
157 ] '[] '[]
152 ] '[] '[]
158 manipulation =
153 manipulation =
159− insertQuery_ #tab
154+ insertInto_ #tab
160− (selectStar (from (table (#other_tab `as` #t))))
155+ (Subquery (selectStar (from (table (#other_tab `as` #t)))))
161in printSQL manipulation
156in printSQL manipulation
162:}
157:}
163INSERT INTO "tab" SELECT * FROM "other_tab" AS "t"
158INSERT INTO "tab" SELECT * FROM "other_tab" AS "t"
@@ -170,9 +165,7 @@ let
170 '[ "tab" ::: 'Table ('[] :=>
165 '[ "tab" ::: 'Table ('[] :=>
171 '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
166 '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
172 , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ])] '[] '[]
167 , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ])] '[] '[]
173− manipulation =
174− update_ #tab (Set 2 `as` #col1)
168+ manipulation = update_ #tab (2 `as` #col1) (#col1 ./= #col2)
175− (#col1 ./= #col2)
176in printSQL manipulation
169in printSQL manipulation
177:}
170:}
178UPDATE "tab" SET "col1" = 2 WHERE ("col1" <> "col2")
171UPDATE "tab" SET "col1" = 2 WHERE ("col1" <> "col2")
@@ -230,11 +223,11 @@ let
230 , "products_deleted" ::: 'Table ProductsTable
223 , "products_deleted" ::: 'Table ProductsTable
231 ] '[ 'NotNull 'PGdate] '[]
224 ] '[ 'NotNull 'PGdate] '[]
232 manipulation = with
225 manipulation = with
233− (deleteFrom #products NoUsing (#date .< param @1) ReturningStar `as` #deleted_rows)
226+ (deleteFrom #products NoUsing (#date .< param @1) ReturningStar `as` #del)
234− (insertQuery_ #products_deleted (selectStar (from (view (#deleted_rows `as` #t)))))
227+ (insertInto_ #products_deleted (Subquery (selectStar (from (view #del)))))
235in printSQL manipulation
228in printSQL manipulation
236:}
229:}
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"
238-}
231-}
239
232
240newtype Manipulation
233newtype Manipulation
squeal-postgresql/test/Specs/ExceptionHandling.hs+2−2
@@ -55,8 +55,8 @@ instance SOP.HasDatatypeInfo User
55
55
56insertUser :: Manipulation Schema '[ 'NotNull 'PGtext, 'NotNull ('PGvararray ('Null 'PGint2))]
56insertUser :: Manipulation Schema '[ 'NotNull 'PGtext, 'NotNull ('PGvararray ('Null 'PGint2))]
57 '[ "fromOnly" ::: 'NotNull 'PGint4 ]
57 '[ "fromOnly" ::: 'NotNull 'PGint4 ]
58−insertUser = insertRows #users
58+insertUser = insertInto #users
59− (Default `as` #id :* Set (param @1) `as` #name :* Set (param @2) `as` #vec) []
59+ (Values_ (defaultAs #id :* param @1 `as` #name :* param @2 `as` #vec))
60 OnConflictDoRaise (Returning (#id `as` #fromOnly))
60 OnConflictDoRaise (Returning (#id `as` #fromOnly))
61
61
62setup :: Definition '[] Schema
62setup :: Definition '[] Schema