kamctl: added DBPORT option
[sip-router] / utils / kamctl / kamctl.pgsql
1 #
2 #
3 # control tool for maintaining Kamailio
4 #
5 #===================================================================
6
7 ##### ----------------------------------------------- #####
8 ### PGSQL specific variables and functions
9 #
10
11 ##### ----------------------------------------------- #####
12 ### load SQL base
13 #
14 if [ -f "$MYLIBDIR/kamctl.sqlbase" ]; then
15         . "$MYLIBDIR/kamctl.sqlbase"
16 else
17         echo "Cannot load SQL core functions '$MYLIBDIR/kamctl.sqlbase' - exiting ..."
18         exit -1
19 fi
20
21 ##### ----------------------------------------------- #####
22 ### binaries
23 if [ -z "$PGSQL" ] ; then
24         locate_tool psql
25         if [ -z "$TOOLPATH" ] ; then
26                 echo "error: 'psql' tool not found: set PGSQL variable to correct tool path"
27                 exit
28         fi
29         PGSQL="$TOOLPATH"
30 fi
31
32
33 # input: sql query, optional pgsql command-line params
34 pgsql_query() {
35         # if password not yet queried, query it now
36         prompt_pw "PgSQL password for user '$DBRWUSER@$DBHOST'"
37         mecho "pgsql_query: $PGSQL $2 -A -q -t -P fieldsep='    ' -h $DBHOST -U $DBRWUSER $DBNAME -c '$1'"
38         if [ -z "$DBPORT" ] ; then
39                 PGPASSWORD="$DBRWPW" $PGSQL $2 \
40                         -A -q -t \
41                         -P fieldsep="   " \
42                         -h $DBHOST \
43                         -U $DBRWUSER \
44                         $DBNAME \
45                         -c "$1"
46         else
47                 PGPASSWORD="$DBRWPW" $PGSQL $2 \
48                         -A -q -t \
49                         -P fieldsep="   " \
50                         -h $DBHOST \
51                         -p $DBPORT \
52                         -U $DBRWUSER \
53                         $DBNAME \
54                         -c "$1"
55         fi
56 }
57
58 # input: sql query, optional pgsql command-line params
59 pgsql_ro_query() {
60         mdbg "pgsql_ro_query: $PGSQL $2 -h $DBHOST -U $DBROUSER $DBNAME -c '$1'"
61         if [ -z "$DBPORT" ] ; then
62                 PGPASSWORD="$DBROPW" $PGSQL $2 \
63                         -h $DBHOST \
64                         -U $DBROUSER \
65                         $DBNAME \
66                         -c "$1"
67         else
68                 PGPASSWORD="$DBROPW" $PGSQL $2 \
69                         -h $DBHOST \
70                         -p $DBPORT \
71                         -U $DBROUSER \
72                         $DBNAME \
73                         -c "$1"
74         fi
75 }
76
77 DBCMD=pgsql_query
78 DBROCMD=pgsql_ro_query
79 DBRAWPARAMS="-A -q -t"
80