Skip to content

Commit ffa3eef

Browse files
authored
feat: EXPOSED-654 Allow customizing the check constraint names of columns with check constraints (#2340)
1 parent 4f594d4 commit ffa3eef

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed

exposed-core/api/exposed-core.api

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2532,7 +2532,8 @@ public class org/jetbrains/exposed/sql/Table : org/jetbrains/exposed/sql/ColumnS
25322532
public final fun blob (Ljava/lang/String;Z)Lorg/jetbrains/exposed/sql/Column;
25332533
public static synthetic fun blob$default (Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;ZILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Column;
25342534
public final fun bool (Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
2535-
public final fun byte (Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
2535+
public final fun byte (Ljava/lang/String;Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
2536+
public static synthetic fun byte$default (Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Column;
25362537
public final fun char (Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
25372538
public final fun char (Ljava/lang/String;ILjava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
25382539
public static synthetic fun char$default (Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;ILjava/lang/String;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Column;
@@ -2582,7 +2583,8 @@ public class org/jetbrains/exposed/sql/Table : org/jetbrains/exposed/sql/ColumnS
25822583
public static synthetic fun index$default (Lorg/jetbrains/exposed/sql/Table;Lorg/jetbrains/exposed/sql/Column;Ljava/lang/String;ZILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Column;
25832584
public static synthetic fun index$default (Lorg/jetbrains/exposed/sql/Table;Z[Lorg/jetbrains/exposed/sql/Column;ILjava/lang/Object;)V
25842585
public fun innerJoin (Lorg/jetbrains/exposed/sql/ColumnSet;)Lorg/jetbrains/exposed/sql/Join;
2585-
public final fun integer (Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
2586+
public final fun integer (Ljava/lang/String;Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
2587+
public static synthetic fun integer$default (Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Column;
25862588
public fun join (Lorg/jetbrains/exposed/sql/ColumnSet;Lorg/jetbrains/exposed/sql/JoinType;Lorg/jetbrains/exposed/sql/Expression;Lorg/jetbrains/exposed/sql/Expression;ZLkotlin/jvm/functions/Function1;)Lorg/jetbrains/exposed/sql/Join;
25872589
public final fun largeText (Ljava/lang/String;Ljava/lang/String;Z)Lorg/jetbrains/exposed/sql/Column;
25882590
public static synthetic fun largeText$default (Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Column;
@@ -2618,23 +2620,27 @@ public class org/jetbrains/exposed/sql/Table : org/jetbrains/exposed/sql/ColumnS
26182620
public final fun registerCompositeColumn (Lorg/jetbrains/exposed/sql/CompositeColumn;)Lorg/jetbrains/exposed/sql/CompositeColumn;
26192621
public final fun replaceColumn (Lorg/jetbrains/exposed/sql/Column;Lorg/jetbrains/exposed/sql/Column;)Lorg/jetbrains/exposed/sql/Column;
26202622
public fun rightJoin (Lorg/jetbrains/exposed/sql/ColumnSet;)Lorg/jetbrains/exposed/sql/Join;
2621-
public final fun short (Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
2623+
public final fun short (Ljava/lang/String;Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
2624+
public static synthetic fun short$default (Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Column;
26222625
public final fun text (Ljava/lang/String;Ljava/lang/String;Z)Lorg/jetbrains/exposed/sql/Column;
26232626
public static synthetic fun text$default (Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Column;
26242627
public final fun transform (Lorg/jetbrains/exposed/sql/Column;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/exposed/sql/Column;
26252628
public final fun transform (Lorg/jetbrains/exposed/sql/Column;Lorg/jetbrains/exposed/sql/ColumnTransformer;)Lorg/jetbrains/exposed/sql/Column;
26262629
public final fun transformNullable (Lorg/jetbrains/exposed/sql/Column;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/exposed/sql/Column;
26272630
public final fun transformNullable (Lorg/jetbrains/exposed/sql/Column;Lorg/jetbrains/exposed/sql/ColumnTransformer;)Lorg/jetbrains/exposed/sql/Column;
2628-
public final fun ubyte (Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
2629-
public final fun uinteger (Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
2631+
public final fun ubyte (Ljava/lang/String;Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
2632+
public static synthetic fun ubyte$default (Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Column;
2633+
public final fun uinteger (Ljava/lang/String;Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
2634+
public static synthetic fun uinteger$default (Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Column;
26302635
public final fun ulong (Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
26312636
public final fun uniqueIndex (Ljava/lang/String;[Lorg/jetbrains/exposed/sql/Column;Ljava/util/List;Lkotlin/jvm/functions/Function1;)V
26322637
public final fun uniqueIndex (Lorg/jetbrains/exposed/sql/Column;Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
26332638
public final fun uniqueIndex ([Lorg/jetbrains/exposed/sql/Column;Lkotlin/jvm/functions/Function1;)V
26342639
public static synthetic fun uniqueIndex$default (Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;[Lorg/jetbrains/exposed/sql/Column;Ljava/util/List;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
26352640
public static synthetic fun uniqueIndex$default (Lorg/jetbrains/exposed/sql/Table;Lorg/jetbrains/exposed/sql/Column;Ljava/lang/String;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Column;
26362641
public static synthetic fun uniqueIndex$default (Lorg/jetbrains/exposed/sql/Table;[Lorg/jetbrains/exposed/sql/Column;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
2637-
public final fun ushort (Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
2642+
public final fun ushort (Ljava/lang/String;Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
2643+
public static synthetic fun ushort$default (Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Column;
26382644
public final fun uuid (Ljava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
26392645
public final fun varchar (Ljava/lang/String;ILjava/lang/String;)Lorg/jetbrains/exposed/sql/Column;
26402646
public static synthetic fun varchar$default (Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;ILjava/lang/String;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Column;

exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -708,48 +708,57 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
708708

709709
// Numeric columns
710710

711-
/** Creates a numeric column, with the specified [name], for storing 1-byte integers. */
712-
fun byte(name: String): Column<Byte> = registerColumn(name, ByteColumnType()).apply {
713-
check("${generatedSignedCheckPrefix}byte_${this.unquotedName()}") { it.between(Byte.MIN_VALUE, Byte.MAX_VALUE) }
711+
/** Creates a numeric column, with the specified [name], for storing 1-byte integers.
712+
* An optional [checkConstraintName] can be passed to allow customizing the check constraint name when needed.
713+
*/
714+
fun byte(name: String, checkConstraintName: String? = null): Column<Byte> = registerColumn(name, ByteColumnType()).apply {
715+
check(checkConstraintName ?: "${generatedSignedCheckPrefix}byte_${this.unquotedName()}") { it.between(Byte.MIN_VALUE, Byte.MAX_VALUE) }
714716
}
715717

716718
/** Creates a numeric column, with the specified [name], for storing 1-byte unsigned integers.
719+
* An optional [checkConstraintName] can be passed to allow customizing the check constraint name when needed.
717720
*
718721
* **Note:** If the database being used is not MySQL, MariaDB, or SQL Server, this column will use the
719722
* database's 2-byte integer type with a check constraint that ensures storage of only values
720723
* between 0 and [UByte.MAX_VALUE] inclusive.
721724
*/
722-
fun ubyte(name: String): Column<UByte> = registerColumn(name, UByteColumnType()).apply {
723-
check("${generatedUnsignedCheckPrefix}byte_${this.unquotedName()}") { it.between(0u, UByte.MAX_VALUE) }
725+
fun ubyte(name: String, checkConstraintName: String? = null): Column<UByte> = registerColumn(name, UByteColumnType()).apply {
726+
check(checkConstraintName ?: "${generatedUnsignedCheckPrefix}byte_${this.unquotedName()}") { it.between(0u, UByte.MAX_VALUE) }
724727
}
725728

726-
/** Creates a numeric column, with the specified [name], for storing 2-byte integers. */
727-
fun short(name: String): Column<Short> = registerColumn(name, ShortColumnType()).apply {
728-
check("${generatedSignedCheckPrefix}short_${this.unquotedName()}") { it.between(Short.MIN_VALUE, Short.MAX_VALUE) }
729+
/** Creates a numeric column, with the specified [name], for storing 2-byte integers.
730+
* An optional [checkConstraintName] can be passed to allow customizing the check constraint name when needed.
731+
*/
732+
fun short(name: String, checkConstraintName: String? = null): Column<Short> = registerColumn(name, ShortColumnType()).apply {
733+
check(checkConstraintName ?: "${generatedSignedCheckPrefix}short_${this.unquotedName()}") { it.between(Short.MIN_VALUE, Short.MAX_VALUE) }
729734
}
730735

731736
/** Creates a numeric column, with the specified [name], for storing 2-byte unsigned integers.
737+
* An optional [checkConstraintName] can be passed to allow customizing the check constraint name when needed.
732738
*
733739
* **Note:** If the database being used is not MySQL or MariaDB, this column will use the database's 4-byte
734740
* integer type with a check constraint that ensures storage of only values between 0 and [UShort.MAX_VALUE] inclusive.
735741
*/
736-
fun ushort(name: String): Column<UShort> = registerColumn(name, UShortColumnType()).apply {
737-
check("$generatedUnsignedCheckPrefix${this.unquotedName()}") { it.between(0u, UShort.MAX_VALUE) }
742+
fun ushort(name: String, checkConstraintName: String? = null): Column<UShort> = registerColumn(name, UShortColumnType()).apply {
743+
check(checkConstraintName ?: "$generatedUnsignedCheckPrefix${this.unquotedName()}") { it.between(0u, UShort.MAX_VALUE) }
738744
}
739745

740-
/** Creates a numeric column, with the specified [name], for storing 4-byte integers. */
741-
fun integer(name: String): Column<Int> = registerColumn(name, IntegerColumnType()).apply {
742-
check("${generatedSignedCheckPrefix}integer_${this.unquotedName()}") { it.between(Int.MIN_VALUE, Int.MAX_VALUE) }
746+
/** Creates a numeric column, with the specified [name], for storing 4-byte integers.
747+
* An optional [checkConstraintName] can be passed to allow customizing the check constraint name when needed.
748+
*/
749+
fun integer(name: String, checkConstraintName: String? = null): Column<Int> = registerColumn(name, IntegerColumnType()).apply {
750+
check(checkConstraintName ?: "${generatedSignedCheckPrefix}integer_${this.unquotedName()}") { it.between(Int.MIN_VALUE, Int.MAX_VALUE) }
743751
}
744752

745753
/** Creates a numeric column, with the specified [name], for storing 4-byte unsigned integers.
754+
* An optional [checkConstraintName] can be passed to allow customizing the check constraint name when needed.
746755
*
747756
* **Note:** If the database being used is not MySQL or MariaDB, this column will use the database's
748757
* 8-byte integer type with a check constraint that ensures storage of only values
749758
* between 0 and [UInt.MAX_VALUE] inclusive.
750759
*/
751-
fun uinteger(name: String): Column<UInt> = registerColumn(name, UIntegerColumnType()).apply {
752-
check("$generatedUnsignedCheckPrefix${this.unquotedName()}") { it.between(0u, UInt.MAX_VALUE) }
760+
fun uinteger(name: String, checkConstraintName: String? = null): Column<UInt> = registerColumn(name, UIntegerColumnType()).apply {
761+
check(checkConstraintName ?: "$generatedUnsignedCheckPrefix${this.unquotedName()}") { it.between(0u, UInt.MAX_VALUE) }
753762
}
754763

755764
/** Creates a numeric column, with the specified [name], for storing 8-byte integers. */

exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/NumericColumnTypesTests.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,35 @@ class NumericColumnTypesTests : DatabaseTestsBase() {
161161
)
162162
}
163163
}
164+
165+
@Test
166+
fun testCustomCheckConstraintName() {
167+
val tester = object : Table("tester") {
168+
val byte = byte("byte_column", checkConstraintName = "custom_byte_check")
169+
val ubyte = ubyte("ubyte_column", checkConstraintName = "custom_ubyte_check")
170+
val short = short("short_column", checkConstraintName = "custom_short_check")
171+
val ushort = ushort("ushort_column", checkConstraintName = "custom_ushort_check")
172+
val integer = integer("integer_column", checkConstraintName = "custom_integer_check")
173+
val uinteger = uinteger("uinteger_column", checkConstraintName = "custom_uinteger_check")
174+
}
175+
176+
withTables(tester) {
177+
assertEquals(
178+
"CREATE TABLE ${addIfNotExistsIfSupported()}${tester.nameInDatabaseCase()} (" +
179+
"${tester.byte.nameInDatabaseCase()} ${tester.byte.columnType} NOT NULL, " +
180+
"${tester.ubyte.nameInDatabaseCase()} ${tester.ubyte.columnType} NOT NULL, " +
181+
"${tester.short.nameInDatabaseCase()} ${tester.short.columnType} NOT NULL, " +
182+
"${tester.ushort.nameInDatabaseCase()} ${tester.ushort.columnType} NOT NULL, " +
183+
"${tester.integer.nameInDatabaseCase()} ${tester.integer.columnType} NOT NULL, " +
184+
"${tester.uinteger.nameInDatabaseCase()} ${tester.uinteger.columnType} NOT NULL, " +
185+
"CONSTRAINT custom_byte_check CHECK (${tester.byte.nameInDatabaseCase()} BETWEEN ${Byte.MIN_VALUE} AND ${Byte.MAX_VALUE}), " +
186+
"CONSTRAINT custom_ubyte_check CHECK (${tester.ubyte.nameInDatabaseCase()} BETWEEN 0 AND ${UByte.MAX_VALUE}), " +
187+
"CONSTRAINT custom_short_check CHECK (${tester.short.nameInDatabaseCase()} BETWEEN ${Short.MIN_VALUE} AND ${Short.MAX_VALUE}), " +
188+
"CONSTRAINT custom_ushort_check CHECK (${tester.ushort.nameInDatabaseCase()} BETWEEN 0 AND ${UShort.MAX_VALUE}), " +
189+
"CONSTRAINT custom_integer_check CHECK (${tester.integer.nameInDatabaseCase()} BETWEEN ${Int.MIN_VALUE} AND ${Int.MAX_VALUE}), " +
190+
"CONSTRAINT custom_uinteger_check CHECK (${tester.uinteger.nameInDatabaseCase()} BETWEEN 0 AND ${UInt.MAX_VALUE}))",
191+
tester.ddl
192+
)
193+
}
194+
}
164195
}

0 commit comments

Comments
 (0)