Skip to content

Commit

Permalink
Do not resolve if not necessary
Browse files Browse the repository at this point in the history
...
  • Loading branch information
helje5 committed Dec 7, 2024
1 parent 4f45fc6 commit fe59d54
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions Sources/ZeeQL/Control/FetchSpecification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,26 @@ extension FetchSpecification { // Default Imp
/**
* FIXME: document me. This seems to return values for all hints which end in
* 'BindPattern'. The values are retrieved by applying the
* KeyValueStringFormatter with the given object.
* `KeyValueStringFormatter` with the given object.
*
* This formatter does stuff like '%(lastname)s'.
* This formatter does stuff like `%(lastname)s`.
*
* Sample:
*
* var fs = FetchSpecification()
* fs[hint: "CustomQueryExpressionHintKeyBindPattern"] =
* "%%(tables)s WHERE id = %(id)s"
* ```swift
* var fs = FetchSpecification()
* fs[hint: "CustomQueryExpressionHintKeyBindPattern"] =
* "%%(tables)s WHERE id = %(id)s"
* ```
*/
func resolveHintBindPatternsWith(bindings: Any?) -> [ String : Any ] {
guard !hints.isEmpty else { return [:] }
@usableFromInline
func resolveHintBindPatterns(with bindings: Any?) -> [ String : Any ]? {
guard !hints.isEmpty else { return nil }

var didBind = false
var boundHints = hints
for ( key, value ) in hints {
guard key.hasSuffix("BindPattern") else { continue }
didBind = true

let sValue = "\(value)" // Hm

Expand All @@ -107,7 +111,10 @@ extension FetchSpecification { // Default Imp
boundHints.removeValue(forKey: key)
boundHints[bKey] = fValue
}
return boundHints
return didBind ? boundHints : nil
}
var hasUnresolvedBindPatterns: Bool {
hints.keys.contains(where: { $0.hasSuffix("BindPattern") })
}

/**
Expand All @@ -122,17 +129,19 @@ extension FetchSpecification { // Default Imp
* The syntax for bind-pattern hints is `%(binding)s` (note the trailing
* format specifier!).
*/
@inlinable
public func resolvingBindings(_ bindings: Any?) throws -> FetchSpecification {
var boundFS = self
let newHints = resolveHintBindPatterns(with: bindings)
let hasUnresolved = qualifier?.hasUnresolvedBindings ?? false
if newHints == nil && !hasUnresolved { return self }

boundFS.hints = resolveHintBindPatternsWith(bindings: bindings)

if let q = qualifier {
var boundFS = self
if let newHints { boundFS.hints = newHints }
if hasUnresolved, let q = boundFS.qualifier {
boundFS.qualifier =
try q.qualifierWith(bindings: bindings,
requiresAll: requiresAllQualifierBindingVariables)
}

return boundFS
}
}
Expand Down

0 comments on commit fe59d54

Please sign in to comment.