1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use crate::connection::MySqlConnection;
use crate::options::MySqlConnectOptions;
use futures_core::future::BoxFuture;
use rbdc::db::{ConnectOptions, Connection, Driver, Placeholder};
use rbdc::Error;
use std::any::Any;
use std::ops::Deref;
use std::str::FromStr;
use std::sync::Arc;

#[derive(Debug)]
pub struct MysqlDriver {}

impl Driver for MysqlDriver {
    fn name(&self) -> &str {
        "mysql"
    }

    fn connect(&self, url: &str) -> BoxFuture<Result<Box<dyn Connection>, Error>> {
        let url = url.to_owned();
        Box::pin(async move {
            let conn = MySqlConnection::establish(&url.parse()?).await?;
            Ok(Box::new(conn) as Box<dyn Connection>)
        })
    }

    fn connect_opt<'a>(
        &'a self,
        opt: &'a dyn ConnectOptions,
    ) -> BoxFuture<Result<Box<dyn Connection>, Error>> {
        let opt = opt.downcast_ref().unwrap();
        Box::pin(async move {
            let conn = MySqlConnection::establish(opt).await?;
            Ok(Box::new(conn) as Box<dyn Connection>)
        })
    }

    fn default_option(&self) -> Box<dyn ConnectOptions> {
        Box::new(MySqlConnectOptions::default())
    }
}

impl Placeholder for MysqlDriver {
    fn exchange(&self, sql: &str) -> String {
        sql.to_string()
    }
}

#[cfg(test)]
mod test {
    use crate::driver::MysqlDriver;
    use rbdc::block_on;
    use rbdc::db::Driver;
    use rbdc::pool::Pool;
    use rbs::{to_value, Value};
    use std::collections::BTreeMap;

    #[test]
    fn test_mysql_pool() {
        let task = async move {
            let pool =
                Pool::new_url(MysqlDriver {}, "mysql://root:123456@localhost:3306/test").unwrap();
            std::thread::sleep(std::time::Duration::from_secs(2));
            let mut conn = pool.get().await.unwrap();
            let data = conn
                .get_values("select * from biz_activity", vec![])
                .await
                .unwrap();
            for mut x in data {
                println!("row: {}", x);
            }
        };
        block_on!(task);
    }

    #[test]
    fn test_mysql_rows() {
        let task = async move {
            let mut d = MysqlDriver {};
            let mut c = d
                .connect("mysql://root:123456@localhost:3306/test")
                .await
                .unwrap();
            let data = c
                .get_values("select * from biz_activity", vec![])
                .await
                .unwrap();
            for mut x in data {
                println!("row: {}", x);
            }
        };
        block_on!(task);
    }

    //
    // #[tokio::test]
    // async fn test_mysql_count() {
    //     let mut d = MysqlDriver {};
    //     let mut c = d
    //         .connect("mysql://root:123456@localhost:3306/test")
    //         .await
    //         .unwrap();
    //     let data = c
    //         .exec(
    //             "update biz_activity set pc_link = '111' where id  = '1'",
    //             vec![],
    //         )
    //         .await
    //         .unwrap();
    //     println!("{}", data);
    // }
    //

    #[test]
    fn test_mysql_param() {
        let task = async move {
            let mut d = MysqlDriver {};
            let mut c = d
                .connect("mysql://root:123456@localhost:3306/test")
                .await
                .unwrap();
            let param = vec![
                Value::String("http://www.test.com".to_string()),
                Value::U64(1658848837828).into_ext("Timestamp"),
                Value::String("12312".to_string()),
            ];
            println!("param => {}", Value::Array(param.clone()));
            let data = c
                .exec(
                    "update biz_activity set pc_link = ?,create_time = ? where id  = ?",
                    param,
                )
                .await
                .unwrap();
            println!("{}", data);
        };
        block_on!(task);
    }
}