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            // no cache
204            #[cfg(feature = "odbc")]
205            AnyConnectionKind::Odbc(_) => 0,
206        }
207    }
208
209    fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
210        match &mut self.0 {
211            #[cfg(feature = "postgres")]
212            AnyConnectionKind::Postgres(conn) => conn.clear_cached_statements(),
213
214            #[cfg(feature = "mysql")]
215            AnyConnectionKind::MySql(conn) => conn.clear_cached_statements(),
216
217            #[cfg(feature = "sqlite")]
218            AnyConnectionKind::Sqlite(conn) => conn.clear_cached_statements(),
219
220            // no cache
221            #[cfg(feature = "mssql")]
222            AnyConnectionKind::Mssql(_) => Box::pin(futures_util::future::ok(())),
223
224            // no cache
225            #[cfg(feature = "odbc")]
226            AnyConnectionKind::Odbc(_) => Box::pin(futures_util::future::ok(())),
227        }
228    }
229
230    #[doc(hidden)]
231    fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> {
232        delegate_to_mut!(self.flush())
233    }
234
235    #[doc(hidden)]
236    fn should_flush(&self) -> bool {
237        delegate_to!(self.should_flush())
238    }
239
240    fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
241        match &mut self.0 {
242            #[cfg(feature = "postgres")]
243            AnyConnectionKind::Postgres(conn) => conn.dbms_name(),
244
245            #[cfg(feature = "mysql")]
246            AnyConnectionKind::MySql(conn) => conn.dbms_name(),
247
248            #[cfg(feature = "sqlite")]
249            AnyConnectionKind::Sqlite(conn) => conn.dbms_name(),
250
251            #[cfg(feature = "mssql")]
252            AnyConnectionKind::Mssql(conn) => conn.dbms_name(),
253
254            #[cfg(feature = "odbc")]
255            AnyConnectionKind::Odbc(conn) => conn.dbms_name(),
256        }
257    }
258}
259
260#[cfg(feature = "postgres")]
261impl From<postgres::PgConnection> for AnyConnection {
262    fn from(conn: postgres::PgConnection) -> Self {
263        AnyConnection(AnyConnectionKind::Postgres(conn))
264    }
265}
266
267#[cfg(feature = "mssql")]
268impl From<mssql::MssqlConnection> for AnyConnection {
269    fn from(conn: mssql::MssqlConnection) -> Self {
270        AnyConnection(AnyConnectionKind::Mssql(conn))
271    }
272}
273
274#[cfg(feature = "mysql")]
275impl From<mysql::MySqlConnection> for AnyConnection {
276    fn from(conn: mysql::MySqlConnection) -> Self {
277        AnyConnection(AnyConnectionKind::MySql(conn))
278    }
279}
280
281#[cfg(feature = "sqlite")]
282impl From<sqlite::SqliteConnection> for AnyConnection {
283    fn from(conn: sqlite::SqliteConnection) -> Self {
284        AnyConnection(AnyConnectionKind::Sqlite(conn))
285    }
286}
287
288#[cfg(feature = "odbc")]
289impl From<odbc::OdbcConnection> for AnyConnection {
290    fn from(conn: odbc::OdbcConnection) -> Self {
291        AnyConnection(AnyConnectionKind::Odbc(conn))
292    }
293}