Skip to content

Commit ec0a8ff

Browse files
committed
add a check for the result of updating the connection authentication settings
We don't check the result of updating the connection authentication settings in the conn: reset() method and failure will be detected only when trying to execute a request. But in programming, it is a good practice to check the result of the function. The patch adds a check on the result of updating the connection authentication settings. On failure, an exception will be thrown.
1 parent f1a6b5d commit ec0a8ff

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

mysql/driver.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,14 @@ lua_mysql_reset(lua_State *L)
663663
const char *user = lua_tostring(L, 2);
664664
const char *pass = lua_tostring(L, 3);
665665
const char *db = lua_tostring(L, 4);
666-
mysql_change_user(raw_conn, user, pass, db);
667666

668-
return 0;
667+
if (mysql_change_user(raw_conn, user, pass, db) == 0) {
668+
lua_pushboolean(L, 1);
669+
} else {
670+
lua_pushboolean(L, 0);
671+
}
672+
673+
return 1;
669674
}
670675

671676
LUA_API int

mysql/init.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,13 @@ conn_mt = {
123123
self.queue:put(false)
124124
error('Connection is broken')
125125
end
126-
self.conn:reset(user, pass, db)
126+
-- If the update of the connection settings fails, we must set
127+
-- the connection to a "broken" state and throw an error.
128+
local status = self.conn:reset(user, pass, db)
129+
if not status then
130+
self.queue:put(false)
131+
error('Сonnection settings update failed.')
132+
end
127133
self.queue:put(true)
128134
end,
129135
quote = function(self, value)

test/mysql.test.lua

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,40 @@ local function test_connection_pool(test, pool)
431431
-- }}}
432432
end
433433

434+
local function test_connection_reset(test, pool)
435+
test:plan(2)
436+
437+
assert(pool.queue:is_full(), 'test case precondition fails')
438+
439+
-- Case: valid credentials were used to "reset" the connection
440+
test:test('reset connection successfully', function(test)
441+
test:plan(1)
442+
443+
assert(pool.size >= 1, 'test case precondition fails')
444+
445+
local conn = pool:get()
446+
conn:reset(pool.user, pool.pass, pool.db)
447+
test:ok(conn:ping(), 'connection "reset" successfully')
448+
pool:put(conn)
449+
end)
450+
451+
-- Case: invalid credentials were used to "reset" the connection
452+
test:test('reset connection failed', function(test)
453+
test:plan(1)
454+
455+
assert(pool.size >= 1, 'test case precondition fails')
456+
457+
local conn = pool:get()
458+
local check = pcall(conn.reset, conn, "guinea pig", pool.pass, pool.db)
459+
test:ok(not check, 'connection "reset" fails')
460+
pool:put(conn)
461+
end)
462+
463+
assert(pool.queue:is_full(), 'test case postcondition fails')
464+
end
465+
434466
local test = tap.test('mysql connector')
435-
test:plan(6)
467+
test:plan(7)
436468

437469
test:test('connection old api', test_old_api, conn)
438470
local pool_conn = p:get()
@@ -442,6 +474,7 @@ test:test('garbage collection', test_gc, p)
442474
test:test('concurrent connections', test_conn_concurrent, p)
443475
test:test('int64', test_mysql_int64, p)
444476
test:test('connection pool', test_connection_pool, p)
477+
test:test('connection reset', test_connection_reset, p)
445478
p:close()
446479

447480
os.exit(test:check() and 0 or 1)

0 commit comments

Comments
 (0)