This repository has been archived by the owner on Nov 20, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_wrapper.sh
executable file
·158 lines (147 loc) · 4.39 KB
/
sql_wrapper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
####################################################
# GRIM REPO ########################################
####################################################
# sql_wrapper.sh
#
# Wrapper for the SQLite DB... This is in its
# own file, so it is possible to change this "module"
# with something else, like a mySQL database or something
# similar.
#
# The database consists of a single table:
# Database name: grim_repo
# Table name: checksums
# +-----------------+-------------------+
# | checksum (int) | filepath (String) |
# +-----------------+-------------------+
####################################################
databasename="$GR_PATH/grim_repo.sqlite";
tablename="checksums";
####
# clear_database
#
# Deletes the entire database (querying user first)
# Usefull when testing the program, needing to clear
# database and set up a testcase
function clear_database {
rm $sql_wrapper_rm_flag $databasename;
}
####
# create_database
#
# Creates the database. Does not check if it already exists
# Echoes potential errors
function create_database {
#create table
err=`sqlite3 $databasename "CREATE TABLE $tablename ( checksum TEXT, filepath TEXT, UNIQUE(filepath) );"`
log 0 "sqlite3 $databasename \"CREATE TABLE $tablename ( checksum TEXT, filepath TEXT, UNIQUE(filepath) );\""
#check and react if error occured
if [ $? != "0" ]; then
echo $err;
exit 30;
fi;
err=`sqlite3 $databasename "CREATE INDEX checksum_index on checksums ( checksum );"`;
#check and react if error occured
if [ $? != "0" ]; then
log 2 "$err";
exit 30;
fi;
}
####
# do_query
#
# Queries the database with the parameter. Then echoes the result.
#
# $1 - query: The query for the database
function do_query {
query=$1;
#echo "QUERY $query" DONE;
#check that database exists, else create:
if [ ! -f "$databasename" ]; then
create_database;
fi;
result=`sqlite3 $databasename "$query"`;
errorcode=$?;
#echo do query result: $result with errorcode $?;
echo $result;
return $errorcode;
}
####
# get_checksum
#
# Retrieves checksum based on filepath and local_root.
# The resulting checksum is echoed back, and nothing
# is echoed if it does not exist
#
# $1 - filepath: the entire path to the dir or file including filename
function get_checksum {
filepath=$1;
#query="\"SELECT checksum FROM $tablename WHERE filepath=\\\"$filepath\\\"
query="SELECT checksum FROM $tablename WHERE filepath=\"$filepath\"";
#echo do_query "$query"
checksum=`do_query "$query"`;
#result=`sqlite3 $databasename "$query"`;
errorcode=$?;
if [ $errorcode != "0" ]; then
log 2 "ERROR occured while trying to retrieve checksum from database";
exit 31;
fi;
echo $checksum;
}
####
# set_checksum
#
# Sets a checksum in the database, for the file defined
# by the parameters passed to the function. It does not
# matter whether the file already exists in the database or
# not. If it exists, the checksum will be updated, if it does
# not exist, it will be inserted as a new entry into the database
#
# $1 - filepath: the entire path to the dir or file including filename
# $2 - checksum: the new checksum
function set_checksum {
filepath=$1;
checksum=$2;
log 0 "do_query INSERT OR REPLACE INTO $tablename ( checksum, filepath ) VALUES ( \"$checksum\", \"$filepath\" )";
res=`do_query "INSERT OR REPLACE INTO $tablename ( checksum, filepath ) VALUES ( \"$checksum\", \"$filepath\" );"`;
log 0 "$res"
if [ $? != "0" ]; then
log 2 "ERROR occured while trying to enter or update checksum to database";
log 2 "$res";
exit 32;
fi;
}
####
# delete_file_entry
#
# To be used when a file is deleted from local machine.
# The function simply deletes the file passed as parameter
#
# $1 - filepath: the entire path to the file including filename
function delete_file_entry {
filepath=$1;
res=`do_query "DELETE FROM $tablename WHERE filepath=\"$filepath\""`;
if [ $? != "0" ]; then
log 2 "ERROR occured while trying to delete entry from database";
log 2 "$res";
exit 33;
fi;
}
####
# delete_dir_entry
#
# To be used when a dir is deleted from local machine.
# The function deletes the dir passed as parameter as
# well as all the sub_entries
#
# $1 - filepath: the entire path to the file including filename
function delete_dir_entry {
filepath=$1;
res=`do_query "DELETE FROM $tablename WHERE filepath LIKE \"${filepath}%\""`;
if [ $? != "0" ]; then
log 2 "ERROR occured while trying to delete entry from database";
log 2 "$res";
exit 33;
fi;
}