@@ -6,11 +6,10 @@ local bit = bit
6
6
local ngx = ngx
7
7
local tonumber = tonumber
8
8
local setmetatable = setmetatable
9
- local error = error
10
9
11
- module ( ... )
10
+ local _M = {}
12
11
13
- _VERSION = ' 0.2'
12
+ _M . VERSION = ' 0.2'
14
13
15
14
local STATE_CONNECTED = 1
16
15
local STATE_COMMAND_SENT = 2
@@ -34,7 +33,7 @@ converters[701] = tonumber
34
33
-- NUMERICOID
35
34
converters [1700 ] = tonumber
36
35
37
- function new (self )
36
+ function _M . new ()
38
37
local sock , err = ngx .socket .tcp ()
39
38
if not sock then
40
39
return nil , err
@@ -43,7 +42,7 @@ function new(self)
43
42
return setmetatable ({ sock = sock , env = {}}, mt )
44
43
end
45
44
46
- function set_timeout (self , timeout )
45
+ function _M . set_timeout (self , timeout )
47
46
local sock = self .sock
48
47
if not sock then
49
48
return nil , " not initialized"
@@ -66,9 +65,9 @@ local function _get_data_n(data, len, i)
66
65
return d , i + len
67
66
end
68
67
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
72
71
73
72
local function _set_byte4 (n )
74
73
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)
87
86
return {data , " \0 " }
88
87
end
89
88
90
- function _send_packet (self , data , len , typ )
89
+ local function _send_packet (self , data , len , typ )
91
90
local sock = self .sock
92
91
local packet
93
92
if typ then
@@ -105,7 +104,7 @@ function _send_packet(self, data, len, typ)
105
104
return sock :send (packet )
106
105
end
107
106
108
- function _parse_error_packet (packet )
107
+ local function _parse_error_packet (packet )
109
108
local pos = 1
110
109
local flg , value , msg
111
110
msg = {}
@@ -128,15 +127,16 @@ function _parse_error_packet(packet)
128
127
return msg
129
128
end
130
129
131
- function _recv_packet (self )
130
+ local function _recv_packet (self )
132
131
-- receive type
133
132
local sock = self .sock
134
133
local typ , err = sock :receive (1 )
135
134
if not typ then
136
135
return nil , nil , " failed to receive packet type: " .. err
137
136
end
138
137
-- receive length
139
- local data , err = sock :receive (4 )
138
+ local data
139
+ data , err = sock :receive (4 )
140
140
if not data then
141
141
return nil , nil , " failed to read packet length: " .. err
142
142
end
@@ -152,26 +152,25 @@ function _recv_packet(self)
152
152
return data , typ
153
153
end
154
154
155
- function _compute_token (self , user , password , salt )
155
+ local function _compute_token (user , password , salt )
156
156
local token1 = ngx .md5 (password .. user )
157
157
local token2 = ngx .md5 (token1 .. salt )
158
158
return " md5" .. token2
159
159
end
160
160
161
- function connect (self , opts )
161
+ function _M . connect (self , opts )
162
162
local sock = self .sock
163
163
if not sock then
164
164
return nil , " not initialized"
165
165
end
166
-
166
+
167
167
local ok , err
168
168
169
169
self .compact = opts .compact
170
170
171
171
local host = opts .host
172
172
local database = opts .database or " "
173
173
local user = opts .user or " "
174
- local host = opts .host
175
174
local pool = opts .pool
176
175
local password = opts .password
177
176
@@ -217,10 +216,11 @@ function connect(self, opts)
217
216
-- packet_len + PG_PROTOCOL + user + database + end
218
217
-- req_len = 4 + 4 + string.len(user) + 6 + string.len(database) + 10 + 1
219
218
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 )
221
221
if not bytes then
222
222
return nil , " failed to send client authentication packet1: " .. err
223
- end
223
+ end
224
224
-- receive salt packet (len + data) no type
225
225
local packet , typ
226
226
packet , typ , err = _recv_packet (self )
@@ -230,12 +230,12 @@ function connect(self, opts)
230
230
if typ ~= ' R' then
231
231
return nil , " handshake error, got packet type:" .. typ
232
232
end
233
- local auth_type = string.sub (packet , 1 , 4 )
233
+ -- local auth_type = string.sub(packet, 1, 4)
234
234
local salt = string.sub (packet , 5 , 8 )
235
235
-- send passsowrd
236
- req = {_to_cstring (_compute_token (self , user , password , salt ))}
236
+ req = {_to_cstring (_compute_token (user , password , salt ))}
237
237
req_len = 40
238
- local bytes , err = _send_packet (self , req , req_len , ' p' )
238
+ bytes , err = _send_packet (self , req , req_len , ' p' )
239
239
if not bytes then
240
240
return nil , " failed to send client authentication packet2: " .. err
241
241
end
@@ -244,7 +244,9 @@ function connect(self, opts)
244
244
if typ ~= ' R' then
245
245
return nil , " auth return type not support"
246
246
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
248
250
return nil , " authentication failed"
249
251
end
250
252
while true do
@@ -254,9 +256,10 @@ function connect(self, opts)
254
256
end
255
257
-- env
256
258
if typ == ' S' then
259
+ local k , v
257
260
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 )
260
263
self .env [k ] = v
261
264
end
262
265
-- secret key
@@ -279,7 +282,7 @@ function connect(self, opts)
279
282
end
280
283
end
281
284
282
- function set_keepalive (self , ...)
285
+ function _M . set_keepalive (self , ...)
283
286
local sock = self .sock
284
287
if not sock then
285
288
return nil , " not initialized"
@@ -292,15 +295,15 @@ function set_keepalive(self, ...)
292
295
return sock :setkeepalive (... )
293
296
end
294
297
295
- function get_reused_times (self )
298
+ function _M . get_reused_times (self )
296
299
local sock = self .sock
297
300
if not sock then
298
301
return nil , " not initialized"
299
302
end
300
303
return sock :getreusedtimes ()
301
304
end
302
305
303
- function close (self )
306
+ function _M . close (self )
304
307
local sock = self .sock
305
308
if not sock then
306
309
return nil , " not initialized"
@@ -309,7 +312,7 @@ function close(self)
309
312
return sock :close ()
310
313
end
311
314
312
- function send_query (self , query )
315
+ local function send_query (self , query )
313
316
if self .state ~= STATE_CONNECTED then
314
317
return nil , " cannot send query in the current context: "
315
318
.. (self .state or " nil" )
@@ -327,7 +330,7 @@ function send_query(self, query)
327
330
return bytes , err
328
331
end
329
332
330
- function read_result (self )
333
+ local function read_result (self )
331
334
if self .state ~= STATE_COMMAND_SENT then
332
335
return nil , " cannot read result in the current context: " .. self .state
333
336
end
@@ -348,7 +351,7 @@ function read_result(self)
348
351
-- packet of fields
349
352
if typ == ' T' then
350
353
local field_num , pos = _get_byte2 (packet , 1 )
351
- for i = 1 , field_num do
354
+ for _ = 1 , field_num do
352
355
local field = {}
353
356
field .name , pos = _from_cstring (packet , pos )
354
357
field .table_id , pos = _get_byte4 (packet , pos )
@@ -388,7 +391,7 @@ function read_result(self)
388
391
local name = field .name
389
392
row [name ] = data
390
393
end
391
- end
394
+ end
392
395
table.insert (res , row )
393
396
end
394
397
if typ == ' E' then
@@ -408,11 +411,11 @@ function read_result(self)
408
411
self .state = STATE_CONNECTED
409
412
break
410
413
end
411
- end
414
+ end
412
415
return res , err
413
416
end
414
417
415
- function query (self , query )
418
+ function _M . query (self , query )
416
419
local bytes , err = send_query (self , query )
417
420
if not bytes then
418
421
return nil , " failed to send query: " .. err
@@ -421,17 +424,9 @@ function query(self, query)
421
424
return read_result (self )
422
425
end
423
426
424
- function escape_string (str )
427
+ function _M . escape_string (str )
425
428
local new = string.gsub (str , " ['\\ ]" , " %0%0" )
426
429
return new
427
430
end
428
431
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