#![allow(dead_code)]
use sqlite3_connector as ffi;
use std::{error, fmt};
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_descr(
($connection:expr, $result:expr) => (
match $result.ret_code {
::ffi::SQLITE_OK => {}
code => error!($connection, code),
}
);
($result:expr) => (
match $result.ret_code {
::ffi::SQLITE_OK => {}
code => return Err(::Error {
code: Some(code as isize),
message: None,
}),
}
);
);
macro_rules! ok_raw(
($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,
}),
}
);
);
#[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 sqlite3_connector;
mod statement;
pub use connection::Connection;
pub use connection::OpenFlags;
pub use cursor::Cursor;
pub use statement::{Bindable, Readable, State, Statement};
pub use sqlite3_connector::*;
#[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: ffi::Sqlite3DbHandle) -> Option<Error> {
unsafe {
let code = ffi::sqlite3_errcode(raw);
if code == ffi::SQLITE_OK {
return None;
}
let message = ffi::sqlite3_errmsg(raw);
Some(Error {
code: Some(code as isize),
message: Some(message),
})
}
}