Skip to content

Commit

Permalink
binary type doesn't honor max length #993
Browse files Browse the repository at this point in the history
Client-side length validation for BinaryColumnType
  • Loading branch information
Tapac committed Jul 12, 2020
1 parent 0c0b93a commit ffcc91a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,13 @@ open class CharColumnType(
}
}

override fun notNullValueToDB(value: Any): Any {
require(value is String && value.codePointCount(0, value.length) <= colLength) {
"Value '$value' can't be stored to database column because exceeds length ($colLength)"
}
return value
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down Expand Up @@ -528,6 +535,13 @@ open class VarCharColumnType(
}
}

override fun notNullValueToDB(value: Any): Any {
require(value is String && value.codePointCount(0, value.length) <= colLength) {
"Value '$value' can't be stored to database column because exceeds length ($colLength)"
}
return value
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down Expand Up @@ -599,6 +613,13 @@ class BinaryColumnType(
) : BasicBinaryColumnType() {
override fun sqlType(): String = currentDialect.dataTypeProvider.binaryType(length)

override fun notNullValueToDB(value: Any): Any {
require(value is ByteArray && value.size <= length) {
"Value '$value' can't be stored to database column because exceeds length ($length)"
}
return value
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down Expand Up @@ -764,9 +785,9 @@ class EnumerationNameColumnType<T : Enum<T>>(
else -> error("$value of ${value::class.qualifiedName} is not valid for enum ${klass.qualifiedName}")
}

override fun notNullValueToDB(value: Any): String = when (value) {
is String -> value
is Enum<*> -> value.name
override fun notNullValueToDB(value: Any): Any = when (value) {
is String -> super.notNullValueToDB(value)
is Enum<*> -> super.notNullValueToDB(value.name)
else -> error("$value of ${value::class.qualifiedName} is not valid for enum ${klass.qualifiedName}")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@ abstract class UpdateBuilder<out T>(type: StatementType, targets: List<Table>):
when {
values.containsKey(column) -> error("$column is already initialized")
!column.columnType.nullable && value == null -> error("Trying to set null to not nullable column $column")
column.columnType is VarCharColumnType && value is String && value.codePointCount(0, value.length) > (column.columnType as VarCharColumnType).colLength -> {
error("Value '$value' can't be stored to database column because exceeds length ${(column.columnType as VarCharColumnType).colLength}")
}
column.columnType is CharColumnType && value is String && value.codePointCount(0, value.length) != (column.columnType as CharColumnType).colLength -> {
error("Value '$value' can't be stored to database column because length is not equal to ${(column.columnType as CharColumnType).colLength}")
}
else -> {
values[column] = value
}
else -> values[column] = value
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class InsertTests : DatabaseTestsBase() {
val emojis = "\uD83D\uDC68\uD83C\uDFFF\u200D\uD83D\uDC69\uD83C\uDFFF\u200D\uD83D\uDC67\uD83C\uDFFF\u200D\uD83D\uDC66\uD83C\uDFFF"

withTables(listOf(TestDB.SQLITE, TestDB.H2, TestDB.H2_MYSQL, TestDB.POSTGRESQL, TestDB.POSTGRESQLNG), table) {
expectException<IllegalStateException> {
expectException<IllegalArgumentException> {
table.insert {
it[table.emoji] = emojis
}
Expand Down

0 comments on commit ffcc91a

Please sign in to comment.