-
Notifications
You must be signed in to change notification settings - Fork 0
/
psql.sh
executable file
·71 lines (62 loc) · 1.28 KB
/
psql.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
#!/bin/bash -e
# @installable
# queries the local database
X=$(dirname `readlink -f ${BASH_SOURCE[0]}`)
source $X/env
[[ -f $LOCAL_ENV ]] && source $LOCAL_ENV
source $X/log.sh
source $X/prop.sh
MYSELF() { readlink -f "${BASH_SOURCE[0]}"; }
MYDIR() { echo "$(dirname $(MYSELF))"; }
MYNAME() { echo "$(basename $(MYSELF))"; }
CALLER=$(basename `readlink -f $0`)
separator="|"
# TODO support for different ports and hosts
connection="psql -U $DB_USER $DB_NAME"
ops='qAtX'
if [[ $# -lt 1 ]]; then
$connection
fi
query="$1"; shift
if [[ ! -n "$query" ]]; then
err "arg 1 must be the query"
exit 1
fi
if [[ "$query" == --create-db ]]; then
info "starting db $DB_NAME ..."
psql -U $DB_USER -c "create database $DB_NAME"
exit 0
fi
if [[ "$query" == --connection ]]; then
echo "$connection"
exit 0
fi
while test $# -gt 0
do
case "$1" in
--separator|-s)
shift
separator="$1"
;;
--ops)
shift
ops="$1"
;;
--full)
ops="c"
;;
--raw)
noop=noop
;;
-*)
echo "bad option '$1'"
exit 1
;;
esac
shift
done
if [[ -f "$query" ]]; then
$connection -$ops --field-separator="$separator" < "$query"
else
$connection -$ops --field-separator="$separator" -c "$query"
fi