Compare commits

...

4 Commits

3 changed files with 55 additions and 16 deletions

View File

@ -1,12 +1,12 @@
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "tags" (
"id" INTEGER,
"label" text,
"label" TEXT,
PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "files" (
"id" INTEGER,
"filename" text,
"filename" TEXT,
PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "tags_ties" (
@ -19,8 +19,8 @@ CREATE TABLE IF NOT EXISTS "tags_ties" (
);
CREATE TABLE IF NOT EXISTS "hashes" (
"id" INTEGER,
"md5" text,
"fid" int,
"md5" TEXT,
"fid" INTEGER,
PRIMARY KEY("id" AUTOINCREMENT),
FOREIGN KEY("fid") REFERENCES "files"("id")
);

51
tag.sh
View File

@ -18,11 +18,10 @@ set -ueo pipefail
# FIXME: adding tags doesn't work
SCRIPT_DIR=$(dirname $(readlink "$0"))
CURRENT_DIR=$(dirname "$PWD")
SCRIPT_NAME=$(basename "$0")
DB_FILE="${CURRENT_DIR}/tags.db"
DB_FILE="${PWD}/tags.db"
[ "$1" = "--db" ] && DB_FILE="$2" && shift 2
@ -78,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() {
@ -92,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."
@ -109,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"
}
@ -117,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() {

View File

@ -3,6 +3,7 @@ set -ueo pipefail
SCRIPT_DIR=$(dirname $(readlink -f "$0"))
DB_FILE="test.db"
FILENAME="test.mp4"
oneTimeSetUp() {
tag --db "$DB_FILE" init >/dev/null
@ -27,7 +28,6 @@ testTagAdded() {
}
testFilenameAdded() {
local FILENAME="test.mp4"
tag --db "$DB_FILE" add path "$FILENAME" >/dev/null
RESULT=$(sqlite3 "$DB_FILE" "SELECT filename from files WHERE filename = '$FILENAME'")
assertEquals "$RESULT" "$FILENAME"
@ -35,9 +35,15 @@ testFilenameAdded() {
testHashAdded() {
local HASH="fbe2153ce0614d76a378b2e6fe07cc9e"
tag --db "$DB_FILE" add hash "$HASH" >/dev/null
RESULT=$(sqlite3 "$DB_FILE" "SELECT md5 from hashes WHERE hash = '$HASH'")
tag --db "$DB_FILE" add hash "$FILENAME" "$HASH" >/dev/null
RESULT=$(sqlite3 "$DB_FILE" "SELECT md5 from hashes WHERE md5 = '$HASH'")
assertEquals "$RESULT" "$HASH"
}
testFailAddingHashOnNonexistentFile() {
local HASH="fbe2153ce0614d76a378b2e6fe07cc9e"
tag --db "$DB_FILE" add hash "nonexistentfile" "$HASH" 2>/dev/null
assertEquals "$?" 1
}
. ../shunit2/shunit2