Skip to content

Commit d80a5ef

Browse files
committed
Support partial ranges for completeness
1 parent e0eb663 commit d80a5ef

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

Sources/SQLite/Typed/Operators.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,30 @@ public func ~=<V : Value>(lhs: Range<V>, rhs: Expression<V?>) -> Expression<Bool
490490
return Expression("\(rhs.template) >= ? AND \(rhs.template) < ?", rhs.bindings + [lhs.lowerBound.datatypeValue] + rhs.bindings + [lhs.upperBound.datatypeValue])
491491
}
492492

493+
public func ~=<V : Value>(lhs: PartialRangeThrough<V>, rhs: Expression<V>) -> Expression<Bool> where V.Datatype : Comparable & Value {
494+
return Expression("\(rhs.template) <= ?", rhs.bindings + [lhs.upperBound.datatypeValue])
495+
}
496+
497+
public func ~=<V : Value>(lhs: PartialRangeThrough<V>, rhs: Expression<V?>) -> Expression<Bool?> where V.Datatype : Comparable & Value {
498+
return Expression("\(rhs.template) <= ?", rhs.bindings + [lhs.upperBound.datatypeValue])
499+
}
500+
501+
public func ~=<V : Value>(lhs: PartialRangeUpTo<V>, rhs: Expression<V>) -> Expression<Bool> where V.Datatype : Comparable & Value {
502+
return Expression("\(rhs.template) < ?", rhs.bindings + [lhs.upperBound.datatypeValue])
503+
}
504+
505+
public func ~=<V : Value>(lhs: PartialRangeUpTo<V>, rhs: Expression<V?>) -> Expression<Bool?> where V.Datatype : Comparable & Value {
506+
return Expression("\(rhs.template) < ?", rhs.bindings + [lhs.upperBound.datatypeValue])
507+
}
508+
509+
public func ~=<V : Value>(lhs: PartialRangeFrom<V>, rhs: Expression<V>) -> Expression<Bool> where V.Datatype : Comparable & Value {
510+
return Expression("\(rhs.template) >= ?", rhs.bindings + [lhs.lowerBound.datatypeValue])
511+
}
512+
513+
public func ~=<V : Value>(lhs: PartialRangeFrom<V>, rhs: Expression<V?>) -> Expression<Bool?> where V.Datatype : Comparable & Value {
514+
return Expression("\(rhs.template) >= ?", rhs.bindings + [lhs.lowerBound.datatypeValue])
515+
}
516+
493517
// MARK: -
494518

495519
public func &&(lhs: Expression<Bool>, rhs: Expression<Bool>) -> Expression<Bool> {

Tests/SQLiteTests/OperatorsTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,21 @@ class OperatorsTests : XCTestCase {
260260
AssertSQL("\"doubleOptional\" >= 1.2 AND \"doubleOptional\" < 4.5", 1.2..<4.5 ~= doubleOptional)
261261
}
262262

263+
func test_patternMatchingOperator_withComparablePartialRangeThrough_buildsBooleanExpression() {
264+
AssertSQL("\"double\" <= 4.5", ...4.5 ~= double)
265+
AssertSQL("\"doubleOptional\" <= 4.5", ...4.5 ~= doubleOptional)
266+
}
267+
268+
func test_patternMatchingOperator_withComparablePartialRangeUpTo_buildsBooleanExpression() {
269+
AssertSQL("\"double\" < 4.5", ..<4.5 ~= double)
270+
AssertSQL("\"doubleOptional\" < 4.5", ..<4.5 ~= doubleOptional)
271+
}
272+
273+
func test_patternMatchingOperator_withComparablePartialRangeFrom_buildsBooleanExpression() {
274+
AssertSQL("\"double\" >= 4.5", 4.5... ~= double)
275+
AssertSQL("\"doubleOptional\" >= 4.5", 4.5... ~= doubleOptional)
276+
}
277+
263278
func test_patternMatchingOperator_withomparableClosedRangeString_buildsBetweenBooleanExpression() {
264279
AssertSQL("\"string\" BETWEEN 'a' AND 'b'", "a"..."b" ~= string)
265280
AssertSQL("\"stringOptional\" BETWEEN 'a' AND 'b'", "a"..."b" ~= stringOptional)

0 commit comments

Comments
 (0)