sql_middleware/pool/
connection.rs

1#[cfg(feature = "postgres")]
2use deadpool_postgres::Object as PostgresObject;
3
4#[cfg(feature = "sqlite")]
5use deadpool_sqlite::Object as SqliteObject;
6
7#[cfg(feature = "libsql")]
8use deadpool_libsql::Object as LibsqlObject;
9#[cfg(feature = "turso")]
10use turso::Connection as TursoConnection;
11
12use super::types::MiddlewarePool;
13use crate::error::SqlMiddlewareDbError;
14
15pub enum MiddlewarePoolConnection {
16    #[cfg(feature = "postgres")]
17    Postgres(PostgresObject),
18    #[cfg(feature = "sqlite")]
19    Sqlite(SqliteObject),
20    #[cfg(feature = "mssql")]
21    Mssql(deadpool::managed::Object<deadpool_tiberius::Manager>),
22    #[cfg(feature = "libsql")]
23    Libsql(LibsqlObject),
24    #[cfg(feature = "turso")]
25    Turso(TursoConnection),
26}
27
28// Manual Debug implementation because deadpool_tiberius::Manager doesn't implement Debug
29impl std::fmt::Debug for MiddlewarePoolConnection {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        match self {
32            #[cfg(feature = "postgres")]
33            Self::Postgres(conn) => f.debug_tuple("Postgres").field(conn).finish(),
34            #[cfg(feature = "sqlite")]
35            Self::Sqlite(conn) => f.debug_tuple("Sqlite").field(conn).finish(),
36            #[cfg(feature = "mssql")]
37            Self::Mssql(_) => f
38                .debug_tuple("Mssql")
39                .field(&"<TiberiusConnection>")
40                .finish(),
41            #[cfg(feature = "libsql")]
42            Self::Libsql(conn) => f.debug_tuple("Libsql").field(conn).finish(),
43            #[cfg(feature = "turso")]
44            Self::Turso(_) => f.debug_tuple("Turso").field(&"<Connection>").finish(),
45        }
46    }
47}
48
49impl MiddlewarePool {
50    /// Get a connection from the pool
51    ///
52    /// # Errors
53    /// Returns `SqlMiddlewareDbError::PoolErrorPostgres` or `SqlMiddlewareDbError::PoolErrorSqlite` if the pool fails to provide a connection.
54    pub async fn get_connection(
55        pool: &MiddlewarePool,
56    ) -> Result<MiddlewarePoolConnection, SqlMiddlewareDbError> {
57        match pool {
58            #[cfg(feature = "postgres")]
59            MiddlewarePool::Postgres(pool) => {
60                let conn: PostgresObject = pool
61                    .get()
62                    .await
63                    .map_err(SqlMiddlewareDbError::PoolErrorPostgres)?;
64                Ok(MiddlewarePoolConnection::Postgres(conn))
65            }
66            #[cfg(feature = "sqlite")]
67            MiddlewarePool::Sqlite(pool) => {
68                let conn: SqliteObject = pool
69                    .get()
70                    .await
71                    .map_err(SqlMiddlewareDbError::PoolErrorSqlite)?;
72                Ok(MiddlewarePoolConnection::Sqlite(conn))
73            }
74            #[cfg(feature = "mssql")]
75            MiddlewarePool::Mssql(pool) => {
76                let conn = pool
77                    .get()
78                    .await
79                    .map_err(SqlMiddlewareDbError::PoolErrorMssql)?;
80                Ok(MiddlewarePoolConnection::Mssql(conn))
81            }
82            #[cfg(feature = "libsql")]
83            MiddlewarePool::Libsql(pool) => {
84                let conn: LibsqlObject = pool
85                    .get()
86                    .await
87                    .map_err(SqlMiddlewareDbError::PoolErrorLibsql)?;
88                Ok(MiddlewarePoolConnection::Libsql(conn))
89            }
90            #[cfg(feature = "turso")]
91            MiddlewarePool::Turso(db) => {
92                let conn: TursoConnection = db.connect().map_err(SqlMiddlewareDbError::from)?;
93                Ok(MiddlewarePoolConnection::Turso(conn))
94            }
95            #[allow(unreachable_patterns)]
96            _ => Err(SqlMiddlewareDbError::Unimplemented(
97                "This database type is not enabled in the current build".to_string(),
98            )),
99        }
100    }
101}