113 lines
3.6 KiB
Rust
Raw Normal View History

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.
pub struct Database {
2015-05-29 11:24:01 -04:00
raw: *mut raw::sqlite3,
phantom: PhantomData<raw::sqlite3>,
2015-05-29 09:44:06 -04:00
}
2015-06-07 22:15:59 -04:00
/// 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.
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
impl Database {
2015-05-29 09:44:06 -04:00
/// Open a database.
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
}
/// Execute an SQL statement.
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
unsafe {
match callback {
Some(callback) => {
let mut callback = Box::new(callback);
2015-05-29 16:41:34 -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-05-29 09:44:06 -04:00
},
None => {
2015-05-29 16:41:34 -04:00
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
/// Create a prepared statement.
#[inline]
2015-06-07 22:17:07 -04:00
pub fn prepare_statement<'l>(&'l self, sql: &str) -> Result<Statement<'l>> {
::statement::new(self, sql)
2015-05-29 11:24:01 -04:00
}
2015-06-07 22:15:59 -04:00
/// 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(())
}
2015-05-29 09:44:06 -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]
pub fn as_raw(database: &Database) -> *mut raw::sqlite3 {
2015-05-29 14:15:01 -04:00
database.raw
}
2015-06-07 22:15:59 -04:00
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 }
}
}
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 }
}
}