pub struct Tx<DB: Database, E = Error>(/* private fields */);Expand description
An actix extractor for a database transaction.
&mut Tx implements sqlx::Executor so it can be used directly with sqlx::query()
(and sqlx::query_as(), the corresponding macros, etc.):
use actix_sqlx_tx::Tx;
use sqlx::Sqlite;
async fn handler(mut tx: Tx<Sqlite>) -> Result<(), sqlx::Error> {
sqlx::query("...").execute(&mut tx).await?;
/* ... */
}It also implements Deref<Target = sqlx::Transaction> and DerefMut, so you can call
methods from Transaction and its traits:
use actix_sqlx_tx::Tx;
use sqlx::{Acquire as _, Sqlite};
async fn handler(mut tx: Tx<Sqlite>) -> Result<(), sqlx::Error> {
let inner = tx.begin().await?;
/* ... */
}The E generic parameter controls the error type returned when the extractor fails. This can be
used to configure the error response returned when the extractor fails:
use actix_web::{
http::{header::ContentType, StatusCode},
HttpResponse, ResponseError,
};
use actix_sqlx_tx::Tx;
use sqlx::Sqlite;
[derive(Debug)]
struct MyError(actix_sqlx_tx::Error);
// The error type must implement From<actix_sqlx_tx::Error>
impl From<actix_sqlx_tx::Error> for MyError {
fn from(error: actix_sqlx_tx::Error) -> Self {
Self(error)
}
}
// The error type must implement ResponseError
impl ResponseError for MyError {
fn error_response(&self) -> HttpResponse {
HttpResponse::build(self.status_code())
.insert_header(ContentType::html())
.body(self.to_string())
}
fn status_code(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
}
async fn handler(tx: Tx<Sqlite, MyError>) {
/* ... */
}Implementations§
Source§impl<DB: Database, E> Tx<DB, E>
impl<DB: Database, E> Tx<DB, E>
Sourcepub async fn commit(self) -> Result<(), Error>
pub async fn commit(self) -> Result<(), Error>
Explicitly commit the transaction.
By default, the transaction will be committed when a successful response is returned
(specifically, when the Service middleware intercepts an HTTP 2XX
response). This method allows the transaction to be committed explicitly.
Note: trying to use the Tx extractor again after calling commit will currently
generate Error::OverlappingExtractors errors. This may change in future.
Trait Implementations§
Source§impl<DB: Database, E> AsMut<Transaction<'static, DB>> for Tx<DB, E>
impl<DB: Database, E> AsMut<Transaction<'static, DB>> for Tx<DB, E>
Source§fn as_mut(&mut self) -> &mut Transaction<'static, DB>
fn as_mut(&mut self) -> &mut Transaction<'static, DB>
Source§impl<DB: Database, E> AsRef<Transaction<'static, DB>> for Tx<DB, E>
impl<DB: Database, E> AsRef<Transaction<'static, DB>> for Tx<DB, E>
Source§fn as_ref(&self) -> &Transaction<'static, DB>
fn as_ref(&self) -> &Transaction<'static, DB>
Source§impl<'c, E: Debug + Send> Executor<'c> for &'c mut Tx<Any, E>
impl<'c, E: Debug + Send> Executor<'c> for &'c mut Tx<Any, E>
type Database = Any
Source§fn fetch_many<'e, 'q: 'e, Q>(
self,
query: Q,
) -> BoxStream<'e, Result<Either<<Self::Database as Database>::QueryResult, <Self::Database as Database>::Row>, Error>>
fn fetch_many<'e, 'q: 'e, Q>( self, query: Q, ) -> BoxStream<'e, Result<Either<<Self::Database as Database>::QueryResult, <Self::Database as Database>::Row>, Error>>
Source§fn fetch_optional<'e, 'q: 'e, Q>(
self,
query: Q,
) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>
fn fetch_optional<'e, 'q: 'e, Q>( self, query: Q, ) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>
Source§fn prepare_with<'e, 'q: 'e>(
self,
sql: &'q str,
parameters: &'e [<Self::Database as Database>::TypeInfo],
) -> BoxFuture<'e, Result<<Self::Database as HasStatement<'q>>::Statement, Error>>where
'c: 'e,
fn prepare_with<'e, 'q: 'e>(
self,
sql: &'q str,
parameters: &'e [<Self::Database as Database>::TypeInfo],
) -> BoxFuture<'e, Result<<Self::Database as HasStatement<'q>>::Statement, Error>>where
'c: 'e,
Source§fn execute<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Source§fn execute_many<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute_many<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Source§fn fetch<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
fn fetch<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
Source§fn fetch_all<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
fn fetch_all<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
Vec.Source§impl<'c, E: Debug + Send> Executor<'c> for &'c mut Tx<Mssql, E>
impl<'c, E: Debug + Send> Executor<'c> for &'c mut Tx<Mssql, E>
type Database = Mssql
Source§fn fetch_many<'e, 'q: 'e, Q>(
self,
query: Q,
) -> BoxStream<'e, Result<Either<<Self::Database as Database>::QueryResult, <Self::Database as Database>::Row>, Error>>
fn fetch_many<'e, 'q: 'e, Q>( self, query: Q, ) -> BoxStream<'e, Result<Either<<Self::Database as Database>::QueryResult, <Self::Database as Database>::Row>, Error>>
Source§fn fetch_optional<'e, 'q: 'e, Q>(
self,
query: Q,
) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>
fn fetch_optional<'e, 'q: 'e, Q>( self, query: Q, ) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>
Source§fn prepare_with<'e, 'q: 'e>(
self,
sql: &'q str,
parameters: &'e [<Self::Database as Database>::TypeInfo],
) -> BoxFuture<'e, Result<<Self::Database as HasStatement<'q>>::Statement, Error>>where
'c: 'e,
fn prepare_with<'e, 'q: 'e>(
self,
sql: &'q str,
parameters: &'e [<Self::Database as Database>::TypeInfo],
) -> BoxFuture<'e, Result<<Self::Database as HasStatement<'q>>::Statement, Error>>where
'c: 'e,
Source§fn execute<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Source§fn execute_many<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute_many<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Source§fn fetch<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
fn fetch<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
Source§fn fetch_all<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
fn fetch_all<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
Vec.Source§impl<'c, E: Debug + Send> Executor<'c> for &'c mut Tx<MySql, E>
impl<'c, E: Debug + Send> Executor<'c> for &'c mut Tx<MySql, E>
type Database = MySql
Source§fn fetch_many<'e, 'q: 'e, Q>(
self,
query: Q,
) -> BoxStream<'e, Result<Either<<Self::Database as Database>::QueryResult, <Self::Database as Database>::Row>, Error>>
fn fetch_many<'e, 'q: 'e, Q>( self, query: Q, ) -> BoxStream<'e, Result<Either<<Self::Database as Database>::QueryResult, <Self::Database as Database>::Row>, Error>>
Source§fn fetch_optional<'e, 'q: 'e, Q>(
self,
query: Q,
) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>
fn fetch_optional<'e, 'q: 'e, Q>( self, query: Q, ) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>
Source§fn prepare_with<'e, 'q: 'e>(
self,
sql: &'q str,
parameters: &'e [<Self::Database as Database>::TypeInfo],
) -> BoxFuture<'e, Result<<Self::Database as HasStatement<'q>>::Statement, Error>>where
'c: 'e,
fn prepare_with<'e, 'q: 'e>(
self,
sql: &'q str,
parameters: &'e [<Self::Database as Database>::TypeInfo],
) -> BoxFuture<'e, Result<<Self::Database as HasStatement<'q>>::Statement, Error>>where
'c: 'e,
Source§fn execute<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Source§fn execute_many<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute_many<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Source§fn fetch<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
fn fetch<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
Source§fn fetch_all<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
fn fetch_all<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
Vec.Source§impl<'c, E: Debug + Send> Executor<'c> for &'c mut Tx<Postgres, E>
impl<'c, E: Debug + Send> Executor<'c> for &'c mut Tx<Postgres, E>
type Database = Postgres
Source§fn fetch_many<'e, 'q: 'e, Q>(
self,
query: Q,
) -> BoxStream<'e, Result<Either<<Self::Database as Database>::QueryResult, <Self::Database as Database>::Row>, Error>>
fn fetch_many<'e, 'q: 'e, Q>( self, query: Q, ) -> BoxStream<'e, Result<Either<<Self::Database as Database>::QueryResult, <Self::Database as Database>::Row>, Error>>
Source§fn fetch_optional<'e, 'q: 'e, Q>(
self,
query: Q,
) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>
fn fetch_optional<'e, 'q: 'e, Q>( self, query: Q, ) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>
Source§fn prepare_with<'e, 'q: 'e>(
self,
sql: &'q str,
parameters: &'e [<Self::Database as Database>::TypeInfo],
) -> BoxFuture<'e, Result<<Self::Database as HasStatement<'q>>::Statement, Error>>where
'c: 'e,
fn prepare_with<'e, 'q: 'e>(
self,
sql: &'q str,
parameters: &'e [<Self::Database as Database>::TypeInfo],
) -> BoxFuture<'e, Result<<Self::Database as HasStatement<'q>>::Statement, Error>>where
'c: 'e,
Source§fn execute<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Source§fn execute_many<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute_many<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Source§fn fetch<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
fn fetch<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
Source§fn fetch_all<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
fn fetch_all<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
Vec.Source§impl<'c, E: Debug + Send> Executor<'c> for &'c mut Tx<Sqlite, E>
impl<'c, E: Debug + Send> Executor<'c> for &'c mut Tx<Sqlite, E>
type Database = Sqlite
Source§fn fetch_many<'e, 'q: 'e, Q>(
self,
query: Q,
) -> BoxStream<'e, Result<Either<<Self::Database as Database>::QueryResult, <Self::Database as Database>::Row>, Error>>
fn fetch_many<'e, 'q: 'e, Q>( self, query: Q, ) -> BoxStream<'e, Result<Either<<Self::Database as Database>::QueryResult, <Self::Database as Database>::Row>, Error>>
Source§fn fetch_optional<'e, 'q: 'e, Q>(
self,
query: Q,
) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>
fn fetch_optional<'e, 'q: 'e, Q>( self, query: Q, ) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>
Source§fn prepare_with<'e, 'q: 'e>(
self,
sql: &'q str,
parameters: &'e [<Self::Database as Database>::TypeInfo],
) -> BoxFuture<'e, Result<<Self::Database as HasStatement<'q>>::Statement, Error>>where
'c: 'e,
fn prepare_with<'e, 'q: 'e>(
self,
sql: &'q str,
parameters: &'e [<Self::Database as Database>::TypeInfo],
) -> BoxFuture<'e, Result<<Self::Database as HasStatement<'q>>::Statement, Error>>where
'c: 'e,
Source§fn execute<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Source§fn execute_many<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute_many<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Source§fn fetch<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
fn fetch<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
Source§fn fetch_all<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
fn fetch_all<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
Vec.Source§impl<DB: Database, E> FromRequest for Tx<DB, E>
impl<DB: Database, E> FromRequest for Tx<DB, E>
Auto Trait Implementations§
impl<DB, E> Freeze for Tx<DB, E>
impl<DB, E = Error> !RefUnwindSafe for Tx<DB, E>
impl<DB, E> Send for Tx<DB, E>where
E: Send,
impl<DB, E> Sync for Tx<DB, E>
impl<DB, E> Unpin for Tx<DB, E>
impl<DB, E = Error> !UnwindSafe for Tx<DB, E>
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 moreSource§impl<R> Rng for R
impl<R> Rng for R
Source§fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
StandardUniform distribution. Read moreSource§fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>
fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>
Source§fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
Source§fn random_bool(&mut self, p: f64) -> bool
fn random_bool(&mut self, p: f64) -> bool
p of being true. Read moreSource§fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
numerator/denominator of being
true. Read moreSource§fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
Source§fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>where
D: Distribution<T>,
Self: Sized,
fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>where
D: Distribution<T>,
Self: Sized,
Source§fn gen<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn gen<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
random to avoid conflict with the new gen keyword in Rust 2024.Rng::random.Source§fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
random_rangeRng::random_range.Source§impl<R> TryRngCore for R
impl<R> TryRngCore for R
Source§type Error = Infallible
type Error = Infallible
Source§fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
u32.Source§fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
u64.Source§fn try_fill_bytes(
&mut self,
dst: &mut [u8],
) -> Result<(), <R as TryRngCore>::Error>
fn try_fill_bytes( &mut self, dst: &mut [u8], ) -> Result<(), <R as TryRngCore>::Error>
dest entirely with random data.Source§fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
UnwrapMut wrapper.Source§fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
Self: Sized,
fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
Self: Sized,
RngCore to a RngReadAdapter.