Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move await* to upper class ExecutableQuery #3524

Merged
merged 1 commit into from Sep 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,19 +1,19 @@
package app.cash.sqldelight.async.coroutines

import app.cash.sqldelight.Query
import app.cash.sqldelight.ExecutableQuery

suspend fun <T : Any> Query<T>.awaitAsList(): List<T> = execute { cursor ->
suspend fun <T : Any> ExecutableQuery<T>.awaitAsList(): List<T> = execute { cursor ->
val result = mutableListOf<T>()
while (cursor.next()) result.add(mapper(cursor))
result
}.await()

suspend fun <T : Any> Query<T>.awaitAsOne(): T {
suspend fun <T : Any> ExecutableQuery<T>.awaitAsOne(): T {
return awaitAsOneOrNull()
?: throw NullPointerException("ResultSet returned null for $this")
}

suspend fun <T : Any> Query<T>.awaitAsOneOrNull(): T? = execute { cursor ->
suspend fun <T : Any> ExecutableQuery<T>.awaitAsOneOrNull(): T? = execute { cursor ->
if (!cursor.next()) return@execute null
val value = mapper(cursor)
check(!cursor.next()) { "ResultSet returned more than 1 row for $this" }
Expand Down