Skip to content

Commit

Permalink
Enable the use of binary type without length in oracle and postgresql… (
Browse files Browse the repository at this point in the history
  • Loading branch information
hfazai authored and Tapac committed Dec 17, 2019
1 parent c019481 commit e636a44
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,11 @@ open class TextColumnType(collate: String? = null) : StringColumnType(collate) {
}
}

class BinaryColumnType(val length: Int) : ColumnType() {
override fun sqlType(): String = currentDialect.dataTypeProvider.binaryType(length)
/**
* Implements the binary column type.
*/
open class BasicBinaryColumnType : ColumnType() {
override fun sqlType(): String = currentDialect.dataTypeProvider.binaryType()

override fun valueFromDB(value: Any): Any {
if (value is Blob) {
Expand All @@ -287,6 +290,15 @@ class BinaryColumnType(val length: Int) : ColumnType() {
}
}

/**
* Implements the binary column type.
*
* @param length The maximum amount of bytes to store
*/
class BinaryColumnType(val length: Int) : BasicBinaryColumnType() {
override fun sqlType(): String = currentDialect.dataTypeProvider.binaryType(length)
}

class BlobColumnType : ColumnType() {
override fun sqlType(): String = currentDialect.dataTypeProvider.blobType()

Expand Down
15 changes: 14 additions & 1 deletion exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,24 @@ open class Table(name: String = ""): ColumnSet(), DdlAware {
/**
* A binary column to store an array of bytes.
*
* @sample org.jetbrains.exposed.sql.tests.shared.DDLTests.testBinary
*
* @param name The column name
* @param length The maximum amount of bytes to store
* @param length The maximum amount of bytes to store, this parameter is necessary only in H2, SQLite, MySQL,
* MariaDB, and SQL Server dialects.
*/
fun binary(name: String, length: Int): Column<ByteArray> = registerColumn(name, BinaryColumnType(length))

/**
* A binary column to store an array of bytes. This function is supported only by Oracle and PostgeSQL dialects.
* If you are using another dialect, please use instead the [binary] function by adding the length parameter.
*
* @sample org.jetbrains.exposed.sql.tests.shared.DDLTests.testBinaryWithoutLength
*
* @param name The column name
*/
fun binary(name: String): Column<ByteArray> = registerColumn(name, BasicBinaryColumnType())

/**
* A uuid column to store a UUID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.util.concurrent.ConcurrentHashMap

internal typealias TableAndColumnName = Pair<String, String>

open class DataTypeProvider {
abstract class DataTypeProvider {
open fun integerAutoincType() = "INT AUTO_INCREMENT"

open fun integerType() = "INT"
Expand All @@ -30,6 +30,8 @@ open class DataTypeProvider {

open fun binaryType(length: Int): String = "VARBINARY($length)"

open abstract fun binaryType(): String

open fun booleanType(): String = "BOOLEAN"

open fun booleanToStatementString(bool: Boolean) = bool.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import java.util.*

internal object H2DataTypeProvider : DataTypeProvider() {
override fun uuidType(): String = "UUID"

override fun binaryType(): String {
exposedLogger.error("The length of the Binary column is missing.")
error("The length of the Binary column is missing.")
}
}

private val Transaction.isMySQLMode: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import java.math.BigDecimal

internal object MysqlDataTypeProvider : DataTypeProvider() {
override fun dateTimeType(): String = if ((currentDialect as MysqlDialect).isFractionDateTimeSupported()) "DATETIME(6)" else "DATETIME"

override fun binaryType(): String {
exposedLogger.error("The length of the Binary column is missing.")
error("The length of the Binary column is missing.")
}
}

internal open class MysqlFunctionProvider : FunctionProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ internal object OracleDataTypeProvider : DataTypeProvider() {

override fun blobType() = "BLOB"

override fun binaryType(length: Int): String = "BLOB"
override fun binaryType(length: Int): String {
exposedLogger.warn("The length of the binary column is not required.")
return binaryType()
}

override fun binaryType(): String = "BLOB"

override fun booleanType() = "CHAR(1)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ internal object PostgreSQLDataTypeProvider : DataTypeProvider() {

override fun blobType(): String = "bytea"

override fun binaryType(length: Int): String = "bytea"
override fun binaryType(length: Int): String {
exposedLogger.warn("The length of the binary column is not required.")
return binaryType()
}

override fun binaryType(): String = "bytea"

override fun uuidToDB(value: UUID): Any = value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ internal object SQLServerDataTypeProvider : DataTypeProvider() {
override fun uuidType() = "uniqueidentifier"

override fun uuidToDB(value: UUID) = value.toString()

override fun binaryType(): String {
exposedLogger.error("The length of the Binary column is missing.")
error("The length of the Binary column is missing.")
}
}

internal object SQLServerFunctionProvider : FunctionProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ internal object SQLiteDataTypeProvider : DataTypeProvider() {
override fun booleanToStatementString(bool: Boolean) = if (bool) "1" else "0"
override fun dateTimeType(): String = "NUMERIC"
override val blobAsStream: Boolean = true

override fun binaryType(): String {
exposedLogger.error("The length of the Binary column is missing.")
error("The length of the Binary column is missing.")
}
}

internal object SQLiteFunctionProvider : FunctionProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,42 @@ class DDLTests : DatabaseTestsBase() {
}
}

@Test
fun testBinaryWithoutLength() {
val tableWithBinary = object : Table("TableWithBinary") {
val binaryColumn = binary("binaryColumn")
}

fun SizedIterable<ResultRow>.readAsString() = map { String(it[tableWithBinary.binaryColumn]) }

withDb(listOf(TestDB.ORACLE,TestDB.POSTGRESQL)) {
val exposedBytes = "Exposed".toByteArray()
val kotlinBytes = "Kotlin".toByteArray()

SchemaUtils.create(tableWithBinary)

tableWithBinary.insert {
it[tableWithBinary.binaryColumn] = exposedBytes
}
val insertedExposed = tableWithBinary.selectAll().readAsString().single()

assertEquals("Exposed", insertedExposed)

tableWithBinary.insert {
it[tableWithBinary.binaryColumn] = kotlinBytes
}

assertEqualCollections(tableWithBinary.selectAll().readAsString(), "Exposed", "Kotlin")

val insertedKotlin = tableWithBinary.select { tableWithBinary.binaryColumn eq kotlinBytes }.readAsString()
assertEqualCollections(insertedKotlin, "Kotlin")

SchemaUtils.drop(tableWithBinary)
}
}

@Test fun testBinary() {
val t = object : Table() {
val t = object : Table("t") {
val binary = binary("bytes", 10)
val byteCol = binary("byteCol", 1).clientDefault { byteArrayOf(0) }
}
Expand Down

0 comments on commit e636a44

Please sign in to comment.