pub struct Transaction { /* private fields */ }Expand description
A database transaction. Sends ROLLBACK on drop if not committed.
§Example
let mut tx = pool.begin().await?;
tx.simple_query("INSERT INTO t VALUES (1)").await?;
tx.commit().await?;Implementations§
Source§impl Transaction
impl Transaction
Sourcepub async fn commit(self) -> Result<(), DriverError>
pub async fn commit(self) -> Result<(), DriverError>
Commit the transaction.
Automatically flushes any deferred operations before committing.
Sourcepub async fn rollback(self) -> Result<(), DriverError>
pub async fn rollback(self) -> Result<(), DriverError>
Rollback the transaction explicitly.
Discards any deferred operations without sending them.
Sourcepub async fn query(
&mut self,
sql: &str,
sql_hash: u64,
params: &[&(dyn Encode + Sync)],
arena: &mut Arena,
) -> Result<QueryResult, DriverError>
pub async fn query( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], arena: &mut Arena, ) -> Result<QueryResult, DriverError>
Execute a prepared query within the transaction.
Automatically flushes any deferred operations before executing the query, ensuring read-your-writes consistency.
Sourcepub async fn execute(
&mut self,
sql: &str,
sql_hash: u64,
params: &[&(dyn Encode + Sync)],
) -> Result<u64, DriverError>
pub async fn execute( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], ) -> Result<u64, DriverError>
Execute without result rows within the transaction.
Sourcepub async fn execute_pipeline(
&mut self,
sql: &str,
sql_hash: u64,
param_sets: &[&[&(dyn Encode + Sync)]],
) -> Result<Vec<u64>, DriverError>
pub async fn execute_pipeline( &mut self, sql: &str, sql_hash: u64, param_sets: &[&[&(dyn Encode + Sync)]], ) -> Result<Vec<u64>, DriverError>
Execute the same statement N times with different params in one pipeline.
All N Bind+Execute messages are sent with one Sync at the end. One round-trip for N operations within the transaction.
Sourcepub async fn for_each<F>(
&mut self,
sql: &str,
sql_hash: u64,
params: &[&(dyn Encode + Sync)],
f: F,
) -> Result<(), DriverError>
pub async fn for_each<F>( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], f: F, ) -> Result<(), DriverError>
Process each row directly from the wire buffer within a transaction.
Automatically flushes any deferred operations first.
Sourcepub async fn for_each_raw<F>(
&mut self,
sql: &str,
sql_hash: u64,
params: &[&(dyn Encode + Sync)],
f: F,
) -> Result<(), DriverError>
pub async fn for_each_raw<F>( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], f: F, ) -> Result<(), DriverError>
Process each DataRow as raw bytes within a transaction.
The closure receives the raw DataRow message payload. Generated code decodes columns sequentially inline — no PgDataRow, no SmallVec.
Automatically flushes any deferred operations first.
Sourcepub async fn simple_query(&mut self, sql: &str) -> Result<(), DriverError>
pub async fn simple_query(&mut self, sql: &str) -> Result<(), DriverError>
Simple query within the transaction.
Automatically flushes any deferred operations first.
Sourcepub async fn defer_execute(
&mut self,
sql: &str,
sql_hash: u64,
params: &[&(dyn Encode + Sync)],
) -> Result<(), DriverError>
pub async fn defer_execute( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], ) -> Result<(), DriverError>
Buffer an execute for deferred pipeline flush.
The operation is not sent to the server immediately. Instead, the
Bind+Execute message bytes are buffered internally. The buffered
operations are sent as a single pipeline on commit()
or flush_deferred().
If the statement has not been prepared yet, a single round-trip is made to prepare it (Parse+Describe+Sync). After that, the Bind+Execute bytes are buffered with no I/O.
Note: Because execution is deferred, the affected row count is not
available until flush. Use flush_deferred() if you need per-operation
counts, or commit() if you only need correctness.
Any read operation (query, for_each, for_each_raw, simple_query)
automatically flushes deferred operations first to ensure
read-your-writes consistency.
§Example
let mut tx = pool.begin().await?;
let sql = "INSERT INTO t (v) VALUES ($1)";
let hash = bsql_driver_postgres::hash_sql(sql);
// These are buffered, not sent:
tx.defer_execute(sql, hash, &[&1i32]).await?;
tx.defer_execute(sql, hash, &[&2i32]).await?;
tx.defer_execute(sql, hash, &[&3i32]).await?;
// commit() flushes all 3 as one pipeline + COMMIT = 2 round-trips total
tx.commit().await?;Sourcepub async fn flush_deferred(&mut self) -> Result<Vec<u64>, DriverError>
pub async fn flush_deferred(&mut self) -> Result<Vec<u64>, DriverError>
Flush all deferred operations as a single pipeline.
Sends all buffered Bind+Execute messages + one Sync in a single TCP write. Returns the affected row count for each deferred operation.
After this call, the deferred buffer is empty and new operations can be deferred again.
Sourcepub fn deferred_count(&self) -> usize
pub fn deferred_count(&self) -> usize
Number of operations currently buffered for deferred execution.