diff options
author | Jesse Luehrs <doy@tozt.net> | 2018-11-10 00:50:24 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2018-11-10 00:50:24 -0500 |
commit | bd1e965f936c9fe652bd940c1f12e3327f06a222 (patch) | |
tree | db9b8e5dae39de9b53c83ce2c1138454be005576 /src/db.rs | |
parent | 6d4abd89b2dfcdce0bd59d627a31e92fcc745c94 (diff) | |
download | lastfm-query-bd1e965f936c9fe652bd940c1f12e3327f06a222.tar.gz lastfm-query-bd1e965f936c9fe652bd940c1f12e3327f06a222.zip |
add sql subcommand for raw queries
this was more painful than i think it should have been
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 26 |
1 files changed, 26 insertions, 0 deletions
@@ -90,4 +90,30 @@ impl DB { } Ok(()) } + + pub fn query<F: Fn(&rusqlite::Row)>( + &self, + query: &str, + f: F + ) -> failure::Fallible<Vec<String>> { + 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<Vec<()>> = rows.collect(); + + errs.map(|_| cols) + } } |