Fixed same file having multiple of the same tag and added a tag renaming subcommand
parent
93b0e0c373
commit
e6b5457476
|
@ -30,6 +30,7 @@ pub enum TagsCommands {
|
|||
List,
|
||||
Add { add: Vec<String> },
|
||||
Remove { remove: Vec<String> },
|
||||
Rename { old: String, new: String },
|
||||
}
|
||||
|
||||
#[derive(Debug, Args, PartialEq, Eq)]
|
||||
|
|
|
@ -50,7 +50,8 @@ fn init_db(conn: &Connection) {
|
|||
conn.execute(
|
||||
r#"CREATE TABLE IF NOT EXISTS file_tag(
|
||||
file_id INT REFERENCES file(id),
|
||||
tag_id INT REFERENCES tag(id)
|
||||
tag_id INT REFERENCES tag(id),
|
||||
PRIMARY KEY (file_id, tag_id)
|
||||
);"#,
|
||||
(),
|
||||
)
|
||||
|
|
33
src/tags.rs
33
src/tags.rs
|
@ -68,7 +68,7 @@ pub fn handle_tag(args: TagArgs) {
|
|||
pub fn handle_tags(args: TagsArgs) {
|
||||
try_database();
|
||||
|
||||
let conn = Connection::open(DB_PATH).unwrap();
|
||||
let mut conn = Connection::open(DB_PATH).unwrap();
|
||||
|
||||
match args.commands {
|
||||
Some(cli::TagsCommands::List) | None => {
|
||||
|
@ -79,11 +79,20 @@ pub fn handle_tags(args: TagsArgs) {
|
|||
}
|
||||
w.flush().unwrap();
|
||||
}
|
||||
Some(cli::TagsCommands::Add { add }) => add_tags(&conn, add),
|
||||
Some(cli::TagsCommands::Add { add }) => add_tags(&mut conn, add),
|
||||
Some(cli::TagsCommands::Remove { remove }) => remove_tags(&conn, remove),
|
||||
Some(cli::TagsCommands::Rename { old, new }) => rename_tag(&conn, old, new),
|
||||
};
|
||||
}
|
||||
|
||||
fn rename_tag(conn: &Connection, old: String, new: String) {
|
||||
let mut stmt = conn
|
||||
.prepare("UPDATE tag SET name = ? WHERE name = ?")
|
||||
.unwrap();
|
||||
|
||||
stmt.execute([&new, &old]).unwrap();
|
||||
}
|
||||
|
||||
fn list_tags(conn: &Connection) -> Vec<String> {
|
||||
let mut stmt = conn.prepare("SELECT name FROM tag").unwrap();
|
||||
let result = stmt.query_map([], |row| row.get(0)).unwrap();
|
||||
|
@ -111,17 +120,21 @@ fn remove_tags(conn: &Connection, tags: Vec<String>) {
|
|||
conn.execute(&query, params_from_iter(tags)).unwrap();
|
||||
}
|
||||
|
||||
fn add_tags(conn: &Connection, tags: Vec<String>) {
|
||||
let mut query = r#"INSERT INTO tag(name) VALUES"#.to_string();
|
||||
fn add_tags(conn: &mut Connection, tags: Vec<String>) {
|
||||
let tx = conn.transaction().unwrap();
|
||||
{
|
||||
let mut query = r#"INSERT INTO tag(name) VALUES"#.to_string();
|
||||
|
||||
for (i, _tag) in tags.iter().enumerate() {
|
||||
query.push_str("(?)");
|
||||
if i < tags.len() - 1 {
|
||||
query.push(',');
|
||||
for (i, _tag) in tags.iter().enumerate() {
|
||||
query.push_str("(?)");
|
||||
if i < tags.len() - 1 {
|
||||
query.push(',');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
conn.execute(&query, params_from_iter(tags)).unwrap();
|
||||
tx.execute(&query, params_from_iter(tags)).unwrap();
|
||||
}
|
||||
tx.commit().unwrap();
|
||||
}
|
||||
|
||||
fn tag_file(conn: &mut Connection, file: PathBuf, tags: Vec<String>) {
|
||||
|
|
Loading…
Reference in New Issue