From 9f1eccdf1a25ef0f1bdd4a157bd425c424084650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Fri, 11 Nov 2022 12:55:09 +0100 Subject: [PATCH] Initial commit --- database.sql | 27 +++++++++++++++++++ tag.sh | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 database.sql create mode 100755 tag.sh diff --git a/database.sql b/database.sql new file mode 100644 index 0000000..55544fa --- /dev/null +++ b/database.sql @@ -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; \ No newline at end of file diff --git a/tag.sh b/tag.sh new file mode 100755 index 0000000..617d0b8 --- /dev/null +++ b/tag.sh @@ -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 + + +