2015-06-14 11:33:55 -04:00
|
|
|
//! Interface to [SQLite][1].
|
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
|
|
|
//! ```
|
2015-07-04 08:53:26 -04:00
|
|
|
//! let connection = sqlite::open(":memory:").unwrap();
|
2015-06-14 11:33:55 -04:00
|
|
|
//!
|
2015-07-04 09:29:46 -04:00
|
|
|
//! connection.execute("
|
2015-08-01 16:23:05 -04:00
|
|
|
//! CREATE TABLE users (id INTEGER, name VARCHAR(255));
|
|
|
|
//! INSERT INTO users (id, name) VALUES (42, 'Alice');
|
2015-07-04 09:29:46 -04:00
|
|
|
//! ").unwrap();
|
2015-06-14 11:33:55 -04:00
|
|
|
//!
|
2015-08-01 16:23:05 -04:00
|
|
|
//! connection.process("SELECT * FROM users", |pairs| {
|
2015-06-14 11:33:55 -04:00
|
|
|
//! for &(column, value) in pairs.iter() {
|
|
|
|
//! println!("{} = {}", column, value.unwrap());
|
|
|
|
//! }
|
|
|
|
//! true
|
|
|
|
//! }).unwrap();
|
2015-07-30 08:12:43 -04:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! The same example using prepared statements:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! use sqlite::State;
|
|
|
|
//!
|
|
|
|
//! let connection = sqlite::open(":memory:").unwrap();
|
|
|
|
//!
|
|
|
|
//! connection.execute("
|
2015-08-01 16:23:05 -04:00
|
|
|
//! CREATE TABLE users (id INTEGER, name VARCHAR(255))
|
2015-07-30 08:12:43 -04:00
|
|
|
//! ");
|
|
|
|
//!
|
|
|
|
//! let mut statement = connection.prepare("
|
2015-08-01 16:23:05 -04:00
|
|
|
//! INSERT INTO users (id, name) VALUES (?, ?)
|
2015-07-30 08:12:43 -04:00
|
|
|
//! ").unwrap();
|
|
|
|
//! statement.bind(1, 42).unwrap();
|
|
|
|
//! statement.bind(2, "Alice").unwrap();
|
|
|
|
//! assert_eq!(statement.step().unwrap(), State::Done);
|
2015-07-04 09:27:13 -04:00
|
|
|
//!
|
2015-08-01 16:23:05 -04:00
|
|
|
//! let mut statement = connection.prepare("SELECT * FROM users").unwrap();
|
2015-07-30 08:12:43 -04:00
|
|
|
//! while let State::Row = statement.step().unwrap() {
|
2015-07-04 09:27:13 -04:00
|
|
|
//! println!("id = {}", statement.read::<i64>(0).unwrap());
|
|
|
|
//! println!("name = {}", statement.read::<String>(1).unwrap());
|
|
|
|
//! }
|
2015-06-14 11:33:55 -04:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! [1]: https://www.sqlite.org
|
|
|
|
|
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-08-01 17:38:08 -04:00
|
|
|
use std::{error, fmt};
|
|
|
|
|
2015-05-29 09:44:06 -04:00
|
|
|
macro_rules! raise(
|
2015-08-01 17:38:08 -04:00
|
|
|
($message:expr) => (return Err(::Error { code: None, message: Some($message.to_string()) }));
|
2015-05-29 09:44:06 -04:00
|
|
|
);
|
2015-05-28 21:30:02 -04:00
|
|
|
|
2015-06-19 20:30:28 -04:00
|
|
|
macro_rules! error(
|
2015-08-01 17:38:08 -04:00
|
|
|
($connection:expr, $code:expr) => (match ::last_error($connection) {
|
2015-07-27 11:38:54 -04:00
|
|
|
Some(error) => return Err(error),
|
2015-08-01 17:38:08 -04:00
|
|
|
_ => return Err(::Error { code: Some($code as isize), message: None }),
|
2015-07-27 11:38:54 -04:00
|
|
|
});
|
2015-06-08 17:43:31 -04:00
|
|
|
);
|
|
|
|
|
2015-06-19 20:30:28 -04:00
|
|
|
macro_rules! ok(
|
2015-07-27 11:38:54 -04:00
|
|
|
($connection:expr, $result:expr) => (match $result {
|
|
|
|
::ffi::SQLITE_OK => {},
|
|
|
|
code => error!($connection, code),
|
|
|
|
});
|
|
|
|
($result:expr) => (match $result {
|
|
|
|
::ffi::SQLITE_OK => {},
|
2015-08-01 17:38:08 -04:00
|
|
|
code => return Err(::Error { code: Some(code as isize), message: None }),
|
2015-05-29 09:44:06 -04:00
|
|
|
});
|
|
|
|
);
|
2015-05-28 21:30:02 -04:00
|
|
|
|
2015-06-19 20:23:12 -04:00
|
|
|
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()
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
2015-07-27 11:38:54 -04:00
|
|
|
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"),
|
|
|
|
});
|
2015-05-29 09:44:06 -04:00
|
|
|
);
|
2015-05-28 21:30:02 -04:00
|
|
|
|
2015-08-01 17:38:08 -04:00
|
|
|
/// An error.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Error {
|
|
|
|
/// The error code.
|
|
|
|
pub code: Option<isize>,
|
|
|
|
/// The error message.
|
|
|
|
pub message: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A result.
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
2015-08-01 16:23:05 -04:00
|
|
|
/// A data type.
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
pub enum Type {
|
|
|
|
/// Binary data.
|
|
|
|
Blob,
|
2015-08-01 17:41:25 -04:00
|
|
|
/// A 64-bit floating-point number.
|
2015-08-01 16:23:05 -04:00
|
|
|
Float,
|
|
|
|
/// A 64-bit signed integer.
|
|
|
|
Integer,
|
|
|
|
/// An absence of a value.
|
|
|
|
Null,
|
|
|
|
/// A string.
|
|
|
|
String,
|
|
|
|
}
|
|
|
|
|
2015-08-01 17:38:08 -04:00
|
|
|
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",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-04 08:53:26 -04:00
|
|
|
mod connection;
|
2015-05-29 11:24:01 -04:00
|
|
|
mod statement;
|
2015-05-28 19:19:08 -04:00
|
|
|
|
2015-07-04 08:53:26 -04:00
|
|
|
pub use connection::Connection;
|
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-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-08-01 15:14:10 -04:00
|
|
|
pub fn open<T: AsRef<std::path::Path>>(path: T) -> Result<Connection> {
|
2015-07-04 08:53:26 -04:00
|
|
|
Connection::open(path)
|
2015-05-28 17:21:43 -04:00
|
|
|
}
|
2015-08-01 17:38:08 -04:00
|
|
|
|
|
|
|
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)) })
|
|
|
|
}
|
|
|
|
}
|