From 84c53ef6b9a03f345db6c7228fe48ea59be30cd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 13 Jul 2018 09:09:24 +0200 Subject: [PATCH] improve value conversion diagnostics more --- GRDB/Core/DatabaseValue.swift | 53 ++++++--- GRDB/Core/Row.swift | 103 +++++++++--------- .../DatabaseValueConvertibleErrorTests.swift | 100 ++++++++++------- 3 files changed, 149 insertions(+), 107 deletions(-) diff --git a/GRDB/Core/DatabaseValue.swift b/GRDB/Core/DatabaseValue.swift index 117e7bf2a..28962ef3b 100644 --- a/GRDB/Core/DatabaseValue.swift +++ b/GRDB/Core/DatabaseValue.swift @@ -207,16 +207,45 @@ extension DatabaseValue { // MARK: - Lossless conversions struct ValueConversionDebuggingInfo { - var statement: SelectStatement? - var row: Row? - var columnIndex: Int? - var columnName: String? + var _statement: SelectStatement? + var _row: Row? + var _columnIndex: Int? + var _columnName: String? + var statement: SelectStatement? { + return _statement ?? _row?.statement + } + + var row: Row? { + return _row ?? _statement.map { Row(statement: $0) } + } + + var columnIndex: Int? { + if let columnIndex = _columnIndex { + return columnIndex + } + 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) + return row[rowIndex].0 + } + return nil + } + init(statement: SelectStatement? = nil, row: Row? = nil, columnIndex: Int? = nil, columnName: String? = nil) { - self.statement = statement - self.row = row - self.columnIndex = columnIndex - self.columnName = columnName + _statement = statement + _row = row + _columnIndex = columnIndex + _columnName = columnName } } @@ -231,14 +260,12 @@ struct ValueConversionError: Error, CustomStringConvertible { extras.append("column: `\(columnName)`") } if let columnIndex = debugInfo.columnIndex { - extras.append("index: \(columnIndex)") + extras.append("column index: \(columnIndex)") } - let row = debugInfo.row ?? debugInfo.statement.map { Row(statement: $0) } - if let row = row { + if let row = debugInfo.row { extras.append("row: \(row)") } - let statement = debugInfo.statement ?? debugInfo.row?.statement - if let statement = statement { + if let statement = debugInfo.statement { extras.append("statement: `\(statement.sql)`") if statement.arguments.isEmpty == false { extras.append("arguments: \(statement.arguments)") diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 01d977f32..cbfab9bfe 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -132,6 +132,50 @@ extension Row { extension Row { + // MARK: - Extracting values: primitive + + func index(ofColumn name: String) -> Int? { + return impl.index(ofColumn: name) + } + + func decodeIfPresent( + _ type: Value.Type, + atUncheckedIndex index: Int, + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value? + { + return try impl.decodeIfPresent(Value.self, atUncheckedIndex: index, debugInfo: debugInfo) + } + + func decode( + _ type: Value.Type, + atUncheckedIndex index: Int, + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value + { + return try impl.decode(Value.self, atUncheckedIndex: index, debugInfo: debugInfo) + } + + func fastDecodeIfPresent( + _ type: Value.Type, + atUncheckedIndex index: Int, + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value? + { + if let sqliteStatement = sqliteStatement { + return Value.convertOptional(sqliteStatement: sqliteStatement, index: Int32(index)) + } + return try impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: index, debugInfo: debugInfo) + } + + func fastDecode( + _ type: Value.Type, + atUncheckedIndex index: Int, + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value + { + if let sqliteStatement = sqliteStatement { + return try Value.convert(sqliteStatement: sqliteStatement, index: Int32(index), debugInfo: debugInfo) + } + return try impl.fastDecode(Value.self, atUncheckedIndex: index, debugInfo: debugInfo) + } + // MARK: - Extracting Values /// Returns true if and only if one column contains a non-null value, or if @@ -180,44 +224,6 @@ extension Row { return impl.databaseValue(atUncheckedIndex: index).storage.value } - func decodeIfPresent( - _ type: Value.Type, - atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value? - { - return try impl.decodeIfPresent(Value.self, atUncheckedIndex: index, debugInfo: debugInfo) - } - - func decode( - _ type: Value.Type, - atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value - { - return try impl.decode(Value.self, atUncheckedIndex: index, debugInfo: debugInfo) - } - - func fastDecodeIfPresent( - _ type: Value.Type, - atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value? - { - if let sqliteStatement = sqliteStatement { - return Value.convertOptional(sqliteStatement: sqliteStatement, index: Int32(index)) - } - return try impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: index, debugInfo: debugInfo) - } - - func fastDecode( - _ type: Value.Type, - atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value - { - if let sqliteStatement = sqliteStatement { - return try Value.convert(sqliteStatement: sqliteStatement, index: Int32(index), debugInfo: debugInfo) - } - return try impl.fastDecode(Value.self, atUncheckedIndex: index, debugInfo: debugInfo) - } - /// Returns the value at given index, converted to the requested type. /// /// Indexes span from 0 for the leftmost column to (row.count - 1) for the @@ -291,7 +297,7 @@ extension Row { // if row["foo"] != nil { ... } // // Without this method, the code above would not compile. - guard let index = impl.index(ofColumn: columnName) else { + guard let index = index(ofColumn: columnName) else { return nil } return impl.databaseValue(atUncheckedIndex: index).storage.value @@ -306,7 +312,7 @@ extension Row { /// nil. Otherwise the SQLite value is converted to the requested type /// `Value`. Should this conversion fail, a fatal error is raised. public subscript(_ columnName: String) -> Value? { - guard let index = impl.index(ofColumn: columnName) else { + guard let index = index(ofColumn: columnName) else { return nil } return try! decodeIfPresent(Value.self, atUncheckedIndex: index, debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) @@ -325,7 +331,7 @@ extension Row { /// StatementColumnConvertible. It *may* trigger SQLite built-in conversions /// (see https://www.sqlite.org/datatype3.html). public subscript(_ columnName: String) -> Value? { - guard let index = impl.index(ofColumn: columnName) else { + guard let index = index(ofColumn: columnName) else { return nil } return try! fastDecodeIfPresent(Value.self, atUncheckedIndex: index, debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) @@ -341,7 +347,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(_ columnName: String) -> Value { - guard let index = impl.index(ofColumn: columnName) else { + guard let index = index(ofColumn: columnName) else { // Programmer error fatalError("no such column: \(columnName)") } @@ -362,7 +368,7 @@ extension Row { /// StatementColumnConvertible. It *may* trigger SQLite built-in conversions /// (see https://www.sqlite.org/datatype3.html). public subscript(_ columnName: String) -> Value { - guard let index = impl.index(ofColumn: columnName) else { + guard let index = index(ofColumn: columnName) else { // Programmer error fatalError("no such column: \(columnName)") } @@ -465,7 +471,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(named columnName: String) -> Data? { - guard let index = impl.index(ofColumn: columnName) else { + guard let index = index(ofColumn: columnName) else { return nil } return try! impl.dataNoCopy(atUncheckedIndex: index, debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) @@ -1214,8 +1220,7 @@ protocol RowImpl { func fastDecodeIfPresent(_ type: Value.Type, atUncheckedIndex index: Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value? func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Data? - // This method MUST be case-insensitive, and returns the index of the - // leftmost column that matches *name*. + /// Returns the index of the leftmost column that matches *name* (case-insensitive) func index(ofColumn name: String) -> Int? // row.impl is guaranteed to be self. @@ -1308,8 +1313,6 @@ private struct ArrayRowImpl : RowImpl { return columns[index].0 } - // This method MUST be case-insensitive, and returns the index of the - // leftmost column that matches *name*. func index(ofColumn name: String) -> Int? { let lowercaseName = name.lowercased() return columns.index { (column, _) in column.lowercased() == lowercaseName } @@ -1344,8 +1347,6 @@ private struct StatementCopyRowImpl : RowImpl { return columnNames[index] } - // This method MUST be case-insensitive, and returns the index of the - // leftmost column that matches *name*. func index(ofColumn name: String) -> Int? { let lowercaseName = name.lowercased() return columnNames.index { $0.lowercased() == lowercaseName } @@ -1417,8 +1418,6 @@ private struct StatementRowImpl : RowImpl { return statementRef.takeUnretainedValue().columnNames[index] } - // This method MUST be case-insensitive, and returns the index of the - // leftmost column that matches *name*. func index(ofColumn name: String) -> Int? { if let index = lowercaseColumnIndexes[name] { return index diff --git a/Tests/GRDBTests/DatabaseValueConvertibleErrorTests.swift b/Tests/GRDBTests/DatabaseValueConvertibleErrorTests.swift index 4d6349589..28dd7226a 100644 --- a/Tests/GRDBTests/DatabaseValueConvertibleErrorTests.swift +++ b/Tests/GRDBTests/DatabaseValueConvertibleErrorTests.swift @@ -24,57 +24,73 @@ class DatabaseValueConvertibleErrorTests: GRDBTestCase { } struct Record3: Codable, FetchableRecord { - var name: Value1 + var team: Value1 + } + + struct Record4: FetchableRecord { + var team: Value1 + + init(row: Row) { + team = row["team"] + } } enum Value1: String, DatabaseValueConvertible, Codable { - case baz + case valid } func testError() throws { // TODO: find a way to turn those into real tests let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in -// let statement = try db.makeSelectStatement("SELECT NULL AS name, ? AS team") -// statement.arguments = ["foo"] -// let row = try Row.fetchOne(statement)! -// -// // could not convert database value NULL to String (column: `name`, index: 0, row: [name:NULL team:"foo"]) -// _ = Record1(row: row) -// -// // could not convert database value NULL to String (column: `name`, index: 0, row: [name:NULL team:"foo"], statement: `SELECT NULL AS name, ? AS team`, arguments: ["foo"]) -// _ = try Record1.fetchOne(statement) -// -// // could not convert database value NULL to String (column: `name`, index: 0, row: [name:NULL team:"foo"]) -// _ = Record2(row: row) -// -// // could not convert database value NULL to String (column: `name`, index: 0, row: [name:NULL team:"foo"], statement: `SELECT NULL AS name, ? AS team`, arguments: ["foo"]) -// _ = try Record2.fetchOne(statement) -// -// // could not convert database value NULL to Value1 (column: `name`, row: [name:NULL team:"foo"]) -// _ = Record3(row: row) -// - // TODO: we miss column index below -// // could not convert database value NULL to Value1 (column: `name`, row: [name:NULL team:"foo"], statement: `SELECT NULL AS name, ? AS team`, arguments: ["foo"]) -// _ = try Record3.fetchOne(statement) -// -// // could not convert database value NULL to String (index: 0, row: [name:NULL team:"foo"], statement: `SELECT NULL AS name, ? AS team`, arguments: ["foo"]) -// _ = try String.fetchAll(statement) -// -// // could not convert database value NULL to String (column: `name`, index: 0, row: [name:NULL team:"foo"]) -// _ = row["name"] as String -// -// // could not convert database value NULL to String (index: 0, row: [name:NULL team:"foo"]) -// _ = row[0] as String -// -// // could not convert database value NULL to Value1 (index: 0, row: [name:NULL team:"foo"], statement: `SELECT NULL AS name, ? AS team`, arguments: ["foo"]) -// _ = try Value1.fetchAll(statement) -// -// // could not convert database value NULL to Value1 (column: `name`, index: 0, row: [name:NULL team:"foo"]) -// _ = row["name"] as Value1 -// -// // could not convert database value NULL to Value1 (index: 0, row: [name:NULL team:"foo"]) -// _ = row[0] as Value1 + let statement = try db.makeSelectStatement("SELECT NULL AS name, ? AS team") + statement.arguments = ["invalid"] + let row = try Row.fetchOne(statement)! + + // could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:"invalid"]) + _ = Record1(row: row) + + // could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:"foo"], statement: `SELECT NULL AS name, ? AS team`, arguments: ["foo"]) + _ = try Record1.fetchOne(statement) + + // could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:"foo"]) + _ = Record2(row: row) + + // could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:"foo"], statement: `SELECT NULL AS name, ? AS team`, arguments: ["foo"]) + _ = try Record2.fetchOne(statement) + + // could not convert database value "foo" to Value1 (column: `team`, column index: 1, row: [name:NULL team:"foo"]) + _ = Record3(row: row) + + // could not convert database value "foo" to Value1 (column: `team`, column index: 1, row: [name:NULL team:"foo"], statement: `SELECT NULL AS name, ? AS team`, arguments: ["foo"]) + _ = try Record3.fetchOne(statement) + + // could not convert database value "foo" to Value1 (column: `team`, column index: 1, row: [name:NULL team:"foo"]) + _ = Record4(row: row) + + // could not convert database value "foo" to Value1 (column: `team`, column index: 1, row: [name:NULL team:"foo"], statement: `SELECT NULL AS name, ? AS team`, arguments: ["foo"]) + _ = try Record4.fetchOne(statement) + + // could not convert database value NULL to String (column index: 0, row: [name:NULL team:"foo"], statement: `SELECT NULL AS name, ? AS team`, arguments: ["foo"]) + _ = try String.fetchAll(statement) + + // could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:"foo"]) + _ = row["name"] as String + + // could not convert database value NULL to String (column index: 0, row: [name:NULL team:"foo"]) + _ = row[0] as String + + // could not convert database value NULL to Value1 (column index: 0, row: [name:NULL team:"foo"], statement: `SELECT NULL AS name, ? AS team`, arguments: ["foo"]) + _ = try Value1.fetchAll(statement) + + // could not convert database value "foo" to Value1 (column: `team`, column index: 1, row: [name:NULL team:"foo"], statement: `SELECT NULL AS name, ? AS team`, arguments: ["foo"]) + _ = try Value1.fetchOne(statement, adapter: SuffixRowAdapter(fromIndex: 1)) + + // could not convert database value NULL to Value1 (column: `name`, column index: 0, row: [name:NULL team:"foo"]) + _ = row["name"] as Value1 + + // could not convert database value NULL to Value1 (index: 0, row: [name:NULL team:"foo"]) + _ = row[0] as Value1 } } }