Skip to content

Commit

Permalink
Add a few public initializers
Browse files Browse the repository at this point in the history
...
  • Loading branch information
helje5 committed Nov 17, 2024
1 parent 897a72f commit bfb2a08
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
30 changes: 22 additions & 8 deletions Sources/ZeeQL/Control/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
// ZeeQL
//
// Created by Helge Hess on 15/02/2017.
// Copyright © 2017-2019 ZeeZide GmbH. All rights reserved.
// Copyright © 2017-2024 ZeeZide GmbH. All rights reserved.
//

/**
* Expressions are:
* - Qualifier
* - SortOrdering
* - Constant
* - Key
* ``Expression``'s are:
* - ``Qualifier``
* - ``SortOrdering``
* - ``Constant``
* - ``Key``
*/
public protocol Expression {
// TBD: Collides w/ Swift 6 `Expression` in Foundation?

func addReferencedKeys(to set: inout Set<String>)

Expand All @@ -23,13 +24,21 @@ public protocol Expression {

}

public struct QualifierBindingNotFound : Swift.Error {
let binding : String
public struct QualifierBindingNotFound : Swift.Error, Hashable {
public let binding : String

@inlinable
init(binding: String) { self.binding = binding }
}

#if swift(>=5.5)
extension QualifierBindingNotFound : Sendable {}
#endif


public extension Expression {

@inlinable
var allReferencedKeys : [ String ] {
var keys = Set<String>()
addReferencedKeys(to: &keys)
Expand All @@ -39,6 +48,7 @@ public extension Expression {

// MARK: - Bindings

@inlinable
var bindingKeys : [ String ] {
var keys = Set<String>()
addBindingKeys(to: &keys)
Expand All @@ -48,9 +58,13 @@ public extension Expression {

public extension Expression { // default implementations

@inlinable
func addReferencedKeys(to set: inout Set<String>) {}

@inlinable
var hasUnresolvedBindings : Bool { return !bindingKeys.isEmpty }
@inlinable
func addBindingKeys(to set: inout Set<String>) { } // noop
@inlinable
func keyPathForBindingKey(_ variable: String) -> String? { return nil }
}
10 changes: 8 additions & 2 deletions Sources/ZeeQL/Control/QualifierVariable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
// ZeeQL
//
// Created by Helge Hess on 16/02/2017.
// Copyright © 2017 ZeeZide GmbH. All rights reserved.
// Copyright © 2017-2024 ZeeZide GmbH. All rights reserved.
//

public struct QualifierVariable {
public struct QualifierVariable: Hashable {

public let key : String

@inlinable
public init(key: String) { self.key = key }
}

extension QualifierVariable : CustomStringConvertible {
public var description : String {
return "<QualifierVariable: \(key)>"
}
}

#if swift(>=5.5)
extension QualifierVariable : Sendable {}
#endif
22 changes: 18 additions & 4 deletions Sources/ZeeQL/Control/SQLQualifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
// ZeeQL
//
// Created by Helge Hess on 28/02/17.
// Copyright © 2017 ZeeZide GmbH. All rights reserved.
// Copyright © 2017-2024 ZeeZide GmbH. All rights reserved.
//

public struct SQLQualifier : Qualifier, Equatable {
public struct SQLQualifier : Qualifier, Hashable {
// TBD: maybe rename to 'RawQualifier'?

public enum Part: Equatable {
public enum Part: Hashable {
// TODO: lowercase cases for modern Swift

case RawSQLValue(String)
case QualifierVariable(String)

@inlinable
public static func ==(lhs: Part, rhs: Part) -> Bool {
switch ( lhs, rhs ) {
case ( RawSQLValue(let a), RawSQLValue(let b) ):
Expand All @@ -28,12 +31,14 @@ public struct SQLQualifier : Qualifier, Equatable {

public let parts : [ Part ]

@inlinable
public init(parts: [ Part ]) {
self.parts = parts // TODO: compact?
}

// MARK: - Bindings

@inlinable
public func addBindingKeys(to set: inout Set<String>) {
for part in parts {
if case .QualifierVariable(let key) = part {
Expand All @@ -42,13 +47,15 @@ public struct SQLQualifier : Qualifier, Equatable {
}
}

@inlinable
public var hasUnresolvedBindings : Bool {
for part in parts {
if case .QualifierVariable = part { return true }
}
return false
}

@inlinable
public func qualifierWith(bindings: Any?, requiresAll: Bool) throws
-> Qualifier?
{
Expand All @@ -74,10 +81,12 @@ public struct SQLQualifier : Qualifier, Equatable {

// MARK: - Equality

@inlinable
public static func ==(lhs: SQLQualifier, rhs: SQLQualifier) -> Bool {
return lhs.parts == rhs.parts
}


@inlinable
public func isEqual(to object: Any?) -> Bool {
guard let other = object as? SQLQualifier else { return false }
return self == other
Expand All @@ -104,3 +113,8 @@ public struct SQLQualifier : Qualifier, Equatable {
ms += "]"
}
}

#if swift(>=5.5)
extension SQLQualifier : Sendable {}
extension SQLQualifier.Part : Sendable {}
#endif

0 comments on commit bfb2a08

Please sign in to comment.