Add missing methods to GenericClient

This adds `execute_raw`,  `query_one`, `query_opt`, `query_raw`,
and `prepare_typed` to the generic trait introduced in #525.
This commit is contained in:
dvic 2020-01-27 10:20:30 +01:00
parent a54ee29704
commit afc9b2835d
No known key found for this signature in database
GPG Key ID: FF6528EC6B2AA032
3 changed files with 155 additions and 1 deletions

View File

@ -506,6 +506,7 @@ impl Client {
#[async_trait]
impl GenericClient for Client {
/// Like `Client::execute`.
async fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send,
@ -513,6 +514,17 @@ impl GenericClient for Client {
self.execute(query, params).await
}
/// Like `Client::execute_raw`.
async fn execute_raw<'b, I, T>(&self, statement: &T, params: I) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send,
I: IntoIterator<Item = &'b dyn ToSql> + Sync + Send,
I::IntoIter: ExactSizeIterator,
{
self.execute_raw(statement, params).await
}
/// Like `Client::query`.
async fn query<T>(
&mut self,
query: &T,
@ -524,10 +536,55 @@ impl GenericClient for Client {
self.query(query, params).await
}
/// Like `Client::query_one`.
async fn query_one<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Row, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
self.query_one(statement, params).await
}
/// Like `Client::query_opt`.
async fn query_opt<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Option<Row>, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
self.query_opt(statement, params).await
}
/// Like `Client::query_raw`.
async fn query_raw<'b, T, I>(&self, statement: &T, params: I) -> Result<RowStream, Error>
where
T: ?Sized + ToStatement + Sync + Send,
I: IntoIterator<Item = &'b dyn ToSql> + Sync + Send,
I::IntoIter: ExactSizeIterator,
{
self.query_raw(statement, params).await
}
/// Like `Client::prepare`.
async fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
self.prepare(query).await
}
/// Like `Client::prepare_typed`.
async fn prepare_typed(
&self,
query: &str,
parameter_types: &[Type],
) -> Result<Statement, Error> {
self.prepare_typed(query, parameter_types).await
}
/// Like `Client::transaction`.
async fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
self.transaction().await
}

View File

@ -1,4 +1,5 @@
use crate::types::ToSql;
use crate::query::RowStream;
use crate::types::{ToSql, Type};
use crate::{Error, Row, Statement, ToStatement, Transaction};
use async_trait::async_trait;
@ -10,6 +11,13 @@ pub trait GenericClient {
where
T: ?Sized + ToStatement + Sync + Send;
/// Like `Client::execute_raw`.
async fn execute_raw<'b, I, T>(&self, statement: &T, params: I) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send,
I: IntoIterator<Item = &'b dyn ToSql> + Sync + Send,
I::IntoIter: ExactSizeIterator;
/// Like `Client::query`.
async fn query<T>(
&mut self,
@ -19,9 +27,41 @@ pub trait GenericClient {
where
T: ?Sized + ToStatement + Sync + Send;
/// Like `Client::query_one`.
async fn query_one<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Row, Error>
where
T: ?Sized + ToStatement + Sync + Send;
/// Like `Client::query_opt`.
async fn query_opt<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Option<Row>, Error>
where
T: ?Sized + ToStatement + Sync + Send;
/// Like `Client::query_raw`.
async fn query_raw<'b, T, I>(&self, statement: &T, params: I) -> Result<RowStream, Error>
where
T: ?Sized + ToStatement + Sync + Send,
I: IntoIterator<Item = &'b dyn ToSql> + Sync + Send,
I::IntoIter: ExactSizeIterator;
/// Like `Client::prepare`.
async fn prepare(&mut self, query: &str) -> Result<Statement, Error>;
/// Like `Client::prepare_typed`.
async fn prepare_typed(
&self,
query: &str,
parameter_types: &[Type],
) -> Result<Statement, Error>;
/// Like `Client::transaction`.
async fn transaction(&mut self) -> Result<Transaction<'_>, Error>;
}

View File

@ -289,6 +289,7 @@ impl<'a> Transaction<'a> {
#[async_trait]
impl crate::GenericClient for Transaction<'_> {
/// Like `Transaction::execute`.
async fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send,
@ -296,6 +297,17 @@ impl crate::GenericClient for Transaction<'_> {
self.execute(query, params).await
}
/// Like `Transaction::execute_raw`.
async fn execute_raw<'b, I, T>(&self, statement: &T, params: I) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send,
I: IntoIterator<Item = &'b dyn ToSql> + Sync + Send,
I::IntoIter: ExactSizeIterator,
{
self.execute_raw(statement, params).await
}
/// Like `Transaction::query`.
async fn query<T>(
&mut self,
query: &T,
@ -307,10 +319,55 @@ impl crate::GenericClient for Transaction<'_> {
self.query(query, params).await
}
/// Like `Transaction::query_one`.
async fn query_one<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Row, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
self.query_one(statement, params).await
}
/// Like `Transaction::query_opt`.
async fn query_opt<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Option<Row>, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
self.query_opt(statement, params).await
}
/// Like `Transaction::query_raw`.
async fn query_raw<'b, T, I>(&self, statement: &T, params: I) -> Result<RowStream, Error>
where
T: ?Sized + ToStatement + Sync + Send,
I: IntoIterator<Item = &'b dyn ToSql> + Sync + Send,
I::IntoIter: ExactSizeIterator,
{
self.query_raw(statement, params).await
}
/// Like `Transaction::prepare`.
async fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
self.prepare(query).await
}
/// Like `Transaction::prepare_typed`.
async fn prepare_typed(
&self,
query: &str,
parameter_types: &[Type],
) -> Result<Statement, Error> {
self.prepare_typed(query, parameter_types).await
}
/// Like `Transaction::transaction`.
#[allow(clippy::needless_lifetimes)]
async fn transaction<'a>(&'a mut self) -> Result<Transaction<'a>, Error> {
self.transaction().await