2015-06-14 11:33:55 -04:00
|
|
|
//! Interface to [SQLite][1].
|
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
2016-06-16 10:55:14 +02:00
|
|
|
//! Open a connection, create a table, and insert some rows:
|
2015-08-03 16:12:09 -04:00
|
|
|
//!
|
2015-06-14 11:33:55 -04:00
|
|
|
//! ```
|
2015-07-04 08:53:26 -04:00
|
|
|
//! let connection = sqlite::open(":memory:").unwrap();
|
2015-06-14 11:33:55 -04:00
|
|
|
//!
|
2017-08-22 17:47:44 +02:00
|
|
|
//! connection
|
|
|
|
//! .execute(
|
|
|
|
//! "
|
|
|
|
//! CREATE TABLE users (name TEXT, age INTEGER);
|
|
|
|
//! INSERT INTO users (name, age) VALUES ('Alice', 42);
|
|
|
|
//! INSERT INTO users (name, age) VALUES ('Bob', 69);
|
|
|
|
//! ",
|
|
|
|
//! )
|
|
|
|
//! .unwrap();
|
2015-08-03 17:23:22 -04:00
|
|
|
//! ```
|
|
|
|
//!
|
2016-06-16 10:55:14 +02:00
|
|
|
//! Select some rows and process them one by one as plain text:
|
2015-06-14 11:33:55 -04:00
|
|
|
//!
|
2015-08-03 17:23:22 -04:00
|
|
|
//! ```
|
|
|
|
//! # let connection = sqlite::open(":memory:").unwrap();
|
2017-08-22 17:47:44 +02:00
|
|
|
//! # connection
|
|
|
|
//! # .execute(
|
|
|
|
//! # "
|
|
|
|
//! # CREATE TABLE users (name TEXT, age INTEGER);
|
|
|
|
//! # INSERT INTO users (name, age) VALUES ('Alice', 42);
|
|
|
|
//! # INSERT INTO users (name, age) VALUES ('Bob', 69);
|
|
|
|
//! # ",
|
|
|
|
//! # )
|
|
|
|
//! # .unwrap();
|
|
|
|
//! connection
|
|
|
|
//! .iterate("SELECT * FROM users WHERE age > 50", |pairs| {
|
|
|
|
//! for &(column, value) in pairs.iter() {
|
|
|
|
//! println!("{} = {}", column, value.unwrap());
|
|
|
|
//! }
|
|
|
|
//! true
|
|
|
|
//! })
|
|
|
|
//! .unwrap();
|
2015-07-30 08:12:43 -04:00
|
|
|
//! ```
|
|
|
|
//!
|
2016-06-16 10:55:14 +02:00
|
|
|
//! The same query using a prepared statement, which is much more efficient than
|
|
|
|
//! the previous technique:
|
2015-07-30 08:12:43 -04:00
|
|
|
//!
|
|
|
|
//! ```
|
2015-08-03 16:52:51 -04:00
|
|
|
//! use sqlite::State;
|
2015-08-03 17:23:22 -04:00
|
|
|
//! # let connection = sqlite::open(":memory:").unwrap();
|
2017-08-22 17:47:44 +02:00
|
|
|
//! # connection
|
|
|
|
//! # .execute(
|
|
|
|
//! # "
|
|
|
|
//! # CREATE TABLE users (name TEXT, age INTEGER);
|
|
|
|
//! # INSERT INTO users (name, age) VALUES ('Alice', 42);
|
|
|
|
//! # INSERT INTO users (name, age) VALUES ('Bob', 69);
|
|
|
|
//! # ",
|
|
|
|
//! # )
|
|
|
|
//! # .unwrap();
|
|
|
|
//!
|
|
|
|
//! let mut statement = connection
|
2017-08-23 06:06:04 +02:00
|
|
|
//! .prepare("SELECT * FROM users WHERE age > ?")
|
2017-08-22 17:47:44 +02:00
|
|
|
//! .unwrap();
|
2015-08-03 16:12:09 -04:00
|
|
|
//!
|
2015-08-03 17:23:22 -04:00
|
|
|
//! statement.bind(1, 50).unwrap();
|
2015-07-04 09:27:13 -04:00
|
|
|
//!
|
2015-08-03 17:05:03 -04:00
|
|
|
//! while let State::Row = statement.next().unwrap() {
|
2015-08-03 16:52:51 -04:00
|
|
|
//! println!("name = {}", statement.read::<String>(0).unwrap());
|
|
|
|
//! println!("age = {}", statement.read::<i64>(1).unwrap());
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
2015-12-20 07:00:31 +01:00
|
|
|
//! The same query using a cursor, which is a wrapper around a prepared
|
2016-06-16 10:55:14 +02:00
|
|
|
//! statement providing the concept of row and featuring all-at-once binding:
|
2015-08-03 16:52:51 -04:00
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! use sqlite::Value;
|
2015-08-03 17:23:22 -04:00
|
|
|
//! # let connection = sqlite::open(":memory:").unwrap();
|
2017-08-22 17:47:44 +02:00
|
|
|
//! # connection
|
|
|
|
//! # .execute(
|
|
|
|
//! # "
|
|
|
|
//! # CREATE TABLE users (name TEXT, age INTEGER);
|
|
|
|
//! # INSERT INTO users (name, age) VALUES ('Alice', 42);
|
|
|
|
//! # INSERT INTO users (name, age) VALUES ('Bob', 69);
|
|
|
|
//! # ",
|
|
|
|
//! # )
|
|
|
|
//! # .unwrap();
|
|
|
|
//!
|
|
|
|
//! let mut cursor = connection
|
2017-08-23 06:06:04 +02:00
|
|
|
//! .prepare("SELECT * FROM users WHERE age > ?")
|
2017-08-22 17:47:44 +02:00
|
|
|
//! .unwrap()
|
|
|
|
//! .cursor();
|
2015-08-03 16:52:51 -04:00
|
|
|
//!
|
2015-08-03 17:23:22 -04:00
|
|
|
//! cursor.bind(&[Value::Integer(50)]).unwrap();
|
2015-08-03 16:52:51 -04:00
|
|
|
//!
|
2015-08-03 17:10:30 -04:00
|
|
|
//! while let Some(row) = cursor.next().unwrap() {
|
2015-08-03 22:02:46 -04:00
|
|
|
//! println!("name = {}", row[0].as_string().unwrap());
|
|
|
|
//! println!("age = {}", row[1].as_integer().unwrap());
|
2015-07-04 09:27:13 -04:00
|
|
|
//! }
|
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(
|
2017-08-23 06:06:04 +02: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(
|
2017-08-23 10:09:04 +02:00
|
|
|
($connection:expr, $code:expr) => (
|
|
|
|
match ::last_error($connection) {
|
|
|
|
Some(error) => return Err(error),
|
|
|
|
_ => return Err(::Error {
|
|
|
|
code: Some($code as isize),
|
|
|
|
message: None,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
);
|
2015-06-08 17:43:31 -04:00
|
|
|
);
|
|
|
|
|
2015-06-19 20:30:28 -04:00
|
|
|
macro_rules! ok(
|
2017-08-23 10:09:04 +02:00
|
|
|
($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,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
);
|
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(
|
2017-08-23 10:09:04 +02:00
|
|
|
($path:expr) => (
|
|
|
|
match $path.to_str() {
|
|
|
|
Some(path) => {
|
|
|
|
match ::std::ffi::CString::new(path) {
|
|
|
|
Ok(string) => string,
|
|
|
|
_ => raise!("failed to process a path"),
|
|
|
|
}
|
2017-08-23 08:06:53 +02:00
|
|
|
}
|
2017-08-23 10:09:04 +02:00
|
|
|
_ => raise!("failed to process a path"),
|
2017-08-23 06:06:04 +02:00
|
|
|
}
|
2017-08-23 10:09:04 +02:00
|
|
|
);
|
2015-07-27 11:38:54 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
macro_rules! str_to_cstr(
|
2017-08-23 10:09:04 +02:00
|
|
|
($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 {
|
2015-08-01 17:50:06 -04:00
|
|
|
/// The binary type.
|
2015-08-01 17:51:43 -04:00
|
|
|
Binary,
|
2015-08-02 20:15:15 -04:00
|
|
|
/// The floating-point type.
|
2015-08-01 16:23:05 -04:00
|
|
|
Float,
|
2015-08-02 20:15:15 -04:00
|
|
|
/// The integer type.
|
2015-08-01 16:23:05 -04:00
|
|
|
Integer,
|
2015-08-01 17:50:06 -04:00
|
|
|
/// The string type.
|
2015-08-01 16:23:05 -04:00
|
|
|
String,
|
2015-08-02 20:34:27 -04:00
|
|
|
/// The null type.
|
|
|
|
Null,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A typed value.
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub enum Value {
|
|
|
|
/// Binary data.
|
|
|
|
Binary(Vec<u8>),
|
|
|
|
/// A floating-point number.
|
|
|
|
Float(f64),
|
2015-08-03 22:00:53 -04:00
|
|
|
/// An integer number.
|
2015-08-02 20:34:27 -04:00
|
|
|
Integer(i64),
|
|
|
|
/// A string.
|
|
|
|
String(String),
|
|
|
|
/// A null value.
|
|
|
|
Null,
|
2015-08-01 16:23:05 -04:00
|
|
|
}
|
|
|
|
|
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-08-03 22:00:53 -04:00
|
|
|
impl Value {
|
|
|
|
/// Return the binary data if the value is `Binary`.
|
|
|
|
#[inline]
|
|
|
|
pub fn as_binary(&self) -> Option<&[u8]> {
|
|
|
|
if let &Value::Binary(ref value) = self {
|
|
|
|
return Some(value);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the floating-point number if the value is `Float`.
|
|
|
|
#[inline]
|
|
|
|
pub fn as_float(&self) -> Option<f64> {
|
|
|
|
if let &Value::Float(value) = self {
|
|
|
|
return Some(value);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the integer number if the value is `Integer`.
|
|
|
|
#[inline]
|
|
|
|
pub fn as_integer(&self) -> Option<i64> {
|
|
|
|
if let &Value::Integer(value) = self {
|
|
|
|
return Some(value);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the string if the value is `String`.
|
|
|
|
#[inline]
|
|
|
|
pub fn as_string(&self) -> Option<&str> {
|
|
|
|
if let &Value::String(ref value) = self {
|
|
|
|
return Some(value);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2015-08-04 08:28:00 -04:00
|
|
|
|
|
|
|
/// Return the type.
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
2015-08-03 22:00:53 -04:00
|
|
|
}
|
|
|
|
|
2015-07-04 08:53:26 -04:00
|
|
|
mod connection;
|
2015-08-03 17:10:30 -04:00
|
|
|
mod cursor;
|
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-08-03 17:10:30 -04:00
|
|
|
pub use cursor::Cursor;
|
2017-08-26 07:52:28 +02:00
|
|
|
pub use statement::{Bindable, Readable, State, Statement};
|
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
|
|
|
|
2015-08-01 21:38:54 -04:00
|
|
|
/// Return the version number of SQLite.
|
|
|
|
///
|
|
|
|
/// For instance, the version `3.8.11.1` corresponds to the integer `3008011`.
|
|
|
|
#[inline]
|
|
|
|
pub fn version() -> usize {
|
|
|
|
unsafe { ffi::sqlite3_libversion_number() as usize }
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2017-08-22 17:34:03 +02:00
|
|
|
Some(Error {
|
|
|
|
code: Some(code as isize),
|
|
|
|
message: Some(c_str_to_string!(message)),
|
|
|
|
})
|
2015-08-01 17:38:08 -04:00
|
|
|
}
|
|
|
|
}
|