pub struct Db { /* private fields */ }Expand description
Handle to a database: a pool, or an open transaction.
Implementations§
Source§impl Db
impl Db
Sourcepub async fn transaction<F, Fut, T, E>(&self, f: F) -> Result<T, E>
pub async fn transaction<F, Fut, T, E>(&self, f: F) -> Result<T, E>
Run f in a transaction, or a savepoint inside a transaction.
Cancelling a closure, child scope or statement makes the transaction unusable. An outer success then fails and rolls back. Concurrent statements and sibling scopes are rejected; recursive nesting through the child is supported. Use the child handle while its scope is active. Escaped handles cannot execute after their scope finishes. Cancellation during COMMIT can leave the server outcome unknown; callers must not retry blindly.
Args: f: Closure receiving the scoped transaction handle.
Returns: The closure result after commit or confirmed savepoint release.
§Errors
Closure, backend, capability, busy-scope or aborted-transaction errors.
Sourcepub async fn transaction_with<F, Fut, T, E>(
&self,
isolation: IsolationLevel,
f: F,
) -> Result<T, E>
pub async fn transaction_with<F, Fut, T, E>( &self, isolation: IsolationLevel, f: F, ) -> Result<T, E>
Run a scoped transaction at an explicit isolation level.
Args: isolation: A level supported by the backend; disallowed in nesting. f: Closure receiving the scoped transaction handle.
Returns: The closure result after successful commit.
§Errors
As transaction, plus unsupported isolation.
Sourcepub async fn schema_change<F, Fut, T, E>(
&self,
transactional: bool,
f: F,
) -> Result<T, E>
pub async fn schema_change<F, Fut, T, E>( &self, transactional: bool, f: F, ) -> Result<T, E>
Run f on a dedicated schema connection, optionally transactional.
SQLite disables foreign keys before BEGIN and restores them afterwards. A cancelled nested scope makes the connection unusable; it cannot be committed through an escaped handle.
Args: transactional: Whether to wrap schema work in a transaction. f: Closure receiving the dedicated connection handle.
Returns: The closure result after the backend finishes schema work.
§Errors
Closure/backend errors; SQLite rejects an already-scoped connection.
Sourcepub fn on_commit(&self, hook: impl FnOnce() + Send + 'static)
pub fn on_commit(&self, hook: impl FnOnce() + Send + 'static)
Register work after the outermost transaction successfully commits.
Hooks of failed or abandoned scopes are discarded. Registration on closed, aborted or temporarily inactive handles is discarded as well. Outside a transaction the callback runs immediately.
Args: hook: Callback invoked after commit.
Returns: None.
Source§impl Db
impl Db
Sourcepub fn with_signals(self, signals: Signals) -> Self
pub fn with_signals(self, signals: Signals) -> Self
Attach the Signals registry model operations dispatch to.
The registry is shared by clones of this handle and by the transaction handles opened from it.
Sourcepub fn same_database(&self, other: &Db) -> bool
pub fn same_database(&self, other: &Db) -> bool
Whether self and other talk to the same database: two handles of
one pool, or a transaction and the pool it was opened on.
Sourcepub fn capabilities(&self) -> BackendCapabilities
pub fn capabilities(&self) -> BackendCapabilities
Declared capabilities of the underlying backend.
Sourcepub fn read_parameter_count(
&self,
plan: &QueryPlan,
) -> Result<Option<usize>, OrmError>
pub fn read_parameter_count( &self, plan: &QueryPlan, ) -> Result<Option<usize>, OrmError>
Count read-plan binds through the backend compiler without I/O.
Args: plan: Complete target read plan, including existing filters.
Returns: Exact count when supported; None for non-reporting backends.
§Errors
Foreign database origin, invalid plan or unsupported capability.
Sourcepub fn in_transaction(&self) -> bool
pub fn in_transaction(&self) -> bool
Whether this handle runs inside a transaction.
Sourcepub fn is_scoped(&self) -> bool
pub fn is_scoped(&self) -> bool
Whether this handle owns a connection or transaction scope.
Returns: True for scoped handles; false for a pool handle.
Sourcepub async fn raw_sql(
&self,
sql: &str,
params: Vec<Value>,
) -> Result<QueryResult, OrmError>
pub async fn raw_sql( &self, sql: &str, params: Vec<Value>, ) -> Result<QueryResult, OrmError>
Raw SQL returning rows (db.raw_sql("SELECT .. WHERE id = ?", params![id])).
Placeholders follow the backend (? on SQLite, $1 on PostgreSQL).
Parameters are always bound; never format untrusted input into sql.
§Errors
Backend errors.