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 ;
53use tokio_postgres:: tls:: { MakeTlsConnect , TlsConnect } ;
64use 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 } ;
106
117/// 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+ }
1512
1613impl Client {
14+ pub ( crate ) fn new ( runtime : Runtime , client : tokio_postgres:: Client ) -> Client {
15+ Client { runtime, client }
16+ }
17+
1718 /// A convenience function which parses a configuration string into a `Config` and then connects to the database.
1819 ///
1920 /// See the documentation for [`Config`] for information about the connection syntax.
2021 ///
21- /// Requires the `runtime` Cargo feature (enabled by default).
22- ///
2322 /// [`Config`]: config/struct.Config.html
24- #[ cfg( feature = "runtime" ) ]
2523 pub fn connect < T > ( params : & str , tls_mode : T ) -> Result < Client , Error >
2624 where
2725 T : MakeTlsConnect < Socket > + ' static + Send ,
@@ -78,7 +76,7 @@ impl Client {
7876 where
7977 T : ?Sized + ToStatement ,
8078 {
81- executor :: block_on ( self . 0 . execute ( query, params) )
79+ self . runtime . block_on ( self . client . execute ( query, params) )
8280 }
8381
8482 /// Executes a statement, returning the resulting rows.
@@ -114,7 +112,7 @@ impl Client {
114112 where
115113 T : ?Sized + ToStatement ,
116114 {
117- executor :: block_on ( self . 0 . query ( query, params) )
115+ self . runtime . block_on ( self . client . query ( query, params) )
118116 }
119117
120118 /// Executes a statement which returns a single row, returning it.
@@ -151,7 +149,7 @@ impl Client {
151149 where
152150 T : ?Sized + ToStatement ,
153151 {
154- executor :: block_on ( self . 0 . query_one ( query, params) )
152+ self . runtime . block_on ( self . client . query_one ( query, params) )
155153 }
156154
157155 /// Executes a statement which returns zero or one rows, returning it.
@@ -197,7 +195,7 @@ impl Client {
197195 where
198196 T : ?Sized + ToStatement ,
199197 {
200- executor :: block_on ( self . 0 . query_opt ( query, params) )
198+ self . runtime . block_on ( self . client . query_opt ( query, params) )
201199 }
202200
203201 /// A maximally-flexible version of `query`.
@@ -235,8 +233,10 @@ impl Client {
235233 I : IntoIterator < Item = & ' a dyn ToSql > ,
236234 I :: IntoIter : ExactSizeIterator ,
237235 {
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) )
240240 }
241241
242242 /// Creates a new prepared statement.
@@ -263,7 +263,7 @@ impl Client {
263263 /// # }
264264 /// ```
265265 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) )
267267 }
268268
269269 /// Like `prepare`, but allows the types of query parameters to be explicitly specified.
@@ -294,7 +294,8 @@ impl Client {
294294 /// # }
295295 /// ```
296296 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) )
298299 }
299300
300301 /// Executes a `COPY FROM STDIN` statement, returning the number of rows created.
@@ -327,8 +328,8 @@ impl Client {
327328 where
328329 T : ?Sized + ToStatement ,
329330 {
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) )
332333 }
333334
334335 /// Executes a `COPY TO STDOUT` statement, returning a reader of the resulting data.
@@ -358,8 +359,8 @@ impl Client {
358359 where
359360 T : ?Sized + ToStatement ,
360361 {
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)
363364 }
364365
365366 /// Executes a sequence of SQL statements using the simple query protocol.
@@ -378,7 +379,7 @@ impl Client {
378379 /// functionality to safely imbed that data in the request. Do not form statements via string concatenation and pass
379380 /// them to this method!
380381 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) )
382383 }
383384
384385 /// Executes a sequence of SQL statements using the simple query protocol.
@@ -392,7 +393,7 @@ impl Client {
392393 /// functionality to safely embed that data in the request. Do not form statements via string concatenation and pass
393394 /// them to this method!
394395 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) )
396397 }
397398
398399 /// Begins a new database transaction.
@@ -416,35 +417,14 @@ impl Client {
416417 /// # }
417418 /// ```
418419 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) )
421422 }
422423
423424 /// Determines if the client's connection has already closed.
424425 ///
425426 /// If this returns `true`, the client is no longer usable.
426427 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 ( )
449429 }
450430}
0 commit comments