• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4892

20 Dec 2025 01:15PM UTC coverage: 65.571% (+0.02%) from 65.549%
#4892

push

travis-ci

web-flow
feat: support taos_connect_with func (#33952)

* feat: support taos_connect_with

* refactor: enhance connection options and add tests for taos_set_option and taos_connect_with

* fix: handle NULL keys and values in taos_connect_with options

* fix: revert TAOSWS_GIT_TAG to default value "main"

* docs: add TLS configuration options for WebSocket connections in documentation

* docs: modify zh docs and add en docs

* chore: update taos.cfg

* docs: add examples

* docs: add error handling for connection failure in example code

2 of 82 new or added lines in 3 files covered. (2.44%)

527 existing lines in 120 files now uncovered.

182859 of 278870 relevant lines covered (65.57%)

104634355.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

37.06
/source/client/src/clientMain.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "catalog.h"
17
#include "clientInt.h"
18
#include "clientLog.h"
19
#include "clientMonitor.h"
20
#include "clientStmt.h"
21
#include "clientStmt2.h"
22
#include "functionMgt.h"
23
#include "os.h"
24
#include "query.h"
25
#include "scheduler.h"
26
#include "tcompare.h"
27
#include "tconv.h"
28
#include "tdatablock.h"
29
#include "tglobal.h"
30
#include "tmisce.h"
31
#include "tmsg.h"
32
#include "tref.h"
33
#include "trpc.h"
34
#include "tversion.h"
35
#include "version.h"
36
#include "clientSession.h"
37
#include "ttime.h"
38

39
#define TSC_VAR_NOT_RELEASE 1
40
#define TSC_VAR_RELEASED    0
41

42
#ifdef TAOSD_INTEGRATED
43
extern void shellStopDaemon();
44
#endif
45

46
static int32_t sentinel = TSC_VAR_NOT_RELEASE;
47
static int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper);
48

49
int taos_options(TSDB_OPTION option, const void *arg, ...) {
573,101✔
50
  if (arg == NULL) {
573,101✔
51
    return TSDB_CODE_INVALID_PARA;
×
52
  }
53
  static int32_t lock = 0;
54

55
  for (int i = 1; atomic_val_compare_exchange_32(&lock, 0, 1) != 0; ++i) {
573,101✔
UNCOV
56
    if (i % 1000 == 0) {
×
UNCOV
57
      (void)sched_yield();
×
58
    }
59
  }
60

61
  int ret = taos_options_imp(option, (const char *)arg);
573,101✔
62
  atomic_store_32(&lock, 0);
573,101✔
63
  return ret;
573,101✔
64
}
65

66
#if !defined(WINDOWS) && !defined(TD_ASTRA)
67
static void freeTz(void *p) {
×
68
  timezone_t tz = *(timezone_t *)p;
×
69
  tzfree(tz);
×
70
}
×
71

72
int32_t tzInit() {
1,289,876✔
73
  pTimezoneMap = taosHashInit(0, MurmurHash3_32, false, HASH_ENTRY_LOCK);
1,289,876✔
74
  if (pTimezoneMap == NULL) {
1,289,876✔
75
    return terrno;
×
76
  }
77
  taosHashSetFreeFp(pTimezoneMap, freeTz);
1,289,876✔
78

79
  pTimezoneNameMap = taosHashInit(0, taosIntHash_64, false, HASH_ENTRY_LOCK);
1,289,876✔
80
  if (pTimezoneNameMap == NULL) {
1,289,876✔
81
    return terrno;
×
82
  }
83
  return 0;
1,289,876✔
84
}
85

86
void tzCleanup() {
1,289,911✔
87
  taosHashCleanup(pTimezoneMap);
1,289,911✔
88
  taosHashCleanup(pTimezoneNameMap);
1,289,911✔
89
}
1,289,911✔
90

91
static timezone_t setConnnectionTz(const char *val) {
×
92
  timezone_t  tz = NULL;
×
93
  timezone_t *tmp = taosHashGet(pTimezoneMap, val, strlen(val));
×
94
  if (tmp != NULL && *tmp != NULL) {
×
95
    tz = *tmp;
×
96
    goto END;
×
97
  }
98

99
  tscDebug("set timezone to %s", val);
×
100
  tz = tzalloc(val);
×
101
  if (tz == NULL) {
×
102
    tscWarn("%s unknown timezone %s change to UTC", __func__, val);
×
103
    tz = tzalloc("UTC");
×
104
    if (tz == NULL) {
×
105
      tscError("%s set timezone UTC error", __func__);
×
106
      terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
107
      goto END;
×
108
    }
109
  }
110
  int32_t code = taosHashPut(pTimezoneMap, val, strlen(val), &tz, sizeof(timezone_t));
×
111
  if (code != 0) {
×
112
    tscError("%s put timezone to tz map error:%d", __func__, code);
×
113
    tzfree(tz);
×
114
    tz = NULL;
×
115
    goto END;
×
116
  }
117

118
  time_t tx1 = taosGetTimestampSec();
×
119
  char   output[TD_TIMEZONE_LEN] = {0};
×
120
  code = taosFormatTimezoneStr(tx1, val, tz, output);
×
121
  if (code == 0) {
×
122
    code = taosHashPut(pTimezoneNameMap, &tz, sizeof(timezone_t), output, strlen(output) + 1);
×
123
  }
124
  if (code != 0) {
×
125
    tscError("failed to put timezone %s to map", val);
×
126
  }
127

128
END:
×
129
  return tz;
×
130
}
131
#endif
132

133
static int32_t setConnectionOption(TAOS *taos, TSDB_OPTION_CONNECTION option, const char *val) {
×
134
  if (taos == NULL) {
×
135
    return terrno = TSDB_CODE_INVALID_PARA;
×
136
  }
137

138
#ifdef WINDOWS
139
  if (option == TSDB_OPTION_CONNECTION_TIMEZONE) {
140
    return terrno = TSDB_CODE_NOT_SUPPORTTED_IN_WINDOWS;
141
  }
142
#endif
143

144
  if (option < TSDB_OPTION_CONNECTION_CLEAR || option >= TSDB_MAX_OPTIONS_CONNECTION) {
×
145
    return terrno = TSDB_CODE_INVALID_PARA;
×
146
  }
147

148
  int32_t code = taos_init();
×
149
  // initialize global config
150
  if (code != 0) {
×
151
    return terrno = code;
×
152
  }
153

154
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
155
  if (NULL == pObj) {
×
156
    tscError("invalid parameter for %s", __func__);
×
157
    return terrno;
×
158
  }
159

160
  if (option == TSDB_OPTION_CONNECTION_CLEAR) {
×
161
    val = NULL;
×
162
  }
163

164
#ifndef DISALLOW_NCHAR_WITHOUT_ICONV
165
  if (option == TSDB_OPTION_CONNECTION_CHARSET || option == TSDB_OPTION_CONNECTION_CLEAR) {
×
166
    if (val != NULL) {
×
167
      if (!taosValidateEncodec(val)) {
×
168
        code = terrno;
×
169
        goto END;
×
170
      }
171
      void *tmp = taosConvInit(val);
×
172
      if (tmp == NULL) {
×
173
        code = terrno;
×
174
        goto END;
×
175
      }
176
      pObj->optionInfo.charsetCxt = tmp;
×
177
    } else {
178
      pObj->optionInfo.charsetCxt = NULL;
×
179
    }
180
  }
181
#endif
182
  if (option == TSDB_OPTION_CONNECTION_TIMEZONE || option == TSDB_OPTION_CONNECTION_CLEAR) {
×
183
#if !defined(WINDOWS) && !defined(TD_ASTRA)
184
    if (val != NULL) {
×
185
      if (val[0] == 0) {
×
186
        val = "UTC";
×
187
      }
188
      timezone_t tz = setConnnectionTz(val);
×
189
      if (tz == NULL) {
×
190
        code = terrno;
×
191
        goto END;
×
192
      }
193
      pObj->optionInfo.timezone = tz;
×
194
    } else {
195
      pObj->optionInfo.timezone = NULL;
×
196
    }
197
#endif
198
  }
199

200
  if (option == TSDB_OPTION_CONNECTION_USER_APP || option == TSDB_OPTION_CONNECTION_CLEAR) {
×
201
    if (val != NULL) {
×
202
      tstrncpy(pObj->optionInfo.userApp, val, sizeof(pObj->optionInfo.userApp));
×
203
    } else {
204
      pObj->optionInfo.userApp[0] = 0;
×
205
    }
206
  }
207

208
  if (option == TSDB_OPTION_CONNECTION_CONNECTOR_INFO || option == TSDB_OPTION_CONNECTION_CLEAR) {
×
209
    if (val != NULL) {
×
210
      tstrncpy(pObj->optionInfo.cInfo, val, sizeof(pObj->optionInfo.cInfo));
×
211
    } else {
212
      pObj->optionInfo.cInfo[0] = 0;
×
213
    }
214
  }
215

216
  if (option == TSDB_OPTION_CONNECTION_USER_IP || option == TSDB_OPTION_CONNECTION_CLEAR) {
×
217
    SIpRange dualIp = {0};
×
218
    if (val != NULL) {
×
219
      pObj->optionInfo.userIp = taosInetAddr(val);
×
220
      SIpAddr addr = {0};
×
221
      code = taosGetIpFromFqdn(tsEnableIpv6, val, &addr);
×
222
      if (code == 0) {
×
223
        code = tIpStrToUint(&addr, &pObj->optionInfo.userDualIp);
×
224
      }
225
      if (code != 0) {
×
226
        tscError("ipv6 flag %d failed to convert user ip %s to dual ip since %s", tsEnableIpv6 ? 1 : 0, val,
×
227
                 tstrerror(code));
228
        pObj->optionInfo.userIp = INADDR_NONE;
×
229
        pObj->optionInfo.userDualIp = dualIp;
×
230
        code = 0;
×
231
      }
232
    } else {
233
      pObj->optionInfo.userIp = INADDR_NONE;
×
234
      pObj->optionInfo.userDualIp = dualIp;
×
235
    }
236
  }
237

238
END:
×
239
  releaseTscObj(*(int64_t *)taos);
×
240
  return terrno = code;
×
241
}
242

243
int taos_options_connection(TAOS *taos, TSDB_OPTION_CONNECTION option, const void *arg, ...) {
×
244
  return setConnectionOption(taos, option, (const char *)arg);
×
245
}
246

247
// this function may be called by user or system, or by both simultaneously.
248
void taos_cleanup(void) {
1,290,678✔
249
  tscInfo("start to cleanup client environment");
1,290,678✔
250
  if (atomic_val_compare_exchange_32(&sentinel, TSC_VAR_NOT_RELEASE, TSC_VAR_RELEASED) != TSC_VAR_NOT_RELEASE) {
1,290,678✔
251
    return;
767✔
252
  }
253

254
  monitorClose();
1,289,911✔
255
  tscStopCrashReport();
1,289,911✔
256

257
  hbMgrCleanUp();
1,289,911✔
258

259
  catalogDestroy();
1,289,911✔
260
  schedulerDestroy();
1,289,911✔
261

262
  fmFuncMgtDestroy();
1,289,911✔
263
  qCleanupKeywordsTable();
1,289,911✔
264

265
#if !defined(WINDOWS) && !defined(TD_ASTRA)
266
  tzCleanup();
1,289,911✔
267
#endif
268
  tmqMgmtClose();
1,289,911✔
269

270
  int32_t id = clientReqRefPool;
1,289,911✔
271
  clientReqRefPool = -1;
1,289,911✔
272
  taosCloseRef(id);
1,289,911✔
273

274
  id = clientConnRefPool;
1,289,911✔
275
  clientConnRefPool = -1;
1,289,911✔
276
  taosCloseRef(id);
1,289,911✔
277

278
  nodesDestroyAllocatorSet();
1,289,911✔
279
  cleanupAppInfo();
1,289,911✔
280
  rpcCleanup();
1,289,911✔
281
  tscDebug("rpc cleanup");
1,289,911✔
282

283
  if (TSDB_CODE_SUCCESS != cleanupTaskQueue()) {
1,289,911✔
284
    tscWarn("failed to cleanup task queue");
×
285
  }
286

287
  sessMgtDestroy();
1,289,911✔
288

289
  taosConvDestroy();
1,289,911✔
290
  DestroyRegexCache();
1,289,911✔
291
#ifdef TAOSD_INTEGRATED
292
  shellStopDaemon();
293
#endif
294
  tscInfo("all local resources released");
1,289,911✔
295
  taosCleanupCfg();
1,289,911✔
296
#ifndef TAOSD_INTEGRATED
297
  taosCloseLog();
1,289,911✔
298
#endif
299
}
300

301
static setConfRet taos_set_config_imp(const char *config) {
56✔
302
  setConfRet ret = {SET_CONF_RET_SUCC, {0}};
56✔
303
  // TODO: need re-implementation
304
  return ret;
56✔
305
}
306

307
setConfRet taos_set_config(const char *config) {
56✔
308
  // TODO  pthread_mutex_lock(&setConfMutex);
309
  setConfRet ret = taos_set_config_imp(config);
56✔
310
  //  pthread_mutex_unlock(&setConfMutex);
311
  return ret;
56✔
312
}
313

314
TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) {
2,828,544✔
315
  tscInfo("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
2,828,544✔
316
  if (user == NULL) {
2,828,916✔
317
    user = TSDB_DEFAULT_USER;
276,654✔
318
  }
319

320
  if (pass == NULL) {
2,828,916✔
321
    pass = TSDB_DEFAULT_PASS;
276,654✔
322
  }
323

324
  STscObj *pObj = NULL;
2,828,916✔
325
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, NULL, db, port, CONN_TYPE__QUERY, &pObj);
2,829,538✔
326
  if (TSDB_CODE_SUCCESS == code) {
2,829,246✔
327
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
2,823,721✔
328
    if (NULL == rid) {
2,823,721✔
329
      tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db);
×
330
      return NULL;
×
331
    }
332
    *rid = pObj->id;
2,823,721✔
333
    return (TAOS *)rid;
2,823,721✔
334
  } else {
335
    terrno = code;
5,525✔
336
  }
337

338
  return NULL;
5,525✔
339
}
340

NEW
341
void taos_set_option(OPTIONS *options, const char *key, const char *value) {
×
NEW
342
  if (options == NULL || key == NULL || value == NULL) {
×
NEW
343
    terrno = TSDB_CODE_INVALID_PARA;
×
NEW
344
    tscError("taos_set_option invalid parameter, options: %p, key: %p, value: %p", options, key, value);
×
NEW
345
    return;
×
346
  }
347

NEW
348
  size_t count = (size_t)options->count;
×
NEW
349
  size_t len = sizeof(options->keys) / sizeof(options->keys[0]);
×
NEW
350
  if (count >= len) {
×
NEW
351
    terrno = TSDB_CODE_INVALID_PARA;
×
NEW
352
    tscError("taos_set_option overflow, count: %zu, reached capacity: %zu", count, len);
×
NEW
353
    return;
×
354
  }
355

NEW
356
  options->keys[count] = key;
×
NEW
357
  options->values[count] = value;
×
NEW
358
  options->count = (uint16_t)(count + 1);
×
359
}
360

NEW
361
static int set_connection_option_or_close(TAOS *taos, TSDB_OPTION_CONNECTION option, const char *value) {
×
NEW
362
  if (value == NULL) return TSDB_CODE_SUCCESS;
×
NEW
363
  int code = taos_options_connection(taos, option, value);
×
NEW
364
  if (code != TSDB_CODE_SUCCESS) {
×
NEW
365
    tscError("failed to set option(%d): %s", (int)option, value);
×
NEW
366
    taos_close(taos);
×
NEW
367
    return code;
×
368
  }
NEW
369
  return TSDB_CODE_SUCCESS;
×
370
}
371

NEW
372
TAOS *taos_connect_with(const OPTIONS *options) {
×
NEW
373
  const char *ip = NULL;
×
NEW
374
  const char *user = NULL;
×
NEW
375
  const char *pass = NULL;
×
NEW
376
  const char *db = NULL;
×
NEW
377
  uint16_t port = 0;
×
378

NEW
379
  const char *charset = NULL;
×
NEW
380
  const char *timezone = NULL;
×
NEW
381
  const char *userIp = NULL;
×
NEW
382
  const char *userApp = NULL;
×
NEW
383
  const char *connectorInfo = NULL;
×
384

NEW
385
  if (options && options->count > 0) {
×
NEW
386
    size_t count = (size_t)options->count;
×
NEW
387
    for (size_t i = 0; i < count; ++i) {
×
NEW
388
      const char *key = options->keys[i];
×
NEW
389
      const char *value = options->values[i];
×
NEW
390
      if (key == NULL || value == NULL) {
×
NEW
391
        tscWarn("taos_connect_with option key or value is NULL, index: %zu", i);
×
NEW
392
        continue;
×
393
      }
394

NEW
395
      if (strcmp(key, "ip") == 0) {
×
NEW
396
        ip = value;
×
NEW
397
      } else if (strcmp(key, "user") == 0) {
×
NEW
398
        user = value;
×
NEW
399
      } else if (strcmp(key, "pass") == 0) {
×
NEW
400
        pass = value;
×
NEW
401
      } else if (strcmp(key, "db") == 0) {
×
NEW
402
        db = value;
×
NEW
403
      } else if (strcmp(key, "port") == 0) {
×
NEW
404
        port = (uint16_t)atoi(value);
×
NEW
405
      } else if (strcmp(key, "charset") == 0) {
×
NEW
406
        charset = value;
×
NEW
407
      } else if (strcmp(key, "timezone") == 0) {
×
NEW
408
        timezone = value;
×
NEW
409
      } else if (strcmp(key, "userIp") == 0) {
×
NEW
410
        userIp = value;
×
NEW
411
      } else if (strcmp(key, "userApp") == 0) {
×
NEW
412
        userApp = value;
×
NEW
413
      } else if (strcmp(key, "connectorInfo") == 0) {
×
NEW
414
        connectorInfo = value;
×
415
      } else {
NEW
416
        tscWarn("taos_connect_with unknown option key: %s", key);
×
417
      }
418
    }
419
  }
420

NEW
421
  TAOS* taos = taos_connect(ip, user, pass, db, port);
×
NEW
422
  if (taos == NULL) return NULL;
×
423

NEW
424
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_CHARSET, charset) != TSDB_CODE_SUCCESS) return NULL;
×
NEW
425
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_TIMEZONE, timezone) != TSDB_CODE_SUCCESS) return NULL;
×
NEW
426
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_USER_IP, userIp) != TSDB_CODE_SUCCESS) return NULL;
×
NEW
427
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_USER_APP, userApp) != TSDB_CODE_SUCCESS) return NULL;
×
NEW
428
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_CONNECTOR_INFO, connectorInfo) != TSDB_CODE_SUCCESS) return NULL;
×
429

