MysqlUtil.lua 2.29 KB
local skynet = require "skynet"
local mysqlproxy = require "shared.mysqlproxy"
require "utils.CommonFunc"


function getDbCfgVal(tbName, keyName, fieldName)
    local sql = string.format("SELECT * FROM `%s` WHERE `key` = '%s';", tbName, keyName)
    local res = mysqlproxy:query(sql)
    if not next(res) or res.errno then
        return
    end

    return res[1][fieldName]
end

function setDbCfgVal(tbName, keyName, fieldName, fieldVal)
    if type(fieldVal) == "string" then fieldVal = string.format("'%s'", fieldVal) end
    local sql = string.format("UPDATE `%s` SET `%s` = %s WHERE `key` = '%s';", tbName, fieldName, fieldVal, keyName)
    mysqlproxy:query(sql)
end

function getFriendCount(roleId)
    local res = mysqlproxy:query(string.format("SELECT `id` FROM `Friend` WHERE `roleid` = %s", roleId))
    if res["errno"] then
        return globalCsv.friendListLimit
    end

    return #res
end

function addFriend(roleId, friendId)
	local stmt_csp = mysqlproxy:prepare("call add_friends(?, ?, ?)")
    --logdump(stmt_csp, '')
    local r = mysqlproxy:execute(stmt_csp, roleId, friendId, skynet.timex())
    if r[1][1]["t_error"] == 0 then
		rpcRole(roleId, "addFriend", friendId)
		rpcRole(friendId, "addFriend", roleId)
    end
    mysqlproxy:stmt_close(stmt_csp)
end

function delFriend(roleId, friendId)
	local stmt_csp = mysqlproxy:prepare("call del_friends(?, ?)")
    --logdump(stmt_csp, '')
    local r = mysqlproxy:execute(stmt_csp, roleId, friendId)
    if r[1][1]["t_error"] == 0 then
		rpcRole(roleId, "delFriend", friendId)
		rpcRole(friendId, "delFriend", roleId)
    end
    mysqlproxy:stmt_close(stmt_csp)
end

function roleExists(roleId)
    local res = mysqlproxy:query(string.format("SELECT `id` FROM `Role` WHERE `del` = 0 and `id` = %s", roleId))
    if res["errno"] or not next(res) then
        return false
    end

    return true
end

function roleUidExists(uid)
    local res = mysqlproxy:query(string.format("SELECT `name` FROM `Role` WHERE `del` = 0 and `uid` = '%s'", uid))
    if res["errno"] or not next(res) then
        return false
    end

    return true, res[1]["name"]
end

function roleUnRegister(roleId)
    local res = mysqlproxy:query(string.format("UPDATE`Role` SET `del` = 1 WHERE `id` = %s", roleId))
    if res["errno"] or not next(res) then
        return false
    end
    return true
end