cdbc_mysql/options/
connect.rs

1use cdbc::connection::ConnectOptions;
2use cdbc::error::Error;
3use cdbc::executor::Executor;
4use crate::{MySqlConnectOptions, MySqlConnection};
5use std::time::Duration;
6
7impl ConnectOptions for MySqlConnectOptions {
8    type Connection = MySqlConnection;
9
10    fn connect(&self,d:Duration) -> Result<Self::Connection, Error>
11    where
12        Self::Connection: Sized,
13    {
14
15            let mut conn = MySqlConnection::establish(self,d)?;
16
17            // After the connection is established, we initialize by configuring a few
18            // connection parameters
19
20            // https://mariadb.com/kb/en/sql-mode/
21
22            // PIPES_AS_CONCAT - Allows using the pipe character (ASCII 124) as string concatenation operator.
23            //                   This means that "A" || "B" can be used in place of CONCAT("A", "B").
24
25            // NO_ENGINE_SUBSTITUTION - If not set, if the available storage engine specified by a CREATE TABLE is
26            //                          not available, a warning is given and the default storage
27            //                          engine is used instead.
28
29            // NO_ZERO_DATE - Don't allow '0000-00-00'. This is invalid in Rust.
30
31            // NO_ZERO_IN_DATE - Don't allow 'YYYY-00-00'. This is invalid in Rust.
32
33            // --
34
35            // Setting the time zone allows us to assume that the output
36            // from a TIMESTAMP field is UTC
37
38            // --
39
40            // https://mathiasbynens.be/notes/mysql-utf8mb4
41
42            let mut options = String::new();
43            options.push_str(r#"SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT,NO_ENGINE_SUBSTITUTION')),"#);
44            options.push_str(r#"time_zone='+00:00',"#);
45            options.push_str(&format!(
46                r#"NAMES {} COLLATE {};"#,
47                conn.stream.charset.as_str(),
48                conn.stream.collation.as_str()
49            ));
50
51            conn.execute(&*options)?;
52
53            Ok(conn)
54
55    }
56}