Slightly adjust the signature of query_map

This commit is contained in:
Marcus Klaas 2015-05-07 15:36:29 +02:00 committed by John Gallagher
parent ea911fbdbd
commit 51f6c15c6c
2 changed files with 36 additions and 20 deletions

View File

@ -41,14 +41,17 @@ fn main() {
&[&me.name, &me.time_created, &me.data]).unwrap(); &[&me.name, &me.time_created, &me.data]).unwrap();
let mut stmt = conn.prepare("SELECT id, name, time_created, data FROM person").unwrap(); let mut stmt = conn.prepare("SELECT id, name, time_created, data FROM person").unwrap();
for row in stmt.query(&[]).unwrap().map(|row| row.unwrap()) { let mut person_iter = stmt.query_map(&[], |row| {
let person = Person { Person {
id: row.get(0), id: row.get(0),
name: row.get(1), name: row.get(1),
time_created: row.get(2), time_created: row.get(2),
data: row.get(3) data: row.get(3)
}; }
println!("Found person {:?}", person); }).unwrap();
for person in person_iter {
println!("Found person {:?}", person.unwrap());
} }
} }
``` ```
@ -87,6 +90,10 @@ fn bad_function_will_panic(conn: &SqliteConnection) -> SqliteResult<i64> {
There are other, less obvious things that may result in a panic as well, such as calling There are other, less obvious things that may result in a panic as well, such as calling
`collect()` on a `SqliteRows` and then trying to use the collected rows. `collect()` on a `SqliteRows` and then trying to use the collected rows.
The method `query_map()` is an alternative to `query()` and is guaranteed not to panic. This method
returns an iterator over rows after they have been mapped to a static type, e.g., types without
references to other values.
## Author ## Author
John Gallagher, johnkgallagher@gmail.com John Gallagher, johnkgallagher@gmail.com

View File

@ -36,14 +36,17 @@
//! &[&me.name, &me.time_created, &me.data]).unwrap(); //! &[&me.name, &me.time_created, &me.data]).unwrap();
//! //!
//! let mut stmt = conn.prepare("SELECT id, name, time_created, data FROM person").unwrap(); //! let mut stmt = conn.prepare("SELECT id, name, time_created, data FROM person").unwrap();
//! for row in stmt.query(&[]).unwrap().map(|row| row.unwrap()) { //! let mut person_iter = stmt.query_map(&[], |row| {
//! let person = Person { //! Person {
//! id: row.get(0), //! id: row.get(0),
//! name: row.get(1), //! name: row.get(1),
//! time_created: row.get(2), //! time_created: row.get(2),
//! data: row.get(3) //! data: row.get(3)
//! }; //! }
//! println!("Found person {:?}", person); //! }).unwrap();
//!
//! for person in person_iter {
//! println!("Found person {:?}", person.unwrap());
//! } //! }
//! } //! }
//! ``` //! ```
@ -636,17 +639,20 @@ impl<'conn> SqliteStatement<'conn> {
Ok(SqliteRows::new(self)) Ok(SqliteRows::new(self))
} }
pub fn query_map<'a, 'map, T, F>(&'a mut self, params: &[&ToSql], f: F) /// Executes the prepared statement and maps a function over the resulting
-> SqliteResult<MappedRows<'a, 'map, T>> /// rows.
///
/// Unlike the iterator produced by `query`, the returned iterator does not expose the possibility
/// for accessing stale rows.
pub fn query_map<'a, T, F>(&'a mut self, params: &[&ToSql], f: F)
-> SqliteResult<MappedRows<'a, F>>
where T: 'static, where T: 'static,
F: 'map + Fn(SqliteRow) -> T { F: FnMut(SqliteRow) -> T {
self.reset_if_needed(); let row_iter = try!(self.query(params));
let rows = try!(self.query(params));
Ok(MappedRows{ Ok(MappedRows{
rows: rows, rows: row_iter,
map: Box::new(f), map: f,
}) })
} }
@ -700,16 +706,19 @@ impl<'conn> Drop for SqliteStatement<'conn> {
} }
} }
pub struct MappedRows<'stmt, 'map, T> { /// An iterator over the mapped resulting rows of a query.
pub struct MappedRows<'stmt, F> {
rows: SqliteRows<'stmt>, rows: SqliteRows<'stmt>,
map: Box<Fn(SqliteRow<'stmt>) -> T + 'map>, map: F,
} }
impl<'stmt, 'map, T: 'static> Iterator for MappedRows<'stmt, 'map, T> { impl<'stmt, T, F> Iterator for MappedRows<'stmt, F>
where T: 'static,
F: FnMut(SqliteRow) -> T {
type Item = SqliteResult<T>; type Item = SqliteResult<T>;
fn next(&mut self) -> Option<SqliteResult<T>> { fn next(&mut self) -> Option<SqliteResult<T>> {
self.rows.next().map(|row_result| row_result.map(|row| (*self.map)(row))) self.rows.next().map(|row_result| row_result.map(|row| (self.map)(row)))
} }
} }