@@ -77,7 +77,7 @@ impl Client {
77
77
/// # Ok(())
78
78
/// # }
79
79
/// ```
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 >
81
81
where
82
82
T : ?Sized + ToStatement ,
83
83
{
@@ -116,7 +116,7 @@ impl Client {
116
116
/// # Ok(())
117
117
/// # }
118
118
/// ```
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 >
120
120
where
121
121
T : ?Sized + ToStatement ,
122
122
{
@@ -148,11 +148,7 @@ impl Client {
148
148
/// }
149
149
/// # Ok(())
150
150
/// # }
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 >
156
152
where
157
153
T : ?Sized + ToStatement ,
158
154
{
@@ -183,7 +179,7 @@ impl Client {
183
179
/// # Ok(())
184
180
/// # }
185
181
/// ```
186
- pub fn prepare ( & mut self , query : & str ) -> Result < Statement , Error > {
182
+ pub fn prepare ( & self , query : & str ) -> Result < Statement , Error > {
187
183
self . 0 . prepare ( query) . wait ( )
188
184
}
189
185
@@ -213,7 +209,7 @@ impl Client {
213
209
/// }
214
210
/// # Ok(())
215
211
/// # }
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 > {
217
213
self . 0 . prepare_typed ( query, types) . wait ( )
218
214
}
219
215
@@ -234,12 +230,7 @@ impl Client {
234
230
/// # Ok(())
235
231
/// # }
236
232
/// ```
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 >
243
234
where
244
235
T : ?Sized + ToStatement ,
245
236
R : Read ,
@@ -269,11 +260,7 @@ impl Client {
269
260
/// # Ok(())
270
261
/// # }
271
262
/// ```
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 >
277
264
where
278
265
T : ?Sized + ToStatement ,
279
266
{
@@ -297,7 +284,7 @@ impl Client {
297
284
/// Prepared statements should be use for any query which contains user-specified data, as they provided the
298
285
/// functionality to safely imbed that data in the request. Do not form statements via string concatenation and pass
299
286
/// 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 > {
301
288
self . simple_query_iter ( query) ?. collect ( )
302
289
}
303
290
@@ -309,7 +296,7 @@ impl Client {
309
296
/// Prepared statements should be use for any query which contains user-specified data, as they provided the
310
297
/// functionality to safely imbed that data in the request. Do not form statements via string concatenation and pass
311
298
/// 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 > {
313
300
Ok ( SimpleQueryIter :: new ( self . 0 . simple_query ( query) ) )
314
301
}
315
302
@@ -333,7 +320,7 @@ impl Client {
333
320
/// # Ok(())
334
321
/// # }
335
322
/// ```
336
- pub fn transaction ( & mut self ) -> Result < Transaction < ' _ > , Error > {
323
+ pub fn transaction ( & self ) -> Result < Transaction < ' _ > , Error > {
337
324
self . simple_query ( "BEGIN" ) ?;
338
325
Ok ( Transaction :: new ( self ) )
339
326
}
@@ -351,9 +338,9 @@ impl Client {
351
338
}
352
339
353
340
/// 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 {
355
342
&mut self.0
356
- }
343
+ }*/
357
344
358
345
/// Consumes the client, returning the inner nonblocking client.
359
346
pub fn into_inner ( self ) -> tokio_postgres:: Client {
@@ -384,3 +371,43 @@ where
384
371
}
385
372
}
386
373
}
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
+ }
0 commit comments