summaryrefslogtreecommitdiff
path: root/src/database.rs
diff options
context:
space:
mode:
authorJakob L. Kreuze <zerodaysfordays@sdf.org>2020-06-20 21:46:58 -0400
committerJakob L. Kreuze <zerodaysfordays@sdf.org>2020-06-20 21:46:58 -0400
commit45b48fd9b9ee21759701d3d4f445d8bf141d3f9e (patch)
tree9ee8c8c4f228605807ca80e89d8b636a8fd0eb79 /src/database.rs
parentda1226901c244da02395fc9656051f8797d23800 (diff)
[birka-cli] add 'tags' command.
Diffstat (limited to 'src/database.rs')
-rw-r--r--src/database.rs19
1 files changed, 12 insertions, 7 deletions
diff --git a/src/database.rs b/src/database.rs
index 5591d6a..c7c84a0 100644
--- a/src/database.rs
+++ b/src/database.rs
@@ -357,18 +357,23 @@ impl TagDatabase {
.collect())
}
- pub fn top_tags(&self, n: i64) -> Result<Vec<(i64, String)>> {
+ pub fn top_tags(&self, n: Option<i64>) -> Result<Vec<(i64, String)>> {
Ok(self
.0
- .prepare(
+ .prepare(&format!(
"SELECT tags.name, COUNT(mapping.tag) AS tag_count
FROM mapping
INNER JOIN tags ON tags.id = mapping.tag
GROUP BY tag
ORDER BY tag_count DESC
- LIMIT ?",
- )?
- .query_map(params![n], |row| -> rusqlite::Result<(i64, String)> {
+ {limit}",
+ limit = if let Some(count) = n {
+ format!("LIMIT {}", count)
+ } else {
+ "".into()
+ }
+ ))?
+ .query_map(params![], |row| -> rusqlite::Result<(i64, String)> {
Ok((row.get(1)?, row.get(0)?))
})?
.filter_map(|s| s.ok())
@@ -599,7 +604,7 @@ mod tests {
tb.tag_file(ids[1].id, &vec!["2"]).unwrap();
tb.tag_file(ids[2].id, &vec!["1"]).unwrap();
tb.tag_file(ids[2].id, &vec!["2"]).unwrap();
- assert_eq!(tb.top_tags(2).unwrap().len(), 2);
- assert_eq!(tb.top_tags(2).unwrap()[0].0, 2);
+ assert_eq!(tb.top_tags(Some(2)).unwrap().len(), 2);
+ assert_eq!(tb.top_tags(Some(2)).unwrap()[0].0, 2);
}
}