diff --git a/GRDB Tests/RowModel/PrimaryKeyMultipleTests.swift b/GRDB Tests/RowModel/PrimaryKeyMultipleTests.swift index eced72e0d..8c383ab79 100644 --- a/GRDB Tests/RowModel/PrimaryKeyMultipleTests.swift +++ b/GRDB Tests/RowModel/PrimaryKeyMultipleTests.swift @@ -39,19 +39,13 @@ class Citizenship: RowModel { } override var databaseDictionary: [String: SQLiteValueConvertible?] { - return ["personID": personID, "countryName": countryName, "grantedTimestamp": grantedDate?.timeIntervalSince1970] + return ["personID": personID, "countryName": countryName, "grantedTimestamp": DBDate(grantedDate)] } override func updateFromDatabaseRow(row: Row) { - if row.hasColumn("personID") { personID = row.value(named: "personID") } - if row.hasColumn("countryName") { countryName = row.value(named: "countryName") } - if row.hasColumn("grantedTimestamp") { - if let timestamp: NSTimeInterval = row.value(named: "grantedTimestamp") { - grantedDate = NSDate(timeIntervalSince1970: timestamp) - } else { - grantedDate = nil - } - } + if let v = row.sqliteValue(named: "personID") { personID = v.value() } + if let v = row.sqliteValue(named: "countryName") { countryName = v.value() } + if let v = row.sqliteValue(named: "grantedTimestamp") { grantedDate = (v.value() as DBDate?)?.date } } static func setupInDatabase(db: Database) throws { diff --git a/GRDB Tests/RowModel/PrimaryKeyNoneTests.swift b/GRDB Tests/RowModel/PrimaryKeyNoneTests.swift index e225b9e1e..2d49f9e24 100644 --- a/GRDB Tests/RowModel/PrimaryKeyNoneTests.swift +++ b/GRDB Tests/RowModel/PrimaryKeyNoneTests.swift @@ -37,7 +37,7 @@ class Item: RowModel { } override func updateFromDatabaseRow(row: Row) { - if row.hasColumn("name") { name = row.value(named: "name") } + if let v = row.sqliteValue(named: "name") { name = v.value() } } static func setupInDatabase(db: Database) throws { diff --git a/GRDB Tests/RowModel/PrimaryKeyRowIDTests.swift b/GRDB Tests/RowModel/PrimaryKeyRowIDTests.swift index 6ad9e943e..7e0ce661f 100644 --- a/GRDB Tests/RowModel/PrimaryKeyRowIDTests.swift +++ b/GRDB Tests/RowModel/PrimaryKeyRowIDTests.swift @@ -49,13 +49,6 @@ class Person: RowModel { } override func updateFromDatabaseRow(row: Row) { -// // V1 -// if row.hasColumn("id") { id = row.value(named: "id") } -// if row.hasColumn("name") { name = row.value(named: "name") } -// if row.hasColumn("age") { age = row.value(named: "age") } -// if row.hasColumn("creationTimestamp") { creationDate = (row.value(named: "creationTimestamp") as DBDate?)?.date } - - // V2 if let v = row.sqliteValue(named: "id") { id = v.value() } if let v = row.sqliteValue(named: "name") { name = v.value() } if let v = row.sqliteValue(named: "age") { age = v.value() } diff --git a/GRDB Tests/RowModel/PrimaryKeySingleTests.swift b/GRDB Tests/RowModel/PrimaryKeySingleTests.swift index 9ac4775d1..2f96b28f5 100644 --- a/GRDB Tests/RowModel/PrimaryKeySingleTests.swift +++ b/GRDB Tests/RowModel/PrimaryKeySingleTests.swift @@ -43,9 +43,9 @@ class Pet: RowModel { } override func updateFromDatabaseRow(row: Row) { - if row.hasColumn("UUID") { UUID = row.value(named: "UUID") } - if row.hasColumn("name") { name = row.value(named: "name") } - if row.hasColumn("masterID") { masterID = row.value(named: "masterID") } + if let v = row.sqliteValue(named: "UUID") { UUID = v.value() } + if let v = row.sqliteValue(named: "name") { name = v.value() } + if let v = row.sqliteValue(named: "masterID") { masterID = v.value() } } init (UUID: String? = nil, name: String? = nil, masterID: Int64? = nil) { diff --git a/GRDB Tests/RowModel/RowModelSubClassTests.swift b/GRDB Tests/RowModel/RowModelSubClassTests.swift index 4b77795e8..68b6b357c 100644 --- a/GRDB Tests/RowModel/RowModelSubClassTests.swift +++ b/GRDB Tests/RowModel/RowModelSubClassTests.swift @@ -30,7 +30,7 @@ class PersonWithPet: Person { override func updateFromDatabaseRow(row: Row) { super.updateFromDatabaseRow(row) - if row.hasColumn("petCount") { petCount = row.value(named: "petCount") } + if let v = row.sqliteValue(named: "petCount") { petCount = v.value() } } } diff --git a/GRDB/Row.swift b/GRDB/Row.swift index 8bc772fcc..6c809adf4 100644 --- a/GRDB/Row.swift +++ b/GRDB/Row.swift @@ -27,26 +27,26 @@ A row is the result of a database query. */ public struct Row: CollectionType { - // MARK: - Columns - /** - Return whether the row contains a column named *name*. + // MARK: - SQLiteValue - Usage: + /// Helper method for tests. + /// Public experimental (TODO: document) + public func sqliteValue(atIndex index: Int) -> SQLiteValue { + return impl.sqliteValue(atIndex: index) + } - if row.hasColumn("name") { - let name: String? = row.value(named:"name") + /// Public Experimental (TODO: document) + public func sqliteValue(named columnName: String) -> SQLiteValue? { + if let index = impl.indexForColumn(named: columnName) { + return impl.sqliteValue(atIndex: index) + } else { + return nil } - - - parameter name: A column name. - - returns: true if the row has such column. - */ - public func hasColumn(name: String) -> Bool { - return impl.sqliteDictionary.indexForKey(name) != nil } - // MARK: - Values by column index + // MARK: - SQLiteValueConvertible value /** Returns the value at given index. @@ -105,9 +105,6 @@ public struct Row: CollectionType { return impl.sqliteValue(atIndex: index).value() } - - // MARK: - Values by column name - /** Returns the value for the given column. @@ -222,20 +219,6 @@ public struct Row: CollectionType { */ let impl: RowImpl - /// Helper method for tests. - func sqliteValue(atIndex index: Int) -> SQLiteValue { - return impl.sqliteValue(atIndex: index) - } - - /// Experimental - public func sqliteValue(named columnName: String) -> SQLiteValue? { - if let index = impl.indexForColumn(named: columnName) { - return impl.sqliteValue(atIndex: index) - } else { - return nil - } - } - // MARK: Initializers diff --git a/README.md b/README.md index e907ac9ec..49d4da830 100644 --- a/README.md +++ b/README.md @@ -508,15 +508,15 @@ By overriding `updateFromDatabaseRow`, you can load persons from the database: class Person : RowModel { ... - // Boring and not DRY, but straightforward: + // Boring but straightforward: override func updateFromDatabaseRow(row: Row) { - if row.hasColumn("id") { id = row.value(named: "id") } // Int64 - if row.hasColumn("age") { age = row.value(named: "age") } // Int - if row.hasColumn("name") { name = row.value(named: "name") } // String - if row.hasColumn("creationTimestamp") { // NSDate + if let v = row.sqliteValue(named: "id") { id = v.value() } // Int64 + if let v = row.sqliteValue(named: "name") { name = v.value() } // Int + if let v = row.sqliteValue(named: "age") { age = v.value() } // String + if let v = row.sqliteValue(named: "creationTimestamp") { // NSDate // Use the DBDate custom type declared above: - creationDate = (row.value(named: "creationTimestamp") as DBDate?)?.date + creationDate = (v.value() as DBDate?)?.date } } }