Merge pull request #561 from dvic/add-methods-generic-client

Add missing methods to GenericClient
This commit is contained in:
Steven Fackler 2020-01-27 10:40:01 -05:00 committed by GitHub
commit 5429a79997
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 146 additions and 22 deletions

View File

@ -506,28 +506,72 @@ impl Client {
#[async_trait] #[async_trait]
impl GenericClient for Client { impl GenericClient for Client {
async fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error> async fn execute<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where where
T: ?Sized + ToStatement + Sync + Send, T: ?Sized + ToStatement + Sync + Send,
{ {
self.execute(query, params).await self.execute(query, params).await
} }
async fn query<T>( async fn execute_raw<'b, I, T>(&self, statement: &T, params: I) -> Result<u64, Error>
&mut self, where
query: &T, T: ?Sized + ToStatement + Sync + Send,
params: &[&(dyn ToSql + Sync)], I: IntoIterator<Item = &'b dyn ToSql> + Sync + Send,
) -> Result<Vec<Row>, Error> I::IntoIter: ExactSizeIterator,
{
self.execute_raw(statement, params).await
}
async fn query<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>, Error>
where where
T: ?Sized + ToStatement + Sync + Send, T: ?Sized + ToStatement + Sync + Send,
{ {
self.query(query, params).await self.query(query, params).await
} }
async fn prepare(&mut self, query: &str) -> Result<Statement, Error> { 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
}
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
}
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
}
async fn prepare(&self, query: &str) -> Result<Statement, Error> {
self.prepare(query).await self.prepare(query).await
} }
async fn prepare_typed(
&self,
query: &str,
parameter_types: &[Type],
) -> Result<Statement, Error> {
self.prepare_typed(query, parameter_types).await
}
async fn transaction(&mut self) -> Result<Transaction<'_>, Error> { async fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
self.transaction().await 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 crate::{Error, Row, Statement, ToStatement, Transaction};
use async_trait::async_trait; use async_trait::async_trait;
@ -6,21 +7,56 @@ use async_trait::async_trait;
#[async_trait] #[async_trait]
pub trait GenericClient { pub trait GenericClient {
/// Like `Client::execute`. /// Like `Client::execute`.
async fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error> async fn execute<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where where
T: ?Sized + ToStatement + Sync + Send; 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`. /// Like `Client::query`.
async fn query<T>( async fn query<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>, Error>
&mut self,
query: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Vec<Row>, Error>
where where
T: ?Sized + ToStatement + Sync + Send; 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`. /// Like `Client::prepare`.
async fn prepare(&mut self, query: &str) -> Result<Statement, Error>; async fn prepare(&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`. /// Like `Client::transaction`.
async fn transaction(&mut self) -> Result<Transaction<'_>, Error>; async fn transaction(&mut self) -> Result<Transaction<'_>, Error>;

View File

@ -289,28 +289,72 @@ impl<'a> Transaction<'a> {
#[async_trait] #[async_trait]
impl crate::GenericClient for Transaction<'_> { impl crate::GenericClient for Transaction<'_> {
async fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error> async fn execute<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where where
T: ?Sized + ToStatement + Sync + Send, T: ?Sized + ToStatement + Sync + Send,
{ {
self.execute(query, params).await self.execute(query, params).await
} }
async fn query<T>( async fn execute_raw<'b, I, T>(&self, statement: &T, params: I) -> Result<u64, Error>
&mut self, where
query: &T, T: ?Sized + ToStatement + Sync + Send,
params: &[&(dyn ToSql + Sync)], I: IntoIterator<Item = &'b dyn ToSql> + Sync + Send,
) -> Result<Vec<Row>, Error> I::IntoIter: ExactSizeIterator,
{
self.execute_raw(statement, params).await
}
async fn query<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>, Error>
where where
T: ?Sized + ToStatement + Sync + Send, T: ?Sized + ToStatement + Sync + Send,
{ {
self.query(query, params).await self.query(query, params).await
} }
async fn prepare(&mut self, query: &str) -> Result<Statement, Error> { 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
}
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
}
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
}
async fn prepare(&self, query: &str) -> Result<Statement, Error> {
self.prepare(query).await self.prepare(query).await
} }
async fn prepare_typed(
&self,
query: &str,
parameter_types: &[Type],
) -> Result<Statement, Error> {
self.prepare_typed(query, parameter_types).await
}
#[allow(clippy::needless_lifetimes)] #[allow(clippy::needless_lifetimes)]
async fn transaction<'a>(&'a mut self) -> Result<Transaction<'a>, Error> { async fn transaction<'a>(&'a mut self) -> Result<Transaction<'a>, Error> {
self.transaction().await self.transaction().await