2019-06-04 12:12:40 +10:00
|
|
|
package org.thoughtcrime.securesms.loki
|
|
|
|
|
2019-06-17 11:57:40 +10:00
|
|
|
import android.content.ContentValues
|
2019-06-04 12:12:40 +10:00
|
|
|
import net.sqlcipher.Cursor
|
|
|
|
import net.sqlcipher.database.SQLiteDatabase
|
|
|
|
import org.thoughtcrime.securesms.util.Base64
|
|
|
|
|
|
|
|
fun <T> SQLiteDatabase.get(table: String, query: String, arguments: Array<String>, get: (Cursor) -> T): T? {
|
|
|
|
var cursor: Cursor? = null
|
|
|
|
try {
|
2019-06-19 15:45:40 +10:00
|
|
|
cursor = query(table, null, query, arguments, null, null, null)
|
2019-06-04 12:12:40 +10:00
|
|
|
if (cursor != null && cursor.moveToFirst()) { return get(cursor) }
|
|
|
|
} catch (e: Exception) {
|
|
|
|
// Do nothing
|
|
|
|
} finally {
|
|
|
|
cursor?.close()
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2019-06-17 11:57:40 +10:00
|
|
|
fun SQLiteDatabase.insertOrUpdate(table: String, values: ContentValues, whereClause: String, whereArgs: Array<String>) {
|
2019-06-19 15:45:40 +10:00
|
|
|
val id = insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_IGNORE).toInt()
|
2019-06-17 11:57:40 +10:00
|
|
|
if (id == -1) {
|
2019-06-19 15:45:40 +10:00
|
|
|
update(table, values, whereClause, whereArgs)
|
2019-06-17 11:57:40 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:12:40 +10:00
|
|
|
fun Cursor.getInt(columnName: String): Int {
|
2019-06-19 15:45:40 +10:00
|
|
|
return getInt(getColumnIndexOrThrow(columnName))
|
2019-06-04 12:12:40 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
fun Cursor.getString(columnName: String): String {
|
2019-06-19 15:45:40 +10:00
|
|
|
return getString(getColumnIndexOrThrow(columnName))
|
2019-06-04 12:12:40 +10:00
|
|
|
}
|
|
|
|
|
2019-06-04 14:16:09 +10:00
|
|
|
fun Cursor.getBase64EncodedData(columnName: String): ByteArray {
|
2019-06-19 15:45:40 +10:00
|
|
|
return Base64.decode(getString(columnName))
|
2019-06-04 12:12:40 +10:00
|
|
|
}
|