Merge branch 'query_row' of https://github.com/pfernie/rusqlite into pfernie-query_row

This commit is contained in:
John Gallagher 2016-12-30 23:42:38 -05:00
commit 3815b6beef

View File

@ -303,12 +303,12 @@ impl Connection {
/// Will return `Err` if `sql` cannot be converted to a C-compatible string or if the /// Will return `Err` if `sql` cannot be converted to a C-compatible string or if the
/// underlying SQLite call fails. /// underlying SQLite call fails.
pub fn query_row<T, F>(&self, sql: &str, params: &[&ToSql], f: F) -> Result<T> pub fn query_row<T, F>(&self, sql: &str, params: &[&ToSql], f: F) -> Result<T>
where F: FnOnce(Row) -> T where F: FnOnce(&Row) -> T
{ {
let mut stmt = try!(self.prepare(sql)); let mut stmt = try!(self.prepare(sql));
let mut rows = try!(stmt.query(params)); let mut rows = try!(stmt.query(params));
rows.get_expected_row().map(f) rows.get_expected_row().map(|r| f(&r))
} }
/// Convenience method to execute a query that is expected to return a single row, /// Convenience method to execute a query that is expected to return a single row,
@ -339,13 +339,13 @@ impl Connection {
params: &[&ToSql], params: &[&ToSql],
f: F) f: F)
-> result::Result<T, E> -> result::Result<T, E>
where F: FnOnce(Row) -> result::Result<T, E>, where F: FnOnce(&Row) -> result::Result<T, E>,
E: convert::From<Error> E: convert::From<Error>
{ {
let mut stmt = try!(self.prepare(sql)); let mut stmt = try!(self.prepare(sql));
let mut rows = try!(stmt.query(params)); let mut rows = try!(stmt.query(params));
rows.get_expected_row().map_err(E::from).and_then(f) rows.get_expected_row().map_err(E::from).and_then(|r| f(&r))
} }
/// Convenience method to execute a query that is expected to return a single row. /// Convenience method to execute a query that is expected to return a single row.
@ -369,7 +369,7 @@ impl Connection {
/// does exactly the same thing. /// does exactly the same thing.
#[deprecated(since = "0.1.0", note = "Use query_row instead")] #[deprecated(since = "0.1.0", note = "Use query_row instead")]
pub fn query_row_safe<T, F>(&self, sql: &str, params: &[&ToSql], f: F) -> Result<T> pub fn query_row_safe<T, F>(&self, sql: &str, params: &[&ToSql], f: F) -> Result<T>
where F: FnOnce(Row) -> T where F: FnOnce(&Row) -> T
{ {
self.query_row(sql, params, f) self.query_row(sql, params, f)
} }