From b37c5d9fa094ef2b09707ca384473cc7f27ba6b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 26 Feb 2017 15:26:44 +0100 Subject: [PATCH] #171: introduce ResultCode --- GRDB/Core/Database.swift | 44 +++++-- GRDB/Core/DatabaseError.swift | 122 +++++++++++++++++- GRDB/Core/DatabasePool.swift | 2 +- GRDB/Core/DatabaseReader.swift | 6 +- GRDB/Core/DatabaseValueConvertible.swift | 6 +- GRDB/Core/RowAdapter.swift | 2 +- GRDB/Core/Statement.swift | 20 +-- GRDB/Core/StatementColumnConvertible.swift | 2 +- GRDB/FTS/FTS5Tokenizer.swift | 4 +- GRDB/Migrations/Migration.swift | 2 +- GRDB/Record/RowConvertible.swift | 2 +- GRDB/Record/TableMapping.swift | 2 +- .../DatabasePoolSchemaCacheTests.swift | 2 +- .../DatabaseQueueSchemaCacheTests.swift | 2 +- Tests/Private/RecordUniqueIndexTests.swift | 8 +- .../Public/Core/Database/DatabaseTests.swift | 2 +- .../Core/Database/ReadOnlyDatabaseTests.swift | 2 +- .../DatabaseCursor/DatabaseCursorTests.swift | 2 +- .../DatabaseError/DatabaseErrorTests.swift | 12 +- .../DatabasePoolConcurrencyTests.swift | 6 +- .../DatabaseQueueConcurrencyTests.swift | 6 +- .../DatabaseQueue/DatabaseQueueTests.swift | 6 +- .../DatabaseValueConvertibleFetchTests.swift | 36 +++--- .../Public/Core/Function/FunctionTests.swift | 12 +- .../Core/Persistable/PersistableTests.swift | 2 +- .../Public/Core/Row/RowConvertibleTests.swift | 14 +- Tests/Public/Core/Row/RowFetchTests.swift | 14 +- .../Core/Savepoint/SavepointTests.swift | 4 +- .../Core/Statement/SelectStatementTests.swift | 4 +- .../Core/Statement/UpdateStatementTests.swift | 6 +- ...StatementColumnConvertibleFetchTests.swift | 28 ++-- .../TransactionObserverTests.swift | 16 +-- Tests/Public/Encryption/EncryptionTests.swift | 16 +-- .../FetchedRecordsControllerTests.swift | 2 +- .../Migrations/DatabaseMigratorTests.swift | 4 +- .../VirtualTableModuleTests.swift | 4 +- 36 files changed, 281 insertions(+), 143 deletions(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 8405eee7d3..6e3b662f5a 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -269,7 +269,7 @@ public final class Database { return Int(sqlite3_total_changes(sqliteConnection)) } - var lastErrorCode: Int32 { return sqlite3_errcode(sqliteConnection) } + var lastErrorCode: ResultCode { return ResultCode(rawValue: sqlite3_errcode(sqliteConnection)) } var lastErrorMessage: String? { return String(cString: sqlite3_errmsg(sqliteConnection)) } /// True if the database connection is currently in a transaction. @@ -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(code: code, message: String(cString: sqlite3_errmsg(sqliteConnection))) + throw DatabaseError(code: ResultCode(rawValue: code), message: String(cString: sqlite3_errmsg(sqliteConnection))) } do { @@ -329,7 +329,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(code: code, message: String(cString: sqlite3_errmsg(sqliteConnection))) + throw DatabaseError(code: ResultCode(rawValue: code), message: String(cString: sqlite3_errmsg(sqliteConnection))) } } } catch { @@ -655,7 +655,7 @@ extension Database { } let validateRemainingArguments = { if !arguments.values.isEmpty { - throw DatabaseError(code: SQLITE_MISUSE, message: "wrong number of statement arguments: \(initialValuesCount)") + throw DatabaseError(code: .SQLITE_MISUSE, message: "wrong number of statement arguments: \(initialValuesCount)") } } @@ -681,7 +681,7 @@ extension Database { var sqliteStatement: SQLiteStatement? = nil let code = sqlite3_prepare_v2(sqliteConnection, statementStart, -1, &sqliteStatement, &statementEnd) guard code == SQLITE_OK else { - error = DatabaseError(code: code, message: lastErrorMessage, sql: sql) + error = DatabaseError(code: ResultCode(rawValue: code), message: lastErrorMessage, sql: sql) break } @@ -770,7 +770,7 @@ extension Database { if let message = error.message { sqlite3_result_error(context, message, -1) } - sqlite3_result_error_code(context, Int32(error.code)) + sqlite3_result_error_code(context, error.code.rawValue) } catch { sqlite3_result_error(context, "\(error)", -1) } @@ -778,7 +778,7 @@ extension Database { guard code == SQLITE_OK else { // Assume a GRDB bug: there is no point throwing any error. - fatalError(DatabaseError(code: code, message: lastErrorMessage).description) + fatalError(DatabaseError(code: ResultCode(rawValue: code), message: lastErrorMessage).description) } } @@ -793,7 +793,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(code: code, message: lastErrorMessage).description) + fatalError(DatabaseError(code: ResultCode(rawValue: code), message: lastErrorMessage).description) } } } @@ -882,7 +882,7 @@ extension Database { }, nil) guard code == SQLITE_OK else { // Assume a GRDB bug: there is no point throwing any error. - fatalError(DatabaseError(code: code, message: lastErrorMessage).description) + fatalError(DatabaseError(code: ResultCode(rawValue: code), message: lastErrorMessage).description) } } @@ -1626,7 +1626,7 @@ extension Database { // TODO: test that isInsideTransaction, savepointStack, transaction // observers, etc. are in good shape when such an implicit rollback // happens. - guard let underlyingError = underlyingError as? DatabaseError, [SQLITE_FULL, SQLITE_IOERR, SQLITE_BUSY, SQLITE_NOMEM].contains(Int32(underlyingError.code)) else { + guard let underlyingError = underlyingError as? DatabaseError, [.SQLITE_FULL, .SQLITE_IOERR, .SQLITE_BUSY, .SQLITE_NOMEM].contains(underlyingError.primaryResultCode) else { throw error } } @@ -2458,3 +2458,27 @@ class SavepointStack { } } } + +struct ForeignKeyErrors : Error { + let occurrences: [Row] +} +extension Database { + func inTransactionWithDeferredForeignKeys(_ kind: TransactionKind? = nil, _ block: () throws -> TransactionCompletion) throws { + try execute("PRAGMA foreign_keys = OFF") + defer { + // Not supposed to fail + try! execute("PRAGMA foreign_keys = ON") + } + try inTransaction(kind) { + let completion = try block() + if completion == .commit { + let rows = try Row.fetchAll(self, "PRAGMA foreign_key_check") + guard rows.isEmpty else { + throw ForeignKeyErrors(occurrences: rows) + } + } + return completion + } + } +} + diff --git a/GRDB/Core/DatabaseError.swift b/GRDB/Core/DatabaseError.swift index f7aff8a54e..ba9c40d343 100644 --- a/GRDB/Core/DatabaseError.swift +++ b/GRDB/Core/DatabaseError.swift @@ -18,11 +18,125 @@ import Foundation #endif #endif +public struct ResultCode : RawRepresentable, Equatable, CustomStringConvertible { + public let rawValue: Int32 + + public init(rawValue: Int32) { + self.rawValue = rawValue + } + + public var primaryResultCode: ResultCode { + return ResultCode(rawValue: rawValue & 0xFF) + } + + public var description: String { + return "\(rawValue) (\(String(cString: sqlite3_errstr(rawValue))))" + } + + public static func == (_ lhs: ResultCode, _ rhs: ResultCode) -> Bool { + return lhs.rawValue == rhs.rawValue + } + + public static let SQLITE_OK = ResultCode(rawValue: 0) // Successful result + public static let SQLITE_ERROR = ResultCode(rawValue: 1) // SQL error or missing database + public static let SQLITE_INTERNAL = ResultCode(rawValue: 2) // Internal logic error in SQLite + public static let SQLITE_PERM = ResultCode(rawValue: 3) // Access permission denied + public static let SQLITE_ABORT = ResultCode(rawValue: 4) // Callback routine requested an abort + public static let SQLITE_BUSY = ResultCode(rawValue: 5) // The database file is locked + public static let SQLITE_LOCKED = ResultCode(rawValue: 6) // A table in the database is locked + public static let SQLITE_NOMEM = ResultCode(rawValue: 7) // A malloc() failed + public static let SQLITE_READONLY = ResultCode(rawValue: 8) // Attempt to write a readonly database + public static let SQLITE_INTERRUPT = ResultCode(rawValue: 9) // Operation terminated by sqlite3_interrupt() + public static let SQLITE_IOERR = ResultCode(rawValue: 10) // Some kind of disk I/O error occurred + public static let SQLITE_CORRUPT = ResultCode(rawValue: 11) // The database disk image is malformed + public static let SQLITE_NOTFOUND = ResultCode(rawValue: 12) // Unknown opcode in sqlite3_file_control() + public static let SQLITE_FULL = ResultCode(rawValue: 13) // Insertion failed because database is full + public static let SQLITE_CANTOPEN = ResultCode(rawValue: 14) // Unable to open the database file + public static let SQLITE_PROTOCOL = ResultCode(rawValue: 15) // Database lock protocol error + public static let SQLITE_EMPTY = ResultCode(rawValue: 16) // Database is empty + public static let SQLITE_SCHEMA = ResultCode(rawValue: 17) // The database schema changed + public static let SQLITE_TOOBIG = ResultCode(rawValue: 18) // String or BLOB exceeds size limit + public static let SQLITE_CONSTRAINT = ResultCode(rawValue: 19) // Abort due to constraint violation + public static let SQLITE_MISMATCH = ResultCode(rawValue: 20) // Data type mismatch + public static let SQLITE_MISUSE = ResultCode(rawValue: 21) // Library used incorrectly + public static let SQLITE_NOLFS = ResultCode(rawValue: 22) // Uses OS features not supported on host + public static let SQLITE_AUTH = ResultCode(rawValue: 23) // Authorization denied + public static let SQLITE_FORMAT = ResultCode(rawValue: 24) // Auxiliary database format error + public static let SQLITE_RANGE = ResultCode(rawValue: 25) // 2nd parameter to sqlite3_bind out of range + public static let SQLITE_NOTADB = ResultCode(rawValue: 26) // File opened that is not a database file + public static let SQLITE_NOTICE = ResultCode(rawValue: 27) // Notifications from sqlite3_log() + public static let SQLITE_WARNING = ResultCode(rawValue: 28) // Warnings from sqlite3_log() + public static let SQLITE_ROW = ResultCode(rawValue: 100) // sqlite3_step() has another row ready + public static let SQLITE_DONE = ResultCode(rawValue: 101) // sqlite3_step() has finished executing + + public static let SQLITE_IOERR_READ = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (1<<8))) + public static let SQLITE_IOERR_SHORT_READ = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (2<<8))) + public static let SQLITE_IOERR_WRITE = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (3<<8))) + public static let SQLITE_IOERR_FSYNC = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (4<<8))) + public static let SQLITE_IOERR_DIR_FSYNC = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (5<<8))) + public static let SQLITE_IOERR_TRUNCATE = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (6<<8))) + public static let SQLITE_IOERR_FSTAT = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (7<<8))) + public static let SQLITE_IOERR_UNLOCK = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (8<<8))) + public static let SQLITE_IOERR_RDLOCK = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (9<<8))) + public static let SQLITE_IOERR_DELETE = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (10<<8))) + public static let SQLITE_IOERR_BLOCKED = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (11<<8))) + public static let SQLITE_IOERR_NOMEM = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (12<<8))) + public static let SQLITE_IOERR_ACCESS = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (13<<8))) + public static let SQLITE_IOERR_CHECKRESERVEDLOCK = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (14<<8))) + public static let SQLITE_IOERR_LOCK = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (15<<8))) + public static let SQLITE_IOERR_CLOSE = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (16<<8))) + public static let SQLITE_IOERR_DIR_CLOSE = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (17<<8))) + public static let SQLITE_IOERR_SHMOPEN = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (18<<8))) + public static let SQLITE_IOERR_SHMSIZE = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (19<<8))) + public static let SQLITE_IOERR_SHMLOCK = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (20<<8))) + public static let SQLITE_IOERR_SHMMAP = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (21<<8))) + public static let SQLITE_IOERR_SEEK = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (22<<8))) + public static let SQLITE_IOERR_DELETE_NOENT = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (23<<8))) + public static let SQLITE_IOERR_MMAP = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (24<<8))) + public static let SQLITE_IOERR_GETTEMPPATH = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (25<<8))) + public static let SQLITE_IOERR_CONVPATH = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (26<<8))) + public static let SQLITE_IOERR_VNODE = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (27<<8))) + public static let SQLITE_IOERR_AUTH = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (28<<8))) + public static let SQLITE_LOCKED_SHAREDCACHE = ResultCode(rawValue: (SQLITE_LOCKED.rawValue | (1<<8))) + public static let SQLITE_BUSY_RECOVERY = ResultCode(rawValue: (SQLITE_BUSY.rawValue | (1<<8))) + public static let SQLITE_BUSY_SNAPSHOT = ResultCode(rawValue: (SQLITE_BUSY.rawValue | (2<<8))) + public static let SQLITE_CANTOPEN_NOTEMPDIR = ResultCode(rawValue: (SQLITE_CANTOPEN.rawValue | (1<<8))) + public static let SQLITE_CANTOPEN_ISDIR = ResultCode(rawValue: (SQLITE_CANTOPEN.rawValue | (2<<8))) + public static let SQLITE_CANTOPEN_FULLPATH = ResultCode(rawValue: (SQLITE_CANTOPEN.rawValue | (3<<8))) + public static let SQLITE_CANTOPEN_CONVPATH = ResultCode(rawValue: (SQLITE_CANTOPEN.rawValue | (4<<8))) + public static let SQLITE_CORRUPT_VTAB = ResultCode(rawValue: (SQLITE_CORRUPT.rawValue | (1<<8))) + public static let SQLITE_READONLY_RECOVERY = ResultCode(rawValue: (SQLITE_READONLY.rawValue | (1<<8))) + public static let SQLITE_READONLY_CANTLOCK = ResultCode(rawValue: (SQLITE_READONLY.rawValue | (2<<8))) + public static let SQLITE_READONLY_ROLLBACK = ResultCode(rawValue: (SQLITE_READONLY.rawValue | (3<<8))) + public static let SQLITE_READONLY_DBMOVED = ResultCode(rawValue: (SQLITE_READONLY.rawValue | (4<<8))) + public static let SQLITE_ABORT_ROLLBACK = ResultCode(rawValue: (SQLITE_ABORT.rawValue | (2<<8))) + public static let SQLITE_CONSTRAINT_CHECK = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (1<<8))) + public static let SQLITE_CONSTRAINT_COMMITHOOK = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (2<<8))) + public static let SQLITE_CONSTRAINT_FOREIGNKEY = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (3<<8))) + public static let SQLITE_CONSTRAINT_FUNCTION = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (4<<8))) + public static let SQLITE_CONSTRAINT_NOTNULL = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (5<<8))) + public static let SQLITE_CONSTRAINT_PRIMARYKEY = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (6<<8))) + public static let SQLITE_CONSTRAINT_TRIGGER = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (7<<8))) + public static let SQLITE_CONSTRAINT_UNIQUE = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (8<<8))) + public static let SQLITE_CONSTRAINT_VTAB = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (9<<8))) + public static let SQLITE_CONSTRAINT_ROWID = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (10<<8))) + public static let SQLITE_NOTICE_RECOVER_WAL = ResultCode(rawValue: (SQLITE_NOTICE.rawValue | (1<<8))) + public static let SQLITE_NOTICE_RECOVER_ROLLBACK = ResultCode(rawValue: (SQLITE_NOTICE.rawValue | (2<<8))) + public static let SQLITE_WARNING_AUTOINDEX = ResultCode(rawValue: (SQLITE_WARNING.rawValue | (1<<8))) + public static let SQLITE_AUTH_USER = ResultCode(rawValue: (SQLITE_AUTH.rawValue | (1<<8))) + public static let SQLITE_OK_LOAD_PERMANENTLY = ResultCode(rawValue: (SQLITE_OK.rawValue | (1<<8))) + +} + /// DatabaseError wraps an SQLite error. public struct DatabaseError : Error { /// The SQLite error code (see https://www.sqlite.org/c3ref/c_abort.html). - public let code: Int32 + public let code: ResultCode + + public var primaryResultCode: ResultCode { + return code.primaryResultCode + } /// The SQLite error message. public let message: String? @@ -30,7 +144,7 @@ public struct DatabaseError : Error { /// The SQL query that yielded the error (if relevant). public let sql: String? - public init(code: Int32 = SQLITE_ERROR, message: String? = nil, sql: String? = nil, arguments: StatementArguments? = nil) { + public init(code: ResultCode = .SQLITE_ERROR, message: String? = nil, sql: String? = nil, arguments: StatementArguments? = nil) { self.code = code self.message = message self.sql = sql @@ -48,7 +162,7 @@ public struct DatabaseError : Error { extension DatabaseError: CustomStringConvertible { /// A textual representation of `self`. public var description: String { - var description = "SQLite error \(code)" + var description = "SQLite error \(code.rawValue)" if let sql = sql { description += " with statement `\(sql)`" } @@ -71,7 +185,7 @@ extension DatabaseError : CustomNSError { /// NSError bridging: the error code within the given domain. public var errorCode: Int { - return Int(code) + return Int(code.rawValue) } /// NSError bridging: the user-info dictionary. diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index d7a1fdbae7..818d4eccab 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(code: code, message: db.lastErrorMessage, sql: nil) + throw DatabaseError(code: ResultCode(rawValue: code), message: db.lastErrorMessage, sql: nil) } } } diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index 999456a307..8c4521b221 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -144,7 +144,7 @@ extension DatabaseReader { throw DatabaseError(code: dbDest.lastErrorCode, message: dbDest.lastErrorMessage) } guard Int(bitPattern: backup) != Int(SQLITE_ERROR) else { - throw DatabaseError(code: SQLITE_ERROR) + throw DatabaseError(code: .SQLITE_ERROR) } afterBackupInit?() @@ -158,7 +158,7 @@ extension DatabaseReader { case SQLITE_OK: afterBackupStep?() case let code: - throw DatabaseError(code: code, message: dbDest.lastErrorMessage) + throw DatabaseError(code: ResultCode(rawValue: code), message: dbDest.lastErrorMessage) } } } catch { @@ -170,7 +170,7 @@ extension DatabaseReader { case SQLITE_OK: break case let code: - throw DatabaseError(code: code, message: dbDest.lastErrorMessage) + throw DatabaseError(code: ResultCode(rawValue: code), message: dbDest.lastErrorMessage) } dbDest.clearSchemaCache() diff --git a/GRDB/Core/DatabaseValueConvertible.swift b/GRDB/Core/DatabaseValueConvertible.swift index 6e90dcf11c..ed4fc48034 100644 --- a/GRDB/Core/DatabaseValueConvertible.swift +++ b/GRDB/Core/DatabaseValueConvertible.swift @@ -121,7 +121,7 @@ extension DatabaseValueConvertible { if let value = Self.fromDatabaseValue(dbv) { return value } else { - throw DatabaseError(code: SQLITE_ERROR, message: "could not convert database value \(dbv) to \(Self.self)", sql: statement.sql, arguments: arguments) + throw DatabaseError(code: .SQLITE_ERROR, message: "could not convert database value \(dbv) to \(Self.self)", sql: statement.sql, arguments: arguments) } } } @@ -165,7 +165,7 @@ extension DatabaseValueConvertible { } else if dbv.isNull { return nil } else { - throw DatabaseError(code: SQLITE_ERROR, message: "could not convert database value \(dbv) to \(Self.self)", sql: statement.sql, arguments: arguments) + throw DatabaseError(code: .SQLITE_ERROR, message: "could not convert database value \(dbv) to \(Self.self)", sql: statement.sql, arguments: arguments) } } return try cursor.next() ?? nil @@ -336,7 +336,7 @@ extension Optional where Wrapped: DatabaseValueConvertible { } else if dbv.isNull { return nil } else { - throw DatabaseError(code: SQLITE_ERROR, message: "could not convert database value \(dbv) to \(Wrapped.self)", sql: statement.sql, arguments: arguments) + throw DatabaseError(code: .SQLITE_ERROR, message: "could not convert database value \(dbv) to \(Wrapped.self)", sql: statement.sql, arguments: arguments) } } } diff --git a/GRDB/Core/RowAdapter.swift b/GRDB/Core/RowAdapter.swift index b2af8e0d1c..4f97c05968 100644 --- a/GRDB/Core/RowAdapter.swift +++ b/GRDB/Core/RowAdapter.swift @@ -208,7 +208,7 @@ public struct ColumnMapping : RowAdapter { .map { (mappedColumn, baseColumn) -> (Int, String) in guard let index = layout.layoutIndex(ofColumn: baseColumn) else { let columnNames = layout.layoutColumns.map { $0.1 } - throw DatabaseError(code: SQLITE_MISUSE, message: "Mapping references missing column \(baseColumn). Valid column names are: \(columnNames.joined(separator: ", ")).") + throw DatabaseError(code: .SQLITE_MISUSE, message: "Mapping references missing column \(baseColumn). Valid column names are: \(columnNames.joined(separator: ", ")).") } let baseIndex = layout.layoutColumns[index].0 return (baseIndex, mappedColumn) diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index d8da471815..8f38ad1f93 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -62,12 +62,12 @@ public class Statement { } guard code == SQLITE_OK else { - throw DatabaseError(code: code, message: database.lastErrorMessage, sql: sql) + throw DatabaseError(code: ResultCode(rawValue: code), message: database.lastErrorMessage, sql: sql) } guard remainingSQL.isEmpty else { sqlite3_finalize(sqliteStatement) - throw DatabaseError(code: SQLITE_MISUSE, message: "Multiple statements found. To execute multiple statements, use Database.execute() instead.", sql: sql, arguments: nil) + throw DatabaseError(code: .SQLITE_MISUSE, message: "Multiple statements found. To execute multiple statements, use Database.execute() instead.", sql: sql, arguments: nil) } self.database = database @@ -86,7 +86,7 @@ public class Statement { // throwing any error. let code = sqlite3_reset(sqliteStatement) guard code == SQLITE_OK else { - fatalError(DatabaseError(code: code, message: database.lastErrorMessage, sql: sql).description) + fatalError(DatabaseError(code: ResultCode(rawValue: 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(code: code, message: database.lastErrorMessage, sql: sql).description) + fatalError(DatabaseError(code: ResultCode(rawValue: 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(code: code, message: database.lastErrorMessage, sql: sql).description) + fatalError(DatabaseError(code: ResultCode(rawValue: code), message: database.lastErrorMessage, sql: sql).description) } } @@ -327,7 +327,7 @@ public final class DatabaseCursor : Cursor { return try element() case let errorCode: statement.database.selectStatementDidFail(statement) - throw DatabaseError(code: errorCode, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments) + throw DatabaseError(code: ResultCode(rawValue: errorCode), message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments) } } } @@ -432,7 +432,7 @@ public final class UpdateStatement : Statement { // Let database rethrow eventual transaction observer error: try database.updateStatementDidFail(self) - throw DatabaseError(code: errorCode, message: database.lastErrorMessage, sql: sql, arguments: self.arguments) // Error uses self.arguments, not the optional arguments parameter. + throw DatabaseError(code: ResultCode(rawValue: errorCode), message: database.lastErrorMessage, sql: sql, arguments: self.arguments) // Error uses self.arguments, not the optional arguments parameter. } } } @@ -778,20 +778,20 @@ public struct StatementArguments { if let databaseValue = namedValues[argumentName] { return databaseValue } else if values.isEmpty { - throw DatabaseError(code: SQLITE_MISUSE, message: "missing statement argument: \(argumentName)", sql: statement.sql, arguments: nil) + throw DatabaseError(code: .SQLITE_MISUSE, message: "missing statement argument: \(argumentName)", sql: statement.sql, arguments: nil) } else { return values.removeFirst() } } else { if values.isEmpty { - throw DatabaseError(code: SQLITE_MISUSE, message: "wrong number of statement arguments: \(initialValuesCount)", sql: statement.sql, arguments: nil) + throw DatabaseError(code: .SQLITE_MISUSE, message: "wrong number of statement arguments: \(initialValuesCount)", sql: statement.sql, arguments: nil) } else { return values.removeFirst() } } } if !allowingRemainingValues && !values.isEmpty { - throw DatabaseError(code: SQLITE_MISUSE, message: "wrong number of statement arguments: \(initialValuesCount)", sql: statement.sql, arguments: nil) + throw DatabaseError(code: .SQLITE_MISUSE, message: "wrong number of statement arguments: \(initialValuesCount)", sql: statement.sql, arguments: nil) } return bindings } diff --git a/GRDB/Core/StatementColumnConvertible.swift b/GRDB/Core/StatementColumnConvertible.swift index 28673c448d..d5497124e2 100644 --- a/GRDB/Core/StatementColumnConvertible.swift +++ b/GRDB/Core/StatementColumnConvertible.swift @@ -77,7 +77,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { let sqliteStatement = statement.sqliteStatement return statement.fetchCursor(arguments: arguments) { if sqlite3_column_type(sqliteStatement, columnIndex) == SQLITE_NULL { - throw DatabaseError(code: SQLITE_ERROR, message: "could not convert database value NULL to \(Self.self)", sql: statement.sql, arguments: arguments) + throw DatabaseError(code: .SQLITE_ERROR, message: "could not convert database value NULL to \(Self.self)", sql: statement.sql, arguments: arguments) } return Self.init(sqliteStatement: sqliteStatement, index: columnIndex) } diff --git a/GRDB/FTS/FTS5Tokenizer.swift b/GRDB/FTS/FTS5Tokenizer.swift index c3b2209f47..fc153138ff 100644 --- a/GRDB/FTS/FTS5Tokenizer.swift +++ b/GRDB/FTS/FTS5Tokenizer.swift @@ -116,7 +116,7 @@ init(xTokenizer: fts5_tokenizer, contextPointer: UnsafeMutableRawPointer?, arguments: [String]) throws { guard let xCreate = xTokenizer.xCreate else { - throw DatabaseError(code: SQLITE_ERROR, message: "nil fts5_tokenizer.xCreate") + throw DatabaseError(code: .SQLITE_ERROR, message: "nil fts5_tokenizer.xCreate") } self.xTokenizer = xTokenizer @@ -189,7 +189,7 @@ /// } public func makeTokenizer(_ descriptor: FTS5TokenizerDescriptor) throws -> FTS5Tokenizer { guard let api = FTS5.api(self) else { - throw DatabaseError(code: SQLITE_MISUSE, message: "FTS5 API not found") + throw DatabaseError(code: .SQLITE_MISUSE, message: "FTS5 API not found") } let xTokenizerPointer: UnsafeMutablePointer = .allocate(capacity: 1) diff --git a/GRDB/Migrations/Migration.swift b/GRDB/Migrations/Migration.swift index acce837a70..bb5b53cfcb 100644 --- a/GRDB/Migrations/Migration.swift +++ b/GRDB/Migrations/Migration.swift @@ -86,7 +86,7 @@ struct Migration { // // Let's turn any violation into an SQLITE_CONSTRAINT // error, and rollback the transaction. - throw DatabaseError(code: SQLITE_CONSTRAINT, message: "FOREIGN KEY constraint failed") + throw DatabaseError(code: .SQLITE_CONSTRAINT, message: "FOREIGN KEY constraint failed") } // > 11. Commit the transaction started in step 2. diff --git a/GRDB/Record/RowConvertible.swift b/GRDB/Record/RowConvertible.swift index cbbbe5cb14..499af35601 100644 --- a/GRDB/Record/RowConvertible.swift +++ b/GRDB/Record/RowConvertible.swift @@ -454,7 +454,7 @@ extension RowConvertible where Self: TableMapping { GRDBPrecondition(dictionary.count > 0, "Invalid empty key dictionary") let columns = Array(dictionary.keys) guard let orderedColumns = try db.columnsForUniqueKey(columns, in: databaseTableName) else { - let error = DatabaseError(code: SQLITE_MISUSE, message: "table \(databaseTableName) has no unique index on column(s) \(columns.sorted().joined(separator: ", "))") + let error = DatabaseError(code: .SQLITE_MISUSE, message: "table \(databaseTableName) has no unique index on column(s) \(columns.sorted().joined(separator: ", "))") if fatalErrorOnMissingUniqueIndex { // Programmer error fatalError(error.description) diff --git a/GRDB/Record/TableMapping.swift b/GRDB/Record/TableMapping.swift index 5f3b56b37f..b52ea70385 100644 --- a/GRDB/Record/TableMapping.swift +++ b/GRDB/Record/TableMapping.swift @@ -206,7 +206,7 @@ extension TableMapping { GRDBPrecondition(dictionary.count > 0, "Invalid empty key dictionary") let columns = Array(dictionary.keys) guard let orderedColumns = try db.columnsForUniqueKey(columns, in: databaseTableName) else { - let error = DatabaseError(code: SQLITE_MISUSE, message: "table \(databaseTableName) has no unique index on column(s) \(columns.sorted().joined(separator: ", "))") + let error = DatabaseError(code: .SQLITE_MISUSE, message: "table \(databaseTableName) has no unique index on column(s) \(columns.sorted().joined(separator: ", "))") if fatalErrorOnMissingUniqueIndex { fatalError(error.description) } else { diff --git a/Tests/Private/DatabasePoolSchemaCacheTests.swift b/Tests/Private/DatabasePoolSchemaCacheTests.swift index c984cdeed8..3e46cfc4fb 100644 --- a/Tests/Private/DatabasePoolSchemaCacheTests.swift +++ b/Tests/Private/DatabasePoolSchemaCacheTests.swift @@ -95,7 +95,7 @@ class DatabasePoolSchemaCacheTests : GRDBTestCase { _ = try db.primaryKey("items") XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message!, "no such table: items") XCTAssertEqual(error.description, "SQLite error 1: no such table: items") } diff --git a/Tests/Private/DatabaseQueueSchemaCacheTests.swift b/Tests/Private/DatabaseQueueSchemaCacheTests.swift index 7854ee8b3a..9971dc16ba 100644 --- a/Tests/Private/DatabaseQueueSchemaCacheTests.swift +++ b/Tests/Private/DatabaseQueueSchemaCacheTests.swift @@ -46,7 +46,7 @@ class DatabaseQueueSchemaCacheTests : GRDBTestCase { _ = try db.primaryKey("items") XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message!, "no such table: items") XCTAssertEqual(error.description, "SQLite error 1: no such table: items") } diff --git a/Tests/Private/RecordUniqueIndexTests.swift b/Tests/Private/RecordUniqueIndexTests.swift index 611b718109..525cc844df 100644 --- a/Tests/Private/RecordUniqueIndexTests.swift +++ b/Tests/Private/RecordUniqueIndexTests.swift @@ -27,7 +27,7 @@ class RecordUniqueIndexTests: GRDBTestCase { _ = try Person.makeFetchByKeyStatement(db, keys: [["id": nil, "email": nil]], fatalErrorOnMissingUniqueIndex: false) XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 21) // SQLITE_MISUSE + XCTAssertEqual(error.code, .SQLITE_MISUSE) XCTAssertEqual(error.message!, "table persons has no unique index on column(s) email, id") XCTAssertEqual(error.description, "SQLite error 21: table persons has no unique index on column(s) email, id") } @@ -35,7 +35,7 @@ class RecordUniqueIndexTests: GRDBTestCase { _ = try Person.makeFetchByKeyStatement(db, keys: [["name": nil]], fatalErrorOnMissingUniqueIndex: false) XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 21) // SQLITE_MISUSE + XCTAssertEqual(error.code, .SQLITE_MISUSE) XCTAssertEqual(error.message!, "table persons has no unique index on column(s) name") XCTAssertEqual(error.description, "SQLite error 21: table persons has no unique index on column(s) name") } @@ -55,7 +55,7 @@ class RecordUniqueIndexTests: GRDBTestCase { _ = try Person.makeDeleteByKeyStatement(db, keys: [["id": nil, "email": nil]], fatalErrorOnMissingUniqueIndex: false) XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 21) // SQLITE_MISUSE + XCTAssertEqual(error.code, .SQLITE_MISUSE) XCTAssertEqual(error.message!, "table persons has no unique index on column(s) email, id") XCTAssertEqual(error.description, "SQLite error 21: table persons has no unique index on column(s) email, id") } @@ -63,7 +63,7 @@ class RecordUniqueIndexTests: GRDBTestCase { _ = try Person.makeDeleteByKeyStatement(db, keys: [["name": nil]], fatalErrorOnMissingUniqueIndex: false) XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 21) // SQLITE_MISUSE + XCTAssertEqual(error.code, .SQLITE_MISUSE) XCTAssertEqual(error.message!, "table persons has no unique index on column(s) name") XCTAssertEqual(error.description, "SQLite error 21: table persons has no unique index on column(s) name") } diff --git a/Tests/Public/Core/Database/DatabaseTests.swift b/Tests/Public/Core/Database/DatabaseTests.swift index a2972a1c23..0e5265e9d9 100644 --- a/Tests/Public/Core/Database/DatabaseTests.swift +++ b/Tests/Public/Core/Database/DatabaseTests.swift @@ -316,7 +316,7 @@ class DatabaseTests : GRDBTestCase { } XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 19) // SQLITE_CONSTRAINT + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) XCTAssertEqual(error.message!, "FOREIGN KEY constraint failed") XCTAssertEqual(error.sql!, "COMMIT TRANSACTION") XCTAssertEqual(error.description, "SQLite error 19 with statement `COMMIT TRANSACTION`: FOREIGN KEY constraint failed") diff --git a/Tests/Public/Core/Database/ReadOnlyDatabaseTests.swift b/Tests/Public/Core/Database/ReadOnlyDatabaseTests.swift index 716a24ea5f..c75d649aba 100644 --- a/Tests/Public/Core/Database/ReadOnlyDatabaseTests.swift +++ b/Tests/Public/Core/Database/ReadOnlyDatabaseTests.swift @@ -28,7 +28,7 @@ class ReadOnlyDatabaseTests : GRDBTestCase { } XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 8) // SQLITE_READONLY + XCTAssertEqual(error.code, .SQLITE_READONLY) XCTAssertEqual(error.message!, "attempt to write a readonly database") XCTAssertEqual(error.sql!, "CREATE TABLE items (id INTEGER PRIMARY KEY)") XCTAssertEqual(error.description, "SQLite error 8 with statement `CREATE TABLE items (id INTEGER PRIMARY KEY)`: attempt to write a readonly database") diff --git a/Tests/Public/Core/DatabaseCursor/DatabaseCursorTests.swift b/Tests/Public/Core/DatabaseCursor/DatabaseCursorTests.swift index 1970240410..25b3e190cb 100644 --- a/Tests/Public/Core/DatabaseCursor/DatabaseCursorTests.swift +++ b/Tests/Public/Core/DatabaseCursor/DatabaseCursorTests.swift @@ -47,7 +47,7 @@ class DatabaseCursorTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, "SELECT 1 UNION ALL SELECT throw()") XCTAssertEqual(error.description, "SQLite error 1 with statement `SELECT 1 UNION ALL SELECT throw()`: \(customError)") diff --git a/Tests/Public/Core/DatabaseError/DatabaseErrorTests.swift b/Tests/Public/Core/DatabaseError/DatabaseErrorTests.swift index afd7306483..042452b27e 100644 --- a/Tests/Public/Core/DatabaseError/DatabaseErrorTests.swift +++ b/Tests/Public/Core/DatabaseError/DatabaseErrorTests.swift @@ -22,7 +22,7 @@ class DatabaseErrorTests: GRDBTestCase { return .commit } } catch let error as DatabaseError { - XCTAssertEqual(error.code, 19) // SQLITE_CONSTRAINT + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) XCTAssertEqual(error.message!.lowercased(), "foreign key constraint failed") // lowercased: accept multiple SQLite version XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (?, ?)") XCTAssertEqual(error.description.lowercased(), "sqlite error 19 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed") @@ -56,7 +56,7 @@ class DatabaseErrorTests: GRDBTestCase { } } } catch let error as DatabaseError { - XCTAssertEqual(error.code, 19) // SQLITE_CONSTRAINT + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) XCTAssertEqual(error.message!.lowercased(), "foreign key constraint failed") // lowercased: accept multiple SQLite version XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (?, ?)") XCTAssertEqual(error.description.lowercased(), "sqlite error 19 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed") @@ -78,7 +78,7 @@ class DatabaseErrorTests: GRDBTestCase { try db.execute("INSERT INTO pets (masterId, name) VALUES (?, ?)", arguments: [1, "Bobby"]) XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 19) // SQLITE_CONSTRAINT + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) XCTAssertEqual(error.message!.lowercased(), "foreign key constraint failed") // lowercased: accept multiple SQLite version XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (?, ?)") XCTAssertEqual(error.description.lowercased(), "sqlite error 19 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed") @@ -92,7 +92,7 @@ class DatabaseErrorTests: GRDBTestCase { try statement.execute(arguments: [1, "Bobby"]) XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 19) // SQLITE_CONSTRAINT + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) XCTAssertEqual(error.message!.lowercased(), "foreign key constraint failed") // lowercased: accept multiple SQLite version XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (?, ?)") XCTAssertEqual(error.description.lowercased(), "sqlite error 19 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed") @@ -107,7 +107,7 @@ class DatabaseErrorTests: GRDBTestCase { try statement.execute() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 19) // SQLITE_CONSTRAINT + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) XCTAssertEqual(error.message!.lowercased(), "foreign key constraint failed") // lowercased: accept multiple SQLite version XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (?, ?)") XCTAssertEqual(error.description.lowercased(), "sqlite error 19 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed") @@ -127,7 +127,7 @@ class DatabaseErrorTests: GRDBTestCase { "INSERT INTO pets (masterId, name) VALUES (1, 'Bobby')") XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 19) // SQLITE_CONSTRAINT + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) XCTAssertEqual(error.message!.lowercased(), "foreign key constraint failed") // lowercased: accept multiple SQLite version XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (1, 'Bobby')") XCTAssertEqual(error.description.lowercased(), "sqlite error 19 with statement `insert into pets (masterid, name) values (1, 'bobby')`: foreign key constraint failed") diff --git a/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift b/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift index 3f28ca9000..707754cfa3 100644 --- a/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift +++ b/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift @@ -248,7 +248,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { try db.execute("BEGIN DEFERRED TRANSACTION") XCTFail("Expected error") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message!, "cannot start a transaction within a transaction") XCTAssertEqual(error.sql!, "BEGIN DEFERRED TRANSACTION") XCTAssertEqual(error.description, "SQLite error 1 with statement `BEGIN DEFERRED TRANSACTION`: cannot start a transaction within a transaction") @@ -281,7 +281,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { try dbPool.read { _ in } XCTFail("Expected error") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 5) // SQLITE_BUSY + XCTAssertEqual(error.code, .SQLITE_BUSY) XCTAssertEqual(error.message!, "database is locked") XCTAssertTrue(error.sql == nil) XCTAssertEqual(error.description, "SQLite error 5: database is locked") @@ -897,7 +897,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { } XCTFail("Expected error") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 5) // SQLITE_BUSY + XCTAssertEqual(error.code, .SQLITE_BUSY) XCTAssertEqual(error.message!, "database is locked") XCTAssertTrue(error.sql == nil) XCTAssertEqual(error.description, "SQLite error 5: database is locked") diff --git a/Tests/Public/Core/DatabaseQueue/DatabaseQueueConcurrencyTests.swift b/Tests/Public/Core/DatabaseQueue/DatabaseQueueConcurrencyTests.swift index 59001196f6..744fd381e0 100644 --- a/Tests/Public/Core/DatabaseQueue/DatabaseQueueConcurrencyTests.swift +++ b/Tests/Public/Core/DatabaseQueue/DatabaseQueueConcurrencyTests.swift @@ -113,7 +113,7 @@ class ConcurrencyTests: GRDBTestCase { _ = group.wait(timeout: .distantFuture) if let concurrencyError = concurrencyError { - XCTAssertEqual(concurrencyError.code, 5) // SQLITE_BUSY + XCTAssertEqual(concurrencyError.code, .SQLITE_BUSY) XCTAssertEqual(concurrencyError.sql, "INSERT INTO stuffs (id) VALUES (NULL)") } else { XCTFail("Expected concurrency error") @@ -171,7 +171,7 @@ class ConcurrencyTests: GRDBTestCase { _ = group.wait(timeout: .distantFuture) if let concurrencyError = concurrencyError { - XCTAssertEqual(concurrencyError.code, 5) // SQLITE_BUSY + XCTAssertEqual(concurrencyError.code, .SQLITE_BUSY) XCTAssertEqual(concurrencyError.sql, "BEGIN EXCLUSIVE TRANSACTION") } else { XCTFail("Expected concurrency error") @@ -229,7 +229,7 @@ class ConcurrencyTests: GRDBTestCase { _ = group.wait(timeout: .distantFuture) if let concurrencyError = concurrencyError { - XCTAssertEqual(concurrencyError.code, 5) // SQLITE_BUSY + XCTAssertEqual(concurrencyError.code, .SQLITE_BUSY) XCTAssertEqual(concurrencyError.sql, "BEGIN IMMEDIATE TRANSACTION") } else { XCTFail("Expected concurrency error") diff --git a/Tests/Public/Core/DatabaseQueue/DatabaseQueueTests.swift b/Tests/Public/Core/DatabaseQueue/DatabaseQueueTests.swift index 56e1789ece..df3f12eef5 100644 --- a/Tests/Public/Core/DatabaseQueue/DatabaseQueueTests.swift +++ b/Tests/Public/Core/DatabaseQueue/DatabaseQueueTests.swift @@ -21,7 +21,7 @@ class DatabaseQueueTests: GRDBTestCase { _ = try DatabaseQueue(path: url.path) XCTFail("Expected error") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 26) // SQLITE_NOTADB + XCTAssertEqual(error.code, .SQLITE_NOTADB) XCTAssertEqual(error.message!.lowercased(), "file is encrypted or is not a database") // lowercased: accept multiple SQLite version XCTAssertTrue(error.sql == nil) XCTAssertEqual(error.description.lowercased(), "sqlite error 26: file is encrypted or is not a database") @@ -55,7 +55,7 @@ class DatabaseQueueTests: GRDBTestCase { } catch let error as DatabaseError { // expected error - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message!.lowercased(), "no such function: succ") // lowercaseString: accept multiple SQLite version XCTAssertEqual(error.sql!, "SELECT succ(1)") XCTAssertEqual(error.description.lowercased(), "sqlite error 1 with statement `select succ(1)`: no such function: succ") @@ -86,7 +86,7 @@ class DatabaseQueueTests: GRDBTestCase { } catch let error as DatabaseError { // expected error - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message!.lowercased(), "no such collation sequence: test_collation_foo") // lowercaseString: accept multiple SQLite version XCTAssertEqual(error.sql!, "CREATE TABLE files_fail (name TEXT COLLATE TEST_COLLATION_FOO)") XCTAssertEqual(error.description.lowercased(), "sqlite error 1 with statement `create table files_fail (name text collate test_collation_foo)`: no such collation sequence: test_collation_foo") diff --git a/Tests/Public/Core/DatabaseValueConvertible/DatabaseValueConvertibleFetchTests.swift b/Tests/Public/Core/DatabaseValueConvertible/DatabaseValueConvertibleFetchTests.swift index baff6a6b40..8b3789cd6f 100644 --- a/Tests/Public/Core/DatabaseValueConvertible/DatabaseValueConvertibleFetchTests.swift +++ b/Tests/Public/Core/DatabaseValueConvertible/DatabaseValueConvertibleFetchTests.swift @@ -68,7 +68,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "could not convert database value NULL to \(Fetched.self)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: could not convert database value NULL to \(Fetched.self)") @@ -77,7 +77,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "could not convert database value \"foo\" to \(Fetched.self)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: could not convert database value \"foo\" to \(Fetched.self)") @@ -118,7 +118,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -127,7 +127,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 21) // SQLITE_MISUSE + XCTAssertEqual(error.code, .SQLITE_MISUSE) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 21 with statement `\(sql)`: \(customError)") @@ -161,7 +161,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try cursor() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") @@ -223,7 +223,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "could not convert database value NULL to \(Fetched.self)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: could not convert database value NULL to \(Fetched.self)") @@ -261,7 +261,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -295,7 +295,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") @@ -403,7 +403,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try value() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "could not convert database value \"foo\" to \(Fetched.self)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: could not convert database value \"foo\" to \(Fetched.self)") @@ -441,7 +441,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try value() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -475,7 +475,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try value() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") @@ -543,7 +543,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "could not convert database value \"foo\" to \(Fetched.self)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: could not convert database value \"foo\" to \(Fetched.self)") @@ -584,7 +584,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -593,7 +593,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 21) // SQLITE_MISUSE + XCTAssertEqual(error.code, .SQLITE_MISUSE) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 21 with statement `\(sql)`: \(customError)") @@ -627,7 +627,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try cursor() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") @@ -691,7 +691,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "could not convert database value \"foo\" to \(Fetched.self)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: could not convert database value \"foo\" to \(Fetched.self)") @@ -729,7 +729,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -763,7 +763,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") diff --git a/Tests/Public/Core/Function/FunctionTests.swift b/Tests/Public/Core/Function/FunctionTests.swift index 502a51d4ae..38c340f339 100644 --- a/Tests/Public/Core/Function/FunctionTests.swift +++ b/Tests/Public/Core/Function/FunctionTests.swift @@ -305,7 +305,7 @@ class FunctionTests: GRDBTestCase { try db.execute("INSERT INTO items VALUES (f(1))") XCTFail("Expected DatabaseError") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "custom error message") } } @@ -316,7 +316,7 @@ class FunctionTests: GRDBTestCase { assertNoError { let dbQueue = try makeDatabaseQueue() let fn = DatabaseFunction("f", argumentCount: 1) { databaseValues in - throw DatabaseError(code: 123) + throw DatabaseError(code: ResultCode(rawValue: 123)) } dbQueue.add(function: fn) try dbQueue.inDatabase { db in @@ -325,7 +325,7 @@ class FunctionTests: GRDBTestCase { try db.execute("INSERT INTO items VALUES (f(1))") XCTFail("Expected DatabaseError") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 123) + XCTAssertEqual(error.code.rawValue, 123) XCTAssertEqual(error.message, "unknown error") } } @@ -336,7 +336,7 @@ class FunctionTests: GRDBTestCase { assertNoError { let dbQueue = try makeDatabaseQueue() let fn = DatabaseFunction("f", argumentCount: 1) { databaseValues in - throw DatabaseError(code: 123, message: "custom error message") + throw DatabaseError(code: ResultCode(rawValue: 123), message: "custom error message") } dbQueue.add(function: fn) try dbQueue.inDatabase { db in @@ -345,7 +345,7 @@ class FunctionTests: GRDBTestCase { try db.execute("INSERT INTO items VALUES (f(1))") XCTFail("Expected DatabaseError") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 123) + XCTAssertEqual(error.code.rawValue, 123) XCTAssertEqual(error.message, "custom error message") } } @@ -365,7 +365,7 @@ class FunctionTests: GRDBTestCase { try db.execute("INSERT INTO items VALUES (f(1))") XCTFail("Expected DatabaseError") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertTrue(error.message!.contains("CustomErrorDomain")) XCTAssertTrue(error.message!.contains("123")) XCTAssertTrue(error.message!.contains("custom error message")) diff --git a/Tests/Public/Core/Persistable/PersistableTests.swift b/Tests/Public/Core/Persistable/PersistableTests.swift index dd8ee049d6..b12ccedb18 100644 --- a/Tests/Public/Core/Persistable/PersistableTests.swift +++ b/Tests/Public/Core/Persistable/PersistableTests.swift @@ -793,7 +793,7 @@ class PersistableTests: GRDBTestCase { do { try Citizenship(personID: person.id!, countryIsoCode: "US").insert(db) } catch let error as DatabaseError { - XCTAssertEqual(error.code, 19) // SQLITE_CONSTRAINT + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) } try Citizenship(personID: person.id!, countryIsoCode: "FR").insert(db) } diff --git a/Tests/Public/Core/Row/RowConvertibleTests.swift b/Tests/Public/Core/Row/RowConvertibleTests.swift index 16fc8e2eb0..5d038eaf0a 100644 --- a/Tests/Public/Core/Row/RowConvertibleTests.swift +++ b/Tests/Public/Core/Row/RowConvertibleTests.swift @@ -86,7 +86,7 @@ class RowConvertibleTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -95,7 +95,7 @@ class RowConvertibleTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 21) // SQLITE_MISUSE + XCTAssertEqual(error.code, .SQLITE_MISUSE) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 21 with statement `\(sql)`: \(customError)") @@ -129,7 +129,7 @@ class RowConvertibleTests: GRDBTestCase { _ = try cursor() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") @@ -195,7 +195,7 @@ class RowConvertibleTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -229,7 +229,7 @@ class RowConvertibleTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") @@ -319,7 +319,7 @@ class RowConvertibleTests: GRDBTestCase { _ = try value() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -353,7 +353,7 @@ class RowConvertibleTests: GRDBTestCase { _ = try value() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") diff --git a/Tests/Public/Core/Row/RowFetchTests.swift b/Tests/Public/Core/Row/RowFetchTests.swift index 6307b68f8d..e23e3cc987 100644 --- a/Tests/Public/Core/Row/RowFetchTests.swift +++ b/Tests/Public/Core/Row/RowFetchTests.swift @@ -57,7 +57,7 @@ class RowFetchTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -66,7 +66,7 @@ class RowFetchTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 21) // SQLITE_MISUSE + XCTAssertEqual(error.code, .SQLITE_MISUSE) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 21 with statement `\(sql)`: \(customError)") @@ -100,7 +100,7 @@ class RowFetchTests: GRDBTestCase { _ = try cursor() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") @@ -165,7 +165,7 @@ class RowFetchTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -199,7 +199,7 @@ class RowFetchTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") @@ -288,7 +288,7 @@ class RowFetchTests: GRDBTestCase { _ = try value() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -322,7 +322,7 @@ class RowFetchTests: GRDBTestCase { _ = try value() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") diff --git a/Tests/Public/Core/Savepoint/SavepointTests.swift b/Tests/Public/Core/Savepoint/SavepointTests.swift index 6a25ed6128..3e32835705 100644 --- a/Tests/Public/Core/Savepoint/SavepointTests.swift +++ b/Tests/Public/Core/Savepoint/SavepointTests.swift @@ -592,11 +592,11 @@ class SavepointTests: GRDBTestCase { do { try db.inSavepoint { try insertItem(db, name: "item1") - throw DatabaseError(code: 123) + throw DatabaseError(code: ResultCode(rawValue: 123)) } XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 123) + XCTAssertEqual(error.code.rawValue, 123) } try db.inSavepoint { diff --git a/Tests/Public/Core/Statement/SelectStatementTests.swift b/Tests/Public/Core/Statement/SelectStatementTests.swift index 5ad954a446..51778e0a3f 100644 --- a/Tests/Public/Core/Statement/SelectStatementTests.swift +++ b/Tests/Public/Core/Statement/SelectStatementTests.swift @@ -95,7 +95,7 @@ class SelectStatementTests : GRDBTestCase { _ = try db.makeSelectStatement("SELECT * FROM blah") XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message!, "no such table: blah") XCTAssertEqual(error.sql!, "SELECT * FROM blah") XCTAssertEqual(error.description, "SQLite error 1 with statement `SELECT * FROM blah`: no such table: blah") @@ -125,7 +125,7 @@ class SelectStatementTests : GRDBTestCase { _ = try String.fetchAll(db.cachedSelectStatement(sql)) XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message!, "boom") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: boom") diff --git a/Tests/Public/Core/Statement/UpdateStatementTests.swift b/Tests/Public/Core/Statement/UpdateStatementTests.swift index e791358ff8..1cf488a355 100644 --- a/Tests/Public/Core/Statement/UpdateStatementTests.swift +++ b/Tests/Public/Core/Statement/UpdateStatementTests.swift @@ -293,7 +293,7 @@ class UpdateStatementTests : GRDBTestCase { _ = try db.makeUpdateStatement("UPDATE blah SET id = 12") XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message!, "no such table: blah") XCTAssertEqual(error.sql!, "UPDATE blah SET id = 12") XCTAssertEqual(error.description, "SQLite error 1 with statement `UPDATE blah SET id = 12`: no such table: blah") @@ -310,7 +310,7 @@ class UpdateStatementTests : GRDBTestCase { _ = try db.makeUpdateStatement("UPDATE persons SET age = 1; UPDATE persons SET age = 2;") XCTFail("Expected error") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 21) // SQLITE_MISUSE + XCTAssertEqual(error.code, .SQLITE_MISUSE) XCTAssertEqual(error.message!, "Multiple statements found. To execute multiple statements, use Database.execute() instead.") XCTAssertEqual(error.sql!, "UPDATE persons SET age = 1; UPDATE persons SET age = 2;") XCTAssertEqual(error.description, "SQLite error 21 with statement `UPDATE persons SET age = 1; UPDATE persons SET age = 2;`: Multiple statements found. To execute multiple statements, use Database.execute() instead.") @@ -327,7 +327,7 @@ class UpdateStatementTests : GRDBTestCase { _ = try db.makeUpdateStatement("UPDATE persons SET age = 1;x") XCTFail("Expected error") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 21) // SQLITE_MISUSE + XCTAssertEqual(error.code, .SQLITE_MISUSE) XCTAssertEqual(error.message!, "Multiple statements found. To execute multiple statements, use Database.execute() instead.") XCTAssertEqual(error.sql!, "UPDATE persons SET age = 1;x") XCTAssertEqual(error.description, "SQLite error 21 with statement `UPDATE persons SET age = 1;x`: Multiple statements found. To execute multiple statements, use Database.execute() instead.") diff --git a/Tests/Public/Core/StatementColumnConvertible/StatementColumnConvertibleFetchTests.swift b/Tests/Public/Core/StatementColumnConvertible/StatementColumnConvertibleFetchTests.swift index 3ec678e56e..5c1c48993e 100644 --- a/Tests/Public/Core/StatementColumnConvertible/StatementColumnConvertibleFetchTests.swift +++ b/Tests/Public/Core/StatementColumnConvertible/StatementColumnConvertibleFetchTests.swift @@ -144,7 +144,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "could not convert database value NULL to \(Fetched.self)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: could not convert database value NULL to \(Fetched.self)") @@ -190,7 +190,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -199,7 +199,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 21) // SQLITE_MISUSE + XCTAssertEqual(error.code, .SQLITE_MISUSE) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 21 with statement `\(sql)`: \(customError)") @@ -233,7 +233,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { _ = try cursor() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") @@ -296,7 +296,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "could not convert database value NULL to \(Fetched.self)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: could not convert database value NULL to \(Fetched.self)") @@ -334,7 +334,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -368,7 +368,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") @@ -478,7 +478,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { _ = try value() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -512,7 +512,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { _ = try value() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") @@ -586,7 +586,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { _ = try cursor.next() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "could not convert database value \"foo\" to \(Fetched.self)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: could not convert database value \"foo\" to \(Fetched.self)") @@ -625,7 +625,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { _ = try cursor() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") @@ -693,7 +693,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "could not convert database value \"foo\" to \(Fetched.self)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: could not convert database value \"foo\" to \(Fetched.self)") @@ -731,7 +731,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "\(customError)") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)") @@ -765,7 +765,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase { _ = try array() XCTFail() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: nonExistingTable") XCTAssertEqual(error.sql!, sql) XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: no such table: nonExistingTable") diff --git a/Tests/Public/Core/TransactionObserver/TransactionObserverTests.swift b/Tests/Public/Core/TransactionObserver/TransactionObserverTests.swift index 4324f9e941..a31116e8e2 100644 --- a/Tests/Public/Core/TransactionObserver/TransactionObserverTests.swift +++ b/Tests/Public/Core/TransactionObserver/TransactionObserverTests.swift @@ -796,7 +796,7 @@ class TransactionObserverTests: GRDBTestCase { try Artwork(title: "meh").save(db) XCTFail("Expected Error") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 19) + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) #if SQLITE_ENABLE_PREUPDATE_HOOK XCTAssertEqual(observer.willChangeCount, 0) #endif @@ -809,7 +809,7 @@ class TransactionObserverTests: GRDBTestCase { } XCTFail("Expected Error") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 19) + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) #if SQLITE_ENABLE_PREUPDATE_HOOK XCTAssertEqual(observer.willChangeCount, 0) #endif @@ -834,7 +834,7 @@ class TransactionObserverTests: GRDBTestCase { XCTFail("Expected Error") } catch let error as DatabaseError { // Immediate constraint check has failed. - XCTAssertEqual(error.code, 19) + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) #if SQLITE_ENABLE_PREUPDATE_HOOK XCTAssertEqual(observer.willChangeCount, 0) #endif @@ -848,7 +848,7 @@ class TransactionObserverTests: GRDBTestCase { } XCTFail("Expected Error") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 19) + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) #if SQLITE_ENABLE_PREUPDATE_HOOK XCTAssertEqual(observer.willChangeCount, 0) #endif @@ -930,7 +930,7 @@ class TransactionObserverTests: GRDBTestCase { try Artwork(title: "meh").save(db) XCTFail("Expected Error") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 19) + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) #if SQLITE_ENABLE_PREUPDATE_HOOK XCTAssertEqual(observer.willChangeCount, 0) #endif @@ -943,7 +943,7 @@ class TransactionObserverTests: GRDBTestCase { } XCTFail("Expected Error") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 19) + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) #if SQLITE_ENABLE_PREUPDATE_HOOK XCTAssertEqual(observer.willChangeCount, 0) #endif @@ -969,7 +969,7 @@ class TransactionObserverTests: GRDBTestCase { XCTFail("Expected Error") } catch let error as DatabaseError { // Immediate constraint check has failed. - XCTAssertEqual(error.code, 19) + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) #if SQLITE_ENABLE_PREUPDATE_HOOK XCTAssertEqual(observer.willChangeCount, 0) #endif @@ -983,7 +983,7 @@ class TransactionObserverTests: GRDBTestCase { } XCTFail("Expected Error") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 19) + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) #if SQLITE_ENABLE_PREUPDATE_HOOK XCTAssertEqual(observer.willChangeCount, 0) #endif diff --git a/Tests/Public/Encryption/EncryptionTests.swift b/Tests/Public/Encryption/EncryptionTests.swift index 91c5cb0536..a25b85742c 100644 --- a/Tests/Public/Encryption/EncryptionTests.swift +++ b/Tests/Public/Encryption/EncryptionTests.swift @@ -40,7 +40,7 @@ class EncryptionTests: GRDBTestCase { do { _ = try makeDatabaseQueue() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 26) // SQLITE_NOTADB + XCTAssertEqual(error.code, .SQLITE_NOTADB) XCTAssertEqual(error.message!, "file is encrypted or is not a database") XCTAssertTrue(error.sql == nil) XCTAssertEqual(error.description, "SQLite error 26: file is encrypted or is not a database") @@ -66,7 +66,7 @@ class EncryptionTests: GRDBTestCase { do { _ = try makeDatabaseQueue() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 26) // SQLITE_NOTADB + XCTAssertEqual(error.code, .SQLITE_NOTADB) XCTAssertEqual(error.message!, "file is encrypted or is not a database") XCTAssertTrue(error.sql == nil) XCTAssertEqual(error.description, "SQLite error 26: file is encrypted or is not a database") @@ -146,7 +146,7 @@ class EncryptionTests: GRDBTestCase { do { _ = try makeDatabasePool() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 26) // SQLITE_NOTADB + XCTAssertEqual(error.code, .SQLITE_NOTADB) XCTAssertEqual(error.message!, "file is encrypted or is not a database") XCTAssertTrue(error.sql == nil) XCTAssertEqual(error.description, "SQLite error 26: file is encrypted or is not a database") @@ -171,7 +171,7 @@ class EncryptionTests: GRDBTestCase { do { _ = try makeDatabasePool() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 26) // SQLITE_NOTADB + XCTAssertEqual(error.code, .SQLITE_NOTADB) XCTAssertEqual(error.message!, "file is encrypted or is not a database") XCTAssertTrue(error.sql == nil) XCTAssertEqual(error.description, "SQLite error 26: file is encrypted or is not a database") @@ -248,7 +248,7 @@ class EncryptionTests: GRDBTestCase { do { _ = try makeDatabasePool() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 26) // SQLITE_NOTADB + XCTAssertEqual(error.code, .SQLITE_NOTADB) XCTAssertEqual(error.message!, "file is encrypted or is not a database") XCTAssertTrue(error.sql == nil) XCTAssertEqual(error.description, "SQLite error 26: file is encrypted or is not a database") @@ -273,7 +273,7 @@ class EncryptionTests: GRDBTestCase { do { _ = try makeDatabasePool() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 26) // SQLITE_NOTADB + XCTAssertEqual(error.code, .SQLITE_NOTADB) XCTAssertEqual(error.message!, "file is encrypted or is not a database") XCTAssertTrue(error.sql == nil) XCTAssertEqual(error.description, "SQLite error 26: file is encrypted or is not a database") @@ -353,7 +353,7 @@ class EncryptionTests: GRDBTestCase { do { _ = try makeDatabaseQueue() } catch let error as DatabaseError { - XCTAssertEqual(error.code, 26) // SQLITE_NOTADB + XCTAssertEqual(error.code, .SQLITE_NOTADB) XCTAssertEqual(error.message!, "file is encrypted or is not a database") XCTAssertTrue(error.sql == nil) XCTAssertEqual(error.description, "SQLite error 26: file is encrypted or is not a database") @@ -377,7 +377,7 @@ class EncryptionTests: GRDBTestCase { do { _ = try makeDatabaseQueue(filename: "plaintext.sqlite") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 26) // SQLITE_NOTADB + XCTAssertEqual(error.code, .SQLITE_NOTADB) XCTAssertEqual(error.message!, "file is encrypted or is not a database") XCTAssertTrue(error.sql == nil) XCTAssertEqual(error.description, "SQLite error 26: file is encrypted or is not a database") diff --git a/Tests/Public/FetchedRecordsController/FetchedRecordsControllerTests.swift b/Tests/Public/FetchedRecordsController/FetchedRecordsControllerTests.swift index 5485c3551e..12ee249d6f 100644 --- a/Tests/Public/FetchedRecordsController/FetchedRecordsControllerTests.swift +++ b/Tests/Public/FetchedRecordsController/FetchedRecordsControllerTests.swift @@ -892,7 +892,7 @@ class FetchedRecordsControllerTests: GRDBTestCase { } waitForExpectations(timeout: 1, handler: nil) if let error = error as? DatabaseError { - XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.code, .SQLITE_ERROR) XCTAssertEqual(error.message, "no such table: persons") XCTAssertEqual(error.sql!, "SELECT * FROM \"persons\"") XCTAssertEqual(error.description, "SQLite error 1 with statement `SELECT * FROM \"persons\"`: no such table: persons") diff --git a/Tests/Public/Migrations/DatabaseMigratorTests.swift b/Tests/Public/Migrations/DatabaseMigratorTests.swift index 06ff9e81f4..c9a8d34659 100644 --- a/Tests/Public/Migrations/DatabaseMigratorTests.swift +++ b/Tests/Public/Migrations/DatabaseMigratorTests.swift @@ -118,7 +118,7 @@ class DatabaseMigratorTests : GRDBTestCase { // The first migration should be committed. // The second migration should be rollbacked. - XCTAssertEqual(error.code, 19) // SQLITE_CONSTRAINT + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) XCTAssertEqual(error.message!.lowercased(), "foreign key constraint failed") // lowercased: accept multiple SQLite version XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (?, ?)") XCTAssertEqual(error.description.lowercased(), "sqlite error 19 with statement `insert into pets (masterid, name) values (?, ?)` arguments [123, \"bobby\"]: foreign key constraint failed") @@ -172,7 +172,7 @@ class DatabaseMigratorTests : GRDBTestCase { // Migration 1 and 2 should be committed. // Migration 3 should not be committed. - XCTAssertEqual(error.code, 19) // SQLITE_CONSTRAINT + XCTAssertEqual(error.code, .SQLITE_CONSTRAINT) XCTAssertEqual(error.message!, "FOREIGN KEY constraint failed") XCTAssertTrue(error.sql == nil) XCTAssertEqual(error.description, "SQLite error 19: FOREIGN KEY constraint failed") diff --git a/Tests/Public/SQLTableBuilder/VirtualTableModuleTests.swift b/Tests/Public/SQLTableBuilder/VirtualTableModuleTests.swift index 412f2223d9..23106b4aef 100644 --- a/Tests/Public/SQLTableBuilder/VirtualTableModuleTests.swift +++ b/Tests/Public/SQLTableBuilder/VirtualTableModuleTests.swift @@ -40,7 +40,7 @@ private struct ThrowingFTS3TokenizeModule : VirtualTableModule { } func database(_ db: Database, didCreate tableName: String, using definition: FTS3TokenizeTableDefinition) throws { - throw DatabaseError(code: 123) + throw DatabaseError(code: ResultCode(rawValue: 123)) } } @@ -73,7 +73,7 @@ class VirtualTableModuleTests: GRDBTestCase { } XCTFail("Expected DatabaseError") } catch let error as DatabaseError { - XCTAssertEqual(error.code, 123) + XCTAssertEqual(error.code.rawValue, 123) } assertDidExecute(sql: "CREATE VIRTUAL TABLE \"test\" USING fts3tokenize(simple)") XCTAssertFalse(try db.tableExists("test"))