From f72ffcbb43eca873b0405c24339cb7dac22b25a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 12 Jul 2017 09:03:54 +0200 Subject: [PATCH] SE-0165 --- GRDB/Core/Row.swift | 6 +++++- GRDB/Core/RowAdapter.swift | 9 +++++---- GRDB/Core/Statement.swift | 14 ++++++-------- GRDB/Record/TableMapping.swift | 2 +- GRDB/Utils/Utils.swift | 18 ------------------ 5 files changed, 17 insertions(+), 32 deletions(-) diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index d60a3d776..23943a498 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -962,7 +962,11 @@ private struct StatementRowImpl : RowImpl { self.sqliteStatement = sqliteStatement // Optimize row.value(named: "...") let lowercaseColumnNames = (0..(layoutColumns: S) where S.Iterator.Element == (Int, String) { self.layoutColumns = Array(layoutColumns) - self.lowercaseColumnIndexes = Dictionary(keyValueSequence: layoutColumns - .enumerated() - .map { ($1.1.lowercased(), $0) } - .reversed()) // reversed() so that the the dictionary caches leftmost indexes + self.lowercaseColumnIndexes = Dictionary( + layoutColumns + .enumerated() + .map { ($0.element.1.lowercased(), $0.offset) }, + uniquingKeysWith: { (left, _) in left }) // keep leftmost indexes } func baseColumnIndex(atMappingIndex index: Int) -> Int { diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index 6274e954a..715f7c97a 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -250,7 +250,9 @@ public final class SelectStatement : Statement { /// Cache for indexOfColumn(). Keys are lowercase. private lazy var columnIndexes: [String: Int] = { - return Dictionary(keyValueSequence: self.columnNames.enumerated().map { ($1.lowercased(), $0) }.reversed()) + return Dictionary( + self.columnNames.enumerated().map { ($0.element.lowercased(), $0.offset) }, + uniquingKeysWith: { (left, _) in left }) // keep leftmost indexes }() /// Returns the index of the leftmost column named `name`, in a @@ -284,11 +286,7 @@ public final class SelectStatement : Statement { } mutating func insert(column: String, ofTable table: String) { - if columns[table] != nil { - columns[table]!.insert(column) - } else { - columns[table] = [column] - } + columns[table, default: []].insert(column) } /// If true, selection is unknown @@ -619,7 +617,7 @@ public struct StatementArguments { /// - parameter sequence: A sequence of (key, value) pairs /// - returns: A StatementArguments. public init(_ dictionary: [String: DatabaseValueConvertible?]) { - namedValues = Dictionary(keys: dictionary.keys) { dictionary[$0]!?.databaseValue ?? .null } + namedValues = dictionary.mapValues { $0?.databaseValue ?? .null } } /// Initializes arguments from a sequence of (key, value) pairs, such as @@ -631,7 +629,7 @@ public struct StatementArguments { /// - parameter sequence: A sequence of (key, value) pairs /// - returns: A StatementArguments. public init(_ sequence: Sequence) where Sequence.Iterator.Element == (String, DatabaseValueConvertible?) { - namedValues = Dictionary(keyValueSequence: sequence.map { ($0, $1?.databaseValue ?? .null) }) + namedValues = Dictionary(uniqueKeysWithValues: sequence.map { ($0.0, $0.1?.databaseValue ?? .null) }) } /// Initializes arguments from [AnyHashable: Any]. diff --git a/GRDB/Record/TableMapping.swift b/GRDB/Record/TableMapping.swift index b4f2dac0a..d79d1cc5e 100644 --- a/GRDB/Record/TableMapping.swift +++ b/GRDB/Record/TableMapping.swift @@ -217,7 +217,7 @@ extension TableMapping { static func rowPrimaryKey(_ db: Database) throws -> (Row) -> [String: DatabaseValue] { if let primaryKey = try db.primaryKey(databaseTableName) { let columns = primaryKey.columns - return { row in Dictionary(keys: columns) { row.value(named: $0) } } + return { row in Dictionary(uniqueKeysWithValues: columns.lazy.map { ($0, row.value(named: $0)) }) } } else if selectsRowID { return { row in [Column.rowID.name: row.value(Column.rowID)] } } else { diff --git a/GRDB/Utils/Utils.swift b/GRDB/Utils/Utils.swift index 2f7c7addf..cbd2cdcda 100644 --- a/GRDB/Utils/Utils.swift +++ b/GRDB/Utils/Utils.swift @@ -69,21 +69,3 @@ extension Array { } } -extension Dictionary { - - /// Create a dictionary with the keys and values in the given sequence. - init(keyValueSequence: Sequence) where Sequence.Iterator.Element == (Key, Value) { - self.init(minimumCapacity: keyValueSequence.underestimatedCount) - for (key, value) in keyValueSequence { - self[key] = value - } - } - - /// Create a dictionary from keys and a value builder. - init(keys: Sequence, value: (Key) -> Value) where Sequence.Iterator.Element == Key { - self.init(minimumCapacity: keys.underestimatedCount) - for key in keys { - self[key] = value(key) - } - } -}