NEW
430
  return taos;
×
431
}
432

433
TAOS *taos_connect_with_dsn(const char *dsn) {
×
434
  terrno = TSDB_CODE_OPS_NOT_SUPPORT;
×
435
  tscError("taos_connect_with_dsn not supported");
×
436
  return NULL;
×
437
}
438

439
int taos_set_notify_cb(TAOS *taos, __taos_notify_fn_t fp, void *param, int type) {
2,660✔
440
  if (taos == NULL) {
2,660✔
441
    terrno = TSDB_CODE_INVALID_PARA;
×
442
    return terrno;
×
443
  }
444

445
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2,660✔
446
  if (NULL == pObj) {
2,660✔
447
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
448
    tscError("invalid parameter for %s", __func__);
×
449
    return terrno;
×
450
  }
451

452
  switch (type) {
2,660✔
453
    case TAOS_NOTIFY_PASSVER: {
760✔
454
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
760✔
455
      pObj->passInfo.fp = fp;
760✔
456
      pObj->passInfo.param = param;
760✔
457
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
760✔
458
      break;
760✔
459
    }
460
    case TAOS_NOTIFY_WHITELIST_VER: {
×
461
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
462
      pObj->whiteListInfo.fp = fp;
×
463
      pObj->whiteListInfo.param = param;
×
464
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
465
      break;
×
466
    }
467
    case TAOS_NOTIFY_USER_DROPPED: {
1,900✔
468
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
1,900✔
469
      pObj->userDroppedInfo.fp = fp;
1,900✔
470
      pObj->userDroppedInfo.param = param;
1,900✔
471
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
1,900✔
472
      break;
1,900✔
473
    }
474
    case TAOS_NOTIFY_DATETIME_WHITELIST_VER: {
×
475
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
476
      pObj->dateTimeWhiteListInfo.fp = fp;
×
477
      pObj->dateTimeWhiteListInfo.param = param;
×
478
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
479
      break;
×
480
    }
481
    default: {
×
482
      terrno = TSDB_CODE_INVALID_PARA;
×
483
      releaseTscObj(*(int64_t *)taos);
×
484
      return terrno;
×
485
    }
486
  }
487

488
  releaseTscObj(*(int64_t *)taos);
2,660✔
489
  return 0;
2,660✔
490
}
491

492
typedef struct SFetchWhiteListInfo {
493
  int64_t                     connId;
494
  __taos_async_whitelist_fn_t userCbFn;
495
  void                       *userParam;
496
} SFetchWhiteListInfo;
497

498
int32_t fetchWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
499
  SFetchWhiteListInfo *pInfo = (SFetchWhiteListInfo *)param;
×
500
  TAOS                *taos = &pInfo->connId;
×
501
  if (code != TSDB_CODE_SUCCESS) {
×
502
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
503
    taosMemoryFree(pMsg->pData);
×
504
    taosMemoryFree(pMsg->pEpSet);
×
505
    taosMemoryFree(pInfo);
×
506
    return code;
×
507
  }
508

509
  SGetUserIpWhiteListRsp wlRsp;
×
510
  if (TSDB_CODE_SUCCESS != tDeserializeSGetUserIpWhiteListRsp(pMsg->pData, pMsg->len, &wlRsp)) {
×
511
    taosMemoryFree(pMsg->pData);
×
512
    taosMemoryFree(pMsg->pEpSet);
×
513
    taosMemoryFree(pInfo);
×
514
    tFreeSGetUserIpWhiteListRsp(&wlRsp);
×
515
    return terrno;
×
516
  }
517

518
  uint64_t *pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(uint64_t));
×
519
  if (pWhiteLists == NULL) {
×
520
    taosMemoryFree(pMsg->pData);
×
521
    taosMemoryFree(pMsg->pEpSet);
×
522
    taosMemoryFree(pInfo);
×
523
    tFreeSGetUserIpWhiteListRsp(&wlRsp);
×
524
    return terrno;
×
525
  }
526

527
  for (int i = 0; i < wlRsp.numWhiteLists; ++i) {
×
528
    pWhiteLists[i] = ((uint64_t)wlRsp.pWhiteLists[i].mask << 32) | wlRsp.pWhiteLists[i].ip;
×
529
  }
530

531
  pInfo->userCbFn(pInfo->userParam, code, taos, wlRsp.numWhiteLists, pWhiteLists);
×
532

533
  taosMemoryFree(pWhiteLists);
×
534
  taosMemoryFree(pMsg->pData);
×
535
  taosMemoryFree(pMsg->pEpSet);
×
536
  taosMemoryFree(pInfo);
×
537
  tFreeSGetUserIpWhiteListRsp(&wlRsp);
×
538
  return code;
×
539
}
540

541
void taos_fetch_whitelist_a(TAOS *taos, __taos_async_whitelist_fn_t fp, void *param) {
×
542
  if (NULL == taos) {
×
543
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
544
    return;
×
545
  }
546

547
  int64_t connId = *(int64_t *)taos;
×
548

549
  STscObj *pTsc = acquireTscObj(connId);
×
550
  if (NULL == pTsc) {
×
551
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
552
    return;
×
553
  }
554

555
  SGetUserWhiteListReq req;
×
556
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
557
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
558
  if (msgLen < 0) {
×
559
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
560
    releaseTscObj(connId);
×
561
    return;
×
562
  }
563

564
  void *pReq = taosMemoryMalloc(msgLen);
×
565
  if (pReq == NULL) {
×
566
    fp(param, terrno, taos, 0, NULL);
×
567
    releaseTscObj(connId);
×
568
    return;
×
569
  }
570

571
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
572
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
573
    taosMemoryFree(pReq);
×
574
    releaseTscObj(connId);
×
575
    return;
×
576
  }
577

578
  SFetchWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchWhiteListInfo));
×
579
  if (pParam == NULL) {
×
580
    fp(param, terrno, taos, 0, NULL);
×
581
    taosMemoryFree(pReq);
×
582
    releaseTscObj(connId);
×
583
    return;
×
584
  }
585

586
  pParam->connId = connId;
×
587
  pParam->userCbFn = fp;
×
588

589
  pParam->userParam = param;
×
590
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
591
  if (pSendInfo == NULL) {
×
592
    fp(param, terrno, taos, 0, NULL);
×
593
    taosMemoryFree(pParam);
×
594
    taosMemoryFree(pReq);
×
595
    releaseTscObj(connId);
×
596
    return;
×
597
  }
598

599
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
600
  pSendInfo->requestId = generateRequestId();
×
601
  pSendInfo->requestObjRefId = 0;
×
602
  pSendInfo->param = pParam;
×
603
  pSendInfo->fp = fetchWhiteListCallbackFn;
×
604
  pSendInfo->msgType = TDMT_MND_GET_USER_IP_WHITELIST;
×
605

606
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
607
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
608
    tscWarn("failed to async send msg to server");
×
609
  }
610
  releaseTscObj(connId);
×
611
  return;
×
612
}
613

614

615

616
typedef struct SFetchIpWhiteListInfo {
617
  int64_t connId;
618
  bool supportNeg;
619
  void   *userParam;
620

621
  __taos_async_ip_whitelist_fn_t userCbFn;
622
} SFetchIpWhiteListInfo;
623

624

625

626
int32_t fetchIpWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
627
  int32_t lino = 0;
×
628
  char  **pWhiteLists = NULL;
×
629

630
  SGetUserIpWhiteListRsp wlRsp = {0};
×
631

632
  SFetchIpWhiteListInfo *pInfo = (SFetchIpWhiteListInfo *)param;
×
633
  TAOS *taos = &pInfo->connId;
×
634

635
  if (code != TSDB_CODE_SUCCESS) {
×
636
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
637
    TAOS_CHECK_GOTO(code, &lino, _error);
×
638
  }
639

640
  if ((code = tDeserializeSGetUserIpWhiteListDualRsp(pMsg->pData, pMsg->len, &wlRsp)) != TSDB_CODE_SUCCESS) {
×
641
    TAOS_CHECK_GOTO(code, &lino, _error);
×
642
  }
643

644
  pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(char *));
×
645
  if (pWhiteLists == NULL) {
×
646
    code = terrno;
×
647
    TAOS_CHECK_GOTO(code, &lino, _error);
×
648
  }
649

650
  int32_t numWhiteLists =0;
×
651
  for (int32_t i = 0; i < wlRsp.numWhiteLists; i++) {
×
652
    SIpRange *pIpRange = &wlRsp.pWhiteListsDual[i];
×
653
    if (!pInfo->supportNeg && pIpRange->neg) {
×
654
      continue;
×
655
    }
656
    SIpAddr   ipAddr = {0};
×
657

658
    code = tIpUintToStr(pIpRange, &ipAddr);
×
659
    TAOS_CHECK_GOTO(code, &lino, _error);
×
660

661
    char *ip = taosMemCalloc(1, IP_RESERVE_CAP);
×
662
    if (ip == NULL) {
×
663
      code = terrno;
×
664
      TAOS_CHECK_GOTO(code, &lino, _error);
×
665
    }
666
    if (ipAddr.type == 0) {
×
667
      if (pInfo->supportNeg) {
×
668
        snprintf(ip, IP_RESERVE_CAP, "%c %s/%d", pIpRange->neg ? '-' : '+', ipAddr.ipv4, ipAddr.mask);
×
669
      } else {
670
        snprintf(ip, IP_RESERVE_CAP, "%s/%d", ipAddr.ipv4, ipAddr.mask);
×
671
      }
672
    } else {
673
      if (ipAddr.ipv6[0] == 0) {
×
674
        memcpy(ipAddr.ipv6, "::", 2);
×
675
      }
676
      if (pInfo->supportNeg) {
×
677
        snprintf(ip, IP_RESERVE_CAP, "%c %s/%d", pIpRange->neg ? '-' : '+', ipAddr.ipv6, ipAddr.mask);
×
678
      } else {
679
        snprintf(ip, IP_RESERVE_CAP, "%s/%d", ipAddr.ipv6, ipAddr.mask);
×
680
      }
681
    }
682
    pWhiteLists[numWhiteLists++] = ip;
×
683
  }
684

685
  pInfo->userCbFn(pInfo->userParam, code, taos, numWhiteLists, pWhiteLists);
×
686
_error:
×
687
  if (pWhiteLists != NULL) {
×
688
    for (int32_t i = 0; i < numWhiteLists; i++) {
×
689
      taosMemFree(pWhiteLists[i]);
×
690
    }
691
    taosMemoryFree(pWhiteLists);
×
692
  }
693
  taosMemoryFree(pMsg->pData);
×
694
  taosMemoryFree(pMsg->pEpSet);
×
695
  taosMemoryFree(pInfo);
×
696
  tFreeSGetUserIpWhiteListDualRsp(&wlRsp);
×
697
  return code;
×
698
}
699

700

701

702
static void taosFetchIpWhiteList(TAOS *taos, __taos_async_whitelist_dual_stack_fn_t fp, void *param, bool supportNeg) {
×
703
  if (NULL == taos) {
×
704
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
705
    return;
×
706
  }
707
  int64_t connId = *(int64_t *)taos;
×
708

709
  STscObj *pTsc = acquireTscObj(connId);
×
710
  if (NULL == pTsc) {
×
711
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
712
    return;
×
713
  }
714

715
  SGetUserWhiteListReq req;
×
716
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
717
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
718
  if (msgLen < 0) {
×
719
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
720
    releaseTscObj(connId);
×
721
    return;
×
722
  }
723

724
  void *pReq = taosMemoryMalloc(msgLen);
×
725
  if (pReq == NULL) {
×
726
    fp(param, terrno, taos, 0, NULL);
×
727
    releaseTscObj(connId);
×
728
    return;
×
729
  }
730

731
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
732
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
733
    taosMemoryFree(pReq);
×
734
    releaseTscObj(connId);
×
735
    return;
×
736
  }
737

738
  SFetchIpWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchIpWhiteListInfo));
×
739
  if (pParam == NULL) {
×
740
    fp(param, terrno, taos, 0, NULL);
×
741
    taosMemoryFree(pReq);
×
742
    releaseTscObj(connId);
×
743
    return;
×
744
  }
745

746
  pParam->connId = connId;
×
747
  pParam->supportNeg = supportNeg;
×
748
  pParam->userCbFn = fp;
×
749
  pParam->userParam = param;
×
750

751
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
752
  if (pSendInfo == NULL) {
×
753
    fp(param, terrno, taos, 0, NULL);
×
754
    taosMemoryFree(pParam);
×
755
    taosMemoryFree(pReq);
×
756
    releaseTscObj(connId);
×
757
    return;
×
758
  }
759

760
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
761
  pSendInfo->requestId = generateRequestId();
×
762
  pSendInfo->requestObjRefId = 0;
×
763
  pSendInfo->param = pParam;
×
764
  pSendInfo->fp = fetchIpWhiteListCallbackFn;
×
765
  pSendInfo->msgType = TDMT_MND_GET_USER_IP_WHITELIST_DUAL;
×
766

767
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
768
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
769
    tscWarn("failed to async send msg to server");
×
770
  }
771
  releaseTscObj(connId);
×
772
  return;
×
773
}
774

775

776

777
void taos_fetch_whitelist_dual_stack_a(TAOS *taos, __taos_async_whitelist_dual_stack_fn_t fp, void *param) {
×
778
  taosFetchIpWhiteList(taos, fp, param, false);
×
779
}
×
780

781

782

783
void taos_fetch_ip_whitelist_a(TAOS *taos, __taos_async_ip_whitelist_fn_t fp, void *param) {
×
784
  taosFetchIpWhiteList(taos, fp, param, true);
×
785
}
×
786

787

788
typedef struct SFetchDateTimeWhiteListInfo {
789
  int64_t                              connId;
790
  void                                *userParam;
791
  __taos_async_datetime_whitelist_fn_t userCbFn;
792
} SFetchDateTimeWhiteListInfo;
793

794

795

796
static const char* weekdays[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
797
int32_t fetchDateTimeWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
798
  int32_t lino = 0;
×
799
  char  **pWhiteLists = NULL;
×
800

801
  SUserDateTimeWhiteList wlRsp = {0};
×
802

803
  SFetchDateTimeWhiteListInfo *pInfo = (SFetchDateTimeWhiteListInfo *)param;
×
804
  TAOS *taos = &pInfo->connId;
×
805

806
  if (code != TSDB_CODE_SUCCESS) {
×
807
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
808
    TAOS_CHECK_GOTO(code, &lino, _error);
×
809
  }
810

811
  if ((code = tDeserializeSUserDateTimeWhiteList(pMsg->pData, pMsg->len, &wlRsp)) != TSDB_CODE_SUCCESS) {
×
812
    TAOS_CHECK_GOTO(code, &lino, _error);
×
813
  }
814

815
  pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(char *));
×
816
  if (pWhiteLists == NULL) {
×
817
    code = terrno;
×
818
    TAOS_CHECK_GOTO(code, &lino, _error);
×
819
  }
820

821
  int32_t numWhiteLists =0;
×
822
  for (int32_t i = 0; i < wlRsp.numWhiteLists; i++) {
×
823
    SDateTimeWhiteListItem *item = &wlRsp.pWhiteLists[i];
×
824

825
    char *p = taosMemCalloc(1, 128);
×
826
    if (p == NULL) {
×
827
      code = terrno;
×
828
      TAOS_CHECK_GOTO(code, &lino, _error);
×
829
    }
830

831
    int duration = item->duration / 60;
×
832

833
    if (item->absolute) {
×
834
      struct STm tm;
×
835
      (void)taosTs2Tm(item->start, TSDB_TIME_PRECISION_SECONDS, &tm, NULL);
×
836
      snprintf(p, 128, "%c %04d-%02d-%02d %02d:%02d %d", item->neg ? '-' : '+', tm.tm.tm_year + 1900, tm.tm.tm_mon + 1, tm.tm.tm_mday, tm.tm.tm_hour, tm.tm.tm_min, duration);
×
837
    } else {
838
      int day = item->start / 86400;
×
839
      int hour = (item->start % 86400) / 3600;
×
840
      int minute = (item->start % 3600) / 60;
×
841
      snprintf(p, 128, "%c %s %02d:%02d %d", item->neg ? '-' : '+', weekdays[day], hour, minute, duration);
×
842
    }
843
    pWhiteLists[numWhiteLists++] = p;
×
844
  }
845

846
  pInfo->userCbFn(pInfo->userParam, code, taos, numWhiteLists, pWhiteLists);
×
847
_error:
×
848
  if (pWhiteLists != NULL) {
×
849
    for (int32_t i = 0; i < numWhiteLists; i++) {
×
850
      taosMemFree(pWhiteLists[i]);
×
851
    }
852
    taosMemoryFree(pWhiteLists);
×
853
  }
854
  taosMemoryFree(pMsg->pData);
×
855
  taosMemoryFree(pMsg->pEpSet);
×
856
  taosMemoryFree(pInfo);
×
857
  tFreeSUserDateTimeWhiteList(&wlRsp);
×
858
  return code;
×
859
}
860

861

862

