diff options
| author | Miquel Sabaté Solà <mssola@mssola.com> | 2026-01-21 22:05:10 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <mssola@mssola.com> | 2026-01-21 22:05:10 +0100 |
| commit | cab0faea15c270dcefd2d88fc1f91477eca00c58 (patch) | |
| tree | c677165db9f6fb416c451056b82fd52acd1452db /lib | |
| parent | 8499c476f9a1097b0a7aebe688c6d98d4b7d227b (diff) | |
| download | mihi-cab0faea15c270dcefd2d88fc1f91477eca00c58.tar.gz mihi-cab0faea15c270dcefd2d88fc1f91477eca00c58.zip | |
words: add the --tag flag to the ls command
Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/mihi/src/lib.rs | 53 |
1 files changed, 47 insertions, 6 deletions
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<String>) -> Result<Vec<String>, 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<String>, tags: &[String]) -> Result<Vec<String>, 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::<Vec<_>>() + .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::<Vec<_>>() + .join(", "), + ) + .as_str(), + ) + .unwrap() + }; stmt.query([]).unwrap() } }; |
