diff --git a/Cargo.toml b/Cargo.toml index 75d0d77..9519a6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,7 +52,7 @@ buildtime_bindgen = ["libsqlite3-sys/buildtime_bindgen"] limits = [] loadable_extension = ["libsqlite3-sys/loadable_extension"] hooks = [] -preupdate_hook = ["libsqlite3-sys/preupdate_hook"] +preupdate_hook = ["libsqlite3-sys/preupdate_hook", "hooks"] i128_blob = [] sqlcipher = ["libsqlite3-sys/sqlcipher"] unlock_notify = ["libsqlite3-sys/unlock_notify"] diff --git a/src/hooks.rs b/src/hooks.rs deleted file mode 100644 index b349078..0000000 --- a/src/hooks.rs +++ /dev/null @@ -1,434 +0,0 @@ -//! Commit, Data Change and Rollback Notification Callbacks -#![allow(non_camel_case_types)] -use std::os::raw::c_void; - -use crate::ffi; - -/// Action Codes -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -#[repr(i32)] -#[non_exhaustive] -#[allow(clippy::upper_case_acronyms)] -pub enum Action { - /// Unsupported / unexpected action - UNKNOWN = -1, - /// DELETE command - SQLITE_DELETE = ffi::SQLITE_DELETE, - /// INSERT command - SQLITE_INSERT = ffi::SQLITE_INSERT, - /// UPDATE command - SQLITE_UPDATE = ffi::SQLITE_UPDATE, -} - -impl From for Action { - #[inline] - fn from(code: i32) -> Action { - match code { - ffi::SQLITE_DELETE => Action::SQLITE_DELETE, - ffi::SQLITE_INSERT => Action::SQLITE_INSERT, - ffi::SQLITE_UPDATE => Action::SQLITE_UPDATE, - _ => Action::UNKNOWN, - } - } -} - -impl Connection { - /// `feature = "hooks"` Register a callback function to be invoked whenever - /// a transaction is committed. - /// - /// The callback returns `true` to rollback. - #[inline] - pub fn commit_hook<'c, F>(&'c self, hook: Option) - where - F: FnMut() -> bool + Send + 'c, - { - self.db.borrow_mut().commit_hook(hook); - } - - /// `feature = "hooks"` Register a callback function to be invoked whenever - /// a transaction is committed. - /// - /// The callback returns `true` to rollback. - #[inline] - pub fn rollback_hook<'c, F>(&'c self, hook: Option) - where - F: FnMut() + Send + 'c, - { - self.db.borrow_mut().rollback_hook(hook); - } - - /// `feature = "hooks"` Register a callback function to be invoked whenever - /// a row is updated, inserted or deleted in a rowid table. - /// - /// The callback parameters are: - /// - /// - the type of database update (SQLITE_INSERT, SQLITE_UPDATE or - /// SQLITE_DELETE), - /// - the name of the database ("main", "temp", ...), - /// - the name of the table that is updated, - /// - the ROWID of the row that is updated. - #[inline] - pub fn update_hook<'c, F>(&'c self, hook: Option) - where - F: FnMut(Action, &str, &str, i64) + Send + 'c, - { - self.db.borrow_mut().update_hook(hook); - } - - /// `feature = "hooks"` Register a query progress callback. - /// - /// The parameter `num_ops` is the approximate number of virtual machine - /// instructions that are evaluated between successive invocations of the - /// `handler`. If `num_ops` is less than one then the progress handler - /// is disabled. - /// - /// If the progress callback returns `true`, the operation is interrupted. - pub fn progress_handler(&self, num_ops: c_int, handler: Option) - where - F: FnMut() -> bool + Send + RefUnwindSafe + 'static, - { - self.db.borrow_mut().progress_handler(num_ops, handler); - } -} - -impl InnerConnection { - #[inline] - pub fn remove_hooks(&mut self) { - self.update_hook(None::); - self.commit_hook(None:: bool>); - self.rollback_hook(None::); - self.progress_handler(0, None:: bool>); - } - - fn commit_hook<'c, F>(&'c mut self, hook: Option) - where - F: FnMut() -> bool + Send + 'c, - { - unsafe extern "C" fn call_boxed_closure(p_arg: *mut c_void) -> c_int - where - F: FnMut() -> bool, - { - let r = catch_unwind(|| { - let boxed_hook: *mut F = p_arg as *mut F; - (*boxed_hook)() - }); - if let Ok(true) = r { - 1 - } else { - 0 - } - } - - // unlike `sqlite3_create_function_v2`, we cannot specify a `xDestroy` with - // `sqlite3_commit_hook`. so we keep the `xDestroy` function in - // `InnerConnection.free_boxed_hook`. - let free_commit_hook = if hook.is_some() { - Some(free_boxed_hook:: as unsafe fn(*mut c_void)) - } else { - None - }; - - let previous_hook = match hook { - Some(hook) => { - let boxed_hook: *mut F = Box::into_raw(Box::new(hook)); - unsafe { - ffi::sqlite3_commit_hook( - self.db(), - Some(call_boxed_closure::), - boxed_hook as *mut _, - ) - } - } - _ => unsafe { ffi::sqlite3_commit_hook(self.db(), None, ptr::null_mut()) }, - }; - if !previous_hook.is_null() { - if let Some(free_boxed_hook) = self.free_commit_hook { - unsafe { free_boxed_hook(previous_hook) }; - } - } - self.free_commit_hook = free_commit_hook; - } - - fn rollback_hook<'c, F>(&'c mut self, hook: Option) - where - F: FnMut() + Send + 'c, - { - unsafe extern "C" fn call_boxed_closure(p_arg: *mut c_void) - where - F: FnMut(), - { - let _ = catch_unwind(|| { - let boxed_hook: *mut F = p_arg as *mut F; - (*boxed_hook)(); - }); - } - - let free_rollback_hook = if hook.is_some() { - Some(free_boxed_hook:: as unsafe fn(*mut c_void)) - } else { - None - }; - - let previous_hook = match hook { - Some(hook) => { - let boxed_hook: *mut F = Box::into_raw(Box::new(hook)); - unsafe { - ffi::sqlite3_rollback_hook( - self.db(), - Some(call_boxed_closure::), - boxed_hook as *mut _, - ) - } - } - _ => unsafe { ffi::sqlite3_rollback_hook(self.db(), None, ptr::null_mut()) }, - }; - if !previous_hook.is_null() { - if let Some(free_boxed_hook) = self.free_rollback_hook { - unsafe { free_boxed_hook(previous_hook) }; - } - } - self.free_rollback_hook = free_rollback_hook; - } - - fn update_hook<'c, F>(&'c mut self, hook: Option) - where - F: FnMut(Action, &str, &str, i64) + Send + 'c, - { - unsafe extern "C" fn call_boxed_closure( - p_arg: *mut c_void, - action_code: c_int, - db_str: *const c_char, - tbl_str: *const c_char, - row_id: i64, - ) where - F: FnMut(Action, &str, &str, i64), - { - use std::ffi::CStr; - use std::str; - - let action = Action::from(action_code); - let db_name = { - let c_slice = CStr::from_ptr(db_str).to_bytes(); - str::from_utf8(c_slice) - }; - let tbl_name = { - let c_slice = CStr::from_ptr(tbl_str).to_bytes(); - str::from_utf8(c_slice) - }; - - let _ = catch_unwind(|| { - let boxed_hook: *mut F = p_arg as *mut F; - (*boxed_hook)( - action, - db_name.expect("illegal db name"), - tbl_name.expect("illegal table name"), - row_id, - ); - }); - } - - let free_update_hook = if hook.is_some() { - Some(free_boxed_hook:: as unsafe fn(*mut c_void)) - } else { - None - }; - - let previous_hook = match hook { - Some(hook) => { - let boxed_hook: *mut F = Box::into_raw(Box::new(hook)); - unsafe { - ffi::sqlite3_update_hook( - self.db(), - Some(call_boxed_closure::), - boxed_hook as *mut _, - ) - } - } - _ => unsafe { ffi::sqlite3_update_hook(self.db(), None, ptr::null_mut()) }, - }; - if !previous_hook.is_null() { - if let Some(free_boxed_hook) = self.free_update_hook { - unsafe { free_boxed_hook(previous_hook) }; - } - } - self.free_update_hook = free_update_hook; - } - - fn progress_handler(&mut self, num_ops: c_int, handler: Option) - where - F: FnMut() -> bool + Send + RefUnwindSafe + 'static, - { - unsafe extern "C" fn call_boxed_closure(p_arg: *mut c_void) -> c_int - where - F: FnMut() -> bool, - { - let r = catch_unwind(|| { - let boxed_handler: *mut F = p_arg as *mut F; - (*boxed_handler)() - }); - if let Ok(true) = r { - 1 - } else { - 0 - } - } - - match handler { - Some(handler) => { - let boxed_handler = Box::new(handler); - unsafe { - ffi::sqlite3_progress_handler( - self.db(), - num_ops, - Some(call_boxed_closure::), - &*boxed_handler as *const F as *mut _, - ) - } - self.progress_handler = Some(boxed_handler); - } - _ => { - unsafe { ffi::sqlite3_progress_handler(self.db(), num_ops, None, ptr::null_mut()) } - self.progress_handler = None; - } - }; - } -} - -unsafe fn free_boxed_hook(p: *mut c_void) { - drop(Box::from_raw(p as *mut F)); -} - - #[cfg(test)] - mod test { - use super::super::Action; - use crate::{Connection, Result}; - use std::sync::atomic::{AtomicBool, Ordering}; - - #[test] - fn test_commit_hook() -> Result<()> { - let db = Connection::open_in_memory()?; - - let mut called = false; - db.commit_hook(Some(|| { - called = true; - false - })); - db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;")?; - assert!(called); - Ok(()) - } - - #[test] - fn test_fn_commit_hook() -> Result<()> { - let db = Connection::open_in_memory()?; - - fn hook() -> bool { - true - } - - db.commit_hook(Some(hook)); - db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;") - .unwrap_err(); - Ok(()) - } - - #[test] - fn test_rollback_hook() -> Result<()> { - let db = Connection::open_in_memory()?; - - let mut called = false; - db.rollback_hook(Some(|| { - called = true; - })); - db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); ROLLBACK;")?; - assert!(called); - Ok(()) - } - - #[test] - fn test_update_hook() -> Result<()> { - let db = Connection::open_in_memory()?; - - let mut called = false; - db.update_hook(Some(|action, db: &str, tbl: &str, row_id| { - assert_eq!(Action::SQLITE_INSERT, action); - assert_eq!("main", db); - assert_eq!("foo", tbl); - assert_eq!(1, row_id); - called = true; - })); - db.execute_batch("CREATE TABLE foo (t TEXT)")?; - db.execute_batch("INSERT INTO foo VALUES ('lisa')")?; - assert!(called); - Ok(()) - } - - #[test] - fn test_progress_handler() -> Result<()> { - let db = Connection::open_in_memory()?; - - static CALLED: AtomicBool = AtomicBool::new(false); - db.progress_handler( - 1, - Some(|| { - CALLED.store(true, Ordering::Relaxed); - false - }), - ); - db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;")?; - assert!(CALLED.load(Ordering::Relaxed)); - Ok(()) - } - - #[test] - fn test_progress_handler_interrupt() -> Result<()> { - let db = Connection::open_in_memory()?; - - fn handler() -> bool { - true - } - - db.progress_handler(1, Some(handler)); - db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;") - .unwrap_err(); - Ok(()) - } - } - - #[test] - fn test_authorizer() -> Result<()> { - use super::{AuthAction, AuthContext, Authorization}; - - let db = Connection::open_in_memory()?; - db.execute_batch("CREATE TABLE foo (public TEXT, private TEXT)") - .unwrap(); - - let authorizer = move |ctx: AuthContext<'_>| match ctx.action { - AuthAction::Read { - column_name: "private", - .. - } => Authorization::Ignore, - AuthAction::DropTable { .. } => Authorization::Deny, - AuthAction::Pragma { .. } => panic!("shouldn't be called"), - _ => Authorization::Allow, - }; - - db.authorizer(Some(authorizer)); - db.execute_batch( - "BEGIN TRANSACTION; INSERT INTO foo VALUES ('pub txt', 'priv txt'); COMMIT;", - ) - .unwrap(); - db.query_row_and_then("SELECT * FROM foo", [], |row| -> Result<()> { - assert_eq!(row.get::<_, String>("public")?, "pub txt"); - assert!(row.get::<_, Option>("private")?.is_none()); - Ok(()) - }) - .unwrap(); - db.execute_batch("DROP TABLE foo").unwrap_err(); - - db.authorizer(None::) -> Authorization>); - db.execute_batch("PRAGMA user_version=1").unwrap(); // Disallowed by first authorizer, but it's now removed. - - Ok(()) - } -} diff --git a/src/hooks/mod.rs b/src/hooks/mod.rs new file mode 100644 index 0000000..eacf8d7 --- /dev/null +++ b/src/hooks/mod.rs @@ -0,0 +1,813 @@ +//! Commit, Data Change and Rollback Notification Callbacks +#![allow(non_camel_case_types)] + +use std::os::raw::{c_char, c_int, c_void}; +use std::panic::{catch_unwind, RefUnwindSafe}; +use std::ptr; + +use crate::ffi; + +use crate::{Connection, InnerConnection}; + +#[cfg(feature = "preupdate_hook")] +pub use preupdate_hook::*; + +#[cfg(feature = "preupdate_hook")] +mod preupdate_hook; + +/// Action Codes +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[repr(i32)] +#[non_exhaustive] +#[allow(clippy::upper_case_acronyms)] +pub enum Action { + /// Unsupported / unexpected action + UNKNOWN = -1, + /// DELETE command + SQLITE_DELETE = ffi::SQLITE_DELETE, + /// INSERT command + SQLITE_INSERT = ffi::SQLITE_INSERT, + /// UPDATE command + SQLITE_UPDATE = ffi::SQLITE_UPDATE, +} + +impl From for Action { + #[inline] + fn from(code: i32) -> Action { + match code { + ffi::SQLITE_DELETE => Action::SQLITE_DELETE, + ffi::SQLITE_INSERT => Action::SQLITE_INSERT, + ffi::SQLITE_UPDATE => Action::SQLITE_UPDATE, + _ => Action::UNKNOWN, + } + } +} + +/// The context received by an authorizer hook. +/// +/// See for more info. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct AuthContext<'c> { + /// The action to be authorized. + pub action: AuthAction<'c>, + + /// The database name, if applicable. + pub database_name: Option<&'c str>, + + /// The inner-most trigger or view responsible for the access attempt. + /// `None` if the access attempt was made by top-level SQL code. + pub accessor: Option<&'c str>, +} + +/// Actions and arguments found within a statement during +/// preparation. +/// +/// See for more info. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[non_exhaustive] +#[allow(missing_docs)] +pub enum AuthAction<'c> { + /// This variant is not normally produced by SQLite. You may encounter it + // if you're using a different version than what's supported by this library. + Unknown { + /// The unknown authorization action code. + code: i32, + /// The third arg to the authorizer callback. + arg1: Option<&'c str>, + /// The fourth arg to the authorizer callback. + arg2: Option<&'c str>, + }, + CreateIndex { + index_name: &'c str, + table_name: &'c str, + }, + CreateTable { + table_name: &'c str, + }, + CreateTempIndex { + index_name: &'c str, + table_name: &'c str, + }, + CreateTempTable { + table_name: &'c str, + }, + CreateTempTrigger { + trigger_name: &'c str, + table_name: &'c str, + }, + CreateTempView { + view_name: &'c str, + }, + CreateTrigger { + trigger_name: &'c str, + table_name: &'c str, + }, + CreateView { + view_name: &'c str, + }, + Delete { + table_name: &'c str, + }, + DropIndex { + index_name: &'c str, + table_name: &'c str, + }, + DropTable { + table_name: &'c str, + }, + DropTempIndex { + index_name: &'c str, + table_name: &'c str, + }, + DropTempTable { + table_name: &'c str, + }, + DropTempTrigger { + trigger_name: &'c str, + table_name: &'c str, + }, + DropTempView { + view_name: &'c str, + }, + DropTrigger { + trigger_name: &'c str, + table_name: &'c str, + }, + DropView { + view_name: &'c str, + }, + Insert { + table_name: &'c str, + }, + Pragma { + pragma_name: &'c str, + /// The pragma value, if present (e.g., `PRAGMA name = value;`). + pragma_value: Option<&'c str>, + }, + Read { + table_name: &'c str, + column_name: &'c str, + }, + Select, + Transaction { + operation: TransactionOperation, + }, + Update { + table_name: &'c str, + column_name: &'c str, + }, + Attach { + filename: &'c str, + }, + Detach { + database_name: &'c str, + }, + AlterTable { + database_name: &'c str, + table_name: &'c str, + }, + Reindex { + index_name: &'c str, + }, + Analyze { + table_name: &'c str, + }, + CreateVtable { + table_name: &'c str, + module_name: &'c str, + }, + DropVtable { + table_name: &'c str, + module_name: &'c str, + }, + Function { + function_name: &'c str, + }, + Savepoint { + operation: TransactionOperation, + savepoint_name: &'c str, + }, + Recursive, +} + +impl<'c> AuthAction<'c> { + fn from_raw(code: i32, arg1: Option<&'c str>, arg2: Option<&'c str>) -> Self { + match (code, arg1, arg2) { + (ffi::SQLITE_CREATE_INDEX, Some(index_name), Some(table_name)) => Self::CreateIndex { + index_name, + table_name, + }, + (ffi::SQLITE_CREATE_TABLE, Some(table_name), _) => Self::CreateTable { table_name }, + (ffi::SQLITE_CREATE_TEMP_INDEX, Some(index_name), Some(table_name)) => { + Self::CreateTempIndex { + index_name, + table_name, + } + } + (ffi::SQLITE_CREATE_TEMP_TABLE, Some(table_name), _) => { + Self::CreateTempTable { table_name } + } + (ffi::SQLITE_CREATE_TEMP_TRIGGER, Some(trigger_name), Some(table_name)) => { + Self::CreateTempTrigger { + trigger_name, + table_name, + } + } + (ffi::SQLITE_CREATE_TEMP_VIEW, Some(view_name), _) => { + Self::CreateTempView { view_name } + } + (ffi::SQLITE_CREATE_TRIGGER, Some(trigger_name), Some(table_name)) => { + Self::CreateTrigger { + trigger_name, + table_name, + } + } + (ffi::SQLITE_CREATE_VIEW, Some(view_name), _) => Self::CreateView { view_name }, + (ffi::SQLITE_DELETE, Some(table_name), None) => Self::Delete { table_name }, + (ffi::SQLITE_DROP_INDEX, Some(index_name), Some(table_name)) => Self::DropIndex { + index_name, + table_name, + }, + (ffi::SQLITE_DROP_TABLE, Some(table_name), _) => Self::DropTable { table_name }, + (ffi::SQLITE_DROP_TEMP_INDEX, Some(index_name), Some(table_name)) => { + Self::DropTempIndex { + index_name, + table_name, + } + } + (ffi::SQLITE_DROP_TEMP_TABLE, Some(table_name), _) => { + Self::DropTempTable { table_name } + } + (ffi::SQLITE_DROP_TEMP_TRIGGER, Some(trigger_name), Some(table_name)) => { + Self::DropTempTrigger { + trigger_name, + table_name, + } + } + (ffi::SQLITE_DROP_TEMP_VIEW, Some(view_name), _) => Self::DropTempView { view_name }, + (ffi::SQLITE_DROP_TRIGGER, Some(trigger_name), Some(table_name)) => Self::DropTrigger { + trigger_name, + table_name, + }, + (ffi::SQLITE_DROP_VIEW, Some(view_name), _) => Self::DropView { view_name }, + (ffi::SQLITE_INSERT, Some(table_name), _) => Self::Insert { table_name }, + (ffi::SQLITE_PRAGMA, Some(pragma_name), pragma_value) => Self::Pragma { + pragma_name, + pragma_value, + }, + (ffi::SQLITE_READ, Some(table_name), Some(column_name)) => Self::Read { + table_name, + column_name, + }, + (ffi::SQLITE_SELECT, ..) => Self::Select, + (ffi::SQLITE_TRANSACTION, Some(operation_str), _) => Self::Transaction { + operation: TransactionOperation::from_str(operation_str), + }, + (ffi::SQLITE_UPDATE, Some(table_name), Some(column_name)) => Self::Update { + table_name, + column_name, + }, + (ffi::SQLITE_ATTACH, Some(filename), _) => Self::Attach { filename }, + (ffi::SQLITE_DETACH, Some(database_name), _) => Self::Detach { database_name }, + (ffi::SQLITE_ALTER_TABLE, Some(database_name), Some(table_name)) => Self::AlterTable { + database_name, + table_name, + }, + (ffi::SQLITE_REINDEX, Some(index_name), _) => Self::Reindex { index_name }, + (ffi::SQLITE_ANALYZE, Some(table_name), _) => Self::Analyze { table_name }, + (ffi::SQLITE_CREATE_VTABLE, Some(table_name), Some(module_name)) => { + Self::CreateVtable { + table_name, + module_name, + } + } + (ffi::SQLITE_DROP_VTABLE, Some(table_name), Some(module_name)) => Self::DropVtable { + table_name, + module_name, + }, + (ffi::SQLITE_FUNCTION, _, Some(function_name)) => Self::Function { function_name }, + (ffi::SQLITE_SAVEPOINT, Some(operation_str), Some(savepoint_name)) => Self::Savepoint { + operation: TransactionOperation::from_str(operation_str), + savepoint_name, + }, + (ffi::SQLITE_RECURSIVE, ..) => Self::Recursive, + (code, arg1, arg2) => Self::Unknown { code, arg1, arg2 }, + } + } +} + +pub(crate) type BoxedAuthorizer = + Box FnMut(AuthContext<'c>) -> Authorization + Send + 'static>; + +/// A transaction operation. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[non_exhaustive] +#[allow(missing_docs)] +pub enum TransactionOperation { + Unknown, + Begin, + Release, + Rollback, +} + +impl TransactionOperation { + fn from_str(op_str: &str) -> Self { + match op_str { + "BEGIN" => Self::Begin, + "RELEASE" => Self::Release, + "ROLLBACK" => Self::Rollback, + _ => Self::Unknown, + } + } +} + +/// [`authorizer`](Connection::authorizer) return code +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[non_exhaustive] +pub enum Authorization { + /// Authorize the action. + Allow, + /// Don't allow access, but don't trigger an error either. + Ignore, + /// Trigger an error. + Deny, +} + +impl Authorization { + fn into_raw(self) -> c_int { + match self { + Self::Allow => ffi::SQLITE_OK, + Self::Ignore => ffi::SQLITE_IGNORE, + Self::Deny => ffi::SQLITE_DENY, + } + } +} + +impl Connection { + /// Register a callback function to be invoked whenever + /// a transaction is committed. + /// + /// The callback returns `true` to rollback. + #[inline] + pub fn commit_hook(&self, hook: Option) + where + F: FnMut() -> bool + Send + 'static, + { + self.db.borrow_mut().commit_hook(hook); + } + + /// Register a callback function to be invoked whenever + /// a transaction is committed. + #[inline] + pub fn rollback_hook(&self, hook: Option) + where + F: FnMut() + Send + 'static, + { + self.db.borrow_mut().rollback_hook(hook); + } + + /// Register a callback function to be invoked whenever + /// a row is updated, inserted or deleted in a rowid table. + /// + /// The callback parameters are: + /// + /// - the type of database update (`SQLITE_INSERT`, `SQLITE_UPDATE` or + /// `SQLITE_DELETE`), + /// - the name of the database ("main", "temp", ...), + /// - the name of the table that is updated, + /// - the ROWID of the row that is updated. + #[inline] + pub fn update_hook(&self, hook: Option) + where + F: FnMut(Action, &str, &str, i64) + Send + 'static, + { + self.db.borrow_mut().update_hook(hook); + } + + /// Register a query progress callback. + /// + /// The parameter `num_ops` is the approximate number of virtual machine + /// instructions that are evaluated between successive invocations of the + /// `handler`. If `num_ops` is less than one then the progress handler + /// is disabled. + /// + /// If the progress callback returns `true`, the operation is interrupted. + pub fn progress_handler(&self, num_ops: c_int, handler: Option) + where + F: FnMut() -> bool + Send + RefUnwindSafe + 'static, + { + self.db.borrow_mut().progress_handler(num_ops, handler); + } + + /// Register an authorizer callback that's invoked + /// as a statement is being prepared. + #[inline] + pub fn authorizer<'c, F>(&self, hook: Option) + where + F: for<'r> FnMut(AuthContext<'r>) -> Authorization + Send + RefUnwindSafe + 'static, + { + self.db.borrow_mut().authorizer(hook); + } +} + +impl InnerConnection { + #[inline] + pub fn remove_hooks(&mut self) { + self.update_hook(None::); + self.commit_hook(None:: bool>); + self.rollback_hook(None::); + self.progress_handler(0, None:: bool>); + self.authorizer(None::) -> Authorization>); + } + + fn commit_hook(&mut self, hook: Option) + where + F: FnMut() -> bool + Send + 'static, + { + unsafe extern "C" fn call_boxed_closure(p_arg: *mut c_void) -> c_int + where + F: FnMut() -> bool, + { + let r = catch_unwind(|| { + let boxed_hook: *mut F = p_arg.cast::(); + (*boxed_hook)() + }); + c_int::from(r.unwrap_or_default()) + } + + // unlike `sqlite3_create_function_v2`, we cannot specify a `xDestroy` with + // `sqlite3_commit_hook`. so we keep the `xDestroy` function in + // `InnerConnection.free_boxed_hook`. + let free_commit_hook = if hook.is_some() { + Some(free_boxed_hook:: as unsafe fn(*mut c_void)) + } else { + None + }; + + let previous_hook = match hook { + Some(hook) => { + let boxed_hook: *mut F = Box::into_raw(Box::new(hook)); + unsafe { + ffi::sqlite3_commit_hook( + self.db(), + Some(call_boxed_closure::), + boxed_hook.cast(), + ) + } + } + _ => unsafe { ffi::sqlite3_commit_hook(self.db(), None, ptr::null_mut()) }, + }; + if !previous_hook.is_null() { + if let Some(free_boxed_hook) = self.free_commit_hook { + unsafe { free_boxed_hook(previous_hook) }; + } + } + self.free_commit_hook = free_commit_hook; + } + + fn rollback_hook(&mut self, hook: Option) + where + F: FnMut() + Send + 'static, + { + unsafe extern "C" fn call_boxed_closure(p_arg: *mut c_void) + where + F: FnMut(), + { + drop(catch_unwind(|| { + let boxed_hook: *mut F = p_arg.cast::(); + (*boxed_hook)(); + })); + } + + let free_rollback_hook = if hook.is_some() { + Some(free_boxed_hook:: as unsafe fn(*mut c_void)) + } else { + None + }; + + let previous_hook = match hook { + Some(hook) => { + let boxed_hook: *mut F = Box::into_raw(Box::new(hook)); + unsafe { + ffi::sqlite3_rollback_hook( + self.db(), + Some(call_boxed_closure::), + boxed_hook.cast(), + ) + } + } + _ => unsafe { ffi::sqlite3_rollback_hook(self.db(), None, ptr::null_mut()) }, + }; + if !previous_hook.is_null() { + if let Some(free_boxed_hook) = self.free_rollback_hook { + unsafe { free_boxed_hook(previous_hook) }; + } + } + self.free_rollback_hook = free_rollback_hook; + } + + fn update_hook(&mut self, hook: Option) + where + F: FnMut(Action, &str, &str, i64) + Send + 'static, + { + unsafe extern "C" fn call_boxed_closure( + p_arg: *mut c_void, + action_code: c_int, + p_db_name: *const c_char, + p_table_name: *const c_char, + row_id: i64, + ) where + F: FnMut(Action, &str, &str, i64), + { + let action = Action::from(action_code); + drop(catch_unwind(|| { + let boxed_hook: *mut F = p_arg.cast::(); + (*boxed_hook)( + action, + expect_utf8(p_db_name, "database name"), + expect_utf8(p_table_name, "table name"), + row_id, + ); + })); + } + + let free_update_hook = if hook.is_some() { + Some(free_boxed_hook:: as unsafe fn(*mut c_void)) + } else { + None + }; + + let previous_hook = match hook { + Some(hook) => { + let boxed_hook: *mut F = Box::into_raw(Box::new(hook)); + unsafe { + ffi::sqlite3_update_hook( + self.db(), + Some(call_boxed_closure::), + boxed_hook.cast(), + ) + } + } + _ => unsafe { ffi::sqlite3_update_hook(self.db(), None, ptr::null_mut()) }, + }; + if !previous_hook.is_null() { + if let Some(free_boxed_hook) = self.free_update_hook { + unsafe { free_boxed_hook(previous_hook) }; + } + } + self.free_update_hook = free_update_hook; + } + + fn progress_handler(&mut self, num_ops: c_int, handler: Option) + where + F: FnMut() -> bool + Send + RefUnwindSafe + 'static, + { + unsafe extern "C" fn call_boxed_closure(p_arg: *mut c_void) -> c_int + where + F: FnMut() -> bool, + { + let r = catch_unwind(|| { + let boxed_handler: *mut F = p_arg.cast::(); + (*boxed_handler)() + }); + c_int::from(r.unwrap_or_default()) + } + + if let Some(handler) = handler { + let boxed_handler = Box::new(handler); + unsafe { + ffi::sqlite3_progress_handler( + self.db(), + num_ops, + Some(call_boxed_closure::), + &*boxed_handler as *const F as *mut _, + ); + } + self.progress_handler = Some(boxed_handler); + } else { + unsafe { ffi::sqlite3_progress_handler(self.db(), num_ops, None, ptr::null_mut()) } + self.progress_handler = None; + }; + } + + fn authorizer<'c, F>(&'c mut self, authorizer: Option) + where + F: for<'r> FnMut(AuthContext<'r>) -> Authorization + Send + RefUnwindSafe + 'static, + { + unsafe extern "C" fn call_boxed_closure<'c, F>( + p_arg: *mut c_void, + action_code: c_int, + param1: *const c_char, + param2: *const c_char, + db_name: *const c_char, + trigger_or_view_name: *const c_char, + ) -> c_int + where + F: FnMut(AuthContext<'c>) -> Authorization + Send + 'static, + { + catch_unwind(|| { + let action = AuthAction::from_raw( + action_code, + expect_optional_utf8(param1, "authorizer param 1"), + expect_optional_utf8(param2, "authorizer param 2"), + ); + let auth_ctx = AuthContext { + action, + database_name: expect_optional_utf8(db_name, "database name"), + accessor: expect_optional_utf8( + trigger_or_view_name, + "accessor (inner-most trigger or view)", + ), + }; + let boxed_hook: *mut F = p_arg.cast::(); + (*boxed_hook)(auth_ctx) + }) + .map_or_else(|_| ffi::SQLITE_ERROR, Authorization::into_raw) + } + + let callback_fn = authorizer + .as_ref() + .map(|_| call_boxed_closure::<'c, F> as unsafe extern "C" fn(_, _, _, _, _, _) -> _); + let boxed_authorizer = authorizer.map(Box::new); + + match unsafe { + ffi::sqlite3_set_authorizer( + self.db(), + callback_fn, + boxed_authorizer + .as_ref() + .map_or_else(ptr::null_mut, |f| &**f as *const F as *mut _), + ) + } { + ffi::SQLITE_OK => { + self.authorizer = boxed_authorizer.map(|ba| ba as _); + } + err_code => { + // The only error that `sqlite3_set_authorizer` returns is `SQLITE_MISUSE` + // when compiled with `ENABLE_API_ARMOR` and the db pointer is invalid. + // This library does not allow constructing a null db ptr, so if this branch + // is hit, something very bad has happened. Panicking instead of returning + // `Result` keeps this hook's API consistent with the others. + panic!("unexpectedly failed to set_authorizer: {}", unsafe { + crate::error::error_from_handle(self.db(), err_code) + }); + } + } + } +} + +unsafe fn free_boxed_hook(p: *mut c_void) { + drop(Box::from_raw(p.cast::())); +} + +unsafe fn expect_utf8<'a>(p_str: *const c_char, description: &'static str) -> &'a str { + expect_optional_utf8(p_str, description) + .unwrap_or_else(|| panic!("received empty {description}")) +} + +unsafe fn expect_optional_utf8<'a>( + p_str: *const c_char, + description: &'static str, +) -> Option<&'a str> { + if p_str.is_null() { + return None; + } + std::ffi::CStr::from_ptr(p_str) + .to_str() + .unwrap_or_else(|_| panic!("received non-utf8 string as {description}")) + .into() +} + +#[cfg(test)] +mod test { + use super::Action; + use crate::{Connection, Result}; + use std::sync::atomic::{AtomicBool, Ordering}; + + #[test] + fn test_commit_hook() -> Result<()> { + let db = Connection::open_in_memory()?; + + static CALLED: AtomicBool = AtomicBool::new(false); + db.commit_hook(Some(|| { + CALLED.store(true, Ordering::Relaxed); + false + })); + db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;")?; + assert!(CALLED.load(Ordering::Relaxed)); + Ok(()) + } + + #[test] + fn test_fn_commit_hook() -> Result<()> { + let db = Connection::open_in_memory()?; + + fn hook() -> bool { + true + } + + db.commit_hook(Some(hook)); + db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;") + .unwrap_err(); + Ok(()) + } + + #[test] + fn test_rollback_hook() -> Result<()> { + let db = Connection::open_in_memory()?; + + static CALLED: AtomicBool = AtomicBool::new(false); + db.rollback_hook(Some(|| { + CALLED.store(true, Ordering::Relaxed); + })); + db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); ROLLBACK;")?; + assert!(CALLED.load(Ordering::Relaxed)); + Ok(()) + } + + #[test] + fn test_update_hook() -> Result<()> { + let db = Connection::open_in_memory()?; + + static CALLED: AtomicBool = AtomicBool::new(false); + db.update_hook(Some(|action, db: &str, tbl: &str, row_id| { + assert_eq!(Action::SQLITE_INSERT, action); + assert_eq!("main", db); + assert_eq!("foo", tbl); + assert_eq!(1, row_id); + CALLED.store(true, Ordering::Relaxed); + })); + db.execute_batch("CREATE TABLE foo (t TEXT)")?; + db.execute_batch("INSERT INTO foo VALUES ('lisa')")?; + assert!(CALLED.load(Ordering::Relaxed)); + Ok(()) + } + + #[test] + fn test_progress_handler() -> Result<()> { + let db = Connection::open_in_memory()?; + + static CALLED: AtomicBool = AtomicBool::new(false); + db.progress_handler( + 1, + Some(|| { + CALLED.store(true, Ordering::Relaxed); + false + }), + ); + db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;")?; + assert!(CALLED.load(Ordering::Relaxed)); + Ok(()) + } + + #[test] + fn test_progress_handler_interrupt() -> Result<()> { + let db = Connection::open_in_memory()?; + + fn handler() -> bool { + true + } + + db.progress_handler(1, Some(handler)); + db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;") + .unwrap_err(); + Ok(()) + } + + #[test] + fn test_authorizer() -> Result<()> { + use super::{AuthAction, AuthContext, Authorization}; + + let db = Connection::open_in_memory()?; + db.execute_batch("CREATE TABLE foo (public TEXT, private TEXT)") + .unwrap(); + + let authorizer = move |ctx: AuthContext<'_>| match ctx.action { + AuthAction::Read { + column_name: "private", + .. + } => Authorization::Ignore, + AuthAction::DropTable { .. } => Authorization::Deny, + AuthAction::Pragma { .. } => panic!("shouldn't be called"), + _ => Authorization::Allow, + }; + + db.authorizer(Some(authorizer)); + db.execute_batch( + "BEGIN TRANSACTION; INSERT INTO foo VALUES ('pub txt', 'priv txt'); COMMIT;", + ) + .unwrap(); + db.query_row_and_then("SELECT * FROM foo", [], |row| -> Result<()> { + assert_eq!(row.get::<_, String>("public")?, "pub txt"); + assert!(row.get::<_, Option>("private")?.is_none()); + Ok(()) + }) + .unwrap(); + db.execute_batch("DROP TABLE foo").unwrap_err(); + + db.authorizer(None::) -> Authorization>); + db.execute_batch("PRAGMA user_version=1").unwrap(); // Disallowed by first authorizer, but it's now removed. + + Ok(()) + } +} diff --git a/src/hooks/preupdate_hook.rs b/src/hooks/preupdate_hook.rs new file mode 100644 index 0000000..4f30416 --- /dev/null +++ b/src/hooks/preupdate_hook.rs @@ -0,0 +1,333 @@ +use std::fmt::Debug; +use std::os::raw::{c_char, c_int, c_void}; +use std::panic::catch_unwind; +use std::ptr; + +use super::expect_utf8; +use super::free_boxed_hook; +use super::Action; + +use crate::ffi; +use crate::inner_connection::InnerConnection; +use crate::types::ValueRef; +use crate::Connection; + +/// The possible cases for when a PreUpdateHook gets triggered. Allows access to the relevant +/// functions for each case through the contained values. +#[derive(Debug)] +pub enum PreUpdateCase { + /// Pre-update hook was triggered by an insert. + Insert(PreUpdateNewValueAccessor), + /// Pre-update hook was triggered by a delete. + Delete(PreUpdateOldValueAccessor), + /// Pre-update hook was triggered by an update. + Update { + #[allow(missing_docs)] + old_value_accessor: PreUpdateOldValueAccessor, + #[allow(missing_docs)] + new_value_accessor: PreUpdateNewValueAccessor, + }, + /// This variant is not normally produced by SQLite. You may encounter it + /// if you're using a different version than what's supported by this library. + Unknown, +} + +impl From for Action { + fn from(puc: PreUpdateCase) -> Action { + match puc { + PreUpdateCase::Insert(_) => Action::SQLITE_INSERT, + PreUpdateCase::Delete(_) => Action::SQLITE_DELETE, + PreUpdateCase::Update { .. } => Action::SQLITE_UPDATE, + PreUpdateCase::Unknown => Action::UNKNOWN, + } + } +} + +/// An accessor to access the old values of the row being deleted/updated during the preupdate callback. +#[derive(Debug)] +pub struct PreUpdateOldValueAccessor { + db: *mut ffi::sqlite3, + old_row_id: i64, +} + +impl PreUpdateOldValueAccessor { + /// Get the amount of columns in the row being deleted/updated. + pub fn get_column_count(&self) -> i32 { + unsafe { ffi::sqlite3_preupdate_count(self.db) } + } + + /// Get the depth of the query that triggered the preupdate hook. + /// Returns 0 if the preupdate callback was invoked as a result of + /// a direct insert, update, or delete operation; + /// 1 for inserts, updates, or deletes invoked by top-level triggers; + /// 2 for changes resulting from triggers called by top-level triggers; and so forth. + pub fn get_query_depth(&self) -> i32 { + unsafe { ffi::sqlite3_preupdate_depth(self.db) } + } + + /// Get the row id of the row being updated/deleted. + pub fn get_old_row_id(&self) -> i64 { + self.old_row_id + } + + /// Get the value of the row being updated/deleted at the specified index. + pub fn get_old_column_value(&self, i: i32) -> ValueRef { + let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut(); + unsafe { + ffi::sqlite3_preupdate_old(self.db, i, &mut p_value); + ValueRef::from_value(p_value) + } + } +} + +/// An accessor to access the new values of the row being inserted/updated +/// during the preupdate callback. +#[derive(Debug)] +pub struct PreUpdateNewValueAccessor { + db: *mut ffi::sqlite3, + new_row_id: i64, +} + +impl PreUpdateNewValueAccessor { + /// Get the amount of columns in the row being inserted/updated. + pub fn get_column_count(&self) -> i32 { + unsafe { ffi::sqlite3_preupdate_count(self.db) } + } + + /// Get the depth of the query that triggered the preupdate hook. + /// Returns 0 if the preupdate callback was invoked as a result of + /// a direct insert, update, or delete operation; + /// 1 for inserts, updates, or deletes invoked by top-level triggers; + /// 2 for changes resulting from triggers called by top-level triggers; and so forth. + pub fn get_query_depth(&self) -> i32 { + unsafe { ffi::sqlite3_preupdate_depth(self.db) } + } + + /// Get the row id of the row being inserted/updated. + pub fn get_new_row_id(&self) -> i64 { + self.new_row_id + } + + /// Get the value of the row being updated/deleted at the specified index. + pub fn get_new_column_value(&self, i: i32) -> ValueRef { + let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut(); + unsafe { + ffi::sqlite3_preupdate_new(self.db, i, &mut p_value); + ValueRef::from_value(p_value) + } + } +} + +impl Connection { + /// Register a callback function to be invoked before + /// a row is updated, inserted or deleted. + /// + /// The callback parameters are: + /// + /// - the name of the database ("main", "temp", ...), + /// - the name of the table that is updated, + /// - a variant of the PreUpdateCase enum which allows access to extra functions depending + /// on whether it's an update, delete or insert. + #[inline] + pub fn preupdate_hook(&self, hook: Option) + where + F: FnMut(Action, &str, &str, &PreUpdateCase) + Send, + { + self.db.borrow_mut().preupdate_hook(hook); + } +} + +impl InnerConnection { + #[inline] + pub fn remove_preupdate_hook(&mut self) { + self.preupdate_hook(None::); + } + + fn preupdate_hook<'c, F>(&'c mut self, hook: Option) + where + F: FnMut(Action, &str, &str, &PreUpdateCase) + Send + 'c, + { + unsafe extern "C" fn call_boxed_closure( + p_arg: *mut c_void, + sqlite: *mut ffi::sqlite3, + action_code: c_int, + db_name: *const c_char, + tbl_name: *const c_char, + old_row_id: i64, + new_row_id: i64, + ) where + F: FnMut(Action, &str, &str, &PreUpdateCase), + { + let action = Action::from(action_code); + + let preupdate_case = match action { + Action::SQLITE_INSERT => PreUpdateCase::Insert(PreUpdateNewValueAccessor { + db: sqlite, + new_row_id, + }), + Action::SQLITE_DELETE => PreUpdateCase::Delete(PreUpdateOldValueAccessor { + db: sqlite, + old_row_id, + }), + Action::SQLITE_UPDATE => PreUpdateCase::Update { + old_value_accessor: PreUpdateOldValueAccessor { + db: sqlite, + old_row_id, + }, + new_value_accessor: PreUpdateNewValueAccessor { + db: sqlite, + new_row_id, + }, + }, + Action::UNKNOWN => PreUpdateCase::Unknown, + }; + + let _ = catch_unwind(|| { + let boxed_hook: *mut F = p_arg as *mut F; + (*boxed_hook)( + action, + expect_utf8(db_name, "database name"), + expect_utf8(tbl_name, "table name"), + &preupdate_case, + ); + }); + } + + let free_preupdate_hook = if hook.is_some() { + Some(free_boxed_hook:: as unsafe fn(*mut c_void)) + } else { + None + }; + + let previous_hook = match hook { + Some(hook) => { + let boxed_hook: *mut F = Box::into_raw(Box::new(hook)); + unsafe { + ffi::sqlite3_preupdate_hook( + self.db(), + Some(call_boxed_closure::), + boxed_hook as *mut _, + ) + } + } + _ => unsafe { ffi::sqlite3_preupdate_hook(self.db(), None, ptr::null_mut()) }, + }; + if !previous_hook.is_null() { + if let Some(free_boxed_hook) = self.free_preupdate_hook { + unsafe { free_boxed_hook(previous_hook) }; + } + } + self.free_preupdate_hook = free_preupdate_hook; + } +} + +#[cfg(test)] +mod test { + use super::super::Action; + use super::PreUpdateCase; + use crate::{Connection, Result}; + + #[test] + fn test_preupdate_hook_insert() -> Result<()> { + let db = Connection::open_in_memory()?; + + let mut called = false; + db.preupdate_hook(Some(|action, db: &str, tbl: &str, case: &PreUpdateCase| { + assert_eq!(Action::SQLITE_INSERT, action); + assert_eq!("main", db); + assert_eq!("foo", tbl); + match case { + PreUpdateCase::Insert(accessor) => { + assert_eq!(1, accessor.get_column_count()); + assert_eq!(1, accessor.get_new_row_id()); + assert_eq!(0, accessor.get_query_depth()); + assert_eq!("lisa", accessor.get_new_column_value(0).as_str().unwrap()); + assert_eq!(0, accessor.get_query_depth()); + } + _ => panic!("wrong preupdate case"), + } + called = true; + })); + db.execute_batch("CREATE TABLE foo (t TEXT)")?; + db.execute_batch("INSERT INTO foo VALUES ('lisa')")?; + assert!(called); + Ok(()) + } + + #[test] + fn test_preupdate_hook_delete() -> Result<()> { + let db = Connection::open_in_memory()?; + + let mut called = false; + + db.execute_batch("CREATE TABLE foo (t TEXT)")?; + db.execute_batch("INSERT INTO foo VALUES ('lisa')")?; + + db.preupdate_hook(Some(|action, db: &str, tbl: &str, case: &PreUpdateCase| { + assert_eq!(Action::SQLITE_DELETE, action); + assert_eq!("main", db); + assert_eq!("foo", tbl); + match case { + PreUpdateCase::Delete(accessor) => { + assert_eq!(1, accessor.get_column_count()); + assert_eq!(1, accessor.get_old_row_id()); + assert_eq!(0, accessor.get_query_depth()); + assert_eq!("lisa", accessor.get_old_column_value(0).as_str().unwrap()); + assert_eq!(0, accessor.get_query_depth()); + } + _ => panic!("wrong preupdate case"), + } + called = true; + })); + + db.execute_batch("DELETE from foo")?; + assert!(called); + Ok(()) + } + + #[test] + fn test_preupdate_hook_update() -> Result<()> { + let db = Connection::open_in_memory()?; + + let mut called = false; + + db.execute_batch("CREATE TABLE foo (t TEXT)")?; + db.execute_batch("INSERT INTO foo VALUES ('lisa')")?; + + db.preupdate_hook(Some(|action, db: &str, tbl: &str, case: &PreUpdateCase| { + assert_eq!(Action::SQLITE_UPDATE, action); + assert_eq!("main", db); + assert_eq!("foo", tbl); + match case { + PreUpdateCase::Update { + old_value_accessor, + new_value_accessor, + } => { + assert_eq!(1, old_value_accessor.get_column_count()); + assert_eq!(1, old_value_accessor.get_old_row_id()); + assert_eq!(0, old_value_accessor.get_query_depth()); + assert_eq!( + "lisa", + old_value_accessor.get_old_column_value(0).as_str().unwrap() + ); + assert_eq!(0, old_value_accessor.get_query_depth()); + + assert_eq!(1, new_value_accessor.get_column_count()); + assert_eq!(1, new_value_accessor.get_new_row_id()); + assert_eq!(0, new_value_accessor.get_query_depth()); + assert_eq!( + "janice", + new_value_accessor.get_new_column_value(0).as_str().unwrap() + ); + assert_eq!(0, new_value_accessor.get_query_depth()); + } + _ => panic!("wrong preupdate case"), + } + called = true; + })); + + db.execute_batch("UPDATE foo SET t = 'janice'")?; + assert!(called); + Ok(()) + } +} diff --git a/src/inner_connection.rs b/src/inner_connection.rs index edd4682..282eb5e 100644 --- a/src/inner_connection.rs +++ b/src/inner_connection.rs @@ -31,6 +31,10 @@ pub struct InnerConnection { pub free_update_hook: Option, #[cfg(feature = "hooks")] pub progress_handler: Option bool + Send>>, + #[cfg(feature = "hooks")] + pub authorizer: Option, + #[cfg(feature = "preupdate_hook")] + pub free_preupdate_hook: Option, owned: bool, } @@ -51,6 +55,10 @@ impl InnerConnection { free_update_hook: None, #[cfg(feature = "hooks")] progress_handler: None, + #[cfg(feature = "hooks")] + authorizer: None, + #[cfg(feature = "preupdate_hook")] + free_preupdate_hook: None, owned, } } @@ -333,6 +341,64 @@ impl InnerConnection { #[cfg(not(feature = "hooks"))] #[inline] fn remove_hooks(&mut self) {} + + #[cfg(not(feature = "preupdate_hook"))] + #[inline] + fn remove_preupdate_hook(&mut self) {} + + pub fn db_readonly(&self, db_name: super::DatabaseName<'_>) -> Result { + let name = db_name.as_cstring()?; + let r = unsafe { ffi::sqlite3_db_readonly(self.db, name.as_ptr()) }; + 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()), + )), + } + } + + #[cfg(feature = "modern_sqlite")] // 3.37.0 + pub fn txn_state( + &self, + db_name: Option>, + ) -> Result { + let r = if let Some(ref name) = db_name { + let name = name.as_cstring()?; + unsafe { ffi::sqlite3_txn_state(self.db, name.as_ptr()) } + } else { + unsafe { ffi::sqlite3_txn_state(self.db, ptr::null()) } + }; + match r { + 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()), + )), + } + } + + #[inline] + #[cfg(feature = "release_memory")] + pub fn release_memory(&self) -> Result<()> { + self.decode_result(unsafe { ffi::sqlite3_db_release_memory(self.db) }) + } + + #[cfg(feature = "modern_sqlite")] // 3.41.0 + pub fn is_interrupted(&self) -> bool { + unsafe { ffi::sqlite3_is_interrupted(self.db) == 1 } + } } impl Drop for InnerConnection { diff --git a/src/lib.rs b/src/lib.rs index a35eaab..7572908 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,7 +111,7 @@ mod context; #[cfg(feature = "functions")] #[cfg_attr(docsrs, doc(cfg(feature = "functions")))] pub mod functions; -#[cfg(any(feature = "hooks", feature = "preupdate_hook"))] +#[cfg(feature = "hooks")] #[cfg_attr(docsrs, doc(cfg(feature = "hooks")))] pub mod hooks; mod inner_connection;