863
void taos_fetch_datetime_whitelist_a(TAOS *taos, __taos_async_datetime_whitelist_fn_t fp, void *param) {
×
864
  if (NULL == taos) {
×
865
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
866
    return;
×
867
  }
868
  int64_t connId = *(int64_t *)taos;
×
869

870
  STscObj *pTsc = acquireTscObj(connId);
×
871
  if (NULL == pTsc) {
×
872
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
873
    return;
×
874
  }
875

876
  SGetUserWhiteListReq req;
×
877
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
878
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
879
  if (msgLen < 0) {
×
880
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
881
    releaseTscObj(connId);
×
882
    return;
×
883
  }
884

885
  void *pReq = taosMemoryMalloc(msgLen);
×
886
  if (pReq == NULL) {
×
887
    fp(param, terrno, taos, 0, NULL);
×
888
    releaseTscObj(connId);
×
889
    return;
×
890
  }
891

892
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
893
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
894
    taosMemoryFree(pReq);
×
895
    releaseTscObj(connId);
×
896
    return;
×
897
  }
898

899
  SFetchDateTimeWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchDateTimeWhiteListInfo));
×
900
  if (pParam == NULL) {
×
901
    fp(param, terrno, taos, 0, NULL);
×
902
    taosMemoryFree(pReq);
×
903
    releaseTscObj(connId);
×
904
    return;
×
905
  }
906

907
  pParam->connId = connId;
×
908
  pParam->userCbFn = fp;
×
909
  pParam->userParam = param;
×
910

911
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
912
  if (pSendInfo == NULL) {
×
913
    fp(param, terrno, taos, 0, NULL);
×
914
    taosMemoryFree(pParam);
×
915
    taosMemoryFree(pReq);
×
916
    releaseTscObj(connId);
×
917
    return;
×
918
  }
919

920
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
921
  pSendInfo->requestId = generateRequestId();
×
922
  pSendInfo->requestObjRefId = 0;
×
923
  pSendInfo->param = pParam;
×
924
  pSendInfo->fp = fetchDateTimeWhiteListCallbackFn;
×
925
  pSendInfo->msgType = TDMT_MND_GET_USER_DATETIME_WHITELIST;
×
926

927
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
928
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
929
    tscWarn("failed to async send msg to server");
×
930
  }
931
  releaseTscObj(connId);
×
932
  return;
×
933
}
934

935

936

937
void taos_close_internal(void *taos) {
2,873,569✔
938
  if (taos == NULL) {
2,873,569✔
939
    return;
×
940
  }
941
  int32_t code = 0;
2,873,569✔
942

943
  STscObj *pTscObj = (STscObj *)taos;
2,873,569✔
944
  tscDebug("conn:0x%" PRIx64 ", try to close connection, numOfReq:%d", pTscObj->id, pTscObj->numOfReqs);
2,873,569✔
945

946
  SSessParam para = {.type = SESSION_PER_USER, .value = -1};
2,873,569✔
947
  code = sessMgtUpdateUserMetric((char *)pTscObj->user, &para);
2,873,569✔
948
  if (code != TSDB_CODE_SUCCESS) {
2,874,234✔
949
    tscWarn("conn:0x%" PRIx64 ", failed to update user:%s metric when close connection, code:%d", pTscObj->id,
×
950
            pTscObj->user, code);
951
  } 
952

953
  if (TSDB_CODE_SUCCESS != taosRemoveRef(clientConnRefPool, pTscObj->id)) {
2,874,234✔
954
    tscError("conn:0x%" PRIx64 ", failed to remove ref from conn pool", pTscObj->id);
×
955
  }
956
}
957

958
void taos_close(TAOS *taos) {
2,821,200✔
959
  if (taos == NULL) {
2,821,200✔
UNCOV
960
    return;
×
961
  }
962

963
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2,821,200✔
964
  if (NULL == pObj) {
2,820,943✔
965
    taosMemoryFree(taos);
×
966
    return;
×
967
  }
968

969
  taos_close_internal(pObj);
2,820,943✔
970
  releaseTscObj(*(int64_t *)taos);
2,821,608✔
971
  taosMemoryFree(taos);
2,821,608✔
972
}
973

974
int taos_errno(TAOS_RES *res) {
816,564,856✔
975
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
816,564,856✔
976
    return terrno;
27,453✔
977
  }
978

979
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
816,538,886✔
980
    return 0;
261,319✔
981
  }
982

983
  return ((SRequestObj *)res)->code;
816,277,453✔
984
}
985

986
const char *taos_errstr(TAOS_RES *res) {
21,999,260✔
987
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
21,999,260✔
988
    if (*(taosGetErrMsg()) == 0) {
26,696✔
989
      return (const char *)tstrerror(terrno);
26,661✔
990
    } else {
991
      (void)snprintf(taosGetErrMsgReturn(), ERR_MSG_LEN, "%s", taosGetErrMsg());
35✔
992
      return (const char*)taosGetErrMsgReturn();
35✔
993
    }
994
  }
995

996
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
21,972,564✔
997
    return "success";
×
998
  }
999

1000
  SRequestObj *pRequest = (SRequestObj *)res;
21,972,564✔
1001
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
21,972,564✔
1002
    return pRequest->msgBuf;
18,029,700✔
1003
  } else {
1004
    return (const char *)tstrerror(pRequest->code);
3,942,864✔
1005
  }
1006
}
1007

1008
void taos_free_result(TAOS_RES *res) {
654,020,732✔
1009
  if (NULL == res) {
654,020,732✔
1010
    return;
1,734,638✔
1011
  }
1012

1013
  tscTrace("res:%p, will be freed", res);
652,286,094✔
1014

1015
  if (TD_RES_QUERY(res)) {
652,286,162✔
1016
    SRequestObj *pRequest = (SRequestObj *)res;
648,056,542✔
1017
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
648,056,542✔
1018
    destroyRequest(pRequest);
648,058,084✔
1019
    return;
648,058,763✔
1020
  }
1021

1022
  SMqRspObj *pRsp = (SMqRspObj *)res;
4,227,430✔
1023
  if (TD_RES_TMQ(res)) {
4,227,430✔
1024
    tDeleteMqDataRsp(&pRsp->dataRsp);
4,212,052✔
1025
    doFreeReqResultInfo(&pRsp->resInfo);
4,211,945✔
1026
  } else if (TD_RES_TMQ_METADATA(res)) {
14,727✔
1027
    tDeleteSTaosxRsp(&pRsp->dataRsp);
648✔
1028
    doFreeReqResultInfo(&pRsp->resInfo);
648✔
1029
  } else if (TD_RES_TMQ_META(res)) {
14,079✔
1030
    tDeleteMqMetaRsp(&pRsp->metaRsp);
12,291✔
1031
  } else if (TD_RES_TMQ_BATCH_META(res)) {
1,788✔
1032
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
1,788✔
1033
  } else if (TD_RES_TMQ_RAW(res)) {
×
1034
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
1035
  }
1036
  taosMemoryFree(pRsp);
4,226,779✔
1037
}
1038

UNCOV
1039
void taos_kill_query(TAOS *taos) {
×
UNCOV
1040
  if (NULL == taos) {
×
UNCOV
1041
    return;
×
1042
  }
1043

UNCOV
1044
  int64_t  rid = *(int64_t *)taos;
×
UNCOV
1045
  STscObj *pTscObj = acquireTscObj(rid);
×
UNCOV
1046
  if (pTscObj) {
×
UNCOV
1047
    stopAllRequests(pTscObj->pRequests);
×
1048
  }
UNCOV
1049
  releaseTscObj(rid);
×
1050
}
1051

1052
int taos_field_count(TAOS_RES *res) {
2,147,483,647✔
1053
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1054
    return 0;
×
1055
  }
1056

1057
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
1058
  return pResInfo->numOfCols;
2,147,483,647✔
1059
}
1060

1061
int taos_num_fields(TAOS_RES *res) { return taos_field_count(res); }
2,147,483,647✔
1062

1063
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
1,413,370,632✔
1064
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,413,370,632✔
1065
    return NULL;
2,487,879✔
1066
  }
1067

1068
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
1,410,867,337✔
1069
  return pResInfo->userFields;
1,410,867,337✔
1070
}
1071

1072
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
646,583,327✔
1073
TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqid) {
838✔
1074
  return taosQueryImplWithReqid(taos, sql, false, reqid);
838✔
1075
}
1076

1077
TAOS_FIELD_E *taos_fetch_fields_e(TAOS_RES *res) {
877✔
1078
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
877✔
1079
    return NULL;
×
1080
  }
1081
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
877✔
1082
  return pResInfo->fields;
877✔
1083
}
1084

1085
TAOS_ROW taos_fetch_row(TAOS_RES *res) {
2,147,483,647✔
1086
  if (res == NULL) {
2,147,483,647✔
1087
    return NULL;
×
1088
  }
1089

1090
  if (TD_RES_QUERY(res)) {
2,147,483,647✔
1091
    SRequestObj *pRequest = (SRequestObj *)res;
1,298,896,432✔
1092
    if (pRequest->killed) {
1,298,896,432✔
1093
      tscInfo("query has been killed, can not fetch more row.");
×
1094
      pRequest->code = TSDB_CODE_TSC_QUERY_KILLED;
×
1095
      return NULL;
×
1096
    }
1097

1098
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
1,298,898,968✔
1099
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
1,298,908,079✔
1100
      return NULL;
×
1101
    }
1102

1103
    if (pRequest->inCallback) {
1,298,847,700✔
1104
      tscError("can not call taos_fetch_row before query callback ends.");
×
1105
      terrno = TSDB_CODE_TSC_INVALID_OPERATION;
×
1106
      return NULL;
×
1107
    }
1108

1109
    return doAsyncFetchRows(pRequest, true, true);
1,298,880,992✔
1110
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,319,013,354✔
1111
    SMqRspObj      *msg = ((SMqRspObj *)res);
1,318,856,053✔
1112
    SReqResultInfo *pResultInfo = NULL;
1,318,856,053✔
1113
    if (msg->resIter == -1) {
1,319,069,816✔
1114
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
3,943,045✔
1115
        return NULL;
×
1116
      }
1117
    } else {
1118
      pResultInfo = tmqGetCurResInfo(res);
1,315,144,658✔
1119
    }
1120

1121
    if (pResultInfo->current < pResultInfo->numOfRows) {
1,319,087,703✔
1122
      doSetOneRowPtr(pResultInfo);
1,293,671,397✔
1123
      pResultInfo->current += 1;
1,293,846,035✔
1124
      return pResultInfo->row;
1,293,870,361✔
1125
    } else {
1126
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
25,404,570✔
1127
        return NULL;
3,942,810✔
1128
      }
1129

1130
      doSetOneRowPtr(pResultInfo);
21,460,761✔
1131
      pResultInfo->current += 1;
21,461,865✔
1132
      return pResultInfo->row;
21,461,758✔
1133
    }
1134
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1135
    return NULL;
×
1136
  } else {
1137
    tscError("invalid result passed to taos_fetch_row");
×
1138
    terrno = TSDB_CODE_TMQ_INVALID_DATA;
×
1139
    return NULL;
×
1140
  }
1141
}
1142

1143
int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
1,317,451,164✔
1144
  return taos_print_row_with_size(str, INT32_MAX, row, fields, num_fields);
1,317,451,164✔
1145
}
1146
int taos_print_row_with_size(char *str, uint32_t size, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
1,317,886,765✔
1147
  int32_t len = 0;
1,317,886,765✔
1148
  for (int i = 0; i < num_fields; ++i) {
2,147,483,647✔
1149
    if (i > 0 && len < size - 1) {
2,147,483,647✔
1150
      str[len++] = ' ';
2,147,483,647✔
1151
    }
1152

1153
    if (row[i] == NULL) {
2,147,483,647✔
1154
      len += tsnprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
33,453,581✔
1155
      continue;
33,453,889✔
1156
    }
1157

1158
    switch (fields[i].type) {
2,147,483,647✔
1159
      case TSDB_DATA_TYPE_TINYINT:
76,891,794✔
1160
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
76,891,794✔
1161
        break;
76,965,553✔
1162

1163
      case TSDB_DATA_TYPE_UTINYINT:
140,800✔
1164
        len += tsnprintf(str + len, size - len, "%u", *((uint8_t *)row[i]));
140,800✔
1165
        break;
140,800✔
1166

1167
      case TSDB_DATA_TYPE_SMALLINT:
140,800✔
1168
        len += tsnprintf(str + len, size - len, "%d", *((int16_t *)row[i]));
140,800✔
1169
        break;
140,800✔
1170

1171
      case TSDB_DATA_TYPE_USMALLINT:
140,800✔
1172
        len += tsnprintf(str + len, size - len, "%u", *((uint16_t *)row[i]));
140,800✔
1173
        break;
140,800✔
1174

1175
      case TSDB_DATA_TYPE_INT:
1,239,299,188✔
1176
        len += tsnprintf(str + len, size - len, "%d", *((int32_t *)row[i]));
1,239,299,188✔
1177
        break;
1,239,688,322✔
1178

1179
      case TSDB_DATA_TYPE_UINT:
140,800✔
1180
        len += tsnprintf(str + len, size - len, "%u", *((uint32_t *)row[i]));
140,800✔
1181
        break;
140,800✔
1182

1183
      case TSDB_DATA_TYPE_BIGINT:
1,142,128,614✔
1184
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
1,142,128,614✔
1185
        break;
1,142,130,933✔
1186

1187
      case TSDB_DATA_TYPE_UBIGINT:
140,800✔
1188
        len += tsnprintf(str + len, size - len, "%" PRIu64, *((uint64_t *)row[i]));
140,800✔
1189
        break;
140,767✔
1190

1191
      case TSDB_DATA_TYPE_FLOAT: {
152,522,426✔
1192
        float fv = 0;
152,522,426✔
1193
        fv = GET_FLOAT_VAL(row[i]);
152,522,426✔
1194
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
152,568,686✔
1195
      } break;
153,116,867✔
1196

1197
      case TSDB_DATA_TYPE_DOUBLE: {
528,547,752✔
1198
        double dv = 0;
528,547,752✔
1199
        dv = GET_DOUBLE_VAL(row[i]);
528,547,752✔
1200
        len += snprintf(str + len, size - len, "%.*g", DBL_DIG, dv);
528,548,135✔
1201
      } break;
528,548,894✔
1202

1203
      case TSDB_DATA_TYPE_VARBINARY: {
141,111✔
1204
        void    *data = NULL;
141,111✔
1205
        uint32_t tmp = 0;
141,111✔
1206
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
141,111✔
1207
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
141,111✔
1208
          break;
×
1209
        }
1210
        uint32_t copyLen = TMIN(size - len - 1, tmp);
141,012✔
1211
        (void)memcpy(str + len, data, copyLen);
141,012✔
1212
        len += copyLen;
141,012✔
1213
        taosMemoryFree(data);
141,012✔
1214
      } break;
141,111✔
1215
      case TSDB_DATA_TYPE_BINARY:
1,533,451,306✔
1216
      case TSDB_DATA_TYPE_NCHAR:
1217
      case TSDB_DATA_TYPE_GEOMETRY: {
1218
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
1,533,451,306✔
1219
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
1,533,465,692✔
1220
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
417,012,638✔
1221
          if (charLen > fields[i].bytes || charLen < 0) {
1,116,629,429✔
1222
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
×
1223
            break;
×
1224
          }
1225
        } else {
1226
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
416,871,227✔
1227
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
363✔
1228
            break;
×
1229
          }
1230
        }
1231

1232
        uint32_t copyLen = TMIN(size - len - 1, charLen);
1,533,567,730✔
1233
        (void)memcpy(str + len, row[i], copyLen);
1,533,567,730✔
1234
        len += copyLen;
1,533,617,359✔
1235
      } break;
1,533,617,359✔
1236
      case TSDB_DATA_TYPE_BLOB:
×
1237
      case TSDB_DATA_TYPE_MEDIUMBLOB: {
1238
        void    *data = NULL;
×
1239
        uint32_t tmp = 0;
×
1240
        int32_t  charLen = blobDataLen((char *)row[i] - BLOBSTR_HEADER_SIZE);
×
1241
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
×
1242
          break;
×
1243
        }
1244

1245
        uint32_t copyLen = TMIN(size - len - 1, tmp);
×
1246
        (void)memcpy(str + len, data, copyLen);
×
1247
        len += copyLen;
×
1248

1249
        taosMemoryFree(data);
×
1250
      } break;
×
1251

1252
      case TSDB_DATA_TYPE_TIMESTAMP:
1,757,891,334✔
1253
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
1,757,891,334✔
1254
        break;
1,757,875,596✔
1255

1256
      case TSDB_DATA_TYPE_BOOL:
140,767✔
1257
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
140,767✔
1258
        break;
140,800✔
1259
      case TSDB_DATA_TYPE_DECIMAL64:
×
1260
      case TSDB_DATA_TYPE_DECIMAL: {
1261
        uint32_t decimalLen = strlen(row[i]);
×
1262
        uint32_t copyLen = TMIN(size - len - 1, decimalLen);
×
1263
        (void)memcpy(str + len, row[i], copyLen);
×
1264
        len += copyLen;
×
1265
      } break;
×
1266
      default:
×
1267
        break;
×
1268
    }
1269

1270
    if (len >= size - 1) {
2,147,483,647✔
1271
      break;
×
1272
    }
1273
  }
1274
  if (len < size) {
1,315,093,734✔
1275
    str[len] = 0;
1,318,102,653✔
1276
  }
1277

1278
  return len;
1,318,118,463✔
1279
}
1280

1281
int *taos_fetch_lengths(TAOS_RES *res) {
2,147,483,647✔
1282
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1283
    return NULL;
57✔
1284
  }
1285

1286
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
1287
  return pResInfo->length;
2,147,483,647✔
1288
}
1289

1290
TAOS_ROW *taos_result_block(TAOS_RES *res) {
×
1291
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1292
    terrno = TSDB_CODE_INVALID_PARA;
×
1293
    return NULL;
×
1294
  }
