forked from SteamWar/SteamWar
@@ -7,6 +7,7 @@ import com.github.ajalt.mordant.rendering.TextColors
|
|||||||
import com.github.ajalt.mordant.rendering.TextStyles
|
import com.github.ajalt.mordant.rendering.TextStyles
|
||||||
import de.steamwar.db.Database
|
import de.steamwar.db.Database
|
||||||
import de.steamwar.db.execute
|
import de.steamwar.db.execute
|
||||||
|
import de.steamwar.db.executeScript
|
||||||
import de.steamwar.db.useDb
|
import de.steamwar.db.useDb
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
@@ -24,17 +25,20 @@ class ResetCommand : CliktCommand() {
|
|||||||
|
|
||||||
execute("SET FOREIGN_KEY_CHECKS=0;") { }
|
execute("SET FOREIGN_KEY_CHECKS=0;") { }
|
||||||
|
|
||||||
val tables = execute("SHOW TABLES;") { it.getString(1) }
|
val databaseObjects = execute("SHOW FULL TABLES;") { it.getString(1) to it.getString(2) }
|
||||||
for (table in tables) {
|
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("DROP TABLE IF EXISTS `${table.replace("`", "``")}`;") { }
|
||||||
}
|
}
|
||||||
|
|
||||||
execute(schema) { }
|
executeScript(schema)
|
||||||
|
|
||||||
val seed = javaClass.getResource("/db/reset-seed.sql")
|
val seed = javaClass.getResource("/db/reset-seed.sql")
|
||||||
?: throw CliktError("Reset seed file not found!")
|
?: throw CliktError("Reset seed file not found!")
|
||||||
|
|
||||||
execute(seed.readText()) { }
|
executeScript(seed.readText())
|
||||||
|
|
||||||
execute("SET FOREIGN_KEY_CHECKS=1;") { }
|
execute("SET FOREIGN_KEY_CHECKS=1;") { }
|
||||||
|
|
||||||
|
|||||||
@@ -83,6 +83,113 @@ fun <T> JdbcTransaction.executeSingle(sql: String, transform: (ResultSet) -> T):
|
|||||||
}.single()
|
}.single()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun JdbcTransaction.executeScript(sql: String) {
|
||||||
|
for (statement in splitSqlScript(sql)) {
|
||||||
|
exec(statement) { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun splitSqlScript(sql: String): List<String> {
|
||||||
|
val statements = mutableListOf<String>()
|
||||||
|
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) {
|
fun useDb(statement: JdbcTransaction.() -> Unit) {
|
||||||
de.steamwar.db.Database.ensureConnected()
|
de.steamwar.db.Database.ensureConnected()
|
||||||
transaction(de.steamwar.db.Database.db, statement = statement)
|
transaction(de.steamwar.db.Database.db, statement = statement)
|
||||||
|
|||||||
Reference in New Issue
Block a user