Add optional && and || operators to Where clause

This commit is contained in:
Tommaso Piazza
2017-06-27 13:58:16 +02:00
parent f72efc80b2
commit 790454f514
2 changed files with 98 additions and 0 deletions

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
*/