From e13892d703aa44e91aba39af3b6e553aeadae3c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 7 Jul 2015 07:51:46 +0200 Subject: [PATCH] RowModel supports conflict resolutions: INSERT OR REPLACE, etc. --- .../RowModel/PrimaryKeyRowIDTests.swift | 4 +- GRDB/RowModel.swift | 59 ++++++++++++++++--- README.md | 4 +- TODO.md | 1 - 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/GRDB Tests/RowModel/PrimaryKeyRowIDTests.swift b/GRDB Tests/RowModel/PrimaryKeyRowIDTests.swift index 075fe34cd..4738a63c7 100644 --- a/GRDB Tests/RowModel/PrimaryKeyRowIDTests.swift +++ b/GRDB Tests/RowModel/PrimaryKeyRowIDTests.swift @@ -55,13 +55,13 @@ class Person: RowModel { if let v = row["creationTimestamp"] { creationDate = (v.value() as DBDate?)?.date } } - override func insert(db: Database) throws { + override func insert(db: Database, conflictResolution: ConflictResolution? = nil) throws { // TODO: test if creationDate == nil { creationDate = NSDate() } - try super.insert(db) + try super.insert(db, conflictResolution: conflictResolution) } init (name: String? = nil, age: Int? = nil) { diff --git a/GRDB/RowModel.swift b/GRDB/RowModel.swift index 897973e45..2e07a2917 100644 --- a/GRDB/RowModel.swift +++ b/GRDB/RowModel.swift @@ -63,8 +63,16 @@ public class RowModel { // MARK: - CRUD + public enum ConflictResolution { + case Replace + case Rollback + case Abort + case Fail + case Ignore + } + /// Inserts - public func insert(db: Database) throws { + public func insert(db: Database, conflictResolution: ConflictResolution? = nil) throws { // Table name @@ -109,7 +117,24 @@ public class RowModel { let columns = insertedDic.keys let columnSQL = ",".join(columns.map { $0.sqliteQuotedIdentifier }) let valuesSQL = ",".join([String](count: columns.count, repeatedValue: "?")) - let sql = "INSERT INTO \(tableName.sqliteQuotedIdentifier) (\(columnSQL)) VALUES (\(valuesSQL))" + let verb: String + if let conflictResolution = conflictResolution { + switch conflictResolution { + case .Replace: + verb = "INSERT OR REPLACE" + case .Rollback: + verb = "INSERT OR ROLLBACK" + case .Abort: + verb = "INSERT OR ABORT" + case .Fail: + verb = "INSERT OR FAIL" + case .Ignore: + verb = "INSERT OR IGNORE" + } + } else { + verb = "INSERT" + } + let sql = "\(verb) INTO \(tableName.sqliteQuotedIdentifier) (\(columnSQL)) VALUES (\(valuesSQL))" try db.execute(sql, bindings: Bindings(insertedDic.values)) @@ -123,7 +148,8 @@ public class RowModel { /// Throws an error if the model has no table name, or no primary key. /// Returns true if the model still exists in the database and has been updated. - public func update(db: Database) throws -> Bool { + /// See https://www.sqlite.org/lang_update.html + public func update(db: Database, conflictResolution: ConflictResolution? = nil) throws -> Bool { // Table name @@ -163,9 +189,25 @@ public class RowModel { let updateSQL = ",".join(updatedDictionary.keys.map { column in "\(column.sqliteQuotedIdentifier)=?" }) let whereSQL = " AND ".join(primaryKeyDictionary.keys.map { column in "\(column.sqliteQuotedIdentifier)=?" }) let bindings = Bindings(Array(updatedDictionary.values) + Array(primaryKeyDictionary.values)) - let sql = "UPDATE \(tableName.sqliteQuotedIdentifier) SET \(updateSQL) WHERE \(whereSQL)" + let verb: String + if let conflictResolution = conflictResolution { + switch conflictResolution { + case .Replace: + verb = "UPDATE OR REPLACE" + case .Rollback: + verb = "UPDATE OR ROLLBACK" + case .Abort: + verb = "UPDATE OR ABORT" + case .Fail: + verb = "UPDATE OR FAIL" + case .Ignore: + verb = "UPDATE OR IGNORE" + } + } else { + verb = "UPDATE" + } + let sql = "\(verb) \(tableName.sqliteQuotedIdentifier) SET \(updateSQL) WHERE \(whereSQL)" try db.execute(sql, bindings: bindings) - return db.changes > 0 } @@ -175,12 +217,13 @@ public class RowModel { /// /// Returns true if the model has been inserted, or if it still exists in /// the database and has been updated. - final public func save(db: Database) throws -> Bool { + final public func save(db: Database, conflictResolution: ConflictResolution? = nil) throws -> Bool { if let _ = self.dynamicType.primaryKeyDictionary(databaseDictionary) { - return try update(db) + // Primary key set: insert + return try update(db, conflictResolution: conflictResolution) } else { // No primary key: insert - try insert(db) + try insert(db, conflictResolution: conflictResolution) return true } } diff --git a/README.md b/README.md index 2ed1aaaff..298f8aec6 100644 --- a/README.md +++ b/README.md @@ -743,7 +743,7 @@ class Book : RowModel { ... // Before insertion, set uuid primary key if not set yet. - override func insert(db: Database) throws { + override func insert(db: Database, conflictResolution: ConflictResolution? = nil) throws { { if uuid == nil { uuid = NSUUID().UUIDString } @@ -759,7 +759,7 @@ There are a lot of possible customizations: class Person : RowModel { ... - override func insert(db: Database) throws { + override func insert(db: Database, conflictResolution: ConflictResolution? = nil) throws { { creationDate = NSDate() try validate() try super.insert(db) diff --git a/TODO.md b/TODO.md index 0e76a5433..f8d45c0eb 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,4 @@ - [ ] RowModel support for implicit RowID column (see https://www.sqlite.org/autoinc.html) - [ ] db.fetch(RowModel.type, primaryKeys:[...]) // several primary keys - [ ] Let Bindings eat arrays, or make it easy to generate series of ?,?,?,? -- [ ] Investigate INSERT OR REPLACE - [ ] Pierlo complains that databaseDictionary returns a dictionary of values when updateFromDatabaseRow eats a Row. See if we can do something about that.