Skip to main content

Tx

Trait Tx 

Source
pub trait Tx: MaybeSend {
    // Required methods
    fn execute(
        &mut self,
        sql: &str,
        params: &[SqlParam],
    ) -> impl Future<Output = Result<u64, DbError>> + MaybeSend;
    fn fetch_json(
        &mut self,
        sql: &str,
        params: &[SqlParam],
    ) -> impl Future<Output = Result<Vec<Value>, DbError>> + MaybeSend;
    fn fetch_values(
        &mut self,
        sql: &str,
        params: &[SqlParam],
    ) -> impl Future<Output = Result<Vec<Vec<Value>>, DbError>> + MaybeSend;
    fn fetch_raw(
        &mut self,
        sql: &str,
        params: &[SqlParam],
        count_column: &str,
    ) -> impl Future<Output = Result<RawRows, DbError>> + MaybeSend;
    fn commit(self) -> impl Future<Output = Result<(), DbError>> + MaybeSend;
    fn queue(&mut self, sql: &str, params: &[SqlParam]);
    fn fetch_json_then(
        &mut self,
        sql: &str,
        params: &[SqlParam],
        then: &str,
    ) -> impl Future<Output = FetchedThen> + MaybeSend;
    fn fetch_json_and_commit(
        self,
        sql: &str,
        params: &[SqlParam],
    ) -> impl Future<Output = Result<Vec<Value>, DbError>> + MaybeSend;

    // Provided method
    fn fetch_json_then_and_commit(
        self,
        sql: &str,
        params: &[SqlParam],
        then: &str,
    ) -> impl Future<Output = FetchedThen> + MaybeSend
       where Self: Sized { ... }
}
Expand description

An open transaction. Dropped without Tx::commit, it rolls back.

Required Methods§

Source

fn execute( &mut self, sql: &str, params: &[SqlParam], ) -> impl Future<Output = Result<u64, DbError>> + MaybeSend

Run a statement for its effect. Returns the rows it affected.

Source

fn fetch_json( &mut self, sql: &str, params: &[SqlParam], ) -> impl Future<Output = Result<Vec<Value>, DbError>> + MaybeSend

Every row as a JSON object keyed by column name, each column decoded by its PostgreSQL type – see row_json::row_to_json for the rules, which every driver has to follow exactly.

Source

fn fetch_values( &mut self, sql: &str, params: &[SqlParam], ) -> impl Future<Output = Result<Vec<Vec<Value>>, DbError>> + MaybeSend

Every row as its columns in order, decoded by the same rules as Tx::fetch_json. For statements whose columns are read by position: a count, a plan, a pair of JSON values.

Source

fn fetch_raw( &mut self, sql: &str, params: &[SqlParam], count_column: &str, ) -> impl Future<Output = Result<RawRows, DbError>> + MaybeSend

The result of a statement whose first column is a body to send as it is: a media type the schema renders itself. See RawRows.

Source

fn commit(self) -> impl Future<Output = Result<(), DbError>> + MaybeSend

Commit the transaction.

Source

fn queue(&mut self, sql: &str, params: &[SqlParam])

Run a statement for its effect, but not yet: it is sent with the next statement, ahead of it, and a failure is reported as that statement’s.

For the statements that set a transaction up – SET TRANSACTION, the request’s settings, a pre-request function – whose only answer worth having is whether they failed. A driver that can pipeline sends them together with the query in one flight rather than waiting on each in turn; one that cannot runs them first, one by one, which is what the caller would have done anyway. Either way they run in order, before the next statement.

If one fails, nothing after it takes effect. One PostgreSQL refuses aborts the transaction, so a statement already sent behind it is refused in turn (and a COMMIT behind it rolls back); one that fails before it is sent – a parameter that cannot be encoded – stops the statements behind it from being sent at all. The failure is reported as the next statement’s, marked DbError::was_queued.

Source

fn fetch_json_then( &mut self, sql: &str, params: &[SqlParam], then: &str, ) -> impl Future<Output = FetchedThen> + MaybeSend

Tx::fetch_json, and then then – a statement with no parameters, whose columns are read by position, as by Tx::fetch_values.

Two statements whose order matters but whose second does not depend on what the first returned, so a driver that can pipeline sends both at once. For a function’s response settings, read back after it ran.

Source

fn fetch_json_and_commit( self, sql: &str, params: &[SqlParam], ) -> impl Future<Output = Result<Vec<Value>, DbError>> + MaybeSend

Tx::fetch_json as the transaction’s last statement, and Tx::commit.

For a read, where nothing that follows can change whether it should commit, so a driver that can pipeline sends the COMMIT with the query. A failed query is reported as it would be by Tx::fetch_json, and the transaction rolls back.

Provided Methods§

Source

fn fetch_json_then_and_commit( self, sql: &str, params: &[SqlParam], then: &str, ) -> impl Future<Output = FetchedThen> + MaybeSend
where Self: Sized,

Tx::fetch_json_then as the transaction’s last statements, and Tx::commit.

For a read that has to read its response settings back – one a pre-request function may have set them on – and then has nothing left that could change whether it should commit. A driver that can pipeline sends all three at once; the default runs them in turn.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§