mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-01-25 01:57:29 +00:00
* WIP rt refactors * refactor: break drivers out into separate crates also cleans up significant technical debt
61 lines
1.6 KiB
Rust
61 lines
1.6 KiB
Rust
use futures_core::future::BoxFuture;
|
|
|
|
use crate::error::Error;
|
|
use crate::executor::Executor;
|
|
|
|
use crate::{PgConnection, Postgres};
|
|
|
|
pub(crate) use sqlx_core::transaction::*;
|
|
|
|
/// Implementation of [`TransactionManager`] for PostgreSQL.
|
|
pub struct PgTransactionManager;
|
|
|
|
impl TransactionManager for PgTransactionManager {
|
|
type Database = Postgres;
|
|
|
|
fn begin(conn: &mut PgConnection) -> BoxFuture<'_, Result<(), Error>> {
|
|
Box::pin(async move {
|
|
conn.execute(&*begin_ansi_transaction_sql(conn.transaction_depth))
|
|
.await?;
|
|
|
|
conn.transaction_depth += 1;
|
|
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
fn commit(conn: &mut PgConnection) -> BoxFuture<'_, Result<(), Error>> {
|
|
Box::pin(async move {
|
|
if conn.transaction_depth > 0 {
|
|
conn.execute(&*commit_ansi_transaction_sql(conn.transaction_depth))
|
|
.await?;
|
|
|
|
conn.transaction_depth -= 1;
|
|
}
|
|
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
fn rollback(conn: &mut PgConnection) -> BoxFuture<'_, Result<(), Error>> {
|
|
Box::pin(async move {
|
|
if conn.transaction_depth > 0 {
|
|
conn.execute(&*rollback_ansi_transaction_sql(conn.transaction_depth))
|
|
.await?;
|
|
|
|
conn.transaction_depth -= 1;
|
|
}
|
|
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
fn start_rollback(conn: &mut PgConnection) {
|
|
if conn.transaction_depth > 0 {
|
|
conn.queue_simple_query(&rollback_ansi_transaction_sql(conn.transaction_depth));
|
|
|
|
conn.transaction_depth -= 1;
|
|
}
|
|
}
|
|
}
|