use libc::{c_double, c_int}; use raw; use std::marker::PhantomData; use {Database, Result, ResultCode}; /// A prepared statement. pub struct Statement<'l> { raw: *mut raw::sqlite3_stmt, phantom: PhantomData<(&'l raw::sqlite3, raw::sqlite3_stmt)>, } /// A binding of a parameter of a prepared statement. pub enum Binding<'l> { Float(usize, f64), Integer(usize, i64), Text(usize, &'l str), } /// A value stored in a result row of a query. pub trait Value { /// Read the value stored in a specific column. fn read(statement: &Statement, i: usize) -> Result; } impl<'l> Statement<'l> { /// Bind values to the parameters. /// /// The leftmost parameter has the index 1. pub fn bind(&mut self, bindings: &[Binding]) -> Result<()> { for binding in bindings.iter() { match *binding { Binding::Float(i, value) => unsafe { debug_assert!(i > 0, "the indexing starts from 1"); success!(raw::sqlite3_bind_double(self.raw, i as c_int, value as c_double)); }, Binding::Integer(i, value) => unsafe { debug_assert!(i > 0, "the indexing starts from 1"); success!(raw::sqlite3_bind_int64(self.raw, i as c_int, value as raw::sqlite3_int64)); }, Binding::Text(i, value) => unsafe { debug_assert!(i > 0, "the indexing starts from 1"); success!(raw::sqlite3_bind_text(self.raw, i as c_int, str_to_c_str!(value), -1, None)); }, } } Ok(()) } /// Return the value stored in a specific column of the current result row. /// /// The leftmost column has the index 0. #[inline] pub fn column(&self, i: usize) -> Result { ::read(self, i) } /// Evaluate the statement. #[inline] pub fn step(&mut self) -> ResultCode { unsafe { ::result::code_from_raw(raw::sqlite3_step(self.raw)) } } /// Reset the statement. #[inline] pub fn reset(&mut self) -> Result<()> { unsafe { success!(raw::sqlite3_reset(self.raw)) }; Ok(()) } } impl<'l> Drop for Statement<'l> { #[inline] fn drop(&mut self) { unsafe { ::raw::sqlite3_finalize(self.raw) }; } } impl Value for f64 { fn read(statement: &Statement, i: usize) -> Result { Ok(unsafe { ::raw::sqlite3_column_double(statement.raw, i as c_int) as f64 }) } } impl Value for i64 { fn read(statement: &Statement, i: usize) -> Result { Ok(unsafe { ::raw::sqlite3_column_int64(statement.raw, i as c_int) as i64 }) } } impl Value for String { fn read(statement: &Statement, i: usize) -> Result { unsafe { let pointer = ::raw::sqlite3_column_text(statement.raw, i as c_int); if pointer.is_null() { raise!("cannot read a TEXT column"); } Ok(c_str_to_string!(pointer)) } } } #[inline] pub fn new<'l>(database: &'l Database, sql: &str) -> Result> { let mut raw = 0 as *mut _; unsafe { success!(database, raw::sqlite3_prepare(::database::as_raw(database), str_to_c_str!(sql), -1, &mut raw, 0 as *mut _)); } Ok(Statement { raw: raw, phantom: PhantomData }) }