Move non-mutating updateChanges(_:with:) from PersistableRecord to PersistableRecord & AnyObject.

This commit is contained in:
Gwendal Roué committed 2019-01-05 12:35:28 +01:00
1 parent f70d04c76a
commit bf0c9b5c27
1 file changed
+35 -31
+35 -31
View File
@@ -765,7 +765,42 @@ extension MutablePersistableRecord {
}
return try Row.fetchOne(statement) != nil
}
}
extension MutablePersistableRecord where Self: AnyObject {
// MARK: - Record Comparison
/// Mutates the record according to the provided closure, and then, if the
/// record has any difference from its previous version, executes an
/// UPDATE statement so that those differences and only those difference are
/// saved in the database.
///
/// This method is guaranteed to have saved the eventual differences in the
/// database if it returns without error.
///
/// For example:
///
/// if let player = try Player.fetchOne(db, key: 42) {
/// try player.updateChanges(db) {
/// $0.score += 10
/// $0.hasAward = true
/// }
/// }
///
/// - parameter db: A database connection.
/// - parameter change: A closure that modifies the record.
/// - returns: Whether the record had changes.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
/// PersistenceError.recordNotFound is thrown if the primary key does not
/// match any row in the database and record could not be updated.
@discardableResult
public func updateChanges(_ db: Database, with change: (Self) throws -> Void) throws -> Bool {
let container = PersistenceContainer(self)
try change(self)
return try updateChanges(db, from: container)
}
}
extension MutablePersistableRecord {
@@ -973,37 +1008,6 @@ extension PersistableRecord {
try performSave(db)
}
/// Mutates the record according to the provided closure, and then, if the
/// record has any difference from its previous version, executes an
/// UPDATE statement so that those differences and only those difference are
/// saved in the database.
///
/// This method is guaranteed to have saved the eventual differences in the
/// database if it returns without error.
///
/// For example:
///
/// if let player = try Player.fetchOne(db, key: 42) {
/// try player.updateChanges(db) {
/// $0.score += 10
/// $0.hasAward = true
/// }
/// }
///
/// - parameter db: A database connection.
/// - parameter change: A closure that modifies the record.
/// - returns: Whether the record had changes.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
/// PersistenceError.recordNotFound is thrown if the primary key does not
/// match any row in the database and record could not be updated.
@discardableResult
public func updateChanges(_ db: Database, with change: (Self) throws -> Void) throws -> Bool {
let container = PersistenceContainer(self)
try change(self)
return try updateChanges(db, from: container)
}
// MARK: - Immutable CRUD Internals
/// Don't invoke this method directly: it is an internal method for types