Add SqliteConnection.execute_named,query_named_row.

This commit is contained in:
Gwenael Treguier 2015-08-07 02:19:57 +02:00
parent 7ff9d4056b
commit 3b673a74b2

View File

@ -4,10 +4,31 @@ use libc::{c_int};
use super::ffi; use super::ffi;
use {SqliteResult, SqliteError, SqliteConnection, SqliteStatement, SqliteRows}; use {SqliteResult, SqliteError, SqliteConnection, SqliteStatement, SqliteRows, SqliteRow};
use types::{ToSql}; use types::{ToSql};
impl SqliteConnection { impl SqliteConnection {
/// Convenience method to prepare and execute a single SQL statement with named parameter(s).
pub fn execute_named(&self, sql: &str, params: &[(&str, &ToSql)]) -> SqliteResult<c_int> {
self.prepare(sql).and_then(|mut stmt| stmt.execute_named(params))
}
/// Convenience method to execute a query with named parameter(s) that is expected to return a single row.
///
/// If the query returns more than one row, all rows except the first are ignored.
pub fn query_named_row<T, F>(&self, sql: &str, params: &[(&str, &ToSql)], f: F) -> SqliteResult<T>
where F: FnOnce(SqliteRow) -> T {
let mut stmt = try!(self.prepare(sql));
let mut rows = try!(stmt.query_named(params));
match rows.next() {
Some(row) => row.map(f),
None => Err(SqliteError{
code: ffi::SQLITE_NOTICE,
message: "Query did not return a row".to_string(),
})
}
}
} }
impl<'conn> SqliteStatement<'conn> { impl<'conn> SqliteStatement<'conn> {
@ -42,7 +63,7 @@ impl<'conn> SqliteStatement<'conn> {
/// ///
/// On success, returns the number of rows that were changed or inserted or deleted (via /// On success, returns the number of rows that were changed or inserted or deleted (via
/// `sqlite3_changes`). /// `sqlite3_changes`).
pub fn named_execute(&mut self, params: &[(&str, &ToSql)]) -> SqliteResult<c_int> { pub fn execute_named(&mut self, params: &[(&str, &ToSql)]) -> SqliteResult<c_int> {
unsafe { unsafe {
try!(self.bind_named_parameters(params)); try!(self.bind_named_parameters(params));
self.execute_() self.execute_()
@ -50,7 +71,7 @@ impl<'conn> SqliteStatement<'conn> {
} }
/// Execute the prepared statement with named parameter(s), returning an iterator over the resulting rows. /// Execute the prepared statement with named parameter(s), returning an iterator over the resulting rows.
pub fn named_query<'a>(&'a mut self, params: &[(&str, &ToSql)]) -> SqliteResult<SqliteRows<'a>> { pub fn query_named<'a>(&'a mut self, params: &[(&str, &ToSql)]) -> SqliteResult<SqliteRows<'a>> {
self.reset_if_needed(); self.reset_if_needed();
unsafe { unsafe {
@ -77,23 +98,34 @@ impl<'conn> SqliteStatement<'conn> {
mod test { mod test {
use SqliteConnection; use SqliteConnection;
#[test]
fn test_execute_named() {
let db = SqliteConnection::open_in_memory().unwrap();
db.execute_batch("CREATE TABLE foo(x INTEGER)").unwrap();
assert_eq!(db.execute_named("INSERT INTO foo(x) VALUES (:x)", &[(":x", &1i32)]).unwrap(), 1);
assert_eq!(db.execute_named("INSERT INTO foo(x) VALUES (:x)", &[(":x", &2i32)]).unwrap(), 1);
assert_eq!(3i32, db.query_named_row("SELECT SUM(x) FROM foo WHERE x > :x", &[(":x", &0i32)], |r| r.get(0)).unwrap());
}
#[test] #[test]
fn test_named_execute() { fn test_stmt_execute_named() {
let db = SqliteConnection::open_in_memory().unwrap(); let db = SqliteConnection::open_in_memory().unwrap();
let sql = "CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag INTEGER)"; let sql = "CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag INTEGER)";
db.execute_batch(sql).unwrap(); db.execute_batch(sql).unwrap();
let mut stmt = db.prepare("INSERT INTO test (id, name, flag) VALUES (:id, :name, :flag)").unwrap(); let mut stmt = db.prepare("INSERT INTO test (id, name, flag) VALUES (:id, :name, :flag)").unwrap();
stmt.named_execute(&[(":name", &"one")]).unwrap(); stmt.execute_named(&[(":name", &"one")]).unwrap();
} }
#[test] #[test]
fn test_named_query() { fn test_query_named() {
let db = SqliteConnection::open_in_memory().unwrap(); let db = SqliteConnection::open_in_memory().unwrap();
let sql = "CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag INTEGER)"; let sql = "CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag INTEGER)";
db.execute_batch(sql).unwrap(); db.execute_batch(sql).unwrap();
let mut stmt = db.prepare("SELECT * FROM test where name = :name").unwrap(); let mut stmt = db.prepare("SELECT * FROM test where name = :name").unwrap();
stmt.named_query(&[(":name", &"one")]).unwrap(); stmt.query_named(&[(":name", &"one")]).unwrap();
} }
} }