-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmysqllog.lua
66 lines (55 loc) · 1.73 KB
/
mysqllog.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
--encoding=utf-8
local mysql = require "resty.mysql"
local HOST = ngx.var.MYSQL_HOST
local PORT = tonumber(ngx.var.MYSQL_PORT)
local DATABASE = ngx.var.MYSQL_DATABASE
local USER = ngx.var.MYSQL_USER
local PASSWORD = ngx.var.MYSQL_PASSWORD
local print = ngx.say
module(..., package.seeall)
function append(tab, args)
local db, err = mysql:new()
if not db then
print("failed to instantiate mysql: ", err)
return
end
db:set_timeout(1000) -- 1 sec
local ok, err, errno, sqlstate = db:connect{
host = HOST,
port = PORT,
database = DATABASE,
user = USER,
password = PASSWORD,
max_packet_size = 1024 * 1024}
if not ok then
print("failed to connect: ", err, ": ", errno, " ", sqlstate)
return
end
local basesql = string.format("insert into `%s`", tab)
local fields, values = " (", " values ("
for k, v in pairs(args) do
fields = fields .. k .. ","
values = values .. v .. ","
end
if string.sub(fields, -1)== "," then
fields = string.sub(fields, 1, -2)
values = string.sub(values, 1, -2)
end
fields = fields .. ")"
values = values .. ")"
local sql = basesql .. fields .. values
local res, err, errno, sqlstate = db:query(sql)
if not res then
print("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
return
end
-- print(res.affected_rows, " rows inserted into table ", "(last insert id: ", res.insert_id, ")")
print('ok')
-- put it into the connection pool of size 100,
-- with 10 seconds max idle timeout
local ok, err = db:set_keepalive(10000, 100)
if not ok then
print("failed to set keepalive: ", err)
return
end
end