Skip to main content

sql_middleware/pool/connection/
mod.rs

1#[cfg(feature = "mssql")]
2mod mssql;
3#[cfg(feature = "postgres")]
4mod postgres;
5#[cfg(feature = "sqlite")]
6mod sqlite;
7#[cfg(feature = "turso")]
8mod turso;
9
10#[cfg(feature = "postgres")]
11use crate::postgres::typed::PgManager;
12#[cfg(any(feature = "postgres", feature = "mssql"))]
13use bb8::PooledConnection;
14#[cfg(feature = "mssql")]
15use bb8_tiberius::ConnectionManager;
16
17use super::types::MiddlewarePool;
18use crate::error::SqlMiddlewareDbError;
19#[cfg(feature = "sqlite")]
20use crate::sqlite::SqliteConnection;
21
22#[cfg(feature = "turso")]
23use ::turso::Connection as TursoConnection;
24
25pub enum MiddlewarePoolConnection {
26    #[cfg(feature = "postgres")]
27    Postgres {
28        client: PooledConnection<'static, PgManager>,
29        translate_placeholders: bool,
30    },
31    #[cfg(feature = "sqlite")]
32    Sqlite {
33        conn: Option<SqliteConnection>,
34        translate_placeholders: bool,
35    },
36    #[cfg(feature = "mssql")]
37    Mssql {
38        conn: PooledConnection<'static, ConnectionManager>,
39        translate_placeholders: bool,
40    },
41    #[cfg(feature = "turso")]
42    Turso {
43        conn: TursoConnection,
44        translate_placeholders: bool,
45    },
46}
47
48// Manual Debug implementation because some pool variants do not expose `Debug`
49impl std::fmt::Debug for MiddlewarePoolConnection {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        match self {
52            #[cfg(feature = "postgres")]
53            Self::Postgres { client, .. } => f.debug_tuple("Postgres").field(client).finish(),
54            #[cfg(feature = "sqlite")]
55            Self::Sqlite { conn, .. } => f.debug_tuple("Sqlite").field(conn).finish(),
56            #[cfg(feature = "mssql")]
57            Self::Mssql { .. } => f
58                .debug_tuple("Mssql")
59                .field(&"<TiberiusConnection>")
60                .finish(),
61            #[cfg(feature = "turso")]
62            Self::Turso { .. } => f.debug_tuple("Turso").field(&"<Connection>").finish(),
63        }
64    }
65}
66
67impl MiddlewarePool {
68    /// Get a connection from the pool
69    ///
70    /// # Errors
71    /// Returns `SqlMiddlewareDbError::PoolErrorPostgres` or `SqlMiddlewareDbError::PoolErrorSqlite` if the pool fails to provide a connection.
72    pub async fn get_connection(
73        pool: &MiddlewarePool,
74        translate_placeholders: bool,
75    ) -> Result<MiddlewarePoolConnection, SqlMiddlewareDbError> {
76        match pool {
77            #[cfg(feature = "postgres")]
78            MiddlewarePool::Postgres(pool) => {
79                postgres::get_connection(pool, translate_placeholders).await
80            }
81            #[cfg(feature = "sqlite")]
82            MiddlewarePool::Sqlite(pool) => {
83                sqlite::get_connection(pool, translate_placeholders).await
84            }
85            #[cfg(feature = "mssql")]
86            MiddlewarePool::Mssql(pool) => {
87                mssql::get_connection(pool, translate_placeholders).await
88            }
89            #[cfg(feature = "turso")]
90            MiddlewarePool::Turso(db) => turso::get_connection(db, translate_placeholders),
91            #[allow(unreachable_patterns)]
92            _ => Err(SqlMiddlewareDbError::Unimplemented(
93                "This database type is not enabled in the current build".to_string(),
94            )),
95        }
96    }
97}
98
99impl MiddlewarePoolConnection {
100    /// Pool-default translation toggle attached to this connection.
101    #[must_use]
102    pub fn translation_default(&self) -> bool {
103        match self {
104            #[cfg(feature = "postgres")]
105            MiddlewarePoolConnection::Postgres {
106                translate_placeholders,
107                ..
108            } => *translate_placeholders,
109            #[cfg(feature = "sqlite")]
110            MiddlewarePoolConnection::Sqlite {
111                translate_placeholders,
112                ..
113            } => *translate_placeholders,
114            #[cfg(feature = "mssql")]
115            MiddlewarePoolConnection::Mssql {
116                translate_placeholders,
117                ..
118            } => *translate_placeholders,
119            #[cfg(feature = "turso")]
120            MiddlewarePoolConnection::Turso {
121                translate_placeholders,
122                ..
123            } => *translate_placeholders,
124        }
125    }
126}