forked from liuhaopen/SkynetMMO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbserver.lua
156 lines (140 loc) · 3.54 KB
/
dbserver.lua
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
local skynet = require "skynet"
local mysql = require "skynet.db.mysql"
require "skynet.manager"
require "common.util"
--[[
用法:
local dbserver = skynet.localname(".your db name")
local is_succeed = skynet.call(dbserver, "lua", "insert", "Account", {account_id=7, password="123"})
print('Cat:main.lua[insert] is_succeed', is_succeed)
local is_succeed, result = skynet.call(dbserver, "lua", "select_all", "Account")
if is_succeed then
print("Cat:main [start:30] result:", result)
PrintTable(result)
print("Cat:main [end]")
end
--]]
local db
local function ping()
while true do
if db then
db:query("select l;")
end
skynet.sleep(3600*1000)
end
end
local CMD = {}
function CMD.open( conf )
-- print("Cat:dbserver [start:18] conf:", conf)
-- PrintTable(conf)
-- print("Cat:dbserver [end]")
db = mysql.connect(conf)
skynet.fork(ping)
skynet.register(conf.name or "."..conf.database)
end
function CMD.close( conf )
if db then
db:disconnect()
db = nil
end
end
function CMD.insert( tablename, rows )
local cols = {}
local vals = {}
for k, v in pairs(rows) do
table.insert(cols, k)
if type(v) == "string" then
v = mysql.quote_sql_str(v)
end
table.insert(vals, v)
end
vals = table.concat(vals, ",")
cols = table.concat(cols, ",")
local sql = string.format("insert into %s(%s) values(%s);", tablename, cols, vals)
local result = db:query(sql)
if result.errno then
skynet.error(result.err)
return false
end
return true
end
function CMD.delete( tablename, key, value )
local sql = string.format("delete from %s where %s = %s;", tablename, key, mysql.quote_sql_str(tostring(value)))
local result = db:query(sql)
if result.errno then
skynet.error(result.err)
return false
end
return true
end
function CMD.query(command)
local result = db:query(command)
if result.errno then
skynet.error(result.err)
return false
end
return true, result
end
function CMD.update( tablename, key, value, row )
local t = {}
for k,v in pairs(row) do
if type(v) == "string" then
v = mysql.quote_sql_str(v)
end
table.insert(t, k.."="..v)
end
local setvalues = table.concat(t, ",")
local sql = string.format("update %s set %s where %s = '%s';", tablename, setvalues, key, value)
local result = db:query(sql)
if result.errno then
skynet.error(result.err)
return false
end
return true
end
function CMD.select_by_key( tablename, key, value )
local sql = string.format("select * from %s where %s = '%s';", tablename, key, value)
local result = db:query(sql)
if result.errno then
skynet.error(result.err)
return false
end
return true, result
end
function CMD.select_one_by_key( tablename, key, value )
local sql = string.format("select * from %s where %s = '%s';", tablename, key, value)
local result = db:query(sql)
if result.errno then
skynet.error(result.err)
return false
end
return true, result and result[1]
end
function CMD.select_by_condition( tablename, condition )
local sql = string.format("select * from %s where %s;", tablename, condition)
local result = db:query(sql)
if result.errno then
skynet.error(result.err)
return false
end
return true, result
end
function CMD.select_all( tablename )
local sql = string.format("select * from %s;", tablename)
local result = db:query(sql)
if result.errno then
skynet.error(result.err)
return false
end
return true, result
end
skynet.start(function()
skynet.dispatch("lua", function(session, source, cmd, ...)
local f = assert(CMD[cmd], "can't not find cmd :"..(cmd or "empty"))
if session == 0 then
f(...)
else
skynet.ret(skynet.pack(f(...)))
end
end)
end)