1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-23 01:06:17 +00:00

Modified Settings class to only represent internal Settings. Also, renamed it to DbSettings.

This commit is contained in:
Fabian Schlenz 2018-03-05 06:55:24 +01:00
parent 42dc500514
commit 75786e39b4
2 changed files with 13 additions and 65 deletions

View File

@ -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 (?, ?)") val ps = conn!!.prepareStatement("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)")
ps.setString(1, key) ps.setString(1, key)
if (value==null) { if (value==null) {
@ -552,16 +558,6 @@ class Database private constructor(var client: TelegramClient) {
ps.execute() ps.execute()
} }
fun settingsGetSettings(): Map<String, String?> {
val rs = stmt!!.executeQuery("SELECT key, value FROM settings")
val result = mutableMapOf<String, String?>()
while (rs.next()) {
result.set(rs.getString(1), rs.getString(2))
}
rs.close()
return result
}
fun getIdsFromQuery(query: String): LinkedList<Int> { fun getIdsFromQuery(query: String): LinkedList<Int> {
try { try {
val list = LinkedList<Int>() val list = LinkedList<Int>()

View File

@ -1,60 +1,12 @@
package de.fabianonline.telegram_backup package de.fabianonline.telegram_backup
class Settings() { class DbSettings() {
private fun fetchValue(name: String): String? = Database.getInstance().fetchSetting(name)
private fun saveValue(name: String, value: String?) = Database.getInstance().saveSetting(name, value)
val options = arrayOf( var pts: String?
Option("gmaps_api_key", "abc", "This contains the api key bla foo bar.") get() = fetchValue("pts")
) set(x: String?) = saveValue("pts", x)
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))
}
}
} }