From d27a577e55c55d2637455305e43067beda59ed91 Mon Sep 17 00:00:00 2001 From: Jocelyne Date: Mon, 14 Oct 2024 18:52:13 +0200 Subject: [PATCH 1/2] chore!: Change H2 Oracle longType and longAutoincType from NUMBER(19) to BIGINT and add CHECK constraint in Oracle and SQLite --- .../Writerside/topics/Breaking-Changes.md | 2 ++ .../kotlin/org/jetbrains/exposed/sql/Table.kt | 36 ++++++++++++++++++- .../exposed/sql/vendors/OracleDialect.kt | 8 +++-- .../org/jetbrains/exposed/DefaultsTest.kt | 8 +++-- .../jetbrains/exposed/JodaTimeDefaultsTest.kt | 8 +++-- .../sql/kotlin/datetime/DefaultsTest.kt | 8 +++-- .../shared/types/NumericColumnTypesTests.kt | 31 ++++++++++++++++ 7 files changed, 92 insertions(+), 9 deletions(-) diff --git a/documentation-website/Writerside/topics/Breaking-Changes.md b/documentation-website/Writerside/topics/Breaking-Changes.md index b7e737d407..adcd8dd8e3 100644 --- a/documentation-website/Writerside/topics/Breaking-Changes.md +++ b/documentation-website/Writerside/topics/Breaking-Changes.md @@ -44,6 +44,8 @@ that is also type restricted to `Comparable` (for example, `avg()`) will also require defining a new function. In this event, please also leave a comment on [YouTrack](https://youtrack.jetbrains.com/issue/EXPOSED-577) with a use case so the original function signature can be potentially reassessed. +* In H2 Oracle, the `long()` column now maps to data type `BIGINT` instead of `NUMBER(19)`. + In Oracle and SQLite, using the long column in a table now also creates a CHECK constraint to ensure that no out-of-range values are inserted. ## 0.55.0 * The `DeleteStatement` property `table` is now deprecated in favor of `targetsSet`, which holds a `ColumnSet` that may be a `Table` or `Join`. diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt index 11171dac14..f12bb357c5 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt @@ -1698,6 +1698,11 @@ open class Table(name: String = "") : ColumnSet(), DdlAware { append("IF NOT EXISTS ") } append(TransactionManager.current().identity(this@Table)) + + // Add CHECK constraint to Long columns in Oracle and SQLite. + // It is done here because special handling is necessary based on the dialect. + addLongColumnCheckConstraintIfNeeded() + if (columns.isNotEmpty()) { columns.joinTo(this, prefix = " (") { column -> column.descriptionDdl(false) @@ -1739,7 +1744,8 @@ open class Table(name: String = "") : ColumnSet(), DdlAware { }.let { if (currentDialect !is SQLiteDialect && currentDialect !is OracleDialect) { it.filterNot { (name, _) -> - name.startsWith("${generatedSignedCheckPrefix}integer") + name.startsWith("${generatedSignedCheckPrefix}integer") || + name.startsWith("${generatedSignedCheckPrefix}long") } } else { it @@ -1764,6 +1770,34 @@ open class Table(name: String = "") : ColumnSet(), DdlAware { return createAutoIncColumnSequence() + createTable + createConstraint } + private fun addLongColumnCheckConstraintIfNeeded() { + if (currentDialect is OracleDialect || currentDialect is SQLiteDialect) { + columns.filter { it.columnType is LongColumnType }.forEach { column -> + val name = column.name + val checkName = "${generatedSignedCheckPrefix}long_$name" + if (checkConstraints.none { it.first == checkName }) { + column.check(checkName) { + if (currentDialect is SQLiteDialect) { + fun typeOf(value: String) = object : ExpressionWithColumnType() { + override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { append("typeof($value)") } + override val columnType: IColumnType = TextColumnType() + } + + val typeCondition = Expression.build { typeOf(name) eq stringLiteral("integer") } + if (column.columnType.nullable) { + column.isNull() or typeCondition + } else { + typeCondition + } + } else { + it.between(Long.MIN_VALUE, Long.MAX_VALUE) + } + } + } + } + } + } + private fun createAutoIncColumnSequence(): List { return autoIncColumn?.autoIncColumnType?.sequence?.createStatement().orEmpty() } diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/OracleDialect.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/OracleDialect.kt index 54c9e6d298..4f3b8fedda 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/OracleDialect.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/OracleDialect.kt @@ -33,8 +33,12 @@ internal object OracleDataTypeProvider : DataTypeProvider() { override fun integerAutoincType(): String = integerType() override fun uintegerType(): String = "NUMBER(10)" override fun uintegerAutoincType(): String = "NUMBER(10)" - override fun longType(): String = "NUMBER(19)" - override fun longAutoincType(): String = "NUMBER(19)" + override fun longType(): String = if (currentDialect.h2Mode == H2Dialect.H2CompatibilityMode.Oracle) { + "BIGINT" + } else { + "NUMBER(19)" + } + override fun longAutoincType(): String = longType() override fun ulongType(): String = "NUMBER(20)" override fun ulongAutoincType(): String = "NUMBER(20)" override fun varcharType(colLength: Int): String = "VARCHAR2($colLength CHAR)" diff --git a/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt b/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt index 2ed097d4af..2fa9e8d1ef 100644 --- a/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt +++ b/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt @@ -273,8 +273,12 @@ class DefaultsTest : DatabaseTestsBase() { "${"t9".inProperCase()} $timeType${testTable.t9.constraintNamePart()} ${tLiteral.itOrNull()}, " + "${"t10".inProperCase()} $timeType${testTable.t10.constraintNamePart()} ${tLiteral.itOrNull()}" + when (testDb) { - TestDB.SQLITE, TestDB.ORACLE -> - ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + TestDB.SQLITE -> + ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + + ", CONSTRAINT chk_t_signed_long_l CHECK (typeof(l) = 'integer')" + TestDB.ORACLE -> + ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + + ", CONSTRAINT chk_t_signed_long_l CHECK (L BETWEEN ${Long.MIN_VALUE} AND ${Long.MAX_VALUE})" else -> "" } + ")" diff --git a/exposed-jodatime/src/test/kotlin/org/jetbrains/exposed/JodaTimeDefaultsTest.kt b/exposed-jodatime/src/test/kotlin/org/jetbrains/exposed/JodaTimeDefaultsTest.kt index 407cda1494..754567f78e 100644 --- a/exposed-jodatime/src/test/kotlin/org/jetbrains/exposed/JodaTimeDefaultsTest.kt +++ b/exposed-jodatime/src/test/kotlin/org/jetbrains/exposed/JodaTimeDefaultsTest.kt @@ -210,8 +210,12 @@ class JodaTimeDefaultsTest : DatabaseTestsBase() { "${"t5".inProperCase()} $timeType${testTable.t5.constraintNamePart()} ${tLiteral.itOrNull()}, " + "${"t6".inProperCase()} $timeType${testTable.t6.constraintNamePart()} ${tLiteral.itOrNull()}" + when (testDb) { - TestDB.SQLITE, TestDB.ORACLE -> - ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + TestDB.SQLITE -> + ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + + ", CONSTRAINT chk_t_signed_long_l CHECK (typeof(l) = 'integer')" + TestDB.ORACLE -> + ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + + ", CONSTRAINT chk_t_signed_long_l CHECK (L BETWEEN ${Long.MIN_VALUE} AND ${Long.MAX_VALUE})" else -> "" } + ")" diff --git a/exposed-kotlin-datetime/src/test/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/DefaultsTest.kt b/exposed-kotlin-datetime/src/test/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/DefaultsTest.kt index 22dcbf87e0..c83516675e 100644 --- a/exposed-kotlin-datetime/src/test/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/DefaultsTest.kt +++ b/exposed-kotlin-datetime/src/test/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/DefaultsTest.kt @@ -271,8 +271,12 @@ class DefaultsTest : DatabaseTestsBase() { "${"t9".inProperCase()} $timeType${testTable.t9.constraintNamePart()} ${tLiteral.itOrNull()}, " + "${"t10".inProperCase()} $timeType${testTable.t10.constraintNamePart()} ${tLiteral.itOrNull()}" + when (testDb) { - TestDB.SQLITE, TestDB.ORACLE -> - ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + TestDB.SQLITE -> + ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + + ", CONSTRAINT chk_t_signed_long_l CHECK (typeof(l) = 'integer')" + TestDB.ORACLE -> + ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + + ", CONSTRAINT chk_t_signed_long_l CHECK (L BETWEEN ${Long.MIN_VALUE} AND ${Long.MAX_VALUE})" else -> "" } + ")" diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/NumericColumnTypesTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/NumericColumnTypesTests.kt index 10dcaeaaeb..f457456d9e 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/NumericColumnTypesTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/NumericColumnTypesTests.kt @@ -108,6 +108,37 @@ class NumericColumnTypesTests : DatabaseTestsBase() { } } + @Test + fun testLongAcceptsOnlyAllowedRange() { + val testTable = object : Table("test_table") { + val long = long("long_column") + } + + withTables(testTable) { testDb -> + val columnName = testTable.long.nameInDatabaseCase() + val ddlEnding = when (testDb) { + TestDB.SQLITE -> "CHECK (typeof($columnName) = 'integer'))" + TestDB.ORACLE -> "CHECK ($columnName BETWEEN ${Long.MIN_VALUE} and ${Long.MAX_VALUE}))" + else -> "($columnName ${testTable.long.columnType} NOT NULL)" + } + assertTrue(testTable.ddl.single().endsWith(ddlEnding, ignoreCase = true)) + + testTable.insert { it[long] = Long.MIN_VALUE } + testTable.insert { it[long] = Long.MAX_VALUE } + assertEquals(2, testTable.select(testTable.long).count()) + + val tableName = testTable.nameInDatabaseCase() + assertFailAndRollback(message = "Out-of-range error (or CHECK constraint violation for SQLite & Oracle)") { + val outOfRangeValue = Long.MIN_VALUE.toBigDecimal() - 1.toBigDecimal() + exec("INSERT INTO $tableName ($columnName) VALUES ($outOfRangeValue)") + } + assertFailAndRollback(message = "Out-of-range error (or CHECK constraint violation for SQLite & Oracle)") { + val outOfRangeValue = Long.MAX_VALUE.toBigDecimal() + 1.toBigDecimal() + exec("INSERT INTO $tableName ($columnName) VALUES ($outOfRangeValue)") + } + } + } + @Test fun testParams() { val testTable = object : Table("test_table") { From 3f2e4b2e7641fdbba20a50184c53102ee01ad04e Mon Sep 17 00:00:00 2001 From: Jocelyne Date: Tue, 26 Nov 2024 13:23:35 +0100 Subject: [PATCH 2/2] chore: Exclude SQLite from out-of-range check since the raw SQL behaviour is that it is not possible to enforce the range without a special CHECK constraint that checks the type --- .../Writerside/topics/Breaking-Changes.md | 25 +++++++++- .../kotlin/org/jetbrains/exposed/sql/Table.kt | 47 +++++-------------- .../org/jetbrains/exposed/DefaultsTest.kt | 3 +- .../jetbrains/exposed/JodaTimeDefaultsTest.kt | 3 +- .../sql/kotlin/datetime/DefaultsTest.kt | 3 +- .../sql/tests/shared/ddl/CreateTableTests.kt | 42 ++++++++++++++--- .../shared/types/NumericColumnTypesTests.kt | 21 +++++---- 7 files changed, 86 insertions(+), 58 deletions(-) diff --git a/documentation-website/Writerside/topics/Breaking-Changes.md b/documentation-website/Writerside/topics/Breaking-Changes.md index adcd8dd8e3..31379b92c3 100644 --- a/documentation-website/Writerside/topics/Breaking-Changes.md +++ b/documentation-website/Writerside/topics/Breaking-Changes.md @@ -24,6 +24,29 @@ -- Starting from version 0.57.0 INSERT INTO TEST DEFAULT VALUES ``` +* In H2 Oracle, the `long()` column now maps to data type `BIGINT` instead of `NUMBER(19)`. + In Oracle, using the long column in a table now also creates a CHECK constraint to ensure that no out-of-range values are inserted. + Exposed does not ensure this behaviour for SQLite. If you want to do that, please use the following CHECK constraint: + +```kotlin +val long = long("long_column").check { column -> + fun typeOf(value: String) = object : ExpressionWithColumnType() { + override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { append("typeof($value)") } + override val columnType: IColumnType = TextColumnType() + } + Expression.build { typeOf(column.name) eq stringLiteral("integer") } +} + +val long = long("long_column").nullable().check { column -> + fun typeOf(value: String) = object : ExpressionWithColumnType() { + override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { append("typeof($value)") } + override val columnType: IColumnType = TextColumnType() + } + + val typeCondition = Expression.build { typeOf(column.name) eq stringLiteral("integer") } + column.isNull() or typeCondition +} +``` ## 0.56.0 * If the `distinct` parameter of `groupConcat()` is set to `true`, when using Oracle or SQL Server, this will now fail early with an @@ -44,8 +67,6 @@ that is also type restricted to `Comparable` (for example, `avg()`) will also require defining a new function. In this event, please also leave a comment on [YouTrack](https://youtrack.jetbrains.com/issue/EXPOSED-577) with a use case so the original function signature can be potentially reassessed. -* In H2 Oracle, the `long()` column now maps to data type `BIGINT` instead of `NUMBER(19)`. - In Oracle and SQLite, using the long column in a table now also creates a CHECK constraint to ensure that no out-of-range values are inserted. ## 0.55.0 * The `DeleteStatement` property `table` is now deprecated in favor of `targetsSet`, which holds a `ColumnSet` that may be a `Table` or `Join`. diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt index f12bb357c5..0bd0905090 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt @@ -753,7 +753,9 @@ open class Table(name: String = "") : ColumnSet(), DdlAware { } /** Creates a numeric column, with the specified [name], for storing 8-byte integers. */ - fun long(name: String): Column = registerColumn(name, LongColumnType()) + fun long(name: String): Column = registerColumn(name, LongColumnType()).apply { + check("${generatedSignedCheckPrefix}long_${this.unquotedName()}") { it.between(Long.MIN_VALUE, Long.MAX_VALUE) } + } /** Creates a numeric column, with the specified [name], for storing 8-byte unsigned integers. * @@ -1699,10 +1701,6 @@ open class Table(name: String = "") : ColumnSet(), DdlAware { } append(TransactionManager.current().identity(this@Table)) - // Add CHECK constraint to Long columns in Oracle and SQLite. - // It is done here because special handling is necessary based on the dialect. - addLongColumnCheckConstraintIfNeeded() - if (columns.isNotEmpty()) { columns.joinTo(this, prefix = " (") { column -> column.descriptionDdl(false) @@ -1744,8 +1742,15 @@ open class Table(name: String = "") : ColumnSet(), DdlAware { }.let { if (currentDialect !is SQLiteDialect && currentDialect !is OracleDialect) { it.filterNot { (name, _) -> - name.startsWith("${generatedSignedCheckPrefix}integer") || - name.startsWith("${generatedSignedCheckPrefix}long") + name.startsWith("${generatedSignedCheckPrefix}integer") + } + } else { + it + } + }.let { + if (currentDialect !is OracleDialect) { + it.filterNot { (name, _) -> + name.startsWith("${generatedSignedCheckPrefix}long") } } else { it @@ -1770,34 +1775,6 @@ open class Table(name: String = "") : ColumnSet(), DdlAware { return createAutoIncColumnSequence() + createTable + createConstraint } - private fun addLongColumnCheckConstraintIfNeeded() { - if (currentDialect is OracleDialect || currentDialect is SQLiteDialect) { - columns.filter { it.columnType is LongColumnType }.forEach { column -> - val name = column.name - val checkName = "${generatedSignedCheckPrefix}long_$name" - if (checkConstraints.none { it.first == checkName }) { - column.check(checkName) { - if (currentDialect is SQLiteDialect) { - fun typeOf(value: String) = object : ExpressionWithColumnType() { - override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { append("typeof($value)") } - override val columnType: IColumnType = TextColumnType() - } - - val typeCondition = Expression.build { typeOf(name) eq stringLiteral("integer") } - if (column.columnType.nullable) { - column.isNull() or typeCondition - } else { - typeCondition - } - } else { - it.between(Long.MIN_VALUE, Long.MAX_VALUE) - } - } - } - } - } - } - private fun createAutoIncColumnSequence(): List { return autoIncColumn?.autoIncColumnType?.sequence?.createStatement().orEmpty() } diff --git a/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt b/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt index 2fa9e8d1ef..f88ddd9f2d 100644 --- a/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt +++ b/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt @@ -274,8 +274,7 @@ class DefaultsTest : DatabaseTestsBase() { "${"t10".inProperCase()} $timeType${testTable.t10.constraintNamePart()} ${tLiteral.itOrNull()}" + when (testDb) { TestDB.SQLITE -> - ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + - ", CONSTRAINT chk_t_signed_long_l CHECK (typeof(l) = 'integer')" + ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" TestDB.ORACLE -> ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + ", CONSTRAINT chk_t_signed_long_l CHECK (L BETWEEN ${Long.MIN_VALUE} AND ${Long.MAX_VALUE})" diff --git a/exposed-jodatime/src/test/kotlin/org/jetbrains/exposed/JodaTimeDefaultsTest.kt b/exposed-jodatime/src/test/kotlin/org/jetbrains/exposed/JodaTimeDefaultsTest.kt index 754567f78e..a2c16e69cd 100644 --- a/exposed-jodatime/src/test/kotlin/org/jetbrains/exposed/JodaTimeDefaultsTest.kt +++ b/exposed-jodatime/src/test/kotlin/org/jetbrains/exposed/JodaTimeDefaultsTest.kt @@ -211,8 +211,7 @@ class JodaTimeDefaultsTest : DatabaseTestsBase() { "${"t6".inProperCase()} $timeType${testTable.t6.constraintNamePart()} ${tLiteral.itOrNull()}" + when (testDb) { TestDB.SQLITE -> - ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + - ", CONSTRAINT chk_t_signed_long_l CHECK (typeof(l) = 'integer')" + ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" TestDB.ORACLE -> ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + ", CONSTRAINT chk_t_signed_long_l CHECK (L BETWEEN ${Long.MIN_VALUE} AND ${Long.MAX_VALUE})" diff --git a/exposed-kotlin-datetime/src/test/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/DefaultsTest.kt b/exposed-kotlin-datetime/src/test/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/DefaultsTest.kt index c83516675e..b46b607536 100644 --- a/exposed-kotlin-datetime/src/test/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/DefaultsTest.kt +++ b/exposed-kotlin-datetime/src/test/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/DefaultsTest.kt @@ -272,8 +272,7 @@ class DefaultsTest : DatabaseTestsBase() { "${"t10".inProperCase()} $timeType${testTable.t10.constraintNamePart()} ${tLiteral.itOrNull()}" + when (testDb) { TestDB.SQLITE -> - ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + - ", CONSTRAINT chk_t_signed_long_l CHECK (typeof(l) = 'integer')" + ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" TestDB.ORACLE -> ", CONSTRAINT chk_t_signed_integer_id CHECK (${"id".inProperCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE})" + ", CONSTRAINT chk_t_signed_long_l CHECK (L BETWEEN ${Long.MIN_VALUE} AND ${Long.MAX_VALUE})" diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt index 80f1d5c9b0..ba127247c3 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt @@ -320,7 +320,7 @@ class CreateTableTests : DatabaseTestsBase() { fkName = fkName ) } - withDb { + withDb { testDb -> val t = TransactionManager.current() val expected = listOfNotNull( child.autoIncColumn?.autoIncColumnType?.sequence?.createStatement()?.single(), @@ -329,6 +329,11 @@ class CreateTableTests : DatabaseTestsBase() { " CONSTRAINT ${t.db.identifierManager.cutIfNecessaryAndQuote(fkName).inProperCase()}" + " FOREIGN KEY (${t.identity(child.parentId)})" + " REFERENCES ${t.identity(parent)}(${t.identity(parent.id)})" + + if (testDb == TestDB.ORACLE) { + ", CONSTRAINT chk_child1_signed_long_id CHECK (${this.identity(parent.id)} BETWEEN ${Long.MIN_VALUE} AND ${Long.MAX_VALUE})" + } else { + "" + } + ")" ) assertEqualCollections(child.ddl, expected) @@ -346,12 +351,17 @@ class CreateTableTests : DatabaseTestsBase() { onDelete = ReferenceOption.NO_ACTION, ) } - withDb { + withDb { testDb -> val expected = "CREATE TABLE " + addIfNotExistsIfSupported() + "${this.identity(child)} (" + "${child.columns.joinToString { it.descriptionDdl(false) }}," + " CONSTRAINT ${"fk_Child_parent_id__id".inProperCase()}" + " FOREIGN KEY (${this.identity(child.parentId)})" + " REFERENCES ${this.identity(parent)}(${this.identity(parent.id)})" + + if (testDb == TestDB.ORACLE) { + ", CONSTRAINT chk_Child_signed_long_id CHECK (${this.identity(parent.id)} BETWEEN ${Long.MIN_VALUE} AND ${Long.MAX_VALUE})" + } else { + "" + } + ")" assertEquals(child.ddl.last(), expected) } @@ -368,12 +378,17 @@ class CreateTableTests : DatabaseTestsBase() { onDelete = ReferenceOption.NO_ACTION, ) } - withDb { + withDb { testDb -> val expected = "CREATE TABLE " + addIfNotExistsIfSupported() + "${this.identity(child)} (" + "${child.columns.joinToString { it.descriptionDdl(false) }}," + " CONSTRAINT ${"fk_Child2_parent_id__id".inProperCase()}" + " FOREIGN KEY (${this.identity(child.parentId)})" + " REFERENCES ${this.identity(parent)}(${this.identity(parent.id)})" + + if (testDb == TestDB.ORACLE) { + ", CONSTRAINT chk_Child2_signed_long_id CHECK (${this.identity(parent.id)} BETWEEN ${Long.MIN_VALUE} AND ${Long.MAX_VALUE})" + } else { + "" + } + ")" assertEquals(child.ddl.last(), expected) } @@ -394,7 +409,7 @@ class CreateTableTests : DatabaseTestsBase() { fkName = fkName ) } - withDb { + withDb { testDb -> val t = TransactionManager.current() val expected = listOfNotNull( child.autoIncColumn?.autoIncColumnType?.sequence?.createStatement()?.single(), @@ -403,6 +418,11 @@ class CreateTableTests : DatabaseTestsBase() { " CONSTRAINT ${t.db.identifierManager.cutIfNecessaryAndQuote(fkName).inProperCase()}" + " FOREIGN KEY (${t.identity(child.parentId)})" + " REFERENCES ${t.identity(parent)}(${t.identity(parent.uniqueId)})" + + if (testDb == TestDB.ORACLE) { + ", CONSTRAINT chk_child2_signed_long_id CHECK (${this.identity(parent.id)} BETWEEN ${Long.MIN_VALUE} AND ${Long.MAX_VALUE})" + } else { + "" + } + ")" ) assertEqualCollections(child.ddl, expected) @@ -422,7 +442,7 @@ class CreateTableTests : DatabaseTestsBase() { fkName = fkName ) } - withDb { + withDb { testDb -> val t = TransactionManager.current() val expected = listOfNotNull( child.autoIncColumn?.autoIncColumnType?.sequence?.createStatement()?.single(), @@ -431,6 +451,11 @@ class CreateTableTests : DatabaseTestsBase() { " CONSTRAINT ${t.db.identifierManager.cutIfNecessaryAndQuote(fkName).inProperCase()}" + " FOREIGN KEY (${t.identity(child.parentId)})" + " REFERENCES ${t.identity(parent)}(${t.identity(parent.id)})" + + if (testDb == TestDB.ORACLE) { + ", CONSTRAINT chk_child3_signed_long_id CHECK (${this.identity(parent.id)} BETWEEN ${Long.MIN_VALUE} AND ${Long.MAX_VALUE})" + } else { + "" + } + ")" ) assertEqualCollections(child.ddl, expected) @@ -453,7 +478,7 @@ class CreateTableTests : DatabaseTestsBase() { fkName = fkName ) } - withDb { + withDb { testDb -> val t = TransactionManager.current() val expected = listOfNotNull( child.autoIncColumn?.autoIncColumnType?.sequence?.createStatement()?.single(), @@ -462,6 +487,11 @@ class CreateTableTests : DatabaseTestsBase() { " CONSTRAINT ${t.db.identifierManager.cutIfNecessaryAndQuote(fkName).inProperCase()}" + " FOREIGN KEY (${t.identity(child.parentId)})" + " REFERENCES ${t.identity(parent)}(${t.identity(parent.uniqueId)})" + + if (testDb == TestDB.ORACLE) { + ", CONSTRAINT chk_child4_signed_long_id CHECK (${this.identity(parent.id)} BETWEEN ${Long.MIN_VALUE} AND ${Long.MAX_VALUE})" + } else { + "" + } + ")" ) assertEqualCollections(child.ddl, expected) diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/NumericColumnTypesTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/NumericColumnTypesTests.kt index f457456d9e..85fe9726b1 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/NumericColumnTypesTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/NumericColumnTypesTests.kt @@ -117,7 +117,6 @@ class NumericColumnTypesTests : DatabaseTestsBase() { withTables(testTable) { testDb -> val columnName = testTable.long.nameInDatabaseCase() val ddlEnding = when (testDb) { - TestDB.SQLITE -> "CHECK (typeof($columnName) = 'integer'))" TestDB.ORACLE -> "CHECK ($columnName BETWEEN ${Long.MIN_VALUE} and ${Long.MAX_VALUE}))" else -> "($columnName ${testTable.long.columnType} NOT NULL)" } @@ -127,14 +126,18 @@ class NumericColumnTypesTests : DatabaseTestsBase() { testTable.insert { it[long] = Long.MAX_VALUE } assertEquals(2, testTable.select(testTable.long).count()) - val tableName = testTable.nameInDatabaseCase() - assertFailAndRollback(message = "Out-of-range error (or CHECK constraint violation for SQLite & Oracle)") { - val outOfRangeValue = Long.MIN_VALUE.toBigDecimal() - 1.toBigDecimal() - exec("INSERT INTO $tableName ($columnName) VALUES ($outOfRangeValue)") - } - assertFailAndRollback(message = "Out-of-range error (or CHECK constraint violation for SQLite & Oracle)") { - val outOfRangeValue = Long.MAX_VALUE.toBigDecimal() + 1.toBigDecimal() - exec("INSERT INTO $tableName ($columnName) VALUES ($outOfRangeValue)") + // SQLite is excluded because it is not possible to enforce the range without a special CHECK constraint + // that the user can implement if they want to + if (testDb != TestDB.SQLITE) { + val tableName = testTable.nameInDatabaseCase() + assertFailAndRollback(message = "Out-of-range error (or CHECK constraint violation for SQLite & Oracle)") { + val outOfRangeValue = Long.MIN_VALUE.toBigDecimal() - 1.toBigDecimal() + exec("INSERT INTO $tableName ($columnName) VALUES ($outOfRangeValue)") + } + assertFailAndRollback(message = "Out-of-range error (or CHECK constraint violation for SQLite & Oracle)") { + val outOfRangeValue = Long.MAX_VALUE.toBigDecimal() + 1.toBigDecimal() + exec("INSERT INTO $tableName ($columnName) VALUES ($outOfRangeValue)") + } } } }