mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-27 03:51:38 +08:00
Rename SqliteError -> Error.
This commit is contained in:
parent
4327a84edb
commit
f0b6bf9152
@ -1,6 +1,7 @@
|
|||||||
# Version UPCOMING (TBD)
|
# Version UPCOMING (TBD)
|
||||||
|
|
||||||
* Renamed `SqliteConnection` to `Connection`. The old name remains as a typealias.
|
* 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 a variety of `..._named` methods for executing queries using named placeholder parameters.
|
||||||
* Adds `backup` feature that exposes SQLite's online backup API.
|
* Adds `backup` feature that exposes SQLite's online backup API.
|
||||||
* Adds `functions` feature that allows user-defined scalar functions to be added to
|
* Adds `functions` feature that allows user-defined scalar functions to be added to
|
||||||
|
@ -36,7 +36,7 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use ffi;
|
use ffi;
|
||||||
|
|
||||||
use {DatabaseName, Connection, SqliteError, SqliteResult};
|
use {DatabaseName, Connection, Error, SqliteResult};
|
||||||
|
|
||||||
impl Connection {
|
impl Connection {
|
||||||
/// Back up the `name` database to the given destination path.
|
/// Back up the `name` database to the given destination path.
|
||||||
@ -70,8 +70,8 @@ impl Connection {
|
|||||||
|
|
||||||
match r {
|
match r {
|
||||||
Done => Ok(()),
|
Done => Ok(()),
|
||||||
Busy => Err(SqliteError::from_handle(ptr::null_mut(), ffi::SQLITE_BUSY)),
|
Busy => Err(Error::from_handle(ptr::null_mut(), ffi::SQLITE_BUSY)),
|
||||||
Locked => Err(SqliteError::from_handle(ptr::null_mut(), ffi::SQLITE_LOCKED)),
|
Locked => Err(Error::from_handle(ptr::null_mut(), ffi::SQLITE_LOCKED)),
|
||||||
More => unreachable!(),
|
More => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -115,8 +115,8 @@ impl Connection {
|
|||||||
|
|
||||||
match r {
|
match r {
|
||||||
Done => Ok(()),
|
Done => Ok(()),
|
||||||
Busy => Err(SqliteError::from_handle(ptr::null_mut(), ffi::SQLITE_BUSY)),
|
Busy => Err(Error::from_handle(ptr::null_mut(), ffi::SQLITE_BUSY)),
|
||||||
Locked => Err(SqliteError::from_handle(ptr::null_mut(), ffi::SQLITE_LOCKED)),
|
Locked => Err(Error::from_handle(ptr::null_mut(), ffi::SQLITE_LOCKED)),
|
||||||
More => unreachable!(),
|
More => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -201,7 +201,7 @@ impl<'a, 'b> Backup<'a, 'b> {
|
|||||||
from.db.borrow_mut().db,
|
from.db.borrow_mut().db,
|
||||||
from_name.as_ptr());
|
from_name.as_ptr());
|
||||||
if b.is_null() {
|
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
|
b
|
||||||
};
|
};
|
||||||
@ -245,7 +245,7 @@ impl<'a, 'b> Backup<'a, 'b> {
|
|||||||
ffi::SQLITE_BUSY => Ok(Busy),
|
ffi::SQLITE_BUSY => Ok(Busy),
|
||||||
ffi::SQLITE_LOCKED => Ok(Locked),
|
ffi::SQLITE_LOCKED => Ok(Locked),
|
||||||
rc => {
|
rc => {
|
||||||
Err(SqliteError {
|
Err(Error {
|
||||||
code: rc,
|
code: rc,
|
||||||
message: ffi::code_to_str(rc).into(),
|
message: ffi::code_to_str(rc).into(),
|
||||||
})
|
})
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
//! extern crate rusqlite;
|
//! extern crate rusqlite;
|
||||||
//! extern crate regex;
|
//! extern crate regex;
|
||||||
//!
|
//!
|
||||||
//! use rusqlite::{Connection, SqliteError, SqliteResult};
|
//! use rusqlite::{Connection, Error, SqliteResult};
|
||||||
//! use std::collections::HashMap;
|
//! use std::collections::HashMap;
|
||||||
//! use regex::Regex;
|
//! use regex::Regex;
|
||||||
//!
|
//!
|
||||||
@ -26,7 +26,7 @@
|
|||||||
//! match entry {
|
//! match entry {
|
||||||
//! Occupied(occ) => occ.into_mut(),
|
//! Occupied(occ) => occ.into_mut(),
|
||||||
//! Vacant(vac) => {
|
//! 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,
|
//! code: libsqlite3_sys::SQLITE_ERROR,
|
||||||
//! message: format!("Invalid regular expression: {}", e),
|
//! message: format!("Invalid regular expression: {}", e),
|
||||||
//! }));
|
//! }));
|
||||||
@ -65,7 +65,7 @@ pub use ffi::sqlite3_value_numeric_type;
|
|||||||
|
|
||||||
use types::Null;
|
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.
|
/// A trait for types that can be converted into the result of an SQL function.
|
||||||
pub trait ToResult {
|
pub trait ToResult {
|
||||||
@ -228,7 +228,7 @@ impl FromValue for String {
|
|||||||
let utf8_str = str::from_utf8(c_slice);
|
let utf8_str = str::from_utf8(c_slice);
|
||||||
utf8_str.map(|s| s.to_string())
|
utf8_str.map(|s| s.to_string())
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
SqliteError {
|
Error {
|
||||||
code: 0,
|
code: 0,
|
||||||
message: e.to_string(),
|
message: e.to_string(),
|
||||||
}
|
}
|
||||||
@ -302,7 +302,7 @@ impl<'a> Context<'a> {
|
|||||||
if T::parameter_has_valid_sqlite_type(arg) {
|
if T::parameter_has_valid_sqlite_type(arg) {
|
||||||
T::parameter_value(arg)
|
T::parameter_value(arg)
|
||||||
} else {
|
} else {
|
||||||
Err(SqliteError {
|
Err(Error {
|
||||||
code: ffi::SQLITE_MISMATCH,
|
code: ffi::SQLITE_MISMATCH,
|
||||||
message: "Invalid value type".to_string(),
|
message: "Invalid value type".to_string(),
|
||||||
})
|
})
|
||||||
@ -477,7 +477,7 @@ mod test {
|
|||||||
use libc::c_double;
|
use libc::c_double;
|
||||||
use self::regex::Regex;
|
use self::regex::Regex;
|
||||||
|
|
||||||
use {Connection, SqliteError, SqliteResult};
|
use {Connection, Error, SqliteResult};
|
||||||
use ffi;
|
use ffi;
|
||||||
use functions::Context;
|
use functions::Context;
|
||||||
|
|
||||||
@ -519,7 +519,7 @@ mod test {
|
|||||||
None => {
|
None => {
|
||||||
let s = try!(ctx.get::<String>(0));
|
let s = try!(ctx.get::<String>(0));
|
||||||
let r = try!(Regex::new(&s).map_err(|e| {
|
let r = try!(Regex::new(&s).map_err(|e| {
|
||||||
SqliteError {
|
Error {
|
||||||
code: ffi::SQLITE_ERROR,
|
code: ffi::SQLITE_ERROR,
|
||||||
message: format!("Invalid regular expression: {}", e),
|
message: format!("Invalid regular expression: {}", e),
|
||||||
}
|
}
|
||||||
@ -593,7 +593,7 @@ mod test {
|
|||||||
match entry {
|
match entry {
|
||||||
Occupied(occ) => occ.into_mut(),
|
Occupied(occ) => occ.into_mut(),
|
||||||
Vacant(vac) => {
|
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,
|
code: ffi::SQLITE_ERROR,
|
||||||
message: format!("Invalid regular expression: {}", e),
|
message: format!("Invalid regular expression: {}", e),
|
||||||
}));
|
}));
|
||||||
|
79
src/lib.rs
79
src/lib.rs
@ -89,7 +89,7 @@ mod named_params;
|
|||||||
#[cfg(feature = "functions")] pub mod functions;
|
#[cfg(feature = "functions")] pub mod functions;
|
||||||
|
|
||||||
/// A typedef of the result returned by many methods.
|
/// A typedef of the result returned by many methods.
|
||||||
pub type SqliteResult<T> = Result<T, SqliteError>;
|
pub type SqliteResult<T> = Result<T, Error>;
|
||||||
|
|
||||||
unsafe fn errmsg_to_string(errmsg: *const c_char) -> String {
|
unsafe fn errmsg_to_string(errmsg: *const c_char) -> String {
|
||||||
let c_slice = CStr::from_ptr(errmsg).to_bytes();
|
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()
|
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.
|
/// Encompasses an error result from a call to the SQLite C API.
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct SqliteError {
|
pub struct Error {
|
||||||
/// The error code returned by a SQLite C API call. See [SQLite Result
|
/// The error code returned by a SQLite C API call. See [SQLite Result
|
||||||
/// Codes](http://www.sqlite.org/rescode.html) for details.
|
/// Codes](http://www.sqlite.org/rescode.html) for details.
|
||||||
pub code: c_int,
|
pub code: c_int,
|
||||||
@ -109,26 +112,26 @@ pub struct SqliteError {
|
|||||||
pub message: String,
|
pub message: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for SqliteError {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{} (SQLite error {})", self.message, self.code)
|
write!(f, "{} (SQLite error {})", self.message, self.code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl error::Error for SqliteError {
|
impl error::Error for Error {
|
||||||
fn description(&self) -> &str {
|
fn description(&self) -> &str {
|
||||||
&self.message
|
&self.message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SqliteError {
|
impl Error {
|
||||||
fn from_handle(db: *mut ffi::Struct_sqlite3, code: c_int) -> SqliteError {
|
fn from_handle(db: *mut ffi::Struct_sqlite3, code: c_int) -> Error {
|
||||||
let message = if db.is_null() {
|
let message = if db.is_null() {
|
||||||
ffi::code_to_str(code).to_string()
|
ffi::code_to_str(code).to_string()
|
||||||
} else {
|
} else {
|
||||||
unsafe { errmsg_to_string(ffi::sqlite3_errmsg(db)) }
|
unsafe { errmsg_to_string(ffi::sqlite3_errmsg(db)) }
|
||||||
};
|
};
|
||||||
SqliteError {
|
Error {
|
||||||
code: code,
|
code: code,
|
||||||
message: message,
|
message: message,
|
||||||
}
|
}
|
||||||
@ -137,7 +140,7 @@ impl SqliteError {
|
|||||||
|
|
||||||
fn str_to_cstring(s: &str) -> SqliteResult<CString> {
|
fn str_to_cstring(s: &str) -> SqliteResult<CString> {
|
||||||
CString::new(s).map_err(|_| {
|
CString::new(s).map_err(|_| {
|
||||||
SqliteError {
|
Error {
|
||||||
code: ffi::SQLITE_MISUSE,
|
code: ffi::SQLITE_MISUSE,
|
||||||
message: format!("Could not convert string {} to C-combatible string", s),
|
message: format!("Could not convert string {} to C-combatible string", s),
|
||||||
}
|
}
|
||||||
@ -145,7 +148,7 @@ fn str_to_cstring(s: &str) -> SqliteResult<CString> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn path_to_cstring(p: &Path) -> SqliteResult<CString> {
|
fn path_to_cstring(p: &Path) -> SqliteResult<CString> {
|
||||||
let s = try!(p.to_str().ok_or(SqliteError {
|
let s = try!(p.to_str().ok_or(Error {
|
||||||
code: ffi::SQLITE_MISUSE,
|
code: ffi::SQLITE_MISUSE,
|
||||||
message: format!("Could not convert path {} to UTF-8 string",
|
message: format!("Could not convert path {} to UTF-8 string",
|
||||||
p.to_string_lossy()),
|
p.to_string_lossy()),
|
||||||
@ -382,7 +385,7 @@ impl Connection {
|
|||||||
|
|
||||||
/// Convenience method to execute a query that is expected to return a single row,
|
/// 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.
|
/// 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<SqliteError>`.
|
/// The `Result` type of `f` must implement `std::convert::From<Error>`.
|
||||||
///
|
///
|
||||||
/// ## Example
|
/// ## Example
|
||||||
///
|
///
|
||||||
@ -403,7 +406,7 @@ impl Connection {
|
|||||||
/// underlying SQLite call fails.
|
/// underlying SQLite call fails.
|
||||||
pub fn query_row_and_then<T, E, F>(&self, sql: &str, params: &[&ToSql], f: F) -> Result<T, E>
|
pub fn query_row_and_then<T, E, F>(&self, sql: &str, params: &[&ToSql], f: F) -> Result<T, E>
|
||||||
where F: FnOnce(SqliteRow) -> Result<T, E>,
|
where F: FnOnce(SqliteRow) -> Result<T, E>,
|
||||||
E: convert::From<SqliteError>
|
E: convert::From<Error>
|
||||||
{
|
{
|
||||||
let mut stmt = try!(self.prepare(sql));
|
let mut stmt = try!(self.prepare(sql));
|
||||||
let mut rows = try!(stmt.query(params));
|
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());
|
let r = ffi::sqlite3_open_v2(c_path.as_ptr(), &mut db, flags.bits(), ptr::null());
|
||||||
if r != ffi::SQLITE_OK {
|
if r != ffi::SQLITE_OK {
|
||||||
let e = if db.is_null() {
|
let e = if db.is_null() {
|
||||||
SqliteError {
|
Error {
|
||||||
code: r,
|
code: r,
|
||||||
message: ffi::code_to_str(r).to_string(),
|
message: ffi::code_to_str(r).to_string(),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let e = SqliteError::from_handle(db, r);
|
let e = Error::from_handle(db, r);
|
||||||
ffi::sqlite3_close(db);
|
ffi::sqlite3_close(db);
|
||||||
e
|
e
|
||||||
};
|
};
|
||||||
@ -603,7 +606,7 @@ impl InnerConnection {
|
|||||||
}
|
}
|
||||||
let r = ffi::sqlite3_busy_timeout(db, 5000);
|
let r = ffi::sqlite3_busy_timeout(db, 5000);
|
||||||
if r != ffi::SQLITE_OK {
|
if r != ffi::SQLITE_OK {
|
||||||
let e = SqliteError::from_handle(db, r);
|
let e = Error::from_handle(db, r);
|
||||||
ffi::sqlite3_close(db);
|
ffi::sqlite3_close(db);
|
||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
@ -619,7 +622,7 @@ impl InnerConnection {
|
|||||||
if code == ffi::SQLITE_OK {
|
if code == ffi::SQLITE_OK {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(SqliteError::from_handle(self.db(), code))
|
Err(Error::from_handle(self.db(), code))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -632,7 +635,7 @@ impl InnerConnection {
|
|||||||
} else {
|
} else {
|
||||||
let message = errmsg_to_string(&*errmsg);
|
let message = errmsg_to_string(&*errmsg);
|
||||||
ffi::sqlite3_free(errmsg as *mut c_void);
|
ffi::sqlite3_free(errmsg as *mut c_void);
|
||||||
Err(SqliteError {
|
Err(Error {
|
||||||
code: code,
|
code: code,
|
||||||
message: message,
|
message: message,
|
||||||
})
|
})
|
||||||
@ -693,7 +696,7 @@ impl InnerConnection {
|
|||||||
sql: &str)
|
sql: &str)
|
||||||
-> SqliteResult<SqliteStatement<'a>> {
|
-> SqliteResult<SqliteStatement<'a>> {
|
||||||
if sql.len() >= ::std::i32::MAX as usize {
|
if sql.len() >= ::std::i32::MAX as usize {
|
||||||
return Err(SqliteError {
|
return Err(Error {
|
||||||
code: ffi::SQLITE_TOOBIG,
|
code: ffi::SQLITE_TOOBIG,
|
||||||
message: "statement too long".to_string(),
|
message: "statement too long".to_string(),
|
||||||
});
|
});
|
||||||
@ -789,7 +792,7 @@ impl<'conn> SqliteStatement<'conn> {
|
|||||||
match r {
|
match r {
|
||||||
ffi::SQLITE_DONE => {
|
ffi::SQLITE_DONE => {
|
||||||
if self.column_count != 0 {
|
if self.column_count != 0 {
|
||||||
Err(SqliteError {
|
Err(Error {
|
||||||
code: ffi::SQLITE_MISUSE,
|
code: ffi::SQLITE_MISUSE,
|
||||||
message: "Unexpected column count - did you mean to call query?"
|
message: "Unexpected column count - did you mean to call query?"
|
||||||
.to_string(),
|
.to_string(),
|
||||||
@ -799,7 +802,7 @@ impl<'conn> SqliteStatement<'conn> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ffi::SQLITE_ROW => {
|
ffi::SQLITE_ROW => {
|
||||||
Err(SqliteError {
|
Err(Error {
|
||||||
code: r,
|
code: r,
|
||||||
message: "Unexpected row result - did you mean to call query?".to_string(),
|
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
|
/// Executes the prepared statement and maps a function over the resulting
|
||||||
/// rows, where the function returns a `Result` with `Error` type implementing
|
/// rows, where the function returns a `Result` with `Error` type implementing
|
||||||
/// `std::convert::From<SqliteError>` (so errors can be unified).
|
/// `std::convert::From<Error>` (so errors can be unified).
|
||||||
///
|
///
|
||||||
/// Unlike the iterator produced by `query`, the returned iterator does not expose the possibility
|
/// Unlike the iterator produced by `query`, the returned iterator does not expose the possibility
|
||||||
/// for accessing stale rows.
|
/// for accessing stale rows.
|
||||||
@ -879,7 +882,7 @@ impl<'conn> SqliteStatement<'conn> {
|
|||||||
params: &[&ToSql],
|
params: &[&ToSql],
|
||||||
f: F)
|
f: F)
|
||||||
-> SqliteResult<AndThenRows<'a, F>>
|
-> SqliteResult<AndThenRows<'a, F>>
|
||||||
where E: convert::From<SqliteError>,
|
where E: convert::From<Error>,
|
||||||
F: FnMut(&SqliteRow) -> Result<T, E>
|
F: FnMut(&SqliteRow) -> Result<T, E>
|
||||||
{
|
{
|
||||||
let row_iter = try!(self.query(params));
|
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
|
/// 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> {
|
pub struct AndThenRows<'stmt, F> {
|
||||||
rows: SqliteRows<'stmt>,
|
rows: SqliteRows<'stmt>,
|
||||||
map: F,
|
map: F,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'stmt, T, E, F> Iterator for AndThenRows<'stmt, F>
|
impl<'stmt, T, E, F> Iterator for AndThenRows<'stmt, F>
|
||||||
where E: convert::From<SqliteError>,
|
where E: convert::From<Error>,
|
||||||
F: FnMut(&SqliteRow) -> Result<T, E>
|
F: FnMut(&SqliteRow) -> Result<T, E>
|
||||||
{
|
{
|
||||||
type Item = Result<T, E>;
|
type Item = Result<T, E>;
|
||||||
@ -1042,7 +1045,7 @@ impl<'stmt> SqliteRows<'stmt> {
|
|||||||
match self.next() {
|
match self.next() {
|
||||||
Some(row) => row,
|
Some(row) => row,
|
||||||
None => {
|
None => {
|
||||||
Err(SqliteError {
|
Err(Error {
|
||||||
code: ffi::SQLITE_NOTICE,
|
code: ffi::SQLITE_NOTICE,
|
||||||
message: "Query did not return a row".to_string(),
|
message: "Query did not return a row".to_string(),
|
||||||
})
|
})
|
||||||
@ -1121,21 +1124,21 @@ impl<'stmt> SqliteRow<'stmt> {
|
|||||||
///
|
///
|
||||||
/// ## Failure
|
/// ## 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`.
|
/// 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.
|
/// for this row or if this row is stale.
|
||||||
pub fn get_checked<T: FromSql>(&self, idx: c_int) -> SqliteResult<T> {
|
pub fn get_checked<T: FromSql>(&self, idx: c_int) -> SqliteResult<T> {
|
||||||
if self.row_idx != self.current_row.get() {
|
if self.row_idx != self.current_row.get() {
|
||||||
return Err(SqliteError {
|
return Err(Error {
|
||||||
code: ffi::SQLITE_MISUSE,
|
code: ffi::SQLITE_MISUSE,
|
||||||
message: "Cannot get values from a row after advancing to next row".to_string(),
|
message: "Cannot get values from a row after advancing to next row".to_string(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
unsafe {
|
unsafe {
|
||||||
if idx < 0 || idx >= self.stmt.column_count {
|
if idx < 0 || idx >= self.stmt.column_count {
|
||||||
return Err(SqliteError {
|
return Err(Error {
|
||||||
code: ffi::SQLITE_MISUSE,
|
code: ffi::SQLITE_MISUSE,
|
||||||
message: "Invalid column index".to_string(),
|
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) {
|
if T::column_has_valid_sqlite_type(self.stmt.stmt, idx) {
|
||||||
FromSql::column_result(self.stmt.stmt, idx)
|
FromSql::column_result(self.stmt.stmt, idx)
|
||||||
} else {
|
} else {
|
||||||
Err(SqliteError {
|
Err(Error {
|
||||||
code: ffi::SQLITE_MISMATCH,
|
code: ffi::SQLITE_MISMATCH,
|
||||||
message: "Invalid column type".to_string(),
|
message: "Invalid column type".to_string(),
|
||||||
})
|
})
|
||||||
@ -1418,7 +1421,7 @@ mod test {
|
|||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
enum CustomError {
|
enum CustomError {
|
||||||
SomeError,
|
SomeError,
|
||||||
Sqlite(SqliteError),
|
Sqlite(Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for CustomError {
|
impl fmt::Display for CustomError {
|
||||||
@ -1442,8 +1445,8 @@ mod test {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<SqliteError> for CustomError {
|
impl From<Error> for CustomError {
|
||||||
fn from(se: SqliteError) -> CustomError {
|
fn from(se: Error) -> CustomError {
|
||||||
CustomError::Sqlite(se)
|
CustomError::Sqlite(se)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1492,7 +1495,7 @@ mod test {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
assert_eq!(bad_type,
|
assert_eq!(bad_type,
|
||||||
Err(SqliteError {
|
Err(Error {
|
||||||
code: ffi::SQLITE_MISMATCH,
|
code: ffi::SQLITE_MISMATCH,
|
||||||
message: "Invalid column type".to_owned(),
|
message: "Invalid column type".to_owned(),
|
||||||
}));
|
}));
|
||||||
@ -1503,7 +1506,7 @@ mod test {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
assert_eq!(bad_idx,
|
assert_eq!(bad_idx,
|
||||||
Err(SqliteError {
|
Err(Error {
|
||||||
code: ffi::SQLITE_MISUSE,
|
code: ffi::SQLITE_MISUSE,
|
||||||
message: "Invalid column index".to_owned(),
|
message: "Invalid column index".to_owned(),
|
||||||
}));
|
}));
|
||||||
@ -1555,7 +1558,7 @@ mod test {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
assert_eq!(bad_type,
|
assert_eq!(bad_type,
|
||||||
Err(CustomError::Sqlite(SqliteError {
|
Err(CustomError::Sqlite(Error {
|
||||||
code: ffi::SQLITE_MISMATCH,
|
code: ffi::SQLITE_MISMATCH,
|
||||||
message: "Invalid column type".to_owned(),
|
message: "Invalid column type".to_owned(),
|
||||||
})));
|
})));
|
||||||
@ -1568,7 +1571,7 @@ mod test {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
assert_eq!(bad_idx,
|
assert_eq!(bad_idx,
|
||||||
Err(CustomError::Sqlite(SqliteError {
|
Err(CustomError::Sqlite(Error {
|
||||||
code: ffi::SQLITE_MISUSE,
|
code: ffi::SQLITE_MISUSE,
|
||||||
message: "Invalid column index".to_owned(),
|
message: "Invalid column index".to_owned(),
|
||||||
})));
|
})));
|
||||||
@ -1616,7 +1619,7 @@ mod test {
|
|||||||
});
|
});
|
||||||
|
|
||||||
assert_eq!(bad_type,
|
assert_eq!(bad_type,
|
||||||
Err(CustomError::Sqlite(SqliteError {
|
Err(CustomError::Sqlite(Error {
|
||||||
code: ffi::SQLITE_MISMATCH,
|
code: ffi::SQLITE_MISMATCH,
|
||||||
message: "Invalid column type".to_owned(),
|
message: "Invalid column type".to_owned(),
|
||||||
})));
|
})));
|
||||||
@ -1626,7 +1629,7 @@ mod test {
|
|||||||
});
|
});
|
||||||
|
|
||||||
assert_eq!(bad_idx,
|
assert_eq!(bad_idx,
|
||||||
Err(CustomError::Sqlite(SqliteError {
|
Err(CustomError::Sqlite(Error {
|
||||||
code: ffi::SQLITE_MISUSE,
|
code: ffi::SQLITE_MISUSE,
|
||||||
message: "Invalid column index".to_owned(),
|
message: "Invalid column index".to_owned(),
|
||||||
})));
|
})));
|
||||||
|
@ -2,7 +2,7 @@ use libc::c_int;
|
|||||||
|
|
||||||
use super::ffi;
|
use super::ffi;
|
||||||
|
|
||||||
use {SqliteResult, SqliteError, Connection, SqliteStatement, SqliteRows, SqliteRow,
|
use {SqliteResult, Error, Connection, SqliteStatement, SqliteRows, SqliteRow,
|
||||||
str_to_cstring};
|
str_to_cstring};
|
||||||
use types::ToSql;
|
use types::ToSql;
|
||||||
|
|
||||||
@ -134,7 +134,7 @@ impl<'conn> SqliteStatement<'conn> {
|
|||||||
if let Some(i) = try!(self.parameter_index(name)) {
|
if let Some(i) = try!(self.parameter_index(name)) {
|
||||||
try!(self.conn.decode_result(unsafe { value.bind_parameter(self.stmt, i) }));
|
try!(self.conn.decode_result(unsafe { value.bind_parameter(self.stmt, i) }));
|
||||||
} else {
|
} else {
|
||||||
return Err(SqliteError {
|
return Err(Error {
|
||||||
code: ffi::SQLITE_MISUSE,
|
code: ffi::SQLITE_MISUSE,
|
||||||
message: format!("Invalid parameter name: {}", name),
|
message: format!("Invalid parameter name: {}", name),
|
||||||
});
|
});
|
||||||
|
@ -8,7 +8,7 @@ use std::str;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use super::ffi;
|
use super::ffi;
|
||||||
use {SqliteError, SqliteResult, Connection};
|
use {Error, SqliteResult, Connection};
|
||||||
|
|
||||||
/// Set up the process-wide SQLite error logging callback.
|
/// Set up the process-wide SQLite error logging callback.
|
||||||
/// This function is marked unsafe for two reasons:
|
/// This function is marked unsafe for two reasons:
|
||||||
@ -43,7 +43,7 @@ pub unsafe fn config_log(callback: Option<fn(c_int, &str)>) -> SqliteResult<()>
|
|||||||
};
|
};
|
||||||
|
|
||||||
if rc != ffi::SQLITE_OK {
|
if rc != ffi::SQLITE_OK {
|
||||||
return Err(SqliteError {
|
return Err(Error {
|
||||||
code: rc,
|
code: rc,
|
||||||
message: "sqlite3_config(SQLITE_CONFIG_LOG, ...)".to_string(),
|
message: "sqlite3_config(SQLITE_CONFIG_LOG, ...)".to_string(),
|
||||||
});
|
});
|
||||||
|
@ -59,7 +59,7 @@ use std::ffi::CStr;
|
|||||||
use std::mem;
|
use std::mem;
|
||||||
use std::str;
|
use std::str;
|
||||||
use super::ffi;
|
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_stmt;
|
||||||
pub use ffi::sqlite3_column_type;
|
pub use ffi::sqlite3_column_type;
|
||||||
@ -235,7 +235,7 @@ impl FromSql for String {
|
|||||||
let utf8_str = str::from_utf8(c_slice);
|
let utf8_str = str::from_utf8(c_slice);
|
||||||
utf8_str.map(|s| s.to_string())
|
utf8_str.map(|s| s.to_string())
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
SqliteError {
|
Error {
|
||||||
code: 0,
|
code: 0,
|
||||||
message: e.to_string(),
|
message: e.to_string(),
|
||||||
}
|
}
|
||||||
@ -275,7 +275,7 @@ impl FromSql for time::Timespec {
|
|||||||
time::strptime(&txt, SQLITE_DATETIME_FMT)
|
time::strptime(&txt, SQLITE_DATETIME_FMT)
|
||||||
.map(|tm| tm.to_timespec())
|
.map(|tm| tm.to_timespec())
|
||||||
.map_err(|parse_error| {
|
.map_err(|parse_error| {
|
||||||
SqliteError {
|
Error {
|
||||||
code: ffi::SQLITE_MISMATCH,
|
code: ffi::SQLITE_MISMATCH,
|
||||||
message: format!("{}", parse_error),
|
message: format!("{}", parse_error),
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user