MysqlUtil.lua
2.29 KB
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
72
73
74
75
76
77
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