|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.r2dbc.core |
| 17 | + |
| 18 | +import kotlinx.coroutines.flow.Flow |
| 19 | +import kotlinx.coroutines.reactive.asFlow |
| 20 | +import kotlinx.coroutines.reactive.awaitFirstOrNull |
| 21 | +import kotlinx.coroutines.reactive.awaitSingle |
| 22 | + |
| 23 | +/** |
| 24 | + * Extensions for [ReactiveSelectOperation]. |
| 25 | + * |
| 26 | + * @author Mark Paluch |
| 27 | + * @since 1.1 |
| 28 | + */ |
| 29 | + |
| 30 | +/** |
| 31 | + * Extension for [ReactiveSelectOperation.select] leveraging reified type parameters. |
| 32 | + */ |
| 33 | +inline fun <reified T : Any> ReactiveSelectOperation.select(): ReactiveSelectOperation.ReactiveSelect<T> = |
| 34 | + select(T::class.java) |
| 35 | + |
| 36 | +/** |
| 37 | + * Extension for [ReactiveSelectOperation.SelectWithProjection. as] leveraging reified type parameters. |
| 38 | + */ |
| 39 | +inline fun <reified T : Any> ReactiveSelectOperation.SelectWithProjection<*>.asType(): ReactiveSelectOperation.SelectWithQuery<T> = |
| 40 | + `as`(T::class.java) |
| 41 | + |
| 42 | +/** |
| 43 | + * Non-nullable Coroutines variant of [ReactiveSelectOperation.TerminatingSelect.one]. |
| 44 | + */ |
| 45 | +suspend inline fun <reified T : Any> ReactiveSelectOperation.TerminatingSelect<T>.awaitOne(): T = |
| 46 | + one().awaitSingle() |
| 47 | + |
| 48 | +/** |
| 49 | + * Nullable Coroutines variant of [ReactiveSelectOperation.TerminatingSelect.one]. |
| 50 | + */ |
| 51 | +suspend inline fun <reified T : Any> ReactiveSelectOperation.TerminatingSelect<T>.awaitOneOrNull(): T? = |
| 52 | + one().awaitFirstOrNull() |
| 53 | + |
| 54 | +/** |
| 55 | + * Non-nullable Coroutines variant of [ReactiveSelectOperation.TerminatingSelect.first]. |
| 56 | + */ |
| 57 | +suspend inline fun <reified T : Any> ReactiveSelectOperation.TerminatingSelect<T>.awaitFirst(): T = |
| 58 | + first().awaitSingle() |
| 59 | + |
| 60 | +/** |
| 61 | + * Nullable Coroutines variant of [ReactiveSelectOperation.TerminatingSelect.first]. |
| 62 | + */ |
| 63 | +suspend inline fun <reified T : Any> ReactiveSelectOperation.TerminatingSelect<T>.awaitFirstOrNull(): T? = |
| 64 | + first().awaitFirstOrNull() |
| 65 | + |
| 66 | +/** |
| 67 | + * Coroutines variant of [ReactiveSelectOperation.TerminatingSelect.count]. |
| 68 | + */ |
| 69 | +suspend fun <T : Any> ReactiveSelectOperation.TerminatingSelect<T>.awaitCount(): Long = |
| 70 | + count().awaitSingle() |
| 71 | + |
| 72 | +/** |
| 73 | + * Coroutines variant of [ReactiveSelectOperation.TerminatingSelect.exists]. |
| 74 | + */ |
| 75 | +suspend fun <T : Any> ReactiveSelectOperation.TerminatingSelect<T>.awaitExists(): Boolean = |
| 76 | + exists().awaitSingle() |
| 77 | + |
| 78 | +/** |
| 79 | + * Coroutines [Flow] variant of [ReactiveSelectOperation.TerminatingSelect.all]. |
| 80 | + */ |
| 81 | +fun <T : Any> ReactiveSelectOperation.TerminatingSelect<T>.flow(): Flow<T> = |
| 82 | + all().asFlow() |
0 commit comments