Skip to content

Commit

Permalink
EXPOSED-376 batchUpsert does not return database values on conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
obabichevjb committed May 14, 2024
1 parent fd519d3 commit 629b903
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
2 changes: 2 additions & 0 deletions exposed-core/api/exposed-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -2927,6 +2927,7 @@ public class org/jetbrains/exposed/sql/statements/BatchUpsertStatement : org/jet
public final fun getKeys ()[Lorg/jetbrains/exposed/sql/Column;
public final fun getOnUpdate ()Ljava/util/List;
public final fun getOnUpdateExclude ()Ljava/util/List;
protected fun isColumnValuePreferredFromResultSet (Lorg/jetbrains/exposed/sql/Column;Ljava/lang/Object;)Z
public fun prepareSQL (Lorg/jetbrains/exposed/sql/Transaction;Z)Ljava/lang/String;
}

Expand Down Expand Up @@ -2994,6 +2995,7 @@ public class org/jetbrains/exposed/sql/statements/InsertStatement : org/jetbrain
public final fun getResultedValues ()Ljava/util/List;
public final fun getTable ()Lorg/jetbrains/exposed/sql/Table;
protected fun isColumnValuePreferredFromResultSet (Lorg/jetbrains/exposed/sql/Column;Ljava/lang/Object;)Z
protected final fun isEntityIdClientSideGeneratedUUID (Lorg/jetbrains/exposed/sql/Column;)Z
public final fun isIgnore ()Z
public fun prepareSQL (Lorg/jetbrains/exposed/sql/Transaction;Z)Ljava/lang/String;
public fun prepared (Lorg/jetbrains/exposed/sql/Transaction;Ljava/lang/String;)Lorg/jetbrains/exposed/sql/statements/api/PreparedStatementApi;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ open class BatchUpsertStatement(
}
return functionProvider.upsert(table, arguments!!.first(), onUpdate, onUpdateExclude, null, transaction, keys = keys)
}

override fun isColumnValuePreferredFromResultSet(column: Column<*>, value: Any?): Boolean {
return isEntityIdClientSideGeneratedUUID(column) ||
super.isColumnValuePreferredFromResultSet(column, value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,10 @@ open class InsertStatement<Key : Any>(
builder.args
} ?: emptyList()
}

protected fun isEntityIdClientSideGeneratedUUID(column: Column<*>) =
(column.columnType as? EntityIDColumnType<*>)
?.idColumn
?.takeIf { it.columnType is UUIDColumnType }
?.defaultValueFun != null
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,4 @@ open class UpsertStatement<Key : Any>(
return isEntityIdClientSideGeneratedUUID(column) ||
super.isColumnValuePreferredFromResultSet(column, value)
}

private fun isEntityIdClientSideGeneratedUUID(column: Column<*>) =
(column.columnType as? EntityIDColumnType<*>)
?.idColumn
?.takeIf { it.columnType is UUIDColumnType }
?.defaultValueFun != null
}
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,33 @@ class UpsertTests : DatabaseTestsBase() {
}
}

@Test
fun testBatchUpsertWithUUIDPrimaryKey() {
val tester = object : UUIDTable("batch_upsert_test", "id") {
val key = integer("test_key").uniqueIndex()
val value = text("test_value")
}

withTables(excludeSettings = TestDB.entries - listOf(TestDB.POSTGRESQL, TestDB.POSTGRESQLNG), tester) {
val insertId = tester.insertAndGetId {
it[key] = 1
it[value] = "one"
}

val upsertId = tester.batchUpsert(
data = listOf(1 to "two"),
keys = arrayOf(tester.key),
onUpdateExclude = listOf(tester.id),
) {
this[tester.key] = it.first
this[tester.value] = it.second
}.first()[tester.id]

assertEquals(insertId, upsertId)
assertEquals("two", tester.selectAll().where { tester.id eq insertId }.first()[tester.value])
}
}

private object AutoIncTable : Table("auto_inc_table") {
val id = integer("id").autoIncrement()
val name = varchar("name", 64)
Expand Down

0 comments on commit 629b903

Please sign in to comment.