no more throwing conversion methods, and refactor ValueConversionDebuggingInfo

This commit is contained in:
Gwendal Roué committed 2018-08-09 08:51:50 +02:00
1 parent 89d5bd7945
commit bcc45dc076
8 files changed
+161 -146

No files matched your search

+2 -2
View File
@@ -227,7 +227,7 @@ extension DatabaseValue {
/// conversion error
@available(*, deprecated)
public func losslessConvert<T>(sql: String? = nil, arguments: StatementArguments? = nil) -> T where T : DatabaseValueConvertible {
return require { try T.decode(from: self, debugInfo: ValueConversionDebuggingInfo()) }
return T.decode(from: self, debugInfo: ValueConversionDebuggingInfo())
}
/// Converts the database value to the type Optional<T>.
@@ -251,7 +251,7 @@ extension DatabaseValue {
/// conversion error
@available(*, deprecated)
public func losslessConvert<T>(sql: String? = nil, arguments: StatementArguments? = nil) -> T? where T : DatabaseValueConvertible {
return require { try T.decodeIfPresent(from: self, debugInfo: ValueConversionDebuggingInfo()) }
return T.decodeIfPresent(from: self, debugInfo: ValueConversionDebuggingInfo())
}
}
+82 -82
View File
@@ -6,113 +6,115 @@
// All primitive value conversion methods.
/// An alternative to try!, preferred for conversion methods.
@inline(__always)
func require<T>(file: StaticString = #file, line: UInt = #line, block: () throws -> T) -> T {
do {
return try block()
} catch {
fatalError(String(describing: error), file: file, line: line)
}
}
/// A type that helps the user understanding value conversion errors
struct ValueConversionDebuggingInfo {
private var _statement: SelectStatement?
private var _row: Row?
private var _columnIndex: Int?
private var _columnName: String?
enum Source {
case statement(SelectStatement)
case sql(String, StatementArguments)
case row(Row)
}
enum Column {
case columnIndex(Int)
case columnName(String)
}
private var source: Source?
private var column: Column?
init(statement: SelectStatement? = nil, row: Row? = nil, columnIndex: Int? = nil, columnName: String? = nil) {
_statement = statement
_row = row
_columnIndex = columnIndex
_columnName = columnName
fatalError("not implemented")
}
var statement: SelectStatement? {
return _statement ?? _row?.statement
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? {
return _row ?? _statement.map { Row(statement: $0) }
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
}
}
var columnIndex: Int? {
if let columnIndex = _columnIndex {
return columnIndex
guard let column = column else { return nil }
switch column {
case .columnIndex(let index): return index
case .columnName(let name): return row?.index(ofColumn: name)
}
if let columnName = _columnName, let row = row {
return row.index(ofColumn: columnName)
}
return nil
}
var columnName: String? {
if let columnName = _columnName {
return columnName
}
if let columnIndex = _columnIndex, let row = row {
let rowIndex = row.index(row.startIndex, offsetBy: columnIndex)
guard let column = column else { return nil }
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
case .columnName(let name): return name
}
return nil
}
}
/// A conversion error
struct ValueConversionError<T>: Error, CustomStringConvertible {
var dbValue: DatabaseValue
var debugInfo: ValueConversionDebuggingInfo
var description: String {
var error = "could not convert database value \(dbValue) to \(T.self)"
var extras: [String] = []
if let columnName = debugInfo.columnName {
extras.append("column: `\(columnName)`")
}
if let columnIndex = debugInfo.columnIndex {
extras.append("column index: \(columnIndex)")
}
if let row = debugInfo.row {
extras.append("row: \(row)")
}
if let statement = debugInfo.statement {
extras.append("statement: `\(statement.sql)`")
if statement.arguments.isEmpty == false {
extras.append("arguments: \(statement.arguments)")
}
}
if extras.isEmpty == false {
error += " (" + extras.joined(separator: ", ") + ")"
}
return error
/// The canonical conversion fatal error
func fatalConversionError<T>(to: T.Type, from dbValue: DatabaseValue, debugInfo: ValueConversionDebuggingInfo) -> Never {
var message = "could not convert database value \(dbValue) to \(T.self)"
var extras: [String] = []
if let columnName = debugInfo.columnName {
extras.append("column: `\(columnName)`")
}
if let columnIndex = debugInfo.columnIndex {
extras.append("column index: \(columnIndex)")
}
if let row = debugInfo.row {
extras.append("row: \(row)")
}
if let sql = debugInfo.sql {
extras.append("statement: `\(sql)`")
if let arguments = debugInfo.arguments, arguments.isEmpty == false {
extras.append("arguments: \(arguments)")
}
}
if extras.isEmpty == false {
message += " (" + extras.joined(separator: ", ") + ")"
}
fatalError(message)
}
extension DatabaseValueConvertible {
/// Performs lossless conversion from a database value.
///
/// - throws: ValueConversionError<Self>
static func decode(from dbValue: DatabaseValue, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Self {
static func decode(from dbValue: DatabaseValue, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Self {
if let value = fromDatabaseValue(dbValue) {
return value
} else {
throw ValueConversionError<Self>(dbValue: dbValue, debugInfo: debugInfo())
fatalConversionError(to: Self.self, from: dbValue, debugInfo: debugInfo())
}
}
/// Performs lossless conversion from a database value.
///
/// - throws: ValueConversionError<Self>
static func decodeIfPresent(from dbValue: DatabaseValue, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Self? {
static func decodeIfPresent(from dbValue: DatabaseValue, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> 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 {
throw ValueConversionError<Self>(dbValue: dbValue, debugInfo: debugInfo())
fatalConversionError(to: Self.self, from: dbValue, debugInfo: debugInfo())
}
}
}
@@ -120,12 +122,10 @@ extension DatabaseValueConvertible {
extension DatabaseValueConvertible where Self: StatementColumnConvertible {
/// Performs lossless conversion from a statement value.
///
/// - throws: ValueConversionError<Self>
@inline(__always)
static func decode(from sqliteStatement: SQLiteStatement, index: Int32, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Self {
static func decode(from sqliteStatement: SQLiteStatement, index: Int32, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Self {
if sqlite3_column_type(sqliteStatement, index) == SQLITE_NULL {
throw ValueConversionError<Self>(dbValue: .null, debugInfo: debugInfo())
fatalConversionError(to: Self.self, from: .null, debugInfo: debugInfo())
}
return self.init(sqliteStatement: sqliteStatement, index: index)
}
@@ -146,41 +146,41 @@ extension Row {
func decodeIfPresent<Value: DatabaseValueConvertible>(
_ type: Value.Type,
atUncheckedIndex index: Int,
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value?
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value?
{
return try Value.decodeIfPresent(from: impl.databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
return Value.decodeIfPresent(from: impl.databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
}
@inline(__always)
func decode<Value: DatabaseValueConvertible>(
_ type: Value.Type,
atUncheckedIndex index: Int,
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value
{
return try Value.decode(from: impl.databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
return Value.decode(from: impl.databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
}
@inline(__always)
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(
_ type: Value.Type,
atUncheckedIndex index: Int,
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value?
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value?
{
if let sqliteStatement = sqliteStatement {
return Value.decodeIfPresent(from: sqliteStatement, index: Int32(index))
}
return try impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: index, debugInfo: debugInfo)
return impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: index, debugInfo: debugInfo)
}
@inline(__always)
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(
_ type: Value.Type,
atUncheckedIndex index: Int,
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value
{
if let sqliteStatement = sqliteStatement {
return try Value.decode(from: sqliteStatement, index: Int32(index), debugInfo: debugInfo)
return Value.decode(from: sqliteStatement, index: Int32(index), debugInfo: debugInfo)
}
return try impl.fastDecode(Value.self, atUncheckedIndex: index, debugInfo: debugInfo)
return impl.fastDecode(Value.self, atUncheckedIndex: index, debugInfo: debugInfo)
}
}
+16 -4
View File
@@ -70,10 +70,16 @@ public final class DatabaseValueCursor<Value: DatabaseValueConvertible> : Cursor
return nil
case SQLITE_ROW:
let dbValue = DatabaseValue(sqliteStatement: sqliteStatement, index: columnIndex)
return require { try Value.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo(statement: statement, columnIndex: Int(columnIndex))) }
return Value.decode(
from: dbValue,
debugInfo: ValueConversionDebuggingInfo(statement: statement, columnIndex: Int(columnIndex)))
case let code:
statement.database.selectStatementDidFail(statement)
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
throw DatabaseError(
resultCode: code,
message: statement.database.lastErrorMessage,
sql: statement.sql,
arguments: statement.arguments)
}
}
}
@@ -110,10 +116,16 @@ public final class NullableDatabaseValueCursor<Value: DatabaseValueConvertible>
return nil
case SQLITE_ROW:
let dbValue = DatabaseValue(sqliteStatement: sqliteStatement, index: columnIndex)
return require { try Value.decodeIfPresent(from: dbValue, debugInfo: ValueConversionDebuggingInfo(statement: statement, columnIndex: Int(columnIndex))) }
return Value.decodeIfPresent(
from: dbValue,
debugInfo: ValueConversionDebuggingInfo(statement: statement, columnIndex: Int(columnIndex)))
case let code:
statement.database.selectStatementDidFail(statement)
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
throw DatabaseError(
resultCode: code,
message: statement.database.lastErrorMessage,
sql: statement.sql,
arguments: statement.arguments)
}
}
}
+33 -33
View File
@@ -195,10 +195,10 @@ extension Row {
/// fail, a fatal error is raised.
public subscript<Value: DatabaseValueConvertible>(_ index: Int) -> Value? {
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
return require { try decodeIfPresent(
return decodeIfPresent(
Value.self,
atUncheckedIndex: index,
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) }
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index))
}
/// Returns the value at given index, converted to the requested type.
@@ -215,10 +215,10 @@ extension Row {
/// (see https://www.sqlite.org/datatype3.html).
public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ index: Int) -> Value? {
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
return require { try fastDecodeIfPresent(
return fastDecodeIfPresent(
Value.self,
atUncheckedIndex: index,
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) }
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index))
}
/// Returns the value at given index, converted to the requested type.
@@ -230,10 +230,10 @@ extension Row {
/// SQLite value can not be converted to `Value`.
public subscript<Value: DatabaseValueConvertible>(_ index: Int) -> Value {
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
return require { try decode(
return decode(
Value.self,
atUncheckedIndex: index,
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) }
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index))
}
/// Returns the value at given index, converted to the requested type.
@@ -249,10 +249,10 @@ extension Row {
/// (see https://www.sqlite.org/datatype3.html).
public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ index: Int) -> Value {
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
return require { try fastDecode(
return fastDecode(
Value.self,
atUncheckedIndex: index,
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) }
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index))
}
/// Returns Int64, Double, String, Data or nil, depending on the value
@@ -288,10 +288,10 @@ extension Row {
guard let index = index(ofColumn: columnName) else {
return nil
}
return require { try decodeIfPresent(
return decodeIfPresent(
Value.self,
atUncheckedIndex: index,
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) }
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName))
}
/// Returns the value at given column, converted to the requested type.
@@ -310,10 +310,10 @@ extension Row {
guard let index = index(ofColumn: columnName) else {
return nil
}
return require { try fastDecodeIfPresent(
return fastDecodeIfPresent(
Value.self,
atUncheckedIndex: index,
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) }
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName))
}
/// Returns the value at given column, converted to the requested type.
@@ -330,10 +330,10 @@ extension Row {
// Programmer error
fatalError("no such column: \(columnName)")
}
return require { try decode(
return decode(
Value.self,
atUncheckedIndex: index,
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) }
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName))
}
/// Returns the value at given column, converted to the requested type.
@@ -354,10 +354,10 @@ extension Row {
// Programmer error
fatalError("no such column: \(columnName)")
}
return require { try fastDecode(
return fastDecode(
Value.self,
atUncheckedIndex: index,
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) }
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName))
}
/// Returns Int64, Double, String, NSData or nil, depending on the value
@@ -441,9 +441,9 @@ extension Row {
/// than the row's lifetime.
public func dataNoCopy(atIndex index: Int) -> Data? {
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
return require { try impl.dataNoCopy(
return impl.dataNoCopy(
atUncheckedIndex: index,
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) }
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index))
}
/// Returns the optional Data at given column.
@@ -461,9 +461,9 @@ extension Row {
guard let index = index(ofColumn: columnName) else {
return nil
}
return require { try impl.dataNoCopy(
return impl.dataNoCopy(
atUncheckedIndex: index,
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) }
debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName))
}
/// Returns the optional `NSData` at given column.
@@ -1205,9 +1205,9 @@ protocol RowImpl {
func columnName(atUncheckedIndex index: Int) -> String
func hasNull(atUncheckedIndex index:Int) -> Bool
func databaseValue(atUncheckedIndex index: Int) -> DatabaseValue
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ type: Value.Type, atUncheckedIndex index: Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ type: Value.Type, atUncheckedIndex index: Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value?
func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Data?
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ type: Value.Type, atUncheckedIndex index: Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ type: Value.Type, atUncheckedIndex index: Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value?
func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data?
/// Returns the index of the leftmost column that matches *name* (case-insensitive)
func index(ofColumn name: String) -> Int?
@@ -1242,24 +1242,24 @@ extension RowImpl {
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(
_ type: Value.Type,
atUncheckedIndex index: Int,
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value
{
// default implementation is slow
return try Value.decode(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
return Value.decode(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
}
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(
_ type: Value.Type,
atUncheckedIndex index: Int,
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value?
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value?
{
// default implementation is slow
return try Value.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
return Value.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
}
func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Data? {
func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data? {
// default implementation copies
return try Data.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
return Data.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
}
}
@@ -1359,7 +1359,7 @@ private struct StatementRowImpl : RowImpl {
return sqlite3_column_type(sqliteStatement, Int32(index)) == SQLITE_NULL
}
func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Data? {
func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data? {
guard sqlite3_column_type(sqliteStatement, Int32(index)) != SQLITE_NULL else {
return nil
}
@@ -1376,14 +1376,14 @@ private struct StatementRowImpl : RowImpl {
func fastValue<Value: DatabaseValueConvertible & StatementColumnConvertible>(
atUncheckedIndex index: Int,
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value
{
return try Value.decode(from: sqliteStatement, index: Int32(index), debugInfo: debugInfo)
return Value.decode(from: sqliteStatement, index: Int32(index), debugInfo: debugInfo)
}
func fastOptionalValue<Value: DatabaseValueConvertible & StatementColumnConvertible>(
atUncheckedIndex index: Int,
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value?
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value?
{
return Value.decodeIfPresent(from: sqliteStatement, index: Int32(index))
}
+6 -6
View File
@@ -504,27 +504,27 @@ struct AdaptedRowImpl : RowImpl {
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(
_ type: Value.Type,
atUncheckedIndex index: Int,
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value
{
let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index)
return try base.impl.fastDecode(Value.self, atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation
return base.impl.fastDecode(Value.self, atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation
}
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(
_ type: Value.Type,
atUncheckedIndex index: Int,
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value?
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value?
{
let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index)
return try base.impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation
return base.impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation
}
func dataNoCopy(
atUncheckedIndex index:Int,
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Data?
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data?
{
let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index)
return try base.impl.dataNoCopy(atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation
return base.impl.dataNoCopy(atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation
}
func columnName(atUncheckedIndex index: Int) -> String {
+4 -1
View File
@@ -81,7 +81,10 @@ public final class ColumnCursor<Value: DatabaseValueConvertible & StatementColum
done = true
return nil
case SQLITE_ROW:
return require { try Value.decode(from: sqliteStatement, index: columnIndex, debugInfo: ValueConversionDebuggingInfo(statement: statement, columnIndex: Int(columnIndex))) }
return Value.decode(
from: sqliteStatement,
index: columnIndex,
debugInfo: ValueConversionDebuggingInfo(statement: statement, columnIndex: Int(columnIndex)))
case let code:
statement.database.selectStatementDidFail(statement)
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
@@ -15,20 +15,20 @@ private struct DatabaseValueDecodingContainer: SingleValueDecodingContainer {
/// - returns: A value of the requested type.
/// - throws: `DecodingError.typeMismatch` if the encountered encoded value cannot be converted to the requested type.
/// - throws: `DecodingError.valueNotFound` if the encountered encoded value is null.
func decode(_ type: Bool.Type) throws -> Bool { return require { try Bool.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } }
func decode(_ type: Int.Type) throws -> Int { return require { try Int.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } }
func decode(_ type: Int8.Type) throws -> Int8 { return require { try Int8.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } }
func decode(_ type: Int16.Type) throws -> Int16 { return require { try Int16.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } }
func decode(_ type: Int32.Type) throws -> Int32 { return require { try Int32.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } }
func decode(_ type: Int64.Type) throws -> Int64 { return require { try Int64.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } }
func decode(_ type: UInt.Type) throws -> UInt { return require { try UInt.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } }
func decode(_ type: UInt8.Type) throws -> UInt8 { return require { try UInt8.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } }
func decode(_ type: UInt16.Type) throws -> UInt16 { return require { try UInt16.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } }
func decode(_ type: UInt32.Type) throws -> UInt32 { return require { try UInt32.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } }
func decode(_ type: UInt64.Type) throws -> UInt64 { return require { try UInt64.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } }
func decode(_ type: Float.Type) throws -> Float { return require { try Float.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } }
func decode(_ type: Double.Type) throws -> Double { return require { try Double.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } }
func decode(_ type: String.Type) throws -> String { return require { try String.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } }
func decode(_ type: Bool.Type) throws -> Bool { return Bool.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) }
func decode(_ type: Int.Type) throws -> Int { return Int.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) }
func decode(_ type: Int8.Type) throws -> Int8 { return Int8.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) }
func decode(_ type: Int16.Type) throws -> Int16 { return Int16.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) }
func decode(_ type: Int32.Type) throws -> Int32 { return Int32.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) }
func decode(_ type: Int64.Type) throws -> Int64 { return Int64.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) }
func decode(_ type: UInt.Type) throws -> UInt { return UInt.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) }
func decode(_ type: UInt8.Type) throws -> UInt8 { return UInt8.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) }
func decode(_ type: UInt16.Type) throws -> UInt16 { return UInt16.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) }
func decode(_ type: UInt32.Type) throws -> UInt32 { return UInt32.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) }
func decode(_ type: UInt64.Type) throws -> UInt64 { return UInt64.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) }
func decode(_ type: Float.Type) throws -> Float { return Float.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) }
func decode(_ type: Double.Type) throws -> Double { return Double.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) }
func decode(_ type: String.Type) throws -> String { return String.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) }
/// Decodes a single value of the given type.
///
@@ -41,7 +41,7 @@ private struct DatabaseValueDecodingContainer: SingleValueDecodingContainer {
// Prefer DatabaseValueConvertible decoding over Decodable.
// This allows custom database decoding, such as decoding Date from
// String, for example.
return require { try type.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) as! T }
return type.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) as! T
} else {
return try T(from: DatabaseValueDecoder(dbValue: dbValue, codingPath: codingPath))
}
+3 -3
View File
@@ -75,7 +75,7 @@ private struct RowKeyedDecodingContainer<Key: CodingKey>: 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 require { try type.decodeIfPresent(from: dbValue, debugInfo: ValueConversionDebuggingInfo(row: row, columnName: key.stringValue)) as! T? }
return type.decodeIfPresent(from: dbValue, debugInfo: ValueConversionDebuggingInfo(row: row, columnName: key.stringValue)) as! T?
} else if dbValue.isNull {
return nil
} else {
@@ -114,7 +114,7 @@ private struct RowKeyedDecodingContainer<Key: CodingKey>: 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 require { try type.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo(row: row, columnName: key.stringValue)) as! T }
return type.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo(row: row, columnName: 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 require { try type.decode(from: row[column.stringValue], debugInfo: ValueConversionDebuggingInfo(row: row, columnName: column.stringValue)) as! T }
return type.decode(from: row[column.stringValue], debugInfo: ValueConversionDebuggingInfo(row: row, columnName: column.stringValue)) as! T
} else {
return try T(from: RowDecoder(row: row, codingPath: [column]))
}