Skip to content

Commit 7dc6dc4

Browse files
committed
Merge branch 'master' of https://github.com/darkwrat/tarantool-mysql into pushnull
2 parents bce1862 + 5524aef commit 7dc6dc4

File tree

4 files changed

+45
-69
lines changed

4 files changed

+45
-69
lines changed

CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ if(NOT CMAKE_BUILD_TYPE)
88
endif()
99

1010
# Fetch and patch mariadb-connector-c submodule
11-
execute_process(COMMAND git submodule update --init --recursive)
11+
execute_process(COMMAND git submodule update --init --recursive
12+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
1213
ExternalProject_Add(mariadb-connector-c
1314
SOURCE_DIR "${CMAKE_SOURCE_DIR}/mariadb-connector-c"
14-
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
15-
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/lib)
15+
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_C_FLAGS=-fPIC
16+
-DCMAKE_CXX_FLAGS=-fPIC -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/lib)
1617

1718
include_directories("${CMAKE_BINARY_DIR}/lib/include/mariadb")
1819
link_directories(${MYSQL_LIBRARY_DIRS})

mysql/driver.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ lua_mysql_push_error(struct lua_State *L, MYSQL *conn)
107107
lua_pushnumber(L, -1);
108108
break;
109109
default:
110-
lua_pushnumber(L, 0);
110+
lua_pushnumber(L, 1);
111111
}
112112
safe_pushstring(L, (char *)mysql_error(conn));
113113
return 2;
@@ -219,7 +219,7 @@ lua_mysql_execute(struct lua_State *L)
219219
if (err)
220220
return lua_mysql_push_error(L, conn);
221221

222-
lua_pushnumber(L, 1);
222+
lua_pushnumber(L, 0);
223223
int ret_count = 2;
224224
int result_no = 0;
225225

@@ -309,8 +309,8 @@ lua_mysql_execute_prepared(struct lua_State *L)
309309
* string from lua */
310310
uint64_t *values = NULL;
311311

312-
/* We hope that all should be fine and push 1 (OK) */
313-
lua_pushnumber(L, 1);
312+
/* We hope that all should be fine and push 0 (OK) */
313+
lua_pushnumber(L, 0);
314314
ret_count = 1;
315315
stmt = mysql_stmt_init(conn);
316316
if ((error = !stmt))
@@ -389,7 +389,6 @@ lua_mysql_execute_prepared(struct lua_State *L)
389389
result_binds[col_no].is_null = (my_bool *)malloc(sizeof(my_bool));
390390
}
391391
mysql_stmt_bind_result(stmt, result_binds);
392-
393392
lua_newtable(L);
394393
unsigned int row_idx = 1;
395394
while (true) {
@@ -413,7 +412,7 @@ lua_mysql_execute_prepared(struct lua_State *L)
413412
lua_settable(L, -3);
414413
++row_idx;
415414
}
416-
lua_settable(L, - 3);
415+
lua_settable(L, -3);
417416
ret_count = 2;
418417

419418
done:
@@ -571,7 +570,7 @@ lua_mysql_connect(struct lua_State *L)
571570
return fail ? lua_error(L) : 2;
572571
}
573572

574-
lua_pushinteger(L, 1);
573+
lua_pushnumber(L, 0);
575574
MYSQL **conn_p = (MYSQL **)lua_newuserdata(L, sizeof(MYSQL *));
576575
*conn_p = conn;
577576
luaL_getmetatable(L, mysql_driver_label);

mysql/init.lua

Lines changed: 32 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,15 @@ local ffi = require('ffi')
77
local pool_mt
88
local conn_mt
99

10-
-- pool error helper
11-
local function get_error(raise, msg)
12-
if raise then
13-
error(msg)
14-
end
15-
return nil, msg
16-
end
17-
1810
--create a new connection
19-
local function conn_create(mysql_conn, pool)
11+
local function conn_create(mysql_conn)
2012
local queue = fiber.channel(1)
2113
queue:put(true)
2214
local conn = setmetatable({
2315
usable = true,
24-
pool = pool,
2516
conn = mysql_conn,
2617
queue = queue,
27-
-- we can use ffi gc to return mysql connection to pool
28-
__gc_hook = ffi.gc(ffi.new('void *'),
29-
function(self)
30-
mysql_conn:close()
31-
if not pool.virtual then
32-
pool.queue:put(nil)
33-
end
34-
end)
35-
}, conn_mt)
36-
18+
}, conn_mt)
3719
return conn
3820
end
3921

@@ -45,14 +27,18 @@ local function conn_get(pool)
4527
if mysql_conn == nil then
4628
status, mysql_conn = driver.connect(pool.host, pool.port or 0,
4729
pool.user, pool.pass, pool.db)
48-
if status == -1 then
49-
return get_error(pool.raise, mysql_conn)
50-
end
51-
if status == -2 then
52-
return status
30+
if status < 0 then
31+
return error(mysql_conn)
5332
end
5433
end
55-
return conn_create(mysql_conn, pool)
34+
local conn = conn_create(mysql_conn)
35+
-- we can use ffi gc to return mysql connection to pool
36+
conn.__gc_hook = ffi.gc(ffi.new('void *'),
37+
function(self)
38+
mysql_conn:close()
39+
pool.queue:put(nil)
40+
end)
41+
return conn
5642
end
5743

