rbdc_mysql/options/mod.rs
1use std::path::{Path, PathBuf};
2use fastdate::offset_sec;
3
4mod connect;
5mod parse;
6mod ssl_mode;
7
8use rbdc::net::CertificateInput;
9pub use ssl_mode::MySqlSslMode;
10
11/// Options and flags which can be used to configure a MySQL connection.
12///
13/// A value of `MySqlConnectOptions` can be parsed from a connection URI,
14/// as described by [MySQL](https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-jdbc-url-format.html).
15///
16/// The generic format of the connection URL:
17///
18/// ```text
19/// mysql://[host][/database][?properties]
20/// ```
21///
22/// ## Properties
23///
24/// |Parameter|Default|Description|
25/// |---------|-------|-----------|
26/// | `ssl-mode` | `PREFERRED` | Determines whether or with what priority a secure SSL TCP/IP connection will be negotiated. See [`MySqlSslMode`]. |
27/// | `ssl-ca` | `None` | Sets the name of a file containing a list of trusted SSL Certificate Authorities. |
28/// | `statement-cache-capacity` | `100` | The maximum number of prepared statements stored in the cache. Set to `0` to disable. |
29/// | `socket` | `None` | Path to the unix domain socket, which will be used instead of TCP if set. |
30///
31/// ## offset_sec
32/// DateTime timezone offset sec
33#[derive(Debug, Clone)]
34pub struct MySqlConnectOptions {
35 pub(crate) host: String,
36 pub(crate) port: u16,
37 pub(crate) socket: Option<PathBuf>,
38 pub(crate) username: String,
39 pub(crate) password: Option<String>,
40 pub(crate) database: Option<String>,
41 pub(crate) ssl_mode: MySqlSslMode,
42 pub(crate) ssl_ca: Option<CertificateInput>,
43 pub(crate) statement_cache_capacity: usize,
44 pub(crate) charset: String,
45 pub(crate) collation: Option<String>,
46 pub(crate) offset_sec: i32,
47}
48
49impl Default for MySqlConnectOptions {
50 fn default() -> Self {
51 Self::new()
52 }
53}
54
55impl MySqlConnectOptions {
56 /// Creates a new, default set of options ready for configuration
57 pub fn new() -> Self {
58 Self {
59 port: 3306,
60 host: String::from("localhost"),
61 socket: None,
62 username: String::from("root"),
63 password: None,
64 database: None,
65 charset: String::from("utf8mb4"),
66 collation: None,
67 ssl_mode: MySqlSslMode::Disabled,
68 ssl_ca: None,
69 statement_cache_capacity: 100,
70 offset_sec: offset_sec(),
71 }
72 }
73
74 /// Sets the name of the host to connect to.
75 ///
76 /// The default behavior when the host is not specified,
77 /// is to connect to localhost.
78 pub fn host(mut self, host: &str) -> Self {
79 self.host = host.to_owned();
80 self
81 }
82
83 /// Sets the port to connect to at the server host.
84 ///
85 /// The default port for MySQL is `3306`.
86 pub fn port(mut self, port: u16) -> Self {
87 self.port = port;
88 self
89 }
90
91 /// Pass a path to a Unix socket. This changes the connection stream from
92 /// TCP to UDS.
93 ///
94 /// By default set to `None`.
95 pub fn socket(mut self, path: impl AsRef<Path>) -> Self {
96 self.socket = Some(path.as_ref().to_path_buf());
97 self
98 }
99
100 /// Sets the username to connect as.
101 pub fn username(mut self, username: &str) -> Self {
102 self.username = username.to_owned();
103 self
104 }
105
106 /// Sets the password to connect with.
107 pub fn password(mut self, password: &str) -> Self {
108 self.password = Some(password.to_owned());
109 self
110 }
111
112 /// Sets the database name.
113 pub fn database(mut self, database: &str) -> Self {
114 self.database = Some(database.to_owned());
115 self
116 }
117
118 /// Sets whether or with what priority a secure SSL TCP/IP connection will be negotiated
119 /// with the server.
120 ///
121 /// By default, the SSL mode is [`Preferred`](MySqlSslMode::Preferred), and the client will
122 /// first attempt an SSL connection but fallback to a non-SSL connection on failure.
123 ///
124 /// # Example
125 ///
126 /// ```rust
127 /// # use rbdc_mysql::options::{MySqlSslMode, MySqlConnectOptions};
128 /// let options = MySqlConnectOptions::new()
129 /// .ssl_mode(MySqlSslMode::Required);
130 /// ```
131 pub fn ssl_mode(mut self, mode: MySqlSslMode) -> Self {
132 self.ssl_mode = mode;
133 self
134 }
135
136 /// Sets the name of a file containing a list of trusted SSL Certificate Authorities.
137 ///
138 /// # Example
139 ///
140 /// ```rust
141 /// # use rbdc_mysql::options::{MySqlSslMode, MySqlConnectOptions};
142 /// let options = MySqlConnectOptions::new()
143 /// .ssl_mode(MySqlSslMode::VerifyCa)
144 /// .ssl_ca("path/to/ca.crt");
145 /// ```
146 pub fn ssl_ca(mut self, file_name: impl AsRef<Path>) -> Self {
147 self.ssl_ca = Some(CertificateInput::File(file_name.as_ref().to_owned()));
148 self
149 }
150
151 /// Sets PEM encoded list of trusted SSL Certificate Authorities.
152 ///
153 /// # Example
154 ///
155 /// ```rust
156 /// # use rbdc_mysql::options::{MySqlSslMode, MySqlConnectOptions};
157 /// let options = MySqlConnectOptions::new()
158 /// .ssl_mode(MySqlSslMode::VerifyCa)
159 /// .ssl_ca_from_pem(vec![]);
160 /// ```
161 pub fn ssl_ca_from_pem(mut self, pem_certificate: Vec<u8>) -> Self {
162 self.ssl_ca = Some(CertificateInput::Inline(pem_certificate));
163 self
164 }
165
166 /// Sets the capacity of the connection's statement cache in a number of stored
167 /// distinct statements. Caching is handled using LRU, meaning when the
168 /// amount of queries hits the defined limit, the oldest statement will get
169 /// dropped.
170 ///
171 /// The default cache capacity is 100 statements.
172 pub fn statement_cache_capacity(mut self, capacity: usize) -> Self {
173 self.statement_cache_capacity = capacity;
174 self
175 }
176
177 /// Sets the character set for the connection.
178 ///
179 /// The default character set is `utf8mb4`. This is supported from MySQL 5.5.3.
180 /// If you need to connect to an older version, we recommend you to change this to `utf8`.
181 pub fn charset(mut self, charset: &str) -> Self {
182 self.charset = charset.to_owned();
183 self
184 }
185
186 /// Sets the collation for the connection.
187 ///
188 /// The default collation is derived from the `charset`. Normally, you should only have to set
189 /// the `charset`.
190 pub fn collation(mut self, collation: &str) -> Self {
191 self.collation = Some(collation.to_owned());
192 self
193 }
194
195 /// set default DateTime offset sec
196 ///
197 /// # Example
198 ///
199 /// ```rust
200 /// use rbdc_mysql::options::MySqlConnectOptions;
201 /// let options = MySqlConnectOptions::new()
202 /// .offset_sec(fastdate::offset_sec());
203 /// ```
204 pub fn offset_sec(mut self,offset_sec: i32) -> Self{
205 self.offset_sec = offset_sec;
206 self
207 }
208}