Skip to content

Commit

Permalink
chore: Simplify foreignKeyPart
Browse files Browse the repository at this point in the history
  • Loading branch information
joc-a committed Sep 21, 2023
1 parent 4827c99 commit 92a57aa
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 54 deletions.
15 changes: 15 additions & 0 deletions exposed-core/api/exposed-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -3245,9 +3245,12 @@ public abstract interface class org/jetbrains/exposed/sql/vendors/DatabaseDialec
public abstract fun getSupportsDualTableConcept ()Z
public abstract fun getSupportsIfNotExists ()Z
public abstract fun getSupportsMultipleGeneratedKeys ()Z
public abstract fun getSupportsOnUpdate ()Z
public abstract fun getSupportsOnlyIdentifiersInGeneratedKeys ()Z
public abstract fun getSupportsOrderByNullsFirstLast ()Z
public abstract fun getSupportsRestrictReferenceOption ()Z
public abstract fun getSupportsSequenceAsGeneratedKeys ()Z
public abstract fun getSupportsSetDefaultReferenceOption ()Z
public abstract fun getSupportsSubqueryUnions ()Z
public abstract fun getSupportsTernaryAffectedRowValues ()Z
public abstract fun getSupportsWindowFrameGroupsMode ()Z
Expand Down Expand Up @@ -3285,9 +3288,12 @@ public final class org/jetbrains/exposed/sql/vendors/DatabaseDialect$DefaultImpl
public static fun getSupportsCreateSequence (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSupportsDualTableConcept (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSupportsIfNotExists (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSupportsOnUpdate (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSupportsOnlyIdentifiersInGeneratedKeys (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSupportsOrderByNullsFirstLast (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSupportsRestrictReferenceOption (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSupportsSequenceAsGeneratedKeys (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSupportsSetDefaultReferenceOption (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSupportsSubqueryUnions (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSupportsTernaryAffectedRowValues (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSupportsWindowFrameGroupsMode (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
Expand Down Expand Up @@ -3501,6 +3507,7 @@ public final class org/jetbrains/exposed/sql/vendors/MariaDBDialect : org/jetbra
public fun getFunctionProvider ()Lorg/jetbrains/exposed/sql/vendors/FunctionProvider;
public fun getName ()Ljava/lang/String;
public fun getSupportsOnlyIdentifiersInGeneratedKeys ()Z
public fun getSupportsSetDefaultReferenceOption ()Z
}

public final class org/jetbrains/exposed/sql/vendors/MariaDBDialect$Companion : org/jetbrains/exposed/sql/vendors/VendorDialect$DialectNameProvider {
Expand All @@ -3516,6 +3523,7 @@ public class org/jetbrains/exposed/sql/vendors/MysqlDialect : org/jetbrains/expo
protected fun fillConstraintCacheForTables (Ljava/util/List;)V
public fun getSupportsCreateSequence ()Z
public fun getSupportsOrderByNullsFirstLast ()Z
public fun getSupportsSetDefaultReferenceOption ()Z
public fun getSupportsSubqueryUnions ()Z
public fun getSupportsTernaryAffectedRowValues ()Z
public fun isAllowedAsColumnDefault (Lorg/jetbrains/exposed/sql/Expression;)Z
Expand All @@ -3542,8 +3550,11 @@ public class org/jetbrains/exposed/sql/vendors/OracleDialect : org/jetbrains/exp
public fun getSupportsDualTableConcept ()Z
public fun getSupportsIfNotExists ()Z
public fun getSupportsMultipleGeneratedKeys ()Z
public fun getSupportsOnUpdate ()Z
public fun getSupportsOnlyIdentifiersInGeneratedKeys ()Z
public fun getSupportsOrderByNullsFirstLast ()Z
public fun getSupportsRestrictReferenceOption ()Z
public fun getSupportsSetDefaultReferenceOption ()Z
public fun isAllowedAsColumnDefault (Lorg/jetbrains/exposed/sql/Expression;)Z
public fun listDatabases ()Ljava/lang/String;
public fun modifyColumn (Lorg/jetbrains/exposed/sql/Column;Lorg/jetbrains/exposed/sql/ColumnDiff;)Ljava/util/List;
Expand Down Expand Up @@ -3612,6 +3623,7 @@ public class org/jetbrains/exposed/sql/vendors/SQLServerDialect : org/jetbrains/
public fun getNeedsQuotesWhenSymbolsInNames ()Z
public fun getSupportsIfNotExists ()Z
public fun getSupportsOnlyIdentifiersInGeneratedKeys ()Z
public fun getSupportsRestrictReferenceOption ()Z
public fun getSupportsSequenceAsGeneratedKeys ()Z
public fun isAllowedAsColumnDefault (Lorg/jetbrains/exposed/sql/Expression;)Z
public fun listDatabases ()Ljava/lang/String;
Expand Down Expand Up @@ -3677,9 +3689,12 @@ public abstract class org/jetbrains/exposed/sql/vendors/VendorDialect : org/jetb
public fun getSupportsDualTableConcept ()Z
public fun getSupportsIfNotExists ()Z
public fun getSupportsMultipleGeneratedKeys ()Z
public fun getSupportsOnUpdate ()Z
public fun getSupportsOnlyIdentifiersInGeneratedKeys ()Z
public fun getSupportsOrderByNullsFirstLast ()Z
public fun getSupportsRestrictReferenceOption ()Z
public fun getSupportsSequenceAsGeneratedKeys ()Z
public fun getSupportsSetDefaultReferenceOption ()Z
public fun getSupportsSubqueryUnions ()Z
public fun getSupportsTernaryAffectedRowValues ()Z
public fun getSupportsWindowFrameGroupsMode ()Z
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.jetbrains.exposed.sql

import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.*
import org.jetbrains.exposed.sql.vendors.DatabaseDialect
import org.jetbrains.exposed.sql.vendors.MysqlDialect
import org.jetbrains.exposed.sql.vendors.SQLiteDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.jetbrains.exposed.sql.vendors.currentDialectIfAvailable
import org.jetbrains.exposed.sql.vendors.inProperCase
import java.sql.DatabaseMetaData
Expand Down Expand Up @@ -120,66 +123,34 @@ data class ForeignKeyConstraint(
append("FOREIGN KEY ($fromColumns) REFERENCES $targetTableName($targetColumns)")

if (deleteRule != ReferenceOption.NO_ACTION) {
when (deleteRule) {
ReferenceOption.RESTRICT -> {
when (currentDialect) {
is SQLServerDialect -> {
exposedLogger.warn(
"SQLServer doesn't support FOREIGN KEY with RESTRICT reference option with ON DELETE clause. " +
"Please check your $fromTableName table."
)
}
is OracleDialect -> {
exposedLogger.warn(
"Oracle doesn't support FOREIGN KEY with RESTRICT reference option with ON DELETE clause. " +
"Preventing the deletion of a parent row if a child row references it is the default behaviour." +
"Please check your $fromTableName table."
)
}
else -> append(" ON DELETE $deleteRule")
}
}
ReferenceOption.SET_DEFAULT -> {
when (currentDialect) {
is MariaDBDialect -> exposedLogger.warn(
"MariaDB doesn't support FOREIGN KEY with SET DEFAULT reference option with ON DELETE clause. " +
"Please check your $fromTableName table."
)
is MysqlDialect -> exposedLogger.warn(
"MySQL doesn't support FOREIGN KEY with SET DEFAULT reference option with ON DELETE clause. " +
"Please check your $fromTableName table."
)
is OracleDialect -> exposedLogger.warn(
"Oracle doesn't support FOREIGN KEY with SET DEFAULT reference option with ON DELETE clause. " +
"Please check your $fromTableName table."
)
else -> append(" ON DELETE $deleteRule")
}
}
else -> append(" ON DELETE $deleteRule")
if (deleteRule == ReferenceOption.RESTRICT && !currentDialect.supportsRestrictReferenceOption) {
exposedLogger.warn(
"${currentDialect.name} doesn't support FOREIGN KEY with RESTRICT reference option with ON DELETE clause. " +
"Please check your $fromTableName table."
)
} else if (deleteRule == ReferenceOption.SET_DEFAULT && !currentDialect.supportsSetDefaultReferenceOption) {
exposedLogger.warn(
"${currentDialect.name} doesn't support FOREIGN KEY with SET DEFAULT reference option with ON DELETE clause. " +
"Please check your $fromTableName table."
)
} else {
append(" ON DELETE $deleteRule")
}
}

if (updateRule != ReferenceOption.NO_ACTION) {
if (currentDialect is OracleDialect) {
exposedLogger.warn("Oracle doesn't support FOREIGN KEY with ON UPDATE clause. Please check your $fromTableName table.")
} else if (currentDialect is SQLServerDialect && updateRule == ReferenceOption.RESTRICT) {
if (!currentDialect.supportsOnUpdate) {
exposedLogger.warn("${currentDialect.name} doesn't support FOREIGN KEY with ON UPDATE clause. Please check your $fromTableName table.")
} else if (updateRule == ReferenceOption.RESTRICT && !currentDialect.supportsRestrictReferenceOption) {
exposedLogger.warn(
"${currentDialect.name} doesn't support FOREIGN KEY with RESTRICT reference option with ON UPDATE clause. " +
"Please check your $fromTableName table."
)
} else if (updateRule == ReferenceOption.SET_DEFAULT && !currentDialect.supportsSetDefaultReferenceOption) {
exposedLogger.warn(
"SQLServer doesn't support FOREIGN KEY with RESTRICT reference option with ON UPDATE clause. " +
"${currentDialect.name} doesn't support FOREIGN KEY with SET DEFAULT reference option with ON UPDATE clause. " +
"Please check your $fromTableName table."
)
} else if (updateRule == ReferenceOption.SET_DEFAULT) {
when (currentDialect) {
is MariaDBDialect -> exposedLogger.warn(
"MariaDB doesn't support FOREIGN KEY with SET DEFAULT reference option with ON UPDATE clause. " +
"Please check your $fromTableName table."
)
is MysqlDialect -> exposedLogger.warn(
"MySQL doesn't support FOREIGN KEY with SET DEFAULT reference option with ON UPDATE clause. " +
"Please check your $fromTableName table."
)
else -> append(" ON UPDATE $updateRule")
}
} else {
append(" ON UPDATE $updateRule")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ interface DatabaseDialect {
/** Returns `true` if the dialect supports window function definitions with GROUPS mode in frame clause */
val supportsWindowFrameGroupsMode: Boolean get() = false

val supportsOnUpdate: Boolean get() = true

val supportsSetDefaultReferenceOption: Boolean get() = true

val supportsRestrictReferenceOption: Boolean get() = true

val likePatternSpecialChars: Map<Char, Char?> get() = defaultLikePatternSpecialChars

/** Returns true if autoCommit should be enabled to create/drop database */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class MariaDBDialect : MysqlDialect() {
override val name: String = dialectName
override val functionProvider: FunctionProvider = MariaDBFunctionProvider
override val supportsOnlyIdentifiersInGeneratedKeys: Boolean = true
override val supportsSetDefaultReferenceOption: Boolean = false

override fun createIndex(index: Index): String {
if (index.functions != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ open class MysqlDialect : VendorDialect(dialectName, MysqlDataTypeProvider, Mysq

override val supportsOrderByNullsFirstLast: Boolean = false

override val supportsSetDefaultReferenceOption: Boolean = false

fun isFractionDateTimeSupported(): Boolean = TransactionManager.current().db.isVersionCovers(BigDecimal("5.6"))

// Available from MySQL 8.0.19
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ open class OracleDialect : VendorDialect(dialectName, OracleDataTypeProvider, Or
override val supportsOnlyIdentifiersInGeneratedKeys: Boolean = true
override val supportsDualTableConcept: Boolean = true
override val supportsOrderByNullsFirstLast: Boolean = true
override val supportsOnUpdate: Boolean = false
override val supportsSetDefaultReferenceOption: Boolean = false

// Preventing the deletion of a parent row if a child row references it is the default behaviour in Oracle.
override val supportsRestrictReferenceOption: Boolean = false

override fun isAllowedAsColumnDefault(e: Expression<*>): Boolean = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ open class SQLServerDialect : VendorDialect(dialectName, SQLServerDataTypeProvid
override val needsQuotesWhenSymbolsInNames: Boolean = false
override val supportsSequenceAsGeneratedKeys: Boolean = false
override val supportsOnlyIdentifiersInGeneratedKeys: Boolean = true
override val supportsRestrictReferenceOption: Boolean = false

private val nonAcceptableDefaults = arrayOf("DEFAULT")

Expand Down

0 comments on commit 92a57aa

Please sign in to comment.