Set connection parameter with table as conventional style. Output result for test case.
This commit is contained in:
parent
ffadd16e94
commit
1919d0aa38
|
@ -13,17 +13,49 @@ struct cb_param{
|
||||||
void * stream;
|
void * stream;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int l_connect(lua_State *L){
|
static int l_connect(lua_State *L){
|
||||||
TAOS * taos;
|
TAOS * taos=NULL;
|
||||||
char *host = lua_tostring(L, 1);
|
char* host;
|
||||||
char *user = lua_tostring(L, 2);
|
char* database;
|
||||||
char *password = lua_tostring(L, 3);
|
char* user;
|
||||||
char *database = lua_tostring(L, 4);
|
char* password;
|
||||||
int port =luaL_checknumber(L, 5);
|
int port;
|
||||||
taos_init();
|
|
||||||
|
|
||||||
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
|
|
||||||
|
lua_getfield(L,-1,"host");
|
||||||
|
if (lua_isstring(L,-1)){
|
||||||
|
host = lua_tostring(L, -1);
|
||||||
|
// printf("host = %s\n", host);
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_getfield(L, 1, "port");
|
||||||
|
if (lua_isinteger(L,-1)){
|
||||||
|
port = lua_tointeger(L, -1);
|
||||||
|
//printf("port = %d\n", port);
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_getfield(L, 1, "database");
|
||||||
|
if (lua_isstring(L, -1)){
|
||||||
|
database = lua_tostring(L, -1);
|
||||||
|
//printf("database = %s\n", database);
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_getfield(L, 1, "user");
|
||||||
|
if (lua_isstring(L, -1)){
|
||||||
|
user = lua_tostring(L, -1);
|
||||||
|
//printf("user = %s\n", user);
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_getfield(L, 1, "password");
|
||||||
|
if (lua_isstring(L, -1)){
|
||||||
|
password = lua_tostring(L, -1);
|
||||||
|
//printf("password = %s\n", password);
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_settop(L,0);
|
||||||
|
|
||||||
|
taos_init();
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
int table_index = lua_gettop(L);
|
int table_index = lua_gettop(L);
|
||||||
|
|
||||||
|
@ -31,15 +63,15 @@ static int l_connect(lua_State *L){
|
||||||
if (taos == NULL) {
|
if (taos == NULL) {
|
||||||
printf("failed to connect server, reason:%s\n", taos_errstr(taos));
|
printf("failed to connect server, reason:%s\n", taos_errstr(taos));
|
||||||
|
|
||||||
lua_pushnumber(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(taos));
|
||||||
lua_setfield(L, table_index, "error");
|
lua_setfield(L, table_index, "error");
|
||||||
lua_pushlightuserdata(L,NULL);
|
lua_pushlightuserdata(L,NULL);
|
||||||
lua_setfield(L, table_index, "conn");
|
lua_setfield(L, table_index, "conn");
|
||||||
}else{
|
}else{
|
||||||
printf("success to connect server\n");
|
// printf("success to connect server\n");
|
||||||
lua_pushnumber(L, 0);
|
lua_pushinteger(L, 0);
|
||||||
lua_setfield(L, table_index, "code");
|
lua_setfield(L, table_index, "code");
|
||||||
lua_pushstring(L, taos_errstr(taos));
|
lua_pushstring(L, taos_errstr(taos));
|
||||||
lua_setfield(L, table_index, "error");
|
lua_setfield(L, table_index, "error");
|
||||||
|
@ -62,7 +94,7 @@ static int l_query(lua_State *L){
|
||||||
int32_t code = taos_errno(result);
|
int32_t code = taos_errno(result);
|
||||||
if( code != 0){
|
if( code != 0){
|
||||||
printf("failed, reason:%s\n", taos_errstr(result));
|
printf("failed, reason:%s\n", taos_errstr(result));
|
||||||
lua_pushnumber(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(taos));
|
||||||
lua_setfield(L, table_index, "error");
|
lua_setfield(L, table_index, "error");
|
||||||
|
@ -79,7 +111,7 @@ static int l_query(lua_State *L){
|
||||||
|
|
||||||
int affectRows = taos_affected_rows(result);
|
int affectRows = taos_affected_rows(result);
|
||||||
// printf(" affect rows:%d\r\n", affectRows);
|
// printf(" affect rows:%d\r\n", affectRows);
|
||||||
lua_pushnumber(L, 0);
|
lua_pushinteger(L, 0);
|
||||||
lua_setfield(L, table_index, "code");
|
lua_setfield(L, table_index, "code");
|
||||||
lua_pushinteger(L, affectRows);
|
lua_pushinteger(L, affectRows);
|
||||||
lua_setfield(L, table_index, "affected");
|
lua_setfield(L, table_index, "affected");
|
||||||
|
@ -150,8 +182,8 @@ void stream_cb(void *param, TAOS_RES *result, TAOS_ROW row){
|
||||||
TAOS_FIELD *fields = taos_fetch_fields(result);
|
TAOS_FIELD *fields = taos_fetch_fields(result);
|
||||||
int numFields = taos_num_fields(result);
|
int numFields = taos_num_fields(result);
|
||||||
|
|
||||||
printf("\nnumfields:%d\n", numFields);
|
// printf("\nnumfields:%d\n", numFields);
|
||||||
printf("\n\r-----------------------------------------------------------------------------------\n");
|
//printf("\n\r-----------------------------------------------------------------------------------\n");
|
||||||
|
|
||||||
lua_State *L = p->state;
|
lua_State *L = p->state;
|
||||||
lua_rawgeti(L, LUA_REGISTRYINDEX, p->callback);
|
lua_rawgeti(L, LUA_REGISTRYINDEX, p->callback);
|
||||||
|
@ -204,7 +236,7 @@ void stream_cb(void *param, TAOS_RES *result, TAOS_ROW row){
|
||||||
|
|
||||||
lua_call(L, 1, 0);
|
lua_call(L, 1, 0);
|
||||||
|
|
||||||
printf("-----------------------------------------------------------------------------------\n\r");
|
// printf("-----------------------------------------------------------------------------------\n\r");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_open_stream(lua_State *L){
|
static int l_open_stream(lua_State *L){
|
||||||
|
|
|
@ -1,93 +1,117 @@
|
||||||
local driver = require "luaconnector"
|
local driver = require "luaconnector"
|
||||||
|
|
||||||
local host="127.0.0.1"
|
local config = {
|
||||||
local user="root"
|
host = "127.0.0.1",
|
||||||
local password="taosdata"
|
port = 6030,
|
||||||
local db =nil
|
database = "",
|
||||||
local port=6030
|
user = "root",
|
||||||
local conn
|
password = "taosdata",
|
||||||
|
max_packet_size = 1024 * 1024
|
||||||
|
}
|
||||||
|
|
||||||
local res = driver.connect(host,user,password,db,port)
|
local conn
|
||||||
|
local res = driver.connect(config)
|
||||||
if res.code ~=0 then
|
if res.code ~=0 then
|
||||||
print(res.error)
|
print("connect--- failed: "..res.error)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
conn = res.conn
|
conn = res.conn
|
||||||
|
print("connect--- pass.")
|
||||||
end
|
end
|
||||||
|
|
||||||
local res = driver.query(conn,"drop database if exists demo")
|
local res = driver.query(conn,"drop database if exists demo")
|
||||||
|
|
||||||
res = driver.query(conn,"create database demo")
|
res = driver.query(conn,"create database demo")
|
||||||
if res.code ~=0 then
|
if res.code ~=0 then
|
||||||
print(res.error)
|
print("create db--- failed: "..res.error)
|
||||||
return
|
return
|
||||||
|
else
|
||||||
|
print("create db--- pass.")
|
||||||
end
|
end
|
||||||
|
|
||||||
res = driver.query(conn,"use demo")
|
res = driver.query(conn,"use demo")
|
||||||
if res.code ~=0 then
|
if res.code ~=0 then
|
||||||
print(res.error)
|
print("select db--- failed: "..res.error)
|
||||||
return
|
return
|
||||||
|
else
|
||||||
|
print("select db--- pass.")
|
||||||
end
|
end
|
||||||
|
|
||||||
res = driver.query(conn,"create table m1 (ts timestamp, speed int,owner binary(20))")
|
res = driver.query(conn,"create table m1 (ts timestamp, speed int,owner binary(20))")
|
||||||
if res.code ~=0 then
|
if res.code ~=0 then
|
||||||
print(res.error)
|
print("create table---failed: "..res.error)
|
||||||
return
|
return
|
||||||
|
else
|
||||||
|
print("create table--- pass.")
|
||||||
end
|
end
|
||||||
|
|
||||||
res = driver.query(conn,"insert into m1 values ('2019-09-01 00:00:00.001',0,'robotspace'), ('2019-09-01 00:00:00.002',1,'Hilink'),('2019-09-01 00:00:00.003',2,'Harmony')")
|
res = driver.query(conn,"insert into m1 values ('2019-09-01 00:00:00.001',0,'robotspace'), ('2019-09-01 00:00:00.002',1,'Hilink'),('2019-09-01 00:00:00.003',2,'Harmony')")
|
||||||
if res.code ~=0 then
|
if res.code ~=0 then
|
||||||
print(res.error)
|
print("insert records failed: "..res.error)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
print("insert successfully, affected:"..res.affected)
|
if(res.affected == 3) then
|
||||||
|
print("insert records--- pass")
|
||||||
|
else
|
||||||
|
print("insert records---failed: expect 3 affected records, actually affected "..res.affected)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
res = driver.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)
|
print("select failed: "..res.error)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
print("in lua, result:")
|
if (#(res.item) == 3) then
|
||||||
for i = 1, #(res.item) do
|
print("select--- pass")
|
||||||
print("timestamp:"..res.item[i].ts)
|
else
|
||||||
print("speed:"..res.item[i].speed)
|
print("select--- failed: expect 3 affected records, actually received "..#(res.item))
|
||||||
print("owner:"..res.item[i].owner)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
res = driver.query(conn,"CREATE TABLE thermometer (ts timestamp, degree double) TAGS(location binary(20), type int)")
|
res = driver.query(conn,"CREATE TABLE thermometer (ts timestamp, degree double) TAGS(location binary(20), type int)")
|
||||||
if res.code ~=0 then
|
if res.code ~=0 then
|
||||||
print(res.error)
|
print(res.error)
|
||||||
return
|
return
|
||||||
|
else
|
||||||
|
print("create super table--- pass")
|
||||||
end
|
end
|
||||||
res = driver.query(conn,"CREATE TABLE therm1 USING thermometer TAGS ('beijing', 1)")
|
res = driver.query(conn,"CREATE TABLE therm1 USING thermometer TAGS ('beijing', 1)")
|
||||||
if res.code ~=0 then
|
if res.code ~=0 then
|
||||||
print(res.error)
|
print(res.error)
|
||||||
return
|
return
|
||||||
|
else
|
||||||
|
print("create table--- pass")
|
||||||
end
|
end
|
||||||
|
|
||||||
res = driver.query(conn,"INSERT INTO therm1 VALUES ('2019-09-01 00:00:00.001', 20),('2019-09-01 00:00:00.002', 21)")
|
res = driver.query(conn,"INSERT INTO therm1 VALUES ('2019-09-01 00:00:00.001', 20),('2019-09-01 00:00:00.002', 21)")
|
||||||
|
|
||||||
if res.code ~=0 then
|
if res.code ~=0 then
|
||||||
print(res.error)
|
print(res.error)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
print("insert successfully, affected:"..res.affected)
|
if(res.affected == 2) then
|
||||||
|
print("insert records--- pass")
|
||||||
|
else
|
||||||
|
print("insert records---failed: expect 2 affected records, actually affected "..res.affected)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
res = driver.query(conn,"SELECT COUNT(*) count, AVG(degree) AS av, MAX(degree), MIN(degree) FROM thermometer WHERE location='beijing' or location='tianjin' GROUP BY location, type")
|
res = driver.query(conn,"SELECT COUNT(*) count, AVG(degree) AS av, MAX(degree), MIN(degree) FROM thermometer WHERE location='beijing' or location='tianjin' GROUP BY location, type")
|
||||||
if res.code ~=0 then
|
if res.code ~=0 then
|
||||||
print("select error:"..res.error)
|
print("select from super table--- failed:"..res.error)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
print("in lua, result:")
|
print("select from super table--- pass")
|
||||||
for i = 1, #(res.item) do
|
for i = 1, #(res.item) do
|
||||||
print("res:"..res.item[i].count)
|
print("res:"..res.item[i].count)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function callback(t)
|
function callback(t)
|
||||||
|
print("------------------------")
|
||||||
print("continuous query result:")
|
print("continuous query result:")
|
||||||
for key, value in pairs(t) do
|
for key, value in pairs(t) do
|
||||||
print("key:"..key..", value:"..value)
|
print("key:"..key..", value:"..value)
|
||||||
|
@ -97,25 +121,25 @@ end
|
||||||
local stream
|
local stream
|
||||||
res = driver.open_stream(conn,"SELECT COUNT(*) as count, AVG(degree) as avg, MAX(degree) as max, MIN(degree) as min FROM thermometer interval(2s) sliding(2s);)",0,callback)
|
res = driver.open_stream(conn,"SELECT COUNT(*) as count, AVG(degree) as avg, MAX(degree) as max, MIN(degree) as min FROM thermometer interval(2s) sliding(2s);)",0,callback)
|
||||||
if res.code ~=0 then
|
if res.code ~=0 then
|
||||||
print("open stream error:"..res.error)
|
print("open stream--- failed:"..res.error)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
print("openstream ok")
|
print("open stream--- pass")
|
||||||
stream = res.stream
|
stream = res.stream
|
||||||
end
|
end
|
||||||
|
|
||||||
--From now on we begin continous query in an definite (infinite if you want) loop.
|
print("From now on we start continous insert in an definite (infinite if you want) loop.")
|
||||||
local loop_index = 0
|
local loop_index = 0
|
||||||
while loop_index < 10 do
|
while loop_index < 30 do
|
||||||
local t = os.time()*1000
|
local t = os.time()*1000
|
||||||
local v = loop_index
|
local v = loop_index
|
||||||
res = driver.query(conn,string.format("INSERT INTO therm1 VALUES (%d, %d)",t,v))
|
res = driver.query(conn,string.format("INSERT INTO therm1 VALUES (%d, %d)",t,v))
|
||||||
|
|
||||||
if res.code ~=0 then
|
if res.code ~=0 then
|
||||||
print(res.error)
|
print("continous insertion--- failed:" .. res.error)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
print("insert successfully, affected:"..res.affected)
|
--print("insert successfully, affected:"..res.affected)
|
||||||
end
|
end
|
||||||
os.execute("sleep " .. 1)
|
os.execute("sleep " .. 1)
|
||||||
loop_index = loop_index + 1
|
loop_index = loop_index + 1
|
||||||
|
|
Loading…
Reference in New Issue