From f5bfb9df95c3a740d2e31577b2de54f03ec9b329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 21 Nov 2018 08:37:51 +0100 Subject: [PATCH] MutablePersistableRecord.updateChanges(_:with:) --- GRDB/Record/PersistableRecord.swift | 70 ++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 11 deletions(-) diff --git a/GRDB/Record/PersistableRecord.swift b/GRDB/Record/PersistableRecord.swift index 9c4197a91..c06fb669d 100644 --- a/GRDB/Record/PersistableRecord.swift +++ b/GRDB/Record/PersistableRecord.swift @@ -521,22 +521,71 @@ extension MutablePersistableRecord { try update(db, columns: Set(columns.map { $0.name })) } - /// If the record has differences from the other record, executes an UPDATE - /// statement so that those changes and only those changes are saved in - /// the database. + /// If the record has any difference from the other record, 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 changes in the + /// This method is guaranteed to have saved the eventual differences in the /// database if it returns without error. /// + /// For example: + /// + /// if let oldPlayer = Player.fetchOne(db, key: 42) { + /// var newPlayer = oldPlayer + /// newPlayer.score += 10 + /// newPlayer.hasAward = true + /// try newPlayer.updateChanges(db, from: oldRecord) + /// } + /// /// - parameter db: A database connection. - /// - parameter columns: The columns to update. + /// - parameter record: The comparison 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. + /// - SeeAlso: updateChanges(_:with:) + @discardableResult + public func updateChanges(_ db: Database, from record: MutablePersistableRecord) throws -> Bool { + let oldContainer = PersistenceContainer(record) + let changes = databaseChangesIterator(from: oldContainer) + let changedColumns: Set = changes.reduce(into: []) { $0.insert($1.0) } + if changedColumns.isEmpty { + return false + } else { + try update(db, columns: changedColumns) + return true + } + } + + /// 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 var player = 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 modifise 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, from record: MutablePersistableRecord) throws -> Bool { - let changedColumns = Set(databaseChanges(from: record).keys) + public mutating func updateChanges(_ db: Database, with change: (inout Self) throws -> Void) throws -> Bool { + let oldContainer = PersistenceContainer(self) + try change(&self) + let changes = databaseChangesIterator(from: oldContainer) + let changedColumns: Set = changes.reduce(into: []) { $0.insert($1.0) } if changedColumns.isEmpty { return false } else { @@ -574,7 +623,7 @@ extension MutablePersistableRecord { /// Returns a boolean indicating whether this record and the other record /// have the same database representation. public func databaseEquals(_ record: Self) -> Bool { - return databaseChangesIterator(from: record).next() == nil + return databaseChangesIterator(from: PersistenceContainer(record)).next() == nil } /// A dictionary of values changed from the other record. @@ -586,11 +635,10 @@ extension MutablePersistableRecord { /// same set of columns in their `encode(to:)` method, only the columns /// defined by the receiver record are considered. public func databaseChanges(from record: MutablePersistableRecord) -> [String: DatabaseValue] { - return Dictionary(uniqueKeysWithValues: databaseChangesIterator(from: record)) + return Dictionary(uniqueKeysWithValues: databaseChangesIterator(from: PersistenceContainer(record))) } - fileprivate func databaseChangesIterator(from record: MutablePersistableRecord) -> AnyIterator<(String, DatabaseValue)> { - let oldContainer = PersistenceContainer(record) + fileprivate func databaseChangesIterator(from oldContainer: PersistenceContainer) -> AnyIterator<(String, DatabaseValue)> { var newValueIterator = PersistenceContainer(self).makeIterator() return AnyIterator { // Loop until we find a change, or exhaust columns: