mirror of
https://github.com/InsanusMokrassar/PlaguBot.git
synced 2024-11-01 05:23:47 +00:00
44 lines
1.4 KiB
Kotlin
44 lines
1.4 KiB
Kotlin
package dev.inmo.plagubot.config
|
|
|
|
import dev.inmo.kslog.common.e
|
|
import dev.inmo.kslog.common.logger
|
|
import kotlinx.coroutines.delay
|
|
import kotlinx.serialization.Serializable
|
|
import kotlinx.serialization.Transient
|
|
import org.jetbrains.exposed.sql.Database
|
|
import org.jetbrains.exposed.sql.transactions.transactionManager
|
|
import org.koin.core.scope.Scope
|
|
import org.sqlite.JDBC
|
|
import java.lang.Exception
|
|
import java.sql.Connection
|
|
|
|
inline val Scope.database: Database?
|
|
get() = getOrNull<Database>()
|
|
|
|
@Serializable
|
|
data class DatabaseConfig(
|
|
val url: String = "jdbc:sqlite:file:test?mode=memory&cache=shared",
|
|
val driver: String = JDBC::class.qualifiedName!!,
|
|
val username: String = "",
|
|
val password: String = "",
|
|
val reconnectOptions: DBConnectOptions? = DBConnectOptions()
|
|
) {
|
|
@Transient
|
|
val database: Database = (0 until (reconnectOptions ?.attempts ?: 1)).firstNotNullOfOrNull {
|
|
runCatching {
|
|
Database.connect(
|
|
url,
|
|
driver,
|
|
username,
|
|
password
|
|
).also {
|
|
it.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE // Or Connection.TRANSACTION_READ_UNCOMMITTED
|
|
it.connector().close()
|
|
}
|
|
}.onFailure {
|
|
logger.e(it)
|
|
Thread.sleep(reconnectOptions ?.delay ?: return@onFailure)
|
|
}.getOrNull()
|
|
} ?: error("Unable to create database")
|
|
}
|