mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-26 11:31:37 +08:00
Reduce required lifetime
Extends #825 to - create_collation - commit_hook - rollback_hook - update_hook - table_filter
This commit is contained in:
parent
9c954b8cb5
commit
926977846f
@ -15,9 +15,9 @@ unsafe extern "C" fn free_boxed_value<T>(p: *mut c_void) {
|
|||||||
|
|
||||||
impl Connection {
|
impl Connection {
|
||||||
/// `feature = "collation"` Add or modify a collation.
|
/// `feature = "collation"` Add or modify a collation.
|
||||||
pub fn create_collation<C>(&self, collation_name: &str, x_compare: C) -> Result<()>
|
pub fn create_collation<'c, C>(&'c self, collation_name: &str, x_compare: C) -> Result<()>
|
||||||
where
|
where
|
||||||
C: Fn(&str, &str) -> Ordering + Send + UnwindSafe + 'static,
|
C: Fn(&str, &str) -> Ordering + Send + UnwindSafe + 'c,
|
||||||
{
|
{
|
||||||
self.db
|
self.db
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
@ -39,9 +39,9 @@ impl Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl InnerConnection {
|
impl InnerConnection {
|
||||||
fn create_collation<C>(&mut self, collation_name: &str, x_compare: C) -> Result<()>
|
fn create_collation<'c, C>(&'c mut self, collation_name: &str, x_compare: C) -> Result<()>
|
||||||
where
|
where
|
||||||
C: Fn(&str, &str) -> Ordering + Send + UnwindSafe + 'static,
|
C: Fn(&str, &str) -> Ordering + Send + UnwindSafe + 'c,
|
||||||
{
|
{
|
||||||
unsafe extern "C" fn call_boxed_closure<C>(
|
unsafe extern "C" fn call_boxed_closure<C>(
|
||||||
arg1: *mut c_void,
|
arg1: *mut c_void,
|
||||||
|
@ -334,15 +334,15 @@ impl Connection {
|
|||||||
/// # Failure
|
/// # Failure
|
||||||
///
|
///
|
||||||
/// Will return Err if the function could not be attached to the connection.
|
/// Will return Err if the function could not be attached to the connection.
|
||||||
pub fn create_scalar_function<F, T>(
|
pub fn create_scalar_function<'c, F, T>(
|
||||||
&self,
|
&'c self,
|
||||||
fn_name: &str,
|
fn_name: &str,
|
||||||
n_arg: c_int,
|
n_arg: c_int,
|
||||||
flags: FunctionFlags,
|
flags: FunctionFlags,
|
||||||
x_func: F,
|
x_func: F,
|
||||||
) -> Result<()>
|
) -> Result<()>
|
||||||
where
|
where
|
||||||
F: FnMut(&Context<'_>) -> Result<T> + Send + UnwindSafe + 'static,
|
F: FnMut(&Context<'_>) -> Result<T> + Send + UnwindSafe + 'c,
|
||||||
T: ToSql,
|
T: ToSql,
|
||||||
{
|
{
|
||||||
self.db
|
self.db
|
||||||
@ -411,15 +411,15 @@ impl Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl InnerConnection {
|
impl InnerConnection {
|
||||||
fn create_scalar_function<F, T>(
|
fn create_scalar_function<'c, F, T>(
|
||||||
&mut self,
|
&'c mut self,
|
||||||
fn_name: &str,
|
fn_name: &str,
|
||||||
n_arg: c_int,
|
n_arg: c_int,
|
||||||
flags: FunctionFlags,
|
flags: FunctionFlags,
|
||||||
x_func: F,
|
x_func: F,
|
||||||
) -> Result<()>
|
) -> Result<()>
|
||||||
where
|
where
|
||||||
F: FnMut(&Context<'_>) -> Result<T> + Send + UnwindSafe + 'static,
|
F: FnMut(&Context<'_>) -> Result<T> + Send + UnwindSafe + 'c,
|
||||||
T: ToSql,
|
T: ToSql,
|
||||||
{
|
{
|
||||||
unsafe extern "C" fn call_boxed_closure<F, T>(
|
unsafe extern "C" fn call_boxed_closure<F, T>(
|
||||||
|
48
src/hooks.rs
48
src/hooks.rs
@ -40,9 +40,9 @@ impl Connection {
|
|||||||
/// a transaction is committed.
|
/// a transaction is committed.
|
||||||
///
|
///
|
||||||
/// The callback returns `true` to rollback.
|
/// The callback returns `true` to rollback.
|
||||||
pub fn commit_hook<F>(&self, hook: Option<F>)
|
pub fn commit_hook<'c, F>(&'c self, hook: Option<F>)
|
||||||
where
|
where
|
||||||
F: FnMut() -> bool + Send + 'static,
|
F: FnMut() -> bool + Send + 'c,
|
||||||
{
|
{
|
||||||
self.db.borrow_mut().commit_hook(hook);
|
self.db.borrow_mut().commit_hook(hook);
|
||||||
}
|
}
|
||||||
@ -51,9 +51,9 @@ impl Connection {
|
|||||||
/// a transaction is committed.
|
/// a transaction is committed.
|
||||||
///
|
///
|
||||||
/// The callback returns `true` to rollback.
|
/// The callback returns `true` to rollback.
|
||||||
pub fn rollback_hook<F>(&self, hook: Option<F>)
|
pub fn rollback_hook<'c, F>(&'c self, hook: Option<F>)
|
||||||
where
|
where
|
||||||
F: FnMut() + Send + 'static,
|
F: FnMut() + Send + 'c,
|
||||||
{
|
{
|
||||||
self.db.borrow_mut().rollback_hook(hook);
|
self.db.borrow_mut().rollback_hook(hook);
|
||||||
}
|
}
|
||||||
@ -68,9 +68,9 @@ impl Connection {
|
|||||||
/// - the name of the database ("main", "temp", ...),
|
/// - the name of the database ("main", "temp", ...),
|
||||||
/// - the name of the table that is updated,
|
/// - the name of the table that is updated,
|
||||||
/// - the ROWID of the row that is updated.
|
/// - the ROWID of the row that is updated.
|
||||||
pub fn update_hook<F>(&self, hook: Option<F>)
|
pub fn update_hook<'c, F>(&'c self, hook: Option<F>)
|
||||||
where
|
where
|
||||||
F: FnMut(Action, &str, &str, i64) + Send + 'static,
|
F: FnMut(Action, &str, &str, i64) + Send + 'c,
|
||||||
{
|
{
|
||||||
self.db.borrow_mut().update_hook(hook);
|
self.db.borrow_mut().update_hook(hook);
|
||||||
}
|
}
|
||||||
@ -97,9 +97,9 @@ impl InnerConnection {
|
|||||||
self.progress_handler(0, None::<fn() -> bool>);
|
self.progress_handler(0, None::<fn() -> bool>);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn commit_hook<F>(&mut self, hook: Option<F>)
|
fn commit_hook<'c, F>(&'c mut self, hook: Option<F>)
|
||||||
where
|
where
|
||||||
F: FnMut() -> bool + Send + 'static,
|
F: FnMut() -> bool + Send + 'c,
|
||||||
{
|
{
|
||||||
unsafe extern "C" fn call_boxed_closure<F>(p_arg: *mut c_void) -> c_int
|
unsafe extern "C" fn call_boxed_closure<F>(p_arg: *mut c_void) -> c_int
|
||||||
where
|
where
|
||||||
@ -146,9 +146,9 @@ impl InnerConnection {
|
|||||||
self.free_commit_hook = free_commit_hook;
|
self.free_commit_hook = free_commit_hook;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rollback_hook<F>(&mut self, hook: Option<F>)
|
fn rollback_hook<'c, F>(&'c mut self, hook: Option<F>)
|
||||||
where
|
where
|
||||||
F: FnMut() + Send + 'static,
|
F: FnMut() + Send + 'c,
|
||||||
{
|
{
|
||||||
unsafe extern "C" fn call_boxed_closure<F>(p_arg: *mut c_void)
|
unsafe extern "C" fn call_boxed_closure<F>(p_arg: *mut c_void)
|
||||||
where
|
where
|
||||||
@ -187,9 +187,9 @@ impl InnerConnection {
|
|||||||
self.free_rollback_hook = free_rollback_hook;
|
self.free_rollback_hook = free_rollback_hook;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_hook<F>(&mut self, hook: Option<F>)
|
fn update_hook<'c, F>(&'c mut self, hook: Option<F>)
|
||||||
where
|
where
|
||||||
F: FnMut(Action, &str, &str, i64) + Send + 'static,
|
F: FnMut(Action, &str, &str, i64) + Send + 'c,
|
||||||
{
|
{
|
||||||
unsafe extern "C" fn call_boxed_closure<F>(
|
unsafe extern "C" fn call_boxed_closure<F>(
|
||||||
p_arg: *mut c_void,
|
p_arg: *mut c_void,
|
||||||
@ -306,16 +306,14 @@ mod test {
|
|||||||
fn test_commit_hook() {
|
fn test_commit_hook() {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
|
|
||||||
lazy_static! {
|
let mut called = false;
|
||||||
static ref CALLED: AtomicBool = AtomicBool::new(false);
|
|
||||||
}
|
|
||||||
db.commit_hook(Some(|| {
|
db.commit_hook(Some(|| {
|
||||||
CALLED.store(true, Ordering::Relaxed);
|
called = true;
|
||||||
false
|
false
|
||||||
}));
|
}));
|
||||||
db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;")
|
db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(CALLED.load(Ordering::Relaxed));
|
assert!(called);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -335,34 +333,30 @@ mod test {
|
|||||||
fn test_rollback_hook() {
|
fn test_rollback_hook() {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
|
|
||||||
lazy_static! {
|
let mut called = false;
|
||||||
static ref CALLED: AtomicBool = AtomicBool::new(false);
|
|
||||||
}
|
|
||||||
db.rollback_hook(Some(|| {
|
db.rollback_hook(Some(|| {
|
||||||
CALLED.store(true, Ordering::Relaxed);
|
called = true;
|
||||||
}));
|
}));
|
||||||
db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); ROLLBACK;")
|
db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); ROLLBACK;")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(CALLED.load(Ordering::Relaxed));
|
assert!(called);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_update_hook() {
|
fn test_update_hook() {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
|
|
||||||
lazy_static! {
|
let mut called = false;
|
||||||
static ref CALLED: AtomicBool = AtomicBool::new(false);
|
|
||||||
}
|
|
||||||
db.update_hook(Some(|action, db: &str, tbl: &str, row_id| {
|
db.update_hook(Some(|action, db: &str, tbl: &str, row_id| {
|
||||||
assert_eq!(Action::SQLITE_INSERT, action);
|
assert_eq!(Action::SQLITE_INSERT, action);
|
||||||
assert_eq!("main", db);
|
assert_eq!("main", db);
|
||||||
assert_eq!("foo", tbl);
|
assert_eq!("foo", tbl);
|
||||||
assert_eq!(1, row_id);
|
assert_eq!(1, row_id);
|
||||||
CALLED.store(true, Ordering::Relaxed);
|
called = true;
|
||||||
}));
|
}));
|
||||||
db.execute_batch("CREATE TABLE foo (t TEXT)").unwrap();
|
db.execute_batch("CREATE TABLE foo (t TEXT)").unwrap();
|
||||||
db.execute_batch("INSERT INTO foo VALUES ('lisa')").unwrap();
|
db.execute_batch("INSERT INTO foo VALUES ('lisa')").unwrap();
|
||||||
assert!(CALLED.load(Ordering::Relaxed));
|
assert!(called);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -53,9 +53,9 @@ impl Session<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set a table filter
|
/// Set a table filter
|
||||||
pub fn table_filter<F>(&mut self, filter: Option<F>)
|
pub fn table_filter<'s, F>(&'s mut self, filter: Option<F>)
|
||||||
where
|
where
|
||||||
F: Fn(&str) -> bool + Send + RefUnwindSafe + 'static,
|
F: Fn(&str) -> bool + Send + RefUnwindSafe + 's,
|
||||||
{
|
{
|
||||||
unsafe extern "C" fn call_boxed_closure<F>(
|
unsafe extern "C" fn call_boxed_closure<F>(
|
||||||
p_arg: *mut c_void,
|
p_arg: *mut c_void,
|
||||||
|
Loading…
Reference in New Issue
Block a user