1295

1296
  if (taos_is_update_query(res)) {
×
1297
    return NULL;
×
1298
  }
1299

1300
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
1301
  return &pResInfo->row;
×
1302
}
1303

1304
// todo intergrate with tDataTypes
1305
const char *taos_data_type(int type) {
×
1306
  switch (type) {
×
1307
    case TSDB_DATA_TYPE_NULL:
×
1308
      return "TSDB_DATA_TYPE_NULL";
×
1309
    case TSDB_DATA_TYPE_BOOL:
×
1310
      return "TSDB_DATA_TYPE_BOOL";
×
1311
    case TSDB_DATA_TYPE_TINYINT:
×
1312
      return "TSDB_DATA_TYPE_TINYINT";
×
1313
    case TSDB_DATA_TYPE_SMALLINT:
×
1314
      return "TSDB_DATA_TYPE_SMALLINT";
×
1315
    case TSDB_DATA_TYPE_INT:
×
1316
      return "TSDB_DATA_TYPE_INT";
×
1317
    case TSDB_DATA_TYPE_BIGINT:
×
1318
      return "TSDB_DATA_TYPE_BIGINT";
×
1319
    case TSDB_DATA_TYPE_FLOAT:
×
1320
      return "TSDB_DATA_TYPE_FLOAT";
×
1321
    case TSDB_DATA_TYPE_DOUBLE:
×
1322
      return "TSDB_DATA_TYPE_DOUBLE";
×
1323
    case TSDB_DATA_TYPE_VARCHAR:
×
1324
      return "TSDB_DATA_TYPE_VARCHAR";
×
1325
      //    case TSDB_DATA_TYPE_BINARY:          return "TSDB_DATA_TYPE_VARCHAR";
1326
    case TSDB_DATA_TYPE_TIMESTAMP:
×
1327
      return "TSDB_DATA_TYPE_TIMESTAMP";
×
1328
    case TSDB_DATA_TYPE_NCHAR:
×
1329
      return "TSDB_DATA_TYPE_NCHAR";
×
1330
    case TSDB_DATA_TYPE_JSON:
×
1331
      return "TSDB_DATA_TYPE_JSON";
×
1332
    case TSDB_DATA_TYPE_GEOMETRY:
×
1333
      return "TSDB_DATA_TYPE_GEOMETRY";
×
1334
    case TSDB_DATA_TYPE_UTINYINT:
×
1335
      return "TSDB_DATA_TYPE_UTINYINT";
×
1336
    case TSDB_DATA_TYPE_USMALLINT:
×
1337
      return "TSDB_DATA_TYPE_USMALLINT";
×
1338
    case TSDB_DATA_TYPE_UINT:
×
1339
      return "TSDB_DATA_TYPE_UINT";
×
1340
    case TSDB_DATA_TYPE_UBIGINT:
×
1341
      return "TSDB_DATA_TYPE_UBIGINT";
×
1342
    case TSDB_DATA_TYPE_VARBINARY:
×
1343
      return "TSDB_DATA_TYPE_VARBINARY";
×
1344
    case TSDB_DATA_TYPE_DECIMAL:
×
1345
      return "TSDB_DATA_TYPE_DECIMAL";
×
1346
    case TSDB_DATA_TYPE_BLOB:
×
1347
      return "TSDB_DATA_TYPE_BLOB";
×
1348
    case TSDB_DATA_TYPE_MEDIUMBLOB:
×
1349
      return "TSDB_DATA_TYPE_MEDIUMBLOB";
×
1350
    default:
×
1351
      return "UNKNOWN";
×
1352
  }
1353
}
1354

1355
const char *taos_get_client_info() { return td_version; }
820,439✔
1356

1357
// return int32_t
1358
int taos_affected_rows(TAOS_RES *res) {
500,924,933✔
1359
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
500,924,933✔
1360
      TD_RES_TMQ_BATCH_META(res)) {
500,928,127✔
1361
    return 0;
×
1362
  }
1363

1364
  SRequestObj    *pRequest = (SRequestObj *)res;
500,926,481✔
1365
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
500,926,481✔
1366
  return (int)pResInfo->numOfRows;
500,928,554✔
1367
}
1368

1369
// return int64_t
1370
int64_t taos_affected_rows64(TAOS_RES *res) {
1,338,663✔
1371
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
1,338,663✔
1372
      TD_RES_TMQ_BATCH_META(res)) {
1,338,663✔
1373
    return 0;
×
1374
  }
1375

1376
  SRequestObj    *pRequest = (SRequestObj *)res;
1,338,663✔
1377
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
1,338,663✔
1378
  return pResInfo->numOfRows;
1,338,663✔
1379
}
1380

1381
int taos_result_precision(TAOS_RES *res) {
1,331,718,583✔
1382
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,331,718,583✔
1383
    return TSDB_TIME_PRECISION_MILLI;
×
1384
  }
1385

1386
  if (TD_RES_QUERY(res)) {
1,331,722,020✔
1387
    SRequestObj *pRequest = (SRequestObj *)res;
89,597,600✔
1388
    return pRequest->body.resInfo.precision;
89,597,600✔
1389
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,242,124,483✔
1390
    SReqResultInfo *info = tmqGetCurResInfo(res);
1,242,123,226✔
1391
    return info->precision;
1,242,123,226✔
1392
  }
1393
  return TSDB_TIME_PRECISION_MILLI;
×
1394
}
1395

1396
int taos_select_db(TAOS *taos, const char *db) {
105,937✔
1397
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
105,937✔
1398
  if (pObj == NULL) {
105,937✔
1399
    releaseTscObj(*(int64_t *)taos);
×
1400
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1401
    return TSDB_CODE_TSC_DISCONNECTED;
×
1402
  }
1403

1404
  if (db == NULL || strlen(db) == 0) {
105,937✔
1405
    releaseTscObj(*(int64_t *)taos);
×
1406
    tscError("invalid parameter for %s", db == NULL ? "db is NULL" : "db is empty");
×
1407
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1408
    return terrno;
×
1409
  }
1410

1411
  char sql[256] = {0};
105,937✔
1412
  (void)snprintf(sql, tListLen(sql), "use %s", db);
105,937✔
1413

1414
  TAOS_RES *pRequest = taos_query(taos, sql);
105,937✔
1415
  int32_t   code = taos_errno(pRequest);
105,937✔
1416

1417
  taos_free_result(pRequest);
105,903✔
1418
  releaseTscObj(*(int64_t *)taos);
105,937✔
1419
  return code;
105,937✔
1420
}
1421

1422
void taos_stop_query(TAOS_RES *res) {
651,885,026✔
1423
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
651,885,026✔
1424
      TD_RES_TMQ_BATCH_META(res)) {
651,884,971✔
1425
    return;
34✔
1426
  }
1427

1428
  stopAllQueries((SRequestObj *)res);
651,887,678✔
1429
}
1430

1431
bool taos_is_null(TAOS_RES *res, int32_t row, int32_t col) {
×
1432
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1433
    return true;
×
1434
  }
1435
  SReqResultInfo *pResultInfo = tscGetCurResInfo(res);
×
1436
  if (col >= pResultInfo->numOfCols || col < 0 || row >= pResultInfo->numOfRows || row < 0) {
×
1437
    return true;
×
1438
  }
1439

1440
  SResultColumn *pCol = &pResultInfo->pCol[col];
×
1441
  if (IS_VAR_DATA_TYPE(pResultInfo->fields[col].type)) {
×
1442
    return (pCol->offset[row] == -1);
×
1443
  } else {
1444
    return colDataIsNull_f(pCol, row);
×
1445
  }
1446
}
1447

1448
bool taos_is_update_query(TAOS_RES *res) { return taos_num_fields(res) == 0; }
737,436✔
1449

1450
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
165,968,209✔
1451
  int32_t numOfRows = 0;
165,968,209✔
1452
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
165,968,209✔
1453
  return numOfRows;
165,967,740✔
1454
}
1455

1456
int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) {
165,968,209✔
1457
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
165,968,209✔
1458
    return 0;
×
1459
  }
1460

1461
  if (TD_RES_QUERY(res)) {
165,968,209✔
1462
    SRequestObj *pRequest = (SRequestObj *)res;
161,830,252✔
1463

1464
    (*rows) = NULL;
161,830,252✔
1465
    (*numOfRows) = 0;
161,830,252✔
1466

1467
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
161,830,252✔
1468
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
161,660,315✔
1469
      return pRequest->code;
744,757✔
1470
    }
1471

1472
    (void)doAsyncFetchRows(pRequest, false, true);
161,085,495✔
1473

1474
    // TODO refactor
1475
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
161,085,495✔
1476
    pResultInfo->current = pResultInfo->numOfRows;
161,085,707✔
1477

1478
    (*rows) = pResultInfo->row;
161,085,707✔
1479
    (*numOfRows) = pResultInfo->numOfRows;
161,085,707✔
1480
    return pRequest->code;
161,085,707✔
1481
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
4,137,957✔
1482
    SReqResultInfo *pResultInfo = NULL;
4,137,957✔
1483
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
4,137,957✔
1484
    if (code != 0) return code;
4,137,957✔
1485

1486
    pResultInfo->current = pResultInfo->numOfRows;
3,882,367✔
1487
    (*rows) = pResultInfo->row;
3,882,367✔
1488
    (*numOfRows) = pResultInfo->numOfRows;
3,882,367✔
1489
    return 0;
3,881,898✔
1490
  } else {
1491
    tscError("taos_fetch_block_s invalid res type");
×
1492
    return TSDB_CODE_TMQ_INVALID_DATA;
×
1493
  }
1494
}
1495

1496
int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) {
128,841✔
1497
  *numOfRows = 0;
128,841✔
1498
  *pData = NULL;
128,841✔
1499

1500
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
128,841✔
1501
    return 0;
×
1502
  }
1503

1504
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
128,841✔
1505
    SReqResultInfo *pResultInfo = NULL;
123,285✔
1506
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
123,285✔
1507
    if (code != 0) {
123,285✔
1508
      (*numOfRows) = 0;
4,191✔
1509
      return 0;
4,191✔
1510
    }
1511

1512
    pResultInfo->current = pResultInfo->numOfRows;
119,094✔
1513
    (*numOfRows) = pResultInfo->numOfRows;
119,094✔
1514
    (*pData) = (void *)pResultInfo->pData;
119,094✔
1515
    return 0;
119,094✔
1516
  }
1517

1518
  SRequestObj *pRequest = (SRequestObj *)res;
5,556✔
1519

1520
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
5,556✔
1521
      pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
5,556✔
1522
    return pRequest->code;
×
1523
  }
1524

1525
  (void)doAsyncFetchRows(pRequest, false, false);
5,556✔
1526

1527
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
5,556✔
1528

1529
  pResultInfo->current = pResultInfo->numOfRows;
5,556✔
1530
  (*numOfRows) = pResultInfo->numOfRows;
5,556✔
1531
  (*pData) = (void *)pResultInfo->pData;
5,556✔
1532

1533
  return pRequest->code;
5,556✔
1534
}
1535

1536
int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) {
111,029,844✔
1537
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
111,029,844✔
1538
    return 0;
×
1539
  }
1540

1541
  int32_t numOfFields = taos_num_fields(res);
111,029,844✔
1542
  if (columnIndex < 0 || columnIndex >= numOfFields || numOfFields == 0) {
111,029,844✔
1543
    return 0;
×
1544
  }
1545

1546
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
111,029,844✔
1547
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
111,029,844✔
1548
  if (!IS_VAR_DATA_TYPE(pField->type)) {
111,029,844✔
1549
    return 0;
×
1550
  }
1551

1552
  return pResInfo->pCol[columnIndex].offset;
111,029,844✔
1553
}
1554

1555
int taos_is_null_by_column(TAOS_RES *res, int columnIndex, bool result[], int *rows) {
310,362,634✔
1556
  if (res == NULL || result == NULL || rows == NULL || *rows <= 0 || columnIndex < 0 || TD_RES_TMQ_META(res) ||
310,362,634✔
1557
      TD_RES_TMQ_RAW(res) || TD_RES_TMQ_BATCH_META(res)) {
310,362,634✔
1558
    return TSDB_CODE_INVALID_PARA;
×
1559
  }
1560

1561
  int32_t numOfFields = taos_num_fields(res);
310,362,634✔
1562
  if (columnIndex >= numOfFields || numOfFields == 0) {
310,362,634✔
1563
    return TSDB_CODE_INVALID_PARA;
×
1564
  }
1565

1566
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
310,362,634✔
1567
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
310,362,634✔
1568
  SResultColumn  *pCol = &pResInfo->pCol[columnIndex];
310,362,634✔
1569

1570
  if (*rows > pResInfo->numOfRows) {
310,362,634✔
1571
    *rows = pResInfo->numOfRows;
×
1572
  }
1573
  if (IS_VAR_DATA_TYPE(pField->type)) {
310,362,634✔
1574
    for (int i = 0; i < *rows; i++) {
×
1575
      if (pCol->offset[i] == -1) {
×
1576
        result[i] = true;
×
1577
      } else {
1578
        result[i] = false;
×
1579
      }
1580
    }
1581
  } else {
1582
    for (int i = 0; i < *rows; i++) {
2,147,483,647✔
1583
      if (colDataIsNull_f(pCol, i)) {
2,147,483,647✔
1584
        result[i] = true;
2,147,483,647✔
1585
      } else {
1586
        result[i] = false;
2,147,483,647✔
1587
      }
1588
    }
1589
  }
1590
  return 0;
310,362,634✔
1591
}
1592

1593
int taos_validate_sql(TAOS *taos, const char *sql) {
×
1594
  TAOS_RES *pObj = taosQueryImpl(taos, sql, true, TD_REQ_FROM_APP);
×
1595

1596
  int code = taos_errno(pObj);
×
1597

1598
  taos_free_result(pObj);
×
1599
  return code;
×
1600
}
1601

1602
void taos_reset_current_db(TAOS *taos) {
×
1603
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
1604
  if (pTscObj == NULL) {
×
1605
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1606
    return;
×
1607
  }
1608

1609
  resetConnectDB(pTscObj);
×
1610

1611
  releaseTscObj(*(int64_t *)taos);
×
1612
}
1613

1614
const char *taos_get_server_info(TAOS *taos) {
37,839✔
1615
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
37,839✔
1616
  if (pTscObj == NULL) {
37,839✔
1617
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1618
    return NULL;
×
1619
  }
1620

1621
  releaseTscObj(*(int64_t *)taos);
37,839✔
1622

1623
  return pTscObj->sDetailVer;
37,839✔
1624
}
1625

1626
int taos_get_current_db(TAOS *taos, char *database, int len, int *required) {
600✔
1627
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
600✔
1628
  if (pTscObj == NULL) {
600✔
1629
    return TSDB_CODE_TSC_DISCONNECTED;
×
1630
  }
1631

1632
  int code = TSDB_CODE_SUCCESS;
600✔
1633
  (void)taosThreadMutexLock(&pTscObj->mutex);
600✔
1634
  if (database == NULL || len <= 0) {
600✔
1635
    if (required != NULL) *required = strlen(pTscObj->db) + 1;
300✔
1636
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
300✔
1637
  } else if (len < strlen(pTscObj->db) + 1) {
300✔
1638
    tstrncpy(database, pTscObj->db, len);
150✔
1639
    if (required) *required = strlen(pTscObj->db) + 1;
150✔
1640
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
150✔
1641
  } else {
1642
    tstrncpy(database, pTscObj->db, len);
150✔
1643
    code = 0;
150✔
1644
  }
1645
_return:
600✔
1646
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
600✔
1647
  releaseTscObj(*(int64_t *)taos);
600✔
1648
  return code;
600✔
1649
}
1650

1651
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
1,303,279,447✔
1652
  if (NULL == pWrapper) {
1,303,279,447✔
1653
    return;
654,246,223✔
1654
  }
1655
  destoryCatalogReq(pWrapper->pCatalogReq);
649,033,224✔
1656
  taosMemoryFree(pWrapper->pCatalogReq);
649,032,291✔
1657
  qDestroyParseContext(pWrapper->pParseCtx);
649,028,697✔
1658
  taosMemoryFree(pWrapper);
649,027,671✔
1659
}
1660

1661
void destroyCtxInRequest(SRequestObj *pRequest) {
2,707,154✔
1662
  schedulerFreeJob(&pRequest->body.queryJob, 0);
2,707,154✔
1663
  qDestroyQuery(pRequest->pQuery);
2,707,154✔
1664
  pRequest->pQuery = NULL;
2,707,154✔
1665
  destorySqlCallbackWrapper(pRequest->pWrapper);
2,707,154✔
1666
  pRequest->pWrapper = NULL;
2,707,154✔
1667
}
2,707,154✔
1668

1669
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
177,874,263✔
1670
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
177,874,263✔
1671
  SRequestObj         *pRequest = pWrapper->pRequest;
177,874,263✔
1672
  SQuery              *pQuery = pRequest->pQuery;
177,875,250✔
1673

1674
  qDebug("req:0x%" PRIx64 ", start to semantic analysis, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
177,875,627✔
1675

1676
  int64_t analyseStart = taosGetTimestampUs();
177,875,627✔
1677
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
177,875,627✔
1678
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
177,875,056✔
1679

1680
  if (TSDB_CODE_SUCCESS == code) {
177,875,083✔
1681
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
177,868,745✔
1682
  }
1683

1684
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
177,868,383✔
1685

1686
  if (pRequest->parseOnly) {
177,871,800✔
1687
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
303,965✔
1688
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
303,965✔
1689
  }
1690

1691
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
177,873,055✔
1692
}
177,866,342✔
1693

