sql_middleware/pool/connection/
mod.rs

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