Initial Commit
This commit is contained in:
83
src/main/kotlin/db/Database.kt
Normal file
83
src/main/kotlin/db/Database.kt
Normal file
@@ -0,0 +1,83 @@
|
||||
package de.steamwar.db
|
||||
|
||||
import com.github.ajalt.clikt.core.CliktError
|
||||
import org.jetbrains.exposed.v1.core.Expression
|
||||
import org.jetbrains.exposed.v1.core.ExpressionWithColumnType
|
||||
import org.jetbrains.exposed.v1.core.Function
|
||||
import org.jetbrains.exposed.v1.core.IColumnType
|
||||
import org.jetbrains.exposed.v1.core.LongColumnType
|
||||
import org.jetbrains.exposed.v1.core.QueryBuilder
|
||||
import org.jetbrains.exposed.v1.core.WindowFunction
|
||||
import org.jetbrains.exposed.v1.core.WindowFunctionDefinition
|
||||
import org.jetbrains.exposed.v1.core.append
|
||||
import org.jetbrains.exposed.v1.jdbc.Database
|
||||
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
|
||||
import java.io.File
|
||||
import java.sql.ResultSet
|
||||
import java.util.Properties
|
||||
import kotlin.time.ExperimentalTime
|
||||
import kotlin.time.Instant
|
||||
|
||||
class Database {
|
||||
val host: String
|
||||
val port: String
|
||||
val database: String
|
||||
|
||||
init {
|
||||
val config = File(System.getProperty("user.home"), "mysql.properties")
|
||||
|
||||
if (!config.exists()) {
|
||||
throw CliktError("Config file not found!")
|
||||
}
|
||||
|
||||
val props = Properties();
|
||||
|
||||
props.load(config.inputStream())
|
||||
|
||||
host = props.getProperty("host")
|
||||
port = props.getProperty("port")
|
||||
database = props.getProperty("database")
|
||||
|
||||
val username = props.getProperty("user")
|
||||
val password = props.getProperty("password")
|
||||
|
||||
val url = "jdbc:mariadb://$host:$port/$database"
|
||||
|
||||
Database.Companion.connect(url, driver = "org.mariadb.jdbc.Driver", user = username, password = password)
|
||||
}
|
||||
|
||||
fun <T> execute(sql: String, transform: (ResultSet) -> T): List<T> = transaction {
|
||||
val result = mutableListOf<T>()
|
||||
exec(sql) { rs ->
|
||||
while (rs.next()) {
|
||||
result += transform(rs)
|
||||
}
|
||||
}
|
||||
return@transaction result
|
||||
}
|
||||
|
||||
fun <T> executeSingle(sql: String, transform: (ResultSet) -> T): T? {
|
||||
return execute(sql) { rs ->
|
||||
if (!rs.next()) {
|
||||
return@execute null
|
||||
}
|
||||
transform(rs)
|
||||
}.single()
|
||||
}
|
||||
}
|
||||
|
||||
class UnixTimestamp<T: Any, in S>(
|
||||
val expr: Expression<in S>,
|
||||
columnType: IColumnType<T>
|
||||
) : Function<T?>(columnType), WindowFunction<T?> {
|
||||
override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder {
|
||||
append("UNIX_TIMESTAMP(", expr, ")")
|
||||
}
|
||||
|
||||
override fun over(): WindowFunctionDefinition<T?> {
|
||||
return WindowFunctionDefinition(columnType, this)
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalTime::class)
|
||||
fun ExpressionWithColumnType<Instant>.unixTimestamp(): UnixTimestamp<Long, Instant> = UnixTimestamp(this, LongColumnType())
|
||||
32
src/main/kotlin/db/schema/Punishments.kt
Normal file
32
src/main/kotlin/db/schema/Punishments.kt
Normal file
@@ -0,0 +1,32 @@
|
||||
package de.steamwar.db.schema
|
||||
|
||||
import org.jetbrains.exposed.v1.core.dao.id.EntityID
|
||||
import org.jetbrains.exposed.v1.core.dao.id.IntIdTable
|
||||
import org.jetbrains.exposed.v1.dao.IntEntity
|
||||
import org.jetbrains.exposed.v1.dao.IntEntityClass
|
||||
import org.jetbrains.exposed.v1.datetime.timestamp
|
||||
import kotlin.time.ExperimentalTime
|
||||
|
||||
@OptIn(ExperimentalTime::class)
|
||||
object Punishments: IntIdTable("Punishments", "PunishmentId") {
|
||||
val user = integer("userid").references(SteamwarUserTable.id)
|
||||
val punisher = integer("punisher").references(SteamwarUserTable.id)
|
||||
val type = varchar("type", 16)
|
||||
val reason = text("reason")
|
||||
val starttime = timestamp("starttime")
|
||||
val endtime = timestamp("endtime")
|
||||
val perma = bool("perma")
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalTime::class)
|
||||
class Punishment(id: EntityID<Int>): IntEntity(id) {
|
||||
companion object : IntEntityClass<Punishment>(Punishments)
|
||||
|
||||
var user by SteamwarUser referencedOn Punishments.user
|
||||
var punisher by SteamwarUser referencedOn Punishments.punisher
|
||||
var type by Punishments.type
|
||||
var reason by Punishments.reason
|
||||
var starttime by Punishments.starttime
|
||||
var endtime by Punishments.endtime
|
||||
var perma by Punishments.perma
|
||||
}
|
||||
12
src/main/kotlin/db/schema/Session.kt
Normal file
12
src/main/kotlin/db/schema/Session.kt
Normal file
@@ -0,0 +1,12 @@
|
||||
package de.steamwar.db.schema
|
||||
|
||||
import org.jetbrains.exposed.v1.core.Table
|
||||
import org.jetbrains.exposed.v1.datetime.timestamp
|
||||
import kotlin.time.ExperimentalTime
|
||||
|
||||
@OptIn(ExperimentalTime::class)
|
||||
object Session: Table("Session") {
|
||||
val user = integer("userid").references(SteamwarUserTable.id)
|
||||
val start = timestamp("starttime")
|
||||
val end = timestamp("endtime")
|
||||
}
|
||||
34
src/main/kotlin/db/schema/SteamwarUser.kt
Normal file
34
src/main/kotlin/db/schema/SteamwarUser.kt
Normal file
@@ -0,0 +1,34 @@
|
||||
package de.steamwar.db.schema
|
||||
|
||||
import org.jetbrains.exposed.v1.core.dao.id.EntityID
|
||||
import org.jetbrains.exposed.v1.core.dao.id.IntIdTable
|
||||
import org.jetbrains.exposed.v1.dao.IntEntity
|
||||
import org.jetbrains.exposed.v1.dao.IntEntityClass
|
||||
|
||||
object SteamwarUserTable: IntIdTable("UserData") {
|
||||
val uuid = uuid("uuid")
|
||||
val username = varchar("username", 255)
|
||||
val team = integer("team").references(TeamTable.id)
|
||||
val leader = bool("leader")
|
||||
val locale = varchar("locale", 16)
|
||||
val manualLocale = bool("manuallocale")
|
||||
val bedrock = bool("bedrock")
|
||||
val password = varchar("password", 1024)
|
||||
val discordId = long("DiscordId")
|
||||
}
|
||||
|
||||
class SteamwarUser(id: EntityID<Int>): IntEntity(id) {
|
||||
companion object : IntEntityClass<SteamwarUser>(SteamwarUserTable)
|
||||
|
||||
var uuid by SteamwarUserTable.uuid
|
||||
var username by SteamwarUserTable.username
|
||||
var team by Team referencedOn SteamwarUserTable.team
|
||||
var leader by SteamwarUserTable.leader
|
||||
var locale by SteamwarUserTable.locale
|
||||
var manualLocale by SteamwarUserTable.manualLocale
|
||||
var bedrock by SteamwarUserTable.bedrock
|
||||
var password by SteamwarUserTable.password
|
||||
var discordId by SteamwarUserTable.discordId
|
||||
|
||||
val punishments by Punishment referrersOn Punishments.user
|
||||
}
|
||||
26
src/main/kotlin/db/schema/Team.kt
Normal file
26
src/main/kotlin/db/schema/Team.kt
Normal file
@@ -0,0 +1,26 @@
|
||||
package de.steamwar.db.schema
|
||||
|
||||
import org.jetbrains.exposed.v1.core.dao.id.EntityID
|
||||
import org.jetbrains.exposed.v1.core.dao.id.IntIdTable
|
||||
import org.jetbrains.exposed.v1.dao.IntEntity
|
||||
import org.jetbrains.exposed.v1.dao.IntEntityClass
|
||||
|
||||
object TeamTable: IntIdTable("Team", "TeamId") {
|
||||
val kuerzel = varchar("TeamKuerzel", 16)
|
||||
val color = varchar("TeamColor", 16)
|
||||
val name = varchar("TeamName", 255)
|
||||
val deleted = bool("TeamDeleted")
|
||||
val address = varchar("Address", 16)
|
||||
val port = integer("Port")
|
||||
}
|
||||
|
||||
class Team(id: EntityID<Int>): IntEntity(id) {
|
||||
companion object : IntEntityClass<Team>(TeamTable)
|
||||
|
||||
var kuerzel by TeamTable.kuerzel
|
||||
var color by TeamTable.color
|
||||
var name by TeamTable.name
|
||||
var deleted by TeamTable.deleted
|
||||
var address by TeamTable.address
|
||||
var port by TeamTable.port
|
||||
}
|
||||
Reference in New Issue
Block a user