use std::sync::Arc;
use pg_wired::{AsyncConn, AsyncPool};
use crate::encode::SqlParam;
use crate::error::TypedError;
use crate::row::Row;
#[derive(Debug, Clone)]
pub struct SharedTypedPool {
pool: Arc<AsyncPool>,
}
impl SharedTypedPool {
pub async fn connect(
addr: &str,
user: &str,
password: &str,
database: &str,
size: usize,
) -> Result<Self, pg_wired::PgWireError> {
let pool = AsyncPool::connect(addr, user, password, database, size).await?;
Ok(Self { pool })
}
pub async fn get(&self) -> SharedTypedClient {
let conn = self.pool.get_async().await;
SharedTypedClient { conn }
}
pub fn size(&self) -> usize {
self.pool.size()
}
pub async fn alive_count(&self) -> usize {
self.pool.alive_count().await
}
pub async fn close(&self) -> Result<(), pg_wired::PgWireError> {
self.pool.close().await
}
}
#[derive(Debug, Clone)]
pub struct SharedTypedClient {
conn: Arc<AsyncConn>,
}
impl SharedTypedClient {
pub fn conn(&self) -> &AsyncConn {
&self.conn
}
pub async fn query(&self, sql: &str, params: &[&dyn SqlParam]) -> Result<Vec<Row>, TypedError> {
crate::query::Client::query_on_conn(&self.conn, sql, params).await
}
pub async fn execute(&self, sql: &str, params: &[&dyn SqlParam]) -> Result<u64, TypedError> {
crate::query::Client::execute_on_conn(&self.conn, sql, params).await
}
pub async fn simple_query(&self, sql: &str) -> Result<(), TypedError> {
crate::query::Client::simple_query_on_conn(&self.conn, sql).await
}
pub fn is_alive(&self) -> bool {
self.conn.is_alive()
}
}