Add example on how to use Transaction as Executor (#3311)

* add example using Transaction as Executor to docs

* change tx.as_mut() to &mut *tx
This commit is contained in:
Leon Lux 2024-07-23 00:15:57 +02:00 committed by GitHub
parent eaad7b2c9a
commit 940d9fb994
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -46,6 +46,24 @@ pub trait TransactionManager {
/// executed after it was established to be rolled back, restoring the transaction state to
/// what it was at the time of the savepoint.
///
/// A transaction can be used as an [`Executor`] when performing queries:
/// ```rust,no_run
/// # use sqlx_core::acquire::Acquire;
/// # async fn example() -> sqlx::Result<()> {
/// # let id = 1;
/// # let mut conn: sqlx::PgConnection = unimplemented!();
/// let mut tx = conn.begin().await?;
///
/// let result = sqlx::query("DELETE FROM \"testcases\" WHERE id = $1")
/// .bind(id)
/// .execute(&mut *tx)
/// .await?
/// .rows_affected();
///
/// tx.commit().await
/// # }
/// ```
/// [`Executor`]: crate::executor::Executor
/// [`Connection::begin`]: crate::connection::Connection::begin()
/// [`Pool::begin`]: crate::pool::Pool::begin()
/// [`commit`]: Self::commit()