use std::future::Future;
use tokio_postgres::types::{BorrowToSql, ToSql, Type};
use tokio_postgres::RowStream;
use tokio_postgres::{Error, Row, Statement, ToStatement};
use crate::{Client, ClientWrapper, Transaction};
mod private {
pub trait Sealed {}
}
pub trait GenericClient: Sync + private::Sealed {
fn execute<T>(
&self,
query: &T,
params: &[&(dyn ToSql + Sync)],
) -> impl Future<Output = Result<u64, Error>> + Send
where
T: ?Sized + ToStatement + Sync + Send;
fn execute_raw<P, I, T>(
&self,
statement: &T,
params: I,
) -> impl Future<Output = Result<u64, Error>> + Send
where
T: ?Sized + ToStatement + Sync + Send,
P: BorrowToSql,
I: IntoIterator<Item = P> + Sync + Send,
I::IntoIter: ExactSizeIterator;
fn query<T>(
&self,
query: &T,
params: &[&(dyn ToSql + Sync)],
) -> impl Future<Output = Result<Vec<Row>, Error>> + Send
where
T: ?Sized + ToStatement + Sync + Send;
fn query_one<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> impl Future<Output = Result<Row, Error>> + Send
where
T: ?Sized + ToStatement + Sync + Send;
fn query_opt<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> impl Future<Output = Result<Option<Row>, Error>> + Send
where
T: ?Sized + ToStatement + Sync + Send;
fn query_raw<T, P, I>(
&self,
statement: &T,
params: I,
) -> impl Future<Output = Result<RowStream, Error>> + Send
where
T: ?Sized + ToStatement + Sync + Send,
P: BorrowToSql,
I: IntoIterator<Item = P> + Sync + Send,
I::IntoIter: ExactSizeIterator;
fn prepare(&self, query: &str) -> impl Future<Output = Result<Statement, Error>> + Send;
fn prepare_typed(
&self,
query: &str,
parameter_types: &[Type],
) -> impl Future<Output = Result<Statement, Error>> + Send;
fn prepare_cached(&self, query: &str) -> impl Future<Output = Result<Statement, Error>> + Send;
fn prepare_typed_cached(
&self,
query: &str,
types: &[Type],
) -> impl Future<Output = Result<Statement, Error>> + Send;
fn transaction(&mut self) -> impl Future<Output = Result<Transaction<'_>, Error>> + Send;
fn batch_execute(&self, query: &str) -> impl Future<Output = Result<(), Error>> + Send;
}
impl private::Sealed for Client {}
impl GenericClient for Client {
async fn execute<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
tokio_postgres::Client::execute(self, query, params).await
}
async fn execute_raw<P, I, T>(&self, statement: &T, params: I) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send,
P: BorrowToSql,
I: IntoIterator<Item = P> + Sync + Send,
I::IntoIter: ExactSizeIterator,
{
tokio_postgres::Client::execute_raw(self, statement, params).await
}
async fn query<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
tokio_postgres::Client::query(self, query, params).await
}
async fn query_one<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Row, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
tokio_postgres::Client::query_one(self, statement, params).await
}
async fn query_opt<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Option<Row>, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
tokio_postgres::Client::query_opt(self, statement, 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,
{
tokio_postgres::Client::query_raw(self, statement, params).await
}
async fn prepare(&self, query: &str) -> Result<Statement, Error> {
tokio_postgres::Client::prepare(self, query).await
}
async fn prepare_typed(
&self,
query: &str,
parameter_types: &[Type],
) -> Result<Statement, Error> {
tokio_postgres::Client::prepare_typed(self, query, parameter_types).await
}
async fn prepare_cached(&self, query: &str) -> Result<Statement, Error> {
ClientWrapper::prepare_cached(self, query).await
}
async fn prepare_typed_cached(&self, query: &str, types: &[Type]) -> Result<Statement, Error> {
ClientWrapper::prepare_typed_cached(self, query, types).await
}
async fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
ClientWrapper::transaction(self).await
}
async fn batch_execute(&self, query: &str) -> Result<(), Error> {
tokio_postgres::Client::batch_execute(self, query).await
}
}
impl private::Sealed for Transaction<'_> {}
#[allow(clippy::needless_lifetimes)]
impl GenericClient for Transaction<'_> {
async fn execute<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
tokio_postgres::Transaction::execute(self, query, params).await
}
async fn execute_raw<P, I, T>(&self, statement: &T, params: I) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send,
P: BorrowToSql,
I: IntoIterator<Item = P> + Sync + Send,
I::IntoIter: ExactSizeIterator,
{
tokio_postgres::Transaction::execute_raw(self, statement, params).await
}
async fn query<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
tokio_postgres::Transaction::query(self, query, params).await
}
async fn query_one<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Row, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
tokio_postgres::Transaction::query_one(self, statement, params).await
}
async fn query_opt<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Option<Row>, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
tokio_postgres::Transaction::query_opt(self, statement, 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,
{
tokio_postgres::Transaction::query_raw(self, statement, params).await
}
async fn prepare(&self, query: &str) -> Result<Statement, Error> {
tokio_postgres::Transaction::prepare(self, query).await
}
async fn prepare_typed(
&self,
query: &str,
parameter_types: &[Type],
) -> Result<Statement, Error> {
tokio_postgres::Transaction::prepare_typed(self, query, parameter_types).await
}
async fn prepare_cached(&self, query: &str) -> Result<Statement, Error> {
Transaction::prepare_cached(self, query).await
}
async fn prepare_typed_cached(&self, query: &str, types: &[Type]) -> Result<Statement, Error> {
Transaction::prepare_typed_cached(self, query, types).await
}
#[allow(clippy::needless_lifetimes)]
async fn transaction<'a>(&'a mut self) -> Result<Transaction<'a>, Error> {
Transaction::transaction(self).await
}
async fn batch_execute(&self, query: &str) -> Result<(), Error> {
tokio_postgres::Transaction::batch_execute(self, query).await
}
}