Merge pull request #525 from colingm/generic-connection

Adding in generic connection trait back to the lib
This commit is contained in:
Steven Fackler 2020-01-02 20:39:37 -05:00 committed by GitHub
commit 6deef22769
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 2 deletions

View File

@ -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;
@ -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> {
self.prepare(query)
}
fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
self.transaction()
}
}

View File

@ -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>;
}

View File

@ -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;
@ -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;

View File

@ -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};
@ -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()
}
}