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

taosdata / TDengine / #4909

30 Dec 2025 10:52AM UTC coverage: 65.542% (+0.2%) from 65.386%
#4909

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

857 existing lines in 113 files now uncovered.

193924 of 295877 relevant lines covered (65.54%)

120594206.86 hits per line

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

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

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

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

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

254
  monitorClose();
1,224,036✔
255
  tscStopCrashReport();
1,224,036✔
256

257
  hbMgrCleanUp();
1,224,036✔
258

259
  catalogDestroy();
1,224,036✔
260
  schedulerDestroy();
1,224,036✔
261

262
  fmFuncMgtDestroy();
1,224,036✔
263
  qCleanupKeywordsTable();
1,224,036✔
264

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

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

274
  id = clientConnRefPool;
1,224,036✔
275
  clientConnRefPool = -1;
1,224,036✔
276
  taosCloseRef(id);
1,224,036✔
277

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

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

287
  sessMgtDestroy();
1,224,036✔
288

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

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

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

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

320
  if (pass == NULL) {
2,020,297✔
321
    pass = TSDB_DEFAULT_PASS;
157,887✔
322
  }
323

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

338
  return NULL;
1,036✔
339
}
340

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

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

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

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

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

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

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

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

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

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

430
  return taos;
×
431
}
432

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

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

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

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

488
  releaseTscObj(*(int64_t *)taos);
1,330✔
489
  return 0;
1,330✔
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,126,130✔
938
  if (taos == NULL) {
2,126,130✔
939
    return;
×
940
  }
941
  int32_t code = 0;
2,126,130✔
942

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

946
  SSessParam para = {.type = SESSION_PER_USER, .value = -1, .noCheck = 1};
2,126,130✔
947

948
  code = tscUpdateSessMetric(pTscObj, &para);
2,126,130✔
949
  if (code != TSDB_CODE_SUCCESS) {
2,126,092✔
950
    tscWarn("conn:0x%" PRIx64 ", failed to update user:%s metric when close connection, code:%d", pTscObj->id,
×
951
            pTscObj->user, code);
952
  }
953

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

960
void taos_close(TAOS *taos) {
2,018,084✔
961
  if (taos == NULL) {
2,018,084✔
UNCOV
962
    return;
×
963
  }
964

965
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2,018,084✔
966
  if (NULL == pObj) {
2,018,084✔
967
    taosMemoryFree(taos);
×
968
    return;
×
969
  }
970

971
  taos_close_internal(pObj);
2,018,084✔
972
  releaseTscObj(*(int64_t *)taos);
2,018,084✔
973
  taosMemoryFree(taos);
2,018,084✔
974
}
975

976
int taos_errno(TAOS_RES *res) {
1,063,309,154✔
977
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,063,309,154✔
978
    return terrno;
7,431✔
979
  }
980

981
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,063,303,583✔
982
    return 0;
348,620✔
983
  }
984

985
  return ((SRequestObj *)res)->code;
1,062,955,644✔
986
}
987

988
const char *taos_errstr(TAOS_RES *res) {
73,584,348✔
989
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
73,584,348✔
990
    if (*(taosGetErrMsg()) == 0) {
5,126✔
991
      return (const char *)tstrerror(terrno);
5,111✔
992
    } else {
993
      (void)snprintf(taosGetErrMsgReturn(), ERR_MSG_LEN, "%s", taosGetErrMsg());
15✔
994
      return (const char*)taosGetErrMsgReturn();
15✔
995
    }
996
  }
997

998
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
73,579,222✔
999
    return "success";
×
1000
  }
1001

1002
  SRequestObj *pRequest = (SRequestObj *)res;
73,579,222✔
1003
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
73,579,222✔
1004
    return pRequest->msgBuf;
21,525,236✔
1005
  } else {
1006
    return (const char *)tstrerror(pRequest->code);
52,053,986✔
1007
  }
1008
}
1009

1010
void taos_free_result(TAOS_RES *res) {
806,812,068✔
1011
  if (NULL == res) {
806,812,068✔
1012
    return;
6,702,613✔
1013
  }
1014

1015
  tscTrace("res:%p, will be freed", res);
800,109,455✔
1016

1017
  if (TD_RES_QUERY(res)) {
800,109,455✔
1018
    SRequestObj *pRequest = (SRequestObj *)res;
785,102,854✔
1019
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
785,102,854✔
1020
    destroyRequest(pRequest);
785,103,555✔
1021
    return;
785,097,947✔
1022
  }
1023

1024
  SMqRspObj *pRsp = (SMqRspObj *)res;
15,008,807✔
1025
  if (TD_RES_TMQ(res)) {
15,008,807✔
1026
    tDeleteMqDataRsp(&pRsp->dataRsp);
14,945,591✔
1027
    doFreeReqResultInfo(&pRsp->resInfo);
14,945,591✔
1028
  } else if (TD_RES_TMQ_METADATA(res)) {
63,571✔
1029
    tDeleteSTaosxRsp(&pRsp->dataRsp);
2,955✔
1030
    doFreeReqResultInfo(&pRsp->resInfo);
2,955✔
1031
  } else if (TD_RES_TMQ_META(res)) {
60,947✔
1032
    tDeleteMqMetaRsp(&pRsp->metaRsp);
55,022✔
1033
  } else if (TD_RES_TMQ_BATCH_META(res)) {
5,925✔
1034
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
5,925✔
1035
  } else if (TD_RES_TMQ_RAW(res)) {
×
1036
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
1037
  }
1038
  taosMemoryFree(pRsp);
15,008,169✔
1039
}
1040

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

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

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

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

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

1065
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
2,147,483,647✔
1066
  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✔
1067
    return NULL;
3,237,925✔
1068
  }
1069

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

1074
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
781,244,431✔
1075
TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqid) {
238✔
1076
  return taosQueryImplWithReqid(taos, sql, false, reqid);
238✔
1077
}
1078

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

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

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

1100
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
1,057,426,017✔
1101
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
1,057,427,984✔
1102
      return NULL;
332✔
1103
    }
1104

1105
    if (pRequest->inCallback) {
1,057,423,368✔
1106
      tscError("can not call taos_fetch_row before query callback ends.");
×
1107
      terrno = TSDB_CODE_TSC_INVALID_OPERATION;
×
1108
      return NULL;
×
1109
    }
1110

1111
    return doAsyncFetchRows(pRequest, true, true);
1,057,427,448✔
1112
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,147,483,647✔
1113
    SMqRspObj      *msg = ((SMqRspObj *)res);
2,147,483,647✔
1114
    SReqResultInfo *pResultInfo = NULL;
2,147,483,647✔
1115
    if (msg->resIter == -1) {
2,147,483,647✔
1116
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
14,578,827✔
1117
        return NULL;
×
1118
      }
1119
    } else {
1120
      pResultInfo = tmqGetCurResInfo(res);
2,147,483,647✔
1121
    }
1122

1123
    if (pResultInfo->current < pResultInfo->numOfRows) {
2,147,483,647✔
1124
      doSetOneRowPtr(pResultInfo);
2,147,483,647✔
1125
      pResultInfo->current += 1;
2,147,483,647✔
1126
      return pResultInfo->row;
2,147,483,647✔
1127
    } else {
1128
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
93,780,429✔
1129
        return NULL;
14,577,512✔
1130
      }
1131

1132
      doSetOneRowPtr(pResultInfo);
79,197,677✔
1133
      pResultInfo->current += 1;
79,201,593✔
1134
      return pResultInfo->row;
79,201,924✔
1135
    }
1136
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1137
    return NULL;
×
1138
  } else {
1139
    tscError("invalid result passed to taos_fetch_row");
×
1140
    terrno = TSDB_CODE_TMQ_INVALID_DATA;
×
1141
    return NULL;
×
1142
  }
1143
}
1144

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

1155
    if (row[i] == NULL) {
2,147,483,647✔
1156
      len += tsnprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
133,633,583✔
1157
      continue;
133,637,219✔
1158
    }
1159

1160
    switch (fields[i].type) {
2,147,483,647✔
1161
      case TSDB_DATA_TYPE_TINYINT:
12,614,070✔
1162
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
12,614,070✔
1163
        break;
12,607,938✔
1164

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

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

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

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

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

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

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

1193
      case TSDB_DATA_TYPE_FLOAT: {
25,095,875✔
1194
        float fv = 0;
25,095,875✔
1195
        fv = GET_FLOAT_VAL(row[i]);
25,095,875✔
1196
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
25,075,337✔
1197
      } break;
25,093,523✔
1198

1199
      case TSDB_DATA_TYPE_DOUBLE: {
2,147,483,647✔
1200
        double dv = 0;
2,147,483,647✔
1201
        dv = GET_DOUBLE_VAL(row[i]);
2,147,483,647✔
1202
        len += snprintf(str + len, size - len, "%.*g", DBL_DIG, dv);
2,147,483,647✔
1203
      } break;
2,147,483,647✔
1204

1205
      case TSDB_DATA_TYPE_VARBINARY: {
58,800✔
1206
        void    *data = NULL;
58,800✔
1207
        uint32_t tmp = 0;
58,800✔
1208
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
58,800✔
1209
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
58,800✔
1210
          break;
×
1211
        }
1212
        uint32_t copyLen = TMIN(size - len - 1, tmp);
58,800✔
1213
        (void)memcpy(str + len, data, copyLen);
58,800✔
1214
        len += copyLen;
58,800✔
1215
        taosMemoryFree(data);
58,800✔
1216
      } break;
58,800✔
1217
      case TSDB_DATA_TYPE_BINARY:
2,147,483,647✔
1218
      case TSDB_DATA_TYPE_NCHAR:
1219
      case TSDB_DATA_TYPE_GEOMETRY: {
1220
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
2,147,483,647✔
1221
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
2,147,483,647✔
1222
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
1,733,035,379✔
1223
          if (charLen > fields[i].bytes || charLen < 0) {
2,147,483,647✔
1224
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
14,955✔
1225
            break;
×
1226
          }
1227
        } else {
1228
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
1,732,978,735✔
1229
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
616✔
1230
            break;
×
1231
          }
1232
        }
1233

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

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

1251
        taosMemoryFree(data);
×
1252
      } break;
×
1253

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

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

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

1280
  return len;
2,147,483,647✔
1281
}
1282

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

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

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

1298
  if (taos_is_update_query(res)) {
×
1299
    return NULL;
×
1300
  }
1301

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

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

1357
const char *taos_get_client_info() { return td_version; }
832,464✔
1358

1359
// return int32_t
1360
int taos_affected_rows(TAOS_RES *res) {
559,834,378✔
1361
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
559,834,378✔
1362
      TD_RES_TMQ_BATCH_META(res)) {
559,835,786✔
1363
    return 0;
×
1364
  }
1365

1366
  SRequestObj    *pRequest = (SRequestObj *)res;
559,835,063✔
1367
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
559,835,063✔
1368
  return (int)pResInfo->numOfRows;
559,835,619✔
1369
}
1370

1371
// return int64_t
1372
int64_t taos_affected_rows64(TAOS_RES *res) {
1,278,115✔
1373
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
1,278,115✔
1374
      TD_RES_TMQ_BATCH_META(res)) {
1,278,115✔
1375
    return 0;
×
1376
  }
1377

1378
  SRequestObj    *pRequest = (SRequestObj *)res;
1,278,115✔
1379
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
1,278,115✔
1380
  return pResInfo->numOfRows;
1,278,115✔
1381
}
1382

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

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

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

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

1413
  char sql[256] = {0};
38,226✔
1414
  (void)snprintf(sql, tListLen(sql), "use %s", db);
38,226✔
1415

1416
  TAOS_RES *pRequest = taos_query(taos, sql);
38,226✔
1417
  int32_t   code = taos_errno(pRequest);
38,226✔
1418

1419
  taos_free_result(pRequest);
38,226✔
1420
  releaseTscObj(*(int64_t *)taos);
38,226✔
1421
  return code;
38,226✔
1422
}
1423

1424
void taos_stop_query(TAOS_RES *res) {
787,700,275✔
1425
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
787,700,275✔
1426
      TD_RES_TMQ_BATCH_META(res)) {
787,703,469✔
UNCOV
1427
    return;
×
1428
  }
1429

1430
  stopAllQueries((SRequestObj *)res);
787,699,759✔
1431
}
1432

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

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

1450
bool taos_is_update_query(TAOS_RES *res) { return taos_num_fields(res) == 0; }
298,632✔
1451

1452
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
205,229,288✔
1453
  int32_t numOfRows = 0;
205,229,288✔
1454
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
205,229,288✔
1455
  return numOfRows;
205,229,288✔
1456
}
1457

1458
int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) {
205,229,288✔
1459
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
205,229,288✔
1460
    return 0;
×
1461
  }
1462

1463
  if (TD_RES_QUERY(res)) {
205,229,288✔
1464
    SRequestObj *pRequest = (SRequestObj *)res;
201,167,945✔
1465

1466
    (*rows) = NULL;
201,167,945✔
1467
    (*numOfRows) = 0;
201,167,945✔
1468

1469
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
201,167,945✔
1470
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
200,856,953✔
1471
      return pRequest->code;
1,290,897✔
1472
    }
1473

1474
    (void)doAsyncFetchRows(pRequest, false, true);
199,877,048✔
1475

1476
    // TODO refactor
1477
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
199,877,048✔
1478
    pResultInfo->current = pResultInfo->numOfRows;
199,877,048✔
1479

1480
    (*rows) = pResultInfo->row;
199,877,048✔
1481
    (*numOfRows) = pResultInfo->numOfRows;
199,877,048✔
1482
    return pRequest->code;
199,877,048✔
1483
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
4,060,862✔
1484
    SReqResultInfo *pResultInfo = NULL;
4,061,343✔
1485
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
4,061,343✔
1486
    if (code != 0) return code;
4,061,343✔
1487

1488
    pResultInfo->current = pResultInfo->numOfRows;
3,722,111✔
1489
    (*rows) = pResultInfo->row;
3,722,111✔
1490
    (*numOfRows) = pResultInfo->numOfRows;
3,722,111✔
1491
    return 0;
3,722,111✔
1492
  } else {
1493
    tscError("taos_fetch_block_s invalid res type");
×
1494
    return TSDB_CODE_TMQ_INVALID_DATA;
×
1495
  }
1496
}
1497

1498
int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) {
64,853✔
1499
  *numOfRows = 0;
64,853✔
1500
  *pData = NULL;
64,853✔
1501

1502
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
64,853✔
1503
    return 0;
×
1504
  }
1505

1506
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
64,853✔
1507
    SReqResultInfo *pResultInfo = NULL;
61,253✔
1508
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
61,253✔
1509
    if (code != 0) {
61,253✔
1510
      (*numOfRows) = 0;
971✔
1511
      return 0;
971✔
1512
    }
1513

1514
    pResultInfo->current = pResultInfo->numOfRows;
60,282✔
1515
    (*numOfRows) = pResultInfo->numOfRows;
60,282✔
1516
    (*pData) = (void *)pResultInfo->pData;
60,282✔
1517
    return 0;
60,282✔
1518
  }
1519

1520
  SRequestObj *pRequest = (SRequestObj *)res;
3,600✔
1521

1522
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
3,600✔
1523
      pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
3,600✔
1524
    return pRequest->code;
×
1525
  }
1526

1527
  (void)doAsyncFetchRows(pRequest, false, false);
3,600✔
1528

1529
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
3,600✔
1530

1531
  pResultInfo->current = pResultInfo->numOfRows;
3,600✔
1532
  (*numOfRows) = pResultInfo->numOfRows;
3,600✔
1533
  (*pData) = (void *)pResultInfo->pData;
3,600✔
1534

1535
  return pRequest->code;
3,600✔
1536
}
1537

1538
int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) {
124,845,083✔
1539
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
124,845,083✔
1540
    return 0;
×
1541
  }
1542

1543
  int32_t numOfFields = taos_num_fields(res);
124,845,083✔
1544
  if (columnIndex < 0 || columnIndex >= numOfFields || numOfFields == 0) {
124,845,083✔
1545
    return 0;
×
1546
  }
1547

1548
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
124,845,083✔
1549
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
124,845,083✔
1550
  if (!IS_VAR_DATA_TYPE(pField->type)) {
124,845,083✔
1551
    return 0;
×
1552
  }
1553

1554
  return pResInfo->pCol[columnIndex].offset;
124,845,083✔
1555
}
1556

1557
int taos_is_null_by_column(TAOS_RES *res, int columnIndex, bool result[], int *rows) {
356,175,052✔
1558
  if (res == NULL || result == NULL || rows == NULL || *rows <= 0 || columnIndex < 0 || TD_RES_TMQ_META(res) ||
356,175,052✔
1559
      TD_RES_TMQ_RAW(res) || TD_RES_TMQ_BATCH_META(res)) {
356,176,982✔
1560
    return TSDB_CODE_INVALID_PARA;
×
1561
  }
1562

1563
  int32_t numOfFields = taos_num_fields(res);
356,174,350✔
1564
  if (columnIndex >= numOfFields || numOfFields == 0) {
356,176,982✔
1565
    return TSDB_CODE_INVALID_PARA;
×
1566
  }
1567

1568
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
356,176,982✔
1569
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
356,176,982✔
1570
  SResultColumn  *pCol = &pResInfo->pCol[columnIndex];
356,175,578✔
1571

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

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

1598
  int code = taos_errno(pObj);
×
1599

1600
  taos_free_result(pObj);
×
1601
  return code;
×
1602
}
1603

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

1611
  resetConnectDB(pTscObj);
×
1612

1613
  releaseTscObj(*(int64_t *)taos);
×
1614
}
1615

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

1623
  releaseTscObj(*(int64_t *)taos);
4,630✔
1624

1625
  return pTscObj->sDetailVer;
4,630✔
1626
}
1627

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

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

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

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

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

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

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

1696
    default:
×
1697
        TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1698
  }
1699

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

1706
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
1,572,573,931✔
1707
  if (NULL == pWrapper) {
1,572,573,931✔
1708
    return;
789,735,932✔
1709
  }
1710
  destoryCatalogReq(pWrapper->pCatalogReq);
782,837,999✔
1711
  taosMemoryFree(pWrapper->pCatalogReq);
782,836,682✔
1712
  qDestroyParseContext(pWrapper->pParseCtx);
782,834,524✔
1713
  taosMemoryFree(pWrapper);
782,831,903✔
1714
}
1715

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

1724
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
252,151,835✔
1725
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
252,151,835✔
1726
  SRequestObj         *pRequest = pWrapper->pRequest;
252,151,835✔
1727
  SQuery              *pQuery = pRequest->pQuery;
252,152,326✔
1728

1729
  qDebug("req:0x%" PRIx64 ", start to semantic analysis, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
252,152,332✔
1730

1731
  int64_t analyseStart = taosGetTimestampUs();
252,152,280✔
1732
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
252,152,280✔
1733
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
252,151,878✔
1734

1735
  if (TSDB_CODE_SUCCESS == code) {
252,151,887✔
1736
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
252,151,619✔
1737
  }
1738

1739
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
252,145,382✔
1740

1741
  if (pRequest->parseOnly) {
252,149,687✔
1742
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
9,007✔
1743
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
9,007✔
1744
  }
1745

1746
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
252,150,506✔
1747
}
252,148,392✔
1748

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

1776
    *ppTarget = pTarget;
×
1777
  }
1778

1779
  return code;
×
1780
}
1781

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

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

1813
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
252,140,171✔
1814
  SRequestObj *pRequest = pWrapper->pRequest;
252,140,171✔
1815
  SQuery      *pQuery = pRequest->pQuery;
252,147,070✔
1816

1817
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
252,148,721✔
1818
    SNode *prevRoot = pQuery->pPrevRoot;
×
1819
    pQuery->pPrevRoot = NULL;
×
1820
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
×
1821
    return;
×
1822
  }
1823

1824
  if (code == TSDB_CODE_SUCCESS) {
252,149,044✔
1825
    pRequest->stableQuery = pQuery->stableQuery;
197,313,910✔
1826
    if (pQuery->pRoot) {
197,311,114✔
1827
      pRequest->stmtType = pQuery->pRoot->type;
197,311,506✔
1828
    }
1829

1830
    if (pQuery->haveResultSet) {
197,308,754✔
1831
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
131,357,711✔
1832
                              pRequest->stmtBindVersion > 0);
131,357,543✔
1833
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
131,360,120✔
1834
    }
1835
  }
1836

1837
  if (code == TSDB_CODE_SUCCESS) {
252,143,122✔
1838
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
197,313,252✔
1839
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
197,304,210✔
1840
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
197,300,234✔
1841

1842
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
197,306,235✔
1843
  } else {
1844
    destorySqlCallbackWrapper(pWrapper);
54,829,870✔
1845
    pRequest->pWrapper = NULL;
54,829,870✔
1846
    qDestroyQuery(pRequest->pQuery);
54,829,870✔
1847
    pRequest->pQuery = NULL;
54,829,870✔
1848

1849
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
54,829,870✔
1850
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
2,042,639✔
1851
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1852
      restartAsyncQuery(pRequest, code);
2,042,639✔
1853
      return;
2,042,639✔
1854
    }
1855

1856
    // return to app directly
1857
    tscError("req:0x%" PRIx64 ", error occurs, code:%s, return to user app, QID:0x%" PRIx64, pRequest->self,
52,787,231✔
1858
             tstrerror(code), pRequest->requestId);
1859
    pRequest->code = code;
52,787,231✔
1860
    returnToUser(pRequest);
52,787,231✔
1861
  }
1862
}
1863

1864
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
258,551,455✔
1865
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
516,356,702✔
1866
                           .requestId = pWrapper->pParseCtx->requestId,
258,556,726✔
1867
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
258,555,940✔
1868
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
258,554,940✔
1869

1870
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
516,363,674✔
1871

1872
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
259,303,022✔
1873
                                &pWrapper->pRequest->body.queryJob);
258,557,133✔
1874
}
1875

1876
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code);
1877

1878
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
779,360,108✔
1879
  int32_t code = TSDB_CODE_SUCCESS;
779,360,108✔
1880
  switch (pWrapper->pRequest->pQuery->execStage) {
779,360,108✔
1881
    case QUERY_EXEC_STAGE_PARSE: {
6,411,917✔
1882
      // continue parse after get metadata
1883
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
6,411,917✔
1884
      break;
6,411,930✔
1885
    }
1886
    case QUERY_EXEC_STAGE_ANALYSE: {
252,147,506✔
1887
      // analysis after get metadata
1888
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
252,147,506✔
1889
      break;
252,147,170✔
1890
    }
1891
    case QUERY_EXEC_STAGE_SCHEDULE: {
520,808,365✔
1892
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
520,808,365✔
1893
      break;
520,807,441✔
1894
    }
1895
    default:
×
1896
      break;
×
1897
  }
1898
  return code;
779,366,527✔
1899
}
1900

1901
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code) {
6,411,930✔
1902
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
6,411,930✔
1903
  SRequestObj         *pRequest = pWrapper->pRequest;
6,411,930✔
1904
  SQuery              *pQuery = pRequest->pQuery;
6,411,930✔
1905

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

1910
  if (code == TSDB_CODE_SUCCESS) {
6,411,930✔
1911
    // pWrapper->pCatalogReq->forceUpdate = false;
1912
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
6,374,879✔
1913
  }
1914

1915
  if (TSDB_CODE_SUCCESS == code) {
6,411,930✔
1916
    code = phaseAsyncQuery(pWrapper);
5,876,794✔
1917
  }
1918

1919
  if (TSDB_CODE_SUCCESS != code) {
6,411,930✔
1920
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
535,136✔
1921
             tstrerror(code), pWrapper->pRequest->requestId);
1922
    destorySqlCallbackWrapper(pWrapper);
535,136✔
1923
    pRequest->pWrapper = NULL;
535,136✔
1924
    terrno = code;
535,136✔
1925
    pRequest->code = code;
535,136✔
1926
    doRequestCallback(pRequest, code);
535,136✔
1927
  }
1928
}
6,411,930✔
1929

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

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

1947
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
120,783✔
1948
  int64_t connId = *(int64_t *)taos;
120,783✔
1949
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
120,783✔
1950
}
120,783✔
1951

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

1957
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
782,834,040✔
1958
  const STscObj *pTscObj = pRequest->pTscObj;
782,834,040✔
1959

1960
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
782,838,685✔
1961
  if (*pCxt == NULL) {
782,831,465✔
1962
    return terrno;
×
1963
  }
1964

1965
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
1,562,662,504✔
1966
                           .requestRid = pRequest->self,
782,833,193✔
1967
                           .acctId = pTscObj->acctId,
782,836,479✔
1968
                           .db = pRequest->pDb,
782,836,387✔
1969
                           .topicQuery = false,
1970
                           .pSql = pRequest->sqlstr,
782,841,139✔
1971
                           .sqlLen = pRequest->sqlLen,
782,841,176✔
1972
                           .pMsg = pRequest->msgBuf,
782,839,472✔
1973
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1974
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
782,836,058✔
1975
                           .pStmtCb = NULL,
1976
                           .pUser = pTscObj->user,
782,840,231✔
1977
                           .userId = pTscObj->userId,
782,840,915✔
1978
                           .pEffectiveUser = pRequest->effectiveUser,
782,840,141✔
1979
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
782,837,845✔
1980
                           .enableSysInfo = pTscObj->sysInfo,
782,840,117✔
1981
                           .async = true,
1982
                           .svrVer = pTscObj->sVer,
782,838,943✔
1983
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
782,840,578✔
1984
                           .allocatorId = pRequest->allocatorRefId,
782,839,154✔
1985
                           .parseSqlFp = clientParseSql,
1986
                           .parseSqlParam = pWrapper,
1987
                           .setQueryFp = setQueryRequest,
1988
                           .timezone = pTscObj->optionInfo.timezone,
782,835,520✔
1989
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
782,840,533✔
1990
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
782,837,884✔
1991
  (*pCxt)->biMode = biMode;
782,833,492✔
1992
  return TSDB_CODE_SUCCESS;
782,834,402✔
1993
}
1994

1995
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
782,833,644✔
1996
  int32_t              code = TSDB_CODE_SUCCESS;
782,833,644✔
1997
  STscObj             *pTscObj = pRequest->pTscObj;
782,833,644✔
1998
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
782,838,773✔
1999
  if (pWrapper == NULL) {
782,835,281✔
2000
    code = terrno;
×
2001
  } else {
2002
    pWrapper->pRequest = pRequest;
782,835,281✔
2003
    pRequest->pWrapper = pWrapper;
782,834,334✔
2004
    *ppWrapper = pWrapper;
782,834,757✔
2005
  }
2006

2007
  if (TSDB_CODE_SUCCESS == code) {
782,836,669✔
2008
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
782,834,849✔
2009
  }
2010

2011
  if (TSDB_CODE_SUCCESS == code) {
782,836,927✔
2012
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
782,836,927✔
2013
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
782,840,572✔
2014
  }
2015

2016
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
782,834,210✔
2017
    int64_t syntaxStart = taosGetTimestampUs();
782,838,942✔
2018

2019
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
782,838,942✔
2020
    if (pWrapper->pCatalogReq == NULL) {
782,830,318✔
2021
      code = terrno;
×
2022
    } else {
2023
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
782,831,724✔
2024
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
782,834,820✔
2025
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
782,836,553✔
2026
    }
2027

2028
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
782,830,720✔
2029
  }
2030

2031
  return code;
782,840,747✔
2032
}
2033

2034
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
783,515,319✔
2035
  SSqlCallbackWrapper *pWrapper = NULL;
783,515,319✔
2036
  int32_t              code = TSDB_CODE_SUCCESS;
783,519,563✔
2037

2038
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
783,519,563✔
2039
    code = pRequest->prevCode;
681,785✔
2040
    terrno = code;
681,785✔
2041
    pRequest->code = code;
681,785✔
2042
    tscDebug("req:0x%" PRIx64 ", call sync query cb with code:%s", pRequest->self, tstrerror(code));
681,785✔
2043
    doRequestCallback(pRequest, code);
681,785✔
2044
    return;
681,785✔
2045
  }
2046

2047
  if (TSDB_CODE_SUCCESS == code) {
782,834,716✔
2048
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
782,832,874✔
2049
  }
2050

2051
  if (TSDB_CODE_SUCCESS == code) {
782,836,142✔
2052
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
773,483,138✔
2053
    code = phaseAsyncQuery(pWrapper);
773,487,386✔
2054
  }
2055

2056
  if (TSDB_CODE_SUCCESS != code) {
782,832,947✔
2057
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
9,352,608✔
2058
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
9,288,514✔
2059
               pRequest->requestId);
2060
    } else {
2061
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
64,094✔
2062
               pRequest->requestId);
2063
    }
2064

2065
    destorySqlCallbackWrapper(pWrapper);
9,352,608✔
2066
    pRequest->pWrapper = NULL;
9,352,608✔
2067
    qDestroyQuery(pRequest->pQuery);
9,352,608✔
2068
    pRequest->pQuery = NULL;
9,352,608✔
2069

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

2083
    terrno = code;
9,342,913✔
2084
    pRequest->code = code;
9,342,913✔
2085
    doRequestCallback(pRequest, code);
9,342,913✔
2086
  }
2087
}
2088

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

2121
typedef struct SAsyncFetchParam {
2122
  SRequestObj      *pReq;
2123
  __taos_async_fn_t fp;
2124
  void             *param;
2125
} SAsyncFetchParam;
2126

2127
static int32_t doAsyncFetch(void *pParam) {
153,987,509✔
2128
  SAsyncFetchParam *param = pParam;
153,987,509✔
2129
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
153,987,509✔
2130
  taosMemoryFree(param);
153,987,461✔
2131
  return TSDB_CODE_SUCCESS;
153,986,901✔
2132
}
2133

2134
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
153,989,903✔
2135
  if (res == NULL || fp == NULL) {
153,989,903✔
2136
    tscError("taos_fetch_rows_a invalid paras");
×
2137
    return;
×
2138
  }
2139
  if (!TD_RES_QUERY(res)) {
153,989,980✔
2140
    tscError("taos_fetch_rows_a res is NULL");
×
2141
    fp(param, res, TSDB_CODE_APP_ERROR);
×
2142
    return;
×
2143
  }
2144

2145
  SRequestObj *pRequest = res;
153,989,980✔
2146
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
153,989,980✔
2147
    fp(param, res, 0);
2,500✔
2148
    return;
2,500✔
2149
  }
2150

2151
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
153,987,359✔
2152
  if (!pParam) {
153,987,436✔
2153
    fp(param, res, terrno);
×
2154
    return;
×
2155
  }
2156
  pParam->pReq = pRequest;
153,987,436✔
2157
  pParam->fp = fp;
153,987,436✔
2158
  pParam->param = param;
153,987,411✔
2159
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
153,987,461✔
2160
  if (TSDB_CODE_SUCCESS != code) {
153,987,534✔
2161
    taosMemoryFree(pParam);
×
2162
    fp(param, res, code);
×
2163
    return;
×
2164
  }
2165
}
2166

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

2179
  // set the current block is all consumed
2180
  pResultInfo->convertUcs4 = false;
136✔
2181

2182
  // it is a local executed query, no need to do async fetch
2183
  taos_fetch_rows_a(pRequest, fp, param);
136✔
2184
}
2185

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

2197
  return pRequest->body.resInfo.pData;
80✔
2198
}
2199

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

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

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

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

2228
  SRequestConnInfo conn = {
×
2229
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2230

2231
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2232

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

2236
  code = catalogGetDBVgInfo(pCtg, &conn, dbFName, dbInfo);
×
2237
  if (code) {
×
2238
    goto _return;
×
2239
  }
2240

2241
_return:
×
2242

2243
  terrno = code;
×
2244

2245
  destroyRequest(pRequest);
×
2246
  return code;
×
2247
}
2248

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

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

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

2269
  pRequest->syncQuery = true;
×
2270

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

2278
  SRequestConnInfo conn = {
×
2279
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2280

2281
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2282

2283
  SName tableName = {0};
×
2284
  toName(pTscObj->acctId, db, table, &tableName);
×
2285

2286
  SVgroupInfo vgInfo;
×
2287
  code = catalogGetTableHashVgroup(pCtg, &conn, &tableName, &vgInfo);
×
2288
  if (code) {
×
2289
    goto _return;
×
2290
  }
2291

2292
  *vgId = vgInfo.vgId;
×
2293

2294
_return:
×
2295

2296
  terrno = code;
×
2297

2298
  destroyRequest(pRequest);
×
2299
  return code;
×
2300
}
2301

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

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

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

2322
  pRequest->syncQuery = true;
×
2323

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

2331
  SRequestConnInfo conn = {
×
2332
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2333

2334
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2335

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

2341
_return:
×
2342

2343
  terrno = code;
×
2344

2345
  destroyRequest(pRequest);
×
2346
  return code;
×
2347
}
2348

2349
int taos_load_table_info(TAOS *taos, const char *tableNameList) {
1,126✔
2350
  if (NULL == taos) {
1,126✔
2351
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2352
    return terrno;
×
2353
  }
2354

2355
  int64_t       connId = *(int64_t *)taos;
1,126✔
2356
  const int32_t MAX_TABLE_NAME_LENGTH = 12 * 1024 * 1024;  // 12MB list
1,126✔
2357
  int32_t       code = 0;
1,126✔
2358
  SRequestObj  *pRequest = NULL;
1,126✔
2359
  SCatalogReq   catalogReq = {0};
1,126✔
2360

2361
  if (NULL == tableNameList) {
1,126✔
2362
    return TSDB_CODE_SUCCESS;
×
2363
  }
2364

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

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

2380
  pRequest->syncQuery = true;
1,126✔
2381

2382
  STscObj *pTscObj = pRequest->pTscObj;
1,126✔
2383
  code = transferTableNameList(tableNameList, pTscObj->acctId, pTscObj->db, &catalogReq.pTableMeta);
1,126✔
2384
  if (code) {
1,126✔
2385
    goto _return;
×
2386
  }
2387

2388
  SCatalog *pCtg = NULL;
1,126✔
2389
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
1,126✔
2390
  if (code != TSDB_CODE_SUCCESS) {
1,126✔
2391
    goto _return;
×
2392
  }
2393

2394
  SRequestConnInfo conn = {
1,126✔
2395
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
1,126✔
2396

2397
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
1,126✔
2398

2399
  code = catalogAsyncGetAllMeta(pCtg, &conn, &catalogReq, syncCatalogFn, pRequest->body.interParam, NULL);
1,126✔
2400
  if (code) {
1,126✔
2401
    goto _return;
×
2402
  }
2403

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

2416
TAOS_STMT *taos_stmt_init(TAOS *taos) {
126,277✔
2417
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
126,277✔
2418
  if (NULL == pObj) {
126,277✔
2419
    tscError("invalid parameter for %s", __FUNCTION__);
×
2420
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2421
    return NULL;
×
2422
  }
2423

2424
  TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL);
126,277✔
2425
  if (NULL == pStmt) {
126,277✔
2426
    tscError("stmt init failed, errcode:%s", terrstr());
×
2427
  }
2428
  releaseTscObj(*(int64_t *)taos);
126,277✔
2429

2430
  return pStmt;
126,277✔
2431
}
2432

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

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

2447
  return pStmt;
×
2448
}
2449

2450
TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options) {
9,140✔
2451
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
9,140✔
2452
  if (NULL == pObj) {
9,140✔
2453
    tscError("invalid parameter for %s", __FUNCTION__);
×
2454
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2455
    return NULL;
×
2456
  }
2457

2458
  TAOS_STMT *pStmt = stmtInit(pObj, options->reqId, options);
9,140✔
2459
  if (NULL == pStmt) {
9,140✔
2460
    tscError("stmt init failed, errcode:%s", terrstr());
×
2461
  }
2462
  releaseTscObj(*(int64_t *)taos);
9,140✔
2463

2464
  return pStmt;
9,140✔
2465
}
2466

2467
int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
3,360,684✔
2468
  if (stmt == NULL || sql == NULL) {
3,360,684✔
UNCOV
2469
    tscError("NULL parameter for %s", __FUNCTION__);
×
UNCOV
2470
    terrno = TSDB_CODE_INVALID_PARA;
×
2471
    return terrno;
×
2472
  }
2473

2474
  return stmtPrepare(stmt, sql, length);
3,361,653✔
2475
}
2476

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

2484
  int32_t code = stmtSetTbName(stmt, name);
5,821✔
2485
  if (code) {
5,821✔
2486
    return code;
563✔
2487
  }
2488

2489
  if (tags) {
5,258✔
2490
    return stmtSetTbTags(stmt, tags);
5,258✔
2491
  }
2492

2493
  return TSDB_CODE_SUCCESS;
×
2494
}
2495

2496
int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name) {
5,498,532✔
2497
  if (stmt == NULL || name == NULL) {
5,498,532✔
UNCOV
2498
    tscError("NULL parameter for %s", __FUNCTION__);
×
UNCOV
2499
    terrno = TSDB_CODE_INVALID_PARA;
×
2500
    return terrno;
×
2501
  }
2502

2503
  return stmtSetTbName(stmt, name);
5,501,556✔
2504
}
2505

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

2513
  return stmtSetTbTags(stmt, tags);
176✔
2514
}
2515

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

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

2525
  return stmtGetTagFields(stmt, fieldNum, fields);
×
2526
}
2527

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

2535
  return stmtGetColFields(stmt, fieldNum, fields);
×
2536
}
2537

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

2545
int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
26,363✔
2546
  if (stmt == NULL || bind == NULL) {
26,363✔
2547
    tscError("NULL parameter for %s", __FUNCTION__);
×
2548
    terrno = TSDB_CODE_INVALID_PARA;
×
2549
    return terrno;
×
2550
  }
2551

2552
  if (bind->num > 1) {
26,363✔
2553
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
3,684✔
2554
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
3,684✔
2555
    return terrno;
3,684✔
2556
  }
2557

2558
  return stmtBindBatch(stmt, bind, -1);
22,679✔
2559
}
2560

2561
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
60,754,349✔
2562
  if (stmt == NULL || bind == NULL) {
60,754,349✔
2563
    tscError("NULL parameter for %s", __FUNCTION__);
×
2564
    terrno = TSDB_CODE_INVALID_PARA;
×
2565
    return terrno;
×
2566
  }
2567

2568
  if (bind->num <= 0 || bind->num > INT16_MAX) {
61,530,459✔
UNCOV
2569
    tscError("invalid bind num %d", bind->num);
×
UNCOV
2570
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2571
    return terrno;
×
2572
  }
2573

2574
  int32_t insert = 0;
61,831,485✔
2575
  int32_t code = stmtIsInsert(stmt, &insert);
61,653,638✔
2576
  if (TSDB_CODE_SUCCESS != code) {
61,611,373✔
2577
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2578
    return code;
×
2579
  }
2580
  if (0 == insert && bind->num > 1) {
61,611,373✔
2581
    tscError("only one row data allowed for query");
×
2582
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2583
    return terrno;
×
2584
  }
2585

2586
  return stmtBindBatch(stmt, bind, -1);
61,611,373✔
2587
}
2588

2589
int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind, int colIdx) {
1,120✔
2590
  if (stmt == NULL || bind == NULL) {
1,120✔
2591
    tscError("NULL parameter for %s", __FUNCTION__);
×
2592
    terrno = TSDB_CODE_INVALID_PARA;
×
2593
    return terrno;
×
2594
  }
2595

2596
  if (colIdx < 0) {
1,120✔
2597
    tscError("invalid bind column idx %d", colIdx);
×
2598
    terrno = TSDB_CODE_INVALID_PARA;
×
2599
    return terrno;
×
2600
  }
2601

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

2614
  return stmtBindBatch(stmt, bind, colIdx);
1,120✔
2615
}
2616

2617
int taos_stmt_add_batch(TAOS_STMT *stmt) {
58,266,594✔
2618
  if (stmt == NULL) {
58,266,594✔
2619
    tscError("NULL parameter for %s", __FUNCTION__);
×
2620
    terrno = TSDB_CODE_INVALID_PARA;
×
2621
    return terrno;
×
2622
  }
2623

2624
  return stmtAddBatch(stmt);
58,266,594✔
2625
}
2626

2627
int taos_stmt_execute(TAOS_STMT *stmt) {
3,697,130✔
2628
  if (stmt == NULL) {
3,697,130✔
2629
    tscError("NULL parameter for %s", __FUNCTION__);
×
2630
    terrno = TSDB_CODE_INVALID_PARA;
×
2631
    return terrno;
×
2632
  }
2633

2634
  return stmtExec(stmt);
3,697,130✔
2635
}
2636

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

2644
  return stmtIsInsert(stmt, insert);
×
2645
}
2646

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

2654
  return stmtGetParamNum(stmt, nums);
×
2655
}
2656

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

2664
  return stmtGetParam(stmt, idx, type, bytes);
×
2665
}
2666

2667
TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt) {
9,068✔
2668
  if (stmt == NULL) {
9,068✔
2669
    tscError("NULL parameter for %s", __FUNCTION__);
×
2670
    terrno = TSDB_CODE_INVALID_PARA;
×
2671
    return NULL;
×
2672
  }
2673

2674
  return stmtUseResult(stmt);
9,068✔
2675
}
2676

2677
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
9,626✔
2678

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

2686
  return stmtAffectedRows(stmt);
2,966✔
2687
}
2688

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

2696
  return stmtAffectedRowsOnce(stmt);
80✔
2697
}
2698

2699
int taos_stmt_close(TAOS_STMT *stmt) {
135,417✔
2700
  if (stmt == NULL) {
135,417✔
2701
    tscError("NULL parameter for %s", __FUNCTION__);
×
2702
    terrno = TSDB_CODE_INVALID_PARA;
×
2703
    return terrno;
×
2704
  }
2705

2706
  return stmtClose(stmt);
135,417✔
2707
}
2708

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

2722
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
3,139✔
2723

2724
  releaseTscObj(*(int64_t *)taos);
3,139✔
2725

2726
  return pStmt;
3,139✔
2727
}
2728

2729
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
3,192✔
2730
  if (stmt == NULL || sql == NULL) {
3,192✔
2731
    tscError("NULL parameter for %s", __FUNCTION__);
×
2732
    terrno = TSDB_CODE_INVALID_PARA;
×
2733
    return terrno;
×
2734
  }
2735

2736
  return stmtPrepare2(stmt, sql, length);
3,251✔
2737
}
2738

2739
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
553,587✔
2740
  if (stmt == NULL) {
553,587✔
2741
    tscError("NULL parameter for %s", __FUNCTION__);
×
2742
    terrno = TSDB_CODE_INVALID_PARA;
×
2743
    return terrno;
×
2744
  }
2745

2746
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
553,587✔
2747
  int32_t    code = TSDB_CODE_SUCCESS;
553,587✔
2748
  STMT2_DLOG_E("start to bind param");
553,587✔
2749

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

2760
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 1) {
553,445✔
2761
    STMT2_ELOG_E("async bind param is still working, please try again later");
84✔
2762
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
84✔
2763
    return terrno;
×
2764
  }
2765

2766
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
553,417✔
2767
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
2768
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
2769
    }
2770
    pStmt->execSemWaited = true;
×
2771
  }
2772

2773
  for (int i = 0; i < bindv->count; ++i) {
1,695,954✔
2774
    SVCreateTbReq *pCreateTbReq = NULL;
1,141,997✔
2775
    if (!isQuery) {
1,141,905✔
2776
      STMT2_TLOG("start to bind %dth table", i);
1,141,819✔
2777
      if (bindv->tbnames && bindv->tbnames[i]) {
1,141,847✔
2778
        code = stmtSetTbName2(stmt, bindv->tbnames[i]);
1,141,981✔
2779
        if (code) {
1,141,715✔
2780
          terrno = code;
×
2781
          STMT2_ELOG("set tbname failed, code:%s", tstrerror(code));
×
2782
          return terrno;
×
2783
        }
2784
      }
2785

2786
      if (bindv->tags && bindv->tags[i]) {
1,141,660✔
2787
        code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
565,690✔
2788
      } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
575,983✔
2789
        code = stmtCheckTags2(stmt, &pCreateTbReq);
111,888✔
2790
      } else if (pStmt->sql.autoCreateTbl) {
463,359✔
2791
        code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
×
2792
      }
2793

2794
      if (code) {
1,141,341✔
2795
        terrno = code;
×
2796
        STMT2_ELOG("set tags failed, code:%s", tstrerror(code));
×
2797
        return terrno;
×
2798
      }
2799
    }
2800

2801
    if (bindv->bind_cols && bindv->bind_cols[i]) {
1,141,427✔
2802
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
1,142,085✔
2803

2804
      if (bind->num <= 0 || bind->num > INT16_MAX) {
1,142,085✔
2805
        STMT2_ELOG("bind num:%d must > 0 and < INT16_MAX", bind->num);
276✔
2806
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
276✔
2807
        return terrno;
×
2808
      }
2809

2810
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
1,141,607✔
2811
      if (TSDB_CODE_SUCCESS != code) {
1,142,367✔
2812
        terrno = code;
×
2813
        STMT2_ELOG("bind batch failed, code:%s", tstrerror(code));
×
2814
        return terrno;
×
2815
      }
2816
    }
2817
  }
2818

2819
  return code;
554,095✔
2820
}
2821

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

2829
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2830

2831
  ThreadArgs *args = (ThreadArgs *)taosMemoryMalloc(sizeof(ThreadArgs));
×
2832
  args->stmt = stmt;
×
2833
  args->bindv = bindv;
×
2834
  args->col_idx = col_idx;
×
2835
  args->fp = fp;
×
2836
  args->param = param;
×
2837

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

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

2858
  return code_s;
×
2859
}
2860

2861
int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows) {
553,947✔
2862
  if (stmt == NULL) {
553,947✔
2863
    tscError("NULL parameter for %s", __FUNCTION__);
×
2864
    terrno = TSDB_CODE_INVALID_PARA;
×
2865
    return terrno;
×
2866
  }
2867

2868
  return stmtExec2(stmt, affected_rows);
553,947✔
2869
}
2870

2871
int taos_stmt2_close(TAOS_STMT2 *stmt) {
3,139✔
2872
  if (stmt == NULL) {
3,139✔
2873
    tscError("NULL parameter for %s", __FUNCTION__);
×
2874
    terrno = TSDB_CODE_INVALID_PARA;
×
2875
    return terrno;
×
2876
  }
2877

2878
  return stmtClose2(stmt);
3,139✔
2879
}
2880

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

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

2898
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
22✔
2899
  STMT2_DLOG_E("start to get fields");
22✔
2900

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

2909
  tscError("Invalid sql for stmt %s", pStmt->sql.sqlStr);
×
2910
  return TSDB_CODE_PAR_SYNTAX_ERROR;
×
2911
}
2912

2913
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_ALL *fields) {
22✔
2914
  (void)stmt;
2915
  if (!fields) return;
22✔
2916
  taosMemoryFree(fields);
22✔
2917
}
2918

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

2926
  return stmtUseResult2(stmt);
8✔
2927
}
2928

2929
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmtErrstr2(stmt); }
×
2930

2931
int taos_set_conn_mode(TAOS *taos, int mode, int value) {
2,028✔
2932
  int32_t code = 0;
2,028✔
2933
  if (taos == NULL) {
2,028✔
2934
    terrno = TSDB_CODE_INVALID_PARA;
×
2935
    return terrno;
×
2936
  }
2937

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

2956
char *getBuildInfo() { return td_buildinfo; }
×
2957

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

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

2972
  code = tscCheckConnSessionMetric(pObj);
×
2973
  TAOS_CHECK_GOTO(code, &lino, _error);
×
2974

2975
_error:
×
2976
  releaseTscObj(*(int64_t *)taos);
×
2977

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

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

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

2999
  char *sql = taosMemoryMalloc((size_t)len + 1);
×
3000
  if (sql == NULL) {
×
3001
    return terrno;
×
3002
  }
3003

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

3015
  *ppSql = sql;
×
3016
  if (pLen != NULL) {
×
3017
    *pLen = (uint32_t)len;
×
3018
  }
3019
  return TSDB_CODE_SUCCESS;
×
3020
}
3021

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

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

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

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

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

3053
  pRequest->type = TDMT_MND_REGISTER_INSTANCE;
×
3054
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsg, .len = msgLen, .handle = NULL};
×
3055

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

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

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

3078
  code = pRequest->code;
×
3079
  terrno = code;
×
3080

3081
_cleanup:
×
3082
  destroyRequest(pRequest);
×
3083
  return code;
×
3084
}
3085

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

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

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

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

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

3127
  int32_t code = taos_init();
×
3128
  if (code != TSDB_CODE_SUCCESS) {
×
3129
    return code;
×
3130
  }
3131

3132
  SConfig *pCfg = taosGetCfg();
×
3133
  if (pCfg == NULL) {
×
3134
    return terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3135
  }
3136

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

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

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

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

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

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

3179
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
×
3180
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
×
3181
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
×
3182
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
×
3183
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
×
3184

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

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

3198
  // Prepare epSet
3199
  tstrncpy(epSet.eps[0].fqdn, firstEp.fqdn, TSDB_FQDN_LEN);
×
3200
  epSet.eps[0].port = firstEp.port;
×
3201

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

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

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

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

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

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

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

3256
  if (rpcRsp.pCont != NULL) {
×
3257
    rpcFreeCont(rpcRsp.pCont);
×
3258
  }
3259
  rpcClose(clientRpc);
×
3260

3261
  terrno = code;
×
3262
  return code;
×
3263
}
3264

3265
int32_t taos_list_instances(const char *filter_type, char ***pList, int32_t *pCount) {
×
3266
  if (pList == NULL || pCount == NULL) {
×
3267
    return TSDB_CODE_INVALID_PARA;
×
3268
  }
3269

3270
  int32_t code = taos_init();
×
3271
  if (code != TSDB_CODE_SUCCESS) {
×
3272
    terrno = code;
×
3273
    return code;
×
3274
  }
3275

3276
  SConfig *pCfg = taosGetCfg();
×
3277
  if (pCfg == NULL) {
×
3278
    terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3279
    return TSDB_CODE_CFG_NOT_FOUND;
×
3280
  }
3281

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

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

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

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

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

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

3327
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
×
3328
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
×
3329
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
×
3330
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
×
3331
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
×
3332

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

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

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

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

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

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

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

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

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

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

3436
  if (rpcRsp.pCont != NULL) {
×
3437
    rpcFreeCont(rpcRsp.pCont);
×
3438
  }
3439
  rpcClose(clientRpc);
×
3440

3441
  return TSDB_CODE_SUCCESS;
×
3442
}
3443

3444
void taos_free_instances(char ***pList, int32_t count) {
×
3445
  if (pList == NULL || *pList == NULL || count <= 0) {
×
3446
    return;
×
3447
  }
3448

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

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