Skip to content

Commit

Permalink
Calculate schema name for a table once
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeySoshin committed Feb 1, 2023
1 parent e60a306 commit 2b421f0
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,15 @@ class Join(
onColumn != null && otherColumn != null -> {
join(otherTable, joinType, onColumn, otherColumn, additionalConstraint)
}

onColumn != null || otherColumn != null -> {
error("Can't prepare join on $table and $otherTable when only column from a one side provided.")
}

additionalConstraint != null -> {
join(otherTable, joinType, emptyList(), additionalConstraint)
}

else -> {
implicitJoin(otherTable, joinType)
}
Expand Down Expand Up @@ -250,10 +253,12 @@ class Join(
joinType != JoinType.CROSS && fkKeys.isEmpty() -> {
error("Cannot join with $otherTable as there is no matching primary key/foreign key pair and constraint missing")
}

fkKeys.any { it.second.size > 1 } -> {
val references = fkKeys.joinToString(" & ") { "${it.first} -> ${it.second.joinToString()}" }
error("Cannot join with $otherTable as there is multiple primary key <-> foreign key references.\n$references")
}

else -> {
val cond = fkKeys.filter { it.second.size == 1 }.map { it.first to it.second.single() }
join(otherTable, joinType, cond, null)
Expand Down Expand Up @@ -329,6 +334,17 @@ class Join(
*/
@Suppress("TooManyFunctions")
open class Table(name: String = "") : ColumnSet(), DdlAware {

val schema: String

init {
schema = if (name.contains(".")) {
name.substringBefore(".")
} else {
""
}
}

/** Returns the table name. */
open val tableName: String = when {
name.isNotEmpty() -> name
Expand All @@ -339,9 +355,6 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
val tableNameWithoutScheme: String
get() = tableName.substringAfter(".")

val schema: String
get() = tableName.substringBefore(".")

// Table name may contain quotes, remove those before appending
internal val tableNameWithoutSchemeSanitized: String get() = tableNameWithoutScheme.replace("\"", "").replace("'", "")

Expand Down Expand Up @@ -483,25 +496,25 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
fun byte(name: String): Column<Byte> = registerColumn(name, ByteColumnType())

/** Creates a numeric column, with the specified [name], for storing 1-byte unsigned integers. */
fun ubyte(name: String): Column<UByte> = registerColumn(name, UByteColumnType())
fun ubyte(name: String): Column<UByte> = registerColumn(name, UByteColumnType())

/** Creates a numeric column, with the specified [name], for storing 2-byte integers. */
fun short(name: String): Column<Short> = registerColumn(name, ShortColumnType())

/** Creates a numeric column, with the specified [name], for storing 2-byte unsigned integers. */
fun ushort(name: String): Column<UShort> = registerColumn(name, UShortColumnType())
fun ushort(name: String): Column<UShort> = registerColumn(name, UShortColumnType())

/** Creates a numeric column, with the specified [name], for storing 4-byte integers. */
fun integer(name: String): Column<Int> = registerColumn(name, IntegerColumnType())

/** Creates a numeric column, with the specified [name], for storing 4-byte unsigned integers. */
fun uinteger(name: String): Column<UInt> = registerColumn(name, UIntegerColumnType())
fun uinteger(name: String): Column<UInt> = registerColumn(name, UIntegerColumnType())

/** Creates a numeric column, with the specified [name], for storing 8-byte integers. */
fun long(name: String): Column<Long> = registerColumn(name, LongColumnType())

/** Creates a numeric column, with the specified [name], for storing 8-byte unsigned integers. */
fun ulong(name: String): Column<ULong> = registerColumn(name, ULongColumnType())
fun ulong(name: String): Column<ULong> = registerColumn(name, ULongColumnType())

/** Creates a numeric column, with the specified [name], for storing 4-byte (single precision) floating-point numbers. */
fun float(name: String): Column<Float> = registerColumn(name, FloatColumnType())
Expand Down Expand Up @@ -1079,6 +1092,7 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
is ColumnType -> {
this.withColumnType(AutoIncColumnType(columnType, idSeqName, "${tableName}_${name}_seq"))
}

else -> error("Unsupported column type for auto-increment $columnType")
}

Expand Down

0 comments on commit 2b421f0

Please sign in to comment.