use crate::error::Result;
use crate::row::{Row, SimpleQueryMessage};
use crate::types::ToSql;
use crate::Connection;
#[allow(async_fn_in_trait)]
pub trait GenericClient {
async fn query(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>>;
async fn query_one(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Row>;
async fn query_opt(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)])
-> Result<Option<Row>>;
async fn execute(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<u64>;
async fn simple_query(&mut self, sql: &str) -> Result<Vec<SimpleQueryMessage>>;
async fn query_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<Vec<Row>>;
async fn query_typed_one(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<Row>;
async fn query_typed_opt(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<Option<Row>>;
async fn execute_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<u64>;
async fn execute_pipeline(
&mut self,
batch: crate::pipeline::batch::PipelineBatch,
) -> Result<Vec<crate::pipeline::QueryResult>>;
}
impl GenericClient for Connection {
async fn query(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>> {
Connection::query(self, sql, params).await
}
async fn query_one(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Row> {
Connection::query_one(self, sql, params).await
}
async fn query_opt(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Option<Row>> {
Connection::query_opt(self, sql, params).await
}
async fn execute(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<u64> {
Connection::execute(self, sql, params).await
}
async fn simple_query(&mut self, sql: &str) -> Result<Vec<SimpleQueryMessage>> {
Connection::simple_query(self, sql).await
}
async fn query_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<Vec<Row>> {
Connection::query_typed(self, sql, params).await
}
async fn query_typed_one(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<Row> {
Connection::query_typed_one(self, sql, params).await
}
async fn query_typed_opt(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<Option<Row>> {
Connection::query_typed_opt(self, sql, params).await
}
async fn execute_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<u64> {
Connection::execute_typed(self, sql, params).await
}
async fn execute_pipeline(
&mut self,
batch: crate::pipeline::batch::PipelineBatch,
) -> Result<Vec<crate::pipeline::QueryResult>> {
Connection::execute_pipeline(self, batch).await
}
}
pub trait AsPool {
fn with_conn<'a, R, F>(
&'a self,
f: F,
) -> impl std::future::Future<Output = Result<R>> + Send + 'a
where
F: FnOnce(&'a mut Connection) -> futures_core::future::BoxFuture<'a, Result<R>> + Send + 'a,
R: Send + 'a;
}
impl<P> GenericClient for &P
where
P: AsPool + Sync,
{
async fn query(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>> {
let sql = sql.to_owned();
let params_vec: Vec<&(dyn ToSql + Sync)> = params.to_vec();
(**self)
.with_conn(move |c| Box::pin(async move { c.query(&sql, ¶ms_vec).await }))
.await
}
async fn query_one(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Row> {
let sql = sql.to_owned();
let params_vec: Vec<&(dyn ToSql + Sync)> = params.to_vec();
(**self)
.with_conn(move |c| Box::pin(async move { c.query_one(&sql, ¶ms_vec).await }))
.await
}
async fn query_opt(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Option<Row>> {
let sql = sql.to_owned();
let params_vec: Vec<&(dyn ToSql + Sync)> = params.to_vec();
(**self)
.with_conn(move |c| Box::pin(async move { c.query_opt(&sql, ¶ms_vec).await }))
.await
}
async fn execute(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<u64> {
let sql = sql.to_owned();
let params_vec: Vec<&(dyn ToSql + Sync)> = params.to_vec();
(**self)
.with_conn(move |c| Box::pin(async move { c.execute(&sql, ¶ms_vec).await }))
.await
}
async fn simple_query(&mut self, sql: &str) -> Result<Vec<crate::row::SimpleQueryMessage>> {
let sql = sql.to_owned();
(**self)
.with_conn(move |c| Box::pin(async move { c.simple_query(&sql).await }))
.await
}
async fn query_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<Vec<Row>> {
let sql = sql.to_owned();
let params_vec: Vec<(&(dyn ToSql + Sync), crate::Oid)> = params.to_vec();
(**self)
.with_conn(move |c| Box::pin(async move { c.query_typed(&sql, ¶ms_vec).await }))
.await
}
async fn query_typed_one(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<Row> {
let sql = sql.to_owned();
let params_vec: Vec<(&(dyn ToSql + Sync), crate::Oid)> = params.to_vec();
(**self)
.with_conn(move |c| Box::pin(async move { c.query_typed_one(&sql, ¶ms_vec).await }))
.await
}
async fn query_typed_opt(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<Option<Row>> {
let sql = sql.to_owned();
let params_vec: Vec<(&(dyn ToSql + Sync), crate::Oid)> = params.to_vec();
(**self)
.with_conn(move |c| Box::pin(async move { c.query_typed_opt(&sql, ¶ms_vec).await }))
.await
}
async fn execute_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<u64> {
let sql = sql.to_owned();
let params_vec: Vec<(&(dyn ToSql + Sync), crate::Oid)> = params.to_vec();
(**self)
.with_conn(move |c| Box::pin(async move { c.execute_typed(&sql, ¶ms_vec).await }))
.await
}
async fn execute_pipeline(
&mut self,
batch: crate::pipeline::batch::PipelineBatch,
) -> Result<Vec<crate::pipeline::QueryResult>> {
(**self)
.with_conn(move |c| Box::pin(async move { c.execute_pipeline(batch).await }))
.await
}
}
impl GenericClient for crate::PooledConnection {
async fn query(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>> {
Connection::query(self, sql, params).await
}
async fn query_one(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Row> {
Connection::query_one(self, sql, params).await
}
async fn query_opt(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Option<Row>> {
Connection::query_opt(self, sql, params).await
}
async fn execute(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<u64> {
Connection::execute(self, sql, params).await
}
async fn simple_query(&mut self, sql: &str) -> Result<Vec<SimpleQueryMessage>> {
Connection::simple_query(self, sql).await
}
async fn query_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<Vec<Row>> {
Connection::query_typed(self, sql, params).await
}
async fn query_typed_one(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<Row> {
Connection::query_typed_one(self, sql, params).await
}
async fn query_typed_opt(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<Option<Row>> {
Connection::query_typed_opt(self, sql, params).await
}
async fn execute_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), crate::Oid)],
) -> Result<u64> {
Connection::execute_typed(self, sql, params).await
}
async fn execute_pipeline(
&mut self,
batch: crate::pipeline::batch::PipelineBatch,
) -> Result<Vec<crate::pipeline::QueryResult>> {
Connection::execute_pipeline(self, batch).await
}
}