mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Remove support for partial update
This commit is contained in:
1 parent
c3fdbd694f
commit
338197ba3f
6 files changed
+22
-93
No files matched your search
@@ -73,9 +73,9 @@ struct PersistableCustomizedCountry : DatabasePersistable {
|
||||
try performInsert(db)
|
||||
}
|
||||
|
||||
func update(db: Database, columns: [String]? = nil) throws {
|
||||
func update(db: Database) throws {
|
||||
willUpdate()
|
||||
try performUpdate(db, columns: columns)
|
||||
try performUpdate(db)
|
||||
}
|
||||
|
||||
func save(db: Database) throws {
|
||||
@@ -186,53 +186,6 @@ class DatabasePersistableTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testPartialUpdatePersistablePersonClass() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let person1 = PersistablePersonClass(id: nil, name: "Arthur", age: 42)
|
||||
try person1.insert(db)
|
||||
let person2 = PersistablePersonClass(id: nil, name: "Barbara", age: 39)
|
||||
try person2.insert(db)
|
||||
|
||||
person1.name = "Craig"
|
||||
person1.age = 18
|
||||
do {
|
||||
try person1.update(db, columns: [])
|
||||
|
||||
let rows = Row.fetchAll(db, "SELECT * FROM persons ORDER BY id")
|
||||
XCTAssertEqual(rows.count, 2)
|
||||
XCTAssertEqual(rows[0].value(named: "id") as Int64, person1.id!)
|
||||
XCTAssertEqual(rows[0].value(named: "name") as String, "Arthur")
|
||||
XCTAssertEqual(rows[0].value(named: "age") as Int, 42)
|
||||
XCTAssertEqual(rows[1].value(named: "id") as Int64, person2.id!)
|
||||
XCTAssertEqual(rows[1].value(named: "name") as String, "Barbara")
|
||||
}
|
||||
do {
|
||||
try person1.update(db, columns: ["name"])
|
||||
|
||||
let rows = Row.fetchAll(db, "SELECT * FROM persons ORDER BY id")
|
||||
XCTAssertEqual(rows.count, 2)
|
||||
XCTAssertEqual(rows[0].value(named: "id") as Int64, person1.id!)
|
||||
XCTAssertEqual(rows[0].value(named: "name") as String, "Craig")
|
||||
XCTAssertEqual(rows[0].value(named: "age") as Int, 42)
|
||||
XCTAssertEqual(rows[1].value(named: "id") as Int64, person2.id!)
|
||||
XCTAssertEqual(rows[1].value(named: "name") as String, "Barbara")
|
||||
}
|
||||
do {
|
||||
try person1.update(db, columns: ["name", "age"])
|
||||
|
||||
let rows = Row.fetchAll(db, "SELECT * FROM persons ORDER BY id")
|
||||
XCTAssertEqual(rows.count, 2)
|
||||
XCTAssertEqual(rows[0].value(named: "id") as Int64, person1.id!)
|
||||
XCTAssertEqual(rows[0].value(named: "name") as String, "Craig")
|
||||
XCTAssertEqual(rows[0].value(named: "age") as Int, 18)
|
||||
XCTAssertEqual(rows[1].value(named: "id") as Int64, person2.id!)
|
||||
XCTAssertEqual(rows[1].value(named: "name") as String, "Barbara")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testSavePersistablePersonClass() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
@@ -63,9 +63,9 @@ struct MutablePersistableCustomizedCountry : MutableDatabasePersistable {
|
||||
try performInsert(db)
|
||||
}
|
||||
|
||||
func update(db: Database, columns: [String]? = nil) throws {
|
||||
func update(db: Database) throws {
|
||||
willUpdate()
|
||||
try performUpdate(db, columns: columns)
|
||||
try performUpdate(db)
|
||||
}
|
||||
|
||||
mutating func save(db: Database) throws {
|
||||
|
||||
@@ -37,9 +37,9 @@ class PersonWithOverrides : Person {
|
||||
try super.insert(db)
|
||||
}
|
||||
|
||||
override func update(db: Database, columns: [String]? = nil) throws {
|
||||
override func update(db: Database) throws {
|
||||
lastSavingMethod = .Update
|
||||
try super.update(db, columns: columns)
|
||||
try super.update(db)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -99,11 +99,10 @@ public protocol MutableDatabasePersistable : DatabaseTableMapping {
|
||||
/// that they invoke the performUpdate() method.
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - columns: An array of updated columns. If nil, all columns are updated.
|
||||
/// - throws: A DatabaseError is thrown whenever a SQLite error occurs.
|
||||
/// PersistenceError.NotFound is thrown if the primary key does not
|
||||
/// match any row in the database.
|
||||
func update(db: Database, columns: [String]?) throws
|
||||
func update(db: Database) throws
|
||||
|
||||
/// Executes an INSERT or an UPDATE statement so that `self` is saved in
|
||||
/// the database.
|
||||
@@ -170,8 +169,8 @@ public extension MutableDatabasePersistable {
|
||||
/// Executes an UPDATE statement.
|
||||
///
|
||||
/// The default implementation for update() invokes performUpdate().
|
||||
func update(db: Database, columns: [String]? = nil) throws {
|
||||
try performUpdate(db, columns: columns)
|
||||
func update(db: Database) throws {
|
||||
try performUpdate(db)
|
||||
}
|
||||
|
||||
/// Executes an INSERT or an UPDATE statement so that `self` is saved in
|
||||
@@ -226,8 +225,8 @@ public extension MutableDatabasePersistable {
|
||||
/// that adopt MutableDatabasePersistable can invoke performUpdate() in
|
||||
/// their implementation of update(). They should not provide their own
|
||||
/// implementation of performUpdate().
|
||||
func performUpdate(db: Database, columns: [String]?) throws {
|
||||
let changes = try DataMapper(db, self).updateStatement(columns).execute()
|
||||
func performUpdate(db: Database) throws {
|
||||
let changes = try DataMapper(db, self).updateStatement().execute()
|
||||
if changes.changedRowCount == 0 {
|
||||
throw PersistenceError.NotFound(self)
|
||||
}
|
||||
@@ -252,7 +251,7 @@ public extension MutableDatabasePersistable {
|
||||
}
|
||||
|
||||
do {
|
||||
try update(db, columns: nil)
|
||||
try update(db)
|
||||
} catch PersistenceError.NotFound {
|
||||
// TODO: check that the not persisted objet is self
|
||||
//
|
||||
@@ -414,7 +413,7 @@ public extension DatabasePersistable {
|
||||
}
|
||||
|
||||
do {
|
||||
try update(db, columns: nil)
|
||||
try update(db)
|
||||
} catch PersistenceError.NotFound {
|
||||
// TODO: check that the not persisted objet is self
|
||||
//
|
||||
@@ -534,7 +533,7 @@ final class DataMapper {
|
||||
return insertStatement
|
||||
}
|
||||
|
||||
func updateStatement(requestedColumns: [String]?) -> UpdateStatement {
|
||||
func updateStatement() -> UpdateStatement {
|
||||
// Fail early if primary key does not resolve to a database row.
|
||||
guard let primaryKeyDictionary = resolvingPrimaryKeyDictionary else {
|
||||
fatalError("invalid primary key in \(persistable)")
|
||||
@@ -542,20 +541,6 @@ final class DataMapper {
|
||||
|
||||
var updatedDictionary = persistentDictionary
|
||||
|
||||
if let requestedColumns = requestedColumns {
|
||||
let updatableColums = Array(updatedDictionary.keys)
|
||||
|
||||
// Validate requested columns
|
||||
for column in requestedColumns where !updatableColums.contains(column) {
|
||||
fatalError("unknown column: \(column)")
|
||||
}
|
||||
|
||||
// Only update requested columns
|
||||
for column in updatableColums where !requestedColumns.contains(column) {
|
||||
updatedDictionary.removeValueForKey(column)
|
||||
}
|
||||
}
|
||||
|
||||
// Don't update primary key columns
|
||||
for column in primaryKeyDictionary.keys {
|
||||
updatedDictionary.removeValueForKey(column)
|
||||
|
||||
@@ -212,7 +212,7 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
/// Executes an UPDATE statement.
|
||||
///
|
||||
/// On success, this method sets the *hasPersistentChangedValues* flag
|
||||
/// to false if the columns argument is nil.
|
||||
/// to false.
|
||||
///
|
||||
/// This method is guaranteed to have updated a row in the database if it
|
||||
/// returns without error.
|
||||
@@ -221,14 +221,9 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
/// - throws: A DatabaseError is thrown whenever a 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, columns: [String]? = nil) throws {
|
||||
try performUpdate(db, columns: columns)
|
||||
|
||||
// When all columns have been saved, it is safe to reset the
|
||||
// hasPersistentChangedValues flag:
|
||||
if columns == nil {
|
||||
hasPersistentChangedValues = false
|
||||
}
|
||||
public func update(db: Database) throws {
|
||||
try performUpdate(db)
|
||||
hasPersistentChangedValues = false
|
||||
}
|
||||
|
||||
/// Executes an INSERT or an UPDATE statement so that `self` is saved in
|
||||
|
||||
@@ -1582,7 +1582,6 @@ Types that adopt DatabasePersistable or MutableDatabasePersistable are given def
|
||||
```swift
|
||||
try object.insert(db) // INSERT
|
||||
try object.update(db) // UPDATE
|
||||
try object.update(db, columns:[...]) // Partial update
|
||||
try object.save(db) // Inserts or updates
|
||||
try object.delete(db) // DELETE
|
||||
object.exists(db) // Bool
|
||||
@@ -1643,9 +1642,9 @@ struct Link : DatabasePersistable {
|
||||
try performInsert(db)
|
||||
}
|
||||
|
||||
func update(db: Database, columns: [String]? = nil) throws {
|
||||
func update(db: Database) throws {
|
||||
try validate()
|
||||
try performUpdate(db, columns: columns)
|
||||
try performUpdate(db)
|
||||
}
|
||||
|
||||
func validate() throws {
|
||||
@@ -1715,7 +1714,6 @@ Yet, it does a few things well:
|
||||
let person = Person(...)
|
||||
try person.insert(db) // INSERT
|
||||
try person.update(db) // UPDATE
|
||||
try person.update(db, columns:[...]) // Partial update
|
||||
try person.save(db) // Inserts or updates
|
||||
try person.delete(db) // DELETE
|
||||
```
|
||||
@@ -1767,7 +1765,6 @@ class Person {
|
||||
// Persistence
|
||||
func insert(db: Database) throws
|
||||
func update(db: Database) throws
|
||||
func update(db: Database, columns: [String]? = nil) throws
|
||||
func save(db: Database) throws
|
||||
func delete(db: Database) throws -> Bool
|
||||
func exists(db: Database) -> Bool
|
||||
@@ -1986,7 +1983,6 @@ try dbQueue.inDatabase { db in
|
||||
let person = Person(...)
|
||||
try person.insert(db) // INSERT
|
||||
try person.update(db) // UPDATE
|
||||
try person.update(db, columns:[...]) // Partial update
|
||||
try person.save(db) // Inserts or updates
|
||||
try person.delete(db) // DELETE
|
||||
person.exists(db) // Bool
|
||||
@@ -2104,10 +2100,10 @@ You can use some external library such as [GRValidation](https://github.com/grou
|
||||
class Person : Record, Validable {
|
||||
var name: String?
|
||||
|
||||
override func update(db: Database, columns: [String]? = nil) throws {
|
||||
override func update(db: Database) throws {
|
||||
// Validate before update
|
||||
try validate()
|
||||
try super.update(db, columns: columns)
|
||||
try super.update(db)
|
||||
}
|
||||
|
||||
override func insert(db: Database) throws {
|
||||
|
||||
Reference in new issue
Block a user