Add sanity check for insert that does not do an insertion

This commit is contained in:
John Gallagher 2016-05-16 11:15:07 -05:00
parent 504b16dc98
commit 7b174c97f8
2 changed files with 37 additions and 9 deletions

View File

@ -9,10 +9,16 @@ impl<'conn> Statement<'conn> {
/// # Failure /// # Failure
/// Will return `Err` if no row is inserted or many rows are inserted. /// Will return `Err` if no row is inserted or many rows are inserted.
pub fn insert(&mut self, params: &[&ToSql]) -> Result<i64> { pub fn insert(&mut self, params: &[&ToSql]) -> Result<i64> {
// Some non-insertion queries could still return 1 change (an UPDATE, for example), so
// to guard against that we can check that the connection's last_insert_rowid() changes
// after we execute the statement.
let prev_rowid = self.conn.last_insert_rowid();
let changes = try!(self.execute(params)); let changes = try!(self.execute(params));
let new_rowid = self.conn.last_insert_rowid();
match changes { match changes {
1 => Ok(self.conn.last_insert_rowid()), 1 if prev_rowid != new_rowid => Ok(new_rowid),
_ => Err(Error::QueryInsertedRows(changes)) 1 if prev_rowid == new_rowid => Err(Error::StatementFailedToInsertRow),
_ => Err(Error::StatementChangedRows(changes))
} }
} }
@ -45,16 +51,31 @@ mod test {
assert_eq!(stmt.insert(&[&1i32]).unwrap(), 1); assert_eq!(stmt.insert(&[&1i32]).unwrap(), 1);
assert_eq!(stmt.insert(&[&2i32]).unwrap(), 2); assert_eq!(stmt.insert(&[&2i32]).unwrap(), 2);
match stmt.insert(&[&1i32]).unwrap_err() { match stmt.insert(&[&1i32]).unwrap_err() {
Error::QueryInsertedRows(0) => (), Error::StatementChangedRows(0) => (),
err => panic!("Unexpected error {}", err), err => panic!("Unexpected error {}", err),
} }
let mut multi = db.prepare("INSERT INTO foo (x) SELECT 3 UNION ALL SELECT 4").unwrap(); let mut multi = db.prepare("INSERT INTO foo (x) SELECT 3 UNION ALL SELECT 4").unwrap();
match multi.insert(&[]).unwrap_err() { match multi.insert(&[]).unwrap_err() {
Error::QueryInsertedRows(2) => (), Error::StatementChangedRows(2) => (),
err => panic!("Unexpected error {}", err), err => panic!("Unexpected error {}", err),
} }
} }
#[test]
fn test_insert_failures() {
let db = Connection::open_in_memory().unwrap();
db.execute_batch("CREATE TABLE foo(x INTEGER UNIQUE)").unwrap();
let mut insert = db.prepare("INSERT INTO foo (x) VALUES (?)").unwrap();
let mut update = db.prepare("UPDATE foo SET x = ?").unwrap();
assert_eq!(insert.insert(&[&1i32]).unwrap(), 1);
match update.insert(&[&2i32]) {
Err(Error::StatementFailedToInsertRow) => (),
r => panic!("Unexpected result {:?}", r),
}
}
#[test] #[test]
fn test_exists() { fn test_exists() {
let db = Connection::open_in_memory().unwrap(); let db = Connection::open_in_memory().unwrap();

View File

@ -57,7 +57,11 @@ pub enum Error {
InvalidColumnType, InvalidColumnType,
/// Error when a query that was expected to insert one row did not insert any or insert many. /// Error when a query that was expected to insert one row did not insert any or insert many.
QueryInsertedRows(c_int), StatementChangedRows(c_int),
/// Error when a query that was expected to insert a row did not change the connection's
/// last_insert_rowid().
StatementFailedToInsertRow,
/// Error returned by `functions::Context::get` when the function argument cannot be converted /// Error returned by `functions::Context::get` when the function argument cannot be converted
/// to the requested type. /// to the requested type.
@ -105,7 +109,8 @@ impl fmt::Display for Error {
Error::InvalidColumnIndex(i) => write!(f, "Invalid column index: {}", i), Error::InvalidColumnIndex(i) => write!(f, "Invalid column index: {}", i),
Error::InvalidColumnName(ref name) => write!(f, "Invalid column name: {}", name), Error::InvalidColumnName(ref name) => write!(f, "Invalid column name: {}", name),
Error::InvalidColumnType => write!(f, "Invalid column type"), Error::InvalidColumnType => write!(f, "Invalid column type"),
Error::QueryInsertedRows(i) => write!(f, "Query inserted {} rows", i), Error::StatementChangedRows(i) => write!(f, "Query changed {} rows", i),
Error::StatementFailedToInsertRow => write!(f, "Statement failed to insert new row"),
#[cfg(feature = "functions")] #[cfg(feature = "functions")]
Error::InvalidFunctionParameterType => write!(f, "Invalid function parameter type"), Error::InvalidFunctionParameterType => write!(f, "Invalid function parameter type"),
@ -136,7 +141,8 @@ impl error::Error for Error {
Error::InvalidColumnIndex(_) => "invalid column index", Error::InvalidColumnIndex(_) => "invalid column index",
Error::InvalidColumnName(_) => "invalid column name", Error::InvalidColumnName(_) => "invalid column name",
Error::InvalidColumnType => "invalid column type", Error::InvalidColumnType => "invalid column type",
Error::QueryInsertedRows(_) => "query inserted zero or more than one row", Error::StatementChangedRows(_) => "query inserted zero or more than one row",
Error::StatementFailedToInsertRow => "statement failed to insert new row",
#[cfg(feature = "functions")] #[cfg(feature = "functions")]
Error::InvalidFunctionParameterType => "invalid function parameter type", Error::InvalidFunctionParameterType => "invalid function parameter type",
@ -162,7 +168,8 @@ impl error::Error for Error {
Error::InvalidColumnName(_) | Error::InvalidColumnName(_) |
Error::InvalidColumnType | Error::InvalidColumnType |
Error::InvalidPath(_) => None, Error::InvalidPath(_) => None,
Error::QueryInsertedRows(_) => None, Error::StatementChangedRows(_) => None,
Error::StatementFailedToInsertRow => None,
#[cfg(feature = "functions")] #[cfg(feature = "functions")]
Error::InvalidFunctionParameterType => None, Error::InvalidFunctionParameterType => None,