mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Replace TransactionObserverType.databaseShouldCommit with databaseWillCommit
This commit is contained in:
1 parent
a85c3bb7d7
commit
e573713b54
5 files changed
+159
-16
No files matched your search
@@ -5,18 +5,41 @@ class TransactionObserver : TransactionObserverType {
|
||||
var lastTransactionCompletion: TransactionCompletion! = nil
|
||||
var lastCommittedEvents: [DatabaseEvent] = []
|
||||
var events: [DatabaseEvent] = []
|
||||
var commitError: ErrorType?
|
||||
|
||||
var didChangeCount: Int = 0
|
||||
var willCommitCount: Int = 0
|
||||
var didCommitCount: Int = 0
|
||||
var didRollbackCount: Int = 0
|
||||
|
||||
func resetCounts() {
|
||||
didChangeCount = 0
|
||||
willCommitCount = 0
|
||||
didCommitCount = 0
|
||||
didRollbackCount = 0
|
||||
}
|
||||
|
||||
func databaseDidChangeWithEvent(event: DatabaseEvent) {
|
||||
didChangeCount++
|
||||
events.append(event)
|
||||
}
|
||||
|
||||
func databaseDidCommit() {
|
||||
func databaseWillCommit() throws {
|
||||
willCommitCount++
|
||||
if let commitError = commitError {
|
||||
throw commitError
|
||||
}
|
||||
}
|
||||
|
||||
func databaseDidCommit(db: Database) {
|
||||
didCommitCount++
|
||||
lastTransactionCompletion = .Commit
|
||||
lastCommittedEvents = events
|
||||
events = []
|
||||
}
|
||||
|
||||
func databaseDidRollback(db: Database) {
|
||||
didRollbackCount++
|
||||
lastTransactionCompletion = .Rollback
|
||||
lastCommittedEvents = []
|
||||
events = []
|
||||
@@ -130,7 +153,12 @@ class TransactionObserverTests: GRDBTestCase {
|
||||
let artist = Artist(name: "Gerhard Richter")
|
||||
|
||||
//
|
||||
self.observer.resetCounts()
|
||||
try artist.save(db)
|
||||
XCTAssertEqual(self.observer.didChangeCount, 1)
|
||||
XCTAssertEqual(self.observer.willCommitCount, 1)
|
||||
XCTAssertEqual(self.observer.didCommitCount, 1)
|
||||
XCTAssertEqual(self.observer.didRollbackCount, 0)
|
||||
XCTAssertEqual(self.observer.lastCommittedEvents.count, 1)
|
||||
let event = self.observer.lastCommittedEvents.filter { event in
|
||||
self.match(event: event, kind: .Insert, tableName: "artists", rowId: artist.id!)
|
||||
@@ -148,7 +176,12 @@ class TransactionObserverTests: GRDBTestCase {
|
||||
artist.name = "Vincent Fournier"
|
||||
|
||||
//
|
||||
self.observer.resetCounts()
|
||||
try artist.save(db)
|
||||
XCTAssertEqual(self.observer.didChangeCount, 1)
|
||||
XCTAssertEqual(self.observer.willCommitCount, 1)
|
||||
XCTAssertEqual(self.observer.didCommitCount, 1)
|
||||
XCTAssertEqual(self.observer.didRollbackCount, 0)
|
||||
XCTAssertEqual(self.observer.lastCommittedEvents.count, 1)
|
||||
let event = self.observer.lastCommittedEvents.filter {
|
||||
self.match(event: $0, kind: .Update, tableName: "artists", rowId: artist.id!)
|
||||
@@ -165,7 +198,12 @@ class TransactionObserverTests: GRDBTestCase {
|
||||
try artist.save(db)
|
||||
|
||||
//
|
||||
self.observer.resetCounts()
|
||||
try artist.delete(db)
|
||||
XCTAssertEqual(self.observer.didChangeCount, 1)
|
||||
XCTAssertEqual(self.observer.willCommitCount, 1)
|
||||
XCTAssertEqual(self.observer.didCommitCount, 1)
|
||||
XCTAssertEqual(self.observer.didRollbackCount, 0)
|
||||
XCTAssertEqual(self.observer.lastCommittedEvents.count, 1)
|
||||
let event = self.observer.lastCommittedEvents.filter {
|
||||
self.match(event: $0, kind: .Delete, tableName: "artists", rowId: artist.id!)
|
||||
@@ -186,7 +224,12 @@ class TransactionObserverTests: GRDBTestCase {
|
||||
try artwork2.save(db)
|
||||
|
||||
//
|
||||
self.observer.resetCounts()
|
||||
try artist.delete(db)
|
||||
XCTAssertEqual(self.observer.didChangeCount, 3)
|
||||
XCTAssertEqual(self.observer.willCommitCount, 1)
|
||||
XCTAssertEqual(self.observer.didCommitCount, 1)
|
||||
XCTAssertEqual(self.observer.didRollbackCount, 0)
|
||||
XCTAssertEqual(self.observer.lastCommittedEvents.count, 3)
|
||||
let artistEvent = self.observer.lastCommittedEvents.filter {
|
||||
self.match(event: $0, kind: .Delete, tableName: "artists", rowId: artist.id!)
|
||||
@@ -211,19 +254,40 @@ class TransactionObserverTests: GRDBTestCase {
|
||||
let artwork2 = Artwork(title: "Ema (Nude on a Staircase)")
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
self.observer.resetCounts()
|
||||
try artist.save(db)
|
||||
XCTAssertEqual(self.observer.didChangeCount, 1)
|
||||
XCTAssertEqual(self.observer.willCommitCount, 0)
|
||||
XCTAssertEqual(self.observer.didCommitCount, 0)
|
||||
XCTAssertEqual(self.observer.didRollbackCount, 0)
|
||||
XCTAssertEqual(self.observer.lastCommittedEvents.count, 0)
|
||||
|
||||
artwork1.artistId = artist.id
|
||||
artwork2.artistId = artist.id
|
||||
XCTAssertEqual(self.observer.lastCommittedEvents.count, 0)
|
||||
|
||||
self.observer.resetCounts()
|
||||
try artwork1.save(db)
|
||||
XCTAssertEqual(self.observer.didChangeCount, 1)
|
||||
XCTAssertEqual(self.observer.willCommitCount, 0)
|
||||
XCTAssertEqual(self.observer.didCommitCount, 0)
|
||||
XCTAssertEqual(self.observer.didRollbackCount, 0)
|
||||
XCTAssertEqual(self.observer.lastCommittedEvents.count, 0)
|
||||
|
||||
self.observer.resetCounts()
|
||||
try artwork2.save(db)
|
||||
XCTAssertEqual(self.observer.didChangeCount, 1)
|
||||
XCTAssertEqual(self.observer.willCommitCount, 0)
|
||||
XCTAssertEqual(self.observer.didCommitCount, 0)
|
||||
XCTAssertEqual(self.observer.didRollbackCount, 0)
|
||||
XCTAssertEqual(self.observer.lastCommittedEvents.count, 0)
|
||||
|
||||
self.observer.resetCounts()
|
||||
return .Commit
|
||||
}
|
||||
XCTAssertEqual(self.observer.didChangeCount, 0)
|
||||
XCTAssertEqual(self.observer.willCommitCount, 1)
|
||||
XCTAssertEqual(self.observer.didCommitCount, 1)
|
||||
XCTAssertEqual(self.observer.didRollbackCount, 0)
|
||||
XCTAssertEqual(observer.lastTransactionCompletion, TransactionCompletion.Commit)
|
||||
XCTAssertEqual(observer.lastCommittedEvents.count, 3)
|
||||
let artistEvent = observer.lastCommittedEvents.filter {
|
||||
@@ -259,10 +323,69 @@ class TransactionObserverTests: GRDBTestCase {
|
||||
try artwork2.save(db)
|
||||
XCTAssertEqual(self.observer.lastCommittedEvents.count, 0)
|
||||
|
||||
self.observer.resetCounts()
|
||||
return .Rollback
|
||||
}
|
||||
XCTAssertEqual(self.observer.didChangeCount, 0)
|
||||
XCTAssertEqual(self.observer.willCommitCount, 0)
|
||||
XCTAssertEqual(self.observer.didCommitCount, 0)
|
||||
XCTAssertEqual(self.observer.didRollbackCount, 1)
|
||||
XCTAssertEqual(observer.lastTransactionCompletion, TransactionCompletion.Rollback)
|
||||
XCTAssertEqual(observer.lastCommittedEvents.count, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func testWillCommitError() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let artist = Artist(name: "Gerhard Richter")
|
||||
try artist.save(db)
|
||||
}
|
||||
|
||||
self.observer.commitError = NSError(domain: "foo", code: 0, userInfo: nil)
|
||||
do {
|
||||
try dbQueue.inDatabase { db in
|
||||
let artist = Artist(name: "Gerhard Richter")
|
||||
self.observer.resetCounts()
|
||||
try artist.save(db)
|
||||
}
|
||||
XCTFail("Error expected")
|
||||
} catch let error as NSError {
|
||||
XCTAssertEqual(self.observer.didChangeCount, 1)
|
||||
XCTAssertEqual(self.observer.willCommitCount, 1)
|
||||
XCTAssertEqual(self.observer.didCommitCount, 0)
|
||||
XCTAssertEqual(self.observer.didRollbackCount, 1)
|
||||
XCTAssertEqual(error.domain, "foo")
|
||||
XCTAssertEqual(error.code, 0)
|
||||
}
|
||||
|
||||
do {
|
||||
try dbQueue.inTransaction { db in
|
||||
let artist = Artist(name: "Gerhard Richter")
|
||||
try artist.save(db)
|
||||
self.observer.resetCounts()
|
||||
return .Commit
|
||||
}
|
||||
XCTFail("Error expected")
|
||||
} catch let error as NSError {
|
||||
XCTAssertEqual(self.observer.didChangeCount, 0)
|
||||
XCTAssertEqual(self.observer.willCommitCount, 1)
|
||||
XCTAssertEqual(self.observer.didCommitCount, 0)
|
||||
XCTAssertEqual(self.observer.didRollbackCount, 1)
|
||||
XCTAssertEqual(error.domain, "foo")
|
||||
XCTAssertEqual(error.code, 0)
|
||||
}
|
||||
|
||||
self.observer.commitError = nil
|
||||
try dbQueue.inDatabase { db in
|
||||
let artist = Artist(name: "Gerhard Richter")
|
||||
try artist.save(db)
|
||||
}
|
||||
|
||||
let artistCount = dbQueue.inDatabase { db in
|
||||
Int.fetchOne(db, "SELECT COUNT(*) FROM artists")
|
||||
}
|
||||
XCTAssertEqual(artistCount, 2)
|
||||
}
|
||||
}
|
||||
}
|
||||
+30
-10
@@ -203,7 +203,7 @@ public final class Database {
|
||||
*/
|
||||
let transactionObserver: TransactionObserverType?
|
||||
|
||||
func updateStatementDidFail() {
|
||||
func updateStatementDidFail() throws {
|
||||
// TODO: trigger this callback on a COMMIT or a ROLLBACK statement, and
|
||||
// see if the callbacks of sqlite3_commit_hook and sqlite3_rollback_hook
|
||||
// have been called before.
|
||||
@@ -213,6 +213,21 @@ public final class Database {
|
||||
//
|
||||
// If not, we don't even now how to tell the delegate that something
|
||||
// went wrong with previously notified databaseEvents :-(
|
||||
//
|
||||
// Anyway.
|
||||
//
|
||||
// Below, we handle the case of implicit transactions.
|
||||
// The transactionObserver has thrown an error, and the implicit
|
||||
// transactions around a statement has failed. Throw the transaction
|
||||
// error instead of the statement error:
|
||||
if let pendingTransactionError = self.pendingTransactionError {
|
||||
self.pendingTransactionError = nil
|
||||
self.pendingTransactionCompletion = nil
|
||||
if let transactionObserver = transactionObserver {
|
||||
transactionObserver.databaseDidRollback(self)
|
||||
}
|
||||
throw pendingTransactionError
|
||||
}
|
||||
}
|
||||
|
||||
func updateStatementDidExecute() {
|
||||
@@ -226,9 +241,9 @@ public final class Database {
|
||||
if let transactionObserver = transactionObserver {
|
||||
switch pendingTransactionCompletion {
|
||||
case .Commit:
|
||||
transactionObserver.databaseDidCommit()
|
||||
transactionObserver.databaseDidCommit(self)
|
||||
case .Rollback:
|
||||
transactionObserver.databaseDidRollback()
|
||||
transactionObserver.databaseDidRollback(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -237,6 +252,9 @@ public final class Database {
|
||||
// completion. See setupTransactionHooks().
|
||||
private var pendingTransactionCompletion: TransactionCompletion? = nil
|
||||
|
||||
// If not nil, the last commit hook has thrown an error.
|
||||
private var pendingTransactionError: ErrorType?
|
||||
|
||||
private func setupTransactionHooks() {
|
||||
// TODO: now that transactionObserver is a `let` property, don't
|
||||
// install any hook unless the observer is not null.
|
||||
@@ -265,11 +283,13 @@ public final class Database {
|
||||
return 0 // commit
|
||||
}
|
||||
|
||||
if transactionObserver.databaseShouldCommit() {
|
||||
do {
|
||||
try transactionObserver.databaseWillCommit()
|
||||
return 0 // commit
|
||||
} catch {
|
||||
db.pendingTransactionError = error
|
||||
return 1 // rollback
|
||||
}
|
||||
|
||||
return 1 // rollback
|
||||
}, dbPointer)
|
||||
|
||||
sqlite3_rollback_hook(sqliteConnection, { dbPointer in
|
||||
@@ -568,15 +588,15 @@ public protocol TransactionObserverType : class {
|
||||
|
||||
/**
|
||||
When a transaction is about to be committed, the transaction delegate has an
|
||||
opportunity to rollback pending changes.
|
||||
opportunity to rollback pending changes by throwing an error.
|
||||
|
||||
This method is called on the database queue.
|
||||
|
||||
**WARNING**: this method must not change the database.
|
||||
|
||||
- returns: Whether the transaction should be committed.
|
||||
- throws: An eventual error that rollbacks pending changes.
|
||||
*/
|
||||
func databaseShouldCommit() -> Bool
|
||||
func databaseWillCommit() throws
|
||||
|
||||
/**
|
||||
Database changes have been committed.
|
||||
@@ -603,7 +623,7 @@ public extension TransactionObserverType {
|
||||
func databaseDidChangeWithEvent(event: DatabaseEvent) { }
|
||||
|
||||
/// Default implementation return true.
|
||||
func databaseShouldCommit() -> Bool { return true }
|
||||
func databaseWillCommit() throws { }
|
||||
|
||||
/// Default implementation does nothing.
|
||||
func databaseDidCommit(db: Database) { }
|
||||
|
||||
@@ -32,7 +32,7 @@ public final class UpdateStatement : Statement {
|
||||
|
||||
let code = sqlite3_step(sqliteStatement)
|
||||
guard code == SQLITE_DONE else {
|
||||
database.updateStatementDidFail()
|
||||
try database.updateStatementDidFail()
|
||||
throw DatabaseError(code: code, message: database.lastErrorMessage, sql: sql, arguments: self.arguments)
|
||||
}
|
||||
|
||||
|
||||
@@ -961,8 +961,8 @@ public protocol TransactionObserverType : class {
|
||||
// Notifies a database change (insert, update, or delete):
|
||||
func databaseDidChangeWithEvent(event: DatabaseEvent)
|
||||
|
||||
// An opportunity to rollback pending changes.
|
||||
func databaseShouldCommit() -> Bool
|
||||
// An opportunity to rollback pending changes by throwing an error.
|
||||
func databaseWillCommit() throws
|
||||
|
||||
// Database changes have been committed.
|
||||
func databaseDidCommit(db: Database)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
- [ ] Support for NSURL
|
||||
- [ ] TransactionObserverType
|
||||
- [X] Since databaseDidCommit() and databaseDidRollback() can touch the database, we need to provide the database instance.
|
||||
- [ ] databaseShouldCommit() should be renamed databaseWillCommit(), and throws. The error should obviously pop up.
|
||||
- [X] databaseShouldCommit() should be renamed databaseWillCommit(), and throws. The error should obviously pop up.
|
||||
- [ ] See if TransactionObserver makes it possible to provide external data storage.
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user