diff --git a/GRDB/Core/DatabaseValue.swift b/GRDB/Core/DatabaseValue.swift index acd23942e..f505c8d6e 100644 --- a/GRDB/Core/DatabaseValue.swift +++ b/GRDB/Core/DatabaseValue.swift @@ -105,6 +105,8 @@ public struct DatabaseValue: Hashable, CustomStringConvertible, DatabaseValueCon case SQLITE_FLOAT: storage = .double(sqlite3_value_double(sqliteValue)) case SQLITE_TEXT: + // Builds an invalid string when decoding a blob that contains + // invalid UTF8 data. storage = .string(String(cString: sqlite3_value_text(sqliteValue)!)) case SQLITE_BLOB: if let bytes = sqlite3_value_blob(sqliteValue) { @@ -129,6 +131,8 @@ public struct DatabaseValue: Hashable, CustomStringConvertible, DatabaseValueCon case SQLITE_FLOAT: storage = .double(sqlite3_column_double(sqliteStatement, Int32(index))) case SQLITE_TEXT: + // Builds an invalid string when decoding a blob that contains + // invalid UTF8 data. storage = .string(String(cString: sqlite3_column_text(sqliteStatement, Int32(index)))) case SQLITE_BLOB: if let bytes = sqlite3_column_blob(sqliteStatement, Int32(index)) { diff --git a/GRDB/Core/DatabaseValueConversion.swift b/GRDB/Core/DatabaseValueConversion.swift index 2874c8558..a83b9308a 100644 --- a/GRDB/Core/DatabaseValueConversion.swift +++ b/GRDB/Core/DatabaseValueConversion.swift @@ -131,6 +131,16 @@ func fatalConversionError(to: T.Type, from dbValue: DatabaseValue?, conversio fatalError(conversionErrorMessage(to: T.self, from: dbValue, conversionContext: conversionContext), file: file, line: line) } +func fatalConversionError(to: T.Type, sqliteStatement: SQLiteStatement, index: Int32) -> Never { + let sql = String(cString: sqlite3_sql(sqliteStatement)) + .trimmingCharacters(in: statementSeparatorCharacterSet) + + fatalConversionError( + to: T.self, + from: DatabaseValue(sqliteStatement: sqliteStatement, index: index), + conversionContext: ValueConversionContext(sql: sql, arguments: nil)) +} + // MARK: - DatabaseValueConvertible /// Lossless conversions from database values and rows diff --git a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift index 92e0b9288..8c8b0776a 100644 --- a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift +++ b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift @@ -76,14 +76,14 @@ public struct DatabaseDateComponents : DatabaseValueConvertible, StatementColumn /// - index: The column index. public init(sqliteStatement: SQLiteStatement, index: Int32) { guard let cString = sqlite3_column_text(sqliteStatement, index) else { - fatalError("could not convert database value \(DatabaseValue(sqliteStatement: sqliteStatement, index: index)) to DatabaseDateComponents") + fatalConversionError(to: DatabaseDateComponents.self, sqliteStatement: sqliteStatement, index: index) } let length = Int(sqlite3_column_bytes(sqliteStatement, index)) // avoid an strlen let optionalComponents = cString.withMemoryRebound(to: Int8.self, capacity: length + 1 /* trailing \0 */) { cString in SQLiteDateParser().components(cString: cString, length: length) } guard let components = optionalComponents else { - fatalError("could not convert database value \(String(cString: cString)) to DatabaseDateComponents") + fatalConversionError(to: DatabaseDateComponents.self, sqliteStatement: sqliteStatement, index: index) } self.dateComponents = components.dateComponents self.format = components.format diff --git a/GRDB/Core/Support/Foundation/Date.swift b/GRDB/Core/Support/Foundation/Date.swift index c5e1e1d6b..2d8ea3bc7 100644 --- a/GRDB/Core/Support/Foundation/Date.swift +++ b/GRDB/Core/Support/Foundation/Date.swift @@ -129,13 +129,11 @@ extension Date: StatementColumnConvertible { case SQLITE_TEXT: let databaseDateComponents = DatabaseDateComponents(sqliteStatement: sqliteStatement, index: index) guard let date = Date(databaseDateComponents: databaseDateComponents) else { - let dbValue = DatabaseValue(sqliteStatement: sqliteStatement, index: index) - fatalError("could not convert database value \(dbValue) to Date") + fatalConversionError(to: Date.self, sqliteStatement: sqliteStatement, index: index) } self.init(timeIntervalSinceReferenceDate: date.timeIntervalSinceReferenceDate) default: - let dbValue = DatabaseValue(sqliteStatement: sqliteStatement, index: index) - fatalError("could not convert database value \(dbValue) to Date") + fatalConversionError(to: Date.self, sqliteStatement: sqliteStatement, index: index) } } } diff --git a/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift b/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift index 4397ffe7e..8e92e53d8 100644 --- a/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift +++ b/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift @@ -105,7 +105,7 @@ extension Int: DatabaseValueConvertible, StatementColumnConvertible { if let v = Int(exactly: int64) { self = v } else { - fatalError("could not convert database value \(int64) to Int") + fatalConversionError(to: Int.self, sqliteStatement: sqliteStatement, index: index) } } @@ -133,7 +133,7 @@ extension Int8: DatabaseValueConvertible, StatementColumnConvertible { if let v = Int8(exactly: int64) { self = v } else { - fatalError("could not convert database value \(int64) to Int8") + fatalConversionError(to: Int8.self, sqliteStatement: sqliteStatement, index: index) } } @@ -161,7 +161,7 @@ extension Int16: DatabaseValueConvertible, StatementColumnConvertible { if let v = Int16(exactly: int64) { self = v } else { - fatalError("could not convert database value \(int64) to Int16") + fatalConversionError(to: Int16.self, sqliteStatement: sqliteStatement, index: index) } } @@ -189,7 +189,7 @@ extension Int32: DatabaseValueConvertible, StatementColumnConvertible { if let v = Int32(exactly: int64) { self = v } else { - fatalError("could not convert database value \(int64) to Int32") + fatalConversionError(to: Int32.self, sqliteStatement: sqliteStatement, index: index) } } @@ -249,7 +249,7 @@ extension UInt: DatabaseValueConvertible, StatementColumnConvertible { if let v = UInt(exactly: int64) { self = v } else { - fatalError("could not convert database value \(int64) to UInt") + fatalConversionError(to: UInt.self, sqliteStatement: sqliteStatement, index: index) } } @@ -277,7 +277,7 @@ extension UInt8: DatabaseValueConvertible, StatementColumnConvertible { if let v = UInt8(exactly: int64) { self = v } else { - fatalError("could not convert database value \(int64) to UInt8") + fatalConversionError(to: UInt8.self, sqliteStatement: sqliteStatement, index: index) } } @@ -305,7 +305,7 @@ extension UInt16: DatabaseValueConvertible, StatementColumnConvertible { if let v = UInt16(exactly: int64) { self = v } else { - fatalError("could not convert database value \(int64) to UInt16") + fatalConversionError(to: UInt16.self, sqliteStatement: sqliteStatement, index: index) } } @@ -333,7 +333,7 @@ extension UInt32: DatabaseValueConvertible, StatementColumnConvertible { if let v = UInt32(exactly: int64) { self = v } else { - fatalError("could not convert database value \(int64) to UInt32") + fatalConversionError(to: UInt32.self, sqliteStatement: sqliteStatement, index: index) } } @@ -361,7 +361,7 @@ extension UInt64: DatabaseValueConvertible, StatementColumnConvertible { if let v = UInt64(exactly: int64) { self = v } else { - fatalError("could not convert database value \(int64) to UInt64") + fatalConversionError(to: UInt64.self, sqliteStatement: sqliteStatement, index: index) } } @@ -445,6 +445,8 @@ extension String: DatabaseValueConvertible, StatementColumnConvertible { /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. public init(sqliteStatement: SQLiteStatement, index: Int32) { + // Builds an invalid string when decoding a blob that contains + // invalid UTF8 data. self = String(cString: sqlite3_column_text(sqliteStatement, Int32(index))!) }