TransactionObserver: provide default implementation for rarely used and stabilized databaseWillChange() and databaseWillCommit() methods

This commit is contained in:
Gwendal Roué committed 2018-01-14 22:58:12 +01:00
1 parent 5d463d6bff
commit 4501d7dcde
8 files changed
+10 -37

No files matched your search

+10 -4
View File
@@ -51,11 +51,7 @@ extension Database {
// Ignore individual changes and transaction rollbacks
func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool { return false }
#if SQLITE_ENABLE_PREUPDATE_HOOK
func databaseWillChange(with event: DatabasePreUpdateEvent) { }
#endif
func databaseDidChange(with event: DatabaseEvent) { }
func databaseWillCommit() throws { }
func databaseDidRollback(_ db: Database) { }
// On commit, run closure
@@ -665,6 +661,16 @@ public protocol TransactionObserver : class {
}
extension TransactionObserver {
/// Default implementation does nothing
public func databaseWillCommit() throws {
}
#if SQLITE_ENABLE_PREUPDATE_HOOK
/// Default implementation does nothing
public func databaseWillChange(with event: DatabasePreUpdateEvent) {
}
#endif
/// After this method has been called, the `databaseDidChange(with:)`
/// method won't be called until the next transaction.
///
@@ -559,9 +559,6 @@ private final class FetchedRecordsObserver<Record: RowConvertible> : Transaction
ignoreDatabaseChangesUntilNextTransaction()
}
/// Part of the TransactionObserverType protocol
func databaseWillCommit() throws { }
/// Part of the TransactionObserverType protocol
func databaseDidRollback(_ db: Database) {
needsComputeChanges = false
@@ -192,12 +192,8 @@ private class UserTypedRequest2<T> : TypedRequest {
private class UserTransactionObserver : TransactionObserver {
func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool { return false }
func databaseDidChange(with event: DatabaseEvent) { }
func databaseWillCommit() throws { }
func databaseDidCommit(_ db: Database) { }
func databaseDidRollback(_ db: Database) { }
#if SQLITE_ENABLE_PREUPDATE_HOOK
func databaseWillChange(with event: DatabasePreUpdateEvent) { }
#endif
}
// MARK: - VirtualTableModule
@@ -42,9 +42,6 @@ private class Observer : TransactionObserver {
allRecordedEvents.append(event.copy())
}
func databaseWillCommit() throws {
}
func databaseDidCommit(_ db: Database) {
}
@@ -174,9 +174,6 @@ class MutablePersistablePersistenceConflictPolicyTests: GRDBTestCase {
transactionEvents.append(event.copy())
}
func databaseWillCommit() throws {
}
func databaseDidCommit(_ db: Database) {
events = transactionEvents
transactionEvents = []
@@ -186,11 +183,6 @@ class MutablePersistablePersistenceConflictPolicyTests: GRDBTestCase {
events = []
transactionEvents = []
}
#if SQLITE_ENABLE_PREUPDATE_HOOK
func databaseWillChange(with event: DatabasePreUpdateEvent) {
}
#endif
}
let dbQueue = try makeDatabaseQueue()
@@ -148,8 +148,6 @@ class SelectStatementTests : GRDBTestCase {
didChange = true
}
func databaseWillCommit() throws { }
func databaseDidCommit(_ db: Database) {
triggered = didChange
didChange = false
@@ -158,10 +156,6 @@ class SelectStatementTests : GRDBTestCase {
func databaseDidRollback(_ db: Database) {
didChange = false
}
#if SQLITE_ENABLE_PREUPDATE_HOOK
func databaseWillChange(with event: DatabasePreUpdateEvent) { }
#endif
}
try db.create(table: "table1") { t in
@@ -26,9 +26,6 @@ private class Observer : TransactionObserver {
events.append(event.copy())
}
func databaseWillCommit() throws {
}
func databaseDidCommit(_ db: Database) {
lastCommittedEvents = events
events = []
@@ -43,18 +43,12 @@ class TruncateOptimizationTests: GRDBTestCase {
return true
}
#if SQLITE_ENABLE_PREUPDATE_HOOK
func databaseWillChange(with event: DatabasePreUpdateEvent) { }
#endif
func databaseDidChange(with event: DatabaseEvent) {
if case .delete = event.kind {
deletionEvents[event.tableName, default: 0] += 1
}
}
func databaseWillCommit() throws { }
func databaseDidCommit(_ db: Database) {
if !deletionEvents.isEmpty {
notify(deletionEvents)