Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support MERGE statements from the ANSI Standard #499

Open
joshuamzm opened this issue Aug 10, 2023 · 2 comments
Open

Support MERGE statements from the ANSI Standard #499

joshuamzm opened this issue Aug 10, 2023 · 2 comments
Assignees
Labels
enhancement needs analysis I need to think about this!

Comments

@joshuamzm
Copy link

Reference: https://en.wikipedia.org/wiki/Merge_%28SQL%29

Conversation: https://clojurians.slack.com/archives/C66EM8D5H/p1691702334368519

@seancorfield
Copy link
Owner

:merge-into would be a strange hybrid that would take [:table {:using ...} case-clauses] as arguments, and we'd need [:matched] as special syntax to make this work I think... or perhaps overlay :case / :else logic? I need to think about this some more.

@seancorfield
Copy link
Owner

seancorfield commented Aug 12, 2023

It can't overlay :case / :else even tho' it has (on the surface) a similar structure: the update / insert / delete clauses it supports are only partial statements (because merge into implies the table).

The optional case predicates make this even harder, or at least more irregular as a DSL.

The :case logic could be mapped onto this, sort of, since matched clauses are like case clauses except that the condition is optional and, if present, merges onto when matched and (otherwise just when matched) followed by a DSL map assumed for :set or {:delete nil} (not many options for that) and the :else condition could map onto when not matched but would have an optional condition (again and <predicate>) followed by a DSL map assumed for :columns (optional) and :values.

{:merge-into [:table [:source [:= :table.id :source.id]] :foo {:set {:a 1}} {:set {:b 1}} :else {:values [{:a 2 :b 2}]}]}
=>
MERGE INTO table USING source ON table.id = source.id
  WHEN MATCHED AND foo THEN UPDATE SET a = 1
  WHEN MATCHED THEN UPDATE SET b = 1
  WHEN NOT MATCHED THEN INSERT (a, b) VALUES (1, 1)

Something like that...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement needs analysis I need to think about this!
Projects
None yet
Development

No branches or pull requests

2 participants