Skip to content

Commit

Permalink
Report correct error when decoding NULL into Date or Data
Browse files Browse the repository at this point in the history
Fixes #1512
  • Loading branch information
groue committed Mar 21, 2024
1 parent 5413dcf commit ac8c4ae
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion GRDB/Record/FetchableRecord+Decodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -591,9 +591,19 @@ extension DatabaseDataDecodingStrategy {

fileprivate func decode(fromRow row: Row, atUncheckedIndex index: Int) throws -> Data {
if let sqliteStatement = row.sqliteStatement {
let statementIndex = CInt(index)

if sqlite3_column_type(sqliteStatement, statementIndex) == SQLITE_NULL {
throw RowDecodingError.valueMismatch(
Data.self,
sqliteStatement: sqliteStatement,
index: statementIndex,
context: RowDecodingContext(row: row, key: .columnIndex(index)))
}

return try decode(
fromStatement: sqliteStatement,
atUncheckedIndex: CInt(index),
atUncheckedIndex: statementIndex,
context: RowDecodingContext(row: row, key: .columnIndex(index)))
} else {
return try decode(
Expand Down Expand Up @@ -690,6 +700,16 @@ extension DatabaseDateDecodingStrategy {

fileprivate func decode(fromRow row: Row, atUncheckedIndex index: Int) throws -> Date {
if let sqliteStatement = row.sqliteStatement {
let statementIndex = CInt(index)

if sqlite3_column_type(sqliteStatement, statementIndex) == SQLITE_NULL {
throw RowDecodingError.valueMismatch(
Date.self,
sqliteStatement: sqliteStatement,
index: statementIndex,
context: RowDecodingContext(row: row, key: .columnIndex(index)))
}

return try decode(
fromStatement: sqliteStatement,
atUncheckedIndex: CInt(index),
Expand Down

0 comments on commit ac8c4ae

Please sign in to comment.