mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Restore column ordering in filter(db, keys: [["a": ..., "b": ...], ...]) removed in 2b2e92c810
This commit is contained in:
1 parent
cc762e4e1d
commit
cdc6370ee5
2 files changed
+13
-3
No files matched your search
@@ -1210,7 +1210,7 @@ extension Database {
|
||||
/// If there exists a unique key on columns, return the columns
|
||||
/// ordered as the matching index (or primay key). Case of returned columns
|
||||
/// is not guaranteed.
|
||||
func columnsForUniqueKey(_ columns: [String], in tableName: String) throws -> [String]? {
|
||||
func columnsForUniqueKey<T: Sequence>(_ columns: T, in tableName: String) throws -> [String]? where T.Iterator.Element == String {
|
||||
let primaryKey = try self.primaryKey(tableName) // first, so that we fail early and consistently should the table not exist
|
||||
let lowercasedColumns = Set(columns.map { $0.lowercased() })
|
||||
if let index = try indexes(on: tableName).first(where: { index in index.isUnique && Set(index.columns.map { $0.lowercased() }) == lowercasedColumns }) {
|
||||
|
||||
@@ -75,8 +75,13 @@ extension TableMapping {
|
||||
static func filter(_ db: Database, keys: [[String: DatabaseValueConvertible?]], fatalErrorOnMissingUniqueIndex: Bool = true) throws -> QueryInterfaceRequest<Self> {
|
||||
// SELECT * FROM table WHERE ((a=? AND b=?) OR (c=? AND d=?) OR ...)
|
||||
let keyPredicates: [SQLExpression] = try keys.map { key in
|
||||
// Prevent filter(db, keys: [[:]])
|
||||
GRDBPrecondition(!key.isEmpty, "Invalid empty key dictionary")
|
||||
guard try db.table(databaseTableName, hasUniqueKey: key.keys) else {
|
||||
|
||||
// Prevent filter(db, keys: [["foo": 1, "bar": 2]]) where
|
||||
// ("foo", "bar") is not a unique key (primary key or columns of a
|
||||
// unique index)
|
||||
guard let orderedColumns = try db.columnsForUniqueKey(key.keys, in: databaseTableName) else {
|
||||
let message = "table \(databaseTableName) has no unique index on column(s) \(key.keys.sorted().joined(separator: ", "))"
|
||||
if fatalErrorOnMissingUniqueIndex {
|
||||
fatalError(message)
|
||||
@@ -84,7 +89,12 @@ extension TableMapping {
|
||||
throw DatabaseError(resultCode: .SQLITE_MISUSE, message: message)
|
||||
}
|
||||
}
|
||||
let columnPredicates: [SQLExpression] = key.map { (column, value) in Column(column) == value }
|
||||
|
||||
let lowercaseOrderedColumns = orderedColumns.map { $0.lowercased() }
|
||||
let columnPredicates: [SQLExpression] = key
|
||||
// Sort key columns in the same order as the unique index
|
||||
.sorted { (kv1, kv2) in lowercaseOrderedColumns.index(of: kv1.0.lowercased())! < lowercaseOrderedColumns.index(of: kv2.0.lowercased())! }
|
||||
.map { (column, value) in Column(column) == value }
|
||||
return SQLBinaryOperator.and.join(columnPredicates)! // not nil because columnPredicates is not empty
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user