#[macro_use]
mod macros;
pub mod impls;
pub mod operation;
pub(crate) mod row;
pub mod statements;
pub mod types;
#[cfg(feature = "vista")]
pub mod vista;
use sqlx::Connection as _;
use sqlx::postgres::{PgConnection, PgPool, PgPoolOptions};
pub use types::{AnyPostgresType, PostgresType};
crate::define_typed_ident!(
PgIdent,
pg_ident,
AnyPostgresType,
crate::condition::PostgresCondition
);
#[derive(Clone)]
pub struct PostgresDB {
pool: PgPool,
}
impl PostgresDB {
pub async fn connect(url: &str) -> Result<Self, sqlx::Error> {
PgConnection::connect(url).await?.close().await?;
let pool = PgPoolOptions::new().connect_lazy(url)?;
Ok(Self { pool })
}
pub fn pool(&self) -> &PgPool {
&self.pool
}
pub async fn aggregate(
&self,
select: &statements::PostgresSelect,
func: &str,
column: impl vantage_expressions::Expressive<AnyPostgresType>,
) -> vantage_core::Result<AnyPostgresType> {
use vantage_expressions::ExprDataSource;
let expr = select.as_aggregate(func, column);
let result = self.execute(&expr).await?;
Ok(crate::sql_exec::first_cell::<Self>(result))
}
}