This commit is contained in:
Gwendal Roué committed 2017-08-18 14:20:35 +02:00
1 parent e0f2305cd9
commit f72ffcbb43
5 files changed
+17 -32

No files matched your search

+5 -1
View File
@@ -962,7 +962,11 @@ private struct StatementRowImpl : RowImpl {
self.sqliteStatement = sqliteStatement
// Optimize row.value(named: "...")
let lowercaseColumnNames = (0..<sqlite3_column_count(sqliteStatement)).map { String(cString: sqlite3_column_name(sqliteStatement, Int32($0))).lowercased() }
self.lowercaseColumnIndexes = Dictionary(keyValueSequence: lowercaseColumnNames.enumerated().map { ($1, $0) }.reversed())
self.lowercaseColumnIndexes = Dictionary(
lowercaseColumnNames
.enumerated()
.map { ($0.element, $0.offset) },
uniquingKeysWith: { (left, _) in left }) // keep leftmost indexes
}
var count: Int {
+5 -4
View File
@@ -39,10 +39,11 @@ public struct LayoutedColumnMapping {
/// try Row.fetchOne(db, "SELECT NULL, 'foo', 'bar'", adapter: FooBarAdapter())
public init<S: Sequence>(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 {
+6 -8
View File
@@ -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: Swift.Sequence>(_ 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].
+1 -1
View File
@@ -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 {
-18
View File
@@ -69,21 +69,3 @@ extension Array {
}
}
extension Dictionary {
/// Create a dictionary with the keys and values in the given sequence.
init<Sequence: Swift.Sequence>(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<Sequence: Swift.Sequence>(keys: Sequence, value: (Key) -> Value) where Sequence.Iterator.Element == Key {
self.init(minimumCapacity: keys.underestimatedCount)
for key in keys {
self[key] = value(key)
}
}
}