Skip to content

Update documentation and conn:reset() #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ make
make install
```

#### Run tests

To run the tests, the following preparatory steps must be completed:
* Run mysql instance.
* Connect a client to the instance and execute the commands:
* `CREATE USER 'test_user' IDENTIFIED WITH mysql_native_password BY 'pass';`
* `CREATE DATABASE tarantool_mysql_test;`
* `GRANT ALL PRIVILEGES ON *.* TO 'test_user';`
* Define MYSQL environment variable using the following format:<br/>
`ip:port:user:user_pass:db_name:`<br/>
Example:<br/>
`export MYSQL=127.0.0.1:3306:test_user:pass:tarantool_mysql_test:`

The tests can now be run by `make check`.

#### tarantoolctl rocks

You can also use tarantoolctl rocks:
Expand Down Expand Up @@ -77,18 +92,20 @@ Connect to a database.
- `use_numeric_result` - provide result of the "conn:execute" as ordered list
(true/false); default value: false

Throws an error on failure.

*Returns*:

- `connection ~= nil` on success
- `error(reason)` on error

### `conn:execute(statement, ...)`

Execute a statement with arguments in the current transaction.

Throws an error on failure.

*Returns*:

- `error(reason)` on error
- `results, true` on success, where `results` is in the following form:

(when `use_numeric_result = false` or is not set on a pool/connection creation)
Expand Down Expand Up @@ -165,10 +182,23 @@ Execute a dummy statement to check that connection is alive.

Quote a query string.

Throws an error on failure.

*Returns*:

- `quoted_string` on success
- `error(reason)` on error

### `conn:reset(user, pass, db)`

Update the connection authentication settings.

*Options*:

- `user` - username
- `pass` - password
- `db` - database name

Throws an error on failure.

### `pool = mysql.pool_create(opts)`

Expand All @@ -185,10 +215,11 @@ Create a connection pool with count of size established connections.
- `use_numeric_result` - provide result of the "conn:execute" as ordered list
(true/false); default value: false

Throws an error on failure.

*Returns*

- `pool ~= nil` on success
- `error(reason)` on error

### `conn = pool:get(opts)`

Expand Down
9 changes: 7 additions & 2 deletions mysql/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,14 @@ lua_mysql_reset(lua_State *L)
const char *user = lua_tostring(L, 2);
const char *pass = lua_tostring(L, 3);
const char *db = lua_tostring(L, 4);
mysql_change_user(raw_conn, user, pass, db);

return 0;
if (mysql_change_user(raw_conn, user, pass, db) == 0) {
lua_pushboolean(L, 1);
} else {
lua_pushboolean(L, 0);
}

return 1;
}

LUA_API int
Expand Down
34 changes: 20 additions & 14 deletions mysql/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ local function conn_get(pool, timeout)
pool.user, pool.pass,
pool.db, pool.use_numeric_result)
if status < 0 then
return error(mysql_conn)
error(mysql_conn)
end
end

Expand Down Expand Up @@ -70,11 +70,11 @@ conn_mt = {
__index = {
execute = function(self, sql, ...)
if not self.usable then
return error('Connection is not usable')
error('Connection is not usable')
end
if not self.queue:get() then
self.queue:put(false)
return error('Connection is broken')
error('Connection is broken')
end
local status, datas
if select('#', ...) > 0 then
Expand All @@ -84,7 +84,7 @@ conn_mt = {
end
if status ~= 0 then
self.queue:put(status > 0)
return error(datas)
error(datas)
end
self.queue:put(true)
return datas, true
Expand All @@ -104,11 +104,11 @@ conn_mt = {
end,
close = function(self)
if not self.usable then
return error('Connection is not usable')
error('Connection is not usable')
end
if not self.queue:get() then
self.queue:put(false)
return error('Connection is broken')
error('Connection is broken')
end
self.usable = false
self.conn:close()
Expand All @@ -117,22 +117,28 @@ conn_mt = {
end,
reset = function(self, user, pass, db)
if not self.usable then
return error('Connection is not usable')
error('Connection is not usable')
end
if not self.queue:get() then
self.queue:put(false)
return error('Connection is broken')
error('Connection is broken')
end
-- If the update of the connection settings fails, we must set
-- the connection to a "broken" state and throw an error.
local status = self.conn:reset(user, pass, db)
if not status then
self.queue:put(false)
error('Сonnection settings update failed.')
end
self.conn:reset(user, pass, db)
self.queue:put(true)
end,
quote = function(self, value)
if not self.usable then
return error('Connection is not usable')
error('Connection is not usable')
end
if not self.queue:get() then
self.queue:put(false)
return error('Connection is broken')
error('Connection is broken')
end
local ret = self.conn:quote(value)
self.queue:put(true)
Expand All @@ -158,7 +164,7 @@ local function pool_create(opts)
mysql_conn:close()
end
if status < 0 then
return error(conn)
error(conn)
end
end
queue:put(conn)
Expand Down Expand Up @@ -197,7 +203,7 @@ local function pool_get(self, opts)
opts = opts or {}

if not self.usable then
return error('Pool is not usable')
error('Pool is not usable')
end
local conn = conn_get(self, opts.timeout)

Expand Down Expand Up @@ -234,7 +240,7 @@ local function connect(opts)
opts.user, opts.password,
opts.db, opts.use_numeric_result)
if status < 0 then
return error(mysql_conn)
error(mysql_conn)
end
return conn_create(mysql_conn)
end
Expand Down
35 changes: 34 additions & 1 deletion test/mysql.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,40 @@ local function test_connection_pool(test, pool)
-- }}}
end

local function test_connection_reset(test, pool)
test:plan(2)

assert(pool.queue:is_full(), 'test case precondition fails')

-- Case: valid credentials were used to "reset" the connection
test:test('reset connection successfully', function(test)
test:plan(1)

assert(pool.size >= 1, 'test case precondition fails')

local conn = pool:get()
conn:reset(pool.user, pool.pass, pool.db)
test:ok(conn:ping(), 'connection "reset" successfully')
pool:put(conn)
end)

-- Case: invalid credentials were used to "reset" the connection
test:test('reset connection failed', function(test)
test:plan(1)

assert(pool.size >= 1, 'test case precondition fails')

local conn = pool:get()
local check = pcall(conn.reset, conn, "guinea pig", pool.pass, pool.db)
test:ok(not check, 'connection "reset" fails')
pool:put(conn)
end)

assert(pool.queue:is_full(), 'test case postcondition fails')
end

local test = tap.test('mysql connector')
test:plan(6)
test:plan(7)

test:test('connection old api', test_old_api, conn)
local pool_conn = p:get()
Expand All @@ -442,6 +474,7 @@ test:test('garbage collection', test_gc, p)
test:test('concurrent connections', test_conn_concurrent, p)
test:test('int64', test_mysql_int64, p)
test:test('connection pool', test_connection_pool, p)
test:test('connection reset', test_connection_reset, p)
p:close()

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