mirror of
https://github.com/isar/rusqlite.git
synced 2025-03-26 07:16:05 +08:00
Introduce err macro
This commit is contained in:
parent
8081589201
commit
e025f283f5
15
src/error.rs
15
src/error.rs
@ -412,6 +412,21 @@ pub fn error_from_sqlite_code(code: c_int, message: Option<String>) -> Error {
|
||||
Error::SqliteFailure(ffi::Error::new(code), message)
|
||||
}
|
||||
|
||||
macro_rules! err {
|
||||
($code:expr $(,)?) => {
|
||||
$crate::error::error_from_sqlite_code($code, None)
|
||||
};
|
||||
($code:expr, $msg:literal $(,)?) => {
|
||||
$crate::error::error_from_sqlite_code($code, Some(format!($msg)))
|
||||
};
|
||||
($code:expr, $err:expr $(,)?) => {
|
||||
$crate::error::error_from_sqlite_code($code, Some(format!($err)))
|
||||
};
|
||||
($code:expr, $fmt:expr, $($arg:tt)*) => {
|
||||
$crate::error::error_from_sqlite_code($code, Some(format!($fmt, $($arg)*)))
|
||||
};
|
||||
}
|
||||
|
||||
#[cold]
|
||||
pub unsafe fn error_from_handle(db: *mut ffi::sqlite3, code: c_int) -> Error {
|
||||
error_from_sqlite_code(code, error_msg(db, code))
|
||||
|
@ -90,7 +90,7 @@ impl InnerConnection {
|
||||
let r = ffi::sqlite3_open_v2(c_path.as_ptr(), &mut db, flags.bits(), z_vfs);
|
||||
if r != ffi::SQLITE_OK {
|
||||
let e = if db.is_null() {
|
||||
error_from_sqlite_code(r, Some(c_path.to_string_lossy().to_string()))
|
||||
err!(r, "{}", c_path.to_string_lossy())
|
||||
} else {
|
||||
let mut e = error_from_handle(db, r);
|
||||
if let Error::SqliteFailure(
|
||||
@ -101,10 +101,7 @@ impl InnerConnection {
|
||||
Some(msg),
|
||||
) = e
|
||||
{
|
||||
e = Error::SqliteFailure(
|
||||
ffi::Error::new(r),
|
||||
Some(format!("{msg}: {}", c_path.to_string_lossy())),
|
||||
);
|
||||
e = err!(r, "{msg}: {}", c_path.to_string_lossy());
|
||||
}
|
||||
ffi::sqlite3_close(db);
|
||||
e
|
||||
@ -344,14 +341,11 @@ impl InnerConnection {
|
||||
match r {
|
||||
0 => Ok(false),
|
||||
1 => Ok(true),
|
||||
-1 => Err(Error::SqliteFailure(
|
||||
ffi::Error::new(ffi::SQLITE_MISUSE),
|
||||
Some(format!("{db_name:?} is not the name of a database")),
|
||||
)),
|
||||
_ => Err(error_from_sqlite_code(
|
||||
r,
|
||||
Some("Unexpected result".to_owned()),
|
||||
-1 => Err(err!(
|
||||
ffi::SQLITE_MISUSE,
|
||||
"{db_name:?} is not the name of a database"
|
||||
)),
|
||||
_ => Err(err!(r, "Unexpected result")),
|
||||
}
|
||||
}
|
||||
|
||||
@ -370,14 +364,11 @@ impl InnerConnection {
|
||||
0 => Ok(super::transaction::TransactionState::None),
|
||||
1 => Ok(super::transaction::TransactionState::Read),
|
||||
2 => Ok(super::transaction::TransactionState::Write),
|
||||
-1 => Err(Error::SqliteFailure(
|
||||
ffi::Error::new(ffi::SQLITE_MISUSE),
|
||||
Some(format!("{db_name:?} is not the name of a valid schema")),
|
||||
)),
|
||||
_ => Err(error_from_sqlite_code(
|
||||
r,
|
||||
Some("Unexpected result".to_owned()),
|
||||
-1 => Err(err!(
|
||||
ffi::SQLITE_MISUSE,
|
||||
"{db_name:?} is not the name of a valid schema"
|
||||
)),
|
||||
_ => Err(err!(r, "Unexpected result")),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -93,6 +93,7 @@ pub use crate::version::*;
|
||||
#[doc(hidden)]
|
||||
pub use rusqlite_macros::__bind;
|
||||
|
||||
#[macro_use]
|
||||
mod error;
|
||||
|
||||
#[cfg(not(feature = "loadable_extension"))]
|
||||
@ -321,10 +322,7 @@ fn str_for_sqlite(s: &[u8]) -> Result<(*const c_char, c_int, ffi::sqlite3_destru
|
||||
// failed.
|
||||
fn len_as_c_int(len: usize) -> Result<c_int> {
|
||||
if len >= (c_int::MAX as usize) {
|
||||
Err(Error::SqliteFailure(
|
||||
ffi::Error::new(ffi::SQLITE_TOOBIG),
|
||||
None,
|
||||
))
|
||||
Err(err!(ffi::SQLITE_TOOBIG))
|
||||
} else {
|
||||
Ok(len as c_int)
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
//! Run-Time Limits
|
||||
|
||||
use crate::{ffi, Connection, Error, Result};
|
||||
use crate::{ffi, Connection, Result};
|
||||
use std::os::raw::c_int;
|
||||
|
||||
/// Run-Time limit categories, for use with [`Connection::limit`] and
|
||||
@ -57,10 +57,7 @@ impl Connection {
|
||||
let c = self.db.borrow();
|
||||
let rc = unsafe { ffi::sqlite3_limit(c.db(), limit as c_int, -1) };
|
||||
if rc < 0 {
|
||||
return Err(Error::SqliteFailure(
|
||||
ffi::Error::new(ffi::SQLITE_RANGE),
|
||||
Some(format!("{limit:?} is invalid")),
|
||||
));
|
||||
return Err(err!(ffi::SQLITE_RANGE, "{limit:?} is invalid"));
|
||||
}
|
||||
Ok(rc)
|
||||
}
|
||||
@ -71,18 +68,12 @@ impl Connection {
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "limits")))]
|
||||
pub fn set_limit(&self, limit: Limit, new_val: i32) -> Result<i32> {
|
||||
if new_val < 0 {
|
||||
return Err(Error::SqliteFailure(
|
||||
ffi::Error::new(ffi::SQLITE_RANGE),
|
||||
Some(format!("{new_val} is invalid")),
|
||||
));
|
||||
return Err(err!(ffi::SQLITE_RANGE, "{new_val} is invalid"));
|
||||
}
|
||||
let c = self.db.borrow_mut();
|
||||
let rc = unsafe { ffi::sqlite3_limit(c.db(), limit as c_int, new_val) };
|
||||
if rc < 0 {
|
||||
return Err(Error::SqliteFailure(
|
||||
ffi::Error::new(ffi::SQLITE_RANGE),
|
||||
Some(format!("{limit:?} is invalid")),
|
||||
));
|
||||
return Err(err!(ffi::SQLITE_RANGE, "{limit:?} is invalid"));
|
||||
}
|
||||
Ok(rc)
|
||||
}
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
use std::ops::Deref;
|
||||
|
||||
use crate::error::Error;
|
||||
use crate::ffi;
|
||||
use crate::types::{ToSql, ToSqlOutput, ValueRef};
|
||||
use crate::{Connection, DatabaseName, Result, Row};
|
||||
@ -35,10 +34,7 @@ impl Sql {
|
||||
self.buf.push_str(keyword);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::SqliteFailure(
|
||||
ffi::Error::new(ffi::SQLITE_MISUSE),
|
||||
Some(format!("Invalid keyword \"{keyword}\"")),
|
||||
))
|
||||
Err(err!(ffi::SQLITE_MISUSE, "Invalid keyword \"{keyword}\""))
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,24 +62,15 @@ impl Sql {
|
||||
ToSqlOutput::Owned(ref v) => ValueRef::from(v),
|
||||
#[cfg(feature = "blob")]
|
||||
ToSqlOutput::ZeroBlob(_) => {
|
||||
return Err(Error::SqliteFailure(
|
||||
ffi::Error::new(ffi::SQLITE_MISUSE),
|
||||
Some(format!("Unsupported value \"{value:?}\"")),
|
||||
));
|
||||
return Err(err!(ffi::SQLITE_MISUSE, "Unsupported value \"{value:?}\""));
|
||||
}
|
||||
#[cfg(feature = "functions")]
|
||||
ToSqlOutput::Arg(_) => {
|
||||
return Err(Error::SqliteFailure(
|
||||
ffi::Error::new(ffi::SQLITE_MISUSE),
|
||||
Some(format!("Unsupported value \"{value:?}\"")),
|
||||
));
|
||||
return Err(err!(ffi::SQLITE_MISUSE, "Unsupported value \"{value:?}\""));
|
||||
}
|
||||
#[cfg(feature = "array")]
|
||||
ToSqlOutput::Array(_) => {
|
||||
return Err(Error::SqliteFailure(
|
||||
ffi::Error::new(ffi::SQLITE_MISUSE),
|
||||
Some(format!("Unsupported value \"{value:?}\"")),
|
||||
));
|
||||
return Err(err!(ffi::SQLITE_MISUSE, "Unsupported value \"{value:?}\""));
|
||||
}
|
||||
};
|
||||
match value {
|
||||
@ -98,10 +85,7 @@ impl Sql {
|
||||
self.push_string_literal(s);
|
||||
}
|
||||
_ => {
|
||||
return Err(Error::SqliteFailure(
|
||||
ffi::Error::new(ffi::SQLITE_MISUSE),
|
||||
Some(format!("Unsupported value \"{value:?}\"")),
|
||||
));
|
||||
return Err(err!(ffi::SQLITE_MISUSE, "Unsupported value \"{value:?}\""));
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
|
@ -608,10 +608,7 @@ impl Statement<'_> {
|
||||
}
|
||||
#[cfg(feature = "functions")]
|
||||
ToSqlOutput::Arg(_) => {
|
||||
return Err(Error::SqliteFailure(
|
||||
ffi::Error::new(ffi::SQLITE_MISUSE),
|
||||
Some(format!("Unsupported value \"{value:?}\"")),
|
||||
));
|
||||
return Err(err!(ffi::SQLITE_MISUSE, "Unsupported value \"{value:?}\""));
|
||||
}
|
||||
#[cfg(feature = "array")]
|
||||
ToSqlOutput::Array(a) => {
|
||||
|
@ -501,10 +501,7 @@ impl IndexInfo {
|
||||
let idx = constraint_idx as c_int;
|
||||
let collation = unsafe { ffi::sqlite3_vtab_collation(self.0, idx) };
|
||||
if collation.is_null() {
|
||||
return Err(Error::SqliteFailure(
|
||||
ffi::Error::new(ffi::SQLITE_MISUSE),
|
||||
Some(format!("{constraint_idx} is out of range")),
|
||||
));
|
||||
return Err(err!(ffi::SQLITE_MISUSE, "{constraint_idx} is out of range"));
|
||||
}
|
||||
Ok(unsafe { CStr::from_ptr(collation) }.to_str()?)
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ use crate::vtab::{
|
||||
eponymous_only_module, Context, IndexConstraintOp, IndexInfo, VTab, VTabConfig, VTabConnection,
|
||||
VTabCursor, Values,
|
||||
};
|
||||
use crate::{Connection, Error, Result};
|
||||
use crate::{error::error_from_sqlite_code, Connection, Result};
|
||||
|
||||
/// Register the `generate_series` module.
|
||||
pub fn load_module(conn: &Connection) -> Result<()> {
|
||||
@ -108,10 +108,7 @@ unsafe impl<'vtab> VTab<'vtab> for SeriesTab {
|
||||
debug_assert_eq!(Ok("BINARY"), info.collation(*j));
|
||||
}
|
||||
if !(unusable_mask & !idx_num).is_empty() {
|
||||
return Err(Error::SqliteFailure(
|
||||
ffi::Error::new(ffi::SQLITE_CONSTRAINT),
|
||||
None,
|
||||
));
|
||||
return Err(error_from_sqlite_code(ffi::SQLITE_CONSTRAINT, None));
|
||||
}
|
||||
if idx_num.contains(QueryPlanFlags::BOTH) {
|
||||
// Both start= and stop= boundaries are available.
|
||||
|
Loading…
x
Reference in New Issue
Block a user