1
+ use crate :: connection:: Connection ;
1
2
use crate :: {
2
3
CancelToken , Config , CopyInWriter , CopyOutReader , RowIter , Statement , ToStatement , Transaction ,
3
4
TransactionBuilder ,
4
5
} ;
5
- use std:: ops:: { Deref , DerefMut } ;
6
- use tokio:: runtime:: Runtime ;
7
6
use tokio_postgres:: tls:: { MakeTlsConnect , TlsConnect } ;
8
7
use tokio_postgres:: types:: { ToSql , Type } ;
9
8
use tokio_postgres:: { Error , Row , SimpleQueryMessage , Socket } ;
10
9
11
- pub ( crate ) struct Rt < ' a > ( pub & ' a mut Runtime ) ;
12
-
13
- // no-op impl to extend the borrow until drop
14
- impl Drop for Rt < ' _ > {
15
- fn drop ( & mut self ) { }
16
- }
17
-
18
- impl Deref for Rt < ' _ > {
19
- type Target = Runtime ;
20
-
21
- #[ inline]
22
- fn deref ( & self ) -> & Runtime {
23
- self . 0
24
- }
25
- }
26
-
27
- impl DerefMut for Rt < ' _ > {
28
- #[ inline]
29
- fn deref_mut ( & mut self ) -> & mut Runtime {
30
- self . 0
31
- }
32
- }
33
-
34
10
/// A synchronous PostgreSQL client.
35
11
pub struct Client {
36
- runtime : Runtime ,
12
+ connection : Connection ,
37
13
client : tokio_postgres:: Client ,
38
14
}
39
15
40
16
impl Client {
41
- pub ( crate ) fn new ( runtime : Runtime , client : tokio_postgres:: Client ) -> Client {
42
- Client { runtime , client }
17
+ pub ( crate ) fn new ( connection : Connection , client : tokio_postgres:: Client ) -> Client {
18
+ Client { connection , client }
43
19
}
44
20
45
21
/// A convenience function which parses a configuration string into a `Config` and then connects to the database.
@@ -62,10 +38,6 @@ impl Client {
62
38
Config :: new ( )
63
39
}
64
40
65
- fn rt ( & mut self ) -> Rt < ' _ > {
66
- Rt ( & mut self . runtime )
67
- }
68
-
69
41
/// Executes a statement, returning the number of rows modified.
70
42
///
71
43
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
@@ -104,7 +76,7 @@ impl Client {
104
76
where
105
77
T : ?Sized + ToStatement ,
106
78
{
107
- self . runtime . block_on ( self . client . execute ( query, params) )
79
+ self . connection . block_on ( self . client . execute ( query, params) )
108
80
}
109
81
110
82
/// Executes a statement, returning the resulting rows.
@@ -140,7 +112,7 @@ impl Client {
140
112
where
141
113
T : ?Sized + ToStatement ,
142
114
{
143
- self . runtime . block_on ( self . client . query ( query, params) )
115
+ self . connection . block_on ( self . client . query ( query, params) )
144
116
}
145
117
146
118
/// Executes a statement which returns a single row, returning it.
@@ -177,7 +149,8 @@ impl Client {
177
149
where
178
150
T : ?Sized + ToStatement ,
179
151
{
180
- self . runtime . block_on ( self . client . query_one ( query, params) )
152
+ self . connection
153
+ . block_on ( self . client . query_one ( query, params) )
181
154
}
182
155
183
156
/// Executes a statement which returns zero or one rows, returning it.
@@ -223,7 +196,8 @@ impl Client {
223
196
where
224
197
T : ?Sized + ToStatement ,
225
198
{
226
- self . runtime . block_on ( self . client . query_opt ( query, params) )
199
+ self . connection
200
+ . block_on ( self . client . query_opt ( query, params) )
227
201
}
228
202
229
203
/// A maximally-flexible version of `query`.
@@ -289,9 +263,9 @@ impl Client {
289
263
I :: IntoIter : ExactSizeIterator ,
290
264
{
291
265
let stream = self
292
- . runtime
266
+ . connection
293
267
. block_on ( self . client . query_raw ( query, params) ) ?;
294
- Ok ( RowIter :: new ( self . rt ( ) , stream) )
268
+ Ok ( RowIter :: new ( self . connection . as_ref ( ) , stream) )
295
269
}
296
270
297
271
/// Creates a new prepared statement.
@@ -318,7 +292,7 @@ impl Client {
318
292
/// # }
319
293
/// ```
320
294
pub fn prepare ( & mut self , query : & str ) -> Result < Statement , Error > {
321
- self . runtime . block_on ( self . client . prepare ( query) )
295
+ self . connection . block_on ( self . client . prepare ( query) )
322
296
}
323
297
324
298
/// Like `prepare`, but allows the types of query parameters to be explicitly specified.
@@ -349,7 +323,7 @@ impl Client {
349
323
/// # }
350
324
/// ```
351
325
pub fn prepare_typed ( & mut self , query : & str , types : & [ Type ] ) -> Result < Statement , Error > {
352
- self . runtime
326
+ self . connection
353
327
. block_on ( self . client . prepare_typed ( query, types) )
354
328
}
355
329
@@ -380,8 +354,8 @@ impl Client {
380
354
where
381
355
T : ?Sized + ToStatement ,
382
356
{
383
- let sink = self . runtime . block_on ( self . client . copy_in ( query) ) ?;
384
- Ok ( CopyInWriter :: new ( self . rt ( ) , sink) )
357
+ let sink = self . connection . block_on ( self . client . copy_in ( query) ) ?;
358
+ Ok ( CopyInWriter :: new ( self . connection . as_ref ( ) , sink) )
385
359
}
386
360
387
361
/// Executes a `COPY TO STDOUT` statement, returning a reader of the resulting data.
@@ -408,8 +382,8 @@ impl Client {
408
382
where
409
383
T : ?Sized + ToStatement ,
410
384
{
411
- let stream = self . runtime . block_on ( self . client . copy_out ( query) ) ?;
412
- Ok ( CopyOutReader :: new ( self . rt ( ) , stream) )
385
+ let stream = self . connection . block_on ( self . client . copy_out ( query) ) ?;
386
+ Ok ( CopyOutReader :: new ( self . connection . as_ref ( ) , stream) )
413
387
}
414
388
415
389
/// Executes a sequence of SQL statements using the simple query protocol.
@@ -428,7 +402,7 @@ impl Client {
428
402
/// functionality to safely imbed that data in the request. Do not form statements via string concatenation and pass
429
403
/// them to this method!
430
404
pub fn simple_query ( & mut self , query : & str ) -> Result < Vec < SimpleQueryMessage > , Error > {
431
- self . runtime . block_on ( self . client . simple_query ( query) )
405
+ self . connection . block_on ( self . client . simple_query ( query) )
432
406
}
433
407
434
408
/// Executes a sequence of SQL statements using the simple query protocol.
@@ -442,7 +416,7 @@ impl Client {
442
416
/// functionality to safely embed that data in the request. Do not form statements via string concatenation and pass
443
417
/// them to this method!
444
418
pub fn batch_execute ( & mut self , query : & str ) -> Result < ( ) , Error > {
445
- self . runtime . block_on ( self . client . batch_execute ( query) )
419
+ self . connection . block_on ( self . client . batch_execute ( query) )
446
420
}
447
421
448
422
/// Begins a new database transaction.
@@ -466,8 +440,8 @@ impl Client {
466
440
/// # }
467
441
/// ```
468
442
pub fn transaction ( & mut self ) -> Result < Transaction < ' _ > , Error > {
469
- let transaction = self . runtime . block_on ( self . client . transaction ( ) ) ?;
470
- Ok ( Transaction :: new ( & mut self . runtime , transaction) )
443
+ let transaction = self . connection . block_on ( self . client . transaction ( ) ) ?;
444
+ Ok ( Transaction :: new ( self . connection . as_ref ( ) , transaction) )
471
445
}
472
446
473
447
/// Returns a builder for a transaction with custom settings.
@@ -494,7 +468,7 @@ impl Client {
494
468
/// # }
495
469
/// ```
496
470
pub fn build_transaction ( & mut self ) -> TransactionBuilder < ' _ > {
497
- TransactionBuilder :: new ( & mut self . runtime , self . client . build_transaction ( ) )
471
+ TransactionBuilder :: new ( self . connection . as_ref ( ) , self . client . build_transaction ( ) )
498
472
}
499
473
500
474
/// Constructs a cancellation token that can later be used to request
0 commit comments