aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger/database/Database.kt
blob: 9dc4e005a1e251e9b2f3887c6dffe782002e522c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package moe.nea.ledger.database

import moe.nea.ledger.Ledger
import java.sql.DriverManager

object Database {
	val connection = DriverManager.getConnection("jdbc:sqlite:${Ledger.dataFolder.resolve("database.db")}")

	object MetaTable : Table("LedgerMeta") {
		val key = column("key", DBString)
		val value = column("value", DBString)

		init {
			unique(key)
		}
	}

	fun init() {
		MetaTable.createIfNotExists(connection)
		val meta = MetaTable.selectAll(connection).associate { it[MetaTable.key] to it[MetaTable.value] }
		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()
		}
	}

}