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-29 09:44:06 -04:00
|
|
|
macro_rules! raise(
|
2015-05-29 14:04:39 -04:00
|
|
|
($message:expr) => (return Err(::Error::from($message)));
|
2015-05-29 09:44:06 -04:00
|
|
|
);
|
2015-05-28 21:30:02 -04:00
|
|
|
|
2015-05-29 09:44:06 -04:00
|
|
|
macro_rules! success(
|
2015-05-29 14:33:39 -04:00
|
|
|
($result:expr, $database:expr) => (
|
|
|
|
match $result {
|
|
|
|
::raw::SQLITE_OK => {},
|
|
|
|
code => match ::Error::last($database) {
|
|
|
|
Some(error) => return Err(error),
|
|
|
|
None => return Err(::Error::from(::result::code_from_raw(code))),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2015-05-29 09:44:06 -04:00
|
|
|
($result:expr) => (
|
|
|
|
match $result {
|
|
|
|
::raw::SQLITE_OK => {},
|
2015-05-29 14:33:39 -04:00
|
|
|
code => return Err(::Error {
|
|
|
|
code: ::result::code_from_raw(code),
|
|
|
|
message: Some(c_str_to_string!(unsafe {
|
|
|
|
::raw::sqlite3_errstr(code)
|
|
|
|
})),
|
|
|
|
}),
|
2015-05-28 21:30:02 -04:00
|
|
|
}
|
2015-05-29 09:44:06 -04:00
|
|
|
);
|
|
|
|
);
|
2015-05-28 21:30:02 -04:00
|
|
|
|
2015-05-29 09:44:06 -04:00
|
|
|
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
|
|
|
|
2015-05-29 09:44:06 -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 21:30:02 -04:00
|
|
|
}
|
2015-05-29 09:44:06 -04:00
|
|
|
);
|
|
|
|
);
|
2015-05-28 21:30:02 -04:00
|
|
|
|
2015-05-29 13:08:02 -04:00
|
|
|
macro_rules! c_str_to_string(
|
|
|
|
($cstr:expr) => (
|
|
|
|
String::from_utf8_lossy(::std::ffi::CStr::from_ptr($cstr as *const _).to_bytes())
|
|
|
|
.into_owned()
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
2015-05-29 09:44:06 -04:00
|
|
|
mod database;
|
2015-05-29 13:50:05 -04:00
|
|
|
mod error;
|
|
|
|
mod result;
|
2015-05-29 11:24:01 -04:00
|
|
|
mod statement;
|
2015-05-28 19:19:08 -04:00
|
|
|
|
2015-05-29 09:44:06 -04:00
|
|
|
pub use database::{Database, ExecuteCallback};
|
2015-05-29 13:50:05 -04:00
|
|
|
pub use error::Error;
|
|
|
|
pub use result::{Result, ResultCode};
|
2015-05-29 13:34:48 -04:00
|
|
|
pub use statement::{Statement, Binding, Value};
|
2015-05-28 19:19:08 -04:00
|
|
|
|
2015-05-28 19:21:48 -04:00
|
|
|
/// Open a database.
|
2015-05-28 19:19:08 -04:00
|
|
|
#[inline]
|
2015-05-29 14:04:39 -04:00
|
|
|
pub fn open(path: &std::path::Path) -> Result<Database> {
|
2015-05-28 19:19:08 -04:00
|
|
|
Database::open(path)
|
2015-05-28 17:21:43 -04:00
|
|
|
}
|