Initial commit

This commit is contained in:
Lukáš Kucharczyk 2022-11-11 12:55:09 +01:00
commit 9f1eccdf1a
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
2 changed files with 103 additions and 0 deletions

27
database.sql Normal file
View File

@ -0,0 +1,27 @@
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "tags" (
"tid" INTEGER,
"label" text,
PRIMARY KEY("tid" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "files" (
"fid" INTEGER,
"filename" text,
PRIMARY KEY("fid" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "tags_ties" (
"id" INTEGER,
"tid" INTEGER,
"fid" INTEGER,
PRIMARY KEY("id" AUTOINCREMENT),
FOREIGN KEY("tid") REFERENCES "tags_ties"("tid"),
FOREIGN KEY("fid") REFERENCES "files"("fid")
);
CREATE TABLE IF NOT EXISTS "hashes" (
"id" INTEGER,
"md5" text,
"fid" int,
PRIMARY KEY("id" AUTOINCREMENT),
FOREIGN KEY("fid") REFERENCES "files"("fid")
);
COMMIT;

76
tag.sh Executable file
View File

@ -0,0 +1,76 @@
#!/usr/bin/env bash
set -ueo pipefail
# usage:
# - add (tag|filename) $VALUE - adds a new tag or filename of $VALUE
# - list (tags|files) - lists all the values in a table
# - bytag $TAG - list all files by tag label
# - tag $FILENAME $TAG - tag $FILENAME with $TAG, $TAG can be repeated
# - listtags $FILENAME - list all tags for $FILENAME
fail() {
echo "$1" >&2;
exit 1
}
listtags() {
[ -z "$1" ] && fail "No filename supplied."
FILENAME="$1"
sqlite3 -table tags.db "SELECT filename, label from files INNER JOIN tags_ties ON tags_ties.fid = files.fid INNER JOIN tags ON tags.tid = tags_ties.tid WHERE filename = \"$FILENAME\""
}
if [[ "$1" = "add" ]]; then
shift
[ "$1" = "tag" ] && TABLE="tags" && COLUMN="label"
[ "$1" = "file" ] && TABLE="files" && COLUMN="filename"
shift
[ -z "$1" ] && fail "No value supplied."
COUNTER=0
while true; do
NEW_TAG="$1"
shift
sqlite3 -table tags.db "INSERT INTO $TABLE ($COLUMN) VALUES (\"${NEW_TAG}\")"
COUNTER=$((COUNTER++))
[ -z "${1:-}" ] && break
done
sqlite3 -table tags.db "SELECT * FROM \"$TABLE\" LIMIT $COUNTER"
exit 0
fi
if [[ "$1" = "list" ]]; then
[ -z "$2" ] && fail "No table supplied."
TABLE_NAME="$2"
sqlite3 -table tags.db "SELECT * FROM \"${TABLE_NAME}\""
exit 0
fi
if [[ "$1" = "bytag" ]]; then
[ -z "$2" ] && fail "No tag supplied."
TAG_NAME="$2"
sqlite3 -table tags.db "SELECT filename FROM files WHERE fid = (SELECT fid FROM tags_ties WHERE tid = (SELECT tid FROM tags WHERE label = \"${TAG_NAME}\"))"
exit 0
fi
if [[ "$1" = "tag" ]]; then
shift
[ -z "$1" ] && fail "No filename supplied."
FILENAME="$1"
shift
[ -z "$1" ] && fail "No tag supplied."
while true; do
LABEL="$1"
shift
sqlite3 -table tags.db "INSERT INTO tags_ties (fid, tid) VALUES ((SELECT fid FROM files WHERE filename = \"$FILENAME\"),(SELECT tid FROM tags WHERE label = \"$LABEL\"))"
[ -z "${1:-}" ] && break
done
listtags "$FILENAME"
exit 0
fi
if [[ "$1" = "listtags" ]]; then
listtags "$2"
exit 0
fi