mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
Add initial implementation for query_map
This commit is contained in:
parent
0bddece5b7
commit
95f511d437
97
src/lib.rs
97
src/lib.rs
@ -630,6 +630,37 @@ impl<'conn> SqliteStatement<'conn> {
|
|||||||
self.reset_if_needed();
|
self.reset_if_needed();
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
|
try!(self.bind_parameters(params));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(SqliteRows::new(self))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn query_map<'a, 'map, T, F>(&'a mut self, params: &[&ToSql], f: &'map F)
|
||||||
|
-> SqliteResult<MappedRows<'a, 'map, T>>
|
||||||
|
where T: 'static,
|
||||||
|
F: Fn(MappedRow) -> T {
|
||||||
|
self.reset_if_needed();
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
try!(self.bind_parameters(params));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(MappedRows {
|
||||||
|
stmt: self,
|
||||||
|
map: f
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Consumes the statement.
|
||||||
|
///
|
||||||
|
/// Functionally equivalent to the `Drop` implementation, but allows callers to see any errors
|
||||||
|
/// that occur.
|
||||||
|
pub fn finalize(mut self) -> SqliteResult<()> {
|
||||||
|
self.finalize_()
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn bind_parameters(&mut self, params: &[&ToSql]) -> SqliteResult<()> {
|
||||||
assert!(params.len() as c_int == ffi::sqlite3_bind_parameter_count(self.stmt),
|
assert!(params.len() as c_int == ffi::sqlite3_bind_parameter_count(self.stmt),
|
||||||
"incorrect number of parameters to query(): expected {}, got {}",
|
"incorrect number of parameters to query(): expected {}, got {}",
|
||||||
ffi::sqlite3_bind_parameter_count(self.stmt),
|
ffi::sqlite3_bind_parameter_count(self.stmt),
|
||||||
@ -640,16 +671,8 @@ impl<'conn> SqliteStatement<'conn> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.needs_reset = true;
|
self.needs_reset = true;
|
||||||
Ok(SqliteRows::new(self))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Consumes the statement.
|
Ok(())
|
||||||
///
|
|
||||||
/// Functionally equivalent to the `Drop` implementation, but allows callers to see any errors
|
|
||||||
/// that occur.
|
|
||||||
pub fn finalize(mut self) -> SqliteResult<()> {
|
|
||||||
self.finalize_()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reset_if_needed(&mut self) {
|
fn reset_if_needed(&mut self) {
|
||||||
@ -679,6 +702,44 @@ impl<'conn> Drop for SqliteStatement<'conn> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct MappedRows<'stmt, 'map, T> {
|
||||||
|
stmt: &'stmt SqliteStatement<'stmt>,
|
||||||
|
map: &'map Fn(MappedRow) -> T
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'stmt, 'map, T: 'static> Iterator for MappedRows<'stmt, 'map, T> {
|
||||||
|
type Item = SqliteResult<T>;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<SqliteResult<T>> {
|
||||||
|
match unsafe { ffi::sqlite3_step(self.stmt.stmt) } {
|
||||||
|
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) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// An iterator over the resulting rows of a query.
|
/// An iterator over the resulting rows of a query.
|
||||||
///
|
///
|
||||||
/// ## Warning
|
/// ## Warning
|
||||||
@ -947,6 +1008,24 @@ mod test {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_query_map() {
|
||||||
|
let db = checked_memory_handle();
|
||||||
|
let sql = "BEGIN;
|
||||||
|
CREATE TABLE foo(x INTEGER, y TEXT);
|
||||||
|
INSERT INTO foo VALUES(4, \"hello\");
|
||||||
|
INSERT INTO foo VALUES(3, \", \");
|
||||||
|
INSERT INTO foo VALUES(2, \"world\");
|
||||||
|
INSERT INTO foo VALUES(1, \"!\");
|
||||||
|
END;";
|
||||||
|
db.execute_batch(sql).unwrap();
|
||||||
|
|
||||||
|
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
||||||
|
let results: SqliteResult<Vec<String>> = query.query_map(&[], &(|row| row.get(1))).unwrap().collect();
|
||||||
|
|
||||||
|
assert_eq!(results.unwrap().concat(), "hello, world!");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_query_row() {
|
fn test_query_row() {
|
||||||
let db = checked_memory_handle();
|
let db = checked_memory_handle();
|
||||||
|
Loading…
Reference in New Issue
Block a user