mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Partial update WIP: public API
This commit is contained in:
1 parent
d8ada8c9d3
commit
50ce433bcb
6 files changed
+54
-16
No files matched your search
@@ -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<String>) 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<String>) throws {
|
||||
try performUpdate(db, columns: columns)
|
||||
}
|
||||
|
||||
/// Executes an UPDATE statement.
|
||||
///
|
||||
/// TODO: columns
|
||||
///
|
||||
/// The default implementation for update() invokes performUpdate().
|
||||
func update<S: SequenceType where S.Generator.Element == SQLColumn>(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<S: SequenceType where S.Generator.Element == String>(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<String>) 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<String>) -> UpdateStatement {
|
||||
// Fail early if primary key does not resolve to a database row.
|
||||
let primaryKeyColumns = primaryKey?.columns ?? []
|
||||
let primaryKeyValues = databaseValues(forColumns: primaryKeyColumns, inDictionary: persistentDictionary)
|
||||
|
||||
@@ -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<String>) 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)
|
||||
}
|
||||
|
||||
@@ -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<String>) throws {
|
||||
try validate()
|
||||
try performUpdate(db)
|
||||
try performUpdate(db, columns: columns)
|
||||
}
|
||||
|
||||
func validate() throws {
|
||||
|
||||
@@ -69,9 +69,9 @@ private struct MutablePersistableCustomizedCountry : MutablePersistable {
|
||||
try performInsert(db)
|
||||
}
|
||||
|
||||
func update(db: Database) throws {
|
||||
func update(db: Database, columns: Set<String>) throws {
|
||||
willUpdate()
|
||||
try performUpdate(db)
|
||||
try performUpdate(db, columns: columns)
|
||||
}
|
||||
|
||||
mutating func save(db: Database) throws {
|
||||
|
||||
@@ -79,9 +79,9 @@ private struct PersistableCustomizedCountry : Persistable {
|
||||
try performInsert(db)
|
||||
}
|
||||
|
||||
func update(db: Database) throws {
|
||||
func update(db: Database, columns: Set<String>) throws {
|
||||
willUpdate()
|
||||
try performUpdate(db)
|
||||
try performUpdate(db, columns: columns)
|
||||
}
|
||||
|
||||
func save(db: Database) throws {
|
||||
|
||||
@@ -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<String>) throws {
|
||||
lastSavingMethod = .Update
|
||||
try super.update(db)
|
||||
try super.update(db, columns: columns)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user