Skip to content

Commit 8dfc386

Browse files
committed
fix sql system
1 parent 486df94 commit 8dfc386

File tree

5 files changed

+64
-24
lines changed

5 files changed

+64
-24
lines changed

common/src/main/kotlin/me/regadpole/plumbot/PlumBot.kt

+17-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import me.regadpole.plumbot.bot.QQBot
66
import me.regadpole.plumbot.config.ConfigLoader
77
import me.regadpole.plumbot.config.ConfigResolver
88
import me.regadpole.plumbot.config.LangConfig
9+
import me.regadpole.plumbot.database.Database
10+
import me.regadpole.plumbot.database.MySQL
11+
import me.regadpole.plumbot.database.SQLite
912
import taboolib.common.platform.Plugin
1013
import taboolib.common.platform.function.disablePlugin
1114
import taboolib.common.platform.function.info
@@ -21,6 +24,8 @@ object PlumBot : Plugin() {
2124

2225
private lateinit var lang: LangConfig
2326

27+
private lateinit var database: Database
28+
2429
override fun onLoad() {
2530
ConfigResolver.loadConfig()
2631
config = ConfigResolver.getConfigLoader()
@@ -30,6 +35,13 @@ object PlumBot : Plugin() {
3035

3136
// 项目使用TabooLib Start Jar 创建!
3237
override fun onEnable() {
38+
database = when(config.getConfig().database.type?.lowercase()) {
39+
"sqlite" -> SQLite()
40+
"mysql" -> MySQL()
41+
else -> error("Unknown database type.")
42+
}
43+
database.initialize()
44+
info("Loaded database")
3345
info("Successfully running PlumBot!")
3446
}
3547

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

5668
override fun onDisable() {
57-
when (config.getConfig().bot.mode) {
58-
"go-cqhttp", "kook" -> {
59-
bot.shutdown()
60-
}
61-
else -> {
62-
warning("无法正常关闭服务,将在服务器关闭后强制关闭")
63-
disablePlugin()
64-
}
65-
}
69+
bot.shutdown()
70+
database.close()
6671
}
6772

6873
fun reloadConfig() {
@@ -81,5 +86,7 @@ object PlumBot : Plugin() {
8186
fun getLangConfig(): LangConfig {
8287
return lang
8388
}
84-
89+
fun getDatabase(): Database {
90+
return database
91+
}
8592
}

common/src/main/kotlin/me/regadpole/plumbot/database/Database.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ interface Database {
2626

2727
fun addBind(user: String, name: String)
2828

29-
fun removeBind(user: String, name: String)
29+
fun removeBind(user: String, id: Int)
3030
fun removeBind(user: String)
3131
fun removeBindByName(name: String)
3232

33-
fun getBind(user: String): List<String>
33+
fun getBind(user: String): MutableMap<Int, String>
3434
fun getBindByName(name: String): String?
3535
}

common/src/main/kotlin/me/regadpole/plumbot/database/MySQL.kt

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package me.regadpole.plumbot.database
22

33
import me.regadpole.plumbot.PlumBot
44
import taboolib.common.platform.function.info
5-
import taboolib.common.util.asList
65
import taboolib.module.database.ColumnOptionSQL
76
import taboolib.module.database.ColumnTypeSQL
87
import taboolib.module.database.Table
@@ -55,9 +54,9 @@ class MySQL: Database {
5554
}
5655
}
5756

58-
override fun removeBind(user: String, name: String) {
57+
override fun removeBind(user: String, id: Int) {
5958
table.delete(dataSource) {
60-
where { "user" eq user and ("name" eq name) }
59+
where { "user" eq user and ("id" eq id) }
6160
}
6261
}
6362

@@ -73,11 +72,15 @@ class MySQL: Database {
7372
}
7473
}
7574

76-
override fun getBind(user: String): List<String> {
77-
return table.select(dataSource) {
75+
override fun getBind(user: String): MutableMap<Int, String> {
76+
val map: MutableMap<Int, String> = LinkedHashMap()
77+
val result = table.select(dataSource) {
78+
rows("id")
7879
rows("name")
7980
where("user" eq user)
80-
}.asList()
81+
}
82+
result.forEach { map[getInt("id")] = getString("name") }
83+
return map
8184
}
8285

8386
override fun getBindByName(name: String): String? {

common/src/main/kotlin/me/regadpole/plumbot/database/SQLite.kt

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package me.regadpole.plumbot.database
22

33
import me.regadpole.plumbot.PlumBot
44
import taboolib.common.platform.function.info
5-
import taboolib.common.util.asList
65
import taboolib.module.database.ColumnTypeSQLite
76
import taboolib.module.database.Table
87
import javax.sql.DataSource
@@ -52,9 +51,9 @@ class SQLite: Database {
5251
}
5352
}
5453

55-
override fun removeBind(user: String, name: String) {
54+
override fun removeBind(user: String, id: Int) {
5655
table.delete(dataSource) {
57-
where { "user" eq user and ("name" eq name) }
56+
where { "user" eq user and ("id" eq id) }
5857
}
5958
}
6059

@@ -70,11 +69,15 @@ class SQLite: Database {
7069
}
7170
}
7271

73-
override fun getBind(user: String): List<String> {
74-
return table.select(dataSource) {
72+
override fun getBind(user: String): MutableMap<Int, String> {
73+
val map: MutableMap<Int, String> = LinkedHashMap()
74+
val result = table.select(dataSource) {
75+
rows("id")
7576
rows("name")
7677
where("user" eq user)
77-
}.asList()
78+
}
79+
result.forEach { map[getInt("id")] = getString("name") }
80+
return map
7881
}
7982

8083
override fun getBindByName(name: String): String? {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package me.regadpole.plumbot.internal
2+
3+
import me.regadpole.plumbot.PlumBot
4+
5+
6+
class WhitelistHelper {
7+
fun checkCount(user: String): Boolean {
8+
val idList = PlumBot.getDatabase().getBind(user).values
9+
val maxCount: Int = PlumBot.getConfig().getConfig().whiteList.maxCount
10+
if (idList.isEmpty()) return true
11+
return idList.size < maxCount
12+
}
13+
14+
fun checkIDNotExist(name: String): Boolean {
15+
return PlumBot.getDatabase().getBindByName(name).isNullOrEmpty()
16+
}
17+
18+
fun addAndGet(user: String, name: String): MutableMap<Int, String> {
19+
PlumBot.getDatabase().addBind(user, name)
20+
return PlumBot.getDatabase().getBind(user)
21+
}
22+
23+
fun removeAndGet(user: String, id: Int): MutableMap<Int, String> {
24+
PlumBot.getDatabase().removeBind(user, id)
25+
return PlumBot.getDatabase().getBind(user)
26+
}
27+
}

0 commit comments

Comments
 (0)