sql_middleware/
executor.rs

1use async_trait::async_trait;
2
3use crate::error::SqlMiddlewareDbError;
4use crate::pool::MiddlewarePoolConnection;
5use crate::results::ResultSet;
6use crate::types::RowValues;
7
8#[cfg(feature = "libsql")]
9use crate::libsql;
10#[cfg(feature = "mssql")]
11use crate::mssql;
12#[cfg(feature = "postgres")]
13use crate::postgres;
14#[cfg(feature = "sqlite")]
15use crate::sqlite;
16#[cfg(feature = "turso")]
17use crate::turso;
18
19#[async_trait]
20pub trait AsyncDatabaseExecutor {
21    /// Executes a batch of SQL queries (can be a mix of reads/writes) within a transaction. No parameters are supported.
22    async fn execute_batch(&mut self, query: &str) -> Result<(), SqlMiddlewareDbError>;
23
24    /// Executes a single SELECT statement and returns the result set.
25    async fn execute_select(
26        &mut self,
27        query: &str,
28        params: &[RowValues],
29    ) -> Result<ResultSet, SqlMiddlewareDbError>;
30
31    /// Executes a single DML statement (INSERT, UPDATE, DELETE, etc.) and returns the number of rows affected.
32    async fn execute_dml(
33        &mut self,
34        query: &str,
35        params: &[RowValues],
36    ) -> Result<usize, SqlMiddlewareDbError>;
37}
38
39#[async_trait]
40impl AsyncDatabaseExecutor for MiddlewarePoolConnection {
41    /// Executes a batch of SQL queries within a transaction by delegating to the specific database module.
42    async fn execute_batch(&mut self, query: &str) -> Result<(), SqlMiddlewareDbError> {
43        match self {
44            #[cfg(feature = "postgres")]
45            MiddlewarePoolConnection::Postgres(pg_client) => {
46                postgres::execute_batch(pg_client, query).await
47            }
48            #[cfg(feature = "sqlite")]
49            MiddlewarePoolConnection::Sqlite(sqlite_client) => {
50                sqlite::execute_batch(sqlite_client, query).await
51            }
52            #[cfg(feature = "mssql")]
53            MiddlewarePoolConnection::Mssql(mssql_client) => {
54                mssql::execute_batch(mssql_client, query).await
55            }
56            #[cfg(feature = "libsql")]
57            MiddlewarePoolConnection::Libsql(libsql_client) => {
58                libsql::execute_batch(libsql_client, query).await
59            }
60            #[cfg(feature = "turso")]
61            MiddlewarePoolConnection::Turso(turso_conn) => {
62                turso::execute_batch(turso_conn, query).await
63            }
64            #[allow(unreachable_patterns)]
65            _ => Err(SqlMiddlewareDbError::Unimplemented(
66                "This database type is not enabled in the current build".to_string(),
67            )),
68        }
69    }
70
71    async fn execute_select(
72        &mut self,
73        query: &str,
74        params: &[RowValues],
75    ) -> Result<ResultSet, SqlMiddlewareDbError> {
76        match self {
77            #[cfg(feature = "postgres")]
78            MiddlewarePoolConnection::Postgres(pg_client) => {
79                postgres::execute_select(pg_client, query, params).await
80            }
81            #[cfg(feature = "sqlite")]
82            MiddlewarePoolConnection::Sqlite(sqlite_client) => {
83                sqlite::execute_select(sqlite_client, query, params).await
84            }
85            #[cfg(feature = "mssql")]
86            MiddlewarePoolConnection::Mssql(mssql_client) => {
87                mssql::execute_select(mssql_client, query, params).await
88            }
89            #[cfg(feature = "libsql")]
90            MiddlewarePoolConnection::Libsql(libsql_client) => {
91                libsql::execute_select(libsql_client, query, params).await
92            }
93            #[cfg(feature = "turso")]
94            MiddlewarePoolConnection::Turso(turso_conn) => {
95                turso::execute_select(turso_conn, query, params).await
96            }
97            #[allow(unreachable_patterns)]
98            _ => Err(SqlMiddlewareDbError::Unimplemented(
99                "This database type is not enabled in the current build".to_string(),
100            )),
101        }
102    }
103
104    async fn execute_dml(
105        &mut self,
106        query: &str,
107        params: &[RowValues],
108    ) -> Result<usize, SqlMiddlewareDbError> {
109        match self {
110            #[cfg(feature = "postgres")]
111            MiddlewarePoolConnection::Postgres(pg_client) => {
112                postgres::execute_dml(pg_client, query, params).await
113            }
114            #[cfg(feature = "sqlite")]
115            MiddlewarePoolConnection::Sqlite(sqlite_client) => {
116                sqlite::execute_dml(sqlite_client, query, params).await
117            }
118            #[cfg(feature = "mssql")]
119            MiddlewarePoolConnection::Mssql(mssql_client) => {
120                mssql::execute_dml(mssql_client, query, params).await
121            }
122            #[cfg(feature = "libsql")]
123            MiddlewarePoolConnection::Libsql(libsql_client) => {
124                libsql::execute_dml(libsql_client, query, params).await
125            }
126            #[cfg(feature = "turso")]
127            MiddlewarePoolConnection::Turso(turso_conn) => {
128                turso::execute_dml(turso_conn, query, params).await
129            }
130            #[allow(unreachable_patterns)]
131            _ => Err(SqlMiddlewareDbError::Unimplemented(
132                "This database type is not enabled in the current build".to_string(),
133            )),
134        }
135    }
136}