Make incorrect number of paramaters an error
This commit is contained in:
parent
63c09eb083
commit
7c7c80dcaa
@ -57,10 +57,6 @@ impl Client {
|
|||||||
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
||||||
/// with the `prepare` method.
|
/// with the `prepare` method.
|
||||||
///
|
///
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the number of parameters provided does not match the number expected.
|
|
||||||
///
|
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
@ -96,10 +92,6 @@ impl Client {
|
|||||||
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
||||||
/// with the `prepare` method.
|
/// with the `prepare` method.
|
||||||
///
|
///
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the number of parameters provided does not match the number expected.
|
|
||||||
///
|
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
@ -134,10 +126,6 @@ impl Client {
|
|||||||
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
||||||
/// with the `prepare` method.
|
/// with the `prepare` method.
|
||||||
///
|
///
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the number of parameters provided does not match the number expected.
|
|
||||||
///
|
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
@ -172,10 +160,6 @@ impl Client {
|
|||||||
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
||||||
/// with the `prepare` method.
|
/// with the `prepare` method.
|
||||||
///
|
///
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the number of parameters provided does not match the number expected.
|
|
||||||
///
|
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
@ -213,10 +197,6 @@ impl Client {
|
|||||||
/// It takes an iterator of parameters rather than a slice, and returns an iterator of rows rather than collecting
|
/// It takes an iterator of parameters rather than a slice, and returns an iterator of rows rather than collecting
|
||||||
/// them into an array.
|
/// them into an array.
|
||||||
///
|
///
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the number of parameters provided does not match the number expected.
|
|
||||||
///
|
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
|
@ -230,10 +230,6 @@ impl Client {
|
|||||||
/// The `statement` argument can either be a `Statement`, or a raw query string. If the same statement will be
|
/// The `statement` argument can either be a `Statement`, or a raw query string. If the same statement will be
|
||||||
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
||||||
/// with the `prepare` method.
|
/// with the `prepare` method.
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the number of parameters provided does not match the number expected.
|
|
||||||
pub async fn query<T>(
|
pub async fn query<T>(
|
||||||
&self,
|
&self,
|
||||||
statement: &T,
|
statement: &T,
|
||||||
@ -258,10 +254,6 @@ impl Client {
|
|||||||
/// The `statement` argument can either be a `Statement`, or a raw query string. If the same statement will be
|
/// The `statement` argument can either be a `Statement`, or a raw query string. If the same statement will be
|
||||||
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
||||||
/// with the `prepare` method.
|
/// with the `prepare` method.
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the number of parameters provided does not match the number expected.
|
|
||||||
pub async fn query_one<T>(
|
pub async fn query_one<T>(
|
||||||
&self,
|
&self,
|
||||||
statement: &T,
|
statement: &T,
|
||||||
@ -295,10 +287,6 @@ impl Client {
|
|||||||
/// The `statement` argument can either be a `Statement`, or a raw query string. If the same statement will be
|
/// The `statement` argument can either be a `Statement`, or a raw query string. If the same statement will be
|
||||||
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
||||||
/// with the `prepare` method.
|
/// with the `prepare` method.
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the number of parameters provided does not match the number expected.
|
|
||||||
pub async fn query_opt<T>(
|
pub async fn query_opt<T>(
|
||||||
&self,
|
&self,
|
||||||
statement: &T,
|
statement: &T,
|
||||||
@ -331,10 +319,6 @@ impl Client {
|
|||||||
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
||||||
/// with the `prepare` method.
|
/// with the `prepare` method.
|
||||||
///
|
///
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the number of parameters provided does not match the number expected.
|
|
||||||
///
|
|
||||||
/// [`query`]: #method.query
|
/// [`query`]: #method.query
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
@ -382,10 +366,6 @@ impl Client {
|
|||||||
/// with the `prepare` method.
|
/// with the `prepare` method.
|
||||||
///
|
///
|
||||||
/// If the statement does not modify any rows (e.g. `SELECT`), 0 is returned.
|
/// If the statement does not modify any rows (e.g. `SELECT`), 0 is returned.
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the number of parameters provided does not match the number expected.
|
|
||||||
pub async fn execute<T>(
|
pub async fn execute<T>(
|
||||||
&self,
|
&self,
|
||||||
statement: &T,
|
statement: &T,
|
||||||
@ -406,10 +386,6 @@ impl Client {
|
|||||||
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
/// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
|
||||||
/// with the `prepare` method.
|
/// with the `prepare` method.
|
||||||
///
|
///
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the number of parameters provided does not match the number expected.
|
|
||||||
///
|
|
||||||
/// [`execute`]: #method.execute
|
/// [`execute`]: #method.execute
|
||||||
pub async fn execute_raw<T, P, I>(&self, statement: &T, params: I) -> Result<u64, Error>
|
pub async fn execute_raw<T, P, I>(&self, statement: &T, params: I) -> Result<u64, Error>
|
||||||
where
|
where
|
||||||
@ -426,10 +402,6 @@ impl Client {
|
|||||||
///
|
///
|
||||||
/// PostgreSQL does not support parameters in `COPY` statements, so this method does not take any. The copy *must*
|
/// PostgreSQL does not support parameters in `COPY` statements, so this method does not take any. The copy *must*
|
||||||
/// be explicitly completed via the `Sink::close` or `finish` methods. If it is not, the copy will be aborted.
|
/// be explicitly completed via the `Sink::close` or `finish` methods. If it is not, the copy will be aborted.
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the statement contains parameters.
|
|
||||||
pub async fn copy_in<T, U>(&self, statement: &T) -> Result<CopyInSink<U>, Error>
|
pub async fn copy_in<T, U>(&self, statement: &T) -> Result<CopyInSink<U>, Error>
|
||||||
where
|
where
|
||||||
T: ?Sized + ToStatement,
|
T: ?Sized + ToStatement,
|
||||||
@ -442,10 +414,6 @@ impl Client {
|
|||||||
/// Executes a `COPY TO STDOUT` statement, returning a stream of the resulting data.
|
/// Executes a `COPY TO STDOUT` statement, returning a stream of the resulting data.
|
||||||
///
|
///
|
||||||
/// PostgreSQL does not support parameters in `COPY` statements, so this method does not take any.
|
/// PostgreSQL does not support parameters in `COPY` statements, so this method does not take any.
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the statement contains parameters.
|
|
||||||
pub async fn copy_out<T>(&self, statement: &T) -> Result<CopyOutStream, Error>
|
pub async fn copy_out<T>(&self, statement: &T) -> Result<CopyOutStream, Error>
|
||||||
where
|
where
|
||||||
T: ?Sized + ToStatement,
|
T: ?Sized + ToStatement,
|
||||||
|
@ -344,6 +344,7 @@ enum Kind {
|
|||||||
ToSql(usize),
|
ToSql(usize),
|
||||||
FromSql(usize),
|
FromSql(usize),
|
||||||
Column(String),
|
Column(String),
|
||||||
|
Parameters(usize, usize),
|
||||||
Closed,
|
Closed,
|
||||||
Db,
|
Db,
|
||||||
Parse,
|
Parse,
|
||||||
@ -383,6 +384,9 @@ impl fmt::Display for Error {
|
|||||||
Kind::ToSql(idx) => write!(fmt, "error serializing parameter {}", idx)?,
|
Kind::ToSql(idx) => write!(fmt, "error serializing parameter {}", idx)?,
|
||||||
Kind::FromSql(idx) => write!(fmt, "error deserializing column {}", idx)?,
|
Kind::FromSql(idx) => write!(fmt, "error deserializing column {}", idx)?,
|
||||||
Kind::Column(column) => write!(fmt, "invalid column `{}`", column)?,
|
Kind::Column(column) => write!(fmt, "invalid column `{}`", column)?,
|
||||||
|
Kind::Parameters(real, expected) => {
|
||||||
|
write!(fmt, "expected {expected} parameters but got {real}")?
|
||||||
|
}
|
||||||
Kind::Closed => fmt.write_str("connection closed")?,
|
Kind::Closed => fmt.write_str("connection closed")?,
|
||||||
Kind::Db => fmt.write_str("db error")?,
|
Kind::Db => fmt.write_str("db error")?,
|
||||||
Kind::Parse => fmt.write_str("error parsing response from server")?,
|
Kind::Parse => fmt.write_str("error parsing response from server")?,
|
||||||
@ -474,6 +478,10 @@ impl Error {
|
|||||||
Error::new(Kind::Column(column), None)
|
Error::new(Kind::Column(column), None)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn parameters(real: usize, expected: usize) -> Error {
|
||||||
|
Error::new(Kind::Parameters(real, expected), None)
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn tls(e: Box<dyn error::Error + Sync + Send>) -> Error {
|
pub(crate) fn tls(e: Box<dyn error::Error + Sync + Send>) -> Error {
|
||||||
Error::new(Kind::Tls, Some(e))
|
Error::new(Kind::Tls, Some(e))
|
||||||
}
|
}
|
||||||
|
@ -167,12 +167,9 @@ where
|
|||||||
let param_types = statement.params();
|
let param_types = statement.params();
|
||||||
let params = params.into_iter();
|
let params = params.into_iter();
|
||||||
|
|
||||||
assert!(
|
if param_types.len() != params.len() {
|
||||||
param_types.len() == params.len(),
|
return Err(Error::parameters(params.len(), param_types.len()));
|
||||||
"expected {} parameters but got {}",
|
}
|
||||||
param_types.len(),
|
|
||||||
params.len()
|
|
||||||
);
|
|
||||||
|
|
||||||
let (param_formats, params): (Vec<_>, Vec<_>) = params
|
let (param_formats, params): (Vec<_>, Vec<_>) = params
|
||||||
.zip(param_types.iter())
|
.zip(param_types.iter())
|
||||||
|
Loading…
Reference in New Issue
Block a user