Even better feedback for conversion errors from StatementColumnConvertible

This commit is contained in:
Gwendal Roué committed 2018-08-15 16:49:07 +02:00
1 parent e8dacb31cc
commit b47f5864ac
3 files changed
+90 -5

No files matched your search

+9 -4
View File
@@ -67,6 +67,13 @@ extension ValueConversionContext {
sql: statement.sql,
arguments: statement.arguments,
column: nil)
} else if let sqliteStatement = row.sqliteStatement {
let sql = String(cString: sqlite3_sql(sqliteStatement)).trimmingCharacters(in: statementSeparatorCharacterSet)
self.init(
row: row.copy(),
sql: sql,
arguments: nil,
column: nil)
} else {
self.init(
row: row.copy(),
@@ -132,13 +139,11 @@ func fatalConversionError<T>(to: T.Type, from dbValue: DatabaseValue?, conversio
}
func fatalConversionError<T>(to: T.Type, sqliteStatement: SQLiteStatement, index: Int32) -> Never {
let sql = String(cString: sqlite3_sql(sqliteStatement))
.trimmingCharacters(in: statementSeparatorCharacterSet)
let row = Row(sqliteStatement: sqliteStatement)
fatalConversionError(
to: T.self,
from: DatabaseValue(sqliteStatement: sqliteStatement, index: index),
conversionContext: ValueConversionContext(sql: sql, arguments: nil))
conversionContext: ValueConversionContext(row).atColumn(Int(index)))
}
// MARK: - DatabaseValueConvertible
+32
View File
@@ -94,6 +94,15 @@ public final class Row : Equatable, Hashable, RandomAccessCollection, Expressibl
self.count = Int(sqlite3_column_count(sqliteStatement))
}
/// Creates a row that maps an SQLite statement. Further calls to
/// sqlite3_step() modify the row.
init(sqliteStatement: SQLiteStatement) {
self.sqliteStatement = sqliteStatement
self.statementRef = nil
self.impl = SQLiteStatementRowImpl(sqliteStatement: sqliteStatement)
self.count = Int(sqlite3_column_count(sqliteStatement))
}
/// Creates a row that contain a copy of the current state of the
/// SQLite statement. Further calls to sqlite3_step() do not modify the row.
///
@@ -1407,6 +1416,29 @@ private struct StatementRowImpl : RowImpl {
}
}
// This one is not optimized at all, since it is only used in fatal conversion errors, so far
private struct SQLiteStatementRowImpl : RowImpl {
let sqliteStatement: SQLiteStatement
var count: Int { return Int(sqlite3_column_count(sqliteStatement)) }
var isFetched: Bool { return true }
func columnName(atUncheckedIndex index: Int) -> String {
return String(cString: sqlite3_column_name(sqliteStatement, Int32(index)))
}
func databaseValue(atUncheckedIndex index: Int) -> DatabaseValue {
return DatabaseValue(sqliteStatement: sqliteStatement, index: Int32(index))
}
func index(ofColumn name: String) -> Int? {
let name = name.lowercased()
for index in 0..<count where columnName(atUncheckedIndex: index).lowercased() == name {
return index
}
return nil
}
}
/// See Row.init()
private struct EmptyRowImpl : RowImpl {
var count: Int {
@@ -321,7 +321,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
}
}
func testStatementColumnConvertible() throws {
func testStatementColumnConvertible1() throws {
// Those tests are tightly coupled to GRDB decoding code.
// Each test comes with one or several commented crashing code snippets that trigger it.
let dbQueue = try makeDatabaseQueue()
@@ -358,6 +358,54 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
}
}
func testStatementColumnConvertible2() throws {
// Those tests are tightly coupled to GRDB decoding code.
// Each test comes with one or several commented crashing code snippets that trigger it.
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
let statement = try db.makeSelectStatement("SELECT ? AS foo")
statement.arguments = [1000]
let row = try Row.fetchOne(statement)!
// _ = try Row.fetchCursor(statement).map { $0["missing"] as Int8 }.next()
try Row.fetchCursor(statement).forEach { row in
XCTAssertEqual(
conversionErrorMessage(
to: Int8.self,
from: nil,
conversionContext: ValueConversionContext(row).atColumn("missing")),
"could not read Int8 from missing column `missing` (row: [foo:1000], sql: `SELECT ? AS foo`, arguments: [1000])")
}
// _ = try Int8.fetchAll(statement)
try statement.makeCursor().forEach {
let sqliteStatement = statement.sqliteStatement
XCTAssertEqual(
conversionErrorMessage(
to: Int8.self,
from: DatabaseValue(sqliteStatement: sqliteStatement, index: 0),
conversionContext: ValueConversionContext(Row(sqliteStatement: sqliteStatement)).atColumn(0)),
"could not convert database value 1000 to Int8 (column: `foo`, column index: 0, row: [foo:1000], sql: `SELECT ? AS foo`)")
}
// _ = row["name"] as Int8
XCTAssertEqual(
conversionErrorMessage(
to: Int8.self,
from: row["foo"],
conversionContext: ValueConversionContext(row).atColumn("foo")),
"could not convert database value 1000 to Int8 (column: `foo`, column index: 0, row: [foo:1000])")
// _ = row[0] as Int8
XCTAssertEqual(
conversionErrorMessage(
to: Int8.self,
from: row[0],
conversionContext: ValueConversionContext(row).atColumn(0)),
"could not convert database value 1000 to Int8 (column: `foo`, column index: 0, row: [foo:1000])")
}
}
func testDecodableDatabaseValueConvertible() throws {
enum Value: String, DatabaseValueConvertible, Decodable {
case valid