Skip to content

Commit

Permalink
Added support for mapping cursors
Browse files Browse the repository at this point in the history
Calling "requery" on a cursor may cause the driver to re-use queries, which is unwanted behavior.

*This commit is related to issue #529 [1]*

[1] #529
  • Loading branch information
JaniruTEC committed Apr 13, 2024
1 parent 6b1a9b5 commit af17db5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
15 changes: 15 additions & 0 deletions data/src/main/java/org/cryptomator/data/db/SQLiteCacheControl.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cryptomator.data.db

import android.database.Cursor
import androidx.sqlite.db.SupportSQLiteOpenHelper
import org.cryptomator.data.db.sqlmapping.SQLMappingFunction
import org.cryptomator.data.db.sqlmapping.asMapped
Expand All @@ -19,7 +20,21 @@ object SQLiteCacheControl {
override fun mapWhereClause(whereClause: String?): String {
return map(whereClause ?: "1 = 1")
}

override fun mapCursor(cursor: Cursor): Cursor {
return NoRequeryCursor(cursor)
}
}

fun SupportSQLiteOpenHelper.Factory.asCacheControlled(): SupportSQLiteOpenHelper.Factory = asMapped(RandomUUIDMapping)
}

private class NoRequeryCursor(
private val delegateCursor: Cursor
) : Cursor by delegateCursor {

@Deprecated("Deprecated in Java")
override fun requery(): Boolean {
throw UnsupportedOperationException()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ internal class MappingSupportSQLiteDatabase(
}

override fun query(query: SupportSQLiteQuery): Cursor {
return delegate.query(map(query))
return mapCursor(delegate.query(map(query)))
}

override fun query(query: SupportSQLiteQuery, cancellationSignal: CancellationSignal?): Cursor {
return delegate.query(map(query), cancellationSignal)
return mapCursor(delegate.query(map(query), cancellationSignal))
}

override fun query(query: String): Cursor {
return delegate.query(map(query))
return mapCursor(delegate.query(map(query)))
}

override fun query(query: String, bindArgs: Array<out Any?>): Cursor {
return delegate.query(map(query), bindArgs)
return mapCursor(delegate.query(map(query), bindArgs))
}

override fun insert(table: String, conflictAlgorithm: Int, values: ContentValues): Long {
Expand Down Expand Up @@ -79,6 +79,10 @@ internal class MappingSupportSQLiteDatabase(
return MappingSupportSQLiteQuery(query)
}

private fun mapCursor(cursor: Cursor): Cursor {
return mappingFunction.mapCursor(cursor)
}

private fun mapWhereClause(whereClause: String?): String? {
if (whereClause != null && whereClause.isBlank()) {
throw IllegalArgumentException()
Expand Down Expand Up @@ -202,4 +206,6 @@ interface SQLMappingFunction {

fun mapWhereClause(whereClause: String?): String?

fun mapCursor(cursor: Cursor): Cursor

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ class MappingSupportSQLiteDatabaseTest {
identityMapping = MappingSupportSQLiteDatabase(delegateMock, object : SQLMappingFunction {
override fun map(sql: String): String = sql
override fun mapWhereClause(whereClause: String?): String? = whereClause
override fun mapCursor(cursor: Cursor): Cursor = cursor
})
commentMapping = MappingSupportSQLiteDatabase(delegateMock, object : SQLMappingFunction {
override fun map(sql: String): String = "$sql -- Comment!"
override fun mapWhereClause(whereClause: String?): String = map(whereClause ?: "1 = 1")
override fun mapCursor(cursor: Cursor): Cursor = cursor //TODO
})
}

Expand Down

0 comments on commit af17db5

Please sign in to comment.