From 4501d7dcde2ed6b3145ee3420780bb42073d81e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 14 Jan 2018 22:58:12 +0100 Subject: [PATCH] TransactionObserver: provide default implementation for rarely used and stabilized databaseWillChange() and databaseWillCommit() methods --- GRDB/Core/TransactionObserver.swift | 14 ++++++++++---- GRDB/Record/FetchedRecordsController.swift | 3 --- Tests/GRDBTests/CompilationProtocolTests.swift | 4 ---- Tests/GRDBTests/DatabaseSavepointTests.swift | 3 --- ...PersistablePersistenceConflictPolicyTests.swift | 8 -------- Tests/GRDBTests/SelectStatementTests.swift | 6 ------ .../TransactionObserverSavepointsTests.swift | 3 --- Tests/GRDBTests/TruncateOptimizationTests.swift | 6 ------ 8 files changed, 10 insertions(+), 37 deletions(-) diff --git a/GRDB/Core/TransactionObserver.swift b/GRDB/Core/TransactionObserver.swift index ff6706f16..54ae26032 100644 --- a/GRDB/Core/TransactionObserver.swift +++ b/GRDB/Core/TransactionObserver.swift @@ -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. /// diff --git a/GRDB/Record/FetchedRecordsController.swift b/GRDB/Record/FetchedRecordsController.swift index 88eaba244..c9a2a95ba 100644 --- a/GRDB/Record/FetchedRecordsController.swift +++ b/GRDB/Record/FetchedRecordsController.swift @@ -559,9 +559,6 @@ private final class FetchedRecordsObserver : Transaction ignoreDatabaseChangesUntilNextTransaction() } - /// Part of the TransactionObserverType protocol - func databaseWillCommit() throws { } - /// Part of the TransactionObserverType protocol func databaseDidRollback(_ db: Database) { needsComputeChanges = false diff --git a/Tests/GRDBTests/CompilationProtocolTests.swift b/Tests/GRDBTests/CompilationProtocolTests.swift index 6fae5642d..882d246ea 100644 --- a/Tests/GRDBTests/CompilationProtocolTests.swift +++ b/Tests/GRDBTests/CompilationProtocolTests.swift @@ -192,12 +192,8 @@ private class UserTypedRequest2 : 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 diff --git a/Tests/GRDBTests/DatabaseSavepointTests.swift b/Tests/GRDBTests/DatabaseSavepointTests.swift index 91b923de7..b86fba0cb 100644 --- a/Tests/GRDBTests/DatabaseSavepointTests.swift +++ b/Tests/GRDBTests/DatabaseSavepointTests.swift @@ -42,9 +42,6 @@ private class Observer : TransactionObserver { allRecordedEvents.append(event.copy()) } - func databaseWillCommit() throws { - } - func databaseDidCommit(_ db: Database) { } diff --git a/Tests/GRDBTests/MutablePersistablePersistenceConflictPolicyTests.swift b/Tests/GRDBTests/MutablePersistablePersistenceConflictPolicyTests.swift index 36f32c613..a076d0f49 100644 --- a/Tests/GRDBTests/MutablePersistablePersistenceConflictPolicyTests.swift +++ b/Tests/GRDBTests/MutablePersistablePersistenceConflictPolicyTests.swift @@ -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() diff --git a/Tests/GRDBTests/SelectStatementTests.swift b/Tests/GRDBTests/SelectStatementTests.swift index ee959b3b7..3e4a9e13f 100644 --- a/Tests/GRDBTests/SelectStatementTests.swift +++ b/Tests/GRDBTests/SelectStatementTests.swift @@ -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 diff --git a/Tests/GRDBTests/TransactionObserverSavepointsTests.swift b/Tests/GRDBTests/TransactionObserverSavepointsTests.swift index f399f37f0..f57a847b3 100644 --- a/Tests/GRDBTests/TransactionObserverSavepointsTests.swift +++ b/Tests/GRDBTests/TransactionObserverSavepointsTests.swift @@ -26,9 +26,6 @@ private class Observer : TransactionObserver { events.append(event.copy()) } - func databaseWillCommit() throws { - } - func databaseDidCommit(_ db: Database) { lastCommittedEvents = events events = [] diff --git a/Tests/GRDBTests/TruncateOptimizationTests.swift b/Tests/GRDBTests/TruncateOptimizationTests.swift index 905ed03e2..bf089252a 100644 --- a/Tests/GRDBTests/TruncateOptimizationTests.swift +++ b/Tests/GRDBTests/TruncateOptimizationTests.swift @@ -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)