From fb7f87fbbe8bd852e98b164adb1e01e269002288 Mon Sep 17 00:00:00 2001 From: Wynd Date: Sun, 4 May 2025 22:47:29 +0300 Subject: [PATCH] Added on delete actions for file_tag --- src/main.rs | 4 ++-- src/tags.rs | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8741360..e71d03a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,8 +41,8 @@ fn init_db() -> anyhow::Result<()> { conn.execute( r#"CREATE TABLE file_tag( - file_id INT REFERENCES file(id), - tag_id INT REFERENCES tag(id), + file_id INTEGER REFERENCES file(id) ON DELETE CASCADE, + tag_id INTEGER REFERENCES tag(id) ON DELETE CASCADE, PRIMARY KEY (file_id, tag_id) );"#, (), diff --git a/src/tags.rs b/src/tags.rs index 5743b81..ffb6b7e 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -6,7 +6,7 @@ use std::{ use anyhow::{Result, anyhow}; -use rusqlite::{Connection, params, params_from_iter}; +use rusqlite::{Connection, params, params_from_iter, types::Value}; use crate::{ cli::{self, FilesArgs, TagArgs, TagsArgs, UntagArgs}, @@ -255,19 +255,20 @@ fn untag_file(conn: &mut Connection, file: PathBuf, tags: Option>) - INNER JOIN tag ON file_tag.tag_id = tag.id"# .to_string(); - match tags { + let params = match tags { Some(tags) => { let tags = tags.join(","); sql.push_str(" WHERE file.path = ?1 AND tag.name IN (?2))"); - let mut stmt = tx.prepare(&sql)?; - stmt.execute(params![file, tags])?; + vec![Value::Text(file), Value::Text(tags)] } None => { sql.push_str(" WHERE file.path = ?1)"); - let mut stmt = tx.prepare(&sql)?; - stmt.execute(params![file])?; + vec![Value::Text(file)] } }; + + let mut stmt = tx.prepare(&sql)?; + stmt.execute(params_from_iter(params))?; } tx.commit()?;