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

taosdata / TDengine / #4986

15 Mar 2026 08:32AM UTC coverage: 37.305% (-31.3%) from 68.601%
#4986

push

travis-ci

tomchon
test: keep docs and unit test

125478 of 336361 relevant lines covered (37.3%)

1134847.06 hits per line

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

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

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

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

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

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

86
void tzCleanup() {
48✔
87
  taosHashCleanup(pTimezoneMap);
48✔
88
  taosHashCleanup(pTimezoneNameMap);
48✔
89
}
48✔
90

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

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

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

128
END:
35✔
129
  return tz;
58✔
130
}
131
#endif
132

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

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

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

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

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

160
  if (option == TSDB_OPTION_CONNECTION_CLEAR) {
138✔
161
    val = NULL;
3✔
162
  }
163

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

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

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

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

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

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

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

254
  monitorClose();
48✔
255
  tscStopCrashReport();
48✔
256

257
  hbMgrCleanUp();
48✔
258

259
  catalogDestroy();
48✔
260
  schedulerDestroy();
48✔
261

262
  fmFuncMgtDestroy();
48✔
263
  qCleanupKeywordsTable();
48✔
264

265
#if !defined(WINDOWS) && !defined(TD_ASTRA)
266
  tzCleanup();
48✔
267
#endif
268
  tmqMgmtClose();
48✔
269

270
  int32_t id = clientReqRefPool;
48✔
271
  clientReqRefPool = -1;
48✔
272
  taosCloseRef(id);
48✔
273

274
  id = clientConnRefPool;
48✔
275
  clientConnRefPool = -1;
48✔
276
  taosCloseRef(id);
48✔
277

278
  nodesDestroyAllocatorSet();
48✔
279
  cleanupAppInfo();
48✔
280
  rpcCleanup();
48✔
281
  tscDebug("rpc cleanup");
48✔
282

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

287
  sessMgtDestroy();
48✔
288

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

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

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

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

320
  if (pass == NULL) {
274✔
321
    pass = TSDB_DEFAULT_PASS;
5✔
322
  }
323

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

338
  return NULL;
4✔
339
}
340

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

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

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

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

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

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

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

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

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

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

431
  return taos;
4✔
432
}
433

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

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

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

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

496
  releaseTscObj(*(int64_t *)taos);
×
497
  return 0;
×
498
}
499

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

627
  __taos_async_ip_whitelist_fn_t userCbFn;
628
} SFetchIpWhiteListInfo;
629

630
int32_t fetchIpWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
631
  int32_t lino = 0;
×
632
  char  **pWhiteLists = NULL;
×
633

634
  SGetUserIpWhiteListRsp wlRsp = {0};
×
635

636
  SFetchIpWhiteListInfo *pInfo = (SFetchIpWhiteListInfo *)param;
×
637
  TAOS                  *taos = &pInfo->connId;
×
638

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

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

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

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

662
    code = tIpUintToStr(pIpRange, &ipAddr);
×
663
    TAOS_CHECK_GOTO(code, &lino, _error);
×
664

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

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

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

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

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

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

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

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

748
  pParam->connId = connId;
×
749
  pParam->supportNeg = supportNeg;
×
750
  pParam->userCbFn = fp;
×
751
  pParam->userParam = param;
×
752

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

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

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

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

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

785
typedef struct SFetchDateTimeWhiteListInfo {
786
  int64_t                              connId;
787
  void                                *userParam;
788
  __taos_async_datetime_whitelist_fn_t userCbFn;
789
} SFetchDateTimeWhiteListInfo;
790

791
static const char *weekdays[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
792
int32_t            fetchDateTimeWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
793
  int32_t lino = 0;
×
794
  char  **pWhiteLists = NULL;
×
795

796
  SUserDateTimeWhiteList wlRsp = {0};
×
797

798
  SFetchDateTimeWhiteListInfo *pInfo = (SFetchDateTimeWhiteListInfo *)param;
×
799
  TAOS                        *taos = &pInfo->connId;
×
800

801
  if (code != TSDB_CODE_SUCCESS) {
×
802
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
803
    TAOS_CHECK_GOTO(code, &lino, _error);
×
804
  }
805

806
  if ((code = tDeserializeSUserDateTimeWhiteList(pMsg->pData, pMsg->len, &wlRsp)) != TSDB_CODE_SUCCESS) {
×
807
    TAOS_CHECK_GOTO(code, &lino, _error);
×
808
  }
809

810
  pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(char *));
×
811
  if (pWhiteLists == NULL) {
×
812
    code = terrno;
×
813
    TAOS_CHECK_GOTO(code, &lino, _error);
×
814
  }
815

816
  int32_t numWhiteLists = 0;
×
817
  for (int32_t i = 0; i < wlRsp.numWhiteLists; i++) {
×
818
    SDateTimeWhiteListItem *item = &wlRsp.pWhiteLists[i];
×
819

820
    char *p = taosMemCalloc(1, 128);
×
821
    if (p == NULL) {
×
822
      code = terrno;
×
823
      TAOS_CHECK_GOTO(code, &lino, _error);
×
824
    }
825

826
    int duration = item->duration / 60;
×
827

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

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

857
void taos_fetch_datetime_whitelist_a(TAOS *taos, __taos_async_datetime_whitelist_fn_t fp, void *param) {
×
858
  if (NULL == taos) {
×
859
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
860
    return;
×
861
  }
862
  int64_t connId = *(int64_t *)taos;
×
863

864
  STscObj *pTsc = acquireTscObj(connId);
×
865
  if (NULL == pTsc) {
×
866
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
867
    return;
×
868
  }
869

870
  SGetUserWhiteListReq req;
871
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
872
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
873
  if (msgLen < 0) {
×
874
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
875
    releaseTscObj(connId);
×
876
    return;
×
877
  }
878

879
  void *pReq = taosMemoryMalloc(msgLen);
×
880
  if (pReq == NULL) {
×
881
    fp(param, terrno, taos, 0, NULL);
×
882
    releaseTscObj(connId);
×
883
    return;
×
884
  }
885

886
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
887
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
888
    taosMemoryFree(pReq);
×
889
    releaseTscObj(connId);
×
890
    return;
×
891
  }
892

893
  SFetchDateTimeWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchDateTimeWhiteListInfo));
×
894
  if (pParam == NULL) {
×
895
    fp(param, terrno, taos, 0, NULL);
×
896
    taosMemoryFree(pReq);
×
897
    releaseTscObj(connId);
×
898
    return;
×
899
  }
900

901
  pParam->connId = connId;
×
902
  pParam->userCbFn = fp;
×
903
  pParam->userParam = param;
×
904

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

914
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
915
  pSendInfo->requestId = generateRequestId();
×
916
  pSendInfo->requestObjRefId = 0;
×
917
  pSendInfo->param = pParam;
×
918
  pSendInfo->fp = fetchDateTimeWhiteListCallbackFn;
×
919
  pSendInfo->msgType = TDMT_MND_GET_USER_DATETIME_WHITELIST;
×
920

921
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
922
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
923
    tscWarn("failed to async send msg to server");
×
924
  }
925
  releaseTscObj(connId);
×
926
  return;
×
927
}
928

929
void taos_close_internal(void *taos) {
271✔
930
  if (taos == NULL) {
271✔
931
    return;
×
932
  }
933
  int32_t code = 0;
271✔
934

935
  STscObj *pTscObj = (STscObj *)taos;
271✔
936
  tscDebug("conn:0x%" PRIx64 ", try to close connection, numOfReq:%d", pTscObj->id, pTscObj->numOfReqs);
271✔
937

938
  SSessParam para = {.type = SESSION_PER_USER, .value = -1, .noCheck = 1};
271✔
939

940
  code = tscUpdateSessMetric(pTscObj, &para);
271✔
941
  if (code != TSDB_CODE_SUCCESS) {
271✔
942
    tscWarn("conn:0x%" PRIx64 ", failed to update user:%s metric when close connection, code:%d", pTscObj->id,
×
943
            pTscObj->user, code);
944
  }
945

946
  code = tscUnrefSessMetric(pTscObj);
271✔
947
  if (TSDB_CODE_SUCCESS != taosRemoveRef(clientConnRefPool, pTscObj->id)) {
271✔
948
    tscError("conn:0x%" PRIx64 ", failed to remove ref from conn pool", pTscObj->id);
×
949
  }
950
}
951

952
void taos_close(TAOS *taos) {
259✔
953
  if (taos == NULL) {
259✔
954
    return;
1✔
955
  }
956

957
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
258✔
958
  if (NULL == pObj) {
258✔
959
    taosMemoryFree(taos);
×
960
    return;
×
961
  }
962

963
  taos_close_internal(pObj);
258✔
964
  releaseTscObj(*(int64_t *)taos);
258✔
965
  taosMemoryFree(taos);
258✔
966
}
967

968
int taos_errno(TAOS_RES *res) {
2,490✔
969
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,490✔
970
    return terrno;
7✔
971
  }
972

973
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,483✔
974
    return 0;
×
975
  }
976

977
  return ((SRequestObj *)res)->code;
2,483✔
978
}
979

