sqlx_core_oldapi/odbc/options/
mod.rs

1use crate::connection::{ConnectOptions, LogSettings};
2use crate::error::Error;
3use futures_core::future::BoxFuture;
4use log::LevelFilter;
5use std::fmt::{self, Debug, Formatter};
6use std::str::FromStr;
7use std::time::Duration;
8
9use crate::odbc::OdbcConnection;
10
11#[derive(Clone)]
12pub struct OdbcConnectOptions {
13    pub(crate) conn_str: String,
14    pub(crate) log_settings: LogSettings,
15}
16
17impl OdbcConnectOptions {
18    pub fn connection_string(&self) -> &str {
19        &self.conn_str
20    }
21}
22
23impl Debug for OdbcConnectOptions {
24    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
25        f.debug_struct("OdbcConnectOptions")
26            .field("conn_str", &"<redacted>")
27            .finish()
28    }
29}
30
31impl FromStr for OdbcConnectOptions {
32    type Err = Error;
33
34    fn from_str(s: &str) -> Result<Self, Self::Err> {
35        // Accept forms:
36        // - "odbc:DSN=Name;..." -> strip scheme
37        // - "odbc:Name" -> interpret as DSN
38        // - "DSN=Name;..." or full ODBC connection string
39        let mut t = s.trim();
40        if let Some(rest) = t.strip_prefix("odbc:") {
41            t = rest;
42        }
43        let conn_str = if t.contains('=') {
44            // Looks like an ODBC key=value connection string
45            t.to_string()
46        } else {
47            // Bare DSN name
48            format!("DSN={}", t)
49        };
50
51        Ok(Self {
52            conn_str,
53            log_settings: LogSettings::default(),
54        })
55    }
56}
57
58impl ConnectOptions for OdbcConnectOptions {
59    type Connection = OdbcConnection;
60
61    fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
62    where
63        Self::Connection: Sized,
64    {
65        Box::pin(OdbcConnection::establish(self))
66    }
67
68    fn log_statements(&mut self, level: LevelFilter) -> &mut Self {
69        self.log_settings.log_statements(level);
70        self
71    }
72
73    fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) -> &mut Self {
74        self.log_settings.log_slow_statements(level, duration);
75        self
76    }
77}