Extract RawStatement wrapper around *mut sqlite3_stmt.

This commit is contained in:
John Gallagher
2016-05-17 10:06:43 -05:00
parent b76196ae1a
commit f6aba80f4b
3 changed files with 106 additions and 51 deletions

View File

@@ -1,7 +1,5 @@
use libc::c_int;
use super::ffi;
use {Result, Error, Connection, Statement, Rows, Row, str_to_cstring};
use types::ToSql;
@@ -56,11 +54,7 @@ impl<'conn> Statement<'conn> {
/// is valid but not a bound parameter of this statement.
pub fn parameter_index(&self, name: &str) -> Result<Option<i32>> {
let c_name = try!(str_to_cstring(name));
let c_index = unsafe { ffi::sqlite3_bind_parameter_index(self.stmt, c_name.as_ptr()) };
Ok(match c_index {
0 => None, // A zero is returned if no matching parameter is found.
n => Some(n),
})
Ok(self.stmt.bind_parameter_index(&c_name))
}
/// Execute the prepared statement with named parameter(s). If any parameters
@@ -87,7 +81,7 @@ impl<'conn> Statement<'conn> {
/// which case `query` should be used instead), or the underling SQLite call fails.
pub fn execute_named(&mut self, params: &[(&str, &ToSql)]) -> Result<c_int> {
try!(self.bind_parameters_named(params));
unsafe { self.execute_() }
self.execute_()
}
/// Execute the prepared statement with named parameter(s), returning an iterator over the
@@ -120,7 +114,7 @@ impl<'conn> Statement<'conn> {
fn bind_parameters_named(&mut self, params: &[(&str, &ToSql)]) -> Result<()> {
for &(name, value) in params {
if let Some(i) = try!(self.parameter_index(name)) {
try!(self.conn.decode_result(unsafe { value.bind_parameter(self.stmt, i) }));
try!(self.conn.decode_result(unsafe { value.bind_parameter(self.stmt.ptr(), i) }));
} else {
return Err(Error::InvalidParameterName(name.into()));
}