From bf0c9b5c2716b3505d2ea3da19db349bbd6cf0de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 5 Jan 2019 12:35:28 +0100 Subject: [PATCH] Move non-mutating updateChanges(_:with:) from PersistableRecord to PersistableRecord & AnyObject. --- GRDB/Record/PersistableRecord.swift | 66 +++++++++++++++-------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/GRDB/Record/PersistableRecord.swift b/GRDB/Record/PersistableRecord.swift index 47a33a7ff..3c454cfcf 100644 --- a/GRDB/Record/PersistableRecord.swift +++ b/GRDB/Record/PersistableRecord.swift @@ -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