Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EXPOSED-365 Unable to insert values into Array column
Browse files Browse the repository at this point in the history
obabichevjb committed May 2, 2024
1 parent 1d89710 commit 5a32712
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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

/**
Original file line number Diff line number Diff line change
@@ -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() {
@@ -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
@@ -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() }

0 comments on commit 5a32712

Please sign in to comment.