Skip to content

Commit 4f54069

Browse files
authored
Merge pull request sfackler#999 from SvizelPritula/master
Make incorrect number of paramaters an error
2 parents 63c09eb + 7c7c80d commit 4f54069

File tree

4 files changed

+11
-58
lines changed

4 files changed

+11
-58
lines changed

postgres/src/client.rs

-20
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ impl Client {
5757
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
5858
/// with the `prepare` method.
5959
///
60-
/// # Panics
61-
///
62-
/// Panics if the number of parameters provided does not match the number expected.
63-
///
6460
/// # Example
6561
///
6662
/// ```no_run
@@ -96,10 +92,6 @@ impl Client {
9692
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
9793
/// with the `prepare` method.
9894
///
99-
/// # Panics
100-
///
101-
/// Panics if the number of parameters provided does not match the number expected.
102-
///
10395
/// # Examples
10496
///
10597
/// ```no_run
@@ -134,10 +126,6 @@ impl Client {
134126
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
135127
/// with the `prepare` method.
136128
///
137-
/// # Panics
138-
///
139-
/// Panics if the number of parameters provided does not match the number expected.
140-
///
141129
/// # Examples
142130
///
143131
/// ```no_run
@@ -172,10 +160,6 @@ impl Client {
172160
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
173161
/// with the `prepare` method.
174162
///
175-
/// # Panics
176-
///
177-
/// Panics if the number of parameters provided does not match the number expected.
178-
///
179163
/// # Examples
180164
///
181165
/// ```no_run
@@ -213,10 +197,6 @@ impl Client {
213197
/// It takes an iterator of parameters rather than a slice, and returns an iterator of rows rather than collecting
214198
/// them into an array.
215199
///
216-
/// # Panics
217-
///
218-
/// Panics if the number of parameters provided does not match the number expected.
219-
///
220200
/// # Examples
221201
///
222202
/// ```no_run

tokio-postgres/src/client.rs

-32
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,6 @@ impl Client {
230230
/// The `statement` argument can either be a `Statement`, or a raw query string. If the same statement will be
231231
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
232232
/// with the `prepare` method.
233-
///
234-
/// # Panics
235-
///
236-
/// Panics if the number of parameters provided does not match the number expected.
237233
pub async fn query<T>(
238234
&self,
239235
statement: &T,
@@ -258,10 +254,6 @@ impl Client {
258254
/// The `statement` argument can either be a `Statement`, or a raw query string. If the same statement will be
259255
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
260256
/// with the `prepare` method.
261-
///
262-
/// # Panics
263-
///
264-
/// Panics if the number of parameters provided does not match the number expected.
265257
pub async fn query_one<T>(
266258
&self,
267259
statement: &T,
@@ -295,10 +287,6 @@ impl Client {
295287
/// The `statement` argument can either be a `Statement`, or a raw query string. If the same statement will be
296288
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
297289
/// with the `prepare` method.
298-
///
299-
/// # Panics
300-
///
301-
/// Panics if the number of parameters provided does not match the number expected.
302290
pub async fn query_opt<T>(
303291
&self,
304292
statement: &T,
@@ -331,10 +319,6 @@ impl Client {
331319
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
332320
/// with the `prepare` method.
333321
///
334-
/// # Panics
335-
///
336-
/// Panics if the number of parameters provided does not match the number expected.
337-
///
338322
/// [`query`]: #method.query
339323
///
340324
/// # Examples
@@ -382,10 +366,6 @@ impl Client {
382366
/// with the `prepare` method.
383367
///
384368
/// If the statement does not modify any rows (e.g. `SELECT`), 0 is returned.
385-
///
386-
/// # Panics
387-
///
388-
/// Panics if the number of parameters provided does not match the number expected.
389369
pub async fn execute<T>(
390370
&self,
391371
statement: &T,
@@ -406,10 +386,6 @@ impl Client {
406386
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
407387
/// with the `prepare` method.
408388
///
409-
/// # Panics
410-
///
411-
/// Panics if the number of parameters provided does not match the number expected.
412-
///
413389
/// [`execute`]: #method.execute
414390
pub async fn execute_raw<T, P, I>(&self, statement: &T, params: I) -> Result<u64, Error>
415391
where
@@ -426,10 +402,6 @@ impl Client {
426402
///
427403
/// PostgreSQL does not support parameters in `COPY` statements, so this method does not take any. The copy *must*
428404
/// be explicitly completed via the `Sink::close` or `finish` methods. If it is not, the copy will be aborted.
429-
///
430-
/// # Panics
431-
///
432-
/// Panics if the statement contains parameters.
433405
pub async fn copy_in<T, U>(&self, statement: &T) -> Result<CopyInSink<U>, Error>
434406
where
435407
T: ?Sized + ToStatement,
@@ -442,10 +414,6 @@ impl Client {
442414
/// Executes a `COPY TO STDOUT` statement, returning a stream of the resulting data.
443415
///
444416
/// PostgreSQL does not support parameters in `COPY` statements, so this method does not take any.
445-
///
446-
/// # Panics
447-
///
448-
/// Panics if the statement contains parameters.
449417
pub async fn copy_out<T>(&self, statement: &T) -> Result<CopyOutStream, Error>
450418
where
451419
T: ?Sized + ToStatement,

tokio-postgres/src/error/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ enum Kind {
344344
ToSql(usize),
345345
FromSql(usize),
346346
Column(String),
347+
Parameters(usize, usize),
347348
Closed,
348349
Db,
349350
Parse,
@@ -383,6 +384,9 @@ impl fmt::Display for Error {
383384
Kind::ToSql(idx) => write!(fmt, "error serializing parameter {}", idx)?,
384385
Kind::FromSql(idx) => write!(fmt, "error deserializing column {}", idx)?,
385386
Kind::Column(column) => write!(fmt, "invalid column `{}`", column)?,
387+
Kind::Parameters(real, expected) => {
388+
write!(fmt, "expected {expected} parameters but got {real}")?
389+
}
386390
Kind::Closed => fmt.write_str("connection closed")?,
387391
Kind::Db => fmt.write_str("db error")?,
388392
Kind::Parse => fmt.write_str("error parsing response from server")?,
@@ -474,6 +478,10 @@ impl Error {
474478
Error::new(Kind::Column(column), None)
475479
}
476480

481+
pub(crate) fn parameters(real: usize, expected: usize) -> Error {
482+
Error::new(Kind::Parameters(real, expected), None)
483+
}
484+
477485
pub(crate) fn tls(e: Box<dyn error::Error + Sync + Send>) -> Error {
478486
Error::new(Kind::Tls, Some(e))
479487
}

tokio-postgres/src/query.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,9 @@ where
167167
let param_types = statement.params();
168168
let params = params.into_iter();
169169

170-
assert!(
171-
param_types.len() == params.len(),
172-
"expected {} parameters but got {}",
173-
param_types.len(),
174-
params.len()
175-
);
170+
if param_types.len() != params.len() {
171+
return Err(Error::parameters(params.len(), param_types.len()));
172+
}
176173

177174
let (param_formats, params): (Vec<_>, Vec<_>) = params
178175
.zip(param_types.iter())

0 commit comments

Comments
 (0)