SQL Interpolation: add support for collations

This commit is contained in:
Gwendal Roué committed 2021-01-28 08:10:30 +01:00
1 parent 1d41807e25
commit 4decdd7dea
3 files changed
+54 -2

No files matched your search

+8 -1
View File
@@ -417,9 +417,16 @@ This chapter lists all kinds of supported interpolations.
```swift
// SELECT * FROM player ORDER BY name DESC
"SELECT * FROM player WHERE id IN \(Column("name").desc)"
"SELECT * FROM player ORDER BY \(Column("name").desc)"
```
- Database Collations:
```swift
"SELECT * FROM player ORDER BY email COLLATING \(.nocase)"
"SELECT * FROM player ORDER BY email COLLATING \(.localizedCompare)"
```
- Subqueries:
```swift
@@ -271,7 +271,7 @@ extension SQLInterpolation {
elements.append(.expression(expressible.sqlExpression))
}
// MARK: Common Table Expressions
// MARK: - Common Table Expressions
/// Appends the table name of the common table expression.
///
@@ -305,4 +305,26 @@ extension SQLInterpolation {
elements.append(.subquery(cte.cte.requestPromise))
elements.append(.sql(")"))
}
// MARK: - Collations
/// Appends the name of the collation.
///
/// let request: SQLRequest<Player> = """
/// SELECT * FROM player
/// ORDER BY name COLLATING \(DatabaseCollation.localizedCaseInsensitiveCompare)
/// """
public mutating func appendInterpolation(_ collation: DatabaseCollation) {
elements.append(.sql(collation.name))
}
/// Appends the name of the collation.
///
/// let request: SQLRequest<Player> = """
/// SELECT * FROM player
/// ORDER BY email COLLATING \(.nocase)
/// """
public mutating func appendInterpolation(_ collation: Database.CollationName) {
elements.append(.sql(collation.rawValue))
}
}
+23
View File
@@ -739,6 +739,29 @@ extension SQLLiteralTests {
}
}
func testCollationInterpolation() throws {
try makeDatabaseQueue().inDatabase { db in
do {
// Database.CollationName
let query: SQLLiteral = "SELECT * FROM player ORDER BY email COLLATION \(.nocase)"
let (sql, arguments) = try query.build(db)
XCTAssertEqual(sql, """
SELECT * FROM player ORDER BY email COLLATION NOCASE
""")
XCTAssertEqual(arguments, [])
}
do {
// DatabaseCollation
let query: SQLLiteral = "SELECT * FROM player ORDER BY name COLLATION \(.localizedCompare)"
let (sql, arguments) = try query.build(db)
XCTAssertEqual(sql, """
SELECT * FROM player ORDER BY name COLLATION swiftLocalizedCompare
""")
XCTAssertEqual(arguments, [])
}
}
}
func testIsEmpty() {
XCTAssertTrue(SQLLiteral(elements: []).isEmpty)
XCTAssertTrue(SQLLiteral(sql: "").isEmpty)