From 09697f259384f8d5a293674dc78eb20e94f49f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 27 Nov 2016 15:23:00 +0100 Subject: [PATCH] DAO.init() throws DatabaseError instead of crashing whenever table does not exist, or database can't be accessed --- GRDB/Record/Persistable.swift | 16 ++++++++-------- GRDB/Record/Record.swift | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/GRDB/Record/Persistable.swift b/GRDB/Record/Persistable.swift index 2d3f22c48..d5cd99bc9 100644 --- a/GRDB/Record/Persistable.swift +++ b/GRDB/Record/Persistable.swift @@ -298,12 +298,12 @@ public extension MutablePersistable { // MARK: - CRUD Internals - fileprivate func canUpdate(_ db: Database) -> Bool { + fileprivate func canUpdate(_ db: Database) throws -> Bool { // Fail early if database table does not exist. let databaseTableName = type(of: self).databaseTableName // Update according to explicit primary key, or implicit rowid. - let primaryKey = try! db.primaryKey(databaseTableName) ?? PrimaryKeyInfo.hiddenRowID + let primaryKey = try db.primaryKey(databaseTableName) ?? PrimaryKeyInfo.hiddenRowID // We need a primary key value in the persistentDictionary let persistentDictionary = self.persistentDictionary @@ -322,7 +322,7 @@ public extension MutablePersistable { /// implementation of performInsert(). mutating func performInsert(_ db: Database) throws { let conflictResolutionForInsert = type(of: self).persistenceConflictPolicy.conflictResolutionForInsert - let dao = DAO(db, self) + let dao = try DAO(db, self) try dao.insertStatement(onConflict: conflictResolutionForInsert).execute() if !conflictResolutionForInsert.invalidatesLastInsertedRowID { @@ -368,7 +368,7 @@ public extension MutablePersistable { // that override insert or save have opportunity to perform their // custom job. - if canUpdate(db) { + if try canUpdate(db) { do { try update(db) } catch PersistenceError.recordNotFound { @@ -531,7 +531,7 @@ public extension Persistable { /// implementation of performInsert(). func performInsert(_ db: Database) throws { let conflictResolutionForInsert = type(of: self).persistenceConflictPolicy.conflictResolutionForInsert - let dao = DAO(db, self) + let dao = try DAO(db, self) try dao.insertStatement(onConflict: conflictResolutionForInsert).execute() if !conflictResolutionForInsert.invalidatesLastInsertedRowID { @@ -552,7 +552,7 @@ public extension Persistable { // Make sure we call self.insert and self.update so that classes that // override insert or save have opportunity to perform their custom job. - if canUpdate(db) { + if try canUpdate(db) { do { try update(db) } catch PersistenceError.recordNotFound { @@ -592,10 +592,10 @@ final class DAO { /// The table primary key let primaryKey: PrimaryKeyInfo - init(_ db: Database, _ record: MutablePersistable) { + init(_ db: Database, _ record: MutablePersistable) throws { // Fail early if database table does not exist. let databaseTableName = type(of: record).databaseTableName - let primaryKey = try! db.primaryKey(databaseTableName) ?? PrimaryKeyInfo.hiddenRowID + let primaryKey = try db.primaryKey(databaseTableName) ?? PrimaryKeyInfo.hiddenRowID // Fail early if persistentDictionary is empty let persistentDictionary = record.persistentDictionary diff --git a/GRDB/Record/Record.swift b/GRDB/Record/Record.swift index 258d9f793..d655257f3 100644 --- a/GRDB/Record/Record.swift +++ b/GRDB/Record/Record.swift @@ -233,7 +233,7 @@ open class Record : RowConvertible, TableMapping, Persistable { // same persistentDictionary for both insertion, and change tracking. let conflictResolutionForInsert = type(of: self).persistenceConflictPolicy.conflictResolutionForInsert - let dao = DAO(db, self) + let dao = try DAO(db, self) var persistentDictionary = dao.persistentDictionary try dao.insertStatement(onConflict: conflictResolutionForInsert).execute() @@ -286,7 +286,7 @@ open class Record : RowConvertible, TableMapping, Persistable { // // So let's provide our custom implementation of insert, which uses the // same persistentDictionary for both update, and change tracking. - let dao = DAO(db, self) + let dao = try DAO(db, self) guard let statement = try dao.updateStatement(columns: columns, onConflict: type(of: self).persistenceConflictPolicy.conflictResolutionForUpdate) else { // Nil primary key throw PersistenceError.recordNotFound(self)