980
const char *taos_errstr(TAOS_RES *res) {
47✔
981
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
47✔
982
    if (*(taosGetErrMsg()) == 0) {
3✔
983
      return (const char *)tstrerror(terrno);
3✔
984
    } else {
985
      (void)snprintf(taosGetErrMsgReturn(), ERR_MSG_LEN, "%s", taosGetErrMsg());
×
986
      return (const char *)taosGetErrMsgReturn();
×
987
    }
988
  }
989

990
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
44✔
991
    return "success";
×
992
  }
993

994
  SRequestObj *pRequest = (SRequestObj *)res;
44✔
995
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
44✔
996
    return pRequest->msgBuf;
26✔
997
  } else {
998
    return (const char *)tstrerror(pRequest->code);
18✔
999
  }
1000
}
1001

1002
void taos_free_result(TAOS_RES *res) {
3,824✔
1003
  if (NULL == res) {
3,824✔
1004
    return;
359✔
1005
  }
1006

1007
  tscTrace("res:%p, will be freed", res);
3,465✔
1008

1009
  if (TD_RES_QUERY(res)) {
3,465✔
1010
    SRequestObj *pRequest = (SRequestObj *)res;
3,159✔
1011
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
3,159✔
1012
    destroyRequest(pRequest);
3,159✔
1013
    return;
3,159✔
1014
  }
1015

1016
  SMqRspObj *pRsp = (SMqRspObj *)res;
306✔
1017
  if (TD_RES_TMQ(res)) {
306✔
1018
    tDeleteMqDataRsp(&pRsp->dataRsp);
306✔
1019
    doFreeReqResultInfo(&pRsp->resInfo);
306✔
1020
  } else if (TD_RES_TMQ_METADATA(res)) {
×
1021
    tDeleteSTaosxRsp(&pRsp->dataRsp);
×
1022
    doFreeReqResultInfo(&pRsp->resInfo);
×
1023
  } else if (TD_RES_TMQ_META(res)) {
×
1024
    tDeleteMqMetaRsp(&pRsp->metaRsp);
×
1025
  } else if (TD_RES_TMQ_BATCH_META(res)) {
×
1026
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
×
1027
  } else if (TD_RES_TMQ_RAW(res)) {
×
1028
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
1029
  }
1030
  taosMemoryFree(pRsp);
306✔
1031
}
1032

1033
void taos_kill_query(TAOS *taos) {
×
1034
  if (NULL == taos) {
×
1035
    return;
×
1036
  }
1037

1038
  int64_t  rid = *(int64_t *)taos;
×
1039
  STscObj *pTscObj = acquireTscObj(rid);
×
1040
  if (pTscObj) {
×
1041
    stopAllRequests(pTscObj->pRequests);
×
1042
  }
1043
  releaseTscObj(rid);
×
1044
}
1045

1046
int taos_field_count(TAOS_RES *res) {
5,876✔
1047
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
5,876✔
1048
    return 0;
×
1049
  }
1050

1051
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
5,876✔
1052
  return pResInfo->numOfCols;
5,876✔
1053
}
1054

1055
int taos_num_fields(TAOS_RES *res) { return taos_field_count(res); }
5,872✔
1056

1057
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
27✔
1058
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
27✔
1059
    return NULL;
12✔
1060
  }
1061

1062
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
15✔
1063
  return pResInfo->userFields;
15✔
1064
}
1065

1066
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
2,527✔
1067
TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqid) {
2✔
1068
  return taosQueryImplWithReqid(taos, sql, false, reqid);
2✔
1069
}
1070

1071
TAOS_FIELD_E *taos_fetch_fields_e(TAOS_RES *res) {
×
1072
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1073
    return NULL;
×
1074
  }
1075
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
1076
  return pResInfo->fields;
×
1077
}
1078

1079
TAOS_ROW taos_fetch_row(TAOS_RES *res) {
6,132✔
1080
  if (res == NULL) {
6,132✔
1081
    return NULL;
×
1082
  }
1083

1084
  if (TD_RES_QUERY(res)) {
6,132✔
1085
    SRequestObj *pRequest = (SRequestObj *)res;
5,418✔
1086
    if (pRequest->killed) {
5,418✔
1087
      tscInfo("query has been killed, can not fetch more row.");
×
1088
      pRequest->code = TSDB_CODE_TSC_QUERY_KILLED;
×
1089
      return NULL;
×
1090
    }
1091

1092
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
5,418✔
1093
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
5,418✔
1094
      return NULL;
3✔
1095
    }
1096

1097
    if (pRequest->inCallback) {
5,415✔
1098
      tscError("can not call taos_fetch_row before query callback ends.");
3✔
1099
      terrno = TSDB_CODE_TSC_INVALID_OPERATION;
3✔
1100
      return NULL;
3✔
1101
    }
1102

1103
    return doAsyncFetchRows(pRequest, true, true);
5,412✔
1104
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
714✔
1105
    SMqRspObj      *msg = ((SMqRspObj *)res);
714✔
1106
    SReqResultInfo *pResultInfo = NULL;
714✔
1107
    if (msg->resIter == -1) {
714✔
1108
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
306✔
1109
        return NULL;
×
1110
      }
1111
    } else {
1112
      pResultInfo = tmqGetCurResInfo(res);
408✔
1113
    }
1114

1115
    if (pResultInfo->current < pResultInfo->numOfRows) {
714✔
1116
      doSetOneRowPtr(pResultInfo);
408✔
1117
      pResultInfo->current += 1;
408✔
1118
      return pResultInfo->row;
408✔
1119
    } else {
1120
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
306✔
1121
        return NULL;
306✔
1122
      }
1123

1124
      doSetOneRowPtr(pResultInfo);
×
1125
      pResultInfo->current += 1;
×
1126
      return pResultInfo->row;
×
1127
    }
1128
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1129
    return NULL;
×
1130
  } else {
1131
    tscError("invalid result passed to taos_fetch_row");
×
1132
    terrno = TSDB_CODE_TMQ_INVALID_DATA;
×
1133
    return NULL;
×
1134
  }
1135
}
1136

1137
int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
721✔
1138
  return taos_print_row_with_size(str, INT32_MAX, row, fields, num_fields);
721✔
1139
}
1140
int taos_print_row_with_size(char *str, uint32_t size, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
721✔
1141
  int32_t len = 0;
721✔
1142
  for (int i = 0; i < num_fields; ++i) {
1,711✔
1143
    if (i > 0 && len < size - 1) {
990✔
1144
      str[len++] = ' ';
269✔
1145
    }
1146

1147
    if (row[i] == NULL) {
990✔
1148
      len += tsnprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
18✔
1149
      continue;
18✔
1150
    }
1151

1152
    switch (fields[i].type) {
972✔
1153
      case TSDB_DATA_TYPE_TINYINT:
17✔
1154
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
17✔
1155
        break;
17✔
1156

1157
      case TSDB_DATA_TYPE_UTINYINT:
×
1158
        len += tsnprintf(str + len, size - len, "%u", *((uint8_t *)row[i]));
×
1159
        break;
×
1160

1161
      case TSDB_DATA_TYPE_SMALLINT:
2✔
1162
        len += tsnprintf(str + len, size - len, "%d", *((int16_t *)row[i]));
2✔
1163
        break;
2✔
1164

1165
      case TSDB_DATA_TYPE_USMALLINT:
×
1166
        len += tsnprintf(str + len, size - len, "%u", *((uint16_t *)row[i]));
×
1167
        break;
×
1168

1169
      case TSDB_DATA_TYPE_INT:
208✔
1170
        len += tsnprintf(str + len, size - len, "%d", *((int32_t *)row[i]));
208✔
1171
        break;
208✔
1172

1173
      case TSDB_DATA_TYPE_UINT:
×
1174
        len += tsnprintf(str + len, size - len, "%u", *((uint32_t *)row[i]));
×
1175
        break;
×
1176

1177
      case TSDB_DATA_TYPE_BIGINT:
4✔
1178
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
4✔
1179
        break;
4✔
1180

1181
      case TSDB_DATA_TYPE_UBIGINT:
×
1182
        len += tsnprintf(str + len, size - len, "%" PRIu64, *((uint64_t *)row[i]));
×
1183
        break;
×
1184

1185
      case TSDB_DATA_TYPE_FLOAT: {
×
1186
        float fv = 0;
×
1187
        fv = GET_FLOAT_VAL(row[i]);
×
1188
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
×
1189
      } break;
×
1190

1191
      case TSDB_DATA_TYPE_DOUBLE: {
×
1192
        double dv = 0;
×
1193
        dv = GET_DOUBLE_VAL(row[i]);
×
1194
        len += snprintf(str + len, size - len, "%.*g", DBL_DIG, dv);
×
1195
      } break;
×
1196

1197
      case TSDB_DATA_TYPE_VARBINARY: {
×
1198
        void    *data = NULL;
×
1199
        uint32_t tmp = 0;
×
1200
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
×
1201
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
×
1202
          break;
×
1203
        }
1204
        uint32_t copyLen = TMIN(size - len - 1, tmp);
×
1205
        (void)memcpy(str + len, data, copyLen);
×
1206
        len += copyLen;
×
1207
        taosMemoryFree(data);
×
1208
      } break;
×
1209
      case TSDB_DATA_TYPE_BINARY:
534✔
1210
      case TSDB_DATA_TYPE_NCHAR:
1211
      case TSDB_DATA_TYPE_GEOMETRY: {
1212
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
534✔
1213
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
534✔
1214
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
×
1215
          if (charLen > fields[i].bytes || charLen < 0) {
534✔
1216
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
×
1217
            break;
×
1218
          }
1219
        } else {
1220
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
×
1221
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
×
1222
            break;
×
1223
          }
1224
        }
1225

1226
        uint32_t copyLen = TMIN(size - len - 1, charLen);
534✔
1227
        (void)memcpy(str + len, row[i], copyLen);
534✔
1228
        len += copyLen;
534✔
1229
      } break;
