-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibmysql.lua
More file actions
160 lines (136 loc) · 4.26 KB
/
libmysql.lua
File metadata and controls
160 lines (136 loc) · 4.26 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
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
156
157
158
159
160
local sgsub = string.gsub
local gmatch = string.gmatch
local tinsert = table.insert
local quote_sql_str = ngx.quote_sql_str
local type = type
local ipairs = ipairs
local pairs = pairs
local random = math.random
local randomseed = math.randomseed
--local cjson = require("cjson")
local mytime = ngx.time()
local mysql = require("resty.mysql")
local config = require("config")["mysql"]
local _DB = {}
function _DB:new(self,database)
if not database then
databaseconfig = config["default"]
else
if database and type(database) ~= "string" then
ngx.log(ngx.ERR, "database is not string")
return nil, "database is not string"
end
databaseconfig = config[database]
end
return setmetatable({databaseconfig=databaseconfig}, { __index = _DB})
end
local function exec(self,sql,forType)
if not sql then
ngx.log(ngx.ERR, "sql parse error! please check")
return nil, "sql parse error! please check"
end
local db, err = mysql:new()
if not db then
return nil, "failed to instantiate mysql: "..err
end
local config = self.databaseconfig
local conf,random_num
if forType == "main" then
conf = config["main"]
else
if not config["query"] then
conf = config["main"]
elseif #config["query"] == 1 then
conf = config["query"][1]
else
randomseed(mytime)
random_num = random(1,#config["query"])
conf = config["query"][random_num]
end
end
db:set_timeout(conf.timeout) -- 1 sec
local ok, err, errno, sqlstate = db:connect(conf.connect_config)
if not ok then
--再尝试其他读库
if random_num then
for i=1,#config["query"],1 do
if i ~= random_num then
conf = config["query"][i]
ok, err, errno, sqlstate = db:connect(conf.connect_config)
end
if ok then
break
end
end
end
end
if not ok then
ngx.log(ngx.ERR,"failed to connect: ", err, ": ", errno, " ", sqlstate)
return nil
end
--ngx.log(ngx.ERR, "connected to mysql, reused_times:", db:get_reused_times(), " sql:", sql)
db:query("SET NAMES utf8")
local res, err, errno, sqlstate = db:query(sql)
if not res then
ngx.log(ngx.ERR, "bad result: ", err, ": ", errno, ": ", sqlstate, ".")
end
local ok, err = db:set_keepalive(conf.pool_config.max_idle_timeout, conf.pool_config.pool_size)
if not ok then
ngx.log(ngx.ERR,"failed to set keepalive: ",err)
end
return res, err, errno, sqlstate
end
local function table_is_array(t)
if type(t) ~= "table" then return false end
local i = 0
for _ in pairs(t) do
i = i + 1
if t[i] == nil then return false end
end
return true
end
local function split(str, delimiter)
if str==nil or str=='' or delimiter==nil then
return nil
end
local result = {}
for match in (str..delimiter):gmatch("(.-)"..delimiter) do
tinsert(result, match)
end
return result
end
local function compose(t, params)
if t==nil or params==nil or type(t)~="table" or type(params)~="table" or #t~=#params+1 or #t==0 then
return nil
else
local result = t[1]
for i=1, #params do
result = result .. params[i].. t[i+1]
end
return result
end
end
local function parse_sql(sql, params)
if not params or not table_is_array(params) or #params == 0 then
return sql
end
local new_params = {}
for i, v in ipairs(params) do
if v and type(v) == "string" then
v = quote_sql_str(v)
end
tinsert(new_params, v)
end
local t = split(sql,"?")
local sql = compose(t, new_params)
return sql
end
function _DB.query(self,sql, params)
local sql = parse_sql(sql, params)
return exec(self,sql,"query")
end
function _DB.main(self,sql, params)
local sql = parse_sql(sql, params)
return exec(self,sql,"main")
end
return _DB