use std::time::Duration;
use super::Config;
use crate::error::Error;
#[derive(Clone, PartialEq, Eq)]
pub struct PoolConfig {
connection_config: Config,
min_connections: Option<usize>,
max_connections: Option<usize>,
connection_increment: Option<usize>,
ping_interval: Option<Duration>,
ping_timeout: Option<Duration>,
}
impl PoolConfig {
pub(crate) fn validate(&self) -> Result<(), Error> {
if self.max_connections() < self.min_connections() {
Err(Error::pool_max_less_than_min())
} else if self.connection_increment() == 0
&& self.max_connections() != self.min_connections()
{
Err(Error::pool_increment_zero())
} else {
self.connection_config.validate()
}
}
pub fn auth_mode(&self) -> u8 {
self.connection_config.auth_mode()
}
pub fn cclass(&self) -> Option<&str> {
self.connection_config.cclass()
}
pub fn connection_config(&self) -> &Config {
&self.connection_config
}
pub fn connection_increment(&self) -> usize {
self.connection_increment.unwrap_or(1)
}
pub fn driver_name(&self) -> &str {
self.connection_config.driver_name()
}
pub fn get_connect_descriptor(&self) -> String {
self.connection_config.get_connect_descriptor()
}
pub fn machine(&self) -> &str {
self.connection_config.machine()
}
pub fn max_connections(&self) -> usize {
self.max_connections.unwrap_or(2)
}
pub fn min_connections(&self) -> usize {
self.min_connections.unwrap_or(1)
}
pub fn osuser(&self) -> &str {
self.connection_config.osuser()
}
pub fn ping_interval(&self) -> Option<Duration> {
self.ping_interval
}
pub fn ping_timeout(&self) -> Duration {
self.ping_timeout.unwrap_or(Duration::from_secs(5))
}
pub fn program(&self) -> &str {
self.connection_config.program()
}
pub fn set_auth_mode(mut self, value: u8) -> Self {
self.connection_config = self.connection_config.set_auth_mode(value);
self
}
pub fn set_cclass(mut self, value: impl Into<String>) -> Self {
self.connection_config = self.connection_config.set_cclass(value);
self
}
pub fn set_config_dir(mut self, value: &str) -> Self {
self.connection_config = self.connection_config.set_config_dir(value);
self
}
pub fn set_connection_increment(mut self, value: usize) -> Self {
self.connection_increment = Some(value);
self
}
pub fn set_connect_string(
mut self,
connect_string: &str,
) -> Result<Self, Error> {
self.connection_config =
self.connection_config.set_connect_string(connect_string)?;
Ok(self)
}
pub fn set_credentials(self, user: &str, password: &str) -> Self {
self.set_user(user).set_password(password)
}
pub fn set_driver_name(mut self, value: impl Into<String>) -> Self {
self.connection_config = self.connection_config.set_driver_name(value);
self
}
pub fn set_machine(
mut self,
value: impl Into<String>,
) -> Result<Self, Error> {
self.connection_config = self.connection_config.set_machine(value)?;
Ok(self)
}
pub fn set_max_connections(mut self, value: usize) -> Self {
self.max_connections = Some(value);
self
}
pub fn set_min_connections(mut self, value: usize) -> Self {
self.min_connections = Some(value);
self
}
pub fn set_osuser(
mut self,
value: impl Into<String>,
) -> Result<Self, Error> {
self.connection_config = self.connection_config.set_osuser(value)?;
Ok(self)
}
pub fn set_password(mut self, value: &str) -> Self {
self.connection_config = self.connection_config.set_password(value);
self
}
pub fn set_ping_interval(mut self, value: Option<Duration>) -> Self {
self.ping_interval = value;
self
}
pub fn set_ping_timeout(mut self, value: Duration) -> Self {
self.ping_timeout = Some(value);
self
}
pub fn set_program(
mut self,
value: impl Into<String>,
) -> Result<Self, Error> {
self.connection_config = self.connection_config.set_program(value)?;
Ok(self)
}
pub fn set_stmtcachesize(mut self, value: usize) -> Self {
self.connection_config =
self.connection_config.set_stmtcachesize(value);
self
}
pub fn set_terminal(mut self, value: impl Into<String>) -> Self {
self.connection_config = self.connection_config.set_terminal(value);
self
}
pub fn set_user(mut self, value: &str) -> Self {
self.connection_config = self.connection_config.set_user(value);
self
}
pub fn set_wallet_location(mut self, value: impl Into<String>) -> Self {
self.connection_config =
self.connection_config.set_wallet_location(value);
self
}
pub fn set_wallet_password(mut self, value: &str) -> Self {
self.connection_config =
self.connection_config.set_wallet_password(value);
self
}
pub fn stmtcachesize(&self) -> usize {
self.connection_config.stmtcachesize()
}
pub fn terminal(&self) -> &str {
self.connection_config.terminal()
}
pub fn user(&self) -> Option<&str> {
self.connection_config.user()
}
pub fn wallet_location(&self) -> Option<&str> {
self.connection_config.wallet_location()
}
}
impl Default for PoolConfig {
fn default() -> Self {
Self {
connection_config: Config::default(),
min_connections: None,
max_connections: None,
connection_increment: None,
ping_interval: Some(Duration::from_secs(60)),
ping_timeout: None,
}
}
}