534✔
1230
      case TSDB_DATA_TYPE_BLOB:
×
1231
      case TSDB_DATA_TYPE_MEDIUMBLOB: {
1232
        void    *data = NULL;
×
1233
        uint32_t tmp = 0;
×
1234
        int32_t  charLen = blobDataLen((char *)row[i] - BLOBSTR_HEADER_SIZE);
×
1235
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
×
1236
          break;
×
1237
        }
1238

1239
        uint32_t copyLen = TMIN(size - len - 1, tmp);
×
1240
        (void)memcpy(str + len, data, copyLen);
×
1241
        len += copyLen;
×
1242

1243
        taosMemoryFree(data);
×
1244
      } break;
×
1245

1246
      case TSDB_DATA_TYPE_TIMESTAMP:
205✔
1247
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
205✔
1248
        break;
205✔
1249

1250
      case TSDB_DATA_TYPE_BOOL:
2✔
1251
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
2✔
1252
        break;
2✔
1253
      case TSDB_DATA_TYPE_DECIMAL64:
×
1254
      case TSDB_DATA_TYPE_DECIMAL: {
1255
        uint32_t decimalLen = strlen(row[i]);
×
1256
        uint32_t copyLen = TMIN(size - len - 1, decimalLen);
×
1257
        (void)memcpy(str + len, row[i], copyLen);
×
1258
        len += copyLen;
×
1259
      } break;
×
1260
      default:
×
1261
        break;
×
1262
    }
1263

1264
    if (len >= size - 1) {
972✔
1265
      break;
×
1266
    }
1267
  }
1268
  if (len < size) {
721✔
1269
    str[len] = 0;
721✔
1270
  }
1271

1272
  return len;
721✔
1273
}
1274

1275
int *taos_fetch_lengths(TAOS_RES *res) {
×
1276
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1277
    return NULL;
×
1278
  }
1279

1280
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
1281
  return pResInfo->length;
×
1282
}
1283

1284
TAOS_ROW *taos_result_block(TAOS_RES *res) {
×
1285
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1286
    terrno = TSDB_CODE_INVALID_PARA;
×
1287
    return NULL;
×
1288
  }
1289

1290
  if (taos_is_update_query(res)) {
×
1291
    return NULL;
×
1292
  }
1293

1294
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
1295
  return &pResInfo->row;
×
1296
}
1297

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

1349
const char *taos_get_client_info() { return td_version; }
6✔
1350

1351
// return int32_t
1352
int taos_affected_rows(TAOS_RES *res) {
477✔
1353
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
477✔
1354
      TD_RES_TMQ_BATCH_META(res)) {
477✔
1355
    return 0;
×
1356
  }
1357

1358
  SRequestObj    *pRequest = (SRequestObj *)res;
477✔
1359
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
477✔
1360
  return (int)pResInfo->numOfRows;
477✔
1361
}
1362

1363
// return int64_t
1364
int64_t taos_affected_rows64(TAOS_RES *res) {
6✔
1365
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
6✔
1366
      TD_RES_TMQ_BATCH_META(res)) {
6✔
1367
    return 0;
×
1368
  }
1369

1370
  SRequestObj    *pRequest = (SRequestObj *)res;
6✔
1371
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
6✔
1372
  return pResInfo->numOfRows;
6✔
1373
}
1374

1375
int taos_result_precision(TAOS_RES *res) {
×
1376
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1377
    return TSDB_TIME_PRECISION_MILLI;
×
1378
  }
1379

1380
  if (TD_RES_QUERY(res)) {
×
1381
    SRequestObj *pRequest = (SRequestObj *)res;
×
1382
    return pRequest->body.resInfo.precision;
×
1383
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
×
1384
    SReqResultInfo *info = tmqGetCurResInfo(res);
×
1385
    return info->precision;
×
1386
  }
1387
  return TSDB_TIME_PRECISION_MILLI;
×
1388
}
1389

1390
int taos_select_db(TAOS *taos, const char *db) {
×
1391
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
1392
  if (pObj == NULL) {
×
1393
    releaseTscObj(*(int64_t *)taos);
×
1394
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1395
    return TSDB_CODE_TSC_DISCONNECTED;
×
1396
  }
1397

1398
  if (db == NULL || strlen(db) == 0) {
×
1399
    releaseTscObj(*(int64_t *)taos);
×
1400
    tscError("invalid parameter for %s", db == NULL ? "db is NULL" : "db is empty");
×
1401
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1402
    return terrno;
×
1403
  }
1404

1405
  char sql[256] = {0};
×
1406
  (void)snprintf(sql, tListLen(sql), "use %s", db);
×
1407

1408
  TAOS_RES *pRequest = taos_query(taos, sql);
×
1409
  int32_t   code = taos_errno(pRequest);
×
1410

1411
  taos_free_result(pRequest);
×
1412
  releaseTscObj(*(int64_t *)taos);
×
1413
  return code;
×
1414
}
1415

1416
void taos_stop_query(TAOS_RES *res) {
3,451✔
1417
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
3,451✔
1418
      TD_RES_TMQ_BATCH_META(res)) {
3,451✔
1419
    return;
×
1420
  }
1421

1422
  stopAllQueries((SRequestObj *)res);
3,451✔
1423
}
1424

1425
bool taos_is_null(TAOS_RES *res, int32_t row, int32_t col) {
×
1426
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1427
    return true;
×
1428
  }
1429
  SReqResultInfo *pResultInfo = tscGetCurResInfo(res);
×
1430
  if (col >= pResultInfo->numOfCols || col < 0 || row >= pResultInfo->numOfRows || row < 0) {
×
1431
    return true;
×
1432
  }
1433

1434
  SResultColumn *pCol = &pResultInfo->pCol[col];
×
1435
  if (IS_VAR_DATA_TYPE(pResultInfo->fields[col].type)) {
×
1436
    return (pCol->offset[row] == -1);
×
1437
  } else {
1438
    return colDataIsNull_f(pCol, row);
×
1439
  }
1440
}
1441

1442
bool taos_is_update_query(TAOS_RES *res) { return taos_num_fields(res) == 0; }
×
1443

1444
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
×
1445
  int32_t numOfRows = 0;
×
1446
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
×
1447
  return numOfRows;
×
1448
}
1449

1450
int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) {
×
1451
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1452
    return 0;
×
1453
  }
1454

1455
  if (TD_RES_QUERY(res)) {
×
1456
    SRequestObj *pRequest = (SRequestObj *)res;
×
1457

1458
    (*rows) = NULL;
×
1459
    (*numOfRows) = 0;
×
1460

1461
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
×
1462
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
×
1463
      return pRequest->code;
×
1464
    }
1465

1466
    (void)doAsyncFetchRows(pRequest, false, true);
×
1467

1468
    // TODO refactor
1469
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
×
1470
    pResultInfo->current = pResultInfo->numOfRows;
×
1471

1472
    (*rows) = pResultInfo->row;
×
1473
    (*numOfRows) = pResultInfo->numOfRows;
×
1474
    return pRequest->code;
×
1475
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
×
1476
    SReqResultInfo *pResultInfo = NULL;
×
1477
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
×
1478
    if (code != 0) return code;
×
1479

1480
    pResultInfo->current = pResultInfo->numOfRows;
×
1481
    (*rows) = pResultInfo->row;
×
1482
    (*numOfRows) = pResultInfo->numOfRows;
×
1483
    return 0;
×
1484
  } else {
1485
    tscError("taos_fetch_block_s invalid res type");
×
1486
    return TSDB_CODE_TMQ_INVALID_DATA;
×
1487
  }
1488
}
1489

1490
int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) {
×
1491
  *numOfRows = 0;
×
1492
  *pData = NULL;
×
1493

1494
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1495
    return 0;
×
1496
  }
1497

1498
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
×
1499
    SReqResultInfo *pResultInfo = NULL;
×
1500
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
×
1501
    if (code != 0) {
×
1502
      (*numOfRows) = 0;
×
1503
      return 0;
×
1504
    }
1505

1506
    pResultInfo->current = pResultInfo->numOfRows;
×
1507
    (*numOfRows) = pResultInfo->numOfRows;
×
1508
    (*pData) = (void *)pResultInfo->pData;
×
1509
    return 0;
×
1510
  }
1511

1512
  SRequestObj *pRequest = (SRequestObj *)res;
×
1513

1514
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
×
1515
      pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
×
1516
    return pRequest->code;
×
1517
  }
1518

1519
  (void)doAsyncFetchRows(pRequest, false, false);
×
1520

