Skip to content

Commit 1f185e2

Browse files
committed
Add collation operator
Signed-off-by: Stephen Celis <stephen@stephencelis.com>
1 parent 162ee29 commit 1f185e2

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

SQLite Common Tests/ExpressionTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,13 @@ class ExpressionTests: XCTestCase {
243243
ExpectExecutionMatches(db, "('Hello' MATCH 'ello')", query)
244244
}
245245

246+
func test_collateOperator_withStringExpression_buildsCollationExpression() {
247+
let string = Expression<String>(value: "Hello ")
248+
ExpectExecutionMatches(db, "('Hello ' COLLATE BINARY)", users.select(collate(.Binary, string)))
249+
ExpectExecutionMatches(db, "('Hello ' COLLATE NOCASE)", users.select(collate(.NoCase, string)))
250+
ExpectExecutionMatches(db, "('Hello ' COLLATE RTRIM)", users.select(collate(.RTrim, string)))
251+
}
252+
246253
func test_doubleAndOperator_withBooleanExpressions_buildsCompoundExpression() {
247254
let bool = Expression<Bool>(value: true)
248255
let query = users.select(bool && bool)

SQLite Common/Expression.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,20 @@ public prefix func ~(rhs: Expression<Int>) -> Expression<Int> {
174174
return wrap(__FUNCTION__, rhs)
175175
}
176176

177+
public enum Collation: String {
178+
179+
case Binary = "BINARY"
180+
181+
case NoCase = "NOCASE"
182+
183+
case RTrim = "RTRIM"
184+
185+
}
186+
187+
public func collate(collation: Collation, expression: Expression<String>) -> Expression<String> {
188+
return infix("COLLATE", expression, Expression(collation.rawValue))
189+
}
190+
177191
// MARK: - Predicates
178192

179193
public func ==<T: protocol<Value, Equatable>>(lhs: Expression<T>, rhs: Expression<T>) -> Expression<Bool> {

0 commit comments

Comments
 (0)