JetBrains / Exposed

Kotlin SQL Framework
http://jetbrains.github.io/Exposed/
Apache License 2.0
8.05k stars 674 forks source link

feat: EXPOSED-334 Support MERGE statement #2047

Closed obabichevjb closed 1 month ago

obabichevjb commented 2 months ago

This PR introduces a new mergeFrom method query method, allowing execute underlying sql merge command. The merge method supports merging data from one table or select query into another table, with conditional logic for matched and unmatched rows.

The method can be used in the following way:

Dest.mergeFrom(Source, on = { Source.key eq Dest.key() }) {
  whenNotMatchedInsert {
      it[Dest.key] = Source.key
      it[Dest.value] = Source.value
  }

  whenMatchedDelete(and = (Source.value eq 1))

  whenMatchedUpdate(and = (Source.value eq 1)) {
      it[Dest.key] = Source.key
      it[Dest.value] = (Dest.value + Source.value) * 10
  }
}

Merge command is supported by the databases: SQL Server, Postgres, Oracle, H2

e5l commented 1 month ago

Please update the PR description

obabichevjb commented 1 month ago

Please update the PR description

Updated