aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/cli/src/words.rs11
-rw-r--r--lib/mihi/src/lib.rs36
2 files changed, 42 insertions, 5 deletions
diff --git a/crates/cli/src/words.rs b/crates/cli/src/words.rs
index a54e679..1567612 100644
--- a/crates/cli/src/words.rs
+++ b/crates/cli/src/words.rs
@@ -1010,6 +1010,15 @@ fn rm(mut args: IntoIter<String>) -> i32 {
}
};
+ // Fetch the word object for it which will serve as the initial values.
+ let word = match mihi::find_by(selection.as_str()) {
+ Ok(word) => word,
+ Err(e) => {
+ println!("error: words: {e}");
+ return 1;
+ }
+ };
+
let ans = Confirm::new(
format!("Do you really want to remove '{selection}' from the database?").as_str(),
)
@@ -1017,7 +1026,7 @@ fn rm(mut args: IntoIter<String>) -> i32 {
.prompt();
match ans {
- Ok(true) => match mihi::delete_word(&selection) {
+ Ok(true) => match mihi::delete_word(&word) {
Ok(_) => println!("Removed '{selection}' from the database!"),
Err(e) => {
println!("error: words: {e}");
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
+ )),
}
}