From f3a2e5f24302fc9dc7224497fbb902ed545a7132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 5 Sep 2015 19:15:42 +0200 Subject: [PATCH] Drop RowModel.setDatabaseValue(_:forColumn:). This leaves RowModel.updateFromRow(_) as the only method that updates a RowModel. RowModel.updateFromRow was introduced in order to let models update compound properties such as CLLocationCoordinate2D. The problem was that RowModel.updateFromRow could be overriden so that it would handle 100% of the updating job, leaving RowModel.setDatabaseValue as a method that could not reliably be used to update a RowModel, and thus had to be dropped. --- DemoApps/GRDBDemoiOS/GRDBDemoiOS/Person.swift | 15 +- .../MinimalPrimaryKeyRowIDTests.swift | 11 +- .../MinimalPrimaryKeySingleTests.swift | 11 +- .../RowModel/PrimaryKeyMultipleTests.swift | 15 +- .../Public/RowModel/PrimaryKeyNoneTests.swift | 11 +- .../RowModel/PrimaryKeyRowIDTests.swift | 17 +- .../RowModel/PrimaryKeySingleTests.swift | 13 +- ...leWithReplaceConflictResolutionTests.swift | 16 +- .../Public/RowModel/RowModelEditedTests.swift | 11 +- .../Public/RowModel/RowModelEventsTests.swift | 11 +- .../RowModel/RowModelSubClassTests.swift | 11 +- .../RowModel/RowModelUpdateFromRowTests.swift | 19 +-- GRDB/DatabaseDate.swift | 13 +- GRDB/RowModel.swift | 106 +++++-------- Playground.playground/Contents.swift | 15 +- README.md | 147 +++++------------- RELEASE_NOTES.md | 1 + 17 files changed, 194 insertions(+), 249 deletions(-) diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Person.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Person.swift index 63a3de667..043f69095 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Person.swift +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Person.swift @@ -21,13 +21,16 @@ class Person : RowModel { return Table(named: "persons", primaryKey: .RowID("id")) } - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "id": id = dbv.value() - case "firstName": firstName = dbv.value() - case "lastName": lastName = dbv.value() - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + for (column, dbv) in row { + switch column { + case "id": id = dbv.value() + case "firstName": firstName = dbv.value() + case "lastName": lastName = dbv.value() + default: break + } } + super.updateFromRow(row) // Subclasses are required to call super. } override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] { diff --git a/GRDB Tests/Public/RowModel/MinimalPrimaryKeyRowIDTests.swift b/GRDB Tests/Public/RowModel/MinimalPrimaryKeyRowIDTests.swift index 85f376bf2..5d3eb2102 100644 --- a/GRDB Tests/Public/RowModel/MinimalPrimaryKeyRowIDTests.swift +++ b/GRDB Tests/Public/RowModel/MinimalPrimaryKeyRowIDTests.swift @@ -14,11 +14,14 @@ class MinimalRowID: RowModel { return ["id": id] } - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "id": id = dbv.value() - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + for (column, dbv) in row { + switch column { + case "id": id = dbv.value() + default: break + } } + super.updateFromRow(row) // Subclasses are required to call super. } static func setupInDatabase(db: Database) throws { diff --git a/GRDB Tests/Public/RowModel/MinimalPrimaryKeySingleTests.swift b/GRDB Tests/Public/RowModel/MinimalPrimaryKeySingleTests.swift index 143463158..5232eca18 100644 --- a/GRDB Tests/Public/RowModel/MinimalPrimaryKeySingleTests.swift +++ b/GRDB Tests/Public/RowModel/MinimalPrimaryKeySingleTests.swift @@ -14,11 +14,14 @@ class MinimalSingle: RowModel { return ["UUID": UUID] } - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "UUID": UUID = dbv.value() - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + for (column, dbv) in row { + switch column { + case "UUID": UUID = dbv.value() + default: break + } } + super.updateFromRow(row) // Subclasses are required to call super. } static func setupInDatabase(db: Database) throws { diff --git a/GRDB Tests/Public/RowModel/PrimaryKeyMultipleTests.swift b/GRDB Tests/Public/RowModel/PrimaryKeyMultipleTests.swift index 775fa98a2..e1b203d40 100644 --- a/GRDB Tests/Public/RowModel/PrimaryKeyMultipleTests.swift +++ b/GRDB Tests/Public/RowModel/PrimaryKeyMultipleTests.swift @@ -18,13 +18,16 @@ class Citizenship: RowModel { "native": native] } - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "personName": personName = dbv.value() - case "countryName": countryName = dbv.value() - case "native": native = dbv.value() - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + for (column, dbv) in row { + switch column { + case "personName": personName = dbv.value() + case "countryName": countryName = dbv.value() + case "native": native = dbv.value() + default: break + } } + super.updateFromRow(row) // Subclasses are required to call super. } init (personName: String? = nil, countryName: String? = nil, native: Bool? = nil) { diff --git a/GRDB Tests/Public/RowModel/PrimaryKeyNoneTests.swift b/GRDB Tests/Public/RowModel/PrimaryKeyNoneTests.swift index ea4504e0d..48373c1bf 100644 --- a/GRDB Tests/Public/RowModel/PrimaryKeyNoneTests.swift +++ b/GRDB Tests/Public/RowModel/PrimaryKeyNoneTests.swift @@ -13,11 +13,14 @@ class Item: RowModel { return ["name": name] } - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "name": name = dbv.value() - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + for (column, dbv) in row { + switch column { + case "name": name = dbv.value() + default: break + } } + super.updateFromRow(row) // Subclasses are required to call super. } init (name: String? = nil) { diff --git a/GRDB Tests/Public/RowModel/PrimaryKeyRowIDTests.swift b/GRDB Tests/Public/RowModel/PrimaryKeyRowIDTests.swift index fe73a78c6..952a33d1b 100644 --- a/GRDB Tests/Public/RowModel/PrimaryKeyRowIDTests.swift +++ b/GRDB Tests/Public/RowModel/PrimaryKeyRowIDTests.swift @@ -21,14 +21,17 @@ class Person: RowModel { ] } - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "id": id = dbv.value() - case "name": name = dbv.value() - case "age": age = dbv.value() - case "creationDate": creationDate = (dbv.value() as DatabaseDate?)?.date - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + for (column, dbv) in row { + switch column { + case "id": id = dbv.value() + case "name": name = dbv.value() + case "age": age = dbv.value() + case "creationDate": creationDate = (dbv.value() as DatabaseDate?)?.date + default: break + } } + super.updateFromRow(row) // Subclasses are required to call super. } init (id: Int64? = nil, name: String? = nil, age: Int? = nil, creationDate: NSDate? = nil) { diff --git a/GRDB Tests/Public/RowModel/PrimaryKeySingleTests.swift b/GRDB Tests/Public/RowModel/PrimaryKeySingleTests.swift index 10083d5e5..cf93c0fdb 100644 --- a/GRDB Tests/Public/RowModel/PrimaryKeySingleTests.swift +++ b/GRDB Tests/Public/RowModel/PrimaryKeySingleTests.swift @@ -16,12 +16,15 @@ class Pet: RowModel { "name": name] } - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "UUID": UUID = dbv.value() - case "name": name = dbv.value() - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + for (column, dbv) in row { + switch column { + case "UUID": UUID = dbv.value() + case "name": name = dbv.value() + default: break + } } + super.updateFromRow(row) // Subclasses are required to call super. } init (UUID: String? = nil, name: String? = nil) { diff --git a/GRDB Tests/Public/RowModel/PrimaryKeySingleWithReplaceConflictResolutionTests.swift b/GRDB Tests/Public/RowModel/PrimaryKeySingleWithReplaceConflictResolutionTests.swift index 5bf65713a..b508065a8 100644 --- a/GRDB Tests/Public/RowModel/PrimaryKeySingleWithReplaceConflictResolutionTests.swift +++ b/GRDB Tests/Public/RowModel/PrimaryKeySingleWithReplaceConflictResolutionTests.swift @@ -13,15 +13,15 @@ class Email : RowModel { return ["email": email, "label": label] } - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "email": - self.email = dbv.value() - case "label": - self.label = dbv.value() - default: - super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + for (column, dbv) in row { + switch column { + case "email": email = dbv.value() + case "label": label = dbv.value() + default: break + } } + super.updateFromRow(row) // Subclasses are required to call super. } } diff --git a/GRDB Tests/Public/RowModel/RowModelEditedTests.swift b/GRDB Tests/Public/RowModel/RowModelEditedTests.swift index eec003ab1..503115d8c 100644 --- a/GRDB Tests/Public/RowModel/RowModelEditedTests.swift +++ b/GRDB Tests/Public/RowModel/RowModelEditedTests.swift @@ -8,11 +8,14 @@ class IntegerPropertyOnRealAffinityColumn : RowModel { return ["value": value] } - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "value": value = dbv.value() - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + for (column, dbv) in row { + switch column { + case "value": value = dbv.value() + default: break + } } + super.updateFromRow(row) // Subclasses are required to call super. } } diff --git a/GRDB Tests/Public/RowModel/RowModelEventsTests.swift b/GRDB Tests/Public/RowModel/RowModelEventsTests.swift index 83ca0915e..7266cee79 100644 --- a/GRDB Tests/Public/RowModel/RowModelEventsTests.swift +++ b/GRDB Tests/Public/RowModel/RowModelEventsTests.swift @@ -9,11 +9,14 @@ class EventRecorder : RowModel { return "eventRecorders" } - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "id": id = dbv.value() - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + for (column, dbv) in row { + switch column { + case "id": id = dbv.value() + default: break + } } + super.updateFromRow(row) // Subclasses are required to call super. } override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] { diff --git a/GRDB Tests/Public/RowModel/RowModelSubClassTests.swift b/GRDB Tests/Public/RowModel/RowModelSubClassTests.swift index b184239ac..fa183a8ef 100644 --- a/GRDB Tests/Public/RowModel/RowModelSubClassTests.swift +++ b/GRDB Tests/Public/RowModel/RowModelSubClassTests.swift @@ -10,11 +10,14 @@ class PersonWithOverrides: Person { var extra: Int! var lastSavingMethod: SavingMethod? - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "extra": extra = dbv.value() - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + for (column, dbv) in row { + switch column { + case "extra": extra = dbv.value() + default: break + } } + super.updateFromRow(row) // Subclasses are required to call super. } override func insert(db: Database) throws { diff --git a/GRDB Tests/Public/RowModel/RowModelUpdateFromRowTests.swift b/GRDB Tests/Public/RowModel/RowModelUpdateFromRowTests.swift index 5b55dd1dd..56e6468b8 100644 --- a/GRDB Tests/Public/RowModel/RowModelUpdateFromRowTests.swift +++ b/GRDB Tests/Public/RowModel/RowModelUpdateFromRowTests.swift @@ -54,16 +54,17 @@ class Placemark : RowModel { } } - // Update other columns - super.updateFromRow(row) - } - - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "id": id = dbv.value() - case "name": name = dbv.value() - default: super.setDatabaseValue(dbv, forColumn: column) + // Other columns + for (column, dbv) in row { + switch column { + case "id": id = dbv.value() + case "name": name = dbv.value() + default: break + } } + + // Subclasses are required to call super. + super.updateFromRow(row) } } diff --git a/GRDB/DatabaseDate.swift b/GRDB/DatabaseDate.swift index ce1a9ce76..5c2e62eb2 100644 --- a/GRDB/DatabaseDate.swift +++ b/GRDB/DatabaseDate.swift @@ -32,11 +32,14 @@ Usage: return ["birthDate": DatabaseDate(birthDate), ...] } - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "birthDate": birthDate = (dbv.value() as DatabaseDate?)?.date - case ... - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + for (column, dbv) in row { + switch column { + case "birthDate": birthDate = (dbv.value() as DatabaseDate?)?.date + case ... + } + } + super.updateFromRow(row) // Subclasses are required to call super. } } diff --git a/GRDB/RowModel.swift b/GRDB/RowModel.swift index d78d2ec8e..da8c6de74 100644 --- a/GRDB/RowModel.swift +++ b/GRDB/RowModel.swift @@ -7,7 +7,7 @@ designed to be subclassed. Subclasses opt in RowModel features by overriding all or part of the core methods that define their relationship with the database: -- setDatabaseValue(_:forColumn:) +- updateFromRow(_) - databaseTable - storedDatabaseDictionary */ @@ -23,44 +23,6 @@ public class RowModel { } - // MARK: - Core methods - - /** - Returns a table definition. - - The insert, update, save, delete and reload methods require it: they raise - a fatal error if databaseTableName is nil. - - The implementation of the base class RowModel returns nil. - */ - public class var databaseTableName: String? { - return nil - } - - /** - Returns the values that should be stored in the database. - - Subclasses must include primary key columns, if any, in the returned - dictionary. - - The implementation of the base class RowModel returns an empty dictionary. - */ - public var storedDatabaseDictionary: [String: DatabaseValueConvertible?] { - return [:] - } - - /** - Updates `self` with a database value. - - The implementation of the base class RowModel does nothing. - - - parameter dbv: A DatabaseValue. - - parameter column: A column name. - */ - public func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - } - - // MARK: - Initializers /** @@ -104,6 +66,41 @@ public class RowModel { } + // MARK: - Core methods + + /** + Returns a table definition. + + The insert, update, save, delete and reload methods require it: they raise + a fatal error if databaseTableName is nil. + + The implementation of the base class RowModel returns nil. + */ + public class var databaseTableName: String? { + return nil + } + + /** + Returns the values that should be stored in the database. + + Subclasses must include primary key columns, if any, in the returned + dictionary. + + The implementation of the base class RowModel returns an empty dictionary. + */ + public var storedDatabaseDictionary: [String: DatabaseValueConvertible?] { + return [:] + } + + /** + Updates self from a row. + + *Important*: subclasses must invoke super's implementation. + */ + public func updateFromRow(row: Row) { + } + + // MARK: - Events /** @@ -115,18 +112,7 @@ public class RowModel { } - // MARK: - Update - - /** - Updates self from a row. - - *Important*: subclasses must invoke super's implementation. - */ - public func updateFromRow(row: Row) { - for (column, databaseValue) in row { - setDatabaseValue(databaseValue, forColumn: column) - } - } + // MARK: - Copy /** Updates `self` from `other.storedDatabaseDictionary`. @@ -223,24 +209,6 @@ public class RowModel { fatalError("\(self.dynamicType).storedDatabaseDictionary must return the value for the primary key `(managedColumn)`") } if rowID == nil { - // IMPLEMENTATION NOTE: - // - // We update the ID with updateFromRow(), and not - // setDatabaseValue(_:forColumn:). Rationale: - // - // 1. If subclass updates its ID in setDatabaseValue(), then the - // default updateFromRow() runs, which calls - // setDatabaseValue(), and updates the ID. - // - // 2. If subclass overrides updateFromRow() and updates its ID - // in setDatabaseValue(), then the subclasses calls super - // from updateFromRow() (as it is required to do), which in - // turns call setDatabaseValue(), and updates the ID. - // - // 3. If subclass overrides updateFromRow() and updates its ID - // in updateFromRow(), not in setDatabaseValue(), which it is - // allowed to do, then using setDatabaseValue() would not - // update the ID. updateFromRow(Row(dictionary: [managedColumn: changes.insertedRowID])) } } diff --git a/Playground.playground/Contents.swift b/Playground.playground/Contents.swift index 3f8c90bcb..ceda9ee08 100644 --- a/Playground.playground/Contents.swift +++ b/Playground.playground/Contents.swift @@ -39,13 +39,16 @@ class Person : RowModel { return "persons" } - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "id": id = dbv.value() - case "firstName": firstName = dbv.value() - case "lastName": lastName = dbv.value() - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + for (column, dbv) in row { + switch column { + case "id": id = dbv.value() + case "firstName": firstName = dbv.value() + case "lastName": lastName = dbv.value() + default: break + } } + super.updateFromRow(row) // Subclasses are required to call super. } override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] { diff --git a/README.md b/README.md index 5699aae8c..a5a10bd8a 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,6 @@ To fiddle with the library, open the `GRDB.xcworkspace` workspace: it contains a - [RowModel API Quick Tour](#rowmodel-api-quick-tour) - [Core Methods](#core-methods) - [Fetching Row Models](#fetching-row-models) - - [Compound Properties](#compound-properties) - [Ad Hoc Subclasses](#ad-hoc-subclasses) - [Tables and Primary Keys](#tables-and-primary-keys) - [Insert, Update and Delete](#insert-update-and-delete) @@ -400,22 +399,22 @@ DatabaseDate.fetchAll(db, "SELECT ...") // [DatabaseDate?] DatabaseDate.fetchOne(db, "SELECT ...") // DatabaseDate? ``` -Use NSDate in a [Row Model](#row-models): +Use NSDate in a RowModel (see [Fetching Row Models](#fetching-row-models) for more information): ```swift class Person : RowModel { var birthDate: NSDate? - + override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] { return ["birthDate": DatabaseDate(birthDate), ...] } - - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "birthDate": birthDate = (dbv.value() as DatabaseDate?)?.date - case ... - default: super.setDatabaseValue(dbv, forColumn: column) + + override func updateFromRow(row: Row) { + if let dbv = row["birthDate"] { + let dbDate = dbv.value() as DatabaseDate? + birthDate = dbDate?.date } + ... } } ``` @@ -455,12 +454,12 @@ DatabaseDateComponents.fetchAll(db, "SELECT ...") // [DatabaseDateComponents?] DatabaseDateComponents.fetchOne(db, "SELECT ...") // DatabaseDateComponents? ``` -Use NSDateComponents in a [Row Model](#row-models): +Use NSDate in a RowModel (see [Fetching Row Models](#fetching-row-models) for more information): ```swift class Person : RowModel { var birthDateComponents: NSDateComponents? - + override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] { // Store birth date as YYYY-MM-DD: let dbComponents = DatabaseDateComponents( @@ -468,15 +467,13 @@ class Person : RowModel { format: .YMD) return ["birthDate": dbComponents, ...] } - - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "birthDate": - let dbComponents: DatabaseDateComponents? = dbv.value() + + override func updateFromRow(row: Row) { + if let dbv = row["birthDate"] { + let dbComponents = dbv.value() as DatabaseDateComponents? birthDateComponents = dbComponents?.dateComponents - case ... - default: super.setDatabaseValue(dbv, forColumn: column) } + ... } } ``` @@ -821,7 +818,6 @@ Yet, it does a few things well: - [RowModel API Quick Tour](#rowmodel-api-quick-tour) - [Core Methods](#core-methods) - [Fetching Row Models](#fetching-row-models) - - [Compound Properties](#compound-properties) - [Ad Hoc Subclasses](#ad-hoc-subclasses) - [Tables and Primary Keys](#tables-and-primary-keys) - [Insert, Update and Delete](#insert-update-and-delete) @@ -874,11 +870,11 @@ Person.fetchOne(statement, arguments: ...) // Person? Subclasses opt in RowModel features by overriding all or part of the core methods that define their relationship with the database: -| Core Methods | fetch | insert | update | delete | reload | -|:-------------------------------- |:-----:|:------:|:------:|:------:|:------:| -| `setDatabaseValue(_:forColumn:)` | ✓ | | | | ✓ | -| `databaseTableName` | | ✓ | ✓ | ✓ | ✓ | -| `storedDatabaseDictionary` | | ✓ | ✓ | ✓ | ✓ | +| Core Methods | fetch | insert | update | delete | reload | +|:-------------------------- |:-----:|:------:|:------:|:------:|:------:| +| `updateFromRow` | ✓ | | | | ✓ | +| `databaseTableName` | | ✓ | ✓ | ✓ | ✓ | +| `storedDatabaseDictionary` | | ✓ | ✓ | ✓ | ✓ | ### Fetching Row Models @@ -893,17 +889,21 @@ class Person : RowModel { } ``` -The `setDatabaseValue(_:forColumn:)` method assigns database values to properties: +The `updateFromRow` method assigns database values to properties: ```swift class Person : RowModel { - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "id": id = dbv.value() - case "age": age = dbv.value() - case "name": name = dbv.value() - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + // Iterate (ColumnName, DatabaseValue) pairs in row: + for (column, dbv) in row { + switch column { + case "id": id = dbv.value() + case "age": age = dbv.value() + case "name": name = dbv.value() + default: break + } } + super.updateFromRow(row) // Subclasses are required to call super. } } ``` @@ -939,70 +939,6 @@ for person in persons { ... } // OK ``` -#### Compound Properties - -Some properties don't fit well in a single column: - -```swift -class Placemark : RowModel { - // Stored in two columns: latitude and longitude - var coordinate: CLLocationCoordinate2D? -} -``` - -A solution is of course to declare two convenience properties, latitude and longitude, and implement coordinate on top of them, as a computed property. `setDatabaseValue()` would then update latitude and longitude separately: - -```swift -// Solution 1: computed property: -class Placemark : RowModel { - var latitude: CLLocationDegrees? - var longitude: CLLocationDegrees? - var coordinate: CLLocationCoordinate2D? { - switch (latitude, longitude) { - case (let latitude?, let longitude?): - // Both latitude and longitude are not nil. - return CLLocationCoordinate2DMake(latitude, longitude) - default: - return nil - } - } - - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "latitude": latitude = dbv.value() - case "longitude": longitude = dbv.value() - default: super.setDatabaseValue(dbv, forColumn: column) - } - } -} -``` - -Another solution is to override the `updateFromRow()` method, where you can process a row as a whole: - -```swift -// Solution 2: process rows as a whole: -class Placemark : RowModel { - var coordinate: CLLocationCoordinate2D? - - override func updateFromRow(row: Row) { - if let latitude = row["latitude"], let longitude = row["longitude"] { - // Both columns are present. - switch (latitude.value() as Double?, longitude.value() as Double?) { - case (let latitude?, let longitude?): - // Both latitude and longitude are not nil. - coordinate = CLLocationCoordinate2DMake(latitude, longitude) - default: - coordinate = nil - } - } - super.updateFromRow(row) // Subclasses are required to call super. - } -} -``` - -The remaining columns are handled by `setDatabaseValue()` as described above. - - #### Ad Hoc Subclasses Swift makes it very easy to create small and private types. This is a wonderful opportunity to create **ad hoc subclasses** that provide support for custom queries with extra columns. @@ -1016,11 +952,11 @@ class PersonsViewController: UITableViewController { private class PersonViewModel : Person { var bookCount: Int! - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "bookCount": bookCount = dbv.value() - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + if dbv = row["bookCount"] { + bookCount = dbv.value() } + super.updateFromRow(row) // Subclasses are required to call super. } } @@ -1172,12 +1108,15 @@ class Person : RowModel { } /// Updates `self` with a database value. - override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) { - switch column { - case "id": id = dbv.value() - case ... - default: super.setDatabaseValue(dbv, forColumn: column) + override func updateFromRow(row: Row) { + // Iterate (ColumnName, DatabaseValue) pairs in row: + for (column, dbv) in row { + switch column { + case "id": id = dbv.value() + case... + } } + super.updateFromRow(row) // Subclasses are required to call super. } } diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 996b27e3b..421e0a0cf 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -12,6 +12,7 @@ Release Notes - `RowModel.edited` has been renamed `RowModel.databaseEdited`. - `RowModel.databaseTable` has been replaced with `RowModel.databaseTableName` which returns a String. +- `RowModel.setDatabaseValue(_:forColumn:)` has been removed. Use and override `RowModel.updateFromRow(_)` instead. ## 0.11.0