mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-28 04:25:18 +00:00
Fix database issues
This commit is contained in:
parent
26ba7fb387
commit
3afd8b8b7f
@ -8,7 +8,7 @@ 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 {
|
||||
cursor = this.query(table, null, query, arguments, null, null, null)
|
||||
cursor = query(table, null, query, arguments, null, null, null)
|
||||
if (cursor != null && cursor.moveToFirst()) { return get(cursor) }
|
||||
} catch (e: Exception) {
|
||||
// Do nothing
|
||||
@ -19,20 +19,20 @@ fun <T> SQLiteDatabase.get(table: String, query: String, arguments: Array<String
|
||||
}
|
||||
|
||||
fun SQLiteDatabase.insertOrUpdate(table: String, values: ContentValues, whereClause: String, whereArgs: Array<String>) {
|
||||
val id = this.insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_IGNORE).toInt()
|
||||
val id = insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_IGNORE).toInt()
|
||||
if (id == -1) {
|
||||
this.update(table, values, whereClause, whereArgs)
|
||||
update(table, values, whereClause, whereArgs)
|
||||
}
|
||||
}
|
||||
|
||||
fun Cursor.getInt(columnName: String): Int {
|
||||
return this.getInt(this.getColumnIndexOrThrow(columnName))
|
||||
return getInt(getColumnIndexOrThrow(columnName))
|
||||
}
|
||||
|
||||
fun Cursor.getString(columnName: String): String {
|
||||
return this.getString(this.getColumnIndexOrThrow(columnName))
|
||||
return getString(getColumnIndexOrThrow(columnName))
|
||||
}
|
||||
|
||||
fun Cursor.getBase64EncodedData(columnName: String): ByteArray {
|
||||
return Base64.decode(this.getString(columnName))
|
||||
return Base64.decode(getString(columnName))
|
||||
}
|
@ -34,7 +34,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
||||
val database = databaseHelper.readableDatabase
|
||||
return database.get(swarmCache, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey)) { cursor ->
|
||||
val swarmAsString = cursor.getString(cursor.getColumnIndexOrThrow(swarm))
|
||||
swarmAsString.split(",").map { targetAsString ->
|
||||
swarmAsString.split(", ").map { targetAsString ->
|
||||
val components = targetAsString.split("?port=")
|
||||
LokiAPITarget(components[0], components[1].toInt())
|
||||
}
|
||||
@ -43,10 +43,11 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
||||
|
||||
override fun setSwarmCache(hexEncodedPublicKey: String, newValue: List<LokiAPITarget>) {
|
||||
val database = databaseHelper.writableDatabase
|
||||
val swarmAsString = newValue.joinToString(",") { target ->
|
||||
val swarmAsString = newValue.joinToString(", ") { target ->
|
||||
"${target.address}?port=${target.port}"
|
||||
}
|
||||
database.update(swarmCache, wrap(mapOf( swarm to swarmAsString )), "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey))
|
||||
val row = wrap(mapOf( Companion.hexEncodedPublicKey to hexEncodedPublicKey, swarm to swarmAsString ))
|
||||
database.insertOrUpdate(swarmCache, row, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey))
|
||||
}
|
||||
|
||||
override fun getLastMessageHashValue(target: LokiAPITarget): String? {
|
||||
@ -58,21 +59,23 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
||||
|
||||
override fun setLastMessageHashValue(target: LokiAPITarget, newValue: String) {
|
||||
val database = databaseHelper.writableDatabase
|
||||
database.update(lastMessageHashValueCache, wrap(mapOf( lastMessageHashValue to newValue )), "${Companion.target} = ?", wrap(target.address))
|
||||
val row = wrap(mapOf( Companion.target to target.address, lastMessageHashValue to newValue ))
|
||||
database.insertOrUpdate(lastMessageHashValueCache, row, "${Companion.target} = ?", wrap(target.address))
|
||||
}
|
||||
|
||||
override fun getReceivedMessageHashValues(): Set<String>? {
|
||||
val database = databaseHelper.readableDatabase
|
||||
return database.get(receivedMessageHashValuesCache, "$userID = ?", wrap(userPublicKey)) { cursor ->
|
||||
val receivedMessageHashValuesAsString = cursor.getString(cursor.getColumnIndexOrThrow(receivedMessageHashValues))
|
||||
receivedMessageHashValuesAsString.split(",").toSet()
|
||||
receivedMessageHashValuesAsString.split(", ").toSet()
|
||||
}
|
||||
}
|
||||
|
||||
override fun setReceivedMessageHashValues(newValue: Set<String>) {
|
||||
val database = databaseHelper.writableDatabase
|
||||
val receivedMessageHashValuesAsString = newValue.joinToString(",")
|
||||
database.update(receivedMessageHashValuesCache, wrap(mapOf( receivedMessageHashValues to receivedMessageHashValuesAsString )), "$userID = ?", wrap(userPublicKey))
|
||||
val receivedMessageHashValuesAsString = newValue.joinToString(", ")
|
||||
val row = wrap(mapOf( userID to userPublicKey, receivedMessageHashValues to receivedMessageHashValuesAsString ))
|
||||
database.insertOrUpdate(receivedMessageHashValuesCache, row, "$userID = ?", wrap(userPublicKey))
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user