Skip to content

Commit

Permalink
fix: EXPOSED-42 Can't create BLOB column with default value
Browse files Browse the repository at this point in the history
In `nonNullValueToString` for `BlobColumnType`, the `ExposedBlob` is now converted into its hexadecimal representation as a `String`
  • Loading branch information
joc-a committed May 12, 2023
1 parent 1145dda commit d9dc20b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.sql.statements.DefaultValueMarker
import org.jetbrains.exposed.sql.statements.api.ExposedBlob
import org.jetbrains.exposed.sql.statements.api.PreparedStatementApi
import org.jetbrains.exposed.sql.vendors.H2Dialect
import org.jetbrains.exposed.sql.vendors.MariaDBDialect
import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.SQLServerDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.jetbrains.exposed.sql.vendors.h2Mode
import org.jetbrains.exposed.sql.vendors.*
import java.io.InputStream
import java.math.BigDecimal
import java.math.MathContext
Expand Down Expand Up @@ -737,7 +732,17 @@ class BlobColumnType : ColumnType() {

override fun notNullValueToDB(value: Any): Any = value

override fun nonNullValueToString(value: Any): String = "?"
override fun nonNullValueToString(value: Any): String =
if (value is ExposedBlob) {
val hexString = HexFormat.of().formatHex(value.bytes)
when (currentDialect) {
is H2Dialect, is SQLiteDialect -> "X'$hexString'"
is MariaDBDialect, is MysqlDialect, is SQLServerDialect -> "0x$hexString"
is PostgreSQLNGDialect, is PostgreSQLDialect -> """E'\\x$hexString'"""
is OracleDialect -> "HEXTORAW('$hexString')"
else -> error("$currentDialect not supported")
}
} else error("Unexpected value of type Blob: $value of ${value::class.qualifiedName}")

override fun readObject(rs: ResultSet, index: Int) = when {
currentDialect is SQLServerDialect -> rs.getBytes(index)?.let(::ExposedBlob)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,37 @@ class DDLTests : DatabaseTestsBase() {
}
}

@Test
fun testBlobDefault() {
val defaultBlobStr = "test"
val defaultBlob = ExposedBlob(defaultBlobStr.encodeToByteArray())

val TestTable = object : Table("TestTable") {
val number = integer("number")
val blobWithDefault = blob("blobWithDefault").default(defaultBlob)
}

withDb { testDb ->
when (testDb) {
TestDB.MYSQL -> {
expectException<ExposedSQLException> {
SchemaUtils.create(TestTable)
}
}
else -> {
SchemaUtils.create(TestTable)

TestTable.insert {
it[number] = 1
}
assertEquals(defaultBlobStr, String(TestTable.selectAll().first()[TestTable.blobWithDefault].bytes))

SchemaUtils.drop(TestTable)
}
}
}
}

@Test
fun testBinaryWithoutLength() {
val tableWithBinary = object : Table("TableWithBinary") {
Expand Down

0 comments on commit d9dc20b

Please sign in to comment.