mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
improve value conversion diagnostics more
This commit is contained in:
1 parent
a8f6ed6d61
commit
84c53ef6b9
3 files changed
+149
-107
No files matched your search
@@ -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<T>: 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)")
|
||||
|
||||
+51
-52
@@ -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<Value: DatabaseValueConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value?
|
||||
{
|
||||
return try impl.decodeIfPresent(Value.self, atUncheckedIndex: index, debugInfo: debugInfo)
|
||||
}
|
||||
|
||||
func decode<Value: DatabaseValueConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value
|
||||
{
|
||||
return try impl.decode(Value.self, atUncheckedIndex: index, debugInfo: debugInfo)
|
||||
}
|
||||
|
||||
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ 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<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ 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<Value: DatabaseValueConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value?
|
||||
{
|
||||
return try impl.decodeIfPresent(Value.self, atUncheckedIndex: index, debugInfo: debugInfo)
|
||||
}
|
||||
|
||||
func decode<Value: DatabaseValueConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value
|
||||
{
|
||||
return try impl.decode(Value.self, atUncheckedIndex: index, debugInfo: debugInfo)
|
||||
}
|
||||
|
||||
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ 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<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ 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<Value: DatabaseValueConvertible>(_ 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<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ 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<Value: DatabaseValueConvertible>(_ 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<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ 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<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ 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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user