Skip to content

Add generic immutable interface. #433

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

Closed
wants to merge 3 commits into from
Closed
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
67 changes: 42 additions & 25 deletions postgres/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub fn execute<T>(&mut self, query: &T, params: &[&dyn ToSql]) -> Result<u64, Error>
pub fn execute<T>(&self, query: &T, params: &[&dyn ToSql]) -> Result<u64, Error>
where
T: ?Sized + ToStatement,
{
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub fn query<T>(&mut self, query: &T, params: &[&dyn ToSql]) -> Result<Vec<Row>, Error>
pub fn query<T>(&self, query: &T, params: &[&dyn ToSql]) -> Result<Vec<Row>, Error>
where
T: ?Sized + ToStatement,
{
Expand Down Expand Up @@ -148,11 +148,7 @@ impl Client {
/// }
/// # Ok(())
/// # }
pub fn query_iter<T>(
&mut self,
query: &T,
params: &[&dyn ToSql],
) -> Result<QueryIter<'_>, Error>
pub fn query_iter<T>(&self, query: &T, params: &[&dyn ToSql]) -> Result<QueryIter<'_>, Error>
where
T: ?Sized + ToStatement,
{
Expand Down Expand Up @@ -183,7 +179,7 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
pub fn prepare(&self, query: &str) -> Result<Statement, Error> {
self.0.prepare(query).wait()
}

Expand Down Expand Up @@ -213,7 +209,7 @@ impl Client {
/// }
/// # Ok(())
/// # }
pub fn prepare_typed(&mut self, query: &str, types: &[Type]) -> Result<Statement, Error> {
pub fn prepare_typed(&self, query: &str, types: &[Type]) -> Result<Statement, Error> {
self.0.prepare_typed(query, types).wait()
}

Expand All @@ -234,12 +230,7 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub fn copy_in<T, R>(
&mut self,
query: &T,
params: &[&dyn ToSql],
reader: R,
) -> Result<u64, Error>
pub fn copy_in<T, R>(&self, query: &T, params: &[&dyn ToSql], reader: R) -> Result<u64, Error>
where
T: ?Sized + ToStatement,
R: Read,
Expand Down Expand Up @@ -269,11 +260,7 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub fn copy_out<T>(
&mut self,
query: &T,
params: &[&dyn ToSql],
) -> Result<CopyOutReader<'_>, Error>
pub fn copy_out<T>(&self, query: &T, params: &[&dyn ToSql]) -> Result<CopyOutReader<'_>, Error>
where
T: ?Sized + ToStatement,
{
Expand All @@ -297,7 +284,7 @@ impl Client {
/// Prepared statements should be use for any query which contains user-specified data, as they provided the
/// functionality to safely imbed that data in the request. Do not form statements via string concatenation and pass
/// them to this method!
pub fn simple_query(&mut self, query: &str) -> Result<Vec<SimpleQueryMessage>, Error> {
pub fn simple_query(&self, query: &str) -> Result<Vec<SimpleQueryMessage>, Error> {
self.simple_query_iter(query)?.collect()
}

Expand All @@ -309,7 +296,7 @@ impl Client {
/// Prepared statements should be use for any query which contains user-specified data, as they provided the
/// functionality to safely imbed that data in the request. Do not form statements via string concatenation and pass
/// them to this method!
pub fn simple_query_iter(&mut self, query: &str) -> Result<SimpleQueryIter<'_>, Error> {
pub fn simple_query_iter(&self, query: &str) -> Result<SimpleQueryIter<'_>, Error> {
Ok(SimpleQueryIter::new(self.0.simple_query(query)))
}

Expand All @@ -333,7 +320,7 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
pub fn transaction(&self) -> Result<Transaction<'_>, Error> {
self.simple_query("BEGIN")?;
Ok(Transaction::new(self))
}
Expand All @@ -351,9 +338,9 @@ impl Client {
}

/// Returns a mutable reference to the inner nonblocking client.
pub fn get_mut(&mut self) -> &mut tokio_postgres::Client {
/*pub fn get_mut(&mut self) -> &mut tokio_postgres::Client {
&mut self.0
}
}*/

/// Consumes the client, returning the inner nonblocking client.
pub fn into_inner(self) -> tokio_postgres::Client {
Expand Down Expand Up @@ -384,3 +371,33 @@ where
}
}
}

/// A trait allowing abstraction over connections and transactions
pub trait GenericClient {
/// Like `Client::execute`.
fn execute(&self, query: &str, params: &[&dyn ToSql]) -> Result<u64, Error>;

/// Like `Client::query`.
fn query(&self, query: &str, params: &[&dyn ToSql]) -> Result<Vec<Row>, Error>;

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

/// Like `Client::transaction`.
fn transaction(&self) -> Result<Transaction<'_>, Error>;
}

impl GenericClient for Client {
fn execute(&self, query: &str, params: &[&dyn ToSql]) -> Result<u64, Error> {
self.execute(query, params)
}
fn query(&self, query: &str, params: &[&dyn ToSql]) -> Result<Vec<Row>, Error> {
self.query(query, params)
}
fn prepare(&self, query: &str) -> Result<Statement, Error> {
self.prepare(query)
}
fn transaction(&self) -> Result<Transaction<'_>, Error> {
self.transaction()
}
}
36 changes: 18 additions & 18 deletions postgres/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::*;

#[test]
fn prepare() {
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
let client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();

let stmt = client.prepare("SELECT 1::INT, $1::TEXT").unwrap();
assert_eq!(stmt.params(), &[Type::TEXT]);
Expand All @@ -17,7 +17,7 @@ fn prepare() {

#[test]
fn query_prepared() {
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
let client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();

let stmt = client.prepare("SELECT $1::TEXT").unwrap();
let rows = client.query(&stmt, &[&"hello"]).unwrap();
Expand All @@ -27,7 +27,7 @@ fn query_prepared() {

#[test]
fn query_unprepared() {
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
let client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();

let rows = client.query("SELECT $1::TEXT", &[&"hello"]).unwrap();
assert_eq!(rows.len(), 1);
Expand All @@ -36,13 +36,13 @@ fn query_unprepared() {

#[test]
fn transaction_commit() {
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
let client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();

client
.simple_query("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY)")
.unwrap();

let mut transaction = client.transaction().unwrap();
let transaction = client.transaction().unwrap();

transaction
.execute("INSERT INTO foo DEFAULT VALUES", &[])
Expand All @@ -57,13 +57,13 @@ fn transaction_commit() {

#[test]
fn transaction_rollback() {
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
let client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();

client
.simple_query("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY)")
.unwrap();

let mut transaction = client.transaction().unwrap();
let transaction = client.transaction().unwrap();

transaction
.execute("INSERT INTO foo DEFAULT VALUES", &[])
Expand All @@ -77,13 +77,13 @@ fn transaction_rollback() {

#[test]
fn transaction_drop() {
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
let client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();

client
.simple_query("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY)")
.unwrap();

let mut transaction = client.transaction().unwrap();
let transaction = client.transaction().unwrap();

transaction
.execute("INSERT INTO foo DEFAULT VALUES", &[])
Expand All @@ -97,19 +97,19 @@ fn transaction_drop() {

#[test]
fn nested_transactions() {
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
let client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();

client
.simple_query("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)")
.unwrap();

let mut transaction = client.transaction().unwrap();
let transaction = client.transaction().unwrap();

transaction
.execute("INSERT INTO foo (id) VALUES (1)", &[])
.unwrap();

let mut transaction2 = transaction.transaction().unwrap();
let transaction2 = transaction.transaction().unwrap();

transaction2
.execute("INSERT INTO foo (id) VALUES (2)", &[])
Expand All @@ -123,13 +123,13 @@ fn nested_transactions() {
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, i32>(0), 1);

let mut transaction3 = transaction.transaction().unwrap();
let transaction3 = transaction.transaction().unwrap();

transaction3
.execute("INSERT INTO foo (id) VALUES(3)", &[])
.unwrap();

let mut transaction4 = transaction3.transaction().unwrap();
let transaction4 = transaction3.transaction().unwrap();

transaction4
.execute("INSERT INTO foo (id) VALUES(4)", &[])
Expand All @@ -148,7 +148,7 @@ fn nested_transactions() {

#[test]
fn copy_in() {
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
let client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();

client
.simple_query("CREATE TEMPORARY TABLE foo (id INT, name TEXT)")
Expand All @@ -175,7 +175,7 @@ fn copy_in() {

#[test]
fn copy_out() {
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
let client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();

client
.simple_query(
Expand All @@ -198,7 +198,7 @@ fn copy_out() {

#[test]
fn portal() {
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
let client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();

client
.simple_query(
Expand All @@ -207,7 +207,7 @@ fn portal() {
)
.unwrap();

let mut transaction = client.transaction().unwrap();
let transaction = client.transaction().unwrap();

let portal = transaction
.bind("SELECT * FROM foo ORDER BY id", &[])
Expand Down
6 changes: 3 additions & 3 deletions postgres/src/to_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ mod sealed {
/// This trait is "sealed" and cannot be implemented by anything outside this crate.
pub trait ToStatement: sealed::Sealed {
#[doc(hidden)]
fn __statement(&self, client: &mut Client) -> Result<Statement, Error>;
fn __statement(&self, client: &Client) -> Result<Statement, Error>;
}

impl sealed::Sealed for str {}

impl ToStatement for str {
fn __statement(&self, client: &mut Client) -> Result<Statement, Error> {
fn __statement(&self, client: &Client) -> Result<Statement, Error> {
client.prepare(self)
}
}

impl sealed::Sealed for Statement {}

impl ToStatement for Statement {
fn __statement(&self, _: &mut Client) -> Result<Statement, Error> {
fn __statement(&self, _: &Client) -> Result<Statement, Error> {
Ok(self.clone())
}
}
Loading