DAO.init() throws DatabaseError instead of crashing whenever table does not exist, or database can't be accessed

This commit is contained in:
Gwendal Roué committed 2016-11-27 15:23:00 +01:00
1 parent e99e9f3e40
commit 09697f2593
2 files changed
+10 -10

No files matched your search

+8 -8
View File
@@ -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
+2 -2
View File
@@ -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)