1521
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
×
1522

1523
  pResultInfo->current = pResultInfo->numOfRows;
×
1524
  (*numOfRows) = pResultInfo->numOfRows;
×
1525
  (*pData) = (void *)pResultInfo->pData;
×
1526

1527
  return pRequest->code;
×
1528
}
1529

1530
int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) {
×
1531
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1532
    return NULL;
×
1533
  }
1534

1535
  int32_t numOfFields = taos_num_fields(res);
×
1536
  if (columnIndex < 0 || columnIndex >= numOfFields || numOfFields == 0) {
×
1537
    return NULL;
×
1538
  }
1539

1540
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
1541
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
×
1542
  if (!IS_VAR_DATA_TYPE(pField->type)) {
×
1543
    return NULL;
×
1544
  }
1545

1546
  return pResInfo->pCol[columnIndex].offset;
×
1547
}
1548

1549
int taos_is_null_by_column(TAOS_RES *res, int columnIndex, bool result[], int *rows) {
×
1550
  if (res == NULL || result == NULL || rows == NULL || *rows <= 0 || columnIndex < 0 || TD_RES_TMQ_META(res) ||
×
1551
      TD_RES_TMQ_RAW(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1552
    return TSDB_CODE_INVALID_PARA;
×
1553
  }
1554

1555
  int32_t numOfFields = taos_num_fields(res);
×
1556
  if (columnIndex >= numOfFields || numOfFields == 0) {
×
1557
    return TSDB_CODE_INVALID_PARA;
×
1558
  }
1559

1560
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
1561
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
×
1562
  SResultColumn  *pCol = &pResInfo->pCol[columnIndex];
×
1563

1564
  if (*rows > pResInfo->numOfRows) {
×
1565
    *rows = pResInfo->numOfRows;
×
1566
  }
1567
  if (IS_VAR_DATA_TYPE(pField->type)) {
×
1568
    for (int i = 0; i < *rows; i++) {
×
1569
      if (pCol->offset[i] == -1) {
×
1570
        result[i] = true;
×
1571
      } else {
1572
        result[i] = false;
×
1573
      }
1574
    }
1575
  } else {
1576
    for (int i = 0; i < *rows; i++) {
×
1577
      if (colDataIsNull_f(pCol, i)) {
×
1578
        result[i] = true;
×
1579
      } else {
1580
        result[i] = false;
×
1581
      }
1582
    }
1583
  }
1584
  return 0;
×
1585
}
1586

1587
int taos_validate_sql(TAOS *taos, const char *sql) {
×
1588
  TAOS_RES *pObj = taosQueryImpl(taos, sql, true, TD_REQ_FROM_APP);
×
1589

1590
  int code = taos_errno(pObj);
×
1591

1592
  taos_free_result(pObj);
×
1593
  return code;
×
1594
}
1595

1596
void taos_reset_current_db(TAOS *taos) {
×
1597
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
1598
  if (pTscObj == NULL) {
×
1599
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1600
    return;
×
1601
  }
1602

1603
  resetConnectDB(pTscObj);
×
1604

1605
  releaseTscObj(*(int64_t *)taos);
×
1606
}
1607

1608
const char *taos_get_server_info(TAOS *taos) {
×
1609
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
1610
  if (pTscObj == NULL) {
×
1611
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1612
    return NULL;
×
1613
  }
1614

1615
  releaseTscObj(*(int64_t *)taos);
×
1616

1617
  return pTscObj->sDetailVer;
×
1618
}
1619

1620
int taos_get_current_db(TAOS *taos, char *database, int len, int *required) {
×
1621
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
1622
  if (pTscObj == NULL) {
×
1623
    return TSDB_CODE_TSC_DISCONNECTED;
×
1624
  }
1625

1626
  int code = TSDB_CODE_SUCCESS;
×
1627
  (void)taosThreadMutexLock(&pTscObj->mutex);
×
1628
  if (database == NULL || len <= 0) {
×
1629
    if (required != NULL) *required = strlen(pTscObj->db) + 1;
×
1630
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1631
  } else if (len < strlen(pTscObj->db) + 1) {
×
1632
    tstrncpy(database, pTscObj->db, len);
×
1633
    if (required) *required = strlen(pTscObj->db) + 1;
×
1634
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1635
  } else {
1636
    tstrncpy(database, pTscObj->db, len);
×
1637
    code = 0;
×
1638
  }
1639
_return:
×
1640
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
×
1641
  releaseTscObj(*(int64_t *)taos);
×
1642
  return code;
×
1643
}
1644

1645
// buffer is allocated by caller, len is in/out parameter, input is buffer length, output is actual length.
1646
// because this is a general purpose api, buffer is not null-terminated string even for string info, and
1647
// the return length is the actual length of the info, not including null-terminator.
1648
int taos_get_connection_info(TAOS *taos, TSDB_CONNECTION_INFO info, char *buffer, int *len) {
2✔
1649
  if (len == NULL) {
2✔
1650
    return TSDB_CODE_INVALID_PARA;
×
1651
  }
1652

1653
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
2✔
1654
  if (pTscObj == NULL) {
2✔
1655
    return TSDB_CODE_TSC_DISCONNECTED;
×
1656
  }
1657

1658
  int code = TSDB_CODE_SUCCESS;
2✔
1659
  (void)taosThreadMutexLock(&pTscObj->mutex);
2✔
1660

1661
  switch (info) {
2✔
1662
    case TSDB_CONNECTION_INFO_USER: {
1✔
1663
      int userLen = strlen(pTscObj->user);
1✔
1664
      if (buffer == NULL || *len < userLen) {
1✔
1665
        *len = userLen;
×
1666
        TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1667
      } else {
1668
        *len = userLen;
1✔
1669
        (void)memcpy(buffer, pTscObj->user, userLen);
1✔
1670
      }
1671
      break;
1✔
1672
    }
1673

1674
    case TSDB_CONNECTION_INFO_TOKEN: {
1✔
1675
      int tokenLen = strlen(pTscObj->tokenName);
1✔
1676
      if (tokenLen == 0) {
1✔
1677
        *len = 0;
×
1678
      } else if (buffer == NULL || *len < tokenLen) {
1✔
1679
        *len = tokenLen;
×
1680
        TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1681
      } else {
1682
        *len = tokenLen;
1✔
1683
        (void)memcpy(buffer, pTscObj->tokenName, tokenLen);
1✔
1684
      }
1685
      break;
1✔
1686
    }
1687

1688
    default:
×
1689
      TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1690
  }
1691

1692
_return:
×
1693
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
2✔
1694
  releaseTscObj(*(int64_t *)taos);
2✔
1695
  return code;
2✔
1696
}
1697

1698
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
6,038✔
1699
  if (NULL == pWrapper) {
6,038✔
1700
    return;
3,454✔
1701
  }
1702
  destoryCatalogReq(pWrapper->pCatalogReq);
2,584✔
1703
  taosMemoryFree(pWrapper->pCatalogReq);
2,584✔
1704
  qDestroyParseContext(pWrapper->pParseCtx);
2,584✔
1705
  taosMemoryFree(pWrapper);
2,584✔
1706
}
1707

1708
void destroyCtxInRequest(SRequestObj *pRequest) {
3✔
1709
  schedulerFreeJob(&pRequest->body.queryJob, 0);
3✔
1710
  qDestroyQuery(pRequest->pQuery);
3✔
1711
  pRequest->pQuery = NULL;
3✔
1712
  destorySqlCallbackWrapper(pRequest->pWrapper);
3✔
1713
  pRequest->pWrapper = NULL;
3✔
1714
}
3✔
1715

1716
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
1,820✔
1717
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
1,820✔
1718
  SRequestObj         *pRequest = pWrapper->pRequest;
1,820✔
1719
  SQuery              *pQuery = pRequest->pQuery;
1,820✔
1720

1721
  qDebug("req:0x%" PRIx64 ", start to semantic analysis, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
1,820✔
1722

1723
  int64_t analyseStart = taosGetTimestampUs();
1,820✔
1724
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
1,820✔
1725
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
1,820✔
1726

1727
  if (TSDB_CODE_SUCCESS == code) {
1,820✔
1728
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
1,820✔
1729
  }
1730

1731
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
1,820✔
1732

1733
  if (pRequest->parseOnly) {
1,820✔
1734
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
×
1735
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
×
1736
  }
1737

1738
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
1,820✔
1739
}
1,820✔
1740

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

1768
    *ppTarget = pTarget;
×
1769
  }
1770

1771
  return code;
×
1772
}
1773

1774
void handleSubQueryFromAnalyse(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, SNode *pRoot) {
×
1775
  SRequestObj         *pNewRequest = NULL;
×
1776
  SSqlCallbackWrapper *pNewWrapper = NULL;
×
1777
  int32_t              code = buildPreviousRequest(pWrapper->pRequest, pWrapper->pRequest->sqlstr, &pNewRequest);
×
1778
  if (code) {
×
1779
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1780
    return;
×
1781
  }
1782

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

1805
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
1,820✔
1806
  SRequestObj *pRequest = pWrapper->pRequest;
1,820✔
1807
  SQuery      *pQuery = pRequest->pQuery;
1,820✔
1808

1809
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
1,820✔
1810
    SNode *prevRoot = pQuery->pPrevRoot;
×
1811
    pQuery->pPrevRoot = NULL;
×
1812
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
×
1813
    return;
×
1814
  }
