From cc2a2a3545b90e3802bd7af18f22ed6b8a808902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 21 Nov 2018 22:09:41 +0100 Subject: [PATCH] Non-mutating version of updateChanges --- GRDB/Record/PersistableRecord.swift | 32 ++++++++++++++++++- ...MutablePersistableRecordChangesTests.swift | 6 ++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/GRDB/Record/PersistableRecord.swift b/GRDB/Record/PersistableRecord.swift index 8c09473df..eb24cfd83 100644 --- a/GRDB/Record/PersistableRecord.swift +++ b/GRDB/Record/PersistableRecord.swift @@ -641,7 +641,7 @@ extension MutablePersistableRecord { } @discardableResult - private func updateChanges(_ db: Database, from container: PersistenceContainer) throws -> Bool { + fileprivate func updateChanges(_ db: Database, from container: PersistenceContainer) throws -> Bool { let changes = databaseChangesIterator(from: container) let changedColumns: Set = changes.reduce(into: []) { $0.insert($1.0) } if changedColumns.isEmpty { @@ -974,6 +974,36 @@ 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 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, with change: (Self) throws -> Void) throws -> Bool { + let oldContainer = PersistenceContainer(self) + try change(self) + return try updateChanges(db, from: oldContainer) + } + // MARK: - Immutable CRUD Internals diff --git a/Tests/GRDBTests/MutablePersistableRecordChangesTests.swift b/Tests/GRDBTests/MutablePersistableRecordChangesTests.swift index 322c78c03..a3f7db736 100644 --- a/Tests/GRDBTests/MutablePersistableRecordChangesTests.swift +++ b/Tests/GRDBTests/MutablePersistableRecordChangesTests.swift @@ -317,7 +317,8 @@ class MutablePersistableRecordChangesTests: GRDBTestCase { t.column("lastName", .text) } - var record = MyRecord(id: nil, firstName: "Arthur", lastName: "Smith") + // This `let` is part of the test + let record = MyRecord(id: nil, firstName: "Arthur", lastName: "Smith") try record.insert(db) do { @@ -389,7 +390,8 @@ class MutablePersistableRecordChangesTests: GRDBTestCase { t.column("lastName", .text) } - var record = MyRecord(id: nil, firstName: "Arthur", lastName: "Smith") + // This `let` is part of the test + let record = MyRecord(id: nil, firstName: "Arthur", lastName: "Smith") try record.insert(db) do {