diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index 37edc739e47b88236ee98c3ef0f1185c622abf9d..e14c4e60d9d49a660643c89f4642af7b12c8d18a 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -3,19 +3,13 @@ PROJECT(TDengine) IF (TD_LINUX) INCLUDE_DIRECTORIES(. ${TD_SOURCE_DIR}/src/inc ${TD_SOURCE_DIR}/src/client/inc ${TD_SOURCE_DIR}/inc) AUX_SOURCE_DIRECTORY(. SRC) - # ADD_EXECUTABLE(demo apitest.c) - #TARGET_LINK_LIBRARIES(demo taos_static trpc tutil pthread ) - #ADD_EXECUTABLE(sml schemaless.c) - #TARGET_LINK_LIBRARIES(sml taos_static trpc tutil pthread ) - #ADD_EXECUTABLE(subscribe subscribe.c) - #TARGET_LINK_LIBRARIES(subscribe taos_static trpc tutil pthread ) - #ADD_EXECUTABLE(epoll epoll.c) - #TARGET_LINK_LIBRARIES(epoll taos_static trpc tutil pthread lua) add_executable(tmq "") add_executable(stream_demo "") - add_executable(demoapi "") - add_executable(api_reqid "") + add_executable(schemaless "") + add_executable(prepare "") + add_executable(demo "") + add_executable(asyncdemo "") target_sources(tmq PRIVATE @@ -27,16 +21,25 @@ IF (TD_LINUX) "stream_demo.c" ) - target_sources(demoapi + target_sources(schemaless PRIVATE - "demoapi.c" + "schemaless.c" ) - target_sources(api_reqid + target_sources(prepare PRIVATE - "api_with_reqid_test.c" + "prepare.c" ) + target_sources(demo + PRIVATE + "demo.c" + ) + + target_sources(asyncdemo + PRIVATE + "asyncdemo.c" + ) target_link_libraries(tmq taos_static @@ -46,46 +49,30 @@ IF (TD_LINUX) taos_static ) - target_link_libraries(demoapi + target_link_libraries(schemaless taos_static ) - target_link_libraries(api_reqid + target_link_libraries(prepare taos_static ) - - target_include_directories(tmq - PUBLIC "${TD_SOURCE_DIR}/include/os" - PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" - ) - - target_include_directories(stream_demo - PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" - ) - - target_include_directories(demoapi - PUBLIC "${TD_SOURCE_DIR}/include/client" - PUBLIC "${TD_SOURCE_DIR}/include/os" - PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" + target_link_libraries(demo + taos_static ) - target_include_directories(api_reqid - PUBLIC "${TD_SOURCE_DIR}/include/client" - PUBLIC "${TD_SOURCE_DIR}/include/os" - PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" + target_link_libraries(asyncdemo + taos_static ) SET_TARGET_PROPERTIES(tmq PROPERTIES OUTPUT_NAME tmq) SET_TARGET_PROPERTIES(stream_demo PROPERTIES OUTPUT_NAME stream_demo) - SET_TARGET_PROPERTIES(demoapi PROPERTIES OUTPUT_NAME demoapi) - SET_TARGET_PROPERTIES(api_reqid PROPERTIES OUTPUT_NAME api_reqid) + SET_TARGET_PROPERTIES(schemaless PROPERTIES OUTPUT_NAME schemaless) + SET_TARGET_PROPERTIES(prepare PROPERTIES OUTPUT_NAME prepare) + SET_TARGET_PROPERTIES(demo PROPERTIES OUTPUT_NAME demo) + SET_TARGET_PROPERTIES(asyncdemo PROPERTIES OUTPUT_NAME asyncdemo) ENDIF () IF (TD_DARWIN) INCLUDE_DIRECTORIES(. ${TD_SOURCE_DIR}/src/inc ${TD_SOURCE_DIR}/src/client/inc ${TD_SOURCE_DIR}/inc) AUX_SOURCE_DIRECTORY(. SRC) - #ADD_EXECUTABLE(demo demo.c) - #TARGET_LINK_LIBRARIES(demo taos_static trpc tutil pthread lua) - #ADD_EXECUTABLE(epoll epoll.c) - #TARGET_LINK_LIBRARIES(epoll taos_static trpc tutil pthread lua) ENDIF () diff --git a/examples/c/apitest.c b/examples/c/apitest.c deleted file mode 100644 index c179acaf4ef086a702e3130d8ae7effade0f09b9..0000000000000000000000000000000000000000 --- a/examples/c/apitest.c +++ /dev/null @@ -1,1948 +0,0 @@ -// sample code to verify all TDengine API -// to compile: gcc -o apitest apitest.c -ltaos - -#include "cJSON.h" -#include "taoserror.h" - -#include -#include -#include -#include -#include "../../../include/client/taos.h" - -static void prepare_data(TAOS* taos) { - TAOS_RES* result; - result = taos_query(taos, "drop database if exists test;"); - taos_free_result(result); - usleep(100000); - result = taos_query(taos, "create database test precision 'us';"); - taos_free_result(result); - usleep(100000); - taos_select_db(taos, "test"); - - result = taos_query(taos, "create table meters(ts timestamp, a int) tags(area int);"); - taos_free_result(result); - - result = taos_query(taos, "create table t0 using meters tags(0);"); - taos_free_result(result); - result = taos_query(taos, "create table t1 using meters tags(1);"); - taos_free_result(result); - result = taos_query(taos, "create table t2 using meters tags(2);"); - taos_free_result(result); - result = taos_query(taos, "create table t3 using meters tags(3);"); - taos_free_result(result); - result = taos_query(taos, "create table t4 using meters tags(4);"); - taos_free_result(result); - result = taos_query(taos, "create table t5 using meters tags(5);"); - taos_free_result(result); - result = taos_query(taos, "create table t6 using meters tags(6);"); - taos_free_result(result); - result = taos_query(taos, "create table t7 using meters tags(7);"); - taos_free_result(result); - result = taos_query(taos, "create table t8 using meters tags(8);"); - taos_free_result(result); - result = taos_query(taos, "create table t9 using meters tags(9);"); - taos_free_result(result); - - result = taos_query(taos, - "insert into t0 values('2020-01-01 00:00:00.000', 0)" - " ('2020-01-01 00:01:00.000', 0)" - " ('2020-01-01 00:02:00.000', 0)" - " t1 values('2020-01-01 00:00:00.000', 0)" - " ('2020-01-01 00:01:00.000', 0)" - " ('2020-01-01 00:02:00.000', 0)" - " ('2020-01-01 00:03:00.000', 0)" - " t2 values('2020-01-01 00:00:00.000', 0)" - " ('2020-01-01 00:01:00.000', 0)" - " ('2020-01-01 00:01:01.000', 0)" - " ('2020-01-01 00:01:02.000', 0)" - " t3 values('2020-01-01 00:01:02.000', 0)" - " t4 values('2020-01-01 00:01:02.000', 0)" - " t5 values('2020-01-01 00:01:02.000', 0)" - " t6 values('2020-01-01 00:01:02.000', 0)" - " t7 values('2020-01-01 00:01:02.000', 0)" - " t8 values('2020-01-01 00:01:02.000', 0)" - " t9 values('2020-01-01 00:01:02.000', 0)"); - int affected = taos_affected_rows(result); - if (affected != 18) { - printf("\033[31m%d rows affected by last insert statement, but it should be 18\033[0m\n", affected); - } - taos_free_result(result); - // super tables subscription - usleep(1000000); -} - -static int print_result(TAOS_RES* res, int blockFetch) { - TAOS_ROW row = NULL; - int num_fields = taos_num_fields(res); - TAOS_FIELD* fields = taos_fetch_fields(res); - int nRows = 0; - - if (blockFetch) { - int rows = 0; - while ((rows = taos_fetch_block(res, &row))) { - // for (int i = 0; i < rows; i++) { - // char temp[256]; - // taos_print_row(temp, row + i, fields, num_fields); - // puts(temp); - // } - nRows += rows; - } - } else { - while ((row = taos_fetch_row(res))) { - char temp[256] = {0}; - taos_print_row(temp, row, fields, num_fields); - puts(temp); - nRows++; - } - } - - printf("%d rows consumed.\n", nRows); - return nRows; -} - -static void check_row_count(int line, TAOS_RES* res, int expected) { - int actual = print_result(res, expected % 2); - if (actual != expected) { - printf("\033[31mline %d: row count mismatch, expected: %d, actual: %d\033[0m\n", line, expected, actual); - } else { - printf("line %d: %d rows consumed as expected\n", line, actual); - } -} - -static void verify_query(TAOS* taos) { - prepare_data(taos); - - int code = taos_load_table_info(taos, "t0,t1,t2,t3,t4,t5,t6,t7,t8,t9"); - if (code != 0) { - printf("\033[31mfailed to load table info: 0x%08x\033[0m\n", code); - } - - code = taos_validate_sql(taos, "select * from nonexisttable"); - if (code == 0) { - printf("\033[31mimpossible, the table does not exists\033[0m\n"); - } - - code = taos_validate_sql(taos, "select * from meters"); - if (code != 0) { - printf("\033[31mimpossible, the table does exists: 0x%08x\033[0m\n", code); - } - - TAOS_RES* res = taos_query_with_reqid(taos, "select * from meters", genReqid()); - check_row_count(__LINE__, res, 18); - printf("result precision is: %d\n", taos_result_precision(res)); - int c = taos_field_count(res); - printf("field count is: %d\n", c); - int* lengths = taos_fetch_lengths(res); - for (int i = 0; i < c; i++) { - printf("length of column %d is %d\n", i, lengths[i]); - } - taos_free_result(res); - - res = taos_query_with_reqid(taos, "select * from t0", genReqid()); - check_row_count(__LINE__, res, 3); - taos_free_result(res); - - res = taos_query_with_reqid(taos, "select * from nonexisttable", genReqid()); - code = taos_errno(res); - printf("code=%d, error msg=%s\n", code, taos_errstr(res)); - taos_free_result(res); - - res = taos_query_with_reqid(taos, "select * from meters", genReqid()); - taos_stop_query(res); - taos_free_result(res); -} - -void subscribe_callback(TAOS_SUB* tsub, TAOS_RES* res, void* param, int code) { - int rows = print_result(res, *(int*)param); - printf("%d rows consumed in subscribe_callback\n", rows); -} - -static void verify_subscribe(TAOS* taos) { - prepare_data(taos); - - TAOS_SUB* tsub = taos_subscribe(taos, 0, "test", "select * from meters;", NULL, NULL, 0); - TAOS_RES* res = taos_consume(tsub); - check_row_count(__LINE__, res, 18); - - res = taos_consume(tsub); - check_row_count(__LINE__, res, 0); - - TAOS_RES* result; - result = taos_query(taos, "insert into t0 values('2020-01-01 00:02:00.001', 0);"); - taos_free_result(result); - result = taos_query(taos, "insert into t8 values('2020-01-01 00:01:03.000', 0);"); - taos_free_result(result); - res = taos_consume(tsub); - check_row_count(__LINE__, res, 2); - - result = taos_query(taos, "insert into t2 values('2020-01-01 00:01:02.001', 0);"); - taos_free_result(result); - result = taos_query(taos, "insert into t1 values('2020-01-01 00:03:00.001', 0);"); - taos_free_result(result); - res = taos_consume(tsub); - check_row_count(__LINE__, res, 2); - - result = taos_query(taos, "insert into t1 values('2020-01-01 00:03:00.002', 0);"); - taos_free_result(result); - res = taos_consume(tsub); - check_row_count(__LINE__, res, 1); - - // keep progress information and restart subscription - taos_unsubscribe(tsub, 1); - result = taos_query(taos, "insert into t0 values('2020-01-01 00:04:00.000', 0);"); - taos_free_result(result); - tsub = taos_subscribe(taos, 1, "test", "select * from meters;", NULL, NULL, 0); - res = taos_consume(tsub); - check_row_count(__LINE__, res, 24); - - // keep progress information and continue previous subscription - taos_unsubscribe(tsub, 1); - tsub = taos_subscribe(taos, 0, "test", "select * from meters;", NULL, NULL, 0); - res = taos_consume(tsub); - check_row_count(__LINE__, res, 0); - - // don't keep progress information and continue previous subscription - taos_unsubscribe(tsub, 0); - tsub = taos_subscribe(taos, 0, "test", "select * from meters;", NULL, NULL, 0); - res = taos_consume(tsub); - check_row_count(__LINE__, res, 24); - - // single meter subscription - - taos_unsubscribe(tsub, 0); - tsub = taos_subscribe(taos, 0, "test", "select * from t0;", NULL, NULL, 0); - res = taos_consume(tsub); - check_row_count(__LINE__, res, 5); - - res = taos_consume(tsub); - check_row_count(__LINE__, res, 0); - - result = taos_query(taos, "insert into t0 values('2020-01-01 00:04:00.001', 0);"); - taos_free_result(result); - res = taos_consume(tsub); - check_row_count(__LINE__, res, 1); - - taos_unsubscribe(tsub, 0); - - int blockFetch = 0; - tsub = taos_subscribe(taos, 1, "test", "select * from meters;", subscribe_callback, &blockFetch, 1000); - usleep(2000000); - result = taos_query(taos, "insert into t0 values('2020-01-01 00:05:00.001', 0);"); - taos_free_result(result); - usleep(2000000); - taos_unsubscribe(tsub, 0); -} - -void verify_prepare(TAOS* taos) { - TAOS_RES* result = taos_query(taos, "drop database if exists test;"); - taos_free_result(result); - - usleep(100000); - result = taos_query(taos, "create database test;"); - - int code = taos_errno(result); - if (code != 0) { - printf("\033[31mfailed to create database, reason:%s\033[0m\n", taos_errstr(result)); - taos_free_result(result); - return; - } - - taos_free_result(result); - - usleep(100000); - taos_select_db(taos, "test"); - - // create table - const char* sql = - "create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin " - "binary(40), blob nchar(10))"; - result = taos_query_with_reqid(taos, sql, genReqid()); - code = taos_errno(result); - if (code != 0) { - printf("\033[31mfailed to create table, reason:%s\033[0m\n", taos_errstr(result)); - taos_free_result(result); - return; - } - taos_free_result(result); - - // insert 10 records - struct { - int64_t ts; - int8_t b; - int8_t v1; - int16_t v2; - int32_t v4; - int64_t v8; - float f4; - double f8; - char bin[40]; - char blob[80]; - } v = {0}; - - TAOS_STMT* stmt = taos_stmt_init(taos); - TAOS_BIND params[10]; - params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; - params[0].buffer_length = sizeof(v.ts); - params[0].buffer = &v.ts; - params[0].length = ¶ms[0].buffer_length; - params[0].is_null = NULL; - - params[1].buffer_type = TSDB_DATA_TYPE_BOOL; - params[1].buffer_length = sizeof(v.b); - params[1].buffer = &v.b; - params[1].length = ¶ms[1].buffer_length; - params[1].is_null = NULL; - - params[2].buffer_type = TSDB_DATA_TYPE_TINYINT; - params[2].buffer_length = sizeof(v.v1); - params[2].buffer = &v.v1; - params[2].length = ¶ms[2].buffer_length; - params[2].is_null = NULL; - - params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT; - params[3].buffer_length = sizeof(v.v2); - params[3].buffer = &v.v2; - params[3].length = ¶ms[3].buffer_length; - params[3].is_null = NULL; - - params[4].buffer_type = TSDB_DATA_TYPE_INT; - params[4].buffer_length = sizeof(v.v4); - params[4].buffer = &v.v4; - params[4].length = ¶ms[4].buffer_length; - params[4].is_null = NULL; - - params[5].buffer_type = TSDB_DATA_TYPE_BIGINT; - params[5].buffer_length = sizeof(v.v8); - params[5].buffer = &v.v8; - params[5].length = ¶ms[5].buffer_length; - params[5].is_null = NULL; - - params[6].buffer_type = TSDB_DATA_TYPE_FLOAT; - params[6].buffer_length = sizeof(v.f4); - params[6].buffer = &v.f4; - params[6].length = ¶ms[6].buffer_length; - params[6].is_null = NULL; - - params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE; - params[7].buffer_length = sizeof(v.f8); - params[7].buffer = &v.f8; - params[7].length = ¶ms[7].buffer_length; - params[7].is_null = NULL; - - params[8].buffer_type = TSDB_DATA_TYPE_BINARY; - params[8].buffer_length = sizeof(v.bin); - params[8].buffer = v.bin; - params[8].length = ¶ms[8].buffer_length; - params[8].is_null = NULL; - - strcpy(v.blob, "一二三四五六七八九十"); - params[9].buffer_type = TSDB_DATA_TYPE_NCHAR; - params[9].buffer_length = strlen(v.blob); - params[9].buffer = v.blob; - params[9].length = ¶ms[9].buffer_length; - params[9].is_null = NULL; - - int is_null = 1; - - sql = "insert into m1 values(?,?,?,?,?,?,?,?,?,?)"; - code = taos_stmt_prepare(stmt, sql, 0); - if (code != 0) { - printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); - return; - } - v.ts = 1591060628000; - for (int i = 0; i < 10; ++i) { - v.ts += 1; - for (int j = 1; j < 10; ++j) { - params[j].is_null = ((i == j) ? &is_null : 0); - } - v.b = (int8_t)i % 2; - v.v1 = (int8_t)i; - v.v2 = (int16_t)(i * 2); - v.v4 = (int32_t)(i * 4); - v.v8 = (int64_t)(i * 8); - v.f4 = (float)(i * 40); - v.f8 = (double)(i * 80); - for (int j = 0; j < sizeof(v.bin); ++j) { - v.bin[j] = (char)(i + '0'); - } - - taos_stmt_bind_param(stmt, params); - taos_stmt_add_batch(stmt); - } - if (taos_stmt_execute(stmt) != 0) { - printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); - return; - } - taos_stmt_close(stmt); - - // query the records - stmt = taos_stmt_init(taos); - taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0); - v.v1 = 5; - v.v2 = 15; - taos_stmt_bind_param(stmt, params + 2); - if (taos_stmt_execute(stmt) != 0) { - printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); - return; - } - - result = taos_stmt_use_result(stmt); - - TAOS_ROW row; - int rows = 0; - int num_fields = taos_num_fields(result); - TAOS_FIELD* fields = taos_fetch_fields(result); - - // fetch the records row by row - while ((row = taos_fetch_row(result))) { - char temp[256] = {0}; - rows++; - taos_print_row(temp, row, fields, num_fields); - printf("%s\n", temp); - } - - taos_free_result(result); - taos_stmt_close(stmt); -} - -void verify_prepare2(TAOS* taos) { - TAOS_RES* result = taos_query(taos, "drop database if exists test;"); - taos_free_result(result); - usleep(100000); - result = taos_query(taos, "create database test;"); - - int code = taos_errno(result); - if (code != 0) { - printf("\033[31mfailed to create database, reason:%s\033[0m\n", taos_errstr(result)); - taos_free_result(result); - return; - } - taos_free_result(result); - - usleep(100000); - taos_select_db(taos, "test"); - - // create table - const char* sql = - "create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin " - "binary(40), blob nchar(10))"; - result = taos_query(taos, sql); - code = taos_errno(result); - if (code != 0) { - printf("\033[31mfailed to create table, reason:%s\033[0m\n", taos_errstr(result)); - taos_free_result(result); - return; - } - taos_free_result(result); - - // insert 10 records - struct { - int64_t ts[10]; - int8_t b[10]; - int8_t v1[10]; - int16_t v2[10]; - int32_t v4[10]; - int64_t v8[10]; - float f4[10]; - double f8[10]; - char bin[10][40]; - char blob[10][80]; - } v; - - int32_t* t8_len = malloc(sizeof(int32_t) * 10); - int32_t* t16_len = malloc(sizeof(int32_t) * 10); - int32_t* t32_len = malloc(sizeof(int32_t) * 10); - int32_t* t64_len = malloc(sizeof(int32_t) * 10); - int32_t* float_len = malloc(sizeof(int32_t) * 10); - int32_t* double_len = malloc(sizeof(int32_t) * 10); - int32_t* bin_len = malloc(sizeof(int32_t) * 10); - int32_t* blob_len = malloc(sizeof(int32_t) * 10); - - TAOS_STMT* stmt = taos_stmt_init(taos); - TAOS_MULTI_BIND params[10]; - char is_null[10] = {0}; - - params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; - params[0].buffer_length = sizeof(v.ts[0]); - params[0].buffer = v.ts; - params[0].length = t64_len; - params[0].is_null = is_null; - params[0].num = 10; - - params[1].buffer_type = TSDB_DATA_TYPE_BOOL; - params[1].buffer_length = sizeof(v.b[0]); - params[1].buffer = v.b; - params[1].length = t8_len; - params[1].is_null = is_null; - params[1].num = 10; - - params[2].buffer_type = TSDB_DATA_TYPE_TINYINT; - params[2].buffer_length = sizeof(v.v1[0]); - params[2].buffer = v.v1; - params[2].length = t8_len; - params[2].is_null = is_null; - params[2].num = 10; - - params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT; - params[3].buffer_length = sizeof(v.v2[0]); - params[3].buffer = v.v2; - params[3].length = t16_len; - params[3].is_null = is_null; - params[3].num = 10; - - params[4].buffer_type = TSDB_DATA_TYPE_INT; - params[4].buffer_length = sizeof(v.v4[0]); - params[4].buffer = v.v4; - params[4].length = t32_len; - params[4].is_null = is_null; - params[4].num = 10; - - params[5].buffer_type = TSDB_DATA_TYPE_BIGINT; - params[5].buffer_length = sizeof(v.v8[0]); - params[5].buffer = v.v8; - params[5].length = t64_len; - params[5].is_null = is_null; - params[5].num = 10; - - params[6].buffer_type = TSDB_DATA_TYPE_FLOAT; - params[6].buffer_length = sizeof(v.f4[0]); - params[6].buffer = v.f4; - params[6].length = float_len; - params[6].is_null = is_null; - params[6].num = 10; - - params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE; - params[7].buffer_length = sizeof(v.f8[0]); - params[7].buffer = v.f8; - params[7].length = double_len; - params[7].is_null = is_null; - params[7].num = 10; - - params[8].buffer_type = TSDB_DATA_TYPE_BINARY; - params[8].buffer_length = sizeof(v.bin[0]); - params[8].buffer = v.bin; - params[8].length = bin_len; - params[8].is_null = is_null; - params[8].num = 10; - - params[9].buffer_type = TSDB_DATA_TYPE_NCHAR; - params[9].buffer_length = sizeof(v.blob[0]); - params[9].buffer = v.blob; - params[9].length = blob_len; - params[9].is_null = is_null; - params[9].num = 10; - - sql = "insert into ? (ts, b, v1, v2, v4, v8, f4, f8, bin, blob) values(?,?,?,?,?,?,?,?,?,?)"; - code = taos_stmt_prepare(stmt, sql, 0); - if (code != 0) { - printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); - return; - } - - code = taos_stmt_set_tbname(stmt, "m1"); - if (code != 0) { - printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); - return; - } - - int64_t ts = 1591060628000; - for (int i = 0; i < 10; ++i) { - v.ts[i] = ts++; - is_null[i] = 0; - - v.b[i] = (int8_t)i % 2; - v.v1[i] = (int8_t)i; - v.v2[i] = (int16_t)(i * 2); - v.v4[i] = (int32_t)(i * 4); - v.v8[i] = (int64_t)(i * 8); - v.f4[i] = (float)(i * 40); - v.f8[i] = (double)(i * 80); - for (int j = 0; j < sizeof(v.bin[0]); ++j) { - v.bin[i][j] = (char)(i + '0'); - } - strcpy(v.blob[i], "一二三四五六七八九十"); - - t8_len[i] = sizeof(int8_t); - t16_len[i] = sizeof(int16_t); - t32_len[i] = sizeof(int32_t); - t64_len[i] = sizeof(int64_t); - float_len[i] = sizeof(float); - double_len[i] = sizeof(double); - bin_len[i] = sizeof(v.bin[0]); - blob_len[i] = (int32_t)strlen(v.blob[i]); - } - - taos_stmt_bind_param_batch(stmt, params); - taos_stmt_add_batch(stmt); - - if (taos_stmt_execute(stmt) != 0) { - printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); - return; - } - - taos_stmt_close(stmt); - - // query the records - stmt = taos_stmt_init(taos); - taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0); - TAOS_BIND qparams[2]; - - int8_t v1 = 5; - int16_t v2 = 15; - qparams[0].buffer_type = TSDB_DATA_TYPE_TINYINT; - qparams[0].buffer_length = sizeof(v1); - qparams[0].buffer = &v1; - qparams[0].length = &qparams[0].buffer_length; - qparams[0].is_null = NULL; - - qparams[1].buffer_type = TSDB_DATA_TYPE_SMALLINT; - qparams[1].buffer_length = sizeof(v2); - qparams[1].buffer = &v2; - qparams[1].length = &qparams[1].buffer_length; - qparams[1].is_null = NULL; - - taos_stmt_bind_param(stmt, qparams); - if (taos_stmt_execute(stmt) != 0) { - printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); - return; - } - - result = taos_stmt_use_result(stmt); - - TAOS_ROW row; - int rows = 0; - int num_fields = taos_num_fields(result); - TAOS_FIELD* fields = taos_fetch_fields(result); - - // fetch the records row by row - while ((row = taos_fetch_row(result))) { - char temp[256] = {0}; - rows++; - taos_print_row(temp, row, fields, num_fields); - printf("%s\n", temp); - } - - taos_free_result(result); - taos_stmt_close(stmt); - - free(t8_len); - free(t16_len); - free(t32_len); - free(t64_len); - free(float_len); - free(double_len); - free(bin_len); - free(blob_len); -} - -void verify_prepare3(TAOS* taos) { - TAOS_RES* result = taos_query(taos, "drop database if exists test;"); - taos_free_result(result); - usleep(100000); - result = taos_query(taos, "create database test;"); - - int code = taos_errno(result); - if (code != 0) { - printf("\033[31mfailed to create database, reason:%s\033[0m\n", taos_errstr(result)); - taos_free_result(result); - return; - } - taos_free_result(result); - - usleep(100000); - taos_select_db(taos, "test"); - - // create table - const char* sql = - "create stable st1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin " - "binary(40), blob nchar(10)) tags (id1 int, id2 binary(40))"; - result = taos_query(taos, sql); - code = taos_errno(result); - if (code != 0) { - printf("\033[31mfailed to create table, reason:%s\033[0m\n", taos_errstr(result)); - taos_free_result(result); - return; - } - taos_free_result(result); - - TAOS_BIND tags[2]; - - int32_t id1 = 1; - char id2[40] = "abcdefghijklmnopqrstuvwxyz0123456789"; - uintptr_t id2_len = strlen(id2); - - tags[0].buffer_type = TSDB_DATA_TYPE_INT; - tags[0].buffer_length = sizeof(int); - tags[0].buffer = &id1; - tags[0].length = NULL; - tags[0].is_null = NULL; - - tags[1].buffer_type = TSDB_DATA_TYPE_BINARY; - tags[1].buffer_length = sizeof(id2); - tags[1].buffer = id2; - tags[1].length = &id2_len; - tags[1].is_null = NULL; - - // insert 10 records - struct { - int64_t ts[10]; - int8_t b[10]; - int8_t v1[10]; - int16_t v2[10]; - int32_t v4[10]; - int64_t v8[10]; - float f4[10]; - double f8[10]; - char bin[10][40]; - char blob[10][80]; - } v; - - int32_t* t8_len = malloc(sizeof(int32_t) * 10); - int32_t* t16_len = malloc(sizeof(int32_t) * 10); - int32_t* t32_len = malloc(sizeof(int32_t) * 10); - int32_t* t64_len = malloc(sizeof(int32_t) * 10); - int32_t* float_len = malloc(sizeof(int32_t) * 10); - int32_t* double_len = malloc(sizeof(int32_t) * 10); - int32_t* bin_len = malloc(sizeof(int32_t) * 10); - int32_t* blob_len = malloc(sizeof(int32_t) * 10); - - TAOS_STMT* stmt = taos_stmt_init(taos); - TAOS_MULTI_BIND params[10]; - char is_null[10] = {0}; - - params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; - params[0].buffer_length = sizeof(v.ts[0]); - params[0].buffer = v.ts; - params[0].length = t64_len; - params[0].is_null = is_null; - params[0].num = 10; - - params[1].buffer_type = TSDB_DATA_TYPE_BOOL; - params[1].buffer_length = sizeof(v.b[0]); - params[1].buffer = v.b; - params[1].length = t8_len; - params[1].is_null = is_null; - params[1].num = 10; - - params[2].buffer_type = TSDB_DATA_TYPE_TINYINT; - params[2].buffer_length = sizeof(v.v1[0]); - params[2].buffer = v.v1; - params[2].length = t8_len; - params[2].is_null = is_null; - params[2].num = 10; - - params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT; - params[3].buffer_length = sizeof(v.v2[0]); - params[3].buffer = v.v2; - params[3].length = t16_len; - params[3].is_null = is_null; - params[3].num = 10; - - params[4].buffer_type = TSDB_DATA_TYPE_INT; - params[4].buffer_length = sizeof(v.v4[0]); - params[4].buffer = v.v4; - params[4].length = t32_len; - params[4].is_null = is_null; - params[4].num = 10; - - params[5].buffer_type = TSDB_DATA_TYPE_BIGINT; - params[5].buffer_length = sizeof(v.v8[0]); - params[5].buffer = v.v8; - params[5].length = t64_len; - params[5].is_null = is_null; - params[5].num = 10; - - params[6].buffer_type = TSDB_DATA_TYPE_FLOAT; - params[6].buffer_length = sizeof(v.f4[0]); - params[6].buffer = v.f4; - params[6].length = float_len; - params[6].is_null = is_null; - params[6].num = 10; - - params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE; - params[7].buffer_length = sizeof(v.f8[0]); - params[7].buffer = v.f8; - params[7].length = double_len; - params[7].is_null = is_null; - params[7].num = 10; - - params[8].buffer_type = TSDB_DATA_TYPE_BINARY; - params[8].buffer_length = sizeof(v.bin[0]); - params[8].buffer = v.bin; - params[8].length = bin_len; - params[8].is_null = is_null; - params[8].num = 10; - - params[9].buffer_type = TSDB_DATA_TYPE_NCHAR; - params[9].buffer_length = sizeof(v.blob[0]); - params[9].buffer = v.blob; - params[9].length = blob_len; - params[9].is_null = is_null; - params[9].num = 10; - - sql = "insert into ? using st1 tags(?,?) values(?,?,?,?,?,?,?,?,?,?)"; - code = taos_stmt_prepare(stmt, sql, 0); - if (code != 0) { - printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); - return; - } - - code = taos_stmt_set_tbname_tags(stmt, "m1", tags); - if (code != 0) { - printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); - return; - } - - int64_t ts = 1591060628000; - for (int i = 0; i < 10; ++i) { - v.ts[i] = ts++; - is_null[i] = 0; - - v.b[i] = (int8_t)i % 2; - v.v1[i] = (int8_t)i; - v.v2[i] = (int16_t)(i * 2); - v.v4[i] = (int32_t)(i * 4); - v.v8[i] = (int64_t)(i * 8); - v.f4[i] = (float)(i * 40); - v.f8[i] = (double)(i * 80); - for (int j = 0; j < sizeof(v.bin[0]); ++j) { - v.bin[i][j] = (char)(i + '0'); - } - strcpy(v.blob[i], "一二三四五六七八九十"); - - t8_len[i] = sizeof(int8_t); - t16_len[i] = sizeof(int16_t); - t32_len[i] = sizeof(int32_t); - t64_len[i] = sizeof(int64_t); - float_len[i] = sizeof(float); - double_len[i] = sizeof(double); - bin_len[i] = sizeof(v.bin[0]); - blob_len[i] = (int32_t)strlen(v.blob[i]); - } - - taos_stmt_bind_param_batch(stmt, params); - taos_stmt_add_batch(stmt); - - if (taos_stmt_execute(stmt) != 0) { - printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); - return; - } - taos_stmt_close(stmt); - - // query the records - stmt = taos_stmt_init(taos); - taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0); - - TAOS_BIND qparams[2]; - - int8_t v1 = 5; - int16_t v2 = 15; - qparams[0].buffer_type = TSDB_DATA_TYPE_TINYINT; - qparams[0].buffer_length = sizeof(v1); - qparams[0].buffer = &v1; - qparams[0].length = &qparams[0].buffer_length; - qparams[0].is_null = NULL; - - qparams[1].buffer_type = TSDB_DATA_TYPE_SMALLINT; - qparams[1].buffer_length = sizeof(v2); - qparams[1].buffer = &v2; - qparams[1].length = &qparams[1].buffer_length; - qparams[1].is_null = NULL; - - taos_stmt_bind_param(stmt, qparams); - if (taos_stmt_execute(stmt) != 0) { - printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); - return; - } - - result = taos_stmt_use_result(stmt); - - TAOS_ROW row; - int rows = 0; - int num_fields = taos_num_fields(result); - TAOS_FIELD* fields = taos_fetch_fields(result); - - // fetch the records row by row - while ((row = taos_fetch_row(result))) { - char temp[256] = {0}; - rows++; - taos_print_row(temp, row, fields, num_fields); - printf("%s\n", temp); - } - - taos_free_result(result); - taos_stmt_close(stmt); - - free(t8_len); - free(t16_len); - free(t32_len); - free(t64_len); - free(float_len); - free(double_len); - free(bin_len); - free(blob_len); -} - -void retrieve_callback(void* param, TAOS_RES* tres, int numOfRows) { - if (numOfRows > 0) { - printf("%d rows async retrieved\n", numOfRows); - taos_fetch_rows_a(tres, retrieve_callback, param); - } else { - if (numOfRows < 0) { - printf("\033[31masync retrieve failed, code: %d\033[0m\n", numOfRows); - } else { - printf("async retrieve completed\n"); - } - taos_free_result(tres); - } -} - -void select_callback(void* param, TAOS_RES* tres, int code) { - if (code == 0 && tres) { - taos_fetch_rows_a(tres, retrieve_callback, param); - } else { - printf("\033[31masync select failed, code: %d\033[0m\n", code); - } -} - -void verify_async(TAOS* taos) { - prepare_data(taos); - taos_query_a(taos, "select * from meters", select_callback, NULL); - usleep(1000000); -} - -void stream_callback(void* param, TAOS_RES* res, TAOS_ROW row) { - if (res == NULL || row == NULL) { - return; - } - - int num_fields = taos_num_fields(res); - TAOS_FIELD* fields = taos_fetch_fields(res); - - printf("got one row from stream_callback\n"); - char temp[256] = {0}; - taos_print_row(temp, row, fields, num_fields); - puts(temp); -} - -void verify_stream(TAOS* taos) { - prepare_data(taos); - TAOS_STREAM* strm = - taos_open_stream(taos, "select count(*) from meters interval(1m)", stream_callback, 0, NULL, NULL); - printf("waiting for stream data\n"); - usleep(100000); - TAOS_RES* result = taos_query(taos, "insert into t0 values(now, 0)(now+5s,1)(now+10s, 2);"); - taos_free_result(result); - usleep(200000000); - taos_close_stream(strm); -} - -int32_t verify_schema_less(TAOS* taos) { - TAOS_RES* result; - result = taos_query(taos, "drop database if exists test;"); - taos_free_result(result); - usleep(100000); - result = taos_query(taos, "create database test precision 'us' update 1;"); - taos_free_result(result); - usleep(100000); - - taos_select_db(taos, "test"); - result = taos_query(taos, "create stable ste(ts timestamp, f int) tags(t1 bigint)"); - taos_free_result(result); - usleep(100000); - - int code = 0; - - char* lines[] = { - "st,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns", - "st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000ns", - "ste,t2=5f64,t3=L\"ste\" c1=true,c2=4i64,c3=\"iam\" 1626056811823316532ns", - "st,t1=4i64,t2=5f64,t3=\"t4\" c1=3i64,c3=L\"passitagain\",c2=true,c4=5f64 1626006833642000000ns", - "ste,t2=5f64,t3=L\"ste2\" c3=\"iamszhou\",c4=false 1626056811843316532ns", - "ste,t2=5f64,t3=L\"ste2\" c3=\"iamszhou\",c4=false,c5=32i8,c6=64i16,c7=32i32,c8=88.88f32 1626056812843316532ns", - "st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64,c6=7u64 " - "1626006933640000000ns", - "stf,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64,c6=7u64 " - "1626006933640000000ns", - "stf,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 " - "1626006933641000000ns"}; - - code = taos_insert_lines(taos, lines, sizeof(lines) / sizeof(char*)); - - char* lines2[] = { - "stg,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns", - "stg,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000ns"}; - code = taos_insert_lines(taos, &lines2[0], 1); - code = taos_insert_lines(taos, &lines2[1], 1); - - char* lines3[] = { - "sth,t1=4i64,t2=5f64,t4=5f64,ID=\"childtable\" c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 " - "1626006933641ms", - "sth,t1=4i64,t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933654ms"}; - code = taos_insert_lines(taos, lines3, 2); - - char* lines4[] = {"st123456,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns", - "dgtyqodr,t2=5f64,t3=L\"ste\" c1=tRue,c2=4i64,c3=\"iam\" 1626056811823316532ns"}; - code = taos_insert_lines(taos, lines4, 2); - - char* lines5[] = { - "zqlbgs,id=\"zqlbgs_39302_21680\",t0=f,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=11." - "12345f32,t6=22.123456789f64,t7=\"binaryTagValue\",t8=L\"ncharTagValue\" " - "c0=f,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=11.12345f32,c6=22.123456789f64,c7=" - "\"binaryColValue\",c8=L\"ncharColValue\",c9=7u64 1626006833639000000ns", - "zqlbgs,t9=f,id=\"zqlbgs_39302_21680\",t0=f,t1=127i8,t11=127i8,t2=32767i16,t3=2147483647i32,t4=" - "9223372036854775807i64,t5=11.12345f32,t6=22.123456789f64,t7=\"binaryTagValue\",t8=L\"ncharTagValue\",t10=" - "L\"ncharTagValue\" " - "c10=f,c0=f,c1=127i8,c12=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=11.12345f32,c6=22." - "123456789f64,c7=\"binaryColValue\",c8=L\"ncharColValue\",c9=7u64,c11=L\"ncharColValue\" 1626006833639000000ns"}; - code = taos_insert_lines(taos, &lines5[0], 1); - code = taos_insert_lines(taos, &lines5[1], 1); - - char* lines6[] = {"st123456,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns", - "dgtyqodr,t2=5f64,t3=L\"ste\" c1=tRue,c2=4i64,c3=\"iam\" 1626056811823316532ns"}; - code = taos_insert_lines(taos, lines6, 2); - return (code); -} - -void verify_telnet_insert(TAOS* taos) { - TAOS_RES* result; - - result = taos_query(taos, "drop database if exists db;"); - taos_free_result(result); - usleep(100000); - result = taos_query(taos, "create database db precision 'ms';"); - taos_free_result(result); - usleep(100000); - - (void)taos_select_db(taos, "db"); - int32_t code = 0; - - /* metric */ - char* lines0[] = { - "stb0_0 1626006833639000000ns 4i8 host=\"host0\",interface=\"eth0\"", - "stb0_1 1626006833639000000ns 4i8 host=\"host0\",interface=\"eth0\"", - "stb0_2 1626006833639000000ns 4i8 host=\"host0\",interface=\"eth0\"", - }; - code = taos_insert_telnet_lines(taos, lines0, 3); - if (code) { - printf("lines0 code: %d, %s.\n", code, tstrerror(code)); - } - - /* timestamp */ - char* lines1[] = { - "stb1 1626006833s 1i8 host=\"host0\"", "stb1 1626006833639000000ns 2i8 host=\"host0\"", - "stb1 1626006833640000us 3i8 host=\"host0\"", "stb1 1626006833641123 4i8 host=\"host0\"", - "stb1 1626006833651ms 5i8 host=\"host0\"", "stb1 0 6i8 host=\"host0\"", - }; - code = taos_insert_telnet_lines(taos, lines1, 6); - if (code) { - printf("lines1 code: %d, %s.\n", code, tstrerror(code)); - } - - /* metric value */ - // tinyin - char* lines2_0[] = {"stb2_0 1626006833651ms -127i8 host=\"host0\"", "stb2_0 1626006833652ms 127i8 host=\"host0\""}; - code = taos_insert_telnet_lines(taos, lines2_0, 2); - if (code) { - printf("lines2_0 code: %d, %s.\n", code, tstrerror(code)); - } - - // smallint - char* lines2_1[] = {"stb2_1 1626006833651ms -32767i16 host=\"host0\"", - "stb2_1 1626006833652ms 32767i16 host=\"host0\""}; - code = taos_insert_telnet_lines(taos, lines2_1, 2); - if (code) { - printf("lines2_1 code: %d, %s.\n", code, tstrerror(code)); - } - - // int - char* lines2_2[] = {"stb2_2 1626006833651ms -2147483647i32 host=\"host0\"", - "stb2_2 1626006833652ms 2147483647i32 host=\"host0\""}; - code = taos_insert_telnet_lines(taos, lines2_2, 2); - if (code) { - printf("lines2_2 code: %d, %s.\n", code, tstrerror(code)); - } - - // bigint - char* lines2_3[] = {"stb2_3 1626006833651ms -9223372036854775807i64 host=\"host0\"", - "stb2_3 1626006833652ms 9223372036854775807i64 host=\"host0\""}; - code = taos_insert_telnet_lines(taos, lines2_3, 2); - if (code) { - printf("lines2_3 code: %d, %s.\n", code, tstrerror(code)); - } - - // float - char* lines2_4[] = { - "stb2_4 1626006833610ms 3f32 host=\"host0\"", "stb2_4 1626006833620ms -3f32 host=\"host0\"", - "stb2_4 1626006833630ms 3.4f32 host=\"host0\"", "stb2_4 1626006833640ms -3.4f32 host=\"host0\"", - "stb2_4 1626006833650ms 3.4E10f32 host=\"host0\"", "stb2_4 1626006833660ms -3.4e10f32 host=\"host0\"", - "stb2_4 1626006833670ms 3.4E+2f32 host=\"host0\"", "stb2_4 1626006833680ms -3.4e-2f32 host=\"host0\"", - "stb2_4 1626006833690ms 3.15 host=\"host0\"", "stb2_4 1626006833700ms 3.4E38f32 host=\"host0\"", - "stb2_4 1626006833710ms -3.4E38f32 host=\"host0\""}; - code = taos_insert_telnet_lines(taos, lines2_4, 11); - if (code) { - printf("lines2_4 code: %d, %s.\n", code, tstrerror(code)); - } - - // double - char* lines2_5[] = { - "stb2_5 1626006833610ms 3f64 host=\"host0\"", "stb2_5 1626006833620ms -3f64 host=\"host0\"", - "stb2_5 1626006833630ms 3.4f64 host=\"host0\"", "stb2_5 1626006833640ms -3.4f64 host=\"host0\"", - "stb2_5 1626006833650ms 3.4E10f64 host=\"host0\"", "stb2_5 1626006833660ms -3.4e10f64 host=\"host0\"", - "stb2_5 1626006833670ms 3.4E+2f64 host=\"host0\"", "stb2_5 1626006833680ms -3.4e-2f64 host=\"host0\"", - "stb2_5 1626006833690ms 1.7E308f64 host=\"host0\"", "stb2_5 1626006833700ms -1.7E308f64 host=\"host0\""}; - code = taos_insert_telnet_lines(taos, lines2_5, 10); - if (code) { - printf("lines2_5 code: %d, %s.\n", code, tstrerror(code)); - } - - // bool - char* lines2_6[] = {"stb2_6 1626006833610ms t host=\"host0\"", "stb2_6 1626006833620ms T host=\"host0\"", - "stb2_6 1626006833630ms true host=\"host0\"", "stb2_6 1626006833640ms True host=\"host0\"", - "stb2_6 1626006833650ms TRUE host=\"host0\"", "stb2_6 1626006833660ms f host=\"host0\"", - "stb2_6 1626006833670ms F host=\"host0\"", "stb2_6 1626006833680ms false host=\"host0\"", - "stb2_6 1626006833690ms False host=\"host0\"", "stb2_6 1626006833700ms FALSE host=\"host0\""}; - code = taos_insert_telnet_lines(taos, lines2_6, 10); - if (code) { - printf("lines2_6 code: %d, %s.\n", code, tstrerror(code)); - } - - // binary - char* lines2_7[] = {"stb2_7 1626006833610ms \"binary_val.!@#$%^&*\" host=\"host0\"", - "stb2_7 1626006833620ms \"binary_val.:;,./?|+-=\" host=\"host0\"", - "stb2_7 1626006833630ms \"binary_val.()[]{}<>\" host=\"host0\""}; - code = taos_insert_telnet_lines(taos, lines2_7, 3); - if (code) { - printf("lines2_7 code: %d, %s.\n", code, tstrerror(code)); - } - - // nchar - char* lines2_8[] = { - "stb2_8 1626006833610ms L\"nchar_val数值一\" host=\"host0\"", - "stb2_8 1626006833620ms L\"nchar_val数值二\" host=\"host0\"", - }; - code = taos_insert_telnet_lines(taos, lines2_8, 2); - if (code) { - printf("lines2_8 code: %d, %s.\n", code, tstrerror(code)); - } - - /* tags */ - // tag value types - char* lines3_0[] = { - "stb3_0 1626006833610ms 1 " - "t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=3.4E38f32,t6=1.7E308f64,t7=true,t8=\"binary_" - "val_1\",t9=L\"标签值1\"", - "stb3_0 1626006833610ms 2 " - "t1=-127i8,t2=-32767i16,t3=-2147483647i32,t4=-9223372036854775807i64,t5=-3.4E38f32,t6=-1.7E308f64,t7=false,t8=" - "\"binary_val_2\",t9=L\"标签值2\""}; - code = taos_insert_telnet_lines(taos, lines3_0, 2); - if (code) { - printf("lines3_0 code: %d, %s.\n", code, tstrerror(code)); - } - - // tag ID as child table name - char* lines3_1[] = {"stb3_1 1626006833610ms 1 id=\"child_table1\",host=\"host1\"", - "stb3_1 1626006833610ms 2 host=\"host2\",iD=\"child_table2\"", - "stb3_1 1626006833610ms 3 ID=\"child_table3\",host=\"host3\""}; - code = taos_insert_telnet_lines(taos, lines3_1, 3); - if (code) { - printf("lines3_1 code: %d, %s.\n", code, tstrerror(code)); - } - - return; -} - -void verify_json_insert(TAOS* taos) { - TAOS_RES* result; - - result = taos_query(taos, "drop database if exists db;"); - taos_free_result(result); - usleep(100000); - result = taos_query(taos, "create database db precision 'ms';"); - taos_free_result(result); - usleep(100000); - - (void)taos_select_db(taos, "db"); - int32_t code = 0; - - char* message = - "{ \ - \"metric\":\"cpu_load_0\", \ - \"timestamp\": 1626006833610123, \ - \"value\": 55.5, \ - \"tags\": \ - { \ - \"host\": \"ubuntu\", \ - \"interface1\": \"eth0\", \ - \"Id\": \"tb0\" \ - } \ - }"; - - code = taos_insert_json_payload(taos, message); - if (code) { - printf("payload_0 code: %d, %s.\n", code, tstrerror(code)); - } - - char* message1 = - "[ \ - { \ - \"metric\":\"cpu_load_1\", \ - \"timestamp\": 1626006833610123, \ - \"value\": 55.5, \ - \"tags\": \ - { \ - \"host\": \"ubuntu\", \ - \"interface\": \"eth1\", \ - \"Id\": \"tb1\" \ - } \ - }, \ - { \ - \"metric\":\"cpu_load_2\", \ - \"timestamp\": 1626006833610123, \ - \"value\": 55.5, \ - \"tags\": \ - { \ - \"host\": \"ubuntu\", \ - \"interface\": \"eth2\", \ - \"Id\": \"tb2\" \ - } \ - } \ - ]"; - - code = taos_insert_json_payload(taos, message1); - if (code) { - printf("payload_1 code: %d, %s.\n", code, tstrerror(code)); - } - - char* message2 = - "[ \ - { \ - \"metric\":\"cpu_load_3\", \ - \"timestamp\": \ - { \ - \"value\": 1626006833610123, \ - \"type\": \"us\" \ - }, \ - \"value\": \ - { \ - \"value\": 55, \ - \"type\": \"int\" \ - }, \ - \"tags\": \ - { \ - \"host\": \ - { \ - \"value\": \"ubuntu\", \ - \"type\": \"binary\" \ - }, \ - \"interface\": \ - { \ - \"value\": \"eth3\", \ - \"type\": \"nchar\" \ - }, \ - \"ID\": \"tb3\", \ - \"port\": \ - { \ - \"value\": 4040, \ - \"type\": \"int\" \ - } \ - } \ - }, \ - { \ - \"metric\":\"cpu_load_4\", \ - \"timestamp\": 1626006833610123, \ - \"value\": 66.6, \ - \"tags\": \ - { \ - \"host\": \"ubuntu\", \ - \"interface\": \"eth4\", \ - \"Id\": \"tb4\" \ - } \ - } \ - ]"; - code = taos_insert_json_payload(taos, message2); - if (code) { - printf("payload_2 code: %d, %s.\n", code, tstrerror(code)); - } - - cJSON *payload, *tags; - char* payload_str; - - /* Default format */ - // number - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb0_0"); - cJSON_AddNumberToObject(payload, "timestamp", 1626006833610123); - cJSON_AddNumberToObject(payload, "value", 10); - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload0_0 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // true - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb0_1"); - cJSON_AddNumberToObject(payload, "timestamp", 1626006833610123); - cJSON_AddTrueToObject(payload, "value"); - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload0_1 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // false - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb0_2"); - cJSON_AddNumberToObject(payload, "timestamp", 1626006833610123); - cJSON_AddFalseToObject(payload, "value"); - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload0_2 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // string - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb0_3"); - cJSON_AddNumberToObject(payload, "timestamp", 1626006833610123); - cJSON_AddStringToObject(payload, "value", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload0_3 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // timestamp 0 -> current time - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb0_4"); - cJSON_AddNumberToObject(payload, "timestamp", 0); - cJSON_AddNumberToObject(payload, "value", 123); - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload0_4 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // ID - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb0_5"); - cJSON_AddNumberToObject(payload, "timestamp", 0); - cJSON_AddNumberToObject(payload, "value", 123); - tags = cJSON_CreateObject(); - cJSON_AddStringToObject(tags, "ID", "tb0_5"); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddStringToObject(tags, "iD", "tb000"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddStringToObject(tags, "id", "tb555"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload0_5 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - /* Nested format */ - // timestamp - cJSON* timestamp; - // seconds - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb1_0"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 1626006833); - cJSON_AddStringToObject(timestamp, "type", "s"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - cJSON_AddNumberToObject(payload, "value", 10); - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload1_0 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // milleseconds - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb1_1"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 1626006833610); - cJSON_AddStringToObject(timestamp, "type", "ms"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - cJSON_AddNumberToObject(payload, "value", 10); - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload1_1 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // microseconds - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb1_2"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 1626006833610123); - cJSON_AddStringToObject(timestamp, "type", "us"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - cJSON_AddNumberToObject(payload, "value", 10); - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload1_2 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // nanoseconds - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb1_3"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 1626006833610123321); - cJSON_AddStringToObject(timestamp, "type", "ns"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - cJSON_AddNumberToObject(payload, "value", 10); - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload1_3 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // now - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb1_4"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 0); - cJSON_AddStringToObject(timestamp, "type", "ns"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - cJSON_AddNumberToObject(payload, "value", 10); - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload1_4 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // metric value - cJSON* metric_val; - // bool - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb2_0"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 1626006833); - cJSON_AddStringToObject(timestamp, "type", "s"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - metric_val = cJSON_CreateObject(); - cJSON_AddTrueToObject(metric_val, "value"); - cJSON_AddStringToObject(metric_val, "type", "bool"); - cJSON_AddItemToObject(payload, "value", metric_val); - - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload2_0 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // tinyint - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb2_1"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 1626006833); - cJSON_AddStringToObject(timestamp, "type", "s"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - metric_val = cJSON_CreateObject(); - cJSON_AddNumberToObject(metric_val, "value", 127); - cJSON_AddStringToObject(metric_val, "type", "tinyint"); - cJSON_AddItemToObject(payload, "value", metric_val); - - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload2_1 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // smallint - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb2_2"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 1626006833); - cJSON_AddStringToObject(timestamp, "type", "s"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - metric_val = cJSON_CreateObject(); - cJSON_AddNumberToObject(metric_val, "value", 32767); - cJSON_AddStringToObject(metric_val, "type", "smallint"); - cJSON_AddItemToObject(payload, "value", metric_val); - - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload2_2 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // int - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb2_3"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 1626006833); - cJSON_AddStringToObject(timestamp, "type", "s"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - metric_val = cJSON_CreateObject(); - cJSON_AddNumberToObject(metric_val, "value", 2147483647); - cJSON_AddStringToObject(metric_val, "type", "int"); - cJSON_AddItemToObject(payload, "value", metric_val); - - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload2_3 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // bigint - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb2_4"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 1626006833); - cJSON_AddStringToObject(timestamp, "type", "s"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - metric_val = cJSON_CreateObject(); - cJSON_AddNumberToObject(metric_val, "value", 9223372036854775807); - cJSON_AddStringToObject(metric_val, "type", "bigint"); - cJSON_AddItemToObject(payload, "value", metric_val); - - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload2_4 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // float - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb2_5"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 1626006833); - cJSON_AddStringToObject(timestamp, "type", "s"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - metric_val = cJSON_CreateObject(); - cJSON_AddNumberToObject(metric_val, "value", 11.12345); - cJSON_AddStringToObject(metric_val, "type", "float"); - cJSON_AddItemToObject(payload, "value", metric_val); - - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload2_5 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // double - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb2_6"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 1626006833); - cJSON_AddStringToObject(timestamp, "type", "s"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - metric_val = cJSON_CreateObject(); - cJSON_AddNumberToObject(metric_val, "value", 22.123456789); - cJSON_AddStringToObject(metric_val, "type", "double"); - cJSON_AddItemToObject(payload, "value", metric_val); - - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload2_6 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // binary - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb2_7"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 1626006833); - cJSON_AddStringToObject(timestamp, "type", "s"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - metric_val = cJSON_CreateObject(); - cJSON_AddStringToObject(metric_val, "value", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddStringToObject(metric_val, "type", "binary"); - cJSON_AddItemToObject(payload, "value", metric_val); - - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload2_7 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // nchar - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb2_8"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 1626006833); - cJSON_AddStringToObject(timestamp, "type", "s"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - metric_val = cJSON_CreateObject(); - cJSON_AddStringToObject(metric_val, "value", "你好"); - cJSON_AddStringToObject(metric_val, "type", "nchar"); - cJSON_AddItemToObject(payload, "value", metric_val); - - tags = cJSON_CreateObject(); - cJSON_AddTrueToObject(tags, "t1"); - cJSON_AddFalseToObject(tags, "t2"); - cJSON_AddNumberToObject(tags, "t3", 10); - cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"); - cJSON_AddItemToObject(payload, "tags", tags); - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload2_8 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); - - // tag value - cJSON* tag; - - payload = cJSON_CreateObject(); - cJSON_AddStringToObject(payload, "metric", "stb3_0"); - - timestamp = cJSON_CreateObject(); - cJSON_AddNumberToObject(timestamp, "value", 1626006833); - cJSON_AddStringToObject(timestamp, "type", "s"); - cJSON_AddItemToObject(payload, "timestamp", timestamp); - - metric_val = cJSON_CreateObject(); - cJSON_AddStringToObject(metric_val, "value", "hello"); - cJSON_AddStringToObject(metric_val, "type", "nchar"); - cJSON_AddItemToObject(payload, "value", metric_val); - - tags = cJSON_CreateObject(); - - tag = cJSON_CreateObject(); - cJSON_AddTrueToObject(tag, "value"); - cJSON_AddStringToObject(tag, "type", "bool"); - cJSON_AddItemToObject(tags, "t1", tag); - - tag = cJSON_CreateObject(); - cJSON_AddFalseToObject(tag, "value"); - cJSON_AddStringToObject(tag, "type", "bool"); - cJSON_AddItemToObject(tags, "t2", tag); - - tag = cJSON_CreateObject(); - cJSON_AddNumberToObject(tag, "value", 127); - cJSON_AddStringToObject(tag, "type", "tinyint"); - cJSON_AddItemToObject(tags, "t3", tag); - - tag = cJSON_CreateObject(); - cJSON_AddNumberToObject(tag, "value", 32767); - cJSON_AddStringToObject(tag, "type", "smallint"); - cJSON_AddItemToObject(tags, "t4", tag); - - tag = cJSON_CreateObject(); - cJSON_AddNumberToObject(tag, "value", 2147483647); - cJSON_AddStringToObject(tag, "type", "int"); - cJSON_AddItemToObject(tags, "t5", tag); - - tag = cJSON_CreateObject(); - cJSON_AddNumberToObject(tag, "value", 9223372036854775807); - cJSON_AddStringToObject(tag, "type", "bigint"); - cJSON_AddItemToObject(tags, "t6", tag); - - tag = cJSON_CreateObject(); - cJSON_AddNumberToObject(tag, "value", 11.12345); - cJSON_AddStringToObject(tag, "type", "float"); - cJSON_AddItemToObject(tags, "t7", tag); - - tag = cJSON_CreateObject(); - cJSON_AddNumberToObject(tag, "value", 22.1234567890); - cJSON_AddStringToObject(tag, "type", "double"); - cJSON_AddItemToObject(tags, "t8", tag); - - tag = cJSON_CreateObject(); - cJSON_AddStringToObject(tag, "value", "binary_val"); - cJSON_AddStringToObject(tag, "type", "binary"); - cJSON_AddItemToObject(tags, "t9", tag); - - tag = cJSON_CreateObject(); - cJSON_AddStringToObject(tag, "value", "你好"); - cJSON_AddStringToObject(tag, "type", "nchar"); - cJSON_AddItemToObject(tags, "t10", tag); - - cJSON_AddItemToObject(payload, "tags", tags); - - payload_str = cJSON_Print(payload); - // printf("%s\n", payload_str); - - code = taos_insert_json_payload(taos, payload_str); - if (code) { - printf("payload3_0 code: %d, %s.\n", code, tstrerror(code)); - } - free(payload_str); - cJSON_Delete(payload); -} - -int main(int argc, char* argv[]) { - const char* host = "127.0.0.1"; - const char* user = "root"; - const char* passwd = "taosdata"; - - taos_options(TSDB_OPTION_TIMEZONE, "GMT-8"); - TAOS* taos = taos_connect(host, user, passwd, "", 0); - if (taos == NULL) { - printf("\033[31mfailed to connect to db, reason:%s\033[0m\n", taos_errstr(taos)); - exit(1); - } - - char* info = taos_get_server_info(taos); - printf("server info: %s\n", info); - info = taos_get_client_info(taos); - printf("client info: %s\n", info); - - printf("************ verify schema-less *************\n"); - verify_schema_less(taos); - - printf("************ verify telnet-insert *************\n"); - verify_telnet_insert(taos); - - printf("************ verify json-insert *************\n"); - verify_json_insert(taos); - - printf("************ verify query *************\n"); - verify_query(taos); - - printf("********* verify async query **********\n"); - verify_async(taos); - - printf("*********** verify subscribe ************\n"); - verify_subscribe(taos); - - printf("************ verify prepare *************\n"); - verify_prepare(taos); - - printf("************ verify prepare2 *************\n"); - verify_prepare2(taos); - printf("************ verify prepare3 *************\n"); - verify_prepare3(taos); - - printf("************ verify stream *************\n"); - verify_stream(taos); - printf("done\n"); - taos_close(taos); - taos_cleanup(); -} diff --git a/examples/c/asyncdemo.c b/examples/c/asyncdemo.c index 07e61b871ed2bd8e857861d169973f2ff6153a72..83d217769b603ee54f2d5250bdf412990de4b76a 100644 --- a/examples/c/asyncdemo.c +++ b/examples/c/asyncdemo.c @@ -23,8 +23,8 @@ #include #include #include - -#include "../../../include/client/taos.h" +#include +#include "taos.h" int points = 5; int numOfTables = 3; @@ -230,7 +230,7 @@ void taos_insert_call_back(void *param, TAOS_RES *tres, int code) if (tablesInsertProcessed >= numOfTables) { gettimeofday(&systemTime, NULL); et = systemTime.tv_sec * 1000000 + systemTime.tv_usec; - printf("%lld mseconds to insert %d data points\n", (et - st) / 1000, points*numOfTables); + printf("%" PRId64 " mseconds to insert %d data points\n", (et - st) / 1000, points*numOfTables); } } @@ -267,7 +267,7 @@ void taos_retrieve_call_back(void *param, TAOS_RES *tres, int numOfRows) if (tablesSelectProcessed >= numOfTables) { gettimeofday(&systemTime, NULL); et = systemTime.tv_sec * 1000000 + systemTime.tv_usec; - printf("%lld mseconds to query %d data rows\n", (et - st) / 1000, points * numOfTables); + printf("%" PRId64 " mseconds to query %d data rows\n", (et - st) / 1000, points * numOfTables); } taos_free_result(tres); diff --git a/examples/c/demo.c b/examples/c/demo.c index c3d9a5e96a9dbd3cc2f33e047f3befabfcaf9ecb..2eda8cd811804606eeb4d04be11350cfd0c7a1f8 100644 --- a/examples/c/demo.c +++ b/examples/c/demo.c @@ -20,7 +20,7 @@ #include #include #include -#include "../../../include/client/taos.h" // TAOS header file +#include "taos.h" // TAOS header file static void queryDB(TAOS *taos, char *command) { int i; diff --git a/examples/c/epoll.c b/examples/c/epoll.c deleted file mode 100644 index 284268ac4328b5bc814ab8d30931ec92c5c11523..0000000000000000000000000000000000000000 --- a/examples/c/epoll.c +++ /dev/null @@ -1,304 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -// how to use to do a pressure-test upon eok -// tester: cat /dev/urandom | nc -c -// testee: ./debug/build/bin/epoll -l > /dev/null -// compare against: nc -l > /dev/null -// monitor and compare : glances - -#ifdef __APPLE__ -#include "osEok.h" -#else // __APPLE__ -#include -#endif // __APPLE__ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define D(fmt, ...) fprintf(stderr, "%s[%d]%s(): " fmt "\n", basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__) -#define A(statement, fmt, ...) do { \ - if (statement) break; \ - fprintf(stderr, "%s[%d]%s(): assert [%s] failed: %d[%s]: " fmt "\n", \ - basename(__FILE__), __LINE__, __func__, \ - #statement, errno, strerror(errno), \ - ##__VA_ARGS__); \ - abort(); \ -} while (0) - -#define E(fmt, ...) do { \ - fprintf(stderr, "%s[%d]%s(): %d[%s]: " fmt "\n", \ - basename(__FILE__), __LINE__, __func__, \ - errno, strerror(errno), \ - ##__VA_ARGS__); \ -} while (0) - -#include "os.h" - -typedef struct ep_s ep_t; -struct ep_s { - int ep; - - pthread_mutex_t lock; - int sv[2]; // 0 for read, 1 for write; - pthread_t thread; - - volatile unsigned int stopping:1; - volatile unsigned int waiting:1; - volatile unsigned int wakenup:1; -}; - -static int ep_dummy = 0; - -static ep_t* ep_create(void); -static void ep_destroy(ep_t *ep); -static void* routine(void* arg); -static int open_listen(unsigned short port); - -typedef struct fde_s fde_t; -struct fde_s { - int skt; - void (*on_event)(ep_t *ep, struct epoll_event *events, fde_t *client); -}; - -static void listen_event(ep_t *ep, struct epoll_event *ev, fde_t *client); -static void null_event(ep_t *ep, struct epoll_event *ev, fde_t *client); - -#define usage(arg0, fmt, ...) do { \ - if (fmt[0]) { \ - fprintf(stderr, "" fmt "\n", ##__VA_ARGS__); \ - } \ - fprintf(stderr, "usage:\n"); \ - fprintf(stderr, " %s -l : specify listenning port\n", arg0); \ -} while (0) - -int main(int argc, char *argv[]) { - char *prg = basename(argv[0]); - if (argc==1) { - usage(prg, ""); - return 0; - } - ep_t* ep = ep_create(); - A(ep, "failed"); - for (int i=1; i=argc) { - usage(prg, "expecting after -l, but got nothing"); - return 1; // confirmed potential leakage - } - arg = argv[i]; - int port = atoi(arg); - int skt = open_listen(port); - if (skt==-1) continue; - fde_t *client = (fde_t*)calloc(1, sizeof(*client)); - if (!client) { - E("out of memory"); - close(skt); - continue; - } - client->skt = skt; - client->on_event = listen_event; - struct epoll_event ev = {0}; - ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP; - ev.data.ptr = client; - A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ev), ""); - continue; - } - usage(prg, "unknown argument: [%s]", arg); - return 1; - } - char *line = NULL; - size_t linecap = 0; - ssize_t linelen; - while ((linelen = getline(&line, &linecap, stdin)) > 0) { - line[strlen(line)-1] = '\0'; - if (0==strcmp(line, "exit")) break; - if (0==strcmp(line, "quit")) break; - if (line==strstr(line, "close")) { - int fd = 0; - sscanf(line, "close %d", &fd); - if (fd<=2) { - fprintf(stderr, "fd [%d] invalid\n", fd); - continue; - } - A(0==epoll_ctl(ep->ep, EPOLL_CTL_DEL, fd, NULL), ""); - continue; - } - if (strlen(line)==0) continue; - fprintf(stderr, "unknown cmd:[%s]\n", line); - } - ep_destroy(ep); - D(""); - return 0; -} - -ep_t* ep_create(void) { - ep_t *ep = (ep_t*)calloc(1, sizeof(*ep)); - A(ep, "out of memory"); - A(-1!=(ep->ep = epoll_create(1)), ""); - ep->sv[0] = -1; - ep->sv[1] = -1; - A(0==socketpair(AF_LOCAL, SOCK_STREAM, 0, ep->sv), ""); - A(0==pthread_mutex_init(&ep->lock, NULL), ""); - A(0==pthread_mutex_lock(&ep->lock), ""); - struct epoll_event ev = {0}; - ev.events = EPOLLIN; - ev.data.ptr = &ep_dummy; - A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, ep->sv[0], &ev), ""); - A(0==pthread_create(&ep->thread, NULL, routine, ep), ""); - A(0==pthread_mutex_unlock(&ep->lock), ""); - return ep; -} - -static void ep_destroy(ep_t *ep) { - A(ep, "invalid argument"); - ep->stopping = 1; - A(1==send(ep->sv[1], "1", 1, 0), ""); - A(0==pthread_join(ep->thread, NULL), ""); - A(0==pthread_mutex_destroy(&ep->lock), ""); - A(0==close(ep->sv[0]), ""); - A(0==close(ep->sv[1]), ""); - A(0==close(ep->ep), ""); - free(ep); -} - -static void* routine(void* arg) { - A(arg, "invalid argument"); - ep_t *ep = (ep_t*)arg; - - while (!ep->stopping) { - struct epoll_event evs[10]; - memset(evs, 0, sizeof(evs)); - - A(0==pthread_mutex_lock(&ep->lock), ""); - A(ep->waiting==0, "internal logic error"); - ep->waiting = 1; - A(0==pthread_mutex_unlock(&ep->lock), ""); - - int r = epoll_wait(ep->ep, evs, sizeof(evs)/sizeof(evs[0]), -1); - A(r>0, "indefinite epoll_wait shall not timeout:[%d]", r); - - A(0==pthread_mutex_lock(&ep->lock), ""); - A(ep->waiting==1, "internal logic error"); - ep->waiting = 0; - A(0==pthread_mutex_unlock(&ep->lock), ""); - - for (int i=0; idata.ptr == &ep_dummy) { - char c = '\0'; - A(1==recv(ep->sv[0], &c, 1, 0), "internal logic error"); - A(0==pthread_mutex_lock(&ep->lock), ""); - ep->wakenup = 0; - A(0==pthread_mutex_unlock(&ep->lock), ""); - continue; - } - A(ev->data.ptr, "internal logic error"); - fde_t *client = (fde_t*)ev->data.ptr; - client->on_event(ep, ev, client); - continue; - } - } - return NULL; -} - -static int open_listen(unsigned short port) { - int r = 0; - int skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (skt==-1) { - E("socket() failed"); - return -1; - } - do { - struct sockaddr_in si = {0}; - si.sin_family = AF_INET; - si.sin_addr.s_addr = inet_addr("0.0.0.0"); - si.sin_port = htons(port); - r = bind(skt, (struct sockaddr*)&si, sizeof(si)); - if (r) { - E("bind(%u) failed", port); - break; - } - r = listen(skt, 100); - if (r) { - E("listen() failed"); - break; - } - memset(&si, 0, sizeof(si)); - socklen_t len = sizeof(si); - r = getsockname(skt, (struct sockaddr *)&si, &len); - if (r) { - E("getsockname() failed"); - } - A(len==sizeof(si), "internal logic error"); - D("listenning at: %d", ntohs(si.sin_port)); - return skt; - } while (0); - close(skt); - return -1; -} - -static void listen_event(ep_t *ep, struct epoll_event *ev, fde_t *client) { - A(ev->events & EPOLLIN, "internal logic error"); - struct sockaddr_in si = {0}; - socklen_t silen = sizeof(si); - int skt = accept(client->skt, (struct sockaddr*)&si, &silen); - A(skt!=-1, "internal logic error"); - fde_t *server = (fde_t*)calloc(1, sizeof(*server)); - if (!server) { - close(skt); - return; - } - server->skt = skt; - server->on_event = null_event; - struct epoll_event ee = {0}; - ee.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP; - ee.data.ptr = server; - A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ee), ""); -} - -static void null_event(ep_t *ep, struct epoll_event *ev, fde_t *client) { - if (ev->events & EPOLLIN) { - char buf[8192]; - int n = recv(client->skt, buf, sizeof(buf), 0); - A(n>=0 && n<=sizeof(buf), "internal logic error:[%d]", n); - A(n==fwrite(buf, 1, n, stdout), "internal logic error"); - } - if (ev->events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) { - A(0==pthread_mutex_lock(&ep->lock), ""); - A(0==epoll_ctl(ep->ep, EPOLL_CTL_DEL, client->skt, NULL), ""); - A(0==pthread_mutex_unlock(&ep->lock), ""); - close(client->skt); - client->skt = -1; - client->on_event = NULL; - free(client); - } -} - diff --git a/examples/c/makefile b/examples/c/makefile index c85eb4adc515e5fb4e875b8e8e955222bc09190e..244d13fad74d7b9a124102b47d7c7c642a640438 100644 --- a/examples/c/makefile +++ b/examples/c/makefile @@ -7,22 +7,21 @@ LFLAGS = '-Wl,-rpath,/usr/local/taos/driver/' -ltaos -lpthread -lm -lrt CFLAGS = -O3 -g -Wall -Wno-deprecated -fPIC -Wno-unused-result -Wconversion \ -Wno-char-subscripts -D_REENTRANT -Wno-format -D_REENTRANT -DLINUX \ -Wno-unused-function -D_M_X64 -I/usr/local/taos/include -std=gnu99 \ - -I../../../deps/cJson/inc + -I/usr/local/include/cjson all: $(TARGET) exe: gcc $(CFLAGS) ./asyncdemo.c -o $(ROOT)asyncdemo $(LFLAGS) gcc $(CFLAGS) ./demo.c -o $(ROOT)demo $(LFLAGS) gcc $(CFLAGS) ./prepare.c -o $(ROOT)prepare $(LFLAGS) - gcc $(CFLAGS) ./stream.c -o $(ROOT)stream $(LFLAGS) - gcc $(CFLAGS) ./subscribe.c -o $(ROOT)subscribe $(LFLAGS) - gcc $(CFLAGS) ./apitest.c -o $(ROOT)apitest $(LFLAGS) + gcc $(CFLAGS) ./stream_demo.c -o $(ROOT)stream_demo $(LFLAGS) + gcc $(CFLAGS) ./tmq.c -o $(ROOT)tmq $(LFLAGS) + gcc $(CFLAGS) ./schemaless.c -o $(ROOT)schemaless $(LFLAGS) clean: rm $(ROOT)asyncdemo rm $(ROOT)demo rm $(ROOT)prepare - rm $(ROOT)batchprepare - rm $(ROOT)stream - rm $(ROOT)subscribe - rm $(ROOT)apitest + rm $(ROOT)stream_demo + rm $(ROOT)tmq + rm $(ROOT)schemaless diff --git a/examples/c/prepare.c b/examples/c/prepare.c index f0341cfe127d4eadae29ae40a43c35c35ec2f492..4f05b6fb4cbc648bfde10b04be7388b14b37297e 100644 --- a/examples/c/prepare.c +++ b/examples/c/prepare.c @@ -4,7 +4,7 @@ #include #include #include -#include "../../../include/client/taos.h" +#include "taos.h" void taosMsleep(int mseconds); @@ -70,70 +70,89 @@ int main(int argc, char *argv[]) char blob[80]; } v = {0}; + int32_t boolLen = sizeof(int8_t); + int32_t sintLen = sizeof(int16_t); + int32_t intLen = sizeof(int32_t); + int32_t bintLen = sizeof(int64_t); + int32_t floatLen = sizeof(float); + int32_t doubleLen = sizeof(double); + int32_t binLen = sizeof(v.bin); + int32_t ncharLen = 30; + stmt = taos_stmt_init(taos); - TAOS_BIND params[10]; + TAOS_MULTI_BIND params[10]; params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; params[0].buffer_length = sizeof(v.ts); params[0].buffer = &v.ts; - params[0].length = ¶ms[0].buffer_length; + params[0].length = &bintLen; params[0].is_null = NULL; + params[0].num = 1; params[1].buffer_type = TSDB_DATA_TYPE_BOOL; params[1].buffer_length = sizeof(v.b); params[1].buffer = &v.b; - params[1].length = ¶ms[1].buffer_length; + params[1].length = &boolLen; params[1].is_null = NULL; + params[1].num = 1; params[2].buffer_type = TSDB_DATA_TYPE_TINYINT; params[2].buffer_length = sizeof(v.v1); params[2].buffer = &v.v1; - params[2].length = ¶ms[2].buffer_length; + params[2].length = &boolLen; params[2].is_null = NULL; + params[2].num = 1; params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT; params[3].buffer_length = sizeof(v.v2); params[3].buffer = &v.v2; - params[3].length = ¶ms[3].buffer_length; + params[3].length = &sintLen; params[3].is_null = NULL; + params[3].num = 1; params[4].buffer_type = TSDB_DATA_TYPE_INT; params[4].buffer_length = sizeof(v.v4); params[4].buffer = &v.v4; - params[4].length = ¶ms[4].buffer_length; + params[4].length = &intLen; params[4].is_null = NULL; + params[4].num = 1; params[5].buffer_type = TSDB_DATA_TYPE_BIGINT; params[5].buffer_length = sizeof(v.v8); params[5].buffer = &v.v8; - params[5].length = ¶ms[5].buffer_length; + params[5].length = &bintLen; params[5].is_null = NULL; + params[5].num = 1; params[6].buffer_type = TSDB_DATA_TYPE_FLOAT; params[6].buffer_length = sizeof(v.f4); params[6].buffer = &v.f4; - params[6].length = ¶ms[6].buffer_length; + params[6].length = &floatLen; params[6].is_null = NULL; + params[6].num = 1; params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE; params[7].buffer_length = sizeof(v.f8); params[7].buffer = &v.f8; - params[7].length = ¶ms[7].buffer_length; + params[7].length = &doubleLen; params[7].is_null = NULL; + params[7].num = 1; params[8].buffer_type = TSDB_DATA_TYPE_BINARY; params[8].buffer_length = sizeof(v.bin); params[8].buffer = v.bin; - params[8].length = ¶ms[8].buffer_length; + params[8].length = &binLen; params[8].is_null = NULL; + params[8].num = 1; strcpy(v.blob, "一二三四五六七八九十"); params[9].buffer_type = TSDB_DATA_TYPE_NCHAR; - params[9].buffer_length = strlen(v.blob); + params[9].buffer_length = sizeof(v.blob); params[9].buffer = v.blob; - params[9].length = ¶ms[9].buffer_length; + params[9].length = &ncharLen; params[9].is_null = NULL; + params[9].num = 1; - int is_null = 1; + char is_null = 1; sql = "insert into m1 values(?,?,?,?,?,?,?,?,?,?)"; code = taos_stmt_prepare(stmt, sql, 0); @@ -153,7 +172,7 @@ int main(int argc, char *argv[]) v.v8 = (int64_t)(i * 8); v.f4 = (float)(i * 40); v.f8 = (double)(i * 80); - for (int j = 0; j < sizeof(v.bin) - 1; ++j) { + for (int j = 0; j < sizeof(v.bin); ++j) { v.bin[j] = (char)(i + '0'); } diff --git a/examples/c/schemaless.c b/examples/c/schemaless.c index 99aa361b0a8801ff72467ed6f644262998f3c5b4..328a663ae79903e742a0c9650e8e947d6beb43d3 100644 --- a/examples/c/schemaless.c +++ b/examples/c/schemaless.c @@ -1,32 +1,17 @@ -#include "../../../include/client/taos.h" -#include "os.h" -#include "taoserror.h" - +#include "taos.h" #include #include #include #include #include +#include +#include + int numSuperTables = 8; int numChildTables = 4; int numRowsPerChildTable = 2048; -void shuffle(char**lines, size_t n) -{ - if (n > 1) - { - size_t i; - for (i = 0; i < n - 1; i++) - { - size_t j = i + taosRand() / (RAND_MAX / (n - i) + 1); - char* t = lines[j]; - lines[j] = lines[i]; - lines[i] = t; - } - } -} - static int64_t getTimeInUs() { struct timeval systemTime; gettimeofday(&systemTime, NULL); @@ -46,7 +31,7 @@ int main(int argc, char* argv[]) { exit(1); } - char* info = taos_get_server_info(taos); + const char* info = taos_get_server_info(taos); printf("server info: %s\n", info); info = taos_get_client_info(taos); printf("client info: %s\n", info); @@ -61,9 +46,10 @@ int main(int argc, char* argv[]) { time_t ct = time(0); int64_t ts = ct * 1000; - char* lineFormat = "sta%d,t0=true,t1=127i8,t2=32767i16,t3=%di32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=254u8,c6=32770u16,c7=2147483699u32,c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" %lldms"; + char* lineFormat = "sta%d,t0=true,t1=127i8,t2=32767i16,t3=%di32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=254u8,c6=32770u16,c7=2147483699u32,c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" %" PRId64; - char** lines = calloc(numSuperTables * numChildTables * numRowsPerChildTable, sizeof(char*)); + int lineNum = numSuperTables * numChildTables * numRowsPerChildTable; + char** lines = calloc((size_t)lineNum, sizeof(char*)); int l = 0; for (int i = 0; i < numSuperTables; ++i) { for (int j = 0; j < numChildTables; ++j) { @@ -75,13 +61,13 @@ int main(int argc, char* argv[]) { } } } - //shuffle(lines, numSuperTables * numChildTables * numRowsPerChildTable); printf("%s\n", "begin taos_insert_lines"); int64_t begin = getTimeInUs(); - int32_t code = taos_insert_lines(taos, lines, numSuperTables * numChildTables * numRowsPerChildTable); + TAOS_RES *res = taos_schemaless_insert(taos, lines, lineNum, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); int64_t end = getTimeInUs(); - printf("code: %d, %s. time used: %"PRId64"\n", code, tstrerror(code), end-begin); + printf("code: %s. time used: %" PRId64 "\n", taos_errstr(res), end-begin); + taos_free_result(res); return 0; } diff --git a/examples/c/stream_demo.c b/examples/c/stream_demo.c index 46c8297fbac8af6ec7fc3558d690313231569711..46107c7e130bccfc314d816c1ee4b1071803d9da 100644 --- a/examples/c/stream_demo.c +++ b/examples/c/stream_demo.c @@ -108,10 +108,13 @@ int32_t create_stream() { } int main(int argc, char* argv[]) { - int code; if (argc > 1) { printf("env init\n"); - code = init_env(); + int code = init_env(); + if (code) { + return code; + } + } create_stream(); } diff --git a/examples/c/tmq.c b/examples/c/tmq.c index 1147671ea18484468b22f0bcb30211909aea06b7..71e571d4dd06cb6bd1fc51eb6afba958dfc52bae 100644 --- a/examples/c/tmq.c +++ b/examples/c/tmq.c @@ -21,9 +21,6 @@ #include "taos.h" static int running = 1; -static char dbName[64] = "tmqdb"; -static char stbName[64] = "stb"; -static char topicName[64] = "topicname"; static int32_t msg_process(TAOS_RES* msg) { char buf[1024]; @@ -43,7 +40,7 @@ static int32_t msg_process(TAOS_RES* msg) { TAOS_FIELD* fields = taos_fetch_fields(msg); int32_t numOfFields = taos_field_count(msg); - int32_t* length = taos_fetch_lengths(msg); + //int32_t* length = taos_fetch_lengths(msg); int32_t precision = taos_result_precision(msg); rows++; taos_print_row(buf, row, fields, numOfFields); @@ -62,6 +59,13 @@ static int32_t init_env() { TAOS_RES* pRes; // drop database if exists printf("create database\n"); + pRes = taos_query(pConn, "drop topic topicname"); + if (taos_errno(pRes) != 0) { + printf("error in drop tmqdb, reason:%s\n", taos_errstr(pRes)); + return -1; + } + taos_free_result(pRes); + pRes = taos_query(pConn, "drop database if exists tmqdb"); if (taos_errno(pRes) != 0) { printf("error in drop tmqdb, reason:%s\n", taos_errstr(pRes)); @@ -249,7 +253,7 @@ int main(int argc, char* argv[]) { tmq_t* tmq = build_consumer(); if (NULL == tmq) { - fprintf(stderr, "%% build_consumer() fail!\n"); + fprintf(stderr, "build_consumer() fail!\n"); return -1; } @@ -259,7 +263,7 @@ int main(int argc, char* argv[]) { } if ((code = tmq_subscribe(tmq, topic_list))) { - fprintf(stderr, "%% Failed to tmq_subscribe(): %s\n", tmq_err2str(code)); + fprintf(stderr, "Failed to tmq_subscribe(): %s\n", tmq_err2str(code)); } tmq_list_destroy(topic_list); @@ -267,9 +271,9 @@ int main(int argc, char* argv[]) { code = tmq_consumer_close(tmq); if (code) { - fprintf(stderr, "%% Failed to close consumer: %s\n", tmq_err2str(code)); + fprintf(stderr, "Failed to close consumer: %s\n", tmq_err2str(code)); } else { - fprintf(stderr, "%% Consumer closed\n"); + fprintf(stderr, "Consumer closed\n"); } return 0; diff --git a/source/libs/catalog/src/ctgCache.c b/source/libs/catalog/src/ctgCache.c index 47cb0ef55910ddbd6ff77827067167818d87fabe..94ee295fe420ce9891c66b2431f836a036a107f3 100644 --- a/source/libs/catalog/src/ctgCache.c +++ b/source/libs/catalog/src/ctgCache.c @@ -1728,7 +1728,7 @@ int32_t ctgOpUpdateVgroup(SCtgCacheOperation *operation) { } if (dbInfo->vgVersion < 0 || taosHashGetSize(dbInfo->vgHash) <= 0) { - ctgError("invalid db vgInfo, dbFName:%s, vgHash:%p, vgVersion:%d, vgHashSize:%d", dbFName, dbInfo->vgHash, + ctgDebug("invalid db vgInfo, dbFName:%s, vgHash:%p, vgVersion:%d, vgHashSize:%d", dbFName, dbInfo->vgHash, dbInfo->vgVersion, taosHashGetSize(dbInfo->vgHash)); CTG_ERR_JRET(TSDB_CODE_APP_ERROR); } diff --git a/source/libs/scheduler/src/schStatus.c b/source/libs/scheduler/src/schStatus.c index 66d58c95d1c3a36938fb0b5b1fa420c558a33e4a..4c16a81a05003166978dd9ee0f96b2c876f6fefd 100644 --- a/source/libs/scheduler/src/schStatus.c +++ b/source/libs/scheduler/src/schStatus.c @@ -64,7 +64,7 @@ _return: int32_t schHandleOpBeginEvent(int64_t jobId, SSchJob** job, SCH_OP_TYPE type, SSchedulerReq* pReq) { SSchJob* pJob = schAcquireJob(jobId); if (NULL == pJob) { - qWarn("Acquire sch job failed, may be dropped, jobId:0x%" PRIx64, jobId); + qDebug("Acquire sch job failed, may be dropped, jobId:0x%" PRIx64, jobId); SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR); } diff --git a/source/libs/scheduler/src/scheduler.c b/source/libs/scheduler/src/scheduler.c index 1926c26dc91a8159dea24987ebffae8b632ab6cf..7cd5e957b6b8d47871274f80632a65ec11485753 100644 --- a/source/libs/scheduler/src/scheduler.c +++ b/source/libs/scheduler/src/scheduler.c @@ -157,7 +157,7 @@ void schedulerFreeJob(int64_t *jobId, int32_t errCode) { SSchJob *pJob = schAcquireJob(*jobId); if (NULL == pJob) { - qWarn("Acquire sch job failed, may be dropped, jobId:0x%" PRIx64, *jobId); + qDebug("Acquire sch job failed, may be dropped, jobId:0x%" PRIx64, *jobId); return; } diff --git a/examples/c/api_with_reqid_test.c b/tests/script/api/api_with_reqid_test.c similarity index 100% rename from examples/c/api_with_reqid_test.c rename to tests/script/api/api_with_reqid_test.c diff --git a/tests/script/api/apitest.c b/tests/script/api/apitest.c new file mode 100644 index 0000000000000000000000000000000000000000..7280522021b99a75f8315d9dc730eb5dc1eb7176 --- /dev/null +++ b/tests/script/api/apitest.c @@ -0,0 +1,936 @@ +// sample code to verify all TDengine API +// to compile: gcc -o apitest apitest.c -ltaos + +#include "cJSON.h" +#include "taoserror.h" + +#include +#include +#include +#include +#include "../../include/client/taos.h" + +static int64_t count = 10000; + +int64_t genReqid() { + count += 100; + return count; +} + +static void prepare_data(TAOS* taos) { + TAOS_RES* result; + result = taos_query(taos, "drop database if exists test;"); + taos_free_result(result); + usleep(100000); + result = taos_query(taos, "create database test precision 'us';"); + taos_free_result(result); + usleep(100000); + taos_select_db(taos, "test"); + + result = taos_query(taos, "create table meters(ts timestamp, a int) tags(area int);"); + taos_free_result(result); + + result = taos_query(taos, "create table t0 using meters tags(0);"); + taos_free_result(result); + result = taos_query(taos, "create table t1 using meters tags(1);"); + taos_free_result(result); + result = taos_query(taos, "create table t2 using meters tags(2);"); + taos_free_result(result); + result = taos_query(taos, "create table t3 using meters tags(3);"); + taos_free_result(result); + result = taos_query(taos, "create table t4 using meters tags(4);"); + taos_free_result(result); + result = taos_query(taos, "create table t5 using meters tags(5);"); + taos_free_result(result); + result = taos_query(taos, "create table t6 using meters tags(6);"); + taos_free_result(result); + result = taos_query(taos, "create table t7 using meters tags(7);"); + taos_free_result(result); + result = taos_query(taos, "create table t8 using meters tags(8);"); + taos_free_result(result); + result = taos_query(taos, "create table t9 using meters tags(9);"); + taos_free_result(result); + + result = taos_query(taos, + "insert into t0 values('2020-01-01 00:00:00.000', 0)" + " ('2020-01-01 00:01:00.000', 0)" + " ('2020-01-01 00:02:00.000', 0)" + " t1 values('2020-01-01 00:00:00.000', 0)" + " ('2020-01-01 00:01:00.000', 0)" + " ('2020-01-01 00:02:00.000', 0)" + " ('2020-01-01 00:03:00.000', 0)" + " t2 values('2020-01-01 00:00:00.000', 0)" + " ('2020-01-01 00:01:00.000', 0)" + " ('2020-01-01 00:01:01.000', 0)" + " ('2020-01-01 00:01:02.000', 0)" + " t3 values('2020-01-01 00:01:02.000', 0)" + " t4 values('2020-01-01 00:01:02.000', 0)" + " t5 values('2020-01-01 00:01:02.000', 0)" + " t6 values('2020-01-01 00:01:02.000', 0)" + " t7 values('2020-01-01 00:01:02.000', 0)" + " t8 values('2020-01-01 00:01:02.000', 0)" + " t9 values('2020-01-01 00:01:02.000', 0)"); + int affected = taos_affected_rows(result); + if (affected != 18) { + printf("\033[31m%d rows affected by last insert statement, but it should be 18\033[0m\n", affected); + } + taos_free_result(result); + // super tables subscription + usleep(1000000); +} + +static int print_result(TAOS_RES* res, int blockFetch) { + TAOS_ROW row = NULL; + int num_fields = taos_num_fields(res); + TAOS_FIELD* fields = taos_fetch_fields(res); + int nRows = 0; + + if (blockFetch) { + int rows = 0; + while ((rows = taos_fetch_block(res, &row))) { + // for (int i = 0; i < rows; i++) { + // char temp[256]; + // taos_print_row(temp, row + i, fields, num_fields); + // puts(temp); + // } + nRows += rows; + } + } else { + while ((row = taos_fetch_row(res))) { + char temp[256] = {0}; + taos_print_row(temp, row, fields, num_fields); + puts(temp); + nRows++; + } + } + + printf("%d rows consumed.\n", nRows); + return nRows; +} + +static void check_row_count(int line, TAOS_RES* res, int expected) { + int actual = print_result(res, expected % 2); + if (actual != expected) { + printf("\033[31mline %d: row count mismatch, expected: %d, actual: %d\033[0m\n", line, expected, actual); + } else { + printf("line %d: %d rows consumed as expected\n", line, actual); + } +} + +static void verify_query(TAOS* taos) { + prepare_data(taos); + + int code = taos_load_table_info(taos, "t0,t1,t2,t3,t4,t5,t6,t7,t8,t9"); + if (code != 0) { + printf("\033[31mfailed to load table info: 0x%08x\033[0m\n", code); + } + + code = taos_validate_sql(taos, "select * from nonexisttable"); + if (code == 0) { + printf("\033[31mimpossible, the table does not exists\033[0m\n"); + } + + code = taos_validate_sql(taos, "select * from meters"); + if (code != 0) { + printf("\033[31mimpossible, the table does exists: 0x%08x\033[0m\n", code); + } + + TAOS_RES* res = taos_query_with_reqid(taos, "select * from meters", genReqid()); + check_row_count(__LINE__, res, 18); + printf("result precision is: %d\n", taos_result_precision(res)); + int c = taos_field_count(res); + printf("field count is: %d\n", c); + int* lengths = taos_fetch_lengths(res); + for (int i = 0; i < c; i++) { + printf("length of column %d is %d\n", i, lengths[i]); + } + taos_free_result(res); + + res = taos_query_with_reqid(taos, "select * from t0", genReqid()); + check_row_count(__LINE__, res, 3); + taos_free_result(res); + + res = taos_query_with_reqid(taos, "select * from nonexisttable", genReqid()); + code = taos_errno(res); + printf("code=%d, error msg=%s\n", code, taos_errstr(res)); + taos_free_result(res); + + res = taos_query_with_reqid(taos, "select * from meters", genReqid()); + taos_stop_query(res); + taos_free_result(res); +} + +void subscribe_callback(TAOS_SUB* tsub, TAOS_RES* res, void* param, int code) { + int rows = print_result(res, *(int*)param); + printf("%d rows consumed in subscribe_callback\n", rows); +} + +void verify_prepare(TAOS* taos) { + TAOS_RES* result = taos_query(taos, "drop database if exists test;"); + taos_free_result(result); + + usleep(100000); + result = taos_query(taos, "create database test;"); + + int code = taos_errno(result); + if (code != 0) { + printf("\033[31mfailed to create database, reason:%s\033[0m\n", taos_errstr(result)); + taos_free_result(result); + return; + } + + taos_free_result(result); + + usleep(100000); + taos_select_db(taos, "test"); + + // create table + const char* sql = + "create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin " + "binary(40), blob nchar(10))"; + result = taos_query_with_reqid(taos, sql, genReqid()); + code = taos_errno(result); + if (code != 0) { + printf("\033[31mfailed to create table, reason:%s\033[0m\n", taos_errstr(result)); + taos_free_result(result); + return; + } + taos_free_result(result); + + // insert 10 records + struct { + int64_t ts; + int8_t b; + int8_t v1; + int16_t v2; + int32_t v4; + int64_t v8; + float f4; + double f8; + char bin[40]; + char blob[80]; + } v = {0}; + + int32_t boolLen = sizeof(int8_t); + int32_t sintLen = sizeof(int16_t); + int32_t intLen = sizeof(int32_t); + int32_t bintLen = sizeof(int64_t); + int32_t floatLen = sizeof(float); + int32_t doubleLen = sizeof(double); + int32_t binLen = sizeof(v.bin); + int32_t ncharLen = 30; + TAOS_STMT* stmt = taos_stmt_init(taos); + TAOS_MULTI_BIND params[10]; + params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; + params[0].buffer_length = sizeof(v.ts); + params[0].buffer = &v.ts; + params[0].length = &bintLen; + params[0].is_null = NULL; + params[0].num = 1; + + params[1].buffer_type = TSDB_DATA_TYPE_BOOL; + params[1].buffer_length = sizeof(v.b); + params[1].buffer = &v.b; + params[1].length = &boolLen; + params[1].is_null = NULL; + params[1].num = 1; + + params[2].buffer_type = TSDB_DATA_TYPE_TINYINT; + params[2].buffer_length = sizeof(v.v1); + params[2].buffer = &v.v1; + params[2].length = &boolLen; + params[2].is_null = NULL; + params[2].num = 1; + + params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT; + params[3].buffer_length = sizeof(v.v2); + params[3].buffer = &v.v2; + params[3].length = &sintLen; + params[3].is_null = NULL; + params[3].num = 1; + + params[4].buffer_type = TSDB_DATA_TYPE_INT; + params[4].buffer_length = sizeof(v.v4); + params[4].buffer = &v.v4; + params[4].length = &intLen; + params[4].is_null = NULL; + params[4].num = 1; + + params[5].buffer_type = TSDB_DATA_TYPE_BIGINT; + params[5].buffer_length = sizeof(v.v8); + params[5].buffer = &v.v8; + params[5].length = &bintLen; + params[5].is_null = NULL; + params[5].num = 1; + + params[6].buffer_type = TSDB_DATA_TYPE_FLOAT; + params[6].buffer_length = sizeof(v.f4); + params[6].buffer = &v.f4; + params[6].length = &floatLen; + params[6].is_null = NULL; + params[6].num = 1; + + params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE; + params[7].buffer_length = sizeof(v.f8); + params[7].buffer = &v.f8; + params[7].length = &doubleLen; + params[7].is_null = NULL; + params[7].num = 1; + + params[8].buffer_type = TSDB_DATA_TYPE_BINARY; + params[8].buffer_length = sizeof(v.bin); + params[8].buffer = v.bin; + params[8].length = &binLen; + params[8].is_null = NULL; + params[8].num = 1; + + strcpy(v.blob, "一二三四五六七八九十"); + params[9].buffer_type = TSDB_DATA_TYPE_NCHAR; + params[9].buffer_length = sizeof(v.blob); + params[9].buffer = v.blob; + params[9].length = &ncharLen; + params[9].is_null = NULL; + params[9].num = 1; + + char is_null = 1; + + sql = "insert into m1 values(?,?,?,?,?,?,?,?,?,?)"; + code = taos_stmt_prepare(stmt, sql, 0); + if (code != 0) { + printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; + } + v.ts = 1591060628000; + for (int i = 0; i < 10; ++i) { + v.ts += 1; + for (int j = 1; j < 10; ++j) { + params[j].is_null = ((i == j) ? &is_null : 0); + } + v.b = (int8_t)i % 2; + v.v1 = (int8_t)i; + v.v2 = (int16_t)(i * 2); + v.v4 = (int32_t)(i * 4); + v.v8 = (int64_t)(i * 8); + v.f4 = (float)(i * 40); + v.f8 = (double)(i * 80); + for (int j = 0; j < sizeof(v.bin); ++j) { + v.bin[j] = (char)(i + '0'); + } + + taos_stmt_bind_param(stmt, params); + taos_stmt_add_batch(stmt); + } + if (taos_stmt_execute(stmt) != 0) { + printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; + } + taos_stmt_close(stmt); + + // query the records + stmt = taos_stmt_init(taos); + taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0); + v.v1 = 5; + v.v2 = 15; + taos_stmt_bind_param(stmt, params + 2); + if (taos_stmt_execute(stmt) != 0) { + printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; + } + + result = taos_stmt_use_result(stmt); + + TAOS_ROW row; + int rows = 0; + int num_fields = taos_num_fields(result); + TAOS_FIELD* fields = taos_fetch_fields(result); + + // fetch the records row by row + while ((row = taos_fetch_row(result))) { + char temp[256] = {0}; + rows++; + taos_print_row(temp, row, fields, num_fields); + printf("%s\n", temp); + } + + taos_free_result(result); + taos_stmt_close(stmt); +} + +void verify_prepare2(TAOS* taos) { + TAOS_RES* result = taos_query(taos, "drop database if exists test;"); + taos_free_result(result); + usleep(100000); + result = taos_query(taos, "create database test;"); + + int code = taos_errno(result); + if (code != 0) { + printf("\033[31mfailed to create database, reason:%s\033[0m\n", taos_errstr(result)); + taos_free_result(result); + return; + } + taos_free_result(result); + + usleep(100000); + taos_select_db(taos, "test"); + + // create table + const char* sql = + "create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin " + "binary(40), blob nchar(10))"; + result = taos_query(taos, sql); + code = taos_errno(result); + if (code != 0) { + printf("\033[31mfailed to create table, reason:%s\033[0m\n", taos_errstr(result)); + taos_free_result(result); + return; + } + taos_free_result(result); + + // insert 10 records + struct { + int64_t ts[10]; + int8_t b[10]; + int8_t v1[10]; + int16_t v2[10]; + int32_t v4[10]; + int64_t v8[10]; + float f4[10]; + double f8[10]; + char bin[10][40]; + char blob[10][80]; + } v; + + int32_t* t8_len = malloc(sizeof(int32_t) * 10); + int32_t* t16_len = malloc(sizeof(int32_t) * 10); + int32_t* t32_len = malloc(sizeof(int32_t) * 10); + int32_t* t64_len = malloc(sizeof(int32_t) * 10); + int32_t* float_len = malloc(sizeof(int32_t) * 10); + int32_t* double_len = malloc(sizeof(int32_t) * 10); + int32_t* bin_len = malloc(sizeof(int32_t) * 10); + int32_t* blob_len = malloc(sizeof(int32_t) * 10); + + TAOS_STMT* stmt = taos_stmt_init(taos); + TAOS_MULTI_BIND params[10]; + char is_null[10] = {0}; + + params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; + params[0].buffer_length = sizeof(v.ts[0]); + params[0].buffer = v.ts; + params[0].length = t64_len; + params[0].is_null = is_null; + params[0].num = 10; + + params[1].buffer_type = TSDB_DATA_TYPE_BOOL; + params[1].buffer_length = sizeof(v.b[0]); + params[1].buffer = v.b; + params[1].length = t8_len; + params[1].is_null = is_null; + params[1].num = 10; + + params[2].buffer_type = TSDB_DATA_TYPE_TINYINT; + params[2].buffer_length = sizeof(v.v1[0]); + params[2].buffer = v.v1; + params[2].length = t8_len; + params[2].is_null = is_null; + params[2].num = 10; + + params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT; + params[3].buffer_length = sizeof(v.v2[0]); + params[3].buffer = v.v2; + params[3].length = t16_len; + params[3].is_null = is_null; + params[3].num = 10; + + params[4].buffer_type = TSDB_DATA_TYPE_INT; + params[4].buffer_length = sizeof(v.v4[0]); + params[4].buffer = v.v4; + params[4].length = t32_len; + params[4].is_null = is_null; + params[4].num = 10; + + params[5].buffer_type = TSDB_DATA_TYPE_BIGINT; + params[5].buffer_length = sizeof(v.v8[0]); + params[5].buffer = v.v8; + params[5].length = t64_len; + params[5].is_null = is_null; + params[5].num = 10; + + params[6].buffer_type = TSDB_DATA_TYPE_FLOAT; + params[6].buffer_length = sizeof(v.f4[0]); + params[6].buffer = v.f4; + params[6].length = float_len; + params[6].is_null = is_null; + params[6].num = 10; + + params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE; + params[7].buffer_length = sizeof(v.f8[0]); + params[7].buffer = v.f8; + params[7].length = double_len; + params[7].is_null = is_null; + params[7].num = 10; + + params[8].buffer_type = TSDB_DATA_TYPE_BINARY; + params[8].buffer_length = sizeof(v.bin[0]); + params[8].buffer = v.bin; + params[8].length = bin_len; + params[8].is_null = is_null; + params[8].num = 10; + + params[9].buffer_type = TSDB_DATA_TYPE_NCHAR; + params[9].buffer_length = sizeof(v.blob[0]); + params[9].buffer = v.blob; + params[9].length = blob_len; + params[9].is_null = is_null; + params[9].num = 10; + + sql = "insert into ? (ts, b, v1, v2, v4, v8, f4, f8, bin, blob) values(?,?,?,?,?,?,?,?,?,?)"; + code = taos_stmt_prepare(stmt, sql, 0); + if (code != 0) { + printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; + } + + code = taos_stmt_set_tbname(stmt, "m1"); + if (code != 0) { + printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; + } + + int64_t ts = 1591060628000; + for (int i = 0; i < 10; ++i) { + v.ts[i] = ts++; + is_null[i] = 0; + + v.b[i] = (int8_t)i % 2; + v.v1[i] = (int8_t)i; + v.v2[i] = (int16_t)(i * 2); + v.v4[i] = (int32_t)(i * 4); + v.v8[i] = (int64_t)(i * 8); + v.f4[i] = (float)(i * 40); + v.f8[i] = (double)(i * 80); + for (int j = 0; j < sizeof(v.bin[0]); ++j) { + v.bin[i][j] = (char)(i + '0'); + } + strcpy(v.blob[i], "一二三四五六七八九十"); + + t8_len[i] = sizeof(int8_t); + t16_len[i] = sizeof(int16_t); + t32_len[i] = sizeof(int32_t); + t64_len[i] = sizeof(int64_t); + float_len[i] = sizeof(float); + double_len[i] = sizeof(double); + bin_len[i] = sizeof(v.bin[0]); + blob_len[i] = (int32_t)strlen(v.blob[i]); + } + + taos_stmt_bind_param_batch(stmt, params); + taos_stmt_add_batch(stmt); + + if (taos_stmt_execute(stmt) != 0) { + printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; + } + + taos_stmt_close(stmt); + + // query the records + stmt = taos_stmt_init(taos); + taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0); + TAOS_MULTI_BIND qparams[2]; + + int8_t v1 = 5; + int16_t v2 = 15; + int32_t tinyLen = sizeof(v1); + int32_t smallLen = sizeof(v2); + + qparams[0].buffer_type = TSDB_DATA_TYPE_TINYINT; + qparams[0].buffer_length = sizeof(v1); + qparams[0].buffer = &v1; + qparams[0].length = &tinyLen; + qparams[0].is_null = NULL; + qparams[0].num = 1; + + qparams[1].buffer_type = TSDB_DATA_TYPE_SMALLINT; + qparams[1].buffer_length = sizeof(v2); + qparams[1].buffer = &v2; + qparams[1].length = &smallLen; + qparams[1].is_null = NULL; + qparams[1].num = 1; + + taos_stmt_bind_param(stmt, qparams); + if (taos_stmt_execute(stmt) != 0) { + printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; + } + + result = taos_stmt_use_result(stmt); + + TAOS_ROW row; + int rows = 0; + int num_fields = taos_num_fields(result); + TAOS_FIELD* fields = taos_fetch_fields(result); + + // fetch the records row by row + while ((row = taos_fetch_row(result))) { + char temp[256] = {0}; + rows++; + taos_print_row(temp, row, fields, num_fields); + printf("%s\n", temp); + } + + taos_free_result(result); + taos_stmt_close(stmt); + + free(t8_len); + free(t16_len); + free(t32_len); + free(t64_len); + free(float_len); + free(double_len); + free(bin_len); + free(blob_len); +} + +void verify_prepare3(TAOS* taos) { + TAOS_RES* result = taos_query(taos, "drop database if exists test;"); + taos_free_result(result); + usleep(100000); + result = taos_query(taos, "create database test;"); + + int code = taos_errno(result); + if (code != 0) { + printf("\033[31mfailed to create database, reason:%s\033[0m\n", taos_errstr(result)); + taos_free_result(result); + return; + } + taos_free_result(result); + + usleep(100000); + taos_select_db(taos, "test"); + + // create table + const char* sql = + "create stable st1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin " + "binary(40), blob nchar(10)) tags (id1 int, id2 binary(40))"; + result = taos_query(taos, sql); + code = taos_errno(result); + if (code != 0) { + printf("\033[31mfailed to create table, reason:%s\033[0m\n", taos_errstr(result)); + taos_free_result(result); + return; + } + taos_free_result(result); + + TAOS_MULTI_BIND tags[2]; + + int32_t id1 = 1; + char id2[40] = "abcdefghijklmnopqrstuvwxyz0123456789"; + int32_t id2_len = (int32_t)strlen(id2); + + tags[0].buffer_type = TSDB_DATA_TYPE_INT; + tags[0].buffer_length = sizeof(int); + tags[0].buffer = &id1; + tags[0].length = NULL; + tags[0].is_null = NULL; + tags[0].num = 1; + + tags[1].buffer_type = TSDB_DATA_TYPE_BINARY; + tags[1].buffer_length = sizeof(id2); + tags[1].buffer = id2; + tags[1].length = &id2_len; + tags[1].is_null = NULL; + tags[1].num = 1; + + // insert 10 records + struct { + int64_t ts[10]; + int8_t b[10]; + int8_t v1[10]; + int16_t v2[10]; + int32_t v4[10]; + int64_t v8[10]; + float f4[10]; + double f8[10]; + char bin[10][40]; + char blob[10][80]; + } v; + + int32_t* t8_len = malloc(sizeof(int32_t) * 10); + int32_t* t16_len = malloc(sizeof(int32_t) * 10); + int32_t* t32_len = malloc(sizeof(int32_t) * 10); + int32_t* t64_len = malloc(sizeof(int32_t) * 10); + int32_t* float_len = malloc(sizeof(int32_t) * 10); + int32_t* double_len = malloc(sizeof(int32_t) * 10); + int32_t* bin_len = malloc(sizeof(int32_t) * 10); + int32_t* blob_len = malloc(sizeof(int32_t) * 10); + + TAOS_STMT* stmt = taos_stmt_init(taos); + TAOS_MULTI_BIND params[10]; + char is_null[10] = {0}; + + params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; + params[0].buffer_length = sizeof(v.ts[0]); + params[0].buffer = v.ts; + params[0].length = t64_len; + params[0].is_null = is_null; + params[0].num = 10; + + params[1].buffer_type = TSDB_DATA_TYPE_BOOL; + params[1].buffer_length = sizeof(v.b[0]); + params[1].buffer = v.b; + params[1].length = t8_len; + params[1].is_null = is_null; + params[1].num = 10; + + params[2].buffer_type = TSDB_DATA_TYPE_TINYINT; + params[2].buffer_length = sizeof(v.v1[0]); + params[2].buffer = v.v1; + params[2].length = t8_len; + params[2].is_null = is_null; + params[2].num = 10; + + params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT; + params[3].buffer_length = sizeof(v.v2[0]); + params[3].buffer = v.v2; + params[3].length = t16_len; + params[3].is_null = is_null; + params[3].num = 10; + + params[4].buffer_type = TSDB_DATA_TYPE_INT; + params[4].buffer_length = sizeof(v.v4[0]); + params[4].buffer = v.v4; + params[4].length = t32_len; + params[4].is_null = is_null; + params[4].num = 10; + + params[5].buffer_type = TSDB_DATA_TYPE_BIGINT; + params[5].buffer_length = sizeof(v.v8[0]); + params[5].buffer = v.v8; + params[5].length = t64_len; + params[5].is_null = is_null; + params[5].num = 10; + + params[6].buffer_type = TSDB_DATA_TYPE_FLOAT; + params[6].buffer_length = sizeof(v.f4[0]); + params[6].buffer = v.f4; + params[6].length = float_len; + params[6].is_null = is_null; + params[6].num = 10; + + params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE; + params[7].buffer_length = sizeof(v.f8[0]); + params[7].buffer = v.f8; + params[7].length = double_len; + params[7].is_null = is_null; + params[7].num = 10; + + params[8].buffer_type = TSDB_DATA_TYPE_BINARY; + params[8].buffer_length = sizeof(v.bin[0]); + params[8].buffer = v.bin; + params[8].length = bin_len; + params[8].is_null = is_null; + params[8].num = 10; + + params[9].buffer_type = TSDB_DATA_TYPE_NCHAR; + params[9].buffer_length = sizeof(v.blob[0]); + params[9].buffer = v.blob; + params[9].length = blob_len; + params[9].is_null = is_null; + params[9].num = 10; + + sql = "insert into ? using st1 tags(?,?) values(?,?,?,?,?,?,?,?,?,?)"; + code = taos_stmt_prepare(stmt, sql, 0); + if (code != 0) { + printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; + } + + code = taos_stmt_set_tbname_tags(stmt, "m1", tags); + if (code != 0) { + printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; + } + + int64_t ts = 1591060628000; + for (int i = 0; i < 10; ++i) { + v.ts[i] = ts++; + is_null[i] = 0; + + v.b[i] = (int8_t)i % 2; + v.v1[i] = (int8_t)i; + v.v2[i] = (int16_t)(i * 2); + v.v4[i] = (int32_t)(i * 4); + v.v8[i] = (int64_t)(i * 8); + v.f4[i] = (float)(i * 40); + v.f8[i] = (double)(i * 80); + for (int j = 0; j < sizeof(v.bin[0]); ++j) { + v.bin[i][j] = (char)(i + '0'); + } + strcpy(v.blob[i], "一二三四五六七八九十"); + + t8_len[i] = sizeof(int8_t); + t16_len[i] = sizeof(int16_t); + t32_len[i] = sizeof(int32_t); + t64_len[i] = sizeof(int64_t); + float_len[i] = sizeof(float); + double_len[i] = sizeof(double); + bin_len[i] = sizeof(v.bin[0]); + blob_len[i] = (int32_t)strlen(v.blob[i]); + } + + taos_stmt_bind_param_batch(stmt, params); + taos_stmt_add_batch(stmt); + + if (taos_stmt_execute(stmt) != 0) { + printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; + } + taos_stmt_close(stmt); + + // query the records + stmt = taos_stmt_init(taos); + taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0); + + TAOS_MULTI_BIND qparams[2]; + + int8_t v1 = 5; + int16_t v2 = 15; + int32_t tinyLen = sizeof(v1); + int32_t smallLen = sizeof(v2); + + qparams[0].buffer_type = TSDB_DATA_TYPE_TINYINT; + qparams[0].buffer_length = sizeof(v1); + qparams[0].buffer = &v1; + qparams[0].length = &tinyLen; + qparams[0].is_null = NULL; + qparams[0].num = 1; + + qparams[1].buffer_type = TSDB_DATA_TYPE_SMALLINT; + qparams[1].buffer_length = sizeof(v2); + qparams[1].buffer = &v2; + qparams[1].length = &smallLen; + qparams[1].is_null = NULL; + qparams[1].num = 1; + + taos_stmt_bind_param(stmt, qparams); + if (taos_stmt_execute(stmt) != 0) { + printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; + } + + result = taos_stmt_use_result(stmt); + + TAOS_ROW row; + int rows = 0; + int num_fields = taos_num_fields(result); + TAOS_FIELD* fields = taos_fetch_fields(result); + + // fetch the records row by row + while ((row = taos_fetch_row(result))) { + char temp[256] = {0}; + rows++; + taos_print_row(temp, row, fields, num_fields); + printf("%s\n", temp); + } + + taos_free_result(result); + taos_stmt_close(stmt); + + free(t8_len); + free(t16_len); + free(t32_len); + free(t64_len); + free(float_len); + free(double_len); + free(bin_len); + free(blob_len); +} + +void retrieve_callback(void* param, TAOS_RES* tres, int numOfRows) { + if (numOfRows > 0) { + printf("%d rows async retrieved\n", numOfRows); + taos_fetch_rows_a(tres, retrieve_callback, param); + } else { + if (numOfRows < 0) { + printf("\033[31masync retrieve failed, code: %d\033[0m\n", numOfRows); + } else { + printf("async retrieve completed\n"); + } + taos_free_result(tres); + } +} + +void select_callback(void* param, TAOS_RES* tres, int code) { + if (code == 0 && tres) { + taos_fetch_rows_a(tres, retrieve_callback, param); + } else { + printf("\033[31masync select failed, code: %d\033[0m\n", code); + } +} + +void verify_async(TAOS* taos) { + prepare_data(taos); + taos_query_a(taos, "select * from meters", select_callback, NULL); + usleep(1000000); +} + +void stream_callback(void* param, TAOS_RES* res, TAOS_ROW row) { + if (res == NULL || row == NULL) { + return; + } + + int num_fields = taos_num_fields(res); + TAOS_FIELD* fields = taos_fetch_fields(res); + + printf("got one row from stream_callback\n"); + char temp[256] = {0}; + taos_print_row(temp, row, fields, num_fields); + puts(temp); +} + +int main(int argc, char* argv[]) { + const char* host = "127.0.0.1"; + const char* user = "root"; + const char* passwd = "taosdata"; + + taos_options(TSDB_OPTION_TIMEZONE, "GMT-8"); + TAOS* taos = taos_connect(host, user, passwd, "", 0); + if (taos == NULL) { + printf("\033[31mfailed to connect to db, reason:%s\033[0m\n", taos_errstr(taos)); + exit(1); + } + + const char* info = taos_get_server_info(taos); + printf("server info: %s\n", info); + info = taos_get_client_info(taos); + printf("client info: %s\n", info); + + printf("************ verify query *************\n"); + verify_query(taos); + + printf("********* verify async query **********\n"); + verify_async(taos); + + printf("************ verify prepare *************\n"); + verify_prepare(taos); + + printf("************ verify prepare2 *************\n"); + verify_prepare2(taos); + printf("************ verify prepare3 *************\n"); + verify_prepare3(taos); + + printf("done\n"); + taos_close(taos); + taos_cleanup(); +} diff --git a/examples/c/demoapi.c b/tests/script/api/demoapi.c similarity index 99% rename from examples/c/demoapi.c rename to tests/script/api/demoapi.c index 18f1b5a0592a27b1f40c5c7f7c64e6e7341d1a6e..6c060a6325dccc03f18e295dd88329df7e81395c 100644 --- a/examples/c/demoapi.c +++ b/tests/script/api/demoapi.c @@ -21,7 +21,6 @@ #ifndef WINDOWS #include #endif -#include "osSleep.h" #include "taos.h" #define debugPrint(fmt, ...) \ @@ -81,11 +80,9 @@ static void prepare_data(TAOS* taos) { TAOS_RES *res; res = taos_query(taos, "drop database if exists test;"); taos_free_result(res); - taosMsleep(100); res = taos_query(taos, "create database test;"); taos_free_result(res); - taosMsleep(100); if (taos_select_db(taos, "test")) { errorPrint("%s() LN%d: taos_select_db() failed\n", __func__, __LINE__);