Skip to content

Commit

Permalink
add particular text lenght subtypes (#1579)
Browse files Browse the repository at this point in the history
* add particular text lenght subtypes

* fix compillation

Co-authored-by: Aleksandr Shubert <alex.shubert@gmail.com>
  • Loading branch information
lure and Aleksandr Shubert authored Sep 25, 2022
1 parent 9ae360d commit 16af557
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,10 @@ open class VarCharColumnType(
* [eagerLoading] means what content will be loaded immediately when data loaded from database.
*/
open class TextColumnType(collate: String? = null, val eagerLoading: Boolean = false) : StringColumnType(collate) {
open fun preciseType() = currentDialect.dataTypeProvider.textType()

override fun sqlType(): String = buildString {
append(currentDialect.dataTypeProvider.textType())
append(preciseType())
if (collate != null) {
append(" COLLATE ${escape(collate)}")
}
Expand All @@ -638,6 +640,14 @@ open class TextColumnType(collate: String? = null, val eagerLoading: Boolean = f
}
}

open class MediumTextColumnType(collate: String? = null, eagerLoading: Boolean = false) : TextColumnType(collate, eagerLoading) {
override fun preciseType(): String = currentDialect.dataTypeProvider.mediumTextType()
}

open class LargeTextColumnType(collate: String? = null, eagerLoading: Boolean = false) : TextColumnType(collate, eagerLoading) {
override fun preciseType(): String = currentDialect.dataTypeProvider.largeTextType()
}

// Binary columns

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
fun text(name: String, collate: String? = null, eagerLoading: Boolean = false): Column<String> =
registerColumn(name, TextColumnType(collate, eagerLoading))

fun mediumText(name: String, collate: String? = null, eagerLoading: Boolean = false): Column<String> =
registerColumn(name, MediumTextColumnType(collate, eagerLoading))
fun largeText(name: String, collate: String? = null, eagerLoading: Boolean = false): Column<String> =
registerColumn(name, LargeTextColumnType(collate, eagerLoading))

// Binary columns

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,16 @@ abstract class DataTypeProvider {

// Character types

/** Character type for storing strings of variable and _unlimited_ length. */
/** Character type for storing strings of variable length.
* Some database (postgresql) use the same data type name to provide virtually _unlimited_ length. */
open fun textType(): String = "TEXT"

/** Character type for storing strings of _medium_ length. */
open fun mediumTextType(): String = "TEXT"

/** Character type for storing strings of variable and _large_ length. */
open fun largeTextType(): String = "TEXT"

// Binary data types

/** Binary type for storing binary strings of variable and _unlimited_ length. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ internal object MysqlDataTypeProvider : DataTypeProvider() {

override fun ulongType(): String = "BIGINT UNSIGNED"

override fun textType(): String = "longtext"
override fun textType(): String = "text"
// override fun textType(): String = "longtext"

/** Character type for storing strings of variable and _unlimited_ length. */
override fun mediumTextType(): String = "MEDIUMTEXT"

/** Character type for storing strings of variable and _unlimited_ length. */
override fun largeTextType(): String = "LONGTEXT"

override fun booleanFromStringToBoolean(value: String): Boolean = when(value) {
"0" -> false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ internal object OracleDataTypeProvider : DataTypeProvider() {
override fun longAutoincType(): String = "NUMBER(19)"
override fun ulongType(): String = "NUMBER(20)"
override fun textType(): String = "CLOB"
override fun mediumTextType(): String = textType()
override fun largeTextType(): String = textType()
override fun timeType(): String = dateTimeType()
override fun binaryType(): String {
exposedLogger.error("Binary type is unsupported for Oracle. Please use blob column type instead.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ internal object PostgreSQLDataTypeProvider : DataTypeProvider() {
exposedLogger.warn("The length of the binary column is not required.")
return binaryType()
}

override fun blobType(): String = "bytea"
override fun uuidToDB(value: UUID): Any = value
override fun dateTimeType(): String = "TIMESTAMP"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ internal object SQLServerDataTypeProvider : DataTypeProvider() {
* https://docs.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql?view=sql-server-ver15
*/
override fun textType(): String = "VARCHAR(MAX)"
override fun mediumTextType(): String = textType()
override fun largeTextType(): String = textType()

override fun precessOrderByClause(queryBuilder: QueryBuilder, expression: Expression<*>, sortOrder: SortOrder) {
when (sortOrder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,55 @@ class DDLTests : DatabaseTestsBase() {
}
}

@Test fun tableWithDifferentTextTypes() {
val TestTable = object : Table("different_text_column_types") {
val id = integer("id").autoIncrement()
val txt = text("txt")
val txtMed = mediumText("txt_med")
val txtLong = largeText("txt_large")

override val primaryKey: PrimaryKey = PrimaryKey(id)
}

withDb(listOf(TestDB.POSTGRESQL, TestDB.MYSQL)) {
SchemaUtils.create(TestTable)
assertEquals(
"CREATE TABLE " + addIfNotExistsIfSupported() + "${"different_text_column_types".inProperCase()} " +
"(${TestTable.id.nameInDatabaseCase()} ${currentDialectTest.dataTypeProvider.integerAutoincType()} PRIMARY KEY, " +
"${TestTable.txt.nameInDatabaseCase()} ${currentDialectTest.dataTypeProvider.textType()} NOT NULL, " +
"${TestTable.txtMed.nameInDatabaseCase()} ${currentDialectTest.dataTypeProvider.mediumTextType()} NOT NULL, " +
"${TestTable.txtLong.nameInDatabaseCase()} ${currentDialectTest.dataTypeProvider.largeTextType()} NOT NULL)",
TestTable.ddl
)

// double check that different types were applied indeed
assert(
currentDialectTest.name == "postgresql" ||
(
currentDialectTest.dataTypeProvider.textType() != currentDialectTest.dataTypeProvider.mediumTextType() &&
currentDialectTest.dataTypeProvider.mediumTextType() != currentDialectTest.dataTypeProvider.largeTextType() &&
currentDialectTest.dataTypeProvider.textType() != currentDialectTest.dataTypeProvider.largeTextType()
)
)

TestTable.insert {
it[txt] = "1Txt"
it[txtMed] = "1TxtMed"
it[txtLong] = "1TxtLong"
}

val concat = SqlExpressionBuilder.concat(
separator = " ",
listOf(LowerCase(TestTable.txt), UpperCase(TestTable.txtMed), LowerCase(TestTable.txtLong))
)

// just to be sure new type didn't break the functions
TestTable.slice(concat).selectAll().forEach {
assertEquals(it[concat], "1txt 1TXTMED 1txtlong")
}
}
}

@Test fun testDeleteMissingTable() {
val missingTable = Table("missingTable")
withDb {
Expand Down

0 comments on commit 16af557

Please sign in to comment.