Add support for adding hashes

This commit is contained in:
Lukáš Kucharczyk 2022-11-16 08:56:58 +01:00
parent ca5146e17c
commit c86f125ed3
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
1 changed files with 41 additions and 7 deletions

48
tag.sh
View File

@ -77,13 +77,14 @@ add() {
# $TYPE(tag|path|hash) $VALUE1..$VALUEN
[ "$1" = "tag" ] && shift && add_tag "$@" && exit 0
[ "$1" = "path" ] && shift && add_path "$@" && exit 0
[ "$1" = "hash" ] && fail "Adding hashes is not implemented yet." && TABLE="hashes" && COLUMN="md5,filename"
exit 0
[ "$1" = "hash" ] && shift && add_hash "$@" && exit 0
[ "$1" = "file" ] && fail "Use \"$SCRIPT_NAME add path\" instead."
fail "Usage: $SCRIPT_NAME add tag/path/hash"
}
sqlite_query() {
# $QUERY
sqlite3 -table "$DB_FILE" "$1"
sqlite3 "$DB_FILE" "$1"
}
sqlite_lastrows() {
@ -91,7 +92,7 @@ sqlite_lastrows() {
sqlite_query "SELECT * FROM \"$1\" ORDER BY id DESC LIMIT ${2}"
}
sqlite_insert() {
sqlite_insert_single() {
# $TABLE $COLUMN $VALUES
# $VALUES can be comma-delimited
[ -z "$1" ] && fail "No table specified."
@ -108,7 +109,29 @@ sqlite_insert() {
VALUES+=",(\"$1\")"
done
fi
QUERY="INSERT INTO $TABLE ($COLUMN) VALUES ${VALUES} RETURNING *;"
QUERY="PRAGMA foreign_keys=ON;"
QUERY+="INSERT INTO $TABLE ($COLUMN) VALUES ${VALUES} RETURNING *;"
sqlite_query "$QUERY"
}
sqlite_insert_multi() {
# $TABLE $COLUMN $VALUE1 $VALUE2
[ -z "$1" ] && fail "No table specified."
TABLE="$1"
[ -z "$2" ] && fail "No column(s) supplied."
COLUMN="$2"
[[ -z "$3" || -z "$4" ]] && fail "No column values supplied."
shift 2
VALUES="($1,$2)"
if [[ ! -z "${3:-}" && ! -z "${4:-}" ]]; then
while true; do
shift
[ -z "${1:-}" && -z "${2:-}" ] && break
VALUES+=",($1,$2)"
done
fi
QUERY="PRAGMA foreign_keys=ON;"
QUERY+="INSERT INTO $TABLE ($COLUMN) VALUES ${VALUES} RETURNING *;"
sqlite_query "$QUERY"
}
@ -116,14 +139,25 @@ add_tag() {
# $TAG1...$TAGN
TABLE="tags"
COLUMN="label"
sqlite_insert "$TABLE" "$COLUMN" "$@"
sqlite_insert_single "$TABLE" "$COLUMN" "$@"
}
add_path() {
# $PATH1..$PATHN
TABLE="files"
COLUMN="filename"
sqlite_insert "$TABLE" "$COLUMN" "$@"
sqlite_insert_single "$TABLE" "$COLUMN" "$@"
}
add_hash() {
# $FILE $HASH
TABLE="hashes"
COLUMN="fid,md5"
local FILENAME="${1:-}"
[ -z "$FILENAME" ] && fail "No file specified."
RESULT=$(sqlite_query "SELECT id FROM files WHERE filename = '$FILENAME'")
[ -z "$RESULT" ] && fail "File \"$FILENAME\" does not exist in database."
sqlite_insert_multi "$TABLE" "$COLUMN" $RESULT "'$2'"
}
add_path_auto() {