Merge pull request #1159 from gwenn/sqlite_error_code

Use sqlite_error_code where possible
This commit is contained in:
gwenn 2022-04-12 15:19:53 +02:00 committed by GitHub
commit 7fb2d378af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 59 deletions

View File

@ -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()
} }

View File

@ -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(())
} }

View File

@ -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(())
} }