diff --git a/tests/examples/lua/README.md b/tests/examples/lua/README.md new file mode 100644 index 0000000000000000000000000000000000000000..db2d56c937ee560169babfd270088d8cec71d931 --- /dev/null +++ b/tests/examples/lua/README.md @@ -0,0 +1,20 @@ +# TDengine driver connector for Lua + +It's a lua implementation for [TDengine](https://github.com/taosdata/TDengine), an open-sourced big data platform designed and optimized for the Internet of Things (IoT), Connected Cars, Industrial IoT, and IT Infrastructure and Application Monitoring. You may needs install Lua5.3 . + +## Dependencies +- Lua: +``` +https://www.lua.org/ +``` + +## Run with Sample + +Build driver lib: +``` +./build.sh +``` +Run lua sample: +``` +lua test.lua +``` diff --git a/tests/examples/lua/build.sh b/tests/examples/lua/build.sh new file mode 100755 index 0000000000000000000000000000000000000000..8018a3b0d87bf622f6f4c815cba7ba3873a736b5 --- /dev/null +++ b/tests/examples/lua/build.sh @@ -0,0 +1,2 @@ +gcc lua_connector.c -fPIC -shared -o luaconnector.so -Wall -ltaos + diff --git a/tests/examples/lua/lua_connector.c b/tests/examples/lua/lua_connector.c new file mode 100644 index 0000000000000000000000000000000000000000..c42da2789f2fddeea8c46ba07d4b06a434b33755 --- /dev/null +++ b/tests/examples/lua/lua_connector.c @@ -0,0 +1,132 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +static int l_connect(lua_State *L) +{ + TAOS * taos; + char *host = lua_tostring(L, 1); + char *user = lua_tostring(L, 2); + char *password = lua_tostring(L, 3); + char *database = lua_tostring(L, 4); + int port =luaL_checknumber(L, 5); + taos_init(); + + lua_newtable(L); + int table_index = lua_gettop(L); + + taos = taos_connect(host, user,password,database, port); + if (taos == NULL) { + printf("failed to connect to server, reason:%s\n", taos_errstr(taos)); + + lua_pushnumber(L, -1); + lua_setfield(L, table_index, "code"); + lua_pushstring(L, taos_errstr(taos)); + lua_setfield(L, table_index, "error"); + lua_pushlightuserdata(L,NULL); + }else{ + printf("success to connect to server\n"); + lua_pushnumber(L, 0); + lua_setfield(L, table_index, "code"); + lua_pushstring(L, taos_errstr(taos)); + lua_setfield(L, table_index, "error"); + lua_pushlightuserdata(L,taos); + lua_setfield(L, table_index, "conn"); + } + + return 1; +} + +static int l_query(lua_State *L){ + TAOS * taos= lua_topointer(L,1); + 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); + if(taos_query(taos, s)!=0){ + printf("failed, reason:%s\n", taos_errstr(taos)); + lua_pushnumber(L, -1); + lua_setfield(L, table_index, "code"); + lua_pushstring(L, taos_errstr(taos)); + lua_setfield(L, table_index, "error"); + + return 1; + + }else{ + result = taos_use_result(taos); + + if (result == NULL) { + printf("failed to get result, reason:%s\n", taos_errstr(taos)); + lua_pushnumber(L, -2); + lua_setfield(L, table_index, "code"); + lua_pushstring(L, taos_errstr(taos)); + lua_setfield(L, table_index, "error"); + return 1; + } + + TAOS_ROW row; + int rows = 0; + int num_fields = taos_field_count(taos); + TAOS_FIELD *fields = taos_fetch_fields(result); + char temp[256]; + + lua_pushnumber(L, 0); + lua_setfield(L, table_index, "code"); + lua_newtable(L); + + while ((row = taos_fetch_row(result))) { + rows++; + taos_print_row(temp, row, fields, num_fields); + printf("%s\n", temp); + lua_pushnumber(L,rows); + lua_pushstring(L,temp); + lua_settable(L,-3); + } + + taos_free_result(result); + } + + lua_setfield(L, table_index, "item"); + return 1; +} + +static int l_close(lua_State *L){ + TAOS * taos= lua_topointer(L,1); + lua_newtable(L); + int table_index = lua_gettop(L); + + if(taos == NULL){ + lua_pushnumber(L, -1); + lua_setfield(L, table_index, "code"); + lua_pushstring(L, "null pointer."); + lua_setfield(L, table_index, "error"); + }else{ + taos_close(taos); + lua_pushnumber(L, 0); + lua_setfield(L, table_index, "code"); + lua_pushstring(L, "done."); + lua_setfield(L, table_index, "error"); + } + return 1; +} + +static const struct luaL_Reg lib[] = { + {"connect", l_connect}, + {"query", l_query}, + {"close", l_close}, + {NULL, NULL} +}; + +extern int luaopen_luaconnector(lua_State* L) +{ + luaL_newlib(L, lib); + + return 1; +} diff --git a/tests/examples/lua/test.lua b/tests/examples/lua/test.lua new file mode 100644 index 0000000000000000000000000000000000000000..f0210db9f1db6370e5e704760a9580a71dd9b7c1 --- /dev/null +++ b/tests/examples/lua/test.lua @@ -0,0 +1,47 @@ +local mylib = require "luaconnector" + +local host="127.0.0.1" +local user="root" +local password="taosdata" +local db =nil +local port=6030 +local conn + +local res = mylib.connect(host,user,password,db,port) +if res.code ~=0 then + print(res.error) + return +else + conn = res.conn +end + +local res = mylib.query(conn,"drop database demo") + +res = mylib.query(conn,"create database demo") +if res.code ~=0 then + print(res.error) + return +end + +res = mylib.query(conn,"use demo") +if res.code ~=0 then + print(res.error) + return +end + +res = mylib.query(conn,"create table m1 (ts timestamp, speed int)") +if res.code ~=0 then + print(res.error) + return +end + +res = mylib.query(conn,"insert into m1 values (1592222222223,3)") + +res = mylib.query(conn,"select * from m1") + +if res.code ==0 then + print("in lua, result:") + for i=1,#(res.item) do + print(res.item[i]) + end +end