sql_middleware/postgres/
executor.rs1use super::transaction::{Tx, begin_transaction};
2use crate::middleware::{ResultSet, RowValues, SqlMiddlewareDbError};
3use std::ops::DerefMut;
4use tokio_postgres::Client;
5
6pub async fn execute_batch<C>(pg_client: &mut C, query: &str) -> Result<(), SqlMiddlewareDbError>
11where
12 C: DerefMut<Target = Client>,
13{
14 let tx: Tx<'_> = begin_transaction(pg_client).await?;
15 tx.execute_batch(query).await?;
16 tx.commit().await?;
17
18 Ok(())
19}
20
21pub async fn execute_select<C>(
26 pg_client: &mut C,
27 query: &str,
28 params: &[RowValues],
29) -> Result<ResultSet, SqlMiddlewareDbError>
30where
31 C: DerefMut<Target = Client>,
32{
33 let tx: Tx<'_> = begin_transaction(pg_client).await?;
34 let prepared = tx.prepare(query).await?;
35 let result_set = tx.query_prepared(&prepared, params).await?;
36 tx.commit().await?;
37 Ok(result_set)
38}
39
40pub async fn execute_dml<C>(
45 pg_client: &mut C,
46 query: &str,
47 params: &[RowValues],
48) -> Result<usize, SqlMiddlewareDbError>
49where
50 C: DerefMut<Target = Client>,
51{
52 let tx: Tx<'_> = begin_transaction(pg_client).await?;
53 let prepared = tx.prepare(query).await?;
54 let rows = tx.execute_prepared(&prepared, params).await?;
55 tx.commit().await?;
56 Ok(rows)
57}