rbdc_mysql/options/
connect.rs

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