welds_sqlx_mssql/options/
connect.rs

1use crate::error::Error;
2use crate::{MssqlConnectOptions, MssqlConnection};
3use futures_core::future::BoxFuture;
4use log::LevelFilter;
5use percent_encoding::percent_decode_str;
6use sqlx_core::connection::ConnectOptions;
7use std::time::Duration;
8use url::Url;
9
10impl ConnectOptions for MssqlConnectOptions {
11    type Connection = MssqlConnection;
12
13    fn from_url(url: &Url) -> Result<Self, Error> {
14        let mut options = Self::new();
15
16        if let Some(host) = url.host_str() {
17            options = options.host(host);
18        }
19
20        if let Some(port) = url.port() {
21            options = options.port(port);
22        }
23
24        let username = url.username();
25        if !username.is_empty() {
26            options = options.username(
27                &*percent_decode_str(username)
28                    .decode_utf8()
29                    .map_err(Error::config)?,
30            );
31        }
32
33        if let Some(password) = url.password() {
34            options = options.password(
35                &*percent_decode_str(password)
36                    .decode_utf8()
37                    .map_err(Error::config)?,
38            );
39        }
40
41        let path = url.path().trim_start_matches('/');
42        if !path.is_empty() {
43            options = options.database(path);
44        }
45
46        Ok(options)
47    }
48
49    fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
50    where
51        Self::Connection: Sized,
52    {
53        Box::pin(MssqlConnection::establish(self))
54    }
55
56    fn log_statements(mut self, level: LevelFilter) -> Self {
57        self.log_settings.log_statements(level);
58        self
59    }
60
61    fn log_slow_statements(mut self, level: LevelFilter, duration: Duration) -> Self {
62        self.log_settings.log_slow_statements(level, duration);
63        self
64    }
65}