89 lines
2.1 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-06-08 09:37:44 -04:00
#[cfg(test)]
extern crate temporary;
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 16:41:34 -04:00
($database:expr, $result:expr) => (
2015-05-29 14:33:39 -04:00
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 16:41:34 -04:00
code => return Err(::Error::from(::result::code_from_raw(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;
mod error;
mod result;
2015-05-29 11:24:01 -04:00
mod statement;
2015-05-28 19:19:08 -04:00
pub use error::Error;
2015-06-08 11:28:43 -04:00
pub use database::{Database, ExecuteCallback};
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]
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
}
2015-06-08 09:37:44 -04:00
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use temporary::Directory;
macro_rules! ok(
($result:expr) => ($result.unwrap());
);
pub fn setup() -> (PathBuf, Directory) {
let directory = ok!(Directory::new("sqlite"));
(directory.path().join("database.sqlite3"), directory)
}
}