sql_middleware/sqlite/typed/
dml.rs

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