sqlx_core/any/connection/
mod.rs

1use futures_core::future::BoxFuture;
2
3use crate::any::{Any, AnyConnectOptions};
4use crate::connection::{ConnectOptions, Connection};
5use crate::error::Error;
6
7use crate::database::Database;
8pub use backend::AnyConnectionBackend;
9
10use crate::transaction::Transaction;
11
12mod backend;
13mod executor;
14
15/// A connection to _any_ SQLx database.
16///
17/// The database driver used is determined by the scheme
18/// of the connection url.
19///
20/// ```text
21/// postgres://postgres@localhost/test
22/// sqlite://a.sqlite
23/// ```
24#[derive(Debug)]
25pub struct AnyConnection {
26    pub(crate) backend: Box<dyn AnyConnectionBackend>,
27}
28
29impl AnyConnection {
30    /// Returns the name of the database backend in use (e.g. PostgreSQL, MySQL, SQLite, etc.)
31    pub fn backend_name(&self) -> &str {
32        self.backend.name()
33    }
34
35    pub(crate) fn connect(options: &AnyConnectOptions) -> BoxFuture<'_, crate::Result<Self>> {
36        Box::pin(async {
37            let driver = crate::any::driver::from_url(&options.database_url)?;
38            (driver.connect)(options).await
39        })
40    }
41
42    pub(crate) fn connect_with_db<DB: Database>(
43        options: &AnyConnectOptions,
44    ) -> BoxFuture<'_, crate::Result<Self>>
45    where
46        DB::Connection: AnyConnectionBackend,
47        <DB::Connection as Connection>::Options:
48            for<'a> TryFrom<&'a AnyConnectOptions, Error = Error>,
49    {
50        let res = TryFrom::try_from(options);
51
52        Box::pin(async {
53            let options: <DB::Connection as Connection>::Options = res?;
54
55            Ok(AnyConnection {
56                backend: Box::new(options.connect().await?),
57            })
58        })
59    }
60
61    #[cfg(feature = "migrate")]
62    pub(crate) fn get_migrate(
63        &mut self,
64    ) -> crate::Result<&mut (dyn crate::migrate::Migrate + Send + 'static)> {
65        self.backend.as_migrate()
66    }
67}
68
69impl Connection for AnyConnection {
70    type Database = Any;
71
72    type Options = AnyConnectOptions;
73
74    fn close(self) -> BoxFuture<'static, Result<(), Error>> {
75        self.backend.close()
76    }
77
78    fn close_hard(self) -> BoxFuture<'static, Result<(), Error>> {
79        self.backend.close()
80    }
81
82    fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>> {
83        self.backend.ping()
84    }
85
86    fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
87    where
88        Self: Sized,
89    {
90        Transaction::begin(self)
91    }
92
93    fn cached_statements_size(&self) -> usize {
94        self.backend.cached_statements_size()
95    }
96
97    fn clear_cached_statements(&mut self) -> BoxFuture<'_, crate::Result<()>> {
98        self.backend.clear_cached_statements()
99    }
100
101    fn shrink_buffers(&mut self) {
102        self.backend.shrink_buffers()
103    }
104
105    #[doc(hidden)]
106    fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> {
107        self.backend.flush()
108    }
109
110    #[doc(hidden)]
111    fn should_flush(&self) -> bool {
112        self.backend.should_flush()
113    }
114}