Skip to content

Commit

Permalink
feat: [MariaDB] Support RETURNING clause (#2330)
Browse files Browse the repository at this point in the history
* feat: implement returning clause for MariaDB

* fix: clean ReturningTests

* fix: docs update
  • Loading branch information
devgor88 authored Dec 10, 2024
1 parent 6bbceb4 commit aaa219b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@
<p>Supported on: PostgreSQL and SQLite</p>
</tldr>
<p>Some databases allow the return of additional data every time a row is either inserted, updated, or deleted.
Please note that MariaDB only allows the return of this data for insertions and deletions.
This can be accomplished by using one of the following functions:
</p>
<list>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ internal object MariaDBFunctionProvider : MysqlFunctionProvider() {
sql
}
}

override fun returning(
mainSql: String,
returning: List<Expression<*>>,
transaction: Transaction
): String {
return with(QueryBuilder(true)) {
+"$mainSql RETURNING "
returning.appendTo { +it }
toString()
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import kotlin.test.assertIs
import kotlin.test.assertTrue

class ReturningTests : DatabaseTestsBase() {
private val returningSupportedDb = TestDB.ALL_POSTGRES.toSet() + TestDB.SQLITE
private val updateReturningSupportedDb = TestDB.ALL_POSTGRES.toSet() + TestDB.SQLITE
private val returningSupportedDb = updateReturningSupportedDb + TestDB.MARIADB

object Items : IntIdTable("items") {
val name = varchar("name", 32)
Expand Down Expand Up @@ -89,7 +90,7 @@ class ReturningTests : DatabaseTestsBase() {

@Test
fun testUpsertReturning() {
withTables(TestDB.ALL - returningSupportedDb, Items) {
withTables(TestDB.ALL - returningSupportedDb, Items) { testDB ->
// return all columns by default
val result1 = Items.upsertReturning {
it[name] = "A"
Expand All @@ -110,17 +111,18 @@ class ReturningTests : DatabaseTestsBase() {
assertEquals("A", result2[Items.name])
assertEquals(990.0, result2[Items.price])

val result3 = Items.upsertReturning(
returning = listOf(Items.name),
onUpdateExclude = listOf(Items.price),
where = { Items.price greater 500.0 }
) {
it[id] = 1
it[name] = "B"
it[price] = 200.0
}.single()
assertEquals("B", result3[Items.name])

if (testDB != TestDB.MARIADB) {
val result3 = Items.upsertReturning(
returning = listOf(Items.name),
onUpdateExclude = listOf(Items.price),
where = { Items.price greater 500.0 }
) {
it[id] = 1
it[name] = "B"
it[price] = 200.0
}.single()
assertEquals("B", result3[Items.name])
}
assertEquals(1, Items.selectAll().count())
}
}
Expand Down Expand Up @@ -196,7 +198,7 @@ class ReturningTests : DatabaseTestsBase() {

@Test
fun testUpdateReturning() {
withTables(TestDB.enabledDialects() - returningSupportedDb, Items) {
withTables(TestDB.enabledDialects() - updateReturningSupportedDb, Items) {
val input = listOf("A" to 99.0, "B" to 100.0, "C" to 200.0)
Items.batchInsert(input) { (n, p) ->
this[Items.name] = n
Expand Down

0 comments on commit aaa219b

Please sign in to comment.