sqlx_core_oldapi/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            if self.pipes_as_concat {
46                options.push_str(r#"SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT,NO_ENGINE_SUBSTITUTION')),"#);
47            } else {
48                options.push_str(
49                    r#"SET sql_mode=(SELECT CONCAT(@@sql_mode, ',NO_ENGINE_SUBSTITUTION')),"#,
50                );
51            }
52            options.push_str(r#"time_zone='+00:00',"#);
53            options.push_str(&format!(
54                r#"NAMES {} COLLATE {};"#,
55                conn.stream.charset.as_str(),
56                conn.stream.collation.as_str()
57            ));
58
59            conn.execute(&*options).await?;
60
61            Ok(conn)
62        })
63    }
64
65    fn log_statements(&mut self, level: LevelFilter) -> &mut Self {
66        self.log_settings.log_statements(level);
67        self
68    }
69
70    fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) -> &mut Self {
71        self.log_settings.log_slow_statements(level, duration);
72        self
73    }
74}