Skip to content

Commit cfbe47d

Browse files
committed
Update to new lua 5.1 module syntax and make luacheck happy
1 parent 19bc847 commit cfbe47d

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

.luacheckrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
std = "ngx_lua"

lib/resty/postgres.lua

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ local bit = bit
66
local ngx = ngx
77
local tonumber = tonumber
88
local setmetatable = setmetatable
9-
local error = error
109

11-
module(...)
10+
local _M = {}
1211

13-
_VERSION = '0.2'
12+
_M.VERSION = '0.2'
1413

1514
local STATE_CONNECTED = 1
1615
local STATE_COMMAND_SENT = 2
@@ -34,7 +33,7 @@ converters[701] = tonumber
3433
-- NUMERICOID
3534
converters[1700] = tonumber
3635

37-
function new(self)
36+
function _M.new()
3837
local sock, err = ngx.socket.tcp()
3938
if not sock then
4039
return nil, err
@@ -43,7 +42,7 @@ function new(self)
4342
return setmetatable({ sock = sock, env = {}}, mt)
4443
end
4544

46-
function set_timeout(self, timeout)
45+
function _M.set_timeout(self, timeout)
4746
local sock = self.sock
4847
if not sock then
4948
return nil, "not initialized"
@@ -66,9 +65,9 @@ local function _get_data_n(data, len, i)
6665
return d, i+len
6766
end
6867

69-
local function _set_byte2(n)
70-
return string.char(bit.band(bit.rshift(n, 8), 0xff), bit.band(n, 0xff))
71-
end
68+
-- local function _set_byte2(n)
69+
-- return string.char(bit.band(bit.rshift(n, 8), 0xff), bit.band(n, 0xff))
70+
-- end
7271

