From f4f72ebb434f7fa4c5702a02e30fe4d0a515861d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 6 Sep 2017 08:36:13 +0200 Subject: [PATCH] Record.updateChanges(_:) --- GRDB/Record/Record.swift | 22 ++++++++++++++++++++++ TODO.md | 1 + 2 files changed, 23 insertions(+) diff --git a/GRDB/Record/Record.swift b/GRDB/Record/Record.swift index 5826eb4aa..9d08d1283 100644 --- a/GRDB/Record/Record.swift +++ b/GRDB/Record/Record.swift @@ -301,6 +301,28 @@ open class Record : RowConvertible, TableMapping, Persistable { referenceRow = Row(dao.persistenceContainer) } + /// If the record has been changed, executes an UPDATE statement so that + /// those changes and only those changes are saved in the database. + /// + /// On success, this method sets the *hasPersistentChangedValues* flag + /// to false. + /// + /// This method is guaranteed to have saved the eventual changes in the + /// database if it returns without error. + /// + /// - parameter db: A database connection. + /// - parameter columns: The columns to update. + /// - 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. + final public func updateChanges(_ db: Database) throws { + let changedColumns = Set(persistentChangedValues.keys) + guard !changedColumns.isEmpty else { + return + } + try update(db, columns: changedColumns) + } + /// Executes an INSERT or an UPDATE statement so that `self` is saved in /// the database. /// diff --git a/TODO.md b/TODO.md index f65b10482..908903d63 100644 --- a/TODO.md +++ b/TODO.md @@ -19,6 +19,7 @@ GRDB 2.0 - [ ] check performance improvements - [ ] document - [ ] Test new adoption of StatementColumnConvertible by Data. Add support for NSData if possible/needed. +- [ ] Document Record.updateChanges(_:) Not sure