on dev help sign in
Add back "excluded" table

from #67

commitf6b8b5bdc05ec99b492074234d8072589adce832 authorEitan Chatav <eitan.chatav@gmail.com> dateTue 4 Dec 2018, 20:20 (8 years ago) parent4110e02
1 file changed+22−21
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+22−21
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+22−21
@@ -125,18 +125,19 @@ upsert:
125125>>> :{
126126let
127127 manipulation :: Manipulation
128− '[ "tab" ::: 'Table ('["uq" ::: 'Unique '["col1"]] :=>
129− '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
130− , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ])]
131− '[] '[ "sum" ::: 'NotNull 'PGint4]
128+ '[ "customers" ::: 'Table ('["uq" ::: 'Unique '["name"]] :=>
129+ '[ "name" ::: 'NoDef :=> 'NotNull 'PGtext
130+ , "email" ::: 'NoDef :=> 'NotNull 'PGtext ])]
131+ '[] '[]
132132 manipulation =
133− insertInto #tab
134− (Values (2 `as` #col1 :* 4 `as` #col2) [6 `as` #col1 :* 8 `as` #col2])
135− (OnConflict (OnConstraint #uq) (DoUpdate (2 `as` #col1) [#col1 .== #col2]))
136− (Returning ((#col1 + #col2) `as` #sum))
133+ insertInto #customers
134+ (Values_ ("John Smith" `as` #name :* "john@smith.com" `as` #email))
135+ (OnConflict (OnConstraint #uq)
136+ (DoUpdate (((#excluded ! #email) <> ";" <> (#customers ! #email)) `as` #email) []))
137+ (Returning Nil)
137138in printSQL manipulation
138139:}
139−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"
140+INSERT INTO "customers" ("name", "email") VALUES (E'John Smith', E'john@smith.com') ON CONFLICT ON CONSTRAINT "uq" DO UPDATE SET "email" = ("excluded"."email" || (E';' || "customers"."email"))
140141
141142query insert:
142143
@@ -271,7 +272,7 @@ insertInto
271272 , SOP.SListI result )
272273 => Alias tab
273274 -> QueryClause schema params columns
274− -> ConflictClause schema params table
275+ -> ConflictClause tab schema params table
275276 -> ReturningClause schema params row result
276277 -> Manipulation schema params result
277278insertInto tab qry conflict ret = UnsafeManipulation $
@@ -469,37 +470,37 @@ renderReturningClause = \case
469470-- `OnConflictDoNothing` simply avoids inserting a row.
470471-- `OnConflictDoUpdate` updates the existing row that conflicts with the row
471472-- proposed for insertion.
472−data ConflictClause schema params table where
473− OnConflictDoRaise :: ConflictClause schema params table
473+data ConflictClause tab schema params table where
474+ OnConflictDoRaise :: ConflictClause tab schema params table
474475 OnConflict
475476 :: ConflictTarget constraints
476− -> ConflictAction schema params columns
477− -> ConflictClause schema params (constraints :=> columns)
477+ -> ConflictAction tab schema params columns
478+ -> ConflictClause tab schema params (constraints :=> columns)
478479
479480-- | Render a `ConflictClause`.
480481renderConflictClause
481482 :: SOP.SListI (TableToColumns table)
482− => ConflictClause schema params table
483+ => ConflictClause tab schema params table
483484 -> ByteString
484485renderConflictClause = \case
485486 OnConflictDoRaise -> ""
486487 OnConflict target action -> " ON CONFLICT"
487488 <+> renderConflictTarget target <+> renderConflictAction action
488489
489−data ConflictAction schema params columns where
490− DoNothing :: ConflictAction schema params columns
490+data ConflictAction tab schema params columns where
491+ DoNothing :: ConflictAction tab schema params columns
491492 DoUpdate
492493 :: ( row ~ ColumnsToRow columns
493494 , SOP.SListI columns
494495 , columns ~ (col0 ': cols)
495496 , SOP.All (HasIn columns) subcolumns
496497 , AllUnique subcolumns )
497− => NP (ColumnExpression schema '[t ::: row] 'Ungrouped params) subcolumns
498− -> [Condition schema '[t ::: row] 'Ungrouped params]
499− -> ConflictAction schema params columns
498+ => NP (ColumnExpression schema '[tab ::: row, "excluded" ::: row] 'Ungrouped params) subcolumns
499+ -> [Condition schema '[tab ::: row, "excluded" ::: row] 'Ungrouped params]
500+ -> ConflictAction tab schema params columns
500501
501502renderConflictAction
502− :: ConflictAction schema params columns
503+ :: ConflictAction tab schema params columns
503504 -> ByteString
504505renderConflictAction = \case
505506 DoNothing -> "DO NOTHING"