mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
Merge pull request #383 from gwenn/callbacks
Add constraints on callbacks
This commit is contained in:
commit
e72f50de53
@ -95,11 +95,11 @@ impl Connection {
|
|||||||
///
|
///
|
||||||
/// Will return `Err` if the destination path cannot be opened
|
/// Will return `Err` if the destination path cannot be opened
|
||||||
/// or if the restore fails.
|
/// or if the restore fails.
|
||||||
pub fn restore<P: AsRef<Path>>(
|
pub fn restore<P: AsRef<Path>, F: Fn(Progress)>(
|
||||||
&mut self,
|
&mut self,
|
||||||
name: DatabaseName,
|
name: DatabaseName,
|
||||||
src_path: P,
|
src_path: P,
|
||||||
progress: Option<fn(Progress)>,
|
progress: Option<F>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
use self::StepResult::{Busy, Done, Locked, More};
|
use self::StepResult::{Busy, Done, Locked, More};
|
||||||
let src = try!(Connection::open(src_path));
|
let src = try!(Connection::open(src_path));
|
||||||
@ -109,7 +109,7 @@ impl Connection {
|
|||||||
let mut busy_count = 0i32;
|
let mut busy_count = 0i32;
|
||||||
'restore_loop: while r == More || r == Busy {
|
'restore_loop: while r == More || r == Busy {
|
||||||
r = try!(restore.step(100));
|
r = try!(restore.step(100));
|
||||||
if let Some(f) = progress {
|
if let Some(ref f) = progress {
|
||||||
f(restore.progress());
|
f(restore.progress());
|
||||||
}
|
}
|
||||||
if r == Busy {
|
if r == Busy {
|
||||||
|
@ -230,7 +230,7 @@ impl Connection {
|
|||||||
x_func: F,
|
x_func: F,
|
||||||
) -> Result<()>
|
) -> Result<()>
|
||||||
where
|
where
|
||||||
F: FnMut(&Context) -> Result<T>,
|
F: FnMut(&Context) -> Result<T> + Send + 'static,
|
||||||
T: ToSql,
|
T: ToSql,
|
||||||
{
|
{
|
||||||
self.db
|
self.db
|
||||||
@ -281,7 +281,7 @@ impl InnerConnection {
|
|||||||
x_func: F,
|
x_func: F,
|
||||||
) -> Result<()>
|
) -> Result<()>
|
||||||
where
|
where
|
||||||
F: FnMut(&Context) -> Result<T>,
|
F: FnMut(&Context) -> Result<T> + Send + 'static,
|
||||||
T: ToSql,
|
T: ToSql,
|
||||||
{
|
{
|
||||||
unsafe extern "C" fn call_boxed_closure<F, T>(
|
unsafe extern "C" fn call_boxed_closure<F, T>(
|
||||||
|
37
src/hooks.rs
37
src/hooks.rs
@ -96,7 +96,7 @@ impl Connection {
|
|||||||
/// 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<F>(&self, hook: Option<F>)
|
||||||
where
|
where
|
||||||
F: FnMut() -> bool,
|
F: FnMut() -> bool + Send + 'static,
|
||||||
{
|
{
|
||||||
self.db.borrow_mut().commit_hook(hook);
|
self.db.borrow_mut().commit_hook(hook);
|
||||||
}
|
}
|
||||||
@ -106,7 +106,7 @@ impl Connection {
|
|||||||
/// 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<F>(&self, hook: Option<F>)
|
||||||
where
|
where
|
||||||
F: FnMut(),
|
F: FnMut() + Send + 'static,
|
||||||
{
|
{
|
||||||
self.db.borrow_mut().rollback_hook(hook);
|
self.db.borrow_mut().rollback_hook(hook);
|
||||||
}
|
}
|
||||||
@ -122,7 +122,7 @@ impl Connection {
|
|||||||
/// - 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<F>(&self, hook: Option<F>)
|
||||||
where
|
where
|
||||||
F: FnMut(Action, &str, &str, i64),
|
F: FnMut(Action, &str, &str, i64) + Send + 'static,
|
||||||
{
|
{
|
||||||
self.db.borrow_mut().update_hook(hook);
|
self.db.borrow_mut().update_hook(hook);
|
||||||
}
|
}
|
||||||
@ -137,7 +137,7 @@ impl InnerConnection {
|
|||||||
|
|
||||||
fn commit_hook<F>(&mut self, hook: Option<F>)
|
fn commit_hook<F>(&mut self, hook: Option<F>)
|
||||||
where
|
where
|
||||||
F: FnMut() -> bool,
|
F: FnMut() -> bool + Send + 'static,
|
||||||
{
|
{
|
||||||
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
|
||||||
@ -182,7 +182,7 @@ impl InnerConnection {
|
|||||||
|
|
||||||
fn rollback_hook<F>(&mut self, hook: Option<F>)
|
fn rollback_hook<F>(&mut self, hook: Option<F>)
|
||||||
where
|
where
|
||||||
F: FnMut(),
|
F: FnMut() + Send + 'static,
|
||||||
{
|
{
|
||||||
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
|
||||||
@ -221,7 +221,7 @@ impl InnerConnection {
|
|||||||
|
|
||||||
fn update_hook<F>(&mut self, hook: Option<F>)
|
fn update_hook<F>(&mut self, hook: Option<F>)
|
||||||
where
|
where
|
||||||
F: FnMut(Action, &str, &str, i64),
|
F: FnMut(Action, &str, &str, i64) + Send + 'static,
|
||||||
{
|
{
|
||||||
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,
|
||||||
@ -285,20 +285,23 @@ fn free_boxed_hook<F>(p: *mut c_void) {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::Action;
|
use super::Action;
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use Connection;
|
use Connection;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_commit_hook() {
|
fn test_commit_hook() {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
|
|
||||||
let mut called = false;
|
lazy_static! {
|
||||||
|
static ref called: AtomicBool = AtomicBool::new(false);
|
||||||
|
}
|
||||||
db.commit_hook(Some(|| {
|
db.commit_hook(Some(|| {
|
||||||
called = true;
|
called.store(true, Ordering::Relaxed);
|
||||||
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);
|
assert!(called.load(Ordering::Relaxed));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -318,29 +321,33 @@ mod test {
|
|||||||
fn test_rollback_hook() {
|
fn test_rollback_hook() {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
|
|
||||||
let mut called = false;
|
lazy_static! {
|
||||||
|
static ref called: AtomicBool = AtomicBool::new(false);
|
||||||
|
}
|
||||||
db.rollback_hook(Some(|| {
|
db.rollback_hook(Some(|| {
|
||||||
called = true;
|
called.store(true, Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); ROLLBACK;")
|
db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); ROLLBACK;")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(called);
|
assert!(called.load(Ordering::Relaxed));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_update_hook() {
|
fn test_update_hook() {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
|
|
||||||
let mut called = false;
|
lazy_static! {
|
||||||
|
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 = true;
|
called.store(true, Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
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);
|
assert!(called.load(Ordering::Relaxed));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user