Skip to main content

sqlx_core_oldapi/any/connection/
mod.rs

1use futures_core::future::BoxFuture;
2
3use crate::any::{Any, AnyConnectOptions, AnyKind};
4use crate::connection::Connection;
5use crate::error::Error;
6
7#[cfg(feature = "postgres")]
8use crate::postgres;
9
10#[cfg(feature = "sqlite")]
11use crate::sqlite;
12
13#[cfg(feature = "mssql")]
14use crate::mssql;
15
16#[cfg(feature = "mysql")]
17use crate::mysql;
18
19#[cfg(feature = "odbc")]
20use crate::odbc;
21use crate::transaction::Transaction;
22
23mod establish;
24mod executor;
25
26/// A connection to _any_ SQLx database.
27///
28/// The database driver used is determined by the scheme
29/// of the connection url.
30///
31/// ```text
32/// postgres://postgres@localhost/test
33/// sqlite://a.sqlite
34/// ```
35#[derive(Debug)]
36pub struct AnyConnection(pub(super) AnyConnectionKind);
37
38#[derive(Debug)]
39// Used internally in `sqlx-macros`
40#[doc(hidden)]
41#[allow(clippy::large_enum_variant)]
42pub enum AnyConnectionKind {
43    #[cfg(feature = "postgres")]
44    Postgres(postgres::PgConnection),
45
46    #[cfg(feature = "mssql")]
47    Mssql(mssql::MssqlConnection),
48
49    #[cfg(feature = "mysql")]
50    MySql(mysql::MySqlConnection),
51
52    #[cfg(feature = "sqlite")]
53    Sqlite(sqlite::SqliteConnection),
54
55    #[cfg(feature = "odbc")]
56    Odbc(odbc::OdbcConnection),
57}
58
59impl AnyConnectionKind {
60    pub fn kind(&self) -> AnyKind {
61        match self {
62            #[cfg(feature = "postgres")]
63            AnyConnectionKind::Postgres(_) => AnyKind::Postgres,
64
65            #[cfg(feature = "mysql")]
66            AnyConnectionKind::MySql(_) => AnyKind::MySql,
67
68            #[cfg(feature = "sqlite")]
69            AnyConnectionKind::Sqlite(_) => AnyKind::Sqlite,
70
71            #[cfg(feature = "mssql")]
72            AnyConnectionKind::Mssql(_) => AnyKind::Mssql,
73
74            #[cfg(feature = "odbc")]
75            AnyConnectionKind::Odbc(_) => AnyKind::Odbc,
76        }
77    }
78}
79
80impl AnyConnection {
81    pub fn kind(&self) -> AnyKind {
82        self.0.kind()
83    }
84
85    // Used internally in `sqlx-macros`
86    #[doc(hidden)]
87    pub fn private_get_mut(&mut self) -> &mut AnyConnectionKind {
88        &mut self.0
89    }
90}
91
92macro_rules! delegate_to {
93    ($self:ident.$method:ident($($arg:ident),*)) => {
94        match &$self.0 {
95            #[cfg(feature = "postgres")]
96            AnyConnectionKind::Postgres(conn) => conn.$method($($arg),*),
97
98            #[cfg(feature = "mysql")]
99            AnyConnectionKind::MySql(conn) => conn.$method($($arg),*),
100
101            #[cfg(feature = "sqlite")]
102            AnyConnectionKind::Sqlite(conn) => conn.$method($($arg),*),
103
104            #[cfg(feature = "mssql")]
105            AnyConnectionKind::Mssql(conn) => conn.$method($($arg),*),
106
107            #[cfg(feature = "odbc")]
108            AnyConnectionKind::Odbc(conn) => conn.$method($($arg),*),
109        }
110    };
111}
112
113macro_rules! delegate_to_mut {
114    ($self:ident.$method:ident($($arg:ident),*)) => {
115        match &mut $self.0 {
116            #[cfg(feature = "postgres")]
117            AnyConnectionKind::Postgres(conn) => conn.$method($($arg),*),
118
119            #[cfg(feature = "mysql")]
120            AnyConnectionKind::MySql(conn) => conn.$method($($arg),*),
121
122            #[cfg(feature = "sqlite")]
123            AnyConnectionKind::Sqlite(conn) => conn.$method($($arg),*),
124
125            #[cfg(feature = "mssql")]
126            AnyConnectionKind::Mssql(conn) => conn.$method($($arg),*),
127
128            #[cfg(feature = "odbc")]
129            AnyConnectionKind::Odbc(conn) => conn.$method($($arg),*),
130        }
131    };
132}
133
134impl Connection for AnyConnection {
135    type Database = Any;
136
137    type Options = AnyConnectOptions;
138
139    fn close(self) -> BoxFuture<'static, Result<(), Error>> {
140        match self.0 {
141            #[cfg(feature = "postgres")]
142            AnyConnectionKind::Postgres(conn) => conn.close(),
143
144            #[cfg(feature = "mysql")]
145            AnyConnectionKind::MySql(conn) => conn.close(),
146
147            #[cfg(feature = "sqlite")]
148            AnyConnectionKind::Sqlite(conn) => conn.close(),
149
150            #[cfg(feature = "mssql")]
151            AnyConnectionKind::Mssql(conn) => conn.close(),
152
153            #[cfg(feature = "odbc")]
154            AnyConnectionKind::Odbc(conn) => conn.close(),
155        }
156    }
157
158    fn close_hard(self) -> BoxFuture<'static, Result<(), Error>> {
159        match self.0 {
160            #[cfg(feature = "postgres")]
161            AnyConnectionKind::Postgres(conn) => conn.close_hard(),
162
163            #[cfg(feature = "mysql")]
164            AnyConnectionKind::MySql(conn) => conn.close_hard(),
165
166            #[cfg(feature = "sqlite")]
167            AnyConnectionKind::Sqlite(conn) => conn.close_hard(),
168
169            #[cfg(feature = "mssql")]
170            AnyConnectionKind::Mssql(conn) => conn.close_hard(),
171
172            #[cfg(feature = "odbc")]
173            AnyConnectionKind::Odbc(conn) => conn.close_hard(),
174        }
175    }
176
177    fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>> {
178        delegate_to_mut!(self.ping())
179    }
180
181    fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
182    where
183        Self: Sized,
184    {
185        Transaction::begin(self)
186    }
187
188    fn cached_statements_size(&self) -> usize {
189        match &self.0 {
190            #[cfg(feature = "postgres")]
191            AnyConnectionKind::Postgres(conn) => conn.cached_statements_size(),
192
193            #[cfg(feature = "mysql")]
194            AnyConnectionKind::MySql(conn) => conn.cached_statements_size(),
195
196            #[cfg(feature = "sqlite")]
197            AnyConnectionKind::Sqlite(conn) => conn.cached_statements_size(),
198
199            // no cache
200            #[cfg(feature = "mssql")]
201            AnyConnectionKind::Mssql(_) => 0,
202
203            #[cfg(feature = "odbc")]
204            AnyConnectionKind::Odbc(conn) => conn.cached_statements_size(),
205        }
206    }
207
208    fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
209        match &mut self.0 {
210            #[cfg(feature = "postgres")]
211            AnyConnectionKind::Postgres(conn) => conn.clear_cached_statements(),
212
213            #[cfg(feature = "mysql")]
214            AnyConnectionKind::MySql(conn) => conn.clear_cached_statements(),
215
216            #[cfg(feature = "sqlite")]
217            AnyConnectionKind::Sqlite(conn) => conn.clear_cached_statements(),
218
219            // no cache
220            #[cfg(feature = "mssql")]
221            AnyConnectionKind::Mssql(_) => Box::pin(futures_util::future::ok(())),
222
223            #[cfg(feature = "odbc")]
224            AnyConnectionKind::Odbc(conn) => Box::pin(conn.clear_cached_statements()),
225        }
226    }
227
228    #[doc(hidden)]
229    fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> {
230        delegate_to_mut!(self.flush())
231    }
232
233    #[doc(hidden)]
234    fn should_flush(&self) -> bool {
235        delegate_to!(self.should_flush())
236    }
237
238    fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
239        match &mut self.0 {
240            #[cfg(feature = "postgres")]
241            AnyConnectionKind::Postgres(conn) => conn.dbms_name(),
242
243            #[cfg(feature = "mysql")]
244            AnyConnectionKind::MySql(conn) => conn.dbms_name(),
245
246            #[cfg(feature = "sqlite")]
247            AnyConnectionKind::Sqlite(conn) => conn.dbms_name(),
248
249            #[cfg(feature = "mssql")]
250            AnyConnectionKind::Mssql(conn) => conn.dbms_name(),
251
252            #[cfg(feature = "odbc")]
253            AnyConnectionKind::Odbc(conn) => conn.dbms_name(),
254        }
255    }
256}
257
258#[cfg(feature = "postgres")]
259impl From<postgres::PgConnection> for AnyConnection {
260    fn from(conn: postgres::PgConnection) -> Self {
261        AnyConnection(AnyConnectionKind::Postgres(conn))
262    }
263}
264
265#[cfg(feature = "mssql")]
266impl From<mssql::MssqlConnection> for AnyConnection {
267    fn from(conn: mssql::MssqlConnection) -> Self {
268        AnyConnection(AnyConnectionKind::Mssql(conn))
269    }
270}
271
272#[cfg(feature = "mysql")]
273impl From<mysql::MySqlConnection> for AnyConnection {
274    fn from(conn: mysql::MySqlConnection) -> Self {
275        AnyConnection(AnyConnectionKind::MySql(conn))
276    }
277}
278
279#[cfg(feature = "sqlite")]
280impl From<sqlite::SqliteConnection> for AnyConnection {
281    fn from(conn: sqlite::SqliteConnection) -> Self {
282        AnyConnection(AnyConnectionKind::Sqlite(conn))
283    }
284}
285
286#[cfg(feature = "odbc")]
287impl From<odbc::OdbcConnection> for AnyConnection {
288    fn from(conn: odbc::OdbcConnection) -> Self {
289        AnyConnection(AnyConnectionKind::Odbc(conn))
290    }
291}