Skip to main content

we_trust_postgres/
transaction.rs

1use crate::connection::PostgresConnection;
2use tracing::info;
3use yykv_types::{DsError, DsValue};
4
5type Result<T> = std::result::Result<T, DsError>;
6
7pub struct PostgresTransaction {
8    connection: PostgresConnection,
9}
10
11impl PostgresTransaction {
12    pub fn new(connection: PostgresConnection) -> Self {
13        Self { connection }
14    }
15
16    pub async fn execute(&self, sql: &str, params: &[DsValue]) -> Result<u64> {
17        self.connection.execute(sql, params).await
18    }
19
20    pub async fn query(&self, sql: &str, params: &[DsValue]) -> Result<Vec<DsValue>> {
21        self.connection.query(sql, params).await
22    }
23
24    pub async fn commit(self) -> Result<()> {
25        info!("Postgres transaction committed");
26        Ok(())
27    }
28
29    pub async fn rollback(self) -> Result<()> {
30        info!("Postgres transaction rolled back");
31        Ok(())
32    }
33}