From 1bbbc2734e4d9f5dfc9b0dbc12ed05af07166502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 28 Sep 2015 13:07:04 +0200 Subject: [PATCH] Give the database to TransactionObserverType.databaseDidCommit and databaseDidRollback --- .../TransactionObserverTests.swift | 2 +- GRDB/Core/Database.swift | 12 ++++++++---- README.md | 12 ++++++------ TODO.md | 4 ++-- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/GRDB Tests/Public/Core/TransactionObserver/TransactionObserverTests.swift b/GRDB Tests/Public/Core/TransactionObserver/TransactionObserverTests.swift index 46e71b4ff..8d94512e2 100644 --- a/GRDB Tests/Public/Core/TransactionObserver/TransactionObserverTests.swift +++ b/GRDB Tests/Public/Core/TransactionObserver/TransactionObserverTests.swift @@ -16,7 +16,7 @@ class TransactionObserver : TransactionObserverType { events = [] } - func databaseDidRollback() { + func databaseDidRollback(db: Database) { lastTransactionCompletion = .Rollback lastCommittedEvents = [] events = [] diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index cf29383d2..814ee3836 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -582,15 +582,19 @@ public protocol TransactionObserverType : class { Database changes have been committed. This method is called on the database queue. It can change the database. + + - parameter db: A Database. */ - func databaseDidCommit() + func databaseDidCommit(db: Database) /** Database changes have been rollbacked. This method is called on the database queue. It can change the database. + + - parameter db: A Database. */ - func databaseDidRollback() + func databaseDidRollback(db: Database) } /// Default implementations for TransactionObserverType methods. @@ -602,10 +606,10 @@ public extension TransactionObserverType { func databaseShouldCommit() -> Bool { return true } /// Default implementation does nothing. - func databaseDidCommit() { } + func databaseDidCommit(db: Database) { } /// Default implementation does nothing. - func databaseDidRollback() { } + func databaseDidRollback(db: Database) { } } /** diff --git a/README.md b/README.md index 575e9f83f..88a01dba5 100644 --- a/README.md +++ b/README.md @@ -965,16 +965,16 @@ public protocol TransactionObserverType : class { func databaseShouldCommit() -> Bool // Database changes have been committed. - func databaseDidCommit() + func databaseDidCommit(db: Database) // Database changes have been rollbacked. - func databaseDidRollback() + func databaseDidRollback(db: Database) } ``` Those four callbacks are all optional, and all invoked on the database queue. -Changes notified to `database(_:didChangeWithEvent:)` are triggered by `INSERT`, `UPDATE` and `DELETE` statements, and also by `ON DELETE` and `ON UPDATE` actions associated to [foreign keys](https://www.sqlite.org/foreignkeys.html#fk_actions). +Changes notified to `databaseDidChangeWithEvent(_)` are triggered by `INSERT`, `UPDATE` and `DELETE` statements, and also by `ON DELETE` and `ON UPDATE` actions associated to [foreign keys](https://www.sqlite.org/foreignkeys.html#fk_actions). Those changes are not actually applied until `databaseDidCommit(_)` is called. On the other side, `databaseDidRollback(_)` confirms their invalidation: @@ -996,7 +996,7 @@ try dbQueue.inDatabase do { db in ``` -**Warning**: `database(_:didChangeWithEvent:)` and `databaseShouldCommit(_)` *must not* read or write to the database. This limitation does not apply to `databaseDidCommit(_)` and `databaseDidRollback(_)`. +**Warning**: `databaseDidChangeWithEvent(_)` and `databaseShouldCommit()` *must not* read or write to the database. This limitation does not apply to `databaseDidCommit(_)` and `databaseDidRollback(_)`. **Sample code** @@ -1012,7 +1012,7 @@ class TableChangeObserver : TransactionObserverType { changedTableNames.insert(event.tableName) } - func databaseDidCommit() { + func databaseDidCommit(db: Database) { // Extract the names of changed tables, and reset until // next database event: let changedTableNames = self.changedTableNames @@ -1027,7 +1027,7 @@ class TableChangeObserver : TransactionObserverType { } } - func databaseDidRollback() { + func databaseDidRollback(db: Database) { // Forget the names of changed tables: self.changedTableNames = [] } diff --git a/TODO.md b/TODO.md index da11b0f8d..250500fc2 100644 --- a/TODO.md +++ b/TODO.md @@ -1,8 +1,8 @@ - [ ] #2: this commit may be how stephencelis fixed it: https://github.com/stephencelis/SQLite.swift/commit/8f64e357c3a6668c5f011c91ba33be3e8d4b88d0 - [ ] Use @warn_unused_result where applicable - [ ] Support for NSURL -- [ ] TransactionObserver - - [ ] Since databaseDidCommit() and databaseDidRollback() can touch the database, we need to provide the database instance. +- [ ] 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. - [ ] See if TransactionObserver makes it possible to provide external data storage.