// // KeyPath+Querying.swift // CoreStore // // Copyright © 2017 John Rommel Estropia // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. // import CoreData import Foundation // MARK: - KeyPath where Root: NSManagedObject, Value: QueryableAttributeType & Equatable public func == (_ keyPath: KeyPath, _ value: V) -> Where { return Where(keyPath._kvcKeyPathString!, isEqualTo: value) } public func != (_ keyPath: KeyPath, _ value: V) -> Where { return !Where(keyPath._kvcKeyPathString!, isEqualTo: value) } public func ~= (_ sequence: S, _ keyPath: KeyPath) -> Where where S.Iterator.Element == V { return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence) } // MARK: - KeyPath where Root: NSManagedObject, Value: Optional public func == (_ keyPath: KeyPath>, _ value: V?) -> Where { return Where(keyPath._kvcKeyPathString!, isEqualTo: value) } public func != (_ keyPath: KeyPath>, _ value: V?) -> Where { return !Where(keyPath._kvcKeyPathString!, isEqualTo: value) } public func ~= (_ sequence: S, _ keyPath: KeyPath>) -> Where where S.Iterator.Element == V { return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence) } // MARK: - KeyPath where Root: NSManagedObject, Value: QueryableAttributeType & Comparable public func < (_ keyPath: KeyPath, _ value: V) -> Where { return Where("%K < %@", keyPath._kvcKeyPathString!, value) } public func > (_ keyPath: KeyPath, _ value: V) -> Where { return Where("%K > %@", keyPath._kvcKeyPathString!, value) } public func <= (_ keyPath: KeyPath, _ value: V) -> Where { return Where("%K <= %@", keyPath._kvcKeyPathString!, value) } public func >= (_ keyPath: KeyPath, _ value: V) -> Where { return Where("%K >= %@", keyPath._kvcKeyPathString!, value) } // MARK: - KeyPath where Root: NSManagedObject, Value: Optional public func < (_ keyPath: KeyPath>, _ value: V?) -> Where { if let value = value { return Where("%K < %@", keyPath._kvcKeyPathString!, value) } else { return Where("%K < nil", keyPath._kvcKeyPathString!) } } public func > (_ keyPath: KeyPath>, _ value: V?) -> Where { if let value = value { return Where("%K > %@", keyPath._kvcKeyPathString!, value) } else { return Where("%K > nil", keyPath._kvcKeyPathString!) } } public func <= (_ keyPath: KeyPath>, _ value: V?) -> Where { if let value = value { return Where("%K <= %@", keyPath._kvcKeyPathString!, value) } else { return Where("%K <= nil", keyPath._kvcKeyPathString!) } } public func >= (_ keyPath: KeyPath>, _ value: V?) -> Where { if let value = value { return Where("%K >= %@", keyPath._kvcKeyPathString!, value) } else { return Where("%K >= nil", keyPath._kvcKeyPathString!) } } // MARK: - KeyPath where Root: NSManagedObject, Value: NSManagedObject public func == (_ keyPath: KeyPath, _ object: D) -> Where { return Where(keyPath._kvcKeyPathString!, isEqualTo: object) } public func != (_ keyPath: KeyPath, _ object: D) -> Where { return !Where(keyPath._kvcKeyPathString!, isEqualTo: object) } public func ~= (_ sequence: S, _ keyPath: KeyPath) -> Where where S.Iterator.Element == D { return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence) } public func == (_ keyPath: KeyPath, _ objectID: NSManagedObjectID?) -> Where { return Where(keyPath._kvcKeyPathString!, isEqualTo: objectID) } public func != (_ keyPath: KeyPath, _ objectID: NSManagedObjectID?) -> Where { return !Where(keyPath._kvcKeyPathString!, isEqualTo: objectID) } public func ~= (_ sequence: S, _ keyPath: KeyPath) -> Where where S.Iterator.Element == NSManagedObjectID { return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence) } // MARK: - KeyPath where Root: NSManagedObject, Value: Optional public func == (_ keyPath: KeyPath>, _ object: D?) -> Where { return Where(keyPath._kvcKeyPathString!, isEqualTo: object) } public func != (_ keyPath: KeyPath>, _ object: D?) -> Where { return !Where(keyPath._kvcKeyPathString!, isEqualTo: object) } public func ~= (_ sequence: S, _ keyPath: KeyPath>) -> Where where S.Iterator.Element == D { return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence) } public func == (_ keyPath: KeyPath>, _ objectID: NSManagedObjectID?) -> Where { return Where(keyPath._kvcKeyPathString!, isEqualTo: objectID) } public func != (_ keyPath: KeyPath>, _ objectID: NSManagedObjectID?) -> Where { return !Where(keyPath._kvcKeyPathString!, isEqualTo: objectID) } public func ~= (_ sequence: S, _ keyPath: KeyPath>) -> Where where S.Iterator.Element == NSManagedObjectID { return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence) } // MARK: - KeyPath where Root: CoreStoreObject, Value: ValueContainer.Required public func == (_ keyPath: KeyPath.Required>, _ value: V) -> Where { return Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: value) } public func != (_ keyPath: KeyPath.Required>, _ value: V) -> Where { return !Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: value) } public func ~= (_ sequence: S, _ keyPath: KeyPath.Required>) -> Where where S.Iterator.Element == V { return Where(O.meta[keyPath: keyPath].keyPath, isMemberOf: sequence) } // MARK: - KeyPath where Root: CoreStoreObject, Value: ValueContainer.Optional public func == (_ keyPath: KeyPath.Optional>, _ value: V?) -> Where { return Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: value) } public func != (_ keyPath: KeyPath.Optional>, _ value: V?) -> Where { return !Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: value) } public func ~= (_ sequence: S, _ keyPath: KeyPath.Optional>) -> Where where S.Iterator.Element == V { return Where(O.meta[keyPath: keyPath].keyPath, isMemberOf: sequence) } // MARK: - KeyPath where Root: CoreStoreObject, Value: ValueContainer.Required public func < (_ keyPath: KeyPath.Required>, _ value: V) -> Where { return Where("%K < %@", O.meta[keyPath: keyPath].keyPath, value) } public func > (_ keyPath: KeyPath.Required>, _ value: V) -> Where { return Where("%K > %@", O.meta[keyPath: keyPath].keyPath, value) } public func <= (_ keyPath: KeyPath.Required>, _ value: V) -> Where { return Where("%K <= %@", O.meta[keyPath: keyPath].keyPath, value) } public func >= (_ keyPath: KeyPath.Required>, _ value: V) -> Where { return Where("%K >= %@", O.meta[keyPath: keyPath].keyPath, value) } // MARK: - KeyPath where Root: CoreStoreObject, Value: ValueContainer.Optional public func < (_ keyPath: KeyPath.Optional>, _ value: V?) -> Where { if let value = value { return Where("%K < %@", O.meta[keyPath: keyPath].keyPath, value) } else { return Where("%K < nil", O.meta[keyPath: keyPath].keyPath) } } public func > (_ keyPath: KeyPath.Optional>, _ value: V?) -> Where { if let value = value { return Where("%K > %@", O.meta[keyPath: keyPath].keyPath, value) } else { return Where("%K > nil", O.meta[keyPath: keyPath].keyPath) } } public func <= (_ keyPath: KeyPath.Optional>, _ value: V?) -> Where { if let value = value { return Where("%K <= %@", O.meta[keyPath: keyPath].keyPath, value) } else { return Where("%K <= nil", O.meta[keyPath: keyPath].keyPath) } } public func >= (_ keyPath: KeyPath.Optional>, _ value: V?) -> Where { if let value = value { return Where("%K >= %@", O.meta[keyPath: keyPath].keyPath, value) } else { return Where("%K >= nil", O.meta[keyPath: keyPath].keyPath) } } // MARK: - KeyPath where Root: CoreStoreObject, Value: RelationshipContainer.ToOne public func == (_ keyPath: KeyPath.ToOne>, _ object: D) -> Where { return Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object) } public func == (_ keyPath: KeyPath.ToOne>, _ object: D?) -> Where { return Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object) } public func != (_ keyPath: KeyPath.ToOne>, _ object: D) -> Where { return !Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object) } public func != (_ keyPath: KeyPath.ToOne>, _ object: D?) -> Where { return !Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object) } public func ~= (_ sequence: S, _ keyPath: KeyPath.ToOne>) -> Where where S.Iterator.Element == D { return Where(O.meta[keyPath: keyPath].keyPath, isMemberOf: sequence) }