Merge pull request #177 from blender/feature/optional-where-chaining

Add optional && and || operators to Where clause
This commit is contained in:
John Estropia
2017-06-28 13:32:16 +09:00
committed by GitHub
2 changed files with 98 additions and 0 deletions

View File

@@ -256,6 +256,21 @@ final class WhereTests: XCTestCase {
XCTAssertEqual(andWhere.predicate, andPredicate)
XCTAssertEqual(andWhere, whereClause1 && whereClause2 && whereClause3)
}
do {
let andWhere = whereClause1 && whereClause2 && whereClause3
let noneWhere: Where? = nil
let someWhere: Where? = Where("key4", isEqualTo: "value4")
let finalNoneWhere = andWhere && noneWhere
let finalSomeWhere = andWhere && someWhere
let unwrappedFinalSomeWhere = andWhere && someWhere!
XCTAssertEqual(andWhere.predicate, finalNoneWhere.predicate)
XCTAssertEqual(finalSomeWhere.predicate, unwrappedFinalSomeWhere.predicate)
}
do {
let orWhere = whereClause1 || whereClause2 || whereClause3
@@ -272,6 +287,21 @@ final class WhereTests: XCTestCase {
XCTAssertEqual(orWhere.predicate, orPredicate)
XCTAssertEqual(orWhere, whereClause1 || whereClause2 || whereClause3)
}
do {
let orWhere = whereClause1 || whereClause2 || whereClause3
let noneWhere: Where? = nil
let someWhere: Where? = Where("key4", isEqualTo: "value4")
let finalNoneWhere = orWhere && noneWhere
let finalSomeWhere = orWhere && someWhere
let unwrappedFinalSomeWhere = orWhere && someWhere!
XCTAssertEqual(orWhere.predicate, finalNoneWhere.predicate)
XCTAssertEqual(finalSomeWhere.predicate, unwrappedFinalSomeWhere.predicate)
}
}
@objc

View File

@@ -42,6 +42,40 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
return Where(NSCompoundPredicate(type: .and, subpredicates: [left.predicate, right.predicate]))
}
/**
Combines two `Where` predicates together using `AND` operator.
- parameter left: the left hand side `Where` clause
- parameter right: the right hand side `Where` clause
- returns: Return `left` unchanged if `right` is nil
*/
public static func && (left: Where, right: Where?) -> Where {
if right != nil {
return left && right!
}
else {
return left
}
}
/**
Combines two `Where` predicates together using `AND` operator.
- parameter left: the left hand side `Where` clause
- parameter right: the right hand side `Where` clause
- returns: Returns `right` unchanged if `left` is nil
*/
public static func && (left: Where?, right: Where) -> Where {
if left != nil {
return left && right
}
else {
return right
}
}
/**
Combines two `Where` predicates together using `OR` operator
*/
@@ -50,6 +84,40 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
return Where(NSCompoundPredicate(type: .or, subpredicates: [left.predicate, right.predicate]))
}
/**
Combines two `Where` predicates together using `OR` operator.
- parameter left: the left hand side `Where` clause
- parameter right: the right hand side `Where` clause
- returns: Returns `left` unchanged if `right` is nil
*/
public static func || (left: Where, right: Where?) -> Where {
if right != nil {
return left || right!
}
else {
return left
}
}
/**
Combines two `Where` predicates together using `OR` operator.
- parameter left: the left hand side `Where` clause
- parameter right: the right hand side `Where` clause
- returns: Return `right` unchanged if `left` is nil
*/
public static func || (left: Where?, right: Where) -> Where {
if left != nil {
return left || right
}
else {
return right
}
}
/**
Inverts the predicate of a `Where` clause using `NOT` operator
*/