Split files into filenames and paths

This commit is contained in:
Lukáš Kucharczyk 2022-11-16 10:53:07 +01:00
parent f02133d6ab
commit 41362eccf1
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
2 changed files with 28 additions and 12 deletions

View File

@ -6,7 +6,8 @@ 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 "tags_ties" (

37
tag.sh
View File

@ -57,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=""
@ -67,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 = \"$FILENAME\"\
WHERE filename LIKE \"%$FILENAME%\"\
$ADDITIONAL_QUERY"
}
@ -82,6 +82,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 +124,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 +144,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."
FILENAME=$(basename "$FILE")
FILEPATH=$(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 +184,9 @@ tag_exists_in_db() {
file_exists_in_db() {
# $FILENAME
FILENAME=${1:-}
RESULT=$(sqlite_query "SELECT id FROM files WHERE filename = '$FILENAME'")
FILENAME=$(basename "${1:-}")
FILEPATH=$(dirname "${1:-}")
RESULT=$(sqlite_query "SELECT id FROM files WHERE filename = '$FILENAME' AND path = '$FILEPATH'")
if [[ -z "$RESULT" ]]; then
return 1
else
@ -187,6 +194,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 +233,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