Skip to content

Commit

Permalink
Fix double-optional wrap in static query builder
Browse files Browse the repository at this point in the history
If the code attribute is optional, we need an own overload, otherwise
the Optional value is wrapped in the optional `Any?`.
  • Loading branch information
helje5 committed Dec 5, 2024
1 parent 8b3857a commit af3f209
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
23 changes: 22 additions & 1 deletion Sources/ZeeQL/Control/EntityType+Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,32 @@ public extension TypedEntityType where FullEntity: CodeEntity<Self> {
_ value: V
) -> TypedFetchSpecification<Self>
where V: AttributeValue
{
return `where`(key, .EqualTo, value)
}

@inlinable
static func `where`<V>(
_ key: Swift.KeyPath<FullEntity, CodeAttribute<V?>>,
_ operation: ComparisonOperation,
_ value: V?
) -> TypedFetchSpecification<Self>
where V: AttributeValue
{
// if we need no attributes
var fs = TypedFetchSpecification<Self>(entity: Self.entity)
let attribute = Self.e[keyPath: key]
fs.qualifier = KeyValueQualifier(AttributeKey(attribute), .EqualTo, value)
fs.qualifier = KeyValueQualifier(AttributeKey(attribute), operation, value)
return fs
}

@inlinable
static func `where`<V>(
_ key: Swift.KeyPath<FullEntity, CodeAttribute<V?>>,
_ value: V?
) -> TypedFetchSpecification<Self>
where V: AttributeValue
{
return `where`(key, .EqualTo, value)
}
}
28 changes: 27 additions & 1 deletion Sources/ZeeQL/Control/FetchSpecification+Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ public extension DatabaseFetchSpecification

// TODO: select w/ pack iteration

// MARK: - Qualifier

@inlinable
func `where`<V>(_ key: Swift.KeyPath<Object.FullEntity, CodeAttribute<V>>,
_ operation: ComparisonOperation,
Expand All @@ -209,9 +211,30 @@ public extension DatabaseFetchSpecification
_ value: V) -> Self
where V: AttributeValue
{
`where`(key, .EqualTo, value)
return `where`(key, .EqualTo, value)
}

@inlinable
func `where`<V>(_ key: Swift.KeyPath<Object.FullEntity, CodeAttribute<V?>>,
_ operation: ComparisonOperation,
_ value: V?) -> Self
where V: AttributeValue
{
let attribute = Object.e[keyPath: key]
return `where`(KeyValueQualifier(AttributeKey(attribute), operation, value))
}

@inlinable
func `where`<V>(_ key: Swift.KeyPath<Object.FullEntity, CodeAttribute<V?>>,
_ value: V?) -> Self
where V: AttributeValue
{
return `where`(key, .EqualTo, value)
}


// MARK: - Ordering

#if compiler(>=6)
func order<each V: AttributeValue>(
by key: repeat Swift.KeyPath<Object.FullEntity, CodeAttribute<each V>>,
Expand Down Expand Up @@ -239,6 +262,9 @@ public extension DatabaseFetchSpecification
}
#endif // !compiler(>=6)


// MARK: - Prefetch

#if compiler(>=6)
// TODO: can we do both toOne and toMany in one?
// a KeyPath that has the parent class (CodeRelationship) doesn't work?
Expand Down

0 comments on commit af3f209

Please sign in to comment.