WIP: typesafe queries

This commit is contained in:
John Estropia
2017-04-03 22:02:24 +09:00
parent b5d80fd272
commit 6948db516d
2 changed files with 28 additions and 26 deletions

View File

@@ -168,7 +168,7 @@ public extension ManagedObjectProtocol where Self: CoreStoreManagedObject {
public typealias Attribute = AttributeContainer<Self>
public static var meta: Self {
static var meta: Self {
return self.init(nil)
}
@@ -184,6 +184,12 @@ public extension ManagedObjectProtocol where Self: CoreStoreManagedObject {
return attribute(self.meta).keyPath
}
@inline(__always)
public static func `where`(_ condition: (Self) -> Where) -> Where {
return condition(self.meta)
}
}
@@ -211,43 +217,36 @@ public postfix func * <O: ManagedObjectProtocol, V: ImportableAttributeType>(_ a
public extension AttributeContainer.Required where V: CVarArg {
public static func == (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
public static func == (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> Where {
return NSPredicate(format: "%K == %@", argumentArray: [attribute.keyPath, value])
return Where(attribute.keyPath, isEqualTo: value)
}
public static func < (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
public static func < (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> Where {
return NSPredicate(format: "%K < %@", argumentArray: [attribute.keyPath, value])
return Where("%K < %@", attribute.keyPath, value)
}
public static func > (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
public static func > (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> Where {
return NSPredicate(format: "%K > %@", argumentArray: [attribute.keyPath, value])
return Where("%K > %@", attribute.keyPath, value)
}
public static func <= (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
public static func <= (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> Where {
return NSPredicate(format: "%K <= %@", argumentArray: [attribute.keyPath, value])
return Where("%K <= %@", attribute.keyPath, value)
}
public static func >= (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
public static func >= (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> Where {
return NSPredicate(format: "%K >= %@", argumentArray: [attribute.keyPath, value])
return Where("%K >= %@", attribute.keyPath, value)
}
public static func != (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
public static func != (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> Where {
return NSPredicate(format: "%K != %@", argumentArray: [attribute.keyPath, value])
return Where("%K != %@", attribute.keyPath, value)
}
}
public extension AttributeContainer.Optional where V: CVarArg {
public static func == (_ attribute: AttributeContainer<O>.Optional<V>, _ value: V?) -> NSPredicate {
public static func == (_ attribute: AttributeContainer<O>.Optional<V>, _ value: V?) -> Where {
if let value = value {
return NSPredicate(format: "%K == %@", argumentArray: [attribute.keyPath, value])
}
else {
return NSPredicate(format: "%K == nil", attribute.keyPath)
}
return Where(attribute.keyPath, isEqualTo: value)
}
}