Compare commits

..

No commits in common. "1580865b56265d5768fe9fda3799cda2609cdcfa" and "f02133d6abd5cc9793ad9bfe46e54321e971da1b" have entirely different histories.

2 changed files with 14 additions and 39 deletions

View File

@ -6,16 +6,9 @@ CREATE TABLE IF NOT EXISTS "tags" (
);
CREATE TABLE IF NOT EXISTS "files" (
"id" INTEGER,
"filename" TEXT NOT NULL,
"path" TEXT,
"filename" TEXT,
PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "metadata" (
"fid" INTEGER NOT NULL,
"name" TEXT,
PRIMARY KEY("fid"),
FOREIGN KEY("fid") REFERENCES "files"("id")
);
CREATE TABLE IF NOT EXISTS "tags_ties" (
"fid" INTEGER NOT NULL,
"tid" INTEGER NOT NULL,
@ -25,8 +18,8 @@ CREATE TABLE IF NOT EXISTS "tags_ties" (
);
CREATE TABLE IF NOT EXISTS "hashes" (
"id" INTEGER,
"md5" TEXT NOT NULL,
"fid" INTEGER NOT NULL,
"md5" TEXT,
"fid" INTEGER,
PRIMARY KEY("id" AUTOINCREMENT),
FOREIGN KEY("fid") REFERENCES "files"("id")
);

40
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: adjust sqlite_insert_multiple to allow defining columns per insert, currently it's hardcoded to 2
# TODO: merge sqlite_insert_single and sqlite_insert_multiple probably as result of the above
SCRIPT_DIR=$(dirname $(readlink "$0"))
SCRIPT_NAME=$(basename "$0")
@ -60,7 +57,7 @@ listtags() {
FILENAME="$1"
shift
# ! file_exists_in_db "$FILENAME" && fail "File '$FILENAME' does not exist in database."
! file_exists_in_db "$FILENAME" && fail "File '$FILENAME' does not exist in database."
LIMIT="${1:-0}"
ADDITIONAL_QUERY=""
@ -70,7 +67,7 @@ listtags() {
"SELECT filename, label from files \
INNER JOIN tags_ties ON tags_ties.fid = files.id \
INNER JOIN tags ON tags.id = tags_ties.tid \
WHERE filename LIKE \"%$FILENAME%\"\
WHERE filename = \"$FILENAME\"\
$ADDITIONAL_QUERY"
}
@ -85,7 +82,6 @@ add() {
sqlite_query() {
# $QUERY
dbfile_exists || fail "Database file '$DB_FILE' does not exist. Use '$SCRIPT_NAME init' to create default one, or specify filename with '--db'."
sqlite3 "$DB_FILE" "$1"
}
@ -127,8 +123,8 @@ sqlite_insert_multi() {
VALUES="($1,$2)"
if [[ ! -z "${3:-}" && ! -z "${4:-}" ]]; then
while true; do
shift 2
[[ -z "${1:-}" && -z "${2:-}" ]] && break
shift
[ -z "${1:-}" && -z "${2:-}" ] && break
VALUES+=",($1,$2)"
done
fi
@ -147,16 +143,11 @@ add_tag() {
add_path() {
# $PATH1..$PATHN
TABLE="files"
COLUMN="filename,path"
FILES=()
COLUMN="filename"
for FILE in "$@"; do
local FILENAME=$(basename "$FILE")
local FILEPATH=$(realpath $(dirname "$FILE"))
file_exists_in_db "$FILE" && fail "File '$FILE' already exists in database."
FILES+=("'$FILENAME'")
FILES+=("'$FILEPATH'")
file_exists_in_db "$FILE" && fail "File '$FILENAME' already exists in database."
done
sqlite_insert_multi "$TABLE" "$COLUMN" "${FILES[@]}"
sqlite_insert_single "$TABLE" "$COLUMN" "$@"
}
add_hash() {
@ -187,9 +178,8 @@ tag_exists_in_db() {
file_exists_in_db() {
# $FILENAME
local FILENAME=$(basename "${1:-}")
local FILEPATH=$(dirname "${1:-}")
local RESULT=$(sqlite_query "SELECT id FROM files WHERE filename = '$FILENAME' AND path = '$FILEPATH'")
FILENAME=${1:-}
RESULT=$(sqlite_query "SELECT id FROM files WHERE filename = '$FILENAME'")
if [[ -z "$RESULT" ]]; then
return 1
else
@ -197,14 +187,6 @@ file_exists_in_db() {
fi
}
dbfile_exists() {
if [[ ! -f "$DB_FILE" ]]; then
return 1
else
return 0
fi
}
main() {
[ -z "${1:-}" ] && fail "Usage: tag init/add/import/autoimport/list/listtags"
@ -236,14 +218,14 @@ main() {
if [[ "$1" = "list" ]]; then
[ -z "$2" ] && fail "No table supplied."
TABLE_NAME="$2"
sqlite_query "SELECT * FROM \"${TABLE_NAME}\""
sqlite3 -table "$DB_FILE" "SELECT * FROM \"${TABLE_NAME}\""
exit 0
fi
if [[ "$1" = "bytag" ]]; then
[ -z "$2" ] && fail "No tag supplied."
TAG_NAME="$2"
sqlite_query "SELECT filename FROM files WHERE id = (SELECT id FROM tags_ties WHERE id = (SELECT id FROM tags WHERE label = \"${TAG_NAME}\"))"
sqlite3 -table "$DB_FILE" "SELECT filename FROM files WHERE id = (SELECT id FROM tags_ties WHERE id = (SELECT id FROM tags WHERE label = \"${TAG_NAME}\"))"
exit 0
fi