From 2db6e33f421bc766bc4ac7e30578946cc1b8a0ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 2 Jul 2015 20:20:03 +0200 Subject: [PATCH] RowModel.update(db) --- RowModel.swift | 13 +++++++++-- RowModelTests.swift | 56 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/RowModel.swift b/RowModel.swift index 18f34dd04..dc00b1bcc 100644 --- a/RowModel.swift +++ b/RowModel.swift @@ -66,7 +66,8 @@ public class RowModel { // If there is nothing to insert, and primary key is not managed, - // somthing is wrong. + // something is wrong. + guard insertedDic.count > 0 || rowIDColumn != nil else { fatalError("Nothing to insert") } @@ -126,10 +127,18 @@ public class RowModel { primaryKeyDic = dic } + + // If there is nothing to update, something is wrong. + + guard updatedDic.count > 0 else { + fatalError("Nothing to update") + } + + // "UPDATE table SET name = ? WHERE id = ?" let updateSQL = ",".join(updatedDic.keys.map { column in "\(column)=?" }) - let whereSQL = ",".join(primaryKeyDic.keys.map { column in "\(column)=?" }) + let whereSQL = " AND ".join(primaryKeyDic.keys.map { column in "\(column)=?" }) let bindings = Bindings(Array(updatedDic.values) + Array(primaryKeyDic.values)) let sql = "UPDATE \(tableName) SET \(updateSQL) WHERE \(whereSQL)" try db.execute(sql, bindings: bindings) diff --git a/RowModelTests.swift b/RowModelTests.swift index 7a479754a..74ed40487 100644 --- a/RowModelTests.swift +++ b/RowModelTests.swift @@ -106,6 +106,7 @@ class Stuff: RowModel { class Citizenship: RowModel { var personID: Int64? var countryName: String? + var grantedDate: NSDate? override class var databaseTableName: String? { return "citizenships" @@ -116,12 +117,19 @@ class Citizenship: RowModel { } override var databaseDictionary: [String: DatabaseValue?] { - return ["personID": personID, "countryName": countryName] + return ["personID": personID, "countryName": countryName, "grantedTimestamp": grantedDate?.timeIntervalSince1970] } 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 + } + } } } @@ -161,6 +169,7 @@ class RowModelTests: GRDBTests { " REFERENCES persons(ID) " + " ON DELETE CASCADE ON UPDATE CASCADE, " + "countryName TEXT NOT NULL, " + + "grantedTimestamp DOUBLE, " + "PRIMARY KEY (personID, countryName)" + ")") } @@ -371,6 +380,12 @@ class RowModelTests: GRDBTests { assertNoError { let arthur = Person(name: "Arthur", age: 41) + let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! + let dateComponents = NSDateComponents() + dateComponents.year = 1973 + dateComponents.month = 09 + dateComponents.day = 18 + let date = calendar.dateFromComponents(dateComponents)! try dbQueue.inTransaction { db in try arthur.insert(db) @@ -380,6 +395,7 @@ class RowModelTests: GRDBTests { let citizenship = Citizenship() citizenship.personID = arthur.ID citizenship.countryName = "France" + citizenship.grantedDate = date try dbQueue.inTransaction { db in // The tested method @@ -398,6 +414,7 @@ class RowModelTests: GRDBTests { XCTAssertEqual(citizenships.count, 1) XCTAssertEqual(citizenships.first!.personID!, arthur.ID!) XCTAssertEqual(citizenships.first!.countryName!, "France") + XCTAssertEqual(calendar.component(NSCalendarUnit.Year, fromDate: citizenships.first!.grantedDate!), 1973) } } } @@ -474,6 +491,43 @@ class RowModelTests: GRDBTests { } } + func testUpdateRowModelWithMultiplePrimaryKey() { + assertNoError { + let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! + let dateComponents = NSDateComponents() + dateComponents.year = 1973 + dateComponents.month = 09 + dateComponents.day = 18 + let date1 = calendar.dateFromComponents(dateComponents)! + dateComponents.year = 2000 + let date2 = calendar.dateFromComponents(dateComponents)! + XCTAssertFalse(date1.isEqualToDate(date2)) + + try dbQueue.inTransaction { db in + let arthur = Person(name: "Arthur", age: 41) + try arthur.insert(db) + + let citizenship = Citizenship() + citizenship.personID = arthur.ID + citizenship.countryName = "France" + citizenship.grantedDate = date1 + try citizenship.insert(db) + + citizenship.grantedDate = date2 + try citizenship.update(db) // The tested method + return .Commit + } + + // After insertion, model should be present in the database + dbQueue.inDatabase { db in + let citizenships = db.fetchAllModels("SELECT * FROM citizenships", type: Citizenship.self) + XCTAssertEqual(citizenships.count, 1) + XCTAssertEqual(citizenships.first!.countryName!, "France") + XCTAssertEqual(calendar.component(NSCalendarUnit.Year, fromDate: citizenships.first!.grantedDate!), 2000) + } + } + } + func testSelect() { assertNoError { try dbQueue.inTransaction { db in