/
deepdive-sql
executable file
·59 lines (56 loc) · 1.91 KB
/
deepdive-sql
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
#!/usr/bin/env bash
# deepdive-sql -- Runs a SQL query against the database for the DeepDive application
# > deepdive sql
# Starts a SQL prompt for the underlying database.
#
# You must either have the DEEPDIVE_DB_URL environment set to a proper URL or
# run this command under a DeepDive application where the URL is set in a db.url file.
#
# > deepdive sql "SELECT ..."
# > deepdive sql "INSERT ..."
# > deepdive sql "UPDATE ..."
# > deepdive sql "CREATE ..."
# Executes given SQL query against the underlying database and prints the
# result in a database-specific format.
#
# > deepdive sql eval "SELECT ..." [format=FORMAT] [header=1]
# Evaluates a given SQL query and prints the result in the specified FORMAT.
# FORMAT can be tsv, csv, or json, and defaults to tsv format.
# Prints the header line for tsv and csv format when header=1 is given.
##
set -eu
# parse database settings
. load-db-driver.sh
# process optional first SQL argument
if [[ $# -gt 0 ]]; then
sql=$1; shift
# there could be a mode argument we recognize before the SQL
case $sql in
eval)
# get the SQL argument
sql_mode=$sql
if [[ $# -gt 0 ]]; then
sql=$1; shift
else
error "Missing SQL statement for '$sql_mode' mode"
fi
# parse rest of the optional arguments
eval "$(
# default options
format=tsv
header=0
[[ $# -eq 0 ]] || declare -- "$@"
for opt in format header
do printf "$opt=%q; " "${!opt:-}"
done
)"
exec db-query "$sql" "$format" "$header"
;;
*)
# otherwise, we just execute the non-empty SQL passing through rest of the arguments
[[ -z "${sql//[[:space:]]/}" ]] ||
exec db-execute "$sql" "$@"
esac
else
exec db-prompt "$@"
fi