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

EXPOSED-365 Unable to insert values into Array column #2072

Merged
merged 1 commit into from
May 3, 2024
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 @@ -1048,12 +1048,17 @@ class ArrayColumnType<E>(

override fun setParameter(stmt: PreparedStatementApi, index: Int, value: Any?) {
when {
value is Array<*> && isArrayOfByteArrays(value) ->
stmt.setArray(index, delegateType, Array(value.size) { value[it] as ByteArray })
value is Array<*> -> stmt.setArray(index, delegateType, value)
else -> super.setParameter(stmt, index, value)
}
}
}

private fun isArrayOfByteArrays(value: Array<*>) =
value.all { it is ByteArray }

// Date/Time columns

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.jetbrains.exposed.sql.vendors.PostgreSQLDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.junit.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class ArrayColumnTypeTests : DatabaseTestsBase() {
Expand All @@ -26,6 +27,7 @@ class ArrayColumnTypeTests : DatabaseTestsBase() {
val numbers = array<Int>("numbers").default(listOf(5))
val strings = array<String?>("strings", TextColumnType()).default(emptyList())
val doubles = array<Double>("doubles").nullable()
val byteArray = array<ByteArray>("byte_array", BinaryColumnType(32)).nullable()
}

@Test
Expand Down Expand Up @@ -289,4 +291,26 @@ class ArrayColumnTypeTests : DatabaseTestsBase() {
assertTrue(result4.toList().isEmpty())
}
}

@Test
fun testInsertArrayOfByteArrays() {
// POSTGRESQLNG is excluded because the problem may be on their side.
// Related issue: https://github.com/impossibl/pgjdbc-ng/issues/600
// Recheck on our side when the issue is resolved.
withTables(excludeSettings = arrayTypeUnsupportedDb + TestDB.POSTGRESQLNG, ArrayTestTable) {
val testByteArraysList = listOf(
byteArrayOf(0), byteArrayOf(1, 2, 3)
)
ArrayTestTable.insert {
it[byteArray] = testByteArraysList
}
val result = ArrayTestTable.selectAll().first()[ArrayTestTable.byteArray]

assertNotNull(result)
assertEquals(testByteArraysList[0][0], result[0][0])
assertEquals(testByteArraysList[1].toUByteString(), result[1].toUByteString())
}
}
}

private fun ByteArray.toUByteString() = joinToString { it.toUByte().toString() }
Loading