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

taosdata / TDengine / #5011

03 Apr 2026 03:59PM UTC coverage: 72.3% (+0.008%) from 72.292%
#5011

push

travis-ci

web-flow
merge: from main to 3.0 branch #35067

4053 of 5985 new or added lines in 68 files covered. (67.72%)

732 existing lines in 143 files now uncovered.

257430 of 356056 relevant lines covered (72.3%)

131834103.52 hits per line

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

51.87
/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 "clientSession.h"
21
#include "clientStmt.h"
22
#include "clientStmt2.h"
23
#include "functionMgt.h"
24
#include "os.h"
25
#include "query.h"
26
#include "scheduler.h"
27
#include "tcompare.h"
28
#include "tconv.h"
29
#include "tdatablock.h"
30
#include "tglobal.h"
31
#include "tmisce.h"
32
#include "tmsg.h"
33
#include "tref.h"
34
#include "trpc.h"
35
#include "ttime.h"
36
#include "tversion.h"
37
#include "version.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, ...) {
82,182,881✔
50
  if (arg == NULL) {
82,182,881✔
51
    return TSDB_CODE_INVALID_PARA;
×
52
  }
53
  static int32_t lock = 0;
54

55
  for (int i = 1; atomic_val_compare_exchange_32(&lock, 0, 1) != 0; ++i) {
2,147,483,647✔
56
    if (i % 1000 == 0) {
2,147,483,647✔
57
      (void)sched_yield();
10,214,904✔
58
    }
59
  }
60

61
  int ret = taos_options_imp(option, (const char *)arg);
4,196,896✔
62
  atomic_store_32(&lock, 0);
82,186,729✔
63
  return ret;
82,186,729✔
64
}
65

66
#if !defined(WINDOWS) && !defined(TD_ASTRA)
67
static void freeTz(void *p) {
2,340✔
68
  timezone_t tz = *(timezone_t *)p;
2,340✔
69
  tzfree(tz);
2,340✔
70
}
2,340✔
71
#endif
72

73
int32_t tzInit() {
1,447,827✔
74
#if !defined(WINDOWS) && !defined(TD_ASTRA)
75
  pTimezoneMap = taosHashInit(0, MurmurHash3_32, false, HASH_ENTRY_LOCK);
1,447,827✔
76
  if (pTimezoneMap == NULL) {
1,447,827✔
77
    return terrno;
×
78
  }
79
  taosHashSetFreeFp(pTimezoneMap, freeTz);
1,447,827✔
80

81
  pTimezoneNameMap = taosHashInit(0, taosIntHash_64, false, HASH_ENTRY_LOCK);
1,447,827✔
82
  if (pTimezoneNameMap == NULL) {
1,447,827✔
83
    return terrno;
×
84
  }
85
#endif
86
  return 0;
1,447,827✔
87
}
88

89
void tzCleanup() {
1,447,866✔
90
#if !defined(WINDOWS) && !defined(TD_ASTRA)
91
  taosHashCleanup(pTimezoneMap);
1,447,866✔
92
  taosHashCleanup(pTimezoneNameMap);
1,447,866✔
93
#endif
94
}
1,447,866✔
95

96
#if !defined(WINDOWS) && !defined(TD_ASTRA)
97
static timezone_t setConnnectionTz(const char *val) {
3,900✔
98
  timezone_t  tz = NULL;
3,900✔
99
  timezone_t *tmp = taosHashGet(pTimezoneMap, val, strlen(val));
3,900✔
100
  if (tmp != NULL && *tmp != NULL) {
3,900✔
101
    tz = *tmp;
1,560✔
102
    goto END;
1,560✔
103
  }
104

105
  tscDebug("set timezone to %s", val);
2,340✔
106
  tz = tzalloc(val);
2,340✔
107
  if (tz == NULL) {
2,340✔
108
    tscWarn("%s unknown timezone %s change to UTC", __func__, val);
195✔
109
    tz = tzalloc("UTC");
195✔
110
    if (tz == NULL) {
195✔
111
      tscError("%s set timezone UTC error", __func__);
×
112
      terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
113
      goto END;
×
114
    }
115
  }
116
  int32_t code = taosHashPut(pTimezoneMap, val, strlen(val), &tz, sizeof(timezone_t));
2,340✔
117
  if (code != 0) {
2,340✔
118
    tscError("%s put timezone to tz map error:%d", __func__, code);
×
119
    tzfree(tz);
×
120
    tz = NULL;
×
121
    goto END;
×
122
  }
123

124
  time_t tx1 = taosGetTimestampSec();
2,340✔
125
  char   output[TD_TIMEZONE_LEN] = {0};
2,340✔
126
  code = taosFormatTimezoneStr(tx1, val, tz, output);
2,340✔
127
  if (code == 0) {
2,340✔
128
    code = taosHashPut(pTimezoneNameMap, &tz, sizeof(timezone_t), output, strlen(output) + 1);
2,340✔
129
  }
130
  if (code != 0) {
2,340✔
131
    tscError("failed to put timezone %s to map", val);
×
132
  }
133

134
END:
2,340✔
135
  return tz;
3,900✔
136
}
137
#endif
138

139
static int32_t setConnectionOption(TAOS *taos, TSDB_OPTION_CONNECTION option, const char *val) {
9,293✔
140
  if (taos == NULL) {
9,293✔
141
    return terrno = TSDB_CODE_INVALID_PARA;
195✔
142
  }
143

144
#ifdef WINDOWS
145
  if (option == TSDB_OPTION_CONNECTION_TIMEZONE) {
146
    return terrno = TSDB_CODE_NOT_SUPPORTTED_IN_WINDOWS;
147
  }
148
#endif
149

150
  if (option < TSDB_OPTION_CONNECTION_CLEAR || option >= TSDB_MAX_OPTIONS_CONNECTION) {
9,098✔
151
    return terrno = TSDB_CODE_INVALID_PARA;
195✔
152
  }
153

154
  int32_t code = taos_init();
8,903✔
155
  // initialize global config
156
  if (code != 0) {
8,903✔
157
    return terrno = code;
×
158
  }
159

160
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
8,903✔
161
  if (NULL == pObj) {
8,903✔
162
    tscError("invalid parameter for %s", __func__);
×
163
    return terrno;
×
164
  }
165

166
  if (option == TSDB_OPTION_CONNECTION_CLEAR) {
8,903✔
167
    val = NULL;
195✔
168
  }
169

170
#ifndef DISALLOW_NCHAR_WITHOUT_ICONV
171
  if (option == TSDB_OPTION_CONNECTION_CHARSET || option == TSDB_OPTION_CONNECTION_CLEAR) {
8,903✔
172
    if (val != NULL) {
1,560✔
173
      if (!taosValidateEncodec(val)) {
975✔
174
        code = terrno;
195✔
175
        goto END;
195✔
176
      }
177
      void *tmp = taosConvInit(val);
780✔
178
      if (tmp == NULL) {
780✔
179
        code = terrno;
195✔
180
        goto END;
195✔
181
      }
182
      pObj->optionInfo.charsetCxt = tmp;
585✔
183
    } else {
184
      pObj->optionInfo.charsetCxt = NULL;
585✔
185
    }
186
  }
187
#endif
188
  if (option == TSDB_OPTION_CONNECTION_TIMEZONE || option == TSDB_OPTION_CONNECTION_CLEAR) {
8,513✔
189
#if !defined(WINDOWS) && !defined(TD_ASTRA)
190
    if (val != NULL) {
4,290✔
191
      if (val[0] == 0) {
3,900✔
192
        val = "UTC";
195✔
193
      }
194
      timezone_t tz = setConnnectionTz(val);
3,900✔
195
      if (tz == NULL) {
3,900✔
196
        code = terrno;
×
197
        goto END;
×
198
      }
199
      pObj->optionInfo.timezone = tz;
3,900✔
200
    } else {
201
      pObj->optionInfo.timezone = NULL;
390✔
202
    }
203
#endif
204
  }
205

206
  if (option == TSDB_OPTION_CONNECTION_USER_APP || option == TSDB_OPTION_CONNECTION_CLEAR) {
8,513✔
207
    if (val != NULL) {
1,039✔
208
      tstrncpy(pObj->optionInfo.userApp, val, sizeof(pObj->optionInfo.userApp));
649✔
209
    } else {
210
      pObj->optionInfo.userApp[0] = 0;
390✔
211
    }
212
  }
213

214
  if (option == TSDB_OPTION_CONNECTION_CONNECTOR_INFO || option == TSDB_OPTION_CONNECTION_CLEAR) {
8,513✔
215
    if (val != NULL) {
1,039✔
216
      tstrncpy(pObj->optionInfo.cInfo, val, sizeof(pObj->optionInfo.cInfo));
649✔
217
    } else {
218
      pObj->optionInfo.cInfo[0] = 0;
390✔
219
    }
220
  }
221

222
  if (option == TSDB_OPTION_CONNECTION_USER_IP || option == TSDB_OPTION_CONNECTION_CLEAR) {
8,513✔
223
    SIpRange dualIp = {0};
1,755✔
224
    if (val != NULL) {
1,755✔
225
      pObj->optionInfo.userIp = taosInetAddr(val);
1,170✔
226
      SIpAddr addr = {0};
1,170✔
227
      code = taosGetIpFromFqdn(tsEnableIpv6, val, &addr);
1,170✔
228
      if (code == 0) {
1,170✔
229
        code = tIpStrToUint(&addr, &pObj->optionInfo.userDualIp);
585✔
230
      }
231
      if (code != 0) {
1,170✔
232
        tscError("ipv6 flag %d failed to convert user ip %s to dual ip since %s", tsEnableIpv6 ? 1 : 0, val,
585✔
233
                 tstrerror(code));
234
        pObj->optionInfo.userIp = INADDR_NONE;
585✔
235
        pObj->optionInfo.userDualIp = dualIp;
585✔
236
        code = 0;
585✔
237
      }
238
    } else {
239
      pObj->optionInfo.userIp = INADDR_NONE;
585✔
240
      pObj->optionInfo.userDualIp = dualIp;
585✔
241
    }
242
  }
243

244
END:
6,758✔
245
  releaseTscObj(*(int64_t *)taos);
8,903✔
246
  return terrno = code;
8,903✔
247
}
248

249
int taos_options_connection(TAOS *taos, TSDB_OPTION_CONNECTION option, const void *arg, ...) {
9,293✔
250
  return setConnectionOption(taos, option, (const char *)arg);
9,293✔
251
}
252

253
// this function may be called by user or system, or by both simultaneously.
254
void taos_cleanup(void) {
1,448,718✔
255
  tscInfo("start to cleanup client environment");
1,448,718✔
256
  if (atomic_val_compare_exchange_32(&sentinel, TSC_VAR_NOT_RELEASE, TSC_VAR_RELEASED) != TSC_VAR_NOT_RELEASE) {
1,448,718✔
257
    return;
852✔
258
  }
259

260
  monitorClose();
1,447,866✔
261
  tscStopCrashReport();
1,447,866✔
262

263
  hbMgrCleanUp();
1,447,866✔
264

265
  catalogDestroy();
1,447,866✔
266
  schedulerDestroy();
1,447,866✔
267

268
  fmFuncMgtDestroy();
1,447,866✔
269
  qCleanupKeywordsTable();
1,447,866✔
270

271
#if !defined(WINDOWS) && !defined(TD_ASTRA)
272
  tzCleanup();
1,447,866✔
273
#endif
274
  tmqMgmtClose();
1,447,866✔
275

276
  int32_t id = clientReqRefPool;
1,447,866✔
277
  clientReqRefPool = -1;
1,447,866✔
278
  taosCloseRef(id);
1,447,866✔
279

280
  id = clientConnRefPool;
1,447,866✔
281
  clientConnRefPool = -1;
1,447,866✔
282
  taosCloseRef(id);
1,447,866✔
283

284
  nodesDestroyAllocatorSet();
1,447,866✔
285
  cleanupAppInfo();
1,447,866✔
286
  rpcCleanup();
1,447,866✔
287
  tscDebug("rpc cleanup");
1,447,866✔
288

289
  if (TSDB_CODE_SUCCESS != cleanupTaskQueue()) {
1,447,866✔
290
    tscWarn("failed to cleanup task queue");
×
291
  }
292

293
  sessMgtDestroy();
1,447,866✔
294

295
  taosConvDestroy();
1,447,866✔
296
  DestroyRegexCache();
1,447,866✔
297
#ifdef TAOSD_INTEGRATED
298
  shellStopDaemon();
299
#endif
300
  tscInfo("all local resources released");
1,447,866✔
301
  taosCleanupCfg();
1,447,866✔
302
#ifndef TAOSD_INTEGRATED
303
  taosCloseLog();
1,447,866✔
304
#endif
305
}
306

307
static setConfRet taos_set_config_imp(const char *config) {
14✔
308
  setConfRet ret = {SET_CONF_RET_SUCC, {0}};
14✔
309
  // TODO: need re-implementation
310
  return ret;
14✔
311
}
312

313
setConfRet taos_set_config(const char *config) {
14✔
314
  // TODO  pthread_mutex_lock(&setConfMutex);
315
  setConfRet ret = taos_set_config_imp(config);
14✔
316
  //  pthread_mutex_unlock(&setConfMutex);
317
  return ret;
14✔
318
}
319

320
TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) {
84,129,624✔
321
  tscInfo("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
84,129,624✔
322
  if (user == NULL) {
84,129,585✔
323
    user = TSDB_DEFAULT_USER;
189,695✔
324
  }
325

326
  if (pass == NULL) {
84,129,585✔
327
    pass = TSDB_DEFAULT_PASS;
189,694✔
328
  }
329

330
  STscObj *pObj = NULL;
84,129,585✔
331
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
84,129,585✔
332
  if (TSDB_CODE_SUCCESS == code) {
84,115,617✔
333
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
84,110,273✔
334
    if (NULL == rid) {
84,115,974✔
335
      tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db);
×
336
      return NULL;
×
337
    }
338
    *rid = pObj->id;
84,115,974✔
339
    return (TAOS *)rid;
84,115,223✔
340
  } else {
341
    terrno = code;
11,084✔
342
  }
343

344
  return NULL;
11,230✔
345
}
346

347
void taos_set_option(OPTIONS *options, const char *key, const char *value) {
1,170✔
348
  if (options == NULL || key == NULL || value == NULL) {
1,170✔
349
    terrno = TSDB_CODE_INVALID_PARA;
×
350
    tscError("taos_set_option invalid parameter, options: %p, key: %p, value: %p", options, key, value);
×
351
    return;
×
352
  }
353

354
  size_t count = (size_t)options->count;
1,170✔
355
  size_t len = sizeof(options->keys) / sizeof(options->keys[0]);
1,170✔
356
  if (count >= len) {
1,170✔
357
    terrno = TSDB_CODE_INVALID_PARA;
×
358
    tscError("taos_set_option overflow, count: %zu, reached capacity: %zu", count, len);
×
359
    return;
×
360
  }
361

362
  options->keys[count] = key;
1,170✔
363
  options->values[count] = value;
1,170✔
364
  options->count = (uint16_t)(count + 1);
1,170✔
365
}
366

367
static int set_connection_option_or_close(TAOS *taos, TSDB_OPTION_CONNECTION option, const char *value) {
975✔
368
  if (value == NULL) return TSDB_CODE_SUCCESS;
975✔
369
  int code = taos_options_connection(taos, option, value);
×
370
  if (code != TSDB_CODE_SUCCESS) {
×
371
    tscError("failed to set option(%d): %s", (int)option, value);
×
372
    taos_close(taos);
×
373
    return code;
×
374
  }
375
  return TSDB_CODE_SUCCESS;
×
376
}
377

378
TAOS *taos_connect_with(const OPTIONS *options) {
195✔
379
  const char *ip = NULL;
195✔
380
  const char *user = NULL;
195✔
381
  const char *pass = NULL;
195✔
382
  const char *db = NULL;
195✔
383
  uint16_t    port = 0;
195✔
384

385
  const char *charset = NULL;
195✔
386
  const char *timezone = NULL;
195✔
387
  const char *userIp = NULL;
195✔
388
  const char *userApp = NULL;
195✔
389
  const char *connectorInfo = NULL;
195✔
390

391
  if (options && options->count > 0) {
195✔
392
    size_t count = (size_t)options->count;
195✔
393
    for (size_t i = 0; i < count; ++i) {
1,365✔
394
      const char *key = options->keys[i];
1,170✔
395
      const char *value = options->values[i];
1,170✔
396
      if (key == NULL || value == NULL) {
1,170✔
397
        tscWarn("taos_connect_with option key or value is NULL, index: %zu", i);
×
398
        continue;
×
399
      }
400

401
      if (strcmp(key, "ip") == 0) {
1,170✔
402
        ip = value;
195✔
403
      } else if (strcmp(key, "user") == 0) {
975✔
404
        user = value;
195✔
405
      } else if (strcmp(key, "pass") == 0) {
780✔
406
        pass = value;
195✔
407
      } else if (strcmp(key, "db") == 0) {
585✔
408
        db = value;
×
409
      } else if (strcmp(key, "port") == 0) {
585✔
410
        port = (uint16_t)taosStr2Int32(value, NULL, 10);
195✔
411
      } else if (strcmp(key, "charset") == 0) {
390✔
412
        charset = value;
×
413
      } else if (strcmp(key, "timezone") == 0) {
390✔
414
        timezone = value;
×
415
      } else if (strcmp(key, "userIp") == 0) {
390✔
416
        userIp = value;
×
417
      } else if (strcmp(key, "userApp") == 0) {
390✔
418
        userApp = value;
×
419
      } else if (strcmp(key, "connectorInfo") == 0) {
390✔
420
        connectorInfo = value;
×
421
      } else {
422
        tscWarn("taos_connect_with unknown option key: %s", key);
390✔
423
      }
424
    }
425
  }
426

427
  TAOS *taos = taos_connect(ip, user, pass, db, port);
195✔
428
  if (taos == NULL) return NULL;
195✔
429

430
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_CHARSET, charset) != TSDB_CODE_SUCCESS) return NULL;
195✔
431
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_TIMEZONE, timezone) != TSDB_CODE_SUCCESS) return NULL;
195✔
432
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_USER_IP, userIp) != TSDB_CODE_SUCCESS) return NULL;
195✔
433
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_USER_APP, userApp) != TSDB_CODE_SUCCESS) return NULL;
195✔
434
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_CONNECTOR_INFO, connectorInfo) != TSDB_CODE_SUCCESS)
195✔
435
    return NULL;
×
436

437
  return taos;
195✔
438
}
439

440
TAOS *taos_connect_with_dsn(const char *dsn) {
×
441
  terrno = TSDB_CODE_OPS_NOT_SUPPORT;
×
442
  tscError("taos_connect_with_dsn not supported");
×
443
  return NULL;
×
444
}
445

446
int taos_set_notify_cb(TAOS *taos, __taos_notify_fn_t fp, void *param, int type) {
1,330✔
447
  if (taos == NULL) {
1,330✔
448
    terrno = TSDB_CODE_INVALID_PARA;
×
449
    return terrno;
×
450
  }
451

452
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
1,330✔
453
  if (NULL == pObj) {
1,330✔
454
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
455
    tscError("invalid parameter for %s", __func__);
×
456
    return terrno;
×
457
  }
458

459
  switch (type) {
1,330✔
460
    case TAOS_NOTIFY_PASSVER: {
380✔
461
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
380✔
462
      pObj->passInfo.fp = fp;
380✔
463
      pObj->passInfo.param = param;
380✔
464
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
380✔
465
      break;
380✔
466
    }
467
    case TAOS_NOTIFY_WHITELIST_VER: {
×
468
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
469
      pObj->whiteListInfo.fp = fp;
×
470
      pObj->whiteListInfo.param = param;
×
471
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
472
      break;
×
473
    }
474
    case TAOS_NOTIFY_USER_DROPPED: {
950✔
475
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
950✔
476
      pObj->userDroppedInfo.fp = fp;
950✔
477
      pObj->userDroppedInfo.param = param;
950✔
478
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
950✔
479
      break;
950✔
480
    }
481
    case TAOS_NOTIFY_DATETIME_WHITELIST_VER: {
×
482
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
483
      pObj->dateTimeWhiteListInfo.fp = fp;
×
484
      pObj->dateTimeWhiteListInfo.param = param;
×
485
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
486
      break;
×
487
    }
488
    case TAOS_NOTIFY_TOKEN: {
×
489
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
490
      pObj->tokenNotifyInfo.fp = fp;
×
491
      pObj->tokenNotifyInfo.param = param;
×
492
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
493
      break;
×
494
    }
495
    default: {
×
496
      terrno = TSDB_CODE_INVALID_PARA;
×
497
      releaseTscObj(*(int64_t *)taos);
×
498
      return terrno;
×
499
    }
500
  }
501

502
  releaseTscObj(*(int64_t *)taos);
1,330✔
503
  return 0;
1,330✔
504
}
505

506
typedef struct SFetchWhiteListInfo {
507
  int64_t                     connId;
508
  __taos_async_whitelist_fn_t userCbFn;
509
  void                       *userParam;
510
} SFetchWhiteListInfo;
511

512
int32_t fetchWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
513
  SFetchWhiteListInfo *pInfo = (SFetchWhiteListInfo *)param;
×
514
  TAOS                *taos = &pInfo->connId;
×
515
  if (code != TSDB_CODE_SUCCESS) {
×
516
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
517
    taosMemoryFree(pMsg->pData);
×
518
    taosMemoryFree(pMsg->pEpSet);
×
519
    taosMemoryFree(pInfo);
×
520
    return code;
×
521
  }
522

523
  SGetUserIpWhiteListRsp wlRsp;
×
524
  if (TSDB_CODE_SUCCESS != tDeserializeSGetUserIpWhiteListRsp(pMsg->pData, pMsg->len, &wlRsp)) {
×
525
    taosMemoryFree(pMsg->pData);
×
526
    taosMemoryFree(pMsg->pEpSet);
×
527
    taosMemoryFree(pInfo);
×
528
    tFreeSGetUserIpWhiteListRsp(&wlRsp);
×
529
    return terrno;
×
530
  }
531

532
  uint64_t *pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(uint64_t));
×
533
  if (pWhiteLists == NULL) {
×
534
    taosMemoryFree(pMsg->pData);
×
535
    taosMemoryFree(pMsg->pEpSet);
×
536
    taosMemoryFree(pInfo);
×
537
    tFreeSGetUserIpWhiteListRsp(&wlRsp);
×
538
    return terrno;
×
539
  }
540

541
  for (int i = 0; i < wlRsp.numWhiteLists; ++i) {
×
542
    pWhiteLists[i] = ((uint64_t)wlRsp.pWhiteLists[i].mask << 32) | wlRsp.pWhiteLists[i].ip;
×
543
  }
544

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

547
  taosMemoryFree(pWhiteLists);
×
548
  taosMemoryFree(pMsg->pData);
×
549
  taosMemoryFree(pMsg->pEpSet);
×
550
  taosMemoryFree(pInfo);
×
551
  tFreeSGetUserIpWhiteListRsp(&wlRsp);
×
552
  return code;
×
553
}
554

555
void taos_fetch_whitelist_a(TAOS *taos, __taos_async_whitelist_fn_t fp, void *param) {
×
556
  if (NULL == taos) {
×
557
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
558
    return;
×
559
  }
560

561
  int64_t connId = *(int64_t *)taos;
×
562

563
  STscObj *pTsc = acquireTscObj(connId);
×
564
  if (NULL == pTsc) {
×
565
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
566
    return;
×
567
  }
568

569
  SGetUserWhiteListReq req;
×
570
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
571
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
572
  if (msgLen < 0) {
×
573
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
574
    releaseTscObj(connId);
×
575
    return;
×
576
  }
577

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

585
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
586
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
587
    taosMemoryFree(pReq);
×
588
    releaseTscObj(connId);
×
589
    return;
×
590
  }
591

592
  SFetchWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchWhiteListInfo));
×
593
  if (pParam == NULL) {
×
594
    fp(param, terrno, taos, 0, NULL);
×
595
    taosMemoryFree(pReq);
×
596
    releaseTscObj(connId);
×
597
    return;
×
598
  }
599

600
  pParam->connId = connId;
×
601
  pParam->userCbFn = fp;
×
602

603
  pParam->userParam = param;
×
604
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
605
  if (pSendInfo == NULL) {
×
606
    fp(param, terrno, taos, 0, NULL);
×
607
    taosMemoryFree(pParam);
×
608
    taosMemoryFree(pReq);
×
609
    releaseTscObj(connId);
×
610
    return;
×
611
  }
612

613
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
614
  pSendInfo->requestId = generateRequestId();
×
615
  pSendInfo->requestObjRefId = 0;
×
616
  pSendInfo->param = pParam;
×
617
  pSendInfo->fp = fetchWhiteListCallbackFn;
×
618
  pSendInfo->msgType = TDMT_MND_GET_USER_IP_WHITELIST;
×
619

620
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
621
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
622
    tscWarn("failed to async send msg to server");
×
623
  }
624
  releaseTscObj(connId);
×
625
  return;
×
626
}
627

628
typedef struct SFetchIpWhiteListInfo {
629
  int64_t connId;
630
  bool    supportNeg;
631
  void   *userParam;
632

633
  __taos_async_ip_whitelist_fn_t userCbFn;
634
} SFetchIpWhiteListInfo;
635

636
int32_t fetchIpWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
637
  int32_t lino = 0;
×
638
  char  **pWhiteLists = NULL;
×
639

640
  SGetUserIpWhiteListRsp wlRsp = {0};
×
641

642
  SFetchIpWhiteListInfo *pInfo = (SFetchIpWhiteListInfo *)param;
×
643
  TAOS                  *taos = &pInfo->connId;
×
644

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

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

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

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

668
    code = tIpUintToStr(pIpRange, &ipAddr);
×
669
    TAOS_CHECK_GOTO(code, &lino, _error);
×
670

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

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

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

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

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

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

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

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

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

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

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

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

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

787
void taos_fetch_ip_whitelist_a(TAOS *taos, __taos_async_ip_whitelist_fn_t fp, void *param) {
×
788
  taosFetchIpWhiteList(taos, fp, param, true);
×
789
}
×
790

791
typedef struct SFetchDateTimeWhiteListInfo {
792
  int64_t                              connId;
793
  void                                *userParam;
794
  __taos_async_datetime_whitelist_fn_t userCbFn;
795
} SFetchDateTimeWhiteListInfo;
796

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

802
  SUserDateTimeWhiteList wlRsp = {0};
×
803

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

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

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

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

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

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

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

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

848
  pInfo->userCbFn(pInfo->userParam, code, taos, numWhiteLists, pWhiteLists);
×
849
_error:
×
850
  if (pWhiteLists != NULL) {
×
851
    for (int32_t i = 0; i < numWhiteLists; i++) {
×
852
      taosMemFree(pWhiteLists[i]);
×
853
    }
854
    taosMemoryFree(pWhiteLists);
×
855
  }
856
  taosMemoryFree(pMsg->pData);
×
857
  taosMemoryFree(pMsg->pEpSet);
×
858
  taosMemoryFree(pInfo);
×
859
  tFreeSUserDateTimeWhiteList(&wlRsp);
×
860
  return code;
×
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
void taos_close_internal(void *taos) {
84,242,011✔
936
  if (taos == NULL) {
84,242,011✔
937
    return;
322✔
938
  }
939
  int32_t code = 0;
84,241,689✔
940

941
  STscObj *pTscObj = (STscObj *)taos;
84,241,689✔
942
  tscDebug("conn:0x%" PRIx64 ", try to close connection, numOfReq:%d", pTscObj->id, pTscObj->numOfReqs);
84,241,689✔
943

944
  SSessParam para = {.type = SESSION_PER_USER, .value = -1, .noCheck = 1};
84,241,689✔
945

946
  code = tscUpdateSessMetric(pTscObj, &para);
84,241,689✔
947
  if (code != TSDB_CODE_SUCCESS) {
84,243,507✔
948
    tscWarn("conn:0x%" PRIx64 ", failed to update user:%s metric when close connection, code:%d", pTscObj->id,
×
949
            pTscObj->user, code);
950
  }
951

952
  code = tscUnrefSessMetric(pTscObj);
84,243,507✔
953
  if (TSDB_CODE_SUCCESS != taosRemoveRef(clientConnRefPool, pTscObj->id)) {
84,242,149✔
954
    tscError("conn:0x%" PRIx64 ", failed to remove ref from conn pool", pTscObj->id);
×
955
  }
956
}
957

958
void taos_close(TAOS *taos) {
84,113,861✔
959
  if (taos == NULL) {
84,113,861✔
960
    return;
401✔
961
  }
962

963
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
84,113,460✔
964
  if (NULL == pObj) {
84,114,868✔
965
    taosMemoryFree(taos);
×
966
    return;
×
967
  }
968

969
  taos_close_internal(pObj);
84,114,868✔
970
  releaseTscObj(*(int64_t *)taos);
84,113,878✔
971
  taosMemoryFree(taos);
84,113,657✔
972
}
973

974
int taos_errno(TAOS_RES *res) {
1,290,755,075✔
975
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,290,755,075✔
976
    return terrno;
745,271✔
977
  }
978

979
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,290,011,767✔
980
    return 0;
287,037✔
981
  }
982

983
  return ((SRequestObj *)res)->code;
1,289,726,611✔
984
}
985

986
const char *taos_errstr(TAOS_RES *res) {
100,770,048✔
987
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
100,770,048✔
988
    if (*(taosGetErrMsg()) == 0) {
757,654✔
989
      return (const char *)tstrerror(terrno);
757,679✔
990
    } else {
991
      (void)snprintf(taosGetErrMsgReturn(), ERR_MSG_LEN, "%s", taosGetErrMsg());
39✔
992
      return (const char *)taosGetErrMsgReturn();
39✔
993
    }
994
  }
995

996
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
100,012,394✔
997
    return "success";
×
998
  }
999

1000
  SRequestObj *pRequest = (SRequestObj *)res;
100,013,170✔
1001
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
100,013,170✔
1002
    return pRequest->msgBuf;
29,386,782✔
1003
  } else {
1004
    return (const char *)tstrerror(pRequest->code);
70,626,388✔
1005
  }
1006
}
1007

1008
void taos_free_result(TAOS_RES *res) {
926,286,642✔
1009
  if (NULL == res) {
926,286,642✔
1010
    return;
13,498,729✔
1011
  }
1012

1013
  tscTrace("res:%p, will be freed", res);
912,787,913✔
1014

1015
  if (TD_RES_QUERY(res)) {
912,796,249✔
1016
    SRequestObj *pRequest = (SRequestObj *)res;
909,005,311✔
1017
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
909,005,311✔
1018
    destroyRequest(pRequest);
909,006,058✔
1019
    return;
909,001,727✔
1020
  }
1021

1022
  SMqRspObj *pRsp = (SMqRspObj *)res;
3,790,795✔
1023
  if (TD_RES_TMQ(res)) {
3,790,795✔
1024
    tDeleteMqDataRsp(&pRsp->dataRsp);
3,760,092✔
1025
    doFreeReqResultInfo(&pRsp->resInfo);
3,760,092✔
1026
  } else if (TD_RES_TMQ_METADATA(res)) {
27,542✔
1027
    tDeleteSTaosxRsp(&pRsp->dataRsp);
2,077✔
1028
    doFreeReqResultInfo(&pRsp->resInfo);
2,077✔
1029
  } else if (TD_RES_TMQ_META(res)) {
25,465✔
1030
    tDeleteMqMetaRsp(&pRsp->metaRsp);
22,165✔
1031
  } else if (TD_RES_TMQ_BATCH_META(res)) {
3,300✔
1032
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
3,300✔
1033
  } else if (TD_RES_TMQ_RAW(res)) {
×
1034
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
1035
  }
1036
  taosMemoryFree(pRsp);
3,787,634✔
1037
}
1038

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

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

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

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

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

1063
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
2,147,483,647✔
1064
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1065
    return NULL;
3,499,776✔
1066
  }
1067

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

1072
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
901,640,459✔
1073
TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqid) {
205✔
1074
  return taosQueryImplWithReqid(taos, sql, false, reqid);
205✔
1075
}
1076

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

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

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

1099
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
1,347,938,946✔
1100
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
1,347,940,421✔
1101
      if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT) {
309✔
1102
        CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
×
1103
      }
1104
      return NULL;
346✔
1105
    }
1106

1107
    if (pRequest->inCallback) {
1,347,937,958✔
1108
      tscError("can not call taos_fetch_row before query callback ends.");
195✔
1109
      terrno = TSDB_CODE_TSC_INVALID_OPERATION;
195✔
1110
      return NULL;
195✔
1111
    }
1112

1113
    return doAsyncFetchRows(pRequest, true, true);
1,347,937,347✔
1114
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,147,483,647✔
1115
    SMqRspObj      *msg = ((SMqRspObj *)res);
2,147,483,647✔
1116
    SReqResultInfo *pResultInfo = NULL;
2,147,483,647✔
1117
    if (msg->resIter == -1) {
2,147,483,647✔
1118
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
3,445,588✔
1119
        return NULL;
×
1120
      }
1121
    } else {
1122
      pResultInfo = tmqGetCurResInfo(res);
2,147,483,647✔
1123
    }
1124

1125
    if (pResultInfo->current < pResultInfo->numOfRows) {
2,147,483,647✔
1126
      doSetOneRowPtr(pResultInfo);
2,147,483,647✔
1127
      pResultInfo->current += 1;
2,147,483,647✔
1128
      return pResultInfo->row;
2,147,483,647✔
1129
    } else {
1130
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
40,312,785✔
1131
        return NULL;
3,444,128✔
1132
      }
1133

1134
      doSetOneRowPtr(pResultInfo);
36,867,931✔
1135
      pResultInfo->current += 1;
36,868,657✔
1136
      return pResultInfo->row;
36,868,657✔
1137
    }
1138
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1139
    return NULL;
×
1140
  } else {
1141
    tscError("invalid result passed to taos_fetch_row");
×
1142
    terrno = TSDB_CODE_TMQ_INVALID_DATA;
×
1143
    return NULL;
×
1144
  }
1145
}
1146

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

1157
    if (row[i] == NULL) {
2,147,483,647✔
1158
      len += snprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
135,423,632✔
1159
      continue;
135,423,632✔
1160
    }
1161

1162
    switch (fields[i].type) {
2,147,483,647✔
1163
      case TSDB_DATA_TYPE_TINYINT:
8,408,292✔
1164
        len += snprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
8,408,292✔
1165
        break;
8,423,412✔
1166

1167
      case TSDB_DATA_TYPE_UTINYINT:
50,600✔
1168
        len += snprintf(str + len, size - len, "%u", *((uint8_t *)row[i]));
50,600✔
1169
        break;
50,600✔
1170

1171
      case TSDB_DATA_TYPE_SMALLINT:
50,600✔
1172
        len += snprintf(str + len, size - len, "%d", *((int16_t *)row[i]));
50,600✔
1173
        break;
50,600✔
1174

1175
      case TSDB_DATA_TYPE_USMALLINT:
50,600✔
1176
        len += snprintf(str + len, size - len, "%u", *((uint16_t *)row[i]));
50,600✔
1177
        break;
50,600✔
1178

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

1183
      case TSDB_DATA_TYPE_UINT:
50,600✔
1184
        len += snprintf(str + len, size - len, "%u", *((uint32_t *)row[i]));
50,600✔
1185
        break;
50,600✔
1186

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

1191
      case TSDB_DATA_TYPE_UBIGINT:
50,600✔
1192
        len += snprintf(str + len, size - len, "%" PRIu64, *((uint64_t *)row[i]));
50,600✔
1193
        break;
50,600✔
1194

1195
      case TSDB_DATA_TYPE_FLOAT: {
16,805,259✔
1196
        float fv = 0;
16,805,259✔
1197
        fv = GET_FLOAT_VAL(row[i]);
16,805,259✔
1198
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
16,799,323✔
1199
      } break;
16,737,863✔
1200

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

1207
      case TSDB_DATA_TYPE_VARBINARY: {
50,600✔
1208
        void    *data = NULL;
50,600✔
1209
        uint32_t tmp = 0;
50,600✔
1210
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
50,600✔
1211
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
50,600✔
1212
          break;
×
1213
        }
1214
        uint32_t copyLen = TMIN(size - len - 1, tmp);
50,600✔
1215
        (void)memcpy(str + len, data, copyLen);
50,600✔
1216
        len += copyLen;
50,600✔
1217
        taosMemoryFree(data);
50,600✔
1218
      } break;
50,600✔
1219
      case TSDB_DATA_TYPE_BINARY:
2,147,483,647✔
1220
      case TSDB_DATA_TYPE_NCHAR:
1221
      case TSDB_DATA_TYPE_GEOMETRY: {
1222
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
2,147,483,647✔
1223
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
2,147,483,647✔
1224
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
1,870,311,395✔
1225
          if (charLen > fields[i].bytes || charLen < 0) {
2,147,483,647✔
1226
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
40,185✔
1227
            break;
×
1228
          }
1229
        } else {
1230
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
1,870,259,105✔
UNCOV
1231
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
×
1232
            break;
×
1233
          }
1234
        }
1235

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

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

1253
        taosMemoryFree(data);
×
1254
      } break;
×
1255

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

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

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

1282
  return len;
2,147,483,647✔
1283
}
1284

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

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

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

1300
  if (taos_is_update_query(res)) {
×
1301
    return NULL;
×
1302
  }
1303

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

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

1359
const char *taos_get_client_info() { return td_version; }
1,002,456✔
1360

1361
// return int32_t
1362
int taos_affected_rows(TAOS_RES *res) {
595,593,197✔
1363
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
595,593,197✔
1364
      TD_RES_TMQ_BATCH_META(res)) {
595,595,249✔
1365
    return 0;
×
1366
  }
1367

1368
  SRequestObj    *pRequest = (SRequestObj *)res;
595,595,973✔
1369
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
595,595,973✔
1370
  return (int)pResInfo->numOfRows;
595,595,263✔
1371
}
1372

1373
// return int64_t
1374
int64_t taos_affected_rows64(TAOS_RES *res) {
1,370,872✔
1375
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
1,370,872✔
1376
      TD_RES_TMQ_BATCH_META(res)) {
1,370,872✔
1377
    return 0;
×
1378
  }
1379

1380
  SRequestObj    *pRequest = (SRequestObj *)res;
1,370,872✔
1381
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
1,370,872✔
1382
  return pResInfo->numOfRows;
1,370,872✔
1383
}
1384

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

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

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

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

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

1418
  TAOS_RES *pRequest = taos_query(taos, sql);
38,786✔
1419
  int32_t   code = taos_errno(pRequest);
38,786✔
1420

1421
  taos_free_result(pRequest);
38,786✔
1422
  releaseTscObj(*(int64_t *)taos);
38,750✔
1423
  return code;
38,786✔
1424
}
1425

1426
void taos_stop_query(TAOS_RES *res) {
994,182,156✔
1427
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
994,182,156✔
1428
      TD_RES_TMQ_BATCH_META(res)) {
994,196,916✔
1429
    return;
×
1430
  }
1431

1432
  stopAllQueries((SRequestObj *)res);
994,191,066✔
1433
}
1434

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

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

1452
bool taos_is_update_query(TAOS_RES *res) { return taos_num_fields(res) == 0; }
365,721✔
1453

1454
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
317,478,819✔
1455
  int32_t numOfRows = 0;
317,478,819✔
1456
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
317,478,819✔
1457
  return numOfRows;
317,477,902✔
1458
}
1459

1460
int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) {
317,478,819✔
1461
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
317,478,819✔
1462
    return 0;
×
1463
  }
1464

1465
  if (TD_RES_QUERY(res)) {
317,478,406✔
1466
    SRequestObj *pRequest = (SRequestObj *)res;
316,916,195✔
1467
    (*rows) = NULL;
316,916,195✔
1468
    (*numOfRows) = 0;
316,916,195✔
1469

1470
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
316,916,195✔
1471
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
316,563,243✔
1472
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
2,834,808✔
1473
      return pRequest->code;
1,417,404✔
1474
    }
1475

1476
    (void)doAsyncFetchRows(pRequest, false, true);
315,498,791✔
1477

1478
    // TODO refactor
1479
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
315,495,382✔
1480
    pResultInfo->current = pResultInfo->numOfRows;
315,498,378✔
1481

1482
    (*rows) = pResultInfo->row;
315,498,378✔
1483
    (*numOfRows) = pResultInfo->numOfRows;
315,498,791✔
1484
    return pRequest->code;
315,498,378✔
1485
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
562,624✔
1486
    SReqResultInfo *pResultInfo = NULL;
562,624✔
1487
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
562,624✔
1488
    if (code != 0) return code;
562,624✔
1489

1490
    pResultInfo->current = pResultInfo->numOfRows;
282,358✔
1491
    (*rows) = pResultInfo->row;
282,358✔
1492
    (*numOfRows) = pResultInfo->numOfRows;
282,358✔
1493
    return 0;
282,358✔
1494
  } else {
1495
    tscError("taos_fetch_block_s invalid res type");
×
1496
    return TSDB_CODE_TMQ_INVALID_DATA;
×
1497
  }
1498
}
1499

1500
int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) {
5,068✔
1501
  *numOfRows = 0;
5,068✔
1502
  *pData = NULL;
5,068✔
1503

1504
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
5,068✔
1505
    return 0;
×
1506
  }
1507

1508
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
5,068✔
1509
    SReqResultInfo *pResultInfo = NULL;
1,480✔
1510
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
1,480✔
1511
    if (code != 0) {
1,480✔
1512
      (*numOfRows) = 0;
740✔
1513
      return 0;
740✔
1514
    }
1515

1516
    pResultInfo->current = pResultInfo->numOfRows;
740✔
1517
    (*numOfRows) = pResultInfo->numOfRows;
740✔
1518
    (*pData) = (void *)pResultInfo->pData;
740✔
1519
    return 0;
740✔
1520
  }
1521

1522
  SRequestObj *pRequest = (SRequestObj *)res;
3,588✔
1523

1524
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
3,588✔
1525
      pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
3,588✔
1526
    if (pRequest->code == TSDB_SQL_RETRIEVE_EMPTY_RESULT) {
×
1527
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
×
1528
    }
1529
    return pRequest->code;
×
1530
  }
1531

1532
  (void)doAsyncFetchRows(pRequest, false, false);
3,588✔
1533

1534
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
3,588✔
1535

1536
  pResultInfo->current = pResultInfo->numOfRows;
3,588✔
1537
  (*numOfRows) = pResultInfo->numOfRows;
3,588✔
1538
  (*pData) = (void *)pResultInfo->pData;
3,588✔
1539

1540
  return pRequest->code;
3,588✔
1541
}
1542

1543
int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) {
236,689,463✔
1544
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
236,689,463✔
1545
    return NULL;
×
1546
  }
1547

1548
  int32_t numOfFields = taos_num_fields(res);
236,689,463✔
1549
  if (columnIndex < 0 || columnIndex >= numOfFields || numOfFields == 0) {
236,689,463✔
1550
    return NULL;
×
1551
  }
1552

1553
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
236,689,463✔
1554
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
236,689,463✔
1555
  if (!IS_VAR_DATA_TYPE(pField->type)) {
236,689,463✔
1556
    return NULL;
×
1557
  }
1558

1559
  return pResInfo->pCol[columnIndex].offset;
236,689,463✔
1560
}
1561

1562
int taos_is_null_by_column(TAOS_RES *res, int columnIndex, bool result[], int *rows) {
788,880,737✔
1563
  if (res == NULL || result == NULL || rows == NULL || *rows <= 0 || columnIndex < 0 || TD_RES_TMQ_META(res) ||
788,880,737✔
1564
      TD_RES_TMQ_RAW(res) || TD_RES_TMQ_BATCH_META(res)) {
788,880,737✔
1565
    return TSDB_CODE_INVALID_PARA;
×
1566
  }
1567

1568
  int32_t numOfFields = taos_num_fields(res);
788,880,737✔
1569
  if (columnIndex >= numOfFields || numOfFields == 0) {
788,880,737✔
1570
    return TSDB_CODE_INVALID_PARA;
×
1571
  }
1572

1573
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
788,880,737✔
1574
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
788,880,737✔
1575
  SResultColumn  *pCol = &pResInfo->pCol[columnIndex];
788,880,737✔
1576

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

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

1603
  int code = taos_errno(pObj);
×
1604

1605
  taos_free_result(pObj);
×
1606
  return code;
×
1607
}
1608

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

1616
  resetConnectDB(pTscObj);
×
1617

1618
  releaseTscObj(*(int64_t *)taos);
×
1619
}
1620

1621
const char *taos_get_server_info(TAOS *taos) {
3,178✔
1622
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
3,178✔
1623
  if (pTscObj == NULL) {
3,178✔
1624
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1625
    return NULL;
×
1626
  }
1627

1628
  releaseTscObj(*(int64_t *)taos);
3,178✔
1629

1630
  return pTscObj->sDetailVer;
3,178✔
1631
}
1632

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

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

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

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

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

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

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

1701
    default:
×
1702
      TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1703
  }
1704

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

1711
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
1,900,663,183✔
1712
  if (NULL == pWrapper) {
1,900,663,183✔
1713
    return;
996,560,068✔
1714
  }
1715
  destoryCatalogReq(pWrapper->pCatalogReq);
904,103,115✔
1716
  taosMemoryFree(pWrapper->pCatalogReq);
904,166,375✔
1717
  qDestroyParseContext(pWrapper->pParseCtx);
904,168,109✔
1718
  taosMemoryFree(pWrapper);
904,165,903✔
1719
}
1720

1721
void destroyCtxInRequest(SRequestObj *pRequest) {
2,763,541✔
1722
  schedulerFreeJob(&pRequest->body.queryJob, 0);
2,763,541✔
1723
  qDestroyQuery(pRequest->pQuery);
2,763,558✔
1724
  pRequest->pQuery = NULL;
2,763,558✔
1725
  destorySqlCallbackWrapper(pRequest->pWrapper);
2,763,558✔
1726
  pRequest->pWrapper = NULL;
2,763,558✔
1727
}
2,763,558✔
1728

1729
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
428,149,407✔
1730
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
428,149,407✔
1731
  SRequestObj         *pRequest = pWrapper->pRequest;
428,149,407✔
1732
  SQuery              *pQuery = pRequest->pQuery;
428,149,452✔
1733

1734
  qDebug("req:0x%" PRIx64 ", start to semantic analysis, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
428,149,494✔
1735

1736
  int64_t analyseStart = taosGetTimestampUs();
428,151,643✔
1737
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
428,151,643✔
1738
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
428,151,522✔
1739

1740
  if (TSDB_CODE_SUCCESS == code) {
428,151,550✔
1741
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
428,147,119✔
1742
  }
1743

1744
  if (TSDB_CODE_SUCCESS == code) {
428,120,148✔
1745
    code = sqlSecurityCheckASTLevel(pRequest, pQuery);
354,209,649✔
1746
  }
1747

1748
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
428,105,599✔
1749

1750
  if (pRequest->parseOnly) {
428,103,326✔
1751
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
299,419✔
1752
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
299,419✔
1753
  }
1754

1755
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
428,107,671✔
1756
}
428,114,660✔
1757

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

1785
    *ppTarget = pTarget;
×
1786
  }
1787

1788
  return code;
×
1789
}
1790

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

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

1822
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
428,095,206✔
1823
  SRequestObj *pRequest = pWrapper->pRequest;
428,095,206✔
1824
  SQuery      *pQuery = pRequest->pQuery;
428,101,030✔
1825

1826
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
428,094,393✔
1827
    SNode *prevRoot = pQuery->pPrevRoot;
×
1828
    pQuery->pPrevRoot = NULL;
×
1829
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
×
1830
    return;
×
1831
  }
1832

1833
  if (code == TSDB_CODE_SUCCESS) {
428,095,631✔
1834
    pRequest->stableQuery = pQuery->stableQuery;
354,204,119✔
1835
    if (pQuery->pRoot) {
354,194,874✔
1836
      pRequest->stmtType = pQuery->pRoot->type;
354,219,814✔
1837
      if (nodeType(pQuery->pRoot) == QUERY_NODE_DELETE_STMT) {
354,197,914✔
1838
        pRequest->secureDelete = ((SDeleteStmt*)pQuery->pRoot)->secureDelete;
1,616,031✔
1839
      }
1840
    }
1841

1842
    if (pQuery->haveResultSet) {
354,196,678✔
1843
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
191,886,961✔
1844
                              pRequest->stmtBindVersion > 0);
191,888,148✔
1845
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
191,889,352✔
1846
    }
1847
  }
1848

1849
  if (code == TSDB_CODE_SUCCESS) {
428,100,318✔
1850
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
354,196,668✔
1851
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
354,196,912✔
1852
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
354,196,286✔
1853

1854
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
354,184,703✔
1855
  } else {
1856
    destorySqlCallbackWrapper(pWrapper);
73,903,650✔
1857
    pRequest->pWrapper = NULL;
73,909,760✔
1858
    qDestroyQuery(pRequest->pQuery);
73,909,777✔
1859
    pRequest->pQuery = NULL;
73,914,685✔
1860

1861
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
73,914,685✔
1862
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
2,672,375✔
1863
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1864
      restartAsyncQuery(pRequest, code);
2,672,375✔
1865
      return;
2,672,392✔
1866
    }
1867

1868
    // return to app directly
1869
    tscError("req:0x%" PRIx64 ", error occurs, code:%s, return to user app, QID:0x%" PRIx64, pRequest->self,
71,242,293✔
1870
             tstrerror(code), pRequest->requestId);
1871
    pRequest->code = code;
71,259,782✔
1872
    returnToUser(pRequest);
71,259,782✔
1873
  }
1874
}
1875

1876
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
434,919,707✔
1877
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
698,838,671✔
1878
                           .requestId = pWrapper->pParseCtx->requestId,
434,927,210✔
1879
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
434,922,461✔
1880
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
434,922,004✔
1881

1882
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
698,836,950✔
1883

1884
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
605,920,533✔
1885
                                &pWrapper->pRequest->body.queryJob);
434,933,042✔
1886
}
1887

1888
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code);
1889

1890
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
896,843,570✔
1891
  int32_t      code = TSDB_CODE_SUCCESS;
896,843,570✔
1892
  SRequestObj *pRequest = pWrapper->pRequest;
896,843,570✔
1893
  switch (pRequest->pQuery->execStage) {
896,859,602✔
1894
    case QUERY_EXEC_STAGE_PARSE: {
6,798,807✔
1895
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_CATALOG);
13,480,896✔
1896
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
6,798,807✔
1897
      break;
6,798,807✔
1898
    }
1899
    case QUERY_EXEC_STAGE_ANALYSE: {
428,111,568✔
1900
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_CATALOG);
856,254,444✔
1901
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
428,142,647✔
1902
      break;
428,122,058✔
1903
    }
1904
    case QUERY_EXEC_STAGE_SCHEDULE: {
461,935,778✔
1905
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_SCHEDULE);
923,873,742✔
1906
      launchAsyncQuery(pRequest, pRequest->pQuery, NULL, pWrapper);
461,939,180✔
1907
      break;
461,938,432✔
1908
    }
1909
    default:
×
1910
      break;
×
1911
  }
1912
  return code;
896,852,429✔
1913
}
1914

1915
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code) {
6,798,807✔
1916
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
6,798,807✔
1917
  SRequestObj         *pRequest = pWrapper->pRequest;
6,798,807✔
1918
  SQuery              *pQuery = pRequest->pQuery;
6,798,807✔
1919

1920
  pRequest->metric.ctgCostUs += taosGetTimestampUs() - pRequest->metric.ctgStart;
6,798,807✔
1921
  qDebug("req:0x%" PRIx64 ", continue parse query, QID:0x%" PRIx64 ", code:%s", pRequest->self, pRequest->requestId,
6,798,807✔
1922
         tstrerror(code));
1923

1924
  if (code == TSDB_CODE_SUCCESS) {
6,798,807✔
1925
    // pWrapper->pCatalogReq->forceUpdate = false;
1926
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
6,775,817✔
1927
  }
1928

1929
  if (TSDB_CODE_SUCCESS == code) {
6,798,807✔
1930
    code = phaseAsyncQuery(pWrapper);
6,224,645✔
1931
  }
1932

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

1944
void continueInsertFromCsv(SSqlCallbackWrapper *pWrapper, SRequestObj *pRequest) {
12,360✔
1945
  int32_t code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
12,360✔
1946
  if (TSDB_CODE_SUCCESS == code) {
12,360✔
1947
    code = phaseAsyncQuery(pWrapper);
12,360✔
1948
  }
1949

1950
  if (TSDB_CODE_SUCCESS != code) {
12,360✔
1951
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
×
1952
             tstrerror(code), pWrapper->pRequest->requestId);
1953
    destorySqlCallbackWrapper(pWrapper);
×
1954
    pRequest->pWrapper = NULL;
×
1955
    terrno = code;
×
1956
    pRequest->code = code;
×
1957
    doRequestCallback(pRequest, code);
×
1958
  }
1959
}
12,360✔
1960

1961
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
130,912✔
1962
  int64_t connId = *(int64_t *)taos;
130,912✔
1963
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
130,912✔
1964
}
130,912✔
1965

1966
void taos_query_a_with_reqid(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param, int64_t reqid) {
×
1967
  int64_t connId = *(int64_t *)taos;
×
1968
  taosAsyncQueryImplWithReqid(connId, sql, fp, param, false, reqid);
×
1969
}
×
1970

1971
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
904,168,176✔
1972
  const STscObj *pTscObj = pRequest->pTscObj;
904,168,176✔
1973

1974
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
904,173,019✔
1975
  if (*pCxt == NULL) {
904,144,170✔
1976
    return terrno;
×
1977
  }
1978

1979
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
2,147,483,647✔
1980
                           .requestRid = pRequest->self,
904,152,683✔
1981
                           .acctId = pTscObj->acctId,
904,153,086✔
1982
                           .db = pRequest->pDb,
904,153,978✔
1983
                           .topicQuery = false,
1984
                           .pSql = pRequest->sqlstr,
904,160,229✔
1985
                           .sqlLen = pRequest->sqlLen,
904,159,040✔
1986
                           .pMsg = pRequest->msgBuf,
904,160,621✔
1987
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1988
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
904,160,339✔
1989
                           .pStmtCb = NULL,
1990
                           .pUser = pTscObj->user,
904,156,314✔
1991
                           .userId = pTscObj->userId,
904,155,142✔
1992
                           .pEffectiveUser = pRequest->effectiveUser,
904,156,914✔
1993
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
904,155,431✔
1994
                           .enableSysInfo = pTscObj->sysInfo,
904,161,177✔
1995
                           .privInfo = pWrapper->pParseCtx ? pWrapper->pParseCtx->privInfo : 0,
904,172,666✔
1996
                           .async = true,
1997
                           .svrVer = pTscObj->sVer,
904,158,301✔
1998
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
904,159,099✔
1999
                           .allocatorId = pRequest->allocatorRefId,
904,160,442✔
2000
                           .parseSqlFp = clientParseSql,
2001
                           .parseSqlParam = pWrapper,
2002
                           .setQueryFp = setQueryRequest,
2003
                           .timezone = pTscObj->optionInfo.timezone,
904,156,701✔
2004
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
904,153,559✔
2005
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
904,151,013✔
2006
  (*pCxt)->biMode = biMode;
904,168,979✔
2007
  return TSDB_CODE_SUCCESS;
904,171,109✔
2008
}
2009

2010
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
904,162,649✔
2011
  int32_t              code = TSDB_CODE_SUCCESS;
904,162,649✔
2012
  STscObj             *pTscObj = pRequest->pTscObj;
904,162,649✔
2013
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
904,163,745✔
2014
  if (pWrapper == NULL) {
904,170,158✔
2015
    code = terrno;
×
2016
  } else {
2017
    pWrapper->pRequest = pRequest;
904,170,158✔
2018
    pRequest->pWrapper = pWrapper;
904,171,811✔
2019
    *ppWrapper = pWrapper;
904,171,598✔
2020
  }
2021

2022
  if (TSDB_CODE_SUCCESS == code) {
904,170,397✔
2023
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
904,173,911✔
2024
  }
2025

2026
  if (TSDB_CODE_SUCCESS == code) {
904,161,605✔
2027
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
904,165,504✔
2028
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
904,184,032✔
2029
  }
2030

2031
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
904,182,621✔
2032
    int64_t syntaxStart = taosGetTimestampUs();
904,181,034✔
2033

2034
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
904,181,034✔
2035
    if (pWrapper->pCatalogReq == NULL) {
904,160,748✔
2036
      code = terrno;
×
2037
    } else {
2038
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
904,162,203✔
2039
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
904,169,175✔
2040
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
904,167,633✔
2041
    }
2042

2043
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
904,155,423✔
2044
  }
2045

2046
  return code;
904,164,120✔
2047
}
2048

2049
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
905,056,305✔
2050
  SSqlCallbackWrapper *pWrapper = NULL;
905,056,305✔
2051
  int32_t              code = TSDB_CODE_SUCCESS;
905,060,064✔
2052

2053
  CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_PARSE);
1,810,114,932✔
2054

2055
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
905,071,402✔
2056
    code = pRequest->prevCode;
892,837✔
2057
    terrno = code;
892,837✔
2058
    pRequest->code = code;
892,837✔
2059
    tscDebug("req:0x%" PRIx64 ", call sync query cb with code:%s", pRequest->self, tstrerror(code));
892,837✔
2060
    doRequestCallback(pRequest, code);
892,837✔
2061
    return;
892,837✔
2062
  }
2063

2064
  if (TSDB_CODE_SUCCESS == code) {
904,166,653✔
2065
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
904,166,661✔
2066
  }
2067

2068
  if (TSDB_CODE_SUCCESS == code) {
904,151,711✔
2069
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
890,628,121✔
2070
    code = phaseAsyncQuery(pWrapper);
890,636,531✔
2071
  }
2072

2073
  if (TSDB_CODE_SUCCESS != code) {
904,152,407✔
2074
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
13,531,736✔
2075
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
13,462,841✔
2076
               pRequest->requestId);
2077
    } else {
2078
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
68,895✔
2079
               pRequest->requestId);
2080
    }
2081

2082
    destorySqlCallbackWrapper(pWrapper);
13,531,720✔
2083
    pRequest->pWrapper = NULL;
13,532,074✔
2084
    qDestroyQuery(pRequest->pQuery);
13,532,074✔
2085
    pRequest->pQuery = NULL;
13,531,896✔
2086

2087
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
13,531,896✔
2088
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
12,666✔
2089
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
2090
      code = refreshMeta(pRequest->pTscObj, pRequest);
12,666✔
2091
      if (code != 0) {
12,666✔
2092
        tscWarn("req:0x%" PRIx64 ", refresh meta failed, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code,
12,666✔
2093
                tstrerror(code), pRequest->requestId);
2094
      }
2095
      pRequest->prevCode = code;
12,666✔
2096
      doAsyncQuery(pRequest, true);
12,666✔
2097
      return;
12,666✔
2098
    }
2099

2100
    terrno = code;
13,519,230✔
2101
    pRequest->code = code;
13,519,320✔
2102
    doRequestCallback(pRequest, code);
13,519,320✔
2103
  }
2104
}
2105

2106
void restartAsyncQuery(SRequestObj *pRequest, int32_t code) {
2,763,524✔
2107
  tscInfo("restart request:%s p:%p", pRequest->sqlstr, pRequest);
2,763,524✔
2108
  SRequestObj *pUserReq = pRequest;
2,763,524✔
2109
  (void)acquireRequest(pRequest->self);
2,763,524✔
2110
  while (pUserReq) {
2,763,541✔
2111
    if (pUserReq->self == pUserReq->relation.userRefId || pUserReq->relation.userRefId == 0) {
2,763,541✔
2112
      break;
2113
    } else {
2114
      int64_t nextRefId = pUserReq->relation.nextRefId;
×
2115
      (void)releaseRequest(pUserReq->self);
×
2116
      if (nextRefId) {
×
2117
        pUserReq = acquireRequest(nextRefId);
×
2118
      }
2119
    }
2120
  }
2121
  bool hasSubRequest = pUserReq != pRequest || pRequest->relation.prevRefId != 0;
2,763,541✔
2122
  if (pUserReq) {
2,763,541✔
2123
    destroyCtxInRequest(pUserReq);
2,763,541✔
2124
    pUserReq->prevCode = code;
2,763,558✔
2125
    (void)memset(&pUserReq->relation, 0, sizeof(pUserReq->relation));
2,763,558✔
2126
  } else {
2127
    tscError("User req is missing");
×
2128
    (void)removeFromMostPrevReq(pRequest);
×
2129
    return;
×
2130
  }
2131
  if (hasSubRequest)
2,763,524✔
2132
    (void)removeFromMostPrevReq(pRequest);
×
2133
  else
2134
    (void)releaseRequest(pUserReq->self);
2,763,524✔
2135
  doAsyncQuery(pUserReq, true);
2,763,541✔
2136
}
2137

2138
typedef struct SAsyncFetchParam {
2139
  SRequestObj      *pReq;
2140
  __taos_async_fn_t fp;
2141
  void             *param;
2142
} SAsyncFetchParam;
2143

2144
static int32_t doAsyncFetch(void *pParam) {
301,313,637✔
2145
  SAsyncFetchParam *param = pParam;
301,313,637✔
2146
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
301,313,637✔
2147
  taosMemoryFree(param);
301,313,585✔
2148
  return TSDB_CODE_SUCCESS;
301,312,892✔
2149
}
2150

2151
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
301,351,983✔
2152
  if (res == NULL || fp == NULL) {
301,351,983✔
UNCOV
2153
    tscError("taos_fetch_rows_a invalid paras");
×
2154
    return;
×
2155
  }
2156
  if (!TD_RES_QUERY(res)) {
301,351,988✔
2157
    tscError("taos_fetch_rows_a res is NULL");
×
2158
    fp(param, res, TSDB_CODE_APP_ERROR);
×
2159
    return;
×
2160
  }
2161

2162
  SRequestObj *pRequest = res;
301,351,983✔
2163

2164
  // Each fetch call sets phase to IN_PROGRESS
2165

2166
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
301,351,983✔
2167
    CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
76,702✔
2168
    fp(param, res, 0);
38,351✔
2169
    return;
38,351✔
2170
  }
2171

2172
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
301,313,627✔
2173
  if (!pParam) {
301,313,580✔
2174
    CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
×
2175
    fp(param, res, terrno);
×
2176
    return;
×
2177
  }
2178

2179
  CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_IN_PROGRESS);
446,647,159✔
2180
  pParam->pReq = pRequest;
301,313,642✔
2181
  pParam->fp = fp;
301,313,642✔
2182
  pParam->param = param;
301,313,632✔
2183
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
301,313,627✔
2184
  if (TSDB_CODE_SUCCESS != code) {
301,313,642✔
2185
    taosMemoryFree(pParam);
×
2186
    fp(param, res, code);
×
2187
    return;
×
2188
  }
2189
}
2190

2191
void taos_fetch_raw_block_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
152✔
2192
  if (res == NULL || fp == NULL) {
152✔
2193
    tscError("taos_fetch_raw_block_a invalid paras");
×
2194
    return;
×
2195
  }
2196
  if (!TD_RES_QUERY(res)) {
152✔
2197
    tscError("taos_fetch_raw_block_a res is NULL");
×
2198
    return;
×
2199
  }
2200
  SRequestObj    *pRequest = res;
152✔
2201
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
152✔
2202

2203
  CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_IN_PROGRESS);
216✔
2204
  // set the current block is all consumed
2205
  pResultInfo->convertUcs4 = false;
152✔
2206

2207
  // it is a local executed query, no need to do async fetch
2208
  taos_fetch_rows_a(pRequest, fp, param);
152✔
2209
}
2210

2211
const void *taos_get_raw_block(TAOS_RES *res) {
96✔
2212
  if (res == NULL) {
96✔
2213
    tscError("taos_get_raw_block invalid paras");
×
2214
    return NULL;
×
2215
  }
2216
  if (!TD_RES_QUERY(res)) {
96✔
2217
    tscError("taos_get_raw_block res is NULL");
×
2218
    return NULL;
×
2219
  }
2220
  SRequestObj *pRequest = res;
96✔
2221

2222
  return pRequest->body.resInfo.pData;
96✔
2223
}
2224

2225
int taos_get_db_route_info(TAOS *taos, const char *db, TAOS_DB_ROUTE_INFO *dbInfo) {
×
2226
  if (NULL == taos) {
×
2227
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2228
    return terrno;
×
2229
  }
2230

2231
  if (NULL == db || NULL == dbInfo) {
×
2232
    tscError("invalid input param, db:%p, dbInfo:%p", db, dbInfo);
×
2233
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2234
    return terrno;
×
2235
  }
2236

2237
  int64_t      connId = *(int64_t *)taos;
×
2238
  SRequestObj *pRequest = NULL;
×
2239
  char        *sql = "taos_get_db_route_info";
×
2240
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2241
  if (code != TSDB_CODE_SUCCESS) {
×
2242
    terrno = code;
×
2243
    return terrno;
×
2244
  }
2245

2246
  STscObj  *pTscObj = pRequest->pTscObj;
×
2247
  SCatalog *pCtg = NULL;
×
2248
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2249
  if (code != TSDB_CODE_SUCCESS) {
×
2250
    goto _return;
×
2251
  }
2252

2253
  SRequestConnInfo conn = {
×
2254
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2255

2256
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2257

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

2261
  code = catalogGetDBVgInfo(pCtg, &conn, dbFName, dbInfo);
×
2262
  if (code) {
×
2263
    goto _return;
×
2264
  }
2265

2266
_return:
×
2267

2268
  terrno = code;
×
2269

2270
  destroyRequest(pRequest);
×
2271
  return code;
×
2272
}
2273

2274
int taos_get_table_vgId(TAOS *taos, const char *db, const char *table, int *vgId) {
×
2275
  if (NULL == taos) {
×
2276
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2277
    return terrno;
×
2278
  }
2279

2280
  if (NULL == db || NULL == table || NULL == vgId) {
×
2281
    tscError("invalid input param, db:%p, table:%p, vgId:%p", db, table, vgId);
×
2282
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2283
    return terrno;
×
2284
  }
2285

2286
  int64_t      connId = *(int64_t *)taos;
×
2287
  SRequestObj *pRequest = NULL;
×
2288
  char        *sql = "taos_get_table_vgId";
×
2289
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2290
  if (code != TSDB_CODE_SUCCESS) {
×
2291
    return terrno;
×
2292
  }
2293

2294
  pRequest->syncQuery = true;
×
2295

2296
  STscObj  *pTscObj = pRequest->pTscObj;
×
2297
  SCatalog *pCtg = NULL;
×
2298
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2299
  if (code != TSDB_CODE_SUCCESS) {
×
2300
    goto _return;
×
2301
  }
2302

2303
  SRequestConnInfo conn = {
×
2304
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2305

2306
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2307

2308
  SName tableName = {0};
×
2309
  toName(pTscObj->acctId, db, table, &tableName);
×
2310

2311
  SVgroupInfo vgInfo;
×
2312
  code = catalogGetTableHashVgroup(pCtg, &conn, &tableName, &vgInfo);
×
2313
  if (code) {
×
2314
    goto _return;
×
2315
  }
2316

2317
  *vgId = vgInfo.vgId;
×
2318

2319
_return:
×
2320

2321
  terrno = code;
×
2322

2323
  destroyRequest(pRequest);
×
2324
  return code;
×
2325
}
2326

2327
int taos_get_tables_vgId(TAOS *taos, const char *db, const char *table[], int tableNum, int *vgId) {
×
2328
  if (NULL == taos) {
×
2329
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2330
    return terrno;
×
2331
  }
2332

2333
  if (NULL == db || NULL == table || NULL == vgId || tableNum <= 0) {
×
2334
    tscError("invalid input param, db:%p, table:%p, vgId:%p, tbNum:%d", db, table, vgId, tableNum);
×
2335
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2336
    return terrno;
×
2337
  }
2338

2339
  int64_t      connId = *(int64_t *)taos;
×
2340
  SRequestObj *pRequest = NULL;
×
2341
  char        *sql = "taos_get_table_vgId";
×
2342
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2343
  if (code != TSDB_CODE_SUCCESS) {
×
2344
    return terrno;
×
2345
  }
2346

2347
  pRequest->syncQuery = true;
×
2348

2349
  STscObj  *pTscObj = pRequest->pTscObj;
×
2350
  SCatalog *pCtg = NULL;
×
2351
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2352
  if (code != TSDB_CODE_SUCCESS) {
×
2353
    goto _return;
×
2354
  }
2355

2356
  SRequestConnInfo conn = {
×
2357
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2358

2359
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2360

2361
  code = catalogGetTablesHashVgId(pCtg, &conn, pTscObj->acctId, db, table, tableNum, vgId);
×
2362
  if (code) {
×
2363
    goto _return;
×
2364
  }
2365

2366
_return:
×
2367

2368
  terrno = code;
×
2369

2370
  destroyRequest(pRequest);
×
2371
  return code;
×
2372
}
2373

2374
int taos_load_table_info(TAOS *taos, const char *tableNameList) {
1,212✔
2375
  if (NULL == taos) {
1,212✔
2376
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2377
    return terrno;
×
2378
  }
2379

2380
  int64_t       connId = *(int64_t *)taos;
1,212✔
2381
  const int32_t MAX_TABLE_NAME_LENGTH = 12 * 1024 * 1024;  // 12MB list
1,212✔
2382
  int32_t       code = 0;
1,212✔
2383
  SRequestObj  *pRequest = NULL;
1,212✔
2384
  SCatalogReq   catalogReq = {0};
1,212✔
2385

2386
  if (NULL == tableNameList) {
1,212✔
2387
    return TSDB_CODE_SUCCESS;
×
2388
  }
2389

2390
  int32_t length = (int32_t)strlen(tableNameList);
1,212✔
2391
  if (0 == length) {
1,212✔
2392
    return TSDB_CODE_SUCCESS;
×
2393
  } else if (length > MAX_TABLE_NAME_LENGTH) {
1,212✔
2394
    tscError("tableNameList too long, length:%d, maximum allowed:%d", length, MAX_TABLE_NAME_LENGTH);
×
2395
    return TSDB_CODE_TSC_INVALID_OPERATION;
×
2396
  }
2397

2398
  char *sql = "taos_load_table_info";
1,212✔
2399
  code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
1,212✔
2400
  if (code != TSDB_CODE_SUCCESS) {
1,212✔
2401
    terrno = code;
×
2402
    goto _return;
×
2403
  }
2404

2405
  pRequest->syncQuery = true;
1,212✔
2406

2407
  STscObj *pTscObj = pRequest->pTscObj;
1,212✔
2408
  code = transferTableNameList(tableNameList, pTscObj->acctId, pTscObj->db, &catalogReq.pTableMeta);
1,212✔
2409
  if (code) {
1,212✔
2410
    goto _return;
×
2411
  }
2412

2413
  SCatalog *pCtg = NULL;
1,212✔
2414
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
1,212✔
2415
  if (code != TSDB_CODE_SUCCESS) {
1,212✔
2416
    goto _return;
×
2417
  }
2418

2419
  SRequestConnInfo conn = {
1,212✔
2420
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
1,212✔
2421

2422
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
1,212✔
2423

2424
  code = catalogAsyncGetAllMeta(pCtg, &conn, &catalogReq, syncCatalogFn, pRequest->body.interParam, NULL);
1,212✔
2425
  if (code) {
1,212✔
2426
    goto _return;
×
2427
  }
2428

2429
  SSyncQueryParam *pParam = pRequest->body.interParam;
1,212✔
2430
  code = tsem_wait(&pParam->sem);
1,212✔
2431
  if (code) {
1,212✔
2432
    tscError("tsem wait failed, code:%d - %s", code, tstrerror(code));
×
2433
    goto _return;
×
2434
  }
2435
_return:
1,212✔
2436
  destoryCatalogReq(&catalogReq);
1,212✔
2437
  destroyRequest(pRequest);
1,212✔
2438
  return code;
1,212✔
2439
}
2440

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

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

2455
  return pStmt;
26,147✔
2456
}
2457

2458
TAOS_STMT *taos_stmt_init_with_reqid(TAOS *taos, int64_t reqid) {
×
2459
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
2460
  if (NULL == pObj) {
×
2461
    tscError("invalid parameter for %s", __FUNCTION__);
×
2462
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2463
    return NULL;
×
2464
  }
2465

2466
  TAOS_STMT *pStmt = stmtInit(pObj, reqid, NULL);
×
2467
  if (NULL == pStmt) {
×
2468
    tscError("stmt init failed, errcode:%s", terrstr());
×
2469
  }
2470
  releaseTscObj(*(int64_t *)taos);
×
2471

2472
  return pStmt;
×
2473
}
2474

2475
TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options) {
11,970✔
2476
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
11,970✔
2477
  if (NULL == pObj) {
11,970✔
2478
    tscError("invalid parameter for %s", __FUNCTION__);
×
2479
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2480
    return NULL;
×
2481
  }
2482

2483
  TAOS_STMT *pStmt = stmtInit(pObj, options->reqId, options);
11,970✔
2484
  if (NULL == pStmt) {
11,970✔
2485
    tscError("stmt init failed, errcode:%s", terrstr());
×
2486
  }
2487
  releaseTscObj(*(int64_t *)taos);
11,970✔
2488

2489
  return pStmt;
11,970✔
2490
}
2491

2492
int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
6,689,017✔
2493
  if (stmt == NULL || sql == NULL) {
6,689,017✔
2494
    tscError("NULL parameter for %s", __FUNCTION__);
28✔
2495
    terrno = TSDB_CODE_INVALID_PARA;
28✔
2496
    return terrno;
×
2497
  }
2498

2499
  return stmtPrepare(stmt, sql, length);
6,689,337✔
2500
}
2501

2502
int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags) {
7,445✔
2503
  if (stmt == NULL || name == NULL) {
7,445✔
2504
    tscError("NULL parameter for %s", __FUNCTION__);
×
2505
    terrno = TSDB_CODE_INVALID_PARA;
×
2506
    return terrno;
×
2507
  }
2508

2509
  int32_t code = stmtSetTbName(stmt, name);
7,445✔
2510
  if (code) {
7,445✔
2511
    return code;
606✔
2512
  }
2513

2514
  if (tags) {
6,839✔
2515
    return stmtSetTbTags(stmt, tags);
6,839✔
2516
  }
2517

2518
  return TSDB_CODE_SUCCESS;
×
2519
}
2520

2521
int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name) {
8,253,100✔
2522
  if (stmt == NULL || name == NULL) {
8,253,100✔
2523
    tscError("NULL parameter for %s", __FUNCTION__);
×
2524
    terrno = TSDB_CODE_INVALID_PARA;
×
2525
    return terrno;
×
2526
  }
2527

2528
  return stmtSetTbName(stmt, name);
8,253,813✔
2529
}
2530

2531
int taos_stmt_set_tags(TAOS_STMT *stmt, TAOS_MULTI_BIND *tags) {
935✔
2532
  if (stmt == NULL || tags == NULL) {
935✔
2533
    tscError("NULL parameter for %s", __FUNCTION__);
×
2534
    terrno = TSDB_CODE_INVALID_PARA;
×
2535
    return terrno;
×
2536
  }
2537

2538
  return stmtSetTbTags(stmt, tags);
935✔
2539
}
2540

2541
int taos_stmt_set_sub_tbname(TAOS_STMT *stmt, const char *name) { return taos_stmt_set_tbname(stmt, name); }
390✔
2542

2543
int taos_stmt_get_tag_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
780✔
2544
  if (stmt == NULL || NULL == fieldNum) {
780✔
2545
    tscError("NULL parameter for %s", __FUNCTION__);
×
2546
    terrno = TSDB_CODE_INVALID_PARA;
×
2547
    return terrno;
×
2548
  }
2549

2550
  return stmtGetTagFields(stmt, fieldNum, fields);
780✔
2551
}
2552

2553
int taos_stmt_get_col_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
1,170✔
2554
  if (stmt == NULL || NULL == fieldNum) {
1,170✔
2555
    tscError("NULL parameter for %s", __FUNCTION__);
×
2556
    terrno = TSDB_CODE_INVALID_PARA;
×
2557
    return terrno;
×
2558
  }
2559

2560
  return stmtGetColFields(stmt, fieldNum, fields);
1,170✔
2561
}
2562

2563
// let stmt to reclaim TAOS_FIELD_E that was allocated by `taos_stmt_get_tag_fields`/`taos_stmt_get_col_fields`
2564
void taos_stmt_reclaim_fields(TAOS_STMT *stmt, TAOS_FIELD_E *fields) {
×
2565
  (void)stmt;
2566
  if (!fields) return;
×
2567
  taosMemoryFree(fields);
×
2568
}
2569

2570
int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
46,206✔
2571
  if (stmt == NULL || bind == NULL) {
46,206✔
2572
    tscError("NULL parameter for %s", __FUNCTION__);
×
2573
    terrno = TSDB_CODE_INVALID_PARA;
×
2574
    return terrno;
×
2575
  }
2576

2577
  if (bind->num > 1) {
46,206✔
2578
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
3,918✔
2579
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
3,918✔
2580
    return terrno;
3,918✔
2581
  }
2582

2583
  return stmtBindBatch(stmt, bind, -1);
42,288✔
2584
}
2585

2586
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
8,254,453✔
2587
  if (stmt == NULL || bind == NULL) {
8,254,453✔
2588
    tscError("NULL parameter for %s", __FUNCTION__);
6,416✔
2589
    terrno = TSDB_CODE_INVALID_PARA;
6,416✔
2590
    return terrno;
×
2591
  }
2592

2593
  if (bind->num <= 0 || bind->num > INT16_MAX) {
8,248,037✔
2594
    tscError("invalid bind num %d", bind->num);
5,456✔
2595
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
5,456✔
2596
    return terrno;
×
2597
  }
2598

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

2611
  return stmtBindBatch(stmt, bind, -1);
8,247,920✔
2612
}
2613

2614
int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind, int colIdx) {
840✔
2615
  if (stmt == NULL || bind == NULL) {
840✔
2616
    tscError("NULL parameter for %s", __FUNCTION__);
×
2617
    terrno = TSDB_CODE_INVALID_PARA;
×
2618
    return terrno;
×
2619
  }
2620

2621
  if (colIdx < 0) {
840✔
2622
    tscError("invalid bind column idx %d", colIdx);
×
2623
    terrno = TSDB_CODE_INVALID_PARA;
×
2624
    return terrno;
×
2625
  }
2626

2627
  int32_t insert = 0;
840✔
2628
  int32_t code = stmtIsInsert(stmt, &insert);
840✔
2629
  if (TSDB_CODE_SUCCESS != code) {
840✔
2630
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2631
    return code;
×
2632
  }
2633
  if (0 == insert && bind->num > 1) {
840✔
2634
    tscError("only one row data allowed for query");
×
2635
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2636
    return terrno;
×
2637
  }
2638

2639
  return stmtBindBatch(stmt, bind, colIdx);
840✔
2640
}
2641

2642
int taos_stmt_add_batch(TAOS_STMT *stmt) {
6,928,065✔
2643
  if (stmt == NULL) {
6,928,065✔
2644
    tscError("NULL parameter for %s", __FUNCTION__);
×
2645
    terrno = TSDB_CODE_INVALID_PARA;
×
2646
    return terrno;
×
2647
  }
2648

2649
  return stmtAddBatch(stmt);
6,928,065✔
2650
}
2651

2652
int taos_stmt_execute(TAOS_STMT *stmt) {
6,926,174✔
2653
  if (stmt == NULL) {
6,926,174✔
2654
    tscError("NULL parameter for %s", __FUNCTION__);
×
2655
    terrno = TSDB_CODE_INVALID_PARA;
×
2656
    return terrno;
×
2657
  }
2658

2659
  return stmtExec(stmt);
6,926,174✔
2660
}
2661

2662
int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
×
2663
  if (stmt == NULL || insert == NULL) {
×
2664
    tscError("NULL parameter for %s", __FUNCTION__);
×
2665
    terrno = TSDB_CODE_INVALID_PARA;
×
2666
    return terrno;
×
2667
  }
2668

2669
  return stmtIsInsert(stmt, insert);
×
2670
}
2671

2672
int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) {
×
2673
  if (stmt == NULL || nums == NULL) {
×
2674
    tscError("NULL parameter for %s", __FUNCTION__);
×
2675
    terrno = TSDB_CODE_INVALID_PARA;
×
2676
    return terrno;
×
2677
  }
2678

2679
  return stmtGetParamNum(stmt, nums);
×
2680
}
2681

2682
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
780✔
2683
  if (stmt == NULL || type == NULL || NULL == bytes || idx < 0) {
780✔
2684
    tscError("invalid parameter for %s", __FUNCTION__);
×
2685
    terrno = TSDB_CODE_INVALID_PARA;
×
2686
    return terrno;
×
2687
  }
2688

2689
  return stmtGetParam(stmt, idx, type, bytes);
780✔
2690
}
2691

2692
TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt) {
9,696✔
2693
  if (stmt == NULL) {
9,696✔
2694
    tscError("NULL parameter for %s", __FUNCTION__);
×
2695
    terrno = TSDB_CODE_INVALID_PARA;
×
2696
    return NULL;
×
2697
  }
2698

2699
  return stmtUseResult(stmt);
9,696✔
2700
}
2701

2702
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
14,185✔
2703

2704
int taos_stmt_affected_rows(TAOS_STMT *stmt) {
3,142✔
2705
  if (stmt == NULL) {
3,142✔
2706
    tscError("NULL parameter for %s", __FUNCTION__);
×
2707
    terrno = TSDB_CODE_INVALID_PARA;
×
2708
    return 0;
×
2709
  }
2710

2711
  return stmtAffectedRows(stmt);
3,142✔
2712
}
2713

2714
int taos_stmt_affected_rows_once(TAOS_STMT *stmt) {
5,540✔
2715
  if (stmt == NULL) {
5,540✔
2716
    tscError("NULL parameter for %s", __FUNCTION__);
×
2717
    terrno = TSDB_CODE_INVALID_PARA;
×
2718
    return 0;
×
2719
  }
2720

2721
  return stmtAffectedRowsOnce(stmt);
5,540✔
2722
}
2723

2724
int taos_stmt_close(TAOS_STMT *stmt) {
38,117✔
2725
  if (stmt == NULL) {
38,117✔
2726
    tscError("NULL parameter for %s", __FUNCTION__);
×
2727
    terrno = TSDB_CODE_INVALID_PARA;
×
2728
    return terrno;
×
2729
  }
2730

2731
  return stmtClose(stmt);
38,117✔
2732
}
2733

2734
TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option) {
130,443✔
2735
  if (NULL == taos) {
130,443✔
2736
    tscError("NULL parameter for %s", __FUNCTION__);
195✔
2737
    terrno = TSDB_CODE_INVALID_PARA;
195✔
2738
    return NULL;
195✔
2739
  }
2740
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
130,248✔
2741
  if (NULL == pObj) {
130,302✔
2742
    tscError("invalid parameter for %s", __FUNCTION__);
×
2743
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2744
    return NULL;
×
2745
  }
2746

2747
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
130,302✔
2748

2749
  releaseTscObj(*(int64_t *)taos);
130,302✔
2750

2751
  return pStmt;
130,302✔
2752
}
2753

2754
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
136,756✔
2755
  if (stmt == NULL || sql == NULL) {
136,756✔
2756
    tscError("NULL parameter for %s", __FUNCTION__);
195✔
2757
    terrno = TSDB_CODE_INVALID_PARA;
195✔
2758
    return terrno;
195✔
2759
  }
2760

2761
  return stmtPrepare2(stmt, sql, length);
136,561✔
2762
}
2763

2764
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
36,517,083✔
2765
  if (stmt == NULL) {
36,517,083✔
2766
    tscError("NULL parameter for %s", __FUNCTION__);
×
2767
    terrno = TSDB_CODE_INVALID_PARA;
×
2768
    return terrno;
×
2769
  }
2770

2771
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
36,517,083✔
2772
  int32_t    code = TSDB_CODE_SUCCESS;
36,517,083✔
2773
  STMT2_DLOG_E("start to bind param");
36,517,083✔
2774

2775
  // check query bind number
2776
  bool isQuery = (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt)));
36,526,825✔
2777
  if (isQuery) {
36,900,567✔
2778
    if (bindv->count != 1 || bindv->bind_cols[0]->num != 1) {
7,420✔
2779
      terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2780
      STMT2_ELOG_E("query only support one table and one row bind");
×
2781
      return terrno;
×
2782
    }
2783
  }
2784

2785
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 1) {
36,900,567✔
2786
    STMT2_ELOG_E("async bind param is still working, please try again later");
9,901✔
2787
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
9,901✔
2788
    return terrno;
×
2789
  }
2790

2791
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
36,506,223✔
2792
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
4,485✔
2793
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
2794
    }
2795
    pStmt->execSemWaited = true;
4,485✔
2796
  }
2797

2798
  for (int i = 0; i < bindv->count; ++i) {
73,774,563✔
2799
    SVCreateTbReq *pCreateTbReq = NULL;
36,905,060✔
2800
    if (!isQuery) {
37,186,882✔
2801
      STMT2_TLOG("start to bind %dth table", i);
37,294,559✔
2802
      if (bindv->tbnames && bindv->tbnames[i]) {
37,294,433✔
2803
        code = stmtSetTbName2(stmt, bindv->tbnames[i]);
918,667✔
2804
        if (code) {
918,559✔
2805
          terrno = code;
2,145✔
2806
          STMT2_ELOG("set tbname failed, code:%s", stmt2Errstr(stmt));
2,145✔
2807
          return terrno;
3,510✔
2808
        }
2809
      }
2810

2811
      if (bindv->tags && bindv->tags[i]) {
37,252,452✔
2812
        code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
481,553✔
2813
      } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
36,842,024✔
2814
        code = stmtCheckTags2(stmt, &pCreateTbReq);
93,831✔
2815
      } else if (pStmt->sql.autoCreateTbl) {
36,784,867✔
2816
        code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
4,095✔
2817
      }
2818

2819
      if (code) {
37,030,273✔
2820
        terrno = code;
195✔
2821
        STMT2_ELOG("set tags failed, code:%s", stmt2Errstr(stmt));
195✔
2822
        if (pCreateTbReq) {
195✔
2823
          tdDestroySVCreateTbReq(pCreateTbReq);
×
2824
          taosMemoryFreeClear(pCreateTbReq);
×
2825
        }
2826
        return terrno;
195✔
2827
      }
2828
    }
2829

2830
    if (bindv->bind_cols && bindv->bind_cols[i]) {
36,922,401✔
2831
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
37,534,208✔
2832

2833
      if (bind->num <= 0 || bind->num > INT16_MAX) {
37,563,251✔
2834
        STMT2_ELOG("bind num:%d must > 0 and < INT16_MAX", bind->num);
205,823✔
2835
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
205,823✔
2836
        if (pCreateTbReq) {
×
2837
          tdDestroySVCreateTbReq(pCreateTbReq);
×
2838
          taosMemoryFreeClear(pCreateTbReq);
×
2839
        }
2840
        return terrno;
×
2841
      }
2842

2843
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
37,564,660✔
2844
      if (TSDB_CODE_SUCCESS != code) {
36,769,536✔
2845
        terrno = code;
1,170✔
2846
        STMT2_ELOG("bind batch failed, code:%s", stmt2Errstr(stmt));
1,170✔
2847
        if (pCreateTbReq) {
1,170✔
2848
          tdDestroySVCreateTbReq(pCreateTbReq);
390✔
2849
          taosMemoryFreeClear(pCreateTbReq);
390✔
2850
        }
2851
        return terrno;
1,170✔
2852
      }
2853
    }
2854
  }
2855

2856
  return code;
36,875,342✔
2857
}
2858

2859
int taos_stmt2_bind_param_a(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx, __taos_async_fn_t fp,
×
2860
                            void *param) {
2861
  if (stmt == NULL || bindv == NULL || fp == NULL) {
×
2862
    terrno = TSDB_CODE_INVALID_PARA;
×
2863
    return terrno;
×
2864
  }
2865

2866
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2867

2868
  ThreadArgs *args = (ThreadArgs *)taosMemoryMalloc(sizeof(ThreadArgs));
×
2869
  args->stmt = stmt;
×
2870
  args->bindv = bindv;
×
2871
  args->col_idx = col_idx;
×
2872
  args->fp = fp;
×
2873
  args->param = param;
×
2874

2875
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2876
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 0) {
×
2877
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2878
    tscError("async bind param is still working, please try again later");
×
2879
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
×
2880
    return terrno;
×
2881
  }
2882
  (void)atomic_add_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2883
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2884

2885
  int code_s = taosStmt2AsyncBind(stmtAsyncBindThreadFunc, (void *)args);
×
2886
  if (code_s != TSDB_CODE_SUCCESS) {
×
2887
    terrno = code_s;
×
2888
    (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2889
    (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2890
    (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2891
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2892
    tscError("async bind failed, code:%d , %s", code_s, tstrerror(code_s));
×
2893
  }
2894

2895
  return code_s;
×
2896
}
2897

2898
int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows) {
535,481✔
2899
  if (stmt == NULL) {
535,481✔
2900
    tscError("NULL parameter for %s", __FUNCTION__);
×
2901
    terrno = TSDB_CODE_INVALID_PARA;
×
2902
    return terrno;
×
2903
  }
2904

2905
  return stmtExec2(stmt, affected_rows);
535,481✔
2906
}
2907

2908
int taos_stmt2_close(TAOS_STMT2 *stmt) {
128,596✔
2909
  if (stmt == NULL) {
128,596✔
2910
    tscError("NULL parameter for %s", __FUNCTION__);
×
2911
    terrno = TSDB_CODE_INVALID_PARA;
×
2912
    return terrno;
×
2913
  }
2914

2915
  return stmtClose2(stmt);
128,596✔
2916
}
2917

2918
int taos_stmt2_is_insert(TAOS_STMT2 *stmt, int *insert) {
23✔
2919
  if (stmt == NULL || insert == NULL) {
23✔
2920
    tscError("NULL parameter for %s", __FUNCTION__);
×
2921
    terrno = TSDB_CODE_INVALID_PARA;
×
2922
    return terrno;
×
2923
  }
2924
  *insert = stmt2IsInsert(stmt);
23✔
2925
  return TSDB_CODE_SUCCESS;
23✔
2926
}
2927

2928
int taos_stmt2_get_fields(TAOS_STMT2 *stmt, int *count, TAOS_FIELD_ALL **fields) {
17,956✔
2929
  if (stmt == NULL || count == NULL) {
17,956✔
2930
    tscError("NULL parameter for %s", __FUNCTION__);
195✔
2931
    terrno = TSDB_CODE_INVALID_PARA;
195✔
2932
    return terrno;
195✔
2933
  }
2934

2935
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
17,761✔
2936
  STMT2_DLOG_E("start to get fields");
17,761✔
2937

2938
  if (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type ||
17,761✔
2939
      (pStmt->sql.type == 0 && stmt2IsInsert(stmt))) {
16,396✔
2940
    return stmtGetStbColFields2(stmt, count, fields);
13,861✔
2941
  }
2942
  if (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt))) {
3,900✔
2943
    return stmtGetParamNum2(stmt, count);
3,705✔
2944
  }
2945

2946
  tscError("Invalid sql for stmt %s", pStmt->sql.sqlStr);
195✔
2947
  return TSDB_CODE_PAR_SYNTAX_ERROR;
195✔
2948
}
2949

2950
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_ALL *fields) {
13,081✔
2951
  (void)stmt;
2952
  if (!fields) return;
13,081✔
2953
  taosMemoryFree(fields);
9,571✔
2954
}
2955

2956
TAOS_RES *taos_stmt2_result(TAOS_STMT2 *stmt) {
6,445✔
2957
  if (stmt == NULL) {
6,445✔
2958
    tscError("NULL parameter for %s", __FUNCTION__);
×
2959
    terrno = TSDB_CODE_INVALID_PARA;
×
2960
    return NULL;
×
2961
  }
2962

2963
  return stmtUseResult2(stmt);
6,445✔
2964
}
2965

2966
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmt2Errstr(stmt); }
6,045✔
2967

2968
int taos_set_conn_mode(TAOS *taos, int mode, int value) {
2,463✔
2969
  int32_t code = 0;
2,463✔
2970
  if (taos == NULL) {
2,463✔
2971
    terrno = TSDB_CODE_INVALID_PARA;
×
2972
    return terrno;
×
2973
  }
2974

2975
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2,463✔
2976
  if (NULL == pObj) {
2,463✔
2977
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2978
    tscError("invalid parameter for %s", __func__);
×
2979
    return terrno;
×
2980
  }
2981
  switch (mode) {
2,463✔
2982
    case TAOS_CONN_MODE_BI:
2,463✔
2983
      atomic_store_8(&pObj->biMode, value);
2,463✔
2984
      break;
2,463✔
2985
    default:
×
2986
      tscError("not supported mode.");
×
2987
      code = TSDB_CODE_INVALID_PARA;
×
2988
  }
2989
  releaseTscObj(*(int64_t *)taos);
2,463✔
2990
  return code;
2,463✔
2991
}
2992

2993
char *getBuildInfo() { return td_buildinfo; }
×
2994

2995
int32_t taos_connect_is_alive(TAOS *taos) {
×
2996
  int32_t code = 0, lino = 0;
×
2997
  if (taos == NULL) {
×
2998
    terrno = TSDB_CODE_INVALID_PARA;
×
2999
    return terrno;
×
3000
  }
3001

3002
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
3003
  if (NULL == pObj) {
×
3004
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
3005
    tscError("invalid parameter for %s", __func__);
×
3006
    return terrno;
×
3007
  }
3008

3009
  code = tscCheckConnSessionMetric(pObj);
×
3010
  TAOS_CHECK_GOTO(code, &lino, _error);
×
3011

3012
_error:
×
3013
  releaseTscObj(*(int64_t *)taos);
×
3014

3015
  if (code != 0) {
×
3016
    tscError("taos conn failed to check alive, code:%d - %s", code, tstrerror(code));
×
3017
  }
3018

3019
  return code != 0 ? 0 : 1;
×
3020
}
3021
static int32_t buildInstanceRegisterSql(const SInstanceRegisterReq *req, char **ppSql, uint32_t *pLen) {
×
3022
  const char *action = (req->expire < 0) ? "UNREGISTER" : "REGISTER";
×
3023
  int32_t     len = 0;
×
3024

3025
  len += snprintf(NULL, 0, "%s INSTANCE '%s'", action, req->id);
×
3026
  if (req->type[0] != 0) {
×
3027
    len += snprintf(NULL, 0, " TYPE '%s'", req->type);
×
3028
  }
3029
  if (req->desc[0] != 0) {
×
3030
    len += snprintf(NULL, 0, " DESC '%s'", req->desc);
×
3031
  }
3032
  if (req->expire >= 0) {
×
3033
    len += snprintf(NULL, 0, " EXPIRE %d", req->expire);
×
3034
  }
3035

3036
  char *sql = taosMemoryMalloc((size_t)len + 1);
×
3037
  if (sql == NULL) {
×
3038
    return terrno;
×
3039
  }
3040

3041
  int32_t offset = snprintf(sql, (size_t)len + 1, "%s INSTANCE '%s'", action, req->id);
×
3042
  if (req->type[0] != 0) {
×
3043
    offset += snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " TYPE '%s'", req->type);
×
3044
  }
3045
  if (req->desc[0] != 0) {
×
3046
    offset += snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " DESC '%s'", req->desc);
×
3047
  }
3048
  if (req->expire >= 0) {
×
3049
    (void)snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " EXPIRE %d", req->expire);
×
3050
  }
3051

3052
  *ppSql = sql;
×
3053
  if (pLen != NULL) {
×
3054
    *pLen = (uint32_t)len;
×
3055
  }
3056
  return TSDB_CODE_SUCCESS;
×
3057
}
3058

3059
static int32_t sendInstanceRegisterReq(STscObj *pObj, const SInstanceRegisterReq *req) {
×
3060
  SRequestObj *pRequest = NULL;
×
3061
  int32_t      code = createRequest(pObj->id, TDMT_MND_REGISTER_INSTANCE, 0, &pRequest);
×
3062
  if (code != TSDB_CODE_SUCCESS) {
×
3063
    terrno = code;
×
3064
    return code;
×
3065
  }
3066

3067
  code = buildInstanceRegisterSql(req, &pRequest->sqlstr, (uint32_t *)&pRequest->sqlLen);
×
3068
  if (code != TSDB_CODE_SUCCESS) {
×
3069
    goto _cleanup;
×
3070
  }
3071

3072
  int32_t msgLen = tSerializeSInstanceRegisterReq(NULL, 0, (SInstanceRegisterReq *)req);
×
3073
  if (msgLen <= 0) {
×
3074
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3075
    goto _cleanup;
×
3076
  }
3077

3078
  void *pMsg = taosMemoryMalloc(msgLen);
×
3079
  if (pMsg == NULL) {
×
3080
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3081
    goto _cleanup;
×
3082
  }
3083

3084
  if (tSerializeSInstanceRegisterReq(pMsg, msgLen, (SInstanceRegisterReq *)req) < 0) {
×
3085
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3086
    taosMemoryFree(pMsg);
×
3087
    goto _cleanup;
×
3088
  }
3089

3090
  pRequest->type = TDMT_MND_REGISTER_INSTANCE;
×
3091
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsg, .len = msgLen, .handle = NULL};
×
3092

3093
  SMsgSendInfo *pSend = buildMsgInfoImpl(pRequest);
×
3094
  if (pSend == NULL) {
×
3095
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3096
    taosMemoryFree(pMsg);
×
3097
    pRequest->body.requestMsg.pData = NULL;
×
3098
    goto _cleanup;
×
3099
  }
3100

3101
  SEpSet epSet = getEpSet_s(&pObj->pAppInfo->mgmtEp);
×
3102
  code = asyncSendMsgToServer(pObj->pAppInfo->pTransporter, &epSet, NULL, pSend);
×
3103
  if (code != TSDB_CODE_SUCCESS) {
×
3104
    destroySendMsgInfo(pSend);
×
3105
    pRequest->body.requestMsg = (SDataBuf){0};
×
3106
    goto _cleanup;
×
3107
  }
3108

3109
  code = tsem_wait(&pRequest->body.rspSem);
×
3110
  if (code != TSDB_CODE_SUCCESS) {
×
3111
    code = terrno != 0 ? terrno : code;
×
3112
    goto _cleanup;
×
3113
  }
3114

3115
  code = pRequest->code;
×
3116
  terrno = code;
×
3117

3118
_cleanup:
×
3119
  destroyRequest(pRequest);
×
3120
  return code;
×
3121
}
3122

3123
static bool instanceRegisterRpcRfp(int32_t code, tmsg_t msgType) {
×
3124
  if (NEED_REDIRECT_ERROR(code)) {
×
3125
    return true;
×
3126
  } else if (code == TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY || code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE ||
×
3127
             code == TSDB_CODE_SYN_WRITE_STALL || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
×
3128
             code == TSDB_CODE_SYN_RESTORING) {
3129
    tscDebug("client msg type %s should retry since %s", TMSG_INFO(msgType), tstrerror(code));
×
3130
    return true;
×
3131
  } else {
3132
    return false;
×
3133
  }
3134
}
3135

3136
/** Build epSet from firstEp and secondEp in config. pCfg must have valid firstEp. */
3137
static int32_t instanceBuildEpSetFromCfg(SConfig *pCfg, SEpSet *pEpSet) {
5,655✔
3138
  SConfigItem *pFirstEpItem = cfgGetItem(pCfg, "firstEp");
5,655✔
3139
  if (pFirstEpItem == NULL || pFirstEpItem->str == NULL || pFirstEpItem->str[0] == 0) {
5,655✔
3140
    return TSDB_CODE_CFG_NOT_FOUND;
×
3141
  }
3142
  SEp firstEp = {0};
5,655✔
3143
  int32_t code = taosGetFqdnPortFromEp(pFirstEpItem->str, &firstEp);
5,655✔
3144
  if (code != TSDB_CODE_SUCCESS) {
5,655✔
3145
    return code;
×
3146
  }
3147
  pEpSet->inUse = 0;
5,655✔
3148
  pEpSet->numOfEps = 1;
5,655✔
3149
  tstrncpy(pEpSet->eps[0].fqdn, firstEp.fqdn, TSDB_FQDN_LEN);
5,655✔
3150
  pEpSet->eps[0].port = firstEp.port;
5,655✔
3151

3152
  SConfigItem *pSecondEpItem = cfgGetItem(pCfg, "secondEp");
5,655✔
3153
  if (pSecondEpItem != NULL && pSecondEpItem->str != NULL && pSecondEpItem->str[0] != 0) {
5,655✔
3154
    SEp secondEp = {0};
5,655✔
3155
    if (taosGetFqdnPortFromEp(pSecondEpItem->str, &secondEp) == TSDB_CODE_SUCCESS) {
5,655✔
3156
      tstrncpy(pEpSet->eps[1].fqdn, secondEp.fqdn, TSDB_FQDN_LEN);
5,655✔
3157
      pEpSet->eps[1].port = secondEp.port;
5,655✔
3158
      pEpSet->numOfEps = 2;
5,655✔
3159
    }
3160
  }
3161
  return TSDB_CODE_SUCCESS;
5,655✔
3162
}
3163

3164
/** Init and open instance RPC client. label e.g. "INST" or "LIST". Returns handle or NULL. */
3165
static void *instanceOpenRpcClient(const char *label) {
5,655✔
3166
  SRpcInit rpcInit = {0};
5,655✔
3167
  rpcInit.label = (char *)label;
5,655✔
3168
  rpcInit.numOfThreads = 1;
5,655✔
3169
  rpcInit.cfp = NULL;
5,655✔
3170
  rpcInit.sessions = 16;
5,655✔
3171
  rpcInit.connType = TAOS_CONN_CLIENT;
5,655✔
3172
  rpcInit.idleTime = tsShellActivityTimer * 1000;
5,655✔
3173
  rpcInit.compressSize = tsCompressMsgSize;
5,655✔
3174
  rpcInit.user = TSDB_DEFAULT_USER;
5,655✔
3175
  rpcInit.rfp = instanceRegisterRpcRfp;
5,655✔
3176
  rpcInit.retryMinInterval = tsRedirectPeriod;
5,655✔
3177
  rpcInit.retryStepFactor = tsRedirectFactor;
5,655✔
3178
  rpcInit.retryMaxInterval = tsRedirectMaxPeriod;
5,655✔
3179
  rpcInit.retryMaxTimeout = tsMaxRetryWaitTime;
5,655✔
3180

3181
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
5,655✔
3182
  connLimitNum = TMAX(connLimitNum, 10);
5,655✔
3183
  connLimitNum = TMIN(connLimitNum, 500);
5,655✔
3184
  rpcInit.connLimitNum = connLimitNum;
5,655✔
3185
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
5,655✔
3186
  rpcInit.readTimeout = tsReadTimeout;
5,655✔
3187
  rpcInit.ipv6 = tsEnableIpv6;
5,655✔
3188
  rpcInit.enableSSL = tsEnableTLS;
5,655✔
3189

3190
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
5,655✔
3191
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
5,655✔
3192
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
5,655✔
3193
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
5,655✔
3194
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
5,655✔
3195

3196
  int32_t code = taosVersionStrToInt(td_version, &rpcInit.compatibilityVer);
5,655✔
3197
  if (code != TSDB_CODE_SUCCESS) {
5,655✔
3198
    tscError("failed to convert taos version from str to int, errcode:%s", terrstr(code));
×
3199
    terrno = code;
×
3200
    return NULL;
×
3201
  }
3202

3203
  void *clientRpc = rpcOpen(&rpcInit);
5,655✔
3204
  if (clientRpc == NULL) {
5,655✔
3205
    tscError("failed to init instance rpc client since %s", tstrerror(terrno));
×
3206
  }
3207
  return clientRpc;
5,655✔
3208
}
3209

3210
int32_t taos_register_instance(const char *id, const char *type, const char *desc, int32_t expire) {
3,120✔
3211
  if (id == NULL || id[0] == 0) {
3,120✔
3212
    return terrno = TSDB_CODE_INVALID_PARA;
×
3213
  }
3214

3215
  // Validate string lengths
3216
  size_t idLen = strlen(id);
3,120✔
3217
  if (idLen >= TSDB_INSTANCE_ID_LEN) {
3,120✔
3218
    tscError("instance id length %zu exceeds limit %d", idLen, TSDB_INSTANCE_ID_LEN - 1);
×
3219
    return terrno = TSDB_CODE_INVALID_PARA;
×
3220
  }
3221

3222
  if (type != NULL && type[0] != 0) {
3,120✔
3223
    size_t typeLen = strlen(type);
1,950✔
3224
    if (typeLen >= TSDB_INSTANCE_TYPE_LEN) {
1,950✔
3225
      tscError("instance type length %zu exceeds limit %d", typeLen, TSDB_INSTANCE_TYPE_LEN - 1);
×
3226
      return terrno = TSDB_CODE_INVALID_PARA;
×
3227
    }
3228
  }
3229

3230
  if (desc != NULL && desc[0] != 0) {
3,120✔
3231
    size_t descLen = strlen(desc);
1,950✔
3232
    if (descLen >= TSDB_INSTANCE_DESC_LEN) {
1,950✔
3233
      tscError("instance desc length %zu exceeds limit %d", descLen, TSDB_INSTANCE_DESC_LEN - 1);
×
3234
      return terrno = TSDB_CODE_INVALID_PARA;
×
3235
    }
3236
  }
3237

3238
  int32_t code = taos_init();
3,120✔
3239
  if (code != TSDB_CODE_SUCCESS) {
3,120✔
3240
    return code;
×
3241
  }
3242

3243
  SConfig *pCfg = taosGetCfg();
3,120✔
3244
  if (pCfg == NULL) {
3,120✔
3245
    return terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3246
  }
3247

3248
  SEpSet epSet = {0};
3,120✔
3249
  code = instanceBuildEpSetFromCfg(pCfg, &epSet);
3,120✔
3250
  if (code != TSDB_CODE_SUCCESS) {
3,120✔
3251
    return terrno = code;
×
3252
  }
3253

3254
  void *clientRpc = instanceOpenRpcClient("INST");
3,120✔
3255
  if (clientRpc == NULL) {
3,120✔
3256
    return terrno;
×
3257
  }
3258

3259
  SRpcMsg rpcMsg = {0};
3,120✔
3260
  SRpcMsg rpcRsp = {0};
3,120✔
3261

3262
  // Prepare request
3263
  SInstanceRegisterReq req = {0};
3,120✔
3264
  tstrncpy(req.id, id, sizeof(req.id));
3,120✔
3265
  if (type != NULL && type[0] != 0) {
3,120✔
3266
    tstrncpy(req.type, type, sizeof(req.type));
1,950✔
3267
  }
3268
  if (desc != NULL && desc[0] != 0) {
3,120✔
3269
    tstrncpy(req.desc, desc, sizeof(req.desc));
1,950✔
3270
  }
3271
  req.expire = expire;
3,120✔
3272

3273
  int32_t contLen = tSerializeSInstanceRegisterReq(NULL, 0, &req);
3,120✔
3274
  if (contLen <= 0) {
3,120✔
3275
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3276
    rpcClose(clientRpc);
×
3277
    return code;
×
3278
  }
3279

3280
  void *pCont = rpcMallocCont(contLen);
3,120✔
3281
  if (pCont == NULL) {
3,120✔
3282
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3283
    rpcClose(clientRpc);
×
3284
    return code;
×
3285
  }
3286

3287
  if (tSerializeSInstanceRegisterReq(pCont, contLen, &req) < 0) {
3,120✔
3288
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3289
    rpcFreeCont(pCont);
×
3290
    rpcClose(clientRpc);
×
3291
    return code;
×
3292
  }
3293

3294
  rpcMsg.pCont = pCont;
3,120✔
3295
  rpcMsg.contLen = contLen;
3,120✔
3296
  rpcMsg.msgType = TDMT_MND_REGISTER_INSTANCE;
3,120✔
3297
  rpcMsg.info.ahandle = (void *)0x9528;  // Different magic number from server status
3,120✔
3298
  rpcMsg.info.notFreeAhandle = 1;
3,120✔
3299

3300
  code = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
3,120✔
3301
  if (TSDB_CODE_SUCCESS != code) {
3,120✔
3302
    tscError("failed to send instance register req since %s", tstrerror(code));
×
3303
    // rpcSendRecv failed, pCont may not be freed, but check _RETURN1 path
3304
    // In error path, rpcSendRecv may free pCont, but we free it here to be safe
3305
    rpcClose(clientRpc);
×
3306
    return code;
×
3307
  }
3308

3309
  if (rpcRsp.code != 0) {
3,120✔
3310
    code = rpcRsp.code;
×
3311
    tscError("instance register failed, code:%s", tstrerror(code));
×
3312
  } else {
3313
    code = TSDB_CODE_SUCCESS;
3,120✔
3314
  }
3315

3316
  if (rpcRsp.pCont != NULL) {
3,120✔
3317
    rpcFreeCont(rpcRsp.pCont);
3,120✔
3318
  }
3319
  rpcClose(clientRpc);
3,120✔
3320

3321
  terrno = code;
3,120✔
3322
  return code;
3,120✔
3323
}
3324

3325
int32_t taos_list_instances(const char *filter_type, char ***pList, int32_t *pCount) {
2,535✔
3326
  if (pList == NULL || pCount == NULL) {
2,535✔
3327
    return TSDB_CODE_INVALID_PARA;
×
3328
  }
3329

3330
  int32_t code = taos_init();
2,535✔
3331
  if (code != TSDB_CODE_SUCCESS) {
2,535✔
3332
    terrno = code;
×
3333
    return code;
×
3334
  }
3335

3336
  SConfig *pCfg = taosGetCfg();
2,535✔
3337
  if (pCfg == NULL) {
2,535✔
3338
    terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3339
    return TSDB_CODE_CFG_NOT_FOUND;
×
3340
  }
3341

3342
  SEpSet epSet = {0};
2,535✔
3343
  code = instanceBuildEpSetFromCfg(pCfg, &epSet);
2,535✔
3344
  if (code != TSDB_CODE_SUCCESS) {
2,535✔
3345
    terrno = code;
×
3346
    return code;
×
3347
  }
3348

3349
  void *clientRpc = instanceOpenRpcClient("LIST");
2,535✔
3350
  if (clientRpc == NULL) {
2,535✔
3351
    return terrno;
×
3352
  }
3353

3354
  SRpcMsg rpcMsg = {0};
2,535✔
3355
  SRpcMsg rpcRsp = {0};
2,535✔
3356

3357
  SInstanceListReq req = {0};
2,535✔
3358
  if (filter_type != NULL && filter_type[0] != 0) {
2,535✔
3359
    tstrncpy(req.filter_type, filter_type, sizeof(req.filter_type));
1,560✔
3360
  }
3361

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

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

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

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

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

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

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

3444
  if (rpcRsp.pCont != NULL) {
2,535✔
3445
    rpcFreeCont(rpcRsp.pCont);
2,535✔
3446
  }
3447
  rpcClose(clientRpc);
2,535✔
3448

3449
  return TSDB_CODE_SUCCESS;
2,535✔
3450
}
3451

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

3457
  // Free each string in the array
3458
  for (int32_t i = 0; i < count; i++) {
5,460✔
3459
    if ((*pList)[i] != NULL) {
3,705✔
3460
      taosMemoryFree((*pList)[i]);
3,705✔
3461
      (*pList)[i] = NULL;
3,705✔
3462
    }
3463
  }
3464

3465
  // Free the array itself
3466
  taosMemoryFree(*pList);
1,755✔
3467
  *pList = NULL;
1,755✔
3468
}
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