Skip to main content

Transaction

Struct Transaction 

Source
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

Source

pub async fn commit(self) -> Result<(), DriverError>

Commit the transaction.

Automatically flushes any deferred operations before committing.

Source

pub async fn rollback(self) -> Result<(), DriverError>

Rollback the transaction explicitly.

Discards any deferred operations without sending them.

Source

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.

Source

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.

Source

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.

Source

pub async fn for_each<F>( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], f: F, ) -> Result<(), DriverError>
where F: FnMut(PgDataRow<'_>) -> Result<(), DriverError>,

Process each row directly from the wire buffer within a transaction.

Automatically flushes any deferred operations first.

Source

pub async fn for_each_raw<F>( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], f: F, ) -> Result<(), DriverError>
where F: FnMut(&[u8]) -> 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.

Source

pub async fn simple_query(&mut self, sql: &str) -> Result<(), DriverError>

Simple query within the transaction.

Automatically flushes any deferred operations first.

Source

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?;
Source

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.

Source

pub fn deferred_count(&self) -> usize

Number of operations currently buffered for deferred execution.

Trait Implementations§

Source§

impl Drop for Transaction

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.