Return an error instead of panicing when parameter count is wron… (#675)

Fixes #671
This commit is contained in:
Thom Chiovoloni 2020-04-06 12:47:35 -07:00 committed by GitHub
parent 6f6f7ffd9f
commit 0394e114d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 7 deletions

View File

@ -104,6 +104,10 @@ pub enum Error {
/// Error when the SQL contains multiple statements. /// Error when the SQL contains multiple statements.
MultipleStatement, MultipleStatement,
/// Error when the number of bound parameters does not match the number of
/// parameters in the query. The first `usize` is how many parameters were
/// given, the 2nd is how many were expected.
InvalidParameterCount(usize, usize),
} }
impl PartialEq for Error { impl PartialEq for Error {
@ -143,6 +147,9 @@ impl PartialEq for Error {
(Error::UnwindingPanic, Error::UnwindingPanic) => true, (Error::UnwindingPanic, Error::UnwindingPanic) => true,
#[cfg(feature = "functions")] #[cfg(feature = "functions")]
(Error::GetAuxWrongType, Error::GetAuxWrongType) => true, (Error::GetAuxWrongType, Error::GetAuxWrongType) => true,
(Error::InvalidParameterCount(i1, n1), Error::InvalidParameterCount(i2, n2)) => {
i1 == i2 && n1 == n2
}
(..) => false, (..) => false,
} }
} }
@ -228,6 +235,11 @@ impl fmt::Display for Error {
"Invalid column type {} at index: {}, name: {}", "Invalid column type {} at index: {}, name: {}",
t, i, name t, i, name
), ),
Error::InvalidParameterCount(i1, n1) => write!(
f,
"Wrong number of parameters passed to query. Got {}, needed {}",
i1, n1
),
Error::StatementChangedRows(i) => write!(f, "Query changed {} rows", i), Error::StatementChangedRows(i) => write!(f, "Query changed {} rows", i),
#[cfg(feature = "functions")] #[cfg(feature = "functions")]
@ -271,6 +283,7 @@ impl error::Error for Error {
Error::ExecuteReturnedResults => { Error::ExecuteReturnedResults => {
"execute returned results - did you mean to call query?" "execute returned results - did you mean to call query?"
} }
Error::InvalidParameterCount(..) => "Wrong number of parameters passed to query.",
Error::QueryReturnedNoRows => "query returned no rows", Error::QueryReturnedNoRows => "query returned no rows",
Error::InvalidColumnIndex(_) => "invalid column index", Error::InvalidColumnIndex(_) => "invalid column index",
Error::InvalidColumnName(_) => "invalid column name", Error::InvalidColumnName(_) => "invalid column name",
@ -310,6 +323,7 @@ impl error::Error for Error {
| Error::InvalidColumnName(_) | Error::InvalidColumnName(_)
| Error::InvalidColumnType(..) | Error::InvalidColumnType(..)
| Error::InvalidPath(_) | Error::InvalidPath(_)
| Error::InvalidParameterCount(..)
| Error::StatementChangedRows(_) | Error::StatementChangedRows(_)
| Error::InvalidQuery | Error::InvalidQuery
| Error::MultipleStatement => None, | Error::MultipleStatement => None,

View File

@ -450,14 +450,12 @@ impl Statement<'_> {
} }
self.bind_parameter(&p, index)?; self.bind_parameter(&p, index)?;
} }
assert_eq!( if index != expected {
index, expected, Err(Error::InvalidParameterCount(expected, index))
"incorrect number of parameters: expected {}, got {}", } else {
expected, index
);
Ok(()) Ok(())
} }
}
fn bind_parameters_named(&mut self, params: &[(&str, &dyn ToSql)]) -> Result<()> { fn bind_parameters_named(&mut self, params: &[(&str, &dyn ToSql)]) -> Result<()> {
for &(name, value) in params { for &(name, value) in params {