WIP: more chain clause builder utilities

This commit is contained in:
John Rommel Estropia
2017-10-01 00:45:21 +09:00
parent 096e5493a6
commit 85ed815ec2
2 changed files with 27 additions and 39 deletions

View File

@@ -221,6 +221,11 @@ public extension From where D: NSManagedObject {
public extension From where D: CoreStoreObject {
public func `where`(_ clause: (D) -> Where<D>) -> FetchChainBuilder<D> {
return self.fetchChain(appending: clause(D.meta))
}
public func select<R>(_ keyPath: KeyPath<D, ValueContainer<D>.Required<R>>) -> QueryChainBuilder<D, R> {
return self.select(R.self, [SelectTerm<D>.attribute(keyPath)])
@@ -347,6 +352,14 @@ public extension FetchChainBuilder {
}
}
public extension FetchChainBuilder where D: CoreStoreObject {
public func `where`(_ clause: (D) -> Where<D>) -> FetchChainBuilder<D> {
return self.fetchChain(appending: clause(D.meta))
}
}
public extension QueryChainBuilder {
public func groupBy(_ clause: GroupBy<D>) -> QueryChainBuilder<D, R> {
@@ -431,6 +444,11 @@ public extension QueryChainBuilder where D: NSManagedObject {
public extension QueryChainBuilder where D: CoreStoreObject {
public func `where`(_ clause: (D) -> Where<D>) -> QueryChainBuilder<D, R> {
return self.queryChain(appending: clause(D.meta))
}
public func groupBy<T>(_ keyPath: KeyPath<D, ValueContainer<D>.Required<T>>) -> QueryChainBuilder<D, R> {
return self.groupBy(GroupBy<D>(keyPath))
@@ -511,3 +529,12 @@ public extension SectionMonitorChainBuilder {
)
}
}
@available(OSX 10.12, *)
public extension SectionMonitorChainBuilder where D: CoreStoreObject {
public func `where`(_ clause: (D) -> Where<D>) -> SectionMonitorChainBuilder<D> {
return self.sectionMonitorChain(appending: clause(D.meta))
}
}

View File

@@ -21,42 +21,3 @@ public protocol WhereClauseType: AnyWhereClause {
*/
associatedtype ObjectType: DynamicObject
}
public extension WhereClauseType {
/**
Combines two `Where` predicates together using `AND` operator.
- Warning: This operator overload is a workaround for Swift generics' inability to constrain by inheritance (https://bugs.swift.org/browse/SR-5213). In effect, this is less type-safe than other overloads because it allows AND'ing clauses of unrelated `DynamicObject` types.
*/
public static func && <TWhere: WhereClauseType>(left: Self, right: TWhere) -> Self {
return Self.init(NSCompoundPredicate(type: .and, subpredicates: [left.predicate, right.predicate]))
}
/**
Combines two `Where` predicates together using `AND` operator.
- Warning: This operator overload is a workaround for Swift generics' inability to constrain by inheritance (https://bugs.swift.org/browse/SR-5213). In effect, this is less type-safe than other overloads because it allows AND'ing clauses of unrelated `DynamicObject` types.
*/
public static func && <TWhere: WhereClauseType>(left: TWhere, right: Self) -> Self {
return Self.init(NSCompoundPredicate(type: .and, subpredicates: [left.predicate, right.predicate]))
}
/**
Combines two `Where` predicates together using `OR` operator.
- Warning: This operator overload is a workaround for Swift generics' inability to constrain by inheritance (https://bugs.swift.org/browse/SR-5213). In effect, this is less type-safe than other overloads because it allows OR'ing clauses of unrelated `DynamicObject` types.
*/
public static func || <TWhere: WhereClauseType>(left: Self, right: TWhere) -> Self {
return Self.init(NSCompoundPredicate(type: .or, subpredicates: [left.predicate, right.predicate]))
}
/**
Combines two `Where` predicates together using `OR` operator.
- Warning: This operator overload is a workaround for Swift generics' inability to constrain by inheritance (https://bugs.swift.org/browse/SR-5213). In effect, this is less type-safe than other overloads because it allows OR'ing clauses of unrelated `DynamicObject` types.
*/
public static func || <TWhere: WhereClauseType>(left: TWhere, right: Self) -> Self {
return Self.init(NSCompoundPredicate(type: .or, subpredicates: [left.predicate, right.predicate]))
}
}