Skip to content
Snippets Groups Projects
Commit 7bca28ca authored by Konstantin Osipov's avatar Konstantin Osipov
Browse files

Lua: re-implement and test box.insert().

Implement INSERT command in Box. Minor cleanups in Lua.
parent 0fa41534
No related branches found
No related tags found
No related merge requests found
...@@ -276,6 +276,7 @@ tarantool_lua_init() ...@@ -276,6 +276,7 @@ tarantool_lua_init()
return L; return L;
luaL_openlibs(L); luaL_openlibs(L);
luaL_register(L, "box", boxlib); luaL_register(L, "box", boxlib);
lua_pop(L, 1);
lua_register(L, "print", lbox_print); lua_register(L, "print", lbox_print);
tarantool_lua_load_cfg(L, &cfg); tarantool_lua_load_cfg(L, &cfg);
L = mod_lua_init(L); L = mod_lua_init(L);
......
...@@ -36,7 +36,16 @@ function box.replace(namespace, ...) ...@@ -36,7 +36,16 @@ function box.replace(namespace, ...)
unpack(tuple)))) unpack(tuple))))
end 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, ...) function box.update(namespace, key, format, ...)
ops = {...} ops = {...}
......
...@@ -372,8 +372,10 @@ mod_lua_init(struct lua_State *L) ...@@ -372,8 +372,10 @@ mod_lua_init(struct lua_State *L)
lua_pushstring(L, tuplelib_name); lua_pushstring(L, tuplelib_name);
lua_setfield(L, -2, "__metatable"); lua_setfield(L, -2, "__metatable");
luaL_register(L, NULL, lbox_tuple_meta); luaL_register(L, NULL, lbox_tuple_meta);
lua_pop(L, 2);
/* Load box.lua */ /* Load box.lua */
(void) luaL_dostring(L, &_binary_box_lua_start); (void) luaL_dostring(L, &_binary_box_lua_start);
assert(lua_gettop(L) == 0);
return L; return L;
} }
......
No preview for this file type
...@@ -72,6 +72,8 @@ exec sql "call box.select(0, 0, 'abcd')" ...@@ -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, 'abcd')"
exec sql "call box.delete(0, 'defc')" exec sql "call box.delete(0, 'defc')"
exec sql "call box.insert(0, 'test', 'old', 'abcd')" 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.update(0, 'test', '=p=p', 0, 'pass', 1, 'new')"
exec sql "call box.select(0, 0, 'pass')" exec sql "call box.select(0, 0, 'pass')"
exec sql "call box.update(0, 'miss', '+p', 2, '\1\0\0\0')" exec sql "call box.update(0, 'miss', '+p', 2, '\1\0\0\0')"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment