Merge branch 'release-0.7' into merge-0.7.3

This commit is contained in:
John Gallagher 2016-06-01 21:31:26 -04:00
commit 58b7aea4f7
4 changed files with 28 additions and 26 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "rusqlite" name = "rusqlite"
version = "0.7.2" version = "0.7.3"
authors = ["John Gallagher <jgallagher@bignerdranch.com>"] authors = ["John Gallagher <jgallagher@bignerdranch.com>"]
description = "Ergonomic wrapper for SQLite" description = "Ergonomic wrapper for SQLite"
repository = "https://github.com/jgallagher/rusqlite" repository = "https://github.com/jgallagher/rusqlite"

View File

@ -6,6 +6,12 @@
* Added `#[deprecated(since = "...", note = "...")]` flags (new in Rust 1.9 for libraries) to * Added `#[deprecated(since = "...", note = "...")]` flags (new in Rust 1.9 for libraries) to
all deprecated APIs. all deprecated APIs.
# Version 0.7.3 (2016-06-01)
* Fixes an incorrect failure from the `insert()` convenience function when back-to-back inserts to
different tables both returned the same row ID
([#171](https://github.com/jgallagher/rusqlite/issues/171)).
# Version 0.7.2 (2016-05-19) # Version 0.7.2 (2016-05-19)
* BREAKING CHANGE: `Rows` no longer implements `Iterator`. It still has a `next()` method, but * BREAKING CHANGE: `Rows` no longer implements `Iterator`. It still has a `next()` method, but

View File

@ -4,18 +4,20 @@ use types::ToSql;
impl<'conn> Statement<'conn> { impl<'conn> Statement<'conn> {
/// Execute an INSERT and return the ROWID. /// Execute an INSERT and return the ROWID.
/// ///
/// # Note
///
/// This function is a convenience wrapper around `execute()` intended for queries that
/// insert a single item. It is possible to misuse this function in a way that it cannot
/// detect, such as by calling it on a statement which _updates_ a single item rather than
/// inserting one. Please don't do that.
///
/// # 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 if prev_rowid != new_rowid => Ok(new_rowid), 1 => Ok(self.conn.last_insert_rowid()),
1 if prev_rowid == new_rowid => Err(Error::StatementFailedToInsertRow),
_ => Err(Error::StatementChangedRows(changes)), _ => Err(Error::StatementChangedRows(changes)),
} }
} }
@ -57,18 +59,19 @@ mod test {
} }
#[test] #[test]
fn test_insert_failures() { fn test_insert_different_tables() {
// Test for https://github.com/jgallagher/rusqlite/issues/171
let db = Connection::open_in_memory().unwrap(); let db = Connection::open_in_memory().unwrap();
db.execute_batch("CREATE TABLE foo(x INTEGER UNIQUE)").unwrap(); db.execute_batch(r"
let mut insert = db.prepare("INSERT INTO foo (x) VALUES (?)").unwrap(); CREATE TABLE foo(x INTEGER);
let mut update = db.prepare("UPDATE foo SET x = ?").unwrap(); CREATE TABLE bar(x INTEGER);
")
.unwrap();
assert_eq!(insert.insert(&[&1i32]).unwrap(), 1); assert_eq!(db.prepare("INSERT INTO foo VALUES (10)").unwrap().insert(&[]).unwrap(),
1);
match update.insert(&[&2i32]) { assert_eq!(db.prepare("INSERT INTO bar VALUES (10)").unwrap().insert(&[]).unwrap(),
Err(Error::StatementFailedToInsertRow) => (), 1);
r => panic!("Unexpected result {:?}", r),
}
} }
#[test] #[test]

View File

@ -56,10 +56,6 @@ pub enum Error {
/// 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.
StatementChangedRows(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.
#[cfg(feature = "functions")] #[cfg(feature = "functions")]
@ -106,7 +102,6 @@ impl fmt::Display for Error {
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::StatementChangedRows(i) => write!(f, "Query changed {} 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"),
@ -137,7 +132,6 @@ impl error::Error for Error {
Error::InvalidColumnName(_) => "invalid column name", Error::InvalidColumnName(_) => "invalid column name",
Error::InvalidColumnType => "invalid column type", Error::InvalidColumnType => "invalid column type",
Error::StatementChangedRows(_) => "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,8 +156,7 @@ impl error::Error for Error {
Error::InvalidColumnName(_) | Error::InvalidColumnName(_) |
Error::InvalidColumnType | Error::InvalidColumnType |
Error::InvalidPath(_) | Error::InvalidPath(_) |
Error::StatementChangedRows(_) | Error::StatementChangedRows(_) => None,
Error::StatementFailedToInsertRow => None,
#[cfg(feature = "functions")] #[cfg(feature = "functions")]
Error::InvalidFunctionParameterType => None, Error::InvalidFunctionParameterType => None,