# linux script server sql about functions encapsulation.
# Longwei Lai
###################################################################

source "${____FWK_SCRIPT_PATH}/lnx_fwk_path.sh"
source "${____FWK_SCRIPT_PATH}/lnx_fwk_server.sh"
source "${____LOGIC_SCRIPT_PATH}/lnx_logic_defs.sh"

# execute sql command on config database
# arguments:
#   arg1: sql command
# returns:
#   echo style:
#       sql query return
#   return style:
#       0: success
#       <other>: failed
function exec_sql_cmd_on_confdb()
{
    exec_sql_cmd mysql_confdb "$1"
}

# execute sql command on game database
# arguments:
#   arg1: sql command
# returns:
#   echo style:
#       sql query return
#   return style:
#       0: success
#       <other>: failed
function exec_sql_cmd_on_gamedb()
{
    exec_sql_cmd mysql_gamedb "$1"
}

# execute sql command on specific config database(in dbconf.lua)
# arguments:
#   arg1: config name
#   arg2: sql command
# returns:
#   echo style:
#       sql query return
#   return style:
#       0: success
#       <other>: failed
function exec_sql_cmd()
{
    local dbcfg_name="$1"
    local sql_stmt="$2"
    local access_info=(`_get_db_access_info "${dbcfg_name}"`)
    local sql_ret="`python2.6 ${____FWK_SCRIPT_PATH}/mysql_handler.py ${access_info[0]} ${access_info[1]} "${access_info[2]}" "${access_info[3]}" "${access_info[4]}" "${sql_stmt}"`"
    if [ $? -ne 0 ]; then
        echo -n "${sql_ret}"
        return 1
    fi

    echo -n "${sql_ret}"
}

# -------------------- internal implementation ----------------------
function _get_db_access_info()
{
    local dbcfg_name="$1"

    local dbconf_file="${SVR_PATH}/dbconf.lua"
    if [ ! -f "${dbconf_file}" ]; then
        log_err "not found db config file: ${dbconf_file}"
        return 1
    fi

    local can_grep=FALSE
    local sql_host=
    local sql_port=
    local sql_user=
    local sql_passwd=
    local sql_db=
    while read line; do
        if [ ! -z "`echo -n "${line}" | grep "dbconf.${dbcfg_name}"`" ]; then
            can_grep=TRUE
            continue
        fi

        if [ "${can_grep}" != TRUE ]; then
            continue
        fi

        local isend="`echo -n "${line}" | sed -n -r 's/\s*(})\s*/\1/p'`"
        if [ ! -z "${isend}" ]; then
            break
        fi

        local gethost="`echo -n "${line}" | sed -n -r 's/\s*host\s*=\s*"(.*)"\s*,/\1/p'`"
        if [ ! -z "${gethost}" ]; then
            sql_host="${gethost}"
        fi

        local getport="`echo -n "${line}" | sed -n -r 's/\s*port\s*=\s*(.*)\s*,/\1/p'`"
        if [ ! -z "${getport}" ]; then
            sql_port="${getport}"
        fi

        local getuser="`echo -n "${line}" | sed -n -r 's/\s*user\s*=\s*"([^"]*)"\s*,/\1/p'`"
        if [ ! -z "${getuser}" ]; then
            sql_user="${getuser}"
        fi

        local getpasswd="`echo -n "${line}" | sed -n -r 's/\s*password\s*=\s*"([^"]*)"\s*,/\1/p'`"
        if [ ! -z "${getpasswd}" ]; then
            sql_passwd="${getpasswd}"
        fi

        local getdb="`echo -n "${line}" | sed -n -r 's/\s*database\s*=\s*"([^"]*)"\s*,/\1/p'`"
        if [ ! -z "${getdb}" ]; then
            sql_db="${getdb}"
        fi
    done < "${dbconf_file}"

    echo ${sql_host} ${sql_port} ${sql_user} ${sql_passwd} ${sql_db}
}


