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

fix: EXPOSED-116 UUID conversion error with upsert in H2 #1823

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.intellij.lang.annotations.Language
import org.jetbrains.exposed.exceptions.throwUnsupportedException
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.TransactionManager
import java.util.*

internal object H2DataTypeProvider : DataTypeProvider() {
override fun binaryType(): String {
Expand All @@ -12,6 +13,7 @@ internal object H2DataTypeProvider : DataTypeProvider() {
}

override fun uuidType(): String = "UUID"
override fun uuidToDB(value: UUID): Any = value.toString()
override fun dateTimeType(): String = "DATETIME(9)"

override fun timestampWithTimeZoneType(): String = "TIMESTAMP(9) WITH TIME ZONE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jetbrains.exposed.sql.vendors
import org.jetbrains.exposed.exceptions.throwUnsupportedException
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.TransactionManager
import java.util.*

internal object OracleDataTypeProvider : DataTypeProvider() {
override fun byteType(): String = "SMALLINT"
Expand All @@ -26,11 +27,25 @@ internal object OracleDataTypeProvider : DataTypeProvider() {

override fun binaryType(length: Int): String {
@Suppress("MagicNumber")
return if (length < 2000) "RAW ($length)"
else binaryType()
return if (length < 2000) "RAW ($length)" else binaryType()
}

override fun uuidType(): String {
return if ((currentDialect as? H2Dialect)?.h2Mode == H2Dialect.H2CompatibilityMode.Oracle) {
"UUID"
} else {
return "RAW(16)"
}
}

override fun uuidToDB(value: UUID): Any {
return if ((currentDialect as? H2Dialect)?.h2Mode == H2Dialect.H2CompatibilityMode.Oracle) {
H2DataTypeProvider.uuidToDB(value)
} else {
super.uuidToDB(value)
}
}

override fun uuidType(): String = "RAW(16)"
override fun dateTimeType(): String = "TIMESTAMP"
override fun booleanType(): String = "CHAR(1)"
override fun booleanToStatementString(bool: Boolean) = if (bool) "1" else "0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ class UpsertTests : DatabaseTestsBase() {
it[name] = "A"
}

tester.upsert { // insert because only 1 constraint is equal
tester.upsert { // insert because only 1 constraint is equal
it[idA] = 7
it[idB] = insertStmt get tester.idB
it[name] = "B"
}
tester.upsert { // insert because both constraints differ
tester.upsert { // insert because both constraints differ
it[idA] = 99
it[idB] = 99
it[name] = "C"
}
tester.upsert { // update because both constraints match
tester.upsert { // update because both constraints match
it[idA] = insertStmt get tester.idA
it[idB] = insertStmt get tester.idB
it[name] = "D"
Expand Down Expand Up @@ -157,6 +157,32 @@ class UpsertTests : DatabaseTestsBase() {
}
}

@Test
fun testUpsertWithUUIDKeyConflict() {
val tester = object : Table("tester") {
val id = uuid("id").autoGenerate()
val title = text("title")

override val primaryKey = PrimaryKey(id)
}

withTables(tester) { testDb ->
excludingH2Version1(testDb) {
val uuid1 = tester.upsert {
it[title] = "A"
} get tester.id
tester.upsert {
it[id] = uuid1
it[title] = "B"
}

val result = tester.selectAll().single()
assertEquals(uuid1, result[tester.id])
assertEquals("B", result[tester.title])
}
}
}

@Test
fun testUpsertWithNoUniqueConstraints() {
val tester = object : Table("tester") {
Expand Down Expand Up @@ -254,18 +280,18 @@ class UpsertTests : DatabaseTestsBase() {
withTables(tester) { testDb ->
excludingH2Version1(testDb) {
val testWord = "Test"
tester.upsert { // default expression in insert
tester.upsert { // default expression in insert
it[word] = testWord
}
assertEquals("Phrase", tester.selectAll().single()[tester.phrase])

val phraseConcat = concat(" - ", listOf(tester.word, tester.phrase))
tester.upsert(onUpdate = listOf(tester.phrase to phraseConcat)) { // expression in update
tester.upsert(onUpdate = listOf(tester.phrase to phraseConcat)) { // expression in update
it[word] = testWord
}
assertEquals("$testWord - $defaultPhrase", tester.selectAll().single()[tester.phrase])

tester.upsert { // provided expression in insert
tester.upsert { // provided expression in insert
it[word] = "$testWord 2"
it[phrase] = concat(stringLiteral("foo"), stringLiteral("bar"))
}
Expand Down