From 5f84822784446590273adaf8a88b8d544a34e1ba Mon Sep 17 00:00:00 2001 From: devgor88 Date: Thu, 5 Dec 2024 10:59:01 +0300 Subject: [PATCH] fix: Add support for creating sequence in MariaDB --- exposed-core/api/exposed-core.api | 8 ++++++++ .../main/kotlin/org/jetbrains/exposed/sql/Database.kt | 10 ++++++++++ .../sql/statements/api/ExposedDatabaseMetadata.kt | 6 ++++++ .../jetbrains/exposed/sql/vendors/MariaDBDialect.kt | 9 ++++++++- exposed-jdbc/api/exposed-jdbc.api | 2 ++ .../sql/statements/jdbc/JdbcDatabaseMetadataImpl.kt | 2 ++ 6 files changed, 36 insertions(+), 1 deletion(-) diff --git a/exposed-core/api/exposed-core.api b/exposed-core/api/exposed-core.api index fa6f0174e8..4c0ff210cd 100644 --- a/exposed-core/api/exposed-core.api +++ b/exposed-core/api/exposed-core.api @@ -662,6 +662,8 @@ public final class org/jetbrains/exposed/sql/Database { public final fun getDefaultFetchSize ()Ljava/lang/Integer; public final fun getDialect ()Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect; public final fun getIdentifierManager ()Lorg/jetbrains/exposed/sql/statements/api/IdentifierManagerApi; + public final fun getMajorVersion ()I + public final fun getMinorVersion ()I public final fun getSupportsAlterTableWithAddColumn ()Z public final fun getSupportsAlterTableWithDropColumn ()Z public final fun getSupportsMultipleResultSets ()Z @@ -669,6 +671,7 @@ public final class org/jetbrains/exposed/sql/Database { public final fun getUseNestedTransactions ()Z public final fun getVendor ()Ljava/lang/String; public final fun getVersion ()Ljava/math/BigDecimal; + public final fun isVersionCovers (II)Z public final fun isVersionCovers (Ljava/math/BigDecimal;)Z public final fun setUseNestedTransactions (Z)V public fun toString ()Ljava/lang/String; @@ -3562,6 +3565,8 @@ public abstract class org/jetbrains/exposed/sql/statements/api/ExposedDatabaseMe public abstract fun getDatabaseProductVersion ()Ljava/lang/String; public abstract fun getDefaultIsolationLevel ()I public abstract fun getIdentifierManager ()Lorg/jetbrains/exposed/sql/statements/api/IdentifierManagerApi; + public abstract fun getMajorVersion ()I + public abstract fun getMinorVersion ()I public abstract fun getSchemaNames ()Ljava/util/List; public abstract fun getSupportsAlterTableWithAddColumn ()Z public abstract fun getSupportsAlterTableWithDropColumn ()Z @@ -4142,10 +4147,13 @@ public final class org/jetbrains/exposed/sql/vendors/KeywordsKt { public final class org/jetbrains/exposed/sql/vendors/MariaDBDialect : org/jetbrains/exposed/sql/vendors/MysqlDialect { public static final field Companion Lorg/jetbrains/exposed/sql/vendors/MariaDBDialect$Companion; + public static final field SEQUENCE_MIN_MAJOR_VERSION I + public static final field SEQUENCE_MIN_MINOR_VERSION I public fun ()V public fun createIndex (Lorg/jetbrains/exposed/sql/Index;)Ljava/lang/String; public fun getFunctionProvider ()Lorg/jetbrains/exposed/sql/vendors/FunctionProvider; public fun getName ()Ljava/lang/String; + public fun getSupportsCreateSequence ()Z public fun getSupportsOnlyIdentifiersInGeneratedKeys ()Z public fun getSupportsSetDefaultReferenceOption ()Z } diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Database.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Database.kt index 20e3400f03..022f6c600c 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Database.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Database.kt @@ -64,6 +64,16 @@ class Database private constructor( /** Whether the version number of the database is equal to or greater than the provided [version]. */ fun isVersionCovers(version: BigDecimal) = this.version >= version + /** The major version number of the database as a [Int]. */ + val majorVersion by lazy { metadata { majorVersion } } + + /** The minor version number of the database as a [Int]. */ + val minorVersion by lazy { metadata { minorVersion } } + + /** Whether the version number of the database is equal to or greater than the provided [majorVersion] and [minorVersion]. */ + fun isVersionCovers(majorVersion: Int, minorVersion: Int) = + this.majorVersion >= majorVersion && this.minorVersion >= minorVersion + /** Whether the database supports ALTER TABLE with an add column clause. */ val supportsAlterTableWithAddColumn by lazy( LazyThreadSafetyMode.NONE diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/api/ExposedDatabaseMetadata.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/api/ExposedDatabaseMetadata.kt index 78271a0e70..2217c7107a 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/api/ExposedDatabaseMetadata.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/api/ExposedDatabaseMetadata.kt @@ -18,6 +18,12 @@ abstract class ExposedDatabaseMetadata(val database: String) { /** The version number of the database as a `BigDecimal`. */ abstract val version: BigDecimal + /** The major version number of the database. */ + abstract val majorVersion: Int + + /** The minor version number of the database. */ + abstract val minorVersion: Int + /** The name of the database based on the name of the underlying JDBC driver. */ abstract val databaseDialectName: String diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/MariaDBDialect.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/MariaDBDialect.kt index 4299efb852..e3ff4915dc 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/MariaDBDialect.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/MariaDBDialect.kt @@ -1,6 +1,7 @@ package org.jetbrains.exposed.sql.vendors import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.transactions.TransactionManager internal object MariaDBFunctionProvider : MysqlFunctionProvider() { override fun nextVal(seq: Sequence, builder: QueryBuilder) = builder { @@ -58,6 +59,9 @@ class MariaDBDialect : MysqlDialect() { override val functionProvider: FunctionProvider = MariaDBFunctionProvider override val supportsOnlyIdentifiersInGeneratedKeys: Boolean = true override val supportsSetDefaultReferenceOption: Boolean = false + override val supportsCreateSequence: Boolean by lazy { + TransactionManager.current().db.isVersionCovers(SEQUENCE_MIN_MAJOR_VERSION, SEQUENCE_MIN_MINOR_VERSION) + } override fun createIndex(index: Index): String { if (index.functions != null) { @@ -69,5 +73,8 @@ class MariaDBDialect : MysqlDialect() { return super.createIndex(index) } - companion object : DialectNameProvider("MariaDB") + companion object : DialectNameProvider("MariaDB") { + const val SEQUENCE_MIN_MAJOR_VERSION = 10 + const val SEQUENCE_MIN_MINOR_VERSION = 3 + } } diff --git a/exposed-jdbc/api/exposed-jdbc.api b/exposed-jdbc/api/exposed-jdbc.api index 5ee212d776..7e77bb5495 100644 --- a/exposed-jdbc/api/exposed-jdbc.api +++ b/exposed-jdbc/api/exposed-jdbc.api @@ -42,7 +42,9 @@ public final class org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadat public fun getDatabaseProductVersion ()Ljava/lang/String; public fun getDefaultIsolationLevel ()I public fun getIdentifierManager ()Lorg/jetbrains/exposed/sql/statements/api/IdentifierManagerApi; + public fun getMajorVersion ()I public final fun getMetadata ()Ljava/sql/DatabaseMetaData; + public fun getMinorVersion ()I public fun getSchemaNames ()Ljava/util/List; public fun getSupportsAlterTableWithAddColumn ()Z public fun getSupportsAlterTableWithDropColumn ()Z diff --git a/exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadataImpl.kt b/exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadataImpl.kt index e8d8a7d34b..38cc37baea 100644 --- a/exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadataImpl.kt +++ b/exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadataImpl.kt @@ -17,6 +17,8 @@ import java.util.concurrent.ConcurrentHashMap class JdbcDatabaseMetadataImpl(database: String, val metadata: DatabaseMetaData) : ExposedDatabaseMetadata(database) { override val url: String by lazyMetadata { url } override val version: BigDecimal by lazyMetadata { BigDecimal("$databaseMajorVersion.$databaseMinorVersion") } + override val majorVersion: Int by lazyMetadata { databaseMajorVersion } + override val minorVersion: Int by lazyMetadata { databaseMinorVersion } override val databaseDialectName: String by lazyMetadata { when (driverName) {