mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-26 03:11:36 +08:00
Give MappedRows a SqliteRows instead of a SqliteStatement.
This commit is contained in:
parent
3f75300844
commit
ea911fbdbd
39
src/lib.rs
39
src/lib.rs
@ -639,15 +639,13 @@ impl<'conn> SqliteStatement<'conn> {
|
|||||||
pub fn query_map<'a, 'map, T, F>(&'a mut self, params: &[&ToSql], f: F)
|
pub fn query_map<'a, 'map, T, F>(&'a mut self, params: &[&ToSql], f: F)
|
||||||
-> SqliteResult<MappedRows<'a, 'map, T>>
|
-> SqliteResult<MappedRows<'a, 'map, T>>
|
||||||
where T: 'static,
|
where T: 'static,
|
||||||
F: 'map + Fn(MappedRow) -> T {
|
F: 'map + Fn(SqliteRow) -> T {
|
||||||
self.reset_if_needed();
|
self.reset_if_needed();
|
||||||
|
|
||||||
unsafe {
|
let rows = try!(self.query(params));
|
||||||
try!(self.bind_parameters(params));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(MappedRows{
|
Ok(MappedRows{
|
||||||
stmt: self,
|
rows: rows,
|
||||||
map: Box::new(f),
|
map: Box::new(f),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -703,40 +701,15 @@ impl<'conn> Drop for SqliteStatement<'conn> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct MappedRows<'stmt, 'map, T> {
|
pub struct MappedRows<'stmt, 'map, T> {
|
||||||
stmt: &'stmt SqliteStatement<'stmt>,
|
rows: SqliteRows<'stmt>,
|
||||||
map: Box<Fn(MappedRow) -> T + 'map>,
|
map: Box<Fn(SqliteRow<'stmt>) -> T + 'map>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'stmt, 'map, T: 'static> Iterator for MappedRows<'stmt, 'map, T> {
|
impl<'stmt, 'map, T: 'static> Iterator for MappedRows<'stmt, 'map, T> {
|
||||||
type Item = SqliteResult<T>;
|
type Item = SqliteResult<T>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<SqliteResult<T>> {
|
fn next(&mut self) -> Option<SqliteResult<T>> {
|
||||||
match unsafe { ffi::sqlite3_step(self.stmt.stmt) } {
|
self.rows.next().map(|row_result| row_result.map(|row| (*self.map)(row)))
|
||||||
ffi::SQLITE_ROW => {
|
|
||||||
Some(Ok((*self.map)(MappedRow(self.stmt))))
|
|
||||||
},
|
|
||||||
ffi::SQLITE_DONE => None,
|
|
||||||
code => {
|
|
||||||
Some(Err(self.stmt.conn.decode_result(code).unwrap_err()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct MappedRow<'stmt>(&'stmt SqliteStatement<'stmt>);
|
|
||||||
|
|
||||||
impl<'stmt> MappedRow<'stmt> {
|
|
||||||
pub fn get<T: FromSql>(&self, idx: c_int) -> T {
|
|
||||||
self.get_opt(idx).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_opt<T: FromSql>(&self, idx: c_int) -> SqliteResult<T> {
|
|
||||||
// Do assertions because these are logic errors.
|
|
||||||
// We can probably skip them in release builds.
|
|
||||||
assert!(idx >= 0);
|
|
||||||
assert!(idx < unsafe { ffi::sqlite3_column_count(self.0.stmt) });
|
|
||||||
|
|
||||||
unsafe { FromSql::column_result(self.0.stmt, idx) }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user