Skip to content

Commit 5c33bf8

Browse files
committed
Don't take parameters in copy_in and copy_out
Postgres doesn't support them, so we may as well not provide the option! Closes #523
1 parent ac4c63f commit 5c33bf8

File tree

9 files changed

+43
-77
lines changed

9 files changed

+43
-77
lines changed

postgres/src/client.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ impl Client {
302302
///
303303
/// The `query` argument can either be a `Statement`, or a raw query string. The data in the provided reader is
304304
/// passed along to the server verbatim; it is the caller's responsibility to ensure it uses the proper format.
305+
/// PostgreSQL does not support parameters in `COPY` statements, so this method does not take any.
305306
///
306307
/// The copy *must* be explicitly completed via the `finish` method. If it is not, the copy will be aborted.
307308
///
@@ -314,27 +315,24 @@ impl Client {
314315
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
315316
/// let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
316317
///
317-
/// let mut writer = client.copy_in("COPY people FROM stdin", &[])?;
318+
/// let mut writer = client.copy_in("COPY people FROM stdin")?;
318319
/// writer.write_all(b"1\tjohn\n2\tjane\n")?;
319320
/// writer.finish()?;
320321
/// # Ok(())
321322
/// # }
322323
/// ```
323-
pub fn copy_in<T>(
324-
&mut self,
325-
query: &T,
326-
params: &[&(dyn ToSql + Sync)],
327-
) -> Result<CopyInWriter<'_>, Error>
324+
pub fn copy_in<T>(&mut self, query: &T) -> Result<CopyInWriter<'_>, Error>
328325
where
329326
T: ?Sized + ToStatement,
330327
{
331-
let sink = self.runtime.block_on(self.client.copy_in(query, params))?;
328+
let sink = self.runtime.block_on(self.client.copy_in(query))?;
332329
Ok(CopyInWriter::new(&mut self.runtime, sink))
333330
}
334331

335332
/// Executes a `COPY TO STDOUT` statement, returning a reader of the resulting data.
336333
///
337-
/// The `query` argument can either be a `Statement`, or a raw query string.
334+
/// The `query` argument can either be a `Statement`, or a raw query string. PostgreSQL does not support parameters
335+
/// in `COPY` statements, so this method does not take any.
338336
///
339337
/// # Examples
340338
///
@@ -345,21 +343,17 @@ impl Client {
345343
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
346344
/// let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
347345
///
348-
/// let mut reader = client.copy_out("COPY people TO stdout", &[])?;
346+
/// let mut reader = client.copy_out("COPY people TO stdout")?;
349347
/// let mut buf = vec![];
350348
/// reader.read_to_end(&mut buf)?;
351349
/// # Ok(())
352350
/// # }
353351
/// ```
354-
pub fn copy_out<T>(
355-
&mut self,
356-
query: &T,
357-
params: &[&(dyn ToSql + Sync)],
358-
) -> Result<CopyOutReader<'_>, Error>
352+
pub fn copy_out<T>(&mut self, query: &T) -> Result<CopyOutReader<'_>, Error>
359353
where
360354
T: ?Sized + ToStatement,
361355
{
362-
let stream = self.runtime.block_on(self.client.copy_out(query, params))?;
356+
let stream = self.runtime.block_on(self.client.copy_out(query))?;
363357
CopyOutReader::new(&mut self.runtime, stream)
364358
}
365359

postgres/src/test.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ fn copy_in() {
154154
.simple_query("CREATE TEMPORARY TABLE foo (id INT, name TEXT)")
155155
.unwrap();
156156

157-
let mut writer = client.copy_in("COPY foo FROM stdin", &[]).unwrap();
157+
let mut writer = client.copy_in("COPY foo FROM stdin").unwrap();
158158
writer.write_all(b"1\tsteven\n2\ttimothy").unwrap();
159159
writer.finish().unwrap();
160160

@@ -177,7 +177,7 @@ fn copy_in_abort() {
177177
.simple_query("CREATE TEMPORARY TABLE foo (id INT, name TEXT)")
178178
.unwrap();
179179

180-
let mut writer = client.copy_in("COPY foo FROM stdin", &[]).unwrap();
180+
let mut writer = client.copy_in("COPY foo FROM stdin").unwrap();
181181
writer.write_all(b"1\tsteven\n2\ttimothy").unwrap();
182182
drop(writer);
183183

@@ -199,9 +199,7 @@ fn copy_out() {
199199
)
200200
.unwrap();
201201

202-
let mut reader = client
203-
.copy_out("COPY foo (id, name) TO STDOUT", &[])
204-
.unwrap();
202+
let mut reader = client.copy_out("COPY foo (id, name) TO STDOUT").unwrap();
205203
let mut s = String::new();
206204
reader.read_to_string(&mut s).unwrap();
207205
drop(reader);

postgres/src/transaction.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -137,32 +137,20 @@ impl<'a> Transaction<'a> {
137137
}
138138

139139
/// Like `Client::copy_in`.
140-
pub fn copy_in<T>(
141-
&mut self,
142-
query: &T,
143-
params: &[&(dyn ToSql + Sync)],
144-
) -> Result<CopyInWriter<'_>, Error>
140+
pub fn copy_in<T>(&mut self, query: &T) -> Result<CopyInWriter<'_>, Error>
145141
where
146142
T: ?Sized + ToStatement,
147143
{
148-
let sink = self
149-
.runtime
150-
.block_on(self.transaction.copy_in(query, params))?;
144+
let sink = self.runtime.block_on(self.transaction.copy_in(query))?;
151145
Ok(CopyInWriter::new(self.runtime, sink))
152146
}
153147

154148
/// Like `Client::copy_out`.
155-
pub fn copy_out<T>(
156-
&mut self,
157-
query: &T,
158-
params: &[&(dyn ToSql + Sync)],
159-
) -> Result<CopyOutReader<'_>, Error>
149+
pub fn copy_out<T>(&mut self, query: &T) -> Result<CopyOutReader<'_>, Error>
160150
where
161151
T: ?Sized + ToStatement,
162152
{
163-
let stream = self
164-
.runtime
165-
.block_on(self.transaction.copy_out(query, params))?;
153+
let stream = self.runtime.block_on(self.transaction.copy_out(query))?;
166154
CopyOutReader::new(self.runtime, stream)
167155
}
168156

tokio-postgres-binary-copy/src/test.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async fn write_basic() {
2424
.unwrap();
2525

2626
let sink = client
27-
.copy_in("COPY foo (id, bar) FROM STDIN BINARY", &[])
27+
.copy_in("COPY foo (id, bar) FROM STDIN BINARY")
2828
.await
2929
.unwrap();
3030
let writer = BinaryCopyInWriter::new(sink, &[Type::INT4, Type::TEXT]);
@@ -58,7 +58,7 @@ async fn write_many_rows() {
5858
.unwrap();
5959

6060
let sink = client
61-
.copy_in("COPY foo (id, bar) FROM STDIN BINARY", &[])
61+
.copy_in("COPY foo (id, bar) FROM STDIN BINARY")
6262
.await
6363
.unwrap();
6464
let writer = BinaryCopyInWriter::new(sink, &[Type::INT4, Type::TEXT]);
@@ -94,7 +94,7 @@ async fn write_big_rows() {
9494
.unwrap();
9595

9696
let sink = client
97-
.copy_in("COPY foo (id, bar) FROM STDIN BINARY", &[])
97+
.copy_in("COPY foo (id, bar) FROM STDIN BINARY")
9898
.await
9999
.unwrap();
100100
let writer = BinaryCopyInWriter::new(sink, &[Type::INT4, Type::BYTEA]);
@@ -135,7 +135,7 @@ async fn read_basic() {
135135
.unwrap();
136136

137137
let stream = client
138-
.copy_out("COPY foo (id, bar) TO STDIN BINARY", &[])
138+
.copy_out("COPY foo (id, bar) TO STDIN BINARY")
139139
.await
140140
.unwrap();
141141
let rows = BinaryCopyOutStream::new(&[Type::INT4, Type::TEXT], stream)
@@ -164,7 +164,7 @@ async fn read_many_rows() {
164164
.unwrap();
165165

166166
let stream = client
167-
.copy_out("COPY foo (id, bar) TO STDIN BINARY", &[])
167+
.copy_out("COPY foo (id, bar) TO STDIN BINARY")
168168
.await
169169
.unwrap();
170170
let rows = BinaryCopyOutStream::new(&[Type::INT4, Type::TEXT], stream)
@@ -198,7 +198,7 @@ async fn read_big_rows() {
198198
}
199199

200200
let stream = client
201-
.copy_out("COPY foo (id, bar) TO STDIN BINARY", &[])
201+
.copy_out("COPY foo (id, bar) TO STDIN BINARY")
202202
.await
203203
.unwrap();
204204
let rows = BinaryCopyOutStream::new(&[Type::INT4, Type::BYTEA], stream)

tokio-postgres/src/client.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -378,42 +378,40 @@ impl Client {
378378

379379
/// Executes a `COPY FROM STDIN` statement, returning a sink used to write the copy data.
380380
///
381-
/// The copy *must* be explicitly completed via the `Sink::close` or `finish` methods. If it is
382-
/// not, the copy will be aborted.
381+
/// PostgreSQL does not support parameters in `COPY` statements, so this method does not take any. The copy *must*
382+
/// be explicitly completed via the `Sink::close` or `finish` methods. If it is not, the copy will be aborted.
383383
///
384384
/// # Panics
385385
///
386-
/// Panics if the number of parameters provided does not match the number expected.
386+
/// Panics if the statement contains parameters.
387387
pub async fn copy_in<T, U>(
388388
&self,
389389
statement: &T,
390-
params: &[&(dyn ToSql + Sync)],
391390
) -> Result<CopyInSink<U>, Error>
392391
where
393392
T: ?Sized + ToStatement,
394393
U: Buf + 'static + Send,
395394
{
396395
let statement = statement.__convert().into_statement(self).await?;
397-
let params = slice_iter(params);
398-
copy_in::copy_in(self.inner(), statement, params).await
396+
copy_in::copy_in(self.inner(), statement).await
399397
}
400398

401399
/// Executes a `COPY TO STDOUT` statement, returning a stream of the resulting data.
402400
///
401+
/// PostgreSQL does not support parameters in `COPY` statements, so this method does not take any.
402+
///
403403
/// # Panics
404404
///
405-
/// Panics if the number of parameters provided does not match the number expected.
405+
/// Panics if the statement contains parameters.
406406
pub async fn copy_out<T>(
407407
&self,
408408
statement: &T,
409-
params: &[&(dyn ToSql + Sync)],
410409
) -> Result<CopyOutStream, Error>
411410
where
412411
T: ?Sized + ToStatement,
413412
{
414413
let statement = statement.__convert().into_statement(self).await?;
415-
let params = slice_iter(params);
416-
copy_out::copy_out(self.inner(), statement, params).await
414+
copy_out::copy_out(self.inner(), statement).await
417415
}
418416

419417
/// Executes a sequence of SQL statements using the simple query protocol, returning the resulting rows.

tokio-postgres/src/copy_in.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::client::{InnerClient, Responses};
22
use crate::codec::FrontendMessage;
33
use crate::connection::RequestMessages;
4-
use crate::types::ToSql;
5-
use crate::{query, Error, Statement};
4+
use crate::{query, Error, Statement, slice_iter};
65
use bytes::buf::BufExt;
76
use bytes::{Buf, BufMut, BytesMut};
87
use futures::channel::mpsc;
@@ -196,17 +195,14 @@ where
196195
}
197196
}
198197

199-
pub async fn copy_in<'a, I, T>(
198+
pub async fn copy_in<T>(
200199
client: &InnerClient,
201200
statement: Statement,
202-
params: I,
203201
) -> Result<CopyInSink<T>, Error>
204202
where
205-
I: IntoIterator<Item = &'a dyn ToSql>,
206-
I::IntoIter: ExactSizeIterator,
207203
T: Buf + 'static + Send,
208204
{
209-
let buf = query::encode(client, &statement, params)?;
205+
let buf = query::encode(client, &statement, slice_iter(&[]))?;
210206

211207
let (mut sender, receiver) = mpsc::channel(1);
212208
let receiver = CopyInReceiver::new(receiver);

tokio-postgres/src/copy_out.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::client::{InnerClient, Responses};
22
use crate::codec::FrontendMessage;
33
use crate::connection::RequestMessages;
4-
use crate::types::ToSql;
5-
use crate::{query, Error, Statement};
4+
use crate::{query, Error, Statement, slice_iter};
65
use bytes::Bytes;
76
use futures::{ready, Stream};
87
use pin_project_lite::pin_project;
@@ -11,16 +10,11 @@ use std::marker::PhantomPinned;
1110
use std::pin::Pin;
1211
use std::task::{Context, Poll};
1312

14-
pub async fn copy_out<'a, I>(
13+
pub async fn copy_out(
1514
client: &InnerClient,
1615
statement: Statement,
17-
params: I,
18-
) -> Result<CopyOutStream, Error>
19-
where
20-
I: IntoIterator<Item = &'a dyn ToSql>,
21-
I::IntoIter: ExactSizeIterator,
22-
{
23-
let buf = query::encode(client, &statement, params)?;
16+
) -> Result<CopyOutStream, Error> {
17+
let buf = query::encode(client, &statement, slice_iter(&[]))?;
2418
let responses = start(client, buf).await?;
2519
Ok(CopyOutStream {
2620
responses,

tokio-postgres/src/transaction.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -224,25 +224,23 @@ impl<'a> Transaction<'a> {
224224
pub async fn copy_in<T, U>(
225225
&self,
226226
statement: &T,
227-
params: &[&(dyn ToSql + Sync)],
228227
) -> Result<CopyInSink<U>, Error>
229228
where
230229
T: ?Sized + ToStatement,
231230
U: Buf + 'static + Send,
232231
{
233-
self.client.copy_in(statement, params).await
232+
self.client.copy_in(statement).await
234233
}
235234

236235
/// Like `Client::copy_out`.
237236
pub async fn copy_out<T>(
238237
&self,
239238
statement: &T,
240-
params: &[&(dyn ToSql + Sync)],
241239
) -> Result<CopyOutStream, Error>
242240
where
243241
T: ?Sized + ToStatement,
244242
{
245-
self.client.copy_out(statement, params).await
243+
self.client.copy_out(statement).await
246244
}
247245

248246
/// Like `Client::simple_query`.

tokio-postgres/tests/test/main.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ async fn copy_in() {
418418
.into_iter()
419419
.map(Ok::<_, Error>),
420420
);
421-
let sink = client.copy_in("COPY foo FROM STDIN", &[]).await.unwrap();
421+
let sink = client.copy_in("COPY foo FROM STDIN").await.unwrap();
422422
pin_mut!(sink);
423423
sink.send_all(&mut stream).await.unwrap();
424424
let rows = sink.finish().await.unwrap();
@@ -465,7 +465,7 @@ async fn copy_in_large() {
465465
.map(Ok::<_, Error>),
466466
);
467467

468-
let sink = client.copy_in("COPY foo FROM STDIN", &[]).await.unwrap();
468+
let sink = client.copy_in("COPY foo FROM STDIN").await.unwrap();
469469
pin_mut!(sink);
470470
sink.send_all(&mut stream).await.unwrap();
471471
let rows = sink.finish().await.unwrap();
@@ -487,7 +487,7 @@ async fn copy_in_error() {
487487
.unwrap();
488488

489489
{
490-
let sink = client.copy_in("COPY foo FROM STDIN", &[]).await.unwrap();
490+
let sink = client.copy_in("COPY foo FROM STDIN").await.unwrap();
491491
pin_mut!(sink);
492492
sink.send(Bytes::from_static(b"1\tsteven")).await.unwrap();
493493
}
@@ -517,7 +517,7 @@ async fn copy_out() {
517517

518518
let stmt = client.prepare("COPY foo TO STDOUT").await.unwrap();
519519
let data = client
520-
.copy_out(&stmt, &[])
520+
.copy_out(&stmt)
521521
.await
522522
.unwrap()
523523
.try_fold(BytesMut::new(), |mut buf, chunk| {

0 commit comments

Comments
 (0)