Skip to main content

SqlxOpStoreTx

Struct SqlxOpStoreTx 

Source
#[non_exhaustive]
pub struct SqlxOpStoreTx<DB: Database> { /* private fields */ }
Expand description

A SqlxOpStore scoped to one open sqlx transaction — every store method called on it runs inside that transaction, and nothing lands until commit.

Obtain one from [SqlxOpStore::begin_tx]. Dropping it without committing rolls back, so an early ? in the middle of a composed unit of work cannot leave a half-written state behind.

§Composing with the host application’s own writes

This is the point of the type. as_mut hands back the live connection, so the application’s own statements and the store’s run in the same transaction and commit or roll back together:

let mut tx = store.begin_tx().await?;

sqlx::query("INSERT INTO app_users (id, email) VALUES (?1, ?2)")
    .bind(&user_id)
    .bind(&email)
    .execute(tx.as_mut())
    .await?;

tx.store_token(refresh_token).await?;   // same transaction

tx.commit().await?;                     // both, or neither

Reaching the native connection is deliberately an inherent method rather than something on the OpStoreTransaction trait: the host’s own queries are written against a concrete driver, so there is nothing for a backend-agnostic trait to usefully return here. Keeping it off the trait is what lets the trait stay dyn-compatible.

Implementations§

Source§

impl<DB: Database> SqlxOpStoreTx<DB>

Source

pub async fn commit(self) -> Result<(), StoreError>

Commit, making every write in this transaction durable at once.

Source

pub async fn rollback(self) -> Result<(), StoreError>

Roll back, discarding every write in this transaction.

Dropping does this too; calling it explicitly is how a caller gets to see a rollback failure rather than have Drop swallow it.

Trait Implementations§

Source§

impl<DB: Database> AsMut<<DB as Database>::Connection> for SqlxOpStoreTx<DB>

The live connection inside this transaction, for the host application’s own statements — tx.as_mut(), exactly as it reads on a bare sqlx::Transaction, so a caller’s queries look the way they would anywhere else in their code.

Source§

fn as_mut(&mut self) -> &mut DB::Connection

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<DB: Debug + Database> Debug for SqlxOpStoreTx<DB>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<DB> !RefUnwindSafe for SqlxOpStoreTx<DB>

§

impl<DB> !UnwindSafe for SqlxOpStoreTx<DB>

§

impl<DB> Freeze for SqlxOpStoreTx<DB>
where Transaction<'static, DB>: Freeze,

§

impl<DB> Send for SqlxOpStoreTx<DB>
where Transaction<'static, DB>: Send,

§

impl<DB> Sync for SqlxOpStoreTx<DB>
where Transaction<'static, DB>: Sync,

§

impl<DB> Unpin for SqlxOpStoreTx<DB>
where Transaction<'static, DB>: Unpin,

§

impl<DB> UnsafeUnpin for SqlxOpStoreTx<DB>
where Transaction<'static, DB>: UnsafeUnpin,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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