1815

1816
  if (code == TSDB_CODE_SUCCESS) {
1,820✔
1817
    pRequest->stableQuery = pQuery->stableQuery;
1,807✔
1818
    if (pQuery->pRoot) {
1,807✔
1819
      pRequest->stmtType = pQuery->pRoot->type;
1,807✔
1820
    }
1821

1822
    if (pQuery->haveResultSet) {
1,807✔
1823
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
595✔
1824
                              pRequest->stmtBindVersion > 0);
595✔
1825
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
595✔
1826
    }
1827
  }
1828

1829
  if (code == TSDB_CODE_SUCCESS) {
1,820✔
1830
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
1,807✔
1831
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
1,807✔
1832
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
1,807✔
1833

1834
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
1,807✔
1835
  } else {
1836
    destorySqlCallbackWrapper(pWrapper);
13✔
1837
    pRequest->pWrapper = NULL;
13✔
1838
    qDestroyQuery(pRequest->pQuery);
13✔
1839
    pRequest->pQuery = NULL;
13✔
1840

1841
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
13✔
1842
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
3✔
1843
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1844
      restartAsyncQuery(pRequest, code);
3✔
1845
      return;
3✔
1846
    }
1847

1848
    // return to app directly
1849
    tscError("req:0x%" PRIx64 ", error occurs, code:%s, return to user app, QID:0x%" PRIx64, pRequest->self,
10✔
1850
             tstrerror(code), pRequest->requestId);
1851
    pRequest->code = code;
10✔
1852
    returnToUser(pRequest);
10✔
1853
  }
1854
}
1855

1856
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
1,904✔
1857
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
1,904✔
1858
                           .requestId = pWrapper->pParseCtx->requestId,
1,904✔
1859
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
1,904✔
1860
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
1,904✔
1861

1862
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
1,904✔
1863

1864
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
3,808✔
1865
                                &pWrapper->pRequest->body.queryJob);
1,904✔
1866
}
1867

1868
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code);
1869

1870
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
2,524✔
1871
  int32_t code = TSDB_CODE_SUCCESS;
2,524✔
1872
  switch (pWrapper->pRequest->pQuery->execStage) {
2,524✔
1873
    case QUERY_EXEC_STAGE_PARSE: {
84✔
1874
      // continue parse after get metadata
1875
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
84✔
1876
      break;
84✔
1877
    }
1878
    case QUERY_EXEC_STAGE_ANALYSE: {
1,820✔
1879
      // analysis after get metadata
1880
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
1,820✔
1881
      break;
1,820✔
1882
    }
1883
    case QUERY_EXEC_STAGE_SCHEDULE: {
620✔
1884
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
620✔
1885
      break;
620✔
1886
    }
1887
    default:
×
1888
      break;
×
1889
  }
1890
  return code;
2,524✔
1891
}
1892

1893
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code) {
84✔
1894
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
84✔
1895
  SRequestObj         *pRequest = pWrapper->pRequest;
84✔
1896
  SQuery              *pQuery = pRequest->pQuery;
84✔
1897

1898
  pRequest->metric.ctgCostUs += taosGetTimestampUs() - pRequest->metric.ctgStart;
84✔
1899
  qDebug("req:0x%" PRIx64 ", continue parse query, QID:0x%" PRIx64 ", code:%s", pRequest->self, pRequest->requestId,
84✔
1900
         tstrerror(code));
1901

1902
  if (code == TSDB_CODE_SUCCESS) {
84✔
1903
    // pWrapper->pCatalogReq->forceUpdate = false;
1904
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
84✔
1905
  }
1906

1907
  if (TSDB_CODE_SUCCESS == code) {
84✔
1908
    code = phaseAsyncQuery(pWrapper);
78✔
1909
  }
1910

1911
  if (TSDB_CODE_SUCCESS != code) {
84✔
1912
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
6✔
1913
             tstrerror(code), pWrapper->pRequest->requestId);
1914
    destorySqlCallbackWrapper(pWrapper);
6✔
1915
    pRequest->pWrapper = NULL;
6✔
1916
    terrno = code;
6✔
1917
    pRequest->code = code;
6✔
1918
    doRequestCallback(pRequest, code);
6✔
1919
  }
1920
}
84✔
1921

1922
void continueInsertFromCsv(SSqlCallbackWrapper *pWrapper, SRequestObj *pRequest) {
×
1923
  int32_t code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
×
1924
  if (TSDB_CODE_SUCCESS == code) {
×
1925
    code = phaseAsyncQuery(pWrapper);
×
1926
  }
1927

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

1939
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
5✔
1940
  int64_t connId = *(int64_t *)taos;
5✔
1941
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
5✔
1942
}
5✔
1943

1944
void taos_query_a_with_reqid(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param, int64_t reqid) {
×
1945
  int64_t connId = *(int64_t *)taos;
×
1946
  taosAsyncQueryImplWithReqid(connId, sql, fp, param, false, reqid);
×
1947
}
×
1948

1949
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
2,534✔
1950
  const STscObj *pTscObj = pRequest->pTscObj;
2,534✔
1951

1952
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
2,534✔
1953
  if (*pCxt == NULL) {
2,534✔
1954
    return terrno;
×
1955
  }
1956

1957
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
×
1958
                           .requestRid = pRequest->self,
2,534✔
1959
                           .acctId = pTscObj->acctId,
2,534✔
1960
                           .db = pRequest->pDb,
2,534✔
1961
                           .topicQuery = false,
1962
                           .pSql = pRequest->sqlstr,
2,534✔
1963
                           .sqlLen = pRequest->sqlLen,
2,534✔
1964
                           .pMsg = pRequest->msgBuf,
2,534✔
1965
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1966
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
2,534✔
1967
                           .pStmtCb = NULL,
1968
                           .pUser = pTscObj->user,
2,534✔
1969
                           .userId = pTscObj->userId,
2,534✔
1970
                           .pEffectiveUser = pRequest->effectiveUser,
2,534✔
1971
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
2,534✔
1972
                           .enableSysInfo = pTscObj->sysInfo,
2,534✔
1973
                           .privInfo = pWrapper->pParseCtx ? pWrapper->pParseCtx->privInfo : 0,
2,534✔
1974
                           .async = true,
1975
                           .svrVer = pTscObj->sVer,
2,534✔
1976
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
2,534✔
1977
                           .allocatorId = pRequest->allocatorRefId,
2,534✔
1978
                           .parseSqlFp = clientParseSql,
1979
                           .parseSqlParam = pWrapper,
1980
                           .setQueryFp = setQueryRequest,
1981
                           .timezone = pTscObj->optionInfo.timezone,
2,534✔
1982
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
2,534✔
1983
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
2,534✔
1984
  (*pCxt)->biMode = biMode;
2,534✔
1985
  return TSDB_CODE_SUCCESS;
2,534✔
1986
}
1987

1988
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
2,534✔
1989
  int32_t              code = TSDB_CODE_SUCCESS;
2,534✔
1990
  STscObj             *pTscObj = pRequest->pTscObj;
2,534✔
1991
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
2,534✔
1992
  if (pWrapper == NULL) {
2,534✔
1993
    code = terrno;
×
1994
  } else {
1995
    pWrapper->pRequest = pRequest;
2,534✔
1996
    pRequest->pWrapper = pWrapper;
2,534✔
1997
    *ppWrapper = pWrapper;
2,534✔
1998
  }
1999

2000
  if (TSDB_CODE_SUCCESS == code) {
2,534✔
2001
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
2,534✔
2002
  }
2003

2004
  if (TSDB_CODE_SUCCESS == code) {
2,534✔
2005
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
2,534✔
2006
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
2,534✔
2007
  }
2008

2009
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
2,534✔
2010
    int64_t syntaxStart = taosGetTimestampUs();
2,534✔
2011

2012
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
2,534✔
2013
    if (pWrapper->pCatalogReq == NULL) {
2,534✔
2014
      code = terrno;
×
2015
    } else {
2016
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
2,534✔
2017
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
2,534✔
2018
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
2,534✔
2019
    }
2020

2021
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
2,534✔
2022
  }
2023

2024
  return code;
2,534✔
2025
}
2026

2027
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
2,535✔
2028
  SSqlCallbackWrapper *pWrapper = NULL;
2,535✔
2029
  int32_t              code = TSDB_CODE_SUCCESS;
2,535✔
2030

2031
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
2,535✔
2032
    code = pRequest->prevCode;
1✔
2033
    terrno = code;
1✔
2034
    pRequest->code = code;
1✔
2035
    tscDebug("req:0x%" PRIx64 ", call sync query cb with code:%s", pRequest->self, tstrerror(code));
1✔
2036
    doRequestCallback(pRequest, code);
1✔
2037
    return;
1✔
2038
  }
2039

2040
  if (TSDB_CODE_SUCCESS == code) {
2,534✔
2041
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
2,534✔
2042
  }
2043

2044
  if (TSDB_CODE_SUCCESS == code) {
2,534✔
2045
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
2,446✔
2046
    code = phaseAsyncQuery(pWrapper);
2,446✔
2047
  }
