diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt b/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt index 6f0a971..1666fa2 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt @@ -541,7 +541,13 @@ class Database private constructor(var client: TelegramClient) { } } - fun settingsSetValue(key: String, value: String?) { + fun fetchSetting(key: String): String? { + val rs = stmt!!.executeQuery("SELECT value FROM settings WHERE key='${key}'") + rs.next() + return rs.getString(1) + } + + fun saveSetting(key: String, value: String?) { val ps = conn!!.prepareStatement("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)") ps.setString(1, key) if (value==null) { @@ -552,16 +558,6 @@ class Database private constructor(var client: TelegramClient) { ps.execute() } - fun settingsGetSettings(): Map { - val rs = stmt!!.executeQuery("SELECT key, value FROM settings") - val result = mutableMapOf() - while (rs.next()) { - result.set(rs.getString(1), rs.getString(2)) - } - rs.close() - return result - } - fun getIdsFromQuery(query: String): LinkedList { try { val list = LinkedList() diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/Settings.kt b/src/main/kotlin/de/fabianonline/telegram_backup/Settings.kt index ddd31de..7cc36fd 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/Settings.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/Settings.kt @@ -1,60 +1,12 @@ package de.fabianonline.telegram_backup -class Settings() { - - val options = arrayOf( - Option("gmaps_api_key", "abc", "This contains the api key bla foo bar.") - ) +class DbSettings() { + private fun fetchValue(name: String): String? = Database.getInstance().fetchSetting(name) + private fun saveValue(name: String, value: String?) = Database.getInstance().saveSetting(name, value) - init { - var all_settings = Database.getInstance().settingsGetSettings() - for ((key, value) in all_settings) { - val opt = options.find{it.key == key} - if (opt==null) throw IllegalArgumentException("Setting with key ${key} is unknown.") - if (value!=null) opt.value = value - } - } - - fun print() { - val modifiedSettings = options.filter{!it.isDefaultValue()} - val defaultSettings = options.filter{it.isDefaultValue()} - - if (modifiedSettings.count() > 0) { - println("Modified settings:") - modifiedSettings.forEach{it.print()} - println("\n") - } - - if (defaultSettings.count() > 0) { - println("Settings with default value:") - defaultSettings.forEach{it.print()} - println("\n") - } - } - - inner open class HiddenOption(val key: String, val default: String) { - var value: String? = null - - fun save() { - if (isDefaultValue()) { - Database.getInstance().settingsSetValue(key, null) - } else { - Database.getInstance().settingsSetValue(key, value) - } - } - - open fun print() { - println("%-30s %-30s".format(key, value)) - } - - fun isDefaultValue(): Boolean = (value==null || value==default) - - } - inner class Option(key: String, default: String, val descr: String): HiddenOption(key, default) { - override fun print() { - println("%-30s %-30s %s".format(key, value, descr)) - } - } + var pts: String? + get() = fetchValue("pts") + set(x: String?) = saveValue("pts", x) }