diff --git a/Sources/CoreStoreObject+Querying.swift b/Sources/CoreStoreObject+Querying.swift index f7b14f3..3157fc2 100644 --- a/Sources/CoreStoreObject+Querying.swift +++ b/Sources/CoreStoreObject+Querying.swift @@ -53,6 +53,39 @@ public extension DynamicObject where Self: CoreStoreObject { return attribute(self.meta).keyPath } + /** + Extracts the keyPath string from a `CoreStoreObject.Relationship` property. + ``` + let keyPath: String = Person.keyPath { $0.pets } + ``` + */ + public static func keyPath(_ relationship: (Self) -> RelationshipContainer.ToOne) -> String { + + return relationship(self.meta).keyPath + } + + /** + Extracts the keyPath string from a `CoreStoreObject.Relationship` property. + ``` + let keyPath: String = Person.keyPath { $0.pets } + ``` + */ + public static func keyPath(_ relationship: (Self) -> RelationshipContainer.ToManyOrdered) -> String { + + return relationship(self.meta).keyPath + } + + /** + Extracts the keyPath string from a `CoreStoreObject.Relationship` property. + ``` + let keyPath: String = Person.keyPath { $0.pets } + ``` + */ + public static func keyPath(_ relationship: (Self) -> RelationshipContainer.ToManyUnordered) -> String { + + return relationship(self.meta).keyPath + } + /** Creates a `Where` clause from a `CoreStoreObject.Value` property. ``` @@ -67,10 +100,10 @@ public extension DynamicObject where Self: CoreStoreObject { /** Creates an `OrderBy` clause from a `CoreStoreObject.Value` property. ``` - let person = CoreStore.fetchAll(From(), Person.ascending { $0.age }) + let person = CoreStore.fetchAll(From(), Person.orderBy(ascending: { $0.age })) ``` */ - public static func ascending(_ attribute: (Self) -> ValueContainer.Optional) -> OrderBy { + public static func orderBy(ascending attribute: (Self) -> ValueContainer.Required) -> OrderBy { return OrderBy(.ascending(attribute(self.meta).keyPath)) } @@ -78,9 +111,46 @@ public extension DynamicObject where Self: CoreStoreObject { /** Creates an `OrderBy` clause from a `CoreStoreObject.Value` property. ``` - let person = CoreStore.fetchAll(From(), Person.descending { $0.age }) + let person = CoreStore.fetchAll(From(), Person.orderBy(ascending: { $0.age })) ``` */ + public static func orderBy(ascending attribute: (Self) -> ValueContainer.Optional) -> OrderBy { + + return OrderBy(.ascending(attribute(self.meta).keyPath)) + } + + /** + Creates an `OrderBy` clause from a `CoreStoreObject.Value` property. + ``` + let person = CoreStore.fetchAll(From(), Person.orderBy(descending: { $0.age })) + ``` + */ + public static func orderBy(descending attribute: (Self) -> ValueContainer.Required) -> OrderBy { + + return OrderBy(.descending(attribute(self.meta).keyPath)) + } + + /** + Creates an `OrderBy` clause from a `CoreStoreObject.Value` property. + ``` + let person = CoreStore.fetchAll(From(), Person.orderBy(descending: { $0.age })) + ``` + */ + public static func orderBy(descending attribute: (Self) -> ValueContainer.Optional) -> OrderBy { + + return OrderBy(.descending(attribute(self.meta).keyPath)) + } + + + // MARK: Deprecated + + @available(*, deprecated, renamed: "orderBy(ascending:)") + public static func ascending(_ attribute: (Self) -> ValueContainer.Optional) -> OrderBy { + + return OrderBy(.ascending(attribute(self.meta).keyPath)) + } + + @available(*, deprecated, renamed: "orderBy(descending:)") public static func descending(_ attribute: (Self) -> ValueContainer.Optional) -> OrderBy { return OrderBy(.descending(attribute(self.meta).keyPath)) @@ -194,3 +264,57 @@ public extension ValueContainer.Optional { return !Where(attribute.keyPath, isEqualTo: value) } } + + +// MARK: - RelationshipContainer.ToOne + +public extension RelationshipContainer.ToOne { + + /** + Creates a `Where` clause from a `CoreStoreObject.Relationship` property. + ``` + let dog = CoreStore.fetchOne(From(), Dog.where { $0.master == me }) + ``` + */ + @inline(__always) + public static func == (_ relationship: RelationshipContainer.ToOne, _ object: D?) -> Where { + + return Where(relationship.keyPath, isEqualTo: object) + } + + /** + Creates a `Where` clause from a `CoreStoreObject.Relationship` property. + ``` + let dog = CoreStore.fetchOne(From(), Dog.where { $0.master != me }) + ``` + */ + @inline(__always) + public static func != (_ relationship: RelationshipContainer.ToOne, _ object: D?) -> Where { + + return !Where(relationship.keyPath, isEqualTo: object) + } + + /** + Creates a `Where` clause from a `CoreStoreObject.Relationship` property. + ``` + let dog = CoreStore.fetchOne(From(), Dog.where { $0.master ~= me }) + ``` + */ + @inline(__always) + public static func ~= (_ relationship: RelationshipContainer.ToOne, _ object: D?) -> Where { + + return Where(relationship.keyPath, isEqualTo: object) + } + + /** + Creates a `Where` clause from a `CoreStoreObject.Relationship` property. + ``` + let dog = CoreStore.fetchOne(From(), Dog.where { [john, joe, bob] ~= $0.master }) + ``` + */ + @inline(__always) + public static func ~= (_ sequence: S, _ relationship: RelationshipContainer.ToOne) -> Where where S.Iterator.Element == D { + + return Where(relationship.keyPath, isMemberOf: sequence) + } +}