diff --git a/GRDB/Record/Persistable.swift b/GRDB/Record/Persistable.swift index 112cd4c66..8c8927800 100644 --- a/GRDB/Record/Persistable.swift +++ b/GRDB/Record/Persistable.swift @@ -146,7 +146,7 @@ public protocol MutablePersistable : TableMapping { /// /// - returns: Whether a database row was deleted. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - func delete(db: Database) throws -> Bool + func delete(writer: DatabaseWriter) throws -> Bool /// Returns true if and only if the primary key matches a row in /// the database. @@ -194,8 +194,8 @@ public extension MutablePersistable { /// Executes a DELETE statement. /// /// The default implementation for delete() invokes performDelete(). - func delete(db: Database) throws -> Bool { - return try performDelete(db) + func delete(writer: DatabaseWriter) throws -> Bool { + return try performDelete(writer) } /// Returns true if and only if the primary key matches a row in @@ -292,8 +292,10 @@ public extension MutablePersistable { /// that adopt MutablePersistable can invoke performDelete() in /// their implementation of delete(). They should not provide their own /// implementation of performDelete(). - func performDelete(db: Database) throws -> Bool { - return try DataMapper(db, self).deleteStatement().execute().changedRowCount > 0 + func performDelete(writer: DatabaseWriter) throws -> Bool { + return try writer._write { db in + try DataMapper(db, self).deleteStatement().execute().changedRowCount > 0 + } } /// Don't invoke this method directly: it is an internal method for types diff --git a/GRDB/Record/Record.swift b/GRDB/Record/Record.swift index 400bfe885..0ac05be99 100644 --- a/GRDB/Record/Record.swift +++ b/GRDB/Record/Record.swift @@ -285,13 +285,14 @@ public class Record : RowConvertible, TableMapping, Persistable { /// /// - returns: Whether a database row was deleted. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - public func delete(db: Database) throws -> Bool { - let deleted = try performDelete(db) - // Future calls to update() will throw NotFound. Make the user - // a favor and make sure this error is thrown even if she checks the - // hasPersistentChangedValues flag: - hasPersistentChangedValues = true - return deleted + public func delete(writer: DatabaseWriter) throws -> Bool { + defer { + // Future calls to update() will throw NotFound. Make the user + // a favor and make sure this error is thrown even if she checks the + // hasPersistentChangedValues flag: + hasPersistentChangedValues = true + } + return try performDelete(writer) } } diff --git a/Tests/Public/Core/Persistable/MutablePersistableTests.swift b/Tests/Public/Core/Persistable/MutablePersistableTests.swift index 0c460a5f9..841474356 100644 --- a/Tests/Public/Core/Persistable/MutablePersistableTests.swift +++ b/Tests/Public/Core/Persistable/MutablePersistableTests.swift @@ -73,9 +73,9 @@ struct MutablePersistableCustomizedCountry : MutablePersistable { try performSave(writer) } - func delete(db: Database) throws { + func delete(writer: DatabaseWriter) throws { willDelete() - try performDelete(db) + try performDelete(writer) } func exists(db: Database) -> Bool { diff --git a/Tests/Public/Core/Persistable/PersistableTests.swift b/Tests/Public/Core/Persistable/PersistableTests.swift index e39551ad8..17a70e276 100644 --- a/Tests/Public/Core/Persistable/PersistableTests.swift +++ b/Tests/Public/Core/Persistable/PersistableTests.swift @@ -83,9 +83,9 @@ struct PersistableCustomizedCountry : Persistable { try performSave(writer) } - func delete(db: Database) throws { + func delete(writer: DatabaseWriter) throws { willDelete() - try performDelete(db) + try performDelete(writer) } func exists(db: Database) -> Bool {