diff --git a/GRDB Tests/Private/DatabaseError/DatabaseErrorFromStatementCreationTests.swift b/GRDB Tests/Private/DatabaseError/DatabaseErrorFromStatementCreationTests.swift index d67bf19d3..c560c3f23 100644 --- a/GRDB Tests/Private/DatabaseError/DatabaseErrorFromStatementCreationTests.swift +++ b/GRDB Tests/Private/DatabaseError/DatabaseErrorFromStatementCreationTests.swift @@ -1,37 +1,37 @@ -import XCTest -@testable import GRDB - -class DatabaseErrorFromStatementCreationTests: GRDBTestCase { - - func testDatabaseErrorThrownBySelectStatementContainSQL() { - dbQueue.inDatabase { db in - do { - let _ = try SelectStatement(database: db, sql: "SELECT * FROM blah") - XCTFail() - } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) - 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") - } catch { - XCTFail("\(error)") - } - } - } - - func testDatabaseErrorThrownByUpdateStatementContainSQL() { - dbQueue.inDatabase { db in - do { - let _ = try UpdateStatement(database: db, sql: "UPDATE blah SET id = 12") - XCTFail() - } catch let error as DatabaseError { - XCTAssertEqual(error.code, 1) - 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") - } catch { - XCTFail("\(error)") - } - } - } -} +//import XCTest +//@testable import GRDB +// +//class DatabaseErrorFromStatementCreationTests: GRDBTestCase { +// +// func testDatabaseErrorThrownBySelectStatementContainSQL() { +// dbQueue.inDatabase { db in +// do { +// let _ = try SelectStatement(database: db, sql: "SELECT * FROM blah") +// XCTFail() +// } catch let error as DatabaseError { +// XCTAssertEqual(error.code, 1) +// 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") +// } catch { +// XCTFail("\(error)") +// } +// } +// } +// +// func testDatabaseErrorThrownByUpdateStatementContainSQL() { +// dbQueue.inDatabase { db in +// do { +// let _ = try UpdateStatement(database: db, sql: "UPDATE blah SET id = 12") +// XCTFail() +// } catch let error as DatabaseError { +// XCTAssertEqual(error.code, 1) +// 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") +// } catch { +// XCTFail("\(error)") +// } +// } +// } +//} diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 48cd2a5d3..eec2be10b 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -384,7 +384,9 @@ public final class Database { } func assertValid() { - precondition(databaseQueueID == nil || databaseQueueID == dispatch_get_specific(DatabaseQueue.databaseQueueIDKey), "Database was not used on the correct queue. Execute your statements inside DatabaseQueue.inDatabase() or DatabaseQueue.inTransaction(). Consider using fetchAll() method if this error message happens when iterating the result of the fetch() method.") + guard databaseQueueID == nil || databaseQueueID == dispatch_get_specific(DatabaseQueue.databaseQueueIDKey) else { + fatalError("Database was not used on the correct queue. Execute your statements inside DatabaseQueue.inDatabase() or DatabaseQueue.inTransaction(). Consider using fetchAll() method if this error message happens when iterating the result of the fetch() method.") + } } } diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index 4cb0e826a..adfb56c42 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -235,7 +235,9 @@ public final class DatabaseQueue { // // I try not to ship half-baked solutions, so until a complete solution // is found to this problem, I prefer totally disabling reentrancy. - precondition(databaseQueueID != dispatch_get_specific(DatabaseQueue.databaseQueueIDKey), "DatabaseQueue.inDatabase(_:) or DatabaseQueue.inTransaction(_:) was called reentrantly, which would lead to a deadlock.") + guard databaseQueueID != dispatch_get_specific(DatabaseQueue.databaseQueueIDKey) else { + fatalError("DatabaseQueue.inDatabase(_:) or DatabaseQueue.inTransaction(_:) was called reentrantly, which would lead to a deadlock.") + } return try DatabaseQueue.dispatchSync(queue, block: block) } diff --git a/GRDB/Core/Protocols/DatabaseTableMapping.swift b/GRDB/Core/Protocols/DatabaseTableMapping.swift index f322e23de..2cde42178 100644 --- a/GRDB/Core/Protocols/DatabaseTableMapping.swift +++ b/GRDB/Core/Protocols/DatabaseTableMapping.swift @@ -38,7 +38,9 @@ extension DatabaseTableMapping { // Fail early if database table has not one column in its primary key let columns = primaryKey.columns - precondition(columns.count == 1, "Primary key of table \(databaseTableName.quotedDatabaseIdentifier) is not made of a single column. See \(self).databaseTableName()") + guard columns.count == 1 else { + fatalError("Primary key of table \(databaseTableName.quotedDatabaseIdentifier) is not made of a single column. See \(self).databaseTableName()") + } guard let primaryKeyValue = primaryKeyValue else { return nil @@ -64,7 +66,9 @@ extension DatabaseTableMapping { } // Fail early if key is empty. - precondition(dictionary.count > 0, "Invalid empty key") + guard dictionary.count > 0 else { + fatalError("Invalid empty key") + } let whereSQL = dictionary.keys.map { column in "\(column.quotedDatabaseIdentifier)=?" }.joinWithSeparator(" AND ") let sql = "SELECT * FROM \(databaseTableName.quotedDatabaseIdentifier) WHERE \(whereSQL)" diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index 0f695f843..6d787c401 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -56,7 +56,9 @@ public class Statement { switch code { case SQLITE_OK: - precondition(consumedCharactersCount == sqlCodeUnits.count, "Invalid SQL string: multiple statements found. To execute multiple statements, use Database.executeMultiStatement() instead.") + guard consumedCharactersCount == sqlCodeUnits.count else { + fatalError("Invalid SQL string: multiple statements found. To execute multiple statements, use Database.executeMultiStatement() instead.") + } default: throw DatabaseError(code: code, message: database.lastErrorMessage, sql: sql) } @@ -95,7 +97,9 @@ public class Statement { // Exposed for StatementArguments. Don't make this one public unless we keep the arguments property in sync. final func bind(value: DatabaseValueConvertible?, forKey key: String) { let index = Int(sqlite3_bind_parameter_index(sqliteStatement, ":\(key)")) - precondition(index > 0, "Key not found in SQLite statement: `:\(key)`") + guard index > 0 else { + fatalError("Key not found in SQLite statement: `:\(key)`") + } bind(value, atIndex: index) } diff --git a/GRDB/Migrations/DatabaseMigrator.swift b/GRDB/Migrations/DatabaseMigrator.swift index d6da1b133..66e2251f1 100644 --- a/GRDB/Migrations/DatabaseMigrator.swift +++ b/GRDB/Migrations/DatabaseMigrator.swift @@ -62,7 +62,9 @@ public struct DatabaseMigrator { - parameter block: The migration block that performs SQL statements. */ public mutating func registerMigration(identifier: String, _ block: (db: Database) throws -> Void) { - precondition(migrations.map({ $0.identifier }).indexOf(identifier) == nil, "Already registered migration: \"\(identifier)\"") + guard migrations.map({ $0.identifier }).indexOf(identifier) == nil else { + fatalError("Already registered migration: \"\(identifier)\"") + } migrations.append(Migration(identifier: identifier, block: block)) } diff --git a/GRDB/Record/DatabaseStorable.swift b/GRDB/Record/DatabaseStorable.swift index 76bd7abc5..7cd682ff7 100644 --- a/GRDB/Record/DatabaseStorable.swift +++ b/GRDB/Record/DatabaseStorable.swift @@ -103,7 +103,9 @@ final class DataMapper { // Fail early if storedDatabaseDictionary is empty let storedDatabaseDictionary = storable.storedDatabaseDictionary - precondition(storedDatabaseDictionary.count > 0, "Invalid empty dictionary returned from \(storable.dynamicType).storedDatabaseDictionary") + guard storedDatabaseDictionary.count > 0 else { + fatalError("Invalid empty dictionary returned from \(storable.dynamicType).storedDatabaseDictionary") + } self.db = db self.storable = storable