Improve performance of Row.value(named:)

This commit is contained in:
Gwendal Roué committed 2016-01-13 08:28:44 +01:00
1 parent 88ffcd8c1c
commit 98cc352856
2 files changed
+34 -24

No files matched your search

+22 -10
View File
@@ -88,9 +88,11 @@ public final class Row: CollectionType {
/// - returns: An optional *Value*.
public func value<Value: DatabaseValueConvertible>(atIndex index: Int) -> Value? {
precondition(index >= 0 && index < count, "row index out of range")
return impl
.databaseValue(atIndex: index)
.value()
return unsafeValue(atIndex: index)
}
private func unsafeValue<Value: DatabaseValueConvertible>(atIndex index: Int) -> Value? {
return impl.databaseValue(atIndex: index).value()
}
/// Returns the value at given index, converted to the requested type.
@@ -119,6 +121,10 @@ public final class Row: CollectionType {
/// - returns: An optional *Value*.
public func value<Value: protocol<DatabaseValueConvertible, SQLiteStatementConvertible>>(atIndex index: Int) -> Value? {
precondition(index >= 0 && index < count, "row index out of range")
return unsafeValue(atIndex: index)
}
private func unsafeValue<Value: protocol<DatabaseValueConvertible, SQLiteStatementConvertible>>(atIndex index: Int) -> Value? {
let sqliteStatement = self.sqliteStatement
if sqliteStatement != nil {
// Metal row
@@ -154,9 +160,11 @@ public final class Row: CollectionType {
/// - returns: A *Value*.
public func value<Value: DatabaseValueConvertible>(atIndex index: Int) -> Value {
precondition(index >= 0 && index < count, "row index out of range")
return impl
.databaseValue(atIndex: index)
.value()
return unsafeValue(atIndex: index)
}
private func unsafeValue<Value: DatabaseValueConvertible>(atIndex index: Int) -> Value {
return impl.databaseValue(atIndex: index).value()
}
/// Returns the value at given index, converted to the requested type.
@@ -185,6 +193,10 @@ public final class Row: CollectionType {
/// - returns: A *Value*.
public func value<Value: protocol<DatabaseValueConvertible, SQLiteStatementConvertible>>(atIndex index: Int) -> Value {
precondition(index >= 0 && index < count, "row index out of range")
return unsafeValue(atIndex: index)
}
private func unsafeValue<Value: protocol<DatabaseValueConvertible, SQLiteStatementConvertible>>(atIndex index: Int) -> Value {
let sqliteStatement = self.sqliteStatement
if sqliteStatement != nil {
// Metal row
@@ -244,7 +256,7 @@ public final class Row: CollectionType {
guard let index = impl.indexForColumn(named: columnName) else {
return nil
}
return value(atIndex: index)
return unsafeValue(atIndex: index)
}
/// Returns the value at given column, converted to the requested type.
@@ -273,7 +285,7 @@ public final class Row: CollectionType {
guard let index = impl.indexForColumn(named: columnName) else {
return nil
}
return value(atIndex: index)
return unsafeValue(atIndex: index)
}
/// Returns the value at given column, converted to the requested type.
@@ -299,7 +311,7 @@ public final class Row: CollectionType {
guard let index = impl.indexForColumn(named: columnName) else {
fatalError("no such column: \(columnName)")
}
return value(atIndex: index)
return unsafeValue(atIndex: index)
}
/// Returns the value at given column, converted to the requested type.
@@ -330,7 +342,7 @@ public final class Row: CollectionType {
guard let index = impl.indexForColumn(named: columnName) else {
fatalError("no such column: \(columnName)")
}
return value(atIndex: index)
return unsafeValue(atIndex: index)
}
/// Returns the optional `NSData` at given index.
+12 -14
View File
@@ -292,21 +292,19 @@ public final class SelectStatement : Statement {
/// The column index, case insensitive.
func indexForColumn(named name: String) -> Int? {
return lowercaseColumnIndexes[name] ?? lowercaseColumnIndexes[name.lowercaseString]
}
/// Support for indexForColumn(named:)
private lazy var lowercaseColumnIndexes: [String: Int] = {
var indexes = [String: Int]()
let count = self.columnCount
// Reverse so that we return indexes for the leftmost columns.
// SELECT 1 AS a, 2 AS a -> lowercaseColumnIndexes["a”] = 0
for (index, columnName) in self.columnNames.reverse().enumerate() {
indexes[columnName.lowercaseString] = count - index - 1
// This is faster than dictionary lookup
for (index, column) in columnNames.enumerate() {
if column == name {
return index
}
}
return indexes
}()
for (index, column) in columnNames.enumerate() {
if column.lowercaseString == name.lowercaseString {
return index
}
}
return nil
}
}
/// A sequence of elements fetched from the database.