mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
DatabaseCollation no longer uses a deprecated String initializer
This commit is contained in:
1 parent
aa2cdb7ec3
commit
f3acfecc16
3 files changed
+44
-40
No files matched your search
@@ -806,7 +806,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
||||
collationPointer,
|
||||
{ (collationPointer, length1, buffer1, length2, buffer2) in
|
||||
let collation = Unmanaged<DatabaseCollation>.fromOpaque(collationPointer!).takeUnretainedValue()
|
||||
return CInt(collation.function(length1, buffer1, length2, buffer2).rawValue)
|
||||
return collation.xCompare(length1, buffer1.unsafelyUnwrapped, length2, buffer2.unsafelyUnwrapped)
|
||||
}, nil)
|
||||
guard code == SQLITE_OK else {
|
||||
// Assume a GRDB bug: there is no point throwing any error.
|
||||
|
||||
@@ -53,12 +53,21 @@ public final class DatabaseCollation: Identifiable, Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
/// Feeds the `xCompare` parameter of sqlite3_create_collation_v2
|
||||
/// <https://www.sqlite.org/c3ref/create_collation.html>
|
||||
typealias XCompare = @Sendable (
|
||||
_ length1: CInt,
|
||||
_ buffer1: UnsafeRawPointer,
|
||||
_ length2: CInt,
|
||||
_ buffer2: UnsafeRawPointer
|
||||
) -> CInt
|
||||
|
||||
/// The identifier of the collation.
|
||||
public var id: ID { ID(name: name) }
|
||||
|
||||
/// The name of the collation.
|
||||
public let name: String
|
||||
let function: @Sendable (CInt, UnsafeRawPointer?, CInt, UnsafeRawPointer?) -> ComparisonResult
|
||||
let xCompare: XCompare
|
||||
|
||||
/// Creates a collation.
|
||||
///
|
||||
@@ -75,21 +84,16 @@ public final class DatabaseCollation: Identifiable, Sendable {
|
||||
/// - parameters:
|
||||
/// - name: The collation name.
|
||||
/// - function: A function that compares two strings.
|
||||
public init(_ name: String, function: @escaping @Sendable (String, String) -> ComparisonResult) {
|
||||
public convenience init(_ name: String, function: @escaping @Sendable (String, String) -> ComparisonResult) {
|
||||
self.init(name, xCompare: { (length1, buffer1, length2, buffer2) in
|
||||
let string1 = String(decoding: UnsafeRawBufferPointer(start: buffer1, count: Int(length1)), as: UTF8.self)
|
||||
let string2 = String(decoding: UnsafeRawBufferPointer(start: buffer2, count: Int(length2)), as: UTF8.self)
|
||||
return CInt(function(string1, string2).rawValue)
|
||||
})
|
||||
}
|
||||
|
||||
init(_ name: String, xCompare: @escaping XCompare) {
|
||||
self.name = name
|
||||
self.function = { (length1, buffer1, length2, buffer2) in
|
||||
// Buffers are not C strings: they do not end with \0.
|
||||
let string1 = String(
|
||||
bytesNoCopy: UnsafeMutableRawPointer(mutating: buffer1.unsafelyUnwrapped),
|
||||
length: Int(length1),
|
||||
encoding: .utf8,
|
||||
freeWhenDone: false)!
|
||||
let string2 = String(
|
||||
bytesNoCopy: UnsafeMutableRawPointer(mutating: buffer2.unsafelyUnwrapped),
|
||||
length: Int(length2),
|
||||
encoding: .utf8,
|
||||
freeWhenDone: false)!
|
||||
return function(string1, string2)
|
||||
}
|
||||
self.xCompare = xCompare
|
||||
}
|
||||
}
|
||||
@@ -6,38 +6,38 @@ class DatabaseCollationTests: GRDBTestCase {
|
||||
func testDefaultCollations() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE strings (id INTEGER PRIMARY KEY, name TEXT)")
|
||||
try db.execute(sql: "INSERT INTO strings VALUES (1, '1')")
|
||||
try db.execute(sql: "INSERT INTO strings VALUES (2, '2')")
|
||||
try db.execute(sql: "INSERT INTO strings VALUES (3, '10')")
|
||||
try db.execute(sql: "INSERT INTO strings VALUES (4, 'a')")
|
||||
try db.execute(sql: "INSERT INTO strings VALUES (5, 'à')")
|
||||
try db.execute(sql: "INSERT INTO strings VALUES (6, 'A')")
|
||||
try db.execute(sql: "INSERT INTO strings VALUES (7, 'Z')")
|
||||
try db.execute(sql: "INSERT INTO strings VALUES (8, 'z')")
|
||||
try db.execute(sql: """
|
||||
CREATE TABLE strings (id INTEGER PRIMARY KEY, name TEXT);
|
||||
INSERT INTO strings VALUES (1, '1');
|
||||
INSERT INTO strings VALUES (2, '2');
|
||||
INSERT INTO strings VALUES (3, '10');
|
||||
INSERT INTO strings VALUES (4, 'a');
|
||||
INSERT INTO strings VALUES (5, 'à');
|
||||
INSERT INTO strings VALUES (6, 'A');
|
||||
INSERT INTO strings VALUES (7, 'Z');
|
||||
INSERT INTO strings VALUES (8, 'z');
|
||||
INSERT INTO strings VALUES (9, '');
|
||||
INSERT INTO strings VALUES (10, NULL);
|
||||
INSERT INTO strings VALUES (11, x'42FF'); -- Invalid UTF8 "B�"
|
||||
""")
|
||||
|
||||
// Swift 4.2 and Swift 4.1 don't sort strings in the same way
|
||||
if "z" < "à" {
|
||||
XCTAssertEqual(
|
||||
try Int.fetchAll(db, sql: "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.unicodeCompare.name), id"),
|
||||
[1,3,2,6,7,4,8,5])
|
||||
} else {
|
||||
XCTAssertEqual(
|
||||
try Int.fetchAll(db, sql: "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.unicodeCompare.name), id"),
|
||||
[1,3,2,6,7,4,5,8])
|
||||
}
|
||||
// Note that "B�" is always last. We can observe that SQLite
|
||||
// does not invoke the collation for invalid UTF8 strings.
|
||||
XCTAssertEqual(
|
||||
try Int.fetchAll(db, sql: "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.unicodeCompare.name), id"),
|
||||
[10,9,1,3,2,6,7,4,8,5,11])
|
||||
XCTAssertEqual(
|
||||
try Int.fetchAll(db, sql: "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.caseInsensitiveCompare.name), id"),
|
||||
[1,3,2,4,6,5,7,8])
|
||||
[10,9,1,3,2,4,6,5,7,8,11])
|
||||
XCTAssertEqual(
|
||||
try Int.fetchAll(db, sql: "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.localizedCaseInsensitiveCompare.name), id"),
|
||||
[1,3,2,4,6,5,7,8])
|
||||
[10,9,1,3,2,4,6,5,7,8,11])
|
||||
XCTAssertEqual(
|
||||
try Int.fetchAll(db, sql: "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.localizedCompare.name), id"),
|
||||
[1,3,2,4,6,5,8,7])
|
||||
[10,9,1,3,2,4,6,5,8,7,11])
|
||||
XCTAssertEqual(
|
||||
try Int.fetchAll(db, sql: "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.localizedStandardCompare.name), id"),
|
||||
[1,2,3,4,6,5,8,7])
|
||||
[10,9,1,2,3,4,6,5,8,7,11])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user