diff --git a/src/kit/taosdemo/taosdemo.c b/src/kit/taosdemo/taosdemo.c index cf84ecad43b953a2ba0432fee66992866eced324..1cdff80faa50b2168cfc350006b0f6cc85a48457 100644 --- a/src/kit/taosdemo/taosdemo.c +++ b/src/kit/taosdemo/taosdemo.c @@ -708,27 +708,24 @@ void *readTable(void *sarg) { sprintf(command, "select %s from %s%d where ts>= %" PRId64, aggreFunc[j], tb_prefix, i, sTime); double t = getCurrentTime(); - if (taos_query(taos, command) != 0) { - fprintf(stderr, "Failed to query\n"); - taos_close(taos); - exit(EXIT_FAILURE); - } + TAOS_RES *pSql = taos_query(taos, command); + int32_t code = taos_errno(pSql); - TAOS_RES *result = taos_use_result(taos); - if (result == NULL) { - fprintf(stderr, "Failed to retreive results:%s\n", taos_errstr(taos)); + if (code != 0) { + fprintf(stderr, "Failed to query:%s\n", taos_errstr(taos)); + taos_free_result(pSql); taos_close(taos); - exit(1); + exit(EXIT_FAILURE); } - while (taos_fetch_row(result) != NULL) { + while (taos_fetch_row(pSql) != NULL) { count++; } t = getCurrentTime() - t; totalT += t; - taos_free_result(result); + taos_free_result(pSql); } fprintf(fp, "|%10s | %10d | %12.2f | %10.2f |\n", @@ -779,20 +776,18 @@ void *readMetric(void *sarg) { fprintf(fp, "%s\n", command); double t = getCurrentTime(); - if (taos_query(taos, command) != 0) { - fprintf(stderr, "Failed to query\n"); - taos_close(taos); - exit(EXIT_FAILURE); - } - TAOS_RES *result = taos_use_result(taos); - if (result == NULL) { - fprintf(stderr, "Failed to retreive results:%s\n", taos_errstr(taos)); + TAOS_RES *pSql = taos_query(taos, command); + int32_t code = taos_errno(pSql); + + if (code != 0) { + fprintf(stderr, "Failed to query:%s\n", taos_errstr(taos)); + taos_free_result(pSql); taos_close(taos); exit(1); } int count = 0; - while (taos_fetch_row(result) != NULL) { + while (taos_fetch_row(pSql) != NULL) { count++; } t = getCurrentTime() - t; @@ -800,7 +795,7 @@ void *readMetric(void *sarg) { fprintf(fp, "| Speed: %12.2f(per s) | Latency: %.4f(ms) |\n", num_of_tables * num_of_DPT / t, t * 1000); printf("select %10s took %.6f second(s)\n\n", aggreFunc[j], t); - taos_free_result(result); + taos_free_result(pSql); } fprintf(fp, "\n"); } @@ -811,10 +806,19 @@ void *readMetric(void *sarg) { void queryDB(TAOS *taos, char *command) { int i = 5; - while (i > 0) { - if (taos_query(taos, command) == 0) break; + TAOS_RES *pSql = NULL; + int32_t code = -1; + while (i > 0 && code != 0) { + pSql = taos_query(taos, command); + code = taos_errno(pSql); + taos_free_result(pSql); + pSql = NULL; + if (code == 0) { + break; + } i--; } + if (i == 0) { fprintf(stderr, "Failed to run %s, reason: %s\n", command, taos_errstr(taos)); taos_close(taos); @@ -947,6 +951,7 @@ void callBack(void *param, TAOS_RES *res, int code) { break; } } + tb_info->timestamp = tmp_time; taos_query_a(tb_info->taos, buffer, callBack, tb_info); diff --git a/src/kit/taosdump/taosdump.c b/src/kit/taosdump/taosdump.c index 9886a91f48242191a6259a9d15bbc1f3cefc0a81..6b7ffb200155e293ae7e7881d8408335600ffeb7 100644 --- a/src/kit/taosdump/taosdump.c +++ b/src/kit/taosdump/taosdump.c @@ -372,14 +372,12 @@ int taosGetTableRecordInfo(char *table, STableRecordInfo *pTableRecordInfo) { memset(pTableRecordInfo, 0, sizeof(STableRecordInfo)); sprintf(command, "show tables like %s", table); - if (taos_query(taos, command) != 0) { - fprintf(stderr, "failed to run command %s\n", command); - return -1; - } + TAOS_RES *result = taos_query(taos, command);\ + int32_t code = taos_errno(result); - result = taos_use_result(taos); - if (result == NULL) { - fprintf(stderr, "failed to use result\n"); + if (code != 0) { + fprintf(stderr, "failed to run command %s, error: %s\n", command, taos_errstr(result)); + taos_free_result(result); return -1; } @@ -400,14 +398,12 @@ int taosGetTableRecordInfo(char *table, STableRecordInfo *pTableRecordInfo) { if (isSet) return 0; sprintf(command, "show stables like %s", table); - if (taos_query(taos, command) != 0) { - fprintf(stderr, "failed to run command %s\n", command); - return -1; - } - result = taos_use_result(taos); - if (result == NULL) { - fprintf(stderr, "failed to use result\n"); + result = taos_query(taos, command); + code = taos_errno(result); + if (code != 0) { + fprintf(stderr, "failed to run command %s, error: %s\n", command, taos_errstr(result)); + taos_free_result(result); return -1; } @@ -467,14 +463,11 @@ int taosDumpOut(SDumpArguments *arguments) { taosDumpCharset(fp); sprintf(command, "show databases"); - if (taos_query(taos, command) != 0) { + result = taos_query(taos, command); + int32_t code = taos_errno(result); + if (code != 0) { fprintf(stderr, "failed to run command: %s, reason: %s\n", command, taos_errstr(taos)); - goto _exit_failure; - } - - result = taos_use_result(taos); - if (result == NULL) { - fprintf(stderr, "failed to use result\n"); + taos_free_result(result); goto _exit_failure; } @@ -612,7 +605,7 @@ int taosDumpDb(SDbInfo *dbInfo, SDumpArguments *arguments, FILE *fp) { taosDumpCreateDbClause(dbInfo, arguments->with_property, fp); sprintf(command, "use %s", dbInfo->name); - if (taos_query(taos, command) != 0) { + if (taos_errno(taos_query(taos, command)) != 0) { fprintf(stderr, "invalid database %s\n", dbInfo->name); return -1; } @@ -620,14 +613,11 @@ int taosDumpDb(SDbInfo *dbInfo, SDumpArguments *arguments, FILE *fp) { fprintf(fp, "USE %s\n\n", dbInfo->name); sprintf(command, "show tables"); - if (taos_query(taos, command) != 0) { - fprintf(stderr, "failed to run command %s\n", command); - return -1; - } - - result = taos_use_result(taos); - if (result == NULL) { - fprintf(stderr, "failed to use result\n"); + result = taos_query(taos,command); + int32_t code = taos_errno(result); + if (code != 0) { + fprintf(stderr, "failed to run command %s, error: %s\n", command, taos_errstr(result)); + taos_free_result(result); return -1; } @@ -725,14 +715,11 @@ void taosDumpCreateMTableClause(STableDef *tableDes, char *metric, int numOfCols TAOS_ROW row = NULL; sprintf(command, "select %s from %s limit 1", tableDes->cols[counter].field, tableDes->name); - if (taos_query(taos, command) != 0) { - fprintf(stderr, "failed to run command %s\n", command); - return; - } - result = taos_use_result(taos); - if (result == NULL) { - fprintf(stderr, "failed to use result\n"); + result = taos_query(taos, command); + int32_t code = taos_errno(result); + if (code != 0) { + fprintf(stderr, "failed to run command %s, error: %s\n", command, taos_errstr(result)); return; } @@ -806,14 +793,12 @@ int taosGetTableDes(char *table, STableDef *tableDes) { int count = 0; sprintf(command, "describe %s", table); - if (taos_query(taos, command) != 0) { - fprintf(stderr, "failed to run command %s\n", command); - return -1; - } - result = taos_use_result(taos); - if (result == NULL) { - fprintf(stderr, "failed to use result\n"); + result = taos_query(taos, command); + int32_t code = taos_errno(result); + if (code != 0) { + fprintf(stderr, "failed to run command %s, error: %s\n", command, taos_errstr(result)); + taos_free_result(result); return -1; } @@ -889,14 +874,11 @@ int32_t taosDumpMetric(char *metric, SDumpArguments *arguments, FILE *fp) { strcpy(tableRecord.metric, metric); sprintf(command, "select tbname from %s", metric); - if (taos_query(taos, command) != 0) { - fprintf(stderr, "failed to run command %s\n", command); - return -1; - } - - result = taos_use_result(taos); - if (result == NULL) { - fprintf(stderr, "failed to use result\n"); + result = taos_query(taos, command); + int32_t code = taos_errno(result); + if (code != 0) { + fprintf(stderr, "failed to run command %s, error: %s\n", command, taos_errstr(result)); + taos_free_result(result); return -1; } @@ -942,18 +924,16 @@ int taosDumpTableData(FILE *fp, char *tbname, SDumpArguments *arguments) { sprintf(command, "select * from %s where _c0 >= %" PRId64 " and _c0 <= %" PRId64 " order by _c0 asc", tbname, arguments->start_time, arguments->end_time); - if (taos_query(taos, command) != 0) { - fprintf(stderr, "failed to run command %s, reason: %s\n", command, taos_errstr(taos)); - return -1; - } - - result = taos_use_result(taos); - if (result == NULL) { - fprintf(stderr, "failed to use result\n"); + + result = taos_query(taos, command); + int32_t code = taos_errno(result); + if (code != 0) { + fprintf(stderr, "failed to run command %s, reason: %s\n", command, taos_errstr(result)); + taos_free_result(result); return -1; } - numFields = taos_field_count(taos); + numFields = taos_field_count(result); assert(numFields > 0); TAOS_FIELD *fields = taos_fetch_fields(result); tbuf = (char *)malloc(COMMAND_SIZE); @@ -1242,9 +1222,14 @@ int taosDumpIn(SDumpArguments *arguments) { tcommand = command; } taosReplaceCtrlChar(tcommand); - if (taos_query(taos, tcommand) != 0) + result = taos_query(taos, tcommand); + int32_t code = taos_errno(result); + if (code != 0) + { fprintf(stderr, "linenu:%" PRId64 " failed to run command %s reason: %s \ncontinue...\n", linenu, command, taos_errstr(taos)); + } + taos_free_result(result); } pstr = command;