Merge pull request #324 from robotspace/master
Return records with seperated fields in Lua connector.
This commit is contained in:
commit
13fc950bfd
|
@ -1,6 +1,6 @@
|
|||
# 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 .
|
||||
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 need to install Lua5.3 .
|
||||
|
||||
## Dependencies
|
||||
- Lua:
|
||||
|
|
|
@ -22,7 +22,7 @@ static int l_connect(lua_State *L)
|
|||
|
||||
taos = taos_connect(host, user,password,database, port);
|
||||
if (taos == NULL) {
|
||||
printf("failed to connect to server, reason:%s\n", taos_errstr(taos));
|
||||
printf("failed to connect server, reason:%s\n", taos_errstr(taos));
|
||||
|
||||
lua_pushnumber(L, -1);
|
||||
lua_setfield(L, table_index, "code");
|
||||
|
@ -30,7 +30,7 @@ static int l_connect(lua_State *L)
|
|||
lua_setfield(L, table_index, "error");
|
||||
lua_pushlightuserdata(L,NULL);
|
||||
}else{
|
||||
printf("success to connect to server\n");
|
||||
printf("success to connect server\n");
|
||||
lua_pushnumber(L, 0);
|
||||
lua_setfield(L, table_index, "code");
|
||||
lua_pushstring(L, taos_errstr(taos));
|
||||
|
@ -60,6 +60,7 @@ static int l_query(lua_State *L){
|
|||
return 1;
|
||||
|
||||
}else{
|
||||
//printf("success to query.\n");
|
||||
result = taos_use_result(taos);
|
||||
|
||||
if (result == NULL) {
|
||||
|
@ -82,17 +83,61 @@ static int l_query(lua_State *L){
|
|||
lua_newtable(L);
|
||||
|
||||
while ((row = taos_fetch_row(result))) {
|
||||
//printf("row index:%d\n",rows);
|
||||
rows++;
|
||||
taos_print_row(temp, row, fields, num_fields);
|
||||
printf("%s\n", temp);
|
||||
|
||||
lua_pushnumber(L,rows);
|
||||
lua_pushstring(L,temp);
|
||||
lua_newtable(L);
|
||||
|
||||
for (int i = 0; i < num_fields; ++i) {
|
||||
if (row[i] == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
lua_pushstring(L,fields[i].name);
|
||||
//printf("field name:%s,type:%d\n",fields[i].name,fields[i].type);
|
||||
switch (fields[i].type) {
|
||||
case TSDB_DATA_TYPE_TINYINT:
|
||||
lua_pushinteger(L,*((char *)row[i]));
|
||||
break;
|
||||
case TSDB_DATA_TYPE_SMALLINT:
|
||||
lua_pushinteger(L,*((short *)row[i]));
|
||||
break;
|
||||
case TSDB_DATA_TYPE_INT:
|
||||
lua_pushinteger(L,*((int *)row[i]));
|
||||
break;
|
||||
case TSDB_DATA_TYPE_BIGINT:
|
||||
lua_pushinteger(L,*((int64_t *)row[i]));
|
||||
break;
|
||||
case TSDB_DATA_TYPE_FLOAT:
|
||||
lua_pushnumber(L,*((float *)row[i]));
|
||||
break;
|
||||
case TSDB_DATA_TYPE_DOUBLE:
|
||||
lua_pushnumber(L,*((double *)row[i]));
|
||||
break;
|
||||
case TSDB_DATA_TYPE_BINARY:
|
||||
case TSDB_DATA_TYPE_NCHAR:
|
||||
lua_pushstring(L,(char *)row[i]);
|
||||
break;
|
||||
case TSDB_DATA_TYPE_TIMESTAMP:
|
||||
lua_pushinteger(L,*((int64_t *)row[i]));
|
||||
break;
|
||||
case TSDB_DATA_TYPE_BOOL:
|
||||
lua_pushinteger(L,*((char *)row[i]));
|
||||
break;
|
||||
default:
|
||||
lua_pushnil(L);
|
||||
break;
|
||||
}
|
||||
|
||||
lua_settable(L,-3);
|
||||
}
|
||||
|
||||
lua_settable(L,-3);
|
||||
}
|
||||
|
||||
taos_free_result(result);
|
||||
}
|
||||
|
||||
|
||||
lua_setfield(L, table_index, "item");
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
local mylib = require "luaconnector"
|
||||
local driver = require "luaconnector"
|
||||
|
||||
local host="127.0.0.1"
|
||||
local user="root"
|
||||
|
@ -7,7 +7,7 @@ local db =nil
|
|||
local port=6030
|
||||
local conn
|
||||
|
||||
local res = mylib.connect(host,user,password,db,port)
|
||||
local res = driver.connect(host,user,password,db,port)
|
||||
if res.code ~=0 then
|
||||
print(res.error)
|
||||
return
|
||||
|
@ -15,33 +15,44 @@ else
|
|||
conn = res.conn
|
||||
end
|
||||
|
||||
local res = mylib.query(conn,"drop database demo")
|
||||
local res = driver.query(conn,"drop database demo")
|
||||
|
||||
res = mylib.query(conn,"create database demo")
|
||||
res = driver.query(conn,"create database demo")
|
||||
if res.code ~=0 then
|
||||
print(res.error)
|
||||
return
|
||||
end
|
||||
|
||||
res = mylib.query(conn,"use demo")
|
||||
res = driver.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)")
|
||||
res = driver.query(conn,"create table m1 (ts timestamp, speed int,owner binary(20))")
|
||||
if res.code ~=0 then
|
||||
print(res.error)
|
||||
return
|
||||
end
|
||||
|
||||
res = mylib.query(conn,"insert into m1 values (1592222222223,3)")
|
||||
res = driver.query(conn,"insert into m1 values (1592222222222,0,'robotspace'), (1592222222223,1,'Hilink'),(1592222222224,2,'Harmony')")
|
||||
if res.code ~=0 then
|
||||
print(res.error)
|
||||
return
|
||||
end
|
||||
|
||||
res = mylib.query(conn,"select * from m1")
|
||||
res = driver.query(conn,"select * from m1")
|
||||
|
||||
if res.code ==0 then
|
||||
if res.code ~=0 then
|
||||
print("select error:"..res.error)
|
||||
return
|
||||
else
|
||||
print("in lua, result:")
|
||||
for i=1,#(res.item) do
|
||||
print(res.item[i])
|
||||
for i = 1, #(res.item) do
|
||||
print("timestamp:"..res.item[i].ts)
|
||||
print("speed:"..res.item[i].speed)
|
||||
print("owner:"..res.item[i].owner)
|
||||
end
|
||||
end
|
||||
|
||||
driver.close(conn)
|
||||
|
|
Loading…
Reference in New Issue