mysql_connector/connection/
options.rs

1use {
2    super::types::AuthPlugin,
3    crate::{bitflags::CapabilityFlags, Stream},
4    std::{fmt, time::Duration},
5};
6
7pub trait ConnectionOptionsTrait: fmt::Debug + Send + Sync {
8    fn user(&self) -> &str;
9    fn password(&self) -> &str;
10    fn db_name(&self) -> Option<&str>;
11    fn max_allowed_packet(&self) -> Option<usize>;
12    fn timeout(&self) -> Duration;
13    fn allow_cleartext_password(&self) -> bool;
14    fn auth_plugin(&self) -> Option<AuthPlugin>;
15    #[cfg(feature = "caching-sha2-password")]
16    #[cfg_attr(doc, doc(cfg(feature = "caching-sha2-password")))]
17    fn server_key(&self) -> Option<std::sync::Arc<crate::PublicKey>>;
18    #[cfg(not(feature = "time"))]
19    #[cfg_attr(doc, doc(cfg(not(feature = "time"))))]
20    fn sleep(&self) -> Option<&'static (dyn Fn(Duration) -> crate::TimeoutFuture + Send + Sync)>;
21    fn get_capabilities(&self) -> CapabilityFlags;
22}
23
24pub struct ConnectionOptions<T: Stream> {
25    pub user: String,
26    pub password: String,
27    pub db_name: Option<String>,
28    pub connection: T::Options,
29    pub max_allowed_packet: Option<usize>,
30    pub timeout: Duration,
31    pub allow_cleartext_password: bool,
32    /// Ignore auth plugin specified in handshake and start authentication using this plugin.
33    pub auth_plugin: Option<AuthPlugin>,
34    #[cfg(feature = "caching-sha2-password")]
35    #[cfg_attr(doc, doc(cfg(feature = "caching-sha2-password")))]
36    pub server_key: Option<std::sync::Arc<crate::PublicKey>>,
37    #[cfg(not(feature = "time"))]
38    #[cfg_attr(doc, doc(cfg(not(feature = "time"))))]
39    pub sleep: Option<&'static (dyn Fn(Duration) -> crate::TimeoutFuture + Send + Sync)>,
40}
41
42impl<T: Stream> ConnectionOptionsTrait for ConnectionOptions<T> {
43    fn user(&self) -> &str {
44        &self.user
45    }
46
47    fn password(&self) -> &str {
48        &self.password
49    }
50
51    fn db_name(&self) -> Option<&str> {
52        self.db_name.as_deref()
53    }
54
55    fn max_allowed_packet(&self) -> Option<usize> {
56        self.max_allowed_packet
57    }
58
59    fn timeout(&self) -> Duration {
60        self.timeout
61    }
62
63    fn allow_cleartext_password(&self) -> bool {
64        self.allow_cleartext_password
65    }
66
67    fn auth_plugin(&self) -> Option<AuthPlugin> {
68        self.auth_plugin
69    }
70
71    #[cfg(feature = "caching-sha2-password")]
72    fn server_key(&self) -> Option<std::sync::Arc<crate::PublicKey>> {
73        self.server_key.clone()
74    }
75
76    #[cfg(not(feature = "time"))]
77    fn sleep(&self) -> Option<&'static (dyn Fn(Duration) -> crate::TimeoutFuture + Send + Sync)> {
78        self.sleep
79    }
80
81    fn get_capabilities(&self) -> CapabilityFlags {
82        let mut out = CapabilityFlags::PROTOCOL_41
83            | CapabilityFlags::SECURE_CONNECTION
84            | CapabilityFlags::TRANSACTIONS
85            | CapabilityFlags::PS_MULTI_RESULTS
86            | CapabilityFlags::DEPRECATE_EOF
87            | CapabilityFlags::PLUGIN_AUTH;
88
89        if self.db_name.is_some() {
90            out |= CapabilityFlags::CONNECT_WITH_DB;
91        }
92
93        out
94    }
95}
96
97impl<T: Stream> Default for ConnectionOptions<T> {
98    fn default() -> Self {
99        Self {
100            user: String::new(),
101            password: String::new(),
102            db_name: None,
103            connection: Default::default(),
104            max_allowed_packet: None,
105            timeout: Duration::from_secs(10),
106            allow_cleartext_password: false,
107            #[cfg(feature = "caching-sha2-password")]
108            auth_plugin: Some(AuthPlugin::Sha2),
109            #[cfg(not(feature = "caching-sha2-password"))]
110            auth_plugin: None,
111            #[cfg(feature = "caching-sha2-password")]
112            server_key: None,
113            #[cfg(not(feature = "time"))]
114            sleep: None,
115        }
116    }
117}
118
119impl<T: Stream> fmt::Debug for ConnectionOptions<T> {
120    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121        let mut debug = f.debug_struct("ConnectionOptions");
122        debug
123            .field("user", &self.user)
124            .field("password", &self.password)
125            .field("db_name", &self.db_name)
126            .field("connection", &self.connection)
127            .field("max_allowed_packet", &self.max_allowed_packet)
128            .field("timeout", &self.timeout)
129            .field("allow_cleartext_password", &self.allow_cleartext_password)
130            .field("auth_plugin", &self.auth_plugin);
131        #[cfg(feature = "caching-sha2-password")]
132        debug.field("server_key", &self.server_key);
133        debug.finish()
134    }
135}