|
| 1 | +package com.peco2282.devcore.database.jdbc |
| 2 | + |
| 3 | +import com.peco2282.devcore.database.DatabaseBuilder |
| 4 | +import com.peco2282.devcore.database.DatabaseProvider |
| 5 | +import kotlinx.coroutines.Dispatchers |
| 6 | +import kotlinx.coroutines.withContext |
| 7 | +import org.jetbrains.exposed.v1.core.Transaction |
| 8 | +import org.jetbrains.exposed.v1.jdbc.Database |
| 9 | +import org.jetbrains.exposed.v1.jdbc.SchemaUtils |
| 10 | +import org.jetbrains.exposed.v1.jdbc.transactions.transaction |
| 11 | +import java.util.concurrent.CompletableFuture |
| 12 | + |
| 13 | +/** |
| 14 | + * Creates a [DatabaseProvider] using JDBC. |
| 15 | + * |
| 16 | + * @param builder The configuration builder for the database. |
| 17 | + * @return A configured [DatabaseProvider] instance. |
| 18 | + * @throws IllegalStateException If the database configuration is not specified. |
| 19 | + */ |
| 20 | +fun createJdbc(builder: DatabaseBuilder.() -> Unit): DatabaseProvider = DatabaseBuilderImpl().apply(builder).build() |
| 21 | + |
| 22 | +/** |
| 23 | + * Implementation of [DatabaseBuilder] for JDBC connections. |
| 24 | + */ |
| 25 | +internal class DatabaseBuilderImpl : DatabaseBuilder() { |
| 26 | + /** |
| 27 | + * Builds a [DatabaseProvider] with a JDBC connection. |
| 28 | + * Connects to the database and initializes the registered tables. |
| 29 | + * |
| 30 | + * @return A [DatabaseProvider] instance. |
| 31 | + * @throws IllegalStateException If the database configuration is not specified. |
| 32 | + */ |
| 33 | + override fun build(): DatabaseProvider { |
| 34 | + val cfg = config ?: throw IllegalStateException("Database config is not specified") |
| 35 | + val db = Database.connect( |
| 36 | + url = cfg.url, |
| 37 | + driver = cfg.driver, |
| 38 | + user = cfg.user, |
| 39 | + password = cfg.password |
| 40 | + ) |
| 41 | + |
| 42 | + // Table initialization |
| 43 | + transaction(db) { |
| 44 | + SchemaUtils.create(*tables.toTypedArray()) |
| 45 | + } |
| 46 | + |
| 47 | + return DatabaseProviderImpl(db) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Default implementation of [DatabaseProvider]. |
| 53 | + * |
| 54 | + * @property database The JetBrains Exposed [Database] instance. |
| 55 | + */ |
| 56 | +internal class DatabaseProviderImpl(val database: Database) : DatabaseProvider { |
| 57 | + /** |
| 58 | + * Executes a transaction synchronously. |
| 59 | + * |
| 60 | + * @param T The return type of the statement. |
| 61 | + * @param statement The code to execute within the transaction. |
| 62 | + * @return The result of the transaction. |
| 63 | + */ |
| 64 | + override fun <T> dbQuery(statement: Transaction.() -> T): T = transaction(database, statement = statement) |
| 65 | + |
| 66 | + /** |
| 67 | + * Executes a transaction asynchronously using a [CompletableFuture]. |
| 68 | + * |
| 69 | + * @param T The return type of the statement. |
| 70 | + * @param statement The code to execute within the transaction. |
| 71 | + * @return A [CompletableFuture] that will contain the result of the transaction. |
| 72 | + */ |
| 73 | + override fun <T> dbQueryAsync(statement: Transaction.() -> T): CompletableFuture<T> = CompletableFuture.supplyAsync { |
| 74 | + dbQuery(statement) |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Executes a transaction asynchronously using Kotlin coroutines. |
| 79 | + * This implementation uses [Dispatchers.IO] for non-blocking I/O. |
| 80 | + * |
| 81 | + * @param T The return type of the statement. |
| 82 | + * @param statement The code to execute within the transaction. |
| 83 | + * @return The result of the transaction. |
| 84 | + */ |
| 85 | + override suspend fun <T> dbQuerySuspend(statement: Transaction.() -> T): T = withContext(Dispatchers.IO) { |
| 86 | + dbQuery(statement) |
| 87 | + } |
| 88 | +} |
0 commit comments