1
- #[ cfg( feature = "runtime" ) ]
2
- use crate :: Config ;
3
- use crate :: { CopyInWriter , CopyOutReader , RowIter , Statement , ToStatement , Transaction } ;
4
- use futures:: executor;
1
+ use crate :: { Config , CopyInWriter , CopyOutReader , RowIter , Statement , ToStatement , Transaction } ;
2
+ use tokio:: runtime:: Runtime ;
5
3
use tokio_postgres:: tls:: { MakeTlsConnect , TlsConnect } ;
6
4
use tokio_postgres:: types:: { ToSql , Type } ;
7
- #[ cfg( feature = "runtime" ) ]
8
- use tokio_postgres:: Socket ;
9
- use tokio_postgres:: { Error , Row , SimpleQueryMessage } ;
5
+ use tokio_postgres:: { Error , Row , SimpleQueryMessage , Socket } ;
10
6
11
7
/// A synchronous PostgreSQL client.
12
- ///
13
- /// This is a lightweight wrapper over the asynchronous tokio_postgres `Client`.
14
- pub struct Client ( tokio_postgres:: Client ) ;
8
+ pub struct Client {
9
+ runtime : Runtime ,
10
+ client : tokio_postgres:: Client ,
11
+ }
15
12
16
13
impl Client {
14
+ pub ( crate ) fn new ( runtime : Runtime , client : tokio_postgres:: Client ) -> Client {
15
+ Client { runtime, client }
16
+ }
17
+
17
18
/// A convenience function which parses a configuration string into a `Config` and then connects to the database.
18
19
///
19
20
/// See the documentation for [`Config`] for information about the connection syntax.
20
21
///
21
- /// Requires the `runtime` Cargo feature (enabled by default).
22
- ///
23
22
/// [`Config`]: config/struct.Config.html
24
- #[ cfg( feature = "runtime" ) ]
25
23
pub fn connect < T > ( params : & str , tls_mode : T ) -> Result < Client , Error >
26
24
where
27
25
T : MakeTlsConnect < Socket > + ' static + Send ,
@@ -78,7 +76,7 @@ impl Client {
78
76
where
79
77
T : ?Sized + ToStatement ,
80
78
{
81
- executor :: block_on ( self . 0 . execute ( query, params) )
79
+ self . runtime . block_on ( self . client . execute ( query, params) )
82
80
}
83
81
84
82
/// Executes a statement, returning the resulting rows.
@@ -114,7 +112,7 @@ impl Client {
114
112
where
115
113
T : ?Sized + ToStatement ,
116
114
{
117
- executor :: block_on ( self . 0 . query ( query, params) )
115
+ self . runtime . block_on ( self . client . query ( query, params) )
118
116
}
119
117
120
118
/// Executes a statement which returns a single row, returning it.
@@ -151,7 +149,7 @@ impl Client {
151
149
where
152
150
T : ?Sized + ToStatement ,
153
151
{
154
- executor :: block_on ( self . 0 . query_one ( query, params) )
152
+ self . runtime . block_on ( self . client . query_one ( query, params) )
155
153
}
156
154
157
155
/// Executes a statement which returns zero or one rows, returning it.
@@ -197,7 +195,7 @@ impl Client {
197
195
where
198
196
T : ?Sized + ToStatement ,
199
197
{
200
- executor :: block_on ( self . 0 . query_opt ( query, params) )
198
+ self . runtime . block_on ( self . client . query_opt ( query, params) )
201
199
}
202
200
203
201
/// A maximally-flexible version of `query`.
@@ -235,8 +233,10 @@ impl Client {
235
233
I : IntoIterator < Item = & ' a dyn ToSql > ,
236
234
I :: IntoIter : ExactSizeIterator ,
237
235
{
238
- let stream = executor:: block_on ( self . 0 . query_raw ( query, params) ) ?;
239
- Ok ( RowIter :: new ( stream) )
236
+ let stream = self
237
+ . runtime
238
+ . block_on ( self . client . query_raw ( query, params) ) ?;
239
+ Ok ( RowIter :: new ( & mut self . runtime , stream) )
240
240
}
241
241
242
242
/// Creates a new prepared statement.
@@ -263,7 +263,7 @@ impl Client {
263
263
/// # }
264
264
/// ```
265
265
pub fn prepare ( & mut self , query : & str ) -> Result < Statement , Error > {
266
- executor :: block_on ( self . 0 . prepare ( query) )
266
+ self . runtime . block_on ( self . client . prepare ( query) )
267
267
}
268
268
269
269
/// Like `prepare`, but allows the types of query parameters to be explicitly specified.
@@ -294,7 +294,8 @@ impl Client {
294
294
/// # }
295
295
/// ```
296
296
pub fn prepare_typed ( & mut self , query : & str , types : & [ Type ] ) -> Result < Statement , Error > {
297
- executor:: block_on ( self . 0 . prepare_typed ( query, types) )
297
+ self . runtime
298
+ . block_on ( self . client . prepare_typed ( query, types) )
298
299
}
299
300
300
301
/// Executes a `COPY FROM STDIN` statement, returning the number of rows created.
@@ -327,8 +328,8 @@ impl Client {
327
328
where
328
329
T : ?Sized + ToStatement ,
329
330
{
330
- let sink = executor :: block_on ( self . 0 . copy_in ( query, params) ) ?;
331
- Ok ( CopyInWriter :: new ( sink) )
331
+ let sink = self . runtime . block_on ( self . client . copy_in ( query, params) ) ?;
332
+ Ok ( CopyInWriter :: new ( & mut self . runtime , sink) )
332
333
}
333
334
334
335
/// Executes a `COPY TO STDOUT` statement, returning a reader of the resulting data.
@@ -358,8 +359,8 @@ impl Client {
358
359
where
359
360
T : ?Sized + ToStatement ,
360
361
{
361
- let stream = executor :: block_on ( self . 0 . copy_out ( query, params) ) ?;
362
- CopyOutReader :: new ( stream)
362
+ let stream = self . runtime . block_on ( self . client . copy_out ( query, params) ) ?;
363
+ CopyOutReader :: new ( & mut self . runtime , stream)
363
364
}
364
365
365
366
/// Executes a sequence of SQL statements using the simple query protocol.
@@ -378,7 +379,7 @@ impl Client {
378
379
/// functionality to safely imbed that data in the request. Do not form statements via string concatenation and pass
379
380
/// them to this method!
380
381
pub fn simple_query ( & mut self , query : & str ) -> Result < Vec < SimpleQueryMessage > , Error > {
381
- executor :: block_on ( self . 0 . simple_query ( query) )
382
+ self . runtime . block_on ( self . client . simple_query ( query) )
382
383
}
383
384
384
385
/// Executes a sequence of SQL statements using the simple query protocol.
@@ -392,7 +393,7 @@ impl Client {
392
393
/// functionality to safely embed that data in the request. Do not form statements via string concatenation and pass
393
394
/// them to this method!
394
395
pub fn batch_execute ( & mut self , query : & str ) -> Result < ( ) , Error > {
395
- executor :: block_on ( self . 0 . batch_execute ( query) )
396
+ self . runtime . block_on ( self . client . batch_execute ( query) )
396
397
}
397
398
398
399
/// Begins a new database transaction.
@@ -416,35 +417,14 @@ impl Client {
416
417
/// # }
417
418
/// ```
418
419
pub fn transaction ( & mut self ) -> Result < Transaction < ' _ > , Error > {
419
- let transaction = executor :: block_on ( self . 0 . transaction ( ) ) ?;
420
- Ok ( Transaction :: new ( transaction) )
420
+ let transaction = self . runtime . block_on ( self . client . transaction ( ) ) ?;
421
+ Ok ( Transaction :: new ( & mut self . runtime , transaction) )
421
422
}
422
423
423
424
/// Determines if the client's connection has already closed.
424
425
///
425
426
/// If this returns `true`, the client is no longer usable.
426
427
pub fn is_closed ( & self ) -> bool {
427
- self . 0 . is_closed ( )
428
- }
429
-
430
- /// Returns a shared reference to the inner nonblocking client.
431
- pub fn get_ref ( & self ) -> & tokio_postgres:: Client {
432
- & self . 0
433
- }
434
-
435
- /// Returns a mutable reference to the inner nonblocking client.
436
- pub fn get_mut ( & mut self ) -> & mut tokio_postgres:: Client {
437
- & mut self . 0
438
- }
439
-
440
- /// Consumes the client, returning the inner nonblocking client.
441
- pub fn into_inner ( self ) -> tokio_postgres:: Client {
442
- self . 0
443
- }
444
- }
445
-
446
- impl From < tokio_postgres:: Client > for Client {
447
- fn from ( c : tokio_postgres:: Client ) -> Client {
448
- Client ( c)
428
+ self . client . is_closed ( )
449
429
}
450
430
}
0 commit comments