sqlite-wasm-connector/src/connection.rs

249 lines
6.8 KiB
Rust
Raw Normal View History

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;
use {Result, Statement};
2015-05-29 09:44:06 -04:00
2015-08-02 22:29:04 -04:00
/// A database connection.
pub struct Connection {
2015-06-12 14:23:18 -04:00
raw: *mut ffi::sqlite3,
2019-06-08 18:48:58 +02:00
busy_callback: Option<Box<dyn FnMut(usize) -> bool>>,
2015-06-12 14:23:18 -04:00
phantom: PhantomData<ffi::sqlite3>,
2015-05-29 09:44:06 -04:00
}
2019-06-08 19:50:27 +02:00
/// A set of connection flags.
#[derive(Clone, Copy, Debug)]
pub struct ConnectionFlags(c_int);
2016-11-30 12:53:32 +01:00
unsafe impl Send for Connection {}
impl Connection {
2019-06-08 19:50:27 +02:00
/// Open a read-write connection to a new or existing database.
pub fn open<T: AsRef<Path>>(path: T) -> Result<Connection> {
2019-06-08 19:50:27 +02:00
Connection::open_with_flags(path, ConnectionFlags::new().set_create().set_read_write())
2015-05-29 09:44:06 -04:00
}
2019-06-08 19:50:27 +02:00
/// Open a database connection with a specific set of connection flags.
pub fn open_with_flags<T: AsRef<Path>>(path: T, flags: ConnectionFlags) -> Result<Connection> {
let mut raw = 0 as *mut _;
unsafe {
ok!(ffi::sqlite3_open_v2(
path_to_cstr!(path.as_ref()).as_ptr(),
&mut raw,
2019-06-08 19:50:27 +02:00
flags.0,
0 as *const _,
));
}
Ok(Connection {
raw: raw,
busy_callback: None,
phantom: PhantomData,
})
}
2015-08-02 22:29:04 -04:00
/// Execute a statement without processing the resulting rows if any.
#[inline]
2015-08-02 22:29:04 -04:00
pub fn execute<T: AsRef<str>>(&self, statement: T) -> Result<()> {
unsafe {
2017-08-22 17:34:03 +02:00
ok!(
self.raw,
ffi::sqlite3_exec(
self.raw,
str_to_cstr!(statement.as_ref()).as_ptr(),
None,
0 as *mut _,
0 as *mut _,
)
);
}
Ok(())
}
2015-08-02 22:29:04 -04:00
/// Execute a statement and process the resulting rows as plain text.
///
/// The callback is triggered for each row. If the callback returns `false`,
2015-07-04 09:21:38 -04:00
/// no more rows will be processed. For large queries and non-string data
2015-08-03 17:12:15 -04:00
/// types, prepared statement are highly preferable; see `prepare`.
#[inline]
2015-08-03 17:12:15 -04:00
pub fn iterate<T: AsRef<str>, F>(&self, statement: T, callback: F) -> Result<()>
2017-08-22 17:34:03 +02:00
where
F: FnMut(&[(&str, Option<&str>)]) -> bool,
{
unsafe {
2015-06-08 14:52:13 -04:00
let callback = Box::new(callback);
2017-08-22 17:34:03 +02:00
ok!(
self.raw,
ffi::sqlite3_exec(
self.raw,
str_to_cstr!(statement.as_ref()).as_ptr(),
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
/// Create a prepared statement.
#[inline]
2015-08-02 22:29:04 -04:00
pub fn prepare<'l, T: AsRef<str>>(&'l self, statement: T) -> Result<Statement<'l>> {
::statement::new(self.raw, statement)
}
/// 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
/// 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<()>
2017-08-22 17:34:03 +02:00
where
F: FnMut(usize) -> bool + Send + 'static,
{
2015-06-08 14:52:13 -04:00
try!(self.remove_busy_handler());
unsafe {
2015-06-08 14:52:13 -04:00
let callback = Box::new(callback);
2017-08-22 17:34:03 +02:00
let result = ffi::sqlite3_busy_handler(
self.raw,
Some(busy_callback::<F>),
&*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(())
}
/// Set an implicit callback for handling busy events that tries to repeat
/// rejected operations until a timeout expires.
#[inline]
pub fn set_busy_timeout(&mut self, milliseconds: usize) -> Result<()> {
2017-08-22 17:34:03 +02:00
unsafe {
ok!(
self.raw,
ffi::sqlite3_busy_timeout(self.raw, milliseconds as c_int)
2017-08-23 10:13:05 +02:00
);
}
Ok(())
2015-06-07 22:15:59 -04:00
}
/// Remove the callback handling busy events.
#[inline]
pub fn remove_busy_handler(&mut self) -> Result<()> {
2015-07-07 09:33:39 -04:00
self.busy_callback = None;
2017-08-22 17:34:03 +02:00
unsafe {
ok!(
self.raw,
ffi::sqlite3_busy_handler(self.raw, None, 0 as *mut _)
2017-08-23 10:13:05 +02:00
);
}
Ok(())
}
2017-08-23 10:13:59 +02:00
/// Return the raw pointer.
#[inline]
pub fn as_raw(&self) -> *mut ffi::sqlite3 {
self.raw
}
2015-05-29 09:44:06 -04:00
}
impl Drop for Connection {
#[inline]
#[allow(unused_must_use)]
fn drop(&mut self) {
self.remove_busy_handler();
unsafe { ffi::sqlite3_close(self.raw) };
}
2015-05-29 09:44:06 -04:00
}
2019-06-08 19:50:27 +02:00
impl ConnectionFlags {
/// Create a set of connection flags.
#[inline]
pub fn new() -> Self {
ConnectionFlags(0)
}
/// Create the database if it does not already exist.
pub fn set_create(mut self) -> Self {
self.0 |= ffi::SQLITE_OPEN_CREATE;
self
}
2019-06-08 20:15:44 +02:00
/// Open the database in the serialized [threading mode][1].
///
/// [1]: https://www.sqlite.org/threadsafe.html
pub fn set_full_mutex(mut self) -> Self {
self.0 |= ffi::SQLITE_OPEN_FULLMUTEX;
self
}
/// Opens the database in the multi-thread [threading mode][1].
///
/// [1]: https://www.sqlite.org/threadsafe.html
pub fn set_no_mutex(mut self) -> Self {
self.0 |= ffi::SQLITE_OPEN_NOMUTEX;
self
}
2019-06-08 19:50:27 +02:00
/// Open the database for reading only.
pub fn set_read_only(mut self) -> Self {
2019-06-08 20:42:31 +02:00
self.0 |= ffi::SQLITE_OPEN_READONLY;
2019-06-08 19:50:27 +02:00
self
}
/// Open the database for reading and writing.
pub fn set_read_write(mut self) -> Self {
self.0 |= ffi::SQLITE_OPEN_READWRITE;
self
}
}
2017-08-22 17:34:03 +02:00
extern "C" fn busy_callback<F>(callback: *mut c_void, attempts: c_int) -> c_int
where
F: FnMut(usize) -> bool,
{
2017-08-22 17:34:03 +02:00
unsafe {
if (*(callback as *mut F))(attempts as usize) {
1
} else {
0
}
}
2015-06-07 22:15:59 -04:00
}
2017-08-22 17:34:03 +02:00
extern "C" fn process_callback<F>(
callback: *mut c_void,
count: c_int,
values: *mut *mut c_char,
columns: *mut *mut c_char,
) -> c_int
where
F: FnMut(&[(&str, Option<&str>)]) -> bool,
{
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) {
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));
}
2017-08-22 17:34:03 +02:00
if (*(callback as *mut F))(&pairs) {
0
} else {
1
}
2015-05-29 09:44:06 -04:00
}
}