mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-26 11:31:37 +08:00
Add Stmt::named_execute and Stmt::parameter_index.
This commit is contained in:
parent
8bb624cccc
commit
e81757e86d
@ -14,6 +14,7 @@ name = "rusqlite"
|
||||
|
||||
[features]
|
||||
load_extension = ["libsqlite3-sys/load_extension"]
|
||||
named_params = []
|
||||
|
||||
[dependencies]
|
||||
time = "~0.1.0"
|
||||
|
20
src/lib.rs
20
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<T> = Result<T, SqliteError>;
|
||||
@ -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<c_int> {
|
||||
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()),
|
||||
}
|
||||
}
|
||||
|
||||
|
72
src/named_params.rs
Normal file
72
src/named_params.rs
Normal file
@ -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<String, i32> {
|
||||
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<i32> {
|
||||
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<c_int> {
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user