From 3592e0553e5a997cb20bc3ac4d968bcd7248548a Mon Sep 17 00:00:00 2001 From: Colin Maxfield Date: Fri, 20 Dec 2019 20:35:17 -0500 Subject: [PATCH 1/4] Adding in generic connection trait back to the lib --- postgres/src/client.rs | 17 ++++++++++++++++- postgres/src/generic_connection.rs | 18 ++++++++++++++++++ postgres/src/lib.rs | 2 ++ postgres/src/transaction.rs | 17 ++++++++++++++++- 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 postgres/src/generic_connection.rs diff --git a/postgres/src/client.rs b/postgres/src/client.rs index 3e69670d..c4a18b79 100644 --- a/postgres/src/client.rs +++ b/postgres/src/client.rs @@ -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 { + self.execute(query, params) + } + fn query(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result, Error> { + self.query(query, params) + } + fn prepare(&mut self, query: &str) -> Result { + self.prepare(query) + } + fn transaction(&mut self) -> Result, Error> { + self.transaction() + } +} diff --git a/postgres/src/generic_connection.rs b/postgres/src/generic_connection.rs new file mode 100644 index 00000000..fcf1755e --- /dev/null +++ b/postgres/src/generic_connection.rs @@ -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; + + /// Like `Client::query`. + fn query(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result, Error>; + + /// Like `Client::prepare`. + fn prepare(&mut self, query: &str) -> Result; + + /// Like `Client::transaction`. + fn transaction(&mut self) -> Result, Error>; +} diff --git a/postgres/src/lib.rs b/postgres/src/lib.rs index 801eeb9c..0831b80a 100644 --- a/postgres/src/lib.rs +++ b/postgres/src/lib.rs @@ -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; diff --git a/postgres/src/transaction.rs b/postgres/src/transaction.rs index 010e8b62..f3288029 100644 --- a/postgres/src/transaction.rs +++ b/postgres/src/transaction.rs @@ -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 { + self.execute(query, params) + } + fn query(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result, Error> { + self.query(query, params) + } + fn prepare(&mut self, query: &str) -> Result { + self.prepare(query) + } + fn transaction(&mut self) -> Result, Error> { + self.transaction() + } +} From e8aef6579e51155bde223c8a26ee6f8a4b3d6bd9 Mon Sep 17 00:00:00 2001 From: Colin Maxfield Date: Fri, 20 Dec 2019 23:28:32 -0500 Subject: [PATCH 2/4] Fixing fmt on the files I changed, forgot to check prior to previous commit --- postgres/src/client.rs | 5 ++++- postgres/src/generic_connection.rs | 18 +++++++++--------- postgres/src/transaction.rs | 4 +++- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/postgres/src/client.rs b/postgres/src/client.rs index c4a18b79..b24a568a 100644 --- a/postgres/src/client.rs +++ b/postgres/src/client.rs @@ -1,4 +1,7 @@ -use crate::{Config, CopyInWriter, CopyOutReader, GenericConnection, 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}; diff --git a/postgres/src/generic_connection.rs b/postgres/src/generic_connection.rs index fcf1755e..47df4c20 100644 --- a/postgres/src/generic_connection.rs +++ b/postgres/src/generic_connection.rs @@ -1,18 +1,18 @@ use crate::{Statement, Transaction}; -use tokio_postgres::types::{ToSql}; +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; + /// Like `Client::execute`. + fn execute(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result; - /// Like `Client::query`. - fn query(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result, Error>; + /// Like `Client::query`. + fn query(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result, Error>; - /// Like `Client::prepare`. - fn prepare(&mut self, query: &str) -> Result; + /// Like `Client::prepare`. + fn prepare(&mut self, query: &str) -> Result; - /// Like `Client::transaction`. - fn transaction(&mut self) -> Result, Error>; + /// Like `Client::transaction`. + fn transaction(&mut self) -> Result, Error>; } diff --git a/postgres/src/transaction.rs b/postgres/src/transaction.rs index f3288029..c5639387 100644 --- a/postgres/src/transaction.rs +++ b/postgres/src/transaction.rs @@ -1,4 +1,6 @@ -use crate::{CopyInWriter, CopyOutReader, GenericConnection, 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}; From 793e83a4ef05f3710c780bb2c08f1ac556ae8a46 Mon Sep 17 00:00:00 2001 From: Colin Maxfield Date: Sat, 28 Dec 2019 16:32:01 -0500 Subject: [PATCH 3/4] Adding in generic for the query function of genericconnection --- postgres/src/client.rs | 5 ++++- postgres/src/generic_connection.rs | 6 ++++-- postgres/src/transaction.rs | 5 ++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/postgres/src/client.rs b/postgres/src/client.rs index b24a568a..e513e51e 100644 --- a/postgres/src/client.rs +++ b/postgres/src/client.rs @@ -458,7 +458,10 @@ impl GenericConnection for Client { fn execute(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result { self.execute(query, params) } - fn query(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result, Error> { + fn query(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result, Error> + where + T: ?Sized + ToStatement, + { self.query(query, params) } fn prepare(&mut self, query: &str) -> Result { diff --git a/postgres/src/generic_connection.rs b/postgres/src/generic_connection.rs index 47df4c20..a25b283b 100644 --- a/postgres/src/generic_connection.rs +++ b/postgres/src/generic_connection.rs @@ -1,4 +1,4 @@ -use crate::{Statement, Transaction}; +use crate::{Statement, ToStatement, Transaction}; use tokio_postgres::types::ToSql; use tokio_postgres::{Error, Row}; @@ -8,7 +8,9 @@ pub trait GenericConnection { fn execute(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result; /// Like `Client::query`. - fn query(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result, Error>; + fn query(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result, Error> + where + T: ?Sized + ToStatement; /// Like `Client::prepare`. fn prepare(&mut self, query: &str) -> Result; diff --git a/postgres/src/transaction.rs b/postgres/src/transaction.rs index c5639387..de49489a 100644 --- a/postgres/src/transaction.rs +++ b/postgres/src/transaction.rs @@ -184,7 +184,10 @@ impl<'a> GenericConnection for Transaction<'a> { fn execute(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result { self.execute(query, params) } - fn query(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result, Error> { + fn query(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result, Error> + where + T: ?Sized + ToStatement, + { self.query(query, params) } fn prepare(&mut self, query: &str) -> Result { From 508b43075397a38b45ff2a7c9578f14e8e4dad30 Mon Sep 17 00:00:00 2001 From: Colin Maxfield Date: Sat, 28 Dec 2019 16:36:56 -0500 Subject: [PATCH 4/4] Adding generic for execute function for genericconnection --- postgres/src/client.rs | 5 ++++- postgres/src/generic_connection.rs | 4 +++- postgres/src/transaction.rs | 5 ++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/postgres/src/client.rs b/postgres/src/client.rs index e513e51e..e9a03ceb 100644 --- a/postgres/src/client.rs +++ b/postgres/src/client.rs @@ -455,7 +455,10 @@ impl Client { } impl GenericConnection for Client { - fn execute(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result { + fn execute(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result + where + T: ?Sized + ToStatement, + { self.execute(query, params) } fn query(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result, Error> diff --git a/postgres/src/generic_connection.rs b/postgres/src/generic_connection.rs index a25b283b..3a8e6788 100644 --- a/postgres/src/generic_connection.rs +++ b/postgres/src/generic_connection.rs @@ -5,7 +5,9 @@ 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; + fn execute(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result + where + T: ?Sized + ToStatement; /// Like `Client::query`. fn query(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result, Error> diff --git a/postgres/src/transaction.rs b/postgres/src/transaction.rs index de49489a..98b6bff5 100644 --- a/postgres/src/transaction.rs +++ b/postgres/src/transaction.rs @@ -181,7 +181,10 @@ impl<'a> Transaction<'a> { } impl<'a> GenericConnection for Transaction<'a> { - fn execute(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result { + fn execute(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result + where + T: ?Sized + ToStatement, + { self.execute(query, params) } fn query(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result, Error>