Skip to content

Add tokio_postgres::GenericClient #542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions postgres/src/generic_client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{Statement, ToStatement, Transaction};
use tokio_postgres::types::ToSql;
use tokio_postgres::{Error, Row};
use crate::types::ToSql;
use crate::{Error, Row, Statement, ToStatement, Transaction};

/// A trait allowing abstraction over connections and transactions
/// A trait allowing abstraction over connections and transactions.
pub trait GenericClient {
/// Like `Client::execute`.
fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
Expand Down
1 change: 1 addition & 0 deletions tokio-postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ with-serde_json-1 = ["postgres-types/with-serde_json-1"]
with-uuid-0_8 = ["postgres-types/with-uuid-0_8"]

[dependencies]
async-trait = "0.1"
bytes = "0.5"
byteorder = "1.0"
fallible-iterator = "0.2"
Expand Down
40 changes: 34 additions & 6 deletions tokio-postgres/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ use crate::connection::{Request, RequestMessages};
use crate::copy_out::CopyOutStream;
use crate::query::RowStream;
use crate::simple_query::SimpleQueryStream;
use crate::slice_iter;
#[cfg(feature = "runtime")]
use crate::tls::MakeTlsConnect;
use crate::tls::TlsConnect;
use crate::to_statement::ToStatement;
use crate::types::{Oid, ToSql, Type};
#[cfg(feature = "runtime")]
use crate::Socket;
use crate::{copy_in, copy_out, query, CancelToken, CopyInSink, Transaction};
use crate::{prepare, SimpleQueryMessage};
use crate::{simple_query, Row};
use crate::{Error, Statement};
use crate::{
copy_in, copy_out, prepare, query, simple_query, slice_iter, CancelToken, CopyInSink, Error,
GenericClient, Row, SimpleQueryMessage, Statement, ToStatement, Transaction,
};
use async_trait::async_trait;
use bytes::{Buf, BytesMut};
use fallible_iterator::FallibleIterator;
use futures::channel::mpsc;
Expand Down Expand Up @@ -495,3 +494,32 @@ impl Client {
self.inner.sender.is_closed()
}
}

#[async_trait]
impl GenericClient for Client {
async fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
self.execute(query, params).await
}

async fn query<T>(
&mut self,
query: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Vec<Row>, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
self.query(query, params).await
}

async fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
self.prepare(query).await
}

async fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
self.transaction().await
}
}
27 changes: 27 additions & 0 deletions tokio-postgres/src/generic_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::types::ToSql;
use crate::{Error, Row, Statement, ToStatement, Transaction};
use async_trait::async_trait;

/// A trait allowing abstraction over connections and transactions.
#[async_trait]
pub trait GenericClient {
/// Like `Client::execute`.
async fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send;

/// Like `Client::query`.
async fn query<T>(
&mut self,
query: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Vec<Row>, Error>
where
T: ?Sized + ToStatement + Sync + Send;

/// Like `Client::prepare`.
async fn prepare(&mut self, query: &str) -> Result<Statement, Error>;

/// Like `Client::transaction`.
async fn transaction(&mut self) -> Result<Transaction<'_>, Error>;
}
2 changes: 2 additions & 0 deletions tokio-postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub use crate::copy_in::CopyInSink;
pub use crate::copy_out::CopyOutStream;
use crate::error::DbError;
pub use crate::error::Error;
pub use crate::generic_client::GenericClient;
pub use crate::portal::Portal;
pub use crate::query::RowStream;
pub use crate::row::{Row, SimpleQueryRow};
Expand Down Expand Up @@ -140,6 +141,7 @@ mod connection;
mod copy_in;
mod copy_out;
pub mod error;
mod generic_client;
mod maybe_tls_stream;
mod portal;
mod prepare;
Expand Down
31 changes: 31 additions & 0 deletions tokio-postgres/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
bind, query, slice_iter, CancelToken, Client, CopyInSink, Error, Portal, Row,
SimpleQueryMessage, Statement, ToStatement,
};
use async_trait::async_trait;
use bytes::Buf;
use futures::TryStreamExt;
use postgres_protocol::message::frontend;
Expand Down Expand Up @@ -285,3 +286,33 @@ impl<'a> Transaction<'a> {
})
}
}

#[async_trait]
impl crate::GenericClient for Transaction<'_> {
async fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
self.execute(query, params).await
}

async fn query<T>(
&mut self,
query: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Vec<Row>, Error>
where
T: ?Sized + ToStatement + Sync + Send,
{
self.query(query, params).await
}

async fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
self.prepare(query).await
}

#[allow(clippy::needless_lifetimes)]
async fn transaction<'a>(&'a mut self) -> Result<Transaction<'a>, Error> {
self.transaction().await
}
}