diff --git a/Sources/KeyPath+Querying.swift b/Sources/KeyPath+Querying.swift index ceaf7ae..a70491e 100644 --- a/Sources/KeyPath+Querying.swift +++ b/Sources/KeyPath+Querying.swift @@ -439,6 +439,155 @@ public func ~= (_ sequence: S, _ keyPath: KeyPath.Stored + +/** + Creates a `Where` clause by comparing if a property is less than a value + ``` + let person = dataStack.fetchOne(From().where(\.$age < 20)) + ``` + */ +public func < (_ keyPath: KeyPath.Stored>, _ value: V) -> Where { + + return Where("%K < %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) +} + +/** + Creates a `Where` clause by comparing if a property is less than a value + ``` + let person = dataStack.fetchOne(From().where(\.$age < 20)) + ``` + */ +public func < (_ keyPath: KeyPath.Stored>, _ value: V) -> Where where V.Wrapped: Comparable { + + return Where("%K < %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) +} + +/** + Creates a `Where` clause by comparing if a property is greater than a value + ``` + let person = dataStack.fetchOne(From().where(\.$age > 20)) + ``` + */ +public func > (_ keyPath: KeyPath.Stored>, _ value: V) -> Where { + + return Where("%K > %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) +} + +/** + Creates a `Where` clause by comparing if a property is greater than a value + ``` + let person = dataStack.fetchOne(From().where(\.$age > 20)) + ``` + */ +public func > (_ keyPath: KeyPath.Stored>, _ value: V) -> Where where V.Wrapped: Comparable { + + return Where("%K > %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) +} + +/** + Creates a `Where` clause by comparing if a property is less than or equal to a value + ``` + let person = dataStack.fetchOne(From().where(\.$age <= 20)) + ``` + */ +public func <= (_ keyPath: KeyPath.Stored>, _ value: V) -> Where { + + return Where("%K <= %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) +} + +/** + Creates a `Where` clause by comparing if a property is less than or equal to a value + ``` + let person = dataStack.fetchOne(From().where(\.$age <= 20)) + ``` + */ +public func <= (_ keyPath: KeyPath.Stored>, _ value: V) -> Where where V.Wrapped: Comparable { + + return Where("%K <= %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) +} + +/** + Creates a `Where` clause by comparing if a property is greater than or equal to a value + ``` + let person = dataStack.fetchOne(From().where(\.$age >= 20)) + ``` + */ +public func >= (_ keyPath: KeyPath.Stored>, _ value: V) -> Where { + + return Where("%K >= %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) +} + +/** + Creates a `Where` clause by comparing if a property is greater than or equal to a value + ``` + let person = dataStack.fetchOne(From().where(\.$age >= 20)) + ``` + */ +public func >= (_ keyPath: KeyPath.Stored>, _ value: V) -> Where where V.Wrapped: Comparable { + + return Where("%K >= %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) +} + + +// MARK: - KeyPath where Root: CoreStoreObject, Value: FieldContainer.Relationship + +/** + Creates a `Where` clause by comparing if a property is equal to a value + ``` + let dog = dataStack.fetchOne(From().where(\.$master == john)) + ``` + */ +public func == (_ keyPath: KeyPath.Relationship>, _ object: D.DestinationObjectType?) -> Where { + + return Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object) +} + +/** + Creates a `Where` clause by comparing if a property is equal to a value + ``` + let dog = dataStack.fetchOne(From().where(\.master == john)) + ``` + */ +public func == (_ keyPath: KeyPath.Relationship>, _ object: R?) -> Where where D.DestinationObjectType == R.ObjectType { + + return Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object?.objectID()) +} + +/** + Creates a `Where` clause by comparing if a property is not equal to a value + ``` + let dog = dataStack.fetchOne(From().where(\.$master != john)) + ``` + */ +public func != (_ keyPath: KeyPath.Relationship>, _ object: D.DestinationObjectType?) -> Where { + + return !Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object) +} + +/** + Creates a `Where` clause by comparing if a property is not equal to a value + ``` + let dog = dataStack.fetchOne(From().where(\.master != john)) + ``` + */ +public func != (_ keyPath: KeyPath.Relationship>, _ object: R?) -> Where where D.DestinationObjectType == R.ObjectType { + + return !Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object?.objectID()) +} + +/** + Creates a `Where` clause by checking if a sequence contains a value of a property + ``` + let dog = dataStack.fetchOne(From().where([john, bob, joe] ~= \.$master)) + ``` + */ +public func ~= (_ sequence: S, _ keyPath: KeyPath.Relationship>) -> Where where S.Iterator.Element == D.DestinationObjectType { + + return Where(O.meta[keyPath: keyPath].keyPath, isMemberOf: sequence) +} + + // MARK: - KeyPath where Root: CoreStoreObject, Value: ValueContainer.Required /** @@ -513,17 +662,6 @@ public func ~= (_ sequence: S, _ keyPath: KeyPath.Required -/** - Creates a `Where` clause by comparing if a property is less than a value - ``` - let person = dataStack.fetchOne(From().where(\.$age < 20)) - ``` - */ -public func < (_ keyPath: KeyPath.Stored>, _ value: V) -> Where { - - return Where("%K < %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) -} - /** Creates a `Where` clause by comparing if a property is less than a value ``` @@ -535,17 +673,6 @@ public func < (_ keyPath: KeyPath.Require return Where("%K < %@", O.meta[keyPath: keyPath].keyPath, value.cs_toQueryableNativeType()) } -/** - Creates a `Where` clause by comparing if a property is greater than a value - ``` - let person = dataStack.fetchOne(From().where(\.$age > 20)) - ``` - */ -public func > (_ keyPath: KeyPath.Stored>, _ value: V) -> Where { - - return Where("%K > %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) -} - /** Creates a `Where` clause by comparing if a property is greater than a value ``` @@ -557,17 +684,6 @@ public func > (_ keyPath: KeyPath.Require return Where("%K > %@", O.meta[keyPath: keyPath].keyPath, value.cs_toQueryableNativeType()) } -/** - Creates a `Where` clause by comparing if a property is less than or equal to a value - ``` - let person = dataStack.fetchOne(From().where(\.$age <= 20)) - ``` - */ -public func <= (_ keyPath: KeyPath.Stored>, _ value: V) -> Where { - - return Where("%K <= %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) -} - /** Creates a `Where` clause by comparing if a property is less than or equal to a value ``` @@ -579,17 +695,6 @@ public func <= (_ keyPath: KeyPath.Requir return Where("%K <= %@", O.meta[keyPath: keyPath].keyPath, value.cs_toQueryableNativeType()) } -/** - Creates a `Where` clause by comparing if a property is greater than or equal to a value - ``` - let person = dataStack.fetchOne(From().where(\.$age >= 20)) - ``` - */ -public func >= (_ keyPath: KeyPath.Stored>, _ value: V) -> Where { - - return Where("%K >= %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) -} - /** Creates a `Where` clause by comparing if a property is greater than or equal to a value ``` @@ -604,17 +709,6 @@ public func >= (_ keyPath: KeyPath.Requir // MARK: - KeyPath where Root: CoreStoreObject, Value: ValueContainer.Optional -/** - Creates a `Where` clause by comparing if a property is less than a value - ``` - let person = dataStack.fetchOne(From().where(\.$age < 20)) - ``` - */ -public func < (_ keyPath: KeyPath.Stored>, _ value: V) -> Where where V.Wrapped: Comparable { - - return Where("%K < %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) -} - /** Creates a `Where` clause by comparing if a property is less than a value ``` @@ -633,17 +727,6 @@ public func < (_ keyPath: KeyPath.Optional>, _ val } } -/** - Creates a `Where` clause by comparing if a property is greater than a value - ``` - let person = dataStack.fetchOne(From().where(\.$age > 20)) - ``` - */ -public func > (_ keyPath: KeyPath.Stored>, _ value: V) -> Where where V.Wrapped: Comparable { - - return Where("%K > %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) -} - /** Creates a `Where` clause by comparing if a property is greater than a value ``` @@ -662,17 +745,6 @@ public func > (_ keyPath: KeyPath.Optional>, _ val } } -/** - Creates a `Where` clause by comparing if a property is less than or equal to a value - ``` - let person = dataStack.fetchOne(From().where(\.$age <= 20)) - ``` - */ -public func <= (_ keyPath: KeyPath.Stored>, _ value: V) -> Where where V.Wrapped: Comparable { - - return Where("%K <= %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) -} - /** Creates a `Where` clause by comparing if a property is less than or equal to a value ``` @@ -691,17 +763,6 @@ public func <= (_ keyPath: KeyPath.Optional>, _ va } } -/** - Creates a `Where` clause by comparing if a property is greater than or equal to a value - ``` - let person = dataStack.fetchOne(From().where(\.$age >= 20)) - ``` - */ -public func >= (_ keyPath: KeyPath.Stored>, _ value: V) -> Where where V.Wrapped: Comparable { - - return Where("%K >= %@", O.meta[keyPath: keyPath].keyPath, value.cs_toFieldStoredNativeType() as! V.FieldStoredNativeType) -} - /** Creates a `Where` clause by comparing if a property is greater than or equal to a value ``` @@ -723,28 +784,6 @@ public func >= (_ keyPath: KeyPath.Optional>, _ va // MARK: - KeyPath where Root: CoreStoreObject, Value: RelationshipContainer.ToOne -/** - Creates a `Where` clause by comparing if a property is equal to a value - ``` - let dog = dataStack.fetchOne(From().where(\.$master == john)) - ``` - */ -public func == (_ keyPath: KeyPath.Relationship>, _ object: D.DestinationObjectType?) -> Where { - - return Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object) -} - -/** - Creates a `Where` clause by comparing if a property is equal to a value - ``` - let dog = dataStack.fetchOne(From().where(\.master == john)) - ``` - */ -public func == (_ keyPath: KeyPath.Relationship>, _ object: R?) -> Where where D.DestinationObjectType == R.ObjectType { - - return Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object?.objectID()) -} - /** Creates a `Where` clause by comparing if a property is equal to a value ``` @@ -767,28 +806,6 @@ public func == (_ keyPath: KeyPath.ToOne>, return Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object) } -/** - Creates a `Where` clause by comparing if a property is not equal to a value - ``` - let dog = dataStack.fetchOne(From().where(\.$master != john)) - ``` - */ -public func != (_ keyPath: KeyPath.Relationship>, _ object: D.DestinationObjectType?) -> Where { - - return !Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object) -} - -/** - Creates a `Where` clause by comparing if a property is not equal to a value - ``` - let dog = dataStack.fetchOne(From().where(\.master != john)) - ``` - */ -public func != (_ keyPath: KeyPath.Relationship>, _ object: R?) -> Where where D.DestinationObjectType == R.ObjectType { - - return !Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object?.objectID()) -} - /** Creates a `Where` clause by comparing if a property is not equal to a value ``` @@ -811,17 +828,6 @@ public func != (_ keyPath: KeyPath.ToOne>, return !Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object) } -/** - Creates a `Where` clause by checking if a sequence contains a value of a property - ``` - let dog = dataStack.fetchOne(From().where([john, bob, joe] ~= \.$master)) - ``` - */ -public func ~= (_ sequence: S, _ keyPath: KeyPath.Relationship>) -> Where where S.Iterator.Element == D.DestinationObjectType { - - return Where(O.meta[keyPath: keyPath].keyPath, isMemberOf: sequence) -} - /** Creates a `Where` clause by checking if a sequence contains a value of a property ``` diff --git a/Sources/Where.swift b/Sources/Where.swift index f9de0c8..40d88d2 100644 --- a/Sources/Where.swift +++ b/Sources/Where.swift @@ -469,7 +469,7 @@ extension Where where O: CoreStoreObject { - parameter keyPath: the keyPath to compare with - parameter null: the arguments for the `==` operator */ - public init(_ keyPath: KeyPath.Optional>, isEqualTo null: Void?) { + public init(_ keyPath: KeyPath.Stored>, isEqualTo null: Void?) { self.init(O.meta[keyPath: keyPath].keyPath, isEqualTo: null) } @@ -480,11 +480,44 @@ extension Where where O: CoreStoreObject { - parameter keyPath: the keyPath to compare with - parameter null: the arguments for the `==` operator */ - public init(_ keyPath: KeyPath.ToOne>, isEqualTo null: Void?) { + public init(_ keyPath: KeyPath.Relationship>, isEqualTo null: Void?) { self.init(O.meta[keyPath: keyPath].keyPath, isEqualTo: null) } + /** + Initializes a `Where` clause that compares membership + + - parameter keyPath: the keyPath to compare with + - parameter list: the sequence to check membership of + */ + public init(_ keyPath: KeyPath.Stored>, isMemberOf list: S) where S.Iterator.Element == V { + + self.init(O.meta[keyPath: keyPath].keyPath, isMemberOf: list) + } + + /** + Initializes a `Where` clause that compares membership + + - parameter keyPath: the keyPath to compare with + - parameter list: the sequence to check membership of + */ + public init(_ keyPath: KeyPath.Relationship>, isMemberOf list: S) where S.Iterator.Element == V.DestinationObjectType { + + self.init(O.meta[keyPath: keyPath].keyPath, isMemberOf: list) + } + + /** + Initializes a `Where` clause that compares membership + + - parameter keyPath: the keyPath to compare with + - parameter list: the sequence to check membership of + */ + public init(_ keyPath: KeyPath.Relationship>, isMemberOf list: S) where S.Iterator.Element: NSManagedObjectID { + + self.init(O.meta[keyPath: keyPath].keyPath, isMemberOf: list) + } + /** Initializes a `Where` clause that compares equality @@ -507,6 +540,28 @@ extension Where where O: CoreStoreObject { self.init(O.meta[keyPath: keyPath].keyPath, isEqualTo: value) } + /** + Initializes a `Where` clause that compares equality to `nil` + + - parameter keyPath: the keyPath to compare with + - parameter null: the arguments for the `==` operator + */ + public init(_ keyPath: KeyPath.Optional>, isEqualTo null: Void?) { + + self.init(O.meta[keyPath: keyPath].keyPath, isEqualTo: null) + } + + /** + Initializes a `Where` clause that compares equality to `nil` + + - parameter keyPath: the keyPath to compare with + - parameter null: the arguments for the `==` operator + */ + public init(_ keyPath: KeyPath.ToOne>, isEqualTo null: Void?) { + + self.init(O.meta[keyPath: keyPath].keyPath, isEqualTo: null) + } + /** Initializes a `Where` clause that compares equality