Added on delete actions for file_tag

master
Wynd 2025-05-04 22:47:29 +03:00
parent a7c0480a58
commit fb7f87fbbe
2 changed files with 9 additions and 8 deletions

View File

@ -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)
);"#,
(),

View File

@ -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<Vec<String>>) -
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()?;