[tarantool-patches] Re: [PATCH v1 09/10] lua: create vstream implementation for Lua

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Mon Nov 19 20:58:04 MSK 2018


Thanks for the patch! See 1 comment below.

> @@ -111,6 +113,39 @@ sqlerror:
>   }
>   
>   static int
> +lbox_execute(struct lua_State *L)
> +{
> +	struct sqlite3 *db = sql_get();
> +	if (db == NULL)
> +		return luaL_error(L, "not ready");
> +
> +	size_t length;
> +	const char *sql = lua_tolstring(L, 1, &length);
> +	if (sql == NULL)
> +		return luaL_error(L, "usage: box.execute(sqlstring)");
> +
> +	struct sql_request request = {};
> +	struct sql_response response = {};

Please, do not forget about binding parameters. You should write an
analogue of sql_bind_list_decode() but in Lua. I do not know how to
virtualize decoding, so lets just write a Lua version of
sql_bind_list_decode(), it does not look too hard.

> +	request.sql_text = sql;
> +	request.sql_text_len = length;
> +	if (sql_prepare_and_execute(&request, &response, &fiber()->gc) != 0)
> +		goto sqlerror;
> +
> +	int keys;
> +	struct vstream vstream;
> +	lua_vstream_init(&vstream, L);
> +	lua_newtable(L);
> +	if (sql_response_dump(&response, &keys, &vstream) != 0) {
> +		lua_pop(L, 1);
> +		goto sqlerror;
> +	}
> +	return 1;
> +sqlerror:
> +	lua_pushstring(L, sqlite3_errmsg(db));
> +	return lua_error(L);
> +}
> +
> +static int
>   lua_sql_debug(struct lua_State *L)
>   {
>   	struct info_handler info;




More information about the Tarantool-patches mailing list