From bd1e965f936c9fe652bd940c1f12e3327f06a222 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 10 Nov 2018 00:50:24 -0500 Subject: add sql subcommand for raw queries this was more painful than i think it should have been --- src/db.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index 056886e..97522d9 100644 --- a/src/db.rs +++ b/src/db.rs @@ -90,4 +90,30 @@ impl DB { } Ok(()) } + + pub fn query( + &self, + query: &str, + f: F + ) -> failure::Fallible> { + let mut sth = self.conn.prepare(query)?; + + let cols = sth.column_names() + .iter() + .map(|s| s.to_string()) + .collect(); + + let rows = sth.query_and_then( + rusqlite::NO_PARAMS, + |row| { f(row); Ok(()) }, + )?; + // this call to collect() forces it to actually consume the iterator + // (and therefore call the callbacks). what i really want here is for + // there to be a query_for_each or something like that, but the weird + // way lifetimes work for rows makes it difficult to emulate this any + // other way + let errs: failure::Fallible> = rows.collect(); + + errs.map(|_| cols) + } } -- cgit v1.2.3-54-g00ecf