mirror of
https://github.com/isar/rusqlite.git
synced 2025-04-09 00:37:49 +08:00
Extracted the preupdate_hook to a separate cargo feature.
Moved hooks and preupdate_hook into their own modules inside hooks.rs Also created an initial way to access the functions that are available during the callback.
This commit is contained in:
parent
d88f49f830
commit
ceff6cb8b1
@ -37,7 +37,8 @@ trace = ["libsqlite3-sys/min_sqlite_version_3_6_23"]
|
|||||||
bundled = ["libsqlite3-sys/bundled", "modern_sqlite"]
|
bundled = ["libsqlite3-sys/bundled", "modern_sqlite"]
|
||||||
buildtime_bindgen = ["libsqlite3-sys/buildtime_bindgen"]
|
buildtime_bindgen = ["libsqlite3-sys/buildtime_bindgen"]
|
||||||
limits = []
|
limits = []
|
||||||
hooks = ["libsqlite3-sys/preupdate_hook"]
|
hooks = []
|
||||||
|
preupdate_hook = ["libsqlite3-sys/preupdate_hook"]
|
||||||
i128_blob = ["byteorder"]
|
i128_blob = ["byteorder"]
|
||||||
sqlcipher = ["libsqlite3-sys/sqlcipher"]
|
sqlcipher = ["libsqlite3-sys/sqlcipher"]
|
||||||
unlock_notify = ["libsqlite3-sys/unlock_notify"]
|
unlock_notify = ["libsqlite3-sys/unlock_notify"]
|
||||||
|
296
src/hooks.rs
296
src/hooks.rs
@ -1,14 +1,9 @@
|
|||||||
//! `feature = "hooks"` Commit, Data Change and Rollback Notification Callbacks
|
//! `feature = "hooks"` Commit, Data Change and Rollback Notification Callbacks
|
||||||
#![allow(non_camel_case_types)]
|
#![allow(non_camel_case_types)]
|
||||||
|
use std::os::raw::c_void;
|
||||||
use std::os::raw::{c_char, c_int, c_void};
|
|
||||||
use std::panic::{catch_unwind, RefUnwindSafe};
|
|
||||||
use std::ptr;
|
|
||||||
|
|
||||||
use crate::ffi;
|
use crate::ffi;
|
||||||
|
|
||||||
use crate::{Connection, InnerConnection};
|
|
||||||
|
|
||||||
/// `feature = "hooks"` Action Codes
|
/// `feature = "hooks"` Action Codes
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
#[repr(i32)]
|
#[repr(i32)]
|
||||||
@ -36,6 +31,197 @@ impl From<i32> for Action {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe fn free_boxed_hook<F>(p: *mut c_void) {
|
||||||
|
drop(Box::from_raw(p as *mut F));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "preupdate_hook")]
|
||||||
|
mod preupdate_hook {
|
||||||
|
use super::free_boxed_hook;
|
||||||
|
use super::Action;
|
||||||
|
|
||||||
|
use std::os::raw::{c_char, c_int, c_void};
|
||||||
|
use std::panic::catch_unwind;
|
||||||
|
use std::ptr;
|
||||||
|
|
||||||
|
use crate::ffi;
|
||||||
|
use crate::types::ValueRef;
|
||||||
|
use crate::{Connection, InnerConnection};
|
||||||
|
|
||||||
|
// TODO: how to allow user access to these functions, since they should be only accessible in
|
||||||
|
// the scope of a preupdate_hook callback.
|
||||||
|
pub struct PreUpdateHookFunctions {
|
||||||
|
db: *mut ffi::sqlite3,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PreUpdateHookFunctions {
|
||||||
|
pub unsafe fn get_count(&self) -> i32 {
|
||||||
|
ffi::sqlite3_preupdate_count(self.db)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe fn get_old(&self, i: i32) -> ValueRef {
|
||||||
|
let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut();
|
||||||
|
ffi::sqlite3_preupdate_old(self.db, i, &mut p_value);
|
||||||
|
ValueRef::from_value(p_value)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe fn get_new(&self, i: i32) -> ValueRef {
|
||||||
|
let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut();
|
||||||
|
ffi::sqlite3_preupdate_new(self.db, i, &mut p_value);
|
||||||
|
ValueRef::from_value(p_value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Connection {
|
||||||
|
///
|
||||||
|
/// `feature = "preupdate_hook"` Register a callback function to be invoked before
|
||||||
|
/// 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,
|
||||||
|
/// - for an update or delete, the initial ROWID of the row that is going to be updated/deleted. It is undefined for inserts.
|
||||||
|
/// - for an update or insert, the final ROWID of the row that is going to be updated/inserted. It is undefined for deletes.
|
||||||
|
#[inline]
|
||||||
|
pub fn preupdate_hook<'c, F>(&'c self, hook: Option<F>)
|
||||||
|
where
|
||||||
|
F: FnMut(Action, &str, &str, i64, i64, &PreUpdateHookFunctions) + Send + 'c,
|
||||||
|
{
|
||||||
|
self.db.borrow_mut().preupdate_hook(hook);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InnerConnection {
|
||||||
|
#[inline]
|
||||||
|
pub fn remove_preupdate_hook(&mut self) {
|
||||||
|
self.preupdate_hook(None::<fn(Action, &str, &str, i64, i64, &PreUpdateHookFunctions)>);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn preupdate_hook<'c, F>(&'c mut self, hook: Option<F>)
|
||||||
|
where
|
||||||
|
F: FnMut(Action, &str, &str, i64, i64, &PreUpdateHookFunctions) + Send + 'c,
|
||||||
|
{
|
||||||
|
unsafe extern "C" fn call_boxed_closure<F>(
|
||||||
|
p_arg: *mut c_void,
|
||||||
|
sqlite: *mut ffi::sqlite3,
|
||||||
|
action_code: c_int,
|
||||||
|
db_str: *const c_char,
|
||||||
|
tbl_str: *const c_char,
|
||||||
|
row_id: i64,
|
||||||
|
new_row_id: i64,
|
||||||
|
) where
|
||||||
|
F: FnMut(Action, &str, &str, i64, i64, &PreUpdateHookFunctions),
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: how to properly allow a user to use the functions
|
||||||
|
// (sqlite3_preupdate_old,...) that are only in scope
|
||||||
|
// during the callback?
|
||||||
|
// Also how to pass in the rowids, because they can be undefined based on the
|
||||||
|
// action.
|
||||||
|
let preupdate_hook_functions = PreUpdateHookFunctions { db: sqlite };
|
||||||
|
|
||||||
|
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,
|
||||||
|
new_row_id,
|
||||||
|
&preupdate_hook_functions,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let free_preupdate_hook = if hook.is_some() {
|
||||||
|
Some(free_boxed_hook::<F> 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::<F>),
|
||||||
|
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::PreUpdateHookFunctions;
|
||||||
|
use crate::{Connection, Result};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_preupdate_hook() -> Result<()> {
|
||||||
|
let db = Connection::open_in_memory()?;
|
||||||
|
|
||||||
|
let mut called = false;
|
||||||
|
db.preupdate_hook(Some(
|
||||||
|
|action,
|
||||||
|
db: &str,
|
||||||
|
tbl: &str,
|
||||||
|
row_id,
|
||||||
|
new_row_id,
|
||||||
|
_func: &PreUpdateHookFunctions| {
|
||||||
|
assert_eq!(Action::SQLITE_INSERT, action);
|
||||||
|
assert_eq!("main", db);
|
||||||
|
assert_eq!("foo", tbl);
|
||||||
|
assert_eq!(1, row_id);
|
||||||
|
assert_eq!(1, new_row_id);
|
||||||
|
called = true;
|
||||||
|
},
|
||||||
|
));
|
||||||
|
db.execute_batch("CREATE TABLE foo (t TEXT)")?;
|
||||||
|
db.execute_batch("INSERT INTO foo VALUES ('lisa')")?;
|
||||||
|
assert!(called);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "hooks")]
|
||||||
|
mod datachanged_and_friends {
|
||||||
|
use super::free_boxed_hook;
|
||||||
|
use super::Action;
|
||||||
|
|
||||||
|
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};
|
||||||
|
|
||||||
impl Connection {
|
impl Connection {
|
||||||
/// `feature = "hooks"` Register a callback function to be invoked whenever
|
/// `feature = "hooks"` Register a callback function to be invoked whenever
|
||||||
/// a transaction is committed.
|
/// a transaction is committed.
|
||||||
@ -78,25 +264,6 @@ impl Connection {
|
|||||||
{
|
{
|
||||||
self.db.borrow_mut().update_hook(hook);
|
self.db.borrow_mut().update_hook(hook);
|
||||||
}
|
}
|
||||||
///
|
|
||||||
/// `feature = "hooks"` Register a callback function to be invoked before
|
|
||||||
/// 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,
|
|
||||||
/// - for an update or delete, the initial ROWID of the row that is going to be updated/deleted. It is undefined for inserts.
|
|
||||||
/// - for an update or insert, the final ROWID of the row that is going to be updated/inserted. It is undefined for deletes.
|
|
||||||
#[inline]
|
|
||||||
pub fn preupdate_hook<'c, F>(&'c self, hook: Option<F>)
|
|
||||||
where
|
|
||||||
F: FnMut(Action, &str, &str, i64, i64) + Send + 'c,
|
|
||||||
{
|
|
||||||
self.db.borrow_mut().preupdate_hook(hook);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `feature = "hooks"` Register a query progress callback.
|
/// `feature = "hooks"` Register a query progress callback.
|
||||||
///
|
///
|
||||||
@ -118,7 +285,6 @@ impl InnerConnection {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn remove_hooks(&mut self) {
|
pub fn remove_hooks(&mut self) {
|
||||||
self.update_hook(None::<fn(Action, &str, &str, i64)>);
|
self.update_hook(None::<fn(Action, &str, &str, i64)>);
|
||||||
self.preupdate_hook(None::<fn(Action, &str, &str, i64, i64)>);
|
|
||||||
self.commit_hook(None::<fn() -> bool>);
|
self.commit_hook(None::<fn() -> bool>);
|
||||||
self.rollback_hook(None::<fn()>);
|
self.rollback_hook(None::<fn()>);
|
||||||
self.progress_handler(0, None::<fn() -> bool>);
|
self.progress_handler(0, None::<fn() -> bool>);
|
||||||
@ -278,73 +444,6 @@ impl InnerConnection {
|
|||||||
self.free_update_hook = free_update_hook;
|
self.free_update_hook = free_update_hook;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn preupdate_hook<'c, F>(&'c mut self, hook: Option<F>)
|
|
||||||
where
|
|
||||||
F: FnMut(Action, &str, &str, i64, i64) + Send + 'c,
|
|
||||||
{
|
|
||||||
unsafe extern "C" fn call_boxed_closure<F>(
|
|
||||||
p_arg: *mut c_void,
|
|
||||||
_sqlite: *mut ffi::sqlite3,
|
|
||||||
action_code: c_int,
|
|
||||||
db_str: *const c_char,
|
|
||||||
tbl_str: *const c_char,
|
|
||||||
row_id: i64,
|
|
||||||
new_row_id: i64,
|
|
||||||
) where
|
|
||||||
F: FnMut(Action, &str, &str, i64, 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,
|
|
||||||
new_row_id,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let free_preupdate_hook = if hook.is_some() {
|
|
||||||
Some(free_boxed_hook::<F> 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::<F>),
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn progress_handler<F>(&mut self, num_ops: c_int, handler: Option<F>)
|
fn progress_handler<F>(&mut self, num_ops: c_int, handler: Option<F>)
|
||||||
where
|
where
|
||||||
F: FnMut() -> bool + Send + RefUnwindSafe + 'static,
|
F: FnMut() -> bool + Send + RefUnwindSafe + 'static,
|
||||||
@ -378,20 +477,18 @@ impl InnerConnection {
|
|||||||
self.progress_handler = Some(boxed_handler);
|
self.progress_handler = Some(boxed_handler);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
unsafe { ffi::sqlite3_progress_handler(self.db(), num_ops, None, ptr::null_mut()) }
|
unsafe {
|
||||||
|
ffi::sqlite3_progress_handler(self.db(), num_ops, None, ptr::null_mut())
|
||||||
|
}
|
||||||
self.progress_handler = None;
|
self.progress_handler = None;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn free_boxed_hook<F>(p: *mut c_void) {
|
|
||||||
drop(Box::from_raw(p as *mut F));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::Action;
|
use super::super::Action;
|
||||||
use crate::{Connection, Result};
|
use crate::{Connection, Result};
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
|
||||||
@ -485,3 +582,4 @@ mod test {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -33,7 +33,7 @@ pub struct InnerConnection {
|
|||||||
pub free_update_hook: Option<unsafe fn(*mut ::std::os::raw::c_void)>,
|
pub free_update_hook: Option<unsafe fn(*mut ::std::os::raw::c_void)>,
|
||||||
#[cfg(feature = "hooks")]
|
#[cfg(feature = "hooks")]
|
||||||
pub progress_handler: Option<Box<dyn FnMut() -> bool + Send>>,
|
pub progress_handler: Option<Box<dyn FnMut() -> bool + Send>>,
|
||||||
#[cfg(feature = "hooks")]
|
#[cfg(feature = "preupdate_hook")]
|
||||||
pub free_preupdate_hook: Option<unsafe fn(*mut ::std::os::raw::c_void)>,
|
pub free_preupdate_hook: Option<unsafe fn(*mut ::std::os::raw::c_void)>,
|
||||||
owned: bool,
|
owned: bool,
|
||||||
}
|
}
|
||||||
@ -52,9 +52,9 @@ impl InnerConnection {
|
|||||||
#[cfg(feature = "hooks")]
|
#[cfg(feature = "hooks")]
|
||||||
free_update_hook: None,
|
free_update_hook: None,
|
||||||
#[cfg(feature = "hooks")]
|
#[cfg(feature = "hooks")]
|
||||||
free_preupdate_hook: None,
|
|
||||||
#[cfg(feature = "hooks")]
|
|
||||||
progress_handler: None,
|
progress_handler: None,
|
||||||
|
#[cfg(feature = "preupdate_hook")]
|
||||||
|
free_preupdate_hook: None,
|
||||||
owned,
|
owned,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -155,6 +155,7 @@ impl InnerConnection {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
self.remove_hooks();
|
self.remove_hooks();
|
||||||
|
self.remove_preupdate_hook();
|
||||||
let mut shared_handle = self.interrupt_lock.lock().unwrap();
|
let mut shared_handle = self.interrupt_lock.lock().unwrap();
|
||||||
assert!(
|
assert!(
|
||||||
!shared_handle.is_null(),
|
!shared_handle.is_null(),
|
||||||
@ -305,6 +306,10 @@ impl InnerConnection {
|
|||||||
#[cfg(not(feature = "hooks"))]
|
#[cfg(not(feature = "hooks"))]
|
||||||
#[inline]
|
#[inline]
|
||||||
fn remove_hooks(&mut self) {}
|
fn remove_hooks(&mut self) {}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "preupdate_hook"))]
|
||||||
|
#[inline]
|
||||||
|
fn remove_preupdate_hook(&mut self) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for InnerConnection {
|
impl Drop for InnerConnection {
|
||||||
|
@ -101,8 +101,8 @@ pub mod config;
|
|||||||
mod context;
|
mod context;
|
||||||
#[cfg(feature = "functions")]
|
#[cfg(feature = "functions")]
|
||||||
pub mod functions;
|
pub mod functions;
|
||||||
#[cfg(feature = "hooks")]
|
#[cfg(any(feature = "hooks", feature = "preupdate_hook"))]
|
||||||
mod hooks;
|
pub mod hooks;
|
||||||
mod inner_connection;
|
mod inner_connection;
|
||||||
#[cfg(feature = "limits")]
|
#[cfg(feature = "limits")]
|
||||||
pub mod limits;
|
pub mod limits;
|
||||||
|
@ -133,7 +133,12 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "functions", feature = "session", feature = "vtab"))]
|
#[cfg(any(
|
||||||
|
feature = "functions",
|
||||||
|
feature = "session",
|
||||||
|
feature = "vtab",
|
||||||
|
feature = "preupdate_hook"
|
||||||
|
))]
|
||||||
impl<'a> ValueRef<'a> {
|
impl<'a> ValueRef<'a> {
|
||||||
pub(crate) unsafe fn from_value(value: *mut crate::ffi::sqlite3_value) -> ValueRef<'a> {
|
pub(crate) unsafe fn from_value(value: *mut crate::ffi::sqlite3_value) -> ValueRef<'a> {
|
||||||
use crate::ffi;
|
use crate::ffi;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user