Skip to content

Commit 48879cc

Browse files
committed
Add generic immutable interface.
Add a GenericClient trait over Client and Transaction, and replace &mut with &.
1 parent 0c8ecc0 commit 48879cc

File tree

10 files changed

+192
-149
lines changed

10 files changed

+192
-149
lines changed

postgres/src/client.rs

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl Client {
7777
/// # Ok(())
7878
/// # }
7979
/// ```
80-
pub fn execute<T>(&mut self, query: &T, params: &[&dyn ToSql]) -> Result<u64, Error>
80+
pub fn execute<T>(&self, query: &T, params: &[&dyn ToSql]) -> Result<u64, Error>
8181
where
8282
T: ?Sized + ToStatement,
8383
{
@@ -116,7 +116,7 @@ impl Client {
116116
/// # Ok(())
117117
/// # }
118118
/// ```
119-
pub fn query<T>(&mut self, query: &T, params: &[&dyn ToSql]) -> Result<Vec<Row>, Error>
119+
pub fn query<T>(&self, query: &T, params: &[&dyn ToSql]) -> Result<Vec<Row>, Error>
120120
where
121121
T: ?Sized + ToStatement,
122122
{
@@ -148,11 +148,7 @@ impl Client {
148148
/// }
149149
/// # Ok(())
150150
/// # }
151-
pub fn query_iter<T>(
152-
&mut self,
153-
query: &T,
154-
params: &[&dyn ToSql],
155-
) -> Result<QueryIter<'_>, Error>
151+
pub fn query_iter<T>(&self, query: &T, params: &[&dyn ToSql]) -> Result<QueryIter<'_>, Error>
156152
where
157153
T: ?Sized + ToStatement,
158154
{
@@ -183,7 +179,7 @@ impl Client {
183179
/// # Ok(())
184180
/// # }
185181
/// ```
186-
pub fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
182+
pub fn prepare(&self, query: &str) -> Result<Statement, Error> {
187183
self.0.prepare(query).wait()
188184
}
189185

@@ -213,7 +209,7 @@ impl Client {
213209
/// }
214210
/// # Ok(())
215211
/// # }
216-
pub fn prepare_typed(&mut self, query: &str, types: &[Type]) -> Result<Statement, Error> {
212+
pub fn prepare_typed(&self, query: &str, types: &[Type]) -> Result<Statement, Error> {
217213
self.0.prepare_typed(query, types).wait()
218214
}
219215

@@ -234,12 +230,7 @@ impl Client {
234230
/// # Ok(())
235231
/// # }
236232
/// ```
237-
pub fn copy_in<T, R>(
238-
&mut self,
239-
query: &T,
240-
params: &[&dyn ToSql],
241-
reader: R,
242-
) -> Result<u64, Error>
233+
pub fn copy_in<T, R>(&self, query: &T, params: &[&dyn ToSql], reader: R) -> Result<u64, Error>
243234
where
244235
T: ?Sized + ToStatement,
245236
R: Read,
@@ -269,11 +260,7 @@ impl Client {
269260
/// # Ok(())
270261
/// # }
271262
/// ```
272-
pub fn copy_out<T>(
273-
&mut self,
274-
query: &T,
275-
params: &[&dyn ToSql],
276-
) -> Result<CopyOutReader<'_>, Error>
263+
pub fn copy_out<T>(&self, query: &T, params: &[&dyn ToSql]) -> Result<CopyOutReader<'_>, Error>
277264
where
278265
T: ?Sized + ToStatement,
279266
{
@@ -297,7 +284,7 @@ impl Client {
297284
/// Prepared statements should be use for any query which contains user-specified data, as they provided the
298285
/// functionality to safely imbed that data in the request. Do not form statements via string concatenation and pass
299286
/// them to this method!
300-
pub fn simple_query(&mut self, query: &str) -> Result<Vec<SimpleQueryMessage>, Error> {
287+
pub fn simple_query(&self, query: &str) -> Result<Vec<SimpleQueryMessage>, Error> {
301288
self.simple_query_iter(query)?.collect()
302289
}
303290

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

@@ -333,7 +320,7 @@ impl Client {
333320
/// # Ok(())
334321
/// # }
335322
/// ```
336-
pub fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
323+
pub fn transaction(&self) -> Result<Transaction<'_>, Error> {
337324
self.simple_query("BEGIN")?;
338325
Ok(Transaction::new(self))
339326
}
@@ -351,9 +338,9 @@ impl Client {
351338
}
352339

353340
/// Returns a mutable reference to the inner nonblocking client.
354-
pub fn get_mut(&mut self) -> &mut tokio_postgres::Client {
341+
/*pub fn get_mut(&mut self) -> &mut tokio_postgres::Client {
355342
&mut self.0
356-
}
343+
}*/
357344

358345
/// Consumes the client, returning the inner nonblocking client.
359346
pub fn into_inner(self) -> tokio_postgres::Client {
@@ -384,3 +371,43 @@ where
384371
}
385372
}
386373
}
374+
375+
/// A trait allowing abstraction over connections and transactions
376+
pub trait GenericClient {
377+
/// Like `Client::execute`.
378+
fn execute<T>(&self, query: &T, params: &[&dyn ToSql]) -> Result<u64, Error>
379+
where
380+
T: ?Sized + ToStatement;
381+
382+
/// Like `Client::query`.
383+
fn query<T>(&self, query: &T, params: &[&dyn ToSql]) -> Result<Vec<Row>, Error>
384+
where
385+
T: ?Sized + ToStatement;
386+
387+
/// Like `Client::prepare`.
388+
fn prepare(&self, query: &str) -> Result<Statement, Error>;
389+
390+
/// Like `Client::transaction`.
391+
fn transaction(&self) -> Result<Transaction<'_>, Error>;
392+
}
393+
394+
impl GenericClient for Client {
395+
fn execute<T>(&self, query: &T, params: &[&dyn ToSql]) -> Result<u64, Error>
396+
where
397+
T: ?Sized + ToStatement,
398+
{
399+
self.execute(query, params)
400+
}
401+
fn query<T>(&self, query: &T, params: &[&dyn ToSql]) -> Result<Vec<Row>, Error>
402+
where
403+
T: ?Sized + ToStatement,
404+
{
405+
self.query(query, params)
406+
}
407+
fn prepare(&self, query: &str) -> Result<Statement, Error> {
408+
self.prepare(query)
409+
}
410+
fn transaction(&self) -> Result<Transaction<'_>, Error> {
411+
self.transaction()
412+
}
413+
}

postgres/src/test.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::*;
66

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

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

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

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

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

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

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

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

45-
let mut transaction = client.transaction().unwrap();
45+
let transaction = client.transaction().unwrap();
4646

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

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

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

66-
let mut transaction = client.transaction().unwrap();
66+
let transaction = client.transaction().unwrap();
6767

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

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

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

86-
let mut transaction = client.transaction().unwrap();
86+
let transaction = client.transaction().unwrap();
8787

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

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

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

106-
let mut transaction = client.transaction().unwrap();
106+
let transaction = client.transaction().unwrap();
107107

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

112-
let mut transaction2 = transaction.transaction().unwrap();
112+
let transaction2 = transaction.transaction().unwrap();
113113

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

126-
let mut transaction3 = transaction.transaction().unwrap();
126+
let transaction3 = transaction.transaction().unwrap();
127127

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

132-
let mut transaction4 = transaction3.transaction().unwrap();
132+
let transaction4 = transaction3.transaction().unwrap();
133133

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

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

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

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

180180
client
181181
.simple_query(
@@ -198,7 +198,7 @@ fn copy_out() {
198198

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

203203
client
204204
.simple_query(
@@ -207,7 +207,7 @@ fn portal() {
207207
)
208208
.unwrap();
209209

210-
let mut transaction = client.transaction().unwrap();
210+
let transaction = client.transaction().unwrap();
211211

212212
let portal = transaction
213213
.bind("SELECT * FROM foo ORDER BY id", &[])

postgres/src/to_statement.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ mod sealed {
1414
/// This trait is "sealed" and cannot be implemented by anything outside this crate.
1515
pub trait ToStatement: sealed::Sealed {
1616
#[doc(hidden)]
17-
fn __statement(&self, client: &mut Client) -> Result<Statement, Error>;
17+
fn __statement(&self, client: &Client) -> Result<Statement, Error>;
1818
}
1919

2020
impl sealed::Sealed for str {}
2121

2222
impl ToStatement for str {
23-
fn __statement(&self, client: &mut Client) -> Result<Statement, Error> {
23+
fn __statement(&self, client: &Client) -> Result<Statement, Error> {
2424
client.prepare(self)
2525
}
2626
}
2727

2828
impl sealed::Sealed for Statement {}
2929

3030
impl ToStatement for Statement {
31-
fn __statement(&self, _: &mut Client) -> Result<Statement, Error> {
31+
fn __statement(&self, _: &Client) -> Result<Statement, Error> {
3232
Ok(self.clone())
3333
}
3434
}

0 commit comments

Comments
 (0)