Simplify RowModel.updateFromRow()

This commit is contained in:
Gwendal Roué committed 2015-09-04 08:10:59 +02:00
1 parent 9e42695a52
commit 2caf0f65b5
3 files changed
+40 -37

No files matched your search

@@ -187,4 +187,24 @@ class RowModelEditedTests: RowModelTestCase {
}
}
}
func testRowModelIsEditedAfterPrimaryKeyChange() {
// After reload, a model is not edited.
assertNoError {
try dbQueue.inDatabase { db in
let commonAttributes = Row(dictionary:["name": "Arthur", "age": 41])
let person1 = Person(row: commonAttributes)
try person1.insert(db)
let person2 = Person(row: commonAttributes)
try person2.insert(db)
XCTAssertFalse(person1.edited)
XCTAssertFalse(person2.edited)
person1.copyDatabaseValuesFrom(person2)
XCTAssertTrue(person1.edited)
}
}
}
}
+20 -36
View File
@@ -58,6 +58,18 @@ public class RowModel {
/// A primary key that spans accross several columns. Associated strings
/// are column names.
case Columns([String])
/// The columns in the primary key.
public var columns: [String] {
switch self {
case .RowID(let column):
return [column]
case .Column(let column):
return [column]
case .Columns(let columns):
return columns
}
}
}
/// A table definition returned by RowModel.databaseTable.
@@ -178,20 +190,9 @@ public class RowModel {
*Important*: subclasses must invoke super's implementation.
*/
public func updateFromRow(row: Row) {
// IMPLEMENTATION NOTE
//
// Subclasses must call super because we set the edited flag, and
// subclasses may forget to do so.
//
// We set the edited flag because the row may change the primary key,
// and until we try to be smart, turning the edited flag is the safest
// option.
for (column, databaseValue) in row {
setDatabaseValue(databaseValue, forColumn: column)
}
edited = true
}
/**
@@ -466,36 +467,19 @@ public class RowModel {
storedDatabaseDictionary.
*/
lazy var weakPrimaryKeyDictionary: [String: DatabaseValueConvertible?]? = { [unowned self] in
guard let primaryKey = self.databaseTable.primaryKey else {
guard let primaryKeyColumns = self.databaseTable.primaryKey?.columns else {
return nil
}
switch primaryKey {
case .RowID(let column):
if let value = self.storedDatabaseDictionary[column] {
return [column: value]
let storedDatabaseDictionary = self.storedDatabaseDictionary
var primaryKeyDictionary: [String: DatabaseValueConvertible?] = [:]
for column in primaryKeyColumns {
if let value = storedDatabaseDictionary[column] {
primaryKeyDictionary[column] = value
} else {
return [column: nil]
primaryKeyDictionary[column] = nil
}
case .Column(let column):
if let value = self.storedDatabaseDictionary[column] {
return [column: value]
} else {
return [column: nil]
}
case .Columns(let columns):
let storedDatabaseDictionary = self.storedDatabaseDictionary
var primaryKeyDictionary = [String: DatabaseValueConvertible?]()
for column in columns {
if let value = storedDatabaseDictionary[column] {
primaryKeyDictionary[column] = value
} else {
primaryKeyDictionary[column] = nil
}
}
return primaryKeyDictionary
}
return primaryKeyDictionary
}()
/**
-1
View File
@@ -1,4 +1,3 @@
- [ ] #2: this commit may be how stephencelis fixed it: https://github.com/stephencelis/SQLite.swift/commit/8f64e357c3a6668c5f011c91ba33be3e8d4b88d0
- [ ] Use @warn_unused_result
- [ ] Make RowModel.updateFromRow() nicer with the edited flag.
- [ ] RowModel.databaseChanges. It's easy to tell which columns have changed, but difficult to tell how, because there is not always an old value. Should we return an array of changed columns instead? We only track *column changes*, not *property changes* (column "latitude" has changed, but we don't know anything about the "coordinate" property): is it a problem? What is the use case?