rbdc_mysql/driver.rs
1use crate::options::MySqlConnectOptions;
2use futures_core::future::BoxFuture;
3use rbdc::db::{ConnectOptions, Connection, Driver, Placeholder};
4use rbdc::Error;
5
6#[derive(Debug)]
7pub struct MysqlDriver {}
8
9impl Driver for MysqlDriver {
10 fn name(&self) -> &str {
11 "mysql"
12 }
13
14 fn connect(&self, url: &str) -> BoxFuture<'_, Result<Box<dyn Connection>, Error>> {
15 let url = url.to_owned();
16 Box::pin(async move {
17 let mut opt = self.default_option();
18 opt.set_uri(&url)?;
19 if let Some(opt) = opt.downcast_ref::<MySqlConnectOptions>() {
20 let conn = opt.connect().await?;
21 Ok(Box::new(conn) as Box<dyn Connection>)
22 } else {
23 Err(Error::from("downcast_ref failure"))
24 }
25 })
26 }
27
28 fn connect_opt<'a>(
29 &'a self,
30 opt: &'a dyn ConnectOptions,
31 ) -> BoxFuture<'a, Result<Box<dyn Connection>, Error>> {
32 let opt: &MySqlConnectOptions = opt.downcast_ref().unwrap();
33 Box::pin(async move {
34 let conn = opt.connect().await?;
35 Ok(conn)
36 })
37 }
38
39 fn default_option(&self) -> Box<dyn ConnectOptions> {
40 Box::new(MySqlConnectOptions::default())
41 }
42}
43
44impl Placeholder for MysqlDriver {
45 fn exchange(&self, sql: &str) -> String {
46 sql.to_string()
47 }
48}
49
50#[cfg(test)]
51mod test {
52 #[test]
53 fn test_default() {
54 assert_eq!(true, true);
55 }
56}
57// #[cfg(test)]
58// mod test {
59// use crate::driver::MysqlDriver;
60// use rbdc::block_on;
61// use rbdc::db::Driver;
62// use rbdc::pool::Pool;
63// use rbs::{value, Value};
64// use std::collections::BTreeMap;
65//
66// #[test]
67// fn test_mysql_pool() {
68// let task = async move {
69// let pool =
70// Pool::new_url(MysqlDriver {}, "mysql://root:123456@localhost:3306/test").unwrap();
71// std::thread::sleep(std::time::Duration::from_secs(2));
72// let mut conn = pool.get().await.unwrap();
73// let data = conn
74// .get_values("select * from sys_dict where code = ?", vec![Value::String("111".to_string())])
75// .await
76// .unwrap();
77// for mut x in data {
78// println!("row: {}", x);
79// }
80// };
81// block_on!(task);
82// }
83//
84// #[test]
85// fn test_mysql_rows() {
86// let task = async move {
87// let mut d = MysqlDriver {};
88// let mut c = d
89// .connect("mysql://root:123456@localhost:3306/test")
90// .await
91// .unwrap();
92// let data = c
93// .get_values("select * from sys_dict", vec![])
94// .await
95// .unwrap();
96// for mut x in data {
97// println!("row: {}", x);
98// }
99// };
100// block_on!(task);
101// }
102//
103// //
104// // #[tokio::test]
105// // async fn test_mysql_count() {
106// // let mut d = MysqlDriver {};
107// // let mut c = d
108// // .connect("mysql://root:123456@localhost:3306/test")
109// // .await
110// // .unwrap();
111// // let data = c
112// // .exec(
113// // "update biz_activity set pc_link = '111' where id = '1'",
114// // vec![],
115// // )
116// // .await
117// // .unwrap();
118// // println!("{}", data);
119// // }
120// //
121//
122// #[test]
123// fn test_mysql_param() {
124// let task = async move {
125// let mut d = MysqlDriver {};
126// let mut c = d
127// .connect("mysql://root:123456@localhost:3306/test")
128// .await
129// .unwrap();
130// let param = vec![
131// Value::String("http://www.test.com".to_string()),
132// Value::U64(1658848837828).into_ext("Timestamp"),
133// Value::String("12312".to_string()),
134// ];
135// println!("param => {}", Value::Array(param.clone()));
136// let data = c
137// .exec(
138// "update biz_activity set pc_link = ?,create_time = ? where id = ?",
139// param,
140// )
141// .await
142// .unwrap();
143// println!("{}", data);
144// };
145// block_on!(task);
146// }
147// #[test]
148// fn test_count() {
149// let task = async move {
150// let mut d = MysqlDriver {};
151// let mut c = d
152// .connect("mysql://root:123456@localhost:3306/test")
153// .await
154// .unwrap();
155// let param = vec![
156// ];
157// let data = c
158// .get_values(
159// "select count(1) as count from sys_dict where id!='' order by create_date",
160// param,
161// )
162// .await
163// .unwrap();
164// println!("{:?}", data);
165// };
166// block_on!(task);
167// }
168//
169// #[test]
170// fn test_insert() {
171// let task = async move {
172// let mut d = MysqlDriver {};
173// let mut c = d
174// .connect("mysql://root:123456@localhost:3306/test")
175// .await
176// .unwrap();
177// let param:Vec<Value> = vec![
178// "111".into(),
179// "111".into(),
180// "111".into()
181// ,1.into(),
182// Value::Ext("DateTime",Box::new("2022-08-07 21:33:59".into()))
183// ];
184// let data = c
185// .exec(
186// "insert into sys_dict (id,name,code,state,create_date) VALUES (?,?,?,?,?)",
187// param,
188// )
189// .await
190// .unwrap();
191// println!("{:?}", data);
192// };
193// block_on!(task);
194// }
195// }