2048

2049
  if (TSDB_CODE_SUCCESS != code) {
2,534✔
2050
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
88✔
2051
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
88✔
2052
               pRequest->requestId);
2053
    } else {
2054
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
×
2055
               pRequest->requestId);
2056
    }
2057

2058
    destorySqlCallbackWrapper(pWrapper);
88✔
2059
    pRequest->pWrapper = NULL;
88✔
2060
    qDestroyQuery(pRequest->pQuery);
88✔
2061
    pRequest->pQuery = NULL;
88✔
2062

2063
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
88✔
2064
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
×
2065
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
2066
      code = refreshMeta(pRequest->pTscObj, pRequest);
×
2067
      if (code != 0) {
×
2068
        tscWarn("req:0x%" PRIx64 ", refresh meta failed, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code,
×
2069
                tstrerror(code), pRequest->requestId);
2070
      }
2071
      pRequest->prevCode = code;
×
2072
      doAsyncQuery(pRequest, true);
×
2073
      return;
×
2074
    }
2075

2076
    terrno = code;
88✔
2077
    pRequest->code = code;
88✔
2078
    doRequestCallback(pRequest, code);
88✔
2079
  }
2080
}
2081

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

2114
typedef struct SAsyncFetchParam {
2115
  SRequestObj      *pReq;
2116
  __taos_async_fn_t fp;
2117
  void             *param;
2118
} SAsyncFetchParam;
2119

2120
static int32_t doAsyncFetch(void *pParam) {
412✔
2121
  SAsyncFetchParam *param = pParam;
412✔
2122
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
412✔
2123
  taosMemoryFree(param);
412✔
2124
  return TSDB_CODE_SUCCESS;
412✔
2125
}
2126

2127
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
412✔
2128
  if (res == NULL || fp == NULL) {
412✔
2129
    tscError("taos_fetch_rows_a invalid paras");
×
2130
    return;
×
2131
  }
2132
  if (!TD_RES_QUERY(res)) {
412✔
2133
    tscError("taos_fetch_rows_a res is NULL");
×
2134
    fp(param, res, TSDB_CODE_APP_ERROR);
×
2135
    return;
×
2136
  }
2137

2138
  SRequestObj *pRequest = res;
412✔
2139
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
412✔
2140
    fp(param, res, 0);
×
2141
    return;
×
2142
  }
2143

2144
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
412✔
2145
  if (!pParam) {
412✔
2146
    fp(param, res, terrno);
×
2147
    return;
×
2148
  }
2149
  pParam->pReq = pRequest;
412✔
2150
  pParam->fp = fp;
412✔
2151
  pParam->param = param;
412✔
2152
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
412✔
2153
  if (TSDB_CODE_SUCCESS != code) {
412✔
2154
    taosMemoryFree(pParam);
×
2155
    fp(param, res, code);
×
2156
    return;
×
2157
  }
2158
}
2159

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

2172
  // set the current block is all consumed
2173
  pResultInfo->convertUcs4 = false;
4✔
2174

2175
  // it is a local executed query, no need to do async fetch
2176
  taos_fetch_rows_a(pRequest, fp, param);
4✔
2177
}
2178

2179
const void *taos_get_raw_block(TAOS_RES *res) {
×
2180
  if (res == NULL) {
×
2181
    tscError("taos_get_raw_block invalid paras");
×
2182
    return NULL;
×
2183
  }
2184
  if (!TD_RES_QUERY(res)) {
×
2185
    tscError("taos_get_raw_block res is NULL");
×
2186
    return NULL;
×
2187
  }
2188
  SRequestObj *pRequest = res;
×
2189

2190
  return pRequest->body.resInfo.pData;
×
2191
}
2192

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

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

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

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

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

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

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

2229
  code = catalogGetDBVgInfo(pCtg, &conn, dbFName, dbInfo);
×
2230
  if (code) {
×
2231
    goto _return;
×
2232
  }
2233

2234
_return:
×
2235

2236
  terrno = code;
×
2237

2238
  destroyRequest(pRequest);
×
2239
  return code;
×
2240
}
2241

2242
int taos_get_table_vgId(TAOS *taos, const char *db, const char *table, int *vgId) {
×
2243
  if (NULL == taos) {
×
2244
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2245
    return terrno;
×
2246
  }
2247

2248
  if (NULL == db || NULL == table || NULL == vgId) {
×
2249
    tscError("invalid input param, db:%p, table:%p, vgId:%p", db, table, vgId);
×
2250
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2251
    return terrno;
×
2252
  }
2253

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

2262
  pRequest->syncQuery = true;
×
2263

2264
  STscObj  *pTscObj = pRequest->pTscObj;
×
2265
  SCatalog *pCtg = NULL;
×
2266
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2267
  if (code != TSDB_CODE_SUCCESS) {
×
2268
    goto _return;
×
2269
  }
2270

2271
  SRequestConnInfo conn = {
×
2272
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2273

2274
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2275

2276
  SName tableName = {0};
×
2277
  toName(pTscObj->acctId, db, table, &tableName);
×
2278

2279
  SVgroupInfo vgInfo;
2280
  code = catalogGetTableHashVgroup(pCtg, &conn, &tableName, &vgInfo);
×
2281
  if (code) {
×
2282
    goto _return;
×
2283
  }
2284

2285
  *vgId = vgInfo.vgId;
×
2286

2287
_return:
×
2288

2289
  terrno = code;
×
2290

2291
  destroyRequest(pRequest);
×
2292
  return code;
×
2293
}
2294

2295
int taos_get_tables_vgId(TAOS *taos, const char *db, const char *table[], int tableNum, int *vgId) {
×
2296
  if (NULL == taos) {
×
2297
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2298
    return terrno;
×
2299
  }
2300

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

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

2315
  pRequest->syncQuery = true;
×
2316

2317
  STscObj  *pTscObj = pRequest->pTscObj;
×
2318
  SCatalog *pCtg = NULL;
×
2319
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2320
  if (code != TSDB_CODE_SUCCESS) {
×
2321
    goto _return;
×
2322
  }
2323

2324
  SRequestConnInfo conn = {
×
2325
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2326

2327
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2328

2329
  code = catalogGetTablesHashVgId(pCtg, &conn, pTscObj->acctId, db, table, tableNum, vgId);
×
2330
  if (code) {
×
2331
    goto _return;
×
2332
  }
2333

2334
_return:
×
2335

2336
  terrno = code;
×
2337

2338
  destroyRequest(pRequest);
×
2339
  return code;
×
2340
}
2341

2342
int taos_load_table_info(TAOS *taos, const char *tableNameList) {
×
2343
  if (NULL == taos) {
×
2344
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2345
    return terrno;
×
2346
  }
2347

2348
  int64_t       connId = *(int64_t *)taos;
×
2349
  const int32_t MAX_TABLE_NAME_LENGTH = 12 * 1024 * 1024;  // 12MB list
×
2350
  int32_t       code = 0;
×
2351
  SRequestObj  *pRequest = NULL;
×
2352
  SCatalogReq   catalogReq = {0};
×
2353

2354
  if (NULL == tableNameList) {
×
2355
    return TSDB_CODE_SUCCESS;
×
2356
  }
2357

2358
  int32_t length = (int32_t)strlen(tableNameList);
×
2359
  if (0 == length) {
×
2360
    return TSDB_CODE_SUCCESS;
×
2361
  } else if (length > MAX_TABLE_NAME_LENGTH) {
×
2362
    tscError("tableNameList too long, length:%d, maximum allowed:%d", length, MAX_TABLE_NAME_LENGTH);
×
2363
    return TSDB_CODE_TSC_INVALID_OPERATION;
×
2364
  }
2365

2366
  char *sql = "taos_load_table_info";
×
2367
  code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2368
  if (code != TSDB_CODE_SUCCESS) {
×
2369
    terrno = code;
×
2370
    goto _return;
×
2371
  }
2372

2373
  pRequest->syncQuery = true;
×
2374

2375
  STscObj *pTscObj = pRequest->pTscObj;
×
2376
  code = transferTableNameList(tableNameList, pTscObj->acctId, pTscObj->db, &catalogReq.pTableMeta);
×
2377
  if (code) {
×
2378
    goto _return;
×
2379
  }
2380

2381
  SCatalog *pCtg = NULL;
×
2382
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2383
  if (code != TSDB_CODE_SUCCESS) {
×
2384
    goto _return;
×
2385
  }
2386

2387
  SRequestConnInfo conn = {
×
2388
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2389

2390
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2391

2392
  code = catalogAsyncGetAllMeta(pCtg, &conn, &catalogReq, syncCatalogFn, pRequest->body.interParam, NULL);
×
2393
  if (code) {
×
2394
    goto _return;
×
2395
  }
2396

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

2409
TAOS_STMT *taos_stmt_init(TAOS *taos) {
59✔
2410
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
59✔
2411
  if (NULL == pObj) {
59✔
2412
    tscError("invalid parameter for %s", __FUNCTION__);
×
2413
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2414
    return NULL;
×
2415
  }
2416

2417
  TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL);
59✔
2418
  if (NULL == pStmt) {
59✔
2419
    tscError("stmt init failed, errcode:%s", terrstr());
×
2420
  }
2421
  releaseTscObj(*(int64_t *)taos);
59✔
2422

2423
  return pStmt;
59✔
2424
}
2425

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

2434
  TAOS_STMT *pStmt = stmtInit(pObj, reqid, NULL);
×
2435
  if (NULL == pStmt) {
×
2436
    tscError("stmt init failed, errcode:%s", terrstr());
×
2437
  }
2438
  releaseTscObj(*(int64_t *)taos);
×
2439

2440
  return pStmt;
×
2441
}
2442

2443
TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options) {
3✔
2444
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
3✔
2445
  if (NULL == pObj) {
3✔
2446
    tscError("invalid parameter for %s", __FUNCTION__);
×
2447
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2448
    return NULL;
×
2449
  }
2450

2451
  TAOS_STMT *pStmt = stmtInit(pObj, options->reqId, options);
3✔
2452
  if (NULL == pStmt) {
3✔
2453
    tscError("stmt init failed, errcode:%s", terrstr());
×
2454
  }
2455
  releaseTscObj(*(int64_t *)taos);
3✔
2456

2457
  return pStmt;
3✔
2458
}
2459

2460
int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
74✔
2461
  if (stmt == NULL || sql == NULL) {
74✔
2462
    tscError("NULL parameter for %s", __FUNCTION__);
×
2463
    terrno = TSDB_CODE_INVALID_PARA;
×
2464
    return terrno;
×
2465
  }
2466

2467
  return stmtPrepare(stmt, sql, length);
74✔
2468
}
2469

