Skip to main content

sentinel_driver/connection/
transaction_impl.rs

1use super::{notify, Connection, Result, TransactionConfig};
2
3impl Connection {
4    /// Begin a transaction with default settings.
5    pub async fn begin(&mut self) -> Result<()> {
6        self.begin_with(TransactionConfig::new()).await
7    }
8
9    /// Begin a transaction with custom settings.
10    pub async fn begin_with(&mut self, config: TransactionConfig) -> Result<()> {
11        self.simple_query(&config.begin_sql()).await?;
12        Ok(())
13    }
14
15    /// Commit the current transaction.
16    pub async fn commit(&mut self) -> Result<()> {
17        self.simple_query("COMMIT").await?;
18        Ok(())
19    }
20
21    /// Rollback the current transaction.
22    pub async fn rollback(&mut self) -> Result<()> {
23        self.simple_query("ROLLBACK").await?;
24        Ok(())
25    }
26
27    /// Create a savepoint.
28    pub async fn savepoint(&mut self, name: &str) -> Result<()> {
29        self.simple_query(&format!("SAVEPOINT {}", notify::quote_identifier(name)))
30            .await?;
31        Ok(())
32    }
33
34    /// Rollback to a savepoint.
35    pub async fn rollback_to(&mut self, name: &str) -> Result<()> {
36        self.simple_query(&format!(
37            "ROLLBACK TO SAVEPOINT {}",
38            notify::quote_identifier(name)
39        ))
40        .await?;
41        Ok(())
42    }
43}