diff --git a/README.md b/README.md index 74406a6..8121a67 100644 --- a/README.md +++ b/README.md @@ -41,14 +41,17 @@ fn main() { &[&me.name, &me.time_created, &me.data]).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 person = Person { + let mut person_iter = stmt.query_map(&[], |row| { + Person { id: row.get(0), name: row.get(1), time_created: row.get(2), 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 { 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. +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 John Gallagher, johnkgallagher@gmail.com diff --git a/src/lib.rs b/src/lib.rs index 3edb910..ee49605 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,14 +36,17 @@ //! &[&me.name, &me.time_created, &me.data]).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 person = Person { +//! let mut person_iter = stmt.query_map(&[], |row| { +//! Person { //! id: row.get(0), //! name: row.get(1), //! time_created: row.get(2), //! 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)) } - pub fn query_map<'a, 'map, T, F>(&'a mut self, params: &[&ToSql], f: F) - -> SqliteResult> + /// Executes the prepared statement and maps a function over the resulting + /// 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> where T: 'static, - F: 'map + Fn(SqliteRow) -> T { - self.reset_if_needed(); - - let rows = try!(self.query(params)); + F: FnMut(SqliteRow) -> T { + let row_iter = try!(self.query(params)); Ok(MappedRows{ - rows: rows, - map: Box::new(f), + rows: row_iter, + 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>, - map: Box) -> 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; fn next(&mut self) -> Option> { - 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))) } }