diff --git a/CoreStoreTests/WhereTests.swift b/CoreStoreTests/WhereTests.swift index c8f826e..307e404 100644 --- a/CoreStoreTests/WhereTests.swift +++ b/CoreStoreTests/WhereTests.swift @@ -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 diff --git a/Sources/Where.swift b/Sources/Where.swift index f9fe369..06aff22 100644 --- a/Sources/Where.swift +++ b/Sources/Where.swift @@ -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 */