feat: support mysql and postgresql database
This commit is contained in:
9 files changed
+69
-8
No files matched your search
@@ -36,7 +36,10 @@ dependencies {
|
|||||||
implementation(libs.okhttp)
|
implementation(libs.okhttp)
|
||||||
implementation(libs.exposed.core)
|
implementation(libs.exposed.core)
|
||||||
implementation(libs.exposed.jdbc)
|
implementation(libs.exposed.jdbc)
|
||||||
|
implementation(libs.exposed.dao)
|
||||||
implementation(libs.sqlite.jdbc)
|
implementation(libs.sqlite.jdbc)
|
||||||
|
implementation(libs.mysql.jdbc)
|
||||||
|
implementation(libs.postgressql.jdbc)
|
||||||
implementation(libs.motd.pinger)
|
implementation(libs.motd.pinger)
|
||||||
implementation(libs.animated.gif.lib)
|
implementation(libs.animated.gif.lib)
|
||||||
implementation(libs.simple.java.mail)
|
implementation(libs.simple.java.mail)
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ logbackClassicVersion = "1.5.8"
|
|||||||
sl4jVersion = "2.0.16"
|
sl4jVersion = "2.0.16"
|
||||||
awsS3SDKVersion = "2.28.18"
|
awsS3SDKVersion = "2.28.18"
|
||||||
rconLibVersion = "1.1.0"
|
rconLibVersion = "1.1.0"
|
||||||
|
mysqlConnectorVersion = "9.1.0"
|
||||||
|
postgresqlVersion = "42.7.4"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
ronebot = { group = "cn.rtast", name = "ROneBot", version.ref = "ronebotVersion" }
|
ronebot = { group = "cn.rtast", name = "ROneBot", version.ref = "ronebotVersion" }
|
||||||
@@ -22,7 +24,10 @@ motd-pinger = { group = "cn.rtast", name = "MotdPinger", version.ref = "motdPing
|
|||||||
okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhttpVersion" }
|
okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhttpVersion" }
|
||||||
exposed-core = { group = "org.jetbrains.exposed", name = "exposed-core", version.ref = "exposedVersion" }
|
exposed-core = { group = "org.jetbrains.exposed", name = "exposed-core", version.ref = "exposedVersion" }
|
||||||
exposed-jdbc = { group = "org.jetbrains.exposed", name = "exposed-jdbc", version.ref = "exposedVersion" }
|
exposed-jdbc = { group = "org.jetbrains.exposed", name = "exposed-jdbc", version.ref = "exposedVersion" }
|
||||||
|
exposed-dao = { group = "org.jetbrains.exposed", name = "exposed-dao", version.ref = "exposedVersion" }
|
||||||
sqlite-jdbc = { group = "org.xerial", name = "sqlite-jdbc", version.ref = "sqliteJDBCVersion" }
|
sqlite-jdbc = { group = "org.xerial", name = "sqlite-jdbc", version.ref = "sqliteJDBCVersion" }
|
||||||
|
mysql-jdbc = { group = "com.mysql", name = "mysql-connector-j", version.ref = "mysqlConnectorVersion" }
|
||||||
|
postgressql-jdbc = { group = "org.postgresql", name = "postgresql", version.ref = "postgresqlVersion" }
|
||||||
animated-gif-lib = { group = "com.madgag", name = "animated-gif-lib", version.ref = "animatedGifLibVersion" }
|
animated-gif-lib = { group = "com.madgag", name = "animated-gif-lib", version.ref = "animatedGifLibVersion" }
|
||||||
simple-java-mail = { group = "org.simplejavamail", name = "simple-java-mail", version.ref = "simpleJavaMailVersion" }
|
simple-java-mail = { group = "org.simplejavamail", name = "simple-java-mail", version.ref = "simpleJavaMailVersion" }
|
||||||
jsoup = { group = "org.jsoup", name = "jsoup", version.ref = "jsoupVersion" }
|
jsoup = { group = "org.jsoup", name = "jsoup", version.ref = "jsoupVersion" }
|
||||||
|
|||||||
@@ -23,11 +23,11 @@ import cn.rtast.fancybot.commands.misc.JrrpCommand
|
|||||||
import cn.rtast.fancybot.commands.niuzi.NiuziRedeemCommand
|
import cn.rtast.fancybot.commands.niuzi.NiuziRedeemCommand
|
||||||
import cn.rtast.fancybot.commands.misc.ShortLinkCommand
|
import cn.rtast.fancybot.commands.misc.ShortLinkCommand
|
||||||
import cn.rtast.fancybot.entity.Config
|
import cn.rtast.fancybot.entity.Config
|
||||||
|
import cn.rtast.fancybot.enums.DatabaseType
|
||||||
import cn.rtast.fancybot.enums.ImageBedType
|
import cn.rtast.fancybot.enums.ImageBedType
|
||||||
import cn.rtast.fancybot.enums.ImageType
|
import cn.rtast.fancybot.enums.ImageType
|
||||||
import cn.rtast.fancybot.enums.WSType
|
import cn.rtast.fancybot.enums.WSType
|
||||||
import cn.rtast.fancybot.items.*
|
import cn.rtast.fancybot.items.*
|
||||||
import cn.rtast.fancybot.util.Logger
|
|
||||||
import cn.rtast.fancybot.util.file.BlackListManager
|
import cn.rtast.fancybot.util.file.BlackListManager
|
||||||
import cn.rtast.fancybot.util.file.ConfigManager
|
import cn.rtast.fancybot.util.file.ConfigManager
|
||||||
import cn.rtast.fancybot.util.file.NiuziBankManager
|
import cn.rtast.fancybot.util.file.NiuziBankManager
|
||||||
@@ -50,6 +50,14 @@ const val API_RTAST_URL = "https://api.rtast.cn"
|
|||||||
const val PBI_API_URL = "https://pbi.us.kg"
|
const val PBI_API_URL = "https://pbi.us.kg"
|
||||||
|
|
||||||
val DEFAULT_CONFIG = Config(
|
val DEFAULT_CONFIG = Config(
|
||||||
|
database = Config.Database(
|
||||||
|
DatabaseType.SQLite,
|
||||||
|
"127.0.0.1",
|
||||||
|
3306,
|
||||||
|
"root",
|
||||||
|
"114514",
|
||||||
|
"fancybot"
|
||||||
|
),
|
||||||
ncmAPI = "https://ncm.rtast.cn",
|
ncmAPI = "https://ncm.rtast.cn",
|
||||||
wsAddress = "ws://127.0.0.1",
|
wsAddress = "ws://127.0.0.1",
|
||||||
wsType = WSType.Client,
|
wsType = WSType.Client,
|
||||||
|
|||||||
@@ -10,10 +10,10 @@ package cn.rtast.fancybot.db
|
|||||||
import org.jetbrains.exposed.sql.Table
|
import org.jetbrains.exposed.sql.Table
|
||||||
|
|
||||||
object ActionTable : Table("action") {
|
object ActionTable : Table("action") {
|
||||||
val id = integer("id").autoIncrement().nullable()
|
val id = integer("id").autoIncrement()
|
||||||
val action = varchar("action", 255).nullable()
|
val action = varchar("action", 255)
|
||||||
val timestamp = long("timestamp").nullable()
|
val timestamp = long("timestamp")
|
||||||
val triggerId = long("trigger_id").nullable()
|
val triggerId = long("trigger_id")
|
||||||
val result = varchar("result", 255)
|
val result = text("result")
|
||||||
override val primaryKey = PrimaryKey(id)
|
override val primaryKey = PrimaryKey(id)
|
||||||
}
|
}
|
||||||
@@ -7,11 +7,13 @@
|
|||||||
|
|
||||||
package cn.rtast.fancybot.entity
|
package cn.rtast.fancybot.entity
|
||||||
|
|
||||||
|
import cn.rtast.fancybot.enums.DatabaseType
|
||||||
import cn.rtast.fancybot.enums.ImageBedType
|
import cn.rtast.fancybot.enums.ImageBedType
|
||||||
import cn.rtast.fancybot.enums.ImageType
|
import cn.rtast.fancybot.enums.ImageType
|
||||||
import cn.rtast.fancybot.enums.WSType
|
import cn.rtast.fancybot.enums.WSType
|
||||||
|
|
||||||
data class Config(
|
data class Config(
|
||||||
|
val database: Database,
|
||||||
val ncmAPI: String,
|
val ncmAPI: String,
|
||||||
val wsType: WSType,
|
val wsType: WSType,
|
||||||
val wsAddress: String,
|
val wsAddress: String,
|
||||||
@@ -49,4 +51,13 @@ data class Config(
|
|||||||
val apiRtastKey: String,
|
val apiRtastKey: String,
|
||||||
val azureAppClientId: String,
|
val azureAppClientId: String,
|
||||||
val youtubeDataApiKey: String,
|
val youtubeDataApiKey: String,
|
||||||
)
|
) {
|
||||||
|
data class Database(
|
||||||
|
val type: DatabaseType,
|
||||||
|
val host: String,
|
||||||
|
val port: Int,
|
||||||
|
val user: String,
|
||||||
|
val password: String,
|
||||||
|
val database: String,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2024 RTAkland
|
||||||
|
* Author: RTAkland
|
||||||
|
* Date: 2024/10/17
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package cn.rtast.fancybot.enums
|
||||||
|
|
||||||
|
enum class DatabaseType {
|
||||||
|
SQLite, MySQL, PostgreSQL
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
package cn.rtast.fancybot.util
|
package cn.rtast.fancybot.util
|
||||||
|
|
||||||
import cn.rtast.fancybot.ROOT_PATH
|
import cn.rtast.fancybot.ROOT_PATH
|
||||||
|
import cn.rtast.fancybot.configManager
|
||||||
import cn.rtast.fancybot.db.ActionTable
|
import cn.rtast.fancybot.db.ActionTable
|
||||||
import cn.rtast.fancybot.db.BlackListTable
|
import cn.rtast.fancybot.db.BlackListTable
|
||||||
import cn.rtast.fancybot.db.JrrpTable
|
import cn.rtast.fancybot.db.JrrpTable
|
||||||
@@ -15,13 +16,25 @@ import cn.rtast.fancybot.db.MinecraftAccessTokenTable
|
|||||||
import cn.rtast.fancybot.db.NiuziBankTable
|
import cn.rtast.fancybot.db.NiuziBankTable
|
||||||
import cn.rtast.fancybot.db.NiuziTable
|
import cn.rtast.fancybot.db.NiuziTable
|
||||||
import cn.rtast.fancybot.db.RCONTable
|
import cn.rtast.fancybot.db.RCONTable
|
||||||
|
import cn.rtast.fancybot.enums.DatabaseType
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import org.jetbrains.exposed.sql.Database
|
import org.jetbrains.exposed.sql.Database
|
||||||
import org.jetbrains.exposed.sql.SchemaUtils
|
import org.jetbrains.exposed.sql.SchemaUtils
|
||||||
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
||||||
|
|
||||||
suspend fun initDatabase() {
|
suspend fun initDatabase() {
|
||||||
Database.connect("jdbc:sqlite:file:$ROOT_PATH/data.sqlite", "org.sqlite.JDBC")
|
val host = configManager.dbConfig.host
|
||||||
|
val port = configManager.dbConfig.port
|
||||||
|
val user = configManager.dbConfig.user
|
||||||
|
val password = configManager.dbConfig.password
|
||||||
|
val database = configManager.dbConfig.database
|
||||||
|
if (configManager.dbConfig.type == DatabaseType.MySQL) {
|
||||||
|
Database.connect("jdbc:mysql://${host}:${port}/$database", "com.mysql.cj.jdbc.Driver", user, password)
|
||||||
|
} else if (configManager.dbConfig.type == DatabaseType.PostgreSQL) {
|
||||||
|
Database.connect("jdbc:postgresql://$host:$port/$database", driver = "org.postgresql.Driver", user, password)
|
||||||
|
} else {
|
||||||
|
Database.connect("jdbc:sqlite:file:$ROOT_PATH/data.sqlite", "org.sqlite.JDBC")
|
||||||
|
}
|
||||||
suspendedTransaction {
|
suspendedTransaction {
|
||||||
SchemaUtils.createMissingTablesAndColumns(JrrpTable)
|
SchemaUtils.createMissingTablesAndColumns(JrrpTable)
|
||||||
SchemaUtils.createMissingTablesAndColumns(NiuziBankTable)
|
SchemaUtils.createMissingTablesAndColumns(NiuziBankTable)
|
||||||
|
|||||||
@@ -49,4 +49,5 @@ class ConfigManager : JsonFileHandler<Config>("config.json") {
|
|||||||
val apiRtastKey = config.apiRtastKey
|
val apiRtastKey = config.apiRtastKey
|
||||||
val azureAppClientId = config.azureAppClientId
|
val azureAppClientId = config.azureAppClientId
|
||||||
val youtubeDataApiKey = config.youtubeDataApiKey
|
val youtubeDataApiKey = config.youtubeDataApiKey
|
||||||
|
val dbConfig = config.database
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,12 @@
|
|||||||
{
|
{
|
||||||
|
"database": {
|
||||||
|
"type": "SQLite",
|
||||||
|
"host": "127.0.0.1",
|
||||||
|
"port": 3306,
|
||||||
|
"user": "root",
|
||||||
|
"password": "114514",
|
||||||
|
"database": "fancybot"
|
||||||
|
},
|
||||||
"ncmAPI": "https://ncm.rtast.cn",
|
"ncmAPI": "https://ncm.rtast.cn",
|
||||||
"wsType": "Client",
|
"wsType": "Client",
|
||||||
"wsAddress": "ws://127.0.0.1",
|
"wsAddress": "ws://127.0.0.1",
|
||||||
|
|||||||
Reference in New Issue
Block a user