sqlx_xugu/options/
mod.rs

1mod connect;
2mod parse;
3
4use sqlx_core::connection::LogSettings;
5
6#[derive(Debug, Clone)]
7pub struct XuguConnectOptions {
8    pub(crate) host: String,
9    pub(crate) port: u16,
10    pub(crate) user: String,
11    pub(crate) password: String,
12    pub(crate) database: String,
13    pub(crate) charset: String,
14    version: Option<i16>,
15    return_schema: bool,
16    return_rowid: bool,
17    encryptor: Option<String>,
18    time_zone: Option<String>,
19    iso_level: Option<String>,
20    lock_timeout: Option<String>,
21    lob_ret: Option<String>,
22    identity_mode: Option<String>,
23    keyword_filter: Option<String>,
24    disable_binlog: Option<String>,
25    auto_commit: bool,
26    pub(crate) use_ssl: bool,
27    current_schema: Option<String>,
28    compatible_mode: Option<String>,
29
30    pub(crate) log_settings: LogSettings,
31}
32
33impl Default for XuguConnectOptions {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39impl XuguConnectOptions {
40    pub(crate) fn to_conn_str(&self) -> String {
41        let return_schema = if self.return_schema { "on" } else { "off" };
42        let version = self.get_version();
43        // 必要参数
44        let mut conn_str = format!(
45            "login database='{}' user='{}' password='{}' version={} return_schema={} return_cursor_id=on",
46            self.database, self.user, self.password, version, return_schema,
47        );
48
49        // 可选参数
50        if self.return_rowid {
51            conn_str += " return_rowid=true";
52        }
53        if let Some(encryptor) = &self.encryptor {
54            conn_str += " encryptor=";
55            conn_str += encryptor;
56        }
57        if self.charset.is_empty() {
58            conn_str += " char_set=utf8";
59        } else {
60            conn_str = conn_str + " char_set=" + self.charset.as_str();
61        }
62        if let Some(time_zone) = &self.time_zone {
63            conn_str = conn_str + " time_zone='" + time_zone + "'";
64        }
65        if let Some(iso_level) = &self.iso_level {
66            conn_str += " iso_level='";
67            conn_str += iso_level;
68            conn_str += "'";
69        }
70        if let Some(lock_timeout) = &self.lock_timeout {
71            conn_str += " lock_timeout=";
72            conn_str += lock_timeout;
73        }
74        if let Some(lob_ret) = &self.lob_ret {
75            conn_str += " lob_ret='";
76            conn_str += lob_ret;
77            conn_str += "'";
78        }
79        if let Some(identity_mode) = &self.identity_mode {
80            conn_str += " identity_mode='";
81            conn_str += identity_mode;
82            conn_str += "'";
83        }
84        if let Some(keyword_filter) = &self.keyword_filter {
85            conn_str += " keyword_filter='";
86            conn_str += keyword_filter;
87            conn_str += "'";
88        }
89        if let Some(disable_binlog) = &self.disable_binlog {
90            conn_str += " disable_binlog='";
91            conn_str += disable_binlog;
92            conn_str += "'";
93        }
94        if self.auto_commit {
95            conn_str += " auto_commit=on";
96        } else {
97            conn_str += " auto_commit=off";
98        }
99        if let Some(current_schema) = &self.current_schema {
100            conn_str += " current_schema='";
101            conn_str += current_schema;
102            conn_str += "'";
103        }
104        if let Some(compatible_mode) = &self.compatible_mode {
105            conn_str += " compatible_mode='";
106            conn_str += compatible_mode;
107            conn_str += "'";
108        }
109
110        conn_str += "\0";
111        conn_str
112    }
113
114    pub fn get_version(&self) -> i16 {
115        self.version.unwrap_or(301)
116    }
117}
118
119impl XuguConnectOptions {
120    /// Creates a new, default set of options ready for configuration
121    pub fn new() -> Self {
122        Self {
123            host: String::from("127.0.0.1"),
124            port: 5138,
125            database: String::from("SYSTEM"),
126            user: String::from("SYSDBA"),
127            password: String::from("SYSDBA"),
128            version: None,
129            return_schema: true,
130            return_rowid: false,
131            encryptor: None,
132            charset: String::from("utf8"),
133            time_zone: None,
134            iso_level: Some(String::from("READ COMMITTED")),
135            lock_timeout: None,
136            lob_ret: None,
137            identity_mode: None,
138            keyword_filter: None,
139            disable_binlog: None,
140            auto_commit: true,
141            use_ssl: false,
142            current_schema: None,
143            compatible_mode: None,
144
145            log_settings: Default::default(),
146        }
147    }
148
149    /// Sets the name of the host to connect to.
150    ///
151    /// The default behavior when the host is not specified,
152    /// is to connect to localhost.
153    pub fn host(mut self, host: &str) -> Self {
154        host.clone_into(&mut self.host);
155        self
156    }
157
158    /// Sets the port to connect to at the server host.
159    ///
160    /// The default port for MySQL is `3306`.
161    pub fn port(mut self, port: u16) -> Self {
162        self.port = port;
163        self
164    }
165
166    /// Sets the database name.
167    pub fn database(mut self, database: &str) -> Self {
168        self.database = database.to_owned();
169        self
170    }
171
172    /// Sets the user to connect as.
173    pub fn user(mut self, user: &str) -> Self {
174        user.clone_into(&mut self.user);
175        self
176    }
177
178    /// Sets the password to connect with.
179    pub fn password(mut self, password: &str) -> Self {
180        self.password = password.to_owned();
181        self
182    }
183
184    /// Sets the version to connect with.
185    pub fn version(mut self, version: i16) -> Self {
186        self.version = Some(version);
187        self
188    }
189
190    pub fn return_schema(mut self, value: bool) -> Self {
191        self.return_schema = value;
192        self
193    }
194
195    pub fn return_rowid(mut self, value: bool) -> Self {
196        self.return_schema = value;
197        self
198    }
199
200    pub fn encryptor(mut self, value: &str) -> Self {
201        self.encryptor = Some(value.into());
202        self
203    }
204
205    pub fn iso_level(mut self, value: &str) -> Self {
206        self.iso_level = Some(value.into());
207        self
208    }
209
210    pub fn lock_timeout(mut self, value: &str) -> Self {
211        self.lock_timeout = Some(value.into());
212        self
213    }
214
215    pub fn lob_ret(mut self, value: &str) -> Self {
216        self.lob_ret = Some(value.into());
217        self
218    }
219
220    pub fn identity_mode(mut self, value: &str) -> Self {
221        self.identity_mode = Some(value.into());
222        self
223    }
224
225    pub fn keyword_filter(mut self, value: &str) -> Self {
226        self.keyword_filter = Some(value.into());
227        self
228    }
229
230    pub fn disable_binlog(mut self, value: &str) -> Self {
231        self.disable_binlog = Some(value.into());
232        self
233    }
234
235    pub fn auto_commit(mut self, value: bool) -> Self {
236        self.auto_commit = value;
237        self
238    }
239
240    pub fn use_ssl(mut self, value: bool) -> Self {
241        self.use_ssl = value;
242        self
243    }
244
245    pub fn current_schema(mut self, value: &str) -> Self {
246        self.current_schema = Some(value.into());
247        self
248    }
249
250    pub fn compatible_mode(mut self, value: &str) -> Self {
251        self.compatible_mode = Some(value.into());
252        self
253    }
254
255    /// Sets the character set for the connection.
256    ///
257    /// The default character set is `utf8mb4`. This is supported from MySQL 5.5.3.
258    /// If you need to connect to an older version, we recommend you to change this to `utf8`.
259    ///
260    /// Implies [`.set_names(true)`][Self::set_names()].
261    pub fn charset(mut self, charset: &str) -> Self {
262        charset.clone_into(&mut self.charset);
263        self
264    }
265
266    /// If `Some`, sets the `time_zone` option to the given string after connecting to the database.
267    ///
268    /// If `None`, no `time_zone` parameter is sent; the server timezone will be used instead.
269    ///
270    /// Defaults to `Some(String::from("+00:00"))` to ensure all timestamps are in UTC.
271    ///
272    /// ### Warning
273    /// Changing this setting from its default will apply an unexpected skew to any
274    /// `time::OffsetDateTime` or `chrono::DateTime<Utc>` value, whether passed as a parameter or
275    /// decoded as a result. `TIMESTAMP` values are not encoded with their UTC offset in the MySQL
276    /// protocol, so encoding and decoding of these types assumes the server timezone is *always*
277    /// UTC.
278    ///
279    /// If you are changing this option, ensure your application only uses
280    /// `time::PrimitiveDateTime` or `chrono::NaiveDateTime` and that it does not assume these
281    /// timestamps can be placed on a real timeline without applying the proper offset.
282    pub fn timezone(mut self, value: impl Into<Option<String>>) -> Self {
283        self.time_zone = value.into();
284        self
285    }
286}
287
288impl XuguConnectOptions {
289    /// Get the current host.
290    ///
291    /// # Example
292    ///
293    /// ```rust
294    /// # use sqlx_xugu::XuguConnectOptions;
295    /// let options = XuguConnectOptions::new()
296    ///     .host("127.0.0.1");
297    /// assert_eq!(options.get_host(), "127.0.0.1");
298    /// ```
299    pub fn get_host(&self) -> &str {
300        &self.host
301    }
302
303    /// Get the server's port.
304    ///
305    /// # Example
306    ///
307    /// ```rust
308    /// # use sqlx_xugu::XuguConnectOptions;
309    /// let options = XuguConnectOptions::new()
310    ///     .port(5138);
311    /// assert_eq!(options.get_port(), 5138);
312    /// ```
313    pub fn get_port(&self) -> u16 {
314        self.port
315    }
316
317    /// Get the current user.
318    ///
319    /// # Example
320    ///
321    /// ```rust
322    /// # use sqlx_xugu::XuguConnectOptions;
323    /// let options = XuguConnectOptions::new()
324    ///     .user("foo");
325    /// assert_eq!(options.get_user(), "foo");
326    /// ```
327    pub fn get_user(&self) -> &str {
328        &self.user
329    }
330
331    /// Get the current database name.
332    ///
333    /// # Example
334    ///
335    /// ```rust
336    /// # use sqlx_xugu::XuguConnectOptions;
337    /// let options = XuguConnectOptions::new()
338    ///     .database("SYSTEM");
339    /// assert_eq!(options.get_database(), "SYSTEM");
340    /// ```
341    pub fn get_database(&self) -> &str {
342        &self.database
343    }
344
345    /// Get the server charset.
346    ///
347    /// # Example
348    ///
349    /// ```rust
350    /// # use sqlx_xugu::XuguConnectOptions;
351    /// let options = XuguConnectOptions::new();
352    /// assert_eq!(options.get_charset(), "utf8");
353    /// ```
354    pub fn get_charset(&self) -> &str {
355        &self.charset
356    }
357}