2015-06-12 14:23:18 -04:00
|
|
|
use ffi;
|
2015-05-29 11:24:01 -04:00
|
|
|
use libc::{c_double, c_int};
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2015-08-01 16:23:05 -04:00
|
|
|
use {Result, Type};
|
2015-05-29 11:24:01 -04:00
|
|
|
|
|
|
|
/// A prepared statement.
|
|
|
|
pub struct Statement<'l> {
|
2015-06-12 14:23:18 -04:00
|
|
|
raw: (*mut ffi::sqlite3_stmt, *mut ffi::sqlite3),
|
|
|
|
phantom: PhantomData<(ffi::sqlite3_stmt, &'l ffi::sqlite3)>,
|
2015-05-29 11:24:01 -04:00
|
|
|
}
|
|
|
|
|
2015-06-19 11:34:03 -04:00
|
|
|
/// A state of a prepared statement.
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
pub enum State {
|
2015-06-19 13:03:54 -04:00
|
|
|
/// There is a row available for reading.
|
2015-06-19 11:34:03 -04:00
|
|
|
Row,
|
2015-06-19 13:03:54 -04:00
|
|
|
/// There is nothing else to read.
|
|
|
|
Done,
|
2015-06-19 11:34:03 -04:00
|
|
|
}
|
|
|
|
|
2015-06-19 11:31:29 -04:00
|
|
|
/// A parameter of a prepared statement.
|
|
|
|
pub trait Parameter {
|
|
|
|
/// Bind the parameter at a specific location.
|
|
|
|
///
|
|
|
|
/// The leftmost location has the index 1.
|
2015-06-19 13:10:09 -04:00
|
|
|
fn bind(&self, &mut Statement, usize) -> Result<()>;
|
2015-05-29 11:24:01 -04:00
|
|
|
}
|
|
|
|
|
2015-06-19 11:31:29 -04:00
|
|
|
/// A value stored in a prepared statement.
|
2015-05-29 13:08:02 -04:00
|
|
|
pub trait Value {
|
2015-06-08 07:53:28 -04:00
|
|
|
/// Read the value stored in a specific column.
|
2015-06-19 11:31:29 -04:00
|
|
|
///
|
|
|
|
/// The leftmost column has the index 0.
|
|
|
|
fn read(&Statement, usize) -> Result<Self>;
|
2015-05-29 13:08:02 -04:00
|
|
|
}
|
|
|
|
|
2015-05-29 11:24:01 -04:00
|
|
|
impl<'l> Statement<'l> {
|
2015-08-01 13:55:34 -04:00
|
|
|
/// Return the number of columns.
|
|
|
|
#[inline]
|
2015-08-01 16:23:05 -04:00
|
|
|
pub fn columns(&self) -> usize {
|
2015-08-01 13:55:34 -04:00
|
|
|
unsafe { ffi::sqlite3_column_count(self.raw.0) as usize }
|
|
|
|
}
|
|
|
|
|
2015-08-01 16:23:05 -04:00
|
|
|
/// Return the type of a column.
|
|
|
|
///
|
|
|
|
/// The type is revealed after the first step has been taken.
|
|
|
|
#[inline]
|
|
|
|
pub fn kind(&self, i: usize) -> Type {
|
|
|
|
match unsafe { ffi::sqlite3_column_type(self.raw.0, i as c_int) } {
|
|
|
|
ffi::SQLITE_BLOB => Type::Blob,
|
|
|
|
ffi::SQLITE_FLOAT => Type::Float,
|
|
|
|
ffi::SQLITE_INTEGER => Type::Integer,
|
|
|
|
ffi::SQLITE_NULL => Type::Null,
|
|
|
|
ffi::SQLITE_TEXT => Type::String,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 11:31:29 -04:00
|
|
|
/// Bind the parameter at a specific location.
|
2015-05-29 13:34:48 -04:00
|
|
|
///
|
2015-06-19 11:31:29 -04:00
|
|
|
/// The leftmost location has the index 1.
|
|
|
|
#[inline]
|
2015-06-19 13:15:42 -04:00
|
|
|
pub fn bind<T: Parameter>(&mut self, i: usize, parameter: T) -> Result<()> {
|
2015-06-19 11:31:29 -04:00
|
|
|
parameter.bind(self, i)
|
2015-05-29 11:24:01 -04:00
|
|
|
}
|
|
|
|
|
2015-06-19 11:31:29 -04:00
|
|
|
/// Read the value stored in a specific column.
|
2015-06-08 07:53:28 -04:00
|
|
|
///
|
|
|
|
/// The leftmost column has the index 0.
|
2015-05-29 13:08:02 -04:00
|
|
|
#[inline]
|
2015-06-19 13:15:42 -04:00
|
|
|
pub fn read<T: Value>(&self, i: usize) -> Result<T> {
|
2015-06-19 11:31:29 -04:00
|
|
|
Value::read(self, i)
|
2015-05-29 13:08:02 -04:00
|
|
|
}
|
|
|
|
|
2015-07-04 09:21:38 -04:00
|
|
|
/// Evaluate the statement one step at a time.
|
|
|
|
///
|
|
|
|
/// The function should be called multiple times until `State::Done` is
|
|
|
|
/// reached in order to evaluate the statement entirely.
|
2015-06-08 17:43:31 -04:00
|
|
|
pub fn step(&mut self) -> Result<State> {
|
2015-06-12 14:23:18 -04:00
|
|
|
match unsafe { ffi::sqlite3_step(self.raw.0) } {
|
|
|
|
ffi::SQLITE_DONE => Ok(State::Done),
|
|
|
|
ffi::SQLITE_ROW => Ok(State::Row),
|
2015-06-19 20:30:28 -04:00
|
|
|
code => error!(self.raw.1, code),
|
2015-06-08 17:43:31 -04:00
|
|
|
}
|
2015-05-29 11:24:01 -04:00
|
|
|
}
|
|
|
|
|
2015-06-08 07:53:28 -04:00
|
|
|
/// Reset the statement.
|
2015-05-29 11:24:01 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn reset(&mut self) -> Result<()> {
|
2015-06-19 20:30:28 -04:00
|
|
|
unsafe { ok!(self.raw.1, ffi::sqlite3_reset(self.raw.0)) };
|
2015-05-29 11:24:01 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'l> Drop for Statement<'l> {
|
|
|
|
#[inline]
|
|
|
|
fn drop(&mut self) {
|
2015-06-12 14:23:18 -04:00
|
|
|
unsafe { ffi::sqlite3_finalize(self.raw.0) };
|
2015-05-29 11:24:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 11:31:29 -04:00
|
|
|
impl Parameter for f64 {
|
|
|
|
#[inline]
|
2015-06-19 13:10:09 -04:00
|
|
|
fn bind(&self, statement: &mut Statement, i: usize) -> Result<()> {
|
2015-06-19 11:31:29 -04:00
|
|
|
debug_assert!(i > 0, "the indexing starts from 1");
|
|
|
|
unsafe {
|
2015-06-19 20:30:28 -04:00
|
|
|
ok!(statement.raw.1, ffi::sqlite3_bind_double(statement.raw.0, i as c_int,
|
|
|
|
*self as c_double));
|
2015-06-19 11:31:29 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parameter for i64 {
|
|
|
|
#[inline]
|
2015-06-19 13:10:09 -04:00
|
|
|
fn bind(&self, statement: &mut Statement, i: usize) -> Result<()> {
|
2015-06-19 11:31:29 -04:00
|
|
|
debug_assert!(i > 0, "the indexing starts from 1");
|
|
|
|
unsafe {
|
2015-06-19 20:30:28 -04:00
|
|
|
ok!(statement.raw.1, ffi::sqlite3_bind_int64(statement.raw.0, i as c_int,
|
|
|
|
*self as ffi::sqlite3_int64));
|
2015-06-19 11:31:29 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'l> Parameter for &'l str {
|
|
|
|
#[inline]
|
2015-06-19 13:10:09 -04:00
|
|
|
fn bind(&self, statement: &mut Statement, i: usize) -> Result<()> {
|
2015-06-19 11:31:29 -04:00
|
|
|
debug_assert!(i > 0, "the indexing starts from 1");
|
|
|
|
unsafe {
|
2015-06-19 20:30:28 -04:00
|
|
|
ok!(statement.raw.1, ffi::sqlite3_bind_text(statement.raw.0, i as c_int,
|
2015-07-27 11:38:54 -04:00
|
|
|
str_to_cstr!(*self).as_ptr(), -1, None));
|
2015-06-19 11:31:29 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-01 17:19:21 -04:00
|
|
|
impl<'l> Parameter for &'l [u8] {
|
|
|
|
#[inline]
|
|
|
|
fn bind(&self, statement: &mut Statement, i: usize) -> Result<()> {
|
|
|
|
debug_assert!(i > 0, "the indexing starts from 1");
|
|
|
|
unsafe {
|
|
|
|
ok!(statement.raw.1, ffi::sqlite3_bind_blob(statement.raw.0, i as c_int,
|
|
|
|
self.as_ptr() as *const _,
|
|
|
|
self.len() as c_int, None));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 13:08:02 -04:00
|
|
|
impl Value for f64 {
|
2015-06-19 11:31:29 -04:00
|
|
|
#[inline]
|
2015-08-01 17:19:21 -04:00
|
|
|
fn read(statement: &Statement, i: usize) -> Result<Self> {
|
2015-06-12 14:23:18 -04:00
|
|
|
Ok(unsafe { ffi::sqlite3_column_double(statement.raw.0, i as c_int) as f64 })
|
2015-05-29 13:08:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value for i64 {
|
2015-06-19 11:31:29 -04:00
|
|
|
#[inline]
|
2015-08-01 17:19:21 -04:00
|
|
|
fn read(statement: &Statement, i: usize) -> Result<Self> {
|
2015-06-12 14:23:18 -04:00
|
|
|
Ok(unsafe { ffi::sqlite3_column_int64(statement.raw.0, i as c_int) as i64 })
|
2015-05-29 13:08:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value for String {
|
2015-06-19 11:31:29 -04:00
|
|
|
#[inline]
|
2015-08-01 17:19:21 -04:00
|
|
|
fn read(statement: &Statement, i: usize) -> Result<Self> {
|
2015-05-29 13:08:02 -04:00
|
|
|
unsafe {
|
2015-06-12 14:23:18 -04:00
|
|
|
let pointer = ffi::sqlite3_column_text(statement.raw.0, i as c_int);
|
2015-05-29 13:08:02 -04:00
|
|
|
if pointer.is_null() {
|
2015-06-19 20:24:34 -04:00
|
|
|
raise!("cannot read a text column");
|
2015-05-29 13:08:02 -04:00
|
|
|
}
|
|
|
|
Ok(c_str_to_string!(pointer))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-01 17:19:21 -04:00
|
|
|
impl Value for Vec<u8> {
|
|
|
|
#[inline]
|
|
|
|
fn read(statement: &Statement, i: usize) -> Result<Self> {
|
|
|
|
use std::ptr::copy_nonoverlapping as copy;
|
|
|
|
unsafe {
|
|
|
|
let pointer = ffi::sqlite3_column_blob(statement.raw.0, i as c_int);
|
|
|
|
if pointer.is_null() {
|
|
|
|
return Ok(vec![]);
|
|
|
|
}
|
|
|
|
let count = ffi::sqlite3_column_bytes(statement.raw.0, i as c_int) as usize;
|
|
|
|
let mut buffer = Vec::with_capacity(count);
|
|
|
|
buffer.set_len(count);
|
|
|
|
copy(pointer as *const u8, buffer.as_mut_ptr(), count);
|
|
|
|
Ok(buffer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 11:24:01 -04:00
|
|
|
#[inline]
|
2015-07-31 16:05:52 -04:00
|
|
|
pub fn new<'l, T: AsRef<str>>(raw1: *mut ffi::sqlite3, query: T) -> Result<Statement<'l>> {
|
2015-06-08 17:43:31 -04:00
|
|
|
let mut raw0 = 0 as *mut _;
|
2015-05-29 16:58:48 -04:00
|
|
|
unsafe {
|
2015-07-31 16:05:52 -04:00
|
|
|
ok!(raw1, ffi::sqlite3_prepare_v2(raw1, str_to_cstr!(query.as_ref()).as_ptr(), -1,
|
|
|
|
&mut raw0, 0 as *mut _));
|
2015-05-29 16:58:48 -04:00
|
|
|
}
|
2015-06-08 17:43:31 -04:00
|
|
|
Ok(Statement { raw: (raw0, raw1), phantom: PhantomData })
|
2015-05-29 11:24:01 -04:00
|
|
|
}
|