@@ -24,80 +24,92 @@ impl ConnectOptions for MySqlConnectOptions {
2424 {
2525 let mut conn = MySqlConnection :: establish ( self ) . await ?;
2626
27- // After the connection is established, we initialize by configuring a few
28- // connection parameters
27+ apply_connect_options ( & mut conn, self ) . await ?;
2928
30- // https://mariadb.com/kb/en/sql-mode/
29+ Ok ( conn)
30+ }
3131
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").
32+ fn log_statements ( mut self , level : LevelFilter ) -> Self {
33+ self . log_settings . log_statements ( level) ;
34+ self
35+ }
3436
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.
37+ fn log_slow_statements ( mut self , level : LevelFilter , duration : Duration ) -> Self {
38+ self . log_settings . log_slow_statements ( level, duration) ;
39+ self
40+ }
41+ }
3842
39- // NO_ZERO_DATE - Don't allow '0000-00-00'. This is invalid in Rust.
43+ pub ( crate ) async fn apply_connect_options (
44+ conn : & mut MySqlConnection ,
45+ options : & MySqlConnectOptions ,
46+ ) -> Result < ( ) , Error > {
47+ // After the connection is established, we initialize by configuring a few
48+ // connection parameters
4049
41- // NO_ZERO_IN_DATE - Don't allow 'YYYY-00-00'. This is invalid in Rust.
50+ // https://mariadb.com/kb/en/sql-mode/
4251
43- // --
52+ // PIPES_AS_CONCAT - Allows using the pipe character (ASCII 124) as string concatenation operator.
53+ // This means that "A" || "B" can be used in place of CONCAT("A", "B").
4454
45- // Setting the time zone allows us to assume that the output
46- // from a TIMESTAMP field is UTC
55+ // NO_ENGINE_SUBSTITUTION - If not set, if the available storage engine specified by a CREATE TABLE is
56+ // not available, a warning is given and the default storage
57+ // engine is used instead.
4758
48- // --
59+ // NO_ZERO_DATE - Don't allow '0000-00-00'. This is invalid in Rust.
4960
50- // https://mathiasbynens.be/notes/mysql-utf8mb4
61+ // NO_ZERO_IN_DATE - Don't allow 'YYYY-00-00'. This is invalid in Rust.
5162
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- }
63+ // --
5964
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- }
65+ // Setting the time zone allows us to assume that the output
66+ // from a TIMESTAMP field is UTC
6767
68- if let Some ( timezone) = & self . timezone {
69- options. push ( format ! ( r#"time_zone='{}'"# , timezone) ) ;
70- }
68+ // --
7169
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- } ;
70+ // https://mathiasbynens.be/notes/mysql-utf8mb4
8271
83- options. push ( set_names) ;
84- }
72+ let mut sql_mode = Vec :: new ( ) ;
73+ if options. pipes_as_concat {
74+ sql_mode. push ( r#"PIPES_AS_CONCAT"# ) ;
75+ }
76+ if options. no_engine_substitution {
77+ sql_mode. push ( r#"NO_ENGINE_SUBSTITUTION"# ) ;
78+ }
8579
86- if !options. is_empty ( ) {
87- conn. execute ( AssertSqlSafe ( format ! ( r#"SET {};"# , options. join( "," ) ) ) )
88- . await ?;
89- }
80+ let mut session_options = Vec :: new ( ) ;
81+ if !sql_mode. is_empty ( ) {
82+ session_options. push ( format ! (
83+ r#"sql_mode=(SELECT CONCAT(@@sql_mode, ',{}'))"# ,
84+ sql_mode. join( "," )
85+ ) ) ;
86+ }
9087
91- Ok ( conn)
88+ if let Some ( timezone) = & options. timezone {
89+ session_options. push ( format ! ( r#"time_zone='{}'"# , timezone) ) ;
9290 }
9391
94- fn log_statements ( mut self , level : LevelFilter ) -> Self {
95- self . log_settings . log_statements ( level) ;
96- self
92+ if options. set_names {
93+ // As it turns out, we don't _have_ to set a collation if we don't want to.
94+ // We can let the server choose the default collation for the charset.
95+ let set_names = if let Some ( collation) = & options. collation {
96+ format ! ( r#"NAMES {} COLLATE {collation}"# , options. charset, )
97+ } else {
98+ // Leaves the default collation up to the server,
99+ // but ensures statements and results are encoded using the proper charset.
100+ format ! ( "NAMES {}" , options. charset)
101+ } ;
102+
103+ session_options. push ( set_names) ;
97104 }
98105
99- fn log_slow_statements ( mut self , level : LevelFilter , duration : Duration ) -> Self {
100- self . log_settings . log_slow_statements ( level, duration) ;
101- self
106+ if !session_options. is_empty ( ) {
107+ conn. execute ( AssertSqlSafe ( format ! (
108+ r#"SET {};"# ,
109+ session_options. join( "," )
110+ ) ) )
111+ . await ?;
102112 }
113+
114+ Ok ( ( ) )
103115}
0 commit comments