Allow more than one tag, fix listing tags

This commit is contained in:
Lukáš Kucharczyk 2022-11-16 09:59:44 +01:00
parent f43c1d433a
commit f02133d6ab
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
2 changed files with 8 additions and 12 deletions

View File

@ -10,12 +10,11 @@ CREATE TABLE IF NOT EXISTS "files" (
PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "tags_ties" (
"id" INTEGER,
"tid" INTEGER,
"fid" INTEGER,
PRIMARY KEY("id" AUTOINCREMENT),
FOREIGN KEY("tid") REFERENCES "tags"("id"),
FOREIGN KEY("fid") REFERENCES "files"("id")
"fid" INTEGER NOT NULL,
"tid" INTEGER NOT NULL,
PRIMARY KEY("fid", "tid"),
FOREIGN KEY("fid") REFERENCES "files"("id"),
FOREIGN KEY("tid") REFERENCES "tags"("id")
);
CREATE TABLE IF NOT EXISTS "hashes" (
"id" INTEGER,

9
tag.sh
View File

@ -13,9 +13,6 @@ set -ueo pipefail
# - tag $FILENAME $TAG - tag $FILENAME with $TAG, $TAG can be repeated
# - listtags $FILENAME - list all tags for $FILENAME
# TODO: when tagging, allow more than one tag
# FIXME: adding tags doesn't work
SCRIPT_DIR=$(dirname $(readlink "$0"))
SCRIPT_NAME=$(basename "$0")
@ -68,8 +65,8 @@ listtags() {
sqlite_query \
"SELECT filename, label from files \
INNER JOIN tags_ties ON tags_ties.id = files.id \
INNER JOIN tags ON tags.id = tags_ties.id \
INNER JOIN tags_ties ON tags_ties.fid = files.id \
INNER JOIN tags ON tags.id = tags_ties.tid \
WHERE filename = \"$FILENAME\"\
$ADDITIONAL_QUERY"
}
@ -244,7 +241,7 @@ main() {
LABEL="$1"
! tag_exists_in_db "$LABEL" && fail "Tag '$TAG' does not exist in database."
shift
sqlite3 -table "$DB_FILE" "INSERT INTO tags_ties (id, id) VALUES ((SELECT id FROM files WHERE filename = \"$FILENAME\"),(SELECT id FROM tags WHERE label = \"$LABEL\"))"
sqlite_query "INSERT INTO tags_ties (fid, tid) VALUES ((SELECT id FROM files WHERE filename = \"$FILENAME\"),(SELECT id FROM tags WHERE label = \"$LABEL\"))"
COUNTER=$((COUNTER++))
[ -z "${1:-}" ] && break
done