sql_middleware/pool/
interaction.rs1use super::connection::MiddlewarePoolConnection;
2use crate::error::SqlMiddlewareDbError;
3use crate::pool::AnyConnWrapper;
4
5impl MiddlewarePoolConnection {
6 #[allow(unused_variables)]
15 pub async fn interact_async<F, Fut>(
16 &mut self,
17 func: F,
18 ) -> Result<Fut::Output, SqlMiddlewareDbError>
19 where
20 F: FnOnce(AnyConnWrapper<'_>) -> Fut + Send + 'static,
21 Fut: std::future::Future<Output = Result<(), SqlMiddlewareDbError>> + Send + 'static,
22 {
23 match self {
24 #[cfg(feature = "postgres")]
25 MiddlewarePoolConnection::Postgres { client: pg_obj, .. } => {
26 let client: &mut tokio_postgres::Client = pg_obj;
28 Ok(func(AnyConnWrapper::Postgres(client)).await)
29 }
30 #[cfg(feature = "mssql")]
31 MiddlewarePoolConnection::Mssql {
32 conn: mssql_obj, ..
33 } => {
34 let client = &mut **mssql_obj;
36 Ok(func(AnyConnWrapper::Mssql(client)).await)
37 }
38 #[cfg(feature = "libsql")]
39 MiddlewarePoolConnection::Libsql {
40 conn: libsql_obj, ..
41 } => Ok(func(AnyConnWrapper::Libsql(libsql_obj)).await),
42 #[cfg(feature = "sqlite")]
43 MiddlewarePoolConnection::Sqlite { .. } => Err(SqlMiddlewareDbError::Unimplemented(
44 "interact_async is not supported for SQLite; use interact_sync instead".to_string(),
45 )),
46 #[allow(unreachable_patterns)]
47 _ => Err(SqlMiddlewareDbError::Unimplemented(
48 "interact_async is not implemented for this database type".to_string(),
49 )),
50 }
51 }
52
53 #[allow(unused_variables)]
58 pub async fn interact_sync<F, R>(&self, f: F) -> Result<R, SqlMiddlewareDbError>
59 where
60 F: FnOnce(AnyConnWrapper) -> R + Send + 'static,
61 R: Send + 'static,
62 {
63 match self {
64 #[cfg(feature = "sqlite")]
65 MiddlewarePoolConnection::Sqlite {
66 conn: sqlite_conn, ..
67 } => {
68 let conn = sqlite_conn.as_ref().ok_or_else(|| {
69 SqlMiddlewareDbError::ExecutionError(
70 "SQLite connection already taken from pool wrapper".into(),
71 )
72 })?;
73
74 conn.with_connection(move |conn| {
75 let wrapper = AnyConnWrapper::Sqlite(conn);
76 Ok(f(wrapper))
77 })
78 .await
79 }
80 #[cfg(feature = "postgres")]
81 MiddlewarePoolConnection::Postgres { .. } => Err(SqlMiddlewareDbError::Unimplemented(
82 "interact_sync is not supported for Postgres; use interact_async instead"
83 .to_string(),
84 )),
85 #[cfg(feature = "mssql")]
86 MiddlewarePoolConnection::Mssql { .. } => Err(SqlMiddlewareDbError::Unimplemented(
87 "interact_sync is not supported for SQL Server; use interact_async instead"
88 .to_string(),
89 )),
90 #[allow(unreachable_patterns)]
91 _ => Err(SqlMiddlewareDbError::Unimplemented(
92 "interact_sync is not implemented for this database type".to_string(),
93 )),
94 }
95 }
96}