From 7acd51fd61ede10cb8fadcf939891538141e1ece Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 5 Jul 2026 13:45:59 +0200 Subject: [PATCH] Fix Reset Signed-off-by: Chaoscaot --- CLI/src/commands/database/ResetCommand.kt | 12 ++- CLI/src/db/Database.kt | 107 ++++++++++++++++++++++ 2 files changed, 115 insertions(+), 4 deletions(-) diff --git a/CLI/src/commands/database/ResetCommand.kt b/CLI/src/commands/database/ResetCommand.kt index e7fe1688..89fdae5f 100644 --- a/CLI/src/commands/database/ResetCommand.kt +++ b/CLI/src/commands/database/ResetCommand.kt @@ -7,6 +7,7 @@ import com.github.ajalt.mordant.rendering.TextColors import com.github.ajalt.mordant.rendering.TextStyles import de.steamwar.db.Database import de.steamwar.db.execute +import de.steamwar.db.executeScript import de.steamwar.db.useDb import java.io.File @@ -24,17 +25,20 @@ class ResetCommand : CliktCommand() { execute("SET FOREIGN_KEY_CHECKS=0;") { } - val tables = execute("SHOW TABLES;") { it.getString(1) } - for (table in tables) { + val databaseObjects = execute("SHOW FULL TABLES;") { it.getString(1) to it.getString(2) } + for (view in databaseObjects.filter { it.second == "VIEW" }.map { it.first }) { + execute("DROP VIEW IF EXISTS `${view.replace("`", "``")}`;") { } + } + for (table in databaseObjects.filter { it.second == "BASE TABLE" }.map { it.first }) { execute("DROP TABLE IF EXISTS `${table.replace("`", "``")}`;") { } } - execute(schema) { } + executeScript(schema) val seed = javaClass.getResource("/db/reset-seed.sql") ?: throw CliktError("Reset seed file not found!") - execute(seed.readText()) { } + executeScript(seed.readText()) execute("SET FOREIGN_KEY_CHECKS=1;") { } diff --git a/CLI/src/db/Database.kt b/CLI/src/db/Database.kt index e68dc28d..abe85bdf 100644 --- a/CLI/src/db/Database.kt +++ b/CLI/src/db/Database.kt @@ -83,6 +83,113 @@ fun JdbcTransaction.executeSingle(sql: String, transform: (ResultSet) -> T): }.single() } +fun JdbcTransaction.executeScript(sql: String) { + for (statement in splitSqlScript(sql)) { + exec(statement) { } + } +} + +private fun splitSqlScript(sql: String): List { + val statements = mutableListOf() + val current = StringBuilder() + var quote: Char? = null + var inLineComment = false + var inBlockComment = false + var index = 0 + + fun addStatement() { + val statement = current.toString().trim() + if (statement.isNotEmpty()) { + statements += statement + } + current.clear() + } + + while (index < sql.length) { + val char = sql[index] + val next = sql.getOrNull(index + 1) + + if (inLineComment) { + current.append(char) + if (char == '\n') { + inLineComment = false + } + index++ + continue + } + + if (inBlockComment) { + current.append(char) + if (char == '*' && next == '/') { + current.append(next) + inBlockComment = false + index += 2 + } else { + index++ + } + continue + } + + if (quote != null) { + current.append(char) + if (char == '\\' && quote != '`' && next != null) { + current.append(next) + index += 2 + continue + } + if (char == quote) { + if (next == quote) { + current.append(next) + index += 2 + continue + } + quote = null + } + index++ + continue + } + + when { + char == '-' && next == '-' -> { + current.append(char).append(next) + inLineComment = true + index += 2 + } + + char == '#' -> { + current.append(char) + inLineComment = true + index++ + } + + char == '/' && next == '*' -> { + current.append(char).append(next) + inBlockComment = true + index += 2 + } + + char == '\'' || char == '"' || char == '`' -> { + current.append(char) + quote = char + index++ + } + + char == ';' -> { + addStatement() + index++ + } + + else -> { + current.append(char) + index++ + } + } + } + + addStatement() + return statements +} + fun useDb(statement: JdbcTransaction.() -> Unit) { de.steamwar.db.Database.ensureConnected() transaction(de.steamwar.db.Database.db, statement = statement)