pub struct Transaction { /* private fields */ }Expand description
A database transaction. Sends ROLLBACK on drop if not committed.
§Example
let mut tx = pool.begin()?;
tx.simple_query("INSERT INTO t VALUES (1)")?;
tx.commit()?;Implementations§
Source§impl Transaction
impl Transaction
Sourcepub fn commit(self) -> Result<(), DriverError>
pub fn commit(self) -> Result<(), DriverError>
Commit the transaction.
Automatically flushes any deferred operations before committing.
Sourcepub fn rollback(self) -> Result<(), DriverError>
pub fn rollback(self) -> Result<(), DriverError>
Rollback the transaction explicitly.
Discards any deferred operations without sending them.
Sourcepub fn query(
&mut self,
sql: &str,
sql_hash: u64,
params: &[&(dyn Encode + Sync)],
) -> Result<QueryResult, DriverError>
pub fn query( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], ) -> 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 fn execute(
&mut self,
sql: &str,
sql_hash: u64,
params: &[&(dyn Encode + Sync)],
) -> Result<u64, DriverError>
pub fn execute( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], ) -> Result<u64, DriverError>
Execute without result rows within the transaction.
Sourcepub fn execute_pipeline(
&mut self,
sql: &str,
sql_hash: u64,
param_sets: &[&[&(dyn Encode + Sync)]],
) -> Result<Vec<u64>, DriverError>
pub 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.
Sourcepub fn for_each<F>(
&mut self,
sql: &str,
sql_hash: u64,
params: &[&(dyn Encode + Sync)],
f: F,
) -> Result<(), DriverError>
pub 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 fn for_each_raw<F>(
&mut self,
sql: &str,
sql_hash: u64,
params: &[&(dyn Encode + Sync)],
f: F,
) -> Result<(), DriverError>
pub 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.
Automatically flushes any deferred operations first.
Sourcepub fn simple_query(&mut self, sql: &str) -> Result<(), DriverError>
pub fn simple_query(&mut self, sql: &str) -> Result<(), DriverError>
Simple query within the transaction.
Automatically flushes any deferred operations first.
Sourcepub fn defer_execute(
&mut self,
sql: &str,
sql_hash: u64,
params: &[&(dyn Encode + Sync)],
) -> Result<(), DriverError>
pub 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().
§Example
let mut tx = pool.begin()?;
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])?;
tx.defer_execute(sql, hash, &[&2i32])?;
tx.defer_execute(sql, hash, &[&3i32])?;
// commit() flushes all 3 as one pipeline + COMMIT = 2 round-trips total
tx.commit()?;Sourcepub fn flush_deferred(&mut self) -> Result<Vec<u64>, DriverError>
pub 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.
Sourcepub fn deferred_count(&self) -> usize
pub fn deferred_count(&self) -> usize
Number of operations currently buffered for deferred execution.