sql_middleware/pool/connection/
mod.rs1#[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", feature = "turso"))]
13use bb8::PooledConnection;
14#[cfg(feature = "mssql")]
15use bb8_tiberius::ConnectionManager;
16#[cfg(feature = "postgres")]
17use std::collections::HashMap;
18
19use super::types::MiddlewarePool;
20use crate::error::SqlMiddlewareDbError;
21#[cfg(feature = "sqlite")]
22use crate::sqlite::SqliteConnection;
23use crate::types::StatementCacheMode;
24
25#[cfg(feature = "turso")]
26use crate::turso::typed::TursoManager;
27
28pub enum MiddlewarePoolConnection {
29 #[cfg(feature = "postgres")]
30 Postgres {
31 client: PooledConnection<'static, PgManager>,
32 translate_placeholders: bool,
33 statement_cache_mode: StatementCacheMode,
34 prepared_statements: HashMap<String, tokio_postgres::Statement>,
35 },
36 #[cfg(feature = "sqlite")]
37 Sqlite {
38 conn: Option<SqliteConnection>,
39 translate_placeholders: bool,
40 statement_cache_mode: StatementCacheMode,
41 },
42 #[cfg(feature = "mssql")]
43 Mssql {
44 conn: PooledConnection<'static, ConnectionManager>,
45 translate_placeholders: bool,
46 statement_cache_mode: StatementCacheMode,
47 },
48 #[cfg(feature = "turso")]
49 Turso {
50 conn: PooledConnection<'static, TursoManager>,
51 translate_placeholders: bool,
52 statement_cache_mode: StatementCacheMode,
53 },
54}
55
56impl std::fmt::Debug for MiddlewarePoolConnection {
58 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59 match self {
60 #[cfg(feature = "postgres")]
61 Self::Postgres { client, .. } => f.debug_tuple("Postgres").field(client).finish(),
62 #[cfg(feature = "sqlite")]
63 Self::Sqlite { conn, .. } => f.debug_tuple("Sqlite").field(conn).finish(),
64 #[cfg(feature = "mssql")]
65 Self::Mssql { .. } => f
66 .debug_tuple("Mssql")
67 .field(&"<TiberiusConnection>")
68 .finish(),
69 #[cfg(feature = "turso")]
70 Self::Turso { .. } => f.debug_tuple("Turso").field(&"<Connection>").finish(),
71 }
72 }
73}
74
75impl MiddlewarePool {
76 pub async fn get_connection(
81 pool: &MiddlewarePool,
82 translate_placeholders: bool,
83 statement_cache_mode: StatementCacheMode,
84 ) -> Result<MiddlewarePoolConnection, SqlMiddlewareDbError> {
85 match pool {
86 #[cfg(feature = "postgres")]
87 MiddlewarePool::Postgres(pool) => {
88 postgres::get_connection(pool, translate_placeholders, statement_cache_mode).await
89 }
90 #[cfg(feature = "sqlite")]
91 MiddlewarePool::Sqlite(pool) => {
92 sqlite::get_connection(pool, translate_placeholders, statement_cache_mode).await
93 }
94 #[cfg(feature = "mssql")]
95 MiddlewarePool::Mssql(pool) => {
96 mssql::get_connection(pool, translate_placeholders, statement_cache_mode).await
97 }
98 #[cfg(feature = "turso")]
99 MiddlewarePool::Turso(pool) => {
100 turso::get_connection(pool, translate_placeholders, statement_cache_mode).await
101 }
102 #[allow(unreachable_patterns)]
103 _ => Err(SqlMiddlewareDbError::Unimplemented(
104 "This database type is not enabled in the current build".to_string(),
105 )),
106 }
107 }
108}
109
110impl MiddlewarePoolConnection {
111 #[must_use]
113 pub fn translation_default(&self) -> bool {
114 match self {
115 #[cfg(feature = "postgres")]
116 MiddlewarePoolConnection::Postgres {
117 translate_placeholders,
118 ..
119 } => *translate_placeholders,
120 #[cfg(feature = "sqlite")]
121 MiddlewarePoolConnection::Sqlite {
122 translate_placeholders,
123 ..
124 } => *translate_placeholders,
125 #[cfg(feature = "mssql")]
126 MiddlewarePoolConnection::Mssql {
127 translate_placeholders,
128 ..
129 } => *translate_placeholders,
130 #[cfg(feature = "turso")]
131 MiddlewarePoolConnection::Turso {
132 translate_placeholders,
133 ..
134 } => *translate_placeholders,
135 }
136 }
137
138 #[must_use]
140 pub fn statement_cache_mode_default(&self) -> StatementCacheMode {
141 match self {
142 #[cfg(feature = "postgres")]
143 MiddlewarePoolConnection::Postgres {
144 statement_cache_mode,
145 ..
146 } => *statement_cache_mode,
147 #[cfg(feature = "sqlite")]
148 MiddlewarePoolConnection::Sqlite {
149 statement_cache_mode,
150 ..
151 } => *statement_cache_mode,
152 #[cfg(feature = "mssql")]
153 MiddlewarePoolConnection::Mssql {
154 statement_cache_mode,
155 ..
156 } => *statement_cache_mode,
157 #[cfg(feature = "turso")]
158 MiddlewarePoolConnection::Turso {
159 statement_cache_mode,
160 ..
161 } => *statement_cache_mode,
162 }
163 }
164}