diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index f06616374c..f1684786ce 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -311,7 +311,7 @@ public final class Database { var sqliteConnection: SQLiteConnection? = nil let code = sqlite3_open_v2(path, &sqliteConnection, configuration.SQLiteOpenFlags, nil) guard code == SQLITE_OK else { - throw DatabaseError(resultCode: ResultCode(rawValue: code), message: String(cString: sqlite3_errmsg(sqliteConnection))) + throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection))) } do { @@ -319,7 +319,7 @@ public final class Database { do { let code = sqlite3_extended_result_codes(sqliteConnection!, 1) guard code == SQLITE_OK else { - throw DatabaseError(resultCode: ResultCode(rawValue: code), message: String(cString: sqlite3_errmsg(sqliteConnection))) + throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection))) } } @@ -338,7 +338,7 @@ public final class Database { do { let code = sqlite3_exec(sqliteConnection, "SELECT * FROM sqlite_master LIMIT 1", nil, nil, nil) guard code == SQLITE_OK else { - throw DatabaseError(resultCode: ResultCode(rawValue: code), message: String(cString: sqlite3_errmsg(sqliteConnection))) + throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection))) } } } catch { @@ -690,7 +690,7 @@ extension Database { var sqliteStatement: SQLiteStatement? = nil let code = sqlite3_prepare_v2(sqliteConnection, statementStart, -1, &sqliteStatement, &statementEnd) guard code == SQLITE_OK else { - error = DatabaseError(resultCode: ResultCode(rawValue: code), message: lastErrorMessage, sql: sql) + error = DatabaseError(resultCode: code, message: lastErrorMessage, sql: sql) break } @@ -787,7 +787,7 @@ extension Database { guard code == SQLITE_OK else { // Assume a GRDB bug: there is no point throwing any error. - fatalError(DatabaseError(resultCode: ResultCode(rawValue: code), message: lastErrorMessage).description) + fatalError(DatabaseError(resultCode: code, message: lastErrorMessage).description) } } @@ -802,7 +802,7 @@ extension Database { nil, nil, nil, nil, nil) guard code == SQLITE_OK else { // Assume a GRDB bug: there is no point throwing any error. - fatalError(DatabaseError(resultCode: ResultCode(rawValue: code), message: lastErrorMessage).description) + fatalError(DatabaseError(resultCode: code, message: lastErrorMessage).description) } } } @@ -891,7 +891,7 @@ extension Database { }, nil) guard code == SQLITE_OK else { // Assume a GRDB bug: there is no point throwing any error. - fatalError(DatabaseError(resultCode: ResultCode(rawValue: code), message: lastErrorMessage).description) + fatalError(DatabaseError(resultCode: code, message: lastErrorMessage).description) } } @@ -960,7 +960,7 @@ extension Database { sqlite3_key(sqliteConnection, bytes, Int32(data.count)) } guard code == SQLITE_OK else { - throw DatabaseError(resultCode: ResultCode(rawValue: code), message: String(cString: sqlite3_errmsg(sqliteConnection))) + throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection))) } } @@ -979,7 +979,7 @@ extension Database { sqlite3_rekey(sqliteConnection, bytes, Int32(data.count)) } guard code == SQLITE_OK else { - throw DatabaseError(resultCode: ResultCode(rawValue: code), message: String(cString: sqlite3_errmsg(sqliteConnection))) + throw DatabaseError(resultCode: code, message: lastErrorMessage) } } } diff --git a/GRDB/Core/DatabaseError.swift b/GRDB/Core/DatabaseError.swift index df9d62a53e..09112110bd 100644 --- a/GRDB/Core/DatabaseError.swift +++ b/GRDB/Core/DatabaseError.swift @@ -200,6 +200,7 @@ public struct DatabaseError : Error { /// The SQL query that yielded the error (if relevant). public let sql: String? + /// Creates a Database Error public init(resultCode: ResultCode = .SQLITE_ERROR, message: String? = nil, sql: String? = nil, arguments: StatementArguments? = nil) { self.extendedResultCode = resultCode self.message = message @@ -207,6 +208,13 @@ public struct DatabaseError : Error { self.arguments = arguments } + /// Creates a Database Error with a raw Int32 result code. + /// + /// This initializer is not public because library user is not supposed to + /// be exposed to raw result codes. + init(resultCode: Int32, message: String? = nil, sql: String? = nil, arguments: StatementArguments? = nil) { + self.init(resultCode: ResultCode(rawValue: resultCode), message: message, sql: sql, arguments: arguments) + } // MARK: Not public diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 2fcd1c4413..5fc0903b6b 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -133,7 +133,7 @@ public final class DatabasePool { // when kind is not .Passive. let code = sqlite3_wal_checkpoint_v2(db.sqliteConnection, nil, kind.rawValue, nil, nil) guard code == SQLITE_OK else { - throw DatabaseError(resultCode: ResultCode(rawValue: code), message: db.lastErrorMessage, sql: nil) + throw DatabaseError(resultCode: code, message: db.lastErrorMessage, sql: nil) } } } diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index cb2f5a3b9b..b0c7874cd2 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -158,7 +158,7 @@ extension DatabaseReader { case SQLITE_OK: afterBackupStep?() case let code: - throw DatabaseError(resultCode: ResultCode(rawValue: code), message: dbDest.lastErrorMessage) + throw DatabaseError(resultCode: code, message: dbDest.lastErrorMessage) } } } catch { @@ -170,7 +170,7 @@ extension DatabaseReader { case SQLITE_OK: break case let code: - throw DatabaseError(resultCode: ResultCode(rawValue: code), message: dbDest.lastErrorMessage) + throw DatabaseError(resultCode: code, message: dbDest.lastErrorMessage) } dbDest.clearSchemaCache() diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index d0aad47023..b3713ca48d 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -62,7 +62,7 @@ public class Statement { } guard code == SQLITE_OK else { - throw DatabaseError(resultCode: ResultCode(rawValue: code), message: database.lastErrorMessage, sql: sql) + throw DatabaseError(resultCode: code, message: database.lastErrorMessage, sql: sql) } guard remainingSQL.isEmpty else { @@ -86,7 +86,7 @@ public class Statement { // throwing any error. let code = sqlite3_reset(sqliteStatement) guard code == SQLITE_OK else { - fatalError(DatabaseError(resultCode: ResultCode(rawValue: code), message: database.lastErrorMessage, sql: sql).description) + fatalError(DatabaseError(resultCode: code, message: database.lastErrorMessage, sql: sql).description) } } @@ -183,7 +183,7 @@ public class Statement { // It looks like sqlite3_bind_xxx() functions do not access the file system. // They should thus succeed, unless a GRDB bug: there is no point throwing any error. guard code == SQLITE_OK else { - fatalError(DatabaseError(resultCode: ResultCode(rawValue: code), message: database.lastErrorMessage, sql: sql).description) + fatalError(DatabaseError(resultCode: code, message: database.lastErrorMessage, sql: sql).description) } } @@ -194,7 +194,7 @@ public class Statement { // no point throwing any error. let code = sqlite3_clear_bindings(sqliteStatement) guard code == SQLITE_OK else { - fatalError(DatabaseError(resultCode: ResultCode(rawValue: code), message: database.lastErrorMessage, sql: sql).description) + fatalError(DatabaseError(resultCode: code, message: database.lastErrorMessage, sql: sql).description) } } @@ -325,9 +325,9 @@ public final class DatabaseCursor : Cursor { return nil case SQLITE_ROW: return try element() - case let errorCode: + case let code: statement.database.selectStatementDidFail(statement) - throw DatabaseError(resultCode: ResultCode(rawValue: errorCode), message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments) + throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments) } } } @@ -426,13 +426,13 @@ public final class UpdateStatement : Statement { database.updateStatementDidExecute(self) return - case let errorCode: + case let code: // Failure // // Let database rethrow eventual transaction observer error: try database.updateStatementDidFail(self) - throw DatabaseError(resultCode: ResultCode(rawValue: errorCode), message: database.lastErrorMessage, sql: sql, arguments: self.arguments) // Error uses self.arguments, not the optional arguments parameter. + throw DatabaseError(resultCode: code, message: database.lastErrorMessage, sql: sql, arguments: self.arguments) // Error uses self.arguments, not the optional arguments parameter. } } } diff --git a/GRDB/FTS/FTS5CustomTokenizer.swift b/GRDB/FTS/FTS5CustomTokenizer.swift index ff99751ded..cc4bd17191 100644 --- a/GRDB/FTS/FTS5CustomTokenizer.swift +++ b/GRDB/FTS/FTS5CustomTokenizer.swift @@ -134,7 +134,7 @@ } guard code == SQLITE_OK else { // Assume a GRDB bug: there is no point throwing any error. - fatalError(DatabaseError(resultCode: ResultCode(rawValue: code), message: lastErrorMessage).description) + fatalError(DatabaseError(resultCode: code, message: lastErrorMessage).description) } } } diff --git a/GRDB/FTS/FTS5Tokenizer.swift b/GRDB/FTS/FTS5Tokenizer.swift index ffa271a761..50207ad94f 100644 --- a/GRDB/FTS/FTS5Tokenizer.swift +++ b/GRDB/FTS/FTS5Tokenizer.swift @@ -87,7 +87,7 @@ return SQLITE_OK }) if (code != SQLITE_OK) { - throw DatabaseError(resultCode: ResultCode(rawValue: code)) + throw DatabaseError(resultCode: code) } } return context.tokens @@ -147,13 +147,13 @@ } guard code == SQLITE_OK else { - throw DatabaseError(resultCode: ResultCode(rawValue: code), message: "failed fts5_tokenizer.xCreate") + throw DatabaseError(resultCode: code, message: "failed fts5_tokenizer.xCreate") } if let tokenizerPointer = tokenizerPointer { self.tokenizerPointer = tokenizerPointer } else { - throw DatabaseError(resultCode: ResultCode(rawValue: code), message: "nil tokenizer") + throw DatabaseError(resultCode: code, message: "nil tokenizer") } } @@ -205,7 +205,7 @@ xTokenizerPointer) guard code == SQLITE_OK else { - throw DatabaseError(resultCode: ResultCode(rawValue: code)) + throw DatabaseError(resultCode: code) } let contextPointer = contextHandle.pointee diff --git a/GRDB/FTS/FTS5WrapperTokenizer.swift b/GRDB/FTS/FTS5WrapperTokenizer.swift index fc8869bbd9..65b683f542 100644 --- a/GRDB/FTS/FTS5WrapperTokenizer.swift +++ b/GRDB/FTS/FTS5WrapperTokenizer.swift @@ -123,7 +123,7 @@ // Inject token bytes into SQLite let code = tokenCallback(context, flags.rawValue, pToken, nToken, iStart, iEnd) guard code == SQLITE_OK else { - throw DatabaseError(resultCode: ResultCode(rawValue: code), message: "token callback failed") + throw DatabaseError(resultCode: code, message: "token callback failed") } } })