diff options
| author | Miquel Sabaté Solà <mssola@mssola.com> | 2026-01-24 22:33:30 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <mssola@mssola.com> | 2026-01-24 22:33:30 +0100 |
| commit | 108bc6f0446c351b421ba62367e0f4eaf6c62461 (patch) | |
| tree | b2febcf881ed5a3b16518a7bde77a541e046da5f /lib | |
| parent | f310205a283cae4b9675801f4c6e32817a2536a7 (diff) | |
| download | mihi-108bc6f0446c351b421ba62367e0f4eaf6c62461.tar.gz mihi-108bc6f0446c351b421ba62367e0f4eaf6c62461.zip | |
words: delete all relationships on 'rm'
When removing a word, all relationships with other words and tags should
also be cleared. Not that it would litter too much the
database (considering the DB is relatively small), but it doesn't take
too much effort to play nice here.
Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/mihi/src/lib.rs | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/lib/mihi/src/lib.rs b/lib/mihi/src/lib.rs index e26f220..1aabf29 100644 --- a/lib/mihi/src/lib.rs +++ b/lib/mihi/src/lib.rs @@ -1167,15 +1167,43 @@ pub fn update_success(word: &Word, success: isize, steps: isize) -> Result<(), S } } -pub fn delete_word(enunciated: &String) -> Result<(), String> { +/// Delete the given word while also removing any relationship with other words +/// and tags. +pub fn delete_word(word: &Word) -> Result<(), String> { let conn = get_connection()?; + // Remove the word itself. + if let Err(e) = conn.execute( + "DELETE FROM words \ + WHERE id = ?1", + params![word.id], + ) { + return Err(format!("could not remove '{}': {e}", word.enunciated)); + } + + // Remove any relationships that mention this word. + if let Err(e) = conn.execute( + "DELETE FROM word_relations \ + WHERE source_id = ?1 OR destination_id = ?1", + params![word.id], + ) { + return Err(format!( + "could not remove relationships from '{}': {e}", + word.enunciated + )); + } + + // Remove any tag relationships with this now defunct word. match conn.execute( - "DELETE FROM words WHERE enunciated = ?1", - params![enunciated.as_str()], + "DELETE FROM tag_associations \ + WHERE word_id = ?1", + params![word.id], ) { Ok(_) => Ok(()), - Err(e) => Err(format!("could not remove '{enunciated}': {e}")), + Err(e) => Err(format!( + "count not detach words for '{}': {e}", + word.enunciated + )), } } |
