Skip to content

Commit 3ba416d

Browse files
committed
Add tokio_postgres::GenericClient
We have to make the trait methods differ from the normal methods a bit by adding Sync + Send bounds to the ToStatement parameter which is a bit unfortunate, but necessary until GATs async_trait unnecessary. Closes sfackler#357
1 parent a865207 commit 3ba416d

File tree

6 files changed

+97
-10
lines changed

6 files changed

+97
-10
lines changed

postgres/src/generic_client.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use crate::{Statement, ToStatement, Transaction};
2-
use tokio_postgres::types::ToSql;
3-
use tokio_postgres::{Error, Row};
1+
use crate::types::ToSql;
2+
use crate::{Error, Row, Statement, ToStatement, Transaction};
43

5-
/// A trait allowing abstraction over connections and transactions
4+
/// A trait allowing abstraction over connections and transactions.
65
pub trait GenericClient {
76
/// Like `Client::execute`.
87
fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>

tokio-postgres/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ with-serde_json-1 = ["postgres-types/with-serde_json-1"]
3535
with-uuid-0_8 = ["postgres-types/with-uuid-0_8"]
3636

3737
[dependencies]
38+
async-trait = "0.1"
3839
bytes = "0.5"
3940
byteorder = "1.0"
4041
fallible-iterator = "0.2"

tokio-postgres/src/client.rs

+34-6
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@ use crate::connection::{Request, RequestMessages};
44
use crate::copy_out::CopyOutStream;
55
use crate::query::RowStream;
66
use crate::simple_query::SimpleQueryStream;
7-
use crate::slice_iter;
87
#[cfg(feature = "runtime")]
98
use crate::tls::MakeTlsConnect;
109
use crate::tls::TlsConnect;
11-
use crate::to_statement::ToStatement;
1210
use crate::types::{Oid, ToSql, Type};
1311
#[cfg(feature = "runtime")]
1412
use crate::Socket;
15-
use crate::{copy_in, copy_out, query, CancelToken, CopyInSink, Transaction};
16-
use crate::{prepare, SimpleQueryMessage};
17-
use crate::{simple_query, Row};
18-
use crate::{Error, Statement};
13+
use crate::{
14+
copy_in, copy_out, prepare, query, simple_query, slice_iter, CancelToken, CopyInSink, Error,
15+
GenericClient, Row, SimpleQueryMessage, Statement, ToStatement, Transaction,
16+
};
17+
use async_trait::async_trait;
1918
use bytes::{Buf, BytesMut};
2019
use fallible_iterator::FallibleIterator;
2120
use futures::channel::mpsc;
@@ -495,3 +494,32 @@ impl Client {
495494
self.inner.sender.is_closed()
496495
}
497496
}
497+
498+
#[async_trait]
499+
impl GenericClient for Client {
500+
async fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
501+
where
502+
T: ?Sized + ToStatement + Sync + Send,
503+
{
504+
self.execute(query, params).await
505+
}
506+
507+
async fn query<T>(
508+
&mut self,
509+
query: &T,
510+
params: &[&(dyn ToSql + Sync)],
511+
) -> Result<Vec<Row>, Error>
512+
where
513+
T: ?Sized + ToStatement + Sync + Send,
514+
{
515+
self.query(query, params).await
516+
}
517+
518+
async fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
519+
self.prepare(query).await
520+
}
521+
522+
async fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
523+
self.transaction().await
524+
}
525+
}

tokio-postgres/src/generic_client.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::types::ToSql;
2+
use crate::{Error, Row, Statement, ToStatement, Transaction};
3+
use async_trait::async_trait;
4+
5+
/// A trait allowing abstraction over connections and transactions.
6+
#[async_trait]
7+
pub trait GenericClient {
8+
/// Like `Client::execute`.
9+
async fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
10+
where
11+
T: ?Sized + ToStatement + Sync + Send;
12+
13+
/// Like `Client::query`.
14+
async fn query<T>(
15+
&mut self,
16+
query: &T,
17+
params: &[&(dyn ToSql + Sync)],
18+
) -> Result<Vec<Row>, Error>
19+
where
20+
T: ?Sized + ToStatement + Sync + Send;
21+
22+
/// Like `Client::prepare`.
23+
async fn prepare(&mut self, query: &str) -> Result<Statement, Error>;
24+
25+
/// Like `Client::transaction`.
26+
async fn transaction(&mut self) -> Result<Transaction<'_>, Error>;
27+
}

tokio-postgres/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub use crate::copy_in::CopyInSink;
107107
pub use crate::copy_out::CopyOutStream;
108108
use crate::error::DbError;
109109
pub use crate::error::Error;
110+
pub use crate::generic_client::GenericClient;
110111
pub use crate::portal::Portal;
111112
pub use crate::query::RowStream;
112113
pub use crate::row::{Row, SimpleQueryRow};
@@ -140,6 +141,7 @@ mod connection;
140141
mod copy_in;
141142
mod copy_out;
142143
pub mod error;
144+
mod generic_client;
143145
mod maybe_tls_stream;
144146
mod portal;
145147
mod prepare;

tokio-postgres/src/transaction.rs

+30
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
bind, query, slice_iter, CancelToken, Client, CopyInSink, Error, Portal, Row,
1313
SimpleQueryMessage, Statement, ToStatement,
1414
};
15+
use async_trait::async_trait;
1516
use bytes::Buf;
1617
use futures::TryStreamExt;
1718
use postgres_protocol::message::frontend;
@@ -285,3 +286,32 @@ impl<'a> Transaction<'a> {
285286
})
286287
}
287288
}
289+
290+
#[async_trait]
291+
impl crate::GenericClient for Transaction<'_> {
292+
async fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
293+
where
294+
T: ?Sized + ToStatement + Sync + Send,
295+
{
296+
self.execute(query, params).await
297+
}
298+
299+
async fn query<T>(
300+
&mut self,
301+
query: &T,
302+
params: &[&(dyn ToSql + Sync)],
303+
) -> Result<Vec<Row>, Error>
304+
where
305+
T: ?Sized + ToStatement + Sync + Send,
306+
{
307+
self.query(query, params).await
308+
}
309+
310+
async fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
311+
self.prepare(query).await
312+
}
313+
314+
async fn transaction<'a>(&'a mut self) -> Result<Transaction<'a>, Error> {
315+
self.transaction().await
316+
}
317+
}

0 commit comments

Comments
 (0)