use crate::{
ConnectParams, Connection, ConnectionConfiguration, HdbError, HdbResult, IntoConnectParams,
};
use log::trace;
#[derive(Debug)]
pub struct ConnectionManager {
connect_params: ConnectParams,
connect_config: ConnectionConfiguration,
}
impl ConnectionManager {
pub fn new<P: IntoConnectParams>(p: P) -> HdbResult<Self> {
Ok(Self {
connect_params: p.into_connect_params()?,
connect_config: ConnectionConfiguration::default(),
})
}
pub fn with_configuration<P: IntoConnectParams>(
p: P,
c: ConnectionConfiguration,
) -> HdbResult<Self> {
Ok(Self {
connect_params: p.into_connect_params()?,
connect_config: c,
})
}
}
impl r2d2::ManageConnection for ConnectionManager {
type Connection = Connection;
type Error = HdbError;
fn connect(&self) -> Result<Self::Connection, Self::Error> {
trace!("ConnectionManager::connect()");
Connection::with_configuration(&self.connect_params, &self.connect_config)
}
fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
trace!("ConnectionManager::is_valid()");
conn.query("SELECT 'IsConnectionStillAlive' from dummy")
.map(|_| ())
}
fn has_broken(&self, conn: &mut Self::Connection) -> bool {
trace!("ConnectionManager::has_broken()");
conn.is_broken().unwrap_or(false)
}
}