Fix magisk database code in app

This commit is contained in:
topjohnwu 2019-06-06 00:39:24 -07:00
parent 662a5c8ea6
commit f417389a7a
3 changed files with 14 additions and 25 deletions

View File

@ -10,11 +10,12 @@ class SettingsDao : BaseDao() {
condition { equals("key", key) }
}.ignoreElement()
fun put(key: String, value: Int) = query<Insert> {
values(key to value.toString())
fun put(key: String, value: Int) = query<Replace> {
values("key" to key, "value" to value)
}.ignoreElement()
fun fetch(key: String, default: Int = -1) = query<Select> {
fields("value")
condition { equals("key", key) }
}.map { it.firstOrNull()?.values?.firstOrNull()?.toIntOrNull() ?: default }

View File

@ -10,8 +10,8 @@ class StringDao : BaseDao() {
condition { equals("key", key) }
}.ignoreElement()
fun put(key: String, value: String) = query<Insert> {
values(key to value)
fun put(key: String, value: String) = query<Replace> {
values("key" to key, "value" to value)
}.ignoreElement()
fun fetch(key: String, default: String = "") = query<Select> {

View File

@ -32,11 +32,7 @@ class Delete : MagiskQueryBuilder {
}
override fun toString(): String {
return StringBuilder()
.appendln(requestType)
.appendln(table)
.appendln(condition)
.toString()
return listOf(requestType, table, condition).joinToString(" ")
}
}
@ -65,13 +61,7 @@ class Select : MagiskQueryBuilder {
}
override fun toString(): String {
return StringBuilder()
.appendln(requestType)
.appendln(table)
.appendln(condition)
.appendln(orderField)
.toString()
.replace("\n", " ")
return listOf(requestType, table, condition, orderField).joinToString(" ")
}
}
@ -84,23 +74,21 @@ open class Insert : MagiskQueryBuilder {
override lateinit var table: String
private val keys get() = _values.keys.joinToString(",")
private val values get() = _values.values.joinToString(",") { "\"$it\"" }
private var _values: Map<String, String> = mapOf()
private val values get() = _values.values.joinToString(",") {
if (it is String) "\"$it\"" else it.toString()
}
private var _values: Map<String, Any> = mapOf()
fun values(vararg pairs: Pair<String, String>) {
fun values(vararg pairs: Pair<String, Any>) {
_values = pairs.toMap()
}
fun values(values: Map<String, String>) {
fun values(values: Map<String, Any>) {
_values = values
}
override fun toString(): String {
return StringBuilder()
.appendln(requestType)
.appendln(table)
.appendln("($keys) VALUES($values)")
.toString()
return listOf(requestType, table, "($keys) VALUES($values)").joinToString(" ")
}
}