1694
int32_t cloneCatalogReq(SCatalogReq **ppTarget, SCatalogReq *pSrc) {
×
1695
  int32_t      code = TSDB_CODE_SUCCESS;
×
1696
  SCatalogReq *pTarget = taosMemoryCalloc(1, sizeof(SCatalogReq));
×
1697
  if (pTarget == NULL) {
×
1698
    code = terrno;
×
1699
  } else {
1700
    pTarget->pDbVgroup = taosArrayDup(pSrc->pDbVgroup, NULL);
×
1701
    pTarget->pDbCfg = taosArrayDup(pSrc->pDbCfg, NULL);
×
1702
    pTarget->pDbInfo = taosArrayDup(pSrc->pDbInfo, NULL);
×
1703
    pTarget->pTableMeta = taosArrayDup(pSrc->pTableMeta, NULL);
×
1704
    pTarget->pTableHash = taosArrayDup(pSrc->pTableHash, NULL);
×
1705
    pTarget->pUdf = taosArrayDup(pSrc->pUdf, NULL);
×
1706
    pTarget->pIndex = taosArrayDup(pSrc->pIndex, NULL);
×
1707
    pTarget->pUser = taosArrayDup(pSrc->pUser, NULL);
×
1708
    pTarget->pTableIndex = taosArrayDup(pSrc->pTableIndex, NULL);
×
1709
    pTarget->pTableCfg = taosArrayDup(pSrc->pTableCfg, NULL);
×
1710
    pTarget->pTableTag = taosArrayDup(pSrc->pTableTag, NULL);
×
1711
    pTarget->pView = taosArrayDup(pSrc->pView, NULL);
×
1712
    pTarget->pTableTSMAs = taosArrayDup(pSrc->pTableTSMAs, NULL);
×
1713
    pTarget->pTSMAs = taosArrayDup(pSrc->pTSMAs, NULL);
×
1714
    pTarget->pVStbRefDbs = taosArrayDup(pSrc->pVStbRefDbs, NULL);
×
1715
    pTarget->qNodeRequired = pSrc->qNodeRequired;
×
1716
    pTarget->dNodeRequired = pSrc->dNodeRequired;
×
1717
    pTarget->svrVerRequired = pSrc->svrVerRequired;
×
1718
    pTarget->forceUpdate = pSrc->forceUpdate;
×
1719
    pTarget->cloned = true;
×
1720

1721
    *ppTarget = pTarget;
×
1722
  }
1723

1724
  return code;
×
1725
}
1726

1727
void handleSubQueryFromAnalyse(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, SNode *pRoot) {
×
1728
  SRequestObj         *pNewRequest = NULL;
×
1729
  SSqlCallbackWrapper *pNewWrapper = NULL;
×
1730
  int32_t              code = buildPreviousRequest(pWrapper->pRequest, pWrapper->pRequest->sqlstr, &pNewRequest);
×
1731
  if (code) {
×
1732
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1733
    return;
×
1734
  }
1735

1736
  pNewRequest->pQuery = NULL;
×
1737
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode **)&pNewRequest->pQuery);
×
1738
  if (pNewRequest->pQuery) {
×
1739
    pNewRequest->pQuery->pRoot = pRoot;
×
1740
    pRoot = NULL;
×
1741
    pNewRequest->pQuery->execStage = QUERY_EXEC_STAGE_ANALYSE;
×
1742
  }
1743
  if (TSDB_CODE_SUCCESS == code) {
×
1744
    code = prepareAndParseSqlSyntax(&pNewWrapper, pNewRequest, false);
×
1745
  }
1746
  if (TSDB_CODE_SUCCESS == code) {
×
1747
    code = cloneCatalogReq(&pNewWrapper->pCatalogReq, pWrapper->pCatalogReq);
×
1748
  }
1749
  if (TSDB_CODE_SUCCESS == code) {
×
1750
    doAsyncQueryFromAnalyse(pResultMeta, pNewWrapper, code);
×
1751
    nodesDestroyNode(pRoot);
×
1752
  } else {
1753
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1754
    return;
×
1755
  }
1756
}
1757

1758
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
177,861,639✔
1759
  SRequestObj *pRequest = pWrapper->pRequest;
177,861,639✔
1760
  SQuery      *pQuery = pRequest->pQuery;
177,870,061✔
1761

1762
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
177,866,045✔
1763
    SNode *prevRoot = pQuery->pPrevRoot;
×
1764
    pQuery->pPrevRoot = NULL;
×
1765
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
×
1766
    return;
×
1767
  }
1768

1769
  if (code == TSDB_CODE_SUCCESS) {
177,870,214✔
1770
    pRequest->stableQuery = pQuery->stableQuery;
162,378,804✔
1771
    if (pQuery->pRoot) {
162,386,323✔
1772
      pRequest->stmtType = pQuery->pRoot->type;
162,375,629✔
1773
    }
1774

1775
    if (pQuery->haveResultSet) {
162,386,780✔
1776
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
88,375,708✔
1777
                              pRequest->stmtBindVersion > 0);
88,376,270✔
1778
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
88,375,695✔
1779
    }
1780
  }
1781

1782
  if (code == TSDB_CODE_SUCCESS) {
177,848,951✔
1783
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
162,370,605✔
1784
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
162,377,839✔
1785
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
162,373,440✔
1786

1787
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
162,362,132✔
1788
  } else {
1789
    destorySqlCallbackWrapper(pWrapper);
15,478,346✔
1790
    pRequest->pWrapper = NULL;
15,478,346✔
1791
    qDestroyQuery(pRequest->pQuery);
15,478,346✔
1792
    pRequest->pQuery = NULL;
15,478,346✔
1793

1794
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
15,478,346✔
1795
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
2,660,324✔
1796
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1797
      restartAsyncQuery(pRequest, code);
2,660,324✔
1798
      return;
2,660,324✔
1799
    }
1800

1801
    // return to app directly
1802
    tscError("req:0x%" PRIx64 ", error occurs, code:%s, return to user app, QID:0x%" PRIx64, pRequest->self,
12,818,022✔
1803
             tstrerror(code), pRequest->requestId);
1804
    pRequest->code = code;
12,818,022✔
1805
    returnToUser(pRequest);
12,818,022✔
1806
  }
1807
}
1808

1809
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
188,224,364✔
1810
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
375,326,869✔
1811
                           .requestId = pWrapper->pParseCtx->requestId,
188,230,230✔
1812
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
188,227,844✔
1813
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
188,223,323✔
1814

1815
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
375,325,182✔
1816

1817
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
189,350,713✔
1818
                                &pWrapper->pRequest->body.queryJob);
188,224,591✔
1819
}
1820

1821
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code);
1822

1823
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
653,828,606✔
1824
  int32_t code = TSDB_CODE_SUCCESS;
653,828,606✔
1825
  switch (pWrapper->pRequest->pQuery->execStage) {
653,828,606✔
1826
    case QUERY_EXEC_STAGE_PARSE: {
10,358,272✔
1827
      // continue parse after get metadata
1828
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
10,358,272✔
1829
      break;
10,357,878✔
1830
    }
1831
    case QUERY_EXEC_STAGE_ANALYSE: {
177,870,015✔
1832
      // analysis after get metadata
1833
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
177,870,015✔
1834
      break;
177,872,254✔
1835
    }
1836
    case QUERY_EXEC_STAGE_SCHEDULE: {
465,604,583✔
1837
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
465,604,583✔
1838
      break;
465,604,463✔
1839
    }
1840
    default:
×
1841
      break;
×
1842
  }
1843
  return code;
653,834,561✔
1844
}
1845

1846
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code) {
10,358,272✔
1847
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
10,358,272✔
1848
  SRequestObj         *pRequest = pWrapper->pRequest;
10,358,272✔
1849
  SQuery              *pQuery = pRequest->pQuery;
10,358,272✔
1850

1851
  pRequest->metric.ctgCostUs += taosGetTimestampUs() - pRequest->metric.ctgStart;
10,358,272✔
1852
  qDebug("req:0x%" PRIx64 ", continue parse query, QID:0x%" PRIx64 ", code:%s", pRequest->self, pRequest->requestId,
10,358,272✔
1853
         tstrerror(code));
1854

1855
  if (code == TSDB_CODE_SUCCESS) {
10,358,272✔
1856
    // pWrapper->pCatalogReq->forceUpdate = false;
1857
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
10,354,702✔
1858
  }
1859

1860
  if (TSDB_CODE_SUCCESS == code) {
10,358,272✔
1861
    code = phaseAsyncQuery(pWrapper);
9,855,659✔
1862
  }
1863

1864
  if (TSDB_CODE_SUCCESS != code) {
10,358,272✔
1865
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
502,613✔
1866
             tstrerror(code), pWrapper->pRequest->requestId);
1867
    destorySqlCallbackWrapper(pWrapper);
502,613✔
1868
    pRequest->pWrapper = NULL;
502,613✔
1869
    terrno = code;
502,613✔
1870
    pRequest->code = code;
502,613✔
1871
    doRequestCallback(pRequest, code);
502,613✔
1872
  }
1873
}
10,358,272✔
1874

1875
void continueInsertFromCsv(SSqlCallbackWrapper *pWrapper, SRequestObj *pRequest) {
22,731✔
1876
  int32_t code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
22,731✔
1877
  if (TSDB_CODE_SUCCESS == code) {
22,731✔
1878
    code = phaseAsyncQuery(pWrapper);
22,731✔
1879
  }
1880

1881
  if (TSDB_CODE_SUCCESS != code) {
22,731✔
1882
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
×
1883
             tstrerror(code), pWrapper->pRequest->requestId);
1884
    destorySqlCallbackWrapper(pWrapper);
×
1885
    pRequest->pWrapper = NULL;
×
1886
    terrno = code;
×
1887
    pRequest->code = code;
×
1888
    doRequestCallback(pRequest, code);
×
1889
  }
1890
}
22,731✔
1891

1892
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
43,633✔
1893
  int64_t connId = *(int64_t *)taos;
43,633✔
1894
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
43,633✔
1895
}
43,633✔
1896

1897
void taos_query_a_with_reqid(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param, int64_t reqid) {
×
1898
  int64_t connId = *(int64_t *)taos;
×
1899
  taosAsyncQueryImplWithReqid(connId, sql, fp, param, false, reqid);
×
1900
}
×
1901

1902
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
649,039,806✔
1903
  const STscObj *pTscObj = pRequest->pTscObj;
649,039,806✔
1904

1905
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
649,040,391✔
1906
  if (*pCxt == NULL) {
649,039,781✔
1907
    return terrno;
×
1908
  }
1909

1910
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
1,294,752,656✔
1911
                           .requestRid = pRequest->self,
649,040,391✔
1912
                           .acctId = pTscObj->acctId,
649,040,391✔
1913
                           .db = pRequest->pDb,
649,040,284✔
1914
                           .topicQuery = false,
1915
                           .pSql = pRequest->sqlstr,
649,040,165✔
1916
                           .sqlLen = pRequest->sqlLen,
649,040,165✔
1917
                           .pMsg = pRequest->msgBuf,
649,039,385✔
1918
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1919
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
649,039,885✔
1920
                           .pStmtCb = NULL,
1921
                           .pUser = pTscObj->user,
649,039,659✔
1922
                           .pEffectiveUser = pRequest->effectiveUser,
649,039,278✔
1923
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
649,039,659✔
1924
                           .enableSysInfo = pTscObj->sysInfo,
649,039,552✔
1925
                           .async = true,
1926
                           .svrVer = pTscObj->sVer,
649,038,967✔
1927
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
649,040,391✔
1928
                           .allocatorId = pRequest->allocatorRefId,
649,039,442✔
1929
                           .parseSqlFp = clientParseSql,
1930
                           .parseSqlParam = pWrapper,
1931
                           .setQueryFp = setQueryRequest,
1932
                           .timezone = pTscObj->optionInfo.timezone,
649,038,362✔
1933
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
649,038,702✔
1934
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
649,039,860✔
1935
  (*pCxt)->biMode = biMode;
649,037,646✔
1936
  return TSDB_CODE_SUCCESS;
649,039,138✔
1937
}
1938

1939
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
649,039,526✔
1940
  int32_t              code = TSDB_CODE_SUCCESS;
649,039,526✔
1941
  STscObj             *pTscObj = pRequest->pTscObj;
649,039,526✔
1942
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
649,040,165✔
1943
  if (pWrapper == NULL) {
649,040,165✔
1944
    code = terrno;
×
1945
  } else {
1946
    pWrapper->pRequest = pRequest;
649,040,165✔
1947
    pRequest->pWrapper = pWrapper;
649,039,806✔
1948
    *ppWrapper = pWrapper;
649,040,032✔
1949
  }
1950

1951
  if (TSDB_CODE_SUCCESS == code) {
649,039,806✔
1952
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
649,039,806✔
1953
  }
1954

1955
  if (TSDB_CODE_SUCCESS == code) {
649,037,250✔
1956
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
649,037,250✔
1957
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
649,039,347✔
1958
  }
1959

1960
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
649,036,405✔
1961
    int64_t syntaxStart = taosGetTimestampUs();
649,038,752✔
1962

1963
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
649,038,752✔
1964
    if (pWrapper->pCatalogReq == NULL) {
649,031,588✔
1965
      code = terrno;
×
1966
    } else {
1967
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
649,032,894✔
1968
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
649,033,557✔
1969
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
649,035,545✔
1970
    }
1971

1972
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
649,031,962✔
1973
  }
1974

1975
  return code;
649,038,586✔
1976
}
1977

1978
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
649,926,781✔
1979
  SSqlCallbackWrapper *pWrapper = NULL;
649,926,781✔
1980
  int32_t              code = TSDB_CODE_SUCCESS;
649,927,300✔
1981

1982
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
649,927,300✔
1983
    code = pRequest->prevCode;
886,909✔
1984
    terrno = code;
886,909✔
1985
    pRequest->code = code;
886,909✔
1986
    tscDebug("req:0x%" PRIx64 ", call sync query cb with code:%s", pRequest->self, tstrerror(code));
886,909✔
1987
    doRequestCallback(pRequest, code);
886,909✔
1988
    return;
886,909✔
1989
  }
1990

1991
  if (TSDB_CODE_SUCCESS == code) {
649,040,391✔
1992
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
649,040,391✔
1993
  }
1994

1995
  if (TSDB_CODE_SUCCESS == code) {
649,031,865✔
1996
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
643,959,306✔
1997
    code = phaseAsyncQuery(pWrapper);
643,958,004✔
1998
  }
1999

2000
  if (TSDB_CODE_SUCCESS != code) {
649,031,714✔
2001
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
5,073,853✔
2002
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
4,942,998✔
2003
               pRequest->requestId);
2004
    } else {
2005
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
130,855✔
2006
               pRequest->requestId);
2007
    }
2008

2009
    destorySqlCallbackWrapper(pWrapper);
5,073,853✔
2010
    pRequest->pWrapper = NULL;
5,073,853✔
2011
    qDestroyQuery(pRequest->pQuery);
5,073,853✔
2012
    pRequest->pQuery = NULL;
5,073,853✔
2013

2014
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
5,073,853✔
2015
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
10,853✔
2016
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
2017
      code = refreshMeta(pRequest->pTscObj, pRequest);
10,853✔
2018
      if (code != 0) {
10,853✔
2019
        tscWarn("req:0x%" PRIx64 ", refresh meta failed, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code,
10,853✔
2020
                tstrerror(code), pRequest->requestId);
2021
      }
2022
      pRequest->prevCode = code;
10,853✔
2023
      doAsyncQuery(pRequest, true);
10,853✔
2024
      return;
10,853✔
2025
    }
2026

2027
    terrno = code;
5,063,000✔
2028
    pRequest->code = code;
5,063,000✔
2029
    doRequestCallback(pRequest, code);
5,063,000✔
2030
  }
2031
}
2032

2033
void restartAsyncQuery(SRequestObj *pRequest, int32_t code) {
2,707,154✔
2034
  tscInfo("restart request:%s p:%p", pRequest->sqlstr, pRequest);
2,707,154✔
2035
  SRequestObj *pUserReq = pRequest;
2,707,154✔
2036
  (void)acquireRequest(pRequest->self);
2,707,154✔
2037
  while (pUserReq) {
2,707,154✔
2038
    if (pUserReq->self == pUserReq->relation.userRefId || pUserReq->relation.userRefId == 0) {
2,707,154✔
2039
      break;
2040
    } else {
2041
      int64_t nextRefId = pUserReq->relation.nextRefId;
×
2042
      (void)releaseRequest(pUserReq->self);
×
2043
      if (nextRefId) {
×
2044
        pUserReq = acquireRequest(nextRefId);
×
2045
      }
2046
    }
2047
  }
2048
  bool hasSubRequest = pUserReq != pRequest || pRequest->relation.prevRefId != 0;
2,707,154✔
2049
  if (pUserReq) {
2,707,154✔
2050
    destroyCtxInRequest(pUserReq);
2,707,154✔
2051
    pUserReq->prevCode = code;
2,707,154✔
2052
    (void)memset(&pUserReq->relation, 0, sizeof(pUserReq->relation));
2,707,154✔
2053
  } else {
2054
    tscError("User req is missing");
×
2055
    (void)removeFromMostPrevReq(pRequest);
×
2056
    return;
×
2057
  }
2058
  if (hasSubRequest)
2,707,154✔
2059
    (void)removeFromMostPrevReq(pRequest);
×
2060
  else
2061
    (void)releaseRequest(pUserReq->self);
2,707,154✔
2062
  doAsyncQuery(pUserReq, true);
2,707,154✔
2063
}
2064

2065
typedef struct SAsyncFetchParam {
2066
  SRequestObj      *pReq;
2067
  __taos_async_fn_t fp;
2068
  void             *param;
2069
} SAsyncFetchParam;
2070

2071
static int32_t doAsyncFetch(void *pParam) {
101,532,302✔
2072
  SAsyncFetchParam *param = pParam;
101,532,302✔
2073
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
101,532,302✔
2074
  taosMemoryFree(param);
101,530,633✔
2075
  return TSDB_CODE_SUCCESS;
101,530,865✔
2076
}
2077

