Skip to content

Commit d4aab31

Browse files
committed
Introduce Mode enum for H2 compatibility modes with deprecation of old constants
1 parent 95c575a commit d4aab31

File tree

2 files changed

+53
-7
lines changed
  • dataframe-jdbc/src
    • main/kotlin/org/jetbrains/kotlinx/dataframe/io/db
    • test/kotlin/org/jetbrains/kotlinx/dataframe/io/h2

2 files changed

+53
-7
lines changed

dataframe-jdbc/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/db/H2.kt

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public open class H2(public val dialect: DbType = MySql) : DbType("h2") {
1818
require(dialect::class != H2::class) { "H2 database could not be specified with H2 dialect!" }
1919
}
2020

21+
2122
/**
2223
* It contains constants related to different database modes.
2324
*
@@ -29,16 +30,36 @@ public open class H2(public val dialect: DbType = MySql) : DbType("h2") {
2930
* @see [createH2Instance]
3031
*/
3132
public companion object {
32-
/** It represents the mode value "MySQL" for the H2 database. */
33-
public const val MODE_MYSQL: String = "MySQL"
33+
/**
34+
* Represents the compatibility modes supported by an H2 database.
35+
*
36+
* @property value The string value used in H2 JDBC URL and settings.
37+
*/
38+
public enum class Mode(public val value: String) {
39+
MySql("MySQL"),
40+
PostgreSql("PostgreSQL"),
41+
MsSqlServer("MSSQLServer"),
42+
MariaDb("MariaDB");
3443

35-
/** It represents the mode value "PostgreSQL" for the H2 database. */
36-
public const val MODE_POSTGRESQL: String = "PostgreSQL"
44+
public companion object {
45+
/**
46+
* Finds a Mode by its string value (case-insensitive).
47+
*
48+
* @param value The string value to search for.
49+
* @return The matching Mode, or null if not found.
50+
*/
51+
public fun fromValue(value: String): Mode? =
52+
entries.find { it.value.equals(value, ignoreCase = true) }
53+
}
54+
}
3755

38-
/** It represents the mode value "MSSQLServer" for the H2 database. */
56+
@Deprecated("Use Mode.MySql.value instead", ReplaceWith("Mode.MySql.value"))
57+
public const val MODE_MYSQL: String = "MySQL"
58+
@Deprecated("Use Mode.PostgreSql.value instead", ReplaceWith("Mode.PostgreSql.value"))
59+
public const val MODE_POSTGRESQL: String = "PostgreSQL"
60+
@Deprecated("Use Mode.MsSqlServer.value instead", ReplaceWith("Mode.MsSqlServer.value"))
3961
public const val MODE_MSSQLSERVER: String = "MSSQLServer"
40-
41-
/** It represents the mode value "MariaDB" for the H2 database. */
62+
@Deprecated("Use Mode.MariaDb.value instead", ReplaceWith("Mode.MariaDb.value"))
4263
public const val MODE_MARIADB: String = "MariaDB"
4364
}
4465

dataframe-jdbc/src/test/kotlin/org/jetbrains/kotlinx/dataframe/io/h2/h2Test.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,31 @@ class JdbcTest {
974974
exception.message shouldBe "H2 database could not be specified with H2 dialect!"
975975
}
976976

977+
978+
@Test
979+
fun `regular mode for H2 with DbConnectionConfig`() {
980+
val url = "jdbc:h2:mem:testDatabase"
981+
val username = "sa"
982+
val password = ""
983+
984+
val dbConfig = DbConnectionConfig(url, username, password)
985+
986+
val df = DataFrame.readSqlQuery(dbConfig, "SELECT 1")
987+
df.rowsCount() shouldBe 1
988+
}
989+
990+
@Test
991+
fun `regular mode for H2 with Connection`() {
992+
val url = "jdbc:h2:mem:testDatabase"
993+
val username = "sa"
994+
val password = ""
995+
996+
DriverManager.getConnection(url, username, password).use { connection ->
997+
val df = DataFrame.readSqlQuery(connection, "SELECT 1")
998+
df.rowsCount() shouldBe 1
999+
}
1000+
}
1001+
9771002
// helper object created for API testing purposes
9781003
object CustomDB : H2(MySql)
9791004

0 commit comments

Comments
 (0)