Skip to content

Commit 0dca137

Browse files
committed
bugfix: 64-bit integer values in the MySQL packets (like last insert ids) could not be properly parsed due to the lack of support for 64-bit integers in LuaJIT's BitOp library. thanks Azure Wang for the patch in github pull #6.
1 parent 6c36d6b commit 0dca137

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

lib/resty/mysql.lua

+8-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,14 @@ end
7676

7777
local function _get_byte8(data, i)
7878
local a, b, c, d, e, f, g, h = strbyte(data, i, i + 7)
79-
return bor(a, lshift(b, 8), lshift(c, 16), lshift(d, 24), lshift(e, 32),
80-
lshift(f, 40), lshift(g, 48), lshift(h, 56)), i + 8
79+
80+
-- XXX workaround for the lack of 64-bit support in bitop:
81+
local lo = bor(a, lshift(b, 8), lshift(c, 16), lshift(d, 24))
82+
local hi = bor(e, lshift(f, 8), lshift(g, 16), lshift(h, 24))
83+
return lo + hi * 4294967296, i + 8
84+
85+
-- return bor(a, lshift(b, 8), lshift(c, 16), lshift(d, 24), lshift(e, 32),
86+
-- lshift(f, 40), lshift(g, 48), lshift(h, 56)), i + 8
8187
end
8288

8389

t/sanity.t

+62
Original file line numberDiff line numberDiff line change
@@ -1186,3 +1186,65 @@ result: \[\{"sum\(id\)":6\}\], err:nil$
11861186
qr/lua tcp socket keepalive create connection pool for key "my_pool"/
11871187
--- log_level: debug
11881188

1189+
1190+
1191+
=== TEST 18: large insert_id exceeding a 32-bit integer value
1192+
--- http_config eval: $::HttpConfig
1193+
--- config
1194+
location /t {
1195+
content_by_lua '
1196+
local mysql = require("resty.mysql")
1197+
local create_sql = [[
1198+
CREATE TABLE `large_t` (
1199+
`id` bigint(11) NOT NULL AUTO_INCREMENT,
1200+
PRIMARY KEY (`id`)
1201+
) AUTO_INCREMENT=5000000312;
1202+
]]
1203+
local drop_sql = [[
1204+
DROP TABLE IF EXISTS `large_t`;
1205+
]]
1206+
local insert_sql = [[
1207+
INSERT INTO `large_t` VALUES(NULL);
1208+
]]
1209+
local db, err = mysql:new()
1210+
if not db then
1211+
ngx.say("failed to instantiate mysql: ", err)
1212+
return
1213+
end
1214+
db:set_timeout(1000)
1215+
local ok, err = db:connect{
1216+
host = "$TEST_NGINX_MYSQL_HOST",
1217+
port = $TEST_NGINX_MYSQL_PORT,
1218+
database="test",
1219+
user="root",
1220+
password=""}
1221+
if not ok then
1222+
ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
1223+
return
1224+
end
1225+
local res, err = db:query(drop_sql)
1226+
if not res then
1227+
ngx.say("drop table error:" .. err)
1228+
return
1229+
end
1230+
local res, err = db:query(create_sql)
1231+
if not res then
1232+
ngx.say("create table error:" .. err)
1233+
return
1234+
end
1235+
local res, err = db:query(insert_sql)
1236+
if not res then
1237+
ngx.say("insert table error:" .. err)
1238+
return
1239+
else
1240+
ngx.say(res.insert_id)
1241+
end
1242+
';
1243+
}
1244+
--- request
1245+
GET /t
1246+
--- response_body
1247+
5000000312
1248+
--- no_error_log
1249+
[error]
1250+

0 commit comments

Comments
 (0)