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