Skip to content

Commit e85f5ca

Browse files
Update execute interface. Support for passing nil.
1 parent 8ff808f commit e85f5ca

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

mysql/driver.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,14 @@ lua_mysql_execute(struct lua_State *L)
205205
return lua_mysql_push_error(L, conn);
206206

207207
lua_pushnumber(L, 1);
208-
int ret_count = 1;
208+
int ret_count = 2;
209+
int result_no = 0;
209210

211+
lua_newtable(L);
210212
while (true) {
211213
MYSQL_RES *res = mysql_use_result(conn);
212214
if (res) {
215+
lua_pushnumber(L, ++result_no);
213216
int fail = 0;
214217
lua_pushcfunction(L, lua_mysql_fetch_result);
215218
lua_pushlightuserdata(L, conn);
@@ -228,7 +231,7 @@ lua_mysql_execute(struct lua_State *L)
228231
if (fail) {
229232
return lua_error(L);
230233
}
231-
++ret_count;
234+
lua_settable(L, -3);
232235
}
233236
int next_res, status = mysql_next_result_start(&next_res, conn);
234237
while (status) {
@@ -350,6 +353,9 @@ lua_mysql_execute_prepared(struct lua_State *L)
350353
}
351354
if (error)
352355
goto done;
356+
357+
lua_newtable(L);
358+
lua_pushnumber(L, 1);
353359
meta = mysql_stmt_result_metadata(stmt);
354360
if (!meta)
355361
goto done;
@@ -390,6 +396,7 @@ lua_mysql_execute_prepared(struct lua_State *L)
390396
lua_settable(L, -3);
391397
++row_idx;
392398
}
399+
lua_settable(L, - 3);
393400
ret_count = 2;
394401

395402
done:
@@ -509,7 +516,7 @@ lua_mysql_connect(struct lua_State *L)
509516
const char *db = lua_tostring(L, 5);
510517

511518
MYSQL *conn, *tmp_conn = mysql_init(NULL);
512-
if (!conn) {
519+
if (!tmp_conn) {
513520
lua_pushinteger(L, -1);
514521
int fail = safe_pushstring(L,
515522
"Can not allocate memory for connector");

mysql/init.lua

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ end
4141
local function conn_get(pool)
4242
local mysql_conn = pool.queue:get()
4343
local status
44+
local k = mysql_conn
4445
if mysql_conn == nil then
4546
status, mysql_conn = driver.connect(pool.host, pool.port or 0,
4647
pool.user, pool.pass, pool.db)
@@ -77,36 +78,33 @@ conn_mt = {
7778
return get_error(self.pool.raise, 'Connection is broken')
7879
end
7980
local status, datas
80-
if ... ~= nil then
81-
datas = {self.conn:execute_prepared(sql, ...)}
82-
status = datas[1]
81+
if select('#', ...) > 0 then
82+
status, datas = self.conn:execute_prepared(sql, ...)
8383
else
84-
datas = {self.conn:execute(sql)}
85-
status = datas[1]
84+
status, datas = self.conn:execute(sql)
8685
end
8786
if status ~= 1 then
8887
self.queue:put(status == 0)
89-
return get_error(self.pool.raise, datas[2])
88+
return get_error(self.pool.raise, datas)
9089
end
9190
self.queue:put(true)
92-
datas[1] = true
93-
return unpack(datas)
91+
return datas, true
9492
end,
9593
begin = function(self)
96-
return self:execute('BEGIN')
94+
return self:execute('BEGIN') ~= nil
9795
end,
9896
commit = function(self)
99-
return self:execute('COMMIT')
97+
return self:execute('COMMIT') ~= nil
10098
end,
10199
rollback = function(self)
102-
return self:execute('ROLLBACK')
100+
return self:execute('ROLLBACK') ~= nil
103101
end,
104102
ping = function(self)
105103
local pool = self.pool
106104
self.pool = {raise = false}
107105
local data, msg = self:execute('SELECT 1 AS code')
108106
self.pool = pool
109-
return data and msg[1].code == 1
107+
return msg and data[1][1].code == 1
110108
end,
111109
close = function(self)
112110
if not self.usable then

test/mysql.test.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function test_old_api(t, conn)
2222
t:plan(16)
2323
-- Add an extension to 'tap' module
2424
getmetatable(t).__index.q = function(test, stmt, result, ...)
25-
test:is_deeply({conn:execute(stmt, ...)}, {true, result},
25+
test:is_deeply({conn:execute(stmt, ...)}, {{result}, true},
2626
... ~= nil and stmt..' % '..json.encode({...}) or stmt)
2727
end
2828
t:ok(conn:ping(), "ping")
@@ -48,7 +48,7 @@ function test_old_api(t, conn)
4848
end
4949

5050
t:ok(conn:begin(), "begin")
51-
local status = conn:execute("INSERT INTO _tx_test VALUES(10)");
51+
local _, status = conn:execute("INSERT INTO _tx_test VALUES(10)");
5252
t:is(status, true, "insert")
5353
t:q('SELECT * FROM _tx_test', {{ a = 10 }})
5454
t:ok(conn:rollback(), "roolback")
@@ -110,9 +110,9 @@ function test_mysql_int64(t, p)
110110
conn = p:get()
111111
conn:execute('create table int64test (id bigint)')
112112
conn:execute('insert into int64test values(1234567890123456789)')
113-
local r, m = conn:execute('select id from int64test')
113+
local d, s = conn:execute('select id from int64test')
114114
conn:execute('drop table int64test')
115-
t:ok(m[1]['id'] == 1234567890123456789LL, 'int64 test')
115+
t:ok(d[1][1]['id'] == 1234567890123456789LL, 'int64 test')
116116
p:put(conn)
117117
end
118118

0 commit comments

Comments
 (0)