sqlx_mysql/options/
connect.rs

1use crate::connection::ConnectOptions;
2use crate::error::Error;
3use crate::executor::Executor;
4use crate::{MySqlConnectOptions, MySqlConnection};
5use futures_core::future::BoxFuture;
6use log::LevelFilter;
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    fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
22    where
23        Self::Connection: Sized,
24    {
25        Box::pin(async move {
26            let mut conn = MySqlConnection::establish(self).await?;
27
28            // After the connection is established, we initialize by configuring a few
29            // connection parameters
30
31            // https://mariadb.com/kb/en/sql-mode/
32
33            // PIPES_AS_CONCAT - Allows using the pipe character (ASCII 124) as string concatenation operator.
34            //                   This means that "A" || "B" can be used in place of CONCAT("A", "B").
35
36            // NO_ENGINE_SUBSTITUTION - If not set, if the available storage engine specified by a CREATE TABLE is
37            //                          not available, a warning is given and the default storage
38            //                          engine is used instead.
39
40            // NO_ZERO_DATE - Don't allow '0000-00-00'. This is invalid in Rust.
41
42            // NO_ZERO_IN_DATE - Don't allow 'YYYY-00-00'. This is invalid in Rust.
43
44            // --
45
46            // Setting the time zone allows us to assume that the output
47            // from a TIMESTAMP field is UTC
48
49            // --
50
51            // https://mathiasbynens.be/notes/mysql-utf8mb4
52
53            let mut sql_mode = Vec::new();
54            if self.pipes_as_concat {
55                sql_mode.push(r#"PIPES_AS_CONCAT"#);
56            }
57            if self.no_engine_substitution {
58                sql_mode.push(r#"NO_ENGINE_SUBSTITUTION"#);
59            }
60
61            let mut options = Vec::new();
62            if !sql_mode.is_empty() {
63                options.push(format!(
64                    r#"sql_mode=(SELECT CONCAT(@@sql_mode, ',{}'))"#,
65                    sql_mode.join(",")
66                ));
67            }
68            if let Some(timezone) = &self.timezone {
69                options.push(format!(r#"time_zone='{}'"#, timezone));
70            }
71            if self.set_names {
72                options.push(format!(
73                    r#"NAMES {} COLLATE {}"#,
74                    conn.inner.stream.charset.as_str(),
75                    conn.inner.stream.collation.as_str()
76                ))
77            }
78
79            if !options.is_empty() {
80                conn.execute(&*format!(r#"SET {};"#, options.join(",")))
81                    .await?;
82            }
83
84            Ok(conn)
85        })
86    }
87
88    fn log_statements(mut self, level: LevelFilter) -> Self {
89        self.log_settings.log_statements(level);
90        self
91    }
92
93    fn log_slow_statements(mut self, level: LevelFilter, duration: Duration) -> Self {
94        self.log_settings.log_slow_statements(level, duration);
95        self
96    }
97}