#
#
# control tool for maintaining Kamailio
#
#===================================================================

##### ----------------------------------------------- #####
### MySQL specific variables and functions
#

##### ----------------------------------------------- #####
### load SQL base
#
if [ -f "$MYLIBDIR/kamctl.sqlbase" ]; then
	. "$MYLIBDIR/kamctl.sqlbase"
else
	echo "Cannot load SQL core functions '$MYLIBDIR/kamctl.sqlbase' - exiting ..."
	exit 255
fi

##### ----------------------------------------------- #####
### binaries
if [ -z "$MYSQL" ] ; then
	locate_tool mysql
	if [ -z "$TOOLPATH" ] ; then
		echo "error: 'mysql' tool not found: set MYSQL variable to correct tool path"
		exit
	fi
	MYSQL="$TOOLPATH"
fi

# input: sql query, optional mysql command-line params
mysql_query() {
	# if password not yet queried, query it now
	prompt_pw "MySQL password for user '$DBRWUSER@$DBHOST'"
	if [ -z "$DBPORT" ] ; then
		$MYSQL $2 -h $DBHOST -u $DBRWUSER "-p$DBRWPW" -e "$1 ;" $DBNAME
	else
		$MYSQL $2 -h $DBHOST -P $DBPORT -u $DBRWUSER "-p$DBRWPW" -e "$1 ;" $DBNAME
	fi
}

# input: sql query, optional mysql command-line params
mysql_ro_query() {
	if [ -z "$DBPORT" ] ; then
		$MYSQL $2 -h $DBHOST -u $DBROUSER "-p$DBROPW" -e "$1 ;" $DBNAME
	else
		$MYSQL $2 -h $DBHOST -P $DBPORT -u $DBROUSER "-p$DBROPW" -e "$1 ;" $DBNAME
	fi
}

mysql_connect() {
	# if password not yet queried, query it now
	prompt_pw "MySQL password for user '$DBRWUSER@$DBHOST'"
	if [ -z "$DBPORT" ] ; then
		$MYSQL $2 -h $DBHOST -u $DBRWUSER "-p$DBRWPW" $DBNAME
	else
		$MYSQL $2 -h $DBHOST -P $DBPORT -u $DBRWUSER "-p$DBRWPW" $DBNAME
	fi
}

DBCMD=mysql_query
DBROCMD=mysql_ro_query
DBRAWPARAMS="-B"
DBCMDCONNECT=mysql_connect

