sql_middleware/postgres/typed/
dml.rs

1use bb8::PooledConnection;
2
3use crate::middleware::{RowValues, SqlMiddlewareDbError};
4use crate::postgres::query::execute_dml_on_client;
5
6use super::{PgConnection, PgManager};
7
8impl PgConnection<super::core::Idle> {
9    /// Auto-commit batch (BEGIN/COMMIT around it).
10    ///
11    /// # Errors
12    /// Returns `SqlMiddlewareDbError` if the batch execution fails.
13    pub async fn execute_batch(&mut self, sql: &str) -> Result<(), SqlMiddlewareDbError> {
14        crate::postgres::executor::execute_batch(self.conn_mut(), sql).await
15    }
16
17    /// Auto-commit DML.
18    ///
19    /// # Errors
20    /// Returns `SqlMiddlewareDbError` if executing the DML fails.
21    pub async fn dml(
22        &mut self,
23        query: &str,
24        params: &[RowValues],
25    ) -> Result<usize, SqlMiddlewareDbError> {
26        crate::postgres::executor::execute_dml(self.conn_mut(), query, params).await
27    }
28}
29
30impl PgConnection<super::core::InTx> {
31    /// Execute batch inside the open transaction.
32    ///
33    /// # Errors
34    /// Returns `SqlMiddlewareDbError` if executing the batch fails.
35    pub async fn execute_batch(&mut self, sql: &str) -> Result<(), SqlMiddlewareDbError> {
36        self.conn_mut().batch_execute(sql).await.map_err(|e| {
37            SqlMiddlewareDbError::ExecutionError(format!("postgres tx batch error: {e}"))
38        })
39    }
40
41    /// Execute DML inside the open transaction.
42    ///
43    /// # Errors
44    /// Returns `SqlMiddlewareDbError` if executing the DML fails.
45    pub async fn dml(
46        &mut self,
47        query: &str,
48        params: &[RowValues],
49    ) -> Result<usize, SqlMiddlewareDbError> {
50        execute_dml_on_client(self.conn_mut(), query, params, "postgres tx execute error").await
51    }
52}
53
54/// Adapter for query builder dml (typed-postgres target).
55///
56/// # Errors
57/// Returns `SqlMiddlewareDbError` if executing the DML fails.
58pub async fn dml(
59    conn: &mut PooledConnection<'_, PgManager>,
60    query: &str,
61    params: &[RowValues],
62) -> Result<usize, SqlMiddlewareDbError> {
63    execute_dml_on_client(conn, query, params, "postgres dml error").await
64}