Row.sqliteValue(named:) experimentation for better RowModel.updateFromDatabaseRow

This commit is contained in:
Gwendal Roué committed 2015-07-05 17:19:35 +02:00
1 parent 73c230098c
commit 7576008c14
7 files changed
+28 -58

No files matched your search

@@ -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 {
@@ -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 {
@@ -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() }
@@ -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) {
@@ -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() }
}
}
+13 -30
View File
@@ -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
+6 -6
View File
@@ -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
}
}
}