sql_middleware/pool/connection/
mod.rs

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