From 7b8051dc7ea087d3346ba71fcfc10a62310fe39a Mon Sep 17 00:00:00 2001 From: Gwenael Treguier Date: Thu, 6 Aug 2015 21:15:30 +0200 Subject: [PATCH 01/12] Check Rust str length before binding. --- src/types.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/types.rs b/src/types.rs index 1d30b30..f55a80e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -102,8 +102,12 @@ raw_to_impl!(c_double, sqlite3_bind_double); impl<'a> ToSql for &'a str { unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int { + let length = self.len(); + if length > ::std::i32::MAX as usize { + return ffi::SQLITE_TOOBIG; + } match str_to_cstring(self) { - Ok(c_str) => ffi::sqlite3_bind_text(stmt, col, c_str.as_ptr(), -1, + Ok(c_str) => ffi::sqlite3_bind_text(stmt, col, c_str.as_ptr(), length as c_int, ffi::SQLITE_TRANSIENT()), Err(_) => ffi::SQLITE_MISUSE, } From 9c63b9f37a24117c022129b0f37736938df5e563 Mon Sep 17 00:00:00 2001 From: Gwenael Treguier Date: Thu, 6 Aug 2015 21:45:54 +0200 Subject: [PATCH 02/12] Check Rust blob length before binding. --- src/types.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/types.rs b/src/types.rs index f55a80e..5ad4ed7 100644 --- a/src/types.rs +++ b/src/types.rs @@ -122,6 +122,9 @@ impl ToSql for String { impl<'a> ToSql for &'a [u8] { unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int { + if self.len() > ::std::i32::MAX as usize { + return ffi::SQLITE_TOOBIG; + } ffi::sqlite3_bind_blob( stmt, col, mem::transmute(self.as_ptr()), self.len() as c_int, ffi::SQLITE_TRANSIENT()) } From 6bc1a8bb59fd22d07646495b0426703e755a9785 Mon Sep 17 00:00:00 2001 From: Gwenael Treguier Date: Sat, 8 Aug 2015 09:30:50 +0200 Subject: [PATCH 03/12] Check when statement is too long. --- src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index f98780b..c49da64 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -542,6 +542,12 @@ impl InnerSqliteConnection { fn prepare<'a>(&mut self, conn: &'a SqliteConnection, sql: &str) -> SqliteResult> { + if sql.len() >= ::std::i32::MAX as usize { + return Err(SqliteError { + code: ffi::SQLITE_TOOBIG, + message: "statement too long".to_string() + }); + } let mut c_stmt: *mut ffi::sqlite3_stmt = unsafe { mem::uninitialized() }; let c_sql = try!(str_to_cstring(sql)); let r = unsafe { From e1532f5edfeec447de44465494df739f23e5e6a8 Mon Sep 17 00:00:00 2001 From: Patrick Fernie Date: Thu, 27 Aug 2015 10:44:24 -0400 Subject: [PATCH 04/12] Correct idx-checking behavior for SqliteRow::get_checked() --- src/lib.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f98780b..290c4a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -866,9 +866,19 @@ impl<'stmt> SqliteRow<'stmt> { /// Returns a `SQLITE_MISMATCH`-coded `SqliteError` if the underlying SQLite column /// type is not a valid type as a source for `T`. /// - /// Panics if `idx` is outside the range of columns in the returned query or if this row - /// is stale. + /// Returns a `SQLITE_MISUSE`-coded `SqliteError` if `idx` is outside the valid column range + /// for this row or if this row is stale. pub fn get_checked(&self, idx: c_int) -> SqliteResult { + if self.row_idx != self.current_row.get() { + return Err(SqliteError{ code: ffi::SQLITE_MISUSE, + message: "Cannot get values from a row after advancing to next row".to_string() }); + } + unsafe { + if idx < 0 || idx >= ffi::sqlite3_column_count(self.stmt.stmt) { + return Err(SqliteError{ code: ffi::SQLITE_MISUSE, + message: "Invalid column index".to_string() }); + } + } let valid_column_type = unsafe { T::column_has_valid_sqlite_type(self.stmt.stmt, idx) }; From 29072e585b289426bd248fb6ddc523adad8dc833 Mon Sep 17 00:00:00 2001 From: Patrick Fernie Date: Thu, 27 Aug 2015 13:43:43 -0400 Subject: [PATCH 05/12] Implement SqliteStatement::query_and_then() Allows for more ergonomic unification of error types --- src/lib.rs | 226 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 224 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 290c4a2..229cc8e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,6 +55,7 @@ extern crate libsqlite3_sys as ffi; #[macro_use] extern crate bitflags; use std::default::Default; +use std::convert; use std::mem; use std::ptr; use std::fmt; @@ -90,7 +91,7 @@ unsafe fn errmsg_to_string(errmsg: *const c_char) -> String { } /// Encompasses an error result from a call to the SQLite C API. -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct SqliteError { /// The error code returned by a SQLite C API call. See [SQLite Result /// Codes](http://www.sqlite.org/rescode.html) for details. @@ -664,7 +665,7 @@ impl<'conn> SqliteStatement<'conn> { } /// Executes the prepared statement and maps a function over the resulting - /// rows. + /// rows. /// /// Unlike the iterator produced by `query`, the returned iterator does not expose the possibility /// for accessing stale rows. @@ -680,6 +681,25 @@ impl<'conn> SqliteStatement<'conn> { }) } + /// Executes the prepared statement and maps a function over the resulting + /// rows, where the function returns a `Result` with `Error` type implementing + /// `std::convert::From` (so errors can be unified). + /// + /// Unlike the iterator produced by `query`, the returned iterator does not expose the possibility + /// for accessing stale rows. + pub fn query_and_then<'a, T, E, F>(&'a mut self, params: &[&ToSql], f: F) + -> SqliteResult> + where T: 'static, + E: convert::From, + F: FnMut(SqliteRow) -> Result { + let row_iter = try!(self.query(params)); + + Ok(AndThenRows{ + rows: row_iter, + map: f, + }) + } + /// Consumes the statement. /// /// Functionally equivalent to the `Drop` implementation, but allows callers to see any errors @@ -746,6 +766,28 @@ impl<'stmt, T, F> Iterator for MappedRows<'stmt, F> } } +/// An iterator over the mapped resulting rows of a query, with an Error type +/// unifying with SqliteError. +pub struct AndThenRows<'stmt, F> { + rows: SqliteRows<'stmt>, + map: F, +} + +impl<'stmt, T, E, F> Iterator for AndThenRows<'stmt, F> + where T: 'static, + E: convert::From, + F: FnMut(SqliteRow) -> Result { + type Item = Result; + + // Through the magic of FromIterator, if F returns a Result, + // you can collect that to a Result, E> + fn next(&mut self) -> Option { + self.rows.next().map(|row_result| row_result + .map_err(E::from) + .and_then(|row| (self.map)(row))) + } +} + /// An iterator over the resulting rows of a query. /// /// ## Warning @@ -920,6 +962,8 @@ mod test { extern crate tempdir; use super::*; use self::tempdir::TempDir; + use std::error::Error as StdError; + use std::fmt; // this function is never called, but is still type checked; in // particular, calls with specific instantiations will require @@ -1081,6 +1125,184 @@ mod test { assert_eq!(results.unwrap().concat(), "hello, world!"); } + #[test] + fn test_query_and_then() { + let db = checked_memory_handle(); + let sql = "BEGIN; + CREATE TABLE foo(x INTEGER, y TEXT); + INSERT INTO foo VALUES(4, \"hello\"); + INSERT INTO foo VALUES(3, \", \"); + INSERT INTO foo VALUES(2, \"world\"); + INSERT INTO foo VALUES(1, \"!\"); + END;"; + db.execute_batch(sql).unwrap(); + + let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap(); + let results: SqliteResult> = query + .query_and_then(&[], |row| row.get_checked(1)) + .unwrap() + .collect(); + + assert_eq!(results.unwrap().concat(), "hello, world!"); + } + + #[test] + fn test_query_and_then_fails() { + let db = checked_memory_handle(); + let sql = "BEGIN; + CREATE TABLE foo(x INTEGER, y TEXT); + INSERT INTO foo VALUES(4, \"hello\"); + INSERT INTO foo VALUES(3, \", \"); + INSERT INTO foo VALUES(2, \"world\"); + INSERT INTO foo VALUES(1, \"!\"); + END;"; + db.execute_batch(sql).unwrap(); + + let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap(); + let bad_type: SqliteResult> = query + .query_and_then(&[], |row| row.get_checked(1)) + .unwrap() + .collect(); + + assert_eq!(bad_type, Err(SqliteError{ + code: ffi::SQLITE_MISMATCH, + message: "Invalid column type".to_owned(), + })); + + let bad_idx: SqliteResult> = query + .query_and_then(&[], |row| row.get_checked(3)) + .unwrap() + .collect(); + + assert_eq!(bad_idx, Err(SqliteError{ + code: ffi::SQLITE_MISUSE, + message: "Invalid column index".to_owned(), + })); + } + + #[test] + fn test_query_and_then_custom_error() { + #[derive(Debug)] + enum CustomError { + Sqlite(SqliteError), + }; + + impl fmt::Display for CustomError { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match *self { + CustomError::Sqlite(ref se) => write!(f, "{}: {}", self.description(), se), + } + } + } + + impl StdError for CustomError { + fn description(&self) -> &str { "my custom error" } + fn cause(&self) -> Option<&StdError> { + match *self { + CustomError::Sqlite(ref se) => Some(se), + } + } + } + + impl From for CustomError { + fn from(se: SqliteError) -> CustomError { + CustomError::Sqlite(se) + } + } + type CustomResult = Result; + + let db = checked_memory_handle(); + let sql = "BEGIN; + CREATE TABLE foo(x INTEGER, y TEXT); + INSERT INTO foo VALUES(4, \"hello\"); + INSERT INTO foo VALUES(3, \", \"); + INSERT INTO foo VALUES(2, \"world\"); + INSERT INTO foo VALUES(1, \"!\"); + END;"; + db.execute_batch(sql).unwrap(); + + let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap(); + let results: CustomResult> = query + .query_and_then(&[], |row| row.get_checked(1).map_err(CustomError::Sqlite)) + .unwrap() + .collect(); + + assert_eq!(results.unwrap().concat(), "hello, world!"); + } + + #[test] + fn test_query_and_then_custom_error_fails() { + #[derive(Debug, PartialEq)] + enum CustomError { + SomeError, + Sqlite(SqliteError), + }; + + impl fmt::Display for CustomError { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match *self { + CustomError::SomeError => write!(f, "{}", self.description()), + CustomError::Sqlite(ref se) => write!(f, "{}: {}", self.description(), se), + } + } + } + + impl StdError for CustomError { + fn description(&self) -> &str { "my custom error" } + fn cause(&self) -> Option<&StdError> { + match *self { + CustomError::SomeError => None, + CustomError::Sqlite(ref se) => Some(se), + } + } + } + + impl From for CustomError { + fn from(se: SqliteError) -> CustomError { + CustomError::Sqlite(se) + } + } + type CustomResult = Result; + + let db = checked_memory_handle(); + let sql = "BEGIN; + CREATE TABLE foo(x INTEGER, y TEXT); + INSERT INTO foo VALUES(4, \"hello\"); + INSERT INTO foo VALUES(3, \", \"); + INSERT INTO foo VALUES(2, \"world\"); + INSERT INTO foo VALUES(1, \"!\"); + END;"; + db.execute_batch(sql).unwrap(); + + let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap(); + let bad_type: CustomResult> = query + .query_and_then(&[], |row| row.get_checked(1).map_err(CustomError::Sqlite)) + .unwrap() + .collect(); + + assert_eq!(bad_type, Err(CustomError::Sqlite(SqliteError{ + code: ffi::SQLITE_MISMATCH, + message: "Invalid column type".to_owned(), + }))); + + let bad_idx: CustomResult> = query + .query_and_then(&[], |row| row.get_checked(3).map_err(CustomError::Sqlite)) + .unwrap() + .collect(); + + assert_eq!(bad_idx, Err(CustomError::Sqlite(SqliteError{ + code: ffi::SQLITE_MISUSE, + message: "Invalid column index".to_owned(), + }))); + + let non_sqlite_err: CustomResult> = query + .query_and_then(&[], |_| Err(CustomError::SomeError)) + .unwrap() + .collect(); + + assert_eq!(non_sqlite_err, Err(CustomError::SomeError)); + } + #[test] fn test_query_row() { let db = checked_memory_handle(); From e4eda2041e804f38922952cac1ee2e1e974e191b Mon Sep 17 00:00:00 2001 From: Patrick Fernie Date: Thu, 27 Aug 2015 14:01:01 -0400 Subject: [PATCH 06/12] Implement SqliteConnection::query_row_and_then() --- src/lib.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 229cc8e..91f32db 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -302,6 +302,37 @@ impl SqliteConnection { } } + /// Convenience method to execute a query that is expected to return a single row, + /// and execute a mapping via `f` on that returned row with the possibility of failure. + /// The `Result` type of `f` must implement `std::convert::From`. + /// + /// ## Example + /// + /// ```rust,no_run + /// # use rusqlite::{SqliteResult,SqliteConnection}; + /// fn preferred_locale(conn: &SqliteConnection) -> SqliteResult { + /// conn.query_row_and_then("SELECT value FROM preferences WHERE name='locale'", &[], |row| { + /// row.get_checked(0) + /// }) + /// } + /// ``` + /// + /// If the query returns more than one row, all rows except the first are ignored. + pub fn query_row_and_then(&self, sql: &str, params: &[&ToSql], f: F) -> Result + where F: FnOnce(SqliteRow) -> Result, + E: convert::From { + let mut stmt = try!(self.prepare(sql)); + let mut rows = try!(stmt.query(params)); + + match rows.next() { + Some(row) => row.map_err(E::from).and_then(f), + None => Err(E::from(SqliteError{ + code: ffi::SQLITE_NOTICE, + message: "Query did not return a row".to_string(), + })) + } + } + /// Convenience method to execute a query that is expected to return a single row. /// /// ## Example @@ -779,8 +810,6 @@ impl<'stmt, T, E, F> Iterator for AndThenRows<'stmt, F> F: FnMut(SqliteRow) -> Result { type Item = Result; - // Through the magic of FromIterator, if F returns a Result, - // you can collect that to a Result, E> fn next(&mut self) -> Option { self.rows.next().map(|row_result| row_result .map_err(E::from) From 05669082a343ad08166968336810cc72bf51941a Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 1 Aug 2015 12:04:02 +0200 Subject: [PATCH 07/12] Debug db path and stmt sql. --- src/lib.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 530cfc0..eb96b11 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,7 +58,7 @@ use std::default::Default; use std::mem; use std::ptr; use std::fmt; -use std::path::{Path}; +use std::path::{Path,PathBuf}; use std::error; use std::rc::{Rc}; use std::cell::{RefCell, Cell}; @@ -151,6 +151,7 @@ fn path_to_cstring(p: &Path) -> SqliteResult { /// prepare multiple statements at the same time). pub struct SqliteConnection { db: RefCell, + path: Option, } unsafe impl Send for SqliteConnection {} @@ -179,7 +180,7 @@ impl SqliteConnection { -> SqliteResult { let c_path = try!(path_to_cstring(path.as_ref())); InnerSqliteConnection::open_with_flags(&c_path, flags).map(|db| { - SqliteConnection{ db: RefCell::new(db) } + SqliteConnection{ db: RefCell::new(db), path: Some(path.as_ref().to_path_buf()) } }) } @@ -190,7 +191,7 @@ impl SqliteConnection { pub fn open_in_memory_with_flags(flags: SqliteOpenFlags) -> SqliteResult { let c_memory = try!(str_to_cstring(":memory:")); InnerSqliteConnection::open_with_flags(&c_memory, flags).map(|db| { - SqliteConnection{ db: RefCell::new(db) } + SqliteConnection{ db: RefCell::new(db), path: None } }) } @@ -411,7 +412,7 @@ impl SqliteConnection { impl fmt::Debug for SqliteConnection { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "SqliteConnection()") + write!(f, "SqliteConnection( path: {:?} )", &self.path) } } @@ -673,7 +674,7 @@ impl<'conn> SqliteStatement<'conn> { } /// Executes the prepared statement and maps a function over the resulting - /// rows. + /// rows. /// /// Unlike the iterator produced by `query`, the returned iterator does not expose the possibility /// for accessing stale rows. @@ -728,7 +729,11 @@ impl<'conn> SqliteStatement<'conn> { impl<'conn> fmt::Debug for SqliteStatement<'conn> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Statement( conn: {:?}, stmt: {:?} )", self.conn, self.stmt) + let sql = unsafe { + let c_slice = CStr::from_ptr(ffi::sqlite3_sql(self.stmt)).to_bytes(); + str::from_utf8(c_slice) + }; + write!(f, "SqliteStatement( conn: {:?}, stmt: {:?}, sql: {:?} )", self.conn, self.stmt, sql) } } From d07c7ec8a6b9bb2935b0b464aef7137b582a782b Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 20 Sep 2015 20:44:51 -0400 Subject: [PATCH 08/12] Add basic unit test of statement debug including SQL --- src/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index eb96b11..622fb09 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1161,4 +1161,13 @@ mod test { } assert_eq!(db.last_insert_rowid(), 10); } + + #[test] + fn test_statement_debugging() { + let db = checked_memory_handle(); + let query = "SELECT 12345"; + let stmt = db.prepare(query).unwrap(); + + assert!(format!("{:?}", stmt).contains(query)); + } } From 072a336b33d42ec8726e820359e6d9342ada50bb Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 20 Sep 2015 21:28:50 -0400 Subject: [PATCH 09/12] Refactor: Reduce duplication across query_and_then tests. --- src/lib.rs | 342 +++++++++++++++++++++++++---------------------------- 1 file changed, 160 insertions(+), 182 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cd381b2..c142a1c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1003,10 +1003,10 @@ impl<'stmt> SqliteRow<'stmt> { mod test { extern crate libsqlite3_sys as ffi; extern crate tempdir; - use super::*; + pub use super::*; use self::tempdir::TempDir; - use std::error::Error as StdError; - use std::fmt; + pub use std::error::Error as StdError; + pub use std::fmt; // this function is never called, but is still type checked; in // particular, calls with specific instantiations will require @@ -1016,7 +1016,7 @@ mod test { ensure_send::(); } - fn checked_memory_handle() -> SqliteConnection { + pub fn checked_memory_handle() -> SqliteConnection { SqliteConnection::open_in_memory().unwrap() } @@ -1176,184 +1176,6 @@ mod test { assert_eq!(results.unwrap().concat(), "hello, world!"); } - #[test] - fn test_query_and_then() { - let db = checked_memory_handle(); - let sql = "BEGIN; - CREATE TABLE foo(x INTEGER, y TEXT); - INSERT INTO foo VALUES(4, \"hello\"); - INSERT INTO foo VALUES(3, \", \"); - INSERT INTO foo VALUES(2, \"world\"); - INSERT INTO foo VALUES(1, \"!\"); - END;"; - db.execute_batch(sql).unwrap(); - - let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap(); - let results: SqliteResult> = query - .query_and_then(&[], |row| row.get_checked(1)) - .unwrap() - .collect(); - - assert_eq!(results.unwrap().concat(), "hello, world!"); - } - - #[test] - fn test_query_and_then_fails() { - let db = checked_memory_handle(); - let sql = "BEGIN; - CREATE TABLE foo(x INTEGER, y TEXT); - INSERT INTO foo VALUES(4, \"hello\"); - INSERT INTO foo VALUES(3, \", \"); - INSERT INTO foo VALUES(2, \"world\"); - INSERT INTO foo VALUES(1, \"!\"); - END;"; - db.execute_batch(sql).unwrap(); - - let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap(); - let bad_type: SqliteResult> = query - .query_and_then(&[], |row| row.get_checked(1)) - .unwrap() - .collect(); - - assert_eq!(bad_type, Err(SqliteError{ - code: ffi::SQLITE_MISMATCH, - message: "Invalid column type".to_owned(), - })); - - let bad_idx: SqliteResult> = query - .query_and_then(&[], |row| row.get_checked(3)) - .unwrap() - .collect(); - - assert_eq!(bad_idx, Err(SqliteError{ - code: ffi::SQLITE_MISUSE, - message: "Invalid column index".to_owned(), - })); - } - - #[test] - fn test_query_and_then_custom_error() { - #[derive(Debug)] - enum CustomError { - Sqlite(SqliteError), - }; - - impl fmt::Display for CustomError { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - match *self { - CustomError::Sqlite(ref se) => write!(f, "{}: {}", self.description(), se), - } - } - } - - impl StdError for CustomError { - fn description(&self) -> &str { "my custom error" } - fn cause(&self) -> Option<&StdError> { - match *self { - CustomError::Sqlite(ref se) => Some(se), - } - } - } - - impl From for CustomError { - fn from(se: SqliteError) -> CustomError { - CustomError::Sqlite(se) - } - } - type CustomResult = Result; - - let db = checked_memory_handle(); - let sql = "BEGIN; - CREATE TABLE foo(x INTEGER, y TEXT); - INSERT INTO foo VALUES(4, \"hello\"); - INSERT INTO foo VALUES(3, \", \"); - INSERT INTO foo VALUES(2, \"world\"); - INSERT INTO foo VALUES(1, \"!\"); - END;"; - db.execute_batch(sql).unwrap(); - - let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap(); - let results: CustomResult> = query - .query_and_then(&[], |row| row.get_checked(1).map_err(CustomError::Sqlite)) - .unwrap() - .collect(); - - assert_eq!(results.unwrap().concat(), "hello, world!"); - } - - #[test] - fn test_query_and_then_custom_error_fails() { - #[derive(Debug, PartialEq)] - enum CustomError { - SomeError, - Sqlite(SqliteError), - }; - - impl fmt::Display for CustomError { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - match *self { - CustomError::SomeError => write!(f, "{}", self.description()), - CustomError::Sqlite(ref se) => write!(f, "{}: {}", self.description(), se), - } - } - } - - impl StdError for CustomError { - fn description(&self) -> &str { "my custom error" } - fn cause(&self) -> Option<&StdError> { - match *self { - CustomError::SomeError => None, - CustomError::Sqlite(ref se) => Some(se), - } - } - } - - impl From for CustomError { - fn from(se: SqliteError) -> CustomError { - CustomError::Sqlite(se) - } - } - type CustomResult = Result; - - let db = checked_memory_handle(); - let sql = "BEGIN; - CREATE TABLE foo(x INTEGER, y TEXT); - INSERT INTO foo VALUES(4, \"hello\"); - INSERT INTO foo VALUES(3, \", \"); - INSERT INTO foo VALUES(2, \"world\"); - INSERT INTO foo VALUES(1, \"!\"); - END;"; - db.execute_batch(sql).unwrap(); - - let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap(); - let bad_type: CustomResult> = query - .query_and_then(&[], |row| row.get_checked(1).map_err(CustomError::Sqlite)) - .unwrap() - .collect(); - - assert_eq!(bad_type, Err(CustomError::Sqlite(SqliteError{ - code: ffi::SQLITE_MISMATCH, - message: "Invalid column type".to_owned(), - }))); - - let bad_idx: CustomResult> = query - .query_and_then(&[], |row| row.get_checked(3).map_err(CustomError::Sqlite)) - .unwrap() - .collect(); - - assert_eq!(bad_idx, Err(CustomError::Sqlite(SqliteError{ - code: ffi::SQLITE_MISUSE, - message: "Invalid column index".to_owned(), - }))); - - let non_sqlite_err: CustomResult> = query - .query_and_then(&[], |_| Err(CustomError::SomeError)) - .unwrap() - .collect(); - - assert_eq!(non_sqlite_err, Err(CustomError::SomeError)); - } - #[test] fn test_query_row() { let db = checked_memory_handle(); @@ -1431,4 +1253,160 @@ mod test { assert!(format!("{:?}", stmt).contains(query)); } + + mod query_and_then_tests { + extern crate libsqlite3_sys as ffi; + use super::*; + + #[derive(Debug, PartialEq)] + enum CustomError { + SomeError, + Sqlite(SqliteError), + } + + impl fmt::Display for CustomError { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match *self { + CustomError::SomeError => write!(f, "{}", self.description()), + CustomError::Sqlite(ref se) => write!(f, "{}: {}", self.description(), se), + } + } + } + + impl StdError for CustomError { + fn description(&self) -> &str { "my custom error" } + fn cause(&self) -> Option<&StdError> { + match *self { + CustomError::SomeError => None, + CustomError::Sqlite(ref se) => Some(se), + } + } + } + + impl From for CustomError { + fn from(se: SqliteError) -> CustomError { + CustomError::Sqlite(se) + } + } + + type CustomResult = Result; + + #[test] + fn test_query_and_then() { + let db = checked_memory_handle(); + let sql = "BEGIN; + CREATE TABLE foo(x INTEGER, y TEXT); + INSERT INTO foo VALUES(4, \"hello\"); + INSERT INTO foo VALUES(3, \", \"); + INSERT INTO foo VALUES(2, \"world\"); + INSERT INTO foo VALUES(1, \"!\"); + END;"; + db.execute_batch(sql).unwrap(); + + let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap(); + let results: SqliteResult> = query + .query_and_then(&[], |row| row.get_checked(1)) + .unwrap() + .collect(); + + assert_eq!(results.unwrap().concat(), "hello, world!"); + } + + #[test] + fn test_query_and_then_fails() { + let db = checked_memory_handle(); + let sql = "BEGIN; + CREATE TABLE foo(x INTEGER, y TEXT); + INSERT INTO foo VALUES(4, \"hello\"); + INSERT INTO foo VALUES(3, \", \"); + INSERT INTO foo VALUES(2, \"world\"); + INSERT INTO foo VALUES(1, \"!\"); + END;"; + db.execute_batch(sql).unwrap(); + + let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap(); + let bad_type: SqliteResult> = query + .query_and_then(&[], |row| row.get_checked(1)) + .unwrap() + .collect(); + + assert_eq!(bad_type, Err(SqliteError{ + code: ffi::SQLITE_MISMATCH, + message: "Invalid column type".to_owned(), + })); + + let bad_idx: SqliteResult> = query + .query_and_then(&[], |row| row.get_checked(3)) + .unwrap() + .collect(); + + assert_eq!(bad_idx, Err(SqliteError{ + code: ffi::SQLITE_MISUSE, + message: "Invalid column index".to_owned(), + })); + } + + #[test] + fn test_query_and_then_custom_error() { + let db = checked_memory_handle(); + let sql = "BEGIN; + CREATE TABLE foo(x INTEGER, y TEXT); + INSERT INTO foo VALUES(4, \"hello\"); + INSERT INTO foo VALUES(3, \", \"); + INSERT INTO foo VALUES(2, \"world\"); + INSERT INTO foo VALUES(1, \"!\"); + END;"; + db.execute_batch(sql).unwrap(); + + let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap(); + let results: CustomResult> = query + .query_and_then(&[], |row| row.get_checked(1).map_err(CustomError::Sqlite)) + .unwrap() + .collect(); + + assert_eq!(results.unwrap().concat(), "hello, world!"); + } + + #[test] + fn test_query_and_then_custom_error_fails() { + let db = checked_memory_handle(); + let sql = "BEGIN; + CREATE TABLE foo(x INTEGER, y TEXT); + INSERT INTO foo VALUES(4, \"hello\"); + INSERT INTO foo VALUES(3, \", \"); + INSERT INTO foo VALUES(2, \"world\"); + INSERT INTO foo VALUES(1, \"!\"); + END;"; + db.execute_batch(sql).unwrap(); + + let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap(); + let bad_type: CustomResult> = query + .query_and_then(&[], |row| row.get_checked(1).map_err(CustomError::Sqlite)) + .unwrap() + .collect(); + + assert_eq!(bad_type, Err(CustomError::Sqlite(SqliteError{ + code: ffi::SQLITE_MISMATCH, + message: "Invalid column type".to_owned(), + }))); + + let bad_idx: CustomResult> = query + .query_and_then(&[], |row| row.get_checked(3).map_err(CustomError::Sqlite)) + .unwrap() + .collect(); + + assert_eq!(bad_idx, Err(CustomError::Sqlite(SqliteError{ + code: ffi::SQLITE_MISUSE, + message: "Invalid column index".to_owned(), + }))); + + let non_sqlite_err: CustomResult> = query + .query_and_then(&[], |_| Err(CustomError::SomeError)) + .unwrap() + .collect(); + + assert_eq!(non_sqlite_err, Err(CustomError::SomeError)); + } + + } } From 1918dc14d0419b42bb4765d30a989b4af121f757 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 20 Sep 2015 21:30:40 -0400 Subject: [PATCH 10/12] Add tests for query_row_and_then(). --- src/lib.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index c142a1c..83d364d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1408,5 +1408,53 @@ mod test { assert_eq!(non_sqlite_err, Err(CustomError::SomeError)); } + #[test] + fn test_query_row_and_then_custom_error() { + let db = checked_memory_handle(); + let sql = "BEGIN; + CREATE TABLE foo(x INTEGER, y TEXT); + INSERT INTO foo VALUES(4, \"hello\"); + END;"; + db.execute_batch(sql).unwrap(); + + let query = "SELECT x, y FROM foo ORDER BY x DESC"; + let results: CustomResult = db + .query_row_and_then(query, &[], |row| row.get_checked(1).map_err(CustomError::Sqlite)); + + assert_eq!(results.unwrap(), "hello"); + } + + #[test] + fn test_query_row_and_then_custom_error_fails() { + let db = checked_memory_handle(); + let sql = "BEGIN; + CREATE TABLE foo(x INTEGER, y TEXT); + INSERT INTO foo VALUES(4, \"hello\"); + END;"; + db.execute_batch(sql).unwrap(); + + let query = "SELECT x, y FROM foo ORDER BY x DESC"; + let bad_type: CustomResult = db + .query_row_and_then(query, &[], |row| row.get_checked(1).map_err(CustomError::Sqlite)); + + assert_eq!(bad_type, Err(CustomError::Sqlite(SqliteError{ + code: ffi::SQLITE_MISMATCH, + message: "Invalid column type".to_owned(), + }))); + + let bad_idx: CustomResult = db + .query_row_and_then(query, &[], |row| row.get_checked(3).map_err(CustomError::Sqlite)); + + assert_eq!(bad_idx, Err(CustomError::Sqlite(SqliteError{ + code: ffi::SQLITE_MISUSE, + message: "Invalid column index".to_owned(), + }))); + + let non_sqlite_err: CustomResult = db + .query_row_and_then(query, &[], |_| Err(CustomError::SomeError)); + + assert_eq!(non_sqlite_err, Err(CustomError::SomeError)); + } + } } From 7ee69fe103f4a55f8f832fb47157e9992e692c9c Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Mon, 21 Sep 2015 10:31:11 -0400 Subject: [PATCH 11/12] Remove get_opt (superceded by get_checked). --- src/lib.rs | 45 +++++++++++---------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 83d364d..fb3a50c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -941,7 +941,7 @@ impl<'stmt> SqliteRow<'stmt> { /// Panics if `idx` is outside the range of columns in the returned query or if this row /// is stale. pub fn get(&self, idx: c_int) -> T { - self.get_opt(idx).unwrap() + self.get_checked(idx).unwrap() } /// Get the value of a particular column of the result row. @@ -954,37 +954,6 @@ impl<'stmt> SqliteRow<'stmt> { /// Returns a `SQLITE_MISUSE`-coded `SqliteError` if `idx` is outside the valid column range /// for this row or if this row is stale. pub fn get_checked(&self, idx: c_int) -> SqliteResult { - if self.row_idx != self.current_row.get() { - return Err(SqliteError{ code: ffi::SQLITE_MISUSE, - message: "Cannot get values from a row after advancing to next row".to_string() }); - } - unsafe { - if idx < 0 || idx >= ffi::sqlite3_column_count(self.stmt.stmt) { - return Err(SqliteError{ code: ffi::SQLITE_MISUSE, - message: "Invalid column index".to_string() }); - } - } - let valid_column_type = unsafe { - T::column_has_valid_sqlite_type(self.stmt.stmt, idx) - }; - - if valid_column_type { - Ok(self.get(idx)) - } else { - Err(SqliteError{ - code: ffi::SQLITE_MISMATCH, - message: "Invalid column type".to_string(), - }) - } - } - - /// Attempt to get the value of a particular column of the result row. - /// - /// ## Failure - /// - /// Returns a `SQLITE_MISUSE`-coded `SqliteError` if `idx` is outside the valid column range - /// for this row or if this row is stale. - pub fn get_opt(&self, idx: c_int) -> SqliteResult { if self.row_idx != self.current_row.get() { return Err(SqliteError{ code: ffi::SQLITE_MISUSE, message: "Cannot get values from a row after advancing to next row".to_string() }); @@ -994,7 +963,15 @@ impl<'stmt> SqliteRow<'stmt> { return Err(SqliteError{ code: ffi::SQLITE_MISUSE, message: "Invalid column index".to_string() }); } - FromSql::column_result(self.stmt.stmt, idx) + + if T::column_has_valid_sqlite_type(self.stmt.stmt, idx) { + FromSql::column_result(self.stmt.stmt, idx) + } else { + Err(SqliteError{ + code: ffi::SQLITE_MISMATCH, + message: "Invalid column type".to_string(), + }) + } } } } @@ -1226,7 +1203,7 @@ mod test { assert_eq!(2i32, second.get(0)); - let result = first.get_opt::(0); + let result = first.get_checked::(0); assert!(result.unwrap_err().message.contains("advancing to next row")); } From c3bc8b594a40f12eecde0b05e262f2c03380f28d Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Mon, 21 Sep 2015 10:39:13 -0400 Subject: [PATCH 12/12] Bump version to 0.3.0. Updates Changelog and CONTRIBUTORS for changes in this version. --- CONTRIBUTORS.md | 2 ++ Cargo.toml | 2 +- Changelog.md | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 09b1187..e151714 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -5,3 +5,5 @@ rusqlite contributors (sorted alphabetically) * [Marcus Klaas de Vries](https://github.com/marcusklaas) * [gwenn](https://github.com/gwenn) * [Jimmy Lu](https://github.com/Yuhta) +* [Huon Wilson](https://github.com/huonw) +* [Patrick Fernie](https://github.com/pfernie) diff --git a/Cargo.toml b/Cargo.toml index 5d05ed5..0fbf0ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rusqlite" -version = "0.2.0" +version = "0.3.0" authors = ["John Gallagher "] description = "Ergonomic wrapper for SQLite" repository = "https://github.com/jgallagher/rusqlite" diff --git a/Changelog.md b/Changelog.md index 4fc6eff..6ce64d3 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,14 @@ +# Version 0.3.0 (2015-09-21) + +* Removes `get_opt`. Use `get_checked` instead. +* Add `query_row_and_then` and `query_and_then` convenience functions. These are analogous to + `query_row` and `query_map` but allow functions that can fail by returning `Result`s. +* Relax uses of `P: AsRef<...>` from `&P` to `P`. +* Add additional error check for calling `execute` when `query` was intended. +* Improve debug formatting of `SqliteStatement` and `SqliteConnection`. +* Changes documentation of `get_checked` to correctly indicate that it returns errors (not panics) + when given invalid types or column indices. + # Version 0.2.0 (2015-07-26) * Add `column_names()` to `SqliteStatement`.