From df7791a3e72531a4b5bf9219167b9641b2f4e7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 28 Jul 2016 09:05:46 +0200 Subject: [PATCH] Partial update WIP: documentation --- CHANGELOG.md | 28 ++++++++++++++++++++++ GRDB/Record/Persistable.swift | 44 +++++++++++++++++++++-------------- GRDB/Record/Record.swift | 7 +++--- README.md | 13 +++++------ TODO.md | 1 + 5 files changed, 65 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44b390710..e77569957 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,36 @@ Release Notes **New** - `Database.indexes(on:)` returns the indexes defined on a database table + - `Database.table(_:hasUniqueKey:)` returns true if a sequence of columns uniquely identifies a row, that is to say if the columns are the primary key, or if there is a unique index on them. +- MutablePersistable types, including Record subclasses, support partial updates: + + ```swift + try person.update(db) // Full update + try person.update(db, columns: ["age"]) // Only updates the age column + ``` + +**Breaking Changes** + +- MutablePersistable `update` and `performUpdate` methods have changed their signature: + + ```diff + protocol MutablePersistable : TableMapping { + -func update(db: Database) throws + +func update(db: Database, columns: Set) throws + } + + extension MutablePersistable { + func update(db: Database) throws + +func update(db: Database, columns: Set) throws + +func update(db: Database, columns: S) throws + +func update(db: Database, columns: S) throws + -func performUpdate(db: Database) throws + +func performUpdate(db: Database, columns: Set) throws + } + ``` + ## 0.76.0 diff --git a/GRDB/Record/Persistable.swift b/GRDB/Record/Persistable.swift index 3c6f8dc08..5313ec42a 100644 --- a/GRDB/Record/Persistable.swift +++ b/GRDB/Record/Persistable.swift @@ -115,9 +115,8 @@ 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. + /// - parameter columns: The columns to update. /// - 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. @@ -189,36 +188,43 @@ public extension MutablePersistable { /// Executes an UPDATE statement. /// - /// TODO: columns - /// - /// The default implementation for update() invokes performUpdate(). + /// - parameter db: A database connection. + /// - parameter columns: The columns to update. + /// - 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, columns: Set) throws { try performUpdate(db, columns: columns) } /// Executes an UPDATE statement. /// - /// TODO: columns - /// - /// The default implementation for update() invokes performUpdate(). + /// - parameter db: A database connection. + /// - parameter columns: The columns to update. + /// - 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, 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(). + /// - parameter db: A database connection. + /// - parameter columns: The columns to update. + /// - 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, columns: S) throws { try update(db, columns: Set(columns)) } - /// Executes an UPDATE statement. + /// Executes an UPDATE statement that updates all table columns. /// - /// TODO: columns - /// - /// The default implementation for update() invokes performUpdate(). + /// - 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 { let databaseTableName = self.dynamicType.databaseTableName() let columns = try db.columns(in: databaseTableName) @@ -281,12 +287,16 @@ 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(). + /// + /// - parameter db: A database connection. + /// - parameter columns: The columns to update. + /// - 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 performUpdate(db: Database, columns: Set) throws { try DataMapper(db, self).updateStatement(columns: columns).execute() if db.changesCount == 0 { diff --git a/GRDB/Record/Record.swift b/GRDB/Record/Record.swift index 7bbc9e964..9982e84f9 100644 --- a/GRDB/Record/Record.swift +++ b/GRDB/Record/Record.swift @@ -249,12 +249,11 @@ 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. + /// - parameter columns: The columns to update. /// - 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. + /// PersistenceError.NotFound is thrown if the primary key does not + /// match any row in the database. public func update(db: Database, columns: Set) throws { // The simplest code would be: // diff --git a/README.md b/README.md index 64249eab3..4f5ce9bb5 100644 --- a/README.md +++ b/README.md @@ -1860,14 +1860,13 @@ 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 -try pointOfInterest.save(db) // Inserts or updates -try pointOfInterest.delete(db) // DELETE -pointOfInterest.exists(db) // Bool +try pointOfInterest.insert(db) // INSERT +try pointOfInterest.update(db) // UPDATE +try pointOfInterest.update(db, columns: ...) // UPDATE +try pointOfInterest.save(db) // Inserts or updates +try pointOfInterest.delete(db) // DELETE +pointOfInterest.exists(db) // Bool ``` - `insert`, `update`, `save` and `delete` can throw a [DatabaseError](#error-handling) whenever an SQLite integrity check fails. diff --git a/TODO.md b/TODO.md index 2d1ffc459..7847f404d 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,5 @@ - [ ] Partial update. update(db) should call update(db, columns: [String]) (single point of override) + - [ ] cache columns - [ ] Fix "More, when you're interested in specific table columns, you're out of luck, because databaseDidChange does not know about columns: it just knows that a row has been inserted, deleted, or updated, without further detail" - [ ] GRDBCipher: remove limitations on iOS or OS X versions - [ ] FetchedRecordsController: take inspiration from https://github.com/jflinter/Dwifft