forked from SteamWar/SteamWar
Introduce GDPRQuery command for GDPR data generation, refactor SQL statements, and improve null safety across modules.
Signed-off-by: Chaoscaot <max@maxsp.de>
This commit is contained in:
@@ -53,7 +53,7 @@ object EventFightTable : IntIdTable("EventFight", "FightID") {
|
||||
val spectatePort = integer("SpectatePort").nullable()
|
||||
val bestOf = integer("BestOf")
|
||||
val ergebnis = integer("Ergebnis")
|
||||
val fight = integer("Fight").entityId().nullable()
|
||||
val fight = optReference("Fight", FightTable)
|
||||
}
|
||||
|
||||
class EventFight(id: EntityID<Int>) : IntEntity(id), Comparable<EventFight> {
|
||||
|
||||
@@ -45,7 +45,7 @@ public final class GameModeConfig<M, W> {
|
||||
}
|
||||
|
||||
private static String internalName(File f) {
|
||||
return f.getName().replace(".yml", "");
|
||||
return f != null ? f.getName().replace(".yml", "") : constWarGear(null);
|
||||
}
|
||||
|
||||
private static final Map<String, GameModeConfig<?, String>> byFileName;
|
||||
|
||||
@@ -36,7 +36,7 @@ import java.time.Instant
|
||||
import java.util.Date
|
||||
import java.util.function.Consumer
|
||||
|
||||
object PunishmentTable : IntIdTable("Punishment", "PunishmentId") {
|
||||
object PunishmentTable : IntIdTable("Punishments", "PunishmentId") {
|
||||
val userId = reference("UserId", SteamwarUserTable)
|
||||
val punisher = reference("Punisher", SteamwarUserTable)
|
||||
val type = enumerationByName("Type", 32, Punishment.PunishmentType::class)
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.jetbrains.exposed.v1.core.dao.id.EntityID
|
||||
import org.jetbrains.exposed.v1.core.dao.id.IntIdTable
|
||||
import org.jetbrains.exposed.v1.core.eq
|
||||
import org.jetbrains.exposed.v1.core.lowerCase
|
||||
import org.jetbrains.exposed.v1.core.or
|
||||
import org.jetbrains.exposed.v1.dao.IntEntity
|
||||
import org.jetbrains.exposed.v1.dao.IntEntityClass
|
||||
import org.jetbrains.exposed.v1.jdbc.select
|
||||
@@ -48,7 +49,7 @@ class Team(id: EntityID<Int>) : IntEntity(id) {
|
||||
fun byId(id: Int) = teamCache.computeIfAbsent(id) { useDb { Team[id] } }
|
||||
|
||||
@JvmStatic
|
||||
fun get(name: String) = useDb { find { TeamTable.name.lowerCase() eq name.lowercase() }.firstOrNull() }
|
||||
fun get(name: String) = useDb { find { TeamTable.name.lowerCase() eq name.lowercase() or (TeamTable.kuerzel.lowerCase() eq name.lowercase()) }.firstOrNull() }
|
||||
|
||||
@JvmStatic
|
||||
fun getAll() = useDb { all().toList() }
|
||||
@@ -70,7 +71,7 @@ class Team(id: EntityID<Int>) : IntEntity(id) {
|
||||
private set
|
||||
private var teamAddress by TeamTable.address
|
||||
private var teamPort by TeamTable.port
|
||||
val members by lazy { SteamwarUserTable.select(SteamwarUserTable.id).where { SteamwarUserTable.team eq teamId }.map { it[SteamwarUserTable.id].value } }
|
||||
val members by lazy { useDb { SteamwarUserTable.select(SteamwarUserTable.id).where { SteamwarUserTable.team eq teamId }.map { it[SteamwarUserTable.id].value } } }
|
||||
|
||||
fun size() = useDb { SteamwarUser.find { SteamwarUserTable.team eq teamId }.count().toInt() }
|
||||
fun disband(user: SteamwarUser) = useDb {
|
||||
|
||||
@@ -25,7 +25,6 @@ import org.jetbrains.exposed.v1.core.Expression
|
||||
import org.jetbrains.exposed.v1.core.ResultRow
|
||||
import org.jetbrains.exposed.v1.core.StdOutSqlLogger
|
||||
import org.jetbrains.exposed.v1.core.statements.StatementType
|
||||
import org.jetbrains.exposed.v1.core.statements.api.RowApi
|
||||
import org.jetbrains.exposed.v1.dao.IntEntity
|
||||
import org.jetbrains.exposed.v1.dao.IntEntityClass
|
||||
import org.jetbrains.exposed.v1.jdbc.Database
|
||||
@@ -34,14 +33,13 @@ import org.jetbrains.exposed.v1.jdbc.statements.jdbc.JdbcResult
|
||||
import org.jetbrains.exposed.v1.jdbc.transactions.TransactionManager
|
||||
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
|
||||
import java.io.File
|
||||
import java.sql.ResultSet
|
||||
import java.util.Properties
|
||||
|
||||
object KotlinDatabase {
|
||||
lateinit var db: Database
|
||||
var db: Database? = null
|
||||
|
||||
fun ensureConnected() {
|
||||
if(KotlinDatabase::db.isInitialized) return
|
||||
if(db != null) return
|
||||
|
||||
val file = File(System.getProperty("user.home"), "mysql.properties")
|
||||
|
||||
@@ -61,6 +59,12 @@ object KotlinDatabase {
|
||||
password = password
|
||||
)
|
||||
}
|
||||
|
||||
fun close() {
|
||||
db?.connector()?.close()
|
||||
TransactionManager.defaultDatabase?.let { TransactionManager.closeAndUnregister(it) }
|
||||
db = null
|
||||
}
|
||||
}
|
||||
|
||||
fun <T: Any?> useDb(statement: JdbcTransaction.() -> T): T {
|
||||
|
||||
@@ -19,15 +19,39 @@
|
||||
|
||||
package de.steamwar.sql.internal
|
||||
|
||||
import de.steamwar.sql.SteamwarUser
|
||||
import org.jetbrains.exposed.v1.core.*
|
||||
import org.jetbrains.exposed.v1.jdbc.name
|
||||
import org.jetbrains.exposed.v1.jdbc.transactions.TransactionManager
|
||||
import java.sql.ResultSet
|
||||
import java.sql.SQLException
|
||||
|
||||
class Statement {
|
||||
data class Statement(val statement: String) {
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun closeAll() = TransactionManager.defaultDatabase?.let { TransactionManager.closeAndUnregister(it) }
|
||||
fun closeAll() = KotlinDatabase.close()
|
||||
|
||||
@JvmStatic
|
||||
fun productionDatabase() = TransactionManager.defaultDatabase?.name == "production"
|
||||
}
|
||||
|
||||
fun <T> select(user: ResultSetUser<T>, vararg args: Any): T? = useDb {
|
||||
exec(statement, args = args.map { getArgType(it) }) {
|
||||
user.use(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getArgType(obj: Any) = when(obj) {
|
||||
is String -> VarCharColumnType() to obj
|
||||
is Int -> IntegerColumnType() to obj
|
||||
is Long -> LongColumnType() to obj
|
||||
is Boolean -> BooleanColumnType() to obj
|
||||
is SteamwarUser -> IntegerColumnType() to obj.id.value
|
||||
else -> error("Unknown type: ${obj::class.simpleName}")
|
||||
}
|
||||
|
||||
interface ResultSetUser<T> {
|
||||
@Throws(SQLException::class)
|
||||
fun use(rs: ResultSet): T?
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user