A really difficult cleanup process

This commit is contained in:
Gwendal Roué committed 2018-08-14 08:33:06 +02:00
1 parent e49e2db571
commit d9dd293d7f
5 files changed
+77 -108

No files matched your search

+45 -7
View File
@@ -44,8 +44,7 @@ struct ValueConversionContext {
switch column {
case .columnIndex(let index):
guard let row = row else { return nil }
let rowIndex = row.index(row.startIndex, offsetBy: index)
return row[rowIndex].0
return Array(row.columnNames)[index]
case .columnName(let name):
return name
}
@@ -134,8 +133,8 @@ func fatalConversionError<T>(to: T.Type, from dbValue: DatabaseValue?, conversio
// MARK: - DatabaseValueConvertible
/// Lossless conversions from database values and rows
extension DatabaseValueConvertible {
/// Performs lossless conversion from a database value.
@inline(__always)
static func decode(from dbValue: DatabaseValue, conversionContext: @autoclosure () -> ValueConversionContext?) -> Self {
if let value = fromDatabaseValue(dbValue) {
@@ -145,7 +144,13 @@ extension DatabaseValueConvertible {
}
}
/// Performs lossless conversion from a database value.
@inline(__always)
static func decode(from row: Row, atUncheckedIndex index: Int) -> Self {
return decode(
from: row.impl.databaseValue(atUncheckedIndex: index),
conversionContext: ValueConversionContext(row).atColumn(index))
}
@inline(__always)
static func decodeIfPresent(from dbValue: DatabaseValue, conversionContext: @autoclosure () -> ValueConversionContext?) -> Self? {
// Use fromDatabaseValue before checking for null: this allows DatabaseValue to convert NULL to .null.
@@ -157,12 +162,19 @@ extension DatabaseValueConvertible {
fatalConversionError(to: Self.self, from: dbValue, conversionContext: conversionContext())
}
}
@inline(__always)
static func decodeIfPresent(from row: Row, atUncheckedIndex index: Int) -> Self? {
return decodeIfPresent(
from: row.impl.databaseValue(atUncheckedIndex: index),
conversionContext: ValueConversionContext(row).atColumn(index))
}
}
// MARK: - DatabaseValueConvertible & StatementColumnConvertible
/// Lossless conversions from database values and rows
extension DatabaseValueConvertible where Self: StatementColumnConvertible {
/// Performs lossless conversion from a statement value.
@inline(__always)
static func fastDecode(from sqliteStatement: SQLiteStatement, index: Int32, conversionContext: @autoclosure () -> ValueConversionContext?) -> Self {
if sqlite3_column_type(sqliteStatement, index) == SQLITE_NULL {
@@ -171,12 +183,38 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible {
return self.init(sqliteStatement: sqliteStatement, index: index)
}
/// Performs lossless conversion from a statement value.
@inline(__always)
static func fastDecodeIfPresent(from sqliteStatement: SQLiteStatement, index: Int32) -> Self? {
static func fastDecode(from row: Row, atUncheckedIndex index: Int) -> Self {
if let sqliteStatement = row.sqliteStatement {
return fastDecode(
from: sqliteStatement,
index: Int32(index),
conversionContext: ValueConversionContext(row).atColumn(index))
}
return row.impl.fastDecode(
Self.self,
atUncheckedIndex: index,
conversionContext: ValueConversionContext(row).atColumn(index))
}
@inline(__always)
static func fastDecodeIfPresent(from sqliteStatement: SQLiteStatement, atUncheckedIndex index: Int32) -> Self? {
if sqlite3_column_type(sqliteStatement, index) == SQLITE_NULL {
return nil
}
return self.init(sqliteStatement: sqliteStatement, index: index)
}
@inline(__always)
static func fastDecodeIfPresent(from row: Row, atUncheckedIndex index: Int) -> Self? {
if let sqliteStatement = row.sqliteStatement {
return fastDecodeIfPresent(
from: sqliteStatement,
atUncheckedIndex: Int32(index))
}
return row.impl.fastDecodeIfPresent(
Self.self,
atUncheckedIndex: index,
conversionContext: ValueConversionContext(row).atColumn(index))
}
}
+23 -94
View File
@@ -7,6 +7,8 @@ import Foundation
/// A database row.
public final class Row : Equatable, Hashable, RandomAccessCollection, ExpressibleByDictionaryLiteral, CustomStringConvertible, CustomDebugStringConvertible {
// It is not a violation of the Demeter law when another type uses this
// property, which is exposed for optimizations.
let impl: RowImpl
/// Unless we are producing a row array, we use a single row when iterating
@@ -126,7 +128,12 @@ extension Row {
///
/// This method is case-insensitive.
public func hasColumn(_ columnName: String) -> Bool {
return impl.index(ofColumn: columnName) != nil
return index(ofColumn: columnName) != nil
}
@inline(__always)
func index(ofColumn name: String) -> Int? {
return impl.index(ofColumn: name)
}
}
@@ -134,9 +141,10 @@ extension Row {
// MARK: - Extracting Values
/// Fatal errors if index is out of bounds
@inline(__always)
func index(ofColumn name: String) -> Int? {
return impl.index(ofColumn: name)
private func checkIndex(_ index: Int, file: StaticString = #file, line: UInt = #line) {
GRDBPrecondition(index >= 0 && index < count, "row index out of range", file: file, line: line)
}
/// Returns true if and only if one column contains a non-null value, or if
@@ -151,7 +159,7 @@ extension Row {
/// let row = try Row.fetchOne(db, "SELECT NULL, NULL")!
/// row.containsNonNullValue // false
public var containsNonNullValue: Bool {
for i in (0..<count) where !hasNull(atUncheckedIndex: i) {
for i in (0..<count) where !impl.hasNull(atUncheckedIndex: i) {
return true
}
@@ -171,12 +179,7 @@ extension Row {
/// in performance-critical code because it can avoid decoding database
/// values.
public func hasNull(atIndex index: Int) -> Bool {
let index = checkedIndex(index)
return hasNull(atUncheckedIndex: index)
}
@inline(__always)
func hasNull(atUncheckedIndex index: Int) -> Bool {
checkIndex(index)
return impl.hasNull(atUncheckedIndex: index)
}
@@ -186,7 +189,7 @@ extension Row {
/// Indexes span from 0 for the leftmost column to (row.count - 1) for the
/// righmost column.
public subscript(_ index: Int) -> DatabaseValueConvertible? {
let index = checkedIndex(index)
checkIndex(index)
return impl.databaseValue(atUncheckedIndex: index).storage.value
}
@@ -199,7 +202,7 @@ extension Row {
/// value is converted to the requested type `Value`. Should this conversion
/// fail, a fatal error is raised.
public subscript<Value: DatabaseValueConvertible>(_ index: Int) -> Value? {
let index = checkedIndex(index)
checkIndex(index)
return Value.decodeIfPresent(from: self, atUncheckedIndex: index)
}
@@ -216,7 +219,7 @@ extension Row {
/// StatementColumnConvertible. It *may* trigger SQLite built-in conversions
/// (see https://www.sqlite.org/datatype3.html).
public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ index: Int) -> Value? {
let index = checkedIndex(index)
checkIndex(index)
return Value.fastDecodeIfPresent(from: self, atUncheckedIndex: index)
}
@@ -228,7 +231,7 @@ extension Row {
/// This method crashes if the fetched SQLite value is NULL, or if the
/// SQLite value can not be converted to `Value`.
public subscript<Value: DatabaseValueConvertible>(_ index: Int) -> Value {
let index = checkedIndex(index)
checkIndex(index)
return Value.decode(from: self, atUncheckedIndex: index)
}
@@ -244,7 +247,7 @@ extension Row {
/// StatementColumnConvertible. It *may* trigger SQLite built-in conversions
/// (see https://www.sqlite.org/datatype3.html).
public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ index: Int) -> Value {
let index = checkedIndex(index)
checkIndex(index)
return Value.fastDecode(from: self, atUncheckedIndex: index)
}
@@ -427,7 +430,7 @@ extension Row {
/// The returned data does not owns its bytes: it must not be used longer
/// than the row's lifetime.
public func dataNoCopy(atIndex index: Int) -> Data? {
let index = checkedIndex(index)
checkIndex(index)
return impl.dataNoCopy(
atUncheckedIndex: index,
conversionContext: ValueConversionContext(self).atColumn(index))
@@ -450,7 +453,7 @@ extension Row {
}
return impl.dataNoCopy(
atUncheckedIndex: index,
conversionContext: ValueConversionContext(self).atColumn(columnName))
conversionContext: ValueConversionContext(self).atColumn(index))
}
/// Returns the optional `NSData` at given column.
@@ -469,81 +472,6 @@ extension Row {
}
}
// MARK: - Value Decoding Helpers
extension Row {
@inline(__always)
private func checkedIndex(_ index: Int, file: StaticString = #file, line: UInt = #line) -> Int {
GRDBPrecondition(index >= 0 && index < count, "row index out of range", file: file, line: line)
return index
}
}
/// Those methods should be declared on Row, and made generic on value type.
/// But we want to support "dynamic type variable" in generic methods, as in
/// FetchableRecord+Decodable.swift:
///
/// if let type = T.self as? DatabaseValueConvertible.Type {
/// let value = type.decode(from: row, atUncheckedIndex: index)
/// }
///
/// Swift compiler won't accept the `type` variable above to be used as a
/// generic argument. But it accepts the methods declared below.
extension DatabaseValueConvertible {
@inline(__always)
static func decode(from row: Row, atUncheckedIndex index: Int) -> Self {
return decode(
from: row.impl.databaseValue(atUncheckedIndex: index),
conversionContext: ValueConversionContext(row).atColumn(index))
}
@inline(__always)
static func decodeIfPresent(from row: Row, atUncheckedIndex index: Int) -> Self? {
return decodeIfPresent(
from: row.impl.databaseValue(atUncheckedIndex: index),
conversionContext: ValueConversionContext(row).atColumn(index))
}
}
/// Those methods should be declared on Row, and made generic on value type.
/// But we want to support "dynamic type variable" in generic methods, as in
/// FetchableRecord+Decodable.swift:
///
/// if let type = T.self as? (DatabaseValueConvertible & StatementColumnConvertible).Type {
/// let value = type.fastDecode(from: row, atUncheckedIndex: index)
/// }
///
/// Swift compiler won't accept the `type` variable above to be used as a
/// generic argument. But it accepts the methods declared below.
extension DatabaseValueConvertible where Self: StatementColumnConvertible {
@inline(__always)
static func fastDecode(from row: Row, atUncheckedIndex index: Int) -> Self {
if let sqliteStatement = row.sqliteStatement {
return fastDecode(
from: sqliteStatement,
index: Int32(index),
conversionContext: ValueConversionContext(row).atColumn(index))
}
return row.impl.fastDecode(
Self.self,
atUncheckedIndex: index,
conversionContext: ValueConversionContext(row).atColumn(index))
}
@inline(__always)
static func fastDecodeIfPresent(from row: Row, atUncheckedIndex index: Int) -> Self? {
if let sqliteStatement = row.sqliteStatement {
return fastDecodeIfPresent(
from: sqliteStatement,
index: Int32(index))
}
return row.impl.fastDecodeIfPresent(
Self.self,
atUncheckedIndex: index,
conversionContext: ValueConversionContext(row).atColumn(index))
}
}
extension Row {
// MARK: - Extracting DatabaseValue
@@ -1000,7 +928,8 @@ extension Row {
/// Accesses the (ColumnName, DatabaseValue) pair at given index.
public subscript(position: RowIndex) -> (String, DatabaseValue) {
let index = checkedIndex(position.index)
let index = position.index
checkIndex(index)
return (
impl.columnName(atUncheckedIndex: index),
impl.databaseValue(atUncheckedIndex: index))
@@ -1459,7 +1388,7 @@ private struct StatementRowImpl : RowImpl {
atUncheckedIndex index: Int,
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value?
{
return Value.fastDecodeIfPresent(from: sqliteStatement, index: Int32(index))
return Value.fastDecodeIfPresent(from: sqliteStatement, atUncheckedIndex: Int32(index))
}
func columnName(atUncheckedIndex index: Int) -> String {
+7 -5
View File
@@ -493,12 +493,12 @@ struct AdaptedRowImpl : RowImpl {
func hasNull(atUncheckedIndex index: Int) -> Bool {
let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index)
return base.hasNull(atUncheckedIndex: mappedIndex)
return base.impl.hasNull(atUncheckedIndex: mappedIndex)
}
func databaseValue(atUncheckedIndex index: Int) -> DatabaseValue {
let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index)
return base.impl.databaseValue(atUncheckedIndex: mappedIndex) // base.impl: Demeter violation
return base.impl.databaseValue(atUncheckedIndex: mappedIndex)
}
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(
@@ -507,7 +507,7 @@ struct AdaptedRowImpl : RowImpl {
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value
{
let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index)
return base.impl.fastDecode(Value.self, atUncheckedIndex: mappedIndex, conversionContext: conversionContext) // base.impl: Demeter violation
return Value.fastDecode(from: base, atUncheckedIndex: mappedIndex)
}
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(
@@ -516,12 +516,14 @@ struct AdaptedRowImpl : RowImpl {
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value?
{
let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index)
return base.impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: mappedIndex, conversionContext: conversionContext) // base.impl: Demeter violation
return Value.fastDecodeIfPresent(from: base, atUncheckedIndex: mappedIndex)
}
func dataNoCopy(atUncheckedIndex index:Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Data? {
let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index)
return base.impl.dataNoCopy(atUncheckedIndex: mappedIndex, conversionContext: conversionContext) // base.impl: Demeter violation
return base.impl.dataNoCopy(
atUncheckedIndex: mappedIndex,
conversionContext: conversionContext)
}
func columnName(atUncheckedIndex index: Int) -> String {
+1 -1
View File
@@ -142,7 +142,7 @@ public final class FastNullableDatabaseValueCursor<Value: DatabaseValueConvertib
done = true
return nil
case SQLITE_ROW:
return Value.fastDecodeIfPresent(from: sqliteStatement, index: columnIndex)
return Value.fastDecodeIfPresent(from: sqliteStatement, atUncheckedIndex: columnIndex)
case let code:
statement.database.selectStatementDidFail(statement)
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
+1 -1
View File
@@ -78,7 +78,7 @@ private struct RowKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainer
return type.fastDecodeIfPresent(from: row, atUncheckedIndex: index) as! T?
} else if let type = T.self as? DatabaseValueConvertible.Type {
return type.decodeIfPresent(from: row, atUncheckedIndex: index) as! T?
} else if row.hasNull(atUncheckedIndex: index) {
} else if row.impl.hasNull(atUncheckedIndex: index) {
return nil
} else {
return try T(from: RowDecoder(row: row, codingPath: codingPath + [key]))