mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Only the IGNORE conflict policy invalidates the last inserted rowID (#118)
This commit is contained in:
1 parent
4ac562a55a
commit
0c525a8e52
3 files changed
+71
-48
No files matched your search
@@ -44,15 +44,15 @@ private func databaseValues(for columns: [String], inDictionary dictionary: [Str
|
||||
/// See https://www.sqlite.org/lang_conflict.html
|
||||
public struct PersistenceConflictPolicy {
|
||||
/// The conflict resolution algorithm for insertions
|
||||
public let insertionConflictResolution: SQLConflictResolution
|
||||
public let conflictResolutionForInsert: SQLConflictResolution
|
||||
|
||||
/// The conflict resolution algorithm for updates
|
||||
public let updateConflictResolution: SQLConflictResolution
|
||||
public let conflictResolutionForUpdate: SQLConflictResolution
|
||||
|
||||
/// Creates a policy
|
||||
public init(insertion: SQLConflictResolution, update: SQLConflictResolution) {
|
||||
self.insertionConflictResolution = insertion
|
||||
self.updateConflictResolution = update
|
||||
public init(insert: SQLConflictResolution = .abort, update: SQLConflictResolution = .abort) {
|
||||
self.conflictResolutionForInsert = insert
|
||||
self.conflictResolutionForUpdate = update
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,13 +67,13 @@ public protocol MutablePersistable : TableMapping {
|
||||
/// The policy that handles SQLite conflicts when records are inserted
|
||||
/// or updated.
|
||||
///
|
||||
/// This property is optional: its default value specifies ABORT policy
|
||||
/// for both insertions and updates, which has GRDB generate regular
|
||||
/// This property is optional: its default value uses the ABORT policy
|
||||
/// for both insertions and updates, and has GRDB generate regular
|
||||
/// INSERT and UPDATE queries.
|
||||
///
|
||||
/// If insertions are resolved with .replace or .ignore policies, the
|
||||
/// If insertions are resolved with .ignore policy, the
|
||||
/// `didInsert(with:for:)` method is not called upon successful insertion,
|
||||
/// even when there was no conflict, and a row was actually inserted.
|
||||
/// even if a row was actually inserted without any conflict.
|
||||
///
|
||||
/// See https://www.sqlite.org/lang_conflict.html
|
||||
static var persistenceConflictPolicy: PersistenceConflictPolicy { get }
|
||||
@@ -207,7 +207,7 @@ public extension MutablePersistable {
|
||||
/// The default value specifies ABORT policy for both insertions and
|
||||
/// updates, which has GRDB generate regular INSERT and UPDATE queries.
|
||||
static var persistenceConflictPolicy: PersistenceConflictPolicy {
|
||||
return PersistenceConflictPolicy(insertion: .abort, update: .abort)
|
||||
return PersistenceConflictPolicy(insert: .abort, update: .abort)
|
||||
}
|
||||
|
||||
/// Notifies the record that it was succesfully inserted.
|
||||
@@ -319,16 +319,12 @@ public extension MutablePersistable {
|
||||
/// implementation of insert(). They should not provide their own
|
||||
/// implementation of performInsert().
|
||||
mutating func performInsert(_ db: Database) throws {
|
||||
let insertionConflictResolution = type(of: self).persistenceConflictPolicy.insertionConflictResolution
|
||||
let conflictResolutionForInsert = type(of: self).persistenceConflictPolicy.conflictResolutionForInsert
|
||||
let dao = DAO(db, self)
|
||||
try dao.insertStatement(onConflict: insertionConflictResolution).execute()
|
||||
try dao.insertStatement(onConflict: conflictResolutionForInsert).execute()
|
||||
|
||||
switch insertionConflictResolution {
|
||||
case .abort, .fail, .rollback:
|
||||
if !conflictResolutionForInsert.invalidatesLastInsertedRowID {
|
||||
didInsert(with: db.lastInsertedRowID, for: dao.primaryKey?.rowIDColumn)
|
||||
case .replace, .ignore:
|
||||
// Statement may have succeeded without inserting any row
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -346,7 +342,7 @@ public extension MutablePersistable {
|
||||
/// PersistenceError.recordNotFound is thrown if the primary key does not
|
||||
/// match any row in the database.
|
||||
func performUpdate(_ db: Database, columns: Set<String>) throws {
|
||||
guard let statement = DAO(db, self).updateStatement(columns: columns, onConflict: type(of: self).persistenceConflictPolicy.updateConflictResolution) else {
|
||||
guard let statement = DAO(db, self).updateStatement(columns: columns, onConflict: type(of: self).persistenceConflictPolicy.conflictResolutionForUpdate) else {
|
||||
// Nil primary key
|
||||
throw PersistenceError.recordNotFound(self)
|
||||
}
|
||||
@@ -418,6 +414,17 @@ public extension MutablePersistable {
|
||||
|
||||
}
|
||||
|
||||
extension SQLConflictResolution {
|
||||
var invalidatesLastInsertedRowID: Bool {
|
||||
switch self {
|
||||
case .abort, .fail, .rollback, .replace:
|
||||
return false
|
||||
case .ignore:
|
||||
// Statement may have succeeded without inserting any row
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Persistable
|
||||
|
||||
@@ -521,16 +528,12 @@ public extension Persistable {
|
||||
/// implementation of insert(). They should not provide their own
|
||||
/// implementation of performInsert().
|
||||
func performInsert(_ db: Database) throws {
|
||||
let insertionConflictResolution = type(of: self).persistenceConflictPolicy.insertionConflictResolution
|
||||
let conflictResolutionForInsert = type(of: self).persistenceConflictPolicy.conflictResolutionForInsert
|
||||
let dao = DAO(db, self)
|
||||
try dao.insertStatement(onConflict: insertionConflictResolution).execute()
|
||||
try dao.insertStatement(onConflict: conflictResolutionForInsert).execute()
|
||||
|
||||
switch insertionConflictResolution {
|
||||
case .abort, .fail, .rollback:
|
||||
if !conflictResolutionForInsert.invalidatesLastInsertedRowID {
|
||||
didInsert(with: db.lastInsertedRowID, for: dao.primaryKey?.rowIDColumn)
|
||||
case .replace, .ignore:
|
||||
// Statement may have succeeded without inserting any row
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -215,13 +215,12 @@ open class Record : RowConvertible, TableMapping, Persistable {
|
||||
// So let's provide our custom implementation of insert, which uses the
|
||||
// same persistentDictionary for both insertion, and change tracking.
|
||||
|
||||
let insertionConflictResolution = type(of: self).persistenceConflictPolicy.insertionConflictResolution
|
||||
let conflictResolutionForInsert = type(of: self).persistenceConflictPolicy.conflictResolutionForInsert
|
||||
let dao = DAO(db, self)
|
||||
var persistentDictionary = dao.persistentDictionary
|
||||
try dao.insertStatement(onConflict: insertionConflictResolution).execute()
|
||||
try dao.insertStatement(onConflict: conflictResolutionForInsert).execute()
|
||||
|
||||
switch insertionConflictResolution {
|
||||
case .abort, .fail, .rollback:
|
||||
if !conflictResolutionForInsert.invalidatesLastInsertedRowID {
|
||||
let rowID = db.lastInsertedRowID
|
||||
let rowIDColumn = dao.primaryKey?.rowIDColumn
|
||||
didInsert(with: rowID, for: rowIDColumn)
|
||||
@@ -239,9 +238,6 @@ open class Record : RowConvertible, TableMapping, Persistable {
|
||||
}
|
||||
}
|
||||
}
|
||||
case .replace, .ignore:
|
||||
// Statement may have succeeded without inserting any row
|
||||
break
|
||||
}
|
||||
|
||||
// Set hasPersistentChangedValues to false
|
||||
@@ -274,7 +270,7 @@ open class Record : RowConvertible, TableMapping, Persistable {
|
||||
// So let's provide our custom implementation of insert, which uses the
|
||||
// same persistentDictionary for both update, and change tracking.
|
||||
let dao = DAO(db, self)
|
||||
guard let statement = dao.updateStatement(columns: columns, onConflict: type(of: self).persistenceConflictPolicy.updateConflictResolution) else {
|
||||
guard let statement = dao.updateStatement(columns: columns, onConflict: type(of: self).persistenceConflictPolicy.conflictResolutionForUpdate) else {
|
||||
// Nil primary key
|
||||
throw PersistenceError.recordNotFound(self)
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ private struct MixedPolicy: MutablePersistable {
|
||||
var id: Int64?
|
||||
|
||||
static let databaseTableName = "records"
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insertion: .fail, update: .rollback)
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .fail, update: .rollback)
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id]
|
||||
@@ -38,12 +38,13 @@ private struct MixedPolicy: MutablePersistable {
|
||||
|
||||
private struct ReplacePolicy: MutablePersistable {
|
||||
var id: Int64?
|
||||
var email: String
|
||||
|
||||
static let databaseTableName = "records"
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insertion: .replace, update: .replace)
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .replace, update: .replace)
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id]
|
||||
return ["id": id, "email": email]
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
@@ -55,7 +56,7 @@ private struct IgnorePolicy: MutablePersistable {
|
||||
var id: Int64?
|
||||
|
||||
static let databaseTableName = "records"
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insertion: .ignore, update: .ignore)
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .ignore, update: .ignore)
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id]
|
||||
@@ -70,7 +71,7 @@ private struct FailPolicy: MutablePersistable {
|
||||
var id: Int64?
|
||||
|
||||
static let databaseTableName = "records"
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insertion: .fail, update: .fail)
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .fail, update: .fail)
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id]
|
||||
@@ -85,7 +86,7 @@ private struct AbortPolicy: MutablePersistable {
|
||||
var id: Int64?
|
||||
|
||||
static let databaseTableName = "records"
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insertion: .abort, update: .abort)
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .abort, update: .abort)
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id]
|
||||
@@ -100,7 +101,7 @@ private struct RollbackPolicy: MutablePersistable {
|
||||
var id: Int64?
|
||||
|
||||
static let databaseTableName = "records"
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insertion: .rollback, update: .rollback)
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .rollback, update: .rollback)
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id]
|
||||
@@ -113,6 +114,12 @@ private struct RollbackPolicy: MutablePersistable {
|
||||
|
||||
class PersistenceConflictPolicyTests: GRDBTestCase {
|
||||
|
||||
func testPolicyDefaultArguments() {
|
||||
let policy = PersistenceConflictPolicy()
|
||||
XCTAssertEqual(policy.conflictResolutionForInsert, .abort)
|
||||
XCTAssertEqual(policy.conflictResolutionForUpdate, .abort)
|
||||
}
|
||||
|
||||
func testDefaultPolicy() {
|
||||
assertNoError {
|
||||
let db = try makeDatabaseQueue()
|
||||
@@ -125,7 +132,7 @@ class PersistenceConflictPolicyTests: GRDBTestCase {
|
||||
var record = DefaultPolicy(id: nil)
|
||||
try record.insert(db)
|
||||
XCTAssertTrue(self.lastSQLQuery.hasPrefix("INSERT INTO \"records\""))
|
||||
XCTAssertTrue(record.id == 1)
|
||||
XCTAssertEqual(record.id, 1)
|
||||
|
||||
// Update
|
||||
record = DefaultPolicy(id: 1)
|
||||
@@ -147,7 +154,7 @@ class PersistenceConflictPolicyTests: GRDBTestCase {
|
||||
var record = MixedPolicy(id: nil)
|
||||
try record.insert(db)
|
||||
XCTAssertTrue(self.lastSQLQuery.hasPrefix("INSERT OR FAIL INTO \"records\""))
|
||||
XCTAssertTrue(record.id == 1)
|
||||
XCTAssertEqual(record.id, 1)
|
||||
|
||||
// Update
|
||||
record = MixedPolicy(id: 1)
|
||||
@@ -163,18 +170,35 @@ class PersistenceConflictPolicyTests: GRDBTestCase {
|
||||
try db.inDatabase { db in
|
||||
try db.create(table: "records") { t in
|
||||
t.column("id", .integer).primaryKey()
|
||||
t.column("email", .text).unique()
|
||||
}
|
||||
|
||||
// Insert
|
||||
var record = ReplacePolicy(id: nil)
|
||||
var record = ReplacePolicy(id: nil, email: "arthur@example.com")
|
||||
try record.insert(db)
|
||||
XCTAssertTrue(self.lastSQLQuery.hasPrefix("INSERT OR REPLACE INTO \"records\""))
|
||||
XCTAssertTrue(record.id == nil)
|
||||
XCTAssertEqual(record.id, 1)
|
||||
|
||||
// Insert
|
||||
record = ReplacePolicy(id: nil, email: "arthur@example.com")
|
||||
try record.insert(db)
|
||||
XCTAssertTrue(self.lastSQLQuery.hasPrefix("INSERT OR REPLACE INTO \"records\""))
|
||||
XCTAssertEqual(record.id, 2)
|
||||
XCTAssertTrue(ReplacePolicy.fetchCount(db) == 1)
|
||||
|
||||
// Update
|
||||
record = ReplacePolicy(id: 1)
|
||||
record = ReplacePolicy(id: 2, email: "arthur@example.com")
|
||||
try record.update(db)
|
||||
XCTAssertTrue(self.lastSQLQuery.hasPrefix("UPDATE OR REPLACE \"records\""))
|
||||
|
||||
// Update which replaces
|
||||
record = ReplacePolicy(id: 3, email: "barbara@example.com")
|
||||
try record.insert(db)
|
||||
XCTAssertTrue(ReplacePolicy.fetchCount(db) == 2)
|
||||
record.email = "arthur@example.com"
|
||||
try record.update(db)
|
||||
XCTAssertTrue(ReplacePolicy.fetchCount(db) == 1)
|
||||
XCTAssertEqual(Int64.fetchOne(db, "SELECT id FROM records")!, 3)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -213,7 +237,7 @@ class PersistenceConflictPolicyTests: GRDBTestCase {
|
||||
var record = FailPolicy(id: nil)
|
||||
try record.insert(db)
|
||||
XCTAssertTrue(self.lastSQLQuery.hasPrefix("INSERT OR FAIL INTO \"records\""))
|
||||
XCTAssertTrue(record.id == 1)
|
||||
XCTAssertEqual(record.id, 1)
|
||||
|
||||
// Update
|
||||
record = FailPolicy(id: 1)
|
||||
@@ -235,7 +259,7 @@ class PersistenceConflictPolicyTests: GRDBTestCase {
|
||||
var record = AbortPolicy(id: nil)
|
||||
try record.insert(db)
|
||||
XCTAssertTrue(self.lastSQLQuery.hasPrefix("INSERT INTO \"records\""))
|
||||
XCTAssertTrue(record.id == 1)
|
||||
XCTAssertEqual(record.id, 1)
|
||||
|
||||
// Update
|
||||
record = AbortPolicy(id: 1)
|
||||
@@ -257,7 +281,7 @@ class PersistenceConflictPolicyTests: GRDBTestCase {
|
||||
var record = RollbackPolicy(id: nil)
|
||||
try record.insert(db)
|
||||
XCTAssertTrue(self.lastSQLQuery.hasPrefix("INSERT OR ROLLBACK INTO \"records\""))
|
||||
XCTAssertTrue(record.id == 1)
|
||||
XCTAssertEqual(record.id, 1)
|
||||
|
||||
// Update
|
||||
record = RollbackPolicy(id: 1)
|
||||
|
||||
Reference in new issue
Block a user