extern crate libc;
extern crate sqlite3_sys as ffi;
use std::{error, fmt};
macro_rules! raise(
($message:expr) => (return Err(::Error { code: None, message: Some($message.to_string()) }));
);
macro_rules! error(
($connection:expr, $code:expr) => (match ::last_error($connection) {
Some(error) => return Err(error),
_ => return Err(::Error { code: Some($code as isize), message: None }),
});
);
macro_rules! ok(
($connection:expr, $result:expr) => (match $result {
::ffi::SQLITE_OK => {},
code => error!($connection, code),
});
($result:expr) => (match $result {
::ffi::SQLITE_OK => {},
code => return Err(::Error { code: Some(code as isize), message: None }),
});
);
macro_rules! c_str_to_str(
($string:expr) => (::std::str::from_utf8(::std::ffi::CStr::from_ptr($string).to_bytes()));
);
macro_rules! c_str_to_string(
($string:expr) => (
String::from_utf8_lossy(::std::ffi::CStr::from_ptr($string as *const _).to_bytes())
.into_owned()
);
);
macro_rules! path_to_cstr(
($path:expr) => (match $path.to_str() {
Some(path) => match ::std::ffi::CString::new(path) {
Ok(string) => string,
_ => raise!("failed to process a path"),
},
_ => raise!("failed to process a path"),
});
);
macro_rules! str_to_cstr(
($string:expr) => (match ::std::ffi::CString::new($string) {
Ok(string) => string,
_ => raise!("failed to process a string"),
});
);
#[derive(Debug)]
pub struct Error {
pub code: Option<isize>,
pub message: Option<String>,
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Type {
Binary,
Float,
Integer,
String,
Null,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
Binary(Vec<u8>),
Float(f64),
Integer(i64),
String(String),
Null,
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match (self.code, &self.message) {
(Some(code), &Some(ref message)) => write!(formatter, "{} (code {})", message, code),
(Some(code), _) => write!(formatter, "an SQLite error (code {})", code),
(_, &Some(ref message)) => message.fmt(formatter),
_ => write!(formatter, "an SQLite error"),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match self.message {
Some(ref message) => message,
_ => "an SQLite error",
}
}
}
impl Value {
#[inline]
pub fn as_binary(&self) -> Option<&[u8]> {
if let &Value::Binary(ref value) = self {
return Some(value);
}
None
}
#[inline]
pub fn as_float(&self) -> Option<f64> {
if let &Value::Float(value) = self {
return Some(value);
}
None
}
#[inline]
pub fn as_integer(&self) -> Option<i64> {
if let &Value::Integer(value) = self {
return Some(value);
}
None
}
#[inline]
pub fn as_string(&self) -> Option<&str> {
if let &Value::String(ref value) = self {
return Some(value);
}
None
}
pub fn kind(&self) -> Type {
match self {
&Value::Binary(_) => Type::Binary,
&Value::Float(_) => Type::Float,
&Value::Integer(_) => Type::Integer,
&Value::String(_) => Type::String,
&Value::Null => Type::Null,
}
}
}
mod connection;
mod cursor;
mod statement;
pub use connection::Connection;
pub use cursor::Cursor;
pub use statement::{Statement, State, Bindable, Readable};
#[inline]
pub fn open<T: AsRef<std::path::Path>>(path: T) -> Result<Connection> {
Connection::open(path)
}
#[inline]
pub fn version() -> usize {
unsafe { ffi::sqlite3_libversion_number() as usize }
}
fn last_error(raw: *mut ffi::sqlite3) -> Option<Error> {
unsafe {
let code = ffi::sqlite3_errcode(raw);
if code == ffi::SQLITE_OK {
return None;
}
let message = ffi::sqlite3_errmsg(raw);
if message.is_null() {
return None;
}
Some(Error { code: Some(code as isize), message: Some(c_str_to_string!(message)) })
}
}