2078
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
101,534,506✔
2079
  if (res == NULL || fp == NULL) {
101,534,506✔
2080
    tscError("taos_fetch_rows_a invalid paras");
×
2081
    return;
×
2082
  }
2083
  if (!TD_RES_QUERY(res)) {
101,535,302✔
2084
    tscError("taos_fetch_rows_a res is NULL");
×
2085
    fp(param, res, TSDB_CODE_APP_ERROR);
×
2086
    return;
×
2087
  }
2088

2089
  SRequestObj *pRequest = res;
101,534,830✔
2090
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
101,534,830✔
2091
    fp(param, res, 0);
3,000✔
2092
    return;
3,000✔
2093
  }
2094

2095
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
101,532,302✔
2096
  if (!pParam) {
101,532,053✔
2097
    fp(param, res, terrno);
×
2098
    return;
×
2099
  }
2100
  pParam->pReq = pRequest;
101,532,053✔
2101
  pParam->fp = fp;
101,532,302✔
2102
  pParam->param = param;
101,532,302✔
2103
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
101,532,087✔
2104
  if (TSDB_CODE_SUCCESS != code) {
101,532,048✔
2105
    taosMemoryFree(pParam);
×
2106
    fp(param, res, code);
×
2107
    return;
×
2108
  }
2109
}
2110

2111
void taos_fetch_raw_block_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
476✔
2112
  if (res == NULL || fp == NULL) {
476✔
2113
    tscError("taos_fetch_raw_block_a invalid paras");
×
2114
    return;
×
2115
  }
2116
  if (!TD_RES_QUERY(res)) {
476✔
2117
    tscError("taos_fetch_raw_block_a res is NULL");
×
2118
    return;
×
2119
  }
2120
  SRequestObj    *pRequest = res;
476✔
2121
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
476✔
2122

2123
  // set the current block is all consumed
2124
  pResultInfo->convertUcs4 = false;
476✔
2125

2126
  // it is a local executed query, no need to do async fetch
2127
  taos_fetch_rows_a(pRequest, fp, param);
476✔
2128
}
2129

2130
const void *taos_get_raw_block(TAOS_RES *res) {
280✔
2131
  if (res == NULL) {
280✔
2132
    tscError("taos_get_raw_block invalid paras");
×
2133
    return NULL;
×
2134
  }
2135
  if (!TD_RES_QUERY(res)) {
280✔
2136
    tscError("taos_get_raw_block res is NULL");
×
2137
    return NULL;
×
2138
  }
2139
  SRequestObj *pRequest = res;
280✔
2140

2141
  return pRequest->body.resInfo.pData;
280✔
2142
}
2143

2144
int taos_get_db_route_info(TAOS *taos, const char *db, TAOS_DB_ROUTE_INFO *dbInfo) {
×
2145
  if (NULL == taos) {
×
2146
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2147
    return terrno;
×
2148
  }
2149

2150
  if (NULL == db || NULL == dbInfo) {
×
2151
    tscError("invalid input param, db:%p, dbInfo:%p", db, dbInfo);
×
2152
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2153
    return terrno;
×
2154
  }
2155

2156
  int64_t      connId = *(int64_t *)taos;
×
2157
  SRequestObj *pRequest = NULL;
×
2158
  char        *sql = "taos_get_db_route_info";
×
2159
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2160
  if (code != TSDB_CODE_SUCCESS) {
×
2161
    terrno = code;
×
2162
    return terrno;
×
2163
  }
2164

2165
  STscObj  *pTscObj = pRequest->pTscObj;
×
2166
  SCatalog *pCtg = NULL;
×
2167
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2168
  if (code != TSDB_CODE_SUCCESS) {
×
2169
    goto _return;
×
2170
  }
2171

2172
  SRequestConnInfo conn = {
×
2173
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2174

2175
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2176

2177
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
×
2178
  (void)snprintf(dbFName, sizeof(dbFName), "%d.%s", pTscObj->acctId, db);
×
2179

2180
  code = catalogGetDBVgInfo(pCtg, &conn, dbFName, dbInfo);
×
2181
  if (code) {
×
2182
    goto _return;
×
2183
  }
2184

2185
_return:
×
2186

2187
  terrno = code;
×
2188

2189
  destroyRequest(pRequest);
×
2190
  return code;
×
2191
}
2192

2193
int taos_get_table_vgId(TAOS *taos, const char *db, const char *table, int *vgId) {
×
2194
  if (NULL == taos) {
×
2195
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2196
    return terrno;
×
2197
  }
2198

2199
  if (NULL == db || NULL == table || NULL == vgId) {
×
2200
    tscError("invalid input param, db:%p, table:%p, vgId:%p", db, table, vgId);
×
2201
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2202
    return terrno;
×
2203
  }
2204

2205
  int64_t      connId = *(int64_t *)taos;
×
2206
  SRequestObj *pRequest = NULL;
×
2207
  char        *sql = "taos_get_table_vgId";
×
2208
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2209
  if (code != TSDB_CODE_SUCCESS) {
×
2210
    return terrno;
×
2211
  }
2212

2213
  pRequest->syncQuery = true;
×
2214

2215
  STscObj  *pTscObj = pRequest->pTscObj;
×
2216
  SCatalog *pCtg = NULL;
×
2217
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2218
  if (code != TSDB_CODE_SUCCESS) {
×
2219
    goto _return;
×
2220
  }
2221

2222
  SRequestConnInfo conn = {
×
2223
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2224

2225
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2226

2227
  SName tableName = {0};
×
2228
  toName(pTscObj->acctId, db, table, &tableName);
×
2229

2230
  SVgroupInfo vgInfo;
×
2231
  code = catalogGetTableHashVgroup(pCtg, &conn, &tableName, &vgInfo);
×
2232
  if (code) {
×
2233
    goto _return;
×
2234
  }
2235

2236
  *vgId = vgInfo.vgId;
×
2237

2238
_return:
×
2239

2240
  terrno = code;
×
2241

2242
  destroyRequest(pRequest);
×
2243
  return code;
×
2244
}
2245

2246
int taos_get_tables_vgId(TAOS *taos, const char *db, const char *table[], int tableNum, int *vgId) {
×
2247
  if (NULL == taos) {
×
2248
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2249
    return terrno;
×
2250
  }
2251

2252
  if (NULL == db || NULL == table || NULL == vgId || tableNum <= 0) {
×
2253
    tscError("invalid input param, db:%p, table:%p, vgId:%p, tbNum:%d", db, table, vgId, tableNum);
×
2254
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2255
    return terrno;
×
2256
  }
2257

2258
  int64_t      connId = *(int64_t *)taos;
×
2259
  SRequestObj *pRequest = NULL;
×
2260
  char        *sql = "taos_get_table_vgId";
×
2261
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2262
  if (code != TSDB_CODE_SUCCESS) {
×
2263
    return terrno;
×
2264
  }
2265

2266
  pRequest->syncQuery = true;
×
2267

2268
  STscObj  *pTscObj = pRequest->pTscObj;
×
2269
  SCatalog *pCtg = NULL;
×
2270
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2271
  if (code != TSDB_CODE_SUCCESS) {
×
2272
    goto _return;
×
2273
  }
2274

2275
  SRequestConnInfo conn = {
×
2276
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2277

2278
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2279

2280
  code = catalogGetTablesHashVgId(pCtg, &conn, pTscObj->acctId, db, table, tableNum, vgId);
×
2281
  if (code) {
×
2282
    goto _return;
×
2283
  }
2284

2285
_return:
×
2286

2287
  terrno = code;
×
2288

2289
  destroyRequest(pRequest);
×
2290
  return code;
×
2291
}
2292

2293
int taos_load_table_info(TAOS *taos, const char *tableNameList) {
1,278✔
2294
  if (NULL == taos) {
1,278✔
2295
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2296
    return terrno;
×
2297
  }
2298

2299
  int64_t       connId = *(int64_t *)taos;
1,278✔
2300
  const int32_t MAX_TABLE_NAME_LENGTH = 12 * 1024 * 1024;  // 12MB list
1,278✔
2301
  int32_t       code = 0;
1,278✔
2302
  SRequestObj  *pRequest = NULL;
1,278✔
2303
  SCatalogReq   catalogReq = {0};
1,278✔
2304

2305
  if (NULL == tableNameList) {
1,278✔
2306
    return TSDB_CODE_SUCCESS;
×
2307
  }
2308

2309
  int32_t length = (int32_t)strlen(tableNameList);
1,278✔
2310
  if (0 == length) {
1,278✔
2311
    return TSDB_CODE_SUCCESS;
×
2312
  } else if (length > MAX_TABLE_NAME_LENGTH) {
1,278✔
2313
    tscError("tableNameList too long, length:%d, maximum allowed:%d", length, MAX_TABLE_NAME_LENGTH);
×
2314
    return TSDB_CODE_TSC_INVALID_OPERATION;
×
2315
  }
2316

2317
  char *sql = "taos_load_table_info";
1,278✔
2318
  code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
1,278✔
2319
  if (code != TSDB_CODE_SUCCESS) {
1,278✔
2320
    terrno = code;
×
2321
    goto _return;
×
2322
  }
2323

2324
  pRequest->syncQuery = true;
1,278✔
2325

2326
  STscObj *pTscObj = pRequest->pTscObj;
1,278✔
2327
  code = transferTableNameList(tableNameList, pTscObj->acctId, pTscObj->db, &catalogReq.pTableMeta);
1,278✔
2328
  if (code) {
1,278✔
2329
    goto _return;
×
2330
  }
2331

2332
  SCatalog *pCtg = NULL;
1,278✔
2333
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
1,278✔
2334
  if (code != TSDB_CODE_SUCCESS) {
1,278✔
2335
    goto _return;
×
2336
  }
2337

2338
  SRequestConnInfo conn = {
1,278✔
2339
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
1,278✔
2340

2341
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
1,278✔
2342

2343
  code = catalogAsyncGetAllMeta(pCtg, &conn, &catalogReq, syncCatalogFn, pRequest->body.interParam, NULL);
1,278✔
2344
  if (code) {
1,278✔
2345
    goto _return;
×
2346
  }
2347

2348
  SSyncQueryParam *pParam = pRequest->body.interParam;
1,278✔
2349
  code = tsem_wait(&pParam->sem);
1,278✔
2350
  if (code) {
1,278✔
2351
    tscError("tsem wait failed, code:%d - %s", code, tstrerror(code));
×
2352
    goto _return;
×
2353
  }
2354
_return:
1,278✔
2355
  destoryCatalogReq(&catalogReq);
1,278✔
2356
  destroyRequest(pRequest);
1,278✔
2357
  return code;
1,278✔
2358
}
2359

2360
TAOS_STMT *taos_stmt_init(TAOS *taos) {
331,651✔
2361
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
331,651✔
2362
  if (NULL == pObj) {
331,651✔
2363
    tscError("invalid parameter for %s", __FUNCTION__);
×
2364
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2365
    return NULL;
×
2366
  }
2367

2368
  TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL);
331,651✔
2369
  if (NULL == pStmt) {
331,651✔
2370
    tscError("stmt init failed, errcode:%s", terrstr());
×
2371
  }
2372
  releaseTscObj(*(int64_t *)taos);
331,651✔
2373

2374
  return pStmt;
331,651✔
2375
}
2376

2377
TAOS_STMT *taos_stmt_init_with_reqid(TAOS *taos, int64_t reqid) {
×
2378
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
2379
  if (NULL == pObj) {
×
2380
    tscError("invalid parameter for %s", __FUNCTION__);
×
2381
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2382
    return NULL;
×
2383
  }
2384

2385
  TAOS_STMT *pStmt = stmtInit(pObj, reqid, NULL);
×
2386
  if (NULL == pStmt) {
×
2387
    tscError("stmt init failed, errcode:%s", terrstr());
×
2388
  }
2389
  releaseTscObj(*(int64_t *)taos);
×
2390

2391
  return pStmt;
×
2392
}
2393

2394
TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options) {
19,351✔
2395
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
19,351✔
2396
  if (NULL == pObj) {
19,459✔
2397
    tscError("invalid parameter for %s", __FUNCTION__);
×
2398
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2399
    return NULL;
×
2400
  }
2401

2402
  TAOS_STMT *pStmt = stmtInit(pObj, options->reqId, options);
19,459✔
2403
  if (NULL == pStmt) {
19,459✔
2404
    tscError("stmt init failed, errcode:%s", terrstr());
×
2405
  }
2406
  releaseTscObj(*(int64_t *)taos);
19,459✔
2407

2408
  return pStmt;
19,459✔
2409
}
2410

2411
int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
890,788✔
2412
  if (stmt == NULL || sql == NULL) {
890,788✔
2413
    tscError("NULL parameter for %s", __FUNCTION__);
×
2414
    terrno = TSDB_CODE_INVALID_PARA;
×
2415
    return terrno;
×
2416
  }
2417

2418
  return stmtPrepare(stmt, sql, length);
890,842✔
2419
}
2420

2421
int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags) {
7,649✔
2422
  if (stmt == NULL || name == NULL) {
7,649✔
2423
    tscError("NULL parameter for %s", __FUNCTION__);
×
2424
    terrno = TSDB_CODE_INVALID_PARA;
×
2425
    return terrno;
×
2426
  }
2427

2428
  int32_t code = stmtSetTbName(stmt, name);
7,649✔
2429
  if (code) {
7,649✔
2430
    return code;
639✔
2431
  }
2432

2433
  if (tags) {
7,010✔
2434
    return stmtSetTbTags(stmt, tags);
7,010✔
2435
  }
2436

2437
  return TSDB_CODE_SUCCESS;
×
2438
}
2439

2440
int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name) {
12,538,994✔
2441
  if (stmt == NULL || name == NULL) {
12,538,994✔
UNCOV
2442
    tscError("NULL parameter for %s", __FUNCTION__);
×
UNCOV
2443
    terrno = TSDB_CODE_INVALID_PARA;
×
2444
    return terrno;
×
2445
  }
2446

2447
  return stmtSetTbName(stmt, name);
12,544,656✔
2448
}
2449

2450
int taos_stmt_set_tags(TAOS_STMT *stmt, TAOS_MULTI_BIND *tags) {
616✔
2451
  if (stmt == NULL || tags == NULL) {
616✔
2452
    tscError("NULL parameter for %s", __FUNCTION__);
×
2453
    terrno = TSDB_CODE_INVALID_PARA;
×
2454
    return terrno;
×
2455
  }
2456

2457
  return stmtSetTbTags(stmt, tags);
616✔
2458
}
2459

2460
int taos_stmt_set_sub_tbname(TAOS_STMT *stmt, const char *name) { return taos_stmt_set_tbname(stmt, name); }
×
2461

2462
int taos_stmt_get_tag_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
×
2463
  if (stmt == NULL || NULL == fieldNum) {
×
2464
    tscError("NULL parameter for %s", __FUNCTION__);
×
2465
    terrno = TSDB_CODE_INVALID_PARA;
×
2466
    return terrno;
×
2467
  }
2468

2469
  return stmtGetTagFields(stmt, fieldNum, fields);
×
2470
}
2471

2472
int taos_stmt_get_col_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
×
2473
  if (stmt == NULL || NULL == fieldNum) {
×
2474
    tscError("NULL parameter for %s", __FUNCTION__);
×
2475
    terrno = TSDB_CODE_INVALID_PARA;
×
2476
    return terrno;
×
2477
  }
2478

2479
  return stmtGetColFields(stmt, fieldNum, fields);
×
2480
}
2481

2482
// let stmt to reclaim TAOS_FIELD_E that was allocated by `taos_stmt_get_tag_fields`/`taos_stmt_get_col_fields`
2483
void taos_stmt_reclaim_fields(TAOS_STMT *stmt, TAOS_FIELD_E *fields) {
×
2484
  (void)stmt;
2485
  if (!fields) return;
×
2486
  taosMemoryFree(fields);
×
2487
}
2488

2489
int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
33,750✔
2490
  if (stmt == NULL || bind == NULL) {
33,750✔
2491
    tscError("NULL parameter for %s", __FUNCTION__);
×
2492
    terrno = TSDB_CODE_INVALID_PARA;
×
2493
    return terrno;
×
2494
  }
2495

2496
  if (bind->num > 1) {
33,750✔
2497
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
4,128✔
2498
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
4,128✔
2499
    return terrno;
4,128✔
2500
  }
2501

2502
  return stmtBindBatch(stmt, bind, -1);
29,622✔
2503
}
2504

2505
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
520,759,977✔
2506
  if (stmt == NULL || bind == NULL) {
520,759,977✔
2507
    tscError("NULL parameter for %s", __FUNCTION__);
×
2508
    terrno = TSDB_CODE_INVALID_PARA;
×
2509
    return terrno;
×
2510
  }
2511

2512
  if (bind->num <= 0 || bind->num > INT16_MAX) {
528,551,186✔
2513
    tscError("invalid bind num %d", bind->num);
570✔
2514
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
570✔
2515
    return terrno;
×
2516
  }
2517

2518
  int32_t insert = 0;
530,622,525✔
2519
  int32_t code = stmtIsInsert(stmt, &insert);
527,471,963✔
2520
  if (TSDB_CODE_SUCCESS != code) {
530,840,334✔
2521
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2522
    return code;
×
2523
  }
2524
  if (0 == insert && bind->num > 1) {
530,840,334✔
2525
    tscError("only one row data allowed for query");
×
2526
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2527
    return terrno;
×
2528
  }
2529

2530
  return stmtBindBatch(stmt, bind, -1);
530,840,334✔
2531
}
2532

