From 50ce433bcb426bb4fef4d212a7705107cf24fee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 28 Jul 2016 08:25:08 +0200 Subject: [PATCH] Partial update WIP: public API --- GRDB/Record/Persistable.swift | 44 ++++++++++++++++--- GRDB/Record/Record.swift | 8 ++-- README.md | 6 ++- .../Persistable/MutablePersistableTests.swift | 4 +- .../Core/Persistable/PersistableTests.swift | 4 +- Tests/Public/Record/RecordSubClassTests.swift | 4 +- 6 files changed, 54 insertions(+), 16 deletions(-) diff --git a/GRDB/Record/Persistable.swift b/GRDB/Record/Persistable.swift index 3a4a0e164..0a74ffa61 100644 --- a/GRDB/Record/Persistable.swift +++ b/GRDB/Record/Persistable.swift @@ -115,11 +115,13 @@ public protocol MutablePersistable : TableMapping { /// implementation of update(). In their implementation, it is recommended /// that they invoke the performUpdate() method. /// + /// TODO: columns + /// /// - parameter db: A database connection. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. /// PersistenceError.NotFound is thrown if the primary key does not /// match any row in the database. - func update(db: Database) throws + func update(db: Database, columns: Set) throws /// Executes an INSERT or an UPDATE statement so that `self` is saved in /// the database. @@ -187,9 +189,39 @@ public extension MutablePersistable { /// Executes an UPDATE statement. /// + /// TODO: columns + /// + /// The default implementation for update() invokes performUpdate(). + func update(db: Database, columns: Set) throws { + try performUpdate(db, columns: columns) + } + + /// Executes an UPDATE statement. + /// + /// TODO: columns + /// + /// The default implementation for update() invokes performUpdate(). + func update(db: Database, columns: S) throws { + try update(db, columns: Set(columns.map { $0.name })) + } + + /// Executes an UPDATE statement. + /// + /// TODO: columns + /// + /// The default implementation for update() invokes performUpdate(). + func update(db: Database, columns: S) throws { + try update(db, columns: Set(columns)) + } + + /// Executes an UPDATE statement. + /// + /// TODO: columns + /// /// The default implementation for update() invokes performUpdate(). func update(db: Database) throws { - try performUpdate(db) + // TODO + try update(db, columns: Set()) } /// Executes an INSERT or an UPDATE statement so that `self` is saved in @@ -248,12 +280,14 @@ public extension MutablePersistable { /// Don't invoke this method directly: it is an internal method for types /// that adopt MutablePersistable. /// + /// TODO: columns + /// /// performUpdate() provides the default implementation for update(). Types /// that adopt MutablePersistable can invoke performUpdate() in their /// implementation of update(). They should not provide their own /// implementation of performUpdate(). - func performUpdate(db: Database) throws { - try DataMapper(db, self).updateStatement().execute() + func performUpdate(db: Database, columns: Set) throws { + try DataMapper(db, self).updateStatement(columns: columns).execute() if db.changesCount == 0 { throw PersistenceError.NotFound(self) } @@ -499,7 +533,7 @@ final class DataMapper { return statement } - func updateStatement() -> UpdateStatement { + func updateStatement(columns columns: Set) -> UpdateStatement { // Fail early if primary key does not resolve to a database row. let primaryKeyColumns = primaryKey?.columns ?? [] let primaryKeyValues = databaseValues(forColumns: primaryKeyColumns, inDictionary: persistentDictionary) diff --git a/GRDB/Record/Record.swift b/GRDB/Record/Record.swift index c7bc8dcc2..7bbc9e964 100644 --- a/GRDB/Record/Record.swift +++ b/GRDB/Record/Record.swift @@ -249,14 +249,16 @@ public class Record : RowConvertible, TableMapping, Persistable { /// This method is guaranteed to have updated a row in the database if it /// returns without error. /// + /// TODO: columns + /// /// - parameter db: A database connection. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. /// PersistenceError.NotFound is thrown if the primary key does not match /// any row in the database and record could not be updated. - public func update(db: Database) throws { + public func update(db: Database, columns: Set) throws { // The simplest code would be: // - // try performUpdate(db) + // try performUpdate(db, columns: columns) // hasPersistentChangedValues = false // // But this triggers two calls to persistentDictionary, and this is both @@ -266,7 +268,7 @@ public class Record : RowConvertible, TableMapping, Persistable { // So let's provide our custom implementation of insert, which uses the // same persistentDictionary for both update, and change tracking. let dataMapper = DataMapper(db, self) - try dataMapper.updateStatement().execute() + try dataMapper.updateStatement(columns: columns).execute() if db.changesCount == 0 { throw PersistenceError.NotFound(self) } diff --git a/README.md b/README.md index 089de97a8..64249eab3 100644 --- a/README.md +++ b/README.md @@ -1860,6 +1860,8 @@ paris.id // some value [Record](#record-class) subclasses and types that adopt [Persistable](#persistable-protocol) are given default implementations for methods that insert, update, and delete: +TODO: update columns + ```swift try pointOfInterest.insert(db) // INSERT try pointOfInterest.update(db) // UPDATE @@ -1917,9 +1919,9 @@ struct Link : Persistable { try performInsert(db) } - func update(db: Database) throws { + func update(db: Database, columns: Set) throws { try validate() - try performUpdate(db) + try performUpdate(db, columns: columns) } func validate() throws { diff --git a/Tests/Public/Core/Persistable/MutablePersistableTests.swift b/Tests/Public/Core/Persistable/MutablePersistableTests.swift index 19ff3c026..a2fa893dc 100644 --- a/Tests/Public/Core/Persistable/MutablePersistableTests.swift +++ b/Tests/Public/Core/Persistable/MutablePersistableTests.swift @@ -69,9 +69,9 @@ private struct MutablePersistableCustomizedCountry : MutablePersistable { try performInsert(db) } - func update(db: Database) throws { + func update(db: Database, columns: Set) throws { willUpdate() - try performUpdate(db) + try performUpdate(db, columns: columns) } mutating func save(db: Database) throws { diff --git a/Tests/Public/Core/Persistable/PersistableTests.swift b/Tests/Public/Core/Persistable/PersistableTests.swift index 7a58d0094..3791c76d3 100644 --- a/Tests/Public/Core/Persistable/PersistableTests.swift +++ b/Tests/Public/Core/Persistable/PersistableTests.swift @@ -79,9 +79,9 @@ private struct PersistableCustomizedCountry : Persistable { try performInsert(db) } - func update(db: Database) throws { + func update(db: Database, columns: Set) throws { willUpdate() - try performUpdate(db) + try performUpdate(db, columns: columns) } func save(db: Database) throws { diff --git a/Tests/Public/Record/RecordSubClassTests.swift b/Tests/Public/Record/RecordSubClassTests.swift index 71c7190e9..522cfb66b 100644 --- a/Tests/Public/Record/RecordSubClassTests.swift +++ b/Tests/Public/Record/RecordSubClassTests.swift @@ -104,9 +104,9 @@ private class PersonWithOverrides : Person { try super.insert(db) } - override func update(db: Database) throws { + override func update(db: Database, columns: Set) throws { lastSavingMethod = .Update - try super.update(db) + try super.update(db, columns: columns) } }