diesel::pg

Struct TransactionBuilder

Source
pub struct TransactionBuilder<'a> { /* private fields */ }
Expand description

Used to build a transaction, specifying additional details.

This struct is returned by .build_transaction. See the documentation for methods on this struct for usage examples. See the PostgreSQL documentation for SET TRANSACTION for details on the behavior of each option.

Implementations§

Source§

impl<'a> TransactionBuilder<'a>

Source

pub fn read_only(self) -> Self

Makes the transaction READ ONLY

§Example
conn.build_transaction()
    .read_only()
    .run::<_, diesel::result::Error, _>(|| {
        let read_attempt = users.select(name).load::<String>(&conn);
        assert!(read_attempt.is_ok());

        let write_attempt = diesel::insert_into(users)
            .values(name.eq("Ruby"))
            .execute(&conn);
        assert!(write_attempt.is_err());

        Ok(())
    })?;
Source

pub fn read_write(self) -> Self

Makes the transaction READ WRITE

This is the default, unless you’ve changed the default_transaction_read_only configuration parameter.

§Example
conn.build_transaction()
    .read_write()
    .run(|| {
        let read_attempt = users.select(name).load::<String>(&conn);
        assert!(read_attempt.is_ok());

        let write_attempt = diesel::insert_into(users)
            .values(name.eq("Ruby"))
            .execute(&conn);
        assert!(write_attempt.is_ok());

        Ok(())
    })
Source

pub fn deferrable(self) -> Self

Makes the transaction DEFERRABLE

§Example
conn.build_transaction()
    .deferrable()
    .run(|| Ok(()))
Source

pub fn not_deferrable(self) -> Self

Makes the transaction NOT DEFERRABLE

This is the default, unless you’ve changed the default_transaction_deferrable configuration parameter.

§Example
conn.build_transaction()
    .not_deferrable()
    .run(|| Ok(()))
Source

pub fn read_committed(self) -> Self

Makes the transaction ISOLATION LEVEL READ COMMITTED

This is the default, unless you’ve changed the default_transaction_isolation_level configuration parameter.

§Example
conn.build_transaction()
    .read_committed()
    .run(|| Ok(()))
Source

pub fn repeatable_read(self) -> Self

Makes the transaction ISOLATION LEVEL REPEATABLE READ

§Example
conn.build_transaction()
    .repeatable_read()
    .run(|| Ok(()))
Source

pub fn serializable(self) -> Self

Makes the transaction ISOLATION LEVEL SERIALIZABLE

§Example
conn.build_transaction()
    .serializable()
    .run(|| Ok(()))
Source

pub fn run<T, E, F>(&self, f: F) -> Result<T, E>
where F: FnOnce() -> Result<T, E>, E: From<Error>,

Runs the given function inside of the transaction with the parameters given to this builder.

Returns an error if the connection is already inside a transaction, or if the transaction fails to commit or rollback

If the transaction fails to commit due to a SerializationFailure, a rollback will be attempted. If the rollback succeeds, the original error will be returned, otherwise the error generated by the rollback will be returned. In the second case the connection should be considered broken as it contains a uncommitted unabortable open transaction.

Trait Implementations§

Source§

impl<'a> Clone for TransactionBuilder<'a>

Source§

fn clone(&self) -> TransactionBuilder<'a>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> QueryFragment<Pg> for TransactionBuilder<'a>

Source§

fn walk_ast(&self, out: AstPass<'_, Pg>) -> QueryResult<()>

Walk over this QueryFragment for all passes. Read more
Source§

fn to_sql(&self, out: &mut DB::QueryBuilder) -> QueryResult<()>

Converts this QueryFragment to its SQL representation. Read more
Source§

fn collect_binds( &self, out: &mut DB::BindCollector, metadata_lookup: &DB::MetadataLookup, ) -> QueryResult<()>

Serializes all bind parameters in this query. Read more
Source§

fn is_safe_to_cache_prepared(&self) -> QueryResult<bool>

Is this query safe to store in the prepared statement cache? Read more
Source§

impl<'a> Copy for TransactionBuilder<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for TransactionBuilder<'a>

§

impl<'a> !RefUnwindSafe for TransactionBuilder<'a>

§

impl<'a> !Send for TransactionBuilder<'a>

§

impl<'a> !Sync for TransactionBuilder<'a>

§

impl<'a> Unpin for TransactionBuilder<'a>

§

impl<'a> !UnwindSafe for TransactionBuilder<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> IntoSql for T

Source§

fn into_sql<T>(self) -> AsExprOf<Self, T>
where Self: AsExpression<T> + Sized,

Convert self to an expression for Diesel’s query builder. Read more
Source§

fn as_sql<'a, T>(&'a self) -> AsExprOf<&'a Self, T>
where &'a Self: AsExpression<T>,

Convert &self to an expression for Diesel’s query builder. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.