Merge pull request #1419 from gwenn/reset

Check sqlite3_reset result
This commit is contained in:
gwenn 2023-11-25 10:54:33 +01:00 committed by GitHub
commit 75bd09d388
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 11 deletions

View File

@ -1494,7 +1494,7 @@ mod test {
#[test] #[test]
#[cfg(feature = "extra_check")] #[cfg(feature = "extra_check")]
fn test_execute_select() { fn test_execute_select_with_no_row() {
let db = checked_memory_handle(); let db = checked_memory_handle();
let err = db.execute("SELECT 1 WHERE 1 < ?1", [1i32]).unwrap_err(); let err = db.execute("SELECT 1 WHERE 1 < ?1", [1i32]).unwrap_err();
assert_eq!( assert_eq!(
@ -1504,6 +1504,13 @@ mod test {
); );
} }
#[test]
fn test_execute_select_with_row() {
let db = checked_memory_handle();
let err = db.execute("SELECT 1", []).unwrap_err();
assert_eq!(err, Error::ExecuteReturnedResults);
}
#[test] #[test]
#[cfg(feature = "extra_check")] #[cfg(feature = "extra_check")]
fn test_execute_multiple() { fn test_execute_multiple() {

View File

@ -14,9 +14,11 @@ pub struct Rows<'stmt> {
impl<'stmt> Rows<'stmt> { impl<'stmt> Rows<'stmt> {
#[inline] #[inline]
fn reset(&mut self) { fn reset(&mut self) -> Result<()> {
if let Some(stmt) = self.stmt.take() { if let Some(stmt) = self.stmt.take() {
stmt.reset(); stmt.reset()
} else {
Ok(())
} }
} }
@ -105,6 +107,7 @@ impl<'stmt> Rows<'stmt> {
} }
impl Drop for Rows<'_> { impl Drop for Rows<'_> {
#[allow(unused_must_use)]
#[inline] #[inline]
fn drop(&mut self) { fn drop(&mut self) {
self.reset(); self.reset();
@ -217,12 +220,12 @@ impl<'stmt> FallibleStreamingIterator for Rows<'stmt> {
Ok(()) Ok(())
} }
Ok(false) => { Ok(false) => {
self.reset(); let r = self.reset();
self.row = None; self.row = None;
Ok(()) r
} }
Err(e) => { Err(e) => {
self.reset(); let _ = self.reset(); // prevents infinite loop on error
self.row = None; self.row = None;
Err(e) Err(e)
} }

View File

@ -650,9 +650,12 @@ impl Statement<'_> {
fn execute_with_bound_parameters(&mut self) -> Result<usize> { fn execute_with_bound_parameters(&mut self) -> Result<usize> {
self.check_update()?; self.check_update()?;
let r = self.stmt.step(); let r = self.stmt.step();
self.stmt.reset(); let rr = self.stmt.reset();
match r { match r {
ffi::SQLITE_DONE => Ok(self.conn.changes() as usize), ffi::SQLITE_DONE => match rr {
ffi::SQLITE_OK => Ok(self.conn.changes() as usize),
_ => Err(self.conn.decode_result(rr).unwrap_err()),
},
ffi::SQLITE_ROW => Err(Error::ExecuteReturnedResults), ffi::SQLITE_ROW => Err(Error::ExecuteReturnedResults),
_ => Err(self.conn.decode_result(r).unwrap_err()), _ => Err(self.conn.decode_result(r).unwrap_err()),
} }
@ -847,8 +850,11 @@ impl Statement<'_> {
} }
#[inline] #[inline]
pub(super) fn reset(&self) -> c_int { pub(super) fn reset(&self) -> Result<()> {
self.stmt.reset() match self.stmt.reset() {
ffi::SQLITE_OK => Ok(()),
code => Err(self.conn.decode_result(code).unwrap_err()),
}
} }
} }
@ -1274,7 +1280,7 @@ mod test {
assert_eq!(0, stmt.column_count()); assert_eq!(0, stmt.column_count());
stmt.parameter_index("test").unwrap(); stmt.parameter_index("test").unwrap();
stmt.step().unwrap_err(); stmt.step().unwrap_err();
stmt.reset(); stmt.reset().unwrap(); // SQLITE_OMIT_AUTORESET = false
stmt.execute([]).unwrap_err(); stmt.execute([]).unwrap_err();
Ok(()) Ok(())
} }