Adding in generic connection trait back to the lib

This commit is contained in:
Colin Maxfield
2019-12-20 20:35:17 -05:00
parent 37d0a83434
commit 3592e0553e
4 changed files with 52 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
use crate::{Config, CopyInWriter, CopyOutReader, RowIter, Statement, ToStatement, Transaction};
use crate::{Config, CopyInWriter, CopyOutReader, GenericConnection, RowIter, Statement, ToStatement, Transaction};
use std::ops::{Deref, DerefMut};
use tokio::runtime::Runtime;
use tokio_postgres::tls::{MakeTlsConnect, TlsConnect};
@@ -450,3 +450,18 @@ impl Client {
self.client.is_closed()
}
}
impl GenericConnection for Client {
fn execute(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error> {
self.execute(query, params)
}
fn query(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>, Error> {
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,18 @@
use crate::{Statement, 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(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>;
/// Like `Client::query`.
fn query(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>, Error>;
/// Like `Client::prepare`.
fn prepare(&mut self, query: &str) -> Result<Statement, Error>;
/// Like `Client::transaction`.
fn transaction(&mut self) -> Result<Transaction<'_>, Error>;
}

View File

@@ -60,6 +60,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;
@@ -72,6 +73,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,4 +1,4 @@
use crate::{CopyInWriter, CopyOutReader, Portal, RowIter, Rt, Statement, ToStatement};
use crate::{CopyInWriter, CopyOutReader, GenericConnection, Portal, RowIter, Rt, Statement, ToStatement};
use tokio::runtime::Runtime;
use tokio_postgres::types::{ToSql, Type};
use tokio_postgres::{Error, Row, SimpleQueryMessage};
@@ -177,3 +177,18 @@ impl<'a> Transaction<'a> {
})
}
}
impl<'a> GenericConnection for Transaction<'a> {
fn execute(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error> {
self.execute(query, params)
}
fn query(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>, Error> {
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()
}
}