mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-02 22:45:17 +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? {
|
fun <T> SQLiteDatabase.get(table: String, query: String, arguments: Array<String>, get: (Cursor) -> T): T? {
|
||||||
var cursor: Cursor? = null
|
var cursor: Cursor? = null
|
||||||
try {
|
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) }
|
if (cursor != null && cursor.moveToFirst()) { return get(cursor) }
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
// Do nothing
|
// 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>) {
|
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) {
|
if (id == -1) {
|
||||||
this.update(table, values, whereClause, whereArgs)
|
update(table, values, whereClause, whereArgs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Cursor.getInt(columnName: String): Int {
|
fun Cursor.getInt(columnName: String): Int {
|
||||||
return this.getInt(this.getColumnIndexOrThrow(columnName))
|
return getInt(getColumnIndexOrThrow(columnName))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Cursor.getString(columnName: String): String {
|
fun Cursor.getString(columnName: String): String {
|
||||||
return this.getString(this.getColumnIndexOrThrow(columnName))
|
return getString(getColumnIndexOrThrow(columnName))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Cursor.getBase64EncodedData(columnName: String): ByteArray {
|
fun Cursor.getBase64EncodedData(columnName: String): ByteArray {
|
||||||
return Base64.decode(this.getString(columnName))
|
return Base64.decode(getString(columnName))
|
||||||
}
|
}
|
@ -46,7 +46,8 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
|||||||
val swarmAsString = newValue.joinToString(", ") { target ->
|
val swarmAsString = newValue.joinToString(", ") { target ->
|
||||||
"${target.address}?port=${target.port}"
|
"${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? {
|
override fun getLastMessageHashValue(target: LokiAPITarget): String? {
|
||||||
@ -58,7 +59,8 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
|||||||
|
|
||||||
override fun setLastMessageHashValue(target: LokiAPITarget, newValue: String) {
|
override fun setLastMessageHashValue(target: LokiAPITarget, newValue: String) {
|
||||||
val database = databaseHelper.writableDatabase
|
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>? {
|
override fun getReceivedMessageHashValues(): Set<String>? {
|
||||||
@ -72,7 +74,8 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
|||||||
override fun setReceivedMessageHashValues(newValue: Set<String>) {
|
override fun setReceivedMessageHashValues(newValue: Set<String>) {
|
||||||
val database = databaseHelper.writableDatabase
|
val database = databaseHelper.writableDatabase
|
||||||
val receivedMessageHashValuesAsString = newValue.joinToString(", ")
|
val receivedMessageHashValuesAsString = newValue.joinToString(", ")
|
||||||
database.update(receivedMessageHashValuesCache, wrap(mapOf( receivedMessageHashValues to receivedMessageHashValuesAsString )), "$userID = ?", wrap(userPublicKey))
|
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