r2d2_oracle/
lib.rs

1//! Oracle support for the r2d2 connection pool.
2//!
3//! If you want to use chrono data types, enable the ```chrono``` feature:
4//!```toml
5//![dependencies]
6//!r2d2-oracle = { version = "0.2.0", features = ["chrono"] }
7//!```
8
9#![forbid(unsafe_code)]
10// use deny instead of forbid due to bogus warnings, see also https://github.com/rust-lang/rust/issues/81670
11#![deny(warnings)]
12#![deny(missing_docs)]
13#![forbid(missing_debug_implementations)]
14#![forbid(unused)]
15
16pub use oracle;
17pub use r2d2;
18
19/// An `r2d2::ManageConnection` for `oracle::Connection`s.
20///
21/// # Example
22/// ```no_run
23/// use std::thread;
24/// use r2d2_oracle::OracleConnectionManager;
25///
26/// let manager = OracleConnectionManager::new("user", "password", "localhost");
27/// let pool = r2d2::Pool::builder()
28///      .max_size(15)
29///      .build(manager)
30///      .unwrap();
31///
32/// for _ in 0..20 {
33///     let pool = pool.clone();
34///     thread::spawn(move || {
35///         let conn = pool.get().unwrap();
36///         // use the connection
37///         // it will be returned to the pool when it falls out of scope.
38///     });
39/// }
40/// ```
41#[derive(Debug)]
42pub struct OracleConnectionManager {
43    connector: oracle::Connector,
44}
45
46impl OracleConnectionManager {
47    /// Initialise the connection manager with the data needed to create new connections.
48    /// Refer to the documentation of `oracle::Connection` for further details on the parameters.
49    ///
50    /// # Example
51    /// ```
52    /// # use r2d2_oracle::OracleConnectionManager;
53    /// let manager = OracleConnectionManager::new("user", "password", "localhost");
54    /// ```
55    pub fn new(username: &str, password: &str, connect_string: &str) -> OracleConnectionManager {
56        OracleConnectionManager {
57            connector: oracle::Connector::new(username, password, connect_string),
58        }
59    }
60
61    /// Initialise the connection manager with the data needed to create new connections using `oracle::Connector`.
62    /// This allows setting additional connection data.
63    ///
64    /// If a connection can be established only with a username, password and connect string, use `new` instead.
65    ///
66    /// # Example
67    /// ```
68    /// # use r2d2_oracle::OracleConnectionManager;
69    /// // connect system/manager as sysdba
70    /// let mut connector = oracle::Connector::new("system", "manager", "");
71    /// connector.privilege(oracle::Privilege::Sysdba);
72    /// let manager = OracleConnectionManager::from_connector(connector);
73    /// ```
74    pub fn from_connector(connector: oracle::Connector) -> OracleConnectionManager {
75        OracleConnectionManager { connector }
76    }
77}
78
79impl r2d2::ManageConnection for OracleConnectionManager {
80    type Connection = oracle::Connection;
81    type Error = oracle::Error;
82
83    fn connect(&self) -> Result<oracle::Connection, oracle::Error> {
84        self.connector.connect()
85    }
86
87    fn is_valid(&self, conn: &mut oracle::Connection) -> Result<(), oracle::Error> {
88        conn.ping()
89    }
90
91    fn has_broken(&self, conn: &mut oracle::Connection) -> bool {
92        !matches!(conn.status(), Ok(oracle::ConnStatus::Normal))
93    }
94}