aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/moe/nea/ledger')
-rw-r--r--src/main/kotlin/moe/nea/ledger/database/DBSchema.kt34
-rw-r--r--src/main/kotlin/moe/nea/ledger/database/Database.kt7
2 files changed, 39 insertions, 2 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/database/DBSchema.kt b/src/main/kotlin/moe/nea/ledger/database/DBSchema.kt
index 49e2048..5c9099c 100644
--- a/src/main/kotlin/moe/nea/ledger/database/DBSchema.kt
+++ b/src/main/kotlin/moe/nea/ledger/database/DBSchema.kt
@@ -3,6 +3,7 @@ package moe.nea.ledger.database
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
+import java.sql.Timestamp
import java.time.Instant
interface DBSchema {
@@ -122,12 +123,43 @@ abstract class Table(val name: String) {
.execute()
}
+ enum class OnConflict {
+ FAIL,
+ IGNORE,
+ REPLACE,
+ ;
+
+ fun asSql(): String {
+ return name
+ }
+ }
+
+ fun insert(connection: Connection, onConflict: OnConflict = OnConflict.FAIL, block: (InsertStatement) -> Unit) {
+ val insert = InsertStatement(HashMap())
+ block(insert)
+ require(insert.properties.keys == columns.toSet())
+ val columnNames = columns.joinToString { it.sqlName }
+ val valueNames = columns.joinToString { "?" }
+ val statement =
+ connection.prepareAndLog("INSERT OR ${onConflict.asSql()} INTO $sqlName ($columnNames) VALUES ($valueNames)")
+ for ((index, column) in columns.withIndex()) {
+ (column as Column<Any>).type.set(statement, index + 1, insert.properties[column]!!)
+ }
+ statement.execute()
+ }
+
fun selectAll(connection: Connection): Query {
return Query(connection, columns, this)
}
}
+class InsertStatement(val properties: MutableMap<Column<*>, Any>) {
+ operator fun <T : Any> set(key: Column<T>, value: T) {
+ properties[key] = value
+ }
+}
+
fun Connection.prepareAndLog(statement: String): PreparedStatement {
println("Preparing to execute $statement")
return prepareStatement(statement)
@@ -162,7 +194,7 @@ class Query(
query += "OFFSET $skip "
}
}
- val prepared = connection.prepareAndLog(query)
+ val prepared = connection.prepareAndLog(query.trim())
val results = prepared.executeQuery()
return object : Iterator<ResultRow> {
var hasAdvanced = false
diff --git a/src/main/kotlin/moe/nea/ledger/database/Database.kt b/src/main/kotlin/moe/nea/ledger/database/Database.kt
index d14a0e2..633b3a0 100644
--- a/src/main/kotlin/moe/nea/ledger/database/Database.kt
+++ b/src/main/kotlin/moe/nea/ledger/database/Database.kt
@@ -17,7 +17,12 @@ object Database {
fun init() {
MetaTable.createIfNotExists(connection)
val meta = MetaTable.selectAll(connection).associate { it[MetaTable.key] to it[MetaTable.value] }
- println("Old Meta: $meta")
+ val lastLaunch = meta["lastLaunch"]?.toLong() ?: 0L
+ println("Last launch $lastLaunch")
+ MetaTable.insert(connection, Table.OnConflict.REPLACE) {
+ it[MetaTable.key] = "lastLaunch"
+ it[MetaTable.value] = System.currentTimeMillis().toString()
+ }
}
} \ No newline at end of file