2015-06-12 14:23:18 -04:00
|
|
|
use ffi;
|
2015-05-29 09:44:06 -04:00
|
|
|
use libc::{c_char, c_int, c_void};
|
|
|
|
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
|
|
|
|
2015-06-12 14:21:29 -04:00
|
|
|
/// A connection to a database.
|
2015-07-04 08:53:26 -04:00
|
|
|
pub struct Connection<'l> {
|
2015-06-12 14:23:18 -04:00
|
|
|
raw: *mut ffi::sqlite3,
|
2015-06-08 14:52:13 -04:00
|
|
|
busy_callback: Option<Box<FnMut(usize) -> bool + 'l>>,
|
2015-06-12 14:23:18 -04:00
|
|
|
phantom: PhantomData<ffi::sqlite3>,
|
2015-05-29 09:44:06 -04:00
|
|
|
}
|
|
|
|
|
2015-07-04 08:53:26 -04:00
|
|
|
impl<'l> Connection<'l> {
|
2015-06-12 14:21:29 -04:00
|
|
|
/// Open a connection to a new or existing database.
|
2015-07-04 08:53:26 -04:00
|
|
|
pub fn open<T: AsRef<Path>>(path: T) -> Result<Connection<'l>> {
|
2015-05-29 11:24:01 -04:00
|
|
|
let mut raw = 0 as *mut _;
|
2015-05-29 09:44:06 -04:00
|
|
|
unsafe {
|
2015-06-19 20:30:28 -04:00
|
|
|
ok!(ffi::sqlite3_open_v2(path_to_c_str!(path.as_ref()), &mut raw,
|
|
|
|
ffi::SQLITE_OPEN_CREATE | ffi::SQLITE_OPEN_READWRITE,
|
|
|
|
0 as *const _));
|
2015-05-29 09:44:06 -04:00
|
|
|
}
|
2015-07-04 08:53:26 -04:00
|
|
|
Ok(Connection {
|
2015-06-08 14:52:13 -04:00
|
|
|
raw: raw,
|
|
|
|
busy_callback: None,
|
|
|
|
phantom: PhantomData,
|
|
|
|
})
|
2015-05-29 09:44:06 -04:00
|
|
|
}
|
|
|
|
|
2015-06-08 13:27:07 -04:00
|
|
|
/// Execute a query without processing the resulting rows if any.
|
|
|
|
#[inline]
|
2015-06-08 15:00:44 -04:00
|
|
|
pub fn execute(&self, sql: &str) -> Result<()> {
|
2015-06-08 13:27:07 -04:00
|
|
|
unsafe {
|
2015-06-19 20:30:28 -04:00
|
|
|
ok!(self.raw, ffi::sqlite3_exec(self.raw, str_to_c_str!(sql), None, 0 as *mut _,
|
|
|
|
0 as *mut _));
|
2015-06-08 13:27:07 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute a query and process the resulting rows if any.
|
|
|
|
///
|
|
|
|
/// The callback is triggered for each row. If the callback returns `false`,
|
|
|
|
/// no more rows will be processed.
|
|
|
|
#[inline]
|
2015-06-08 15:00:44 -04:00
|
|
|
pub fn process<F>(&self, sql: &str, callback: F) -> Result<()>
|
2015-06-08 21:42:34 -04:00
|
|
|
where F: FnMut(&[(&str, Option<&str>)]) -> bool
|
2015-06-08 13:27:07 -04:00
|
|
|
{
|
|
|
|
unsafe {
|
2015-06-08 14:52:13 -04:00
|
|
|
let callback = Box::new(callback);
|
2015-06-19 20:30:28 -04:00
|
|
|
ok!(self.raw, ffi::sqlite3_exec(self.raw, str_to_c_str!(sql),
|
|
|
|
Some(process_callback::<F>),
|
|
|
|
&*callback as *const F as *mut F 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 13:27:07 -04:00
|
|
|
/// Create a prepared statement.
|
2015-05-29 16:58:48 -04:00
|
|
|
#[inline]
|
2015-06-08 14:52:13 -04:00
|
|
|
pub fn prepare(&'l self, sql: &str) -> Result<Statement<'l>> {
|
2015-06-08 17:43:31 -04:00
|
|
|
::statement::new(self.raw, sql)
|
2015-05-29 11:24:01 -04:00
|
|
|
}
|
2015-06-07 22:15:59 -04:00
|
|
|
|
2015-06-08 13:27:07 -04:00
|
|
|
/// Set a callback for handling busy events.
|
2015-06-08 11:28:43 -04:00
|
|
|
///
|
|
|
|
/// The callback is triggered when the database cannot perform an operation
|
2015-06-08 13:27:07 -04:00
|
|
|
/// due to processing of some other request. If the callback returns `true`,
|
|
|
|
/// the operation will be repeated.
|
|
|
|
pub fn set_busy_handler<F>(&mut self, callback: F) -> Result<()>
|
2015-06-08 14:52:13 -04:00
|
|
|
where F: FnMut(usize) -> bool + 'l
|
2015-06-08 13:27:07 -04:00
|
|
|
{
|
2015-06-08 14:52:13 -04:00
|
|
|
try!(self.remove_busy_handler());
|
2015-06-08 13:27:07 -04:00
|
|
|
unsafe {
|
2015-06-08 14:52:13 -04:00
|
|
|
let callback = Box::new(callback);
|
2015-06-12 14:23:18 -04:00
|
|
|
let result = ffi::sqlite3_busy_handler(self.raw, Some(busy_callback::<F>),
|
2015-06-10 19:20:50 -04:00
|
|
|
&*callback as *const F as *mut F as *mut _);
|
2015-06-08 14:52:13 -04:00
|
|
|
self.busy_callback = Some(callback);
|
2015-06-19 20:30:28 -04:00
|
|
|
ok!(self.raw, result);
|
2015-06-07 22:15:59 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
2015-06-08 07:53:28 -04:00
|
|
|
}
|
|
|
|
|
2015-06-08 13:27:07 -04:00
|
|
|
/// Set an implicit callback for handling busy events that tries to repeat
|
|
|
|
/// rejected operations until a timeout expires.
|
2015-06-08 07:53:28 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn set_busy_timeout(&mut self, milliseconds: usize) -> Result<()> {
|
2015-06-19 20:30:28 -04:00
|
|
|
unsafe { ok!(self.raw, ffi::sqlite3_busy_timeout(self.raw, milliseconds as c_int)) };
|
2015-06-08 07:53:28 -04:00
|
|
|
Ok(())
|
2015-06-07 22:15:59 -04:00
|
|
|
}
|
2015-06-08 13:27:07 -04:00
|
|
|
|
|
|
|
/// Remove the callback handling busy events.
|
|
|
|
#[inline]
|
|
|
|
pub fn remove_busy_handler(&mut self) -> Result<()> {
|
2015-06-08 14:52:13 -04:00
|
|
|
::std::mem::replace(&mut self.busy_callback, None);
|
2015-06-19 20:30:28 -04:00
|
|
|
unsafe { ok!(self.raw, ffi::sqlite3_busy_handler(self.raw, None, 0 as *mut _)) };
|
2015-06-08 13:27:07 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
2015-05-29 09:44:06 -04:00
|
|
|
}
|
|
|
|
|
2015-07-04 08:53:26 -04:00
|
|
|
impl<'l> Drop for Connection<'l> {
|
2015-06-12 15:01:12 -04:00
|
|
|
#[cfg(not(feature = "edge"))]
|
|
|
|
#[inline]
|
|
|
|
#[allow(unused_must_use)]
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.remove_busy_handler();
|
|
|
|
unsafe { ffi::sqlite3_close(self.raw) };
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "edge")]
|
2015-05-29 09:44:06 -04:00
|
|
|
#[inline]
|
2015-06-08 14:52:13 -04:00
|
|
|
#[allow(unused_must_use)]
|
2015-05-29 09:44:06 -04:00
|
|
|
fn drop(&mut self) {
|
2015-06-08 14:52:13 -04:00
|
|
|
self.remove_busy_handler();
|
2015-06-12 14:34:50 -04:00
|
|
|
unsafe { ffi::sqlite3_close_v2(self.raw) };
|
2015-05-29 09:44:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 13:27:07 -04:00
|
|
|
extern fn busy_callback<F>(callback: *mut c_void, attempts: c_int) -> c_int
|
|
|
|
where F: FnMut(usize) -> bool
|
|
|
|
{
|
2015-06-08 14:52:13 -04:00
|
|
|
unsafe { if (*(callback as *mut F))(attempts as usize) { 1 } else { 0 } }
|
2015-06-07 22:15:59 -04:00
|
|
|
}
|
|
|
|
|
2015-06-08 21:42:34 -04:00
|
|
|
extern fn process_callback<F>(callback: *mut c_void, count: c_int, values: *mut *mut c_char,
|
2015-06-08 13:27:07 -04:00
|
|
|
columns: *mut *mut c_char) -> c_int
|
2015-06-08 21:42:34 -04:00
|
|
|
where F: FnMut(&[(&str, Option<&str>)]) -> bool
|
2015-06-08 13:27:07 -04:00
|
|
|
{
|
2015-05-29 09:44:06 -04:00
|
|
|
unsafe {
|
|
|
|
let mut pairs = Vec::with_capacity(count as usize);
|
|
|
|
|
|
|
|
for i in 0..(count as isize) {
|
2015-06-08 21:42:34 -04:00
|
|
|
let column = {
|
|
|
|
let pointer = *columns.offset(i);
|
|
|
|
debug_assert!(!pointer.is_null());
|
|
|
|
c_str_to_str!(pointer).unwrap()
|
|
|
|
};
|
|
|
|
let value = {
|
|
|
|
let pointer = *values.offset(i);
|
|
|
|
if pointer.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(c_str_to_str!(pointer).unwrap())
|
|
|
|
}
|
|
|
|
};
|
2015-05-29 09:44:06 -04:00
|
|
|
pairs.push((column, value));
|
|
|
|
}
|
|
|
|
|
2015-06-08 21:42:34 -04:00
|
|
|
if (*(callback as *mut F))(&pairs) { 0 } else { 1 }
|
2015-05-29 09:44:06 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-08 09:37:44 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2015-07-04 08:53:26 -04:00
|
|
|
use super::Connection;
|
2015-06-08 09:37:44 -04:00
|
|
|
use tests::setup;
|
|
|
|
|
|
|
|
#[test]
|
2015-06-08 15:00:44 -04:00
|
|
|
fn execute() {
|
2015-06-08 09:37:44 -04:00
|
|
|
let (path, _directory) = setup();
|
2015-07-04 08:53:26 -04:00
|
|
|
let connection = Connection::open(&path).unwrap();
|
|
|
|
match connection.execute(":)") {
|
2015-06-08 13:27:07 -04:00
|
|
|
Err(error) => assert_eq!(error.message,
|
|
|
|
Some(String::from(r#"unrecognized token: ":""#))),
|
2015-06-19 20:30:28 -04:00
|
|
|
_ => unreachable!(),
|
2015-06-08 09:37:44 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-08 09:43:13 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_busy_handler() {
|
|
|
|
let (path, _directory) = setup();
|
2015-07-04 08:53:26 -04:00
|
|
|
let mut connection = Connection::open(&path).unwrap();
|
|
|
|
connection.set_busy_handler(|_| true).unwrap();
|
2015-06-08 09:43:13 -04:00
|
|
|
}
|
2015-06-08 09:37:44 -04:00
|
|
|
}
|