mirror of
https://github.com/InsanusMokrassar/PlaguBot.git
synced 2024-06-26 03:28:03 +00:00
49 lines
1.3 KiB
Kotlin
49 lines
1.3 KiB
Kotlin
|
package dev.inmo.plagubot.config
|
||
|
|
||
|
import kotlinx.serialization.Serializable
|
||
|
import kotlinx.serialization.Transient
|
||
|
import org.jetbrains.exposed.sql.Database
|
||
|
import org.jetbrains.exposed.sql.transactions.transactionManager
|
||
|
import org.sqlite.JDBC
|
||
|
import java.sql.Connection
|
||
|
|
||
|
@Serializable
|
||
|
data class DatabaseConfig(
|
||
|
val url: String,
|
||
|
val driver: String = JDBC::class.qualifiedName!!,
|
||
|
val username: String = "",
|
||
|
val password: String = "",
|
||
|
val initAutomatically: Boolean = true
|
||
|
) {
|
||
|
@Transient
|
||
|
private lateinit var _database: Database
|
||
|
val database: Database
|
||
|
get() = try {
|
||
|
_database
|
||
|
} catch (e: UninitializedPropertyAccessException) {
|
||
|
Database.connect(
|
||
|
url,
|
||
|
driver,
|
||
|
username,
|
||
|
password
|
||
|
).also {
|
||
|
_database = it
|
||
|
it.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE // Or Connection.TRANSACTION_READ_UNCOMMITTED
|
||
|
}
|
||
|
}
|
||
|
|
||
|
init {
|
||
|
if (initAutomatically) {
|
||
|
database // init database
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Deprecated(
|
||
|
"Deprecated due to the replacement by lateinit database field with the same functionality",
|
||
|
ReplaceWith("database")
|
||
|
)
|
||
|
fun connect(): Database {
|
||
|
return database
|
||
|
}
|
||
|
}
|