Skip to content

Commit

Permalink
Rework builders to work w/ any TypedProperty
Browse files Browse the repository at this point in the history
Not just `CodeAttribute` and specific `prefetch` relationships.
This also allows usage of toOne and toMany in a single prefetch.
  • Loading branch information
helje5 committed Dec 8, 2024
1 parent d57a158 commit 5d71e3a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 77 deletions.
45 changes: 21 additions & 24 deletions Sources/ZeeQL/Control/EntityType+Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,53 +113,50 @@ public extension TypedEntityObject {
public extension TypedEntityType where FullEntity: CodeEntity<Self> {

// TODO: select w/ pack iteration

// Maybe the `where` should work on typed keys!

@inlinable
static func `where`<V>(
_ key: Swift.KeyPath<FullEntity, CodeAttribute<V>>,
_ operation: ComparisonOperation,
_ value: V
static func `where`<A: TypedProperty>(
_ key : Swift.KeyPath<FullEntity, A>,
_ operation : ComparisonOperation,
_ value : A.T
) -> 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), operation, value)
let property = Self.e[keyPath: key]
fs.qualifier = KeyValueQualifier(StringKey(property.name), operation, value)
return fs
}

@inlinable
static func `where`<V>(
_ key: Swift.KeyPath<FullEntity, CodeAttribute<V>>,
_ value: V
) -> TypedFetchSpecification<Self>
where V: AttributeValue
static func `where`<A>(_ key: Swift.KeyPath<FullEntity, A>,
_ value: A.T) -> TypedFetchSpecification<Self>
where A: TypedProperty
{
return `where`(key, .EqualTo, value)
}

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

@inlinable
static func `where`<V>(
_ key: Swift.KeyPath<FullEntity, CodeAttribute<V?>>,
_ value: V?
) -> TypedFetchSpecification<Self>
where V: AttributeValue
static func `where`<A>(_ key: Swift.KeyPath<FullEntity, A>,
_ value: A.T) -> TypedFetchSpecification<Self>
where A: TypedProperty, A.T: AnyOptional
{
return `where`(key, .EqualTo, value)
}
Expand Down
79 changes: 26 additions & 53 deletions Sources/ZeeQL/Control/FetchSpecification+Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,39 +195,39 @@ public extension DatabaseFetchSpecification
// TODO: select w/ pack iteration

// MARK: - Qualifier


// Maybe the `where` should work on typed keys!

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

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

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

@inlinable
func `where`<V>(_ key: Swift.KeyPath<Object.FullEntity, CodeAttribute<V?>>,
_ value: V?) -> Self
where V: AttributeValue
func `where`<A: TypedProperty>(_ key: Swift.KeyPath<Object.FullEntity, A>,
_ value: A.T) -> Self
where A: TypedProperty, A.T: AnyOptional
{
return `where`(key, .EqualTo, value)
}
Expand All @@ -236,8 +236,9 @@ public extension DatabaseFetchSpecification
// MARK: - Ordering

#if compiler(>=6)
func order<each V: AttributeValue>(
by key: repeat Swift.KeyPath<Object.FullEntity, CodeAttribute<each V>>,
@inlinable
func order<each A: Attribute>(
by key: repeat Swift.KeyPath<Object.FullEntity, each A>,
using selector: SortOrdering.Selector = .CompareAscending
) -> Self
{
Expand Down Expand Up @@ -266,39 +267,10 @@ public extension DatabaseFetchSpecification
// 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?
func prefetch<each O: DatabaseObject>(
_ relationship:
repeat Swift.KeyPath<Object.FullEntity, ToOneRelationship<each O>>,
clear: Bool = false
) -> Self
{
transform {
if clear { $0.prefetchingRelationshipKeyPathes = [] }
for relationship in repeat each relationship {
let relationship = Object.e[keyPath: relationship]
$0.prefetchingRelationshipKeyPathes.append(relationship.name)
}
}
}
func prefetch<each O: DatabaseObject>(
_ relationship:
repeat Swift.KeyPath<Object.FullEntity, ToManyRelationship<each O>>,
clear: Bool = false
) -> Self
{
transform {
if clear { $0.prefetchingRelationshipKeyPathes = [] }
for relationship in repeat each relationship {
let relationship = Object.e[keyPath: relationship]
$0.prefetchingRelationshipKeyPathes.append(relationship.name)
}
}
}
func prefetch<each O: DatabaseObject>(
@inlinable
func prefetch<each O: CodeRelationshipType>(
_ relationship:
repeat Swift.KeyPath<Object.FullEntity, CodeRelationship<each O>>,
repeat Swift.KeyPath<Object.FullEntity, each O>,
clear: Bool = false
) -> Self
{
Expand All @@ -310,5 +282,6 @@ public extension DatabaseFetchSpecification
}
}
}

#endif // compiler(>=6)
}

0 comments on commit 5d71e3a

Please sign in to comment.