use async_trait::async_trait;
use tokio_postgres::{
types::BorrowToSql, Client, Error, RowStream, Statement, ToStatement, Transaction,
};
#[async_trait]
pub trait GenericClient: Send + Sync {
async fn prepare(&self, query: &str) -> Result<Statement, Error>;
async fn execute<T>(
&self,
query: &T,
params: &[&(dyn tokio_postgres::types::ToSql + Sync)],
) -> Result<u64, Error>
where
T: ?Sized + tokio_postgres::ToStatement + Sync + Send;
async fn query_one<T>(
&self,
statement: &T,
params: &[&(dyn tokio_postgres::types::ToSql + Sync)],
) -> Result<tokio_postgres::Row, Error>
where
T: ?Sized + tokio_postgres::ToStatement + Sync + Send;
async fn query_opt<T>(
&self,
statement: &T,
params: &[&(dyn tokio_postgres::types::ToSql + Sync)],
) -> Result<Option<tokio_postgres::Row>, Error>
where
T: ?Sized + tokio_postgres::ToStatement + Sync + Send;
async fn query<T>(
&self,
query: &T,
params: &[&(dyn tokio_postgres::types::ToSql + Sync)],
) -> Result<Vec<tokio_postgres::Row>, Error>
where
T: ?Sized + tokio_postgres::ToStatement + Sync + Send;
async fn query_raw<T, P, I>(&self, statement: &T, params: I) -> Result<RowStream, Error>
where
T: ?Sized + ToStatement + Sync + Send,
P: BorrowToSql,
I: IntoIterator<Item = P> + Sync + Send,
I::IntoIter: ExactSizeIterator;
}
#[async_trait]
impl GenericClient for Transaction<'_> {
async fn prepare(&self, query: &str) -> Result<Statement, Error> {
Transaction::prepare(self, query).await
}
async fn execute<T>(
&self,
query: &T,
params: &[&(dyn tokio_postgres::types::ToSql + Sync)],
) -> Result<u64, Error>
where
T: ?Sized + tokio_postgres::ToStatement + Sync + Send,
{
Transaction::execute(self, query, params).await
}
async fn query_one<T>(
&self,
statement: &T,
params: &[&(dyn tokio_postgres::types::ToSql + Sync)],
) -> Result<tokio_postgres::Row, Error>
where
T: ?Sized + tokio_postgres::ToStatement + Sync + Send,
{
Transaction::query_one(self, statement, params).await
}
async fn query_opt<T>(
&self,
statement: &T,
params: &[&(dyn tokio_postgres::types::ToSql + Sync)],
) -> Result<Option<tokio_postgres::Row>, Error>
where
T: ?Sized + tokio_postgres::ToStatement + Sync + Send,
{
Transaction::query_opt(self, statement, params).await
}
async fn query<T>(
&self,
query: &T,
params: &[&(dyn tokio_postgres::types::ToSql + Sync)],
) -> Result<Vec<tokio_postgres::Row>, Error>
where
T: ?Sized + tokio_postgres::ToStatement + Sync + Send,
{
Transaction::query(self, query, params).await
}
async fn query_raw<T, P, I>(&self, statement: &T, params: I) -> Result<RowStream, Error>
where
T: ?Sized + ToStatement + Sync + Send,
P: BorrowToSql,
I: IntoIterator<Item = P> + Sync + Send,
I::IntoIter: ExactSizeIterator,
{
Transaction::query_raw(self, statement, params).await
}
}
#[async_trait]
impl GenericClient for Client {
async fn prepare(&self, query: &str) -> Result<Statement, Error> {
Client::prepare(self, query).await
}
async fn execute<T>(
&self,
query: &T,
params: &[&(dyn tokio_postgres::types::ToSql + Sync)],
) -> Result<u64, Error>
where
T: ?Sized + tokio_postgres::ToStatement + Sync + Send,
{
Client::execute(self, query, params).await
}
async fn query_one<T>(
&self,
statement: &T,
params: &[&(dyn tokio_postgres::types::ToSql + Sync)],
) -> Result<tokio_postgres::Row, Error>
where
T: ?Sized + tokio_postgres::ToStatement + Sync + Send,
{
Client::query_one(self, statement, params).await
}
async fn query_opt<T>(
&self,
statement: &T,
params: &[&(dyn tokio_postgres::types::ToSql + Sync)],
) -> Result<Option<tokio_postgres::Row>, Error>
where
T: ?Sized + tokio_postgres::ToStatement + Sync + Send,
{
Client::query_opt(self, statement, params).await
}
async fn query<T>(
&self,
query: &T,
params: &[&(dyn tokio_postgres::types::ToSql + Sync)],
) -> Result<Vec<tokio_postgres::Row>, Error>
where
T: ?Sized + tokio_postgres::ToStatement + Sync + Send,
{
Client::query(self, query, params).await
}
async fn query_raw<T, P, I>(&self, statement: &T, params: I) -> Result<RowStream, Error>
where
T: ?Sized + ToStatement + Sync + Send,
P: BorrowToSql,
I: IntoIterator<Item = P> + Sync + Send,
I::IntoIter: ExactSizeIterator,
{
Client::query_raw(self, statement, params).await
}
}