Skip to main content

sql_middleware/executor/
dispatch.rs

1use crate::error::SqlMiddlewareDbError;
2use crate::pool::MiddlewarePoolConnection;
3use crate::query_builder::QueryBuilder;
4use crate::results::ResultSet;
5use crate::types::RowValues;
6
7#[cfg(feature = "mssql")]
8use crate::mssql;
9#[cfg(feature = "postgres")]
10use crate::postgres;
11#[cfg(feature = "sqlite")]
12use crate::sqlite;
13#[cfg(feature = "turso")]
14use crate::turso;
15
16use super::targets::{BatchTarget, QueryTarget};
17
18/// Execute a batch against either a connection or a transaction.
19///
20/// # Errors
21/// Returns an error propagated from the underlying backend execution or transaction context.
22pub async fn execute_batch(
23    target: impl Into<BatchTarget<'_>>,
24    query: &str,
25) -> Result<(), SqlMiddlewareDbError> {
26    match target.into() {
27        BatchTarget::Connection(conn) => conn.execute_batch(query).await,
28        #[cfg(feature = "postgres")]
29        BatchTarget::PostgresTx(tx) => tx.execute_batch(query).await,
30        #[cfg(feature = "mssql")]
31        BatchTarget::MssqlTx(tx) => tx.execute_batch(query).await,
32        #[cfg(feature = "turso")]
33        BatchTarget::TursoTx(tx) => tx.execute_batch(query).await,
34        #[cfg(feature = "turso")]
35        BatchTarget::TypedTurso { conn } => {
36            crate::typed_turso::dml(conn, query, &[]).await?;
37            Ok(())
38        }
39        #[cfg(feature = "turso")]
40        BatchTarget::TypedTursoTx { conn } => {
41            crate::typed_turso::dml(conn, query, &[]).await?;
42            Ok(())
43        }
44    }
45}
46
47/// Start a fluent builder for either a connection or a transaction.
48pub fn query<'a>(target: impl Into<QueryTarget<'a>>, sql: &'a str) -> QueryBuilder<'a, 'a> {
49    QueryBuilder::new_target(target.into(), sql)
50}
51
52pub(crate) async fn execute_select_dispatch(
53    conn: &mut MiddlewarePoolConnection,
54    query: &str,
55    params: &[RowValues],
56) -> Result<ResultSet, SqlMiddlewareDbError> {
57    match conn {
58        #[cfg(feature = "postgres")]
59        MiddlewarePoolConnection::Postgres {
60            client: pg_client, ..
61        } => postgres::execute_query_on_client(pg_client, query, params).await,
62        #[cfg(feature = "sqlite")]
63        MiddlewarePoolConnection::Sqlite { .. } => {
64            let sqlite_client = conn.sqlite_conn_mut()?;
65            sqlite::execute_select(sqlite_client, query, params).await
66        }
67        #[cfg(feature = "mssql")]
68        MiddlewarePoolConnection::Mssql {
69            conn: mssql_client, ..
70        } => mssql::execute_select(mssql_client, query, params).await,
71        #[cfg(feature = "turso")]
72        MiddlewarePoolConnection::Turso {
73            conn: turso_conn, ..
74        } => turso::execute_select(turso_conn, query, params).await,
75        #[allow(unreachable_patterns)]
76        _ => Err(SqlMiddlewareDbError::Unimplemented(
77            "This database type is not enabled in the current build".to_string(),
78        )),
79    }
80}
81
82pub(crate) async fn execute_select_prepared_dispatch(
83    conn: &mut MiddlewarePoolConnection,
84    query: &str,
85    params: &[RowValues],
86) -> Result<ResultSet, SqlMiddlewareDbError> {
87    match conn {
88        #[cfg(feature = "postgres")]
89        MiddlewarePoolConnection::Postgres {
90            client: pg_client, ..
91        } => postgres::query::execute_query_prepared_on_client(pg_client, query, params).await,
92        #[cfg(feature = "sqlite")]
93        MiddlewarePoolConnection::Sqlite { .. } => {
94            let sqlite_client = conn.sqlite_conn_mut()?;
95            sqlite::execute_select(sqlite_client, query, params).await
96        }
97        #[cfg(feature = "mssql")]
98        MiddlewarePoolConnection::Mssql {
99            conn: mssql_client, ..
100        } => mssql::execute_select(mssql_client, query, params).await,
101        #[cfg(feature = "turso")]
102        MiddlewarePoolConnection::Turso {
103            conn: turso_conn, ..
104        } => turso::execute_select(turso_conn, query, params).await,
105        #[allow(unreachable_patterns)]
106        _ => Err(SqlMiddlewareDbError::Unimplemented(
107            "This database type is not enabled in the current build".to_string(),
108        )),
109    }
110}
111
112pub(crate) async fn execute_dml_dispatch(
113    conn: &mut MiddlewarePoolConnection,
114    query: &str,
115    params: &[RowValues],
116) -> Result<usize, SqlMiddlewareDbError> {
117    match conn {
118        #[cfg(feature = "postgres")]
119        MiddlewarePoolConnection::Postgres {
120            client: pg_client, ..
121        } => {
122            postgres::execute_dml_on_client(pg_client, query, params, "postgres execute error")
123                .await
124        }
125        #[cfg(feature = "sqlite")]
126        MiddlewarePoolConnection::Sqlite { .. } => {
127            let sqlite_client = conn.sqlite_conn_mut()?;
128            sqlite::execute_dml(sqlite_client, query, params).await
129        }
130        #[cfg(feature = "mssql")]
131        MiddlewarePoolConnection::Mssql {
132            conn: mssql_client, ..
133        } => mssql::execute_dml(mssql_client, query, params).await,
134        #[cfg(feature = "turso")]
135        MiddlewarePoolConnection::Turso {
136            conn: turso_conn, ..
137        } => turso::execute_dml(turso_conn, query, params).await,
138        #[allow(unreachable_patterns)]
139        _ => Err(SqlMiddlewareDbError::Unimplemented(
140            "This database type is not enabled in the current build".to_string(),
141        )),
142    }
143}
144
145pub(crate) async fn execute_dml_prepared_dispatch(
146    conn: &mut MiddlewarePoolConnection,
147    query: &str,
148    params: &[RowValues],
149) -> Result<usize, SqlMiddlewareDbError> {
150    match conn {
151        #[cfg(feature = "postgres")]
152        MiddlewarePoolConnection::Postgres {
153            client: pg_client, ..
154        } => postgres::query::execute_dml_prepared_on_client(pg_client, query, params).await,
155        #[cfg(feature = "sqlite")]
156        MiddlewarePoolConnection::Sqlite { .. } => {
157            let sqlite_client = conn.sqlite_conn_mut()?;
158            sqlite::execute_dml(sqlite_client, query, params).await
159        }
160        #[cfg(feature = "mssql")]
161        MiddlewarePoolConnection::Mssql {
162            conn: mssql_client, ..
163        } => mssql::execute_dml(mssql_client, query, params).await,
164        #[cfg(feature = "turso")]
165        MiddlewarePoolConnection::Turso {
166            conn: turso_conn, ..
167        } => turso::execute_dml(turso_conn, query, params).await,
168        #[allow(unreachable_patterns)]
169        _ => Err(SqlMiddlewareDbError::Unimplemented(
170            "This database type is not enabled in the current build".to_string(),
171        )),
172    }
173}