summaryrefslogtreecommitdiffstats
path: root/src/db.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2018-11-10 00:50:24 -0500
committerJesse Luehrs <doy@tozt.net>2018-11-10 00:50:24 -0500
commitbd1e965f936c9fe652bd940c1f12e3327f06a222 (patch)
treedb9b8e5dae39de9b53c83ce2c1138454be005576 /src/db.rs
parent6d4abd89b2dfcdce0bd59d627a31e92fcc745c94 (diff)
downloadlastfm-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.rs26
1 files changed, 26 insertions, 0 deletions
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<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)
+ }
}