diff --git a/core/tarantool_lua.m b/core/tarantool_lua.m index 87e6f34020494f60ede77acfc68afbd7d9069005..b6e8b9c58a2cc54089dd9c532f70aebcd2a7b1ff 100644 --- a/core/tarantool_lua.m +++ b/core/tarantool_lua.m @@ -276,6 +276,7 @@ tarantool_lua_init() return L; luaL_openlibs(L); luaL_register(L, "box", boxlib); + lua_pop(L, 1); lua_register(L, "print", lbox_print); tarantool_lua_load_cfg(L, &cfg); L = mod_lua_init(L); diff --git a/mod/box/box.lua b/mod/box/box.lua index 07fce68538534a688ea903b483cbea9f52694fad..68c1e4be4982bcdeb21302a4259a91c7f89a407f 100644 --- a/mod/box/box.lua +++ b/mod/box/box.lua @@ -36,7 +36,16 @@ function box.replace(namespace, ...) unpack(tuple)))) end -box.insert = box.replace +-- insert a tuple (produces an error if a tuple already exists +function box.insert(namespace, ...) + tuple = {...} + return select(2, + box.process(13, box.pack('iii'..string.rep('p', #tuple), + namespace, + 3, -- flags, BOX_RETURN_TUPLE | BOX_ADD + #tuple, -- cardinality + unpack(tuple)))) +end function box.update(namespace, key, format, ...) ops = {...} diff --git a/mod/box/box_lua.m b/mod/box/box_lua.m index 8692c55ca5f4609810db07f53ce404d50ad9d63d..8d609898164e8f8994a93148603b3ddc6235b5de 100644 --- a/mod/box/box_lua.m +++ b/mod/box/box_lua.m @@ -372,8 +372,10 @@ mod_lua_init(struct lua_State *L) lua_pushstring(L, tuplelib_name); lua_setfield(L, -2, "__metatable"); luaL_register(L, NULL, lbox_tuple_meta); + lua_pop(L, 2); /* Load box.lua */ (void) luaL_dostring(L, &_binary_box_lua_start); + assert(lua_gettop(L) == 0); return L; } diff --git a/test/box/lua.result b/test/box/lua.result index a31fbe80e104b378cfd8f632a0345b18019cfc45..58c93387846484f9e6042aa9364b152ee56b7b53 100644 --- a/test/box/lua.result +++ b/test/box/lua.result @@ -216,6 +216,8 @@ Found 1 tuple: call box.insert(0, 'test', 'old', 'abcd') Found 1 tuple: [1953719668, 'old', 1684234849] +call box.insert(0, 'test', 'old', 'abcd') +An error occurred: ER_TUPLE_FOUND, 'Tuple already exists�' call box.update(0, 'test', '=p=p', 0, 'pass', 1, 'new') Found 1 tuple: [1936941424, 'new', 1684234849] diff --git a/test/box/lua.test b/test/box/lua.test index 46de2f87bfa7d2276c9017bc9a6428c8aa462dff..668c216c01264da520709ee78f4563380d1e425a 100644 --- a/test/box/lua.test +++ b/test/box/lua.test @@ -72,6 +72,8 @@ exec sql "call box.select(0, 0, 'abcd')" exec sql "call box.delete(0, 'abcd')" exec sql "call box.delete(0, 'defc')" exec sql "call box.insert(0, 'test', 'old', 'abcd')" +# test that insert produces a duplicate key error +exec sql "call box.insert(0, 'test', 'old', 'abcd')" exec sql "call box.update(0, 'test', '=p=p', 0, 'pass', 1, 'new')" exec sql "call box.select(0, 0, 'pass')" exec sql "call box.update(0, 'miss', '+p', 2, '\1\0\0\0')"