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
//! # r2d2-mysql
//! MySQL support for the r2d2 connection pool (Rust) . see [`r2d2`](http://github.com/sfackler/r2d2.git)  .
//! 
//! #### Install
//! Just include another `[dependencies.*]` section into your Cargo.toml:
//!
//! ```toml
//! [dependencies.r2d2_mysql]
//! git = "https://github.com/outersky/r2d2-mysql"
//! version="0.2.0"
//! ```
//! #### Sample
//!
//! ```
//! extern crate r2d2_mysql;
//! extern crate r2d2;
//! 
//! use std::sync::Arc;
//! use std::thread;
//! 
//! fn main() {
//! 	let db_url =  "mysql://root:12345678@localhost:3306/test";
//!     let config = r2d2::config::Builder::new().pool_size(5).build();   // r2d2::Config::default()
//!     let manager = r2d2_mysql::MysqlConnectionManager::new(db_url).unwrap();
//!     let pool = Arc::new(r2d2::Pool::new(config, manager).unwrap());
//! 
//!     let mut tasks = vec![];
//! 
//!     for i in 0..3 {
//!         let pool = pool.clone();
//!         let th = thread::spawn(move || {
//!             let mut conn = pool.get().unwrap();
//!             conn.query("select user()").unwrap();
//!             println!("thread {} end!" , i );
//!         });
//!         tasks.push(th);
//!     }
//! 
//!     for th in tasks {
//!         let _ = th.join();
//!     }
//! }
//! ```
//! 

#![doc(html_root_url="http://outersky.github.io/r2d2-mysql/doc/v0.2.0/r2d2_mysql/")]
#![crate_name="r2d2_mysql"]
#![crate_type="rlib"]
#![crate_type="dylib"]

extern crate mysql;
extern crate rustc_serialize as serialize;
extern crate r2d2;

pub mod param;
pub mod url;
pub mod pool;

pub use pool::MysqlConnectionManager;
pub use param::connect;

#[cfg(test)]
mod test {
    use r2d2;
    use std::sync::Arc;
    use std::thread;
    use super::{MysqlConnectionManager};

    const DB_URL : &'static str =  "mysql://root:12345678@localhost:3306/test";

    #[test]
    fn query_pool(){
        let config = r2d2::config::Builder::new().pool_size(30).build();   // r2d2::Config::default()
        let manager = MysqlConnectionManager::new(DB_URL).unwrap();
        let pool = Arc::new(r2d2::Pool::new(config, manager).unwrap());

        let mut tasks = vec![];

        for _ in 0..3 {
            let pool = pool.clone();
            let th = thread::spawn(move || {
                let mut conn = pool.get().map_err(|err| println!("get connection from pool error in line:{} ! error: {:?}", line!(), err) ).unwrap();
                conn.query("select user()").map_err(|err| println!("execute query error in line:{} ! error: {:?}", line!(), err) ).unwrap();
            });
            tasks.push(th);
        }

        for th in tasks {
            let _ = th.join();
        }

    }
}