2015-05-29 09:44:06 -04:00
|
|
|
use libc::{c_char, c_int, c_void};
|
|
|
|
use raw;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::path::Path;
|
|
|
|
|
2015-05-29 11:24:01 -04:00
|
|
|
use {Result, Statement};
|
2015-05-29 09:44:06 -04:00
|
|
|
|
|
|
|
/// A database.
|
2015-06-01 11:39:22 -04:00
|
|
|
pub struct Database {
|
2015-05-29 11:24:01 -04:00
|
|
|
raw: *mut raw::sqlite3,
|
2015-06-01 11:39:22 -04:00
|
|
|
phantom: PhantomData<raw::sqlite3>,
|
2015-05-29 09:44:06 -04:00
|
|
|
}
|
|
|
|
|
2015-06-07 22:15:59 -04:00
|
|
|
/// A callback triggered for each row of an executed SQL query. If the callback
|
|
|
|
/// returns `false`, no more rows will be processed.
|
2015-05-29 11:24:01 -04:00
|
|
|
pub type ExecuteCallback<'l> = FnMut(Vec<(String, String)>) -> bool + 'l;
|
2015-05-29 09:44:06 -04:00
|
|
|
|
2015-06-01 11:39:22 -04:00
|
|
|
impl Database {
|
2015-06-08 07:53:28 -04:00
|
|
|
/// Open a database connect.
|
2015-06-01 11:39:22 -04:00
|
|
|
pub fn open(path: &Path) -> Result<Database> {
|
2015-05-29 11:24:01 -04:00
|
|
|
let mut raw = 0 as *mut _;
|
2015-05-29 09:44:06 -04:00
|
|
|
unsafe {
|
2015-05-29 11:24:01 -04:00
|
|
|
success!(raw::sqlite3_open(path_to_c_str!(path), &mut raw));
|
2015-05-29 09:44:06 -04:00
|
|
|
}
|
2015-05-30 10:31:41 -04:00
|
|
|
Ok(Database { raw: raw, phantom: PhantomData })
|
2015-05-29 09:44:06 -04:00
|
|
|
}
|
|
|
|
|
2015-06-08 07:53:28 -04:00
|
|
|
/// Execute an SQL query.
|
2015-06-01 16:27:09 -04:00
|
|
|
pub fn execute<'l>(&self, sql: &str, callback: Option<&mut ExecuteCallback<'l>>)
|
2015-06-03 13:04:12 -04:00
|
|
|
-> Result<()> {
|
2015-05-29 09:44:06 -04:00
|
|
|
|
2015-06-08 07:53:28 -04:00
|
|
|
match callback {
|
|
|
|
Some(callback) => unsafe {
|
|
|
|
let mut callback = Box::new(callback);
|
2015-06-08 11:28:43 -04:00
|
|
|
success!(self, raw::sqlite3_exec(self.raw, str_to_c_str!(sql),
|
|
|
|
Some(execute_callback),
|
|
|
|
&mut callback as *mut _ as *mut _,
|
|
|
|
0 as *mut _));
|
2015-06-08 07:53:28 -04:00
|
|
|
},
|
|
|
|
None => unsafe {
|
|
|
|
success!(self, raw::sqlite3_exec(self.raw, str_to_c_str!(sql), None,
|
|
|
|
0 as *mut _, 0 as *mut _));
|
|
|
|
},
|
2015-05-29 09:44:06 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-05-29 11:24:01 -04:00
|
|
|
|
2015-06-08 07:53:28 -04:00
|
|
|
/// Compile an SQL statement.
|
2015-05-29 16:58:48 -04:00
|
|
|
#[inline]
|
2015-06-08 07:53:28 -04:00
|
|
|
pub fn prepare<'l>(&'l self, sql: &str) -> Result<Statement<'l>> {
|
2015-05-29 16:58:48 -04:00
|
|
|
::statement::new(self, sql)
|
2015-05-29 11:24:01 -04:00
|
|
|
}
|
2015-06-07 22:15:59 -04:00
|
|
|
|
2015-06-08 07:53:28 -04:00
|
|
|
/// Set a callback to handle rejected operations when the database is busy.
|
2015-06-08 11:28:43 -04:00
|
|
|
///
|
|
|
|
/// The callback is triggered when the database cannot perform an operation
|
|
|
|
/// due to processing of a request from another client. If the callback
|
|
|
|
/// returns `true`, the operation will be repeated.
|
|
|
|
pub fn set_busy_handler<F: FnMut(usize) -> bool>(&mut self, callback: Option<F>)
|
|
|
|
-> Result<()> {
|
|
|
|
|
2015-06-08 07:53:28 -04:00
|
|
|
match callback {
|
|
|
|
Some(callback) => unsafe {
|
2015-06-08 11:28:43 -04:00
|
|
|
use std::mem::transmute;
|
|
|
|
let callback = Box::new(callback);
|
|
|
|
success!(raw::sqlite3_busy_handler(self.raw, Some(busy_callback::<F>),
|
|
|
|
transmute::<_, *mut c_void>(callback)));
|
2015-06-08 07:53:28 -04:00
|
|
|
},
|
|
|
|
None => unsafe {
|
2015-06-08 11:28:43 -04:00
|
|
|
success!(raw::sqlite3_busy_handler(self.raw, None, 0 as *mut _));
|
2015-06-08 07:53:28 -04:00
|
|
|
},
|
2015-06-07 22:15:59 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
2015-06-08 07:53:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set a timeout before rejecting an operation when the database is busy.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_busy_timeout(&mut self, milliseconds: usize) -> Result<()> {
|
|
|
|
unsafe { success!(self, raw::sqlite3_busy_timeout(self.raw, milliseconds as c_int)) };
|
|
|
|
Ok(())
|
2015-06-07 22:15:59 -04:00
|
|
|
}
|
2015-05-29 09:44:06 -04:00
|
|
|
}
|
|
|
|
|
2015-06-01 11:39:22 -04:00
|
|
|
impl Drop for Database {
|
2015-05-29 09:44:06 -04:00
|
|
|
#[inline]
|
|
|
|
fn drop(&mut self) {
|
2015-05-29 14:15:01 -04:00
|
|
|
unsafe { raw::sqlite3_close(self.raw) };
|
2015-05-29 09:44:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 14:15:01 -04:00
|
|
|
#[inline]
|
2015-06-01 16:27:09 -04:00
|
|
|
pub fn as_raw(database: &Database) -> *mut raw::sqlite3 {
|
2015-05-29 14:15:01 -04:00
|
|
|
database.raw
|
|
|
|
}
|
|
|
|
|
2015-06-08 11:28:43 -04:00
|
|
|
extern fn busy_callback<F: FnMut(usize) -> bool>(callback: *mut c_void, attempts: c_int) -> c_int {
|
|
|
|
if unsafe { (*(callback as *mut F))(attempts as usize) } { 1 } else { 0 }
|
2015-06-07 22:15:59 -04:00
|
|
|
}
|
|
|
|
|
2015-05-29 09:44:06 -04:00
|
|
|
extern fn execute_callback(callback: *mut c_void, count: c_int, values: *mut *mut c_char,
|
|
|
|
columns: *mut *mut c_char) -> c_int {
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut pairs = Vec::with_capacity(count as usize);
|
|
|
|
|
|
|
|
for i in 0..(count as isize) {
|
2015-05-29 13:08:02 -04:00
|
|
|
let column = c_str_to_string!(*columns.offset(i));
|
|
|
|
let value = c_str_to_string!(*values.offset(i));
|
2015-05-29 09:44:06 -04:00
|
|
|
pairs.push((column, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
let ref mut callback = *(callback as *mut Box<&mut ExecuteCallback>);
|
|
|
|
if callback(pairs) { 0 } else { 1 }
|
|
|
|
}
|
|
|
|
}
|
2015-06-08 09:37:44 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Database;
|
|
|
|
use tests::setup;
|
|
|
|
|
|
|
|
macro_rules! ok(
|
|
|
|
($result:expr) => ($result.unwrap());
|
|
|
|
);
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute() {
|
|
|
|
let (path, _directory) = setup();
|
|
|
|
let database = ok!(Database::open(&path));
|
|
|
|
match database.execute(":)", None) {
|
|
|
|
Err(error) => assert_eq!(error.message, Some(String::from(r#"unrecognized token: ":""#))),
|
|
|
|
_ => assert!(false),
|
|
|
|
}
|
|
|
|
}
|
2015-06-08 09:43:13 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_busy_handler() {
|
|
|
|
let (path, _directory) = setup();
|
|
|
|
let mut database = ok!(Database::open(&path));
|
2015-06-08 11:28:43 -04:00
|
|
|
do_set_busy_handler(&mut database);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_set_busy_handler(database: &mut Database) {
|
|
|
|
ok!(database.set_busy_handler(Some(|_| true)));
|
2015-06-08 09:43:13 -04:00
|
|
|
}
|
2015-06-08 09:37:44 -04:00
|
|
|
}
|