Skip to content

Commit

Permalink
refactor!: Column type safety (#2027)
Browse files Browse the repository at this point in the history
This commit adds type safety in the overridden functions of the interface `IColumnType`, which will reduce the number of checks in those functions. This change propagates to other classes and interfaces that extend and use `IColumnType`.

The following are affected:

1. Custom column types
2. Custom functions extending `CustomFunction`
3. Custom operators extending `CustomOperator`
4. Custom expressions extending `ExpressionWithColumnType`
5. Usages of `CurrentTimestamp` function (class to object)
  • Loading branch information
joc-a authored Apr 11, 2024
1 parent 927878d commit 42ac41b
Show file tree
Hide file tree
Showing 57 changed files with 787 additions and 868 deletions.
107 changes: 73 additions & 34 deletions exposed-core/api/exposed-core.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ open class EntityID<T : Comparable<T>> protected constructor(val table: IdTable<
var _value: Any? = id

/** The identity value of type [T] wrapped by this [EntityID] instance. */
val value: T get() {
if (_value == null) {
invokeOnNoValue()
check(_value != null) { "Entity must be inserted" }
val value: T
get() {
if (_value == null) {
invokeOnNoValue()
check(_value != null) { "Entity must be inserted" }
}

@Suppress("UNCHECKED_CAST")
return _value!! as T
}

@Suppress("UNCHECKED_CAST")
return _value!! as T
}

/** Performs steps when the internal [_value] is accessed without first being initialized. */
protected open fun invokeOnNoValue() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Alias<out T : Table>(val delegate: T, val alias: String) : Table() {
/** The table name along with its [alias]. */
val tableNameWithAlias: String = "${delegate.tableName} $alias"

private fun <T : Any?> Column<T>.clone() = Column<T>(this@Alias, name, columnType)
private fun <T> Column<T>.clone() = Column<T>(this@Alias, name, columnType)

/**
* Returns the original column from the [delegate] table, or `null` if the [column] is not associated
Expand Down Expand Up @@ -111,7 +111,7 @@ class QueryAlias(val query: AbstractQuery<*>, val alias: String) : ColumnSet() {

override infix fun crossJoin(otherTable: ColumnSet): Join = Join(this, otherTable, JoinType.CROSS)

private fun <T : Any?> Column<T>.clone() = Column<T>(table.alias(alias), name, columnType)
private fun <T> Column<T>.clone() = Column<T>(table.alias(alias), name, columnType)
}

/**
Expand Down
17 changes: 9 additions & 8 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Column.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Column<T>(
/** Name of the column. */
val name: String,
/** Data type of the column. */
override val columnType: IColumnType
override val columnType: IColumnType<T & Any>
) : ExpressionWithColumnType<T>(), DdlAware, Comparable<Column<*>> {
/** The foreign key constraint on this column, or `null` if the column is not referencing. */
var foreignKey: ForeignKeyConstraint? = null
Expand Down Expand Up @@ -63,12 +63,13 @@ class Column<T>(
private val isLastColumnInPK: Boolean
get() = this == table.primaryKey?.columns?.last()

internal val isPrimaryConstraintWillBeDefined: Boolean get() = when {
currentDialect is SQLiteDialect && columnType.isAutoInc -> false
table.isCustomPKNameDefined() -> isLastColumnInPK
isOneColumnPK() -> false
else -> isLastColumnInPK
}
internal val isPrimaryConstraintWillBeDefined: Boolean
get() = when {
currentDialect is SQLiteDialect && columnType.isAutoInc -> false
table.isCustomPKNameDefined() -> isLastColumnInPK
isOneColumnPK() -> false
else -> isLastColumnInPK
}

override fun createStatement(): List<String> {
val alterTablePrefix = "ALTER TABLE ${TransactionManager.current().identity(table)} ADD"
Expand Down Expand Up @@ -154,7 +155,7 @@ class Column<T>(
/**
* Returns a copy of this column, but with the given column type.
*/
fun withColumnType(columnType: IColumnType) = Column<T>(
fun withColumnType(columnType: IColumnType<T & Any>) = Column<T>(
table = this.table,
name = this.name,
columnType = columnType
Expand Down
Loading

0 comments on commit 42ac41b

Please sign in to comment.