sqlx_core/any/
options.rs

1use crate::any::AnyConnection;
2use crate::connection::{ConnectOptions, LogSettings};
3use crate::error::Error;
4use futures_core::future::BoxFuture;
5use log::LevelFilter;
6use std::str::FromStr;
7use std::time::Duration;
8use url::Url;
9
10/// Opaque options for connecting to a database. These may only be constructed by parsing from
11/// a connection url.
12///
13/// ```text
14/// postgres://postgres:password@localhost/database
15/// mysql://root:password@localhost/database
16/// ```
17#[derive(Debug, Clone)]
18#[non_exhaustive]
19pub struct AnyConnectOptions {
20    pub database_url: Url,
21    pub log_settings: LogSettings,
22}
23impl FromStr for AnyConnectOptions {
24    type Err = Error;
25
26    fn from_str(url: &str) -> Result<Self, Self::Err> {
27        Ok(AnyConnectOptions {
28            database_url: url
29                .parse::<Url>()
30                .map_err(|e| Error::Configuration(e.into()))?,
31            log_settings: LogSettings::default(),
32        })
33    }
34}
35
36impl ConnectOptions for AnyConnectOptions {
37    type Connection = AnyConnection;
38
39    fn from_url(url: &Url) -> Result<Self, Error> {
40        Ok(AnyConnectOptions {
41            database_url: url.clone(),
42            log_settings: LogSettings::default(),
43        })
44    }
45
46    fn to_url_lossy(&self) -> Url {
47        self.database_url.clone()
48    }
49
50    #[inline]
51    fn connect(&self) -> BoxFuture<'_, Result<AnyConnection, Error>> {
52        AnyConnection::connect(self)
53    }
54
55    fn log_statements(mut self, level: LevelFilter) -> Self {
56        self.log_settings.statements_level = level;
57        self
58    }
59
60    fn log_slow_statements(mut self, level: LevelFilter, duration: Duration) -> Self {
61        self.log_settings.slow_statements_level = level;
62        self.log_settings.slow_statements_duration = duration;
63        self
64    }
65}