TransactionBuilder

Struct TransactionBuilder 

Source
pub struct TransactionBuilder<'a, C> { /* private fields */ }
Available on crate feature postgres_backend only.
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, C> TransactionBuilder<'a, C>
where C: Connection<Backend = Pg, TransactionManager = AnsiTransactionManager>,

Source

pub fn new(connection: &'a mut C) -> Self

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.

Creates a new TransactionBuilder.

Source

pub fn read_only(self) -> Self

Makes the transaction READ ONLY

§Example
conn.build_transaction()
    .read_only()
    .run::<_, diesel::result::Error, _>(|conn| {
        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(|conn| {
    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(|conn| 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(|conn| 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(|conn| Ok(()))
Source

pub fn repeatable_read(self) -> Self

Makes the transaction ISOLATION LEVEL REPEATABLE READ

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

pub fn serializable(self) -> Self

Makes the transaction ISOLATION LEVEL SERIALIZABLE

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

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

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

This function executes the provided closure f inside a database transaction. If there is already an open transaction for the current connection it will return an error. The connection is committed if the closure returns Ok(_), it will be rolled back if it returns Err(_). For both cases the original result value will be returned from this function.

If the transaction fails to commit and requires a rollback according to Postgres, (e.g. serialization failure) a rollback will be attempted. If the rollback fails, the error will be returned in a Error::RollbackErrorOnCommit, from which you will be able to extract both the original commit error and the rollback error. In addition, the connection will be considered broken as it contains a uncommitted unabortable open transaction. Any further interaction with the transaction system will result in an returned error in this case.

Trait Implementations§

Source§

impl<C> QueryFragment<Pg> for TransactionBuilder<'_, C>

Source§

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

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

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

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Converts this QueryFragment to its SQL representation. Read more
Source§

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

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Serializes all bind parameters in this query. Read more
Source§

fn is_safe_to_cache_prepared(&self, backend: &DB) -> QueryResult<bool>

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Is this query safe to store in the prepared statement cache? Read more
Source§

fn is_noop(&self, backend: &DB) -> QueryResult<bool>

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Does walking this AST have any effect?

Auto Trait Implementations§

§

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

§

impl<'a, C> RefUnwindSafe for TransactionBuilder<'a, C>
where C: RefUnwindSafe,

§

impl<'a, C> Send for TransactionBuilder<'a, C>
where C: Send,

§

impl<'a, C> Sync for TransactionBuilder<'a, C>
where C: Sync,

§

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

§

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

Blanket Implementations§

Source§

impl<T> AggregateExpressionMethods for T

Source§

fn aggregate_distinct(self) -> AggregateDistinct<Self>
where Self: DistinctDsl,

DISTINCT modifier for aggregate functions Read more
Source§

fn aggregate_all(self) -> AggregateAll<Self>
where Self: AllDsl,

ALL modifier for aggregate functions Read more
Source§

fn aggregate_filter<P>(self, f: P) -> AggregateFilter<Self, P>
where P: AsExpression<Bool>, Self: FilterDsl<P::Expression>,

Add an aggregate function filter Read more
Source§

fn aggregate_order<O>(self, o: O) -> AggregateOrder<Self, O>
where Self: OrderAggregateDsl<O>,

Add an aggregate function order Read more
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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Sync + Send>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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>

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>, T: SqlType + TypedExpressionType,

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

impl<T, DB> QueryFragmentForCachedStatement<DB> for T
where DB: Backend, <DB as Backend>::QueryBuilder: Default, T: QueryFragment<DB>,

Source§

fn construct_sql(&self, backend: &DB) -> Result<String, Error>

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Convert the query fragment into a SQL string for the given backend
Source§

fn is_safe_to_cache_prepared(&self, backend: &DB) -> Result<bool, Error>

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Check whether it’s safe to cache the query
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.
Source§

impl<T> WindowExpressionMethods for T

Source§

fn over(self) -> Over<Self>
where Self: OverDsl,

Turn a function call into a window function call Read more
Source§

fn window_filter<P>(self, f: P) -> WindowFilter<Self, P>
where P: AsExpression<Bool>, Self: FilterDsl<P::Expression>,

Add a filter to the current window function Read more
Source§

fn partition_by<E>(self, expr: E) -> PartitionBy<Self, E>
where Self: PartitionByDsl<E>,

Add a partition clause to the current window function Read more
Source§

fn window_order<E>(self, expr: E) -> WindowOrder<Self, E>
where Self: OrderWindowDsl<E>,

Add a order clause to the current window function Read more
Source§

fn frame_by<E>(self, expr: E) -> FrameBy<Self, E>
where Self: FrameDsl<E>,

Add a frame clause to the current window function Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,