diff --git a/Documentation/SQLInterpolation.md b/Documentation/SQLInterpolation.md index 82861693f..9cf24ac3d 100644 --- a/Documentation/SQLInterpolation.md +++ b/Documentation/SQLInterpolation.md @@ -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 diff --git a/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift b/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift index f4326571e..0fd649863 100644 --- a/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift +++ b/GRDB/QueryInterface/SQLInterpolation+QueryInterface.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 = """ + /// 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 = """ + /// SELECT * FROM player + /// ORDER BY email COLLATING \(.nocase) + /// """ + public mutating func appendInterpolation(_ collation: Database.CollationName) { + elements.append(.sql(collation.rawValue)) + } } diff --git a/Tests/GRDBTests/SQLLiteralTests.swift b/Tests/GRDBTests/SQLLiteralTests.swift index 7a1c95b57..8b104640b 100644 --- a/Tests/GRDBTests/SQLLiteralTests.swift +++ b/Tests/GRDBTests/SQLLiteralTests.swift @@ -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)