diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index aa406be36..c93537f94 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -88,9 +88,11 @@ public final class Row: CollectionType { /// - returns: An optional *Value*. public func value(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(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>(atIndex index: Int) -> Value? { precondition(index >= 0 && index < count, "row index out of range") + return unsafeValue(atIndex: index) + } + + private func unsafeValue>(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(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(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>(atIndex index: Int) -> Value { precondition(index >= 0 && index < count, "row index out of range") + return unsafeValue(atIndex: index) + } + + private func unsafeValue>(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. diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index 8f0108ecd..94c11ed59 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -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.