Skip to content

Commit ea0f462

Browse files
committed
feature: added new method set_compact_arrays to change the current "compact_arrays" option used by the current object for subsequent queries. thanks Lance Li for suggesting it.
1 parent 1d91ed3 commit ea0f462

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

README.markdown

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,14 @@ Returns the MySQL server version string, like `"5.1.64"`.
241241

242242
You should only call this method after successfully connecting to a MySQL server, otherwise `nil` will be returned.
243243

244+
set_compact_arrays
245+
------------------
246+
`syntax: db:set_compact_arrays(boolean)`
247+
248+
Sets whether to use the "compact-arrays" structure for the resultsets returned by subsequent queries. See the `compact_arrays` option for the `connect` method for more details.
249+
250+
This method was first introduced in the `v0.09` release.
251+
244252
Debugging
245253
=========
246254

lib/resty/mysql.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,11 @@ function query(self, query)
817817
end
818818

819819

820+
function set_compact_arrays(self, value)
821+
self.compact = value
822+
end
823+
824+
820825
-- to prevent use of casual module global variables
821826
getmetatable(resty.mysql).__newindex = function (table, key, val)
822827
error('attempt to write to undeclared variable "' .. key .. '": '

t/compact_arrays.t

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,94 @@ result: {}
252252
--- no_error_log
253253
[error]
254254

255+
256+
257+
=== TEST 4: select query with an non-empty result set - set_compact_arrays
258+
--- http_config eval: $::HttpConfig
259+
--- config
260+
location /t {
261+
content_by_lua '
262+
local cjson = require "cjson"
263+
264+
local mysql = require "resty.mysql"
265+
local db = mysql:new()
266+
267+
db:set_timeout(1000) -- 1 sec
268+
269+
local ok, err, errno, sqlstate = db:connect{
270+
host = "$TEST_NGINX_MYSQL_HOST",
271+
port = $TEST_NGINX_MYSQL_PORT,
272+
database = "ngx_test",
273+
user = "ngx_test",
274+
password = "ngx_test"}
275+
276+
if not ok then
277+
ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
278+
return
279+
end
280+
281+
ngx.say("connected to mysql.")
282+
283+
local res, err, errno, sqlstate = db:query("drop table if exists cats")
284+
if not res then
285+
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
286+
return
287+
end
288+
289+
ngx.say("table cats dropped.")
290+
291+
res, err, errno, sqlstate = db:query("create table cats (id serial primary key, name varchar(5))")
292+
if not res then
293+
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
294+
return
295+
end
296+
297+
ngx.say("table cats created.")
298+
299+
res, err, errno, sqlstate = db:query("insert into cats (name) value (\'Bob\'),(\'\'),(null)")
300+
if not res then
301+
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
302+
return
303+
end
304+
305+
ngx.say(res.affected_rows, " rows inserted into table cats (last id: ", res.insert_id, ")")
306+
307+
db:set_compact_arrays(true)
308+
309+
res, err, errno, sqlstate = db:query("select name, id from cats order by id asc")
310+
if not res then
311+
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
312+
return
313+
end
314+
315+
ngx.say("result: ", cjson.encode(res))
316+
317+
db:set_compact_arrays(false)
318+
319+
res, err, errno, sqlstate = db:query("select name, id from cats order by id desc")
320+
if not res then
321+
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
322+
return
323+
end
324+
325+
ngx.say("result: ", cjson.encode(res))
326+
327+
local ok, err = db:close()
328+
if not ok then
329+
ngx.say("failed to close: ", err)
330+
return
331+
end
332+
';
333+
}
334+
--- request
335+
GET /t
336+
--- response_body
337+
connected to mysql.
338+
table cats dropped.
339+
table cats created.
340+
3 rows inserted into table cats (last id: 1)
341+
result: [["Bob",1],["",2],[null,3]]
342+
result: [{"name":null,"id":3},{"name":"","id":2},{"name":"Bob","id":1}]
343+
--- no_error_log
344+
[error]
345+

0 commit comments

Comments
 (0)