@@ -11,6 +11,7 @@ package me.stageguard.sctimetable.database
1111import com.zaxxer.hikari.HikariConfig
1212import com.zaxxer.hikari.HikariDataSource
1313import kotlinx.coroutines.Dispatchers
14+ import me.stageguard.sctimetable.*
1415import net.mamoe.mirai.utils.error
1516import net.mamoe.mirai.utils.info
1617import net.mamoe.mirai.utils.verbose
@@ -22,10 +23,11 @@ import org.jetbrains.exposed.sql.statements.StatementContext
2223import org.jetbrains.exposed.sql.statements.expandArgs
2324import org.jetbrains.exposed.sql.transactions.transaction
2425import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
25- import me.stageguard.sctimetable.PluginConfig
26- import me.stageguard.sctimetable.PluginMain
2726import me.stageguard.sctimetable.database.model.SchoolTimetables
2827import me.stageguard.sctimetable.database.model.Users
28+ import net.mamoe.mirai.console.util.ConsoleExperimentalApi
29+ import org.jetbrains.exposed.sql.transactions.TransactionManager
30+ import java.sql.Connection
2931
3032object Database {
3133
@@ -35,7 +37,7 @@ object Database {
3537 }
3638
3739 private lateinit var db : Database
38- private lateinit var hikariSource: HikariDataSource
40+ private var hikariSource: HikariDataSource ? = null
3941 private var connectionStatus: ConnectionStatus = ConnectionStatus .DISCONNECTED
4042
4143 fun <T > query (block : (Transaction ) -> T ) : T ? = if (connectionStatus == ConnectionStatus .DISCONNECTED ) {
@@ -49,11 +51,16 @@ object Database {
4951 } else newSuspendedTransaction(context = Dispatchers .IO , db = db) { block(this ) }
5052
5153 fun connect () {
52- db = Database .connect(hikariDataSourceProvider().also {
53- hikariSource = it
54- })
54+ db = when (PluginConfig .database) {
55+ " mysql" -> Database .connect(hikariDataSourceProvider(PluginConfig .mysqlConfig))
56+ " sqlite" -> {
57+ TransactionManager .manager.defaultIsolationLevel = Connection .TRANSACTION_SERIALIZABLE
58+ Database .connect(" jdbc:sqlite:${PluginMain .dataFolder} /${PluginConfig .sqliteConfig.file} " , " org.sqlite.JDBC" )
59+ }
60+ else -> throw InvalidDatabaseConfigException (" 未知的数据库类型或数据库配置未找到:${PluginConfig .database} " )
61+ }
5562 connectionStatus = ConnectionStatus .CONNECTED
56- PluginMain .logger.info { " Database ${ PluginConfig .database.table} is connected." }
63+ PluginMain .logger.info { " Database is connected." }
5764 initDatabase()
5865 }
5966
@@ -71,28 +78,29 @@ object Database {
7178
7279 fun close () {
7380 connectionStatus = ConnectionStatus .DISCONNECTED
74- hikariSource.closeQuietly()
81+ hikariSource? .closeQuietly()
7582 }
7683
77- private fun hikariDataSourceProvider () : HikariDataSource = HikariDataSource (HikariConfig ().apply {
84+ @OptIn(ConsoleExperimentalApi ::class )
85+ private fun hikariDataSourceProvider (config : MySQLorMariaDB ) : HikariDataSource = HikariDataSource (HikariConfig ().apply {
7886 when {
79- PluginConfig .database .address == " " -> throw InvalidDatabaseConfigException (" Database address is not set in config file ${PluginConfig .saveName} ." )
80- PluginConfig .database .table == " " -> {
87+ config .address == " " -> throw InvalidDatabaseConfigException (" Database address is not set in config file ${PluginConfig .saveName} ." )
88+ config .table == " " -> {
8189 PluginMain .logger.warning { " Database table is not set in config file ${PluginConfig .saveName} and now it will be default value 'sctimetabledb'." }
82- PluginConfig .database .table = " sctimetabledb"
90+ config .table = " sctimetabledb"
8391 }
84- PluginConfig .database .user == " " -> throw InvalidDatabaseConfigException (" Database user is not set in config file ${PluginConfig .saveName} ." )
85- PluginConfig .database .password == " " -> throw InvalidDatabaseConfigException (" Database password is not set in config file ${PluginConfig .saveName} ." )
86- PluginConfig .database .maximumPoolSize == null -> {
92+ config .user == " " -> throw InvalidDatabaseConfigException (" Database user is not set in config file ${PluginConfig .saveName} ." )
93+ config .password == " " -> throw InvalidDatabaseConfigException (" Database password is not set in config file ${PluginConfig .saveName} ." )
94+ config .maximumPoolSize == null -> {
8795 PluginMain .logger.warning { " Database maximumPoolSize is not set in config file ${PluginConfig .saveName} and now it will be default value 10." }
88- PluginConfig .database .maximumPoolSize = 10
96+ config .maximumPoolSize = 10
8997 }
9098 }
91- jdbcUrl = " jdbc:mysql://${PluginConfig .database. address} /${PluginConfig .database .table} "
99+ jdbcUrl = " jdbc:mysql://${config. address} /${config .table} "
92100 driverClassName = " com.mysql.cj.jdbc.Driver"
93- username = PluginConfig .database .user
94- password = PluginConfig .database .password
95- maximumPoolSize = PluginConfig .database .maximumPoolSize!!
101+ username = config .user
102+ password = config .password
103+ maximumPoolSize = config .maximumPoolSize!!
96104 poolName = " SCTimetableDB Pool"
97105 })
98106
0 commit comments