mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-17 23:14:05 +01:00
support value querying in Objective C
This commit is contained in:
@@ -170,37 +170,44 @@ public extension CSBaseDataTransaction {
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
Queries aggregate values as specified by the `CSQueryClause`s. Requires at least a `CSSelect` clause, and optional `CSWhere`, `CSOrderBy`, `CSGroupBy`, and `CSTweak` clauses.
|
||||
|
||||
A "query" differs from a "fetch" in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `CSFetchClause`s
|
||||
- parameter selectClause: a `CSSelect` clause indicating the properties to fetch, and with the generic type indicating the return type.
|
||||
- parameter queryClauses: a series of `CSQueryClause` instances for the query request. Accepts `CSWhere`, `CSOrderBy`, `CSGroupBy`, and `CSTweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `CSSelect` parameter.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchObjectIDsFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> [NSManagedObjectID]? {
|
||||
public func queryValueFrom(from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> AnyObject? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.bridgeToSwift.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
"Attempted to query from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
return self.bridgeToSwift.context.fetchObjectIDs(from, fetchClauses)
|
||||
return self.bridgeToSwift.context.queryValue(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Deletes all `NSManagedObject`s that satisfy the specified `DeleteClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
Queries a dictionary of attribute values as specified by the `CSQueryClause`s. Requires at least a `CSSelect` clause, and optional `CSWhere`, `CSOrderBy`, `CSGroupBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter deleteClauses: a series of `DeleteClause` instances for the delete request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number of `NSManagedObject`s deleted
|
||||
A "query" differs from a "fetch" in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter selectClause: a `CSSelect` clause indicating the properties to fetch, and with the generic type indicating the return type.
|
||||
- parameter queryClauses: a series of `CSQueryClause` instances for the query request. Accepts `CSWhere`, `CSOrderBy`, `CSGroupBy`, and `CSTweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `CSSelect` parameter.
|
||||
*/
|
||||
@objc
|
||||
public func deleteAllFrom(from: CSFrom, deleteClauses: [CSDeleteClause]) -> NSNumber? {
|
||||
@warn_unused_result
|
||||
public func queryAttributesFrom(from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> [[NSString: AnyObject]]? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.bridgeToSwift.isRunningInAllowedQueue(),
|
||||
"Attempted to delete from a \(typeName(self)) outside its designated queue."
|
||||
"Attempted to query from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
return self.bridgeToSwift.context.deleteAll(from, deleteClauses)
|
||||
return self.bridgeToSwift.context.queryAttributes(from, selectClause, queryClauses)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,15 +154,36 @@ public extension CSCoreStore {
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, deletes all `NSManagedObject`s that satisfy the specified `DeleteClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
Using the `defaultStack`, queries aggregate values as specified by the `CSQueryClause`s. Requires at least a `CSSelect` clause, and optional `CSWhere`, `CSOrderBy`, `CSGroupBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter deleteClauses: a series of `DeleteClause` instances for the delete request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number of `NSManagedObject`s deleted
|
||||
A "query" differs from a "fetch" in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter selectClause: a `CSSelect` clause indicating the properties to fetch, and with the generic type indicating the return type.
|
||||
- parameter queryClauses: a series of `CSQueryClause` instances for the query request. Accepts `CSWhere`, `CSOrderBy`, `CSGroupBy`, and `CSTweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `CSSelect` parameter.
|
||||
*/
|
||||
@objc
|
||||
public static func deleteAllFrom(from: CSFrom, deleteClauses: [CSDeleteClause]) -> NSNumber? {
|
||||
@warn_unused_result
|
||||
public static func queryValueFrom(from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> AnyObject? {
|
||||
|
||||
return self.defaultStack.deleteAllFrom(from, deleteClauses: deleteClauses)
|
||||
return self.defaultStack.queryValueFrom(from, selectClause: selectClause, queryClauses: queryClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, queries a dictionary of attribute values as specified by the `CSQueryClause`s. Requires at least a `CSSelect` clause, and optional `CSWhere`, `CSOrderBy`, `CSGroupBy`, and `CSTweak` clauses.
|
||||
|
||||
A "query" differs from a "fetch" in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter selectClause: a `CSSelect` clause indicating the properties to fetch, and with the generic type indicating the return type.
|
||||
- parameter queryClauses: a series of `CSQueryClause` instances for the query request. Accepts `CSWhere`, `CSOrderBy`, `CSGroupBy`, and `CSTweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `CSSelect` parameter.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func queryAttributesFrom(from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> [[NSString: AnyObject]]? {
|
||||
|
||||
return self.defaultStack.queryAttributesFrom(from, selectClause: selectClause, queryClauses: queryClauses)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,19 +188,44 @@ public extension CSDataStack {
|
||||
}
|
||||
|
||||
/**
|
||||
Deletes all `NSManagedObject`s that satisfy the specified `DeleteClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
Queries aggregate values as specified by the `CSQueryClause`s. Requires at least a `CSSelect` clause, and optional `CSWhere`, `CSOrderBy`, `CSGroupBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter deleteClauses: a series of `DeleteClause` instances for the delete request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number of `NSManagedObject`s deleted
|
||||
A "query" differs from a "fetch" in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter selectClause: a `CSSelect` clause indicating the properties to fetch, and with the generic type indicating the return type.
|
||||
- parameter queryClauses: a series of `CSQueryClause` instances for the query request. Accepts `CSWhere`, `CSOrderBy`, `CSGroupBy`, and `CSTweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `CSSelect` parameter.
|
||||
*/
|
||||
@objc
|
||||
public func deleteAllFrom(from: CSFrom, deleteClauses: [CSDeleteClause]) -> NSNumber? {
|
||||
@warn_unused_result
|
||||
public func queryValueFrom(from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> AnyObject? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to delete from a \(typeName(self)) outside the main thread."
|
||||
"Attempted to query from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.bridgeToSwift.mainContext.deleteAll(from, deleteClauses)
|
||||
return self.bridgeToSwift.mainContext.queryValue(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Queries a dictionary of attribute values as specified by the `CSQueryClause`s. Requires at least a `CSSelect` clause, and optional `CSWhere`, `CSOrderBy`, `CSGroupBy`, and `CSTweak` clauses.
|
||||
|
||||
A "query" differs from a "fetch" in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter selectClause: a `CSSelect` clause indicating the properties to fetch, and with the generic type indicating the return type.
|
||||
- parameter queryClauses: a series of `CSQueryClause` instances for the query request. Accepts `CSWhere`, `CSOrderBy`, `CSGroupBy`, and `CSTweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `CSSelect` parameter.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func queryAttributesFrom(from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> [[NSString: AnyObject]]? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to query from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.bridgeToSwift.mainContext.queryAttributes(from, selectClause, queryClauses)
|
||||
}
|
||||
}
|
||||
|
||||
350
Sources/ObjectiveC/CSSelect.swift
Normal file
350
Sources/ObjectiveC/CSSelect.swift
Normal file
@@ -0,0 +1,350 @@
|
||||
//
|
||||
// CSSelect.swift
|
||||
// CoreStore
|
||||
//
|
||||
// Copyright © 2016 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 Foundation
|
||||
import CoreData
|
||||
|
||||
|
||||
// MARK: - CSSelectTerm
|
||||
|
||||
/**
|
||||
The `CSSelectTerm` serves as the Objective-C bridging type for `SelectTerm`.
|
||||
*/
|
||||
@objc
|
||||
public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
/**
|
||||
Provides a `CSSelectTerm` to a `CSSelect` clause for querying an entity attribute.
|
||||
```
|
||||
NSString *fullName = [CSCoreStore
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect stringForTerm:[CSSelectTerm attribute:@"fullName"]]
|
||||
fetchClauses:@[[CSWhere keyPath:@"employeeID" isEqualTo: @1111]]];
|
||||
```
|
||||
- parameter keyPath: the attribute name
|
||||
- returns: a `CSSelectTerm` to a `CSSelect` clause for querying an entity attribute
|
||||
*/
|
||||
public static func attribute(keyPath: KeyPath) -> CSSelectTerm {
|
||||
|
||||
return self.init(.Attribute(keyPath))
|
||||
}
|
||||
|
||||
/**
|
||||
Provides a `CSSelectTerm` to a `CSSelect` clause for querying the average value of an attribute.
|
||||
```
|
||||
NSNumber *averageAge = [CSCoreStore
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect numberForTerm:[CSSelectTerm average:@"age" as:nil]]];
|
||||
```
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter `as`: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "average(<attributeName>)" is used
|
||||
- returns: a `CSSelectTerm` to a `CSSelect` clause for querying the average value of an attribute
|
||||
*/
|
||||
public static func average(keyPath: KeyPath, `as` alias: KeyPath?) -> CSSelectTerm {
|
||||
|
||||
return self.init(.Average(keyPath, As: alias))
|
||||
}
|
||||
|
||||
/**
|
||||
Provides a `CSSelectTerm` to a `CSSelect` clause for a count query.
|
||||
```
|
||||
NSNumber *numberOfEmployees = [CSCoreStore
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect numberForTerm:[CSSelectTerm count:@"employeeID" as:nil]]];
|
||||
```
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "count(<attributeName>)" is used
|
||||
- returns: a `SelectTerm` to a `Select` clause for a count query
|
||||
*/
|
||||
public static func count(keyPath: KeyPath, `as` alias: KeyPath?) -> CSSelectTerm {
|
||||
|
||||
return self.init(.Count(keyPath, As: alias))
|
||||
}
|
||||
|
||||
/**
|
||||
Provides a `CSSelectTerm` to a `CSSelect` clause for querying the maximum value for an attribute.
|
||||
```
|
||||
NSNumber *maximumAge = [CSCoreStore
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect numberForTerm:[CSSelectTerm maximum:@"age" as:nil]]];
|
||||
```
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "max(<attributeName>)" is used
|
||||
- returns: a `CSSelectTerm` to a `CSSelect` clause for querying the maximum value for an attribute
|
||||
*/
|
||||
public static func maximum(keyPath: KeyPath, `as` alias: KeyPath?) -> CSSelectTerm {
|
||||
|
||||
return self.init(.Maximum(keyPath, As: alias))
|
||||
}
|
||||
|
||||
/**
|
||||
Provides a `CSSelectTerm` to a `CSSelect` clause for querying the minimum value for an attribute.
|
||||
```
|
||||
NSNumber *minimumAge = [CSCoreStore
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect numberForTerm:[CSSelectTerm minimum:@"age" as:nil]]];
|
||||
```
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "min(<attributeName>)" is used
|
||||
- returns: a `CSSelectTerm` to a `CSSelect` clause for querying the minimum value for an attribute
|
||||
*/
|
||||
public static func minimum(keyPath: KeyPath, `as` alias: KeyPath?) -> CSSelectTerm {
|
||||
|
||||
return self.init(.Minimum(keyPath, As: alias))
|
||||
}
|
||||
|
||||
/**
|
||||
Provides a `CSSelectTerm` to a `CSSelect` clause for querying the sum value for an attribute.
|
||||
```
|
||||
NSNumber *totalAge = [CSCoreStore
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect numberForTerm:[CSSelectTerm sum:@"age" as:nil]]];
|
||||
```
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "sum(<attributeName>)" is used
|
||||
- returns: a `CSSelectTerm` to a `CSSelect` clause for querying the sum value for an attribute
|
||||
*/
|
||||
public static func sum(keyPath: KeyPath, `as` alias: KeyPath?) -> CSSelectTerm {
|
||||
|
||||
return self.init(.Sum(keyPath, As: alias))
|
||||
}
|
||||
|
||||
|
||||
|
||||
// MARK: NSObject
|
||||
|
||||
public override var hash: Int {
|
||||
|
||||
return self.bridgeToSwift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
|
||||
guard let object = object as? CSSelectTerm else {
|
||||
|
||||
return false
|
||||
}
|
||||
return self.bridgeToSwift == object.bridgeToSwift
|
||||
}
|
||||
|
||||
|
||||
// MARK: CoreStoreObjectiveCType
|
||||
|
||||
public let bridgeToSwift: SelectTerm
|
||||
|
||||
public init(_ swiftValue: SelectTerm) {
|
||||
|
||||
self.bridgeToSwift = swiftValue
|
||||
super.init()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - SelectTerm
|
||||
|
||||
extension SelectTerm: CoreStoreSwiftType {
|
||||
|
||||
// MARK: CoreStoreSwiftType
|
||||
|
||||
public typealias ObjectiveCType = CSSelectTerm
|
||||
}
|
||||
|
||||
|
||||
// MARK: - CSSelect
|
||||
|
||||
/**
|
||||
The `CSSelect` serves as the Objective-C bridging type for `Select`.
|
||||
*/
|
||||
@objc
|
||||
public final class CSSelect: NSObject {
|
||||
|
||||
/**
|
||||
Creates a `CSSelect` clause for querying `NSNumber` values.
|
||||
```
|
||||
NSNumber *maximumAge = [CSCoreStore
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect numberForTerm:[CSSelectTerm maximum:@"age" as:nil]]];
|
||||
```
|
||||
- parameter term: the `CSSelectTerm` specifying the attribute/aggregate value to query
|
||||
- returns: a `CSSelect` clause for querying an entity attribute
|
||||
*/
|
||||
public static func numberForTerm(term: CSSelectTerm) -> CSSelect {
|
||||
|
||||
return self.init(Select<NSNumber>(term.bridgeToSwift))
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a `CSSelect` clause for querying `NSDecimalNumber` values.
|
||||
```
|
||||
NSNumber *averageAge = [CSCoreStore
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect decimalNumberForTerm:[CSSelectTerm average:@"age" as:nil]]];
|
||||
```
|
||||
- parameter term: the `CSSelectTerm` specifying the attribute/aggregate value to query
|
||||
- returns: a `CSSelect` clause for querying an entity attribute
|
||||
*/
|
||||
public static func decimalNumberForTerm(term: CSSelectTerm) -> CSSelect {
|
||||
|
||||
return self.init(Select<NSDecimalNumber>(term.bridgeToSwift))
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a `CSSelect` clause for querying `NSString` values.
|
||||
```
|
||||
NSString *fullName = [CSCoreStore
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect stringForTerm:[CSSelectTerm attribute:@"fullName"]]
|
||||
fetchClauses:@[[CSWhere keyPath:@"employeeID" isEqualTo: @1111]]];
|
||||
```
|
||||
- parameter term: the `CSSelectTerm` specifying the attribute/aggregate value to query
|
||||
- returns: a `CSSelect` clause for querying an entity attribute
|
||||
*/
|
||||
public static func stringForTerm(term: CSSelectTerm) -> CSSelect {
|
||||
|
||||
return self.init(Select<NSString>(term.bridgeToSwift))
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a `CSSelect` clause for querying `NSDate` values.
|
||||
```
|
||||
NSDate *lastUpdatedDate = [CSCoreStore
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect dateForTerm:[CSSelectTerm maximum:@"updatedDate" as:nil]]];
|
||||
```
|
||||
- parameter term: the `CSSelectTerm` specifying the attribute/aggregate value to query
|
||||
- returns: a `CSSelect` clause for querying an entity attribute
|
||||
*/
|
||||
public static func dateForTerm(term: CSSelectTerm) -> CSSelect {
|
||||
|
||||
return self.init(Select<NSDate>(term.bridgeToSwift))
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a `CSSelect` clause for querying `NSData` values.
|
||||
```
|
||||
NSData *imageData = [CSCoreStore
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect dataForTerm:[CSSelectTerm attribute:@"imageData" as:nil]]
|
||||
fetchClauses:@[[CSWhere keyPath:@"employeeID" isEqualTo: @1111]]];
|
||||
```
|
||||
- parameter term: the `CSSelectTerm` specifying the attribute/aggregate value to query
|
||||
- returns: a `CSSelect` clause for querying an entity attribute
|
||||
*/
|
||||
public static func dataForTerm(term: CSSelectTerm) -> CSSelect {
|
||||
|
||||
return self.init(Select<NSData>(term.bridgeToSwift))
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a `CSSelect` clause for querying `NSManagedObjectID` values.
|
||||
```
|
||||
NSManagedObjectID *objectIDForOldest = [CSCoreStore
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect managedObjectIDForTerm:[CSSelectTerm attribute:@"age" as:nil]]
|
||||
fetchClauses:@[[CSWhere keyPath:@"employeeID" isEqualTo: @1111]]];
|
||||
```
|
||||
- parameter term: the `CSSelectTerm` specifying the attribute/aggregate value to query
|
||||
- returns: a `CSSelect` clause for querying an entity attribute
|
||||
*/
|
||||
public static func managedObjectIDForTerm(term: CSSelectTerm) -> CSSelect {
|
||||
|
||||
return self.init(Select<NSManagedObjectID>(term.bridgeToSwift))
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a `CSSelect` clause for querying `NSDictionary` of an entity's attribute keys and values.
|
||||
```
|
||||
NSDictionary *keyValues = [CSCoreStore
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect dictionaryForTerm:[CSSelectTerm maximum:@"age" as:nil]]];
|
||||
```
|
||||
- parameter term: the `CSSelectTerm` specifying the attribute/aggregate value to query
|
||||
- returns: a `CSSelect` clause for querying an entity attribute
|
||||
*/
|
||||
public static func dictionaryForTerm(term: CSSelectTerm) -> CSSelect {
|
||||
|
||||
return self.init(Select<NSDictionary>(term.bridgeToSwift))
|
||||
}
|
||||
|
||||
public static func dictionaryForTerms(terms: [CSSelectTerm]) -> CSSelect {
|
||||
|
||||
return self.init(Select<NSDictionary>(terms.map { $0.bridgeToSwift }))
|
||||
}
|
||||
|
||||
|
||||
// MARK: NSObject
|
||||
|
||||
public override var hash: Int {
|
||||
|
||||
return self.attributeType.hashValue
|
||||
^ self.selectTerms.map { $0.hashValue }.reduce(0, combine: ^)
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
|
||||
guard let object = object as? CSSelect else {
|
||||
|
||||
return false
|
||||
}
|
||||
return self.attributeType == object.attributeType
|
||||
&& self.selectTerms == object.selectTerms
|
||||
}
|
||||
|
||||
|
||||
// MARK: CoreStoreObjectiveCType
|
||||
|
||||
public init<T: SelectValueResultType>(_ swiftValue: Select<T>) {
|
||||
|
||||
self.attributeType = T.attributeType
|
||||
self.selectTerms = swiftValue.selectTerms
|
||||
super.init()
|
||||
}
|
||||
|
||||
public init<T: SelectResultType>(_ swiftValue: Select<T>) {
|
||||
|
||||
self.attributeType = .UndefinedAttributeType
|
||||
self.selectTerms = swiftValue.selectTerms
|
||||
super.init()
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal let attributeType: NSAttributeType
|
||||
internal let selectTerms: [SelectTerm]
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Select
|
||||
|
||||
extension Select: CoreStoreSwiftType {
|
||||
|
||||
// MARK: CoreStoreSwiftType
|
||||
|
||||
public var bridgeToObjectiveC: CSSelect {
|
||||
|
||||
return CSSelect(self)
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@ internal extension NSManagedObjectContext {
|
||||
@nonobjc
|
||||
internal func fetchOne(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> NSManagedObject? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 1
|
||||
@@ -49,7 +49,7 @@ internal extension NSManagedObjectContext {
|
||||
@nonobjc
|
||||
internal func fetchAll(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> [NSManagedObject]? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
@@ -62,7 +62,7 @@ internal extension NSManagedObjectContext {
|
||||
@nonobjc
|
||||
internal func fetchCount(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> Int? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
@@ -72,7 +72,7 @@ internal extension NSManagedObjectContext {
|
||||
@nonobjc
|
||||
internal func fetchObjectID(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> NSManagedObjectID? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 1
|
||||
@@ -85,7 +85,7 @@ internal extension NSManagedObjectContext {
|
||||
@nonobjc
|
||||
internal func fetchObjectIDs(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> [NSManagedObjectID]? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
@@ -98,7 +98,7 @@ internal extension NSManagedObjectContext {
|
||||
@nonobjc
|
||||
internal func deleteAll(from: CSFrom, _ deleteClauses: [CSDeleteClause]) -> Int? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
@@ -109,4 +109,33 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
return self.deleteAll(fetchRequest)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func queryValue(from: CSFrom, _ selectClause: CSSelect, _ queryClauses: [CSQueryClause]) -> AnyObject? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
|
||||
let selectTerms = selectClause.selectTerms
|
||||
selectTerms.applyToFetchRequest(fetchRequest, owner: selectClause)
|
||||
queryClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
return self.queryValue(selectTerms, fetchRequest: fetchRequest)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func queryAttributes(from: CSFrom, _ selectClause: CSSelect, _ queryClauses: [CSQueryClause]) -> [[NSString: AnyObject]]? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
|
||||
selectClause.selectTerms.applyToFetchRequest(fetchRequest, owner: selectClause)
|
||||
queryClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
return self.queryAttributes(fetchRequest)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user