mod connection;
mod transaction;
pub use connection::*;
use deadpool_postgres::{tokio_postgres::NoTls, Manager, ManagerConfig, Object, Pool};
pub use transaction::*;
use crate::{
error::*,
execution::{impl_sql_statement_executor, ExecuteResult},
sql::FromQueryResult,
statements::SqlStatement,
};
pub struct DatabaseConnectionPool {
pool: Pool,
}
impl DatabaseConnectionPool {
pub async fn connect(url: &str) -> Result<Self> {
let tokio_postgres_config: deadpool_postgres::tokio_postgres::Config = url.parse()?;
let manager = Manager::from_config(
tokio_postgres_config,
NoTls,
ManagerConfig {
recycling_method: deadpool_postgres::RecyclingMethod::Fast,
},
);
let pool = Pool::builder(manager).build()?;
Ok(Self { pool })
}
pub async fn get(&self) -> Result<DatabaseConnectionFromPool> {
let client = self.pool.get().await?;
Ok(DatabaseConnectionFromPool { client })
}
}
async fn get_raw_executor(database_connection_pool: &DatabaseConnectionPool) -> Result<Object> {
Ok(database_connection_pool.pool.get().await?)
}
impl_sql_statement_executor! {DatabaseConnectionPool, get_raw_executor}