diff --git a/tests/examples/lua/OpenResty/rest/test.lua b/tests/examples/lua/OpenResty/rest/test.lua index c1bde3a6022c38f53dc296e0deda29a8475f599e..179950cbe7cc294cd53a538baecefda28fe30bcc 100644 --- a/tests/examples/lua/OpenResty/rest/test.lua +++ b/tests/examples/lua/OpenResty/rest/test.lua @@ -1,6 +1,6 @@ local driver = require "luaconnector51" local cjson = require "cjson" -ngx.say("start:"..os.time()) +ngx.say("start time:"..os.time()) local config = { @@ -24,10 +24,9 @@ end local res = driver.query(conn,"drop database if exists nginx") if res.code ~=0 then - ngx.say("create db--- failed: "..res.error) - + ngx.say("drop db--- failed: "..res.error) else - ngx.say("create db--- pass.") + ngx.say("drop db--- pass.") end res = driver.query(conn,"create database nginx") if res.code ~=0 then @@ -39,8 +38,7 @@ end res = driver.query(conn,"use nginx") if res.code ~=0 then - ngx.say("select db--- failed: "..res.error) - + ngx.say("select db--- failed: "..res.error) else ngx.say("select db--- pass.") end @@ -79,7 +77,7 @@ else end end - -ngx.say("end:"..os.time()) +driver.close(conn) +ngx.say("end time:"..os.time()) --ngx.log(ngx.ERR,"in test file.") diff --git a/tests/examples/lua/OpenResty/so/luaconnector51.so b/tests/examples/lua/OpenResty/so/luaconnector51.so index 6d26bb8779f438acd2d0d05245a788b0295239d6..442de6e39f909e1aeb869988722b84795c048855 100755 Binary files a/tests/examples/lua/OpenResty/so/luaconnector51.so and b/tests/examples/lua/OpenResty/so/luaconnector51.so differ diff --git a/tests/examples/lua/README.md b/tests/examples/lua/README.md index e578c27119fc13c1d9d986575a8c9c41fcf8b276..dd9c9d07874e455329e43c7f77e806eb3634622c 100644 --- a/tests/examples/lua/README.md +++ b/tests/examples/lua/README.md @@ -25,6 +25,8 @@ lua test.lua http://openresty.org ``` ## Run with OpenResty Sample +**This section demonstrates how to get binary file for connector. To be convenient for trial, an connector has been put into OpenResty work directory. +Because of difference on C API between Lua5.3 and Lua5.1, the files needed by connector for OpenResty are stored in local source directory and configured in script build.sh.** Build driver lib: ``` @@ -33,7 +35,9 @@ cd lua51 ``` Run OpenResty sample: ``` -openresty -p . +cd .. +cd OpenResty +sudo openresty -p . curl http://127.0.0.1:7000/api/test ``` diff --git a/tests/examples/lua/lua51/lua_connector51.c b/tests/examples/lua/lua51/lua_connector51.c index 6b52c4c5291110c46249a282715b0cad94d9239f..9b932337febb204eada021ececa02bc59cf6d5db 100644 --- a/tests/examples/lua/lua51/lua_connector51.c +++ b/tests/examples/lua/lua51/lua_connector51.c @@ -15,10 +15,10 @@ struct cb_param{ static int l_connect(lua_State *L){ TAOS * taos=NULL; - char* host; - char* database; - char* user; - char* password; + const char* host; + const char* database; + const char* user; + const char* password; int port; luaL_checktype(L, 1, LUA_TTABLE); @@ -84,15 +84,15 @@ static int l_connect(lua_State *L){ } static int l_query(lua_State *L){ - TAOS * taos= lua_topointer(L,1); - char *s = lua_tostring(L, 2); + TAOS *taos= (TAOS*)lua_topointer(L,1); + const char* s = lua_tostring(L, 2); TAOS_RES *result; lua_newtable(L); int table_index = lua_gettop(L); // printf("receive command:%s\r\n",s); - result = taos_query(taos,s); - int32_t code = taos_errno(result); + result = taos_query(taos, s); + int32_t code = taos_errno(result); if( code != 0){ printf("failed, reason:%s\n", taos_errstr(result)); lua_pushinteger(L, -1); @@ -107,10 +107,10 @@ static int l_query(lua_State *L){ TAOS_ROW row; int rows = 0; int num_fields = taos_field_count(result); - TAOS_FIELD *fields = taos_fetch_fields(result); - char temp[256]; + const TAOS_FIELD *fields = taos_fetch_fields(result); + //char temp[256]; - int affectRows = taos_affected_rows(result); + const int affectRows = taos_affected_rows(result); // printf(" affect rows:%d\r\n", affectRows); lua_pushinteger(L, 0); lua_setfield(L, table_index, "code"); @@ -237,13 +237,13 @@ void stream_cb(void *param, TAOS_RES *result, TAOS_ROW row){ lua_call(L, 1, 0); - printf("-----------------------------------------------------------------------------------\n\r"); + // printf("-----------------------------------------------------------------------------------\n\r"); } static int l_open_stream(lua_State *L){ int r = luaL_ref(L, LUA_REGISTRYINDEX); - TAOS * taos = lua_topointer(L,1); - char * sqlstr = lua_tostring(L,2); + TAOS * taos = (TAOS*)lua_topointer(L,1); + const char * sqlstr = lua_tostring(L,2); int stime = luaL_checknumber(L,3); lua_newtable(L); @@ -286,7 +286,7 @@ static int l_close_stream(lua_State *L){ } static int l_close(lua_State *L){ - TAOS * taos= lua_topointer(L,1); + TAOS *taos= (TAOS*)lua_topointer(L,1); lua_newtable(L); int table_index = lua_gettop(L); @@ -296,7 +296,7 @@ static int l_close(lua_State *L){ lua_pushstring(L, "null pointer."); lua_setfield(L, table_index, "error"); }else{ - taos_close(taos); + taos_close(taos); lua_pushnumber(L, 0); lua_setfield(L, table_index, "code"); lua_pushstring(L, "done."); diff --git a/tests/examples/lua/lua_connector.c b/tests/examples/lua/lua_connector.c index 39fb86ce478450d4a954fdda09e1b1e52b711499..920d2cdc35c51c833a4d89448ec7e643f555dbc2 100644 --- a/tests/examples/lua/lua_connector.c +++ b/tests/examples/lua/lua_connector.c @@ -15,10 +15,10 @@ struct cb_param{ static int l_connect(lua_State *L){ TAOS * taos=NULL; - char* host; - char* database; - char* user; - char* password; + const char* host; + const char* database; + const char* user; + const char* password; int port; luaL_checktype(L, 1, LUA_TTABLE); @@ -83,15 +83,15 @@ static int l_connect(lua_State *L){ } static int l_query(lua_State *L){ - TAOS * taos= lua_topointer(L,1); - char *s = lua_tostring(L, 2); + TAOS *taos= (TAOS*)lua_topointer(L,1); + const char* s = lua_tostring(L, 2); TAOS_RES *result; lua_newtable(L); int table_index = lua_gettop(L); // printf("receive command:%s\r\n",s); - result = taos_query(taos,s); - int32_t code = taos_errno(result); + result = taos_query(taos, s); + int32_t code = taos_errno(result); if( code != 0){ printf("failed, reason:%s\n", taos_errstr(result)); lua_pushinteger(L, -1); @@ -106,10 +106,10 @@ static int l_query(lua_State *L){ TAOS_ROW row; int rows = 0; int num_fields = taos_field_count(result); - TAOS_FIELD *fields = taos_fetch_fields(result); - char temp[256]; + const TAOS_FIELD *fields = taos_fetch_fields(result); + //char temp[256]; - int affectRows = taos_affected_rows(result); + const int affectRows = taos_affected_rows(result); // printf(" affect rows:%d\r\n", affectRows); lua_pushinteger(L, 0); lua_setfield(L, table_index, "code"); @@ -241,8 +241,8 @@ void stream_cb(void *param, TAOS_RES *result, TAOS_ROW row){ static int l_open_stream(lua_State *L){ int r = luaL_ref(L, LUA_REGISTRYINDEX); - TAOS * taos = lua_topointer(L,1); - char * sqlstr = lua_tostring(L,2); + TAOS * taos = (TAOS*)lua_topointer(L,1); + const char * sqlstr = lua_tostring(L,2); int stime = luaL_checknumber(L,3); lua_newtable(L); @@ -285,7 +285,7 @@ static int l_close_stream(lua_State *L){ } static int l_close(lua_State *L){ - TAOS * taos= lua_topointer(L,1); + TAOS *taos= (TAOS*)lua_topointer(L,1); lua_newtable(L); int table_index = lua_gettop(L); @@ -295,7 +295,7 @@ static int l_close(lua_State *L){ lua_pushstring(L, "null pointer."); lua_setfield(L, table_index, "error"); }else{ - taos_close(taos); + taos_close(taos); lua_pushnumber(L, 0); lua_setfield(L, table_index, "code"); lua_pushstring(L, "done.");