-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsqlgen.sh
More file actions
executable file
·76 lines (55 loc) · 1.89 KB
/
sqlgen.sh
File metadata and controls
executable file
·76 lines (55 loc) · 1.89 KB
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
#!/usr/bin/env bash
# sqlgen.sh
#
# A script that runs SQL queries through the SQLite3 DB and writes the
# output to a file
#
# Parameters:
# $1 is the location of the SQLite DB binary
# $2 is the directory where all SQLite queries are held. This script will
# recursively iterate through all .sql files found. The output will be
# logged into a .out file with the same base file name
source "echof.sh"
SQLITE_BIN="$1"
QUERY_DIR="$2"
QUERY_INPUT_EXT=".sql"
QUERY_OUTPUT_EXT=".out"
TEMP_DB="temp.db"
# Validate parameters and necessary files/directories
validate() {
if [[ -z "${SQLITE_BIN}" ]] ; then
echof "Parameter for sqlite binary file location is undefined"
exit 1
elif [[ -z "${QUERY_DIR}" ]] ; then
echof "Parameter for query files directory is undefined"
exit 2
fi
if ! [[ -f "${SQLITE_BIN}" ]] ; then
echof "Binary file ${SQLITE_BIN} was not found, exiting"
exit 51
elif ! [[ -d "${QUERY_DIR}" ]] ; then
echof "The directory ${QUERY_DIR} does not exist, exiting"
exit 52
fi
}
run_query() {
local QUERY_INPUT_FILE="$1"
local QUERY_BASE_FILE="${QUERY_INPUT_FILE%${QUERY_INPUT_EXT}}"
local QUERY_OUTPUT_FILE="${QUERY_BASE_FILE}${QUERY_OUTPUT_EXT}"
echof "Running query in file ${QUERY_INPUT_FILE}"
./"${SQLITE_BIN}" "${TEMP_DB}" "$(cat "${QUERY_INPUT_FILE}")" | tee "${QUERY_OUTPUT_FILE}"
rm -f "${TEMP_DB}"
echof "Query has been executed, results have been logged in ${QUERY_OUTPUT_FILE}"
}
main() {
validate
echof "Attempting to run all queries in the ${QUERY_DIR} directory"
IFS=$'\n' # Required for filenames with spaces
for QUERY_INPUT_FILE in $(find "${QUERY_DIR}" -name "*${QUERY_INPUT_EXT}"); do
[[ -e "$QUERY_INPUT_FILE" ]] || continue
run_query "${QUERY_INPUT_FILE}"
done
unset IFS
echof "All queries have been executed"
}
main