-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun-sql
executable file
·57 lines (39 loc) · 829 Bytes
/
run-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
#!/bin/sh
# Copyright (C) 2022-2023 by Yuri Victorovich. All rights reserved.
##
## run-sql allows users to run prepared SQL statements against PortsDB
##
##
## set strict mode
##
set -euo pipefail
##
## find CODEBASE
##
SCRIPT=$(readlink -f "$0")
CODEBASE=$(dirname "$SCRIPT")
##
## include functions
##
. $CODEBASE/include/functions.sh
##
## find PortsDB
##
PORTSDB=${PORTSDB:-./ports.sqlite} # default value
[ -f "$PORTSDB" ] || PORTSDB=$CODEBASE/ports.sqlite
[ -f $PORTSDB ] || echo "can't find PortsDB at '$PORTSDB', please set the PORTSDB environment variable"
##
## arguments
##
SQL_FILE="$1"
[ -f "$SQL_FILE" ] || fail "supplied argument '$SQL_FILE' isn't a file"
##
## substitute arguments if any
##
SQL=$(cat $SQL_FILE)
shift
SQL="$(printf "$SQL" "$@")"
##
## execute query
##
sqlite3 $PORTSDB "$SQL"