Skip to content

Commit

Permalink
Make JDBC Configuration from DataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
ayato-p committed Nov 13, 2024
1 parent 3fb05bb commit f24fb0f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
)
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -31,4 +33,23 @@ class JdbcConfigurationTest : FunSpec({
conf.connector.shouldBeInstanceOf<Connector>()
}
}

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<ConfigurationEntry>()
}

test("should be instance of Connector") {
val conf = entry.config as JdbcModuleConfiguration
conf.connector.shouldBeInstanceOf<Connector>()
}
}
})

0 comments on commit f24fb0f

Please sign in to comment.