2470
int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags) {
41✔
2471
  if (stmt == NULL || name == NULL) {
41✔
2472
    tscError("NULL parameter for %s", __FUNCTION__);
×
2473
    terrno = TSDB_CODE_INVALID_PARA;
×
2474
    return terrno;
×
2475
  }
2476

2477
  int32_t code = stmtSetTbName(stmt, name);
41✔
2478
  if (code) {
41✔
2479
    return code;
×
2480
  }
2481

2482
  if (tags) {
41✔
2483
    return stmtSetTbTags(stmt, tags);
41✔
2484
  }
2485

2486
  return TSDB_CODE_SUCCESS;
×
2487
}
2488

2489
int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name) {
69✔
2490
  if (stmt == NULL || name == NULL) {
69✔
2491
    tscError("NULL parameter for %s", __FUNCTION__);
×
2492
    terrno = TSDB_CODE_INVALID_PARA;
×
2493
    return terrno;
×
2494
  }
2495

2496
  return stmtSetTbName(stmt, name);
69✔
2497
}
2498

2499
int taos_stmt_set_tags(TAOS_STMT *stmt, TAOS_MULTI_BIND *tags) {
12✔
2500
  if (stmt == NULL || tags == NULL) {
12✔
2501
    tscError("NULL parameter for %s", __FUNCTION__);
×
2502
    terrno = TSDB_CODE_INVALID_PARA;
×
2503
    return terrno;
×
2504
  }
2505

2506
  return stmtSetTbTags(stmt, tags);
12✔
2507
}
2508

2509
int taos_stmt_set_sub_tbname(TAOS_STMT *stmt, const char *name) { return taos_stmt_set_tbname(stmt, name); }
6✔
2510

2511
int taos_stmt_get_tag_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
12✔
2512
  if (stmt == NULL || NULL == fieldNum) {
12✔
2513
    tscError("NULL parameter for %s", __FUNCTION__);
×
2514
    terrno = TSDB_CODE_INVALID_PARA;
×
2515
    return terrno;
×
2516
  }
2517

2518
  return stmtGetTagFields(stmt, fieldNum, fields);
12✔
2519
}
2520

2521
int taos_stmt_get_col_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
18✔
2522
  if (stmt == NULL || NULL == fieldNum) {
18✔
2523
    tscError("NULL parameter for %s", __FUNCTION__);
×
2524
    terrno = TSDB_CODE_INVALID_PARA;
×
2525
    return terrno;
×
2526
  }
2527

2528
  return stmtGetColFields(stmt, fieldNum, fields);
18✔
2529
}
2530

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

2538
int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
449✔
2539
  if (stmt == NULL || bind == NULL) {
449✔
2540
    tscError("NULL parameter for %s", __FUNCTION__);
×
2541
    terrno = TSDB_CODE_INVALID_PARA;
×
2542
    return terrno;
×
2543
  }
2544

2545
  if (bind->num > 1) {
449✔
2546
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
×
2547
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2548
    return terrno;
×
2549
  }
2550

2551
  return stmtBindBatch(stmt, bind, -1);
449✔
2552
}
2553

2554
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
27✔
2555
  if (stmt == NULL || bind == NULL) {
27✔
2556
    tscError("NULL parameter for %s", __FUNCTION__);
×
2557
    terrno = TSDB_CODE_INVALID_PARA;
×
2558
    return terrno;
×
2559
  }
2560

2561
  if (bind->num <= 0 || bind->num > INT16_MAX) {
27✔
2562
    tscError("invalid bind num %d", bind->num);
×
2563
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2564
    return terrno;
×
2565
  }
2566

2567
  int32_t insert = 0;
27✔
2568
  int32_t code = stmtIsInsert(stmt, &insert);
27✔
2569
  if (TSDB_CODE_SUCCESS != code) {
27✔
2570
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2571
    return code;
×
2572
  }
2573
  if (0 == insert && bind->num > 1) {
27✔
2574
    tscError("only one row data allowed for query");
×
2575
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2576
    return terrno;
×
2577
  }
2578

2579
  return stmtBindBatch(stmt, bind, -1);
27✔
2580
}
2581

2582
int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind, int colIdx) {
×
2583
  if (stmt == NULL || bind == NULL) {
×
2584
    tscError("NULL parameter for %s", __FUNCTION__);
×
2585
    terrno = TSDB_CODE_INVALID_PARA;
×
2586
    return terrno;
×
2587
  }
2588

2589
  if (colIdx < 0) {
×
2590
    tscError("invalid bind column idx %d", colIdx);
×
2591
    terrno = TSDB_CODE_INVALID_PARA;
×
2592
    return terrno;
×
2593
  }
2594

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

2607
  return stmtBindBatch(stmt, bind, colIdx);
×
2608
}
2609

2610
int taos_stmt_add_batch(TAOS_STMT *stmt) {
125✔
2611
  if (stmt == NULL) {
125✔
2612
    tscError("NULL parameter for %s", __FUNCTION__);
×
2613
    terrno = TSDB_CODE_INVALID_PARA;
×
2614
    return terrno;
×
2615
  }
2616

2617
  return stmtAddBatch(stmt);
125✔
2618
}
2619

2620
int taos_stmt_execute(TAOS_STMT *stmt) {
116✔
2621
  if (stmt == NULL) {
116✔
2622
    tscError("NULL parameter for %s", __FUNCTION__);
×
2623
    terrno = TSDB_CODE_INVALID_PARA;
×
2624
    return terrno;
×
2625
  }
2626

2627
  return stmtExec(stmt);
116✔
2628
}
2629

2630
int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
×
2631
  if (stmt == NULL || insert == NULL) {
×
2632
    tscError("NULL parameter for %s", __FUNCTION__);
×
2633
    terrno = TSDB_CODE_INVALID_PARA;
×
2634
    return terrno;
×
2635
  }
2636

2637
  return stmtIsInsert(stmt, insert);
×
2638
}
2639

2640
int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) {
×
2641
  if (stmt == NULL || nums == NULL) {
×
2642
    tscError("NULL parameter for %s", __FUNCTION__);
×
2643
    terrno = TSDB_CODE_INVALID_PARA;
×
2644
    return terrno;
×
2645
  }
2646

2647
  return stmtGetParamNum(stmt, nums);
×
2648
}
2649

2650
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
12✔
2651
  if (stmt == NULL || type == NULL || NULL == bytes || idx < 0) {
12✔
2652
    tscError("invalid parameter for %s", __FUNCTION__);
×
2653
    terrno = TSDB_CODE_INVALID_PARA;
×
2654
    return terrno;
×
2655
  }
2656

2657
  return stmtGetParam(stmt, idx, type, bytes);
12✔
2658
}
2659

2660
TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt) {
×
2661
  if (stmt == NULL) {
×
2662
    tscError("NULL parameter for %s", __FUNCTION__);
×
2663
    terrno = TSDB_CODE_INVALID_PARA;
×
2664
    return NULL;
×
2665
  }
2666

2667
  return stmtUseResult(stmt);
×
2668
}
2669

2670
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
24✔
2671

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

2679
  return stmtAffectedRows(stmt);
×
2680
}
2681

2682
int taos_stmt_affected_rows_once(TAOS_STMT *stmt) {
104✔
2683
  if (stmt == NULL) {
104✔
2684
    tscError("NULL parameter for %s", __FUNCTION__);
×
2685
    terrno = TSDB_CODE_INVALID_PARA;
×
2686
    return 0;
×
2687
  }
2688

2689
  return stmtAffectedRowsOnce(stmt);
104✔
2690
}
2691

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

2699
  return stmtClose(stmt);
62✔
2700
}
2701

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

