From ef10e2e8edb687346662d2392f3f351fa69d25d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 11 Aug 2018 19:45:58 +0200 Subject: [PATCH] General cleanup: - rename ValueConversionDebuggingInfo to ValueConversionContext - allow missing value conversion context (when decoding a raw DatabaseValue) - rename ColumnCursor to FastDatabaseValueCursor - rename NullableColumnCursor to FastNullableDatabaseValueCursor --- CHANGELOG.md | 9 + GRDB/Core/DatabaseValue.swift | 4 +- GRDB/Core/DatabaseValueConversion.swift | 179 +++++++++--------- GRDB/Core/DatabaseValueConvertible.swift | 4 +- GRDB/Core/Row.swift | 98 ++++++---- GRDB/Core/RowAdapter.swift | 15 +- GRDB/Core/StatementColumnConvertible.swift | 38 ++-- GRDB/Record/FetchableRecord+Decodable.swift | 6 +- GRDB/Utils/Utils.swift | 1 + .../DatabasePoolReleaseMemoryTests.swift | 2 +- .../DatabaseQueueReleaseMemoryTests.swift | 2 +- .../DatabaseValueConversionErrorTests.swift | 54 +++--- ...StatementColumnConvertibleFetchTests.swift | 10 +- 13 files changed, 225 insertions(+), 197 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a23b5290..54750e395 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,15 @@ Release Notes + @available(*, deprecated) func losslessConvert(sql: String? = nil, arguments: StatementArguments? = nil) -> T? } + ++final class FastDatabaseValueCursor : Cursor { } ++@available(*, deprecated, renamed: "FastDatabaseValueCursor") ++typealias ColumnCursor = FastDatabaseValueCursor + ++final class FastNullableDatabaseValueCursor : Cursor { } ++@available(*, deprecated, renamed: "FastNullableDatabaseValueCursor") ++typealias NullableColumnCursor = FastNullableDatabaseValueCursor + ``` ### Documentation Diff diff --git a/GRDB/Core/DatabaseValue.swift b/GRDB/Core/DatabaseValue.swift index 6e724ea0c..8b682e523 100644 --- a/GRDB/Core/DatabaseValue.swift +++ b/GRDB/Core/DatabaseValue.swift @@ -227,7 +227,7 @@ extension DatabaseValue { /// conversion error @available(*, deprecated) public func losslessConvert(sql: String? = nil, arguments: StatementArguments? = nil) -> T where T : DatabaseValueConvertible { - return T.decode(from: self, debugInfo: ValueConversionDebuggingInfo()) + return T.decode(from: self, conversionContext: nil) } /// Converts the database value to the type Optional. @@ -251,7 +251,7 @@ extension DatabaseValue { /// conversion error @available(*, deprecated) public func losslessConvert(sql: String? = nil, arguments: StatementArguments? = nil) -> T? where T : DatabaseValueConvertible { - return T.decodeIfPresent(from: self, debugInfo: ValueConversionDebuggingInfo()) + return T.decodeIfPresent(from: self, conversionContext: nil) } } diff --git a/GRDB/Core/DatabaseValueConversion.swift b/GRDB/Core/DatabaseValueConversion.swift index f474c7372..ebc9b6334 100644 --- a/GRDB/Core/DatabaseValueConversion.swift +++ b/GRDB/Core/DatabaseValueConversion.swift @@ -4,61 +4,29 @@ import SQLite3 #endif -// All primitive value conversion methods. +// MARK: - Conversion Context and Errors /// A type that helps the user understanding value conversion errors -struct ValueConversionDebuggingInfo { - enum Source { - case statement(SelectStatement) - case sql(String, StatementArguments) - case row(Row) - } - enum Column { +struct ValueConversionContext { + private enum Column { case columnIndex(Int) case columnName(String) } - private var source: Source? + var row: Row? + var sql: String? + var arguments: StatementArguments? private var column: Column? - init(_ source: Source? = nil, _ column: Column? = nil) { - self.source = source - self.column = column + func atColumn(_ columnIndex: Int) -> ValueConversionContext { + var result = self + result.column = .columnIndex(columnIndex) + return result } - var sql: String? { - guard let source = source else { return nil } - switch source { - case .statement(let statement): - return statement.sql - case .row(let row): - return row.statement?.sql - case .sql(let sql, _): - return sql - } - } - - var arguments: StatementArguments? { - guard let source = source else { return nil } - switch source { - case .statement(let statement): - return statement.arguments - case .row(let row): - return row.statement?.arguments - case .sql(_, let arguments): - return arguments - } - } - - var row: Row? { - guard let source = source else { return nil } - switch source { - case .statement(let statement): - return Row(statement: statement) - case .row(let row): - return row - case .sql: - return nil - } + func atColumn(_ columnName: String) -> ValueConversionContext { + var result = self + result.column = .columnName(columnName) + return result } var columnIndex: Int? { @@ -84,35 +52,62 @@ struct ValueConversionDebuggingInfo { } } +extension ValueConversionContext { + init(_ statement: SelectStatement) { + self.init( + row: Row(statement: statement).copy(), + sql: statement.sql, + arguments: statement.arguments, + column: nil) + } + + init(_ row: Row) { + if let statement = row.statement { + self.init( + row: row.copy(), + sql: statement.sql, + arguments: statement.arguments, + column: nil) + } else { + self.init( + row: row.copy(), + sql: nil, + arguments: nil, + column: nil) + } + } + +} + /// The canonical conversion error message /// /// - parameter dbValue: nil means "missing column" -func conversionErrorMessage(to: T.Type, from dbValue: DatabaseValue?, debugInfo: ValueConversionDebuggingInfo) -> String { +func conversionErrorMessage(to: T.Type, from dbValue: DatabaseValue?, conversionContext: ValueConversionContext?) -> String { var message: String var extras: [String] = [] if let dbValue = dbValue { message = "could not convert database value \(dbValue) to \(T.self)" - if let columnName = debugInfo.columnName { + if let columnName = conversionContext?.columnName { extras.append("column: `\(columnName)`") } - if let columnIndex = debugInfo.columnIndex { + if let columnIndex = conversionContext?.columnIndex { extras.append("column index: \(columnIndex)") } } else { message = "could not read \(T.self) from missing column" - if let columnName = debugInfo.columnName { + if let columnName = conversionContext?.columnName { message += " `\(columnName)`" } } - if let row = debugInfo.row { + if let row = conversionContext?.row { extras.append("row: \(row)") } - if let sql = debugInfo.sql { + if let sql = conversionContext?.sql { extras.append("statement: `\(sql)`") - if let arguments = debugInfo.arguments, arguments.isEmpty == false { + if let arguments = conversionContext?.arguments, arguments.isEmpty == false { extras.append("arguments: \(arguments)") } } @@ -125,48 +120,73 @@ func conversionErrorMessage(to: T.Type, from dbValue: DatabaseValue?, debugIn /// The canonical conversion fatal error /// -/// - parameter dbValue: nil means "missing column" -func fatalConversionError(to: T.Type, from dbValue: DatabaseValue?, debugInfo: ValueConversionDebuggingInfo, file: StaticString = #file, line: UInt = #line) -> Never { - fatalError(conversionErrorMessage(to: T.self, from: dbValue, debugInfo: debugInfo), file: file, line: line) +/// - parameter dbValue: nil means "missing column", for consistency with (row["missing"] as DatabaseValue? == nil) +func fatalConversionError(to: T.Type, from dbValue: DatabaseValue?, conversionContext: ValueConversionContext?, file: StaticString = #file, line: UInt = #line) -> Never { + fatalError(conversionErrorMessage(to: T.self, from: dbValue, conversionContext: conversionContext), file: file, line: line) } +// MARK: - DatabaseValueConvertible + extension DatabaseValueConvertible { /// Performs lossless conversion from a database value. - static func decode(from dbValue: DatabaseValue, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Self { + @inline(__always) + static func decode(from dbValue: DatabaseValue, conversionContext: @autoclosure () -> ValueConversionContext?) -> Self { if let value = fromDatabaseValue(dbValue) { return value } else { - fatalConversionError(to: Self.self, from: dbValue, debugInfo: debugInfo()) + fatalConversionError(to: Self.self, from: dbValue, conversionContext: conversionContext()) } } /// Performs lossless conversion from a database value. - static func decodeIfPresent(from dbValue: DatabaseValue, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Self? { + @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. if let value = fromDatabaseValue(dbValue) { return value } else if dbValue.isNull { return nil } else { - fatalConversionError(to: Self.self, from: dbValue, debugInfo: debugInfo()) + fatalConversionError(to: Self.self, from: dbValue, conversionContext: conversionContext()) } } } -extension DatabaseValueConvertible where Self: StatementColumnConvertible { +extension Row { + @inline(__always) + func decodeIfPresent( + _ type: Value.Type, + atUncheckedIndex index: Int, + conversionContext: @autoclosure () -> ValueConversionContext?) -> Value? + { + return Value.decodeIfPresent(from: impl.databaseValue(atUncheckedIndex: index), conversionContext: conversionContext) + } + @inline(__always) + func decode( + _ type: Value.Type, + atUncheckedIndex index: Int, + conversionContext: @autoclosure () -> ValueConversionContext?) -> Value + { + return Value.decode(from: impl.databaseValue(atUncheckedIndex: index), conversionContext: conversionContext) + } +} + +// MARK: - DatabaseValueConvertible & StatementColumnConvertible + +extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// Performs lossless conversion from a statement value. @inline(__always) - static func decode(from sqliteStatement: SQLiteStatement, index: Int32, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Self { + static func fastDecode(from sqliteStatement: SQLiteStatement, index: Int32, conversionContext: @autoclosure () -> ValueConversionContext?) -> Self { if sqlite3_column_type(sqliteStatement, index) == SQLITE_NULL { - fatalConversionError(to: Self.self, from: .null, debugInfo: debugInfo()) + fatalConversionError(to: Self.self, from: .null, conversionContext: conversionContext()) } return self.init(sqliteStatement: sqliteStatement, index: index) } /// Performs lossless conversion from a statement value. @inline(__always) - static func decodeIfPresent(from sqliteStatement: SQLiteStatement, index: Int32) -> Self? { + static func fastDecodeIfPresent(from sqliteStatement: SQLiteStatement, index: Int32) -> Self? { if sqlite3_column_type(sqliteStatement, index) == SQLITE_NULL { return nil } @@ -175,46 +195,27 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { } extension Row { - - @inline(__always) - func decodeIfPresent( - _ type: Value.Type, - atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value? - { - return Value.decodeIfPresent(from: impl.databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) - } - - @inline(__always) - func decode( - _ type: Value.Type, - atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value - { - return Value.decode(from: impl.databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) - } - @inline(__always) func fastDecodeIfPresent( _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value? + conversionContext: @autoclosure () -> ValueConversionContext?) -> Value? { if let sqliteStatement = sqliteStatement { - return Value.decodeIfPresent(from: sqliteStatement, index: Int32(index)) + return Value.fastDecodeIfPresent(from: sqliteStatement, index: Int32(index)) } - return impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: index, debugInfo: debugInfo) + return impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: index, conversionContext: conversionContext) } @inline(__always) func fastDecode( _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value + conversionContext: @autoclosure () -> ValueConversionContext?) -> Value { if let sqliteStatement = sqliteStatement { - return Value.decode(from: sqliteStatement, index: Int32(index), debugInfo: debugInfo) + return Value.fastDecode(from: sqliteStatement, index: Int32(index), conversionContext: conversionContext) } - return impl.fastDecode(Value.self, atUncheckedIndex: index, debugInfo: debugInfo) + return impl.fastDecode(Value.self, atUncheckedIndex: index, conversionContext: conversionContext) } } diff --git a/GRDB/Core/DatabaseValueConvertible.swift b/GRDB/Core/DatabaseValueConvertible.swift index e35df1e4d..e1ab8a554 100644 --- a/GRDB/Core/DatabaseValueConvertible.swift +++ b/GRDB/Core/DatabaseValueConvertible.swift @@ -72,7 +72,7 @@ public final class DatabaseValueCursor : Cursor let dbValue = DatabaseValue(sqliteStatement: sqliteStatement, index: columnIndex) return Value.decode( from: dbValue, - debugInfo: ValueConversionDebuggingInfo(.statement(statement), .columnIndex(Int(columnIndex)))) + conversionContext: ValueConversionContext(statement).atColumn(Int(columnIndex))) case let code: statement.database.selectStatementDidFail(statement) throw DatabaseError( @@ -118,7 +118,7 @@ public final class NullableDatabaseValueCursor let dbValue = DatabaseValue(sqliteStatement: sqliteStatement, index: columnIndex) return Value.decodeIfPresent( from: dbValue, - debugInfo: ValueConversionDebuggingInfo(.statement(statement), .columnIndex(Int(columnIndex)))) + conversionContext: ValueConversionContext(statement).atColumn(Int(columnIndex))) case let code: statement.database.selectStatementDidFail(statement) throw DatabaseError( diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 9a51a62a5..e2f740b81 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -171,7 +171,7 @@ extension Row { /// in performance-critical code because it can avoid decoding database /// values. public func hasNull(atIndex index: Int) -> Bool { - GRDBPrecondition(index >= 0 && index < count, "row index out of range") + let index = checkedIndex(index) return impl.hasNull(atUncheckedIndex: index) } @@ -181,7 +181,7 @@ extension Row { /// Indexes span from 0 for the leftmost column to (row.count - 1) for the /// righmost column. public subscript(_ index: Int) -> DatabaseValueConvertible? { - GRDBPrecondition(index >= 0 && index < count, "row index out of range") + let index = checkedIndex(index) return impl.databaseValue(atUncheckedIndex: index).storage.value } @@ -194,11 +194,11 @@ extension Row { /// value is converted to the requested type `Value`. Should this conversion /// fail, a fatal error is raised. public subscript(_ index: Int) -> Value? { - GRDBPrecondition(index >= 0 && index < count, "row index out of range") + let index = checkedIndex(index) return decodeIfPresent( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(.row(self), .columnIndex(index))) + conversionContext: ValueConversionContext(self).atColumn(index)) } /// Returns the value at given index, converted to the requested type. @@ -214,11 +214,11 @@ extension Row { /// StatementColumnConvertible. It *may* trigger SQLite built-in conversions /// (see https://www.sqlite.org/datatype3.html). public subscript(_ index: Int) -> Value? { - GRDBPrecondition(index >= 0 && index < count, "row index out of range") + let index = checkedIndex(index) return fastDecodeIfPresent( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(.row(self), .columnIndex(index))) + conversionContext: ValueConversionContext(self).atColumn(index)) } /// Returns the value at given index, converted to the requested type. @@ -229,11 +229,11 @@ 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(_ index: Int) -> Value { - GRDBPrecondition(index >= 0 && index < count, "row index out of range") + let index = checkedIndex(index) return decode( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(.row(self), .columnIndex(index))) + conversionContext: ValueConversionContext(self).atColumn(index)) } /// Returns the value at given index, converted to the requested type. @@ -248,11 +248,11 @@ extension Row { /// StatementColumnConvertible. It *may* trigger SQLite built-in conversions /// (see https://www.sqlite.org/datatype3.html). public subscript(_ index: Int) -> Value { - GRDBPrecondition(index >= 0 && index < count, "row index out of range") + let index = checkedIndex(index) return fastDecode( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(.row(self), .columnIndex(index))) + conversionContext: ValueConversionContext(self).atColumn(index)) } /// Returns Int64, Double, String, Data or nil, depending on the value @@ -291,7 +291,7 @@ extension Row { return decodeIfPresent( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(.row(self), .columnName(columnName))) + conversionContext: ValueConversionContext(self).atColumn(columnName)) } /// Returns the value at given column, converted to the requested type. @@ -313,7 +313,7 @@ extension Row { return fastDecodeIfPresent( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(.row(self), .columnName(columnName))) + conversionContext: ValueConversionContext(self).atColumn(columnName)) } /// Returns the value at given column, converted to the requested type. @@ -331,12 +331,12 @@ extension Row { fatalConversionError( to: Value.self, from: nil, - debugInfo: ValueConversionDebuggingInfo(.row(self), .columnName(columnName))) + conversionContext: ValueConversionContext(self).atColumn(columnName)) } return decode( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(.row(self), .columnName(columnName))) + conversionContext: ValueConversionContext(self).atColumn(columnName)) } /// Returns the value at given column, converted to the requested type. @@ -358,12 +358,12 @@ extension Row { fatalConversionError( to: Value.self, from: nil, - debugInfo: ValueConversionDebuggingInfo(.row(self), .columnName(columnName))) + conversionContext: ValueConversionContext(self).atColumn(columnName)) } return fastDecode( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(.row(self), .columnName(columnName))) + conversionContext: ValueConversionContext(self).atColumn(columnName)) } /// Returns Int64, Double, String, NSData or nil, depending on the value @@ -446,10 +446,10 @@ 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? { - GRDBPrecondition(index >= 0 && index < count, "row index out of range") + let index = checkedIndex(index) return impl.dataNoCopy( atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(.row(self), .columnIndex(index))) + conversionContext: ValueConversionContext(self).atColumn(index)) } /// Returns the optional Data at given column. @@ -469,7 +469,7 @@ extension Row { } return impl.dataNoCopy( atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(.row(self), .columnName(columnName))) + conversionContext: ValueConversionContext(self).atColumn(columnName)) } /// Returns the optional `NSData` at given column. @@ -486,6 +486,12 @@ extension Row { public func dataNoCopy(_ column: ColumnExpression) -> Data? { return dataNoCopy(named: column.name) } + + @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 + } } extension Row { @@ -940,8 +946,7 @@ extension Row { /// Accesses the (ColumnName, DatabaseValue) pair at given index. public subscript(position: RowIndex) -> (String, DatabaseValue) { - let index = position.index - GRDBPrecondition(index >= 0 && index < count, "row index out of range") + let index = checkedIndex(position.index) return ( impl.columnName(atUncheckedIndex: index), impl.databaseValue(atUncheckedIndex: index)) @@ -1211,9 +1216,9 @@ protocol RowImpl { func columnName(atUncheckedIndex index: Int) -> String func hasNull(atUncheckedIndex index:Int) -> Bool func databaseValue(atUncheckedIndex index: Int) -> DatabaseValue - func fastDecode(_ type: Value.Type, atUncheckedIndex index: Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value - func fastDecodeIfPresent(_ type: Value.Type, atUncheckedIndex index: Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value? - func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data? + func fastDecode(_ type: Value.Type, atUncheckedIndex index: Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Value + func fastDecodeIfPresent(_ type: Value.Type, atUncheckedIndex index: Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Value? + func dataNoCopy(atUncheckedIndex index:Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Data? /// Returns the index of the leftmost column that matches *name* (case-insensitive) func index(ofColumn name: String) -> Int? @@ -1226,49 +1231,55 @@ protocol RowImpl { extension RowImpl { func copiedRow(_ row: Row) -> Row { + // unless customized, assume immutable row (see StatementRowImpl and AdaptedRowImpl for customization) return row } func unscopedRow(_ row: Row) -> Row { + // unless customized, assume unadapted row (see AdaptedRowImpl for customization) return row } func unadaptedRow(_ row: Row) -> Row { + // unless customized, assume unadapted row (see AdaptedRowImpl for customization) return row } var scopes: Row.ScopesView { + // unless customized, assume unuscoped row (see AdaptedRowImpl for customization) return Row.ScopesView() } func hasNull(atUncheckedIndex index:Int) -> Bool { + // unless customized, use slow check (see StatementRowImpl and AdaptedRowImpl for customization) return databaseValue(atUncheckedIndex: index).isNull } func fastDecode( _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value + conversionContext: @autoclosure () -> ValueConversionContext?) -> Value { - // default implementation is slow - return Value.decode(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) + // unless customized, use slow decoding (see StatementRowImpl and AdaptedRowImpl for customization) + return Value.decode(from: databaseValue(atUncheckedIndex: index), conversionContext: conversionContext) } func fastDecodeIfPresent( _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value? + conversionContext: @autoclosure () -> ValueConversionContext?) -> Value? { - // default implementation is slow - return Value.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) + // unless customized, use slow decoding (see StatementRowImpl and AdaptedRowImpl for customization) + return Value.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), conversionContext: conversionContext) } - func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data? { - // default implementation copies - return Data.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) + func dataNoCopy(atUncheckedIndex index:Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Data? { + // unless customized, copy data (see StatementRowImpl and AdaptedRowImpl for customization) + return Data.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), conversionContext: conversionContext) } } +// TODO: merge with StatementCopyRowImpl eventually? /// See Row.init(dictionary:) private struct ArrayRowImpl : RowImpl { let columns: [(String, DatabaseValue)] @@ -1300,6 +1311,7 @@ private struct ArrayRowImpl : RowImpl { } +// TODO: merge with ArrayRowImpl eventually? /// See Row.init(copiedFromStatementRef:sqliteStatement:) private struct StatementCopyRowImpl : RowImpl { let dbValues: ContiguousArray @@ -1343,7 +1355,7 @@ private struct StatementRowImpl : RowImpl { init(sqliteStatement: SQLiteStatement, statementRef: Unmanaged) { self.statementRef = statementRef self.sqliteStatement = sqliteStatement - // Optimize row["..."] + // Optimize row[columnName] let lowercaseColumnNames = (0.. Bool { - // Avoid extracting values, because this modifies the statement. + // Avoid extracting values, because this modifies the SQLite statement. return sqlite3_column_type(sqliteStatement, Int32(index)) == SQLITE_NULL } - func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data? { + func dataNoCopy(atUncheckedIndex index:Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Data? { guard sqlite3_column_type(sqliteStatement, Int32(index)) != SQLITE_NULL else { return nil } @@ -1380,18 +1392,20 @@ private struct StatementRowImpl : RowImpl { return DatabaseValue(sqliteStatement: sqliteStatement, index: Int32(index)) } - func fastValue( + func fastDecode( + _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value + conversionContext: @autoclosure () -> ValueConversionContext?) -> Value { - return Value.decode(from: sqliteStatement, index: Int32(index), debugInfo: debugInfo) + return Value.fastDecode(from: sqliteStatement, index: Int32(index), conversionContext: conversionContext) } - func fastOptionalValue( + func fastDecodeIfPresent( + _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value? + conversionContext: @autoclosure () -> ValueConversionContext?) -> Value? { - return Value.decodeIfPresent(from: sqliteStatement, index: Int32(index)) + return Value.fastDecodeIfPresent(from: sqliteStatement, index: Int32(index)) } func columnName(atUncheckedIndex index: Int) -> String { diff --git a/GRDB/Core/RowAdapter.swift b/GRDB/Core/RowAdapter.swift index 6cdf94aff..25c2ddc19 100644 --- a/GRDB/Core/RowAdapter.swift +++ b/GRDB/Core/RowAdapter.swift @@ -504,27 +504,24 @@ struct AdaptedRowImpl : RowImpl { func fastDecode( _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value + conversionContext: @autoclosure () -> ValueConversionContext?) -> Value { let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index) - return base.impl.fastDecode(Value.self, atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation + return base.impl.fastDecode(Value.self, atUncheckedIndex: mappedIndex, conversionContext: conversionContext) // base.impl: Demeter violation } func fastDecodeIfPresent( _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value? + conversionContext: @autoclosure () -> ValueConversionContext?) -> Value? { let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index) - return base.impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation + return base.impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: mappedIndex, conversionContext: conversionContext) // base.impl: Demeter violation } - func dataNoCopy( - atUncheckedIndex index:Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data? - { + func dataNoCopy(atUncheckedIndex index:Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Data? { let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index) - return base.impl.dataNoCopy(atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation + return base.impl.dataNoCopy(atUncheckedIndex: mappedIndex, conversionContext: conversionContext) // base.impl: Demeter violation } func columnName(atUncheckedIndex index: Int) -> String { diff --git a/GRDB/Core/StatementColumnConvertible.swift b/GRDB/Core/StatementColumnConvertible.swift index bdf5af0f4..2ec7b052b 100644 --- a/GRDB/Core/StatementColumnConvertible.swift +++ b/GRDB/Core/StatementColumnConvertible.swift @@ -59,7 +59,7 @@ public protocol StatementColumnConvertible { /// print(name) /// } /// } -public final class ColumnCursor : Cursor { +public final class FastDatabaseValueCursor : Cursor { private let statement: SelectStatement private let sqliteStatement: SQLiteStatement private let columnIndex: Int32 @@ -81,10 +81,10 @@ public final class ColumnCursor = FastDatabaseValueCursor + /// A cursor of optional database values extracted from a single column. /// For example: /// @@ -101,7 +104,7 @@ public final class ColumnCursor") /// } /// } -public final class NullableColumnCursor : Cursor { +public final class FastNullableDatabaseValueCursor : Cursor { private let statement: SelectStatement private let sqliteStatement: SQLiteStatement private let columnIndex: Int32 @@ -123,7 +126,7 @@ public final class NullableColumnCursor = FastNullableDatabaseValueCursor + /// Types that adopt both DatabaseValueConvertible and /// StatementColumnConvertible can be efficiently initialized from /// database values. @@ -160,8 +166,8 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> ColumnCursor { - return try ColumnCursor(statement: statement, arguments: arguments, adapter: adapter) + public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> FastDatabaseValueCursor { + return try FastDatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter) } /// Returns an array of values fetched from a prepared statement. @@ -192,7 +198,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Self? { // fetchOne returns nil if there is no row, or if there is a row with a null value - let cursor = try NullableColumnCursor(statement: statement, arguments: arguments, adapter: adapter) + let cursor = try FastNullableDatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter) return try cursor.next() ?? nil } } @@ -220,7 +226,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> ColumnCursor { + public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> FastDatabaseValueCursor { return try fetchCursor(db, SQLRequest(sql, arguments: arguments, adapter: adapter)) } @@ -277,7 +283,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// - request: A FetchRequest. /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - public static func fetchCursor(_ db: Database, _ request: R) throws -> ColumnCursor { + public static func fetchCursor(_ db: Database, _ request: R) throws -> FastDatabaseValueCursor { let (statement, adapter) = try request.prepare(db) return try fetchCursor(statement, adapter: adapter) } @@ -333,7 +339,7 @@ extension FetchRequest where RowDecoder: DatabaseValueConvertible & StatementCol /// - parameter db: A database connection. /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - public func fetchCursor(_ db: Database) throws -> ColumnCursor { + public func fetchCursor(_ db: Database) throws -> FastDatabaseValueCursor { return try RowDecoder.fetchCursor(db, self) } @@ -399,8 +405,8 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> NullableColumnCursor { - return try NullableColumnCursor(statement: statement, arguments: arguments, adapter: adapter) + public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> FastNullableDatabaseValueCursor { + return try FastNullableDatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter) } /// Returns an array of optional values fetched from a prepared statement. @@ -442,7 +448,7 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> NullableColumnCursor { + public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> FastNullableDatabaseValueCursor { return try fetchCursor(db, SQLRequest(sql, arguments: arguments, adapter: adapter)) } @@ -484,7 +490,7 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv /// - request: A FetchRequest. /// - returns: A cursor over fetched optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - public static func fetchCursor(_ db: Database, _ request: R) throws -> NullableColumnCursor { + public static func fetchCursor(_ db: Database, _ request: R) throws -> FastNullableDatabaseValueCursor { let (statement, adapter) = try request.prepare(db) return try fetchCursor(statement, adapter: adapter) } @@ -525,7 +531,7 @@ extension FetchRequest where RowDecoder: _OptionalProtocol, RowDecoder._Wrapped: /// - parameter db: A database connection. /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - public func fetchCursor(_ db: Database) throws -> NullableColumnCursor { + public func fetchCursor(_ db: Database) throws -> FastNullableDatabaseValueCursor { return try Optional.fetchCursor(db, self) } diff --git a/GRDB/Record/FetchableRecord+Decodable.swift b/GRDB/Record/FetchableRecord+Decodable.swift index a535053cb..4394165c3 100644 --- a/GRDB/Record/FetchableRecord+Decodable.swift +++ b/GRDB/Record/FetchableRecord+Decodable.swift @@ -75,7 +75,7 @@ private struct RowKeyedDecodingContainer: KeyedDecodingContainer if let type = T.self as? DatabaseValueConvertible.Type { // Prefer DatabaseValueConvertible decoding over Decodable. // This allows decoding Date from String, or DatabaseValue from NULL. - return type.decodeIfPresent(from: dbValue, debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName(key.stringValue))) as! T? + return type.decodeIfPresent(from: dbValue, conversionContext: ValueConversionContext(row).atColumn(key.stringValue)) as! T? } else if dbValue.isNull { return nil } else { @@ -114,7 +114,7 @@ private struct RowKeyedDecodingContainer: KeyedDecodingContainer if let type = T.self as? DatabaseValueConvertible.Type { // Prefer DatabaseValueConvertible decoding over Decodable. // This allows decoding Date from String, or DatabaseValue from NULL. - return type.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName(key.stringValue))) as! T + return type.decode(from: dbValue, conversionContext: ValueConversionContext(row).atColumn(key.stringValue)) as! T } else { return try T(from: RowDecoder(row: row, codingPath: codingPath + [key])) } @@ -227,7 +227,7 @@ private struct RowSingleValueDecodingContainer: SingleValueDecodingContainer { if let type = T.self as? DatabaseValueConvertible.Type { // Prefer DatabaseValueConvertible decoding over Decodable. // This allows decoding Date from String, or DatabaseValue from NULL. - return type.decode(from: row[column.stringValue], debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName(column.stringValue))) as! T + return type.decode(from: row[column.stringValue], conversionContext: ValueConversionContext(row).atColumn(column.stringValue)) as! T } else { return try T(from: RowDecoder(row: row, codingPath: [column])) } diff --git a/GRDB/Utils/Utils.swift b/GRDB/Utils/Utils.swift index 8766e852c..08d085da8 100644 --- a/GRDB/Utils/Utils.swift +++ b/GRDB/Utils/Utils.swift @@ -46,6 +46,7 @@ extension Optional : _OptionalProtocol { // MARK: - Internal /// Reserved for GRDB: do not use. +@inline(__always) func GRDBPrecondition(_ condition: @autoclosure() -> Bool, _ message: @autoclosure() -> String = "", file: StaticString = #file, line: UInt = #line) { /// Custom precondition function which aims at solving /// https://bugs.swift.org/browse/SR-905 and diff --git a/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift b/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift index c9ada40da..963269a3d 100644 --- a/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift +++ b/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift @@ -205,7 +205,7 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { } } - var cursor: ColumnCursor? = nil + var cursor: FastDatabaseValueCursor? = nil do { try! makeDatabasePool().write { db in try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") diff --git a/Tests/GRDBTests/DatabaseQueueReleaseMemoryTests.swift b/Tests/GRDBTests/DatabaseQueueReleaseMemoryTests.swift index 70b8066f5..7f2426350 100644 --- a/Tests/GRDBTests/DatabaseQueueReleaseMemoryTests.swift +++ b/Tests/GRDBTests/DatabaseQueueReleaseMemoryTests.swift @@ -120,7 +120,7 @@ class DatabaseQueueReleaseMemoryTests: GRDBTestCase { } } - var cursor: ColumnCursor? = nil + var cursor: FastDatabaseValueCursor? = nil do { try! makeDatabaseQueue().inDatabase { db in try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") diff --git a/Tests/GRDBTests/DatabaseValueConversionErrorTests.swift b/Tests/GRDBTests/DatabaseValueConversionErrorTests.swift index 077249c7c..8a7d91a0b 100644 --- a/Tests/GRDBTests/DatabaseValueConversionErrorTests.swift +++ b/Tests/GRDBTests/DatabaseValueConversionErrorTests.swift @@ -32,7 +32,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: String.self, from: row["name"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))), + conversionContext: ValueConversionContext(row).atColumn("name")), "could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL])") // _ = try Record.fetchOne(statement) @@ -41,7 +41,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: String.self, from: row["name"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))), + conversionContext: ValueConversionContext(row).atColumn("name")), "could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL], statement: `SELECT ? AS name`, arguments: [NULL])") } } @@ -57,7 +57,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: String.self, from: row["name"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))), + conversionContext: ValueConversionContext(row).atColumn("name")), "could not read String from missing column `name` (row: [unused:\"ignored\"])") // _ = try Record.fetchOne(statement) @@ -66,7 +66,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: String.self, from: row["name"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))), + conversionContext: ValueConversionContext(row).atColumn("name")), "could not read String from missing column `name` (row: [unused:\"ignored\"], statement: `SELECT ? AS unused`, arguments: [\"ignored\"])") } } @@ -98,7 +98,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: row["value"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))), + conversionContext: ValueConversionContext(row).atColumn("value")), "could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [1:1 value:\"invalid\"])") // _ = try Record.fetchOne(statement) @@ -107,7 +107,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: row["value"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))), + conversionContext: ValueConversionContext(row).atColumn("value")), "could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [1:1 value:\"invalid\"], statement: `SELECT 1, ? AS value`, arguments: [\"invalid\"])") } } @@ -123,7 +123,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: row["value"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))), + conversionContext: ValueConversionContext(row).atColumn("value")), "could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"])") // _ = try Record.fetchOne(statement) @@ -132,7 +132,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: row["value"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))), + conversionContext: ValueConversionContext(row).atColumn("value")), "could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"], statement: `SELECT ? AS unused`, arguments: [\"ignored\"])") } } @@ -157,7 +157,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: String.self, from: row["name"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))), + conversionContext: ValueConversionContext(row).atColumn("name")), "could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])") // _ = try Record.fetchOne(statement) @@ -166,7 +166,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: String.self, from: row["name"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))), + conversionContext: ValueConversionContext(row).atColumn("name")), "could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"], statement: `SELECT NULL AS name, ? AS team`, arguments: [\"invalid\"])") } } @@ -182,7 +182,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: String.self, from: row["name"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))), + conversionContext: ValueConversionContext(row).atColumn("name")), "could not read String from missing column `name` (row: [unused:\"ignored\"])") // _ = try Record.fetchOne(statement) @@ -191,7 +191,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: String.self, from: row["name"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))), + conversionContext: ValueConversionContext(row).atColumn("name")), "could not read String from missing column `name` (row: [unused:\"ignored\"], statement: `SELECT ? AS unused`, arguments: [\"ignored\"])") } } @@ -219,7 +219,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: row["value"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))), + conversionContext: ValueConversionContext(row).atColumn("value")), "could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [name:NULL value:\"invalid\"])") // _ = try Record.fetchOne(statement) @@ -228,7 +228,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: row["value"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))), + conversionContext: ValueConversionContext(row).atColumn("value")), "could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [name:NULL value:\"invalid\"], statement: `SELECT NULL AS name, ? AS value`, arguments: [\"invalid\"])") } } @@ -244,7 +244,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: row["value"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))), + conversionContext: ValueConversionContext(row).atColumn("value")), "could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"])") // _ = try Record.fetchOne(statement) @@ -253,7 +253,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: row["value"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))), + conversionContext: ValueConversionContext(row).atColumn("value")), "could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"], statement: `SELECT ? AS unused`, arguments: [\"ignored\"])") } } @@ -281,7 +281,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: row["value"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))), + conversionContext: ValueConversionContext(row).atColumn("value")), "could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [name:NULL value:\"invalid\"])") // _ = try Record.fetchOne(statement) @@ -290,7 +290,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: row["value"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))), + conversionContext: ValueConversionContext(row).atColumn("value")), "could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [name:NULL value:\"invalid\"], statement: `SELECT NULL AS name, ? AS value`, arguments: [\"invalid\"])") } } @@ -306,7 +306,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: row["value"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))), + conversionContext: ValueConversionContext(row).atColumn("value")), "could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"])") // _ = try Record.fetchOne(statement) @@ -315,7 +315,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: row["value"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))), + conversionContext: ValueConversionContext(row).atColumn("value")), "could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"], statement: `SELECT ? AS unused`, arguments: [\"ignored\"])") } } @@ -336,7 +336,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: String.self, from: .null, - debugInfo: ValueConversionDebuggingInfo(.statement(statement), .columnIndex(0))), + conversionContext: ValueConversionContext(statement).atColumn(0)), "could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"], statement: `SELECT NULL AS name, ? AS team`, arguments: [\"invalid\"])") } @@ -345,7 +345,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: String.self, from: row["name"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))), + conversionContext: ValueConversionContext(row).atColumn("name")), "could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])") // _ = row[0] as String @@ -353,7 +353,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: String.self, from: row[0], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnIndex(0))), + conversionContext: ValueConversionContext(row).atColumn(0)), "could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])") } } @@ -377,7 +377,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: DatabaseValue(sqliteStatement: statement.sqliteStatement, index: 0), - debugInfo: ValueConversionDebuggingInfo(.statement(statement), .columnIndex(0))), + conversionContext: ValueConversionContext(statement).atColumn(0)), "could not convert database value NULL to \(Value.self) (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"], statement: `SELECT NULL AS name, ? AS team`, arguments: [\"invalid\"])") } @@ -389,7 +389,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: DatabaseValue(sqliteStatement: statement.sqliteStatement, index: Int32(columnIndex)), - debugInfo: ValueConversionDebuggingInfo(.statement(statement), .columnIndex(columnIndex))), + conversionContext: ValueConversionContext(statement).atColumn(columnIndex)), "could not convert database value \"invalid\" to \(Value.self) (column: `team`, column index: 1, row: [name:NULL team:\"invalid\"], statement: `SELECT NULL AS name, ? AS team`, arguments: [\"invalid\"])") } @@ -398,7 +398,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: row["name"], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))), + conversionContext: ValueConversionContext(row).atColumn("name")), "could not convert database value NULL to \(Value.self) (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])") // _ = row[0] as Value @@ -406,7 +406,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase { conversionErrorMessage( to: Value.self, from: row[0], - debugInfo: ValueConversionDebuggingInfo(.row(row), .columnIndex(0))), + conversionContext: ValueConversionContext(row).atColumn(0)), "could not convert database value NULL to \(Value.self) (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])") } } diff --git a/Tests/GRDBTests/StatementColumnConvertibleFetchTests.swift b/Tests/GRDBTests/StatementColumnConvertibleFetchTests.swift index 97121e429..7a4f80ff0 100644 --- a/Tests/GRDBTests/StatementColumnConvertibleFetchTests.swift +++ b/Tests/GRDBTests/StatementColumnConvertibleFetchTests.swift @@ -86,7 +86,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { func testFetchCursor() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in - func test(_ cursor: ColumnCursor) throws { + func test(_ cursor: FastDatabaseValueCursor) throws { var i = try cursor.next()! XCTAssertEqual(i.int, 1) XCTAssertTrue(i.fast) @@ -120,7 +120,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { let customError = NSError(domain: "Custom", code: 0xDEAD) dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError }) try dbQueue.inDatabase { db in - func test(_ cursor: ColumnCursor, sql: String) throws { + func test(_ cursor: FastDatabaseValueCursor, sql: String) throws { do { _ = try cursor.next() XCTFail() @@ -161,7 +161,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { func testFetchCursorCompilationFailure() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in - func test(_ cursor: @autoclosure () throws -> ColumnCursor, sql: String) throws { + func test(_ cursor: @autoclosure () throws -> FastDatabaseValueCursor, sql: String) throws { do { _ = try cursor() XCTFail() @@ -426,7 +426,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { func testOptionalFetchCursor() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in - func test(_ cursor: NullableColumnCursor) throws { + func test(_ cursor: FastNullableDatabaseValueCursor) throws { let i = try cursor.next()! XCTAssertEqual(i!.int, 1) XCTAssertTrue(i!.fast) @@ -456,7 +456,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { func testOptionalFetchCursorCompilationFailure() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in - func test(_ cursor: @autoclosure () throws -> NullableColumnCursor, sql: String) throws { + func test(_ cursor: @autoclosure () throws -> FastNullableDatabaseValueCursor, sql: String) throws { do { _ = try cursor() XCTFail()