173 lines
5.0 KiB
Rust
Raw Normal View History

2015-05-28 19:19:08 -04:00
#![allow(unused_unsafe)]
2015-05-28 17:21:43 -04:00
extern crate libc;
extern crate sqlite3_sys as raw;
2015-05-28 21:30:02 -04:00
use libc::{c_char, c_int, c_void};
use std::marker::PhantomData;
2015-05-28 19:19:08 -04:00
use std::path::Path;
/// A result.
pub type Result<T> = std::result::Result<T, Error>;
/// An error.
#[derive(Debug)]
pub struct Error {
pub code: ErrorCode,
pub message: Option<String>,
}
macro_rules! raise(
($message:expr) => (
return Err(::Error { code: ::ErrorCode::Error, message: Some($message.to_string()) })
);
($code:expr, $message:expr) => (
return Err(::Error { code: $code, message: $message })
);
);
macro_rules! success(
($result:expr) => (
match $result {
::raw::SQLITE_OK => {},
code => raise!(unsafe { ::std::mem::transmute(code as i8) }, None),
}
);
);
macro_rules! path_to_c_str(
($path:expr) => ({
match $path.to_str() {
Some(path) => match ::std::ffi::CString::new(path) {
Ok(string) => string.as_ptr(),
Err(_) => raise!("failed to process a path"),
},
None => raise!("failed to process a path"),
}
});
);
2015-05-28 21:30:02 -04:00
macro_rules! str_to_c_str(
($string:expr) => (
match ::std::ffi::CString::new($string) {
Ok(string) => string.as_ptr(),
Err(_) => raise!("failed to process a string"),
}
);
);
2015-05-28 19:20:58 -04:00
/// An error code.
2015-05-28 18:35:53 -04:00
#[derive(Clone, Copy, Debug)]
2015-05-28 19:19:08 -04:00
pub enum ErrorCode {
2015-05-28 18:35:53 -04:00
Abort = raw::SQLITE_ABORT as isize,
Authorization = raw::SQLITE_AUTH as isize,
Busy = raw::SQLITE_BUSY as isize,
CantOpen = raw::SQLITE_CANTOPEN as isize,
Constraint = raw::SQLITE_CONSTRAINT as isize,
Corruption = raw::SQLITE_CORRUPT as isize,
Done = raw::SQLITE_DONE as isize,
Empty = raw::SQLITE_EMPTY as isize,
Error = raw::SQLITE_ERROR as isize,
Format = raw::SQLITE_FORMAT as isize,
Full = raw::SQLITE_FULL as isize,
Internal = raw::SQLITE_INTERNAL as isize,
Interruption = raw::SQLITE_INTERRUPT as isize,
IOError = raw::SQLITE_IOERR as isize,
Locked = raw::SQLITE_LOCKED as isize,
Mismatch = raw::SQLITE_MISMATCH as isize,
Misuse = raw::SQLITE_MISUSE as isize,
NoLargeFileSupport = raw::SQLITE_NOLFS as isize,
NoMemory = raw::SQLITE_NOMEM as isize,
NotDatabase = raw::SQLITE_NOTADB as isize,
NotFound = raw::SQLITE_NOTFOUND as isize,
Notice = raw::SQLITE_NOTICE as isize,
OK = raw::SQLITE_OK as isize,
Permission = raw::SQLITE_PERM as isize,
Protocol = raw::SQLITE_PROTOCOL as isize,
Range = raw::SQLITE_RANGE as isize,
ReadOnly = raw::SQLITE_READONLY as isize,
Row = raw::SQLITE_ROW as isize,
Schema = raw::SQLITE_SCHEMA as isize,
TooBig = raw::SQLITE_TOOBIG as isize,
Warning = raw::SQLITE_WARNING as isize,
}
2015-05-28 19:19:08 -04:00
/// A database.
2015-05-28 21:30:02 -04:00
pub struct Database<'d> {
2015-05-28 19:19:08 -04:00
db: *mut raw::sqlite3,
2015-05-28 21:30:02 -04:00
_phantom: PhantomData<&'d raw::sqlite3>,
2015-05-28 19:19:08 -04:00
}
2015-05-29 08:58:19 -04:00
pub type ExecuteCallback<'c> = FnMut(Vec<(String, String)>) -> bool + 'c;
2015-05-28 21:30:02 -04:00
impl<'d> Database<'d> {
2015-05-28 19:21:25 -04:00
/// Open a database.
2015-05-28 21:30:02 -04:00
pub fn open(path: &Path) -> Result<Database<'d>> {
2015-05-28 19:19:08 -04:00
let mut db = 0 as *mut _;
2015-05-28 21:30:02 -04:00
unsafe {
success!(raw::sqlite3_open(path_to_c_str!(path), &mut db));
}
Ok(Database { db: db, _phantom: PhantomData })
}
/// Execute an SQL statement.
2015-05-29 08:58:19 -04:00
pub fn execute<'c>(&mut self, sql: &str,
callback: Option<&mut ExecuteCallback<'c>>) -> Result<()> {
2015-05-28 21:30:02 -04:00
unsafe {
match callback {
Some(callback) => {
2015-05-29 08:58:19 -04:00
let mut callback = Box::new(callback);
2015-05-28 21:30:02 -04:00
success!(raw::sqlite3_exec(self.db, str_to_c_str!(sql), Some(execute_callback),
&mut callback as *mut _ as *mut _, 0 as *mut _));
},
None => {
success!(raw::sqlite3_exec(self.db, str_to_c_str!(sql), None,
0 as *mut _, 0 as *mut _));
},
}
}
Ok(())
2015-05-28 19:19:08 -04:00
}
}
2015-05-28 21:30:02 -04:00
impl<'d> Drop for Database<'d> {
2015-05-28 19:19:08 -04:00
#[inline]
fn drop(&mut self) {
unsafe { ::raw::sqlite3_close(self.db) };
}
}
2015-05-28 19:21:48 -04:00
/// Open a database.
2015-05-28 19:19:08 -04:00
#[inline]
pub fn open(path: &Path) -> Result<Database> {
Database::open(path)
2015-05-28 17:21:43 -04:00
}
2015-05-28 21:30:02 -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 {
macro_rules! c_str_to_string(
($string:expr) => (
match ::std::str::from_utf8(::std::ffi::CStr::from_ptr($string).to_bytes()) {
Ok(string) => String::from(string),
Err(_) => return 1,
}
);
);
unsafe {
let mut pairs = Vec::with_capacity(count as usize);
for i in 0..(count as isize) {
let column = c_str_to_string!(*columns.offset(i) as *const _);
let value = c_str_to_string!(*values.offset(i) as *const _);
pairs.push((column, value));
}
2015-05-29 08:58:19 -04:00
let ref mut callback = *(callback as *mut Box<&mut ExecuteCallback>);
2015-05-28 21:30:02 -04:00
if callback(pairs) { 0 } else { 1 }
}
}