Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
helje5 committed Jan 3, 2025
2 parents 6e9fbbc + 5b43abc commit 457529b
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 83 deletions.
6 changes: 3 additions & 3 deletions Sources/ZeeQL/Access/ActiveRecordType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public protocol ActiveRecordType : DatabaseObject,
}

public extension ActiveRecordType {
var globalID: GlobalID? {
return entity.globalIDForRow(self)
}

@inlinable
var globalID: GlobalID? { return entity.globalIDForRow(self) }
}

public protocol DatabaseBoundObject {
Expand Down
50 changes: 32 additions & 18 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 @@ -215,15 +215,15 @@ public extension AdaptorChannel {
}

/**
* This calls performAdaptorOperationN() and returns a success (null) when
* exactly one row was affected.
* This calls ``performAdaptorOperationN(_:)`` and returns a success (null)
* when exactly one row was affected.
*
* - parameters:
* - op: the `AdaptorOperation` object
* - Parameters:
* - 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 @@ -234,11 +234,14 @@ public extension AdaptorChannel {
* or deleteRowsDescri...() with the information contained in the operation
* object.
*
* This method is different to performAdaptorOperation() [w/o 'N' ;-)]
* This method is different to ``performAdaptorOperation(_:)-5j8fn`` [w/o 'N']
* because it returns the count of affected objects (eg how many rows got
* deleted or updated).
*
* - 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 Expand Up @@ -291,14 +294,26 @@ public extension AdaptorChannel {
return affectedRows
}

// MARK: - Adaptor Operations

@inlinable
func performAdaptorOperations(_ ops : [ AdaptorOperation ]) throws {
var ops = ops
try performAdaptorOperations(&ops)
}
@inlinable
func performAdaptorOperation(_ op : AdaptorOperation) throws {
var op = op
try performAdaptorOperation(&op)
}
}

/**
* A `[ String : Any? ]` dictionary with an optional value to represent
* NULL columns.
*
* So not mix up w/ `AdaptorRecord`, which are primarily used for fetching
* (to share the keys of a schema)
* Not to be mixed up w/ ``AdaptorRecord``, which are primarily used for
* fetching (to share the keys of a schema).
*/
public typealias AdaptorRow = Dictionary<String, Any?>

Expand All @@ -317,8 +332,7 @@ public extension AdaptorChannel { // MARK: - Operations
func lockRowComparingAttributes(_ attrs : [ Attribute ]?,
_ entity : Entity,
_ qualifier : Qualifier?,
_ snapshot : AdaptorRow?) throws
-> Bool
_ snapshot : AdaptorRow?) throws -> Bool
{
let q = snapshot != nil ? qualifierToMatchAllValues(snapshot!) : nil
let fs = ModelFetchSpecification(entity: entity,
Expand Down
34 changes: 31 additions & 3 deletions Sources/ZeeQL/Access/AdaptorOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,36 @@
* 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
public static func update(_ e: Entity, set: AdaptorRow, where q: Qualifier)
-> Self
{
var me = AdaptorOperation(entity: e)
me.adaptorOperator = .update
me.changedValues = set
me.qualifier = q
return me
}

@inlinable
public static func insert(_ values: AdaptorRow, into e: Entity) -> Self {
var me = AdaptorOperation(entity: e)
me.adaptorOperator = .insert
me.changedValues = values
return me
}

@inlinable
public static func delete(from e: Entity, where q: Qualifier) -> Self {
var me = AdaptorOperation(entity: e)
me.adaptorOperator = .delete
me.qualifier = q
return me
}

public enum Operator : Int { /* Note: sequence is relevant for comparison */
case none = 0
case lock = 1
Expand All @@ -34,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 @@ -95,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 Expand Up @@ -123,5 +150,6 @@ public class AdaptorOperation: Comparable, EquatableType, SmartDescription {
}

#if swift(>=5.5)
// AdaptorOperation itself can't be sent yet due to the closure.
extension AdaptorOperation.Operator: Sendable {}
#endif
Loading

0 comments on commit 457529b

Please sign in to comment.