From f24fb0f3f6ba0e641f969ea90b22aab0252fb901 Mon Sep 17 00:00:00 2001 From: ayato-p Date: Wed, 13 Nov 2024 19:25:53 +0900 Subject: [PATCH] Make JDBC Configuration from DataSource --- .../jdbc/config/JdbcConfiguration.kt | 24 +++++++++++++++---- .../jdbc/config/JdbcConfigurationTest.kt | 21 ++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/playtest-jdbc/src/main/kotlin/com/uzabase/playtest2/jdbc/config/JdbcConfiguration.kt b/playtest-jdbc/src/main/kotlin/com/uzabase/playtest2/jdbc/config/JdbcConfiguration.kt index 6c1c473..6c28d4c 100644 --- a/playtest-jdbc/src/main/kotlin/com/uzabase/playtest2/jdbc/config/JdbcConfiguration.kt +++ b/playtest-jdbc/src/main/kotlin/com/uzabase/playtest2/jdbc/config/JdbcConfiguration.kt @@ -9,13 +9,23 @@ import java.sql.DriverManager internal sealed interface Connector { fun connect(): Connection - data class JdbcUrl(val url: String): Connector { + companion object { + fun of(url: String) = JdbcUrl(url) + fun of(url: String, username: String, password: String) = JdbcUrlWithCredentials(url, username, password) + fun of(dataSource: javax.sql.DataSource) = DataSource(dataSource) + } + + data class JdbcUrl(val url: String) : Connector { override fun connect(): Connection = DriverManager.getConnection(url) } - data class JdbcUrlWithCredentials(val url: String, val username: String, val password: String): Connector { + data class JdbcUrlWithCredentials(val url: String, val username: String, val password: String) : Connector { override fun connect(): Connection = DriverManager.getConnection(url, username, password) } + + data class DataSource(val dataSource: javax.sql.DataSource) : Connector { + override fun connect(): Connection = dataSource.connection + } } internal data class JdbcModuleKey(val databaseName: String) : ModuleKey @@ -24,11 +34,17 @@ internal data class JdbcModuleConfiguration(val connector: Connector) : ModuleCo fun jdbc(databaseName: String, jdbcUrl: String): ConfigurationEntry = ConfigurationEntry( JdbcModuleKey(databaseName), - JdbcModuleConfiguration(Connector.JdbcUrl(jdbcUrl)) + JdbcModuleConfiguration(Connector.of(jdbcUrl)) ) fun jdbc(databaseName: String, jdbcUrl: String, username: String, password: String): ConfigurationEntry = ConfigurationEntry( JdbcModuleKey(databaseName), - JdbcModuleConfiguration(Connector.JdbcUrlWithCredentials(jdbcUrl, username, password)) + JdbcModuleConfiguration(Connector.of(jdbcUrl, username, password)) + ) + +fun jdbc(databaseName: String, dataSource: javax.sql.DataSource): ConfigurationEntry = + ConfigurationEntry( + JdbcModuleKey(databaseName), + JdbcModuleConfiguration(Connector.of(dataSource)) ) \ No newline at end of file diff --git a/playtest-jdbc/src/test/kotlin/com/uzabase/playtest2/jdbc/config/JdbcConfigurationTest.kt b/playtest-jdbc/src/test/kotlin/com/uzabase/playtest2/jdbc/config/JdbcConfigurationTest.kt index 58940cc..6226f12 100644 --- a/playtest-jdbc/src/test/kotlin/com/uzabase/playtest2/jdbc/config/JdbcConfigurationTest.kt +++ b/playtest-jdbc/src/test/kotlin/com/uzabase/playtest2/jdbc/config/JdbcConfigurationTest.kt @@ -3,6 +3,8 @@ package com.uzabase.playtest2.jdbc.config import com.uzabase.playtest2.core.config.ConfigurationEntry import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.types.shouldBeInstanceOf +import org.postgresql.ds.PGSimpleDataSource +import javax.sql.DataSource class JdbcConfigurationTest : FunSpec({ context("jdbc configuration with jdbc url") { @@ -31,4 +33,23 @@ class JdbcConfigurationTest : FunSpec({ conf.connector.shouldBeInstanceOf() } } + + context("jdbc configuration with DataSource") { + val entry = jdbc("test", PGSimpleDataSource().apply { + serverNames = arrayOf("localhost") + databaseName = "test" + portNumbers = intArrayOf(5432) + user = "alice" + password = "password" + }) + + test("should be instance of ConfigurationEntry") { + entry.shouldBeInstanceOf() + } + + test("should be instance of Connector") { + val conf = entry.config as JdbcModuleConfiguration + conf.connector.shouldBeInstanceOf() + } + } }) \ No newline at end of file