From f817ec86bc6ced5a2b2fda791619bc8339c28dc3 Mon Sep 17 00:00:00 2001 From: gwenn Date: Thu, 26 May 2016 21:16:09 +0200 Subject: [PATCH 1/9] Use new Rust 1.9 attribute: #[deprecated]. --- appveyor.yml | 2 +- src/error.rs | 1 + src/lib.rs | 7 +++++++ src/load_extension_guard.rs | 1 + src/transaction.rs | 2 ++ 5 files changed, 12 insertions(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index e15cb90..50054f6 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,5 +1,5 @@ environment: - TARGET: 1.8.0-x86_64-pc-windows-gnu + TARGET: 1.9.0-x86_64-pc-windows-gnu MSYS2_BITS: 64 install: - ps: Start-FileDownload "https://static.rust-lang.org/dist/rust-${env:TARGET}.exe" diff --git a/src/error.rs b/src/error.rs index 1d6ccd4..580468a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,6 +6,7 @@ use libc::c_int; use {ffi, errmsg_to_string}; /// Old name for `Error`. `SqliteError` is deprecated. +#[deprecated] pub type SqliteError = Error; /// Enum listing possible errors from rusqlite. diff --git a/src/lib.rs b/src/lib.rs index 8d84890..12331a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,6 +105,7 @@ mod raw_statement; const STATEMENT_CACHE_DEFAULT_CAPACITY: usize = 16; /// Old name for `Result`. `SqliteResult` is deprecated. +#[deprecated] pub type SqliteResult = Result; /// A typedef of the result returned by many methods. @@ -151,6 +152,7 @@ impl<'a> DatabaseName<'a> { } /// Old name for `Connection`. `SqliteConnection` is deprecated. +#[deprecated] pub type SqliteConnection = Connection; /// A connection to a SQLite database. @@ -367,6 +369,7 @@ impl Connection { /// /// This method should be considered deprecated. Use `query_row` instead, which now /// does exactly the same thing. + #[deprecated] pub fn query_row_safe(&self, sql: &str, params: &[&ToSql], f: F) -> Result where F: FnOnce(Row) -> T { @@ -507,6 +510,7 @@ struct InnerConnection { } /// Old name for `OpenFlags`. `SqliteOpenFlags` is deprecated. +#[deprecated] pub type SqliteOpenFlags = OpenFlags; bitflags! { @@ -678,6 +682,7 @@ impl Drop for InnerConnection { } /// Old name for `Statement`. `SqliteStatement` is deprecated. +#[deprecated] pub type SqliteStatement<'conn> = Statement<'conn>; /// A prepared statement. @@ -967,6 +972,7 @@ impl<'stmt, T, E, F> Iterator for AndThenRows<'stmt, F> } /// Old name for `Rows`. `SqliteRows` is deprecated. +#[deprecated] pub type SqliteRows<'stmt> = Rows<'stmt>; /// An handle for the resulting rows of a query. @@ -1031,6 +1037,7 @@ impl<'stmt> Drop for Rows<'stmt> { } /// Old name for `Row`. `SqliteRow` is deprecated. +#[deprecated] pub type SqliteRow<'a, 'stmt> = Row<'a, 'stmt>; /// A single result row of a query. diff --git a/src/load_extension_guard.rs b/src/load_extension_guard.rs index 7a02db4..c8f25b8 100644 --- a/src/load_extension_guard.rs +++ b/src/load_extension_guard.rs @@ -1,6 +1,7 @@ use {Result, Connection}; /// Old name for `LoadExtensionGuard`. `SqliteLoadExtensionGuard` is deprecated. +#[deprecated] pub type SqliteLoadExtensionGuard<'conn> = LoadExtensionGuard<'conn>; /// RAII guard temporarily enabling SQLite extensions to be loaded. diff --git a/src/transaction.rs b/src/transaction.rs index e678215..e66ccaa 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -2,6 +2,7 @@ use std::ops::Deref; use {Result, Connection}; /// Old name for `TransactionBehavior`. `SqliteTransactionBehavior` is deprecated. +#[deprecated] pub type SqliteTransactionBehavior = TransactionBehavior; /// Options for transaction behavior. See [BEGIN @@ -28,6 +29,7 @@ pub enum DropBehavior { } /// Old name for `Transaction`. `SqliteTransaction` is deprecated. +#[deprecated] pub type SqliteTransaction<'conn> = Transaction<'conn>; /// Represents a transaction on a database connection. From 6a4eacc927e062185c40150833d8090b35326c2c Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 28 May 2016 11:16:55 +0200 Subject: [PATCH 2/9] Simply use `cargo clippy` --- Cargo.toml | 1 - src/lib.rs | 2 -- 2 files changed, 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index df70e47..5085c32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,6 @@ time = "~0.1.0" bitflags = "0.7" lru-cache = "0.0.7" libc = "~0.2" -clippy = {version = "~0.0.58", optional = true} chrono = { version = "~0.2", optional = true } serde_json = { version = "0.6", optional = true } diff --git a/src/lib.rs b/src/lib.rs index 8d84890..bee0f1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,8 +50,6 @@ //! } //! } //! ``` -#![cfg_attr(feature="clippy", feature(plugin))] -#![cfg_attr(feature="clippy", plugin(clippy))] extern crate libc; extern crate libsqlite3_sys as ffi; From 7c0eba0475896f25d203f94ec22fc07073dbc7ed Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 29 May 2016 20:36:20 -0400 Subject: [PATCH 3/9] Add `since` and `note` for all deprecation tags. --- src/error.rs | 2 +- src/lib.rs | 14 +++++++------- src/load_extension_guard.rs | 2 +- src/transaction.rs | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/error.rs b/src/error.rs index 580468a..aadf688 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,7 +6,7 @@ use libc::c_int; use {ffi, errmsg_to_string}; /// Old name for `Error`. `SqliteError` is deprecated. -#[deprecated] +#[deprecated(since = "0.6.0", note = "Use Error instead")] pub type SqliteError = Error; /// Enum listing possible errors from rusqlite. diff --git a/src/lib.rs b/src/lib.rs index 12331a3..d550111 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,7 +105,7 @@ mod raw_statement; const STATEMENT_CACHE_DEFAULT_CAPACITY: usize = 16; /// Old name for `Result`. `SqliteResult` is deprecated. -#[deprecated] +#[deprecated(since = "0.6.0", note = "Use Result instead")] pub type SqliteResult = Result; /// A typedef of the result returned by many methods. @@ -152,7 +152,7 @@ impl<'a> DatabaseName<'a> { } /// Old name for `Connection`. `SqliteConnection` is deprecated. -#[deprecated] +#[deprecated(since = "0.6.0", note = "Use Connection instead")] pub type SqliteConnection = Connection; /// A connection to a SQLite database. @@ -369,7 +369,7 @@ impl Connection { /// /// This method should be considered deprecated. Use `query_row` instead, which now /// does exactly the same thing. - #[deprecated] + #[deprecated(since = "0.1.0", note = "Use query_row instead")] pub fn query_row_safe(&self, sql: &str, params: &[&ToSql], f: F) -> Result where F: FnOnce(Row) -> T { @@ -510,7 +510,7 @@ struct InnerConnection { } /// Old name for `OpenFlags`. `SqliteOpenFlags` is deprecated. -#[deprecated] +#[deprecated(since = "0.6.0", note = "Use OpenFlags instead")] pub type SqliteOpenFlags = OpenFlags; bitflags! { @@ -682,7 +682,7 @@ impl Drop for InnerConnection { } /// Old name for `Statement`. `SqliteStatement` is deprecated. -#[deprecated] +#[deprecated(since = "0.6.0", note = "Use Statement instead")] pub type SqliteStatement<'conn> = Statement<'conn>; /// A prepared statement. @@ -972,7 +972,7 @@ impl<'stmt, T, E, F> Iterator for AndThenRows<'stmt, F> } /// Old name for `Rows`. `SqliteRows` is deprecated. -#[deprecated] +#[deprecated(since = "0.6.0", note = "Use Rows instead")] pub type SqliteRows<'stmt> = Rows<'stmt>; /// An handle for the resulting rows of a query. @@ -1037,7 +1037,7 @@ impl<'stmt> Drop for Rows<'stmt> { } /// Old name for `Row`. `SqliteRow` is deprecated. -#[deprecated] +#[deprecated(since = "0.6.0", note = "Use Row instead")] pub type SqliteRow<'a, 'stmt> = Row<'a, 'stmt>; /// A single result row of a query. diff --git a/src/load_extension_guard.rs b/src/load_extension_guard.rs index c8f25b8..92db69b 100644 --- a/src/load_extension_guard.rs +++ b/src/load_extension_guard.rs @@ -1,7 +1,7 @@ use {Result, Connection}; /// Old name for `LoadExtensionGuard`. `SqliteLoadExtensionGuard` is deprecated. -#[deprecated] +#[deprecated(since = "0.6.0", note = "Use LoadExtensionGuard instead")] pub type SqliteLoadExtensionGuard<'conn> = LoadExtensionGuard<'conn>; /// RAII guard temporarily enabling SQLite extensions to be loaded. diff --git a/src/transaction.rs b/src/transaction.rs index e66ccaa..347f5cb 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -2,7 +2,7 @@ use std::ops::Deref; use {Result, Connection}; /// Old name for `TransactionBehavior`. `SqliteTransactionBehavior` is deprecated. -#[deprecated] +#[deprecated(since = "0.6.0", note = "Use TransactionBehavior instead")] pub type SqliteTransactionBehavior = TransactionBehavior; /// Options for transaction behavior. See [BEGIN @@ -29,7 +29,7 @@ pub enum DropBehavior { } /// Old name for `Transaction`. `SqliteTransaction` is deprecated. -#[deprecated] +#[deprecated(since = "0.6.0", note = "Use Transaction instead")] pub type SqliteTransaction<'conn> = Transaction<'conn>; /// Represents a transaction on a database connection. From b1b438158d0901f3ac0e744e748312595c194fcc Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 29 May 2016 20:38:46 -0400 Subject: [PATCH 4/9] Remove workaround for Rust compiler bug that was fixed in 1.9. --- src/lib.rs | 5 +---- src/named_params.rs | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bee0f1f..7910941 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -881,10 +881,7 @@ impl<'conn> Statement<'conn> { for (i, p) in params.iter().enumerate() { try!(unsafe { self.conn.decode_result( - // This should be - // `p.bind_parameter(self.stmt.ptr(), (i + 1) as c_int)` - // but that doesn't compile until Rust 1.9 due to a compiler bug. - ToSql::bind_parameter(*p, self.stmt.ptr(), (i + 1) as c_int) + p.bind_parameter(self.stmt.ptr(), (i + 1) as c_int) ) }); } diff --git a/src/named_params.rs b/src/named_params.rs index d864736..5486074 100644 --- a/src/named_params.rs +++ b/src/named_params.rs @@ -205,10 +205,7 @@ impl<'conn> Statement<'conn> { for &(name, value) in params { if let Some(i) = try!(self.parameter_index(name)) { try!(self.conn.decode_result(unsafe { - // This should be - // `value.bind_parameter(self.stmt.ptr(), i)` - // but that doesn't compile until Rust 1.9 due to a compiler bug. - ToSql::bind_parameter(value, self.stmt.ptr(), i) + value.bind_parameter(self.stmt.ptr(), i) })); } else { return Err(Error::InvalidParameterName(name.into())); From 1de7f4ae069cbfe57e49228fc7db773a7deff686 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 29 May 2016 20:39:27 -0400 Subject: [PATCH 5/9] Add deprecation note to Changelog. --- Changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Changelog.md b/Changelog.md index 233a58b..265ddd3 100644 --- a/Changelog.md +++ b/Changelog.md @@ -3,6 +3,8 @@ * BREAKING CHANGE: The `FromSql` trait has been redesigned. It now requires a single, safe method instead of the previous definition which required implementing one or two unsafe methods. +* Added `#[deprecated(since = "...", note = "...")]` flags (new in Rust 1.9 for libraries) to + all deprecated APIs. # Version 0.7.2 (2016-05-19) From ad0b823560852e0939f78a6a4de4e3bda92b2a35 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Wed, 1 Jun 2016 20:52:22 -0400 Subject: [PATCH 6/9] Remove sanity check in `insert()` that could return `StatementFailedToInsertRow`. This was intended to detect an `UPDATE` query passed to `insert`, but incorrectly failed if inserts to different tables caused the same row ID to be returned from both. `UPDATE`s are no longer detectable. --- src/convenient.rs | 37 ++++++++++++++++++++----------------- src/error.rs | 9 +-------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/convenient.rs b/src/convenient.rs index 5f867b4..22817a5 100644 --- a/src/convenient.rs +++ b/src/convenient.rs @@ -4,18 +4,20 @@ use types::ToSql; impl<'conn> Statement<'conn> { /// 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 + /// /// Will return `Err` if no row is inserted or many rows are inserted. pub fn insert(&mut self, params: &[&ToSql]) -> Result { - // 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 new_rowid = self.conn.last_insert_rowid(); match changes { - 1 if prev_rowid != new_rowid => Ok(new_rowid), - 1 if prev_rowid == new_rowid => Err(Error::StatementFailedToInsertRow), + 1 => Ok(self.conn.last_insert_rowid()), _ => Err(Error::StatementChangedRows(changes)), } } @@ -57,18 +59,19 @@ mod 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(); - 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(); + db.execute_batch(r" + CREATE TABLE foo(x INTEGER); + CREATE TABLE bar(x INTEGER); + ") + .unwrap(); - assert_eq!(insert.insert(&[&1i32]).unwrap(), 1); - - match update.insert(&[&2i32]) { - Err(Error::StatementFailedToInsertRow) => (), - r => panic!("Unexpected result {:?}", r), - } + assert_eq!(db.prepare("INSERT INTO foo VALUES (10)").unwrap().insert(&[]).unwrap(), + 1); + assert_eq!(db.prepare("INSERT INTO bar VALUES (10)").unwrap().insert(&[]).unwrap(), + 1); } #[test] diff --git a/src/error.rs b/src/error.rs index 1d6ccd4..f5d37aa 100644 --- a/src/error.rs +++ b/src/error.rs @@ -55,10 +55,6 @@ pub enum Error { /// Error when a query that was expected to insert one row did not insert any or insert many. 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 /// to the requested type. #[cfg(feature = "functions")] @@ -105,7 +101,6 @@ impl fmt::Display for Error { Error::InvalidColumnName(ref name) => write!(f, "Invalid column name: {}", name), Error::InvalidColumnType => write!(f, "Invalid column type"), Error::StatementChangedRows(i) => write!(f, "Query changed {} rows", i), - Error::StatementFailedToInsertRow => write!(f, "Statement failed to insert new row"), #[cfg(feature = "functions")] Error::InvalidFunctionParameterType => write!(f, "Invalid function parameter type"), @@ -136,7 +131,6 @@ impl error::Error for Error { Error::InvalidColumnName(_) => "invalid column name", Error::InvalidColumnType => "invalid column type", Error::StatementChangedRows(_) => "query inserted zero or more than one row", - Error::StatementFailedToInsertRow => "statement failed to insert new row", #[cfg(feature = "functions")] Error::InvalidFunctionParameterType => "invalid function parameter type", @@ -161,8 +155,7 @@ impl error::Error for Error { Error::InvalidColumnName(_) | Error::InvalidColumnType | Error::InvalidPath(_) | - Error::StatementChangedRows(_) | - Error::StatementFailedToInsertRow => None, + Error::StatementChangedRows(_) => None, #[cfg(feature = "functions")] Error::InvalidFunctionParameterType => None, From b235b89555bd59820db3eb9e2b30370f8593acac Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Wed, 1 Jun 2016 21:06:56 -0400 Subject: [PATCH 7/9] Bump to version 0.7.3. --- Cargo.toml | 2 +- Changelog.md | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index df70e47..01134ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rusqlite" -version = "0.7.2" +version = "0.7.3" authors = ["John Gallagher "] description = "Ergonomic wrapper for SQLite" repository = "https://github.com/jgallagher/rusqlite" diff --git a/Changelog.md b/Changelog.md index 358d93f..b7c2d76 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,9 @@ +# 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) * BREAKING CHANGE: `Rows` no longer implements `Iterator`. It still has a `next()` method, but From 95050f10a848cd6861047715f65cf3c421024043 Mon Sep 17 00:00:00 2001 From: gwenn Date: Mon, 13 Jun 2016 20:22:21 +0200 Subject: [PATCH 8/9] Add test with empty blob (issue #174). --- src/types/mod.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/types/mod.rs b/src/types/mod.rs index 758eb5d..61deaf6 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -129,6 +129,17 @@ mod test { assert_eq!(v, v1234); } + #[test] + fn test_empty_blob() { + let db = checked_memory_handle(); + + let empty = vec![]; + db.execute("INSERT INTO foo(b) VALUES (?)", &[&empty]).unwrap(); + + let v: Vec = db.query_row("SELECT b FROM foo", &[], |r| r.get(0)).unwrap(); + assert_eq!(v, empty); + } + #[test] fn test_str() { let db = checked_memory_handle(); From 9f05147660096c9d68a4c60cfc88bba6861dd015 Mon Sep 17 00:00:00 2001 From: gwenn Date: Mon, 13 Jun 2016 20:32:39 +0200 Subject: [PATCH 9/9] Fix issue jgallagher/rusqlite#174 --- src/lib.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c571704..50cf7e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1123,12 +1123,17 @@ impl<'a> ValueRef<'a> { } ffi::SQLITE_BLOB => { let blob = ffi::sqlite3_column_blob(raw, col); - assert!(!blob.is_null(), "unexpected SQLITE_BLOB column type with NULL data"); let len = ffi::sqlite3_column_bytes(raw, col); assert!(len >= 0, "unexpected negative return from sqlite3_column_bytes"); + if len > 0 { + assert!(!blob.is_null(), "unexpected SQLITE_BLOB column type with NULL data"); + ValueRef::Blob(from_raw_parts(blob as *const u8, len as usize)) + } else { + // The return value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. + ValueRef::Blob(&[]) + } - ValueRef::Blob(from_raw_parts(blob as *const u8, len as usize)) } _ => unreachable!("sqlite3_column_type returned invalid value") }