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

taosdata / TDengine / #4898

26 Dec 2025 09:58AM UTC coverage: 65.061% (-0.7%) from 65.717%
#4898

push

travis-ci

web-flow
feat: support encryption of configuration files, data files and metadata files (#33801)

350 of 1333 new or added lines in 31 files covered. (26.26%)

2796 existing lines in 159 files now uncovered.

184024 of 282850 relevant lines covered (65.06%)

113940470.33 hits per line

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

36.75
/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, ...) {
426,166✔
50
  if (arg == NULL) {
426,166✔
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) {
426,166✔
UNCOV
56
    if (i % 1000 == 0) {
×
UNCOV
57
      (void)sched_yield();
×
58
    }
59
  }
60

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

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

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

254
  monitorClose();
1,138,915✔
255
  tscStopCrashReport();
1,138,915✔
256

257
  hbMgrCleanUp();
1,138,915✔
258

259
  catalogDestroy();
1,138,915✔
260
  schedulerDestroy();
1,138,915✔
261

262
  fmFuncMgtDestroy();
1,138,915✔
263
  qCleanupKeywordsTable();
1,138,915✔
264

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

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

274
  id = clientConnRefPool;
1,138,915✔
275
  clientConnRefPool = -1;
1,138,915✔
276
  taosCloseRef(id);
1,138,915✔
277

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

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

287
  sessMgtDestroy();
1,138,915✔
288

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

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

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

314
TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) {
1,973,663✔
315
  tscInfo("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
1,973,663✔
316
  if (user == NULL) {
1,973,752✔
317
    user = TSDB_DEFAULT_USER;
141,252✔
318
  }
319

320
  if (pass == NULL) {
1,973,752✔
321
    pass = TSDB_DEFAULT_PASS;
141,251✔
322
  }
323

324
  STscObj *pObj = NULL;
1,973,752✔
325
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
1,973,794✔
326
  if (TSDB_CODE_SUCCESS == code) {
1,973,601✔
327
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
1,961,706✔
328
    if (NULL == rid) {
1,961,675✔
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;
1,961,675✔
333
    return (TAOS *)rid;
1,961,706✔
334
  } else {
335
    terrno = code;
11,895✔
336
  }
337

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

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

452
  switch (type) {
1,260✔
453
    case TAOS_NOTIFY_PASSVER: {
360✔
454
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
360✔
455
      pObj->passInfo.fp = fp;
360✔
456
      pObj->passInfo.param = param;
360✔
457
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
360✔
458
      break;
360✔
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: {
900✔
468
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
900✔
469
      pObj->userDroppedInfo.fp = fp;
900✔
470
      pObj->userDroppedInfo.param = param;
900✔
471
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
900✔
472
      break;
900✔
473
    }
474
    case TAOS_NOTIFY_DATETIME_WHITELIST_VER: {
×
475
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
476
      pObj->dateTimeWhiteListInfo.fp = fp;
×
477
      pObj->dateTimeWhiteListInfo.param = param;
×
478
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
479
      break;
×
480
    }
481
    default: {
×
482
      terrno = TSDB_CODE_INVALID_PARA;
×
483
      releaseTscObj(*(int64_t *)taos);
×
484
      return terrno;
×
485
    }
486
  }
487

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

614

615

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

621
  __taos_async_ip_whitelist_fn_t userCbFn;
622
} SFetchIpWhiteListInfo;
623

624

625

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

630
  SGetUserIpWhiteListRsp wlRsp = {0};
×
631

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

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

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

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

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

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

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

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

700

701

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

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

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

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

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

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

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

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

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

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

775

776

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

781

782

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

787

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

794

795

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

801
  SUserDateTimeWhiteList wlRsp = {0};
×
802

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

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

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

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

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

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

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

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

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

861

862

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

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

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

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

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

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

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

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

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

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

935

936

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

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

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

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

958
void taos_close(TAOS *taos) {
1,961,840✔
959
  if (taos == NULL) {
1,961,840✔
960
    return;
181✔
961
  }
962

963
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
1,961,659✔
964
  if (NULL == pObj) {
1,961,727✔
965
    taosMemoryFree(taos);
×
966
    return;
×
967
  }
968

969
  taos_close_internal(pObj);
1,961,727✔
970
  releaseTscObj(*(int64_t *)taos);
1,961,727✔
971
  taosMemoryFree(taos);
1,961,727✔
972
}
973

974
int taos_errno(TAOS_RES *res) {
1,018,197,320✔
975
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,018,197,320✔
976
    return terrno;
15,123✔
977
  }
978

979
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,018,184,172✔
980
    return 0;
297,665✔
981
  }
982

983
  return ((SRequestObj *)res)->code;
1,017,884,540✔
984
}
985

986
const char *taos_errstr(TAOS_RES *res) {
75,295,608✔
987
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
75,295,608✔
988
    if (*(taosGetErrMsg()) == 0) {
15,180✔
989
      return (const char *)tstrerror(terrno);
15,166✔
990
    } else {
991
      (void)snprintf(taosGetErrMsgReturn(), ERR_MSG_LEN, "%s", taosGetErrMsg());
14✔
992
      return (const char*)taosGetErrMsgReturn();
14✔
993
    }
994
  }
995

996
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
75,280,428✔
997
    return "success";
×
998
  }
999

1000
  SRequestObj *pRequest = (SRequestObj *)res;
75,280,428✔
1001
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
75,280,428✔
1002
    return pRequest->msgBuf;
20,920,152✔
1003
  } else {
1004
    return (const char *)tstrerror(pRequest->code);
54,360,276✔
1005
  }
1006
}
1007

1008
void taos_free_result(TAOS_RES *res) {
768,195,667✔
1009
  if (NULL == res) {
768,195,667✔
1010
    return;
6,388,773✔
1011
  }
1012

1013
  tscTrace("res:%p, will be freed", res);
761,806,894✔
1014

1015
  if (TD_RES_QUERY(res)) {
761,806,868✔
1016
    SRequestObj *pRequest = (SRequestObj *)res;
750,505,450✔
1017
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
750,505,450✔
1018
    destroyRequest(pRequest);
750,507,199✔
1019
    return;
750,499,295✔
1020
  }
1021

1022
  SMqRspObj *pRsp = (SMqRspObj *)res;
11,291,909✔
1023
  if (TD_RES_TMQ(res)) {
11,291,909✔
1024
    tDeleteMqDataRsp(&pRsp->dataRsp);
11,241,537✔
1025
    doFreeReqResultInfo(&pRsp->resInfo);
11,240,592✔
1026
  } else if (TD_RES_TMQ_METADATA(res)) {
60,750✔
1027
    tDeleteSTaosxRsp(&pRsp->dataRsp);
2,820✔
1028
    doFreeReqResultInfo(&pRsp->resInfo);
2,820✔
1029
  } else if (TD_RES_TMQ_META(res)) {
57,930✔
1030
    tDeleteMqMetaRsp(&pRsp->metaRsp);
52,348✔
1031
  } else if (TD_RES_TMQ_BATCH_META(res)) {
5,582✔
1032
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
5,582✔
UNCOV
1033
  } else if (TD_RES_TMQ_RAW(res)) {
×
UNCOV
1034
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
1035
  }
1036
  taosMemoryFree(pRsp);
11,300,397✔
1037
}
1038

1039
void taos_kill_query(TAOS *taos) {
318✔
1040
  if (NULL == taos) {
318✔
1041
    return;
181✔
1042
  }
1043

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

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

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

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

1063
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
2,147,483,647✔
1064
  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✔
1065
    return NULL;
3,150,409✔
1066
  }
1067

1068
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
1069
  return pResInfo->userFields;
2,147,483,647✔
1070
}
1071

1072
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
746,807,828✔
1073
TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqid) {
203✔
1074
  return taosQueryImplWithReqid(taos, sql, false, reqid);
203✔
1075
}
1076

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

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

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

1098
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
691,344,172✔
1099
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
691,344,608✔
1100
      return NULL;
322✔
1101
    }
1102

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

1109
    return doAsyncFetchRows(pRequest, true, true);
691,341,687✔
1110
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,147,483,647✔
1111
    SMqRspObj      *msg = ((SMqRspObj *)res);
2,147,483,647✔
1112
    SReqResultInfo *pResultInfo = NULL;
2,147,483,647✔
1113
    if (msg->resIter == -1) {
2,147,483,647✔
1114
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
10,921,353✔
1115
        return NULL;
×
1116
      }
1117
    } else {
1118
      pResultInfo = tmqGetCurResInfo(res);
2,147,483,647✔
1119
    }
1120

1121
    if (pResultInfo->current < pResultInfo->numOfRows) {
2,147,483,647✔
1122
      doSetOneRowPtr(pResultInfo);
2,147,483,647✔
1123
      pResultInfo->current += 1;
2,147,483,647✔
1124
      return pResultInfo->row;
2,147,483,647✔
1125
    } else {
1126
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
93,166,948✔
1127
        return NULL;
10,920,058✔
1128
      }
1129

1130
      doSetOneRowPtr(pResultInfo);
82,244,701✔
1131
      pResultInfo->current += 1;
82,246,890✔
1132
      return pResultInfo->row;
82,246,260✔
1133
    }
1134
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1135
    return NULL;
×
1136
  } else {
1137
    tscError("invalid result passed to taos_fetch_row");
×
1138
    terrno = TSDB_CODE_TMQ_INVALID_DATA;
×
1139
    return NULL;
×
1140
  }
1141
}
1142

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

1153
    if (row[i] == NULL) {
2,147,483,647✔
1154
      len += tsnprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
122,958,572✔
1155
      continue;
122,953,788✔
1156
    }
1157

1158
    switch (fields[i].type) {
2,147,483,647✔
1159
      case TSDB_DATA_TYPE_TINYINT:
9,615,078✔
1160
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
9,615,078✔
1161
        break;
9,620,646✔
1162

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

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

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

1175
      case TSDB_DATA_TYPE_INT:
2,147,483,647✔
1176
        len += tsnprintf(str + len, size - len, "%d", *((int32_t *)row[i]));
2,147,483,647✔
1177
        break;
2,147,483,647✔
1178

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

1183
      case TSDB_DATA_TYPE_BIGINT:
2,147,483,647✔
1184
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
2,147,483,647✔
1185
        break;
2,147,483,647✔
1186

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

1191
      case TSDB_DATA_TYPE_FLOAT: {
19,181,027✔
1192
        float fv = 0;
19,181,027✔
1193
        fv = GET_FLOAT_VAL(row[i]);
19,181,027✔
1194
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
19,185,667✔
1195
      } break;
19,185,091✔
1196

1197
      case TSDB_DATA_TYPE_DOUBLE: {
2,093,029,897✔
1198
        double dv = 0;
2,093,029,897✔
1199
        dv = GET_DOUBLE_VAL(row[i]);
2,093,029,897✔
1200
        len += snprintf(str + len, size - len, "%.*g", DBL_DIG, dv);
2,093,024,767✔
1201
      } break;
2,093,022,611✔
1202

1203
      case TSDB_DATA_TYPE_VARBINARY: {
41,990✔
1204
        void    *data = NULL;
41,990✔
1205
        uint32_t tmp = 0;
41,990✔
1206
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
41,990✔
1207
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
41,990✔
1208
          break;
×
1209
        }
1210
        uint32_t copyLen = TMIN(size - len - 1, tmp);
42,000✔
1211
        (void)memcpy(str + len, data, copyLen);
42,000✔
1212
        len += copyLen;
42,000✔
1213
        taosMemoryFree(data);
42,000✔
1214
      } break;
41,990✔
1215
      case TSDB_DATA_TYPE_BINARY:
2,147,483,647✔
1216
      case TSDB_DATA_TYPE_NCHAR:
1217
      case TSDB_DATA_TYPE_GEOMETRY: {
1218
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
2,147,483,647✔
1219
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
2,147,483,647✔
1220
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
1,652,910,688✔
1221
          if (charLen > fields[i].bytes || charLen < 0) {
2,147,483,647✔
1222
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
3,487✔
1223
            break;
×
1224
          }
1225
        } else {
1226
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
1,652,869,060✔
1227
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
664✔
1228
            break;
×
1229
          }
1230
        }
1231

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

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

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

1252
      case TSDB_DATA_TYPE_TIMESTAMP:
2,147,483,647✔
1253
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
2,147,483,647✔
1254
        break;
2,147,483,647✔
1255

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

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

1278
  return len;
2,147,483,647✔
1279
}
1280

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

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

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

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

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

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

1355
const char *taos_get_client_info() { return td_version; }
752,214✔
1356

1357
// return int32_t
1358
int taos_affected_rows(TAOS_RES *res) {
530,914,140✔
1359
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
530,914,140✔
1360
      TD_RES_TMQ_BATCH_META(res)) {
530,914,532✔
1361
    return 0;
×
1362
  }
1363

1364
  SRequestObj    *pRequest = (SRequestObj *)res;
530,914,607✔
1365
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
530,914,607✔
1366
  return (int)pResInfo->numOfRows;
530,914,472✔
1367
}
1368

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

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

1381
int taos_result_precision(TAOS_RES *res) {
2,147,483,647✔
1382
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1383
    return TSDB_TIME_PRECISION_MILLI;
×
1384
  }
1385

1386
  if (TD_RES_QUERY(res)) {
2,147,483,647✔
1387
    SRequestObj *pRequest = (SRequestObj *)res;
121,292,584✔
1388
    return pRequest->body.resInfo.precision;
121,292,584✔
1389
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,147,483,647✔
1390
    SReqResultInfo *info = tmqGetCurResInfo(res);
2,147,483,647✔
1391
    return info->precision;
2,147,483,647✔
1392
  }
1393
  return TSDB_TIME_PRECISION_MILLI;
×
1394
}
1395

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

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

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

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

1417
  taos_free_result(pRequest);
34,367✔
1418
  releaseTscObj(*(int64_t *)taos);
34,380✔
1419
  return code;
34,380✔
1420
}
1421

1422
void taos_stop_query(TAOS_RES *res) {
753,449,605✔
1423
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
753,449,605✔
1424
      TD_RES_TMQ_BATCH_META(res)) {
753,453,289✔
1425
    return;
×
1426
  }
1427

1428
  stopAllQueries((SRequestObj *)res);
753,451,768✔
1429
}
1430

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

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

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

1450
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
205,357,178✔
1451
  int32_t numOfRows = 0;
205,357,178✔
1452
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
205,357,178✔
1453
  return numOfRows;
205,357,178✔
1454
}
1455

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

1461
  if (TD_RES_QUERY(res)) {
205,357,178✔
1462
    SRequestObj *pRequest = (SRequestObj *)res;
196,280,664✔
1463

1464
    (*rows) = NULL;
196,280,664✔
1465
    (*numOfRows) = 0;
196,280,664✔
1466

1467
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
196,280,664✔
1468
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
195,985,451✔
1469
      return pRequest->code;
1,247,103✔
1470
    }
1471

1472
    (void)doAsyncFetchRows(pRequest, false, true);
195,033,561✔
1473

1474
    // TODO refactor
1475
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
195,030,215✔
1476
    pResultInfo->current = pResultInfo->numOfRows;
195,033,561✔
1477

1478
    (*rows) = pResultInfo->row;
195,033,561✔
1479
    (*numOfRows) = pResultInfo->numOfRows;
195,033,561✔
1480
    return pRequest->code;
195,033,561✔
1481
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
9,076,514✔
1482
    SReqResultInfo *pResultInfo = NULL;
9,076,514✔
1483
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
9,076,514✔
1484
    if (code != 0) return code;
9,076,514✔
1485

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

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

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

1504
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
36,528✔
1505
    SReqResultInfo *pResultInfo = NULL;
33,801✔
1506
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
33,801✔
1507
    if (code != 0) {
33,801✔
1508
      (*numOfRows) = 0;
663✔
1509
      return 0;
663✔
1510
    }
1511

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

1518
  SRequestObj *pRequest = (SRequestObj *)res;
2,727✔
1519

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

1525
  (void)doAsyncFetchRows(pRequest, false, false);
2,727✔
1526

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

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

1533
  return pRequest->code;
2,727✔
1534
}
1535

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

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

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

1552
  return pResInfo->pCol[columnIndex].offset;
126,838,670✔
1553
}
1554

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

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

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

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

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

1596
  int code = taos_errno(pObj);
×
1597

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

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

1609
  resetConnectDB(pTscObj);
×
1610

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

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

1621
  releaseTscObj(*(int64_t *)taos);
4,959✔
1622

1623
  return pTscObj->sDetailVer;
4,959✔
1624
}
1625

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

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

1651
// buffer is allocated by caller, len is in/out parameter, input is buffer length, output is actual length.
1652
// because this is a general purpose api, buffer is not null-terminated string even for string info, and
1653
// the return length is the actual length of the info, not including null-terminator.
1654
int taos_get_connection_info(TAOS *taos, TSDB_CONNECTION_INFO info, char* buffer, int* len) {
×
1655
  if (len == NULL) {
×
1656
    return TSDB_CODE_INVALID_PARA;
×
1657
  }
1658

1659
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
1660
  if (pTscObj == NULL) {
×
1661
    return TSDB_CODE_TSC_DISCONNECTED;
×
1662
  }
1663

1664
  int code = TSDB_CODE_SUCCESS;
×
1665
  (void)taosThreadMutexLock(&pTscObj->mutex);
×
1666

1667
  switch (info) {
×
1668
    case TSDB_CONNECTION_INFO_USER: {
×
1669
      int userLen = strlen(pTscObj->user);
×
1670
      if (buffer == NULL || *len < userLen) {
×
1671
        *len = userLen;
×
1672
        TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1673
      } else {
1674
        *len = userLen;
×
1675
        (void)memcpy(buffer, pTscObj->user, userLen);
×
1676
      }
1677
      break;
×
1678
    }
1679

1680
    case TSDB_CONNECTION_INFO_TOKEN: {
×
1681
      int tokenLen = strlen(pTscObj->tokenName);
×
1682
      if (tokenLen == 0) {
×
1683
        *len = 0;
×
1684
      } else if (buffer == NULL || *len < tokenLen) {
×
1685
        *len = tokenLen;
×
1686
        TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1687
      } else {
1688
        *len = tokenLen;
×
1689
        (void)memcpy(buffer, pTscObj->tokenName, tokenLen);
×
1690
      }
1691
      break;
×
1692
    }
1693

1694
    default:
×
1695
        TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1696
  }
1697

1698
_return:
×
1699
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
×
1700
  releaseTscObj(*(int64_t *)taos);
×
1701
  return code;
×
1702
}
1703

1704
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
1,504,168,781✔
1705
  if (NULL == pWrapper) {
1,504,168,781✔
1706
    return;
755,331,414✔
1707
  }
1708
  destoryCatalogReq(pWrapper->pCatalogReq);
748,837,367✔
1709
  taosMemoryFree(pWrapper->pCatalogReq);
748,833,850✔
1710
  qDestroyParseContext(pWrapper->pParseCtx);
748,827,373✔
1711
  taosMemoryFree(pWrapper);
748,822,175✔
1712
}
1713

1714
void destroyCtxInRequest(SRequestObj *pRequest) {
2,181,405✔
1715
  schedulerFreeJob(&pRequest->body.queryJob, 0);
2,181,405✔
1716
  qDestroyQuery(pRequest->pQuery);
2,181,405✔
1717
  pRequest->pQuery = NULL;
2,181,405✔
1718
  destorySqlCallbackWrapper(pRequest->pWrapper);
2,181,405✔
1719
  pRequest->pWrapper = NULL;
2,181,405✔
1720
}
2,181,405✔
1721

1722
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
257,171,689✔
1723
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
257,171,689✔
1724
  SRequestObj         *pRequest = pWrapper->pRequest;
257,171,689✔
1725
  SQuery              *pQuery = pRequest->pQuery;
257,171,693✔
1726

1727
  qDebug("req:0x%" PRIx64 ", start to semantic analysis, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
257,172,108✔
1728

1729
  int64_t analyseStart = taosGetTimestampUs();
257,172,102✔
1730
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
257,172,102✔
1731
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
257,171,683✔
1732

1733
  if (TSDB_CODE_SUCCESS == code) {
257,171,684✔
1734
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
257,169,922✔
1735
  }
1736

1737
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
257,162,977✔
1738

1739
  if (pRequest->parseOnly) {
257,166,888✔
1740
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
226,235✔
1741
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
226,235✔
1742
  }
1743

1744
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
257,167,239✔
1745
}
257,156,969✔
1746

1747
int32_t cloneCatalogReq(SCatalogReq **ppTarget, SCatalogReq *pSrc) {
×
1748
  int32_t      code = TSDB_CODE_SUCCESS;
×
1749
  SCatalogReq *pTarget = taosMemoryCalloc(1, sizeof(SCatalogReq));
×
1750
  if (pTarget == NULL) {
×
1751
    code = terrno;
×
1752
  } else {
1753
    pTarget->pDbVgroup = taosArrayDup(pSrc->pDbVgroup, NULL);
×
1754
    pTarget->pDbCfg = taosArrayDup(pSrc->pDbCfg, NULL);
×
1755
    pTarget->pDbInfo = taosArrayDup(pSrc->pDbInfo, NULL);
×
1756
    pTarget->pTableMeta = taosArrayDup(pSrc->pTableMeta, NULL);
×
1757
    pTarget->pTableHash = taosArrayDup(pSrc->pTableHash, NULL);
×
1758
    pTarget->pUdf = taosArrayDup(pSrc->pUdf, NULL);
×
1759
    pTarget->pIndex = taosArrayDup(pSrc->pIndex, NULL);
×
1760
    pTarget->pUser = taosArrayDup(pSrc->pUser, NULL);
×
1761
    pTarget->pTableIndex = taosArrayDup(pSrc->pTableIndex, NULL);
×
1762
    pTarget->pTableCfg = taosArrayDup(pSrc->pTableCfg, NULL);
×
1763
    pTarget->pTableTag = taosArrayDup(pSrc->pTableTag, NULL);
×
1764
    pTarget->pView = taosArrayDup(pSrc->pView, NULL);
×
1765
    pTarget->pTableTSMAs = taosArrayDup(pSrc->pTableTSMAs, NULL);
×
1766
    pTarget->pTSMAs = taosArrayDup(pSrc->pTSMAs, NULL);
×
1767
    pTarget->pVStbRefDbs = taosArrayDup(pSrc->pVStbRefDbs, NULL);
×
1768
    pTarget->qNodeRequired = pSrc->qNodeRequired;
×
1769
    pTarget->dNodeRequired = pSrc->dNodeRequired;
×
1770
    pTarget->svrVerRequired = pSrc->svrVerRequired;
×
1771
    pTarget->forceUpdate = pSrc->forceUpdate;
×
1772
    pTarget->cloned = true;
×
1773

1774
    *ppTarget = pTarget;
×
1775
  }
1776

1777
  return code;
×
1778
}
1779

1780
void handleSubQueryFromAnalyse(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, SNode *pRoot) {
×
1781
  SRequestObj         *pNewRequest = NULL;
×
1782
  SSqlCallbackWrapper *pNewWrapper = NULL;
×
1783
  int32_t              code = buildPreviousRequest(pWrapper->pRequest, pWrapper->pRequest->sqlstr, &pNewRequest);
×
1784
  if (code) {
×
1785
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1786
    return;
×
1787
  }
1788

1789
  pNewRequest->pQuery = NULL;
×
1790
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode **)&pNewRequest->pQuery);
×
1791
  if (pNewRequest->pQuery) {
×
1792
    pNewRequest->pQuery->pRoot = pRoot;
×
1793
    pRoot = NULL;
×
1794
    pNewRequest->pQuery->execStage = QUERY_EXEC_STAGE_ANALYSE;
×
1795
  }
1796
  if (TSDB_CODE_SUCCESS == code) {
×
1797
    code = prepareAndParseSqlSyntax(&pNewWrapper, pNewRequest, false);
×
1798
  }
1799
  if (TSDB_CODE_SUCCESS == code) {
×
1800
    code = cloneCatalogReq(&pNewWrapper->pCatalogReq, pWrapper->pCatalogReq);
×
1801
  }
1802
  if (TSDB_CODE_SUCCESS == code) {
×
1803
    doAsyncQueryFromAnalyse(pResultMeta, pNewWrapper, code);
×
1804
    nodesDestroyNode(pRoot);
×
1805
  } else {
1806
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1807
    return;
×
1808
  }
1809
}
1810

1811
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
257,142,204✔
1812
  SRequestObj *pRequest = pWrapper->pRequest;
257,142,204✔
1813
  SQuery      *pQuery = pRequest->pQuery;
257,150,454✔
1814

1815
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
257,158,769✔
1816
    SNode *prevRoot = pQuery->pPrevRoot;
×
1817
    pQuery->pPrevRoot = NULL;
×
1818
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
×
1819
    return;
×
1820
  }
1821

1822
  if (code == TSDB_CODE_SUCCESS) {
257,157,738✔
1823
    pRequest->stableQuery = pQuery->stableQuery;
204,041,779✔
1824
    if (pQuery->pRoot) {
204,043,057✔
1825
      pRequest->stmtType = pQuery->pRoot->type;
204,050,888✔
1826
    }
1827

1828
    if (pQuery->haveResultSet) {
204,020,781✔
1829
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
124,159,556✔
1830
                              pRequest->stmtBindVersion > 0);
124,159,634✔
1831
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
124,160,395✔
1832
    }
1833
  }
1834

1835
  if (code == TSDB_CODE_SUCCESS) {
257,143,550✔
1836
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
204,040,784✔
1837
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
204,029,042✔
1838
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
204,036,935✔
1839

1840
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
204,035,712✔
1841
  } else {
1842
    destorySqlCallbackWrapper(pWrapper);
53,102,766✔
1843
    pRequest->pWrapper = NULL;
53,102,766✔
1844
    qDestroyQuery(pRequest->pQuery);
53,102,766✔
1845
    pRequest->pQuery = NULL;
53,102,288✔
1846

1847
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
53,102,288✔
1848
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
2,103,640✔
1849
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1850
      restartAsyncQuery(pRequest, code);
2,103,640✔
1851
      return;
2,103,640✔
1852
    }
1853

1854
    // return to app directly
1855
    tscError("req:0x%" PRIx64 ", error occurs, code:%s, return to user app, QID:0x%" PRIx64, pRequest->self,
50,999,126✔
1856
             tstrerror(code), pRequest->requestId);
1857
    pRequest->code = code;
50,999,126✔
1858
    returnToUser(pRequest);
50,999,126✔
1859
  }
1860
}
1861

1862
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
263,251,214✔
1863
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
525,821,801✔
1864
                           .requestId = pWrapper->pParseCtx->requestId,
263,261,972✔
1865
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
263,250,167✔
1866
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
263,253,870✔
1867

1868
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
525,835,699✔
1869

1870
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
263,931,978✔
1871
                                &pWrapper->pRequest->body.queryJob);
263,262,090✔
1872
}
1873

1874
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code);
1875

1876
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
745,327,121✔
1877
  int32_t code = TSDB_CODE_SUCCESS;
745,327,121✔
1878
  switch (pWrapper->pRequest->pQuery->execStage) {
745,327,121✔
1879
    case QUERY_EXEC_STAGE_PARSE: {
6,108,795✔
1880
      // continue parse after get metadata
1881
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
6,108,795✔
1882
      break;
6,108,795✔
1883
    }
1884
    case QUERY_EXEC_STAGE_ANALYSE: {
257,154,815✔
1885
      // analysis after get metadata
1886
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
257,154,815✔
1887
      break;
257,157,220✔
1888
    }
1889
    case QUERY_EXEC_STAGE_SCHEDULE: {
482,077,043✔
1890
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
482,077,043✔
1891
      break;
482,077,098✔
1892
    }
1893
    default:
×
1894
      break;
×
1895
  }
1896
  return code;
745,343,093✔
1897
}
1898

1899
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code) {
6,108,795✔
1900
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
6,108,795✔
1901
  SRequestObj         *pRequest = pWrapper->pRequest;
6,108,795✔
1902
  SQuery              *pQuery = pRequest->pQuery;
6,108,795✔
1903

1904
  pRequest->metric.ctgCostUs += taosGetTimestampUs() - pRequest->metric.ctgStart;
6,108,795✔
1905
  qDebug("req:0x%" PRIx64 ", continue parse query, QID:0x%" PRIx64 ", code:%s", pRequest->self, pRequest->requestId,
6,108,795✔
1906
         tstrerror(code));
1907

1908
  if (code == TSDB_CODE_SUCCESS) {
6,108,795✔
1909
    // pWrapper->pCatalogReq->forceUpdate = false;
1910
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
6,076,004✔
1911
  }
1912

1913
  if (TSDB_CODE_SUCCESS == code) {
6,108,795✔
1914
    code = phaseAsyncQuery(pWrapper);
5,577,641✔
1915
  }
1916

1917
  if (TSDB_CODE_SUCCESS != code) {
6,108,795✔
1918
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
531,154✔
1919
             tstrerror(code), pWrapper->pRequest->requestId);
1920
    destorySqlCallbackWrapper(pWrapper);
531,154✔
1921
    pRequest->pWrapper = NULL;
531,154✔
1922
    terrno = code;
531,154✔
1923
    pRequest->code = code;
531,154✔
1924
    doRequestCallback(pRequest, code);
531,154✔
1925
  }
1926
}
6,108,795✔
1927

1928
void continueInsertFromCsv(SSqlCallbackWrapper *pWrapper, SRequestObj *pRequest) {
10,889✔
1929
  int32_t code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
10,889✔
1930
  if (TSDB_CODE_SUCCESS == code) {
10,889✔
1931
    code = phaseAsyncQuery(pWrapper);
10,889✔
1932
  }
1933

1934
  if (TSDB_CODE_SUCCESS != code) {
10,889✔
1935
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
×
1936
             tstrerror(code), pWrapper->pRequest->requestId);
1937
    destorySqlCallbackWrapper(pWrapper);
×
1938
    pRequest->pWrapper = NULL;
×
1939
    terrno = code;
×
1940
    pRequest->code = code;
×
1941
    doRequestCallback(pRequest, code);
×
1942
  }
1943
}
10,889✔
1944

1945
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
117,102✔
1946
  int64_t connId = *(int64_t *)taos;
117,102✔
1947
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
117,102✔
1948
}
117,102✔
1949

1950
void taos_query_a_with_reqid(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param, int64_t reqid) {
×
1951
  int64_t connId = *(int64_t *)taos;
×
1952
  taosAsyncQueryImplWithReqid(connId, sql, fp, param, false, reqid);
×
1953
}
×
1954

1955
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
748,829,415✔
1956
  const STscObj *pTscObj = pRequest->pTscObj;
748,829,415✔
1957

1958
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
748,838,804✔
1959
  if (*pCxt == NULL) {
748,823,595✔
1960
    return terrno;
×
1961
  }
1962

1963
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
1,494,535,703✔
1964
                           .requestRid = pRequest->self,
748,830,272✔
1965
                           .acctId = pTscObj->acctId,
748,834,254✔
1966
                           .db = pRequest->pDb,
748,835,882✔
1967
                           .topicQuery = false,
1968
                           .pSql = pRequest->sqlstr,
748,836,899✔
1969
                           .sqlLen = pRequest->sqlLen,
748,835,835✔
1970
                           .pMsg = pRequest->msgBuf,
748,834,040✔
1971
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1972
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
748,835,838✔
1973
                           .pStmtCb = NULL,
1974
                           .pUser = pTscObj->user,
748,834,821✔
1975
                           .pEffectiveUser = pRequest->effectiveUser,
748,831,309✔
1976
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
748,836,559✔
1977
                           .enableSysInfo = pTscObj->sysInfo,
748,834,276✔
1978
                           .async = true,
1979
                           .svrVer = pTscObj->sVer,
748,830,894✔
1980
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
748,839,309✔
1981
                           .allocatorId = pRequest->allocatorRefId,
748,825,668✔
1982
                           .parseSqlFp = clientParseSql,
1983
                           .parseSqlParam = pWrapper,
1984
                           .setQueryFp = setQueryRequest,
1985
                           .timezone = pTscObj->optionInfo.timezone,
748,836,043✔
1986
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
748,831,750✔
1987
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
748,835,788✔
1988
  (*pCxt)->biMode = biMode;
748,827,616✔
1989
  return TSDB_CODE_SUCCESS;
748,828,131✔
1990
}
1991

1992
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
748,825,552✔
1993
  int32_t              code = TSDB_CODE_SUCCESS;
748,825,552✔
1994
  STscObj             *pTscObj = pRequest->pTscObj;
748,825,552✔
1995
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
748,838,576✔
1996
  if (pWrapper == NULL) {
748,827,223✔
1997
    code = terrno;
×
1998
  } else {
1999
    pWrapper->pRequest = pRequest;
748,827,223✔
2000
    pRequest->pWrapper = pWrapper;
748,828,234✔
2001
    *ppWrapper = pWrapper;
748,832,207✔
2002
  }
2003

2004
  if (TSDB_CODE_SUCCESS == code) {
748,831,510✔
2005
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
748,832,923✔
2006
  }
2007

2008
  if (TSDB_CODE_SUCCESS == code) {
748,827,987✔
2009
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
748,827,997✔
2010
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
748,841,990✔
2011
  }
2012

2013
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
748,832,553✔
2014
    int64_t syntaxStart = taosGetTimestampUs();
748,838,503✔
2015

2016
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
748,838,503✔
2017
    if (pWrapper->pCatalogReq == NULL) {
748,820,759✔
2018
      code = terrno;
×
2019
    } else {
2020
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
748,820,419✔
2021
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
748,830,016✔
2022
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
748,825,338✔
2023
    }
2024

2025
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
748,828,344✔
2026
  }
2027

2028
  return code;
748,840,142✔
2029
}
2030

2031
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
749,531,152✔
2032
  SSqlCallbackWrapper *pWrapper = NULL;
749,531,152✔
2033
  int32_t              code = TSDB_CODE_SUCCESS;
749,537,832✔
2034

2035
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
749,537,832✔
2036
    code = pRequest->prevCode;
702,278✔
2037
    terrno = code;
702,278✔
2038
    pRequest->code = code;
702,278✔
2039
    tscDebug("req:0x%" PRIx64 ", call sync query cb with code:%s", pRequest->self, tstrerror(code));
702,278✔
2040
    doRequestCallback(pRequest, code);
702,278✔
2041
    return;
702,278✔
2042
  }
2043

2044
  if (TSDB_CODE_SUCCESS == code) {
748,827,541✔
2045
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
748,826,414✔
2046
  }
2047

2048
  if (TSDB_CODE_SUCCESS == code) {
748,823,758✔
2049
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
739,756,417✔
2050
    code = phaseAsyncQuery(pWrapper);
739,761,440✔
2051
  }
2052

2053
  if (TSDB_CODE_SUCCESS != code) {
748,831,483✔
2054
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
9,072,285✔
2055
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
9,006,913✔
2056
               pRequest->requestId);
2057
    } else {
2058
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
65,372✔
2059
               pRequest->requestId);
2060
    }
2061

2062
    destorySqlCallbackWrapper(pWrapper);
9,072,285✔
2063
    pRequest->pWrapper = NULL;
9,072,285✔
2064
    qDestroyQuery(pRequest->pQuery);
9,072,285✔
2065
    pRequest->pQuery = NULL;
9,072,285✔
2066

2067
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
9,072,285✔
2068
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
10,015✔
2069
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
2070
      code = refreshMeta(pRequest->pTscObj, pRequest);
10,015✔
2071
      if (code != 0) {
10,015✔
2072
        tscWarn("req:0x%" PRIx64 ", refresh meta failed, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code,
10,015✔
2073
                tstrerror(code), pRequest->requestId);
2074
      }
2075
      pRequest->prevCode = code;
10,015✔
2076
      doAsyncQuery(pRequest, true);
10,015✔
2077
      return;
10,015✔
2078
    }
2079

2080
    terrno = code;
9,062,270✔
2081
    pRequest->code = code;
9,062,270✔
2082
    doRequestCallback(pRequest, code);
9,062,270✔
2083
  }
2084
}
2085

2086
void restartAsyncQuery(SRequestObj *pRequest, int32_t code) {
2,181,405✔
2087
  tscInfo("restart request:%s p:%p", pRequest->sqlstr, pRequest);
2,181,405✔
2088
  SRequestObj *pUserReq = pRequest;
2,181,405✔
2089
  (void)acquireRequest(pRequest->self);
2,181,405✔
2090
  while (pUserReq) {
2,181,405✔
2091
    if (pUserReq->self == pUserReq->relation.userRefId || pUserReq->relation.userRefId == 0) {
2,181,405✔
2092
      break;
2093
    } else {
2094
      int64_t nextRefId = pUserReq->relation.nextRefId;
×
2095
      (void)releaseRequest(pUserReq->self);
×
2096
      if (nextRefId) {
×
2097
        pUserReq = acquireRequest(nextRefId);
×
2098
      }
2099
    }
2100
  }
2101
  bool hasSubRequest = pUserReq != pRequest || pRequest->relation.prevRefId != 0;
2,181,405✔
2102
  if (pUserReq) {
2,181,405✔
2103
    destroyCtxInRequest(pUserReq);
2,181,405✔
2104
    pUserReq->prevCode = code;
2,181,405✔
2105
    (void)memset(&pUserReq->relation, 0, sizeof(pUserReq->relation));
2,181,405✔
2106
  } else {
UNCOV
2107
    tscError("User req is missing");
×
UNCOV
2108
    (void)removeFromMostPrevReq(pRequest);
×
2109
    return;
×
2110
  }
2111
  if (hasSubRequest)
2,181,405✔
2112
    (void)removeFromMostPrevReq(pRequest);
×
2113
  else
2114
    (void)releaseRequest(pUserReq->self);
2,181,405✔
2115
  doAsyncQuery(pUserReq, true);
2,181,405✔
2116
}
2117

2118
typedef struct SAsyncFetchParam {
2119
  SRequestObj      *pReq;
2120
  __taos_async_fn_t fp;
2121
  void             *param;
2122
} SAsyncFetchParam;
2123

2124
static int32_t doAsyncFetch(void *pParam) {
138,066,522✔
2125
  SAsyncFetchParam *param = pParam;
138,066,522✔
2126
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
138,066,522✔
2127
  taosMemoryFree(param);
138,066,518✔
2128
  return TSDB_CODE_SUCCESS;
138,066,472✔
2129
}
2130

2131
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
138,068,982✔
2132
  if (res == NULL || fp == NULL) {
138,068,982✔
2133
    tscError("taos_fetch_rows_a invalid paras");
×
2134
    return;
×
2135
  }
2136
  if (!TD_RES_QUERY(res)) {
138,068,987✔
2137
    tscError("taos_fetch_rows_a res is NULL");
×
2138
    fp(param, res, TSDB_CODE_APP_ERROR);
×
2139
    return;
×
2140
  }
2141

2142
  SRequestObj *pRequest = res;
138,068,956✔
2143
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
138,068,956✔
2144
    fp(param, res, 0);
2,470✔
2145
    return;
2,470✔
2146
  }
2147

2148
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
138,066,512✔
2149
  if (!pParam) {
138,066,508✔
2150
    fp(param, res, terrno);
×
2151
    return;
×
2152
  }
2153
  pParam->pReq = pRequest;
138,066,508✔
2154
  pParam->fp = fp;
138,066,512✔
2155
  pParam->param = param;
138,066,517✔
2156
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
138,066,503✔
2157
  if (TSDB_CODE_SUCCESS != code) {
138,066,522✔
2158
    taosMemoryFree(pParam);
×
2159
    fp(param, res, code);
×
2160
    return;
×
2161
  }
2162
}
2163

2164
void taos_fetch_raw_block_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
89✔
2165
  if (res == NULL || fp == NULL) {
89✔
2166
    tscError("taos_fetch_raw_block_a invalid paras");
×
2167
    return;
×
2168
  }
2169
  if (!TD_RES_QUERY(res)) {
89✔
2170
    tscError("taos_fetch_raw_block_a res is NULL");
×
2171
    return;
×
2172
  }
2173
  SRequestObj    *pRequest = res;
89✔
2174
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
89✔
2175

2176
  // set the current block is all consumed
2177
  pResultInfo->convertUcs4 = false;
89✔
2178

2179
  // it is a local executed query, no need to do async fetch
2180
  taos_fetch_rows_a(pRequest, fp, param);
89✔
2181
}
2182

2183
const void *taos_get_raw_block(TAOS_RES *res) {
52✔
2184
  if (res == NULL) {
52✔
2185
    tscError("taos_get_raw_block invalid paras");
×
2186
    return NULL;
×
2187
  }
2188
  if (!TD_RES_QUERY(res)) {
52✔
2189
    tscError("taos_get_raw_block res is NULL");
×
2190
    return NULL;
×
2191
  }
2192
  SRequestObj *pRequest = res;
52✔
2193

2194
  return pRequest->body.resInfo.pData;
52✔
2195
}
2196

2197
int taos_get_db_route_info(TAOS *taos, const char *db, TAOS_DB_ROUTE_INFO *dbInfo) {
×
2198
  if (NULL == taos) {
×
2199
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2200
    return terrno;
×
2201
  }
2202

2203
  if (NULL == db || NULL == dbInfo) {
×
2204
    tscError("invalid input param, db:%p, dbInfo:%p", db, dbInfo);
×
2205
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2206
    return terrno;
×
2207
  }
2208

2209
  int64_t      connId = *(int64_t *)taos;
×
2210
  SRequestObj *pRequest = NULL;
×
2211
  char        *sql = "taos_get_db_route_info";
×
2212
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2213
  if (code != TSDB_CODE_SUCCESS) {
×
2214
    terrno = code;
×
2215
    return terrno;
×
2216
  }
2217

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

2225
  SRequestConnInfo conn = {
×
2226
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2227

2228
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2229

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

2233
  code = catalogGetDBVgInfo(pCtg, &conn, dbFName, dbInfo);
×
2234
  if (code) {
×
2235
    goto _return;
×
2236
  }
2237

2238
_return:
×
2239

2240
  terrno = code;
×
2241

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

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

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

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

2266
  pRequest->syncQuery = true;
×
2267

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

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

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

2280
  SName tableName = {0};
×
2281
  toName(pTscObj->acctId, db, table, &tableName);
×
2282

2283
  SVgroupInfo vgInfo;
×
2284
  code = catalogGetTableHashVgroup(pCtg, &conn, &tableName, &vgInfo);
×
2285
  if (code) {
×
2286
    goto _return;
×
2287
  }
2288

2289
  *vgId = vgInfo.vgId;
×
2290

2291
_return:
×
2292

2293
  terrno = code;
×
2294

2295
  destroyRequest(pRequest);
×
2296
  return code;
×
2297
}
2298

2299
int taos_get_tables_vgId(TAOS *taos, const char *db, const char *table[], int tableNum, int *vgId) {
×
2300
  if (NULL == taos) {
×
2301
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2302
    return terrno;
×
2303
  }
2304

2305
  if (NULL == db || NULL == table || NULL == vgId || tableNum <= 0) {
×
2306
    tscError("invalid input param, db:%p, table:%p, vgId:%p, tbNum:%d", db, table, vgId, tableNum);
×
2307
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2308
    return terrno;
×
2309
  }
2310

2311
  int64_t      connId = *(int64_t *)taos;
×
2312
  SRequestObj *pRequest = NULL;
×
2313
  char        *sql = "taos_get_table_vgId";
×
2314
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2315
  if (code != TSDB_CODE_SUCCESS) {
×
2316
    return terrno;
×
2317
  }
2318

2319
  pRequest->syncQuery = true;
×
2320

2321
  STscObj  *pTscObj = pRequest->pTscObj;
×
2322
  SCatalog *pCtg = NULL;
×
2323
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2324
  if (code != TSDB_CODE_SUCCESS) {
×
2325
    goto _return;
×
2326
  }
2327

2328
  SRequestConnInfo conn = {
×
2329
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2330

2331
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2332

2333
  code = catalogGetTablesHashVgId(pCtg, &conn, pTscObj->acctId, db, table, tableNum, vgId);
×
2334
  if (code) {
×
2335
    goto _return;
×
2336
  }
2337

2338
_return:
×
2339

2340
  terrno = code;
×
2341

2342
  destroyRequest(pRequest);
×
2343
  return code;
×
2344
}
2345

2346
int taos_load_table_info(TAOS *taos, const char *tableNameList) {
1,102✔
2347
  if (NULL == taos) {
1,102✔
2348
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2349
    return terrno;
×
2350
  }
2351

2352
  int64_t       connId = *(int64_t *)taos;
1,102✔
2353
  const int32_t MAX_TABLE_NAME_LENGTH = 12 * 1024 * 1024;  // 12MB list
1,102✔
2354
  int32_t       code = 0;
1,102✔
2355
  SRequestObj  *pRequest = NULL;
1,102✔
2356
  SCatalogReq   catalogReq = {0};
1,102✔
2357

2358
  if (NULL == tableNameList) {
1,102✔
2359
    return TSDB_CODE_SUCCESS;
×
2360
  }
2361

2362
  int32_t length = (int32_t)strlen(tableNameList);
1,102✔
2363
  if (0 == length) {
1,102✔
2364
    return TSDB_CODE_SUCCESS;
×
2365
  } else if (length > MAX_TABLE_NAME_LENGTH) {
1,102✔
2366
    tscError("tableNameList too long, length:%d, maximum allowed:%d", length, MAX_TABLE_NAME_LENGTH);
×
2367
    return TSDB_CODE_TSC_INVALID_OPERATION;
×
2368
  }
2369

2370
  char *sql = "taos_load_table_info";
1,102✔
2371
  code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
1,102✔
2372
  if (code != TSDB_CODE_SUCCESS) {
1,102✔
2373
    terrno = code;
×
2374
    goto _return;
×
2375
  }
2376

2377
  pRequest->syncQuery = true;
1,102✔
2378

2379
  STscObj *pTscObj = pRequest->pTscObj;
1,102✔
2380
  code = transferTableNameList(tableNameList, pTscObj->acctId, pTscObj->db, &catalogReq.pTableMeta);
1,102✔
2381
  if (code) {
1,102✔
2382
    goto _return;
×
2383
  }
2384

2385
  SCatalog *pCtg = NULL;
1,102✔
2386
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
1,102✔
2387
  if (code != TSDB_CODE_SUCCESS) {
1,102✔
2388
    goto _return;
×
2389
  }
2390

2391
  SRequestConnInfo conn = {
1,102✔
2392
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
1,102✔
2393

2394
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
1,102✔
2395

2396
  code = catalogAsyncGetAllMeta(pCtg, &conn, &catalogReq, syncCatalogFn, pRequest->body.interParam, NULL);
1,102✔
2397
  if (code) {
1,102✔
2398
    goto _return;
×
2399
  }
2400

2401
  SSyncQueryParam *pParam = pRequest->body.interParam;
1,102✔
2402
  code = tsem_wait(&pParam->sem);
1,102✔
2403
  if (code) {
1,102✔
2404
    tscError("tsem wait failed, code:%d - %s", code, tstrerror(code));
×
2405
    goto _return;
×
2406
  }
2407
_return:
1,102✔
2408
  destoryCatalogReq(&catalogReq);
1,102✔
2409
  destroyRequest(pRequest);
1,102✔
2410
  return code;
1,102✔
2411
}
2412

2413
TAOS_STMT *taos_stmt_init(TAOS *taos) {
126,746✔
2414
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
126,746✔
2415
  if (NULL == pObj) {
127,632✔
2416
    tscError("invalid parameter for %s", __FUNCTION__);
×
2417
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2418
    return NULL;
×
2419
  }
2420

2421
  TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL);
127,632✔
2422
  if (NULL == pStmt) {
127,751✔
2423
    tscError("stmt init failed, errcode:%s", terrstr());
×
2424
  }
2425
  releaseTscObj(*(int64_t *)taos);
127,751✔
2426

2427
  return pStmt;
127,751✔
2428
}
2429

2430
TAOS_STMT *taos_stmt_init_with_reqid(TAOS *taos, int64_t reqid) {
×
2431
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
2432
  if (NULL == pObj) {
×
2433
    tscError("invalid parameter for %s", __FUNCTION__);
×
2434
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2435
    return NULL;
×
2436
  }
2437

2438
  TAOS_STMT *pStmt = stmtInit(pObj, reqid, NULL);
×
2439
  if (NULL == pStmt) {
×
2440
    tscError("stmt init failed, errcode:%s", terrstr());
×
2441
  }
2442
  releaseTscObj(*(int64_t *)taos);
×
2443

2444
  return pStmt;
×
2445
}
2446

2447
TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options) {
8,272✔
2448
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
8,272✔
2449
  if (NULL == pObj) {
8,272✔
2450
    tscError("invalid parameter for %s", __FUNCTION__);
×
2451
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2452
    return NULL;
×
2453
  }
2454

2455
  TAOS_STMT *pStmt = stmtInit(pObj, options->reqId, options);
8,272✔
2456
  if (NULL == pStmt) {
8,272✔
2457
    tscError("stmt init failed, errcode:%s", terrstr());
×
2458
  }
2459
  releaseTscObj(*(int64_t *)taos);
8,272✔
2460

2461
  return pStmt;
8,272✔
2462
}
2463

2464
int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
3,202,178✔
2465
  if (stmt == NULL || sql == NULL) {
3,202,178✔
2466
    tscError("NULL parameter for %s", __FUNCTION__);
×
2467
    terrno = TSDB_CODE_INVALID_PARA;
×
2468
    return terrno;
×
2469
  }
2470

2471
  return stmtPrepare(stmt, sql, length);
3,202,824✔
2472
}
2473

2474
int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags) {
5,523✔
2475
  if (stmt == NULL || name == NULL) {
5,523✔
2476
    tscError("NULL parameter for %s", __FUNCTION__);
×
2477
    terrno = TSDB_CODE_INVALID_PARA;
×
2478
    return terrno;
×
2479
  }
2480

2481
  int32_t code = stmtSetTbName(stmt, name);
5,523✔
2482
  if (code) {
5,523✔
2483
    return code;
551✔
2484
  }
2485

2486
  if (tags) {
4,972✔
2487
    return stmtSetTbTags(stmt, tags);
4,972✔
2488
  }
2489

2490
  return TSDB_CODE_SUCCESS;
×
2491
}
2492

2493
int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name) {
4,764,698✔
2494
  if (stmt == NULL || name == NULL) {
4,764,698✔
2495
    tscError("NULL parameter for %s", __FUNCTION__);
×
2496
    terrno = TSDB_CODE_INVALID_PARA;
×
2497
    return terrno;
×
2498
  }
2499

2500
  return stmtSetTbName(stmt, name);
4,768,104✔
2501
}
2502

2503
int taos_stmt_set_tags(TAOS_STMT *stmt, TAOS_MULTI_BIND *tags) {
90✔
2504
  if (stmt == NULL || tags == NULL) {
90✔
2505
    tscError("NULL parameter for %s", __FUNCTION__);
×
2506
    terrno = TSDB_CODE_INVALID_PARA;
×
2507
    return terrno;
×
2508
  }
2509

2510
  return stmtSetTbTags(stmt, tags);
90✔
2511
}
2512

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

2515
int taos_stmt_get_tag_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
×
2516
  if (stmt == NULL || NULL == fieldNum) {
×
2517
    tscError("NULL parameter for %s", __FUNCTION__);
×
2518
    terrno = TSDB_CODE_INVALID_PARA;
×
2519
    return terrno;
×
2520
  }
2521

2522
  return stmtGetTagFields(stmt, fieldNum, fields);
×
2523
}
2524

2525
int taos_stmt_get_col_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 stmtGetColFields(stmt, fieldNum, fields);
×
2533
}
2534

2535
// let stmt to reclaim TAOS_FIELD_E that was allocated by `taos_stmt_get_tag_fields`/`taos_stmt_get_col_fields`
2536
void taos_stmt_reclaim_fields(TAOS_STMT *stmt, TAOS_FIELD_E *fields) {
×
2537
  (void)stmt;
2538
  if (!fields) return;
×
2539
  taosMemoryFree(fields);
×
2540
}
2541

2542
int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
25,762✔
2543
  if (stmt == NULL || bind == NULL) {
25,762✔
2544
    tscError("NULL parameter for %s", __FUNCTION__);
×
2545
    terrno = TSDB_CODE_INVALID_PARA;
×
2546
    return terrno;
×
2547
  }
2548

2549
  if (bind->num > 1) {
25,762✔
2550
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
3,606✔
2551
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
3,606✔
2552
    return terrno;
3,606✔
2553
  }
2554

2555
  return stmtBindBatch(stmt, bind, -1);
22,156✔
2556
}
2557

2558
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
61,953,426✔
2559
  if (stmt == NULL || bind == NULL) {
61,953,426✔
2560
    tscError("NULL parameter for %s", __FUNCTION__);
×
2561
    terrno = TSDB_CODE_INVALID_PARA;
×
2562
    return terrno;
×
2563
  }
2564

2565
  if (bind->num <= 0 || bind->num > INT16_MAX) {
62,537,265✔
2566
    tscError("invalid bind num %d", bind->num);
1,998✔
2567
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
1,998✔
2568
    return terrno;
×
2569
  }
2570

2571
  int32_t insert = 0;
62,849,971✔
2572
  int32_t code = stmtIsInsert(stmt, &insert);
62,665,160✔
2573
  if (TSDB_CODE_SUCCESS != code) {
62,849,950✔
2574
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2575
    return code;
×
2576
  }
2577
  if (0 == insert && bind->num > 1) {
62,849,950✔
2578
    tscError("only one row data allowed for query");
×
2579
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2580
    return terrno;
×
2581
  }
2582

2583
  return stmtBindBatch(stmt, bind, -1);
62,849,950✔
2584
}
2585

2586
int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind, int colIdx) {
420✔
2587
  if (stmt == NULL || bind == NULL) {
420✔
2588
    tscError("NULL parameter for %s", __FUNCTION__);
×
2589
    terrno = TSDB_CODE_INVALID_PARA;
×
2590
    return terrno;
×
2591
  }
2592

2593
  if (colIdx < 0) {
420✔
2594
    tscError("invalid bind column idx %d", colIdx);
×
2595
    terrno = TSDB_CODE_INVALID_PARA;
×
2596
    return terrno;
×
2597
  }
2598

2599
  int32_t insert = 0;
420✔
2600
  int32_t code = stmtIsInsert(stmt, &insert);
420✔
2601
  if (TSDB_CODE_SUCCESS != code) {
420✔
2602
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2603
    return code;
×
2604
  }
2605
  if (0 == insert && bind->num > 1) {
420✔
2606
    tscError("only one row data allowed for query");
×
2607
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2608
    return terrno;
×
2609
  }
2610

2611
  return stmtBindBatch(stmt, bind, colIdx);
420✔
2612
}
2613

2614
int taos_stmt_add_batch(TAOS_STMT *stmt) {
59,695,071✔
2615
  if (stmt == NULL) {
59,695,071✔
2616
    tscError("NULL parameter for %s", __FUNCTION__);
×
2617
    terrno = TSDB_CODE_INVALID_PARA;
×
2618
    return terrno;
×
2619
  }
2620

2621
  return stmtAddBatch(stmt);
59,695,071✔
2622
}
2623

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

2631
  return stmtExec(stmt);
3,481,409✔
2632
}
2633

2634
int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
×
2635
  if (stmt == NULL || insert == NULL) {
×
2636
    tscError("NULL parameter for %s", __FUNCTION__);
×
2637
    terrno = TSDB_CODE_INVALID_PARA;
×
2638
    return terrno;
×
2639
  }
2640

2641
  return stmtIsInsert(stmt, insert);
×
2642
}
2643

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

2651
  return stmtGetParamNum(stmt, nums);
×
2652
}
2653

2654
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
×
2655
  if (stmt == NULL || type == NULL || NULL == bytes || idx < 0) {
×
2656
    tscError("invalid parameter for %s", __FUNCTION__);
×
2657
    terrno = TSDB_CODE_INVALID_PARA;
×
2658
    return terrno;
×
2659
  }
2660

2661
  return stmtGetParam(stmt, idx, type, bytes);
×
2662
}
2663

2664
TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt) {
8,816✔
2665
  if (stmt == NULL) {
8,816✔
2666
    tscError("NULL parameter for %s", __FUNCTION__);
×
2667
    terrno = TSDB_CODE_INVALID_PARA;
×
2668
    return NULL;
×
2669
  }
2670

2671
  return stmtUseResult(stmt);
8,816✔
2672
}
2673

2674
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
9,421✔
2675

2676
int taos_stmt_affected_rows(TAOS_STMT *stmt) {
2,852✔
2677
  if (stmt == NULL) {
2,852✔
2678
    tscError("NULL parameter for %s", __FUNCTION__);
×
2679
    terrno = TSDB_CODE_INVALID_PARA;
×
2680
    return 0;
×
2681
  }
2682

2683
  return stmtAffectedRows(stmt);
2,852✔
2684
}
2685

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

2693
  return stmtAffectedRowsOnce(stmt);
80✔
2694
}
2695

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

2703
  return stmtClose(stmt);
135,830✔
2704
}
2705

2706
TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option) {
2,481✔
2707
  if (NULL == taos) {
2,481✔
2708
    tscError("NULL parameter for %s", __FUNCTION__);
×
2709
    terrno = TSDB_CODE_INVALID_PARA;
×
2710
    return NULL;
×
2711
  }
2712
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2,481✔
2713
  if (NULL == pObj) {
2,481✔
2714
    tscError("invalid parameter for %s", __FUNCTION__);
×
2715
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2716
    return NULL;
×
2717
  }
2718

2719
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
2,481✔
2720

2721
  releaseTscObj(*(int64_t *)taos);
2,481✔
2722

2723
  return pStmt;
2,481✔
2724
}
2725

2726
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
2,569✔
2727
  if (stmt == NULL || sql == NULL) {
2,569✔
2728
    tscError("NULL parameter for %s", __FUNCTION__);
×
2729
    terrno = TSDB_CODE_INVALID_PARA;
×
2730
    return terrno;
×
2731
  }
2732

2733
  return stmtPrepare2(stmt, sql, length);
2,569✔
2734
}
2735

2736
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
421,473✔
2737
  if (stmt == NULL) {
421,473✔
2738
    tscError("NULL parameter for %s", __FUNCTION__);
×
2739
    terrno = TSDB_CODE_INVALID_PARA;
×
2740
    return terrno;
×
2741
  }
2742

2743
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
421,473✔
2744
  int32_t    code = TSDB_CODE_SUCCESS;
421,473✔
2745
  STMT2_DLOG_E("start to bind param");
421,473✔
2746

2747
  // check query bind number
2748
  bool isQuery = (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt)));
421,526✔
2749
  if (isQuery) {
421,524✔
2750
    if (bindv->count != 1 || bindv->bind_cols[0]->num != 1) {
8✔
2751
      terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2752
      STMT2_ELOG_E("query only support one table and one row bind");
×
2753
      return terrno;
×
2754
    }
2755
  }
2756

2757
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 1) {
421,524✔
2758
    STMT2_ELOG_E("async bind param is still working, please try again later");
94✔
2759
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
94✔
2760
    return terrno;
×
2761
  }
2762

2763
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
421,527✔
2764
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
2765
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
2766
    }
2767
    pStmt->execSemWaited = true;
×
2768
  }
2769

2770
  for (int i = 0; i < bindv->count; ++i) {
1,285,229✔
2771
    SVCreateTbReq *pCreateTbReq = NULL;
863,597✔
2772
    if (!isQuery) {
863,671✔
2773
      STMT2_TLOG("start to bind %dth table", i);
863,525✔
2774
      if (bindv->tbnames && bindv->tbnames[i]) {
863,550✔
2775
        code = stmtSetTbName2(stmt, bindv->tbnames[i]);
863,574✔
2776
        if (code) {
863,831✔
2777
          terrno = code;
×
2778
          STMT2_ELOG("set tbname failed, code:%s", tstrerror(code));
×
2779
          return terrno;
×
2780
        }
2781
      }
2782

2783
      if (bindv->tags && bindv->tags[i]) {
863,653✔
2784
        code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
404,154✔
2785
      } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
459,618✔
2786
        code = stmtCheckTags2(stmt, &pCreateTbReq);
87,978✔
2787
      } else if (pStmt->sql.autoCreateTbl) {
371,640✔
2788
        code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
×
2789
      }
2790

2791
      if (code) {
863,449✔
2792
        terrno = code;
×
2793
        STMT2_ELOG("set tags failed, code:%s", tstrerror(code));
×
2794
        return terrno;
×
2795
      }
2796
    }
2797

2798
    if (bindv->bind_cols && bindv->bind_cols[i]) {
863,595✔
2799
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
863,821✔
2800

2801
      if (bind->num <= 0 || bind->num > INT16_MAX) {
863,745✔
2802
        STMT2_ELOG("bind num:%d must > 0 and < INT16_MAX", bind->num);
370✔
2803
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
370✔
2804
        return terrno;
×
2805
      }
2806

2807
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
863,563✔
2808
      if (TSDB_CODE_SUCCESS != code) {
863,788✔
UNCOV
2809
        terrno = code;
×
2810
        STMT2_ELOG("bind batch failed, code:%s", tstrerror(code));
×
2811
        return terrno;
×
2812
      }
2813
    }
2814
  }
2815

2816
  return code;
421,638✔
2817
}
2818

2819
int taos_stmt2_bind_param_a(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx, __taos_async_fn_t fp,
×
2820
                            void *param) {
2821
  if (stmt == NULL || bindv == NULL || fp == NULL) {
×
2822
    terrno = TSDB_CODE_INVALID_PARA;
×
2823
    return terrno;
×
2824
  }
2825

2826
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2827

2828
  ThreadArgs *args = (ThreadArgs *)taosMemoryMalloc(sizeof(ThreadArgs));
×
2829
  args->stmt = stmt;
×
2830
  args->bindv = bindv;
×
2831
  args->col_idx = col_idx;
×
2832
  args->fp = fp;
×
2833
  args->param = param;
×
2834

2835
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2836
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 0) {
×
2837
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2838
    tscError("async bind param is still working, please try again later");
×
2839
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
×
2840
    return terrno;
×
2841
  }
2842
  (void)atomic_add_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2843
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2844

2845
  int code_s = taosStmt2AsyncBind(stmtAsyncBindThreadFunc, (void *)args);
×
2846
  if (code_s != TSDB_CODE_SUCCESS) {
×
2847
    terrno = code_s;
×
2848
    (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2849
    (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2850
    (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2851
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2852
    tscError("async bind failed, code:%d , %s", code_s, tstrerror(code_s));
×
2853
  }
2854

2855
  return code_s;
×
2856
}
2857

2858
int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows) {
421,781✔
2859
  if (stmt == NULL) {
421,781✔
2860
    tscError("NULL parameter for %s", __FUNCTION__);
×
2861
    terrno = TSDB_CODE_INVALID_PARA;
×
2862
    return terrno;
×
2863
  }
2864

2865
  return stmtExec2(stmt, affected_rows);
421,781✔
2866
}
2867

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

2875
  return stmtClose2(stmt);
2,481✔
2876
}
2877

2878
int taos_stmt2_is_insert(TAOS_STMT2 *stmt, int *insert) {
26✔
2879
  if (stmt == NULL || insert == NULL) {
26✔
2880
    tscError("NULL parameter for %s", __FUNCTION__);
×
2881
    terrno = TSDB_CODE_INVALID_PARA;
×
2882
    return terrno;
×
2883
  }
2884
  *insert = stmt2IsInsert(stmt);
26✔
2885
  return TSDB_CODE_SUCCESS;
26✔
2886
}
2887

2888
int taos_stmt2_get_fields(TAOS_STMT2 *stmt, int *count, TAOS_FIELD_ALL **fields) {
18✔
2889
  if (stmt == NULL || count == NULL) {
18✔
2890
    tscError("NULL parameter for %s", __FUNCTION__);
×
2891
    terrno = TSDB_CODE_INVALID_PARA;
×
2892
    return terrno;
×
2893
  }
2894

2895
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
18✔
2896
  STMT2_DLOG_E("start to get fields");
18✔
2897

2898
  if (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type ||
18✔
2899
      (pStmt->sql.type == 0 && stmt2IsInsert(stmt))) {
18✔
2900
    return stmtGetStbColFields2(stmt, count, fields);
18✔
2901
  }
2902
  if (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt))) {
×
2903
    return stmtGetParamNum2(stmt, count);
×
2904
  }
2905

2906
  tscError("Invalid sql for stmt %s", pStmt->sql.sqlStr);
×
2907
  return TSDB_CODE_PAR_SYNTAX_ERROR;
×
2908
}
2909

2910
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_ALL *fields) {
18✔
2911
  (void)stmt;
2912
  if (!fields) return;
18✔
2913
  taosMemoryFree(fields);
18✔
2914
}
2915

2916
TAOS_RES *taos_stmt2_result(TAOS_STMT2 *stmt) {
8✔
2917
  if (stmt == NULL) {
8✔
2918
    tscError("NULL parameter for %s", __FUNCTION__);
×
2919
    terrno = TSDB_CODE_INVALID_PARA;
×
2920
    return NULL;
×
2921
  }
2922

2923
  return stmtUseResult2(stmt);
8✔
2924
}
2925

2926
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmtErrstr2(stmt); }
×
2927

2928
int taos_set_conn_mode(TAOS *taos, int mode, int value) {
2,004✔
2929
  int32_t code = 0;
2,004✔
2930
  if (taos == NULL) {
2,004✔
2931
    terrno = TSDB_CODE_INVALID_PARA;
×
2932
    return terrno;
×
2933
  }
2934

2935
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2,004✔
2936
  if (NULL == pObj) {
2,004✔
2937
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2938
    tscError("invalid parameter for %s", __func__);
×
2939
    return terrno;
×
2940
  }
2941
  switch (mode) {
2,004✔
2942
    case TAOS_CONN_MODE_BI:
2,004✔
2943
      atomic_store_8(&pObj->biMode, value);
2,004✔
2944
      break;
2,004✔
2945
    default:
×
2946
      tscError("not supported mode.");
×
2947
      code = TSDB_CODE_INVALID_PARA;
×
2948
  }
2949
  releaseTscObj(*(int64_t *)taos);
2,004✔
2950
  return code;
2,004✔
2951
}
2952

2953
char *getBuildInfo() { return td_buildinfo; }
×
2954

2955
int32_t taos_connect_is_alive(TAOS *taos) {
×
2956
  int32_t code = 0, lino = 0;
×
2957
  if (taos == NULL) {
×
2958
    terrno = TSDB_CODE_INVALID_PARA;
×
2959
    return terrno;
×
2960
  }
2961

2962
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
2963
  if (NULL == pObj) {
×
2964
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2965
    tscError("invalid parameter for %s", __func__);
×
2966
    return terrno;
×
2967
  }
2968

2969
  code = sessMgtCheckConnStatus(pObj->user, &pObj->sessInfo);
×
2970
  TAOS_CHECK_GOTO(code, &lino, _error);
×
2971

2972
_error:
×
2973
  releaseTscObj(*(int64_t *)taos);
×
2974

2975
  if (code != 0) {
×
2976
    tscError("taos conn failed to check alive, code:%d - %s", code, tstrerror(code));
×
2977
  }
2978

2979
  return code != 0 ? 0 : 1;
×
2980
}
2981
static int32_t buildInstanceRegisterSql(const SInstanceRegisterReq *req, char **ppSql, uint32_t *pLen) {
×
2982
  const char *action = (req->expire < 0) ? "UNREGISTER" : "REGISTER";
×
2983
  int32_t     len = 0;
×
2984

2985
  len += snprintf(NULL, 0, "%s INSTANCE '%s'", action, req->id);
×
2986
  if (req->type[0] != 0) {
×
2987
    len += snprintf(NULL, 0, " TYPE '%s'", req->type);
×
2988
  }
2989
  if (req->desc[0] != 0) {
×
2990
    len += snprintf(NULL, 0, " DESC '%s'", req->desc);
×
2991
  }
2992
  if (req->expire >= 0) {
×
2993
    len += snprintf(NULL, 0, " EXPIRE %d", req->expire);
×
2994
  }
2995

2996
  char *sql = taosMemoryMalloc((size_t)len + 1);
×
2997
  if (sql == NULL) {
×
2998
    return terrno;
×
2999
  }
3000

3001
  int32_t offset = snprintf(sql, (size_t)len + 1, "%s INSTANCE '%s'", action, req->id);
×
3002
  if (req->type[0] != 0) {
×
3003
    offset += snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " TYPE '%s'", req->type);
×
3004
  }
3005
  if (req->desc[0] != 0) {
×
3006
    offset += snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " DESC '%s'", req->desc);
×
3007
  }
3008
  if (req->expire >= 0) {
×
3009
    (void)snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " EXPIRE %d", req->expire);
×
3010
  }
3011

3012
  *ppSql = sql;
×
3013
  if (pLen != NULL) {
×
3014
    *pLen = (uint32_t)len;
×
3015
  }
3016
  return TSDB_CODE_SUCCESS;
×
3017
}
3018

3019
static int32_t sendInstanceRegisterReq(STscObj *pObj, const SInstanceRegisterReq *req) {
×
3020
  SRequestObj *pRequest = NULL;
×
3021
  int32_t      code = createRequest(pObj->id, TDMT_MND_REGISTER_INSTANCE, 0, &pRequest);
×
3022
  if (code != TSDB_CODE_SUCCESS) {
×
3023
    terrno = code;
×
3024
    return code;
×
3025
  }
3026

3027
  code = buildInstanceRegisterSql(req, &pRequest->sqlstr, (uint32_t *)&pRequest->sqlLen);
×
3028
  if (code != TSDB_CODE_SUCCESS) {
×
3029
    goto _cleanup;
×
3030
  }
3031

3032
  int32_t msgLen = tSerializeSInstanceRegisterReq(NULL, 0, (SInstanceRegisterReq *)req);
×
3033
  if (msgLen <= 0) {
×
3034
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3035
    goto _cleanup;
×
3036
  }
3037

3038
  void *pMsg = taosMemoryMalloc(msgLen);
×
3039
  if (pMsg == NULL) {
×
3040
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3041
    goto _cleanup;
×
3042
  }
3043

3044
  if (tSerializeSInstanceRegisterReq(pMsg, msgLen, (SInstanceRegisterReq *)req) < 0) {
×
3045
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3046
    taosMemoryFree(pMsg);
×
3047
    goto _cleanup;
×
3048
  }
3049

3050
  pRequest->type = TDMT_MND_REGISTER_INSTANCE;
×
3051
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsg, .len = msgLen, .handle = NULL};
×
3052

3053
  SMsgSendInfo *pSend = buildMsgInfoImpl(pRequest);
×
3054
  if (pSend == NULL) {
×
3055
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3056
    taosMemoryFree(pMsg);
×
3057
    pRequest->body.requestMsg.pData = NULL;
×
3058
    goto _cleanup;
×
3059
  }
3060

3061
  SEpSet epSet = getEpSet_s(&pObj->pAppInfo->mgmtEp);
×
3062
  code = asyncSendMsgToServer(pObj->pAppInfo->pTransporter, &epSet, NULL, pSend);
×
3063
  if (code != TSDB_CODE_SUCCESS) {
×
3064
    destroySendMsgInfo(pSend);
×
3065
    pRequest->body.requestMsg = (SDataBuf){0};
×
3066
    goto _cleanup;
×
3067
  }
3068

3069
  code = tsem_wait(&pRequest->body.rspSem);
×
3070
  if (code != TSDB_CODE_SUCCESS) {
×
3071
    code = terrno != 0 ? terrno : code;
×
3072
    goto _cleanup;
×
3073
  }
3074

3075
  code = pRequest->code;
×
3076
  terrno = code;
×
3077

3078
_cleanup:
×
3079
  destroyRequest(pRequest);
×
3080
  return code;
×
3081
}
3082

3083
static bool instanceRegisterRpcRfp(int32_t code, tmsg_t msgType) {
×
3084
  if (NEED_REDIRECT_ERROR(code)) {
×
3085
    return true;
×
3086
  } else if (code == TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY || code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE ||
×
3087
             code == TSDB_CODE_SYN_WRITE_STALL || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
×
3088
             code == TSDB_CODE_SYN_RESTORING) {
3089
    tscDebug("client msg type %s should retry since %s", TMSG_INFO(msgType), tstrerror(code));
×
3090
    return true;
×
3091
  } else {
3092
    return false;
×
3093
  }
3094
}
3095

3096
int32_t taos_register_instance(const char *id, const char *type, const char *desc, int32_t expire) {
×
3097
  if (id == NULL || id[0] == 0) {
×
3098
    return terrno = TSDB_CODE_INVALID_PARA;
×
3099
  }
3100

3101
  // Validate string lengths
3102
  size_t idLen = strlen(id);
×
3103
  if (idLen >= TSDB_INSTANCE_ID_LEN) {
×
3104
    tscError("instance id length %zu exceeds limit %d", idLen, TSDB_INSTANCE_ID_LEN - 1);
×
3105
    return terrno = TSDB_CODE_INVALID_PARA;
×
3106
  }
3107

3108
  if (type != NULL && type[0] != 0) {
×
3109
    size_t typeLen = strlen(type);
×
3110
    if (typeLen >= TSDB_INSTANCE_TYPE_LEN) {
×
3111
      tscError("instance type length %zu exceeds limit %d", typeLen, TSDB_INSTANCE_TYPE_LEN - 1);
×
3112
      return terrno = TSDB_CODE_INVALID_PARA;
×
3113
    }
3114
  }
3115

3116
  if (desc != NULL && desc[0] != 0) {
×
3117
    size_t descLen = strlen(desc);
×
3118
    if (descLen >= TSDB_INSTANCE_DESC_LEN) {
×
3119
      tscError("instance desc length %zu exceeds limit %d", descLen, TSDB_INSTANCE_DESC_LEN - 1);
×
3120
      return terrno = TSDB_CODE_INVALID_PARA;
×
3121
    }
3122
  }
3123

3124
  int32_t code = taos_init();
×
3125
  if (code != TSDB_CODE_SUCCESS) {
×
3126
    return code;
×
3127
  }
3128

3129
  SConfig *pCfg = taosGetCfg();
×
3130
  if (pCfg == NULL) {
×
3131
    return terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3132
  }
3133

3134
  SConfigItem *pFirstEpItem = cfgGetItem(pCfg, "firstEp");
×
3135
  if (pFirstEpItem == NULL || pFirstEpItem->str == NULL || pFirstEpItem->str[0] == 0) {
×
3136
    return terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3137
  }
3138

3139
  SEp firstEp = {0};
×
3140
  code = taosGetFqdnPortFromEp(pFirstEpItem->str, &firstEp);
×
3141
  if (code != TSDB_CODE_SUCCESS) {
×
3142
    return terrno = code;
×
3143
  }
3144

3145
  void    *clientRpc = NULL;
×
3146
  SEpSet   epSet = {.inUse = 0, .numOfEps = 1};
×
3147
  SRpcMsg  rpcMsg = {0};
×
3148
  SRpcMsg  rpcRsp = {0};
×
3149
  SRpcInit rpcInit = {0};
×
3150

3151
  rpcInit.label = "INST";
×
3152
  rpcInit.numOfThreads = 1;
×
3153
  rpcInit.cfp = NULL;
×
3154
  rpcInit.sessions = 16;
×
3155
  rpcInit.connType = TAOS_CONN_CLIENT;
×
3156
  rpcInit.idleTime = tsShellActivityTimer * 1000;
×
3157
  rpcInit.compressSize = tsCompressMsgSize;
×
3158
  rpcInit.user = TSDB_DEFAULT_USER;
×
3159

3160
  rpcInit.rfp = instanceRegisterRpcRfp;
×
3161
  rpcInit.retryMinInterval = tsRedirectPeriod;
×
3162
  rpcInit.retryStepFactor = tsRedirectFactor;
×
3163
  rpcInit.retryMaxInterval = tsRedirectMaxPeriod;
×
3164
  rpcInit.retryMaxTimeout =
×
3165
      tsMaxRetryWaitTime;  // Use a special user for instance registration (can be configured for whitelist)
3166

3167
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
×
3168
  connLimitNum = TMAX(connLimitNum, 10);
×
3169
  connLimitNum = TMIN(connLimitNum, 500);
×
3170
  rpcInit.connLimitNum = connLimitNum;
×
3171
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
×
3172
  rpcInit.readTimeout = tsReadTimeout;
×
3173
  rpcInit.ipv6 = tsEnableIpv6;
×
3174
  rpcInit.enableSSL = tsEnableTLS;
×
3175

3176
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
×
3177
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
×
3178
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
×
3179
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
×
3180
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
×
3181

3182
  code = taosVersionStrToInt(td_version, &rpcInit.compatibilityVer);
×
3183
  if (code != TSDB_CODE_SUCCESS) {
×
3184
    tscError("failed to convert taos version from str to int, errcode:%s", terrstr(code));
×
3185
    return code;
×
3186
  }
3187

3188
  clientRpc = rpcOpen(&rpcInit);
×
3189
  if (clientRpc == NULL) {
×
3190
    code = terrno;
×
3191
    tscError("failed to init instance register client since %s", tstrerror(code));
×
3192
    return code;
×
3193
  }
3194

3195
  // Prepare epSet
3196
  tstrncpy(epSet.eps[0].fqdn, firstEp.fqdn, TSDB_FQDN_LEN);
×
3197
  epSet.eps[0].port = firstEp.port;
×
3198

3199
  // Prepare request
3200
  SInstanceRegisterReq req = {0};
×
3201
  tstrncpy(req.id, id, sizeof(req.id));
×
3202
  if (type != NULL && type[0] != 0) {
×
3203
    tstrncpy(req.type, type, sizeof(req.type));
×
3204
  }
3205
  if (desc != NULL && desc[0] != 0) {
×
3206
    tstrncpy(req.desc, desc, sizeof(req.desc));
×
3207
  }
3208
  req.expire = expire;
×
3209

3210
  int32_t contLen = tSerializeSInstanceRegisterReq(NULL, 0, &req);
×
3211
  if (contLen <= 0) {
×
3212
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3213
    rpcClose(clientRpc);
×
3214
    return code;
×
3215
  }
3216

3217
  void *pCont = rpcMallocCont(contLen);
×
3218
  if (pCont == NULL) {
×
3219
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3220
    rpcClose(clientRpc);
×
3221
    return code;
×
3222
  }
3223

3224
  if (tSerializeSInstanceRegisterReq(pCont, contLen, &req) < 0) {
×
3225
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3226
    rpcFreeCont(pCont);
×
3227
    rpcClose(clientRpc);
×
3228
    return code;
×
3229
  }
3230

3231
  rpcMsg.pCont = pCont;
×
3232
  rpcMsg.contLen = contLen;
×
3233
  rpcMsg.msgType = TDMT_MND_REGISTER_INSTANCE;
×
3234
  rpcMsg.info.ahandle = (void *)0x9528;  // Different magic number from server status
×
3235
  rpcMsg.info.notFreeAhandle = 1;
×
3236

3237
  code = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
×
3238
  if (TSDB_CODE_SUCCESS != code) {
×
3239
    tscError("failed to send instance register req since %s", tstrerror(code));
×
3240
    // rpcSendRecv failed, pCont may not be freed, but check _RETURN1 path
3241
    // In error path, rpcSendRecv may free pCont, but we free it here to be safe
3242
    rpcClose(clientRpc);
×
3243
    return code;
×
3244
  }
3245

3246
  if (rpcRsp.code != 0) {
×
3247
    code = rpcRsp.code;
×
3248
    tscError("instance register failed, code:%s", tstrerror(code));
×
3249
  } else {
3250
    code = TSDB_CODE_SUCCESS;
×
3251
  }
3252

3253
  if (rpcRsp.pCont != NULL) {
×
3254
    rpcFreeCont(rpcRsp.pCont);
×
3255
  }
3256
  rpcClose(clientRpc);
×
3257

3258
  terrno = code;
×
3259
  return code;
×
3260
}
3261

3262
int32_t taos_list_instances(const char *filter_type, char ***pList, int32_t *pCount) {
×
3263
  if (pList == NULL || pCount == NULL) {
×
3264
    return TSDB_CODE_INVALID_PARA;
×
3265
  }
3266

3267
  int32_t code = taos_init();
×
3268
  if (code != TSDB_CODE_SUCCESS) {
×
3269
    terrno = code;
×
3270
    return code;
×
3271
  }
3272

3273
  SConfig *pCfg = taosGetCfg();
×
3274
  if (pCfg == NULL) {
×
3275
    terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3276
    return TSDB_CODE_CFG_NOT_FOUND;
×
3277
  }
3278

3279
  SConfigItem *pFirstEpItem = cfgGetItem(pCfg, "firstEp");
×
3280
  if (pFirstEpItem == NULL || pFirstEpItem->str == NULL || pFirstEpItem->str[0] == 0) {
×
3281
    terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3282
    return TSDB_CODE_CFG_NOT_FOUND;
×
3283
  }
3284

3285
  SEp firstEp = {0};
×
3286
  code = taosGetFqdnPortFromEp(pFirstEpItem->str, &firstEp);
×
3287
  if (code != TSDB_CODE_SUCCESS) {
×
3288
    terrno = code;
×
3289
    return code;
×
3290
  }
3291

3292
  // Initialize RPC connection (similar to taos_register_instance)
3293
  void    *clientRpc = NULL;
×
3294
  SEpSet   epSet = {.inUse = 0, .numOfEps = 1};
×
3295
  SRpcMsg  rpcMsg = {0};
×
3296
  SRpcMsg  rpcRsp = {0};
×
3297
  SRpcInit rpcInit = {0};
×
3298

3299
  rpcInit.label = "LIST";
×
3300
  rpcInit.numOfThreads = 1;
×
3301
  rpcInit.cfp = NULL;
×
3302
  rpcInit.sessions = 16;
×
3303
  rpcInit.connType = TAOS_CONN_CLIENT;
×
3304
  rpcInit.idleTime = tsShellActivityTimer * 1000;
×
3305
  rpcInit.compressSize = tsCompressMsgSize;
×
3306
  rpcInit.user = TSDB_DEFAULT_USER;
×
3307

3308
  rpcInit.rfp = instanceRegisterRpcRfp;
×
3309
  rpcInit.retryMinInterval = tsRedirectPeriod;
×
3310
  rpcInit.retryStepFactor = tsRedirectFactor;
×
3311
  rpcInit.retryMaxInterval = tsRedirectMaxPeriod;
×
3312
  rpcInit.retryMaxTimeout =
×
3313
      tsMaxRetryWaitTime;  // Use a special user for instance registration (can be configured for whitelist)
3314

3315
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
×
3316
  connLimitNum = TMAX(connLimitNum, 10);
×
3317
  connLimitNum = TMIN(connLimitNum, 500);
×
3318
  rpcInit.connLimitNum = connLimitNum;
×
3319
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
×
3320
  rpcInit.readTimeout = tsReadTimeout;
×
3321
  rpcInit.ipv6 = tsEnableIpv6;
×
3322
  rpcInit.enableSSL = tsEnableTLS;
×
3323

3324
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
×
3325
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
×
3326
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
×
3327
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
×
3328
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
×
3329

3330
  code = taosVersionStrToInt(td_version, &rpcInit.compatibilityVer);
×
3331
  if (code != TSDB_CODE_SUCCESS) {
×
3332
    tscError("failed to convert taos version from str to int, errcode:%s", terrstr(code));
×
3333
    return code;
×
3334
  }
3335

3336
  clientRpc = rpcOpen(&rpcInit);
×
3337
  if (clientRpc == NULL) {
×
3338
    code = terrno;
×
3339
    tscError("failed to init instance list client since %s", tstrerror(code));
×
3340
    terrno = code;
×
3341
    return code;
×
3342
  }
3343

3344
  tstrncpy(epSet.eps[0].fqdn, firstEp.fqdn, TSDB_FQDN_LEN);
×
3345
  epSet.eps[0].port = firstEp.port;
×
3346
  SInstanceListReq req = {0};
×
3347
  if (filter_type != NULL && filter_type[0] != 0) {
×
3348
    tstrncpy(req.filter_type, filter_type, sizeof(req.filter_type));
×
3349
  }
3350

3351
  // Serialize request to get required length
3352
  int32_t contLen = tSerializeSInstanceListReq(NULL, 0, &req);
×
3353
  if (contLen <= 0) {
×
3354
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3355
    rpcClose(clientRpc);
×
3356
    terrno = code;
×
3357
    return code;
×
3358
  }
3359

3360
  // Allocate RPC message buffer (includes message header overhead)
3361
  void *pCont = rpcMallocCont(contLen);
×
3362
  if (pCont == NULL) {
×
3363
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3364
    rpcClose(clientRpc);
×
3365
    terrno = code;
×
3366
    return code;
×
3367
  }
3368

3369
  // Serialize request into the content part (after message header)
3370
  if (tSerializeSInstanceListReq(pCont, contLen, &req) < 0) {
×
3371
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3372
    rpcFreeCont(pCont);
×
3373
    rpcClose(clientRpc);
×
3374
    terrno = code;
×
3375
    return code;
×
3376
  }
3377

3378
  rpcMsg.pCont = pCont;
×
3379
  rpcMsg.contLen = contLen;
×
3380
  rpcMsg.msgType = TDMT_MND_LIST_INSTANCES;
×
3381
  rpcMsg.info.ahandle = (void *)0x9529;  // Different magic number from register
×
3382
  rpcMsg.info.notFreeAhandle = 1;
×
3383

3384
  code = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
×
3385
  if (TSDB_CODE_SUCCESS != code) {
×
3386
    tscError("failed to send instance list req since %s", tstrerror(code));
×
3387
    rpcFreeCont(pCont);
×
3388
    rpcClose(clientRpc);
×
3389
    terrno = code;
×
3390
    return code;
×
3391
  }
3392

3393
  // Check response - rpcRsp.code contains the result code from mnode
3394
  if (rpcRsp.code != 0) {
×
3395
    code = rpcRsp.code;
×
3396
    tscError("instance list failed, code:%s", tstrerror(code));
×
3397
    if (rpcRsp.pCont != NULL) {
×
3398
      rpcFreeCont(rpcRsp.pCont);
×
3399
    }
3400
    rpcClose(clientRpc);
×
3401
    terrno = code;
×
3402
    return code;
×
3403
  }
3404

3405
  // Deserialize response
3406
  if (rpcRsp.pCont != NULL && rpcRsp.contLen > 0) {
×
3407
    SInstanceListRsp rsp = {0};
×
3408
    code = tDeserializeSInstanceListRsp(rpcRsp.pCont, rpcRsp.contLen, &rsp);
×
3409
    if (code != TSDB_CODE_SUCCESS) {
×
3410
      tscError("failed to deserialize instance list rsp, code:%s", tstrerror(code));
×
3411
      if (rsp.ids != NULL) {
×
3412
        for (int32_t i = 0; i < rsp.count; i++) {
×
3413
          if (rsp.ids[i] != NULL) {
×
3414
            taosMemoryFree(rsp.ids[i]);
×
3415
          }
3416
        }
3417
        taosMemoryFree(rsp.ids);
×
3418
        rsp.ids = NULL;
×
3419
      }
3420
      rsp.count = 0;
×
3421
      rpcFreeCont(rpcRsp.pCont);
×
3422
      rpcClose(clientRpc);
×
3423
      terrno = code;
×
3424
      return code;
×
3425
    }
3426
    *pList = rsp.ids;
×
3427
    *pCount = rsp.count;
×
3428
  } else {
3429
    *pList = NULL;
×
3430
    *pCount = 0;
×
3431
  }
3432

3433
  if (rpcRsp.pCont != NULL) {
×
3434
    rpcFreeCont(rpcRsp.pCont);
×
3435
  }
3436
  rpcClose(clientRpc);
×
3437

3438
  return TSDB_CODE_SUCCESS;
×
3439
}
3440

3441
void taos_free_instances(char ***pList, int32_t count) {
×
3442
  if (pList == NULL || *pList == NULL || count <= 0) {
×
3443
    return;
×
3444
  }
3445

3446
  // Free each string in the array
3447
  for (int32_t i = 0; i < count; i++) {
×
3448
    if ((*pList)[i] != NULL) {
×
3449
      taosMemoryFree((*pList)[i]);
×
3450
      (*pList)[i] = NULL;
×
3451
    }
3452
  }
3453

3454
  // Free the array itself
3455
  taosMemoryFree(*pList);
×
3456
  *pList = NULL;
×
3457
}
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