sqlx_mysql/options/
connect.rs

1use crate::connection::ConnectOptions;
2use crate::error::Error;
3use crate::executor::Executor;
4use crate::{MySqlConnectOptions, MySqlConnection};
5use log::LevelFilter;
6use sqlx_core::sql_str::AssertSqlSafe;
7use sqlx_core::Url;
8use std::time::Duration;
9
10impl ConnectOptions for MySqlConnectOptions {
11    type Connection = MySqlConnection;
12
13    fn from_url(url: &Url) -> Result<Self, Error> {
14        Self::parse_from_url(url)
15    }
16
17    fn to_url_lossy(&self) -> Url {
18        self.build_url()
19    }
20
21    async fn connect(&self) -> Result<Self::Connection, Error>
22    where
23        Self::Connection: Sized,
24    {
25        let mut conn = MySqlConnection::establish(self).await?;
26
27        // After the connection is established, we initialize by configuring a few
28        // connection parameters
29
30        // https://mariadb.com/kb/en/sql-mode/
31
32        // PIPES_AS_CONCAT - Allows using the pipe character (ASCII 124) as string concatenation operator.
33        //                   This means that "A" || "B" can be used in place of CONCAT("A", "B").
34
35        // NO_ENGINE_SUBSTITUTION - If not set, if the available storage engine specified by a CREATE TABLE is
36        //                          not available, a warning is given and the default storage
37        //                          engine is used instead.
38
39        // NO_ZERO_DATE - Don't allow '0000-00-00'. This is invalid in Rust.
40
41        // NO_ZERO_IN_DATE - Don't allow 'YYYY-00-00'. This is invalid in Rust.
42
43        // --
44
45        // Setting the time zone allows us to assume that the output
46        // from a TIMESTAMP field is UTC
47
48        // --
49
50        // https://mathiasbynens.be/notes/mysql-utf8mb4
51
52        let mut sql_mode = Vec::new();
53        if self.pipes_as_concat {
54            sql_mode.push(r#"PIPES_AS_CONCAT"#);
55        }
56        if self.no_engine_substitution {
57            sql_mode.push(r#"NO_ENGINE_SUBSTITUTION"#);
58        }
59
60        let mut options = Vec::new();
61        if !sql_mode.is_empty() {
62            options.push(format!(
63                r#"sql_mode=(SELECT CONCAT(@@sql_mode, ',{}'))"#,
64                sql_mode.join(",")
65            ));
66        }
67
68        if let Some(timezone) = &self.timezone {
69            options.push(format!(r#"time_zone='{}'"#, timezone));
70        }
71
72        if self.set_names {
73            // As it turns out, we don't _have_ to set a collation if we don't want to.
74            // We can let the server choose the default collation for the charset.
75            let set_names = if let Some(collation) = &self.collation {
76                format!(r#"NAMES {} COLLATE {collation}"#, self.charset,)
77            } else {
78                // Leaves the default collation up to the server,
79                // but ensures statements and results are encoded using the proper charset.
80                format!("NAMES {}", self.charset)
81            };
82
83            options.push(set_names);
84        }
85
86        if !options.is_empty() {
87            conn.execute(AssertSqlSafe(format!(r#"SET {};"#, options.join(","))))
88                .await?;
89        }
90
91        Ok(conn)
92    }
93
94    fn log_statements(mut self, level: LevelFilter) -> Self {
95        self.log_settings.log_statements(level);
96        self
97    }
98
99    fn log_slow_statements(mut self, level: LevelFilter, duration: Duration) -> Self {
100        self.log_settings.log_slow_statements(level, duration);
101        self
102    }
103}