From e1edeac7b5e0accaac5ba441512e36a32052467d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20He=C3=9F?= Date: Thu, 2 Jan 2025 16:28:37 +0100 Subject: [PATCH] WIP: Make `AdaptorOperation` a struct Since an adaptor operation has a completion closure which is reset when it got performed, a set of methods take inout's now. Also the resultRow value is being updated. Note that the `DatabaseChannel` *does* make use of that to update the related DatabaseOperation objects. --- Sources/ZeeQL/Access/AdaptorChannel.swift | 18 +++++++++--------- Sources/ZeeQL/Access/AdaptorOperation.swift | 6 +++--- Sources/ZeeQL/Access/DatabaseChannel.swift | 13 +++++++++---- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Sources/ZeeQL/Access/AdaptorChannel.swift b/Sources/ZeeQL/Access/AdaptorChannel.swift index 023811c..4c7aa71 100644 --- a/Sources/ZeeQL/Access/AdaptorChannel.swift +++ b/Sources/ZeeQL/Access/AdaptorChannel.swift @@ -29,7 +29,7 @@ public protocol AdaptorChannel : AdaptorQueryType, ModelNameMapper { func begin() throws func commit() throws func rollback() throws - var isTransactionInProgress : Bool { get } + var isTransactionInProgress : Bool { get } // MARK: - Reflection @@ -45,8 +45,8 @@ public protocol AdaptorChannel : AdaptorQueryType, ModelNameMapper { // MARK: - Adaptor Operations - func performAdaptorOperations(_ ops : [ AdaptorOperation ]) throws - func performAdaptorOperation (_ op : AdaptorOperation) throws + func performAdaptorOperations(_ ops : inout [ AdaptorOperation ]) throws + func performAdaptorOperation (_ op : inout AdaptorOperation) throws // MARK: - Operations @@ -184,7 +184,7 @@ public extension AdaptorChannel { * - parameters: * - ops: the array of AdaptorOperation's to be performed */ - func performAdaptorOperations(_ ops: [ AdaptorOperation ]) throws { + func performAdaptorOperations(_ ops: inout [ AdaptorOperation ]) throws { // TBD: we should probably open a transaction if count > 1? Or is this the // responsibility of the user? @@ -202,8 +202,8 @@ public extension AdaptorChannel { if didOpenTx { try begin() } do { - for op in ops { - try performAdaptorOperation(op) + for idx in ops.indices { + try performAdaptorOperation(&ops[idx]) } } catch { @@ -222,8 +222,8 @@ public extension AdaptorChannel { * - op: the ``AdaptorOperation`` to be performed */ @inlinable - func performAdaptorOperation(_ op: AdaptorOperation) throws { - let affectedRows = try performAdaptorOperationN(op) + func performAdaptorOperation(_ op: inout AdaptorOperation) throws { + let affectedRows = try performAdaptorOperationN(&op) guard affectedRows == 1 else { throw AdaptorChannelError.OperationDidNotAffectOne } @@ -241,7 +241,7 @@ public extension AdaptorChannel { * - Parameters: * - op: the ``AdaptorOperation`` to be performed */ - func performAdaptorOperationN(_ op: AdaptorOperation) throws -> Int { + func performAdaptorOperationN(_ op: inout AdaptorOperation) throws -> Int { // TBD: we might want to move evaluation to this method and make // updateValuesInRows..() etc create AdaptorOperation's. This might // easen the creation of non-SQL adaptors. diff --git a/Sources/ZeeQL/Access/AdaptorOperation.swift b/Sources/ZeeQL/Access/AdaptorOperation.swift index baf2260..060dc19 100644 --- a/Sources/ZeeQL/Access/AdaptorOperation.swift +++ b/Sources/ZeeQL/Access/AdaptorOperation.swift @@ -11,7 +11,7 @@ * an INSERT. The object keeps all the relevant information to calculate the * SQL for the operation. */ -public class AdaptorOperation: Comparable, EquatableType, SmartDescription { +public struct AdaptorOperation: Comparable, EquatableType, SmartDescription { // Note: an object because we also return values using this @inlinable @@ -61,7 +61,7 @@ public class AdaptorOperation: Comparable, EquatableType, SmartDescription { public var resultRow : AdaptorRow? /// Run when the operation did complete - open var completionBlock : (() -> ())? + public var completionBlock : (() -> ())? @inlinable public init(entity: Entity) { @@ -122,7 +122,7 @@ public class AdaptorOperation: Comparable, EquatableType, SmartDescription { if bq == q { return self } // no change - let ao = AdaptorOperation(self) // copy + var ao = AdaptorOperation(self) // copy ao.qualifier = bq return ao } diff --git a/Sources/ZeeQL/Access/DatabaseChannel.swift b/Sources/ZeeQL/Access/DatabaseChannel.swift index fa3dce4..09df080 100644 --- a/Sources/ZeeQL/Access/DatabaseChannel.swift +++ b/Sources/ZeeQL/Access/DatabaseChannel.swift @@ -861,14 +861,19 @@ open class DatabaseChannelBase { /* turn db ops into adaptor ops */ - let aops = try adaptorOperationsForDatabaseOperations(ops) + var aops = try adaptorOperationsForDatabaseOperations(ops) guard !aops.isEmpty else { return } /* nothing to do */ /* perform adaptor ops */ var didOpenChannel = false - if adaptorChannel == nil { + let adaptorChannel : AdaptorChannel + if let c = self.adaptorChannel { + adaptorChannel = c + } + else { adaptorChannel = try acquireChannel() + self.adaptorChannel = adaptorChannel didOpenChannel = true } defer { @@ -879,7 +884,7 @@ open class DatabaseChannelBase { // Transactions: The `AdaptorChannel` opens a transaction if there is more // than one operation. Also: the `Database` embeds it into a TX too. - try adaptorChannel!.performAdaptorOperations(aops) + try adaptorChannel.performAdaptorOperations(&aops) // OK, the database operations have been successful. Now we need to handle @@ -971,7 +976,7 @@ open class DatabaseChannelBase { for op in ops { let entity = op.entity - let aop = AdaptorOperation(entity: entity) + var aop = AdaptorOperation(entity: entity) var dbop = op.databaseOperator if case .none = op.databaseOperator {