This commit is contained in:
John Rommel Estropia
2016-07-21 02:45:42 +09:00
parent 267c21063a
commit a638620858
85 changed files with 1621 additions and 1819 deletions
@@ -32,7 +32,7 @@ import CoreData
/**
A `From` clause specifies the source entity and source persistent store for fetch and query methods. A common usage is to just indicate the entity:
```
let person = transaction.fetchOne(From(MyPersonEntity))
let person = transaction.fetchOne(From<MyPersonEntity>())
```
For cases where multiple `NSPersistentStore`s contain the same entity, the source configuration's name needs to be specified as well:
```
@@ -88,7 +88,7 @@ public struct From<T: NSManagedObject> {
CoreStore.assert(
entityClass is T.Type,
"Attempted to create generic type \(cs_typeName(From<T>)) with entity class \(cs_typeName(entityClass))"
"Attempted to create generic type \(cs_typeName(From<T>.self)) with entity class \(cs_typeName(entityClass))"
)
self.init(entityClass: entityClass, configurations: nil)
}
@@ -163,7 +163,7 @@ public struct From<T: NSManagedObject> {
CoreStore.assert(
entityClass is T.Type,
"Attempted to create generic type \(cs_typeName(From<T>)) with entity class \(cs_typeName(entityClass))"
"Attempted to create generic type \(cs_typeName(From<T>.self)) with entity class \(cs_typeName(entityClass))"
)
self.init(entityClass: entityClass, configurations: [configuration] + otherConfigurations)
}
@@ -181,7 +181,7 @@ public struct From<T: NSManagedObject> {
CoreStore.assert(
entityClass is T.Type,
"Attempted to create generic type \(cs_typeName(From<T>)) with entity class \(cs_typeName(entityClass))"
"Attempted to create generic type \(cs_typeName(From<T>.self)) with entity class \(cs_typeName(entityClass))"
)
self.init(entityClass: entityClass, configurations: configurations)
}
@@ -189,7 +189,6 @@ public struct From<T: NSManagedObject> {
// MARK: Internal
@warn_unused_result
internal func applyToFetchRequest<ResultType: NSFetchRequestResult>(_ fetchRequest: NSFetchRequest<ResultType>, context: NSManagedObjectContext, applyAffectedStores: Bool = true) -> Bool {
fetchRequest.entity = context.entityDescriptionForEntityClass(self.entityClass)
@@ -96,7 +96,6 @@ public struct GroupBy: QueryClause, Hashable {
// MARK: - GroupBy: Equatable
@warn_unused_result
public func == (lhs: GroupBy, rhs: GroupBy) -> Bool {
return lhs.keyPaths == rhs.keyPaths
@@ -27,12 +27,12 @@ import Foundation
import CoreData
public func +(left: OrderBy, right: OrderBy) -> OrderBy {
public func + (left: OrderBy, right: OrderBy) -> OrderBy {
return OrderBy(left.sortDescriptors + right.sortDescriptors)
}
public func +=(left: inout OrderBy, right: OrderBy) {
public func += (left: inout OrderBy, right: OrderBy) {
left = left + right
}
@@ -163,7 +163,6 @@ public struct OrderBy: FetchClause, QueryClause, DeleteClause, Hashable {
// MARK: - OrderBy: Equatable
@warn_unused_result
public func == (lhs: OrderBy, rhs: OrderBy) -> Bool {
return lhs.sortDescriptors == rhs.sortDescriptors
@@ -70,15 +70,15 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
Provides a `SelectTerm` to a `Select` clause for querying an entity attribute. A shorter way to do the same is to assign from the string keypath directly:
```
let fullName = CoreStore.queryValue(
From(MyPersonEntity),
Select<String>(.Attribute("fullName")),
From<MyPersonEntity>(),
Select<String>(.attribute("fullName")),
Where("employeeID", isEqualTo: 1111)
)
```
is equivalent to:
```
let fullName = CoreStore.queryValue(
From(MyPersonEntity),
From<MyPersonEntity>(),
Select<String>("fullName"),
Where("employeeID", isEqualTo: 1111)
)
@@ -96,8 +96,8 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
Provides a `SelectTerm` to a `Select` clause for querying the average value of an attribute.
```
let averageAge = CoreStore.queryValue(
From(MyPersonEntity),
Select<Int>(.Average("age"))
From<MyPersonEntity>(),
Select<Int>(.average("age"))
)
```
@@ -119,8 +119,8 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
Provides a `SelectTerm` to a `Select` clause for a count query.
```
let numberOfEmployees = CoreStore.queryValue(
From(MyPersonEntity),
Select<Int>(.Count("employeeID"))
From<MyPersonEntity>(),
Select<Int>(.count("employeeID"))
)
```
@@ -142,8 +142,8 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
Provides a `SelectTerm` to a `Select` clause for querying the maximum value for an attribute.
```
let maximumAge = CoreStore.queryValue(
From(MyPersonEntity),
Select<Int>(.Maximum("age"))
From<MyPersonEntity>(),
Select<Int>(.maximum("age"))
)
```
@@ -165,8 +165,8 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
Provides a `SelectTerm` to a `Select` clause for querying the minimum value for an attribute.
```
let minimumAge = CoreStore.queryValue(
From(MyPersonEntity),
Select<Int>(.Minimum("age"))
From<MyPersonEntity>(),
Select<Int>(.minimum("age"))
)
```
@@ -188,8 +188,8 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
Provides a `SelectTerm` to a `Select` clause for querying the sum value for an attribute.
```
let totalAge = CoreStore.queryValue(
From(MyPersonEntity),
Select<Int>(.Sum("age"))
From<MyPersonEntity>(),
Select<Int>(.sum("age"))
)
```
@@ -211,7 +211,7 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
Provides a `SelectTerm` to a `Select` clause for querying the `NSManagedObjectID`.
```
let objectID = CoreStore.queryValue(
From(MyPersonEntity),
From<MyPersonEntity>(),
Select<NSManagedObjectID>(),
Where("employeeID", isEqualTo: 1111)
)
@@ -276,7 +276,6 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
// MARK: - SelectTerm: Equatable
@warn_unused_result
public func == (lhs: SelectTerm, rhs: SelectTerm) -> Bool {
switch (lhs, rhs) {
@@ -308,15 +307,15 @@ public func == (lhs: SelectTerm, rhs: SelectTerm) -> Bool {
You can bind the return type by specializing the initializer:
```
let maximumAge = CoreStore.queryValue(
From(MyPersonEntity),
Select<Int>(.Maximum("age"))
From<MyPersonEntity>(),
Select<Int>(.maximum("age"))
)
```
or by casting the type of the return value:
```
let maximumAge: Int = CoreStore.queryValue(
From(MyPersonEntity),
Select(.Maximum("age"))
From<MyPersonEntity>(),
Select(.maximum("age"))
)
```
Valid return types depend on the query:
@@ -395,7 +394,6 @@ public extension Select where T: NSManagedObjectID {
// MARK: - Select: Equatable
@warn_unused_result
public func == <T: SelectResultType, U: SelectResultType>(lhs: Select<T>, rhs: Select<U>) -> Bool {
return lhs.selectTerms == rhs.selectTerms
@@ -573,6 +571,38 @@ extension String: SelectValueResultType {
}
// MARK: - Date: SelectValueResultType
extension Date: SelectValueResultType {
public static var attributeType: NSAttributeType {
return .dateAttributeType
}
public static func fromResultObject(_ result: AnyObject) -> Date? {
return result as? Date
}
}
// MARK: - Data: SelectValueResultType
extension Data: SelectValueResultType {
public static var attributeType: NSAttributeType {
return .binaryDataAttributeType
}
public static func fromResultObject(_ result: AnyObject) -> Data? {
return result as? Data
}
}
// MARK: - NSNumber: SelectValueResultType
extension NSNumber: SelectValueResultType {
@@ -635,32 +665,40 @@ extension NSDecimalNumber {
// MARK: - NSDate: SelectValueResultType
extension Date: SelectValueResultType {
extension NSDate: SelectValueResultType {
public static var attributeType: NSAttributeType {
return .dateAttributeType
}
public static func fromResultObject(_ result: AnyObject) -> Date? {
public class func fromResultObject(_ result: AnyObject) -> Self? {
return result as? Date
func forceCast<T: NSDate>(_ object: AnyObject) -> T? {
return (object as? T)
}
return forceCast(result)
}
}
// MARK: - NSData: SelectValueResultType
extension Data: SelectValueResultType {
extension NSData: SelectValueResultType {
public static var attributeType: NSAttributeType {
return .binaryDataAttributeType
}
public static func fromResultObject(_ result: AnyObject) -> Data? {
public class func fromResultObject(_ result: AnyObject) -> Self? {
return result as? Data
func forceCast<T: NSData>(_ object: AnyObject) -> T? {
return (object as? T)
}
return forceCast(result)
}
}
@@ -34,7 +34,7 @@ import CoreData
Sample usage:
```
let employees = transaction.fetchAll(
From(MyPersonEntity),
From<MyPersonEntity>(),
Tweak { (fetchRequest) -> Void in
fetchRequest.includesPendingChanges = false
fetchRequest.fetchLimit = 5
@@ -27,17 +27,17 @@ import Foundation
import CoreData
public func &&(left: Where, right: Where) -> Where {
public func && (left: Where, right: Where) -> Where {
return Where(CompoundPredicate(type: .and, subpredicates: [left.predicate, right.predicate]))
}
public func ||(left: Where, right: Where) -> Where {
public func || (left: Where, right: Where) -> Where {
return Where(CompoundPredicate(type: .or, subpredicates: [left.predicate, right.predicate]))
}
public prefix func !(clause: Where) -> Where {
public prefix func ! (clause: Where) -> Where {
return Where(CompoundPredicate(type: .not, subpredicates: [clause.predicate]))
}
@@ -168,7 +168,6 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
// MARK: - Where: Equatable
@warn_unused_result
public func == (lhs: Where, rhs: Where) -> Bool {
return lhs.predicate == rhs.predicate