diesel_async::pg

Struct TransactionBuilder

Source
pub struct TransactionBuilder<'a, C> { /* private fields */ }
Available on crate feature postgres only.
Expand description

Used to build a transaction, specifying additional details.

This struct is returned by AsyncPgConnection::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: AsyncConnection<Backend = Pg, TransactionManager = AnsiTransactionManager>,

Source

pub fn read_only(self) -> Self

Makes the transaction READ ONLY

§Example
use diesel_async::RunQueryDsl;
conn.build_transaction()
    .read_only()
    .run::<_, diesel::result::Error, _>(|conn| Box::pin(async move {
        let read_attempt = users.select(name).load::<String>(conn).await;
        assert!(read_attempt.is_ok());

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

        Ok(())
    }) as _).await?;
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
use diesel_async::RunQueryDsl;

conn.build_transaction()
    .read_write()
    .run(|conn| Box::pin( async move {
        let read_attempt = users.select(name).load::<String>(conn).await;
        assert!(read_attempt.is_ok());

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

        Ok(())
    }) as _)
    .await
Source

pub fn deferrable(self) -> Self

Makes the transaction DEFERRABLE

§Example
conn.build_transaction()
    .deferrable()
    .run(|conn| Box::pin(async { Ok(()) }))
    .await
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| Box::pin(async { Ok(()) }) as _)
    .await
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| Box::pin(async { Ok(()) }) as _)
    .await
Source

pub fn repeatable_read(self) -> Self

Makes the transaction ISOLATION LEVEL REPEATABLE READ

§Example
conn.build_transaction()
    .repeatable_read()
    .run(|conn| Box::pin(async { Ok(()) }) as _)
    .await
Source

pub fn serializable(self) -> Self

Makes the transaction ISOLATION LEVEL SERIALIZABLE

§Example
conn.build_transaction()
    .serializable()
    .run(|conn| Box::pin(async { Ok(()) }) as _)
    .await
Source

pub async fn run<'b, T, E, F>(&mut self, f: F) -> Result<T, E>
where F: for<'r> FnOnce(&'r mut C) -> ScopedBoxFuture<'b, 'r, Result<T, E>> + Send + 'a, T: 'b, E: From<Error> + 'b,

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 or a ReadOnlyTransaction 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, C> QueryFragment<Pg> for TransactionBuilder<'a, 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 as Backend>::QueryBuilder, backend: &DB, ) -> Result<(), Error>

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 as Backend>::BindCollector<'b>, metadata_lookup: &mut <DB as TypeMetadata>::MetadataLookup, backend: &'b DB, ) -> Result<(), Error>

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) -> Result<bool, Error>

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) -> Result<bool, Error>

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> 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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) -> Self::Expression

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

fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
where &'a Self: AsExpression<T>, T: SqlType + TypedExpressionType,

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

impl<T> Pointable for T

Source§

const ALIGN: usize = _

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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>

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>

Check whether it’s safe to cache the query
Source§

impl<T, Conn> RunQueryDsl<Conn> for T

Source§

fn execute<'conn, 'query>( self, conn: &'conn mut Conn, ) -> Conn::ExecuteFuture<'conn, 'query>
where Conn: AsyncConnection + Send, Self: ExecuteDsl<Conn> + 'query,

Executes the given command, returning the number of rows affected. Read more
Source§

fn load<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> LoadFuture<'conn, 'query, Self, Conn, U>
where U: Send, Conn: AsyncConnection, Self: LoadQuery<'query, Conn, U> + 'query,

Executes the given query, returning a Vec with the returned rows. Read more
Source§

fn load_stream<'conn, 'query, U>( self, conn: &'conn mut Conn, ) -> Self::LoadFuture<'conn>
where Conn: AsyncConnection, U: 'conn, Self: LoadQuery<'query, Conn, U> + 'query,

Executes the given query, returning a Stream with the returned rows. Read more
Source§

fn get_result<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> GetResult<'conn, 'query, Self, Conn, U>
where U: Send + 'conn, Conn: AsyncConnection, Self: LoadQuery<'query, Conn, U> + 'query,

Runs the command, and returns the affected row. Read more
Source§

fn get_results<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> LoadFuture<'conn, 'query, Self, Conn, U>
where U: Send, Conn: AsyncConnection, Self: LoadQuery<'query, Conn, U> + 'query,

Runs the command, returning an Vec with the affected rows. Read more
Source§

fn first<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> GetResult<'conn, 'query, Limit<Self>, Conn, U>
where U: Send + 'conn, Conn: AsyncConnection, Self: LimitDsl, Limit<Self>: LoadQuery<'query, Conn, U> + Send + 'query,

Attempts to load a single record. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more