mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
fix(sqldelight): Custom query function
Basically the same as "executeAsOneOrNull" but without the result count check
This commit is contained in:
parent
2fd6146d32
commit
7f05d16039
5 changed files with 30 additions and 4 deletions
|
@ -23,7 +23,7 @@ class ChapterRepositoryImpl(private val handler: DatabaseHandler) : ChapterRepos
|
||||||
handler.awaitList { chaptersQueries.getChaptersByUrl(url, filterScanlators.toInt().toLong(), Chapter::mapper) }
|
handler.awaitList { chaptersQueries.getChaptersByUrl(url, filterScanlators.toInt().toLong(), Chapter::mapper) }
|
||||||
|
|
||||||
override suspend fun getChapterByUrl(url: String, filterScanlators: Boolean): Chapter? =
|
override suspend fun getChapterByUrl(url: String, filterScanlators: Boolean): Chapter? =
|
||||||
handler.awaitOneOrNull { chaptersQueries.getChaptersByUrl(url, filterScanlators.toInt().toLong(), Chapter::mapper) }
|
handler.awaitFirstOrNull { chaptersQueries.getChaptersByUrl(url, filterScanlators.toInt().toLong(), Chapter::mapper) }
|
||||||
|
|
||||||
override suspend fun getChaptersByUrlAndMangaId(
|
override suspend fun getChaptersByUrlAndMangaId(
|
||||||
url: String,
|
url: String,
|
||||||
|
@ -39,7 +39,7 @@ class ChapterRepositoryImpl(private val handler: DatabaseHandler) : ChapterRepos
|
||||||
mangaId: Long,
|
mangaId: Long,
|
||||||
filterScanlators: Boolean
|
filterScanlators: Boolean
|
||||||
): Chapter? =
|
): Chapter? =
|
||||||
handler.awaitOneOrNull {
|
handler.awaitFirstOrNull {
|
||||||
chaptersQueries.getChaptersByUrlAndMangaId(url, mangaId, filterScanlators.toInt().toLong(), Chapter::mapper)
|
chaptersQueries.getChaptersByUrlAndMangaId(url, mangaId, filterScanlators.toInt().toLong(), Chapter::mapper)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ class MangaRepositoryImpl(private val handler: DatabaseHandler) : MangaRepositor
|
||||||
handler.awaitList { mangasQueries.findAll(Manga::mapper) }
|
handler.awaitList { mangasQueries.findAll(Manga::mapper) }
|
||||||
|
|
||||||
override suspend fun getMangaByUrlAndSource(url: String, source: Long): Manga? =
|
override suspend fun getMangaByUrlAndSource(url: String, source: Long): Manga? =
|
||||||
handler.awaitOneOrNull { mangasQueries.findByUrlAndSource(url, source, Manga::mapper) }
|
handler.awaitFirstOrNull { mangasQueries.findByUrlAndSource(url, source, Manga::mapper) }
|
||||||
|
|
||||||
override suspend fun getMangaById(id: Long): Manga? =
|
override suspend fun getMangaById(id: Long): Manga? =
|
||||||
handler.awaitOneOrNull { mangasQueries.findById(id, Manga::mapper) }
|
handler.awaitOneOrNull { mangasQueries.findById(id, Manga::mapper) }
|
||||||
|
@ -37,7 +37,7 @@ class MangaRepositoryImpl(private val handler: DatabaseHandler) : MangaRepositor
|
||||||
handler.subscribeToList { library_viewQueries.findAll(LibraryManga::mapper) }
|
handler.subscribeToList { library_viewQueries.findAll(LibraryManga::mapper) }
|
||||||
|
|
||||||
override suspend fun getDuplicateFavorite(title: String, source: Long): Manga? =
|
override suspend fun getDuplicateFavorite(title: String, source: Long): Manga? =
|
||||||
handler.awaitOneOrNull { mangasQueries.findDuplicateFavorite(title.lowercase(), source, Manga::mapper) }
|
handler.awaitFirstOrNull { mangasQueries.findDuplicateFavorite(title.lowercase(), source, Manga::mapper) }
|
||||||
|
|
||||||
override suspend fun update(update: MangaUpdate): Boolean {
|
override suspend fun update(update: MangaUpdate): Boolean {
|
||||||
return try {
|
return try {
|
||||||
|
|
|
@ -11,6 +11,7 @@ import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import yokai.data.util.executeAsFirstOrNull
|
||||||
|
|
||||||
class AndroidDatabaseHandler(
|
class AndroidDatabaseHandler(
|
||||||
val db: Database,
|
val db: Database,
|
||||||
|
@ -53,6 +54,13 @@ class AndroidDatabaseHandler(
|
||||||
return dispatch(inTransaction) { block(db).executeAsOneOrNull() }
|
return dispatch(inTransaction) { block(db).executeAsOneOrNull() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun <T : Any> awaitFirstOrNull(
|
||||||
|
inTransaction: Boolean,
|
||||||
|
block: suspend Database.() -> Query<T>
|
||||||
|
): T? {
|
||||||
|
return dispatch(inTransaction) { block(db).executeAsFirstOrNull() }
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun <T : Any> awaitOneOrNullExecutable(
|
override suspend fun <T : Any> awaitOneOrNullExecutable(
|
||||||
inTransaction: Boolean,
|
inTransaction: Boolean,
|
||||||
block: suspend Database.() -> ExecutableQuery<T>,
|
block: suspend Database.() -> ExecutableQuery<T>,
|
||||||
|
|
|
@ -27,6 +27,11 @@ interface DatabaseHandler {
|
||||||
block: suspend Database.() -> Query<T>
|
block: suspend Database.() -> Query<T>
|
||||||
): T?
|
): T?
|
||||||
|
|
||||||
|
suspend fun <T : Any> awaitFirstOrNull(
|
||||||
|
inTransaction: Boolean = false,
|
||||||
|
block: suspend Database.() -> Query<T>
|
||||||
|
): T?
|
||||||
|
|
||||||
suspend fun <T : Any> awaitOneOrNullExecutable(
|
suspend fun <T : Any> awaitOneOrNullExecutable(
|
||||||
inTransaction: Boolean = false,
|
inTransaction: Boolean = false,
|
||||||
block: suspend Database.() -> ExecutableQuery<T>,
|
block: suspend Database.() -> ExecutableQuery<T>,
|
||||||
|
|
13
data/src/commonMain/kotlin/yokai/data/util/SqlDelightUtil.kt
Normal file
13
data/src/commonMain/kotlin/yokai/data/util/SqlDelightUtil.kt
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
package yokai.data.util
|
||||||
|
|
||||||
|
import app.cash.sqldelight.ExecutableQuery
|
||||||
|
import app.cash.sqldelight.db.QueryResult
|
||||||
|
|
||||||
|
fun <T : Any> ExecutableQuery<T>.executeAsFirst(): T {
|
||||||
|
return executeAsFirstOrNull() ?: throw NullPointerException("ResultSet returned null for $this")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T : Any> ExecutableQuery<T>.executeAsFirstOrNull(): T? = execute { cursor ->
|
||||||
|
if (!cursor.next().value) return@execute QueryResult.Value(null)
|
||||||
|
QueryResult.Value(mapper(cursor))
|
||||||
|
}.value
|
Loading…
Add table
Add a link
Reference in a new issue