Use enum for database values

- Breaks previous config files
This commit is contained in:
yitzy299 2021-09-17 21:33:19 +01:00
parent d259bda36b
commit 3b292146b2
4 changed files with 14 additions and 8 deletions

View File

@ -14,7 +14,7 @@ Add the following to the bottom of your Ledger config file:
```toml
[database_extensions]
h2 = true
database = "H2"
```
## MySQL
@ -23,7 +23,7 @@ Add the following to the bottom of your Ledger config file:
```toml
[database_extensions]
mysql = true
database = "MYSQL"
url = ""
username = ""
password = ""

View File

@ -3,8 +3,7 @@ package net.quiltservertools.ledger.databases
import com.github.quiltservertools.libs.com.uchuhimo.konf.ConfigSpec
object DatabaseExtensionSpec : ConfigSpec("database_extensions") {
val h2 by optional(false, "h2")
val mySql by optional(false, "mysql")
val database by optional(Databases.SQLITE, "database")
val userName by optional("root", "username")
val password by optional("", "password")
val url by optional("localhost", "url")

View File

@ -0,0 +1,7 @@
package net.quiltservertools.ledger.databases
enum class Databases {
SQLITE,
MYSQL,
H2
}

View File

@ -13,12 +13,12 @@ class LedgerDatabaseExtension : DatabaseExtension {
override fun getConfigSpecs(): List<ConfigSpec> = listOf(DatabaseExtensionSpec)
override fun getDatabase(server: MinecraftServer): Database {
if (Ledger.config[DatabaseExtensionSpec.h2]) {
if (Ledger.config[DatabaseExtensionSpec.database] == Databases.H2) {
return Database.connect(
url = "jdbc:h2:${server.getSavePath(WorldSavePath.ROOT).resolve("ledger.h2").toFile()};MODE=MySQL",
driver = "org.h2.Driver"
)
} else if (Ledger.config[DatabaseExtensionSpec.mySql]) {
} else if (Ledger.config[DatabaseExtensionSpec.database] == Databases.MYSQL) {
return Database.connect(
url = "jdbc:mysql://${Ledger.config[DatabaseExtensionSpec.url]}?rewriteBatchedStatements=true",
driver = "com.mysql.cj.jdbc.Driver",
@ -30,9 +30,9 @@ class LedgerDatabaseExtension : DatabaseExtension {
}
override fun getIdentifier(): Identifier {
if (Ledger.config.contains(DatabaseExtensionSpec.h2) && Ledger.config[DatabaseExtensionSpec.h2]) {
if (Ledger.config[DatabaseExtensionSpec.database] == Databases.H2) {
return h2Identifier
} else if (Ledger.config.contains(DatabaseExtensionSpec.mySql) && Ledger.config[DatabaseExtensionSpec.mySql]) {
} else if (Ledger.config[DatabaseExtensionSpec.database] == Databases.MYSQL) {
return mySqlIdentifier
}