diff --git a/GRDB Tests/Public/RowModel/RowModelIsEditedTests.swift b/GRDB Tests/Public/RowModel/RowModelIsEditedTests.swift index a7bd1cb8a..845ee0f5d 100644 --- a/GRDB Tests/Public/RowModel/RowModelIsEditedTests.swift +++ b/GRDB Tests/Public/RowModel/RowModelIsEditedTests.swift @@ -48,6 +48,20 @@ class RowModelIsEditedTests: RowModelTestCase { } } + func testRowModelIsNotEditedAfterWiderThanFullFetch() { + // Fetch a model from a row that contains all the columns in + // storedDatabaseDictionary, plus extra ones: An update statement, which + // only saves the columns in storedDatabaseDictionary would perform no + // change. So the model is not edited. + assertNoError { + try dbQueue.inDatabase { db in + try Person(name: "Arthur", age: 41).insert(db) + let person = db.fetchOne(Person.self, "SELECT *, 1 AS foo FROM persons")! + XCTAssertFalse(person.isEdited) + } + } + } + func testRowModelIsEditedAfterPartialFetch() { // Fetch a model from a row that does not contain all the columns in // storedDatabaseDictionary: An update statement saves the columns in diff --git a/GRDB/Row.swift b/GRDB/Row.swift index 5f73a11a9..ce47ac671 100644 --- a/GRDB/Row.swift +++ b/GRDB/Row.swift @@ -248,21 +248,6 @@ public struct Row: CollectionType { } } - func containsSameColumnsAndValuesAsRow(other: Row) -> Bool { - guard count == other.count else { - return true - } - for (key, dbv) in self { - guard let otherDbv = other[key] else { - return true - } - if dbv != otherDbv { - return true - } - } - return false - } - // MARK: - DictionaryRowImpl diff --git a/GRDB/RowModel.swift b/GRDB/RowModel.swift index 19aa8e05c..0fd910fa4 100644 --- a/GRDB/RowModel.swift +++ b/GRDB/RowModel.swift @@ -189,8 +189,17 @@ public class RowModel { return true } - let currentRow = Row(dictionary: storedDatabaseDictionary) - return referenceRow.containsSameColumnsAndValuesAsRow(currentRow) + // All stored database values must match reference database values + for (column, storedValue) in storedDatabaseDictionary { + guard let referenceDatabaseValue = referenceRow[column] else { + return true + } + let storedDatabaseValue = storedValue?.databaseValue ?? .Null + if storedDatabaseValue != referenceDatabaseValue { + return true + } + } + return false } /** diff --git a/TODO.md b/TODO.md index be31db4b7..e1061b958 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,3 @@ - [ ] Fetch with a collection of primary keys: db.fetch(RowModel.type, primaryKeys:[...]). Problem: users will often want to sort, so the plain SELECT * FROM persons WHERE id IN (?,?,?) won't be appreciated. Needs more thought. - [ ] Let Bindings eat arrays, or make it easy to generate series of ?,?,?,? - [ ] Remove RowModelError.Dummy when rdar://problem/21707972 is fixed. -- [ ] Test that RowModel.isEdited returns false when rowmodel is fetched with more columns that it stores. \ No newline at end of file