diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/misc/Expression.kt b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/misc/Expression.kt index 6ceca69..d7748b2 100644 --- a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/misc/Expression.kt +++ b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/misc/Expression.kt @@ -1,7 +1,7 @@ package ru.landgrafhomyak.serdha.api.v0.misc -import ru.landgrafhomyak.serdha.api.v0.runtime.ParametersSetter -import ru.landgrafhomyak.serdha.api.v0.runtime.Row +import ru.landgrafhomyak.serdha.api.v0.runtime.InputRow +import ru.landgrafhomyak.serdha.api.v0.runtime.OutputRow public interface Expression { public val userExtension: ExpressionUserExtension @@ -36,6 +36,6 @@ public interface Expression { } public interface Action { - public fun calculate(input: Row, output: ParametersSetter) + public fun calculate(input: OutputRow, output: InputRow) } } \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/DeleteQuery.kt b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/DeleteQuery.kt index 35de920..f91f73a 100644 --- a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/DeleteQuery.kt +++ b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/DeleteQuery.kt @@ -4,7 +4,7 @@ import ru.landgrafhomyak.serdha.api.v0.misc.Column import ru.landgrafhomyak.serdha.api.v0.misc.DatabaseType import ru.landgrafhomyak.serdha.api.v0.misc.DatabaseTypesProvider import ru.landgrafhomyak.serdha.api.v0.misc.QueryParam -import ru.landgrafhomyak.serdha.api.v0.runtime.ParametersSetter +import ru.landgrafhomyak.serdha.api.v0.runtime.InputRow import ru.landgrafhomyak.serdha.api.v0.table.Table public interface DeleteQuery : _Query { @@ -16,7 +16,7 @@ public interface DeleteQuery : _Query> param(name: String, type: DT): QueryParam - public val selectorParams: ParametersSetter.WithRedirect + public val selectorParams: InputRow.WithRedirect public var selectorColumn: Column<*, DatabaseType.ROW_ID, SelectorTableUserExtension> public fun > returnFromSubquery(name: String, column: Column): Column diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/InsertQuery.kt b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/InsertQuery.kt index 0bbc142..6a5dddf 100644 --- a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/InsertQuery.kt +++ b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/InsertQuery.kt @@ -5,7 +5,7 @@ import ru.landgrafhomyak.serdha.api.v0.table.Table import ru.landgrafhomyak.serdha.api.v0.misc.DatabaseType import ru.landgrafhomyak.serdha.api.v0.misc.DatabaseTypesProvider import ru.landgrafhomyak.serdha.api.v0.misc.QueryParam -import ru.landgrafhomyak.serdha.api.v0.runtime.ParametersSetter +import ru.landgrafhomyak.serdha.api.v0.runtime.InputRow import ru.landgrafhomyak.serdha.api.v0.table.UniqueIndex public interface InsertQuery : _Query { @@ -52,7 +52,7 @@ public interface InsertQuery : _Query : _CommonInsertCreatorScope { public fun > queryParam(name: String, type: DT): QueryParam - public val sourceQueryParams: ParametersSetter.WithRedirect + public val sourceQueryParams: InputRow.WithRedirect public fun > insertConstant(dst: Column, src: QueryParam) public fun > insertFromSubquery(dst: Column, src: QueryParam) diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/SelectQuery.kt b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/SelectQuery.kt index e3091c2..0d01f97 100644 --- a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/SelectQuery.kt +++ b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/SelectQuery.kt @@ -6,7 +6,7 @@ import ru.landgrafhomyak.serdha.api.v0.misc.DatabaseType import ru.landgrafhomyak.serdha.api.v0.misc.DatabaseTypesProvider import ru.landgrafhomyak.serdha.api.v0.misc.Expression import ru.landgrafhomyak.serdha.api.v0.misc.QueryParam -import ru.landgrafhomyak.serdha.api.v0.runtime.ParametersSetter +import ru.landgrafhomyak.serdha.api.v0.runtime.InputRow public interface SelectQuery : _Query { public enum class Order { @@ -40,8 +40,8 @@ public interface SelectQuery : _Query): QueryUserExtension public interface Scope { - public val leftSubqueryParams: ParametersSetter.WithRedirect - public val rightSubqueryParams: ParametersSetter.WithRedirect + public val leftSubqueryParams: InputRow.WithRedirect + public val rightSubqueryParams: InputRow.WithRedirect public fun > selectColumnFromLeft(column: Column): IntermediateColumn public fun > selectColumnFromRight(column: Column): IntermediateColumn @@ -54,7 +54,7 @@ public interface SelectQuery : _Query): QueryUserExtension public interface Scope : _CommonSelectCreatorScope { - public val subqueryParams: ParametersSetter.WithRedirect + public val subqueryParams: InputRow.WithRedirect public fun > selectColumnFromSubquery(param: Column): IntermediateColumn } diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/UpdateQuery.kt b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/UpdateQuery.kt index 33ee673..a76515e 100644 --- a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/UpdateQuery.kt +++ b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/queries/UpdateQuery.kt @@ -5,7 +5,7 @@ import ru.landgrafhomyak.serdha.api.v0.table.Table import ru.landgrafhomyak.serdha.api.v0.misc.DatabaseType import ru.landgrafhomyak.serdha.api.v0.misc.DatabaseTypesProvider import ru.landgrafhomyak.serdha.api.v0.misc.QueryParam -import ru.landgrafhomyak.serdha.api.v0.runtime.ParametersSetter +import ru.landgrafhomyak.serdha.api.v0.runtime.InputRow public interface UpdateQuery : _Query { public interface FromSubquery : UpdateQuery, _Query.Params2Table { @@ -16,7 +16,7 @@ public interface UpdateQuery : _Query> param(name: String, type: DT): QueryParam - public val selectorParams: ParametersSetter.WithRedirect + public val selectorParams: InputRow.WithRedirect public var selectorColumn: Column<*, DatabaseType.ROW_ID, SelectorTableUserExtension> public fun > updateColumnWithSubquery(dst: Column, src: Column) diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/ParametersSetter.kt b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/InputRow.kt similarity index 70% rename from src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/ParametersSetter.kt rename to src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/InputRow.kt index be0a9a1..540864a 100644 --- a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/ParametersSetter.kt +++ b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/InputRow.kt @@ -1,9 +1,10 @@ package ru.landgrafhomyak.serdha.api.v0.runtime +import ru.landgrafhomyak.serdha.api.v0.LowLevelApi import ru.landgrafhomyak.serdha.api.v0.misc.DatabaseType import ru.landgrafhomyak.serdha.api.v0.misc.QueryParam -public interface ParametersSetter { +public interface InputRow { public operator fun set(c: QueryParam, value: RuntimeType) public operator fun set(c: QueryParam, value: Byte): Unit = this.set(c, value) @@ -17,7 +18,28 @@ public interface ParametersSetter { public operator fun set(c: QueryParam, value: Char): Unit = this.set(c, value) public operator fun set(c: QueryParam, value: Boolean): Unit = this.set(c, value) - public interface WithRedirect : ParametersSetter { + public interface WithRedirect : InputRow { public fun > redirect(c: QueryParam, value: QueryParam) } + + @Suppress("ClassName") + @LowLevelApi + public interface _Scope : InputRow { + @Suppress("FunctionName") + @LowLevelApi + public suspend fun _finish(): Next + + @Suppress("FunctionName") + @LowLevelApi + public suspend fun _abort() + } + + + @Suppress("ClassName") + @LowLevelApi + public interface _Iterator : _Scope { + @Suppress("FunctionName") + @LowLevelApi + public fun _next(): Boolean + } } \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/Row.kt b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/OutputRow.kt similarity index 70% rename from src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/Row.kt rename to src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/OutputRow.kt index 773b1e8..c912678 100644 --- a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/Row.kt +++ b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/OutputRow.kt @@ -1,8 +1,9 @@ package ru.landgrafhomyak.serdha.api.v0.runtime +import ru.landgrafhomyak.serdha.api.v0.LowLevelApi import ru.landgrafhomyak.serdha.api.v0.misc.Column -public interface Row { +public interface OutputRow { public operator fun get(c: Column): RuntimeType public operator fun get(c: Column): Byte = this.get(c) @@ -15,4 +16,20 @@ public interface Row { public operator fun get(c: Column): ULong = this.get(c) public operator fun get(c: Column): Char = this.get(c) public operator fun get(c: Column): Boolean = this.get(c) + + @Suppress("ClassName") + @LowLevelApi + public interface _Iterator : OutputRow { + @Suppress("FunctionName") + @LowLevelApi + public suspend fun _next(): Boolean + + @Suppress("FunctionName") + @LowLevelApi + public suspend fun _abort() + + @Suppress("FunctionName") + @LowLevelApi + public suspend fun _finish(): Next + } } \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/SynchronizedDatabase.kt b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/SynchronizedDatabase.kt index 4c0cb1d..7677b9d 100644 --- a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/SynchronizedDatabase.kt +++ b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/SynchronizedDatabase.kt @@ -4,11 +4,7 @@ import ru.landgrafhomyak.serdha.api.v0.LowLevelApi import ru.landgrafhomyak.serdha.api.v0.module.Module import ru.landgrafhomyak.serdha.api.v0.table.RowId import ru.landgrafhomyak.serdha.api.v0.table.Table -import ru.landgrafhomyak.serdha.api.v0.queries.CopyQuery -import ru.landgrafhomyak.serdha.api.v0.queries.DeleteQuery -import ru.landgrafhomyak.serdha.api.v0.queries.InsertQuery -import ru.landgrafhomyak.serdha.api.v0.queries.SelectQuery -import ru.landgrafhomyak.serdha.api.v0.queries.UpdateQuery +import ru.landgrafhomyak.serdha.api.v0.queries._Query public interface SynchronizedDatabase : Module { @Suppress("FunctionName") @@ -17,23 +13,19 @@ public interface SynchronizedDatabase : Module _autoTransactedSelect(query: SelectQuery): _ParametersSetter> + public suspend fun _executeAutoTransactedQuery(compiledQuery: _Query.Void2Table): OutputRow._Iterator @Suppress("FunctionName") @LowLevelApi - public suspend fun _autoTransactedInsert(query: InsertQuery): _ParametersSetter, _ResultSet>?>> + public suspend fun _executeAutoTransactedQuery(compiledQuery: _Query.Params2Table): InputRow._Scope> @Suppress("FunctionName") @LowLevelApi - public suspend fun _autoTransactedCopy(query: CopyQuery): _ParametersSetter>?> + public suspend fun _executeAutoTransactedQuery(compiledQuery: _Query.Table2Void): InputRow._Iterator @Suppress("FunctionName") @LowLevelApi - public suspend fun _autoTransactedUpdate(query: UpdateQuery): _ParametersSetter?> - - @Suppress("FunctionName") - @LowLevelApi - public suspend fun _autoTransactedDelete(query: DeleteQuery): _ParametersSetter?> + public suspend fun _executeAutoTransactedQuery(compiledQuery: _Query.Params2Void): InputRow._Scope public interface AtomicScript { public suspend fun executeTransaction(transaction: Transaction, context: C, args: A): R @@ -47,7 +39,7 @@ public interface SynchronizedDatabase : Module mapOfRowIds( table: Table, - builder: (MutableMap, V>) -> Unit + builder: (MutableMap, V>) -> Unit ): Map, V> public fun mutableMapOfRowIds(table: Table): MutableMap, V> diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/Transaction.kt b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/Transaction.kt index 636e2c7..85b2c99 100644 --- a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/Transaction.kt +++ b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/Transaction.kt @@ -1,32 +1,24 @@ package ru.landgrafhomyak.serdha.api.v0.runtime import ru.landgrafhomyak.serdha.api.v0.LowLevelApi -import ru.landgrafhomyak.serdha.api.v0.queries.CopyQuery -import ru.landgrafhomyak.serdha.api.v0.queries.DeleteQuery -import ru.landgrafhomyak.serdha.api.v0.queries.InsertQuery -import ru.landgrafhomyak.serdha.api.v0.queries.SelectQuery -import ru.landgrafhomyak.serdha.api.v0.queries.UpdateQuery +import ru.landgrafhomyak.serdha.api.v0.queries._Query public interface Transaction { @Suppress("FunctionName") @LowLevelApi - public fun _select(compiledQuery: SelectQuery): _ParametersSetter> + public suspend fun _executeQuery(compiledQuery: _Query.Void2Table): OutputRow._Iterator @Suppress("FunctionName") @LowLevelApi - public fun _insert(compiledQuery: InsertQuery): _ParametersSetter, _ResultSet>?>> + public suspend fun _executeQuery(compiledQuery: _Query.Params2Table): InputRow._Scope> @Suppress("FunctionName") @LowLevelApi - public fun _copy(compiledQuery: CopyQuery): _ParametersSetter>?> + public suspend fun _executeQuery(compiledQuery: _Query.Table2Void): InputRow._Iterator @Suppress("FunctionName") @LowLevelApi - public fun _update(compiledQuery: UpdateQuery): _ParametersSetter?> - - @Suppress("FunctionName") - @LowLevelApi - public fun _delete(compiledQuery: DeleteQuery): _ParametersSetter?> + public suspend fun _executeQuery(compiledQuery: _Query.Params2Void): InputRow._Scope public suspend fun rollback() diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/_ParametersSetter.kt b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/_ParametersSetter.kt deleted file mode 100644 index 393d8d0..0000000 --- a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/_ParametersSetter.kt +++ /dev/null @@ -1,19 +0,0 @@ -package ru.landgrafhomyak.serdha.api.v0.runtime - -import ru.landgrafhomyak.serdha.api.v0.LowLevelApi - -@Suppress("ClassName", "FunctionName") -@LowLevelApi -public interface _ParametersSetter : ParametersSetter { - @LowLevelApi - public suspend fun _execute(): Result - - @LowLevelApi - public suspend fun _abort() - - @LowLevelApi - public interface _Multi : _ParametersSetter { - @LowLevelApi - public fun _next(): Boolean - } -} \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/_ResultSet.kt b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/_ResultSet.kt deleted file mode 100644 index cea86d5..0000000 --- a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/_ResultSet.kt +++ /dev/null @@ -1,19 +0,0 @@ -package ru.landgrafhomyak.serdha.api.v0.runtime - -import ru.landgrafhomyak.serdha.api.v0.LowLevelApi - -@Suppress("ClassName") -@LowLevelApi -public interface _ResultSet : Row { - @Suppress("FunctionName") - @LowLevelApi - public suspend fun _next(): Boolean - - @Suppress("FunctionName") - @LowLevelApi - public suspend fun _abort() - - @Suppress("FunctionName") - @LowLevelApi - public suspend fun _close(): Next -} \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/transaction_methods.kt b/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/transaction_methods.kt deleted file mode 100644 index d41632a..0000000 --- a/src/commonMain/kotlin/ru/landgrafhomyak/serdha/api/v0/runtime/transaction_methods.kt +++ /dev/null @@ -1,1121 +0,0 @@ -@file:JvmName("TransactionMethodsKt") - -package ru.landgrafhomyak.serdha.api.v0.runtime - -import kotlin.contracts.InvocationKind -import kotlin.contracts.contract -import kotlin.jvm.JvmName -import ru.landgrafhomyak.serdha.api.v0.LowLevelApi -import ru.landgrafhomyak.serdha.api.v0.queries.CopyQuery -import ru.landgrafhomyak.serdha.api.v0.queries.DeleteQuery -import ru.landgrafhomyak.serdha.api.v0.queries.InsertQuery -import ru.landgrafhomyak.serdha.api.v0.queries.SelectQuery -import ru.landgrafhomyak.serdha.api.v0.queries.UpdateQuery - - -@Suppress("FunctionName") -@PublishedApi -internal inline fun _safeAutoClose(onAbort: () -> Unit = {}, onSuccess: () -> Unit = {}, action: () -> R): R { - contract { - callsInPlace(action, InvocationKind.EXACTLY_ONCE) - callsInPlace(onAbort, InvocationKind.AT_MOST_ONCE) - callsInPlace(onSuccess, InvocationKind.AT_MOST_ONCE) - } - val ret: R - try { - ret = action() - } catch (e1: Throwable) { - try { - onAbort() - } catch (e2: Throwable) { - e1.addSuppressed(e2) - } - throw e1 - } - onSuccess() - return ret -} - -@Suppress("FunctionName") -@PublishedApi -internal inline fun _safeAutoClose(finally: () -> Unit, action: () -> R): R { - contract { - callsInPlace(action, InvocationKind.EXACTLY_ONCE) - callsInPlace(finally, InvocationKind.EXACTLY_ONCE) - } - return _safeAutoClose(finally, finally, action) -} - -public class QueryWithoutReturnError(message: String, public val query: Any) : Error(message) - -@Suppress("FunctionName", "DuplicatedCode") -@PublishedApi -@OptIn(LowLevelApi::class) -internal suspend inline fun _wrapWithLambdas( - compiledQuery: Any, - queryGetter: () -> _ParametersSetter?>, - parameters: (ParametersSetter) -> Unit, - hasReturning: Boolean, - returning: (_ResultSet) -> Unit, - queryWithoutErrorMessage: String -) { - contract { - callsInPlace(queryGetter, InvocationKind.EXACTLY_ONCE) - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(returning, InvocationKind.AT_MOST_ONCE) - } - val paramsSetter = queryGetter() - - _safeAutoClose(onAbort = { paramsSetter._abort() }) { - parameters(paramsSetter) - } - - val rows = paramsSetter._execute() - if (rows == null) { - if (hasReturning) - throw QueryWithoutReturnError(queryWithoutErrorMessage, compiledQuery) - return - } - - _safeAutoClose(onAbort = { rows._abort() }, onSuccess = { rows._close() }) { - returning(rows) - } -} - -public class ResultNotSingleError : Error() - -@Suppress("FunctionName") -@PublishedApi -@OptIn(LowLevelApi::class) -internal suspend inline fun _wrapWithLambdasSingleOrNull( - compiledQuery: Any, - queryGetter: () -> _ParametersSetter?>, - parameters: (ParametersSetter) -> Unit, - hasReturning: Boolean, - returning: (Row) -> R, - queryWithoutErrorMessage: String -): R? { - contract { - callsInPlace(queryGetter, InvocationKind.EXACTLY_ONCE) - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(returning, InvocationKind.AT_MOST_ONCE) - } - - _wrapWithLambdas( - compiledQuery, queryGetter, parameters, hasReturning, - { rs -> - if (!rs._next()) - return@_wrapWithLambdasSingleOrNull null - val ret = returning(rs) - if (rs._next()) - throw ResultNotSingleError() - return@_wrapWithLambdasSingleOrNull ret - }, - queryWithoutErrorMessage - ) - throw QueryWithoutReturnError(queryWithoutErrorMessage, compiledQuery) -} - - -@Suppress("FunctionName") -@PublishedApi -@OptIn(LowLevelApi::class) -internal suspend inline fun _wrapWithLambdasIterate( - compiledQuery: Any, - queryGetter: () -> _ParametersSetter?>, - parameters: (ParametersSetter) -> Unit, - hasReturning: Boolean, - returning: (Row) -> Unit, - queryWithoutErrorMessage: String -) { - contract { - callsInPlace(queryGetter, InvocationKind.EXACTLY_ONCE) - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(returning, InvocationKind.UNKNOWN) - } - - return _wrapWithLambdas(compiledQuery, queryGetter, parameters, hasReturning, { rs -> while (rs._next()) returning(rs) }, queryWithoutErrorMessage) -} - -@Suppress("FunctionName") -@PublishedApi -@OptIn(LowLevelApi::class) -internal suspend inline fun _wrapWithLambdasMap( - compiledQuery: Any, - queryGetter: () -> _ParametersSetter?>, - parameters: (ParametersSetter) -> Unit, - hasReturning: Boolean, - returning: (Row) -> R, - queryWithoutErrorMessage: String -): List { - contract { - callsInPlace(queryGetter, InvocationKind.EXACTLY_ONCE) - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(returning, InvocationKind.UNKNOWN) - } - - _wrapWithLambdas( - compiledQuery, queryGetter, parameters, hasReturning, - { rs -> - val out = ArrayList() - while (rs._next()) - out.add(returning(rs)) - return@_wrapWithLambdasMap out - }, - queryWithoutErrorMessage - ) - throw QueryWithoutReturnError(queryWithoutErrorMessage, compiledQuery) -} - - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedSelectSingleOrNull( - compiledQuery: SelectQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> R -): R? { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.AT_MOST_ONCE) - } - return _wrapWithLambdasSingleOrNull( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedSelect(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.selectSingleOrNull( - compiledQuery: SelectQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> R -): R? { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.AT_MOST_ONCE) - } - return _wrapWithLambdasSingleOrNull( - compiledQuery = compiledQuery, - queryGetter = { this._select(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.selectThenIterate( - compiledQuery: SelectQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> Unit -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.UNKNOWN) - } - _wrapWithLambdasIterate( - compiledQuery = compiledQuery, - queryGetter = { this._select(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedSelectThenIterate( - compiledQuery: SelectQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> Unit -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.UNKNOWN) - } - _wrapWithLambdasIterate( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedSelect(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.selectThenMap( - compiledQuery: SelectQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> R -): List { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.UNKNOWN) - } - return _wrapWithLambdasMap( - compiledQuery = compiledQuery, - queryGetter = { this._select(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedSelectThenMap( - compiledQuery: SelectQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> R -): List { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.UNKNOWN) - } - return _wrapWithLambdasMap( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedSelect(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - -@Suppress("FunctionName", "DuplicatedCode") -@PublishedApi -@OptIn(LowLevelApi::class) -internal suspend inline fun _wrapInsertWithLambdas( - compiledQuery: InsertQuery, - queryGetter: () -> _ParametersSetter, _ResultSet>?>>, - parameters: (ParametersSetter) -> Unit, - data: Iterable, - rowFormatter: (T, ParametersSetter>) -> Unit, - hasReturning: Boolean, - returningInserted: (_ResultSet) -> Unit, - returningUpdated: (_ResultSet) -> Unit, - queryWithoutErrorMessage: String -) { - contract { - callsInPlace(queryGetter, InvocationKind.EXACTLY_ONCE) - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowFormatter, InvocationKind.UNKNOWN) - callsInPlace(returningInserted, InvocationKind.AT_MOST_ONCE) - callsInPlace(returningUpdated, InvocationKind.AT_MOST_ONCE) - } - val paramsSetter = queryGetter() - - _safeAutoClose(onAbort = { paramsSetter._abort() }) { - parameters(paramsSetter) - } - - val insertableValues = paramsSetter._execute() - _safeAutoClose(onAbort = { insertableValues._abort() }) { - for (e in data) { - insertableValues._next() - rowFormatter(e, insertableValues) - } - } - - val insertedRows = insertableValues._execute() - if (insertedRows == null) { - if (hasReturning) - throw QueryWithoutReturnError(queryWithoutErrorMessage, compiledQuery) - return - } - - _safeAutoClose(onAbort = { insertedRows._abort() }) { - returningInserted(insertedRows) - } - val updatedRows = insertedRows._close() - _safeAutoClose(onAbort = { insertedRows._abort() }, onSuccess = { updatedRows._abort() }) { - returningInserted(insertedRows) - } -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.insert( - compiledQuery: InsertQuery, - parameters: (ParametersSetter) -> Unit = {}, - data: Iterable, - rowFormatter: (T, ParametersSetter>) -> Unit, -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowFormatter, InvocationKind.UNKNOWN) - } - _wrapInsertWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._insert(compiledQuery) }, - parameters = parameters, - data = data, - rowFormatter = rowFormatter, - hasReturning = false, - returningInserted = {}, - returningUpdated = {}, - queryWithoutErrorMessage = "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedInsert( - compiledQuery: InsertQuery, - parameters: (ParametersSetter) -> Unit = {}, - data: Iterable, - rowFormatter: (T, ParametersSetter>) -> Unit, -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowFormatter, InvocationKind.UNKNOWN) - } - _wrapInsertWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedInsert(compiledQuery) }, - parameters = parameters, - data = data, - rowFormatter = rowFormatter, - hasReturning = false, - returningInserted = {}, - returningUpdated = {}, - queryWithoutErrorMessage = "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.insertReturningIterate( - compiledQuery: InsertQuery, - parameters: (ParametersSetter) -> Unit = {}, - data: Iterable, - rowFormatter: (T, ParametersSetter>) -> Unit, - insertedRows: (Row) -> Unit, - updatedRows: (Row) -> Unit, -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowFormatter, InvocationKind.UNKNOWN) - } - _wrapInsertWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._insert(compiledQuery) }, - parameters = parameters, - data = data, - rowFormatter = rowFormatter, - hasReturning = true, - returningInserted = { rs -> while (rs._next()) insertedRows(rs) }, - returningUpdated = { rs -> while (rs._next()) updatedRows(rs) }, - queryWithoutErrorMessage = "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedInsertReturningIterate( - compiledQuery: InsertQuery, - parameters: (ParametersSetter) -> Unit = {}, - data: Iterable, - rowFormatter: (T, ParametersSetter>) -> Unit, - insertedRows: (Row) -> Unit, - updatedRows: (Row) -> Unit, -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowFormatter, InvocationKind.UNKNOWN) - } - _wrapInsertWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedInsert(compiledQuery) }, - parameters = parameters, - data = data, - rowFormatter = rowFormatter, - hasReturning = true, - returningInserted = { rs -> while (rs._next()) insertedRows(rs) }, - returningUpdated = { rs -> while (rs._next()) updatedRows(rs) }, - queryWithoutErrorMessage = "" - ) -} - - -@PublishedApi -internal object SingleIterable : Iterable, Iterator { - private var isReturned = false - - override fun iterator(): Iterator = this - - override fun hasNext(): Boolean = !this.isReturned - - override fun next(): Unit { - if (this.isReturned) throw IllegalStateException() - this.isReturned = true - } -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.insertSingle( - compiledQuery: InsertQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowData: (ParametersSetter>) -> Unit, -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowData, InvocationKind.EXACTLY_ONCE) - } - _wrapInsertWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._insert(compiledQuery) }, - parameters = parameters, - data = SingleIterable, - rowFormatter = { _, r -> rowData(r) }, - hasReturning = false, - returningInserted = {}, - returningUpdated = {}, - queryWithoutErrorMessage = "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedInsertSingle( - compiledQuery: InsertQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowData: (ParametersSetter>) -> Unit, -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowData, InvocationKind.EXACTLY_ONCE) - } - _wrapInsertWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedInsert(compiledQuery) }, - parameters = parameters, - data = SingleIterable, - rowFormatter = { _, r -> rowData(r) }, - hasReturning = false, - returningInserted = {}, - returningUpdated = {}, - queryWithoutErrorMessage = "" - ) -} - -@Suppress("DuplicatedCode") -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.insertSingleReturning( - compiledQuery: InsertQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowData: (ParametersSetter>) -> Unit = {}, - insertedRows: (Row) -> R, - updatedRows: (Row) -> R -): R { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowData, InvocationKind.EXACTLY_ONCE) - callsInPlace(insertedRows, InvocationKind.AT_MOST_ONCE) - callsInPlace(updatedRows, InvocationKind.AT_MOST_ONCE) - } - var isReturned = false - var ret: R? = null - _wrapInsertWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._insert(compiledQuery) }, - parameters = parameters, - hasReturning = true, - data = SingleIterable, - rowFormatter = { _, r -> rowData(r) }, - returningInserted = returningInserted@{ rs -> - if (!rs._next()) return@returningInserted - isReturned = true - ret = insertedRows(rs) - if (rs._next()) throw ResultNotSingleError() - }, - returningUpdated = returningUpdated@{ rs -> - if (!rs._next()) return@returningUpdated - if (isReturned) throw ResultNotSingleError() - isReturned = true - ret = insertedRows(rs) - if (rs._next()) throw ResultNotSingleError() - }, - queryWithoutErrorMessage = "" - ) - if (!isReturned) - throw QueryWithoutReturnError("", compiledQuery) - @Suppress("UNCHECKED_CAST") - return ret as R -} - -@Suppress("DuplicatedCode") -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedInsertSingleReturning( - compiledQuery: InsertQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowData: (ParametersSetter>) -> Unit = {}, - insertedRows: (Row) -> R, - updatedRows: (Row) -> R -): R { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowData, InvocationKind.EXACTLY_ONCE) - callsInPlace(insertedRows, InvocationKind.AT_MOST_ONCE) - callsInPlace(updatedRows, InvocationKind.AT_MOST_ONCE) - } - var isReturned = false - var ret: R? = null - _wrapInsertWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedInsert(compiledQuery) }, - parameters = parameters, - hasReturning = true, - data = SingleIterable, - rowFormatter = { _, r -> rowData(r) }, - returningInserted = returningInserted@{ rs -> - if (!rs._next()) return@returningInserted - isReturned = true - ret = insertedRows(rs) - if (rs._next()) throw ResultNotSingleError() - }, - returningUpdated = returningUpdated@{ rs -> - if (!rs._next()) return@returningUpdated - if (isReturned) throw ResultNotSingleError() - isReturned = true - ret = insertedRows(rs) - if (rs._next()) throw ResultNotSingleError() - }, - queryWithoutErrorMessage = "" - ) - if (!isReturned) - throw QueryWithoutReturnError("", compiledQuery) - @Suppress("UNCHECKED_CAST") - return ret as R -} - -@Suppress("FunctionName", "DuplicatedCode") -@PublishedApi -@OptIn(LowLevelApi::class) -internal suspend inline fun _wrapCopyWithLambdas( - compiledQuery: CopyQuery, - queryGetter: () -> _ParametersSetter>?>, - parameters: (ParametersSetter) -> Unit, - hasReturning: Boolean, - returningInserted: (_ResultSet) -> Unit, - returningUpdated: (_ResultSet) -> Unit, - queryWithoutErrorMessage: String -) { - contract { - callsInPlace(queryGetter, InvocationKind.EXACTLY_ONCE) - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(returningInserted, InvocationKind.AT_MOST_ONCE) - callsInPlace(returningUpdated, InvocationKind.AT_MOST_ONCE) - } - val paramsSetter = queryGetter() - - _safeAutoClose(onAbort = { paramsSetter._abort() }) { - parameters(paramsSetter) - } - - val insertedRows = paramsSetter._execute() - if (insertedRows == null) { - if (hasReturning) - throw QueryWithoutReturnError(queryWithoutErrorMessage, compiledQuery) - return - } - - _safeAutoClose(onAbort = { insertedRows._abort() }) { - returningInserted(insertedRows) - } - val updatedRows = insertedRows._close() - _safeAutoClose(onAbort = { insertedRows._abort() }, onSuccess = { updatedRows._abort() }) { - returningInserted(insertedRows) - } -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.copy( - compiledQuery: CopyQuery, - parameters: (ParametersSetter) -> Unit = {}, -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - } - _wrapCopyWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._copy(compiledQuery) }, - parameters = parameters, - hasReturning = false, - returningInserted = {}, - returningUpdated = {}, - queryWithoutErrorMessage = "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedCopy( - compiledQuery: CopyQuery, - parameters: (ParametersSetter) -> Unit = {}, -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - } - _wrapCopyWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedCopy(compiledQuery) }, - parameters = parameters, - hasReturning = false, - returningInserted = {}, - returningUpdated = {}, - queryWithoutErrorMessage = "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.copyReturningIterate( - compiledQuery: CopyQuery, - parameters: (ParametersSetter) -> Unit = {}, - insertedRows: (Row) -> Unit, - updatedRows: (Row) -> Unit -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(insertedRows, InvocationKind.UNKNOWN) - callsInPlace(updatedRows, InvocationKind.UNKNOWN) - } - _wrapCopyWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._copy(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returningInserted = { rs -> while (rs._next()) insertedRows(rs) }, - returningUpdated = { rs -> while (rs._next()) updatedRows(rs) }, - queryWithoutErrorMessage = "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedCopyReturningIterate( - compiledQuery: CopyQuery, - parameters: (ParametersSetter) -> Unit = {}, - insertedRows: (Row) -> Unit, - updatedRows: (Row) -> Unit -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(insertedRows, InvocationKind.UNKNOWN) - callsInPlace(updatedRows, InvocationKind.UNKNOWN) - } - _wrapCopyWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedCopy(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returningInserted = { rs -> while (rs._next()) insertedRows(rs) }, - returningUpdated = { rs -> while (rs._next()) updatedRows(rs) }, - queryWithoutErrorMessage = "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.copyReturningSingleOrNull( - compiledQuery: CopyQuery, - parameters: (ParametersSetter) -> Unit = {}, - insertedRows: (Row) -> R, - updatedRows: (Row) -> R -): R? { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(insertedRows, InvocationKind.AT_MOST_ONCE) - callsInPlace(updatedRows, InvocationKind.AT_MOST_ONCE) - } - var isReturned = false - var ret: R? = null - _wrapCopyWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._copy(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returningInserted = returningInserted@{ rs -> - if (!rs._next()) return@returningInserted - isReturned = true - ret = insertedRows(rs) - if (rs._next()) throw ResultNotSingleError() - }, - returningUpdated = returningUpdated@{ rs -> - if (!rs._next()) return@returningUpdated - if (isReturned) throw ResultNotSingleError() - isReturned = true - ret = insertedRows(rs) - if (rs._next()) throw ResultNotSingleError() - }, - queryWithoutErrorMessage = "" - ) - if (!isReturned) - return null - return ret -} - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedCopyReturningSingleOrNull( - compiledQuery: CopyQuery, - parameters: (ParametersSetter) -> Unit = {}, - insertedRows: (Row) -> R, - updatedRows: (Row) -> R -): R? { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(insertedRows, InvocationKind.AT_MOST_ONCE) - callsInPlace(updatedRows, InvocationKind.AT_MOST_ONCE) - } - var isReturned = false - var ret: R? = null - _wrapCopyWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedCopy(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returningInserted = returningInserted@{ rs -> - if (!rs._next()) return@returningInserted - isReturned = true - ret = insertedRows(rs) - if (rs._next()) throw ResultNotSingleError() - }, - returningUpdated = returningUpdated@{ rs -> - if (!rs._next()) return@returningUpdated - if (isReturned) throw ResultNotSingleError() - isReturned = true - ret = insertedRows(rs) - if (rs._next()) throw ResultNotSingleError() - }, - queryWithoutErrorMessage = "" - ) - if (!isReturned) - return null - return ret -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.update( - compiledQuery: UpdateQuery, - parameters: (ParametersSetter) -> Unit = {}, -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - } - _wrapWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._update(compiledQuery) }, - parameters = parameters, - hasReturning = false, - returning = {}, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedUpdate( - compiledQuery: UpdateQuery, - parameters: (ParametersSetter) -> Unit = {}, -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - } - _wrapWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedUpdate(compiledQuery) }, - parameters = parameters, - hasReturning = false, - returning = {}, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.updateReturningSingleOrNull( - compiledQuery: UpdateQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> R -): R? { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.AT_MOST_ONCE) - } - return _wrapWithLambdasSingleOrNull( - compiledQuery = compiledQuery, - queryGetter = { this._update(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedUpdateReturningSingleOrNull( - compiledQuery: UpdateQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> R -): R? { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.AT_MOST_ONCE) - } - return _wrapWithLambdasSingleOrNull( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedUpdate(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.updateReturningIterate( - compiledQuery: UpdateQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> Unit -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.UNKNOWN) - } - _wrapWithLambdasIterate( - compiledQuery = compiledQuery, - queryGetter = { this._update(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedUpdateReturningIterate( - compiledQuery: UpdateQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> Unit -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.UNKNOWN) - } - _wrapWithLambdasIterate( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedUpdate(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.updateReturningMap( - compiledQuery: UpdateQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> R -): List { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.UNKNOWN) - } - return _wrapWithLambdasMap( - compiledQuery = compiledQuery, - queryGetter = { this._update(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedUpdateReturningMap( - compiledQuery: UpdateQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> R -): List { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.UNKNOWN) - } - return _wrapWithLambdasMap( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedUpdate(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.delete( - compiledQuery: DeleteQuery, - parameters: (ParametersSetter) -> Unit = {}, -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - } - _wrapWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._delete(compiledQuery) }, - parameters = parameters, - hasReturning = false, - returning = {}, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedDelete( - compiledQuery: DeleteQuery, - parameters: (ParametersSetter) -> Unit = {}, -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - } - _wrapWithLambdas( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedDelete(compiledQuery) }, - parameters = parameters, - hasReturning = false, - returning = {}, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.deleteReturningSingleOrNull( - compiledQuery: DeleteQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> R -): R? { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.AT_MOST_ONCE) - } - return _wrapWithLambdasSingleOrNull( - compiledQuery = compiledQuery, - queryGetter = { this._delete(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedDeleteReturningSingleOrNull( - compiledQuery: DeleteQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> R -): R? { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.AT_MOST_ONCE) - } - return _wrapWithLambdasSingleOrNull( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedDelete(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.deleteReturningIterate( - compiledQuery: DeleteQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> Unit -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.UNKNOWN) - } - _wrapWithLambdasIterate( - compiledQuery = compiledQuery, - queryGetter = { this._delete(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedDeleteReturningIterate( - compiledQuery: DeleteQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> Unit -) { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.UNKNOWN) - } - _wrapWithLambdasIterate( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedDelete(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun Transaction.deleteReturningMap( - compiledQuery: DeleteQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> R -): List { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.UNKNOWN) - } - return _wrapWithLambdasMap( - compiledQuery = compiledQuery, - queryGetter = { this._delete(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend inline fun SynchronizedDatabase<*>.autoTransactedDeleteReturningMap( - compiledQuery: DeleteQuery, - parameters: (ParametersSetter) -> Unit = {}, - rowsConsumer: (Row) -> R -): List { - contract { - callsInPlace(parameters, InvocationKind.EXACTLY_ONCE) - callsInPlace(rowsConsumer, InvocationKind.UNKNOWN) - } - return _wrapWithLambdasMap( - compiledQuery = compiledQuery, - queryGetter = { this._autoTransactedDelete(compiledQuery) }, - parameters = parameters, - hasReturning = true, - returning = rowsConsumer, - "" - ) -} - -@OptIn(LowLevelApi::class) -public suspend fun SynchronizedDatabase<*>.transaction(body: (Transaction) -> R): R { - contract { - callsInPlace(body, InvocationKind.EXACTLY_ONCE) - } - - val tr = this._startTransaction() - _safeAutoClose(onAbort = { tr.rollback(); tr._assertTransactionFinishedAndReleaseResources() }, onSuccess = { tr._assertTransactionFinishedAndReleaseResources() }) { - return@transaction body(tr) - } -} \ No newline at end of file