2715
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
285✔
2716

2717
  releaseTscObj(*(int64_t *)taos);
285✔
2718

2719
  return pStmt;
285✔
2720
}
2721

2722
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
351✔
2723
  if (stmt == NULL || sql == NULL) {
351✔
2724
    tscError("NULL parameter for %s", __FUNCTION__);
2✔
2725
    terrno = TSDB_CODE_INVALID_PARA;
2✔
2726
    return terrno;
2✔
2727
  }
2728

2729
  return stmtPrepare2(stmt, sql, length);
349✔
2730
}
2731

2732
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
411✔
2733
  if (stmt == NULL) {
411✔
2734
    tscError("NULL parameter for %s", __FUNCTION__);
×
2735
    terrno = TSDB_CODE_INVALID_PARA;
×
2736
    return terrno;
×
2737
  }
2738

2739
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
411✔
2740
  int32_t    code = TSDB_CODE_SUCCESS;
411✔
2741
  STMT2_DLOG_E("start to bind param");
411✔
2742

2743
  // check query bind number
2744
  bool isQuery = (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt)));
411✔
2745
  if (isQuery) {
411✔
2746
    if (bindv->count != 1 || bindv->bind_cols[0]->num != 1) {
77✔
2747
      terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2748
      STMT2_ELOG_E("query only support one table and one row bind");
×
2749
      return terrno;
×
2750
    }
2751
  }
2752

2753
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 1) {
411✔
2754
    STMT2_ELOG_E("async bind param is still working, please try again later");
×
2755
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
×
2756
    return terrno;
×
2757
  }
2758

2759
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
411✔
2760
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
46✔
2761
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
2762
    }
2763
    pStmt->execSemWaited = true;
46✔
2764
  }
2765

2766
  for (int i = 0; i < bindv->count; ++i) {
1,092✔
2767
    SVCreateTbReq *pCreateTbReq = NULL;
717✔
2768
    if (!isQuery) {
717✔
2769
      STMT2_TLOG("start to bind %dth table", i);
640✔
2770
      if (bindv->tbnames && bindv->tbnames[i]) {
640✔
2771
        code = stmtSetTbName2(stmt, bindv->tbnames[i]);
606✔
2772
        if (code) {
606✔
2773
          terrno = code;
22✔
2774
          STMT2_ELOG("set tbname failed, code:%s", stmt2Errstr(stmt));
22✔
2775
          return terrno;
36✔
2776
        }
2777
      }
2778

2779
      if (bindv->tags && bindv->tags[i]) {
618✔
2780
        code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
400✔
2781
      } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
218✔
2782
        code = stmtCheckTags2(stmt, &pCreateTbReq);
60✔
2783
      } else if (pStmt->sql.autoCreateTbl) {
158✔
2784
        code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
42✔
2785
      }
2786

2787
      if (code) {
618✔
2788
        terrno = code;
2✔
2789
        STMT2_ELOG("set tags failed, code:%s", stmt2Errstr(stmt));
2✔
2790
        if (pCreateTbReq) {
2✔
2791
          tdDestroySVCreateTbReq(pCreateTbReq);
×
2792
          taosMemoryFreeClear(pCreateTbReq);
×
2793
        }
2794
        return terrno;
2✔
2795
      }
2796
    }
2797

2798
    if (bindv->bind_cols && bindv->bind_cols[i]) {
693✔
2799
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
691✔
2800

2801
      if (bind->num <= 0 || bind->num > INT16_MAX) {
691✔
2802
        STMT2_ELOG("bind num:%d must > 0 and < INT16_MAX", bind->num);
×
2803
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2804
        if (pCreateTbReq) {
×
2805
          tdDestroySVCreateTbReq(pCreateTbReq);
×
2806
          taosMemoryFreeClear(pCreateTbReq);
×
2807
        }
2808
        return terrno;
×
2809
      }
2810

2811
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
691✔
2812
      if (TSDB_CODE_SUCCESS != code) {
691✔
2813
        terrno = code;
12✔
2814
        STMT2_ELOG("bind batch failed, code:%s", stmt2Errstr(stmt));
12✔
2815
        if (pCreateTbReq) {
12✔
2816
          tdDestroySVCreateTbReq(pCreateTbReq);
4✔
2817
          taosMemoryFreeClear(pCreateTbReq);
4✔
2818
        }
2819
        return terrno;
12✔
2820
      }
2821
    }
2822
  }
2823

2824
  return code;
375✔
2825
}
2826

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

2834
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2835

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

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

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

2863
  return code_s;
×
2864
}
2865

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

2873
  return stmtExec2(stmt, affected_rows);
335✔
2874
}
2875

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

2883
  return stmtClose2(stmt);
273✔
2884
}
2885

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

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

2903
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
182✔
2904
  STMT2_DLOG_E("start to get fields");
182✔
2905

2906
  if (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type ||
182✔
2907
      (pStmt->sql.type == 0 && stmt2IsInsert(stmt))) {
168✔
2908
    return stmtGetStbColFields2(stmt, count, fields);
142✔
2909
  }
2910
  if (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt))) {
40✔
2911
    return stmtGetParamNum2(stmt, count);
38✔
2912
  }
2913

2914
  tscError("Invalid sql for stmt %s", pStmt->sql.sqlStr);
2✔
2915
  return TSDB_CODE_PAR_SYNTAX_ERROR;
2✔
2916
}
2917

2918
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_ALL *fields) {
134✔
2919
  (void)stmt;
2920
  if (!fields) return;
134✔
2921
  taosMemoryFree(fields);
98✔
2922
}
2923

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

2931
  return stmtUseResult2(stmt);
67✔
2932
}
2933

2934
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmt2Errstr(stmt); }
64✔
2935

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

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

2961
char *getBuildInfo() { return td_buildinfo; }
×
2962

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

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

2977
  code = tscCheckConnSessionMetric(pObj);
×
2978
  TAOS_CHECK_GOTO(code, &lino, _error);
×
2979

2980
_error:
×
2981
  releaseTscObj(*(int64_t *)taos);
×
2982

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3083
  code = pRequest->code;
×
3084
  terrno = code;
×
3085

3086
_cleanup:
×
3087
  destroyRequest(pRequest);
×
3088
  return code;
×
3089
}
3090

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

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

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

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

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

3132
  int32_t code = taos_init();
56✔
3133
  if (code != TSDB_CODE_SUCCESS) {
56✔
3134
    return code;
×
3135
  }
3136

3137
  SConfig *pCfg = taosGetCfg();
56✔
3138
  if (pCfg == NULL) {
56✔
3139
    return terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3140
  }
3141

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

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

3153
  void    *clientRpc = NULL;
56✔
3154
  SEpSet   epSet = {.inUse = 0, .numOfEps = 1};
56✔
3155
  SRpcMsg  rpcMsg = {0};
56✔
3156
  SRpcMsg  rpcRsp = {0};
56✔
3157
  SRpcInit rpcInit = {0};
56✔
3158

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

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

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

3184
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
56✔
3185
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
56✔
3186
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
56✔
3187
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
56✔
3188
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
56✔
3189

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

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

3203
  // Prepare epSet
3204
  tstrncpy(epSet.eps[0].fqdn, firstEp.fqdn, TSDB_FQDN_LEN);
56✔
3205
  epSet.eps[0].port = firstEp.port;
56✔
3206

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

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

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

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

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

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

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

3261
  if (rpcRsp.pCont != NULL) {
56✔
3262
    rpcFreeCont(rpcRsp.pCont);
56✔
3263
  }
3264
  rpcClose(clientRpc);
56✔
3265

3266
  terrno = code;
56✔
3267
  return code;
56✔
3268
}
3269

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

3275
  int32_t code = taos_init();
44✔
3276
  if (code != TSDB_CODE_SUCCESS) {
44✔
3277
    terrno = code;
×
3278
    return code;
×
3279
  }
3280

3281
  SConfig *pCfg = taosGetCfg();
44✔
3282
  if (pCfg == NULL) {
44✔
3283
    terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3284
    return TSDB_CODE_CFG_NOT_FOUND;
×
3285
  }
3286

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

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

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

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

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

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

3332
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
44✔
3333
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
44✔
3334
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
44✔
3335
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
44✔
3336
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
44✔
3337

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

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

3352
  tstrncpy(epSet.eps[0].fqdn, firstEp.fqdn, TSDB_FQDN_LEN);
44✔
3353
  epSet.eps[0].port = firstEp.port;
44✔
3354
  SInstanceListReq req = {0};
44✔
3355
  if (filter_type != NULL && filter_type[0] != 0) {
44✔
3356
    tstrncpy(req.filter_type, filter_type, sizeof(req.filter_type));
28✔
3357
  }
3358

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

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

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

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

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

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

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

3441
  if (rpcRsp.pCont != NULL) {
44✔
3442
    rpcFreeCont(rpcRsp.pCont);
44✔
3443
  }
3444
  rpcClose(clientRpc);
44✔
3445

3446
  return TSDB_CODE_SUCCESS;
44✔
3447
}
3448

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

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

3462
  // Free the array itself
3463
  taosMemoryFree(*pList);
32✔
3464
  *pList = NULL;
32✔
3465
}
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