Skip to content

Commit a4460b5

Browse files
committed
1 parent 6940620 commit a4460b5

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

Sources/SQLite/Helpers.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ extension String {
7373
}
7474

7575
func infix<T>(_ lhs: Expressible, _ rhs: Expressible, wrap: Bool = true) -> Expression<T> {
76-
let expression = Expression<T>(" \(self) ".join([lhs, rhs]).expression)
76+
return infix([lhs, rhs], wrap: wrap)
77+
}
78+
79+
func infix<T>(_ terms: [Expressible], wrap: Bool = true) -> Expression<T> {
80+
let expression = Expression<T>(" \(self) ".join(terms).expression)
7781
guard wrap else {
7882
return expression
7983
}

Sources/SQLite/Typed/Operators.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,13 @@ public func ~=<V : Value>(lhs: PartialRangeFrom<V>, rhs: Expression<V?>) -> Expr
547547

548548
// MARK: -
549549

550+
public func and(_ terms: Expression<Bool>...) -> Expression<Bool> {
551+
return "AND".infix(terms)
552+
}
553+
public func and(_ terms: [Expression<Bool>]) -> Expression<Bool> {
554+
return "AND".infix(terms)
555+
}
556+
550557
public func &&(lhs: Expression<Bool>, rhs: Expression<Bool>) -> Expression<Bool> {
551558
return Operator.and.infix(lhs, rhs)
552559
}
@@ -571,6 +578,12 @@ public func &&(lhs: Bool, rhs: Expression<Bool>) -> Expression<Bool> {
571578
public func &&(lhs: Bool, rhs: Expression<Bool?>) -> Expression<Bool?> {
572579
return Operator.and.infix(lhs, rhs)
573580
}
581+
public func or(_ terms: Expression<Bool>...) -> Expression<Bool> {
582+
return "OR".infix(terms)
583+
}
584+
public func or(_ terms: [Expression<Bool>]) -> Expression<Bool> {
585+
return "OR".infix(terms)
586+
}
574587

575588
public func ||(lhs: Expression<Bool>, rhs: Expression<Bool>) -> Expression<Bool> {
576589
return Operator.or.infix(lhs, rhs)

Tests/SQLiteTests/OperatorsTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,16 @@ class OperatorsTests : XCTestCase {
290290
AssertSQL("(1 AND \"bool\")", true && bool)
291291
AssertSQL("(1 AND \"boolOptional\")", true && boolOptional)
292292
}
293+
294+
func test_andFunction_withBooleanExpressions_buildsCompoundExpression() {
295+
AssertSQL("(\"bool\" AND \"bool\" AND \"bool\")", and([bool, bool, bool]))
296+
AssertSQL("(\"bool\" AND \"bool\")", and([bool, bool]))
297+
AssertSQL("(\"bool\")", and([bool]))
298+
299+
AssertSQL("(\"bool\" AND \"bool\" AND \"bool\")", and(bool, bool, bool))
300+
AssertSQL("(\"bool\" AND \"bool\")", and(bool, bool))
301+
AssertSQL("(\"bool\")", and(bool))
302+
}
293303

294304
func test_doubleOrOperator_withBooleanExpressions_buildsCompoundExpression() {
295305
AssertSQL("(\"bool\" OR \"bool\")", bool || bool)
@@ -301,6 +311,16 @@ class OperatorsTests : XCTestCase {
301311
AssertSQL("(1 OR \"bool\")", true || bool)
302312
AssertSQL("(1 OR \"boolOptional\")", true || boolOptional)
303313
}
314+
315+
func test_orFunction_withBooleanExpressions_buildsCompoundExpression() {
316+
AssertSQL("(\"bool\" OR \"bool\" OR \"bool\")", or([bool, bool, bool]))
317+
AssertSQL("(\"bool\" OR \"bool\")", or([bool, bool]))
318+
AssertSQL("(\"bool\")", or([bool]))
319+
320+
AssertSQL("(\"bool\" OR \"bool\" OR \"bool\")", or(bool, bool, bool))
321+
AssertSQL("(\"bool\" OR \"bool\")", or(bool, bool))
322+
AssertSQL("(\"bool\")", or(bool))
323+
}
304324

305325
func test_unaryNotOperator_withBooleanExpressions_buildsNotExpression() {
306326
AssertSQL("NOT (\"bool\")", !bool)

0 commit comments

Comments
 (0)