2533
int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind, int colIdx) {
3,920✔
2534
  if (stmt == NULL || bind == NULL) {
3,920✔
2535
    tscError("NULL parameter for %s", __FUNCTION__);
×
2536
    terrno = TSDB_CODE_INVALID_PARA;
×
2537
    return terrno;
×
2538
  }
2539

2540
  if (colIdx < 0) {
3,920✔
2541
    tscError("invalid bind column idx %d", colIdx);
×
2542
    terrno = TSDB_CODE_INVALID_PARA;
×
2543
    return terrno;
×
2544
  }
2545

2546
  int32_t insert = 0;
3,920✔
2547
  int32_t code = stmtIsInsert(stmt, &insert);
3,920✔
2548
  if (TSDB_CODE_SUCCESS != code) {
3,920✔
2549
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2550
    return code;
×
2551
  }
2552
  if (0 == insert && bind->num > 1) {
3,920✔
2553
    tscError("only one row data allowed for query");
×
2554
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2555
    return terrno;
×
2556
  }
2557

2558
  return stmtBindBatch(stmt, bind, colIdx);
3,920✔
2559
}
2560

2561
int taos_stmt_add_batch(TAOS_STMT *stmt) {
509,803,068✔
2562
  if (stmt == NULL) {
509,803,068✔
2563
    tscError("NULL parameter for %s", __FUNCTION__);
×
2564
    terrno = TSDB_CODE_INVALID_PARA;
×
2565
    return terrno;
×
2566
  }
2567

2568
  return stmtAddBatch(stmt);
509,803,068✔
2569
}
2570

2571
int taos_stmt_execute(TAOS_STMT *stmt) {
3,640,316✔
2572
  if (stmt == NULL) {
3,640,316✔
2573
    tscError("NULL parameter for %s", __FUNCTION__);
×
2574
    terrno = TSDB_CODE_INVALID_PARA;
×
2575
    return terrno;
×
2576
  }
2577

2578
  return stmtExec(stmt);
3,640,316✔
2579
}
2580

2581
int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
×
2582
  if (stmt == NULL || insert == NULL) {
×
2583
    tscError("NULL parameter for %s", __FUNCTION__);
×
2584
    terrno = TSDB_CODE_INVALID_PARA;
×
2585
    return terrno;
×
2586
  }
2587

2588
  return stmtIsInsert(stmt, insert);
×
2589
}
2590

2591
int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) {
×
2592
  if (stmt == NULL || nums == NULL) {
×
2593
    tscError("NULL parameter for %s", __FUNCTION__);
×
2594
    terrno = TSDB_CODE_INVALID_PARA;
×
2595
    return terrno;
×
2596
  }
2597

2598
  return stmtGetParamNum(stmt, nums);
×
2599
}
2600

2601
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
×
2602
  if (stmt == NULL || type == NULL || NULL == bytes || idx < 0) {
×
2603
    tscError("invalid parameter for %s", __FUNCTION__);
×
2604
    terrno = TSDB_CODE_INVALID_PARA;
×
2605
    return terrno;
×
2606
  }
2607

2608
  return stmtGetParam(stmt, idx, type, bytes);
×
2609
}
2610

2611
TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt) {
10,568✔
2612
  if (stmt == NULL) {
10,568✔
2613
    tscError("NULL parameter for %s", __FUNCTION__);
×
2614
    terrno = TSDB_CODE_INVALID_PARA;
×
2615
    return NULL;
×
2616
  }
2617

2618
  return stmtUseResult(stmt);
10,568✔
2619
}
2620

2621
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
10,821✔
2622

2623
int taos_stmt_affected_rows(TAOS_STMT *stmt) {
2,948✔
2624
  if (stmt == NULL) {
2,948✔
2625
    tscError("NULL parameter for %s", __FUNCTION__);
×
2626
    terrno = TSDB_CODE_INVALID_PARA;
×
2627
    return 0;
×
2628
  }
2629

2630
  return stmtAffectedRows(stmt);
2,948✔
2631
}
2632

2633
int taos_stmt_affected_rows_once(TAOS_STMT *stmt) {
280✔
2634
  if (stmt == NULL) {
280✔
2635
    tscError("NULL parameter for %s", __FUNCTION__);
×
2636
    terrno = TSDB_CODE_INVALID_PARA;
×
2637
    return 0;
×
2638
  }
2639

2640
  return stmtAffectedRowsOnce(stmt);
280✔
2641
}
2642

2643
int taos_stmt_close(TAOS_STMT *stmt) {
350,853✔
2644
  if (stmt == NULL) {
350,853✔
2645
    tscError("NULL parameter for %s", __FUNCTION__);
×
2646
    terrno = TSDB_CODE_INVALID_PARA;
×
2647
    return terrno;
×
2648
  }
2649

2650
  return stmtClose(stmt);
350,853✔
2651
}
2652

2653
TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option) {
17,984✔
2654
  if (NULL == taos) {
17,984✔
2655
    tscError("NULL parameter for %s", __FUNCTION__);
×
2656
    terrno = TSDB_CODE_INVALID_PARA;
×
2657
    return NULL;
×
2658
  }
2659
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
17,984✔
2660
  if (NULL == pObj) {
17,984✔
2661
    tscError("invalid parameter for %s", __FUNCTION__);
×
2662
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2663
    return NULL;
×
2664
  }
2665

2666
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
17,984✔
2667

2668
  releaseTscObj(*(int64_t *)taos);
17,984✔
2669

2670
  return pStmt;
17,984✔
2671
}
2672

2673
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
18,256✔
2674
  if (stmt == NULL || sql == NULL) {
18,256✔
2675
    tscError("NULL parameter for %s", __FUNCTION__);
×
2676
    terrno = TSDB_CODE_INVALID_PARA;
×
2677
    return terrno;
×
2678
  }
2679

2680
  return stmtPrepare2(stmt, sql, length);
18,256✔
2681
}
2682

2683
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
2,019,832✔
2684
  if (stmt == NULL) {
2,019,832✔
2685
    tscError("NULL parameter for %s", __FUNCTION__);
×
2686
    terrno = TSDB_CODE_INVALID_PARA;
×
2687
    return terrno;
×
2688
  }
2689

2690
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
2,019,832✔
2691
  int32_t    code = TSDB_CODE_SUCCESS;
2,019,832✔
2692
  STMT2_DLOG_E("start to bind param");
2,019,832✔
2693

2694
  // check query bind number
2695
  bool isQuery = (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt)));
2,019,968✔
2696
  if (isQuery) {
2,018,928✔
2697
    if (bindv->count != 1 || bindv->bind_cols[0]->num != 1) {
28✔
2698
      terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2699
      STMT2_ELOG_E("query only support one table and one row bind");
×
2700
      return terrno;
×
2701
    }
2702
  }
2703

2704
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 1) {
2,018,928✔
2705
    STMT2_ELOG_E("async bind param is still working, please try again later");
476✔
2706
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
476✔
2707
    return terrno;
×
2708
  }
2709

2710
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
2,018,340✔
2711
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
2712
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
2713
    }
2714
    pStmt->execSemWaited = true;
×
2715
  }
2716

2717
  for (int i = 0; i < bindv->count; ++i) {
5,911,227✔
2718
    SVCreateTbReq *pCreateTbReq = NULL;
3,891,082✔
2719
    if (!isQuery) {
3,890,822✔
2720
      STMT2_TLOG("start to bind %dth table", i);
3,891,185✔
2721
      if (bindv->tbnames && bindv->tbnames[i]) {
3,891,117✔
2722
        code = stmtSetTbName2(stmt, bindv->tbnames[i]);
3,892,174✔
2723
        if (code) {
3,891,914✔
2724
          terrno = code;
×
2725
          STMT2_ELOG("set tbname failed, code:%s", tstrerror(code));
×
2726
          return terrno;
×
2727
        }
2728
      }
2729

2730
      if (bindv->tags && bindv->tags[i]) {
3,890,082✔
2731
        code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
1,374,164✔
2732
      } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
2,517,998✔
2733
        code = stmtCheckTags2(stmt, &pCreateTbReq);
271,728✔
2734
      } else if (pStmt->sql.autoCreateTbl) {
2,243,930✔
2735
        code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
×
2736
      }
2737

2738
      if (code) {
3,888,801✔
2739
        terrno = code;
×
2740
        STMT2_ELOG("set tags failed, code:%s", tstrerror(code));
×
2741
        return terrno;
×
2742
      }
2743
    }
2744

2745
    if (bindv->bind_cols && bindv->bind_cols[i]) {
3,888,438✔
2746
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
3,891,852✔
2747

2748
      if (bind->num <= 0 || bind->num > INT16_MAX) {
3,891,852✔
2749
        STMT2_ELOG("bind num:%d must > 0 and < INT16_MAX", bind->num);
1,820✔
2750
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
1,820✔
2751
        return terrno;
×
2752
      }
2753

2754
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
3,889,558✔
2755
      if (TSDB_CODE_SUCCESS != code) {
3,893,181✔
2756
        terrno = code;
5✔
2757
        STMT2_ELOG("bind batch failed, code:%s", tstrerror(code));
×
2758
        return terrno;
×
2759
      }
2760
    }
2761
  }
2762

2763
  return code;
2,021,710✔
2764
}
2765

2766
int taos_stmt2_bind_param_a(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx, __taos_async_fn_t fp,
×
2767
                            void *param) {
2768
  if (stmt == NULL || bindv == NULL || fp == NULL) {
×
2769
    terrno = TSDB_CODE_INVALID_PARA;
×
2770
    return terrno;
×
2771
  }
2772

2773
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2774

2775
  ThreadArgs *args = (ThreadArgs *)taosMemoryMalloc(sizeof(ThreadArgs));
×
2776
  args->stmt = stmt;
×
2777
  args->bindv = bindv;
×
2778
  args->col_idx = col_idx;
×
2779
  args->fp = fp;
×
2780
  args->param = param;
×
2781

2782
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2783
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 0) {
×
2784
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2785
    tscError("async bind param is still working, please try again later");
×
2786
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
×
2787
    return terrno;
×
2788
  }
2789
  (void)atomic_add_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2790
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2791

2792
  int code_s = taosStmt2AsyncBind(stmtAsyncBindThreadFunc, (void *)args);
×
2793
  if (code_s != TSDB_CODE_SUCCESS) {
×
2794
    terrno = code_s;
×
2795
    (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2796
    (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2797
    (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2798
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2799
    tscError("async bind failed, code:%d , %s", code_s, tstrerror(code_s));
×
2800
  }
2801

2802
  return code_s;
×
2803
}
2804

2805
int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows) {
2,020,986✔
2806
  if (stmt == NULL) {
2,020,986✔
2807
    tscError("NULL parameter for %s", __FUNCTION__);
×
2808
    terrno = TSDB_CODE_INVALID_PARA;
×
2809
    return terrno;
×
2810
  }
2811

2812
  return stmtExec2(stmt, affected_rows);
2,020,986✔
2813
}
2814

2815
int taos_stmt2_close(TAOS_STMT2 *stmt) {
17,984✔
2816
  if (stmt == NULL) {
17,984✔
2817
    tscError("NULL parameter for %s", __FUNCTION__);
×
2818
    terrno = TSDB_CODE_INVALID_PARA;
×
2819
    return terrno;
×
2820
  }
2821

2822
  return stmtClose2(stmt);
17,984✔
2823
}
2824

2825
int taos_stmt2_is_insert(TAOS_STMT2 *stmt, int *insert) {
110✔
2826
  if (stmt == NULL || insert == NULL) {
110✔
2827
    tscError("NULL parameter for %s", __FUNCTION__);
×
2828
    terrno = TSDB_CODE_INVALID_PARA;
×
2829
    return terrno;
×
2830
  }
2831
  *insert = stmt2IsInsert(stmt);
110✔
2832
  return TSDB_CODE_SUCCESS;
110✔
2833
}
2834

2835
int taos_stmt2_get_fields(TAOS_STMT2 *stmt, int *count, TAOS_FIELD_ALL **fields) {
82✔
2836
  if (stmt == NULL || count == NULL) {
82✔
2837
    tscError("NULL parameter for %s", __FUNCTION__);
×
2838
    terrno = TSDB_CODE_INVALID_PARA;
×
2839
    return terrno;
×
2840
  }
2841

2842
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
82✔
2843
  STMT2_DLOG_E("start to get fields");
82✔
2844

2845
  if (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type ||
82✔
2846
      (pStmt->sql.type == 0 && stmt2IsInsert(stmt))) {
82✔
2847
    return stmtGetStbColFields2(stmt, count, fields);
82✔
2848
  }
2849
  if (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt))) {
×
2850
    return stmtGetParamNum2(stmt, count);
×
2851
  }
2852

2853
  tscError("Invalid sql for stmt %s", pStmt->sql.sqlStr);
×
2854
  return TSDB_CODE_PAR_SYNTAX_ERROR;
×
2855
}
2856

2857
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_ALL *fields) {
82✔
2858
  (void)stmt;
2859
  if (!fields) return;
82✔
2860
  taosMemoryFree(fields);
82✔
2861
}
2862

2863
TAOS_RES *taos_stmt2_result(TAOS_STMT2 *stmt) {
28✔
2864
  if (stmt == NULL) {
28✔
2865
    tscError("NULL parameter for %s", __FUNCTION__);
×
2866
    terrno = TSDB_CODE_INVALID_PARA;
×
2867
    return NULL;
×
2868
  }
2869

2870
  return stmtUseResult2(stmt);
28✔
2871
}
2872

2873
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmtErrstr2(stmt); }
×
2874

2875
int taos_set_conn_mode(TAOS *taos, int mode, int value) {
2,508✔
2876
  if (taos == NULL) {
2,508✔
2877
    terrno = TSDB_CODE_INVALID_PARA;
×
2878
    return terrno;
×
2879
  }
2880

2881
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2,508✔
2882
  if (NULL == pObj) {
2,508✔
2883
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2884
    tscError("invalid parameter for %s", __func__);
×
2885
    return terrno;
×
2886
  }
2887
  switch (mode) {
2,508✔
2888
    case TAOS_CONN_MODE_BI:
2,508✔
2889
      atomic_store_8(&pObj->biMode, value);
2,508✔
2890
      break;
2,508✔
2891
    default:
×
2892
      tscError("not supported mode.");
×
2893
      return TSDB_CODE_INVALID_PARA;
×
2894
  }
2895
  return 0;
2,508✔
2896
}
2897

2898
char *getBuildInfo() { return td_buildinfo; }
×
2899

2900
int32_t taos_connect_is_alive(TAOS *taos) {
×
2901
  int32_t code = 0;
×
2902
  code = TSDB_CODE_TSC_SESS_CONN_TIMEOUT;
×
2903

2904
  if (code != TSDB_CODE_SUCCESS) {
×
2905
    return 0; 
×
2906
  } else {
2907
    return 1;
×
2908
  }
2909
}
2910
static int32_t buildInstanceRegisterSql(const SInstanceRegisterReq *req, char **ppSql, uint32_t *pLen) {
×
2911
  const char *action = (req->expire < 0) ? "UNREGISTER" : "REGISTER";
×
2912
  int32_t     len = 0;
×
2913

2914
  len += snprintf(NULL, 0, "%s INSTANCE '%s'", action, req->id);
×
2915
  if (req->type[0] != 0) {
×
2916
    len += snprintf(NULL, 0, " TYPE '%s'", req->type);
×
2917
  }
2918
  if (req->desc[0] != 0) {
×
2919
    len += snprintf(NULL, 0, " DESC '%s'", req->desc);
×
2920
  }
2921
  if (req->expire >= 0) {
×
2922
    len += snprintf(NULL, 0, " EXPIRE %d", req->expire);
×
2923
  }
2924

2925
  char *sql = taosMemoryMalloc((size_t)len + 1);
×
2926
  if (sql == NULL) {
×
2927
    return terrno;
×
2928
  }
2929

2930
  int32_t offset = snprintf(sql, (size_t)len + 1, "%s INSTANCE '%s'", action, req->id);
×
2931
  if (req->type[0] != 0) {
×
2932
    offset += snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " TYPE '%s'", req->type);
×
2933
  }
2934
  if (req->desc[0] != 0) {
×
2935
    offset += snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " DESC '%s'", req->desc);
×
2936
  }
2937
  if (req->expire >= 0) {
×
2938
    (void)snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " EXPIRE %d", req->expire);
×
2939
  }
2940

2941
  *ppSql = sql;
×
2942
  if (pLen != NULL) {
×
2943
    *pLen = (uint32_t)len;
×
2944
  }
2945
  return TSDB_CODE_SUCCESS;
×
2946
}
2947

2948
static int32_t sendInstanceRegisterReq(STscObj *pObj, const SInstanceRegisterReq *req) {
×
2949
  SRequestObj *pRequest = NULL;
×
2950
  int32_t      code = createRequest(pObj->id, TDMT_MND_REGISTER_INSTANCE, 0, &pRequest);
×
2951
  if (code != TSDB_CODE_SUCCESS) {
×
2952
    terrno = code;
×
2953
    return code;
×
2954
  }
2955

2956
  code = buildInstanceRegisterSql(req, &pRequest->sqlstr, &pRequest->sqlLen);
×
2957
  if (code != TSDB_CODE_SUCCESS) {
×
2958
    goto _cleanup;
×
2959
  }
2960

2961
  int32_t msgLen = tSerializeSInstanceRegisterReq(NULL, 0, (SInstanceRegisterReq *)req);
×
2962
  if (msgLen <= 0) {
×
2963
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
2964
    goto _cleanup;
×
2965
  }
2966

2967
  void *pMsg = taosMemoryMalloc(msgLen);
×
2968
  if (pMsg == NULL) {
×
2969
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
2970
    goto _cleanup;
×
2971
  }
2972

2973
  if (tSerializeSInstanceRegisterReq(pMsg, msgLen, (SInstanceRegisterReq *)req) < 0) {
×
2974
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
2975
    taosMemoryFree(pMsg);
×
2976
    goto _cleanup;
×
2977
  }
2978

2979
  pRequest->type = TDMT_MND_REGISTER_INSTANCE;
×
2980
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsg, .len = msgLen, .handle = NULL};
×
2981

2982
  SMsgSendInfo *pSend = buildMsgInfoImpl(pRequest);
