Compare commits
4 Commits
f02133d6ab
...
1580865b56
Author | SHA1 | Date |
---|---|---|
Lukáš Kucharczyk | 1580865b56 | |
Lukáš Kucharczyk | d162919b3d | |
Lukáš Kucharczyk | 90b0c98675 | |
Lukáš Kucharczyk | 41362eccf1 |
13
database.sql
13
database.sql
|
@ -6,9 +6,16 @@ CREATE TABLE IF NOT EXISTS "tags" (
|
|||
);
|
||||
CREATE TABLE IF NOT EXISTS "files" (
|
||||
"id" INTEGER,
|
||||
"filename" TEXT,
|
||||
"filename" TEXT NOT NULL,
|
||||
"path" 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,
|
||||
|
@ -18,8 +25,8 @@ CREATE TABLE IF NOT EXISTS "tags_ties" (
|
|||
);
|
||||
CREATE TABLE IF NOT EXISTS "hashes" (
|
||||
"id" INTEGER,
|
||||
"md5" TEXT,
|
||||
"fid" INTEGER,
|
||||
"md5" TEXT NOT NULL,
|
||||
"fid" INTEGER NOT NULL,
|
||||
PRIMARY KEY("id" AUTOINCREMENT),
|
||||
FOREIGN KEY("fid") REFERENCES "files"("id")
|
||||
);
|
||||
|
|
40
tag.sh
40
tag.sh
|
@ -13,6 +13,9 @@ 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")
|
||||
|
||||
|
@ -57,7 +60,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=""
|
||||
|
@ -67,7 +70,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 = \"$FILENAME\"\
|
||||
WHERE filename LIKE \"%$FILENAME%\"\
|
||||
$ADDITIONAL_QUERY"
|
||||
}
|
||||
|
||||
|
@ -82,6 +85,7 @@ 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"
|
||||
}
|
||||
|
||||
|
@ -123,8 +127,8 @@ sqlite_insert_multi() {
|
|||
VALUES="($1,$2)"
|
||||
if [[ ! -z "${3:-}" && ! -z "${4:-}" ]]; then
|
||||
while true; do
|
||||
shift
|
||||
[ -z "${1:-}" && -z "${2:-}" ] && break
|
||||
shift 2
|
||||
[[ -z "${1:-}" && -z "${2:-}" ]] && break
|
||||
VALUES+=",($1,$2)"
|
||||
done
|
||||
fi
|
||||
|
@ -143,11 +147,16 @@ add_tag() {
|
|||
add_path() {
|
||||
# $PATH1..$PATHN
|
||||
TABLE="files"
|
||||
COLUMN="filename"
|
||||
COLUMN="filename,path"
|
||||
FILES=()
|
||||
for FILE in "$@"; do
|
||||
file_exists_in_db "$FILE" && fail "File '$FILENAME' already exists in database."
|
||||
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'")
|
||||
done
|
||||
sqlite_insert_single "$TABLE" "$COLUMN" "$@"
|
||||
sqlite_insert_multi "$TABLE" "$COLUMN" "${FILES[@]}"
|
||||
}
|
||||
|
||||
add_hash() {
|
||||
|
@ -178,8 +187,9 @@ tag_exists_in_db() {
|
|||
|
||||
file_exists_in_db() {
|
||||
# $FILENAME
|
||||
FILENAME=${1:-}
|
||||
RESULT=$(sqlite_query "SELECT id FROM files WHERE filename = '$FILENAME'")
|
||||
local FILENAME=$(basename "${1:-}")
|
||||
local FILEPATH=$(dirname "${1:-}")
|
||||
local RESULT=$(sqlite_query "SELECT id FROM files WHERE filename = '$FILENAME' AND path = '$FILEPATH'")
|
||||
if [[ -z "$RESULT" ]]; then
|
||||
return 1
|
||||
else
|
||||
|
@ -187,6 +197,14 @@ 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"
|
||||
|
@ -218,14 +236,14 @@ main() {
|
|||
if [[ "$1" = "list" ]]; then
|
||||
[ -z "$2" ] && fail "No table supplied."
|
||||
TABLE_NAME="$2"
|
||||
sqlite3 -table "$DB_FILE" "SELECT * FROM \"${TABLE_NAME}\""
|
||||
sqlite_query "SELECT * FROM \"${TABLE_NAME}\""
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ "$1" = "bytag" ]]; then
|
||||
[ -z "$2" ] && fail "No tag supplied."
|
||||
TAG_NAME="$2"
|
||||
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}\"))"
|
||||
sqlite_query "SELECT filename FROM files WHERE id = (SELECT id FROM tags_ties WHERE id = (SELECT id FROM tags WHERE label = \"${TAG_NAME}\"))"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
|
|
Loading…
Reference in New Issue