use std::net::ToSocketAddrs;
use crate::GenericClient;
pub struct MysqlTcpConnection<'s, A: ToSocketAddrs> {
pub addr: A,
pub con_info: super::ConnectInfo<'s>,
}
impl<A: ToSocketAddrs + Send + Sync + 'static> r2d2::ManageConnection for MysqlTcpConnection<'static, A> {
type Connection = super::BlockingClient<std::net::TcpStream>;
type Error = super::CommunicationError;
fn connect(&self) -> Result<Self::Connection, Self::Error> {
let stream = std::net::TcpStream::connect(&self.addr)?;
super::BlockingClient::handshake(stream, &self.con_info)
}
fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
conn.fetch_all("Select 1")
.map_err(From::from)
.and_then(|mut s| s.drop_all_rows())
}
fn has_broken(&self, conn: &mut Self::Connection) -> bool {
self.is_valid(conn).is_err()
}
}
impl<M: r2d2::ManageConnection> GenericClient for r2d2::PooledConnection<M>
where
M::Connection: GenericClient,
{
type Stream = <M::Connection as GenericClient>::Stream;
fn stream(&self) -> &Self::Stream {
M::Connection::stream(self)
}
fn stream_mut(&mut self) -> &mut Self::Stream {
M::Connection::stream_mut(self)
}
fn capability(&self) -> crate::protos::CapabilityFlags {
M::Connection::capability(self)
}
}
#[cfg(feature = "autossl")]
#[cfg_attr(docsrs, doc(cfg(feature = "autossl")))]
pub struct MysqlConnection<'s, A: ToSocketAddrs> {
pub addr: A,
pub server_name: rustls::ServerName,
pub con_info: super::autossl_client::SSLConnectInfo<'s>,
}
#[cfg(feature = "autossl")]
impl<A: ToSocketAddrs + Send + Sync + 'static> r2d2::ManageConnection for MysqlConnection<'static, A> {
type Connection = super::BlockingClient<super::autossl_client::DynamicStream>;
type Error = super::autossl_client::ConnectionError;
fn connect(&self) -> Result<Self::Connection, Self::Error> {
super::BlockingClient::connect_autossl(&self.addr, self.server_name.clone(), &self.con_info)
}
fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
conn.fetch_all("Select 1")
.map_err(From::from)
.and_then(|mut s| s.drop_all_rows())
.map_err(From::from)
}
fn has_broken(&self, conn: &mut Self::Connection) -> bool {
self.is_valid(conn).is_err()
}
}