From cab0faea15c270dcefd2d88fc1f91477eca00c58 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Wed, 21 Jan 2026 22:05:10 +0100 Subject: words: add the --tag flag to the ls command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miquel Sabaté Solà --- lib/mihi/src/lib.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/mihi/src/lib.rs b/lib/mihi/src/lib.rs index f563645..6ce450e 100644 --- a/lib/mihi/src/lib.rs +++ b/lib/mihi/src/lib.rs @@ -664,23 +664,64 @@ pub fn update_timestamp(enunciated: &str) -> Result<(), String> { } } -pub fn select_enunciated(filter: Option) -> Result, String> { +/// Select words based on the given `filter` for the enunciated column, which +/// can be further filtered out by providing a set of `tags`. The words selected +/// must then have any of the given tags provided by this vector, and it will be +/// ignored if the passed vector is empty. +pub fn select_enunciated(filter: Option, tags: &[String]) -> Result, String> { let conn = get_connection()?; let mut stmt; let mut it = match filter { Some(filter) => { - stmt = conn + stmt = if tags.is_empty() { + conn .prepare( "SELECT enunciated FROM words WHERE enunciated LIKE ('%' || ?1 || '%') ORDER BY enunciated", ) - .unwrap(); + .unwrap() + } else { + conn.prepare( + format!( + "SELECT w.enunciated \ + FROM words w \ + JOIN tag_associations ta ON w.id = ta.word_id \ + JOIN tags t ON t.id = ta.tag_id \ + WHERE w.enunciated LIKE ('%' || ?1 || '%') AND t.name IN ({}) \ + ORDER BY w.enunciated", + tags.iter() + .map(|t| format!("'{}'", t)) + .collect::>() + .join(", "), + ) + .as_str(), + ) + .unwrap() + }; stmt.query([filter.as_str()]).unwrap() } None => { - stmt = conn - .prepare("SELECT enunciated FROM words ORDER BY enunciated") - .unwrap(); + stmt = if tags.is_empty() { + conn.prepare("SELECT enunciated FROM words ORDER BY enunciated") + .unwrap() + } else { + conn.prepare( + format!( + "SELECT w.enunciated \ + FROM words w \ + JOIN tag_associations ta ON w.id = ta.word_id \ + JOIN tags t ON t.id = ta.tag_id \ + WHERE t.name IN ({}) \ + ORDER BY w.enunciated", + tags.iter() + .map(|t| format!("'{}'", t)) + .collect::>() + .join(", "), + ) + .as_str(), + ) + .unwrap() + }; stmt.query([]).unwrap() } }; -- cgit v1.2.3