diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 4f07ff2b..80a35b34 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -61,19 +61,19 @@ jobs: path: "TDengine" ref: ${{ steps.determine-branch.outputs.value }} - - uses: actions/cache@v3 - with: - path: | - TDengine/release/ - TDengine/debug/ - TDengine/contrib/cJson - TDengine/contrib/cpp-stub - TDengine/contrib/googletest - TDengine/contrib/libuv - TDengine/contrib/lz4 - TDengine/contrib/zlib - TDengine/contrib/rocksdb - key: ${{ runner.os }}-TDengine-build-main + # - uses: actions/cache@v3 + # with: + # path: | + # TDengine/release/ + # TDengine/debug/ + # TDengine/contrib/cJson + # TDengine/contrib/cpp-stub + # TDengine/contrib/googletest + # TDengine/contrib/libuv + # TDengine/contrib/lz4 + # TDengine/contrib/zlib + # TDengine/contrib/rocksdb + # key: ${{ runner.os }}-TDengine-build-main - name: Build & Install TDengine env: diff --git a/taos-ws-sys/examples/Makefile b/taos-ws-sys/examples/Makefile index d65f77fb..8ed020d2 100644 --- a/taos-ws-sys/examples/Makefile +++ b/taos-ws-sys/examples/Makefile @@ -15,3 +15,9 @@ bind: muti: gcc -g ws-muti-bind.c ${WS_FLAGS} -o ./target/ws-muti-bind && ./target/ws-muti-bind + +table: + gcc -g ws-table-muti-bind.c ${WS_FLAGS} -o ./target/ws-table-muti-bind && ./target/ws-table-muti-bind + +get-err: + gcc -g ws-get-err.c ${WS_FLAGS} -o ./target/ws-get-err && ./target/ws-get-err diff --git a/taos-ws-sys/examples/ws-get-err.c b/taos-ws-sys/examples/ws-get-err.c new file mode 100644 index 00000000..c9e9870c --- /dev/null +++ b/taos-ws-sys/examples/ws-get-err.c @@ -0,0 +1,178 @@ +#include +#include +#include +#include "taosws.h" + +/** + * @brief execute sql only. + * + * @param taos + * @param sql + */ +void execute_sql(WS_TAOS *taos, const char *sql) +{ + WS_RES *res = ws_query(taos, sql); + int code = ws_errno(res); + if (code != 0) + { + printf("%s\n", ws_errstr(res)); + ws_free_result(res); + ws_close(taos); + exit(EXIT_FAILURE); + } + ws_free_result(res); +} + +/** + * @brief check return status and exit program when error occur. + * + * @param stmt + * @param code + * @param msg + */ +void check_error_code(WS_STMT *stmt, int code, const char *msg) +{ + if (code != 0) + { + printf("%s. error: %s\n", msg, ws_stmt_errstr(stmt)); + ws_stmt_close(stmt); + exit(EXIT_FAILURE); + } +} + +typedef struct +{ + int64_t ts; + float current; + int voltage; + float phase; +} Row; + +/** + * @brief insert data using stmt API + * + * @param taos + */ +void insert_data(WS_TAOS *taos) +{ + // init + WS_STMT *stmt = ws_stmt_init(taos); + + StmtField *fields = NULL; + int num_fields = 0; + int code; + + code = ws_stmt_get_col_fields(stmt, &fields, &num_fields); + check_error_code(stmt, code, "failed to execute ws_stmt_get_col_fields"); + ws_stmt_reclaim_fields(&fields, num_fields); + + // prepare + const char *sql = "INSERT INTO ? USING meters TAGS(?, ?) VALUES(?, ?, ?, ?)"; + code = ws_stmt_prepare(stmt, sql, (unsigned long)strlen(sql)); + check_error_code(stmt, code, "failed to execute ws_stmt_prepare"); + // bind table name and tags + WS_MULTI_BIND tags[2]; + char *location = "California.SanFrancisco"; + int groupId = 2; + + const int32_t location_len = (int32_t)strlen(location); + const int32_t group_id_len = (int32_t)sizeof(groupId); + + tags[0].buffer_type = TSDB_DATA_TYPE_BINARY; + tags[0].buffer_length = strlen(location); + tags[0].length = &location_len; + tags[0].buffer = location; + tags[0].is_null = NULL; + + tags[1].buffer_type = TSDB_DATA_TYPE_INT; + tags[1].buffer_length = sizeof(int); + tags[1].length = &group_id_len; + tags[1].buffer = &groupId; + tags[1].is_null = NULL; + + code = ws_stmt_set_tbname_tags(stmt, "d1001", tags, 2); + check_error_code(stmt, code, "failed to execute ws_stmt_set_tbname_tags"); + + num_fields = 0; + code = ws_stmt_get_col_fields(stmt, &fields, &num_fields); + check_error_code(stmt, code, "failed to execute ws_stmt_get_col_fields"); + printf("num_fields: %d\n", num_fields); + for (int i = 0; i < num_fields; ++i) + { + printf("field[%d]: %s\n", i, fields[i].name); + } + + // insert two rows + Row rows[2] = { + {1648432611249, 10.3, 219, 0.31}, + {1648432611749, 12.6, 218, 0.33}, + }; + + const int32_t ts_len = (int32_t)sizeof(int64_t); + const int32_t current_len = (int32_t)sizeof(float); + const int32_t voltage_len = (int32_t)sizeof(int); + const int32_t phase_len = (int32_t)sizeof(float); + + WS_MULTI_BIND values[4]; + values[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; + values[0].buffer_length = sizeof(int64_t); + values[0].length = &ts_len; + values[0].is_null = NULL; + values[0].num = 1; + + values[1].buffer_type = TSDB_DATA_TYPE_FLOAT; + values[1].buffer_length = sizeof(float); + values[1].length = ¤t_len; + values[1].is_null = NULL; + values[1].num = 1; + + values[2].buffer_type = TSDB_DATA_TYPE_INT; + values[2].buffer_length = sizeof(int); + values[2].length = &voltage_len; + values[2].is_null = NULL; + values[2].num = 1; + + values[3].buffer_type = TSDB_DATA_TYPE_FLOAT; + values[3].buffer_length = sizeof(float); + values[3].length = &phase_len; + values[3].is_null = NULL; + values[3].num = 1; + + for (int i = 0; i < 2; ++i) + { + values[0].buffer = &rows[i].ts; + values[1].buffer = &rows[i].current; + values[2].buffer = &rows[i].voltage; + values[3].buffer = &rows[i].phase; + + code = ws_stmt_bind_param_batch(stmt, &values[0], 4); // bind param + check_error_code(stmt, code, "failed to execute ws_stmt_bind_param_batch"); + code = ws_stmt_add_batch(stmt); // add batch + check_error_code(stmt, code, "failed to execute ws_stmt_add_batch"); + } + // execute + int32_t affected_rows = 0; + code = ws_stmt_execute(stmt, &affected_rows); + check_error_code(stmt, code, "failed to execute ws_stmt_execute"); + + printf("successfully inserted %d rows\n", affected_rows); + // close + ws_stmt_close(stmt); +} + +int main() +{ + ws_enable_log(); + WS_TAOS *taos = ws_connect_with_dsn("ws://localhost:6041"); + if (taos == NULL) + { + printf("failed to connect to server\n"); + exit(EXIT_FAILURE); + } + execute_sql(taos, "DROP DATABASE IF EXISTS power"); + execute_sql(taos, "CREATE DATABASE power"); + execute_sql(taos, "USE power"); + execute_sql(taos, "CREATE STABLE meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)"); + insert_data(taos); + ws_close(taos); +} diff --git a/taos-ws-sys/examples/ws-table-muti-bind.c b/taos-ws-sys/examples/ws-table-muti-bind.c new file mode 100644 index 00000000..49f1fe9f --- /dev/null +++ b/taos-ws-sys/examples/ws-table-muti-bind.c @@ -0,0 +1,193 @@ +#include +#include +#include +#include "taosws.h" + +/** + * @brief execute sql only. + * + * @param taos + * @param sql + */ +void execute_sql(WS_TAOS *taos, const char *sql) +{ + WS_RES *res = ws_query(taos, sql); + int code = ws_errno(res); + if (code != 0) + { + printf("%s\n", ws_errstr(res)); + ws_free_result(res); + ws_close(taos); + exit(EXIT_FAILURE); + } + ws_free_result(res); +} + +/** + * @brief check return status and exit program when error occur. + * + * @param stmt + * @param code + * @param msg + */ +void check_error_code(WS_STMT *stmt, int code, const char *msg) +{ + if (code != 0) + { + printf("%s. error: %s\n", msg, ws_stmt_errstr(stmt)); + ws_stmt_close(stmt); + exit(EXIT_FAILURE); + } +} + +typedef struct +{ + int64_t ts; + float current; + int voltage; + float phase; +} Row; + +/** + * @brief insert data using stmt API + * + * @param taos + */ +void insert_data(WS_TAOS *taos) +{ + // init + WS_STMT *stmt = ws_stmt_init(taos); + // prepare + const char *sql = "INSERT INTO ? values(?, ?, ?, ?)"; + int code = ws_stmt_prepare(stmt, sql, strlen(sql)); + check_error_code(stmt, code, "failed to execute ws_stmt_prepare"); + // bind table name + + code = ws_stmt_set_tbname(stmt, "t1"); + check_error_code(stmt, code, "failed to execute ws_stmt_set_tbname"); + + // insert two rows with multi binds + WS_MULTI_BIND params[4]; + // values to bind + int8_t data_count = 10; + int64_t ts[] = { + 1648432611249, + 1648432611749, + 1648432612249, + 1648432612749, + 1648432613249, + 1648432614249, + 1648432614749, + 1648432615249, + 1648432615749, + 1648432616249 + }; + float current[] = { + 10.3, + 12.6, + 11.1, + 13.2, + 10.9, + 9.8, + 11.5, + 13.7, + 12.0, + 10.1 + }; + int voltage[] = { + 219, + 218, + 220, + 222, + 217, + 221, + 215, + 219, + 220, + 222 + }; + float phase[] = { + 0.31, + 0.33, + 0.29, + 0.32, + 0.30, + 0.28, + 0.34, + 0.32, + 0.29, + 0.30 + }; + + char is_null[data_count]; + memset(is_null, 0, sizeof(is_null)); + // length array + int32_t int64Len[data_count]; + memset(int64Len, sizeof(int64_t), sizeof(int64Len)); + + int32_t floatLen[data_count]; + memset(floatLen, sizeof(float), sizeof(floatLen)); + + int32_t intLen[data_count]; + memset(intLen, sizeof(int), sizeof(intLen)); + + params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; + params[0].buffer_length = sizeof(int64_t); + params[0].buffer = ts; + params[0].length = int64Len; + params[0].is_null = is_null; + params[0].num = data_count; + + params[1].buffer_type = TSDB_DATA_TYPE_FLOAT; + params[1].buffer_length = sizeof(float); + params[1].buffer = current; + params[1].length = floatLen; + params[1].is_null = is_null; + params[1].num = data_count; + + params[2].buffer_type = TSDB_DATA_TYPE_INT; + params[2].buffer_length = sizeof(int); + params[2].buffer = voltage; + params[2].length = intLen; + params[2].is_null = is_null; + params[2].num = data_count; + + params[3].buffer_type = TSDB_DATA_TYPE_FLOAT; + params[3].buffer_length = sizeof(float); + params[3].buffer = phase; + params[3].length = floatLen; + params[3].is_null = is_null; + params[3].num = data_count; + + code = ws_stmt_bind_param_batch(stmt, params, 4); // bind batch + check_error_code(stmt, code, "failed to execute taos_stmt_bind_param_batch"); + code = ws_stmt_add_batch(stmt); // add batch + check_error_code(stmt, code, "failed to execute taos_stmt_add_batch"); + + // execute + int32_t affected_rows = 0; + code = ws_stmt_execute(stmt, &affected_rows); + check_error_code(stmt, code, "failed to execute ws_stmt_execute"); + + // close + ws_stmt_close(stmt); + + printf("successfully inserted %d rows\n", affected_rows); +} + +int main() +{ + WS_TAOS *taos = ws_connect_with_dsn("ws://localhost:6041"); + if (taos == NULL) + { + printf("failed to connect to server\n"); + exit(EXIT_FAILURE); + } + execute_sql(taos, "DROP DATABASE IF EXISTS power"); + execute_sql(taos, "CREATE DATABASE power"); + execute_sql(taos, "USE power"); + execute_sql(taos, + "CREATE TABLE t1 (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT)"); + insert_data(taos); + ws_close(taos); +}