sqlx_core_guts/mysql/options/connect.rs
1use crate::connection::ConnectOptions;
2use crate::error::Error;
3use crate::executor::Executor;
4use crate::mysql::{MySqlConnectOptions, MySqlConnection};
5use futures_core::future::BoxFuture;
6use log::LevelFilter;
7use std::time::Duration;
8
9impl ConnectOptions for MySqlConnectOptions {
10 type Connection = MySqlConnection;
11
12 fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
13 where
14 Self::Connection: Sized,
15 {
16 Box::pin(async move {
17 let mut conn = MySqlConnection::establish(self).await?;
18
19 // After the connection is established, we initialize by configuring a few
20 // connection parameters
21
22 // https://mariadb.com/kb/en/sql-mode/
23
24 // PIPES_AS_CONCAT - Allows using the pipe character (ASCII 124) as string concatenation operator.
25 // This means that "A" || "B" can be used in place of CONCAT("A", "B").
26
27 // NO_ENGINE_SUBSTITUTION - If not set, if the available storage engine specified by a CREATE TABLE is
28 // not available, a warning is given and the default storage
29 // engine is used instead.
30
31 // NO_ZERO_DATE - Don't allow '0000-00-00'. This is invalid in Rust.
32
33 // NO_ZERO_IN_DATE - Don't allow 'YYYY-00-00'. This is invalid in Rust.
34
35 // --
36
37 // Setting the time zone allows us to assume that the output
38 // from a TIMESTAMP field is UTC
39
40 // --
41
42 // https://mathiasbynens.be/notes/mysql-utf8mb4
43
44 let mut options = String::new();
45 options.push_str(r#"SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT,NO_ENGINE_SUBSTITUTION')),"#);
46 options.push_str(r#"time_zone='+00:00',"#);
47 options.push_str(&format!(
48 r#"NAMES {} COLLATE {};"#,
49 conn.stream.charset.as_str(),
50 conn.stream.collation.as_str()
51 ));
52
53 conn.execute(&*options).await?;
54
55 Ok(conn)
56 })
57 }
58
59 fn log_statements(&mut self, level: LevelFilter) -> &mut Self {
60 self.log_settings.log_statements(level);
61 self
62 }
63
64 fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) -> &mut Self {
65 self.log_settings.log_slow_statements(level, duration);
66 self
67 }
68}