pub struct Transaction { /* private fields */ }Expand description
High-level transaction wrapper with automatic cleanup and enhanced functionality
Implementations§
Source§impl Transaction
impl Transaction
Sourcepub async fn begin(
pool: &ManagedPool,
config: TransactionConfig,
) -> Result<Transaction, ModelError>
pub async fn begin( pool: &ManagedPool, config: TransactionConfig, ) -> Result<Transaction, ModelError>
Create a new transaction with the given configuration
Sourcepub async fn begin_default(
pool: &ManagedPool,
) -> Result<Transaction, ModelError>
pub async fn begin_default( pool: &ManagedPool, ) -> Result<Transaction, ModelError>
Create a transaction with default configuration
Sourcepub async fn begin_read_only(
pool: &ManagedPool,
) -> Result<Transaction, ModelError>
pub async fn begin_read_only( pool: &ManagedPool, ) -> Result<Transaction, ModelError>
Create a read-only transaction
Sourcepub async fn begin_serializable(
pool: &ManagedPool,
) -> Result<Transaction, ModelError>
pub async fn begin_serializable( pool: &ManagedPool, ) -> Result<Transaction, ModelError>
Create a serializable transaction
Sourcepub fn as_mut(&mut self) -> Option<&mut Box<dyn DatabaseTransaction>>
pub fn as_mut(&mut self) -> Option<&mut Box<dyn DatabaseTransaction>>
Get a mutable reference to the underlying database transaction
§Safety
This method provides direct access to the underlying transaction. Care should be taken not to commit or rollback the transaction directly as this will invalidate the Transaction wrapper.
Sourcepub fn as_ref(&self) -> Option<&Box<dyn DatabaseTransaction>>
pub fn as_ref(&self) -> Option<&Box<dyn DatabaseTransaction>>
Get an immutable reference to the underlying database transaction
Sourcepub async fn execute(
&mut self,
sql: &str,
params: &[DatabaseValue],
) -> Result<u64, ModelError>
pub async fn execute( &mut self, sql: &str, params: &[DatabaseValue], ) -> Result<u64, ModelError>
Execute a query within this transaction
Sourcepub async fn fetch_all(
&mut self,
sql: &str,
params: &[DatabaseValue],
) -> Result<Vec<Box<dyn DatabaseRow>>, ModelError>
pub async fn fetch_all( &mut self, sql: &str, params: &[DatabaseValue], ) -> Result<Vec<Box<dyn DatabaseRow>>, ModelError>
Fetch all rows from a query within this transaction
Sourcepub async fn fetch_optional(
&mut self,
sql: &str,
params: &[DatabaseValue],
) -> Result<Option<Box<dyn DatabaseRow>>, ModelError>
pub async fn fetch_optional( &mut self, sql: &str, params: &[DatabaseValue], ) -> Result<Option<Box<dyn DatabaseRow>>, ModelError>
Fetch optional single row from a query within this transaction
Sourcepub async fn execute_with<F, Fut, R>(&mut self, f: F) -> Result<R, ModelError>
pub async fn execute_with<F, Fut, R>(&mut self, f: F) -> Result<R, ModelError>
Execute a closure within the transaction scope with a borrowed transaction
This is a safe way to execute database operations within a transaction. The closure receives access to execute queries against the transaction.
Sourcepub async fn commit(self) -> ModelResult<()>
pub async fn commit(self) -> ModelResult<()>
Commit the transaction
Sourcepub async fn rollback(self) -> ModelResult<()>
pub async fn rollback(self) -> ModelResult<()>
Rollback the transaction
Sourcepub fn is_committed(&self) -> bool
pub fn is_committed(&self) -> bool
Check if the transaction has been committed
Sourcepub fn config(&self) -> &TransactionConfig
pub fn config(&self) -> &TransactionConfig
Get the transaction configuration
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Transaction
impl !RefUnwindSafe for Transaction
impl Send for Transaction
impl Sync for Transaction
impl Unpin for Transaction
impl !UnwindSafe for Transaction
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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