2015-06-14 11:33:55 -04:00
|
|
|
//! Interface to [SQLite][1].
|
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
|
|
|
//! ```
|
2015-06-19 07:51:34 -04:00
|
|
|
//! let database = sqlite::open(":memory:").unwrap();
|
2015-06-14 11:33:55 -04:00
|
|
|
//!
|
|
|
|
//! database.execute(r#"
|
|
|
|
//! CREATE TABLE `users` (id INTEGER, name VARCHAR(255));
|
|
|
|
//! INSERT INTO `users` (id, name) VALUES (1, 'Alice');
|
|
|
|
//! "#).unwrap();
|
|
|
|
//!
|
|
|
|
//! database.process("SELECT * FROM `users`;", |pairs| {
|
|
|
|
//! for &(column, value) in pairs.iter() {
|
|
|
|
//! println!("{} = {}", column, value.unwrap());
|
|
|
|
//! }
|
|
|
|
//! true
|
|
|
|
//! }).unwrap();
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! [1]: https://www.sqlite.org
|
|
|
|
|
2015-05-28 19:19:08 -04:00
|
|
|
#![allow(unused_unsafe)]
|
|
|
|
|
2015-05-28 17:21:43 -04:00
|
|
|
extern crate libc;
|
2015-06-12 14:23:18 -04:00
|
|
|
extern crate sqlite3_sys as ffi;
|
2015-05-28 17:21:43 -04:00
|
|
|
|
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-06-08 17:43:31 -04:00
|
|
|
macro_rules! failure(
|
|
|
|
($database:expr, $code:expr) => (
|
|
|
|
match ::error::last($database) {
|
|
|
|
Some(error) => return Err(error),
|
2015-06-08 18:19:15 -04:00
|
|
|
None => return Err(::Error::from(::ErrorKind::from($code as isize))),
|
2015-06-08 17:43:31 -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 {
|
2015-06-12 14:23:18 -04:00
|
|
|
::ffi::SQLITE_OK => {},
|
2015-06-08 17:43:31 -04:00
|
|
|
code => failure!($database, code),
|
2015-05-29 14:33:39 -04:00
|
|
|
}
|
|
|
|
);
|
2015-05-29 09:44:06 -04:00
|
|
|
($result:expr) => (
|
|
|
|
match $result {
|
2015-06-12 14:23:18 -04:00
|
|
|
::ffi::SQLITE_OK => {},
|
2015-06-08 18:19:15 -04:00
|
|
|
code => return Err(::Error::from(::ErrorKind::from(code as isize))),
|
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;
|
2015-05-29 11:24:01 -04:00
|
|
|
mod statement;
|
2015-05-28 19:19:08 -04:00
|
|
|
|
2015-06-08 13:27:07 -04:00
|
|
|
pub use database::Database;
|
2015-06-08 18:05:17 -04:00
|
|
|
pub use error::{Error, ErrorKind};
|
2015-06-19 11:31:29 -04:00
|
|
|
pub use statement::{Statement, State, Parameter, Value};
|
2015-05-28 19:19:08 -04:00
|
|
|
|
2015-06-08 18:05:17 -04:00
|
|
|
/// A result.
|
2015-06-19 11:31:29 -04:00
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
2015-06-08 18:05:17 -04:00
|
|
|
|
2015-06-12 14:21:29 -04:00
|
|
|
/// Open a connection to a new or existing database.
|
2015-05-28 19:19:08 -04:00
|
|
|
#[inline]
|
2015-06-19 07:51:34 -04:00
|
|
|
pub fn open<'l, P: AsRef<std::path::Path>>(path: P) -> Result<Database<'l>> {
|
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)
|
|
|
|
}
|
|
|
|
}
|