5844
local function conn_put(conn)
@@ -70,21 +56,21 @@ conn_mt = {
7056
__index = {
7157
execute = function(self, sql, ...)
7258
if not self.usable then
73-
return get_error(self.pool.raise, 'Connection is not usable')
59+
return error('Connection is not usable')
7460
end
7561
if not self.queue:get() then
7662
self.queue:put(false)
77-
return get_error(self.pool.raise, 'Connection is broken')
63+
return error('Connection is broken')
7864
end
7965
local status, datas
8066
if select('#', ...) > 0 then
8167
status, datas = self.conn:execute_prepared(sql, ...)
8268
else
8369
status, datas = self.conn:execute(sql)
8470
end
85-
if status ~= 1 then
86-
self.queue:put(status == 0)
87-
return get_error(self.pool.raise, datas)
71+
if status ~= 0 then
72+
self.queue:put(status > 0)
73+
return error(datas)
8874
end
8975
self.queue:put(true)
9076
return datas, true
@@ -99,19 +85,16 @@ conn_mt = {
9985
return self:execute('ROLLBACK') ~= nil
10086
end,
10187
ping = function(self)
102-
local pool = self.pool
103-
self.pool = {raise = false}
104-
local data, msg = self:execute('SELECT 1 AS code')
105-
self.pool = pool
88+
local status, data, msg = pcall(self.execute, self, 'SELECT 1 AS code')
10689
return msg and data[1][1].code == 1
10790
end,
10891
close = function(self)
10992
if not self.usable then
110-
return get_error(self.pool.raise, 'Connection is not usable')
93+
return error('Connection is not usable')
11194
end
11295
if not self.queue:get() then
11396
self.queue:put(false)
114-
return get_error(self.pool.raise, 'Connection is broken')
97+
return error('Connection is broken')
11598
end
11699
self.usable = false
117100
self.conn:close()
@@ -120,22 +103,22 @@ conn_mt = {
120103
end,
121104
reset = function(self, user, pass, db)
122105
if not self.usable then
123-
return get_error(self.pool.raise, 'Connection is not usable')
106+
return error('Connection is not usable')
124107
end
125108
if not self.queue:get() then
126109
self.queue:put(false)
127-
return get_error(self.pool.raise, 'Connection is broken')
110+
return error('Connection is broken')
128111
end
129112
self.conn:reset(user, pass, db)
130113
self.queue:put(true)
131114
end,
132115
quote = function(self, value)
133116
if not self.usable then
134-
return get_error(self.pool.raise, 'Connection is not usable')
117+
return error('Connection is not usable')
135118
end
136119
if not self.queue:get() then
137120
self.queue:put(false)
138-
return get_error(self.pool.raise, 'Connection is broken')
121+
return error('Connection is broken')
139122
end
140123
local ret = self.conn:quote(value)
141124
self.queue:put(true)
@@ -145,7 +128,7 @@ conn_mt = {
145128
}
146129

147130
-- Create connection pool. Accepts mysql connection params (host, port, user,
148-
-- password, dbname), size and raise flag.
131+
-- password, dbname), size.
149132
local function pool_create(opts)
150133
opts = opts or {}
151134
opts.size = opts.size or 1
@@ -158,10 +141,8 @@ local function pool_create(opts)
158141
local mysql_conn = queue:get()
159142
mysql_conn:close()
160143
end
161-
if status == -1 then
162-
return get_error(opts.raise, conn)
163-
else
164-
return -2
144+
if status < 0 then
145+
return error(conn)
165146
end
166147
end
167148
queue:put(conn)
@@ -178,7 +159,6 @@ local function pool_create(opts)
178159

179160
-- private variables
180161
queue = queue,
181-
raise = opts.raise,
182162
usable = true
183163
}, pool_mt)
184164
end
@@ -198,7 +178,7 @@ end
198178
-- Returns connection
199179
local function pool_get(self)
200180
if not self.usable then
201-
return get_error(self.raise, 'Pool is not usable')
181+
return error('Pool is not usable')
202182
end
203183
local conn = conn_get(self)
204184
conn:reset(self.user, self.pass, self.db)
@@ -223,19 +203,15 @@ pool_mt = {
223203
}
224204

225205
-- Create connection. Accepts mysql connection params (host, port, user,
226-
-- password, dbname) separatelly or in one string and raise flag.
206+
-- password, dbname)
227207
local function connect(opts)
228208
opts = opts or {}
229-
local pool = {virtual = true, raise = opts.raise}
230209

231210
local status, mysql_conn = driver.connect(opts.host, opts.port or 0, opts.user, opts.password, opts.db)
232-
if status == -1 then
233-
return get_error(pool.raise, mysql_conn)
234-
end
235-
if status == -2 then
236-
return -2
211+
if status < 0 then
212+
return error(mysql_conn)
237213
end
238-
return conn_create(mysql_conn, pool)
214+
return conn_create(mysql_conn)
239215
end
240216

241217
return {

test/mysql.test.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ local host, port, user, password, db = string.match(os.getenv('MYSQL') or '',
1212
"([^:]*):([^:]*):([^:]*):([^:]*):([^:]*)")
1313

1414
local conn, err = mysql.connect({ host = host, port = port, user = user,
15-
password = password, db = db, raise = false })
15+
password = password, db = db })
1616
if conn == nil then error(err) end
1717

1818
local p, err = mysql.pool_create({ host = host, port = port, user = user,
19-
password = password, db = db, raise = false, size = 2 })
19+
password = password, db = db, size = 2 })
2020

2121
function test_old_api(t, conn)
2222
t:plan(16)
@@ -63,7 +63,7 @@ function test_old_api(t, conn)
6363
end)
6464

6565
t:q('DROP TABLE IF EXISTS unknown_table', nil)
66-
local tuples, reason = conn:execute('DROP TABLE unknown_table')
66+
local status, reason = pcall(conn.execute, conn, 'DROP TABLE unknown_table')
6767
t:like(reason, 'unknown_table', 'error')
6868
t:ok(conn:close(), "close")
6969
end

0 commit comments

Comments
 (0)