mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-22 16:29:20 +08:00
parent
7619f638a8
commit
747a8d36ed
40
src/error.rs
40
src/error.rs
@ -96,6 +96,46 @@ pub enum Error {
|
||||
UnwindingPanic,
|
||||
}
|
||||
|
||||
impl PartialEq for Error {
|
||||
fn eq(&self, other: &Error) -> bool {
|
||||
match (self, other) {
|
||||
(Error::SqliteFailure(e1, s1), Error::SqliteFailure(e2, s2)) => e1 == e2 && s1 == s2,
|
||||
(Error::SqliteSingleThreadedMode, Error::SqliteSingleThreadedMode) => true,
|
||||
(Error::IntegralValueOutOfRange(i1, n1), Error::IntegralValueOutOfRange(i2, n2)) => {
|
||||
i1 == i2 && n1 == n2
|
||||
}
|
||||
(Error::Utf8Error(e1), Error::Utf8Error(e2)) => e1 == e2,
|
||||
(Error::NulError(e1), Error::NulError(e2)) => e1 == e2,
|
||||
(Error::InvalidParameterName(n1), Error::InvalidParameterName(n2)) => n1 == n2,
|
||||
(Error::InvalidPath(p1), Error::InvalidPath(p2)) => p1 == p2,
|
||||
(Error::ExecuteReturnedResults, Error::ExecuteReturnedResults) => true,
|
||||
(Error::QueryReturnedNoRows, Error::QueryReturnedNoRows) => true,
|
||||
(Error::InvalidColumnIndex(i1), Error::InvalidColumnIndex(i2)) => i1 == i2,
|
||||
(Error::InvalidColumnName(n1), Error::InvalidColumnName(n2)) => n1 == n2,
|
||||
(Error::InvalidColumnType(i1, t1), Error::InvalidColumnType(i2, t2)) => {
|
||||
i1 == i2 && t1 == t2
|
||||
}
|
||||
(Error::StatementChangedRows(n1), Error::StatementChangedRows(n2)) => n1 == n2,
|
||||
#[cfg(feature = "functions")]
|
||||
(
|
||||
Error::InvalidFunctionParameterType(i1, t1),
|
||||
Error::InvalidFunctionParameterType(i2, t2),
|
||||
) => i1 == i2 && t1 == t2,
|
||||
#[cfg(feature = "vtab")]
|
||||
(
|
||||
Error::InvalidFilterParameterType(i1, t1),
|
||||
Error::InvalidFilterParameterType(i2, t2),
|
||||
) => i1 == i2 && t1 == t2,
|
||||
(Error::InvalidQuery, Error::InvalidQuery) => true,
|
||||
#[cfg(feature = "vtab")]
|
||||
(Error::ModuleError(s1), Error::ModuleError(s2)) => s1 == s2,
|
||||
#[cfg(feature = "functions")]
|
||||
(Error::UnwindingPanic, Error::UnwindingPanic) => true,
|
||||
(_, _) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<str::Utf8Error> for Error {
|
||||
fn from(err: str::Utf8Error) -> Error {
|
||||
Error::Utf8Error(err)
|
||||
|
25
src/lib.rs
25
src/lib.rs
@ -148,8 +148,8 @@ pub type Result<T> = result::Result<T, Error>;
|
||||
pub trait OptionalExtension<T> {
|
||||
/// Converts a `Result<T>` into a `Result<Option<T>>`.
|
||||
///
|
||||
/// By default, Rusqlite treats 0 rows being returned from a query that is expected to return 1
|
||||
/// row as an error. This method will
|
||||
/// By default, Rusqlite treats 0 rows being returned from a query that is
|
||||
/// expected to return 1 row as an error. This method will
|
||||
/// handle that error, and give you back an `Option<T>` instead.
|
||||
fn optional(self) -> Result<Option<T>>;
|
||||
}
|
||||
@ -392,8 +392,9 @@ impl Connection {
|
||||
/// If the query returns more than one row, all rows except the first are
|
||||
/// ignored.
|
||||
///
|
||||
/// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the query truly is optional,
|
||||
/// you can call `.optional()` on the result of this to get a `Result<Option<T>>`.
|
||||
/// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the
|
||||
/// query truly is optional, you can call `.optional()` on the result of
|
||||
/// this to get a `Result<Option<T>>`.
|
||||
///
|
||||
/// # Failure
|
||||
///
|
||||
@ -415,8 +416,9 @@ impl Connection {
|
||||
/// If the query returns more than one row, all rows except the first are
|
||||
/// ignored.
|
||||
///
|
||||
/// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the query truly is optional,
|
||||
/// you can call `.optional()` on the result of this to get a `Result<Option<T>>`.
|
||||
/// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the
|
||||
/// query truly is optional, you can call `.optional()` on the result of
|
||||
/// this to get a `Result<Option<T>>`.
|
||||
///
|
||||
/// # Failure
|
||||
///
|
||||
@ -1277,9 +1279,8 @@ mod test {
|
||||
fn test_execute_select() {
|
||||
let db = checked_memory_handle();
|
||||
let err = db.execute("SELECT 1 WHERE 1 < ?", &[1i32]).unwrap_err();
|
||||
match err {
|
||||
Error::ExecuteReturnedResults => (),
|
||||
_ => panic!("Unexpected error: {}", err),
|
||||
if err != Error::ExecuteReturnedResults {
|
||||
panic!("Unexpected error: {}", err);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1408,16 +1409,14 @@ mod test {
|
||||
fn test_optional() {
|
||||
let db = checked_memory_handle();
|
||||
|
||||
let result: Result<i64> =
|
||||
db.query_row("SELECT 1 WHERE 0 <> 0", NO_PARAMS, |r| r.get(0));
|
||||
let result: Result<i64> = db.query_row("SELECT 1 WHERE 0 <> 0", NO_PARAMS, |r| r.get(0));
|
||||
let result = result.optional();
|
||||
match result.unwrap() {
|
||||
None => (),
|
||||
_ => panic!("Unexpected result"),
|
||||
}
|
||||
|
||||
let result: Result<i64> =
|
||||
db.query_row("SELECT 1 WHERE 0 == 0", NO_PARAMS, |r| r.get(0));
|
||||
let result: Result<i64> = db.query_row("SELECT 1 WHERE 0 == 0", NO_PARAMS, |r| r.get(0));
|
||||
let result = result.optional();
|
||||
match result.unwrap() {
|
||||
Some(1) => (),
|
||||
|
@ -377,8 +377,9 @@ impl<'conn> Statement<'conn> {
|
||||
/// If the query returns more than one row, all rows except the first are
|
||||
/// ignored.
|
||||
///
|
||||
/// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the query truly is optional,
|
||||
/// you can call `.optional()` on the result of this to get a `Result<Option<T>>`.
|
||||
/// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the
|
||||
/// query truly is optional, you can call `.optional()` on the result of
|
||||
/// this to get a `Result<Option<T>>`.
|
||||
///
|
||||
/// # Failure
|
||||
///
|
||||
|
Loading…
Reference in New Issue
Block a user