sqlx_mysql/options/
connect.rs1use 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 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 let set_names = if let Some(collation) = &self.collation {
76 format!(r#"NAMES {} COLLATE {collation}"#, self.charset,)
77 } else {
78 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}