mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
Return an error instead of panicing when parameter count is wron… (#675)
Fixes #671
This commit is contained in:
parent
6f6f7ffd9f
commit
0394e114d0
14
src/error.rs
14
src/error.rs
@ -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,
|
||||||
|
@ -450,13 +450,11 @@ 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<()> {
|
||||||
|
Loading…
Reference in New Issue
Block a user