From 45b48fd9b9ee21759701d3d4f445d8bf141d3f9e Mon Sep 17 00:00:00 2001 From: "Jakob L. Kreuze" Date: Sat, 20 Jun 2020 21:46:58 -0400 Subject: [birka-cli] add 'tags' command. --- src/birka-cli.rs | 5 +++++ src/birka-web.rs | 2 +- src/database.rs | 19 ++++++++++++------- 3 files changed, 18 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/birka-cli.rs b/src/birka-cli.rs index abbd0d0..4a8681f 100644 --- a/src/birka-cli.rs +++ b/src/birka-cli.rs @@ -108,6 +108,11 @@ fn main() { println!("{}", file.path); } } + "tags" => { + for (count, tag) in tb.top_tags(None).unwrap() { + println!("{}: {}", count, tag); + } + } _ => { eprintln!("Unknown action '{}'", action); std::process::exit(1); diff --git a/src/birka-web.rs b/src/birka-web.rs index a3bed1d..cd68923 100644 --- a/src/birka-web.rs +++ b/src/birka-web.rs @@ -277,7 +277,7 @@ fn display_posts(conn: SiteState, query: String, last: Option) -> Result Result> { + pub fn top_tags(&self, n: Option) -> Result> { 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); } } -- cgit v1.3