mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Don’t setup database observation in read-only connections.
Observation of read-only connections has turned useless since read-only transactions are no longer reported to TransactionObserver, in #1213
This commit is contained in:
1 parent
0879311646
commit
fb6ea57d74
4 files changed
+24
-13
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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<String> {
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user