7372
local function _set_byte4(n)
7473
return string.char(bit.band(bit.rshift(n, 24), 0xff), bit.band(bit.rshift(n, 16), 0xff),
@@ -87,7 +86,7 @@ local function _to_cstring(data)
8786
return {data, "\0"}
8887
end
8988

90-
function _send_packet(self, data, len, typ)
89+
local function _send_packet(self, data, len, typ)
9190
local sock = self.sock
9291
local packet
9392
if typ then
@@ -105,7 +104,7 @@ function _send_packet(self, data, len, typ)
105104
return sock:send(packet)
106105
end
107106

108-
function _parse_error_packet(packet)
107+
local function _parse_error_packet(packet)
109108
local pos = 1
110109
local flg, value, msg
111110
msg = {}
@@ -128,15 +127,16 @@ function _parse_error_packet(packet)
128127
return msg
129128
end
130129

131-
function _recv_packet(self)
130+
local function _recv_packet(self)
132131
-- receive type
133132
local sock = self.sock
134133
local typ, err = sock:receive(1)
135134
if not typ then
136135
return nil, nil, "failed to receive packet type: " .. err
137136
end
138137
-- receive length
139-
local data, err = sock:receive(4)
138+
local data
139+
data, err = sock:receive(4)
140140
if not data then
141141
return nil, nil , "failed to read packet length: " .. err
142142
end
@@ -152,26 +152,25 @@ function _recv_packet(self)
152152
return data, typ
153153
end
154154

155-
function _compute_token(self, user, password, salt)
155+
local function _compute_token(user, password, salt)
156156
local token1 = ngx.md5(password .. user)
157157
local token2 = ngx.md5(token1 .. salt)
158158
return "md5" .. token2
159159
end
160160

161-
function connect(self, opts)
161+
function _M.connect(self, opts)
162162
local sock = self.sock
163163
if not sock then
164164
return nil, "not initialized"
165165
end
166-
166+
167167
local ok, err
168168

169169
self.compact = opts.compact
170170

171171
local host = opts.host
172172
local database = opts.database or ""
173173
local user = opts.user or ""
174-
local host = opts.host
175174
local pool = opts.pool
176175
local password = opts.password
177176

@@ -217,10 +216,11 @@ function connect(self, opts)
217216
-- packet_len + PG_PROTOCOL + user + database + end
218217
-- req_len = 4 + 4 + string.len(user) + 6 + string.len(database) + 10 + 1
219218
req_len = string.len(user) + string.len(database) + 25
220-
local bytes, err = _send_packet(self, req, req_len)
219+
local bytes
220+
bytes, err = _send_packet(self, req, req_len)
221221
if not bytes then
222222
return nil, "failed to send client authentication packet1: " .. err
223-
end
223+
end
224224
-- receive salt packet (len + data) no type
225225
local packet, typ
226226
packet, typ, err = _recv_packet(self)
@@ -230,12 +230,12 @@ function connect(self, opts)
230230
if typ ~= 'R' then
231231
return nil, "handshake error, got packet type:" .. typ
232232
end
233-
local auth_type = string.sub(packet, 1, 4)
233+
-- local auth_type = string.sub(packet, 1, 4)
234234
local salt = string.sub(packet, 5, 8)
235235
-- send passsowrd
236-
req = {_to_cstring(_compute_token(self, user, password, salt))}
236+
req = {_to_cstring(_compute_token(user, password, salt))}
237237
req_len = 40
238-
local bytes, err = _send_packet(self, req, req_len, 'p')
238+
bytes, err = _send_packet(self, req, req_len, 'p')
239239
if not bytes then
240240
return nil, "failed to send client authentication packet2: " .. err
241241
end
@@ -244,7 +244,9 @@ function connect(self, opts)
244244
if typ ~= 'R' then
245245
return nil, "auth return type not support"
246246
end
247-
if packet ~= AUTH_REQ_OK then
247+
if not packet then
248+
return nil, "read packet error:" .. err
249+
elseif packet ~= AUTH_REQ_OK then
248250
return nil, "authentication failed"
249251
end
250252
while true do
@@ -254,9 +256,10 @@ function connect(self, opts)
254256
end
255257
-- env
256258
if typ == 'S' then
259+
local k, v
257260
local pos = 1
258-
local k, pos = _from_cstring(packet, pos)
259-
local v, pos = _from_cstring(packet, pos)
261+
k, pos = _from_cstring(packet, pos)
262+
v = _from_cstring(packet, pos)
260263
self.env[k] = v
261264
end
262265
-- secret key
@@ -279,7 +282,7 @@ function connect(self, opts)
279282
end
280283
end
281284

282-
function set_keepalive(self, ...)
285+
function _M.set_keepalive(self, ...)
283286
local sock = self.sock
284287
if not sock then
285288
return nil, "not initialized"
@@ -292,15 +295,15 @@ function set_keepalive(self, ...)
292295
return sock:setkeepalive(...)
293296
end
294297

295-
function get_reused_times(self)
298+
function _M.get_reused_times(self)
296299
local sock = self.sock
297300
if not sock then
298301
return nil, "not initialized"
299302
end
300303
return sock:getreusedtimes()
301304
end
302305

303-
function close(self)
306+
function _M.close(self)
304307
local sock = self.sock
305308
if not sock then
306309
return nil, "not initialized"
@@ -309,7 +312,7 @@ function close(self)
309312
return sock:close()
310313
end
311314

312-
function send_query(self, query)
315+
local function send_query(self, query)
313316
if self.state ~= STATE_CONNECTED then
314317
return nil, "cannot send query in the current context: "
315318
.. (self.state or "nil")
@@ -327,7 +330,7 @@ function send_query(self, query)
327330
return bytes, err
328331
end
329332

330-
function read_result(self)
333+
local function read_result(self)
331334
if self.state ~= STATE_COMMAND_SENT then
332335
return nil, "cannot read result in the current context: " .. self.state
333336
end
@@ -348,7 +351,7 @@ function read_result(self)
348351
-- packet of fields
349352
if typ == 'T' then
350353
local field_num, pos = _get_byte2(packet, 1)
351-
for i=1, field_num do
354+
for _=1, field_num do
352355
local field = {}
353356
field.name, pos = _from_cstring(packet, pos)
354357
field.table_id, pos = _get_byte4(packet, pos)
@@ -388,7 +391,7 @@ function read_result(self)
388391
local name = field.name
389392
row[name] = data
390393
end
391-
end
394+
end
392395
table.insert(res, row)
393396
end
394397
if typ == 'E' then
@@ -408,11 +411,11 @@ function read_result(self)
408411
self.state = STATE_CONNECTED
409412
break
410413
end
411-
end
414+
end
412415
return res, err
413416
end
414417

415-
function query(self, query)
418+
function _M.query(self, query)
416419
local bytes, err = send_query(self, query)
417420
if not bytes then
418421
return nil, "failed to send query: " .. err
@@ -421,17 +424,9 @@ function query(self, query)
421424
return read_result(self)
422425
end
423426

424-
function escape_string(str)
427+
function _M.escape_string(str)
425428
local new = string.gsub(str, "['\\]", "%0%0")
426429
return new
427430
end
428431

429-
local class_mt = {
430-
-- to prevent use of casual module global variables
431-
__newindex = function (table, key, val)
432-
error('attempt to write to undeclared variable "' .. key .. '"')
433-
end
434-
}
435-
436-
setmetatable(_M, class_mt)
437-
432+
return _M

0 commit comments

Comments
 (0)