mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-22 16:29:20 +08:00
Merge pull request #1159 from gwenn/sqlite_error_code
Use sqlite_error_code where possible
This commit is contained in:
commit
7fb2d378af
12
src/busy.rs
12
src/busy.rs
@ -90,7 +90,7 @@ mod test {
|
|||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::{Connection, Error, ErrorCode, Result, TransactionBehavior};
|
use crate::{Connection, ErrorCode, Result, TransactionBehavior};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default_busy() -> Result<()> {
|
fn test_default_busy() -> Result<()> {
|
||||||
@ -101,12 +101,10 @@ mod test {
|
|||||||
let tx1 = db1.transaction_with_behavior(TransactionBehavior::Exclusive)?;
|
let tx1 = db1.transaction_with_behavior(TransactionBehavior::Exclusive)?;
|
||||||
let db2 = Connection::open(&path)?;
|
let db2 = Connection::open(&path)?;
|
||||||
let r: Result<()> = db2.query_row("PRAGMA schema_version", [], |_| unreachable!());
|
let r: Result<()> = db2.query_row("PRAGMA schema_version", [], |_| unreachable!());
|
||||||
match r.unwrap_err() {
|
assert_eq!(
|
||||||
Error::SqliteFailure(err, _) => {
|
r.unwrap_err().sqlite_error_code(),
|
||||||
assert_eq!(err.code, ErrorCode::DatabaseBusy);
|
Some(ErrorCode::DatabaseBusy)
|
||||||
}
|
);
|
||||||
err => panic!("Unexpected error {}", err),
|
|
||||||
}
|
|
||||||
tx1.rollback()
|
tx1.rollback()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
19
src/lib.rs
19
src/lib.rs
@ -384,9 +384,8 @@ impl Connection {
|
|||||||
///
|
///
|
||||||
/// - Open the database for both reading or writing.
|
/// - Open the database for both reading or writing.
|
||||||
/// - Create the database if one does not exist at the path.
|
/// - Create the database if one does not exist at the path.
|
||||||
/// - Allow the filename to be interpreted as a URI (see
|
/// - Allow the filename to be interpreted as a URI (see <https://www.sqlite.org/uri.html#uri_filenames_in_sqlite>
|
||||||
/// <https://www.sqlite.org/uri.html#uri_filenames_in_sqlite> for
|
/// for details).
|
||||||
/// details).
|
|
||||||
/// - Disables the use of a per-connection mutex.
|
/// - Disables the use of a per-connection mutex.
|
||||||
///
|
///
|
||||||
/// Rusqlite enforces thread-safety at compile time, so additional
|
/// Rusqlite enforces thread-safety at compile time, so additional
|
||||||
@ -1289,7 +1288,7 @@ mod test {
|
|||||||
let filename = "no_such_file.db";
|
let filename = "no_such_file.db";
|
||||||
let result = Connection::open_with_flags(filename, OpenFlags::SQLITE_OPEN_READ_ONLY);
|
let result = Connection::open_with_flags(filename, OpenFlags::SQLITE_OPEN_READ_ONLY);
|
||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
let err = result.err().unwrap();
|
let err = result.unwrap_err();
|
||||||
if let Error::SqliteFailure(e, Some(msg)) = err {
|
if let Error::SqliteFailure(e, Some(msg)) = err {
|
||||||
assert_eq!(ErrorCode::CannotOpen, e.code);
|
assert_eq!(ErrorCode::CannotOpen, e.code);
|
||||||
assert_eq!(ffi::SQLITE_CANTOPEN, e.extended_code);
|
assert_eq!(ffi::SQLITE_CANTOPEN, e.extended_code);
|
||||||
@ -1742,14 +1741,10 @@ mod test {
|
|||||||
|
|
||||||
let result: Result<Vec<i32>> = stmt.query([])?.map(|r| r.get(0)).collect();
|
let result: Result<Vec<i32>> = stmt.query([])?.map(|r| r.get(0)).collect();
|
||||||
|
|
||||||
match result.unwrap_err() {
|
assert_eq!(
|
||||||
Error::SqliteFailure(err, _) => {
|
result.unwrap_err().sqlite_error_code(),
|
||||||
assert_eq!(err.code, ErrorCode::OperationInterrupted);
|
Some(ErrorCode::OperationInterrupted)
|
||||||
}
|
);
|
||||||
err => {
|
|
||||||
panic!("Unexpected error {}", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -272,85 +272,67 @@ mod test {
|
|||||||
// check some invalid types
|
// check some invalid types
|
||||||
|
|
||||||
// 0 is actually a blob (Vec<u8>)
|
// 0 is actually a blob (Vec<u8>)
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(row.get::<_, c_int>(0).unwrap_err()));
|
||||||
row.get::<_, c_int>(0).err().unwrap()
|
assert!(is_invalid_column_type(row.get::<_, c_int>(0).unwrap_err()));
|
||||||
));
|
|
||||||
assert!(is_invalid_column_type(
|
|
||||||
row.get::<_, c_int>(0).err().unwrap()
|
|
||||||
));
|
|
||||||
assert!(is_invalid_column_type(row.get::<_, i64>(0).err().unwrap()));
|
assert!(is_invalid_column_type(row.get::<_, i64>(0).err().unwrap()));
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(
|
||||||
row.get::<_, c_double>(0).err().unwrap()
|
row.get::<_, c_double>(0).unwrap_err()
|
||||||
));
|
|
||||||
assert!(is_invalid_column_type(
|
|
||||||
row.get::<_, String>(0).err().unwrap()
|
|
||||||
));
|
));
|
||||||
|
assert!(is_invalid_column_type(row.get::<_, String>(0).unwrap_err()));
|
||||||
#[cfg(feature = "time")]
|
#[cfg(feature = "time")]
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(
|
||||||
row.get::<_, time::OffsetDateTime>(0).err().unwrap()
|
row.get::<_, time::OffsetDateTime>(0).unwrap_err()
|
||||||
));
|
));
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(
|
||||||
row.get::<_, Option<c_int>>(0).err().unwrap()
|
row.get::<_, Option<c_int>>(0).unwrap_err()
|
||||||
));
|
));
|
||||||
|
|
||||||
// 1 is actually a text (String)
|
// 1 is actually a text (String)
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(row.get::<_, c_int>(1).unwrap_err()));
|
||||||
row.get::<_, c_int>(1).err().unwrap()
|
|
||||||
));
|
|
||||||
assert!(is_invalid_column_type(row.get::<_, i64>(1).err().unwrap()));
|
assert!(is_invalid_column_type(row.get::<_, i64>(1).err().unwrap()));
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(
|
||||||
row.get::<_, c_double>(1).err().unwrap()
|
row.get::<_, c_double>(1).unwrap_err()
|
||||||
));
|
));
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(
|
||||||
row.get::<_, Vec<u8>>(1).err().unwrap()
|
row.get::<_, Vec<u8>>(1).unwrap_err()
|
||||||
));
|
));
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(
|
||||||
row.get::<_, Option<c_int>>(1).err().unwrap()
|
row.get::<_, Option<c_int>>(1).unwrap_err()
|
||||||
));
|
));
|
||||||
|
|
||||||
// 2 is actually an integer
|
// 2 is actually an integer
|
||||||
|
assert!(is_invalid_column_type(row.get::<_, String>(2).unwrap_err()));
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(
|
||||||
row.get::<_, String>(2).err().unwrap()
|
row.get::<_, Vec<u8>>(2).unwrap_err()
|
||||||
));
|
));
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(
|
||||||
row.get::<_, Vec<u8>>(2).err().unwrap()
|
row.get::<_, Option<String>>(2).unwrap_err()
|
||||||
));
|
|
||||||
assert!(is_invalid_column_type(
|
|
||||||
row.get::<_, Option<String>>(2).err().unwrap()
|
|
||||||
));
|
));
|
||||||
|
|
||||||
// 3 is actually a float (c_double)
|
// 3 is actually a float (c_double)
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(row.get::<_, c_int>(3).unwrap_err()));
|
||||||
row.get::<_, c_int>(3).err().unwrap()
|
|
||||||
));
|
|
||||||
assert!(is_invalid_column_type(row.get::<_, i64>(3).err().unwrap()));
|
assert!(is_invalid_column_type(row.get::<_, i64>(3).err().unwrap()));
|
||||||
|
assert!(is_invalid_column_type(row.get::<_, String>(3).unwrap_err()));
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(
|
||||||
row.get::<_, String>(3).err().unwrap()
|
row.get::<_, Vec<u8>>(3).unwrap_err()
|
||||||
));
|
));
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(
|
||||||
row.get::<_, Vec<u8>>(3).err().unwrap()
|
row.get::<_, Option<c_int>>(3).unwrap_err()
|
||||||
));
|
|
||||||
assert!(is_invalid_column_type(
|
|
||||||
row.get::<_, Option<c_int>>(3).err().unwrap()
|
|
||||||
));
|
));
|
||||||
|
|
||||||
// 4 is actually NULL
|
// 4 is actually NULL
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(row.get::<_, c_int>(4).unwrap_err()));
|
||||||
row.get::<_, c_int>(4).err().unwrap()
|
|
||||||
));
|
|
||||||
assert!(is_invalid_column_type(row.get::<_, i64>(4).err().unwrap()));
|
assert!(is_invalid_column_type(row.get::<_, i64>(4).err().unwrap()));
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(
|
||||||
row.get::<_, c_double>(4).err().unwrap()
|
row.get::<_, c_double>(4).unwrap_err()
|
||||||
));
|
));
|
||||||
|
assert!(is_invalid_column_type(row.get::<_, String>(4).unwrap_err()));
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(
|
||||||
row.get::<_, String>(4).err().unwrap()
|
row.get::<_, Vec<u8>>(4).unwrap_err()
|
||||||
));
|
|
||||||
assert!(is_invalid_column_type(
|
|
||||||
row.get::<_, Vec<u8>>(4).err().unwrap()
|
|
||||||
));
|
));
|
||||||
#[cfg(feature = "time")]
|
#[cfg(feature = "time")]
|
||||||
assert!(is_invalid_column_type(
|
assert!(is_invalid_column_type(
|
||||||
row.get::<_, time::OffsetDateTime>(4).err().unwrap()
|
row.get::<_, time::OffsetDateTime>(4).unwrap_err()
|
||||||
));
|
));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user