sql_middleware/pool/
interaction.rs1use super::connection::MiddlewarePoolConnection;
2use crate::error::SqlMiddlewareDbError;
3use crate::query::AnyConnWrapper;
4
5impl MiddlewarePoolConnection {
6 #[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(pg_obj) => {
22 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(mssql_obj) => {
28 let client = &mut **mssql_obj;
30 Ok(func(AnyConnWrapper::Mssql(client)).await)
31 }
32 #[cfg(feature = "libsql")]
33 MiddlewarePoolConnection::Libsql(libsql_obj) => {
34 Ok(func(AnyConnWrapper::Libsql(libsql_obj)).await)
35 }
36 #[cfg(feature = "sqlite")]
37 MiddlewarePoolConnection::Sqlite(_) => Err(SqlMiddlewareDbError::Unimplemented(
38 "interact_async is not supported for SQLite; use interact_sync instead".to_string(),
39 )),
40 #[allow(unreachable_patterns)]
41 _ => Err(SqlMiddlewareDbError::Unimplemented(
42 "interact_async is not implemented for this database type".to_string(),
43 )),
44 }
45 }
46
47 #[allow(unused_variables)]
52 pub async fn interact_sync<F, R>(&self, f: F) -> Result<R, SqlMiddlewareDbError>
53 where
54 F: FnOnce(AnyConnWrapper) -> R + Send + 'static,
55 R: Send + 'static,
56 {
57 match self {
58 #[cfg(feature = "sqlite")]
59 MiddlewarePoolConnection::Sqlite(sqlite_obj) => {
60 sqlite_obj
62 .interact(move |conn| {
63 let wrapper = AnyConnWrapper::Sqlite(conn);
64 Ok(f(wrapper))
65 })
66 .await?
67 }
68 #[cfg(feature = "postgres")]
69 MiddlewarePoolConnection::Postgres(_) => Err(SqlMiddlewareDbError::Unimplemented(
70 "interact_sync is not supported for Postgres; use interact_async instead"
71 .to_string(),
72 )),
73 #[cfg(feature = "mssql")]
74 MiddlewarePoolConnection::Mssql(_) => Err(SqlMiddlewareDbError::Unimplemented(
75 "interact_sync is not supported for SQL Server; use interact_async instead"
76 .to_string(),
77 )),
78 #[allow(unreachable_patterns)]
79 _ => Err(SqlMiddlewareDbError::Unimplemented(
80 "interact_sync is not implemented for this database type".to_string(),
81 )),
82 }
83 }
84}