From a5a5bd80c44bbf3219d2f42d93df0eef76b533ba Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Wed, 20 Mar 2024 01:59:21 -0400 Subject: [PATCH] [+] Helper to read sql file --- .../icu/samnyan/aqua/net/games/GameHelper.kt | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/icu/samnyan/aqua/net/games/GameHelper.kt b/src/main/java/icu/samnyan/aqua/net/games/GameHelper.kt index fe99ee9f..faf4ea03 100644 --- a/src/main/java/icu/samnyan/aqua/net/games/GameHelper.kt +++ b/src/main/java/icu/samnyan/aqua/net/games/GameHelper.kt @@ -44,3 +44,25 @@ fun findTrend(log: List): List { } fun List.acc() = if (isEmpty()) 0.0 else sumOf { it.achievement }.toDouble() / size / 10000.0 + +val insertPattern = """INSERT INTO\s+(\w+)\s*\(([^)]+)\)\s*VALUES\s*\(([^)]+)\);""".toRegex() +data class SqlInsert(val table: String, val mapping: Map) +fun String.asSqlInsert(): SqlInsert { + val match = insertPattern.matchEntire(this) ?: error("Does not match insert pattern") + val (table, rawCols, rawVals) = match.destructured + val cols = rawCols.split(',').map { it.trim(' ', '"') } + + // Parse values with proper quote handling + val vals = mutableListOf() + var startI = 0 + var insideQuote = false + rawVals.forEachIndexed { i, c -> + if (c == ',' && !insideQuote) { + vals.add(rawVals.substring(startI, i).trim(' ', '"')) + startI = i + 1 + } else if (c == '"') insideQuote = !insideQuote + } + + assert(cols.size == vals.size) { "Column and value count mismatch" } + return SqlInsert(table, cols.zip(vals).toMap()) +} \ No newline at end of file