Upsert DSL extension for Exposed, Kotlin SQL framework. Project bases on various solutions provided by community in the official "Exposed: Support upsert functionality" feature request. After 4 years, maintainers still didn't provide a solution, so here's a straightforward alternative.
Supported databases with tests run against real databases using Testcontainers:
DB | Status |
---|---|
H2 | Unsupported |
H2 (MySQL Dialect) | ✅ |
MySQL | ✅ |
MariaDB | ✅ |
Oracle | Not implemented (Licensed to enterprise) |
PostgreSQL | ✅ |
SQL Server | Unsupported |
SQLite | ✅ |
class StatisticsTable : Table("statistics") {
// [...]
val uniqueTypeValue = withUnique("unique_http_method_to_uri", httpMethod, uri)
}
StatisticsTable.upsert(conflictIndex = StatisticsTable.uniqueTypeValue,
insertBody = {
it[this.httpMethod] = record.httpMethod
it[this.uri] = record.uri
it[this.count] = record.count
},
updateBody = {
with(SqlExpressionBuilder) {
it.update(StatisticsTable.count, StatisticsTable.count + record.count)
}
}
)
Notes
ON DUPLICATE KEY
query. dependencies {
implementation("net.dzikoysk:exposed-upsert:1.2.1")
}
You can find all available versions in the repository: