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

taosdata / TDengine / #4936

23 Jan 2026 09:40AM UTC coverage: 66.746% (+0.04%) from 66.708%
#4936

push

travis-ci

web-flow
fix: case failuer caused by the modification of the error description (#34391)

204023 of 305671 relevant lines covered (66.75%)

124768167.97 hits per line

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

36.97
/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, ...) {
605,146✔
50
  if (arg == NULL) {
605,146✔
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) {
2,132,016✔
56
    if (i % 1000 == 0) {
1,526,870✔
57
      (void)sched_yield();
1,505✔
58
    }
59
  }
60

61
  int ret = taos_options_imp(option, (const char *)arg);
605,146✔
62
  atomic_store_32(&lock, 0);
605,146✔
63
  return ret;
605,146✔
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,237,889✔
73
  pTimezoneMap = taosHashInit(0, MurmurHash3_32, false, HASH_ENTRY_LOCK);
1,237,889✔
74
  if (pTimezoneMap == NULL) {
1,237,889✔
75
    return terrno;
×
76
  }
77
  taosHashSetFreeFp(pTimezoneMap, freeTz);
1,237,889✔
78

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

86
void tzCleanup() {
1,237,927✔
87
  taosHashCleanup(pTimezoneMap);
1,237,927✔
88
  taosHashCleanup(pTimezoneNameMap);
1,237,927✔
89
}
1,237,927✔
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,238,561✔
249
  tscInfo("start to cleanup client environment");
1,238,561✔
250
  if (atomic_val_compare_exchange_32(&sentinel, TSC_VAR_NOT_RELEASE, TSC_VAR_RELEASED) != TSC_VAR_NOT_RELEASE) {
1,238,561✔
251
    return;
621✔
252
  }
253

254
  monitorClose();
1,237,940✔
255
  tscStopCrashReport();
1,237,940✔
256

257
  hbMgrCleanUp();
1,237,940✔
258

259
  catalogDestroy();
1,237,927✔
260
  schedulerDestroy();
1,237,927✔
261

262
  fmFuncMgtDestroy();
1,237,927✔
263
  qCleanupKeywordsTable();
1,237,927✔
264

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

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

274
  id = clientConnRefPool;
1,237,927✔
275
  clientConnRefPool = -1;
1,237,927✔
276
  taosCloseRef(id);
1,237,927✔
277

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

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

287
  sessMgtDestroy();
1,237,927✔
288

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

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

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

314
TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) {
2,287,326✔
315
  tscInfo("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
2,287,326✔
316
  if (user == NULL) {
2,287,501✔
317
    user = TSDB_DEFAULT_USER;
157,699✔
318
  }
319

320
  if (pass == NULL) {
2,287,501✔
321
    pass = TSDB_DEFAULT_PASS;
157,698✔
322
  }
323

324
  STscObj *pObj = NULL;
2,287,501✔
325
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
2,287,451✔
326
  if (TSDB_CODE_SUCCESS == code) {
2,287,472✔
327
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
2,277,057✔
328
    if (NULL == rid) {
2,277,057✔
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,277,057✔
333
    return (TAOS *)rid;
2,277,007✔
334
  } else {
335
    terrno = code;
10,436✔
336
  }
337

338
  return NULL;
10,452✔
339
}
340

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

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

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

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

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

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

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

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

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

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

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) {
1,330✔
440
  if (taos == NULL) {
1,330✔
441
    terrno = TSDB_CODE_INVALID_PARA;
×
442
    return terrno;
×
443
  }
444

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

452
  switch (type) {
1,330✔
453
    case TAOS_NOTIFY_PASSVER: {
380✔
454
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
380✔
455
      pObj->passInfo.fp = fp;
380✔
456
      pObj->passInfo.param = param;
380✔
457
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
380✔
458
      break;
380✔
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: {
950✔
468
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
950✔
469
      pObj->userDroppedInfo.fp = fp;
950✔
470
      pObj->userDroppedInfo.param = param;
950✔
471
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
950✔
472
      break;
950✔
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
    case TAOS_NOTIFY_TOKEN: {
×
482
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
483
      pObj->tokenNotifyInfo.fp = fp;
×
484
      pObj->tokenNotifyInfo.param = param;
×
485
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
486
      break;
×
487
    }
488
    default: {
×
489
      terrno = TSDB_CODE_INVALID_PARA;
×
490
      releaseTscObj(*(int64_t *)taos);
×
491
      return terrno;
×
492
    }
493
  }
494

495
  releaseTscObj(*(int64_t *)taos);
1,330✔
496
  return 0;
1,330✔
497
}
498

499
typedef struct SFetchWhiteListInfo {
500
  int64_t                     connId;
501
  __taos_async_whitelist_fn_t userCbFn;
502
  void                       *userParam;
503
} SFetchWhiteListInfo;
504

505
int32_t fetchWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
506
  SFetchWhiteListInfo *pInfo = (SFetchWhiteListInfo *)param;
×
507
  TAOS                *taos = &pInfo->connId;
×
508
  if (code != TSDB_CODE_SUCCESS) {
×
509
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
510
    taosMemoryFree(pMsg->pData);
×
511
    taosMemoryFree(pMsg->pEpSet);
×
512
    taosMemoryFree(pInfo);
×
513
    return code;
×
514
  }
515

516
  SGetUserIpWhiteListRsp wlRsp;
×
517
  if (TSDB_CODE_SUCCESS != tDeserializeSGetUserIpWhiteListRsp(pMsg->pData, pMsg->len, &wlRsp)) {
×
518
    taosMemoryFree(pMsg->pData);
×
519
    taosMemoryFree(pMsg->pEpSet);
×
520
    taosMemoryFree(pInfo);
×
521
    tFreeSGetUserIpWhiteListRsp(&wlRsp);
×
522
    return terrno;
×
523
  }
524

525
  uint64_t *pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(uint64_t));
×
526
  if (pWhiteLists == NULL) {
×
527
    taosMemoryFree(pMsg->pData);
×
528
    taosMemoryFree(pMsg->pEpSet);
×
529
    taosMemoryFree(pInfo);
×
530
    tFreeSGetUserIpWhiteListRsp(&wlRsp);
×
531
    return terrno;
×
532
  }
533

534
  for (int i = 0; i < wlRsp.numWhiteLists; ++i) {
×
535
    pWhiteLists[i] = ((uint64_t)wlRsp.pWhiteLists[i].mask << 32) | wlRsp.pWhiteLists[i].ip;
×
536
  }
537

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

540
  taosMemoryFree(pWhiteLists);
×
541
  taosMemoryFree(pMsg->pData);
×
542
  taosMemoryFree(pMsg->pEpSet);
×
543
  taosMemoryFree(pInfo);
×
544
  tFreeSGetUserIpWhiteListRsp(&wlRsp);
×
545
  return code;
×
546
}
547

548
void taos_fetch_whitelist_a(TAOS *taos, __taos_async_whitelist_fn_t fp, void *param) {
×
549
  if (NULL == taos) {
×
550
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
551
    return;
×
552
  }
553

554
  int64_t connId = *(int64_t *)taos;
×
555

556
  STscObj *pTsc = acquireTscObj(connId);
×
557
  if (NULL == pTsc) {
×
558
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
559
    return;
×
560
  }
561

562
  SGetUserWhiteListReq req;
×
563
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
564
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
565
  if (msgLen < 0) {
×
566
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
567
    releaseTscObj(connId);
×
568
    return;
×
569
  }
570

571
  void *pReq = taosMemoryMalloc(msgLen);
×
572
  if (pReq == NULL) {
×
573
    fp(param, terrno, taos, 0, NULL);
×
574
    releaseTscObj(connId);
×
575
    return;
×
576
  }
577

578
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
579
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
580
    taosMemoryFree(pReq);
×
581
    releaseTscObj(connId);
×
582
    return;
×
583
  }
584

585
  SFetchWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchWhiteListInfo));
×
586
  if (pParam == NULL) {
×
587
    fp(param, terrno, taos, 0, NULL);
×
588
    taosMemoryFree(pReq);
×
589
    releaseTscObj(connId);
×
590
    return;
×
591
  }
592

593
  pParam->connId = connId;
×
594
  pParam->userCbFn = fp;
×
595

596
  pParam->userParam = param;
×
597
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
598
  if (pSendInfo == NULL) {
×
599
    fp(param, terrno, taos, 0, NULL);
×
600
    taosMemoryFree(pParam);
×
601
    taosMemoryFree(pReq);
×
602
    releaseTscObj(connId);
×
603
    return;
×
604
  }
605

606
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
607
  pSendInfo->requestId = generateRequestId();
×
608
  pSendInfo->requestObjRefId = 0;
×
609
  pSendInfo->param = pParam;
×
610
  pSendInfo->fp = fetchWhiteListCallbackFn;
×
611
  pSendInfo->msgType = TDMT_MND_GET_USER_IP_WHITELIST;
×
612

613
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
614
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
615
    tscWarn("failed to async send msg to server");
×
616
  }
617
  releaseTscObj(connId);
×
618
  return;
×
619
}
620

621

622

623
typedef struct SFetchIpWhiteListInfo {
624
  int64_t connId;
625
  bool supportNeg;
626
  void   *userParam;
627

628
  __taos_async_ip_whitelist_fn_t userCbFn;
629
} SFetchIpWhiteListInfo;
630

631

632

633
int32_t fetchIpWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
634
  int32_t lino = 0;
×
635
  char  **pWhiteLists = NULL;
×
636

637
  SGetUserIpWhiteListRsp wlRsp = {0};
×
638

639
  SFetchIpWhiteListInfo *pInfo = (SFetchIpWhiteListInfo *)param;
×
640
  TAOS *taos = &pInfo->connId;
×
641

642
  if (code != TSDB_CODE_SUCCESS) {
×
643
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
644
    TAOS_CHECK_GOTO(code, &lino, _error);
×
645
  }
646

647
  if ((code = tDeserializeSGetUserIpWhiteListDualRsp(pMsg->pData, pMsg->len, &wlRsp)) != TSDB_CODE_SUCCESS) {
×
648
    TAOS_CHECK_GOTO(code, &lino, _error);
×
649
  }
650

651
  pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(char *));
×
652
  if (pWhiteLists == NULL) {
×
653
    code = terrno;
×
654
    TAOS_CHECK_GOTO(code, &lino, _error);
×
655
  }
656

657
  int32_t numWhiteLists =0;
×
658
  for (int32_t i = 0; i < wlRsp.numWhiteLists; i++) {
×
659
    SIpRange *pIpRange = &wlRsp.pWhiteListsDual[i];
×
660
    if (!pInfo->supportNeg && pIpRange->neg) {
×
661
      continue;
×
662
    }
663
    SIpAddr   ipAddr = {0};
×
664

665
    code = tIpUintToStr(pIpRange, &ipAddr);
×
666
    TAOS_CHECK_GOTO(code, &lino, _error);
×
667

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

692
  pInfo->userCbFn(pInfo->userParam, code, taos, numWhiteLists, pWhiteLists);
×
693
_error:
×
694
  if (pWhiteLists != NULL) {
×
695
    for (int32_t i = 0; i < numWhiteLists; i++) {
×
696
      taosMemFree(pWhiteLists[i]);
×
697
    }
698
    taosMemoryFree(pWhiteLists);
×
699
  }
700
  taosMemoryFree(pMsg->pData);
×
701
  taosMemoryFree(pMsg->pEpSet);
×
702
  taosMemoryFree(pInfo);
×
703
  tFreeSGetUserIpWhiteListDualRsp(&wlRsp);
×
704
  return code;
×
705
}
706

707

708

709
static void taosFetchIpWhiteList(TAOS *taos, __taos_async_whitelist_dual_stack_fn_t fp, void *param, bool supportNeg) {
×
710
  if (NULL == taos) {
×
711
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
712
    return;
×
713
  }
714
  int64_t connId = *(int64_t *)taos;
×
715

716
  STscObj *pTsc = acquireTscObj(connId);
×
717
  if (NULL == pTsc) {
×
718
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
719
    return;
×
720
  }
721

722
  SGetUserWhiteListReq req;
×
723
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
724
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
725
  if (msgLen < 0) {
×
726
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
727
    releaseTscObj(connId);
×
728
    return;
×
729
  }
730

731
  void *pReq = taosMemoryMalloc(msgLen);
×
732
  if (pReq == NULL) {
×
733
    fp(param, terrno, taos, 0, NULL);
×
734
    releaseTscObj(connId);
×
735
    return;
×
736
  }
737

738
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
739
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
740
    taosMemoryFree(pReq);
×
741
    releaseTscObj(connId);
×
742
    return;
×
743
  }
744

745
  SFetchIpWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchIpWhiteListInfo));
×
746
  if (pParam == NULL) {
×
747
    fp(param, terrno, taos, 0, NULL);
×
748
    taosMemoryFree(pReq);
×
749
    releaseTscObj(connId);
×
750
    return;
×
751
  }
752

753
  pParam->connId = connId;
×
754
  pParam->supportNeg = supportNeg;
×
755
  pParam->userCbFn = fp;
×
756
  pParam->userParam = param;
×
757

758
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
759
  if (pSendInfo == NULL) {
×
760
    fp(param, terrno, taos, 0, NULL);
×
761
    taosMemoryFree(pParam);
×
762
    taosMemoryFree(pReq);
×
763
    releaseTscObj(connId);
×
764
    return;
×
765
  }
766

767
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
768
  pSendInfo->requestId = generateRequestId();
×
769
  pSendInfo->requestObjRefId = 0;
×
770
  pSendInfo->param = pParam;
×
771
  pSendInfo->fp = fetchIpWhiteListCallbackFn;
×
772
  pSendInfo->msgType = TDMT_MND_GET_USER_IP_WHITELIST_DUAL;
×
773

774
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
775
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
776
    tscWarn("failed to async send msg to server");
×
777
  }
778
  releaseTscObj(connId);
×
779
  return;
×
780
}
781

782

783

784
void taos_fetch_whitelist_dual_stack_a(TAOS *taos, __taos_async_whitelist_dual_stack_fn_t fp, void *param) {
×
785
  taosFetchIpWhiteList(taos, fp, param, false);
×
786
}
×
787

788

789

790
void taos_fetch_ip_whitelist_a(TAOS *taos, __taos_async_ip_whitelist_fn_t fp, void *param) {
×
791
  taosFetchIpWhiteList(taos, fp, param, true);
×
792
}
×
793

794

795
typedef struct SFetchDateTimeWhiteListInfo {
796
  int64_t                              connId;
797
  void                                *userParam;
798
  __taos_async_datetime_whitelist_fn_t userCbFn;
799
} SFetchDateTimeWhiteListInfo;
800

801

802

803
static const char* weekdays[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
804
int32_t fetchDateTimeWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
805
  int32_t lino = 0;
×
806
  char  **pWhiteLists = NULL;
×
807

808
  SUserDateTimeWhiteList wlRsp = {0};
×
809

810
  SFetchDateTimeWhiteListInfo *pInfo = (SFetchDateTimeWhiteListInfo *)param;
×
811
  TAOS *taos = &pInfo->connId;
×
812

813
  if (code != TSDB_CODE_SUCCESS) {
×
814
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
815
    TAOS_CHECK_GOTO(code, &lino, _error);
×
816
  }
817

818
  if ((code = tDeserializeSUserDateTimeWhiteList(pMsg->pData, pMsg->len, &wlRsp)) != TSDB_CODE_SUCCESS) {
×
819
    TAOS_CHECK_GOTO(code, &lino, _error);
×
820
  }
821

822
  pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(char *));
×
823
  if (pWhiteLists == NULL) {
×
824
    code = terrno;
×
825
    TAOS_CHECK_GOTO(code, &lino, _error);
×
826
  }
827

828
  int32_t numWhiteLists =0;
×
829
  for (int32_t i = 0; i < wlRsp.numWhiteLists; i++) {
×
830
    SDateTimeWhiteListItem *item = &wlRsp.pWhiteLists[i];
×
831

832
    char *p = taosMemCalloc(1, 128);
×
833
    if (p == NULL) {
×
834
      code = terrno;
×
835
      TAOS_CHECK_GOTO(code, &lino, _error);
×
836
    }
837

838
    int duration = item->duration / 60;
×
839

840
    if (item->absolute) {
×
841
      struct STm tm;
×
842
      (void)taosTs2Tm(item->start, TSDB_TIME_PRECISION_SECONDS, &tm, NULL);
×
843
      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);
×
844
    } else {
845
      int day = item->start / 86400;
×
846
      int hour = (item->start % 86400) / 3600;
×
847
      int minute = (item->start % 3600) / 60;
×
848
      snprintf(p, 128, "%c %s %02d:%02d %d", item->neg ? '-' : '+', weekdays[day], hour, minute, duration);
×
849
    }
850
    pWhiteLists[numWhiteLists++] = p;
×
851
  }
852

853
  pInfo->userCbFn(pInfo->userParam, code, taos, numWhiteLists, pWhiteLists);
×
854
_error:
×
855
  if (pWhiteLists != NULL) {
×
856
    for (int32_t i = 0; i < numWhiteLists; i++) {
×
857
      taosMemFree(pWhiteLists[i]);
×
858
    }
859
    taosMemoryFree(pWhiteLists);
×
860
  }
861
  taosMemoryFree(pMsg->pData);
×
862
  taosMemoryFree(pMsg->pEpSet);
×
863
  taosMemoryFree(pInfo);
×
864
  tFreeSUserDateTimeWhiteList(&wlRsp);
×
865
  return code;
×
866
}
867

868

869

870
void taos_fetch_datetime_whitelist_a(TAOS *taos, __taos_async_datetime_whitelist_fn_t fp, void *param) {
×
871
  if (NULL == taos) {
×
872
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
873
    return;
×
874
  }
875
  int64_t connId = *(int64_t *)taos;
×
876

877
  STscObj *pTsc = acquireTscObj(connId);
×
878
  if (NULL == pTsc) {
×
879
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
880
    return;
×
881
  }
882

883
  SGetUserWhiteListReq req;
×
884
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
885
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
886
  if (msgLen < 0) {
×
887
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
888
    releaseTscObj(connId);
×
889
    return;
×
890
  }
891

892
  void *pReq = taosMemoryMalloc(msgLen);
×
893
  if (pReq == NULL) {
×
894
    fp(param, terrno, taos, 0, NULL);
×
895
    releaseTscObj(connId);
×
896
    return;
×
897
  }
898

899
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
900
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
901
    taosMemoryFree(pReq);
×
902
    releaseTscObj(connId);
×
903
    return;
×
904
  }
905

906
  SFetchDateTimeWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchDateTimeWhiteListInfo));
×
907
  if (pParam == NULL) {
×
908
    fp(param, terrno, taos, 0, NULL);
×
909
    taosMemoryFree(pReq);
×
910
    releaseTscObj(connId);
×
911
    return;
×
912
  }
913

914
  pParam->connId = connId;
×
915
  pParam->userCbFn = fp;
×
916
  pParam->userParam = param;
×
917

918
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
919
  if (pSendInfo == NULL) {
×
920
    fp(param, terrno, taos, 0, NULL);
×
921
    taosMemoryFree(pParam);
×
922
    taosMemoryFree(pReq);
×
923
    releaseTscObj(connId);
×
924
    return;
×
925
  }
926

927
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
928
  pSendInfo->requestId = generateRequestId();
×
929
  pSendInfo->requestObjRefId = 0;
×
930
  pSendInfo->param = pParam;
×
931
  pSendInfo->fp = fetchDateTimeWhiteListCallbackFn;
×
932
  pSendInfo->msgType = TDMT_MND_GET_USER_DATETIME_WHITELIST;
×
933

934
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
935
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
936
    tscWarn("failed to async send msg to server");
×
937
  }
938
  releaseTscObj(connId);
×
939
  return;
×
940
}
941

942

943

944
void taos_close_internal(void *taos) {
2,408,826✔
945
  if (taos == NULL) {
2,408,826✔
946
    return;
315✔
947
  }
948
  int32_t code = 0;
2,408,511✔
949

950
  STscObj *pTscObj = (STscObj *)taos;
2,408,511✔
951
  tscDebug("conn:0x%" PRIx64 ", try to close connection, numOfReq:%d", pTscObj->id, pTscObj->numOfReqs);
2,408,511✔
952

953
  SSessParam para = {.type = SESSION_PER_USER, .value = -1, .noCheck = 1};
2,408,511✔
954

955
  code = tscUpdateSessMetric(pTscObj, &para);
2,408,469✔
956
  if (code != TSDB_CODE_SUCCESS) {
2,408,469✔
957
    tscWarn("conn:0x%" PRIx64 ", failed to update user:%s metric when close connection, code:%d", pTscObj->id,
×
958
            pTscObj->user, code);
959
  }
960

961
  code = tscUnrefSessMetric(pTscObj);
2,408,469✔
962
  if (TSDB_CODE_SUCCESS != taosRemoveRef(clientConnRefPool, pTscObj->id)) {
2,408,511✔
963
    tscError("conn:0x%" PRIx64 ", failed to remove ref from conn pool", pTscObj->id);
×
964
  }
965
}
966

967
void taos_close(TAOS *taos) {
2,279,486✔
968
  if (taos == NULL) {
2,279,486✔
969
    return;
257✔
970
  }
971

972
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2,279,229✔
973
  if (NULL == pObj) {
2,279,229✔
974
    taosMemoryFree(taos);
×
975
    return;
×
976
  }
977

978
  taos_close_internal(pObj);
2,279,229✔
979
  releaseTscObj(*(int64_t *)taos);
2,279,229✔
980
  taosMemoryFree(taos);
2,279,229✔
981
}
982

983
int taos_errno(TAOS_RES *res) {
1,079,240,376✔
984
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,079,240,376✔
985
    return terrno;
29,974✔
986
  }
987

988
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,079,211,774✔
989
    return 0;
256,140✔
990
  }
991

992
  return ((SRequestObj *)res)->code;
1,078,953,412✔
993
}
994

995
const char *taos_errstr(TAOS_RES *res) {
63,502,330✔
996
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
63,502,330✔
997
    if (*(taosGetErrMsg()) == 0) {
37,557✔
998
      return (const char *)tstrerror(terrno);
37,506✔
999
    } else {
1000
      (void)snprintf(taosGetErrMsgReturn(), ERR_MSG_LEN, "%s", taosGetErrMsg());
51✔
1001
      return (const char*)taosGetErrMsgReturn();
51✔
1002
    }
1003
  }
1004

1005
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
63,464,773✔
1006
    return "success";
×
1007
  }
1008

1009
  SRequestObj *pRequest = (SRequestObj *)res;
63,464,773✔
1010
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
63,464,773✔
1011
    return pRequest->msgBuf;
22,048,004✔
1012
  } else {
1013
    return (const char *)tstrerror(pRequest->code);
41,416,769✔
1014
  }
1015
}
1016

1017
void taos_free_result(TAOS_RES *res) {
833,379,799✔
1018
  if (NULL == res) {
833,379,799✔
1019
    return;
13,238,058✔
1020
  }
1021

1022
  tscTrace("res:%p, will be freed", res);
820,141,741✔
1023

1024
  if (TD_RES_QUERY(res)) {
820,141,755✔
1025
    SRequestObj *pRequest = (SRequestObj *)res;
807,566,536✔
1026
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
807,566,536✔
1027
    destroyRequest(pRequest);
807,566,699✔
1028
    return;
807,561,027✔
1029
  }
1030

1031
  SMqRspObj *pRsp = (SMqRspObj *)res;
12,575,016✔
1032
  if (TD_RES_TMQ(res)) {
12,575,016✔
1033
    tDeleteMqDataRsp(&pRsp->dataRsp);
12,515,151✔
1034
    doFreeReqResultInfo(&pRsp->resInfo);
12,514,808✔
1035
  } else if (TD_RES_TMQ_METADATA(res)) {
62,934✔
1036
    tDeleteSTaosxRsp(&pRsp->dataRsp);
2,298✔
1037
    doFreeReqResultInfo(&pRsp->resInfo);
2,298✔
1038
  } else if (TD_RES_TMQ_META(res)) {
60,636✔
1039
    tDeleteMqMetaRsp(&pRsp->metaRsp);
54,671✔
1040
  } else if (TD_RES_TMQ_BATCH_META(res)) {
5,965✔
1041
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
5,965✔
1042
  } else if (TD_RES_TMQ_RAW(res)) {
×
1043
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
1044
  }
1045
  taosMemoryFree(pRsp);
12,577,399✔
1046
}
1047

1048
void taos_kill_query(TAOS *taos) {
514✔
1049
  if (NULL == taos) {
514✔
1050
    return;
257✔
1051
  }
1052

1053
  int64_t  rid = *(int64_t *)taos;
257✔
1054
  STscObj *pTscObj = acquireTscObj(rid);
257✔
1055
  if (pTscObj) {
257✔
1056
    stopAllRequests(pTscObj->pRequests);
257✔
1057
  }
1058
  releaseTscObj(rid);
257✔
1059
}
1060

1061
int taos_field_count(TAOS_RES *res) {
2,147,483,647✔
1062
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1063
    return 0;
×
1064
  }
1065

1066
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
1067
  return pResInfo->numOfCols;
2,147,483,647✔
1068
}
1069

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

1072
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
2,147,483,647✔
1073
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1074
    return NULL;
3,260,954✔
1075
  }
1076

1077
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
1078
  return pResInfo->userFields;
2,147,483,647✔
1079
}
1080

1081
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
800,435,708✔
1082
TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqid) {
280✔
1083
  return taosQueryImplWithReqid(taos, sql, false, reqid);
280✔
1084
}
1085

1086
TAOS_FIELD_E *taos_fetch_fields_e(TAOS_RES *res) {
165✔
1087
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
165✔
1088
    return NULL;
×
1089
  }
1090
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
165✔
1091
  return pResInfo->fields;
165✔
1092
}
1093

1094
TAOS_ROW taos_fetch_row(TAOS_RES *res) {
2,147,483,647✔
1095
  if (res == NULL) {
2,147,483,647✔
1096
    return NULL;
×
1097
  }
1098

1099
  if (TD_RES_QUERY(res)) {
2,147,483,647✔
1100
    SRequestObj *pRequest = (SRequestObj *)res;
1,672,760,514✔
1101
    if (pRequest->killed) {
1,672,760,514✔
1102
      tscInfo("query has been killed, can not fetch more row.");
×
1103
      pRequest->code = TSDB_CODE_TSC_QUERY_KILLED;
×
1104
      return NULL;
×
1105
    }
1106

1107
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
1,672,755,677✔
1108
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
1,672,762,769✔
1109
      return NULL;
339✔
1110
    }
1111

1112
    if (pRequest->inCallback) {
1,672,758,966✔
1113
      tscError("can not call taos_fetch_row before query callback ends.");
×
1114
      terrno = TSDB_CODE_TSC_INVALID_OPERATION;
×
1115
      return NULL;
×
1116
    }
1117

1118
    return doAsyncFetchRows(pRequest, true, true);
1,672,760,038✔
1119
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,147,483,647✔
1120
    SMqRspObj      *msg = ((SMqRspObj *)res);
2,147,483,647✔
1121
    SReqResultInfo *pResultInfo = NULL;
2,147,483,647✔
1122
    if (msg->resIter == -1) {
2,147,483,647✔
1123
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
12,224,889✔
1124
        return NULL;
×
1125
      }
1126
    } else {
1127
      pResultInfo = tmqGetCurResInfo(res);
2,147,483,647✔
1128
    }
1129

1130
    if (pResultInfo->current < pResultInfo->numOfRows) {
2,147,483,647✔
1131
      doSetOneRowPtr(pResultInfo);
2,147,483,647✔
1132
      pResultInfo->current += 1;
2,147,483,647✔
1133
      return pResultInfo->row;
2,147,483,647✔
1134
    } else {
1135
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
101,238,699✔
1136
        return NULL;
12,223,529✔
1137
      }
1138

1139
      doSetOneRowPtr(pResultInfo);
89,005,566✔
1140
      pResultInfo->current += 1;
89,014,173✔
1141
      return pResultInfo->row;
89,012,801✔
1142
    }
1143
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1144
    return NULL;
×
1145
  } else {
1146
    tscError("invalid result passed to taos_fetch_row");
×
1147
    terrno = TSDB_CODE_TMQ_INVALID_DATA;
×
1148
    return NULL;
×
1149
  }
1150
}
1151

1152
int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
2,147,483,647✔
1153
  return taos_print_row_with_size(str, INT32_MAX, row, fields, num_fields);
2,147,483,647✔
1154
}
1155
int taos_print_row_with_size(char *str, uint32_t size, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
2,147,483,647✔
1156
  int32_t len = 0;
2,147,483,647✔
1157
  for (int i = 0; i < num_fields; ++i) {
2,147,483,647✔
1158
    if (i > 0 && len < size - 1) {
2,147,483,647✔
1159
      str[len++] = ' ';
2,147,483,647✔
1160
    }
1161

1162
    if (row[i] == NULL) {
2,147,483,647✔
1163
      len += tsnprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
128,539,569✔
1164
      continue;
128,538,014✔
1165
    }
1166

1167
    switch (fields[i].type) {
2,147,483,647✔
1168
      case TSDB_DATA_TYPE_TINYINT:
12,629,138✔
1169
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
12,629,138✔
1170
        break;
12,627,975✔
1171

1172
      case TSDB_DATA_TYPE_UTINYINT:
56,800✔
1173
        len += tsnprintf(str + len, size - len, "%u", *((uint8_t *)row[i]));
56,800✔
1174
        break;
56,774✔
1175

1176
      case TSDB_DATA_TYPE_SMALLINT:
56,761✔
1177
        len += tsnprintf(str + len, size - len, "%d", *((int16_t *)row[i]));
56,761✔
1178
        break;
56,800✔
1179

1180
      case TSDB_DATA_TYPE_USMALLINT:
56,774✔
1181
        len += tsnprintf(str + len, size - len, "%u", *((uint16_t *)row[i]));
56,774✔
1182
        break;
56,787✔
1183

1184
      case TSDB_DATA_TYPE_INT:
2,147,483,647✔
1185
        len += tsnprintf(str + len, size - len, "%d", *((int32_t *)row[i]));
2,147,483,647✔
1186
        break;
2,147,483,647✔
1187

1188
      case TSDB_DATA_TYPE_UINT:
56,787✔
1189
        len += tsnprintf(str + len, size - len, "%u", *((uint32_t *)row[i]));
56,787✔
1190
        break;
56,787✔
1191

1192
      case TSDB_DATA_TYPE_BIGINT:
2,147,483,647✔
1193
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
2,147,483,647✔
1194
        break;
2,147,483,647✔
1195

1196
      case TSDB_DATA_TYPE_UBIGINT:
56,787✔
1197
        len += tsnprintf(str + len, size - len, "%" PRIu64, *((uint64_t *)row[i]));
56,787✔
1198
        break;
56,774✔
1199

1200
      case TSDB_DATA_TYPE_FLOAT: {
25,210,663✔
1201
        float fv = 0;
25,210,663✔
1202
        fv = GET_FLOAT_VAL(row[i]);
25,210,663✔
1203
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
25,212,931✔
1204
      } break;
25,234,897✔
1205

1206
      case TSDB_DATA_TYPE_DOUBLE: {
2,147,483,647✔
1207
        double dv = 0;
2,147,483,647✔
1208
        dv = GET_DOUBLE_VAL(row[i]);
2,147,483,647✔
1209
        len += snprintf(str + len, size - len, "%.*g", DBL_DIG, dv);
2,147,483,647✔
1210
      } break;
2,147,483,647✔
1211

1212
      case TSDB_DATA_TYPE_VARBINARY: {
56,787✔
1213
        void    *data = NULL;
56,787✔
1214
        uint32_t tmp = 0;
56,787✔
1215
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
56,787✔
1216
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
56,787✔
1217
          break;
×
1218
        }
1219
        uint32_t copyLen = TMIN(size - len - 1, tmp);
56,748✔
1220
        (void)memcpy(str + len, data, copyLen);
56,748✔
1221
        len += copyLen;
56,748✔
1222
        taosMemoryFree(data);
56,748✔
1223
      } break;
56,774✔
1224
      case TSDB_DATA_TYPE_BINARY:
2,147,483,647✔
1225
      case TSDB_DATA_TYPE_NCHAR:
1226
      case TSDB_DATA_TYPE_GEOMETRY: {
1227
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
2,147,483,647✔
1228
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
2,147,483,647✔
1229
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
1,778,501,728✔
1230
          if (charLen > fields[i].bytes || charLen < 0) {
2,147,483,647✔
1231
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
26,642✔
1232
            break;
×
1233
          }
1234
        } else {
1235
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
1,778,448,426✔
1236
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
247✔
1237
            break;
×
1238
          }
1239
        }
1240

1241
        uint32_t copyLen = TMIN(size - len - 1, charLen);
2,147,483,647✔
1242
        (void)memcpy(str + len, row[i], copyLen);
2,147,483,647✔
1243
        len += copyLen;
2,147,483,647✔
1244
      } break;
2,147,483,647✔
1245
      case TSDB_DATA_TYPE_BLOB:
×
1246
      case TSDB_DATA_TYPE_MEDIUMBLOB: {
1247
        void    *data = NULL;
×
1248
        uint32_t tmp = 0;
×
1249
        int32_t  charLen = blobDataLen((char *)row[i] - BLOBSTR_HEADER_SIZE);
×
1250
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
×
1251
          break;
×
1252
        }
1253

1254
        uint32_t copyLen = TMIN(size - len - 1, tmp);
×
1255
        (void)memcpy(str + len, data, copyLen);
×
1256
        len += copyLen;
×
1257

1258
        taosMemoryFree(data);
×
1259
      } break;
×
1260

1261
      case TSDB_DATA_TYPE_TIMESTAMP:
2,147,483,647✔
1262
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
2,147,483,647✔
1263
        break;
2,147,483,647✔
1264

1265
      case TSDB_DATA_TYPE_BOOL:
56,774✔
1266
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
56,774✔
1267
        break;
56,787✔
1268
      case TSDB_DATA_TYPE_DECIMAL64:
×
1269
      case TSDB_DATA_TYPE_DECIMAL: {
1270
        uint32_t decimalLen = strlen(row[i]);
×
1271
        uint32_t copyLen = TMIN(size - len - 1, decimalLen);
×
1272
        (void)memcpy(str + len, row[i], copyLen);
×
1273
        len += copyLen;
×
1274
      } break;
×
1275
      default:
×
1276
        break;
×
1277
    }
1278

1279
    if (len >= size - 1) {
2,147,483,647✔
1280
      break;
×
1281
    }
1282
  }
1283
  if (len < size) {
2,147,483,647✔
1284
    str[len] = 0;
2,147,483,647✔
1285
  }
1286

1287
  return len;
2,147,483,647✔
1288
}
1289

1290
int *taos_fetch_lengths(TAOS_RES *res) {
2,147,483,647✔
1291
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1292
    return NULL;
×
1293
  }
1294

1295
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
1296
  return pResInfo->length;
2,147,483,647✔
1297
}
1298

1299
TAOS_ROW *taos_result_block(TAOS_RES *res) {
×
1300
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1301
    terrno = TSDB_CODE_INVALID_PARA;
×
1302
    return NULL;
×
1303
  }
1304

1305
  if (taos_is_update_query(res)) {
×
1306
    return NULL;
×
1307
  }
1308

1309
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
1310
  return &pResInfo->row;
×
1311
}
1312

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

1364
const char *taos_get_client_info() { return td_version; }
837,625✔
1365

1366
// return int32_t
1367
int taos_affected_rows(TAOS_RES *res) {
586,058,065✔
1368
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
586,058,065✔
1369
      TD_RES_TMQ_BATCH_META(res)) {
586,058,557✔
1370
    return 0;
23✔
1371
  }
1372

1373
  SRequestObj    *pRequest = (SRequestObj *)res;
586,058,765✔
1374
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
586,058,765✔
1375
  return (int)pResInfo->numOfRows;
586,059,833✔
1376
}
1377

1378
// return int64_t
1379
int64_t taos_affected_rows64(TAOS_RES *res) {
1,274,406✔
1380
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
1,274,406✔
1381
      TD_RES_TMQ_BATCH_META(res)) {
1,274,406✔
1382
    return 0;
×
1383
  }
1384

1385
  SRequestObj    *pRequest = (SRequestObj *)res;
1,274,406✔
1386
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
1,274,406✔
1387
  return pResInfo->numOfRows;
1,274,406✔
1388
}
1389

1390
int taos_result_precision(TAOS_RES *res) {
2,147,483,647✔
1391
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1392
    return TSDB_TIME_PRECISION_MILLI;
×
1393
  }
1394

1395
  if (TD_RES_QUERY(res)) {
2,147,483,647✔
1396
    SRequestObj *pRequest = (SRequestObj *)res;
137,743,556✔
1397
    return pRequest->body.resInfo.precision;
137,743,556✔
1398
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,147,483,647✔
1399
    SReqResultInfo *info = tmqGetCurResInfo(res);
2,147,483,647✔
1400
    return info->precision;
2,147,483,647✔
1401
  }
1402
  return TSDB_TIME_PRECISION_MILLI;
×
1403
}
1404

1405
int taos_select_db(TAOS *taos, const char *db) {
41,358✔
1406
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
41,358✔
1407
  if (pObj == NULL) {
41,358✔
1408
    releaseTscObj(*(int64_t *)taos);
×
1409
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1410
    return TSDB_CODE_TSC_DISCONNECTED;
×
1411
  }
1412

1413
  if (db == NULL || strlen(db) == 0) {
41,358✔
1414
    releaseTscObj(*(int64_t *)taos);
×
1415
    tscError("invalid parameter for %s", db == NULL ? "db is NULL" : "db is empty");
×
1416
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1417
    return terrno;
×
1418
  }
1419

1420
  char sql[256] = {0};
41,358✔
1421
  (void)snprintf(sql, tListLen(sql), "use %s", db);
41,358✔
1422

1423
  TAOS_RES *pRequest = taos_query(taos, sql);
41,358✔
1424
  int32_t   code = taos_errno(pRequest);
41,358✔
1425

1426
  taos_free_result(pRequest);
41,344✔
1427
  releaseTscObj(*(int64_t *)taos);
41,330✔
1428
  return code;
41,358✔
1429
}
1430

1431
void taos_stop_query(TAOS_RES *res) {
810,481,874✔
1432
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
810,481,874✔
1433
      TD_RES_TMQ_BATCH_META(res)) {
810,489,786✔
1434
    return;
9✔
1435
  }
1436

1437
  stopAllQueries((SRequestObj *)res);
810,481,294✔
1438
}
1439

1440
bool taos_is_null(TAOS_RES *res, int32_t row, int32_t col) {
×
1441
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1442
    return true;
×
1443
  }
1444
  SReqResultInfo *pResultInfo = tscGetCurResInfo(res);
×
1445
  if (col >= pResultInfo->numOfCols || col < 0 || row >= pResultInfo->numOfRows || row < 0) {
×
1446
    return true;
×
1447
  }
1448

1449
  SResultColumn *pCol = &pResultInfo->pCol[col];
×
1450
  if (IS_VAR_DATA_TYPE(pResultInfo->fields[col].type)) {
×
1451
    return (pCol->offset[row] == -1);
×
1452
  } else {
1453
    return colDataIsNull_f(pCol, row);
×
1454
  }
1455
}
1456

1457
bool taos_is_update_query(TAOS_RES *res) { return taos_num_fields(res) == 0; }
504,360✔
1458

1459
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
211,078,623✔
1460
  int32_t numOfRows = 0;
211,078,623✔
1461
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
211,078,623✔
1462
  return numOfRows;
211,077,416✔
1463
}
1464

1465
int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) {
211,078,623✔
1466
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
211,078,623✔
1467
    return 0;
×
1468
  }
1469

1470
  if (TD_RES_QUERY(res)) {
211,078,623✔
1471
    SRequestObj *pRequest = (SRequestObj *)res;
202,633,664✔
1472

1473
    (*rows) = NULL;
202,633,664✔
1474
    (*numOfRows) = 0;
202,633,664✔
1475

1476
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
202,633,664✔
1477
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
202,320,618✔
1478
      return pRequest->code;
1,306,320✔
1479
    }
1480

1481
    (void)doAsyncFetchRows(pRequest, false, true);
201,327,344✔
1482

1483
    // TODO refactor
1484
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
201,326,370✔
1485
    pResultInfo->current = pResultInfo->numOfRows;
201,327,344✔
1486

1487
    (*rows) = pResultInfo->row;
201,327,344✔
1488
    (*numOfRows) = pResultInfo->numOfRows;
201,327,344✔
1489
    return pRequest->code;
201,326,857✔
1490
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
8,444,959✔
1491
    SReqResultInfo *pResultInfo = NULL;
8,444,959✔
1492
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
8,444,959✔
1493
    if (code != 0) return code;
8,444,239✔
1494

1495
    pResultInfo->current = pResultInfo->numOfRows;
8,196,628✔
1496
    (*rows) = pResultInfo->row;
8,196,988✔
1497
    (*numOfRows) = pResultInfo->numOfRows;
8,196,988✔
1498
    return 0;
8,196,988✔
1499
  } else {
1500
    tscError("taos_fetch_block_s invalid res type");
×
1501
    return TSDB_CODE_TMQ_INVALID_DATA;
×
1502
  }
1503
}
1504

1505
int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) {
88,263✔
1506
  *numOfRows = 0;
88,263✔
1507
  *pData = NULL;
88,263✔
1508

1509
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
88,263✔
1510
    return 0;
×
1511
  }
1512

1513
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
88,263✔
1514
    SReqResultInfo *pResultInfo = NULL;
84,390✔
1515
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
84,390✔
1516
    if (code != 0) {
84,390✔
1517
      (*numOfRows) = 0;
1,167✔
1518
      return 0;
1,167✔
1519
    }
1520

1521
    pResultInfo->current = pResultInfo->numOfRows;
83,223✔
1522
    (*numOfRows) = pResultInfo->numOfRows;
83,223✔
1523
    (*pData) = (void *)pResultInfo->pData;
83,223✔
1524
    return 0;
83,223✔
1525
  }
1526

1527
  SRequestObj *pRequest = (SRequestObj *)res;
3,873✔
1528

1529
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
3,873✔
1530
      pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
3,873✔
1531
    return pRequest->code;
×
1532
  }
1533

1534
  (void)doAsyncFetchRows(pRequest, false, false);
3,873✔
1535

1536
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
3,873✔
1537

1538
  pResultInfo->current = pResultInfo->numOfRows;
3,873✔
1539
  (*numOfRows) = pResultInfo->numOfRows;
3,873✔
1540
  (*pData) = (void *)pResultInfo->pData;
3,873✔
1541

1542
  return pRequest->code;
3,873✔
1543
}
1544

1545
int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) {
128,035,420✔
1546
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
128,035,420✔
1547
    return 0;
×
1548
  }
1549

1550
  int32_t numOfFields = taos_num_fields(res);
128,035,420✔
1551
  if (columnIndex < 0 || columnIndex >= numOfFields || numOfFields == 0) {
128,035,420✔
1552
    return 0;
×
1553
  }
1554

1555
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
128,035,420✔
1556
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
128,035,420✔
1557
  if (!IS_VAR_DATA_TYPE(pField->type)) {
128,035,420✔
1558
    return 0;
×
1559
  }
1560

1561
  return pResInfo->pCol[columnIndex].offset;
128,035,420✔
1562
}
1563

1564
int taos_is_null_by_column(TAOS_RES *res, int columnIndex, bool result[], int *rows) {
384,894,626✔
1565
  if (res == NULL || result == NULL || rows == NULL || *rows <= 0 || columnIndex < 0 || TD_RES_TMQ_META(res) ||
384,894,626✔
1566
      TD_RES_TMQ_RAW(res) || TD_RES_TMQ_BATCH_META(res)) {
384,894,626✔
1567
    return TSDB_CODE_INVALID_PARA;
×
1568
  }
1569

1570
  int32_t numOfFields = taos_num_fields(res);
384,894,626✔
1571
  if (columnIndex >= numOfFields || numOfFields == 0) {
384,894,626✔
1572
    return TSDB_CODE_INVALID_PARA;
×
1573
  }
1574

1575
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
384,894,626✔
1576
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
384,894,626✔
1577
  SResultColumn  *pCol = &pResInfo->pCol[columnIndex];
384,894,626✔
1578

1579
  if (*rows > pResInfo->numOfRows) {
384,894,626✔
1580
    *rows = pResInfo->numOfRows;
×
1581
  }
1582
  if (IS_VAR_DATA_TYPE(pField->type)) {
384,894,626✔
1583
    for (int i = 0; i < *rows; i++) {
×
1584
      if (pCol->offset[i] == -1) {
×
1585
        result[i] = true;
×
1586
      } else {
1587
        result[i] = false;
×
1588
      }
1589
    }
1590
  } else {
1591
    for (int i = 0; i < *rows; i++) {
2,147,483,647✔
1592
      if (colDataIsNull_f(pCol, i)) {
2,147,483,647✔
1593
        result[i] = true;
2,147,483,647✔
1594
      } else {
1595
        result[i] = false;
2,147,483,647✔
1596
      }
1597
    }
1598
  }
1599
  return 0;
384,894,626✔
1600
}
1601

1602
int taos_validate_sql(TAOS *taos, const char *sql) {
×
1603
  TAOS_RES *pObj = taosQueryImpl(taos, sql, true, TD_REQ_FROM_APP);
×
1604

1605
  int code = taos_errno(pObj);
×
1606

1607
  taos_free_result(pObj);
×
1608
  return code;
×
1609
}
1610

1611
void taos_reset_current_db(TAOS *taos) {
×
1612
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
1613
  if (pTscObj == NULL) {
×
1614
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1615
    return;
×
1616
  }
1617

1618
  resetConnectDB(pTscObj);
×
1619

1620
  releaseTscObj(*(int64_t *)taos);
×
1621
}
1622

1623
const char *taos_get_server_info(TAOS *taos) {
4,954✔
1624
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
4,954✔
1625
  if (pTscObj == NULL) {
4,954✔
1626
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1627
    return NULL;
×
1628
  }
1629

1630
  releaseTscObj(*(int64_t *)taos);
4,954✔
1631

1632
  return pTscObj->sDetailVer;
4,954✔
1633
}
1634

1635
int taos_get_current_db(TAOS *taos, char *database, int len, int *required) {
2,268✔
1636
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
2,268✔
1637
  if (pTscObj == NULL) {
2,268✔
1638
    return TSDB_CODE_TSC_DISCONNECTED;
×
1639
  }
1640

1641
  int code = TSDB_CODE_SUCCESS;
2,268✔
1642
  (void)taosThreadMutexLock(&pTscObj->mutex);
2,268✔
1643
  if (database == NULL || len <= 0) {
2,268✔
1644
    if (required != NULL) *required = strlen(pTscObj->db) + 1;
1,134✔
1645
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
1,134✔
1646
  } else if (len < strlen(pTscObj->db) + 1) {
1,134✔
1647
    tstrncpy(database, pTscObj->db, len);
567✔
1648
    if (required) *required = strlen(pTscObj->db) + 1;
567✔
1649
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
567✔
1650
  } else {
1651
    tstrncpy(database, pTscObj->db, len);
567✔
1652
    code = 0;
567✔
1653
  }
1654
_return:
2,268✔
1655
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
2,268✔
1656
  releaseTscObj(*(int64_t *)taos);
2,268✔
1657
  return code;
2,268✔
1658
}
1659

1660
// buffer is allocated by caller, len is in/out parameter, input is buffer length, output is actual length.
1661
// because this is a general purpose api, buffer is not null-terminated string even for string info, and
1662
// the return length is the actual length of the info, not including null-terminator.
1663
int taos_get_connection_info(TAOS *taos, TSDB_CONNECTION_INFO info, char* buffer, int* len) {
×
1664
  if (len == NULL) {
×
1665
    return TSDB_CODE_INVALID_PARA;
×
1666
  }
1667

1668
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
1669
  if (pTscObj == NULL) {
×
1670
    return TSDB_CODE_TSC_DISCONNECTED;
×
1671
  }
1672

1673
  int code = TSDB_CODE_SUCCESS;
×
1674
  (void)taosThreadMutexLock(&pTscObj->mutex);
×
1675

1676
  switch (info) {
×
1677
    case TSDB_CONNECTION_INFO_USER: {
×
1678
      int userLen = strlen(pTscObj->user);
×
1679
      if (buffer == NULL || *len < userLen) {
×
1680
        *len = userLen;
×
1681
        TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1682
      } else {
1683
        *len = userLen;
×
1684
        (void)memcpy(buffer, pTscObj->user, userLen);
×
1685
      }
1686
      break;
×
1687
    }
1688

1689
    case TSDB_CONNECTION_INFO_TOKEN: {
×
1690
      int tokenLen = strlen(pTscObj->tokenName);
×
1691
      if (tokenLen == 0) {
×
1692
        *len = 0;
×
1693
      } else if (buffer == NULL || *len < tokenLen) {
×
1694
        *len = tokenLen;
×
1695
        TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1696
      } else {
1697
        *len = tokenLen;
×
1698
        (void)memcpy(buffer, pTscObj->tokenName, tokenLen);
×
1699
      }
1700
      break;
×
1701
    }
1702

1703
    default:
×
1704
        TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1705
  }
1706

1707
_return:
×
1708
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
×
1709
  releaseTscObj(*(int64_t *)taos);
×
1710
  return code;
×
1711
}
1712

1713
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
1,615,307,978✔
1714
  if (NULL == pWrapper) {
1,615,307,978✔
1715
    return;
812,940,180✔
1716
  }
1717
  destoryCatalogReq(pWrapper->pCatalogReq);
802,367,798✔
1718
  taosMemoryFree(pWrapper->pCatalogReq);
802,357,935✔
1719
  qDestroyParseContext(pWrapper->pParseCtx);
802,352,801✔
1720
  taosMemoryFree(pWrapper);
802,341,665✔
1721
}
1722

1723
void destroyCtxInRequest(SRequestObj *pRequest) {
2,577,389✔
1724
  schedulerFreeJob(&pRequest->body.queryJob, 0);
2,577,389✔
1725
  qDestroyQuery(pRequest->pQuery);
2,577,389✔
1726
  pRequest->pQuery = NULL;
2,577,389✔
1727
  destorySqlCallbackWrapper(pRequest->pWrapper);
2,577,389✔
1728
  pRequest->pWrapper = NULL;
2,577,389✔
1729
}
2,577,389✔
1730

1731
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
251,891,000✔
1732
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
251,891,000✔
1733
  SRequestObj         *pRequest = pWrapper->pRequest;
251,891,000✔
1734
  SQuery              *pQuery = pRequest->pQuery;
251,891,018✔
1735

1736
  qDebug("req:0x%" PRIx64 ", start to semantic analysis, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
251,891,020✔
1737

1738
  int64_t analyseStart = taosGetTimestampUs();
251,890,584✔
1739
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
251,890,584✔
1740
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
251,890,411✔
1741

1742
  if (TSDB_CODE_SUCCESS == code) {
251,891,075✔
1743
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
251,889,725✔
1744
  }
1745

1746
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
251,878,041✔
1747

1748
  if (pRequest->parseOnly) {
251,882,649✔
1749
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
39,714✔
1750
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
39,714✔
1751
  }
1752

1753
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
251,882,523✔
1754
}
251,881,127✔
1755

1756
int32_t cloneCatalogReq(SCatalogReq **ppTarget, SCatalogReq *pSrc) {
×
1757
  int32_t      code = TSDB_CODE_SUCCESS;
×
1758
  SCatalogReq *pTarget = taosMemoryCalloc(1, sizeof(SCatalogReq));
×
1759
  if (pTarget == NULL) {
×
1760
    code = terrno;
×
1761
  } else {
1762
    pTarget->pDbVgroup = taosArrayDup(pSrc->pDbVgroup, NULL);
×
1763
    pTarget->pDbCfg = taosArrayDup(pSrc->pDbCfg, NULL);
×
1764
    pTarget->pDbInfo = taosArrayDup(pSrc->pDbInfo, NULL);
×
1765
    pTarget->pTableMeta = taosArrayDup(pSrc->pTableMeta, NULL);
×
1766
    pTarget->pTableHash = taosArrayDup(pSrc->pTableHash, NULL);
×
1767
    pTarget->pUdf = taosArrayDup(pSrc->pUdf, NULL);
×
1768
    pTarget->pIndex = taosArrayDup(pSrc->pIndex, NULL);
×
1769
    pTarget->pUser = taosArrayDup(pSrc->pUser, NULL);
×
1770
    pTarget->pTableIndex = taosArrayDup(pSrc->pTableIndex, NULL);
×
1771
    pTarget->pTableCfg = taosArrayDup(pSrc->pTableCfg, NULL);
×
1772
    pTarget->pTableTag = taosArrayDup(pSrc->pTableTag, NULL);
×
1773
    pTarget->pView = taosArrayDup(pSrc->pView, NULL);
×
1774
    pTarget->pTableTSMAs = taosArrayDup(pSrc->pTableTSMAs, NULL);
×
1775
    pTarget->pTSMAs = taosArrayDup(pSrc->pTSMAs, NULL);
×
1776
    pTarget->pVStbRefDbs = taosArrayDup(pSrc->pVStbRefDbs, NULL);
×
1777
    pTarget->qNodeRequired = pSrc->qNodeRequired;
×
1778
    pTarget->dNodeRequired = pSrc->dNodeRequired;
×
1779
    pTarget->svrVerRequired = pSrc->svrVerRequired;
×
1780
    pTarget->forceUpdate = pSrc->forceUpdate;
×
1781
    pTarget->cloned = true;
×
1782

1783
    *ppTarget = pTarget;
×
1784
  }
1785

1786
  return code;
×
1787
}
1788

1789
void handleSubQueryFromAnalyse(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, SNode *pRoot) {
×
1790
  SRequestObj         *pNewRequest = NULL;
×
1791
  SSqlCallbackWrapper *pNewWrapper = NULL;
×
1792
  int32_t              code = buildPreviousRequest(pWrapper->pRequest, pWrapper->pRequest->sqlstr, &pNewRequest);
×
1793
  if (code) {
×
1794
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1795
    return;
×
1796
  }
1797

1798
  pNewRequest->pQuery = NULL;
×
1799
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode **)&pNewRequest->pQuery);
×
1800
  if (pNewRequest->pQuery) {
×
1801
    pNewRequest->pQuery->pRoot = pRoot;
×
1802
    pRoot = NULL;
×
1803
    pNewRequest->pQuery->execStage = QUERY_EXEC_STAGE_ANALYSE;
×
1804
  }
1805
  if (TSDB_CODE_SUCCESS == code) {
×
1806
    code = prepareAndParseSqlSyntax(&pNewWrapper, pNewRequest, false);
×
1807
  }
1808
  if (TSDB_CODE_SUCCESS == code) {
×
1809
    code = cloneCatalogReq(&pNewWrapper->pCatalogReq, pWrapper->pCatalogReq);
×
1810
  }
1811
  if (TSDB_CODE_SUCCESS == code) {
×
1812
    doAsyncQueryFromAnalyse(pResultMeta, pNewWrapper, code);
×
1813
    nodesDestroyNode(pRoot);
×
1814
  } else {
1815
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1816
    return;
×
1817
  }
1818
}
1819

1820
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
251,871,371✔
1821
  SRequestObj *pRequest = pWrapper->pRequest;
251,871,371✔
1822
  SQuery      *pQuery = pRequest->pQuery;
251,881,608✔
1823

1824
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
251,855,844✔
1825
    SNode *prevRoot = pQuery->pPrevRoot;
×
1826
    pQuery->pPrevRoot = NULL;
×
1827
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
×
1828
    return;
×
1829
  }
1830

1831
  if (code == TSDB_CODE_SUCCESS) {
251,856,685✔
1832
    pRequest->stableQuery = pQuery->stableQuery;
204,182,974✔
1833
    if (pQuery->pRoot) {
204,185,286✔
1834
      pRequest->stmtType = pQuery->pRoot->type;
204,191,247✔
1835
    }
1836

1837
    if (pQuery->haveResultSet) {
204,157,262✔
1838
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
134,764,442✔
1839
                              pRequest->stmtBindVersion > 0);
134,763,586✔
1840
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
134,765,448✔
1841
    }
1842
  }
1843

1844
  if (code == TSDB_CODE_SUCCESS) {
251,869,382✔
1845
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
204,179,572✔
1846
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
204,176,510✔
1847
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
204,174,822✔
1848

1849
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
204,181,542✔
1850
  } else {
1851
    destorySqlCallbackWrapper(pWrapper);
47,689,810✔
1852
    pRequest->pWrapper = NULL;
47,689,810✔
1853
    qDestroyQuery(pRequest->pQuery);
47,689,810✔
1854
    pRequest->pQuery = NULL;
47,689,323✔
1855

1856
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
47,689,323✔
1857
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
2,496,545✔
1858
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1859
      restartAsyncQuery(pRequest, code);
2,496,545✔
1860
      return;
2,496,507✔
1861
    }
1862

1863
    // return to app directly
1864
    tscError("req:0x%" PRIx64 ", error occurs, code:%s, return to user app, QID:0x%" PRIx64, pRequest->self,
45,193,265✔
1865
             tstrerror(code), pRequest->requestId);
1866
    pRequest->code = code;
45,193,265✔
1867
    returnToUser(pRequest);
45,193,265✔
1868
  }
1869
}
1870

1871
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
258,432,888✔
1872
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
457,182,332✔
1873
                           .requestId = pWrapper->pParseCtx->requestId,
258,439,221✔
1874
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
258,430,665✔
1875
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
258,442,034✔
1876

1877
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
457,189,939✔
1878

1879
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
318,103,456✔
1880
                                &pWrapper->pRequest->body.queryJob);
258,435,726✔
1881
}
1882

1883
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code);
1884

1885
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
799,427,202✔
1886
  int32_t code = TSDB_CODE_SUCCESS;
799,427,202✔
1887
  switch (pWrapper->pRequest->pQuery->execStage) {
799,427,202✔
1888
    case QUERY_EXEC_STAGE_PARSE: {
6,562,492✔
1889
      // continue parse after get metadata
1890
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
6,562,492✔
1891
      break;
6,562,492✔
1892
    }
1893
    case QUERY_EXEC_STAGE_ANALYSE: {
251,879,718✔
1894
      // analysis after get metadata
1895
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
251,879,718✔
1896
      break;
251,885,901✔
1897
    }
1898
    case QUERY_EXEC_STAGE_SCHEDULE: {
540,999,474✔
1899
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
540,999,474✔
1900
      break;
540,995,081✔
1901
    }
1902
    default:
×
1903
      break;
×
1904
  }
1905
  return code;
799,443,470✔
1906
}
1907

1908
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code) {
6,562,492✔
1909
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
6,562,492✔
1910
  SRequestObj         *pRequest = pWrapper->pRequest;
6,562,492✔
1911
  SQuery              *pQuery = pRequest->pQuery;
6,562,492✔
1912

1913
  pRequest->metric.ctgCostUs += taosGetTimestampUs() - pRequest->metric.ctgStart;
6,562,492✔
1914
  qDebug("req:0x%" PRIx64 ", continue parse query, QID:0x%" PRIx64 ", code:%s", pRequest->self, pRequest->requestId,
6,562,492✔
1915
         tstrerror(code));
1916

1917
  if (code == TSDB_CODE_SUCCESS) {
6,562,492✔
1918
    // pWrapper->pCatalogReq->forceUpdate = false;
1919
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
6,524,865✔
1920
  }
1921

1922
  if (TSDB_CODE_SUCCESS == code) {
6,562,479✔
1923
    code = phaseAsyncQuery(pWrapper);
6,019,280✔
1924
  }
1925

1926
  if (TSDB_CODE_SUCCESS != code) {
6,562,492✔
1927
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
543,199✔
1928
             tstrerror(code), pWrapper->pRequest->requestId);
1929
    destorySqlCallbackWrapper(pWrapper);
543,199✔
1930
    pRequest->pWrapper = NULL;
543,199✔
1931
    terrno = code;
543,199✔
1932
    pRequest->code = code;
543,199✔
1933
    doRequestCallback(pRequest, code);
543,199✔
1934
  }
1935
}
6,562,492✔
1936

1937
void continueInsertFromCsv(SSqlCallbackWrapper *pWrapper, SRequestObj *pRequest) {
11,517✔
1938
  int32_t code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
11,517✔
1939
  if (TSDB_CODE_SUCCESS == code) {
11,517✔
1940
    code = phaseAsyncQuery(pWrapper);
11,517✔
1941
  }
1942

1943
  if (TSDB_CODE_SUCCESS != code) {
11,517✔
1944
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
×
1945
             tstrerror(code), pWrapper->pRequest->requestId);
1946
    destorySqlCallbackWrapper(pWrapper);
×
1947
    pRequest->pWrapper = NULL;
×
1948
    terrno = code;
×
1949
    pRequest->code = code;
×
1950
    doRequestCallback(pRequest, code);
×
1951
  }
1952
}
11,517✔
1953

1954
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
124,584✔
1955
  int64_t connId = *(int64_t *)taos;
124,584✔
1956
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
124,584✔
1957
}
124,584✔
1958

1959
void taos_query_a_with_reqid(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param, int64_t reqid) {
×
1960
  int64_t connId = *(int64_t *)taos;
×
1961
  taosAsyncQueryImplWithReqid(connId, sql, fp, param, false, reqid);
×
1962
}
×
1963

1964
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
802,367,975✔
1965
  const STscObj *pTscObj = pRequest->pTscObj;
802,367,975✔
1966

1967
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
802,373,098✔
1968
  if (*pCxt == NULL) {
802,352,131✔
1969
    return terrno;
×
1970
  }
1971

1972
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
1,537,738,971✔
1973
                           .requestRid = pRequest->self,
802,364,116✔
1974
                           .acctId = pTscObj->acctId,
802,364,845✔
1975
                           .db = pRequest->pDb,
802,363,294✔
1976
                           .topicQuery = false,
1977
                           .pSql = pRequest->sqlstr,
802,367,795✔
1978
                           .sqlLen = pRequest->sqlLen,
802,368,742✔
1979
                           .pMsg = pRequest->msgBuf,
802,369,910✔
1980
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1981
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
802,367,416✔
1982
                           .pStmtCb = NULL,
1983
                           .pUser = pTscObj->user,
802,370,226✔
1984
                           .userId = pTscObj->userId,
802,367,237✔
1985
                           .pEffectiveUser = pRequest->effectiveUser,
802,369,435✔
1986
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
802,368,633✔
1987
                           .enableSysInfo = pTscObj->sysInfo,
802,375,711✔
1988
                           .async = true,
1989
                           .svrVer = pTscObj->sVer,
802,370,701✔
1990
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
802,375,702✔
1991
                           .allocatorId = pRequest->allocatorRefId,
802,368,912✔
1992
                           .parseSqlFp = clientParseSql,
1993
                           .parseSqlParam = pWrapper,
1994
                           .setQueryFp = setQueryRequest,
1995
                           .timezone = pTscObj->optionInfo.timezone,
802,368,034✔
1996
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
802,375,695✔
1997
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
802,362,103✔
1998
  (*pCxt)->biMode = biMode;
802,360,246✔
1999
  return TSDB_CODE_SUCCESS;
802,362,485✔
2000
}
2001

2002
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
802,368,716✔
2003
  int32_t              code = TSDB_CODE_SUCCESS;
802,368,716✔
2004
  STscObj             *pTscObj = pRequest->pTscObj;
802,368,716✔
2005
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
802,371,337✔
2006
  if (pWrapper == NULL) {
802,366,995✔
2007
    code = terrno;
×
2008
  } else {
2009
    pWrapper->pRequest = pRequest;
802,366,995✔
2010
    pRequest->pWrapper = pWrapper;
802,369,084✔
2011
    *ppWrapper = pWrapper;
802,367,840✔
2012
  }
2013

2014
  if (TSDB_CODE_SUCCESS == code) {
802,362,854✔
2015
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
802,366,431✔
2016
  }
2017

2018
  if (TSDB_CODE_SUCCESS == code) {
802,365,536✔
2019
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
802,365,536✔
2020
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
802,375,282✔
2021
  }
2022

2023
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
802,367,607✔
2024
    int64_t syntaxStart = taosGetTimestampUs();
802,372,577✔
2025

2026
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
802,372,577✔
2027
    if (pWrapper->pCatalogReq == NULL) {
802,347,871✔
2028
      code = terrno;
×
2029
    } else {
2030
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
802,354,459✔
2031
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
802,362,021✔
2032
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
802,356,439✔
2033
    }
2034

2035
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
802,363,203✔
2036
  }
2037

2038
  return code;
802,373,577✔
2039
}
2040

2041
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
803,201,947✔
2042
  SSqlCallbackWrapper *pWrapper = NULL;
803,201,947✔
2043
  int32_t              code = TSDB_CODE_SUCCESS;
803,204,779✔
2044

2045
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
803,204,779✔
2046
    code = pRequest->prevCode;
832,962✔
2047
    terrno = code;
832,962✔
2048
    pRequest->code = code;
832,962✔
2049
    tscDebug("req:0x%" PRIx64 ", call sync query cb with code:%s", pRequest->self, tstrerror(code));
832,962✔
2050
    doRequestCallback(pRequest, code);
832,962✔
2051
    return;
832,962✔
2052
  }
2053

2054
  if (TSDB_CODE_SUCCESS == code) {
802,364,681✔
2055
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
802,368,183✔
2056
  }
2057

2058
  if (TSDB_CODE_SUCCESS == code) {
802,358,038✔
2059
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
793,409,707✔
2060
    code = phaseAsyncQuery(pWrapper);
793,416,962✔
2061
  }
2062

2063
  if (TSDB_CODE_SUCCESS != code) {
802,367,839✔
2064
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
8,954,368✔
2065
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
8,892,589✔
2066
               pRequest->requestId);
2067
    } else {
2068
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
61,779✔
2069
               pRequest->requestId);
2070
    }
2071

2072
    destorySqlCallbackWrapper(pWrapper);
8,954,368✔
2073
    pRequest->pWrapper = NULL;
8,954,368✔
2074
    qDestroyQuery(pRequest->pQuery);
8,954,368✔
2075
    pRequest->pQuery = NULL;
8,954,368✔
2076

2077
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
8,954,368✔
2078
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
10,828✔
2079
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
2080
      code = refreshMeta(pRequest->pTscObj, pRequest);
10,828✔
2081
      if (code != 0) {
10,828✔
2082
        tscWarn("req:0x%" PRIx64 ", refresh meta failed, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code,
10,828✔
2083
                tstrerror(code), pRequest->requestId);
2084
      }
2085
      pRequest->prevCode = code;
10,828✔
2086
      doAsyncQuery(pRequest, true);
10,828✔
2087
      return;
10,828✔
2088
    }
2089

2090
    terrno = code;
8,943,540✔
2091
    pRequest->code = code;
8,943,540✔
2092
    doRequestCallback(pRequest, code);
8,943,540✔
2093
  }
2094
}
2095

2096
void restartAsyncQuery(SRequestObj *pRequest, int32_t code) {
2,577,389✔
2097
  tscInfo("restart request:%s p:%p", pRequest->sqlstr, pRequest);
2,577,389✔
2098
  SRequestObj *pUserReq = pRequest;
2,577,389✔
2099
  (void)acquireRequest(pRequest->self);
2,577,389✔
2100
  while (pUserReq) {
2,577,389✔
2101
    if (pUserReq->self == pUserReq->relation.userRefId || pUserReq->relation.userRefId == 0) {
2,577,389✔
2102
      break;
2103
    } else {
2104
      int64_t nextRefId = pUserReq->relation.nextRefId;
×
2105
      (void)releaseRequest(pUserReq->self);
×
2106
      if (nextRefId) {
×
2107
        pUserReq = acquireRequest(nextRefId);
×
2108
      }
2109
    }
2110
  }
2111
  bool hasSubRequest = pUserReq != pRequest || pRequest->relation.prevRefId != 0;
2,577,389✔
2112
  if (pUserReq) {
2,577,389✔
2113
    destroyCtxInRequest(pUserReq);
2,577,389✔
2114
    pUserReq->prevCode = code;
2,577,389✔
2115
    (void)memset(&pUserReq->relation, 0, sizeof(pUserReq->relation));
2,577,389✔
2116
  } else {
2117
    tscError("User req is missing");
×
2118
    (void)removeFromMostPrevReq(pRequest);
×
2119
    return;
×
2120
  }
2121
  if (hasSubRequest)
2,577,389✔
2122
    (void)removeFromMostPrevReq(pRequest);
×
2123
  else
2124
    (void)releaseRequest(pUserReq->self);
2,577,389✔
2125
  doAsyncQuery(pUserReq, true);
2,577,389✔
2126
}
2127

2128
typedef struct SAsyncFetchParam {
2129
  SRequestObj      *pReq;
2130
  __taos_async_fn_t fp;
2131
  void             *param;
2132
} SAsyncFetchParam;
2133

2134
static int32_t doAsyncFetch(void *pParam) {
172,883,290✔
2135
  SAsyncFetchParam *param = pParam;
172,883,290✔
2136
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
172,883,290✔
2137
  taosMemoryFree(param);
172,881,849✔
2138
  return TSDB_CODE_SUCCESS;
172,878,429✔
2139
}
2140

2141
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
172,904,193✔
2142
  if (res == NULL || fp == NULL) {
172,904,193✔
2143
    tscError("taos_fetch_rows_a invalid paras");
×
2144
    return;
×
2145
  }
2146
  if (!TD_RES_QUERY(res)) {
172,904,212✔
2147
    tscError("taos_fetch_rows_a res is NULL");
×
2148
    fp(param, res, TSDB_CODE_APP_ERROR);
×
2149
    return;
×
2150
  }
2151

2152
  SRequestObj *pRequest = res;
172,904,212✔
2153
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
172,904,212✔
2154
    fp(param, res, 0);
20,941✔
2155
    return;
20,941✔
2156
  }
2157

2158
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
172,883,271✔
2159
  if (!pParam) {
172,882,743✔
2160
    fp(param, res, terrno);
×
2161
    return;
×
2162
  }
2163
  pParam->pReq = pRequest;
172,882,743✔
2164
  pParam->fp = fp;
172,882,781✔
2165
  pParam->param = param;
172,883,271✔
2166
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
172,883,252✔
2167
  if (TSDB_CODE_SUCCESS != code) {
172,883,290✔
2168
    taosMemoryFree(pParam);
×
2169
    fp(param, res, code);
×
2170
    return;
×
2171
  }
2172
}
2173

2174
void taos_fetch_raw_block_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
153✔
2175
  if (res == NULL || fp == NULL) {
153✔
2176
    tscError("taos_fetch_raw_block_a invalid paras");
×
2177
    return;
×
2178
  }
2179
  if (!TD_RES_QUERY(res)) {
153✔
2180
    tscError("taos_fetch_raw_block_a res is NULL");
×
2181
    return;
×
2182
  }
2183
  SRequestObj    *pRequest = res;
153✔
2184
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
153✔
2185

2186
  // set the current block is all consumed
2187
  pResultInfo->convertUcs4 = false;
153✔
2188

2189
  // it is a local executed query, no need to do async fetch
2190
  taos_fetch_rows_a(pRequest, fp, param);
153✔
2191
}
2192

2193
const void *taos_get_raw_block(TAOS_RES *res) {
90✔
2194
  if (res == NULL) {
90✔
2195
    tscError("taos_get_raw_block invalid paras");
×
2196
    return NULL;
×
2197
  }
2198
  if (!TD_RES_QUERY(res)) {
90✔
2199
    tscError("taos_get_raw_block res is NULL");
×
2200
    return NULL;
×
2201
  }
2202
  SRequestObj *pRequest = res;
90✔
2203

2204
  return pRequest->body.resInfo.pData;
90✔
2205
}
2206

2207
int taos_get_db_route_info(TAOS *taos, const char *db, TAOS_DB_ROUTE_INFO *dbInfo) {
×
2208
  if (NULL == taos) {
×
2209
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2210
    return terrno;
×
2211
  }
2212

2213
  if (NULL == db || NULL == dbInfo) {
×
2214
    tscError("invalid input param, db:%p, dbInfo:%p", db, dbInfo);
×
2215
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2216
    return terrno;
×
2217
  }
2218

2219
  int64_t      connId = *(int64_t *)taos;
×
2220
  SRequestObj *pRequest = NULL;
×
2221
  char        *sql = "taos_get_db_route_info";
×
2222
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2223
  if (code != TSDB_CODE_SUCCESS) {
×
2224
    terrno = code;
×
2225
    return terrno;
×
2226
  }
2227

2228
  STscObj  *pTscObj = pRequest->pTscObj;
×
2229
  SCatalog *pCtg = NULL;
×
2230
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2231
  if (code != TSDB_CODE_SUCCESS) {
×
2232
    goto _return;
×
2233
  }
2234

2235
  SRequestConnInfo conn = {
×
2236
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2237

2238
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2239

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

2243
  code = catalogGetDBVgInfo(pCtg, &conn, dbFName, dbInfo);
×
2244
  if (code) {
×
2245
    goto _return;
×
2246
  }
2247

2248
_return:
×
2249

2250
  terrno = code;
×
2251

2252
  destroyRequest(pRequest);
×
2253
  return code;
×
2254
}
2255

2256
int taos_get_table_vgId(TAOS *taos, const char *db, const char *table, int *vgId) {
×
2257
  if (NULL == taos) {
×
2258
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2259
    return terrno;
×
2260
  }
2261

2262
  if (NULL == db || NULL == table || NULL == vgId) {
×
2263
    tscError("invalid input param, db:%p, table:%p, vgId:%p", db, table, vgId);
×
2264
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2265
    return terrno;
×
2266
  }
2267

2268
  int64_t      connId = *(int64_t *)taos;
×
2269
  SRequestObj *pRequest = NULL;
×
2270
  char        *sql = "taos_get_table_vgId";
×
2271
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2272
  if (code != TSDB_CODE_SUCCESS) {
×
2273
    return terrno;
×
2274
  }
2275

2276
  pRequest->syncQuery = true;
×
2277

2278
  STscObj  *pTscObj = pRequest->pTscObj;
×
2279
  SCatalog *pCtg = NULL;
×
2280
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2281
  if (code != TSDB_CODE_SUCCESS) {
×
2282
    goto _return;
×
2283
  }
2284

2285
  SRequestConnInfo conn = {
×
2286
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2287

2288
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2289

2290
  SName tableName = {0};
×
2291
  toName(pTscObj->acctId, db, table, &tableName);
×
2292

2293
  SVgroupInfo vgInfo;
×
2294
  code = catalogGetTableHashVgroup(pCtg, &conn, &tableName, &vgInfo);
×
2295
  if (code) {
×
2296
    goto _return;
×
2297
  }
2298

2299
  *vgId = vgInfo.vgId;
×
2300

2301
_return:
×
2302

2303
  terrno = code;
×
2304

2305
  destroyRequest(pRequest);
×
2306
  return code;
×
2307
}
2308

2309
int taos_get_tables_vgId(TAOS *taos, const char *db, const char *table[], int tableNum, int *vgId) {
×
2310
  if (NULL == taos) {
×
2311
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2312
    return terrno;
×
2313
  }
2314

2315
  if (NULL == db || NULL == table || NULL == vgId || tableNum <= 0) {
×
2316
    tscError("invalid input param, db:%p, table:%p, vgId:%p, tbNum:%d", db, table, vgId, tableNum);
×
2317
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2318
    return terrno;
×
2319
  }
2320

2321
  int64_t      connId = *(int64_t *)taos;
×
2322
  SRequestObj *pRequest = NULL;
×
2323
  char        *sql = "taos_get_table_vgId";
×
2324
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2325
  if (code != TSDB_CODE_SUCCESS) {
×
2326
    return terrno;
×
2327
  }
2328

2329
  pRequest->syncQuery = true;
×
2330

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

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

2341
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2342

2343
  code = catalogGetTablesHashVgId(pCtg, &conn, pTscObj->acctId, db, table, tableNum, vgId);
×
2344
  if (code) {
×
2345
    goto _return;
×
2346
  }
2347

2348
_return:
×
2349

2350
  terrno = code;
×
2351

2352
  destroyRequest(pRequest);
×
2353
  return code;
×
2354
}
2355

2356
int taos_load_table_info(TAOS *taos, const char *tableNameList) {
1,146✔
2357
  if (NULL == taos) {
1,146✔
2358
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2359
    return terrno;
×
2360
  }
2361

2362
  int64_t       connId = *(int64_t *)taos;
1,146✔
2363
  const int32_t MAX_TABLE_NAME_LENGTH = 12 * 1024 * 1024;  // 12MB list
1,146✔
2364
  int32_t       code = 0;
1,146✔
2365
  SRequestObj  *pRequest = NULL;
1,146✔
2366
  SCatalogReq   catalogReq = {0};
1,146✔
2367

2368
  if (NULL == tableNameList) {
1,146✔
2369
    return TSDB_CODE_SUCCESS;
×
2370
  }
2371

2372
  int32_t length = (int32_t)strlen(tableNameList);
1,146✔
2373
  if (0 == length) {
1,146✔
2374
    return TSDB_CODE_SUCCESS;
×
2375
  } else if (length > MAX_TABLE_NAME_LENGTH) {
1,146✔
2376
    tscError("tableNameList too long, length:%d, maximum allowed:%d", length, MAX_TABLE_NAME_LENGTH);
×
2377
    return TSDB_CODE_TSC_INVALID_OPERATION;
×
2378
  }
2379

2380
  char *sql = "taos_load_table_info";
1,146✔
2381
  code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
1,146✔
2382
  if (code != TSDB_CODE_SUCCESS) {
1,146✔
2383
    terrno = code;
×
2384
    goto _return;
×
2385
  }
2386

2387
  pRequest->syncQuery = true;
1,146✔
2388

2389
  STscObj *pTscObj = pRequest->pTscObj;
1,146✔
2390
  code = transferTableNameList(tableNameList, pTscObj->acctId, pTscObj->db, &catalogReq.pTableMeta);
1,146✔
2391
  if (code) {
1,146✔
2392
    goto _return;
×
2393
  }
2394

2395
  SCatalog *pCtg = NULL;
1,146✔
2396
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
1,146✔
2397
  if (code != TSDB_CODE_SUCCESS) {
1,146✔
2398
    goto _return;
×
2399
  }
2400

2401
  SRequestConnInfo conn = {
1,146✔
2402
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
1,146✔
2403

2404
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
1,146✔
2405

2406
  code = catalogAsyncGetAllMeta(pCtg, &conn, &catalogReq, syncCatalogFn, pRequest->body.interParam, NULL);
1,146✔
2407
  if (code) {
1,146✔
2408
    goto _return;
×
2409
  }
2410

2411
  SSyncQueryParam *pParam = pRequest->body.interParam;
1,146✔
2412
  code = tsem_wait(&pParam->sem);
1,146✔
2413
  if (code) {
1,146✔
2414
    tscError("tsem wait failed, code:%d - %s", code, tstrerror(code));
×
2415
    goto _return;
×
2416
  }
2417
_return:
1,146✔
2418
  destoryCatalogReq(&catalogReq);
1,146✔
2419
  destroyRequest(pRequest);
1,146✔
2420
  return code;
1,146✔
2421
}
2422

2423
TAOS_STMT *taos_stmt_init(TAOS *taos) {
130,371✔
2424
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
130,371✔
2425
  if (NULL == pObj) {
130,408✔
2426
    tscError("invalid parameter for %s", __FUNCTION__);
×
2427
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2428
    return NULL;
×
2429
  }
2430

2431
  TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL);
130,408✔
2432
  if (NULL == pStmt) {
130,450✔
2433
    tscError("stmt init failed, errcode:%s", terrstr());
×
2434
  }
2435
  releaseTscObj(*(int64_t *)taos);
130,450✔
2436

2437
  return pStmt;
130,450✔
2438
}
2439

2440
TAOS_STMT *taos_stmt_init_with_reqid(TAOS *taos, int64_t reqid) {
×
2441
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
2442
  if (NULL == pObj) {
×
2443
    tscError("invalid parameter for %s", __FUNCTION__);
×
2444
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2445
    return NULL;
×
2446
  }
2447

2448
  TAOS_STMT *pStmt = stmtInit(pObj, reqid, NULL);
×
2449
  if (NULL == pStmt) {
×
2450
    tscError("stmt init failed, errcode:%s", terrstr());
×
2451
  }
2452
  releaseTscObj(*(int64_t *)taos);
×
2453

2454
  return pStmt;
×
2455
}
2456

2457
TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options) {
12,188✔
2458
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
12,188✔
2459
  if (NULL == pObj) {
12,504✔
2460
    tscError("invalid parameter for %s", __FUNCTION__);
×
2461
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2462
    return NULL;
×
2463
  }
2464

2465
  TAOS_STMT *pStmt = stmtInit(pObj, options->reqId, options);
12,504✔
2466
  if (NULL == pStmt) {
12,504✔
2467
    tscError("stmt init failed, errcode:%s", terrstr());
×
2468
  }
2469
  releaseTscObj(*(int64_t *)taos);
12,504✔
2470

2471
  return pStmt;
12,504✔
2472
}
2473

2474
int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
6,622,699✔
2475
  if (stmt == NULL || sql == NULL) {
6,622,699✔
2476
    tscError("NULL parameter for %s", __FUNCTION__);
×
2477
    terrno = TSDB_CODE_INVALID_PARA;
×
2478
    return terrno;
×
2479
  }
2480

2481
  return stmtPrepare(stmt, sql, length);
6,623,997✔
2482
}
2483

2484
int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags) {
5,959✔
2485
  if (stmt == NULL || name == NULL) {
5,959✔
2486
    tscError("NULL parameter for %s", __FUNCTION__);
×
2487
    terrno = TSDB_CODE_INVALID_PARA;
×
2488
    return terrno;
×
2489
  }
2490

2491
  int32_t code = stmtSetTbName(stmt, name);
5,959✔
2492
  if (code) {
5,959✔
2493
    return code;
573✔
2494
  }
2495

2496
  if (tags) {
5,386✔
2497
    return stmtSetTbTags(stmt, tags);
5,386✔
2498
  }
2499

2500
  return TSDB_CODE_SUCCESS;
×
2501
}
2502

2503
int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name) {
8,802,374✔
2504
  if (stmt == NULL || name == NULL) {
8,802,374✔
2505
    tscError("NULL parameter for %s", __FUNCTION__);
×
2506
    terrno = TSDB_CODE_INVALID_PARA;
×
2507
    return terrno;
×
2508
  }
2509

2510
  return stmtSetTbName(stmt, name);
8,804,867✔
2511
}
2512

2513
int taos_stmt_set_tags(TAOS_STMT *stmt, TAOS_MULTI_BIND *tags) {
198✔
2514
  if (stmt == NULL || tags == NULL) {
198✔
2515
    tscError("NULL parameter for %s", __FUNCTION__);
×
2516
    terrno = TSDB_CODE_INVALID_PARA;
×
2517
    return terrno;
×
2518
  }
2519

2520
  return stmtSetTbTags(stmt, tags);
198✔
2521
}
2522

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

2525
int taos_stmt_get_tag_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
×
2526
  if (stmt == NULL || NULL == fieldNum) {
×
2527
    tscError("NULL parameter for %s", __FUNCTION__);
×
2528
    terrno = TSDB_CODE_INVALID_PARA;
×
2529
    return terrno;
×
2530
  }
2531

2532
  return stmtGetTagFields(stmt, fieldNum, fields);
×
2533
}
2534

2535
int taos_stmt_get_col_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
×
2536
  if (stmt == NULL || NULL == fieldNum) {
×
2537
    tscError("NULL parameter for %s", __FUNCTION__);
×
2538
    terrno = TSDB_CODE_INVALID_PARA;
×
2539
    return terrno;
×
2540
  }
2541

2542
  return stmtGetColFields(stmt, fieldNum, fields);
×
2543
}
2544

2545
// let stmt to reclaim TAOS_FIELD_E that was allocated by `taos_stmt_get_tag_fields`/`taos_stmt_get_col_fields`
2546
void taos_stmt_reclaim_fields(TAOS_STMT *stmt, TAOS_FIELD_E *fields) {
×
2547
  (void)stmt;
2548
  if (!fields) return;
×
2549
  taosMemoryFree(fields);
×
2550
}
2551

2552
int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
26,896✔
2553
  if (stmt == NULL || bind == NULL) {
26,896✔
2554
    tscError("NULL parameter for %s", __FUNCTION__);
×
2555
    terrno = TSDB_CODE_INVALID_PARA;
×
2556
    return terrno;
×
2557
  }
2558

2559
  if (bind->num > 1) {
26,896✔
2560
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
3,708✔
2561
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
3,708✔
2562
    return terrno;
3,708✔
2563
  }
2564

2565
  return stmtBindBatch(stmt, bind, -1);
23,188✔
2566
}
2567

2568
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
68,313,016✔
2569
  if (stmt == NULL || bind == NULL) {
68,313,016✔
2570
    tscError("NULL parameter for %s", __FUNCTION__);
×
2571
    terrno = TSDB_CODE_INVALID_PARA;
×
2572
    return terrno;
×
2573
  }
2574

2575
  if (bind->num <= 0 || bind->num > INT16_MAX) {
69,100,833✔
2576
    tscError("invalid bind num %d", bind->num);
23,038✔
2577
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
23,038✔
2578
    return terrno;
×
2579
  }
2580

2581
  int32_t insert = 0;
69,464,385✔
2582
  int32_t code = stmtIsInsert(stmt, &insert);
69,328,814✔
2583
  if (TSDB_CODE_SUCCESS != code) {
69,147,006✔
2584
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2585
    return code;
×
2586
  }
2587
  if (0 == insert && bind->num > 1) {
69,147,006✔
2588
    tscError("only one row data allowed for query");
×
2589
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2590
    return terrno;
×
2591
  }
2592

2593
  return stmtBindBatch(stmt, bind, -1);
69,147,006✔
2594
}
2595

2596
int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind, int colIdx) {
1,260✔
2597
  if (stmt == NULL || bind == NULL) {
1,260✔
2598
    tscError("NULL parameter for %s", __FUNCTION__);
×
2599
    terrno = TSDB_CODE_INVALID_PARA;
×
2600
    return terrno;
×
2601
  }
2602

2603
  if (colIdx < 0) {
1,260✔
2604
    tscError("invalid bind column idx %d", colIdx);
×
2605
    terrno = TSDB_CODE_INVALID_PARA;
×
2606
    return terrno;
×
2607
  }
2608

2609
  int32_t insert = 0;
1,260✔
2610
  int32_t code = stmtIsInsert(stmt, &insert);
1,260✔
2611
  if (TSDB_CODE_SUCCESS != code) {
1,260✔
2612
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2613
    return code;
×
2614
  }
2615
  if (0 == insert && bind->num > 1) {
1,260✔
2616
    tscError("only one row data allowed for query");
×
2617
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2618
    return terrno;
×
2619
  }
2620

2621
  return stmtBindBatch(stmt, bind, colIdx);
1,260✔
2622
}
2623

2624
int taos_stmt_add_batch(TAOS_STMT *stmt) {
65,918,841✔
2625
  if (stmt == NULL) {
65,918,841✔
2626
    tscError("NULL parameter for %s", __FUNCTION__);
×
2627
    terrno = TSDB_CODE_INVALID_PARA;
×
2628
    return terrno;
×
2629
  }
2630

2631
  return stmtAddBatch(stmt);
65,918,841✔
2632
}
2633

2634
int taos_stmt_execute(TAOS_STMT *stmt) {
6,963,176✔
2635
  if (stmt == NULL) {
6,963,176✔
2636
    tscError("NULL parameter for %s", __FUNCTION__);
×
2637
    terrno = TSDB_CODE_INVALID_PARA;
×
2638
    return terrno;
×
2639
  }
2640

2641
  return stmtExec(stmt);
6,963,176✔
2642
}
2643

2644
int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
×
2645
  if (stmt == NULL || insert == NULL) {
×
2646
    tscError("NULL parameter for %s", __FUNCTION__);
×
2647
    terrno = TSDB_CODE_INVALID_PARA;
×
2648
    return terrno;
×
2649
  }
2650

2651
  return stmtIsInsert(stmt, insert);
×
2652
}
2653

2654
int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) {
×
2655
  if (stmt == NULL || nums == NULL) {
×
2656
    tscError("NULL parameter for %s", __FUNCTION__);
×
2657
    terrno = TSDB_CODE_INVALID_PARA;
×
2658
    return terrno;
×
2659
  }
2660

2661
  return stmtGetParamNum(stmt, nums);
×
2662
}
2663

2664
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
×
2665
  if (stmt == NULL || type == NULL || NULL == bytes || idx < 0) {
×
2666
    tscError("invalid parameter for %s", __FUNCTION__);
×
2667
    terrno = TSDB_CODE_INVALID_PARA;
×
2668
    return terrno;
×
2669
  }
2670

2671
  return stmtGetParam(stmt, idx, type, bytes);
×
2672
}
2673

2674
TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt) {
9,168✔
2675
  if (stmt == NULL) {
9,168✔
2676
    tscError("NULL parameter for %s", __FUNCTION__);
×
2677
    terrno = TSDB_CODE_INVALID_PARA;
×
2678
    return NULL;
×
2679
  }
2680

2681
  return stmtUseResult(stmt);
9,168✔
2682
}
2683

2684
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
9,713✔
2685

2686
int taos_stmt_affected_rows(TAOS_STMT *stmt) {
3,010✔
2687
  if (stmt == NULL) {
3,010✔
2688
    tscError("NULL parameter for %s", __FUNCTION__);
×
2689
    terrno = TSDB_CODE_INVALID_PARA;
×
2690
    return 0;
×
2691
  }
2692

2693
  return stmtAffectedRows(stmt);
3,010✔
2694
}
2695

2696
int taos_stmt_affected_rows_once(TAOS_STMT *stmt) {
110✔
2697
  if (stmt == NULL) {
110✔
2698
    tscError("NULL parameter for %s", __FUNCTION__);
×
2699
    terrno = TSDB_CODE_INVALID_PARA;
×
2700
    return 0;
×
2701
  }
2702

2703
  return stmtAffectedRowsOnce(stmt);
110✔
2704
}
2705

2706
int taos_stmt_close(TAOS_STMT *stmt) {
142,954✔
2707
  if (stmt == NULL) {
142,954✔
2708
    tscError("NULL parameter for %s", __FUNCTION__);
×
2709
    terrno = TSDB_CODE_INVALID_PARA;
×
2710
    return terrno;
×
2711
  }
2712

2713
  return stmtClose(stmt);
142,954✔
2714
}
2715

2716
TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option) {
3,095✔
2717
  if (NULL == taos) {
3,095✔
2718
    tscError("NULL parameter for %s", __FUNCTION__);
×
2719
    terrno = TSDB_CODE_INVALID_PARA;
×
2720
    return NULL;
×
2721
  }
2722
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
3,095✔
2723
  if (NULL == pObj) {
3,095✔
2724
    tscError("invalid parameter for %s", __FUNCTION__);
×
2725
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2726
    return NULL;
×
2727
  }
2728

2729
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
3,095✔
2730

2731
  releaseTscObj(*(int64_t *)taos);
3,095✔
2732

2733
  return pStmt;
3,095✔
2734
}
2735

2736
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
3,207✔
2737
  if (stmt == NULL || sql == NULL) {
3,207✔
2738
    tscError("NULL parameter for %s", __FUNCTION__);
×
2739
    terrno = TSDB_CODE_INVALID_PARA;
×
2740
    return terrno;
×
2741
  }
2742

2743
  return stmtPrepare2(stmt, sql, length);
3,207✔
2744
}
2745

2746
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
536,067✔
2747
  if (stmt == NULL) {
536,067✔
2748
    tscError("NULL parameter for %s", __FUNCTION__);
×
2749
    terrno = TSDB_CODE_INVALID_PARA;
×
2750
    return terrno;
×
2751
  }
2752

2753
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
536,067✔
2754
  int32_t    code = TSDB_CODE_SUCCESS;
536,067✔
2755
  STMT2_DLOG_E("start to bind param");
536,067✔
2756

2757
  // check query bind number
2758
  bool isQuery = (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt)));
536,134✔
2759
  if (isQuery) {
536,134✔
2760
    if (bindv->count != 1 || bindv->bind_cols[0]->num != 1) {
9✔
2761
      terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2762
      STMT2_ELOG_E("query only support one table and one row bind");
×
2763
      return terrno;
×
2764
    }
2765
  }
2766

2767
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 1) {
536,134✔
2768
    STMT2_ELOG_E("async bind param is still working, please try again later");
13✔
2769
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
13✔
2770
    return terrno;
×
2771
  }
2772

2773
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
536,002✔
2774
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
2775
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
2776
    }
2777
    pStmt->execSemWaited = true;
×
2778
  }
2779

2780
  for (int i = 0; i < bindv->count; ++i) {
1,628,445✔
2781
    SVCreateTbReq *pCreateTbReq = NULL;
1,092,081✔
2782
    if (!isQuery) {
1,091,989✔
2783
      STMT2_TLOG("start to bind %dth table", i);
1,091,768✔
2784
      if (bindv->tbnames && bindv->tbnames[i]) {
1,091,911✔
2785
        code = stmtSetTbName2(stmt, bindv->tbnames[i]);
1,092,102✔
2786
        if (code) {
1,091,816✔
2787
          terrno = code;
×
2788
          STMT2_ELOG("set tbname failed, code:%s", tstrerror(code));
×
2789
          return terrno;
×
2790
        }
2791
      }
2792

2793
      if (bindv->tags && bindv->tags[i]) {
1,091,823✔
2794
        code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
525,325✔
2795
      } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
566,714✔
2796
        code = stmtCheckTags2(stmt, &pCreateTbReq);
111,972✔
2797
      } else if (pStmt->sql.autoCreateTbl) {
454,420✔
2798
        code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
×
2799
      }
2800

2801
      if (code) {
1,091,646✔
2802
        terrno = code;
×
2803
        STMT2_ELOG("set tags failed, code:%s", tstrerror(code));
×
2804
        return terrno;
×
2805
      }
2806
    }
2807

2808
    if (bindv->bind_cols && bindv->bind_cols[i]) {
1,091,867✔
2809
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
1,091,920✔
2810

2811
      if (bind->num <= 0 || bind->num > INT16_MAX) {
1,092,150✔
2812
        STMT2_ELOG("bind num:%d must > 0 and < INT16_MAX", bind->num);
340✔
2813
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
340✔
2814
        return terrno;
×
2815
      }
2816

2817
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
1,091,778✔
2818
      if (TSDB_CODE_SUCCESS != code) {
1,091,852✔
2819
        terrno = code;
92✔
2820
        STMT2_ELOG("bind batch failed, code:%s", tstrerror(code));
×
2821
        return terrno;
×
2822
      }
2823
    }
2824
  }
2825

2826
  return code;
536,226✔
2827
}
2828

2829
int taos_stmt2_bind_param_a(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx, __taos_async_fn_t fp,
×
2830
                            void *param) {
2831
  if (stmt == NULL || bindv == NULL || fp == NULL) {
×
2832
    terrno = TSDB_CODE_INVALID_PARA;
×
2833
    return terrno;
×
2834
  }
2835

2836
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2837

2838
  ThreadArgs *args = (ThreadArgs *)taosMemoryMalloc(sizeof(ThreadArgs));
×
2839
  args->stmt = stmt;
×
2840
  args->bindv = bindv;
×
2841
  args->col_idx = col_idx;
×
2842
  args->fp = fp;
×
2843
  args->param = param;
×
2844

2845
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2846
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 0) {
×
2847
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2848
    tscError("async bind param is still working, please try again later");
×
2849
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
×
2850
    return terrno;
×
2851
  }
2852
  (void)atomic_add_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2853
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2854

2855
  int code_s = taosStmt2AsyncBind(stmtAsyncBindThreadFunc, (void *)args);
×
2856
  if (code_s != TSDB_CODE_SUCCESS) {
×
2857
    terrno = code_s;
×
2858
    (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2859
    (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2860
    (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2861
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2862
    tscError("async bind failed, code:%d , %s", code_s, tstrerror(code_s));
×
2863
  }
2864

2865
  return code_s;
×
2866
}
2867

2868
int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows) {
536,206✔
2869
  if (stmt == NULL) {
536,206✔
2870
    tscError("NULL parameter for %s", __FUNCTION__);
×
2871
    terrno = TSDB_CODE_INVALID_PARA;
×
2872
    return terrno;
×
2873
  }
2874

2875
  return stmtExec2(stmt, affected_rows);
536,206✔
2876
}
2877

2878
int taos_stmt2_close(TAOS_STMT2 *stmt) {
3,095✔
2879
  if (stmt == NULL) {
3,095✔
2880
    tscError("NULL parameter for %s", __FUNCTION__);
×
2881
    terrno = TSDB_CODE_INVALID_PARA;
×
2882
    return terrno;
×
2883
  }
2884

2885
  return stmtClose2(stmt);
3,095✔
2886
}
2887

2888
int taos_stmt2_is_insert(TAOS_STMT2 *stmt, int *insert) {
35✔
2889
  if (stmt == NULL || insert == NULL) {
35✔
2890
    tscError("NULL parameter for %s", __FUNCTION__);
×
2891
    terrno = TSDB_CODE_INVALID_PARA;
×
2892
    return terrno;
×
2893
  }
2894
  *insert = stmt2IsInsert(stmt);
35✔
2895
  return TSDB_CODE_SUCCESS;
35✔
2896
}
2897

2898
int taos_stmt2_get_fields(TAOS_STMT2 *stmt, int *count, TAOS_FIELD_ALL **fields) {
26✔
2899
  if (stmt == NULL || count == NULL) {
26✔
2900
    tscError("NULL parameter for %s", __FUNCTION__);
×
2901
    terrno = TSDB_CODE_INVALID_PARA;
×
2902
    return terrno;
×
2903
  }
2904

2905
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
26✔
2906
  STMT2_DLOG_E("start to get fields");
26✔
2907

2908
  if (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type ||
26✔
2909
      (pStmt->sql.type == 0 && stmt2IsInsert(stmt))) {
26✔
2910
    return stmtGetStbColFields2(stmt, count, fields);
26✔
2911
  }
2912
  if (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt))) {
×
2913
    return stmtGetParamNum2(stmt, count);
×
2914
  }
2915

2916
  tscError("Invalid sql for stmt %s", pStmt->sql.sqlStr);
×
2917
  return TSDB_CODE_PAR_SYNTAX_ERROR;
×
2918
}
2919

2920
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_ALL *fields) {
26✔
2921
  (void)stmt;
2922
  if (!fields) return;
26✔
2923
  taosMemoryFree(fields);
26✔
2924
}
2925

2926
TAOS_RES *taos_stmt2_result(TAOS_STMT2 *stmt) {
9✔
2927
  if (stmt == NULL) {
9✔
2928
    tscError("NULL parameter for %s", __FUNCTION__);
×
2929
    terrno = TSDB_CODE_INVALID_PARA;
×
2930
    return NULL;
×
2931
  }
2932

2933
  return stmtUseResult2(stmt);
9✔
2934
}
2935

2936
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmtErrstr2(stmt); }
×
2937

2938
int taos_set_conn_mode(TAOS *taos, int mode, int value) {
2,147✔
2939
  int32_t code = 0;
2,147✔
2940
  if (taos == NULL) {
2,147✔
2941
    terrno = TSDB_CODE_INVALID_PARA;
×
2942
    return terrno;
×
2943
  }
2944

2945
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2,147✔
2946
  if (NULL == pObj) {
2,147✔
2947
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2948
    tscError("invalid parameter for %s", __func__);
×
2949
    return terrno;
×
2950
  }
2951
  switch (mode) {
2,147✔
2952
    case TAOS_CONN_MODE_BI:
2,147✔
2953
      atomic_store_8(&pObj->biMode, value);
2,147✔
2954
      break;
2,147✔
2955
    default:
×
2956
      tscError("not supported mode.");
×
2957
      code = TSDB_CODE_INVALID_PARA;
×
2958
  }
2959
  releaseTscObj(*(int64_t *)taos);
2,147✔
2960
  return code;
2,147✔
2961
}
2962

2963
char *getBuildInfo() { return td_buildinfo; }
×
2964

2965
int32_t taos_connect_is_alive(TAOS *taos) {
×
2966
  int32_t code = 0, lino = 0;
×
2967
  if (taos == NULL) {
×
2968
    terrno = TSDB_CODE_INVALID_PARA;
×
2969
    return terrno;
×
2970
  }
2971

2972
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
2973
  if (NULL == pObj) {
×
2974
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2975
    tscError("invalid parameter for %s", __func__);
×
2976
    return terrno;
×
2977
  }
2978

2979
  code = tscCheckConnSessionMetric(pObj);
×
2980
  TAOS_CHECK_GOTO(code, &lino, _error);
×
2981

2982
_error:
×
2983
  releaseTscObj(*(int64_t *)taos);
×
2984

2985
  if (code != 0) {
×
2986
    tscError("taos conn failed to check alive, code:%d - %s", code, tstrerror(code));
×
2987
  }
2988

2989
  return code != 0 ? 0 : 1;
×
2990
}
2991
static int32_t buildInstanceRegisterSql(const SInstanceRegisterReq *req, char **ppSql, uint32_t *pLen) {
×
2992
  const char *action = (req->expire < 0) ? "UNREGISTER" : "REGISTER";
×
2993
  int32_t     len = 0;
×
2994

2995
  len += snprintf(NULL, 0, "%s INSTANCE '%s'", action, req->id);
×
2996
  if (req->type[0] != 0) {
×
2997
    len += snprintf(NULL, 0, " TYPE '%s'", req->type);
×
2998
  }
2999
  if (req->desc[0] != 0) {
×
3000
    len += snprintf(NULL, 0, " DESC '%s'", req->desc);
×
3001
  }
3002
  if (req->expire >= 0) {
×
3003
    len += snprintf(NULL, 0, " EXPIRE %d", req->expire);
×
3004
  }
3005

3006
  char *sql = taosMemoryMalloc((size_t)len + 1);
×
3007
  if (sql == NULL) {
×
3008
    return terrno;
×
3009
  }
3010

3011
  int32_t offset = snprintf(sql, (size_t)len + 1, "%s INSTANCE '%s'", action, req->id);
×
3012
  if (req->type[0] != 0) {
×
3013
    offset += snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " TYPE '%s'", req->type);
×
3014
  }
3015
  if (req->desc[0] != 0) {
×
3016
    offset += snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " DESC '%s'", req->desc);
×
3017
  }
3018
  if (req->expire >= 0) {
×
3019
    (void)snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " EXPIRE %d", req->expire);
×
3020
  }
3021

3022
  *ppSql = sql;
×
3023
  if (pLen != NULL) {
×
3024
    *pLen = (uint32_t)len;
×
3025
  }
3026
  return TSDB_CODE_SUCCESS;
×
3027
}
3028

3029
static int32_t sendInstanceRegisterReq(STscObj *pObj, const SInstanceRegisterReq *req) {
×
3030
  SRequestObj *pRequest = NULL;
×
3031
  int32_t      code = createRequest(pObj->id, TDMT_MND_REGISTER_INSTANCE, 0, &pRequest);
×
3032
  if (code != TSDB_CODE_SUCCESS) {
×
3033
    terrno = code;
×
3034
    return code;
×
3035
  }
3036

3037
  code = buildInstanceRegisterSql(req, &pRequest->sqlstr, (uint32_t *)&pRequest->sqlLen);
×
3038
  if (code != TSDB_CODE_SUCCESS) {
×
3039
    goto _cleanup;
×
3040
  }
3041

3042
  int32_t msgLen = tSerializeSInstanceRegisterReq(NULL, 0, (SInstanceRegisterReq *)req);
×
3043
  if (msgLen <= 0) {
×
3044
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3045
    goto _cleanup;
×
3046
  }
3047

3048
  void *pMsg = taosMemoryMalloc(msgLen);
×
3049
  if (pMsg == NULL) {
×
3050
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3051
    goto _cleanup;
×
3052
  }
3053

3054
  if (tSerializeSInstanceRegisterReq(pMsg, msgLen, (SInstanceRegisterReq *)req) < 0) {
×
3055
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3056
    taosMemoryFree(pMsg);
×
3057
    goto _cleanup;
×
3058
  }
3059

3060
  pRequest->type = TDMT_MND_REGISTER_INSTANCE;
×
3061
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsg, .len = msgLen, .handle = NULL};
×
3062

3063
  SMsgSendInfo *pSend = buildMsgInfoImpl(pRequest);
×
3064
  if (pSend == NULL) {
×
3065
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3066
    taosMemoryFree(pMsg);
×
3067
    pRequest->body.requestMsg.pData = NULL;
×
3068
    goto _cleanup;
×
3069
  }
3070

3071
  SEpSet epSet = getEpSet_s(&pObj->pAppInfo->mgmtEp);
×
3072
  code = asyncSendMsgToServer(pObj->pAppInfo->pTransporter, &epSet, NULL, pSend);
×
3073
  if (code != TSDB_CODE_SUCCESS) {
×
3074
    destroySendMsgInfo(pSend);
×
3075
    pRequest->body.requestMsg = (SDataBuf){0};
×
3076
    goto _cleanup;
×
3077
  }
3078

3079
  code = tsem_wait(&pRequest->body.rspSem);
×
3080
  if (code != TSDB_CODE_SUCCESS) {
×
3081
    code = terrno != 0 ? terrno : code;
×
3082
    goto _cleanup;
×
3083
  }
3084

3085
  code = pRequest->code;
×
3086
  terrno = code;
×
3087

3088
_cleanup:
×
3089
  destroyRequest(pRequest);
×
3090
  return code;
×
3091
}
3092

3093
static bool instanceRegisterRpcRfp(int32_t code, tmsg_t msgType) {
×
3094
  if (NEED_REDIRECT_ERROR(code)) {
×
3095
    return true;
×
3096
  } else if (code == TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY || code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE ||
×
3097
             code == TSDB_CODE_SYN_WRITE_STALL || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
×
3098
             code == TSDB_CODE_SYN_RESTORING) {
3099
    tscDebug("client msg type %s should retry since %s", TMSG_INFO(msgType), tstrerror(code));
×
3100
    return true;
×
3101
  } else {
3102
    return false;
×
3103
  }
3104
}
3105

3106
int32_t taos_register_instance(const char *id, const char *type, const char *desc, int32_t expire) {
×
3107
  if (id == NULL || id[0] == 0) {
×
3108
    return terrno = TSDB_CODE_INVALID_PARA;
×
3109
  }
3110

3111
  // Validate string lengths
3112
  size_t idLen = strlen(id);
×
3113
  if (idLen >= TSDB_INSTANCE_ID_LEN) {
×
3114
    tscError("instance id length %zu exceeds limit %d", idLen, TSDB_INSTANCE_ID_LEN - 1);
×
3115
    return terrno = TSDB_CODE_INVALID_PARA;
×
3116
  }
3117

3118
  if (type != NULL && type[0] != 0) {
×
3119
    size_t typeLen = strlen(type);
×
3120
    if (typeLen >= TSDB_INSTANCE_TYPE_LEN) {
×
3121
      tscError("instance type length %zu exceeds limit %d", typeLen, TSDB_INSTANCE_TYPE_LEN - 1);
×
3122
      return terrno = TSDB_CODE_INVALID_PARA;
×
3123
    }
3124
  }
3125

3126
  if (desc != NULL && desc[0] != 0) {
×
3127
    size_t descLen = strlen(desc);
×
3128
    if (descLen >= TSDB_INSTANCE_DESC_LEN) {
×
3129
      tscError("instance desc length %zu exceeds limit %d", descLen, TSDB_INSTANCE_DESC_LEN - 1);
×
3130
      return terrno = TSDB_CODE_INVALID_PARA;
×
3131
    }
3132
  }
3133

3134
  int32_t code = taos_init();
×
3135
  if (code != TSDB_CODE_SUCCESS) {
×
3136
    return code;
×
3137
  }
3138

3139
  SConfig *pCfg = taosGetCfg();
×
3140
  if (pCfg == NULL) {
×
3141
    return terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3142
  }
3143

3144
  SConfigItem *pFirstEpItem = cfgGetItem(pCfg, "firstEp");
×
3145
  if (pFirstEpItem == NULL || pFirstEpItem->str == NULL || pFirstEpItem->str[0] == 0) {
×
3146
    return terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3147
  }
3148

3149
  SEp firstEp = {0};
×
3150
  code = taosGetFqdnPortFromEp(pFirstEpItem->str, &firstEp);
×
3151
  if (code != TSDB_CODE_SUCCESS) {
×
3152
    return terrno = code;
×
3153
  }
3154

3155
  void    *clientRpc = NULL;
×
3156
  SEpSet   epSet = {.inUse = 0, .numOfEps = 1};
×
3157
  SRpcMsg  rpcMsg = {0};
×
3158
  SRpcMsg  rpcRsp = {0};
×
3159
  SRpcInit rpcInit = {0};
×
3160

3161
  rpcInit.label = "INST";
×
3162
  rpcInit.numOfThreads = 1;
×
3163
  rpcInit.cfp = NULL;
×
3164
  rpcInit.sessions = 16;
×
3165
  rpcInit.connType = TAOS_CONN_CLIENT;
×
3166
  rpcInit.idleTime = tsShellActivityTimer * 1000;
×
3167
  rpcInit.compressSize = tsCompressMsgSize;
×
3168
  rpcInit.user = TSDB_DEFAULT_USER;
×
3169

3170
  rpcInit.rfp = instanceRegisterRpcRfp;
×
3171
  rpcInit.retryMinInterval = tsRedirectPeriod;
×
3172
  rpcInit.retryStepFactor = tsRedirectFactor;
×
3173
  rpcInit.retryMaxInterval = tsRedirectMaxPeriod;
×
3174
  rpcInit.retryMaxTimeout =
×
3175
      tsMaxRetryWaitTime;  // Use a special user for instance registration (can be configured for whitelist)
3176

3177
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
×
3178
  connLimitNum = TMAX(connLimitNum, 10);
×
3179
  connLimitNum = TMIN(connLimitNum, 500);
×
3180
  rpcInit.connLimitNum = connLimitNum;
×
3181
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
×
3182
  rpcInit.readTimeout = tsReadTimeout;
×
3183
  rpcInit.ipv6 = tsEnableIpv6;
×
3184
  rpcInit.enableSSL = tsEnableTLS;
×
3185

3186
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
×
3187
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
×
3188
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
×
3189
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
×
3190
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
×
3191

3192
  code = taosVersionStrToInt(td_version, &rpcInit.compatibilityVer);
×
3193
  if (code != TSDB_CODE_SUCCESS) {
×
3194
    tscError("failed to convert taos version from str to int, errcode:%s", terrstr(code));
×
3195
    return code;
×
3196
  }
3197

3198
  clientRpc = rpcOpen(&rpcInit);
×
3199
  if (clientRpc == NULL) {
×
3200
    code = terrno;
×
3201
    tscError("failed to init instance register client since %s", tstrerror(code));
×
3202
    return code;
×
3203
  }
3204

3205
  // Prepare epSet
3206
  tstrncpy(epSet.eps[0].fqdn, firstEp.fqdn, TSDB_FQDN_LEN);
×
3207
  epSet.eps[0].port = firstEp.port;
×
3208

3209
  // Prepare request
3210
  SInstanceRegisterReq req = {0};
×
3211
  tstrncpy(req.id, id, sizeof(req.id));
×
3212
  if (type != NULL && type[0] != 0) {
×
3213
    tstrncpy(req.type, type, sizeof(req.type));
×
3214
  }
3215
  if (desc != NULL && desc[0] != 0) {
×
3216
    tstrncpy(req.desc, desc, sizeof(req.desc));
×
3217
  }
3218
  req.expire = expire;
×
3219

3220
  int32_t contLen = tSerializeSInstanceRegisterReq(NULL, 0, &req);
×
3221
  if (contLen <= 0) {
×
3222
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3223
    rpcClose(clientRpc);
×
3224
    return code;
×
3225
  }
3226

3227
  void *pCont = rpcMallocCont(contLen);
×
3228
  if (pCont == NULL) {
×
3229
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3230
    rpcClose(clientRpc);
×
3231
    return code;
×
3232
  }
3233

3234
  if (tSerializeSInstanceRegisterReq(pCont, contLen, &req) < 0) {
×
3235
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3236
    rpcFreeCont(pCont);
×
3237
    rpcClose(clientRpc);
×
3238
    return code;
×
3239
  }
3240

3241
  rpcMsg.pCont = pCont;
×
3242
  rpcMsg.contLen = contLen;
×
3243
  rpcMsg.msgType = TDMT_MND_REGISTER_INSTANCE;
×
3244
  rpcMsg.info.ahandle = (void *)0x9528;  // Different magic number from server status
×
3245
  rpcMsg.info.notFreeAhandle = 1;
×
3246

3247
  code = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
×
3248
  if (TSDB_CODE_SUCCESS != code) {
×
3249
    tscError("failed to send instance register req since %s", tstrerror(code));
×
3250
    // rpcSendRecv failed, pCont may not be freed, but check _RETURN1 path
3251
    // In error path, rpcSendRecv may free pCont, but we free it here to be safe
3252
    rpcClose(clientRpc);
×
3253
    return code;
×
3254
  }
3255

3256
  if (rpcRsp.code != 0) {
×
3257
    code = rpcRsp.code;
×
3258
    tscError("instance register failed, code:%s", tstrerror(code));
×
3259
  } else {
3260
    code = TSDB_CODE_SUCCESS;
×
3261
  }
3262

3263
  if (rpcRsp.pCont != NULL) {
×
3264
    rpcFreeCont(rpcRsp.pCont);
×
3265
  }
3266
  rpcClose(clientRpc);
×
3267

3268
  terrno = code;
×
3269
  return code;
×
3270
}
3271

3272
int32_t taos_list_instances(const char *filter_type, char ***pList, int32_t *pCount) {
×
3273
  if (pList == NULL || pCount == NULL) {
×
3274
    return TSDB_CODE_INVALID_PARA;
×
3275
  }
3276

3277
  int32_t code = taos_init();
×
3278
  if (code != TSDB_CODE_SUCCESS) {
×
3279
    terrno = code;
×
3280
    return code;
×
3281
  }
3282

3283
  SConfig *pCfg = taosGetCfg();
×
3284
  if (pCfg == NULL) {
×
3285
    terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3286
    return TSDB_CODE_CFG_NOT_FOUND;
×
3287
  }
3288

3289
  SConfigItem *pFirstEpItem = cfgGetItem(pCfg, "firstEp");
×
3290
  if (pFirstEpItem == NULL || pFirstEpItem->str == NULL || pFirstEpItem->str[0] == 0) {
×
3291
    terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3292
    return TSDB_CODE_CFG_NOT_FOUND;
×
3293
  }
3294

3295
  SEp firstEp = {0};
×
3296
  code = taosGetFqdnPortFromEp(pFirstEpItem->str, &firstEp);
×
3297
  if (code != TSDB_CODE_SUCCESS) {
×
3298
    terrno = code;
×
3299
    return code;
×
3300
  }
3301

3302
  // Initialize RPC connection (similar to taos_register_instance)
3303
  void    *clientRpc = NULL;
×
3304
  SEpSet   epSet = {.inUse = 0, .numOfEps = 1};
×
3305
  SRpcMsg  rpcMsg = {0};
×
3306
  SRpcMsg  rpcRsp = {0};
×
3307
  SRpcInit rpcInit = {0};
×
3308

3309
  rpcInit.label = "LIST";
×
3310
  rpcInit.numOfThreads = 1;
×
3311
  rpcInit.cfp = NULL;
×
3312
  rpcInit.sessions = 16;
×
3313
  rpcInit.connType = TAOS_CONN_CLIENT;
×
3314
  rpcInit.idleTime = tsShellActivityTimer * 1000;
×
3315
  rpcInit.compressSize = tsCompressMsgSize;
×
3316
  rpcInit.user = TSDB_DEFAULT_USER;
×
3317

3318
  rpcInit.rfp = instanceRegisterRpcRfp;
×
3319
  rpcInit.retryMinInterval = tsRedirectPeriod;
×
3320
  rpcInit.retryStepFactor = tsRedirectFactor;
×
3321
  rpcInit.retryMaxInterval = tsRedirectMaxPeriod;
×
3322
  rpcInit.retryMaxTimeout =
×
3323
      tsMaxRetryWaitTime;  // Use a special user for instance registration (can be configured for whitelist)
3324

3325
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
×
3326
  connLimitNum = TMAX(connLimitNum, 10);
×
3327
  connLimitNum = TMIN(connLimitNum, 500);
×
3328
  rpcInit.connLimitNum = connLimitNum;
×
3329
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
×
3330
  rpcInit.readTimeout = tsReadTimeout;
×
3331
  rpcInit.ipv6 = tsEnableIpv6;
×
3332
  rpcInit.enableSSL = tsEnableTLS;
×
3333

3334
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
×
3335
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
×
3336
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
×
3337
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
×
3338
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
×
3339

3340
  code = taosVersionStrToInt(td_version, &rpcInit.compatibilityVer);
×
3341
  if (code != TSDB_CODE_SUCCESS) {
×
3342
    tscError("failed to convert taos version from str to int, errcode:%s", terrstr(code));
×
3343
    return code;
×
3344
  }
3345

3346
  clientRpc = rpcOpen(&rpcInit);
×
3347
  if (clientRpc == NULL) {
×
3348
    code = terrno;
×
3349
    tscError("failed to init instance list client since %s", tstrerror(code));
×
3350
    terrno = code;
×
3351
    return code;
×
3352
  }
3353

3354
  tstrncpy(epSet.eps[0].fqdn, firstEp.fqdn, TSDB_FQDN_LEN);
×
3355
  epSet.eps[0].port = firstEp.port;
×
3356
  SInstanceListReq req = {0};
×
3357
  if (filter_type != NULL && filter_type[0] != 0) {
×
3358
    tstrncpy(req.filter_type, filter_type, sizeof(req.filter_type));
×
3359
  }
3360

3361
  // Serialize request to get required length
3362
  int32_t contLen = tSerializeSInstanceListReq(NULL, 0, &req);
×
3363
  if (contLen <= 0) {
×
3364
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3365
    rpcClose(clientRpc);
×
3366
    terrno = code;
×
3367
    return code;
×
3368
  }
3369

3370
  // Allocate RPC message buffer (includes message header overhead)
3371
  void *pCont = rpcMallocCont(contLen);
×
3372
  if (pCont == NULL) {
×
3373
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3374
    rpcClose(clientRpc);
×
3375
    terrno = code;
×
3376
    return code;
×
3377
  }
3378

3379
  // Serialize request into the content part (after message header)
3380
  if (tSerializeSInstanceListReq(pCont, contLen, &req) < 0) {
×
3381
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3382
    rpcFreeCont(pCont);
×
3383
    rpcClose(clientRpc);
×
3384
    terrno = code;
×
3385
    return code;
×
3386
  }
3387

3388
  rpcMsg.pCont = pCont;
×
3389
  rpcMsg.contLen = contLen;
×
3390
  rpcMsg.msgType = TDMT_MND_LIST_INSTANCES;
×
3391
  rpcMsg.info.ahandle = (void *)0x9529;  // Different magic number from register
×
3392
  rpcMsg.info.notFreeAhandle = 1;
×
3393

3394
  code = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
×
3395
  if (TSDB_CODE_SUCCESS != code) {
×
3396
    tscError("failed to send instance list req since %s", tstrerror(code));
×
3397
    rpcFreeCont(pCont);
×
3398
    rpcClose(clientRpc);
×
3399
    terrno = code;
×
3400
    return code;
×
3401
  }
3402

3403
  // Check response - rpcRsp.code contains the result code from mnode
3404
  if (rpcRsp.code != 0) {
×
3405
    code = rpcRsp.code;
×
3406
    tscError("instance list failed, code:%s", tstrerror(code));
×
3407
    if (rpcRsp.pCont != NULL) {
×
3408
      rpcFreeCont(rpcRsp.pCont);
×
3409
    }
3410
    rpcClose(clientRpc);
×
3411
    terrno = code;
×
3412
    return code;
×
3413
  }
3414

3415
  // Deserialize response
3416
  if (rpcRsp.pCont != NULL && rpcRsp.contLen > 0) {
×
3417
    SInstanceListRsp rsp = {0};
×
3418
    code = tDeserializeSInstanceListRsp(rpcRsp.pCont, rpcRsp.contLen, &rsp);
×
3419
    if (code != TSDB_CODE_SUCCESS) {
×
3420
      tscError("failed to deserialize instance list rsp, code:%s", tstrerror(code));
×
3421
      if (rsp.ids != NULL) {
×
3422
        for (int32_t i = 0; i < rsp.count; i++) {
×
3423
          if (rsp.ids[i] != NULL) {
×
3424
            taosMemoryFree(rsp.ids[i]);
×
3425
          }
3426
        }
3427
        taosMemoryFree(rsp.ids);
×
3428
        rsp.ids = NULL;
×
3429
      }
3430
      rsp.count = 0;
×
3431
      rpcFreeCont(rpcRsp.pCont);
×
3432
      rpcClose(clientRpc);
×
3433
      terrno = code;
×
3434
      return code;
×
3435
    }
3436
    *pList = rsp.ids;
×
3437
    *pCount = rsp.count;
×
3438
  } else {
3439
    *pList = NULL;
×
3440
    *pCount = 0;
×
3441
  }
3442

3443
  if (rpcRsp.pCont != NULL) {
×
3444
    rpcFreeCont(rpcRsp.pCont);
×
3445
  }
3446
  rpcClose(clientRpc);
×
3447

3448
  return TSDB_CODE_SUCCESS;
×
3449
}
3450

3451
void taos_free_instances(char ***pList, int32_t count) {
×
3452
  if (pList == NULL || *pList == NULL || count <= 0) {
×
3453
    return;
×
3454
  }
3455

3456
  // Free each string in the array
3457
  for (int32_t i = 0; i < count; i++) {
×
3458
    if ((*pList)[i] != NULL) {
×
3459
      taosMemoryFree((*pList)[i]);
×
3460
      (*pList)[i] = NULL;
×
3461
    }
3462
  }
3463

3464
  // Free the array itself
3465
  taosMemoryFree(*pList);
×
3466
  *pList = NULL;
×
3467
}
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