Class
SQLiteConnection
final class SQLiteConnection
Overview
SQLiteConnection wraps a single sqlite3* handle and provides fully asynchronous, Swift-concurrency–friendly access to SQLite, as well as a multiplexed hook API that lets you register many Swift observers even though SQLite exposes only one slot per hook in C.
Storage
Choose how the underlying database is created when opening a connection via SQLiteConnection.Storage:
SQLiteConnection.Storage.memory– transient; lives only as long as the connection. Not shareable across processes (and, withSQLITE_OMIT_SHARED_CACHE, not across multiple connections in-process). Great for unit tests and scratch work.SQLiteConnection.Storage.file(path:)– persisted on disk; can be opened by multiple connections/processes subject to SQLite locking rules. Prefer absolute paths.
Observable Hooks
Each connection can fan out these SQLite hooks to multiple callbacks:
Update – row-level
INSERT/UPDATE/DELETEevents. SeeaddUpdateObserver(lifetime:_:).Commit – transaction about to commit; use
setCommitValidator(lifetime:_:)to veto commits oraddCommitObserver(lifetime:_:)for side-effect-only observation.Rollback – transaction was rolled back. See
addRollbackObserver(lifetime:_:).Authorizer – per-statement access control; use
setAuthorizerValidator(lifetime:_:)for access control oraddAuthorizerObserver(lifetime:_:)for side-effect-only observation.
Observer Registration Styles
Style |
API |
Lifetime |
Cleanup |
Notes |
|---|---|---|---|---|
Token-Based (Scoped) |
|
Until |
Auto on token dealloc |
Use for temporary observation. |
Token-Based (Pinned) |
|
Until |
|
Use for long-lived observation. |
Scoped |
|
Active only for closure duration |
Auto |
Handy for tests / temporary instrumentation. |
Threading
Registration is thread-safe. Callbacks run on SQLite’s internal thread, not your event loop or actor. Hop if needed:
let token = try await db.addUpdateObserver(lifetime: .pinned) { [weak self] event in
Task { await self?.myActor.handle(event) } // hop to actor
}
Cleanup
Dropping a
SQLiteHookTokenwithSQLObserverLifetime/scopedlifetime auto-cancels on deallocation.Dropping a
SQLiteHookTokenwithSQLObserverLifetime/pinnedlifetime triggers a debug assertion but leaves the observer active.Call
cancel()to stop an observer early (works for both lifetimes).All observers are torn down automatically when the connection closes; later cancels are safe.
See
SQLiteObserverLifetimefor detailed lifetime behavior.
Topics
Instance Properties
eventLoopThe event loop on which operations on the connection execute.isClosedfalseif the connection is valid,trueif not.loggerThe logger used by the connection.
Instance Methods
addAuthorizerObserver(lifetime:_:)Register an observer for the SQLite authorizer hook.addCommitObserver(lifetime:_:)Register a pure observer for SQLite commit events (cannot veto commits).addRollbackObserver(lifetime:_:)Register an observer for the SQLite rollback hook.addUpdateObserver(lifetime:_:)Register an observer for the SQLite update hook (row-level DML).close()Close the connection and invalidate its handle.install(customFunction:)Install the providedSQLiteCustomFunctionon the connection.lastAutoincrementID()Returns the last value generated by auto-increment functionality (either the version implied byINTEGER PRIMARY KEYor that of the explicitAUTO_INCREMENTmodifier) on this database.query(_:_:_:)Concurrency-aware variant ofquery(_:_:_:)-etrj.query(_:_:logger:_:)Execute a query on the connection, calling the provided closure for each result row (if any).setAuthorizerValidator(lifetime:_:)Set the validator for the SQLite authorizer hook.setCommitValidator(lifetime:_:)Register a validator for SQLite commit events (can veto commits).uninstall(customFunction:)Uninstall the providedSQLiteCustomFunctionfrom the connection.withAuthorizerObserver(_:body:)Execute a block with a temporary authorizer observer.withCommitObserver(_:body:)Execute a block with a temporary commit observer.withConnection(_:)Call the provided closure with a concreteSQLiteConnectioninstance.withRollbackObserver(_:body:)Execute a block with a temporary rollback observer.withUpdateObserver(_:body:)Execute a block with a temporary update observer.
Type Aliases
SQLiteConnection.SQLiteAuthorizerObserverThe type signature for authorizer observer callbacks.SQLiteConnection.SQLiteAuthorizerValidatorThe type signature for authorizer validator callbacks.SQLiteConnection.SQLiteCommitObserverThe type signature for commit observer callbacks (pure observation, cannot veto).SQLiteConnection.SQLiteCommitValidatorThe type signature for commit validator callbacks (can veto commits).SQLiteConnection.SQLiteRollbackHookCallbackThe type signature for rollback hook callbacks.SQLiteConnection.SQLiteUpdateHookCallbackThe type signature for update hook callbacks.
Type Methods
libraryVersion()Return the version of the embedded libsqlite3 as a 32-bit integer value.libraryVersionString()Return the version of the embedded libsqlite3 as a string.open(storage:logger:)Open a new connection to an SQLite database.open(storage:threadPool:logger:on:)Open a new connection to an SQLite database.
Enumerations
SQLiteConnection.StorageThe possible storage types for an SQLite database.
Relationships
Conforms To
SQLiteDatabaseSwift.SendableSwift.SendableMetatype