mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-10-22 07:40:32 +00:00
fixes
This commit is contained in:
@@ -74,6 +74,19 @@ fun SQLiteDatabase.select(
|
||||
table, columns, selection, selectionArgs, groupBy, having, orderBy, limit
|
||||
)
|
||||
|
||||
fun SQLiteDatabase.selectDistinct(
|
||||
table: String,
|
||||
columns: Array<String>? = null,
|
||||
selection: String? = null,
|
||||
selectionArgs: Array<String>? = null,
|
||||
groupBy: String? = null,
|
||||
having: String? = null,
|
||||
orderBy: String? = null,
|
||||
limit: String? = null
|
||||
) = query(
|
||||
true, table, columns, selection, selectionArgs, groupBy, having, orderBy, limit
|
||||
)
|
||||
|
||||
fun makePlaceholders(count: Int): String {
|
||||
return (0 until count).joinToString { "?" }
|
||||
}
|
||||
|
@@ -35,7 +35,9 @@ class OneToManyAndroidRepo<Key, Value>(
|
||||
override val onDataCleared: Flow<Key> = _onDataCleared.asSharedFlow()
|
||||
|
||||
private val idColumnName = "id"
|
||||
private val idColumnArray = arrayOf(idColumnName)
|
||||
private val valueColumnName = "value"
|
||||
private val valueColumnArray = arrayOf(valueColumnName)
|
||||
|
||||
init {
|
||||
helper.blockingWritableTransaction {
|
||||
@@ -105,7 +107,7 @@ class OneToManyAndroidRepo<Key, Value>(
|
||||
}
|
||||
|
||||
override suspend fun contains(k: Key): Boolean = helper.blockingReadableTransaction {
|
||||
select(tableName, selection = "$idColumnName=?", selectionArgs = arrayOf(k.keyAsString()), limit = FirstPagePagination(1).limitClause()).use {
|
||||
select(tableName, selection = "$idColumnName=?", selectionArgs = arrayOf(k.keyAsString()), limit = firstPageWithOneElementPagination.limitClause()).use {
|
||||
it.count > 0
|
||||
}
|
||||
}
|
||||
@@ -121,7 +123,7 @@ class OneToManyAndroidRepo<Key, Value>(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun count(): Long =helper.blockingReadableTransaction {
|
||||
override suspend fun count(): Long = helper.blockingReadableTransaction {
|
||||
select(
|
||||
tableName
|
||||
).use {
|
||||
@@ -130,7 +132,7 @@ class OneToManyAndroidRepo<Key, Value>(
|
||||
}.toLong()
|
||||
|
||||
override suspend fun count(k: Key): Long = helper.blockingReadableTransaction {
|
||||
select(tableName, selection = "$idColumnName=?", selectionArgs = arrayOf(k.keyAsString()), limit = FirstPagePagination(1).limitClause()).use {
|
||||
selectDistinct(tableName, columns = valueColumnArray, selection = "$idColumnName=?", selectionArgs = arrayOf(k.keyAsString()), limit = FirstPagePagination(1).limitClause()).use {
|
||||
it.count
|
||||
}
|
||||
}.toLong()
|
||||
@@ -150,6 +152,7 @@ class OneToManyAndroidRepo<Key, Value>(
|
||||
helper.blockingReadableTransaction {
|
||||
select(
|
||||
tableName,
|
||||
valueColumnArray,
|
||||
selection = "$idColumnName=?",
|
||||
selectionArgs = arrayOf(k.keyAsString()),
|
||||
limit = resultPagination.limitClause()
|
||||
@@ -180,8 +183,9 @@ class OneToManyAndroidRepo<Key, Value>(
|
||||
}
|
||||
val resultPagination = pagination.let { if (reversed) pagination.reverse(count) else pagination }
|
||||
helper.blockingReadableTransaction {
|
||||
select(
|
||||
selectDistinct(
|
||||
tableName,
|
||||
idColumnArray,
|
||||
limit = resultPagination.limitClause()
|
||||
).use { c ->
|
||||
mutableListOf<Key>().also {
|
||||
@@ -205,8 +209,9 @@ class OneToManyAndroidRepo<Key, Value>(
|
||||
): PaginationResult<Key> = count().let { count ->
|
||||
val resultPagination = pagination.let { if (reversed) pagination.reverse(count) else pagination }
|
||||
helper.blockingReadableTransaction {
|
||||
select(
|
||||
selectDistinct(
|
||||
tableName,
|
||||
idColumnArray,
|
||||
selection = "$valueColumnName=?",
|
||||
selectionArgs = arrayOf(v.valueAsString()),
|
||||
limit = resultPagination.limitClause()
|
||||
|
Reference in New Issue
Block a user