/
mysql.lua
835 lines (582 loc) · 18 KB
/
mysql.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
-- Copyright (C) 2012 Zhang "agentzh" Yichun (章亦春)
module("resty.mysql", package.seeall)
_VERSION = '0.10'
local bit = require "bit"
-- constants
local STATE_CONNECTED = 1
local STATE_COMMAND_SENT = 2
local COM_QUERY = 0x03
local SERVER_MORE_RESULTS_EXISTS = 8
local class = resty.mysql
-- global variables
local mt = { __index = class }
local sub = string.sub
local tcp = ngx.socket.tcp
local insert = table.insert
local strlen = string.len
local strbyte = string.byte
local strchar = string.char
local strfind = string.find
local strrep = string.rep
local null = ngx.null
local band = bit.band
local bxor = bit.bxor
local bor = bit.bor
local lshift = bit.lshift
local rshift = bit.rshift
local tohex = bit.tohex
local sha1 = ngx.sha1_bin
local concat = table.concat
-- mysql field value type converters
local converters = {}
for i = 0x01, 0x05 do
-- tiny, short, long, float, double
converters[i] = tonumber
end
-- converters[0x08] = tonumber -- long long
converters[0x09] = tonumber -- int24
converters[0x0d] = tonumber -- year
local function _get_byte2(data, i)
local a, b = strbyte(data, i, i + 1)
return bor(a, lshift(b, 8)), i + 2
end
local function _get_byte3(data, i)
local a, b, c = strbyte(data, i, i + 2)
return bor(a, lshift(b, 8), lshift(c, 16)), i + 3
end
local function _get_byte4(data, i)
local a, b, c, d = strbyte(data, i, i + 3)
return bor(a, lshift(b, 8), lshift(c, 16), lshift(d, 24)), i + 4
end
local function _get_byte8(data, i)
local a, b, c, d, e, f, g, h = strbyte(data, i, i + 7)
return bor(a, lshift(b, 8), lshift(c, 16), lshift(d, 24), lshift(e, 32),
lshift(f, 40), lshift(g, 48), lshift(h, 56)), i + 8
end
local function _set_byte2(n)
return strchar(band(n, 0xff), band(rshift(n, 8), 0xff))
end
local function _set_byte3(n)
return strchar(band(n, 0xff), band(rshift(n, 8), 0xff),
band(rshift(n, 16), 0xff))
end
local function _set_byte4(n)
return strchar(band(n, 0xff), band(rshift(n, 8), 0xff),
band(rshift(n, 16), 0xff), band(rshift(n, 24), 0xff))
end
local function _from_cstring(data, i)
local last = strfind(data, "\0", i, true)
if not last then
return nil, nil
end
return sub(data, i, last), last + 1
end
local function _to_cstring(data)
return {data, "\0"}
end
local function _to_binary_coded_string(data)
return {strchar(strlen(data)), data}
end
local function _dump(data)
local bytes = {}
for i = 1, #data do
insert(bytes, strbyte(data, i, i))
end
return concat(bytes, " ")
end
local function _dumphex(data)
local bytes = {}
for i = 1, #data do
insert(bytes, tohex(strbyte(data, i), 2))
end
return concat(bytes, " ")
end
local function _compute_token(password, scramble)
if password == "" then
return ""
end
local stage1 = sha1(password)
local stage2 = sha1(stage1)
local stage3 = sha1(scramble .. stage2)
local bytes = {}
for i = 1, #stage1 do
insert(bytes,
bxor(strbyte(stage3, i), strbyte(stage1, i)))
end
return strchar(unpack(bytes))
end
function _send_packet(self, req, size)
local sock = self.sock
self.packet_no = self.packet_no + 1
--print("packet no: ", self.packet_no)
local packet = {
_set_byte3(size),
strchar(self.packet_no),
req
}
--print("sending packet...")
return sock:send(packet)
end
function _recv_packet(self)
local sock = self.sock
local data, err = sock:receive(4) -- packet header
if not data then
return nil, nil, "failed to receive packet header: " .. err
end
--print("packet header: ", _dump(data))
local len, pos = _get_byte3(data, 1)
--print("packet length: ", len)
if len == 0 then
return nil, nil, "empty packet"
end
if len > self._max_packet_size then
return nil, nil, "packet size too big: " .. len
end
local num = strbyte(data, pos)
--print("recv packet: packet no: ", num)
self.packet_no = num
data, err = sock:receive(len)
--print("receive returned")
if not data then
return nil, nil, "failed to read packet content: " .. err
end
--print("packet content: ", _dump(data))
--print("packet content (ascii): ", data)
local field_count = strbyte(data, 1)
local typ
if field_count == 0x00 then
typ = "OK"
elseif field_count == 0xff then
typ = "ERR"
elseif field_count == 0xfe then
typ = "EOF"
elseif field_count <= 250 then
typ = "DATA"
end
return data, typ
end
local function _from_length_coded_bin(data, pos)
local first = strbyte(data, pos)
--print("LCB: first: ", first)
if not first then
return nil, pos
end
if first >= 0 and first <= 250 then
return first, pos + 1
end
if first == 251 then
return null, pos + 1
end
if first == 252 then
pos = pos + 1
return _get_byte2(data, pos)
end
if first == 253 then
pos = pos + 1
return _get_byte3(data, pos)
end
if first == 254 then
pos = pos + 1
return _get_byte8(data, pos)
end
return false, pos + 1
end
local function _from_length_coded_str(data, pos)
local len
len, pos = _from_length_coded_bin(data, pos)
if len == nil or len == null then
return null, pos
end
return sub(data, pos, pos + len - 1), pos + len
end
local function _parse_ok_packet(packet)
local res = {}
local pos
res.affected_rows, pos = _from_length_coded_bin(packet, 2)
--print("affected rows: ", res.affected_rows, ", pos:", pos)
res.insert_id, pos = _from_length_coded_bin(packet, pos)
--print("insert id: ", res.insert_id, ", pos:", pos)
res.server_status, pos = _get_byte2(packet, pos)
--print("server status: ", res.server_status, ", pos:", pos)
res.warning_count, pos = _get_byte2(packet, pos)
--print("warning count: ", res.warning_count, ", pos: ", pos)
local message = sub(packet, pos)
if message and message ~= "" then
res.message = message
end
--print("message: ", res.message, ", pos:", pos)
return res
end
local function _parse_eof_packet(packet)
local pos = 2
local warning_count, pos = _get_byte2(packet, pos)
local status_flags = _get_byte2(packet, pos)
return warning_count, status_flags
end
local function _parse_err_packet(packet)
local errno, pos = _get_byte2(packet, 2)
local marker = sub(packet, pos, pos)
local sqlstate
if marker == '#' then
-- with sqlstate
pos = pos + 1
sqlstate = sub(packet, pos, pos + 5 - 1)
pos = pos + 5
end
local message = sub(packet, pos)
return errno, message, sqlstate
end
local function _parse_result_set_header_packet(packet)
local field_count, pos = _from_length_coded_bin(packet, 1)
local extra
extra = _from_length_coded_bin(packet, pos)
return field_count, extra
end
local function _parse_field_packet(data)
local col = {}
local catalog, db, table, orig_table, orig_name, charsetnr, length
local pos
catalog, pos = _from_length_coded_str(data, 1)
--print("catalog: ", col.catalog, ", pos:", pos)
db, pos = _from_length_coded_str(data, pos)
table, pos = _from_length_coded_str(data, pos)
orig_table, pos = _from_length_coded_str(data, pos)
col.name, pos = _from_length_coded_str(data, pos)
orig_name, pos = _from_length_coded_str(data, pos)
pos = pos + 1 -- ignore the filler
charsetnr, pos = _get_byte2(data, pos)
length, pos = _get_byte4(data, pos)
col.type = strbyte(data, pos)
--[[
pos = pos + 1
col.flags, pos = _get_byte2(data, pos)
col.decimals = strbyte(data, pos)
pos = pos + 1
local default = sub(data, pos + 2)
if default and default ~= "" then
col.default = default
end
--]]
return col
end
local function _parse_row_data_packet(data, cols, compact)
local row = {}
local pos = 1
for i = 1, #cols do
local value
value, pos = _from_length_coded_str(data, pos)
local col = cols[i]
local typ = col.type
local name = col.name
--print("row field value: ", value, ", type: ", typ)
if value ~= null then
local conv = converters[typ]
if conv then
value = conv(value)
end
-- insert(row, value)
end
if compact then
insert(row, value)
else
row[name] = value
end
end
return row
end
local function _recv_field_packet(self)
local packet, typ, err = _recv_packet(self)
if not packet then
return nil, err
end
if typ == "ERR" then
local errno, msg, sqlstate = _parse_err_packet(packet)
return nil, msg, errno, sqlstate
end
if typ ~= 'DATA' then
return nil, "bad field packet type: " .. typ
end
-- typ == 'DATA'
return _parse_field_packet(packet)
end
function new(self)
local sock, err = tcp()
if not sock then
return nil, err
end
return setmetatable({ sock = sock }, mt)
end
function set_timeout(self, timeout)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
return sock:settimeout(timeout)
end
function connect(self, opts)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
local max_packet_size = opts.max_packet_size
if not max_packet_size then
max_packet_size = 1024 * 1024 -- default 1 MB
end
self._max_packet_size = max_packet_size
local ok, err
self.compact = opts.compact_arrays
local database = opts.database or ""
local user = opts.user or ""
local pool = opts.pool
local host = opts.host
if host then
local port = opts.port or 3306
if not pool then
pool = concat({user, database, host, port}, ":")
end
ok, err = sock:connect(host, port, { pool = pool })
else
local path = opts.path
if not path then
return nil, 'neither "host" nor "path" options are specified'
end
if not pool then
pool = concat({user, database, path}, ":")
end
ok, err = sock:connect("unix:" .. path, { pool = pool })
end
if not ok then
return nil, 'failed to connect: ' .. err
end
local reused = sock:getreusedtimes()
if reused and reused > 0 then
self.state = STATE_CONNECTED
return 1
end
local packet, typ, err = _recv_packet(self)
if not packet then
return nil, err
end
if typ == "ERR" then
local errno, msg, sqlstate = _parse_err_packet(packet)
return nil, msg, errno, sqlstate
end
self.protocol_ver = strbyte(packet)
--print("protocol version: ", self.protocol_ver)
local server_ver, pos = _from_cstring(packet, 2)
if not server_ver then
return nil, "bad handshake initialization packet: bad server version"
end
--print("server version: ", server_ver)
self._server_ver = server_ver
local thread_id, pos = _get_byte4(packet, pos)
--print("thread id: ", thread_id)
local scramble = sub(packet, pos, pos + 8 - 1)
if not scramble then
return nil, "1st part of scramble not found"
end
pos = pos + 9 -- skip filler
-- two lower bytes
self._server_capabilities, pos = _get_byte2(packet, pos)
--print("server capabilities: ", self._server_capabilities)
self._server_lang = strbyte(packet, pos)
pos = pos + 1
--print("server lang: ", self._server_lang)
self._server_status, pos = _get_byte2(packet, pos)
--print("server status: ", self._server_status)
local more_capabilities
more_capabilities, pos = _get_byte2(packet, pos)
self._server_capabilities = bor(self._server_capabilities,
lshift(more_capabilities, 16))
--print("server capabilities: ", self._server_capabilities)
-- local len = strbyte(packet, pos)
local len = 21 - 8 - 1
--print("scramble len: ", len)
pos = pos + 1 + 10
local scramble_part2 = sub(packet, pos, pos + len - 1)
if not scramble_part2 then
return nil, "2nd part of scramble not found"
end
scramble = scramble .. scramble_part2
--print("scramble: ", _dump(scramble))
local password = opts.password or ""
local token = _compute_token(password, scramble)
-- local client_flags = self._server_capabilities
local client_flags = 260047;
--print("token: ", _dump(token))
local req = {
_set_byte4(client_flags),
_set_byte4(self._max_packet_size),
"\0", -- TODO: add support for charset encoding
strrep("\0", 23),
_to_cstring(user),
_to_binary_coded_string(token),
_to_cstring(database)
}
local packet_len = 4 + 4 + 1 + 23 + strlen(user) + 1
+ strlen(token) + 1 + strlen(database) + 1
-- print("packet content length: ", packet_len)
-- print("packet content: ", _dump(concat(req, "")))
local bytes, err = _send_packet(self, req, packet_len)
if not bytes then
return nil, "failed to send client authentication packet: " .. err
end
--print("packet sent ", bytes, " bytes")
local packet, typ, err = _recv_packet(self)
if not packet then
return nil, "failed to receive the result packet: " .. err
end
if typ == 'ERR' then
local errno, msg, sqlstate = _parse_err_packet(packet)
return nil, msg, errno, sqlstate
end
if typ == 'EOF' then
return nil, "old pre-4.1 authentication protocol not supported"
end
if typ ~= 'OK' then
return nil, "bad packet type: " .. typ
end
self.state = STATE_CONNECTED
return 1
end
function set_keepalive(self, ...)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
if self.state ~= STATE_CONNECTED then
return nil, "cannot be reused in the current connection state: "
.. (self.state or "nil")
end
self.state = nil
return sock:setkeepalive(...)
end
function get_reused_times(self)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
return sock:getreusedtimes()
end
function close(self)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
self.state = nil
return sock:close()
end
function server_ver(self)
return self._server_ver
end
function send_query(self, query)
if self.state ~= STATE_CONNECTED then
return nil, "cannot send query in the current context: "
.. (self.state or "nil")
end
local sock = self.sock
if not sock then
return nil, "not initialized"
end
self.packet_no = -1
local cmd_packet = {strchar(COM_QUERY), query}
local packet_len = 1 + strlen(query)
local bytes, err = _send_packet(self, cmd_packet, packet_len)
if not bytes then
return nil, err
end
self.state = STATE_COMMAND_SENT
--print("packet sent ", bytes, " bytes")
return bytes
end
function read_result(self)
if self.state ~= STATE_COMMAND_SENT then
return nil, "cannot read result in the current context: " .. self.state
end
local sock = self.sock
if not sock then
return nil, "not initialized"
end
local packet, typ, err = _recv_packet(self)
if not packet then
return nil, err
end
if typ == "ERR" then
self.state = STATE_CONNECTED
local errno, msg, sqlstate = _parse_err_packet(packet)
return nil, msg, errno, sqlstate
end
if typ == 'OK' then
local res = _parse_ok_packet(packet)
if res and band(res.server_status, SERVER_MORE_RESULTS_EXISTS) ~= 0 then
return res, "again"
end
self.state = STATE_CONNECTED
return res
end
if typ ~= 'DATA' then
self.state = STATE_CONNECTED
return nil, "packet type " .. typ .. " not supported"
end
-- typ == 'DATA'
--print("read the result set header packet")
local field_count, extra = _parse_result_set_header_packet(packet)
--print("field count: ", field_count)
local cols = {}
for i = 1, field_count do
local col, err, errno, sqlstate = _recv_field_packet(self)
if not col then
return nil, err, errno, sqlstate
end
insert(cols, col)
end
local packet, typ, err = _recv_packet(self)
if not packet then
return nil, err
end
if typ ~= 'EOF' then
return nil, "unexpected packet type " .. typ .. " while eof packet is "
.. "expected"
end
-- typ == 'EOF'
local compact = self.compact
local rows = {}
while true do
--print("reading a row")
packet, typ, err = _recv_packet(self)
if not packet then
return nil, err
end
if typ == 'EOF' then
local warning_count, status_flags = _parse_eof_packet(packet)
--print("status flags: ", status_flags)
if band(status_flags, SERVER_MORE_RESULTS_EXISTS) ~= 0 then
return rows, "again"
end
break
end
-- if typ ~= 'DATA' then
-- return nil, 'bad row packet type: ' .. typ
-- end
-- typ == 'DATA'
local row = _parse_row_data_packet(packet, cols, compact)
insert(rows, row)
end
self.state = STATE_CONNECTED
return rows
end
function query(self, query)
local bytes, err = self:send_query(query)
if not bytes then
return nil, "failed to send query: " .. err
end
return self:read_result()
end
function set_compact_arrays(self, value)
self.compact = value
end
-- to prevent use of casual module global variables
getmetatable(class).__newindex = function (table, key, val)
error('attempt to write to undeclared variable "' .. key .. '": '
.. debug.traceback())
end