Skip to main content

sql_middleware/sqlite/typed/
dml.rs

1use std::sync::Arc;
2
3use crate::adapters::params::convert_params;
4use crate::middleware::{ConversionMode, RowValues, SqlMiddlewareDbError};
5
6use super::SqliteTypedConnection;
7use crate::sqlite::config::SqliteManager;
8use crate::sqlite::params::Params;
9
10impl SqliteTypedConnection<super::core::Idle> {
11    /// Auto-commit batch (BEGIN/COMMIT around it).
12    ///
13    /// # Errors
14    /// Returns `SqlMiddlewareDbError` if executing the batch fails.
15    pub async fn execute_batch(&mut self, sql: &str) -> Result<(), SqlMiddlewareDbError> {
16        let mut tx = super::core::begin_from_conn(self.take_conn()?).await?;
17        tx.execute_batch(sql).await?;
18        let mut idle = tx.commit().await?;
19        self.conn = idle.conn.take();
20        Ok(())
21    }
22
23    /// Auto-commit DML.
24    ///
25    /// # Errors
26    /// Returns `SqlMiddlewareDbError` if executing the DML fails.
27    pub async fn dml(
28        &mut self,
29        query: &str,
30        params: &[RowValues],
31    ) -> Result<usize, SqlMiddlewareDbError> {
32        let mut tx = super::core::begin_from_conn(self.take_conn()?).await?;
33        let rows = tx.dml(query, params).await?;
34        let mut idle = tx.commit().await?;
35        self.conn = idle.conn.take();
36        Ok(rows)
37    }
38}
39
40impl SqliteTypedConnection<super::core::InTx> {
41    /// Execute batch inside the open transaction.
42    ///
43    /// # Errors
44    /// Returns `SqlMiddlewareDbError` if executing the batch fails.
45    pub async fn execute_batch(&mut self, sql: &str) -> Result<(), SqlMiddlewareDbError> {
46        let sql_owned = sql.to_owned();
47        super::core::run_blocking(self.conn_handle()?, move |guard| {
48            guard
49                .execute_batch(&sql_owned)
50                .map_err(SqlMiddlewareDbError::SqliteError)
51        })
52        .await
53    }
54
55    /// Execute DML inside the open transaction.
56    ///
57    /// # Errors
58    /// Returns `SqlMiddlewareDbError` if executing the DML fails.
59    pub async fn dml(
60        &mut self,
61        query: &str,
62        params: &[RowValues],
63    ) -> Result<usize, SqlMiddlewareDbError> {
64        let converted = convert_params::<Params>(params, ConversionMode::Execute)?.0;
65        let sql_owned = query.to_owned();
66        super::core::run_blocking(self.conn_handle()?, move |guard| {
67            let mut stmt = guard
68                .prepare(&sql_owned)
69                .map_err(SqlMiddlewareDbError::SqliteError)?;
70            let refs: Vec<&dyn rusqlite::ToSql> = converted
71                .iter()
72                .map(|v| v as &dyn rusqlite::ToSql)
73                .collect();
74            stmt.execute(&refs[..])
75                .map_err(SqlMiddlewareDbError::SqliteError)
76        })
77        .await
78    }
79}
80
81/// Adapter for query builder dml (typed-sqlite target).
82///
83/// # Errors
84/// Returns `SqlMiddlewareDbError` if converting parameters or executing the statement fails.
85pub async fn dml(
86    conn: &mut bb8::PooledConnection<'_, SqliteManager>,
87    query: &str,
88    params: &[RowValues],
89) -> Result<usize, SqlMiddlewareDbError> {
90    let converted = convert_params::<Params>(params, ConversionMode::Execute)?.0;
91    let sql_owned = query.to_owned();
92    let handle = Arc::clone(&**conn);
93    super::core::run_blocking(handle, move |guard| {
94        let mut stmt = guard
95            .prepare(&sql_owned)
96            .map_err(SqlMiddlewareDbError::SqliteError)?;
97        let refs: Vec<&dyn rusqlite::ToSql> = converted
98            .iter()
99            .map(|v| v as &dyn rusqlite::ToSql)
100            .collect();
101        stmt.execute(&refs[..])
102            .map_err(SqlMiddlewareDbError::SqliteError)
103    })
104    .await
105}