sqlx_mysql/options/
connect.rs1use 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 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}