×
2983
  if (pSend == NULL) {
×
2984
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
2985
    taosMemoryFree(pMsg);
×
2986
    pRequest->body.requestMsg.pData = NULL;
×
2987
    goto _cleanup;
×
2988
  }
2989

2990
  SEpSet epSet = getEpSet_s(&pObj->pAppInfo->mgmtEp);
×
2991
  code = asyncSendMsgToServer(pObj->pAppInfo->pTransporter, &epSet, NULL, pSend);
×
2992
  if (code != TSDB_CODE_SUCCESS) {
×
2993
    destroySendMsgInfo(pSend);
×
2994
    pRequest->body.requestMsg = (SDataBuf){0};
×
2995
    goto _cleanup;
×
2996
  }
2997

2998
  code = tsem_wait(&pRequest->body.rspSem);
×
2999
  if (code != TSDB_CODE_SUCCESS) {
×
3000
    code = terrno != 0 ? terrno : code;
×
3001
    goto _cleanup;
×
3002
  }
3003

3004
  code = pRequest->code;
×
3005
  terrno = code;
×
3006

3007
_cleanup:
×
3008
  destroyRequest(pRequest);
×
3009
  return code;
×
3010
}
3011

3012
static bool instanceRegisterRpcRfp(int32_t code, tmsg_t msgType) {
×
3013
  if (NEED_REDIRECT_ERROR(code)) {
×
3014
    return true;
×
3015
  } else if (code == TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY || code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE ||
×
3016
             code == TSDB_CODE_SYN_WRITE_STALL || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
×
3017
             code == TSDB_CODE_SYN_RESTORING) {
3018
    tscDebug("client msg type %s should retry since %s", TMSG_INFO(msgType), tstrerror(code));
×
3019
    return true;
×
3020
  } else {
3021
    return false;
×
3022
  }
3023
}
3024

3025
int32_t taos_register_instance(const char *id, const char *type, const char *desc, int32_t expire) {
×
3026
  if (id == NULL || id[0] == 0) {
×
3027
    return terrno = TSDB_CODE_INVALID_PARA;
×
3028
  }
3029

3030
  // Validate string lengths
3031
  size_t idLen = strlen(id);
×
3032
  if (idLen >= TSDB_INSTANCE_ID_LEN) {
×
3033
    tscError("instance id length %zu exceeds limit %d", idLen, TSDB_INSTANCE_ID_LEN - 1);
×
3034
    return terrno = TSDB_CODE_INVALID_PARA;
×
3035
  }
3036

3037
  if (type != NULL && type[0] != 0) {
×
3038
    size_t typeLen = strlen(type);
×
3039
    if (typeLen >= TSDB_INSTANCE_TYPE_LEN) {
×
3040
      tscError("instance type length %zu exceeds limit %d", typeLen, TSDB_INSTANCE_TYPE_LEN - 1);
×
3041
      return terrno = TSDB_CODE_INVALID_PARA;
×
3042
    }
3043
  }
3044

3045
  if (desc != NULL && desc[0] != 0) {
×
3046
    size_t descLen = strlen(desc);
×
3047
    if (descLen >= TSDB_INSTANCE_DESC_LEN) {
×
3048
      tscError("instance desc length %zu exceeds limit %d", descLen, TSDB_INSTANCE_DESC_LEN - 1);
×
3049
      return terrno = TSDB_CODE_INVALID_PARA;
×
3050
    }
3051
  }
3052

3053
  int32_t code = taos_init();
×
3054
  if (code != TSDB_CODE_SUCCESS) {
×
3055
    return code;
×
3056
  }
3057

3058
  SConfig *pCfg = taosGetCfg();
×
3059
  if (pCfg == NULL) {
×
3060
    return terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3061
  }
3062

3063
  SConfigItem *pFirstEpItem = cfgGetItem(pCfg, "firstEp");
×
3064
  if (pFirstEpItem == NULL || pFirstEpItem->str == NULL || pFirstEpItem->str[0] == 0) {
×
3065
    return terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3066
  }
3067

3068
  SEp firstEp = {0};
×
3069
  code = taosGetFqdnPortFromEp(pFirstEpItem->str, &firstEp);
×
3070
  if (code != TSDB_CODE_SUCCESS) {
×
3071
    return terrno = code;
×
3072
  }
3073

3074
  void    *clientRpc = NULL;
×
3075
  SEpSet   epSet = {.inUse = 0, .numOfEps = 1};
×
3076
  SRpcMsg  rpcMsg = {0};
×
3077
  SRpcMsg  rpcRsp = {0};
×
3078
  SRpcInit rpcInit = {0};
×
3079

3080
  rpcInit.label = "INST";
×
3081
  rpcInit.numOfThreads = 1;
×
3082
  rpcInit.cfp = NULL;
×
3083
  rpcInit.sessions = 16;
×
3084
  rpcInit.connType = TAOS_CONN_CLIENT;
×
3085
  rpcInit.idleTime = tsShellActivityTimer * 1000;
×
3086
  rpcInit.compressSize = tsCompressMsgSize;
×
3087
  rpcInit.user = TSDB_DEFAULT_USER;
×
3088

3089
  rpcInit.rfp = instanceRegisterRpcRfp;
×
3090
  rpcInit.retryMinInterval = tsRedirectPeriod;
×
3091
  rpcInit.retryStepFactor = tsRedirectFactor;
×
3092
  rpcInit.retryMaxInterval = tsRedirectMaxPeriod;
×
3093
  rpcInit.retryMaxTimeout =
×
3094
      tsMaxRetryWaitTime;  // Use a special user for instance registration (can be configured for whitelist)
3095

3096
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
×
3097
  connLimitNum = TMAX(connLimitNum, 10);
×
3098
  connLimitNum = TMIN(connLimitNum, 500);
×
3099
  rpcInit.connLimitNum = connLimitNum;
×
3100
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
×
3101
  rpcInit.readTimeout = tsReadTimeout;
×
3102
  rpcInit.ipv6 = tsEnableIpv6;
×
3103
  rpcInit.enableSSL = tsEnableTLS;
×
3104

3105
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
×
3106
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
×
3107
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
×
3108
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
×
3109
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
×
3110

3111
  code = taosVersionStrToInt(td_version, &rpcInit.compatibilityVer);
×
3112
  if (code != TSDB_CODE_SUCCESS) {
×
3113
    tscError("failed to convert taos version from str to int, errcode:%s", terrstr(code));
×
3114
    return code;
×
3115
  }
3116

3117
  clientRpc = rpcOpen(&rpcInit);
×
3118
  if (clientRpc == NULL) {
×
3119
    code = terrno;
×
3120
    tscError("failed to init instance register client since %s", tstrerror(code));
×
3121
    return code;
×
3122
  }
3123

3124
  // Prepare epSet
3125
  tstrncpy(epSet.eps[0].fqdn, firstEp.fqdn, TSDB_FQDN_LEN);
×
3126
  epSet.eps[0].port = firstEp.port;
×
3127

3128
  // Prepare request
3129
  SInstanceRegisterReq req = {0};
×
3130
  tstrncpy(req.id, id, sizeof(req.id));
×
3131
  if (type != NULL && type[0] != 0) {
×
3132
    tstrncpy(req.type, type, sizeof(req.type));
×
3133
  }
3134
  if (desc != NULL && desc[0] != 0) {
×
3135
    tstrncpy(req.desc, desc, sizeof(req.desc));
×
3136
  }
3137
  req.expire = expire;
×
3138

3139
  int32_t contLen = tSerializeSInstanceRegisterReq(NULL, 0, &req);
×
3140
  if (contLen <= 0) {
×
3141
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3142
    rpcClose(clientRpc);
×
3143
    return code;
×
3144
  }
3145

3146
  void *pCont = rpcMallocCont(contLen);
×
3147
  if (pCont == NULL) {
×
3148
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3149
    rpcClose(clientRpc);
×
3150
    return code;
×
3151
  }
3152

3153
  if (tSerializeSInstanceRegisterReq(pCont, contLen, &req) < 0) {
×
3154
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3155
    rpcFreeCont(pCont);
×
3156
    rpcClose(clientRpc);
×
3157
    return code;
×
3158
  }
3159

3160
  rpcMsg.pCont = pCont;
×
3161
  rpcMsg.contLen = contLen;
×
3162
  rpcMsg.msgType = TDMT_MND_REGISTER_INSTANCE;
×
3163
  rpcMsg.info.ahandle = (void *)0x9528;  // Different magic number from server status
×
3164
  rpcMsg.info.notFreeAhandle = 1;
×
3165

3166
  code = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
×
3167
  if (TSDB_CODE_SUCCESS != code) {
×
3168
    tscError("failed to send instance register req since %s", tstrerror(code));
×
3169
    // rpcSendRecv failed, pCont may not be freed, but check _RETURN1 path
3170
    // In error path, rpcSendRecv may free pCont, but we free it here to be safe
3171
    rpcClose(clientRpc);
×
3172
    return code;
×
3173
  }
3174

3175
  if (rpcRsp.code != 0) {
×
3176
    code = rpcRsp.code;
×
3177
    tscError("instance register failed, code:%s", tstrerror(code));
×
3178
  } else {
3179
    code = TSDB_CODE_SUCCESS;
×
3180
  }
3181

3182
  if (rpcRsp.pCont != NULL) {
×
3183
    rpcFreeCont(rpcRsp.pCont);
×
3184
  }
3185
  rpcClose(clientRpc);
×
3186

3187
  terrno = code;
×
3188
  return code;
×
3189
}
3190

3191
int32_t taos_list_instances(const char *filter_type, char ***pList, int32_t *pCount) {
×
3192
  if (pList == NULL || pCount == NULL) {
×
3193
    return TSDB_CODE_INVALID_PARA;
×
3194
  }
3195

3196
  int32_t code = taos_init();
×
3197
  if (code != TSDB_CODE_SUCCESS) {
×
3198
    terrno = code;
×
3199
    return code;
×
3200
  }
3201

3202
  SConfig *pCfg = taosGetCfg();
×
3203
  if (pCfg == NULL) {
×
3204
    terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3205
    return TSDB_CODE_CFG_NOT_FOUND;
×
3206
  }
3207

3208
  SConfigItem *pFirstEpItem = cfgGetItem(pCfg, "firstEp");
×
3209
  if (pFirstEpItem == NULL || pFirstEpItem->str == NULL || pFirstEpItem->str[0] == 0) {
×
3210
    terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3211
    return TSDB_CODE_CFG_NOT_FOUND;
×
3212
  }
3213

3214
  SEp firstEp = {0};
×
3215
  code = taosGetFqdnPortFromEp(pFirstEpItem->str, &firstEp);
×
3216
  if (code != TSDB_CODE_SUCCESS) {
×
3217
    terrno = code;
×
3218
    return code;
×
3219
  }
3220

3221
  // Initialize RPC connection (similar to taos_register_instance)
3222
  void    *clientRpc = NULL;
×
3223
  SEpSet   epSet = {.inUse = 0, .numOfEps = 1};
×
3224
  SRpcMsg  rpcMsg = {0};
×
3225
  SRpcMsg  rpcRsp = {0};
×
3226
  SRpcInit rpcInit = {0};
×
3227

3228
  rpcInit.label = "LIST";
×
3229
  rpcInit.numOfThreads = 1;
×
3230
  rpcInit.cfp = NULL;
×
3231
  rpcInit.sessions = 16;
×
3232
  rpcInit.connType = TAOS_CONN_CLIENT;
×
3233
  rpcInit.idleTime = tsShellActivityTimer * 1000;
×
3234
  rpcInit.compressSize = tsCompressMsgSize;
×
3235
  rpcInit.user = TSDB_DEFAULT_USER;
×
3236

3237
  rpcInit.rfp = instanceRegisterRpcRfp;
×
3238
  rpcInit.retryMinInterval = tsRedirectPeriod;
×
3239
  rpcInit.retryStepFactor = tsRedirectFactor;
×
3240
  rpcInit.retryMaxInterval = tsRedirectMaxPeriod;
×
3241
  rpcInit.retryMaxTimeout =
×
3242
      tsMaxRetryWaitTime;  // Use a special user for instance registration (can be configured for whitelist)
3243

3244
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
×
3245
  connLimitNum = TMAX(connLimitNum, 10);
×
3246
  connLimitNum = TMIN(connLimitNum, 500);
×
3247
  rpcInit.connLimitNum = connLimitNum;
×
3248
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
×
3249
  rpcInit.readTimeout = tsReadTimeout;
×
3250
  rpcInit.ipv6 = tsEnableIpv6;
×
3251
  rpcInit.enableSSL = tsEnableTLS;
×
3252

3253
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
×
3254
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
×
3255
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
×
3256
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
×
3257
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
×
3258

3259
  code = taosVersionStrToInt(td_version, &rpcInit.compatibilityVer);
×
3260
  if (code != TSDB_CODE_SUCCESS) {
×
3261
    tscError("failed to convert taos version from str to int, errcode:%s", terrstr(code));
×
3262
    return code;
×
3263
  }
3264

3265
  clientRpc = rpcOpen(&rpcInit);
×
3266
  if (clientRpc == NULL) {
×
3267
    code = terrno;
×
3268
    tscError("failed to init instance list client since %s", tstrerror(code));
×
3269
    terrno = code;
×
3270
    return code;
×
3271
  }
3272

3273
  tstrncpy(epSet.eps[0].fqdn, firstEp.fqdn, TSDB_FQDN_LEN);
×
3274
  epSet.eps[0].port = firstEp.port;
×
3275
  SInstanceListReq req = {0};
×
3276
  if (filter_type != NULL && filter_type[0] != 0) {
×
3277
    tstrncpy(req.filter_type, filter_type, sizeof(req.filter_type));
×
3278
  }
3279

3280
  // Serialize request to get required length
3281
  int32_t contLen = tSerializeSInstanceListReq(NULL, 0, &req);
×
3282
  if (contLen <= 0) {
×
3283
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3284
    rpcClose(clientRpc);
×
3285
    terrno = code;
×
3286
    return code;
×
3287
  }
3288

3289
  // Allocate RPC message buffer (includes message header overhead)
3290
  void *pCont = rpcMallocCont(contLen);
×
3291
  if (pCont == NULL) {
×
3292
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3293
    rpcClose(clientRpc);
×
3294
    terrno = code;
×
3295
    return code;
×
3296
  }
3297

3298
  // Serialize request into the content part (after message header)
3299
  if (tSerializeSInstanceListReq(pCont, contLen, &req) < 0) {
×
3300
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3301
    rpcFreeCont(pCont);
×
3302
    rpcClose(clientRpc);
×
3303
    terrno = code;
×
3304
    return code;
×
3305
  }
3306

3307
  rpcMsg.pCont = pCont;
×
3308
  rpcMsg.contLen = contLen;
×
3309
  rpcMsg.msgType = TDMT_MND_LIST_INSTANCES;
×
3310
  rpcMsg.info.ahandle = (void *)0x9529;  // Different magic number from register
×
3311
  rpcMsg.info.notFreeAhandle = 1;
×
3312

3313
  code = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
×
3314
  if (TSDB_CODE_SUCCESS != code) {
×
3315
    tscError("failed to send instance list req since %s", tstrerror(code));
×
3316
    rpcFreeCont(pCont);
×
3317
    rpcClose(clientRpc);
×
3318
    terrno = code;
×
3319
    return code;
×
3320
  }
3321

3322
  // Check response - rpcRsp.code contains the result code from mnode
3323
  if (rpcRsp.code != 0) {
×
3324
    code = rpcRsp.code;
×
3325
    tscError("instance list failed, code:%s", tstrerror(code));
×
3326
    if (rpcRsp.pCont != NULL) {
×
3327
      rpcFreeCont(rpcRsp.pCont);
×
3328
    }
3329
    rpcClose(clientRpc);
×
3330
    terrno = code;
×
3331
    return code;
×
3332
  }
3333

3334
  // Deserialize response
3335
  if (rpcRsp.pCont != NULL && rpcRsp.contLen > 0) {
×
3336
    SInstanceListRsp rsp = {0};
×
3337
    code = tDeserializeSInstanceListRsp(rpcRsp.pCont, rpcRsp.contLen, &rsp);
×
3338
    if (code != TSDB_CODE_SUCCESS) {
×
3339
      tscError("failed to deserialize instance list rsp, code:%s", tstrerror(code));
×
3340
      if (rsp.ids != NULL) {
×
3341
        for (int32_t i = 0; i < rsp.count; i++) {
×
3342
          if (rsp.ids[i] != NULL) {
×
3343
            taosMemoryFree(rsp.ids[i]);
×
3344
          }
3345
        }
3346
        taosMemoryFree(rsp.ids);
×
3347
        rsp.ids = NULL;
×
3348
      }
3349
      rsp.count = 0;
×
3350
      rpcFreeCont(rpcRsp.pCont);
×
3351
      rpcClose(clientRpc);
×
3352
      terrno = code;
×
3353
      return code;
×
3354
    }
3355
    *pList = rsp.ids;
×
3356
    *pCount = rsp.count;
×
3357
  } else {
3358
    *pList = NULL;
×
3359
    *pCount = 0;
×
3360
  }
3361

3362
  if (rpcRsp.pCont != NULL) {
×
3363
    rpcFreeCont(rpcRsp.pCont);
×
3364
  }
3365
  rpcClose(clientRpc);
×
3366

3367
  return TSDB_CODE_SUCCESS;
×
3368
}
3369

3370
void taos_free_instances(char ***pList, int32_t count) {
×
3371
  if (pList == NULL || *pList == NULL || count <= 0) {
×
3372
    return;
×
3373
  }
3374

3375
  // Free each string in the array
3376
  for (int32_t i = 0; i < count; i++) {
×
3377
    if ((*pList)[i] != NULL) {
×
3378
      taosMemoryFree((*pList)[i]);
×
3379
      (*pList)[i] = NULL;
×
3380
    }
3381
  }
3382

3383
  // Free the array itself
3384
  taosMemoryFree(*pList);
×
3385
  *pList = NULL;
×
3386
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc