Skip to content

Commit

Permalink
WIP: Make AdaptorOperation a struct
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
helje5 committed Jan 2, 2025
1 parent 7204d34 commit e1edeac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
18 changes: 9 additions & 9 deletions Sources/ZeeQL/Access/AdaptorChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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?

Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions Sources/ZeeQL/Access/AdaptorOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 9 additions & 4 deletions Sources/ZeeQL/Access/DatabaseChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e1edeac

Please sign in to comment.