From 4cab7ea4f7defac442d81fdcd74470919db099f5 Mon Sep 17 00:00:00 2001 From: Minglei Jin Date: Mon, 26 Aug 2024 16:45:18 +0800 Subject: [PATCH] stmt2/meta: tbname (multi-insert) parameterized or not --- include/client/taos.h | 1 + source/client/inc/clientStmt2.h | 5 +++-- source/client/src/clientMain.c | 2 ++ source/client/src/clientStmt2.c | 32 ++++++++++++++++++++++++++++++++ tests/script/api/stmt2-nohole.c | 16 ++++++++++++++-- 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/include/client/taos.h b/include/client/taos.h index 90d6712d6e..6a352de0ff 100644 --- a/include/client/taos.h +++ b/include/client/taos.h @@ -199,6 +199,7 @@ typedef enum { TAOS_FIELD_COL = 1, TAOS_FIELD_TAG, TAOS_FIELD_QUERY, + TAOS_FIELD_TBNAME, } TAOS_FIELD_T; typedef struct { diff --git a/source/client/inc/clientStmt2.h b/source/client/inc/clientStmt2.h index e54ac03c55..ad8938c474 100644 --- a/source/client/inc/clientStmt2.h +++ b/source/client/inc/clientStmt2.h @@ -218,12 +218,13 @@ int stmtExec2(TAOS_STMT2 *stmt, int *affected_rows); int stmtPrepare2(TAOS_STMT2 *stmt, const char *sql, unsigned long length); int stmtSetTbName2(TAOS_STMT2 *stmt, const char *tbName); int stmtSetTbTags2(TAOS_STMT2 *stmt, TAOS_STMT2_BIND *tags); +int stmtBindBatch2(TAOS_STMT2 *stmt, TAOS_STMT2_BIND *bind, int32_t colIdx); int stmtGetTagFields2(TAOS_STMT2 *stmt, int *nums, TAOS_FIELD_E **fields); int stmtGetColFields2(TAOS_STMT2 *stmt, int *nums, TAOS_FIELD_E **fields); -int stmtIsInsert2(TAOS_STMT2 *stmt, int *insert); int stmtGetParamNum2(TAOS_STMT2 *stmt, int *nums); +int stmtGetParamTbName(TAOS_STMT2 *stmt, int *nums); +int stmtIsInsert2(TAOS_STMT2 *stmt, int *insert); TAOS_RES *stmtUseResult2(TAOS_STMT2 *stmt); -int stmtBindBatch2(TAOS_STMT2 *stmt, TAOS_STMT2_BIND *bind, int32_t colIdx); const char *stmtErrstr2(TAOS_STMT2 *stmt); #ifdef __cplusplus diff --git a/source/client/src/clientMain.c b/source/client/src/clientMain.c index a81172a737..6fc6eb71b1 100644 --- a/source/client/src/clientMain.c +++ b/source/client/src/clientMain.c @@ -2005,6 +2005,8 @@ int taos_stmt2_get_fields(TAOS_STMT2 *stmt, TAOS_FIELD_T field_type, int *count, return stmtGetTagFields2(stmt, count, fields); } else if (field_type == TAOS_FIELD_QUERY) { return stmtGetParamNum2(stmt, count); + } else if (field_type == TAOS_FIELD_TBNAME) { + return stmtGetParamTbName(stmt, count); } else { tscError("invalid parameter for %s", __FUNCTION__); terrno = TSDB_CODE_INVALID_PARA; diff --git a/source/client/src/clientStmt2.c b/source/client/src/clientStmt2.c index 6b9978c2db..3964cb184f 100644 --- a/source/client/src/clientStmt2.c +++ b/source/client/src/clientStmt2.c @@ -1829,6 +1829,38 @@ int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) { return TSDB_CODE_SUCCESS; } + +int stmtGetParamTbName(TAOS_STMT2* stmt, int* nums) { + STscStmt2* pStmt = (STscStmt2*)stmt; + + STMT_DLOG_E("start to get param num"); + + if (pStmt->errCode != TSDB_CODE_SUCCESS) { + return pStmt->errCode; + } + + STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS)); + + if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 && + STMT_TYPE_MULTI_INSERT != pStmt->sql.type) { + pStmt->bInfo.needParse = false; + } + + if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) { + taos_free_result(pStmt->exec.pRequest); + pStmt->exec.pRequest = NULL; + } + + STMT_ERR_RET(stmtCreateRequest(pStmt)); + + if (pStmt->bInfo.needParse) { + STMT_ERR_RET(stmtParseSql(pStmt)); + } + + *nums = STMT_TYPE_MULTI_INSERT == pStmt->sql.type ? 1 : 0; + + return TSDB_CODE_SUCCESS; +} /* int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) { STscStmt2* pStmt = (STscStmt2*)stmt; diff --git a/tests/script/api/stmt2-nohole.c b/tests/script/api/stmt2-nohole.c index 743ec1e695..dbd441f136 100644 --- a/tests/script/api/stmt2-nohole.c +++ b/tests/script/api/stmt2-nohole.c @@ -70,6 +70,18 @@ void veriry_stmt(TAOS* taos) { } taos_free_result(result); + sql = + "create table m2 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, blob2 " + "nchar(10), 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]; @@ -254,7 +266,7 @@ void veriry_stmt(TAOS* taos) { blob2_buffer += blob_len2[i]; } - char* tbname = "m1"; + char* tbname = "m2"; TAOS_STMT2_BIND* bind_cols[1] = {¶ms[0]}; TAOS_STMT2_BINDV bindv = {1, &tbname, NULL, &bind_cols[0]}; start = clock(); @@ -277,7 +289,7 @@ void veriry_stmt(TAOS* taos) { TAOS_FIELD_E* fields = NULL; int field_count = -1; start = clock(); - code = taos_stmt2_get_fields(stmt, TAOS_FIELD_COL, &field_count, NULL); + code = taos_stmt2_get_fields(stmt, TAOS_FIELD_TBNAME, &field_count, NULL); end = clock(); printf("get fields time:%f\n", (double)(end - start) / CLOCKS_PER_SEC); if (code != 0) {