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
//! # 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="*"
//! ```
//! #### Sample
//!
//! ```
//! extern crate mysql;
//! extern crate r2d2_mysql;
//! extern crate r2d2;
//! use std::env;
//! use std::sync::Arc;
//! use std::thread;
//! use mysql::{Opts,OptsBuilder};
//! use r2d2_mysql::MysqlConnectionManager;
//!
//! fn main() {
//! 	let db_url =  env::var("DATABASE_URL").unwrap();
//!     let opts = Opts::from_url(&db_url).unwrap();
//!     let builder = OptsBuilder::from_opts(opts);
//!     let manager = MysqlConnectionManager::new(builder);
//!     let pool = Arc::new(r2d2::Pool::builder().max_size(4).build(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"]

pub extern crate mysql;
pub extern crate r2d2;

pub mod pool;
pub use pool::MysqlConnectionManager;

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

    #[test]
    fn query_pool() {
        let url = env::var("DATABASE_URL").unwrap();
        let opts = Opts::from_url(&url).unwrap();
        let builder = OptsBuilder::from_opts(opts);
        let manager = MysqlConnectionManager::new(builder);
        let pool = Arc::new(r2d2::Pool::builder().max_size(4).build(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();
        }
    }
}