sqlx_core/any/connection/
mod.rs

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