Skip to content

Commit

Permalink
EXPOSED-365 Unable to insert values into Array column (#2072)
Browse files Browse the repository at this point in the history
  • Loading branch information
obabichevjb authored May 3, 2024
1 parent 048b62a commit d48e772
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
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() }

0 comments on commit d48e772

Please sign in to comment.