diff --git a/Cargo.toml b/Cargo.toml index 5d05ed5..18c2bd3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ name = "rusqlite" [features] load_extension = ["libsqlite3-sys/load_extension"] +named_params = [] [dependencies] time = "~0.1.0" diff --git a/src/lib.rs b/src/lib.rs index 3dd84ae..f2d1791 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -79,6 +79,7 @@ pub use transaction::{SqliteTransactionBehavior, pub mod types; mod transaction; #[cfg(feature = "load_extension")] mod load_extension_guard; +#[cfg(feature = "named_params")] pub mod named_params; /// A typedef of the result returned by many methods. pub type SqliteResult = Result; @@ -615,15 +616,18 @@ impl<'conn> SqliteStatement<'conn> { unsafe { try!(self.bind_parameters(params)); + self.execute_() + } + } - self.needs_reset = true; - let r = ffi::sqlite3_step(self.stmt); - match r { - ffi::SQLITE_DONE => Ok(self.conn.changes()), - ffi::SQLITE_ROW => Err(SqliteError{ code: r, - message: "Unexpected row result - did you mean to call query?".to_string() }), - _ => Err(self.conn.decode_result(r).unwrap_err()), - } + unsafe fn execute_(&mut self) -> SqliteResult { + let r = ffi::sqlite3_step(self.stmt); + ffi::sqlite3_reset(self.stmt); + match r { + ffi::SQLITE_DONE => Ok(self.conn.changes()), + ffi::SQLITE_ROW => Err(SqliteError{ code: r, + message: "Unexpected row result - did you mean to call query?".to_string() }), + _ => Err(self.conn.decode_result(r).unwrap_err()), } } diff --git a/src/named_params.rs b/src/named_params.rs new file mode 100644 index 0000000..b8e78fa --- /dev/null +++ b/src/named_params.rs @@ -0,0 +1,72 @@ +//use std::collections::HashMap; +use std::ffi::{CString}; +use libc::{c_int}; + +use super::ffi; + +use {SqliteResult, SqliteError, SqliteConnection, SqliteStatement}; +use types::{ToSql}; + +impl SqliteConnection { +} + +impl<'conn> SqliteStatement<'conn> { + /*pub fn parameter_names(&self) -> HashMap { + let n = unsafe { ffi::sqlite3_bind_parameter_count(self.stmt) }; + let mut index_by_name = HashMap::with_capacity(n as usize); + for i in 1..n+1 { + let c_name = unsafe { ffi::sqlite3_bind_parameter_name(self.stmt, i) }; + if !c_name.is_null() { + let c_slice = unsafe { CStr::from_ptr(c_name).to_bytes() }; + index_by_name.insert(str::from_utf8(c_slice).unwrap().to_string(), n); + } + } + index_by_name + }*/ + + /// Return the index of an SQL parameter given its name. + /// Return None if `name` is invalid (NulError) or if no matching parameter is found. + pub fn parameter_index(&self, name: &str) -> Option { + unsafe { + CString::new(name).ok().and_then(|c_name| + match ffi::sqlite3_bind_parameter_index(self.stmt, c_name.as_ptr()) { + 0 => None, // A zero is returned if no matching parameter is found. + n => Some(n) + } + ) + + } + } + + /// Execute the prepared statement with named parameter(s). + /// + /// On success, returns the number of rows that were changed or inserted or deleted (via + /// `sqlite3_changes`). + pub fn named_execute(&mut self, params: &[(&str, &ToSql)]) -> SqliteResult { + unsafe { + for &(name, value) in params { + let i = try!(self.parameter_index(name).ok_or(SqliteError{ + code: ffi::SQLITE_MISUSE, + message: format!("Invalid parameter name: {}", name) + })); + try!(self.conn.decode_result(value.bind_parameter(self.stmt, i))); + } + self.execute_() + } + } +} + +#[cfg(test)] +mod test { + use SqliteConnection; + + #[test] + fn test_named_execute() { + let db = SqliteConnection::open_in_memory().unwrap(); + let sql = "CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag INTEGER)"; + db.execute_batch(sql).unwrap(); + + let mut stmt = db.prepare("INSERT INTO test (id, name, flag) VALUES (:id, :name, :flag)").unwrap(); + stmt.named_execute(&[(":name", &"one")]).unwrap(); + } +} \ No newline at end of file