Skip to content

Adding in generic connection trait back to the lib #525

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 5 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
24 changes: 23 additions & 1 deletion postgres/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
CancelToken, Config, CopyInWriter, CopyOutReader, RowIter, Statement, ToStatement, Transaction,
CancelToken, Config, CopyInWriter, CopyOutReader, GenericConnection, RowIter, Statement,
ToStatement, Transaction,
};
use std::ops::{Deref, DerefMut};
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -492,3 +493,24 @@ impl Client {
self.client.is_closed()
}
}

impl GenericConnection for Client {
fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
T: ?Sized + ToStatement,
{
self.execute(query, params)
}
fn query<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>, Error>
where
T: ?Sized + ToStatement,
{
self.query(query, params)
}
fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems strange that you can prepare a statement but not use it through this interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops sorry I didn't even realize that query (and execute) had a generic, I have updated them to include the same trait and generic as the normal implementations

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does mean that the trait isn't object safe, if that's relevant for what you're trying to do with it.

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

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

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

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

/// Like `Client::transaction`.
fn transaction(&mut self) -> Result<Transaction<'_>, Error>;
}
2 changes: 2 additions & 0 deletions postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub use crate::copy_in_writer::CopyInWriter;
pub use crate::copy_out_reader::CopyOutReader;
#[doc(no_inline)]
pub use crate::error::Error;
pub use crate::generic_connection::GenericConnection;
#[doc(no_inline)]
pub use crate::row::{Row, SimpleQueryRow};
pub use crate::row_iter::RowIter;
Expand All @@ -74,6 +75,7 @@ mod client;
pub mod config;
mod copy_in_writer;
mod copy_out_reader;
mod generic_connection;
mod lazy_pin;
mod row_iter;
mod transaction;
Expand Down
24 changes: 23 additions & 1 deletion postgres/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
CancelToken, CopyInWriter, CopyOutReader, Portal, RowIter, Rt, Statement, ToStatement,
CancelToken, CopyInWriter, CopyOutReader, GenericConnection, Portal, RowIter, Rt, Statement,
ToStatement,
};
use tokio::runtime::Runtime;
use tokio_postgres::types::{ToSql, Type};
Expand Down Expand Up @@ -184,3 +185,24 @@ impl<'a> Transaction<'a> {
})
}
}

impl<'a> GenericConnection for Transaction<'a> {
fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
T: ?Sized + ToStatement,
{
self.execute(query, params)
}
fn query<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>, Error>
where
T: ?Sized + ToStatement,
{
self.query(query, params)
}
fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
self.prepare(query)
}
fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
self.transaction()
}
}