From f0b6bf9152f717955df2bcfa7a16187271746f67 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sat, 12 Dec 2015 13:53:58 -0500 Subject: [PATCH] Rename SqliteError -> Error. --- Changelog.md | 1 + src/backup.rs | 14 ++++---- src/functions.rs | 16 ++++----- src/lib.rs | 79 +++++++++++++++++++++++---------------------- src/named_params.rs | 4 +-- src/trace.rs | 4 +-- src/types.rs | 6 ++-- 7 files changed, 64 insertions(+), 60 deletions(-) diff --git a/Changelog.md b/Changelog.md index e1d2597..d23ea20 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,7 @@ # Version UPCOMING (TBD) * Renamed `SqliteConnection` to `Connection`. The old name remains as a typealias. +* Renamed `SqliteError` to `Error`. The old name remains as a typealias. * Adds a variety of `..._named` methods for executing queries using named placeholder parameters. * Adds `backup` feature that exposes SQLite's online backup API. * Adds `functions` feature that allows user-defined scalar functions to be added to diff --git a/src/backup.rs b/src/backup.rs index 5d9c2c2..02181f2 100644 --- a/src/backup.rs +++ b/src/backup.rs @@ -36,7 +36,7 @@ use std::time::Duration; use ffi; -use {DatabaseName, Connection, SqliteError, SqliteResult}; +use {DatabaseName, Connection, Error, SqliteResult}; impl Connection { /// Back up the `name` database to the given destination path. @@ -70,8 +70,8 @@ impl Connection { match r { Done => Ok(()), - Busy => Err(SqliteError::from_handle(ptr::null_mut(), ffi::SQLITE_BUSY)), - Locked => Err(SqliteError::from_handle(ptr::null_mut(), ffi::SQLITE_LOCKED)), + Busy => Err(Error::from_handle(ptr::null_mut(), ffi::SQLITE_BUSY)), + Locked => Err(Error::from_handle(ptr::null_mut(), ffi::SQLITE_LOCKED)), More => unreachable!(), } } @@ -115,8 +115,8 @@ impl Connection { match r { Done => Ok(()), - Busy => Err(SqliteError::from_handle(ptr::null_mut(), ffi::SQLITE_BUSY)), - Locked => Err(SqliteError::from_handle(ptr::null_mut(), ffi::SQLITE_LOCKED)), + Busy => Err(Error::from_handle(ptr::null_mut(), ffi::SQLITE_BUSY)), + Locked => Err(Error::from_handle(ptr::null_mut(), ffi::SQLITE_LOCKED)), More => unreachable!(), } } @@ -201,7 +201,7 @@ impl<'a, 'b> Backup<'a, 'b> { from.db.borrow_mut().db, from_name.as_ptr()); if b.is_null() { - return Err(SqliteError::from_handle(to_db, ffi::sqlite3_errcode(to_db))); + return Err(Error::from_handle(to_db, ffi::sqlite3_errcode(to_db))); } b }; @@ -245,7 +245,7 @@ impl<'a, 'b> Backup<'a, 'b> { ffi::SQLITE_BUSY => Ok(Busy), ffi::SQLITE_LOCKED => Ok(Locked), rc => { - Err(SqliteError { + Err(Error { code: rc, message: ffi::code_to_str(rc).into(), }) diff --git a/src/functions.rs b/src/functions.rs index 3ebe3d0..aa0fdf4 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -12,7 +12,7 @@ //! extern crate rusqlite; //! extern crate regex; //! -//! use rusqlite::{Connection, SqliteError, SqliteResult}; +//! use rusqlite::{Connection, Error, SqliteResult}; //! use std::collections::HashMap; //! use regex::Regex; //! @@ -26,7 +26,7 @@ //! match entry { //! Occupied(occ) => occ.into_mut(), //! Vacant(vac) => { -//! let r = try!(Regex::new(®ex_s).map_err(|e| SqliteError { +//! let r = try!(Regex::new(®ex_s).map_err(|e| Error { //! code: libsqlite3_sys::SQLITE_ERROR, //! message: format!("Invalid regular expression: {}", e), //! })); @@ -65,7 +65,7 @@ pub use ffi::sqlite3_value_numeric_type; use types::Null; -use {SqliteResult, SqliteError, Connection, str_to_cstring, InnerConnection}; +use {SqliteResult, Error, Connection, str_to_cstring, InnerConnection}; /// A trait for types that can be converted into the result of an SQL function. pub trait ToResult { @@ -228,7 +228,7 @@ impl FromValue for String { let utf8_str = str::from_utf8(c_slice); utf8_str.map(|s| s.to_string()) .map_err(|e| { - SqliteError { + Error { code: 0, message: e.to_string(), } @@ -302,7 +302,7 @@ impl<'a> Context<'a> { if T::parameter_has_valid_sqlite_type(arg) { T::parameter_value(arg) } else { - Err(SqliteError { + Err(Error { code: ffi::SQLITE_MISMATCH, message: "Invalid value type".to_string(), }) @@ -477,7 +477,7 @@ mod test { use libc::c_double; use self::regex::Regex; - use {Connection, SqliteError, SqliteResult}; + use {Connection, Error, SqliteResult}; use ffi; use functions::Context; @@ -519,7 +519,7 @@ mod test { None => { let s = try!(ctx.get::(0)); let r = try!(Regex::new(&s).map_err(|e| { - SqliteError { + Error { code: ffi::SQLITE_ERROR, message: format!("Invalid regular expression: {}", e), } @@ -593,7 +593,7 @@ mod test { match entry { Occupied(occ) => occ.into_mut(), Vacant(vac) => { - let r = try!(Regex::new(®ex_s).map_err(|e| SqliteError { + let r = try!(Regex::new(®ex_s).map_err(|e| Error { code: ffi::SQLITE_ERROR, message: format!("Invalid regular expression: {}", e), })); diff --git a/src/lib.rs b/src/lib.rs index 773a333..b718ac6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,7 +89,7 @@ mod named_params; #[cfg(feature = "functions")] pub mod functions; /// A typedef of the result returned by many methods. -pub type SqliteResult = Result; +pub type SqliteResult = Result; unsafe fn errmsg_to_string(errmsg: *const c_char) -> String { let c_slice = CStr::from_ptr(errmsg).to_bytes(); @@ -97,9 +97,12 @@ unsafe fn errmsg_to_string(errmsg: *const c_char) -> String { utf8_str.unwrap_or("Invalid string encoding").to_string() } +/// Old name for `Error`. `SqliteError` is deprecated. +pub type SqliteError = Error; + /// Encompasses an error result from a call to the SQLite C API. #[derive(Debug, PartialEq)] -pub struct SqliteError { +pub struct Error { /// The error code returned by a SQLite C API call. See [SQLite Result /// Codes](http://www.sqlite.org/rescode.html) for details. pub code: c_int, @@ -109,26 +112,26 @@ pub struct SqliteError { pub message: String, } -impl fmt::Display for SqliteError { +impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{} (SQLite error {})", self.message, self.code) } } -impl error::Error for SqliteError { +impl error::Error for Error { fn description(&self) -> &str { &self.message } } -impl SqliteError { - fn from_handle(db: *mut ffi::Struct_sqlite3, code: c_int) -> SqliteError { +impl Error { + fn from_handle(db: *mut ffi::Struct_sqlite3, code: c_int) -> Error { let message = if db.is_null() { ffi::code_to_str(code).to_string() } else { unsafe { errmsg_to_string(ffi::sqlite3_errmsg(db)) } }; - SqliteError { + Error { code: code, message: message, } @@ -137,7 +140,7 @@ impl SqliteError { fn str_to_cstring(s: &str) -> SqliteResult { CString::new(s).map_err(|_| { - SqliteError { + Error { code: ffi::SQLITE_MISUSE, message: format!("Could not convert string {} to C-combatible string", s), } @@ -145,7 +148,7 @@ fn str_to_cstring(s: &str) -> SqliteResult { } fn path_to_cstring(p: &Path) -> SqliteResult { - let s = try!(p.to_str().ok_or(SqliteError { + let s = try!(p.to_str().ok_or(Error { code: ffi::SQLITE_MISUSE, message: format!("Could not convert path {} to UTF-8 string", p.to_string_lossy()), @@ -382,7 +385,7 @@ impl Connection { /// 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`. + /// The `Result` type of `f` must implement `std::convert::From`. /// /// ## Example /// @@ -403,7 +406,7 @@ impl Connection { /// underlying SQLite call fails. pub fn query_row_and_then(&self, sql: &str, params: &[&ToSql], f: F) -> Result where F: FnOnce(SqliteRow) -> Result, - E: convert::From + E: convert::From { let mut stmt = try!(self.prepare(sql)); let mut rows = try!(stmt.query(params)); @@ -589,12 +592,12 @@ impl InnerConnection { let r = ffi::sqlite3_open_v2(c_path.as_ptr(), &mut db, flags.bits(), ptr::null()); if r != ffi::SQLITE_OK { let e = if db.is_null() { - SqliteError { + Error { code: r, message: ffi::code_to_str(r).to_string(), } } else { - let e = SqliteError::from_handle(db, r); + let e = Error::from_handle(db, r); ffi::sqlite3_close(db); e }; @@ -603,7 +606,7 @@ impl InnerConnection { } let r = ffi::sqlite3_busy_timeout(db, 5000); if r != ffi::SQLITE_OK { - let e = SqliteError::from_handle(db, r); + let e = Error::from_handle(db, r); ffi::sqlite3_close(db); return Err(e); } @@ -619,7 +622,7 @@ impl InnerConnection { if code == ffi::SQLITE_OK { Ok(()) } else { - Err(SqliteError::from_handle(self.db(), code)) + Err(Error::from_handle(self.db(), code)) } } @@ -632,7 +635,7 @@ impl InnerConnection { } else { let message = errmsg_to_string(&*errmsg); ffi::sqlite3_free(errmsg as *mut c_void); - Err(SqliteError { + Err(Error { code: code, message: message, }) @@ -693,7 +696,7 @@ impl InnerConnection { sql: &str) -> SqliteResult> { if sql.len() >= ::std::i32::MAX as usize { - return Err(SqliteError { + return Err(Error { code: ffi::SQLITE_TOOBIG, message: "statement too long".to_string(), }); @@ -789,7 +792,7 @@ impl<'conn> SqliteStatement<'conn> { match r { ffi::SQLITE_DONE => { if self.column_count != 0 { - Err(SqliteError { + Err(Error { code: ffi::SQLITE_MISUSE, message: "Unexpected column count - did you mean to call query?" .to_string(), @@ -799,7 +802,7 @@ impl<'conn> SqliteStatement<'conn> { } } ffi::SQLITE_ROW => { - Err(SqliteError { + Err(Error { code: r, message: "Unexpected row result - did you mean to call query?".to_string(), }) @@ -867,7 +870,7 @@ 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). + /// `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. @@ -879,7 +882,7 @@ impl<'conn> SqliteStatement<'conn> { params: &[&ToSql], f: F) -> SqliteResult> - where E: convert::From, + where E: convert::From, F: FnMut(&SqliteRow) -> Result { let row_iter = try!(self.query(params)); @@ -968,14 +971,14 @@ impl<'stmt, T, F> Iterator for MappedRows<'stmt, F> where F: FnMut(&SqliteRow) - } /// An iterator over the mapped resulting rows of a query, with an Error type -/// unifying with SqliteError. +/// unifying with Error. pub struct AndThenRows<'stmt, F> { rows: SqliteRows<'stmt>, map: F, } impl<'stmt, T, E, F> Iterator for AndThenRows<'stmt, F> -where E: convert::From, +where E: convert::From, F: FnMut(&SqliteRow) -> Result { type Item = Result; @@ -1042,7 +1045,7 @@ impl<'stmt> SqliteRows<'stmt> { match self.next() { Some(row) => row, None => { - Err(SqliteError { + Err(Error { code: ffi::SQLITE_NOTICE, message: "Query did not return a row".to_string(), }) @@ -1121,21 +1124,21 @@ impl<'stmt> SqliteRow<'stmt> { /// /// ## Failure /// - /// Returns a `SQLITE_MISMATCH`-coded `SqliteError` if the underlying SQLite column + /// Returns a `SQLITE_MISMATCH`-coded `Error` if the underlying SQLite column /// type is not a valid type as a source for `T`. /// - /// Returns a `SQLITE_MISUSE`-coded `SqliteError` if `idx` is outside the valid column range + /// Returns a `SQLITE_MISUSE`-coded `Error` 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 { + return Err(Error { code: ffi::SQLITE_MISUSE, message: "Cannot get values from a row after advancing to next row".to_string(), }); } unsafe { if idx < 0 || idx >= self.stmt.column_count { - return Err(SqliteError { + return Err(Error { code: ffi::SQLITE_MISUSE, message: "Invalid column index".to_string(), }); @@ -1144,7 +1147,7 @@ impl<'stmt> SqliteRow<'stmt> { if T::column_has_valid_sqlite_type(self.stmt.stmt, idx) { FromSql::column_result(self.stmt.stmt, idx) } else { - Err(SqliteError { + Err(Error { code: ffi::SQLITE_MISMATCH, message: "Invalid column type".to_string(), }) @@ -1418,7 +1421,7 @@ mod test { #[derive(Debug, PartialEq)] enum CustomError { SomeError, - Sqlite(SqliteError), + Sqlite(Error), } impl fmt::Display for CustomError { @@ -1442,8 +1445,8 @@ mod test { } } - impl From for CustomError { - fn from(se: SqliteError) -> CustomError { + impl From for CustomError { + fn from(se: Error) -> CustomError { CustomError::Sqlite(se) } } @@ -1492,7 +1495,7 @@ mod test { .collect(); assert_eq!(bad_type, - Err(SqliteError { + Err(Error { code: ffi::SQLITE_MISMATCH, message: "Invalid column type".to_owned(), })); @@ -1503,7 +1506,7 @@ mod test { .collect(); assert_eq!(bad_idx, - Err(SqliteError { + Err(Error { code: ffi::SQLITE_MISUSE, message: "Invalid column index".to_owned(), })); @@ -1555,7 +1558,7 @@ mod test { .collect(); assert_eq!(bad_type, - Err(CustomError::Sqlite(SqliteError { + Err(CustomError::Sqlite(Error { code: ffi::SQLITE_MISMATCH, message: "Invalid column type".to_owned(), }))); @@ -1568,7 +1571,7 @@ mod test { .collect(); assert_eq!(bad_idx, - Err(CustomError::Sqlite(SqliteError { + Err(CustomError::Sqlite(Error { code: ffi::SQLITE_MISUSE, message: "Invalid column index".to_owned(), }))); @@ -1616,7 +1619,7 @@ mod test { }); assert_eq!(bad_type, - Err(CustomError::Sqlite(SqliteError { + Err(CustomError::Sqlite(Error { code: ffi::SQLITE_MISMATCH, message: "Invalid column type".to_owned(), }))); @@ -1626,7 +1629,7 @@ mod test { }); assert_eq!(bad_idx, - Err(CustomError::Sqlite(SqliteError { + Err(CustomError::Sqlite(Error { code: ffi::SQLITE_MISUSE, message: "Invalid column index".to_owned(), }))); diff --git a/src/named_params.rs b/src/named_params.rs index 60e99d8..0247c7f 100644 --- a/src/named_params.rs +++ b/src/named_params.rs @@ -2,7 +2,7 @@ use libc::c_int; use super::ffi; -use {SqliteResult, SqliteError, Connection, SqliteStatement, SqliteRows, SqliteRow, +use {SqliteResult, Error, Connection, SqliteStatement, SqliteRows, SqliteRow, str_to_cstring}; use types::ToSql; @@ -134,7 +134,7 @@ impl<'conn> SqliteStatement<'conn> { if let Some(i) = try!(self.parameter_index(name)) { try!(self.conn.decode_result(unsafe { value.bind_parameter(self.stmt, i) })); } else { - return Err(SqliteError { + return Err(Error { code: ffi::SQLITE_MISUSE, message: format!("Invalid parameter name: {}", name), }); diff --git a/src/trace.rs b/src/trace.rs index f79f619..9894d3f 100644 --- a/src/trace.rs +++ b/src/trace.rs @@ -8,7 +8,7 @@ use std::str; use std::time::Duration; use super::ffi; -use {SqliteError, SqliteResult, Connection}; +use {Error, SqliteResult, Connection}; /// Set up the process-wide SQLite error logging callback. /// This function is marked unsafe for two reasons: @@ -43,7 +43,7 @@ pub unsafe fn config_log(callback: Option) -> SqliteResult<()> }; if rc != ffi::SQLITE_OK { - return Err(SqliteError { + return Err(Error { code: rc, message: "sqlite3_config(SQLITE_CONFIG_LOG, ...)".to_string(), }); diff --git a/src/types.rs b/src/types.rs index bf5f86c..274e990 100644 --- a/src/types.rs +++ b/src/types.rs @@ -59,7 +59,7 @@ use std::ffi::CStr; use std::mem; use std::str; use super::ffi; -use super::{SqliteResult, SqliteError, str_to_cstring}; +use super::{SqliteResult, Error, str_to_cstring}; pub use ffi::sqlite3_stmt; pub use ffi::sqlite3_column_type; @@ -235,7 +235,7 @@ impl FromSql for String { let utf8_str = str::from_utf8(c_slice); utf8_str.map(|s| s.to_string()) .map_err(|e| { - SqliteError { + Error { code: 0, message: e.to_string(), } @@ -275,7 +275,7 @@ impl FromSql for time::Timespec { time::strptime(&txt, SQLITE_DATETIME_FMT) .map(|tm| tm.to_timespec()) .map_err(|parse_error| { - SqliteError { + Error { code: ffi::SQLITE_MISMATCH, message: format!("{}", parse_error), }