mirror of
https://github.com/fluencelabs/sqlite-wasm-connector
synced 2025-03-15 06:20:50 +00:00
Add set_busy_handler
This commit is contained in:
parent
4ee6d75ce4
commit
87b237aa7d
@ -11,7 +11,12 @@ pub struct Database {
|
||||
phantom: PhantomData<raw::sqlite3>,
|
||||
}
|
||||
|
||||
/// A callback triggered for each row of an executed SQL query.
|
||||
/// A callback triggered when an operation fails due to concurrent activity. If
|
||||
/// the callback returns `true`, the operation will be repeated.
|
||||
pub type BusyCallback<'l> = FnMut(usize) -> bool + 'l;
|
||||
|
||||
/// A callback triggered for each row of an executed SQL query. If the callback
|
||||
/// returns `false`, no more rows will be processed.
|
||||
pub type ExecuteCallback<'l> = FnMut(Vec<(String, String)>) -> bool + 'l;
|
||||
|
||||
impl Database {
|
||||
@ -43,7 +48,6 @@ impl Database {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -52,6 +56,23 @@ impl Database {
|
||||
pub fn statement<'l>(&'l self, sql: &str) -> Result<Statement<'l>> {
|
||||
::statement::new(self, sql)
|
||||
}
|
||||
|
||||
/// Set a callback for handling failures due to concurrent activity.
|
||||
pub fn set_busy_handler(&mut self, callback: Option<&mut BusyCallback>) -> Result<()> {
|
||||
unsafe {
|
||||
match callback {
|
||||
Some(callback) => {
|
||||
let mut callback = Box::new(callback);
|
||||
success!(self, raw::sqlite3_busy_handler(self.raw, Some(busy_callback),
|
||||
&mut callback as *mut _ as *mut _));
|
||||
},
|
||||
None => {
|
||||
success!(self, raw::sqlite3_busy_handler(self.raw, None, 0 as *mut _));
|
||||
},
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Database {
|
||||
@ -66,6 +87,13 @@ pub fn as_raw(database: &Database) -> *mut raw::sqlite3 {
|
||||
database.raw
|
||||
}
|
||||
|
||||
extern fn busy_callback(callback: *mut c_void, attempts: c_int) -> c_int {
|
||||
unsafe {
|
||||
let ref mut callback = *(callback as *mut Box<&mut BusyCallback>);
|
||||
if callback(attempts as usize) { 1 } else { 0 }
|
||||
}
|
||||
}
|
||||
|
||||
extern fn execute_callback(callback: *mut c_void, count: c_int, values: *mut *mut c_char,
|
||||
columns: *mut *mut c_char) -> c_int {
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user