1
+ use crate :: iter:: Iter ;
2
+ #[ cfg( feature = "runtime" ) ]
3
+ use crate :: Config ;
4
+ use crate :: { CopyInWriter , CopyOutReader , Statement , ToStatement , Transaction } ;
1
5
use fallible_iterator:: FallibleIterator ;
2
6
use futures:: executor;
3
- use std:: io:: { BufRead , Read } ;
4
7
use tokio_postgres:: tls:: { MakeTlsConnect , TlsConnect } ;
5
8
use tokio_postgres:: types:: { ToSql , Type } ;
6
9
#[ cfg( feature = "runtime" ) ]
7
10
use tokio_postgres:: Socket ;
8
11
use tokio_postgres:: { Error , Row , SimpleQueryMessage } ;
9
12
10
- use crate :: copy_in_stream:: CopyInStream ;
11
- use crate :: copy_out_reader:: CopyOutReader ;
12
- use crate :: iter:: Iter ;
13
- #[ cfg( feature = "runtime" ) ]
14
- use crate :: Config ;
15
- use crate :: { Statement , ToStatement , Transaction } ;
16
-
17
13
/// A synchronous PostgreSQL client.
18
14
///
19
15
/// This is a lightweight wrapper over the asynchronous tokio_postgres `Client`.
@@ -264,29 +260,33 @@ impl Client {
264
260
/// The `query` argument can either be a `Statement`, or a raw query string. The data in the provided reader is
265
261
/// passed along to the server verbatim; it is the caller's responsibility to ensure it uses the proper format.
266
262
///
263
+ /// The copy *must* be explicitly completed via the `finish` method. If it is not, the copy will be aborted.
264
+ ///
267
265
/// # Examples
268
266
///
269
267
/// ```no_run
270
268
/// use postgres::{Client, NoTls};
269
+ /// use std::io::Write;
271
270
///
272
- /// # fn main() -> Result<(), postgres:: Error> {
271
+ /// # fn main() -> Result<(), Box<dyn std::error:: Error> > {
273
272
/// let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
274
273
///
275
- /// client.copy_in("COPY people FROM stdin", &[], &mut "1\tjohn\n2\tjane\n".as_bytes())?;
274
+ /// let mut writer = client.copy_in("COPY people FROM stdin", &[])?;
275
+ /// writer.write_all(b"1\tjohn\n2\tjane\n")?;
276
+ /// writer.finish()?;
276
277
/// # Ok(())
277
278
/// # }
278
279
/// ```
279
- pub fn copy_in < T , R > (
280
+ pub fn copy_in < T > (
280
281
& mut self ,
281
282
query : & T ,
282
283
params : & [ & ( dyn ToSql + Sync ) ] ,
283
- reader : R ,
284
- ) -> Result < u64 , Error >
284
+ ) -> Result < CopyInWriter < ' _ > , Error >
285
285
where
286
286
T : ?Sized + ToStatement ,
287
- R : Read + Unpin ,
288
287
{
289
- executor:: block_on ( self . 0 . copy_in ( query, params, CopyInStream ( reader) ) )
288
+ let sink = executor:: block_on ( self . 0 . copy_in ( query, params) ) ?;
289
+ Ok ( CopyInWriter :: new ( sink) )
290
290
}
291
291
292
292
/// Executes a `COPY TO STDOUT` statement, returning a reader of the resulting data.
@@ -312,7 +312,7 @@ impl Client {
312
312
& mut self ,
313
313
query : & T ,
314
314
params : & [ & ( dyn ToSql + Sync ) ] ,
315
- ) -> Result < impl BufRead , Error >
315
+ ) -> Result < CopyOutReader < ' _ > , Error >
316
316
where
317
317
T : ?Sized + ToStatement ,
318
318
{
0 commit comments