fix(connector): lua example compile for 3.0 (#11523)
* [TD-13558]<feature>: taos shell refactor add taosTools as submodule * add tools/taos-tools * add more client interface for taosTools compile * update taos-tools * update taos-tools * refactor shell * [TD-13558]<feature>: taos shell test speed * [TD-13558]<feature>: taos -n startup works * taos -n rpc works * taos -n server works * cleanup code since no endPort in 3.0 * update taos-tools * [TD-13558]<feature>: taos -C works * improve taos shell -c WIP * update taos-tools * add demoapi.c * adjust show databases result for 3.0 * test: add platform logic * add nchar * adjust taos_fetch_lengths * print fields * remove show databases check from insert cases * fix lua example compile for 3.0 still not work
This commit is contained in:
parent
b3978108bc
commit
85496cbb4c
|
@ -4,5 +4,5 @@ if [ "$lua_header_installed" = "0" ]; then
|
||||||
sudo apt install -y liblua5.3-dev
|
sudo apt install -y liblua5.3-dev
|
||||||
fi
|
fi
|
||||||
|
|
||||||
gcc -std=c99 lua_connector.c -fPIC -shared -o luaconnector.so -Wall -ltaos -I/usr/include/lua5.3
|
gcc -std=c99 lua_connector.c -fPIC -shared -o luaconnector.so -Wall -ltaos -I/usr/include/lua5.3 -I../../include/client
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
#include <lauxlib.h>
|
#include <stdio.h>
|
||||||
#include <lua.h>
|
|
||||||
#include <lualib.h>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "../../../include/client/taos.h"
|
#include <lua.h>
|
||||||
|
#include <lauxlib.h>
|
||||||
|
#include <lualib.h>
|
||||||
|
#include "taos.h"
|
||||||
|
|
||||||
struct cb_param{
|
struct cb_param{
|
||||||
lua_State* state;
|
lua_State* state;
|
||||||
|
@ -28,14 +28,14 @@ static int l_connect(lua_State *L){
|
||||||
|
|
||||||
luaL_checktype(L, 1, LUA_TTABLE);
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
|
|
||||||
lua_getfield(L,1,"host");
|
lua_getfield(L, 1,"host");
|
||||||
if (lua_isstring(L,-1)){
|
if (lua_isstring(L,-1)){
|
||||||
host = lua_tostring(L, -1);
|
host = lua_tostring(L, -1);
|
||||||
// printf("host = %s\n", host);
|
// printf("host = %s\n", host);
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_getfield(L, 1, "port");
|
lua_getfield(L, 1, "port");
|
||||||
if (lua_isinteger(L,-1)){
|
if (lua_isinteger(L, -1)){
|
||||||
port = lua_tointeger(L, -1);
|
port = lua_tointeger(L, -1);
|
||||||
//printf("port = %d\n", port);
|
//printf("port = %d\n", port);
|
||||||
}
|
}
|
||||||
|
@ -60,8 +60,6 @@ static int l_connect(lua_State *L){
|
||||||
|
|
||||||
lua_settop(L,0);
|
lua_settop(L,0);
|
||||||
|
|
||||||
taos_init();
|
|
||||||
|
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
int table_index = lua_gettop(L);
|
int table_index = lua_gettop(L);
|
||||||
|
|
||||||
|
@ -102,7 +100,7 @@ static int l_query(lua_State *L){
|
||||||
printf("failed, reason:%s\n", taos_errstr(result));
|
printf("failed, reason:%s\n", taos_errstr(result));
|
||||||
lua_pushinteger(L, -1);
|
lua_pushinteger(L, -1);
|
||||||
lua_setfield(L, table_index, "code");
|
lua_setfield(L, table_index, "code");
|
||||||
lua_pushstring(L, taos_errstr(taos));
|
lua_pushstring(L, taos_errstr(result));
|
||||||
lua_setfield(L, table_index, "error");
|
lua_setfield(L, table_index, "error");
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -113,7 +111,6 @@ static int l_query(lua_State *L){
|
||||||
int rows = 0;
|
int rows = 0;
|
||||||
int num_fields = taos_field_count(result);
|
int num_fields = taos_field_count(result);
|
||||||
const TAOS_FIELD *fields = taos_fetch_fields(result);
|
const TAOS_FIELD *fields = taos_fetch_fields(result);
|
||||||
//char temp[256];
|
|
||||||
|
|
||||||
const int affectRows = taos_affected_rows(result);
|
const int affectRows = taos_affected_rows(result);
|
||||||
// printf(" affect rows:%d\r\n", affectRows);
|
// printf(" affect rows:%d\r\n", affectRows);
|
||||||
|
@ -122,7 +119,7 @@ static int l_query(lua_State *L){
|
||||||
lua_pushinteger(L, affectRows);
|
lua_pushinteger(L, affectRows);
|
||||||
lua_setfield(L, table_index, "affected");
|
lua_setfield(L, table_index, "affected");
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
|
|
||||||
while ((row = taos_fetch_row(result))) {
|
while ((row = taos_fetch_row(result))) {
|
||||||
//printf("row index:%d\n",rows);
|
//printf("row index:%d\n",rows);
|
||||||
rows++;
|
rows++;
|
||||||
|
@ -136,17 +133,21 @@ static int l_query(lua_State *L){
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_pushstring(L,fields[i].name);
|
lua_pushstring(L,fields[i].name);
|
||||||
|
int32_t* length = taos_fetch_lengths(result);
|
||||||
switch (fields[i].type) {
|
switch (fields[i].type) {
|
||||||
|
case TSDB_DATA_TYPE_UTINYINT:
|
||||||
case TSDB_DATA_TYPE_TINYINT:
|
case TSDB_DATA_TYPE_TINYINT:
|
||||||
lua_pushinteger(L,*((char *)row[i]));
|
lua_pushinteger(L,*((char *)row[i]));
|
||||||
break;
|
break;
|
||||||
|
case TSDB_DATA_TYPE_USMALLINT:
|
||||||
case TSDB_DATA_TYPE_SMALLINT:
|
case TSDB_DATA_TYPE_SMALLINT:
|
||||||
lua_pushinteger(L,*((short *)row[i]));
|
lua_pushinteger(L,*((short *)row[i]));
|
||||||
break;
|
break;
|
||||||
|
case TSDB_DATA_TYPE_UINT:
|
||||||
case TSDB_DATA_TYPE_INT:
|
case TSDB_DATA_TYPE_INT:
|
||||||
lua_pushinteger(L,*((int *)row[i]));
|
lua_pushinteger(L,*((int *)row[i]));
|
||||||
break;
|
break;
|
||||||
|
case TSDB_DATA_TYPE_UBIGINT:
|
||||||
case TSDB_DATA_TYPE_BIGINT:
|
case TSDB_DATA_TYPE_BIGINT:
|
||||||
lua_pushinteger(L,*((int64_t *)row[i]));
|
lua_pushinteger(L,*((int64_t *)row[i]));
|
||||||
break;
|
break;
|
||||||
|
@ -156,9 +157,11 @@ static int l_query(lua_State *L){
|
||||||
case TSDB_DATA_TYPE_DOUBLE:
|
case TSDB_DATA_TYPE_DOUBLE:
|
||||||
lua_pushnumber(L,*((double *)row[i]));
|
lua_pushnumber(L,*((double *)row[i]));
|
||||||
break;
|
break;
|
||||||
|
case TSDB_DATA_TYPE_JSON:
|
||||||
case TSDB_DATA_TYPE_BINARY:
|
case TSDB_DATA_TYPE_BINARY:
|
||||||
case TSDB_DATA_TYPE_NCHAR:
|
case TSDB_DATA_TYPE_NCHAR:
|
||||||
lua_pushstring(L,(char *)row[i]);
|
//printf("type:%d, max len:%d, current len:%d\n",fields[i].type, fields[i].bytes, length[i]);
|
||||||
|
lua_pushlstring(L,(char *)row[i], length[i]);
|
||||||
break;
|
break;
|
||||||
case TSDB_DATA_TYPE_TIMESTAMP:
|
case TSDB_DATA_TYPE_TIMESTAMP:
|
||||||
lua_pushinteger(L,*((int64_t *)row[i]));
|
lua_pushinteger(L,*((int64_t *)row[i]));
|
||||||
|
@ -166,6 +169,7 @@ static int l_query(lua_State *L){
|
||||||
case TSDB_DATA_TYPE_BOOL:
|
case TSDB_DATA_TYPE_BOOL:
|
||||||
lua_pushinteger(L,*((char *)row[i]));
|
lua_pushinteger(L,*((char *)row[i]));
|
||||||
break;
|
break;
|
||||||
|
case TSDB_DATA_TYPE_NULL:
|
||||||
default:
|
default:
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
break;
|
break;
|
||||||
|
@ -235,112 +239,6 @@ static int l_async_query(lua_State *L){
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stream_cb(void *param, TAOS_RES *result, TAOS_ROW row){
|
|
||||||
struct cb_param* p = (struct cb_param*) param;
|
|
||||||
TAOS_FIELD *fields = taos_fetch_fields(result);
|
|
||||||
int numFields = taos_num_fields(result);
|
|
||||||
|
|
||||||
// printf("\nnumfields:%d\n", numFields);
|
|
||||||
//printf("\n\r-----------------------------------------------------------------------------------\n");
|
|
||||||
|
|
||||||
lua_State *L = p->state;
|
|
||||||
lua_rawgeti(L, LUA_REGISTRYINDEX, p->callback);
|
|
||||||
|
|
||||||
lua_newtable(L);
|
|
||||||
|
|
||||||
for (int i = 0; i < numFields; ++i) {
|
|
||||||
if (row[i] == NULL) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_pushstring(L,fields[i].name);
|
|
||||||
|
|
||||||
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_call(L, 1, 0);
|
|
||||||
|
|
||||||
// printf("-----------------------------------------------------------------------------------\n\r");
|
|
||||||
}
|
|
||||||
|
|
||||||
static int l_open_stream(lua_State *L){
|
|
||||||
int r = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
||||||
TAOS * taos = (TAOS*)lua_topointer(L,1);
|
|
||||||
const char * sqlstr = lua_tostring(L,2);
|
|
||||||
int stime = luaL_checknumber(L,3);
|
|
||||||
|
|
||||||
lua_newtable(L);
|
|
||||||
int table_index = lua_gettop(L);
|
|
||||||
|
|
||||||
struct cb_param *p = malloc(sizeof(struct cb_param));
|
|
||||||
p->state = L;
|
|
||||||
p->callback=r;
|
|
||||||
// printf("r:%d, L:%d\n",r,L);
|
|
||||||
void * s = taos_open_stream(taos,sqlstr,stream_cb,stime,p,NULL);
|
|
||||||
if (s == NULL) {
|
|
||||||
printf("failed to open stream, reason:%s\n", taos_errstr(taos));
|
|
||||||
free(p);
|
|
||||||
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);
|
|
||||||
lua_setfield(L, table_index, "stream");
|
|
||||||
}else{
|
|
||||||
// printf("success to open stream\n");
|
|
||||||
lua_pushnumber(L, 0);
|
|
||||||
lua_setfield(L, table_index, "code");
|
|
||||||
lua_pushstring(L, taos_errstr(taos));
|
|
||||||
lua_setfield(L, table_index, "error");
|
|
||||||
p->stream = s;
|
|
||||||
lua_pushlightuserdata(L,p);
|
|
||||||
lua_setfield(L, table_index, "stream");//stream has different content in lua and c.
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int l_close_stream(lua_State *L){
|
|
||||||
//TODO:get stream and free cb_param
|
|
||||||
struct cb_param *p = lua_touserdata(L,1);
|
|
||||||
taos_close_stream(p->stream);
|
|
||||||
free(p);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int l_close(lua_State *L){
|
static int l_close(lua_State *L){
|
||||||
TAOS *taos= (TAOS*)lua_topointer(L,1);
|
TAOS *taos= (TAOS*)lua_topointer(L,1);
|
||||||
|
@ -367,8 +265,6 @@ static const struct luaL_Reg lib[] = {
|
||||||
{"query", l_query},
|
{"query", l_query},
|
||||||
{"query_a",l_async_query},
|
{"query_a",l_async_query},
|
||||||
{"close", l_close},
|
{"close", l_close},
|
||||||
{"open_stream", l_open_stream},
|
|
||||||
{"close_stream", l_close_stream},
|
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue