diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt index feca48b108..2bfde47a2c 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt @@ -1048,12 +1048,17 @@ class ArrayColumnType( 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 /** diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/ArrayColumnTypeTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/ArrayColumnTypeTests.kt index b09a30a106..8e2759b3c5 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/ArrayColumnTypeTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/ArrayColumnTypeTests.kt @@ -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("numbers").default(listOf(5)) val strings = array("strings", TextColumnType()).default(emptyList()) val doubles = array("doubles").nullable() + val byteArray = array("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() }