diff --git a/CHANGELOG.md b/CHANGELOG.md index 190b3f631d1..b8ddee40a15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## 0.9.20 +* `Repos`: + * `Common`: + * Fixes in `OneToManyAndroidRepo` + * New `CursorIterator` + ## 0.9.19 * `Versions`: diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/CursorIterator.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/CursorIterator.kt new file mode 100644 index 00000000000..9c7d21fcbcd --- /dev/null +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/CursorIterator.kt @@ -0,0 +1,27 @@ +package dev.inmo.micro_utils.repos + +import android.database.Cursor + +class CursorIterator( + private val c: Cursor +) : Iterator { + private var i = 0 + + init { + c.moveToFirst() + } + override fun hasNext(): Boolean { + return i < c.count + } + + override fun next(): Cursor { + i++ + return if (c.moveToNext()) { + c + } else { + throw NoSuchElementException() + } + } +} + +operator fun Cursor.iterator(): CursorIterator = CursorIterator(this) diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt index 1cc184fc2b4..96be3bd2f77 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt @@ -143,7 +143,13 @@ class OneToManyAndroidRepo( }.toLong() override suspend fun count(k: Key): Long = helper.blockingReadableTransaction { - selectDistinct(tableName, columns = valueColumnArray, selection = "$idColumnName=?", selectionArgs = arrayOf(k.keyAsString()), limit = FirstPagePagination(1).limitClause()).use { + select( + tableName, + columns = valueColumnArray, + selection = "$idColumnName=?", + selectionArgs = arrayOf(k.keyAsString()), + limit = FirstPagePagination(1).limitClause() + ).use { it.count } }.toLong()