sql_middleware/pool/
interaction.rs

1use super::connection::MiddlewarePoolConnection;
2use crate::error::SqlMiddlewareDbError;
3use crate::query::AnyConnWrapper;
4
5impl MiddlewarePoolConnection {
6    /// Interact with the connection asynchronously
7    ///
8    /// # Errors
9    /// Returns `SqlMiddlewareDbError::Unimplemented` for unsupported database types.
10    #[allow(unused_variables)]
11    pub async fn interact_async<F, Fut>(
12        &mut self,
13        func: F,
14    ) -> Result<Fut::Output, SqlMiddlewareDbError>
15    where
16        F: FnOnce(AnyConnWrapper<'_>) -> Fut + Send + 'static,
17        Fut: std::future::Future<Output = Result<(), SqlMiddlewareDbError>> + Send + 'static,
18    {
19        match self {
20            #[cfg(feature = "postgres")]
21            MiddlewarePoolConnection::Postgres { client: pg_obj, .. } => {
22                // Assuming PostgresObject dereferences to tokio_postgres::Client
23                let client: &mut tokio_postgres::Client = pg_obj.as_mut();
24                Ok(func(AnyConnWrapper::Postgres(client)).await)
25            }
26            #[cfg(feature = "mssql")]
27            MiddlewarePoolConnection::Mssql {
28                conn: mssql_obj, ..
29            } => {
30                // Get client from Object
31                let client = &mut **mssql_obj;
32                Ok(func(AnyConnWrapper::Mssql(client)).await)
33            }
34            #[cfg(feature = "libsql")]
35            MiddlewarePoolConnection::Libsql {
36                conn: libsql_obj, ..
37            } => Ok(func(AnyConnWrapper::Libsql(libsql_obj)).await),
38            #[cfg(feature = "sqlite")]
39            MiddlewarePoolConnection::Sqlite { .. } => Err(SqlMiddlewareDbError::Unimplemented(
40                "interact_async is not supported for SQLite; use interact_sync instead".to_string(),
41            )),
42            #[allow(unreachable_patterns)]
43            _ => Err(SqlMiddlewareDbError::Unimplemented(
44                "interact_async is not implemented for this database type".to_string(),
45            )),
46        }
47    }
48
49    /// Interact with the connection synchronously
50    ///
51    /// # Errors
52    /// Returns `SqlMiddlewareDbError::Unimplemented` for unsupported database types.
53    #[allow(unused_variables)]
54    pub async fn interact_sync<F, R>(&self, f: F) -> Result<R, SqlMiddlewareDbError>
55    where
56        F: FnOnce(AnyConnWrapper) -> R + Send + 'static,
57        R: Send + 'static,
58    {
59        match self {
60            #[cfg(feature = "sqlite")]
61            MiddlewarePoolConnection::Sqlite {
62                conn: sqlite_conn, ..
63            } => {
64                sqlite_conn
65                    .with_connection(move |conn| {
66                        let wrapper = AnyConnWrapper::Sqlite(conn);
67                        Ok(f(wrapper))
68                    })
69                    .await
70            }
71            #[cfg(feature = "postgres")]
72            MiddlewarePoolConnection::Postgres { .. } => Err(SqlMiddlewareDbError::Unimplemented(
73                "interact_sync is not supported for Postgres; use interact_async instead"
74                    .to_string(),
75            )),
76            #[cfg(feature = "mssql")]
77            MiddlewarePoolConnection::Mssql { .. } => Err(SqlMiddlewareDbError::Unimplemented(
78                "interact_sync is not supported for SQL Server; use interact_async instead"
79                    .to_string(),
80            )),
81            #[allow(unreachable_patterns)]
82            _ => Err(SqlMiddlewareDbError::Unimplemented(
83                "interact_sync is not implemented for this database type".to_string(),
84            )),
85        }
86    }
87}