Skip to content

Commit

Permalink
fix sql system
Browse files Browse the repository at this point in the history
  • Loading branch information
RegadPoleCN committed Aug 29, 2024
1 parent 486df94 commit 8dfc386
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 24 deletions.
27 changes: 17 additions & 10 deletions common/src/main/kotlin/me/regadpole/plumbot/PlumBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import me.regadpole.plumbot.bot.QQBot
import me.regadpole.plumbot.config.ConfigLoader
import me.regadpole.plumbot.config.ConfigResolver
import me.regadpole.plumbot.config.LangConfig
import me.regadpole.plumbot.database.Database
import me.regadpole.plumbot.database.MySQL
import me.regadpole.plumbot.database.SQLite
import taboolib.common.platform.Plugin
import taboolib.common.platform.function.disablePlugin
import taboolib.common.platform.function.info
Expand All @@ -21,6 +24,8 @@ object PlumBot : Plugin() {

private lateinit var lang: LangConfig

private lateinit var database: Database

override fun onLoad() {
ConfigResolver.loadConfig()
config = ConfigResolver.getConfigLoader()
Expand All @@ -30,6 +35,13 @@ object PlumBot : Plugin() {

// 项目使用TabooLib Start Jar 创建!
override fun onEnable() {
database = when(config.getConfig().database.type?.lowercase()) {
"sqlite" -> SQLite()
"mysql" -> MySQL()
else -> error("Unknown database type.")
}
database.initialize()
info("Loaded database")
info("Successfully running PlumBot!")
}

Expand All @@ -54,15 +66,8 @@ object PlumBot : Plugin() {
}

override fun onDisable() {
when (config.getConfig().bot.mode) {
"go-cqhttp", "kook" -> {
bot.shutdown()
}
else -> {
warning("无法正常关闭服务,将在服务器关闭后强制关闭")
disablePlugin()
}
}
bot.shutdown()
database.close()
}

fun reloadConfig() {
Expand All @@ -81,5 +86,7 @@ object PlumBot : Plugin() {
fun getLangConfig(): LangConfig {
return lang
}

fun getDatabase(): Database {
return database
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ interface Database {

fun addBind(user: String, name: String)

fun removeBind(user: String, name: String)
fun removeBind(user: String, id: Int)
fun removeBind(user: String)
fun removeBindByName(name: String)

fun getBind(user: String): List<String>
fun getBind(user: String): MutableMap<Int, String>
fun getBindByName(name: String): String?
}
15 changes: 9 additions & 6 deletions common/src/main/kotlin/me/regadpole/plumbot/database/MySQL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package me.regadpole.plumbot.database

import me.regadpole.plumbot.PlumBot
import taboolib.common.platform.function.info
import taboolib.common.util.asList
import taboolib.module.database.ColumnOptionSQL
import taboolib.module.database.ColumnTypeSQL
import taboolib.module.database.Table
Expand Down Expand Up @@ -55,9 +54,9 @@ class MySQL: Database {
}
}

override fun removeBind(user: String, name: String) {
override fun removeBind(user: String, id: Int) {
table.delete(dataSource) {
where { "user" eq user and ("name" eq name) }
where { "user" eq user and ("id" eq id) }
}
}

Expand All @@ -73,11 +72,15 @@ class MySQL: Database {
}
}

override fun getBind(user: String): List<String> {
return table.select(dataSource) {
override fun getBind(user: String): MutableMap<Int, String> {
val map: MutableMap<Int, String> = LinkedHashMap()
val result = table.select(dataSource) {
rows("id")
rows("name")
where("user" eq user)
}.asList()
}
result.forEach { map[getInt("id")] = getString("name") }
return map
}

override fun getBindByName(name: String): String? {
Expand Down
15 changes: 9 additions & 6 deletions common/src/main/kotlin/me/regadpole/plumbot/database/SQLite.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package me.regadpole.plumbot.database

import me.regadpole.plumbot.PlumBot
import taboolib.common.platform.function.info
import taboolib.common.util.asList
import taboolib.module.database.ColumnTypeSQLite
import taboolib.module.database.Table
import javax.sql.DataSource
Expand Down Expand Up @@ -52,9 +51,9 @@ class SQLite: Database {
}
}

override fun removeBind(user: String, name: String) {
override fun removeBind(user: String, id: Int) {
table.delete(dataSource) {
where { "user" eq user and ("name" eq name) }
where { "user" eq user and ("id" eq id) }
}
}

Expand All @@ -70,11 +69,15 @@ class SQLite: Database {
}
}

override fun getBind(user: String): List<String> {
return table.select(dataSource) {
override fun getBind(user: String): MutableMap<Int, String> {
val map: MutableMap<Int, String> = LinkedHashMap()
val result = table.select(dataSource) {
rows("id")
rows("name")
where("user" eq user)
}.asList()
}
result.forEach { map[getInt("id")] = getString("name") }
return map
}

override fun getBindByName(name: String): String? {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.regadpole.plumbot.internal

import me.regadpole.plumbot.PlumBot


class WhitelistHelper {
fun checkCount(user: String): Boolean {
val idList = PlumBot.getDatabase().getBind(user).values
val maxCount: Int = PlumBot.getConfig().getConfig().whiteList.maxCount
if (idList.isEmpty()) return true
return idList.size < maxCount
}

fun checkIDNotExist(name: String): Boolean {
return PlumBot.getDatabase().getBindByName(name).isNullOrEmpty()
}

fun addAndGet(user: String, name: String): MutableMap<Int, String> {
PlumBot.getDatabase().addBind(user, name)
return PlumBot.getDatabase().getBind(user)
}

fun removeAndGet(user: String, id: Int): MutableMap<Int, String> {
PlumBot.getDatabase().removeBind(user, id)
return PlumBot.getDatabase().getBind(user)
}
}

0 comments on commit 8dfc386

Please sign in to comment.