qail_pg/driver/
transaction.rs

1//! Transaction control methods for PostgreSQL connection.
2
3use super::{PgConnection, PgResult};
4
5impl PgConnection {
6    /// Begin a new transaction.
7    /// After calling this, all queries run within the transaction
8    /// until `commit()` or `rollback()` is called.
9    pub async fn begin_transaction(&mut self) -> PgResult<()> {
10        self.execute_simple("BEGIN").await
11    }
12
13    /// Commit the current transaction.
14    /// Makes all changes since `begin_transaction()` permanent.
15    pub async fn commit(&mut self) -> PgResult<()> {
16        self.execute_simple("COMMIT").await
17    }
18
19    /// Rollback the current transaction.
20    /// Discards all changes since `begin_transaction()`.
21    pub async fn rollback(&mut self) -> PgResult<()> {
22        self.execute_simple("ROLLBACK").await
23    }
24
25    /// Create a named savepoint within the current transaction.
26    /// Savepoints allow partial rollback within a transaction.
27    /// Use `rollback_to()` to return to this savepoint.
28    pub async fn savepoint(&mut self, name: &str) -> PgResult<()> {
29        self.execute_simple(&format!("SAVEPOINT {}", name)).await
30    }
31
32    /// Rollback to a previously created savepoint.
33    /// Discards all changes since the named savepoint was created,
34    /// but keeps the transaction open.
35    pub async fn rollback_to(&mut self, name: &str) -> PgResult<()> {
36        self.execute_simple(&format!("ROLLBACK TO SAVEPOINT {}", name))
37            .await
38    }
39
40    /// Release a savepoint (free resources, if no longer needed).
41    pub async fn release_savepoint(&mut self, name: &str) -> PgResult<()> {
42        self.execute_simple(&format!("RELEASE SAVEPOINT {}", name))
43            .await
44    }
45}