Skip to main content

DbConnectionBlocking

Trait DbConnectionBlocking 

Source
pub trait DbConnectionBlocking: Send + Sync {
    // Required methods
    fn execute_sql(&self, sql: &str, params: Vec<Value>) -> Result<u64, DbError>;
    fn query_map<T: FromRow>(
        &self,
        sql: &str,
        params: Vec<Value>,
    ) -> Result<Vec<T>, DbError>;
    fn execute_batch(&self, sql: &str) -> Result<(), DbError>;
}
Expand description

Synchronous counterpart to DbConnection, for in-process drivers that are natively blocking.

Only RusqliteConnection implements it: SQLite runs in the calling process and never yields, so nothing is gained by going through a runtime. libsql and Postgres talk to a server and are genuinely async; a blocking wrapper there would only hide a block_on.

Reach for this when the consumer has no async of its own – a CLI that would otherwise build a runtime per invocation – or when it already runs its database work on a blocking thread and does not want a second thread hop underneath.

RusqliteConnection implements this and DbConnection, and the two traits share method names. With both in scope a plain conn.execute_sql(..) is ambiguous (E0034); name the trait to pick a path:

DbConnectionBlocking::execute_sql(&conn, sql, params)?;
DbConnection::execute_sql(&conn, sql, params).await?;

Required Methods§

Source

fn execute_sql(&self, sql: &str, params: Vec<Value>) -> Result<u64, DbError>

Execute a write statement (INSERT/UPDATE/DELETE) and return affected row count.

§Errors

Returns DbError::Query if the SQL execution fails.

Source

fn query_map<T: FromRow>( &self, sql: &str, params: Vec<Value>, ) -> Result<Vec<T>, DbError>

Execute a SELECT statement and map each row into T via FromRow.

Unlike the async trait, T needs no Send + 'static bound: rows are decoded and returned on the caller’s own thread.

§Errors

Returns DbError::Query if the SQL execution fails or DbError::RowMapping if a row cannot be converted into T.

Source

fn execute_batch(&self, sql: &str) -> Result<(), DbError>

Execute a batch of SQL statements (e.g., DDL, migrations).

§Errors

Returns DbError::Query if any statement in the batch fails.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§