From fb6ea57d74c2be6457a03c1961d84f43591e8d29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Rou=C3=A9?= Date: Tue, 16 Aug 2022 12:54:40 +0200 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20setup=20database=20observation?= =?UTF-8?q?=20in=20read-only=20connections.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Observation of read-only connections has turned useless since read-only transactions are no longer reported to TransactionObserver, in #1213 --- GRDB/Core/Database+Statements.swift | 6 +++--- GRDB/Core/Database.swift | 12 ++++++++---- GRDB/Core/StatementAuthorizer.swift | 15 +++++++++------ GRDB/Core/TransactionObserver.swift | 4 ++++ 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/GRDB/Core/Database+Statements.swift b/GRDB/Core/Database+Statements.swift index 34abd03b0..df71e9189 100644 --- a/GRDB/Core/Database+Statements.swift +++ b/GRDB/Core/Database+Statements.swift @@ -389,7 +389,7 @@ extension Database { } // Database observation: prepare transaction observers. - observationBroker.statementWillExecute(statement) + observationBroker?.statementWillExecute(statement) } /// May throw a cancelled commit error, if a transaction observer cancels @@ -400,7 +400,7 @@ extension Database { clearSchemaCache() } - try observationBroker.statementDidExecute(statement) + try observationBroker?.statementDidExecute(statement) } /// Always throws an error @@ -416,7 +416,7 @@ extension Database { /// Exposes the user-provided cancelled commit error, if a transaction /// observer has cancelled a transaction. - try observationBroker.statementDidFail(statement) + try observationBroker?.statementDidFail(statement) throw DatabaseError( resultCode: resultCode, diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index a7a171f93..455be5976 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -154,7 +154,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib // Database observation lazy var authorizer = StatementAuthorizer(self) - lazy var observationBroker = DatabaseObservationBroker(self) + private(set) var observationBroker: DatabaseObservationBroker? /// The list of compile options used when building SQLite static func sqliteCompileOptions() throws -> Set { @@ -196,6 +196,12 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib self.sqliteConnection = try Database.openConnection(path: path, flags: configuration.SQLiteOpenFlags) self.description = description self.configuration = configuration + + // We do not report read-only transactions to transaction observers, so + // don't bother installing the observation broker for read-only connections. + if !configuration.readonly { + observationBroker = DatabaseObservationBroker(self) + } } deinit { @@ -237,9 +243,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib setupDefaultFunctions() setupDefaultCollations() setupAuthorizer() - if !configuration.readonly { - observationBroker.installCommitAndRollbackHooks() - } + observationBroker?.installCommitAndRollbackHooks() try activateExtendedCodes() #if SQLITE_HAS_CODEC diff --git a/GRDB/Core/StatementAuthorizer.swift b/GRDB/Core/StatementAuthorizer.swift index 267d4a276..3eec7d82e 100644 --- a/GRDB/Core/StatementAuthorizer.swift +++ b/GRDB/Core/StatementAuthorizer.swift @@ -106,18 +106,21 @@ final class StatementAuthorizer { guard let cString1 = cString1 else { return SQLITE_OK } // Deletions from sqlite_master and sqlite_temp_master are not like - // other deletions: the update hook does not notify them, and they - // are prevented when the truncate optimization is disabled. - // Let's authorize such deletions by returning SQLITE_OK: + // other deletions: `sqlite3_update_hook` does not notify them, and + // they are prevented when the truncate optimization is disabled. + // Let's always authorize such deletions by returning SQLITE_OK: guard strcmp(cString1, "sqlite_master") != 0 else { return SQLITE_OK } guard strcmp(cString1, "sqlite_temp_master") != 0 else { return SQLITE_OK } let tableName = String(cString: cString1) databaseEventKinds.append(.delete(tableName: tableName)) - // Now we prevent the truncate optimization so that transaction - // observers are notified of individual row deletions. - if database.observationBroker.observesDeletions(on: tableName) { + if let observationBroker = database.observationBroker, + observationBroker.observesDeletions(on: tableName) + { + // Prevent the truncate optimization so that + // `sqlite3_update_hook` notifies individual row deletions to + // transaction observers. return SQLITE_IGNORE } else { return SQLITE_OK diff --git a/GRDB/Core/TransactionObserver.swift b/GRDB/Core/TransactionObserver.swift index 6506af4b9..f6fd6f044 100644 --- a/GRDB/Core/TransactionObserver.swift +++ b/GRDB/Core/TransactionObserver.swift @@ -5,6 +5,8 @@ extension Database { /// Add a transaction observer, so that it gets notified of /// database changes. /// + /// This method has no effect on read-only database connections. + /// /// - parameter transactionObserver: A transaction observer. /// - parameter extent: The duration of the observation. The default is /// the observer lifetime (observation lasts until observer @@ -14,6 +16,7 @@ extension Database { extent: TransactionObservationExtent = .observerLifetime) { SchedulingWatchdog.preconditionValidQueue(self) + guard let observationBroker else { return } // Drop cached statements that delete, because the addition of an // observer may change the need for truncate optimization prevention. @@ -26,6 +29,7 @@ extension Database { /// Remove a transaction observer. public func remove(transactionObserver: some TransactionObserver) { SchedulingWatchdog.preconditionValidQueue(self) + guard let observationBroker else { return } // Drop cached statements that delete, because the removal of an // observer may change the need for truncate optimization prevention.