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()?;
tx.simple_query("INSERT INTO t VALUES (1)")?;
tx.commit()?;

Implementations§

Source§

impl Transaction

Source

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

Commit the transaction.

Automatically flushes any deferred operations before committing.

Source

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

Rollback the transaction explicitly.

Discards any deferred operations without sending them.

Source

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.

Source

pub 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 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.

Source

pub 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 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.

Automatically flushes any deferred operations first.

Source

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

Simple query within the transaction.

Automatically flushes any deferred operations first.

Source

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

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.

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.