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

taosdata / TDengine / #4983

13 Mar 2026 03:38AM UTC coverage: 68.653% (+0.07%) from 68.587%
#4983

push

travis-ci

web-flow
feat/6641435300-save-audit-in-self (#34738)

434 of 584 new or added lines in 10 files covered. (74.32%)

434 existing lines in 121 files now uncovered.

212745 of 309883 relevant lines covered (68.65%)

134272959.11 hits per line

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

37.85
/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, ...) {
81,077,179✔
50
  if (arg == NULL) {
81,077,179✔
51
    return TSDB_CODE_INVALID_PARA;
×
52
  }
53
  static int32_t lock = 0;
54

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

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

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

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

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

86
void tzCleanup() {
1,330,648✔
87
  taosHashCleanup(pTimezoneMap);
1,330,648✔
88
  taosHashCleanup(pTimezoneNameMap);
1,330,648✔
89
}
1,330,648✔
90

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

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

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

128
END:
×
129
  return tz;
×
130
}
131
#endif
132

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

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

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

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

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

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

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

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

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

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

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

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

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

254
  monitorClose();
1,330,648✔
255
  tscStopCrashReport();
1,330,648✔
256

257
  hbMgrCleanUp();
1,330,648✔
258

259
  catalogDestroy();
1,330,648✔
260
  schedulerDestroy();
1,330,648✔
261

262
  fmFuncMgtDestroy();
1,330,648✔
263
  qCleanupKeywordsTable();
1,330,648✔
264

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

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

274
  id = clientConnRefPool;
1,330,648✔
275
  clientConnRefPool = -1;
1,330,648✔
276
  taosCloseRef(id);
1,330,648✔
277

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

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

287
  sessMgtDestroy();
1,330,648✔
288

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

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

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

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

320
  if (pass == NULL) {
83,039,448✔
321
    pass = TSDB_DEFAULT_PASS;
197,125✔
322
  }
323

324
  STscObj *pObj = NULL;
83,039,448✔
325
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
83,039,393✔
326
  if (TSDB_CODE_SUCCESS == code) {
83,021,030✔
327
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
83,015,988✔
328
    if (NULL == rid) {
83,014,764✔
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;
83,014,764✔
333
    return (TAOS *)rid;
83,014,764✔
334
  } else {
335
    terrno = code;
12,467✔
336
  }
337

338
  return NULL;
12,659✔
339
}
340

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

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

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

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

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

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

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

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

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

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

431
  return taos;
×
432
}
433

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

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

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

453
  switch (type) {
1,400✔
454
    case TAOS_NOTIFY_PASSVER: {
400✔
455
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
400✔
456
      pObj->passInfo.fp = fp;
400✔
457
      pObj->passInfo.param = param;
400✔
458
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
400✔
459
      break;
400✔
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: {
1,000✔
469
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
1,000✔
470
      pObj->userDroppedInfo.fp = fp;
1,000✔
471
      pObj->userDroppedInfo.param = param;
1,000✔
472
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
1,000✔
473
      break;
1,000✔
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);
1,400✔
497
  return 0;
1,400✔
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) {
83,172,757✔
930
  if (taos == NULL) {
83,172,757✔
931
    return;
322✔
932
  }
933
  int32_t code = 0;
83,172,435✔
934

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

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

940
  code = tscUpdateSessMetric(pTscObj, &para);
83,172,435✔
941
  if (code != TSDB_CODE_SUCCESS) {
83,174,024✔
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);
83,174,024✔
947
  if (TSDB_CODE_SUCCESS != taosRemoveRef(clientConnRefPool, pTscObj->id)) {
83,173,253✔
948
    tscError("conn:0x%" PRIx64 ", failed to remove ref from conn pool", pTscObj->id);
×
949
  }
950
}
951

952
void taos_close(TAOS *taos) {
83,029,432✔
953
  if (taos == NULL) {
83,029,432✔
954
    return;
620✔
955
  }
956

957
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
83,028,812✔
958
  if (NULL == pObj) {
83,029,589✔
959
    taosMemoryFree(taos);
×
960
    return;
×
961
  }
962

963
  taos_close_internal(pObj);
83,029,589✔
964
  releaseTscObj(*(int64_t *)taos);
83,027,999✔
965
  taosMemoryFree(taos);
83,025,678✔
966
}
967

968
int taos_errno(TAOS_RES *res) {
1,384,890,801✔
969
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,384,890,801✔
970
    return terrno;
1,076,371✔
971
  }
972

973
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,383,820,729✔
974
    return 0;
466,152✔
975
  }
976

977
  return ((SRequestObj *)res)->code;
1,383,352,238✔
978
}
979

980
const char *taos_errstr(TAOS_RES *res) {
100,593,963✔
981
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
100,593,963✔
982
    if (*(taosGetErrMsg()) == 0) {
1,095,374✔
983
      return (const char *)tstrerror(terrno);
1,095,177✔
984
    } else {
985
      (void)snprintf(taosGetErrMsgReturn(), ERR_MSG_LEN, "%s", taosGetErrMsg());
60✔
986
      return (const char *)taosGetErrMsgReturn();
60✔
987
    }
988
  }
989

990
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
99,498,589✔
991
    return "success";
×
992
  }
993

994
  SRequestObj *pRequest = (SRequestObj *)res;
99,498,965✔
995
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
99,498,965✔
996
    return pRequest->msgBuf;
29,497,451✔
997
  } else {
998
    return (const char *)tstrerror(pRequest->code);
70,001,514✔
999
  }
1000
}
1001

1002
void taos_free_result(TAOS_RES *res) {
1,049,996,760✔
1003
  if (NULL == res) {
1,049,996,760✔
1004
    return;
13,196,031✔
1005
  }
1006

1007
  tscTrace("res:%p, will be freed", res);
1,036,800,729✔
1008

1009
  if (TD_RES_QUERY(res)) {
1,036,804,626✔
1010
    SRequestObj *pRequest = (SRequestObj *)res;
1,022,368,078✔
1011
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
1,022,368,078✔
1012
    destroyRequest(pRequest);
1,022,369,103✔
1013
    return;
1,022,351,707✔
1014
  }
1015

1016
  SMqRspObj *pRsp = (SMqRspObj *)res;
14,436,052✔
1017
  if (TD_RES_TMQ(res)) {
14,436,052✔
1018
    tDeleteMqDataRsp(&pRsp->dataRsp);
14,360,503✔
1019
    doFreeReqResultInfo(&pRsp->resInfo);
14,360,503✔
1020
  } else if (TD_RES_TMQ_METADATA(res)) {
77,462✔
1021
    tDeleteSTaosxRsp(&pRsp->dataRsp);
3,512✔
1022
    doFreeReqResultInfo(&pRsp->resInfo);
3,512✔
1023
  } else if (TD_RES_TMQ_META(res)) {
73,950✔
1024
    tDeleteMqMetaRsp(&pRsp->metaRsp);
66,873✔
1025
  } else if (TD_RES_TMQ_BATCH_META(res)) {
7,077✔
1026
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
7,077✔
1027
  } else if (TD_RES_TMQ_RAW(res)) {
×
1028
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
1029
  }
1030
  taosMemoryFree(pRsp);
14,436,165✔
1031
}
1032

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

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

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

1051
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
1052
  return pResInfo->numOfCols;
2,147,483,647✔
1053
}
1054

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

1057
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
2,147,483,647✔
1058
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1059
    return NULL;
3,491,604✔
1060
  }
1061

1062
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
1063
  return pResInfo->userFields;
2,147,483,647✔
1064
}
1065

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

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

1079
TAOS_ROW taos_fetch_row(TAOS_RES *res) {
2,147,483,647✔
1080
  if (res == NULL) {
2,147,483,647✔
1081
    return NULL;
×
1082
  }
1083

1084
  if (TD_RES_QUERY(res)) {
2,147,483,647✔
1085
    SRequestObj *pRequest = (SRequestObj *)res;
1,301,226,147✔
1086
    if (pRequest->killed) {
1,301,226,147✔
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 ||
1,301,216,678✔
1093
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
1,301,227,065✔
1094
      return NULL;
349✔
1095
    }
1096

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

1103
    return doAsyncFetchRows(pRequest, true, true);
1,301,223,214✔
1104
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,147,483,647✔
1105
    SMqRspObj      *msg = ((SMqRspObj *)res);
2,147,483,647✔
1106
    SReqResultInfo *pResultInfo = NULL;
2,147,483,647✔
1107
    if (msg->resIter == -1) {
2,147,483,647✔
1108
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
13,854,851✔
1109
        return NULL;
×
1110
      }
1111
    } else {
1112
      pResultInfo = tmqGetCurResInfo(res);
2,147,483,647✔
1113
    }
1114

1115
    if (pResultInfo->current < pResultInfo->numOfRows) {
2,147,483,647✔
1116
      doSetOneRowPtr(pResultInfo);
2,147,483,647✔
1117
      pResultInfo->current += 1;
2,147,483,647✔
1118
      return pResultInfo->row;
2,147,483,647✔
1119
    } else {
1120
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
53,682,196✔
1121
        return NULL;
13,853,411✔
1122
      }
1123

1124
      doSetOneRowPtr(pResultInfo);
39,828,065✔
1125
      pResultInfo->current += 1;
39,828,785✔
1126
      return pResultInfo->row;
39,828,785✔
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) {
2,147,483,647✔
1138
  return taos_print_row_with_size(str, INT32_MAX, row, fields, num_fields);
2,147,483,647✔
1139
}
1140
int taos_print_row_with_size(char *str, uint32_t size, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
2,147,483,647✔
1141
  int32_t len = 0;
2,147,483,647✔
1142
  for (int i = 0; i < num_fields; ++i) {
2,147,483,647✔
1143
    if (i > 0 && len < size - 1) {
2,147,483,647✔
1144
      str[len++] = ' ';
2,147,483,647✔
1145
    }
1146

1147
    if (row[i] == NULL) {
2,147,483,647✔
1148
      len += tsnprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
134,662,161✔
1149
      continue;
134,663,781✔
1150
    }
1151

1152
    switch (fields[i].type) {
2,147,483,647✔
1153
      case TSDB_DATA_TYPE_TINYINT:
11,427,886✔
1154
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
11,427,886✔
1155
        break;
11,424,960✔
1156

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

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

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

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

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

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

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

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

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

1197
      case TSDB_DATA_TYPE_VARBINARY: {
54,600✔
1198
        void    *data = NULL;
54,600✔
1199
        uint32_t tmp = 0;
54,600✔
1200
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
54,600✔
1201
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
54,600✔
1202
          break;
×
1203
        }
1204
        uint32_t copyLen = TMIN(size - len - 1, tmp);
54,600✔
1205
        (void)memcpy(str + len, data, copyLen);
54,600✔
1206
        len += copyLen;
54,600✔
1207
        taosMemoryFree(data);
54,600✔
1208
      } break;
54,600✔
1209
      case TSDB_DATA_TYPE_BINARY:
2,147,483,647✔
1210
      case TSDB_DATA_TYPE_NCHAR:
1211
      case TSDB_DATA_TYPE_GEOMETRY: {
1212
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
2,147,483,647✔
1213
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
2,147,483,647✔
1214
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
1,858,926,343✔
1215
          if (charLen > fields[i].bytes || charLen < 0) {
2,147,483,647✔
1216
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
354✔
1217
            break;
×
1218
          }
1219
        } else {
1220
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
1,858,870,423✔
UNCOV
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);
2,147,483,647✔
1227
        (void)memcpy(str + len, row[i], copyLen);
2,147,483,647✔
1228
        len += copyLen;
2,147,483,647✔
1229
      } break;
2,147,483,647✔
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:
2,147,483,647✔
1247
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
2,147,483,647✔
1248
        break;
2,147,483,647✔
1249

1250
      case TSDB_DATA_TYPE_BOOL:
54,600✔
1251
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
54,600✔
1252
        break;
54,600✔
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) {
2,147,483,647✔
1265
      break;
×
1266
    }
1267
  }
1268
  if (len < size) {
2,147,483,647✔
1269
    str[len] = 0;
2,147,483,647✔
1270
  }
1271

1272
  return len;
2,147,483,647✔
1273
}
1274

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

1280
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
1281
  return pResInfo->length;
2,147,483,647✔
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; }
889,063✔
1350

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

1358
  SRequestObj    *pRequest = (SRequestObj *)res;
702,534,026✔
1359
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
702,534,026✔
1360
  return (int)pResInfo->numOfRows;
702,533,161✔
1361
}
1362

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

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

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

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

1390
int taos_select_db(TAOS *taos, const char *db) {
42,161✔
1391
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
42,161✔
1392
  if (pObj == NULL) {
42,161✔
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) {
42,161✔
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};
42,161✔
1406
  (void)snprintf(sql, tListLen(sql), "use %s", db);
42,161✔
1407

1408
  TAOS_RES *pRequest = taos_query(taos, sql);
42,161✔
1409
  int32_t   code = taos_errno(pRequest);
41,565✔
1410

1411
  taos_free_result(pRequest);
42,161✔
1412
  releaseTscObj(*(int64_t *)taos);
41,565✔
1413
  return code;
42,161✔
1414
}
1415

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

1422
  stopAllQueries((SRequestObj *)res);
1,106,546,094✔
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; }
437,658✔
1443

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

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

1455
  if (TD_RES_QUERY(res)) {
298,663,047✔
1456
    SRequestObj *pRequest = (SRequestObj *)res;
297,745,510✔
1457

1458
    (*rows) = NULL;
297,745,510✔
1459
    (*numOfRows) = 0;
297,745,510✔
1460

1461
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
297,745,510✔
1462
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
297,399,949✔
1463
      return pRequest->code;
1,416,304✔
1464
    }
1465

1466
    (void)doAsyncFetchRows(pRequest, false, true);
296,328,190✔
1467

1468
    // TODO refactor
1469
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
296,328,190✔
1470
    pResultInfo->current = pResultInfo->numOfRows;
296,328,190✔
1471

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

1480
    pResultInfo->current = pResultInfo->numOfRows;
460,233✔
1481
    (*rows) = pResultInfo->row;
460,233✔
1482
    (*numOfRows) = pResultInfo->numOfRows;
460,233✔
1483
    return 0;
460,233✔
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) {
6,292✔
1491
  *numOfRows = 0;
6,292✔
1492
  *pData = NULL;
6,292✔
1493

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

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

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

1512
  SRequestObj *pRequest = (SRequestObj *)res;
3,924✔
1513

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

1519
  (void)doAsyncFetchRows(pRequest, false, false);
3,924✔
1520

1521
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
3,924✔
1522

1523
  pResultInfo->current = pResultInfo->numOfRows;
3,924✔
1524
  (*numOfRows) = pResultInfo->numOfRows;
3,924✔
1525
  (*pData) = (void *)pResultInfo->pData;
3,924✔
1526

1527
  return pRequest->code;
3,924✔
1528
}
1529

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

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

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

1546
  return pResInfo->pCol[columnIndex].offset;
227,264,907✔
1547
}
1548

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

1555
  int32_t numOfFields = taos_num_fields(res);
754,494,762✔
1556
  if (columnIndex >= numOfFields || numOfFields == 0) {
754,497,252✔
1557
    return TSDB_CODE_INVALID_PARA;
×
1558
  }
1559

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

1564
  if (*rows > pResInfo->numOfRows) {
754,497,252✔
1565
    *rows = pResInfo->numOfRows;
×
1566
  }
1567
  if (IS_VAR_DATA_TYPE(pField->type)) {
754,497,252✔
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++) {
2,147,483,647✔
1577
      if (colDataIsNull_f(pCol, i)) {
2,147,483,647✔
1578
        result[i] = true;
2,147,483,647✔
1579
      } else {
1580
        result[i] = false;
2,147,483,647✔
1581
      }
1582
    }
1583
  }
1584
  return 0;
754,497,252✔
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) {
4,905✔
1609
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
4,905✔
1610
  if (pTscObj == NULL) {
4,905✔
1611
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1612
    return NULL;
×
1613
  }
1614

1615
  releaseTscObj(*(int64_t *)taos);
4,905✔
1616

1617
  return pTscObj->sDetailVer;
4,905✔
1618
}
1619

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

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

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

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

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

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

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

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

1698
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
2,126,152,367✔
1699
  if (NULL == pWrapper) {
2,126,152,367✔
1700
    return;
1,108,676,880✔
1701
  }
1702
  destoryCatalogReq(pWrapper->pCatalogReq);
1,017,475,487✔
1703
  taosMemoryFree(pWrapper->pCatalogReq);
1,017,528,826✔
1704
  qDestroyParseContext(pWrapper->pParseCtx);
1,017,528,234✔
1705
  taosMemoryFree(pWrapper);
1,017,516,622✔
1706
}
1707

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

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

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

1723
  int64_t analyseStart = taosGetTimestampUs();
431,290,062✔
1724
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
431,290,062✔
1725
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
431,289,507✔
1726

1727
  if (TSDB_CODE_SUCCESS == code) {
431,290,037✔
1728
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
431,286,899✔
1729
  }
1730

1731
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
431,251,746✔
1732

1733
  if (pRequest->parseOnly) {
431,271,043✔
1734
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
295,691✔
1735
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
295,691✔
1736
  }
1737

1738
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
431,269,140✔
1739
}
431,230,460✔
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) {
431,211,921✔
1806
  SRequestObj *pRequest = pWrapper->pRequest;
431,211,921✔
1807
  SQuery      *pQuery = pRequest->pQuery;
431,237,314✔
1808

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

1816
  if (code == TSDB_CODE_SUCCESS) {
431,218,080✔
1817
    pRequest->stableQuery = pQuery->stableQuery;
358,130,202✔
1818
    if (pQuery->pRoot) {
358,131,529✔
1819
      pRequest->stmtType = pQuery->pRoot->type;
358,152,003✔
1820
    }
1821

1822
    if (pQuery->haveResultSet) {
358,108,407✔
1823
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
190,441,383✔
1824
                              pRequest->stmtBindVersion > 0);
190,440,721✔
1825
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
190,438,524✔
1826
    }
1827
  }
1828

1829
  if (code == TSDB_CODE_SUCCESS) {
431,219,309✔
1830
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
358,108,426✔
1831
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
358,102,973✔
1832
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
358,112,157✔
1833

1834
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
358,109,880✔
1835
  } else {
1836
    destorySqlCallbackWrapper(pWrapper);
73,110,883✔
1837
    pRequest->pWrapper = NULL;
73,121,059✔
1838
    qDestroyQuery(pRequest->pQuery);
73,121,059✔
1839
    pRequest->pQuery = NULL;
73,111,712✔
1840

1841
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
73,111,712✔
1842
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
2,443,833✔
1843
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1844
      restartAsyncQuery(pRequest, code);
2,443,833✔
1845
      return;
2,443,833✔
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,
70,667,879✔
1850
             tstrerror(code), pRequest->requestId);
1851
    pRequest->code = code;
70,686,983✔
1852
    returnToUser(pRequest);
70,686,983✔
1853
  }
1854
}
1855

1856
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
438,061,573✔
1857
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
707,097,540✔
1858
                           .requestId = pWrapper->pParseCtx->requestId,
438,067,860✔
1859
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
438,063,916✔
1860
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
438,069,657✔
1861

1862
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
707,105,561✔
1863

1864
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
607,128,670✔
1865
                                &pWrapper->pRequest->body.queryJob);
438,097,685✔
1866
}
1867

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

1870
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
1,010,049,684✔
1871
  int32_t code = TSDB_CODE_SUCCESS;
1,010,049,684✔
1872
  switch (pWrapper->pRequest->pQuery->execStage) {
1,010,049,684✔
1873
    case QUERY_EXEC_STAGE_PARSE: {
6,826,793✔
1874
      // continue parse after get metadata
1875
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
6,826,793✔
1876
      break;
6,826,780✔
1877
    }
1878
    case QUERY_EXEC_STAGE_ANALYSE: {
431,261,650✔
1879
      // analysis after get metadata
1880
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
431,261,650✔
1881
      break;
431,247,082✔
1882
    }
1883
    case QUERY_EXEC_STAGE_SCHEDULE: {
572,003,025✔
1884
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
572,003,025✔
1885
      break;
572,002,744✔
1886
    }
1887
    default:
×
1888
      break;
×
1889
  }
1890
  return code;
1,010,046,935✔
1891
}
1892

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

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

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

1907
  if (TSDB_CODE_SUCCESS == code) {
6,826,793✔
1908
    code = phaseAsyncQuery(pWrapper);
6,255,738✔
1909
  }
1910

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

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

1928
  if (TSDB_CODE_SUCCESS != code) {
11,918✔
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
}
11,918✔
1938

1939
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
131,049✔
1940
  int64_t connId = *(int64_t *)taos;
131,049✔
1941
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
131,049✔
1942
}
131,049✔
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) {
1,017,531,228✔
1950
  const STscObj *pTscObj = pRequest->pTscObj;
1,017,531,228✔
1951

1952
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
1,017,535,214✔
1953
  if (*pCxt == NULL) {
1,017,513,150✔
1954
    return terrno;
×
1955
  }
1956

1957
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
2,147,483,647✔
1958
                           .requestRid = pRequest->self,
1,017,516,554✔
1959
                           .acctId = pTscObj->acctId,
1,017,525,884✔
1960
                           .db = pRequest->pDb,
1,017,525,693✔
1961
                           .topicQuery = false,
1962
                           .pSql = pRequest->sqlstr,
1,017,528,278✔
1963
                           .sqlLen = pRequest->sqlLen,
1,017,529,587✔
1964
                           .pMsg = pRequest->msgBuf,
1,017,532,654✔
1965
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1966
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
1,017,525,057✔
1967
                           .pStmtCb = NULL,
1968
                           .pUser = pTscObj->user,
1,017,524,677✔
1969
                           .userId = pTscObj->userId,
1,017,528,577✔
1970
                           .pEffectiveUser = pRequest->effectiveUser,
1,017,528,335✔
1971
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
1,017,527,551✔
1972
                           .enableSysInfo = pTscObj->sysInfo,
1,017,527,921✔
1973
                           .privInfo = pWrapper->pParseCtx ? pWrapper->pParseCtx->privInfo : 0,
1,017,537,606✔
1974
                           .async = true,
1975
                           .svrVer = pTscObj->sVer,
1,017,524,755✔
1976
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
1,017,528,859✔
1977
                           .allocatorId = pRequest->allocatorRefId,
1,017,526,420✔
1978
                           .parseSqlFp = clientParseSql,
1979
                           .parseSqlParam = pWrapper,
1980
                           .setQueryFp = setQueryRequest,
1981
                           .timezone = pTscObj->optionInfo.timezone,
1,017,529,910✔
1982
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
1,017,530,180✔
1983
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
1,017,527,025✔
1984
  (*pCxt)->biMode = biMode;
1,017,532,274✔
1985
  return TSDB_CODE_SUCCESS;
1,017,543,074✔
1986
}
1987

1988
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
1,017,524,370✔
1989
  int32_t              code = TSDB_CODE_SUCCESS;
1,017,524,370✔
1990
  STscObj             *pTscObj = pRequest->pTscObj;
1,017,524,370✔
1991
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
1,017,536,022✔
1992
  if (pWrapper == NULL) {
1,017,528,971✔
1993
    code = terrno;
×
1994
  } else {
1995
    pWrapper->pRequest = pRequest;
1,017,528,971✔
1996
    pRequest->pWrapper = pWrapper;
1,017,533,668✔
1997
    *ppWrapper = pWrapper;
1,017,540,079✔
1998
  }
1999

2000
  if (TSDB_CODE_SUCCESS == code) {
1,017,536,883✔
2001
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
1,017,538,358✔
2002
  }
2003

2004
  if (TSDB_CODE_SUCCESS == code) {
1,017,535,621✔
2005
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
1,017,540,649✔
2006
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
1,017,549,745✔
2007
  }
2008

2009
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
1,017,553,989✔
2010
    int64_t syntaxStart = taosGetTimestampUs();
1,017,547,863✔
2011

2012
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
1,017,547,863✔
2013
    if (pWrapper->pCatalogReq == NULL) {
1,017,509,671✔
2014
      code = terrno;
×
2015
    } else {
2016
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
1,017,512,971✔
2017
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
1,017,525,982✔
2018
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
1,017,519,936✔
2019
    }
2020

2021
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
1,017,509,503✔
2022
  }
2023

2024
  return code;
1,017,538,562✔
2025
}
2026

2027
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
1,018,338,975✔
2028
  SSqlCallbackWrapper *pWrapper = NULL;
1,018,338,975✔
2029
  int32_t              code = TSDB_CODE_SUCCESS;
1,018,348,188✔
2030

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

2040
  if (TSDB_CODE_SUCCESS == code) {
1,017,525,867✔
2041
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
1,017,536,119✔
2042
  }
2043

2044
  if (TSDB_CODE_SUCCESS == code) {
1,017,502,113✔
2045
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
1,003,824,790✔
2046
    code = phaseAsyncQuery(pWrapper);
1,003,818,895✔
2047
  }
2048

2049
  if (TSDB_CODE_SUCCESS != code) {
1,017,487,430✔
2050
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
13,696,846✔
2051
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
13,628,473✔
2052
               pRequest->requestId);
2053
    } else {
2054
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
68,373✔
2055
               pRequest->requestId);
2056
    }
2057

2058
    destorySqlCallbackWrapper(pWrapper);
13,696,928✔
2059
    pRequest->pWrapper = NULL;
13,696,838✔
2060
    qDestroyQuery(pRequest->pQuery);
13,696,838✔
2061
    pRequest->pQuery = NULL;
13,696,928✔
2062

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

2076
    terrno = code;
13,685,071✔
2077
    pRequest->code = code;
13,684,899✔
2078
    doRequestCallback(pRequest, code);
13,684,899✔
2079
  }
2080
}
2081

2082
void restartAsyncQuery(SRequestObj *pRequest, int32_t code) {
2,521,130✔
2083
  tscInfo("restart request:%s p:%p", pRequest->sqlstr, pRequest);
2,521,130✔
2084
  SRequestObj *pUserReq = pRequest;
2,521,130✔
2085
  (void)acquireRequest(pRequest->self);
2,521,130✔
2086
  while (pUserReq) {
2,521,130✔
2087
    if (pUserReq->self == pUserReq->relation.userRefId || pUserReq->relation.userRefId == 0) {
2,521,130✔
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;
2,521,130✔
2098
  if (pUserReq) {
2,521,130✔
2099
    destroyCtxInRequest(pUserReq);
2,521,130✔
2100
    pUserReq->prevCode = code;
2,521,130✔
2101
    (void)memset(&pUserReq->relation, 0, sizeof(pUserReq->relation));
2,521,130✔
2102
  } else {
2103
    tscError("User req is missing");
×
2104
    (void)removeFromMostPrevReq(pRequest);
×
2105
    return;
×
2106
  }
2107
  if (hasSubRequest)
2,521,130✔
2108
    (void)removeFromMostPrevReq(pRequest);
×
2109
  else
2110
    (void)releaseRequest(pUserReq->self);
2,521,130✔
2111
  doAsyncQuery(pUserReq, true);
2,521,130✔
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) {
280,438,072✔
2121
  SAsyncFetchParam *param = pParam;
280,438,072✔
2122
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
280,438,072✔
2123
  taosMemoryFree(param);
280,437,989✔
2124
  return TSDB_CODE_SUCCESS;
280,436,401✔
2125
}
2126

2127
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
280,475,291✔
2128
  if (res == NULL || fp == NULL) {
280,475,291✔
2129
    tscError("taos_fetch_rows_a invalid paras");
×
2130
    return;
×
2131
  }
2132
  if (!TD_RES_QUERY(res)) {
280,475,291✔
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;
280,475,291✔
2139
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
280,475,291✔
2140
    fp(param, res, 0);
37,252✔
2141
    return;
37,252✔
2142
  }
2143

2144
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
280,438,039✔
2145
  if (!pParam) {
280,437,936✔
2146
    fp(param, res, terrno);
×
2147
    return;
×
2148
  }
2149
  pParam->pReq = pRequest;
280,437,936✔
2150
  pParam->fp = fp;
280,437,903✔
2151
  pParam->param = param;
280,437,969✔
2152
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
280,437,938✔
2153
  if (TSDB_CODE_SUCCESS != code) {
280,438,072✔
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) {
171✔
2161
  if (res == NULL || fp == NULL) {
171✔
2162
    tscError("taos_fetch_raw_block_a invalid paras");
×
2163
    return;
×
2164
  }
2165
  if (!TD_RES_QUERY(res)) {
171✔
2166
    tscError("taos_fetch_raw_block_a res is NULL");
×
2167
    return;
×
2168
  }
2169
  SRequestObj    *pRequest = res;
171✔
2170
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
171✔
2171

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

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

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

2190
  return pRequest->body.resInfo.pData;
108✔
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) {
1,194✔
2343
  if (NULL == taos) {
1,194✔
2344
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2345
    return terrno;
×
2346
  }
2347

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

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

2358
  int32_t length = (int32_t)strlen(tableNameList);
1,194✔
2359
  if (0 == length) {
1,194✔
2360
    return TSDB_CODE_SUCCESS;
×
2361
  } else if (length > MAX_TABLE_NAME_LENGTH) {
1,194✔
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";
1,194✔
2367
  code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
1,194✔
2368
  if (code != TSDB_CODE_SUCCESS) {
1,194✔
2369
    terrno = code;
×
2370
    goto _return;
×
2371
  }
2372

2373
  pRequest->syncQuery = true;
1,194✔
2374

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

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

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

2390
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
1,194✔
2391

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

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

2409
TAOS_STMT *taos_stmt_init(TAOS *taos) {
19,859✔
2410
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
19,859✔
2411
  if (NULL == pObj) {
19,859✔
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);
19,859✔
2418
  if (NULL == pStmt) {
19,859✔
2419
    tscError("stmt init failed, errcode:%s", terrstr());
×
2420
  }
2421
  releaseTscObj(*(int64_t *)taos);
19,859✔
2422

2423
  return pStmt;
19,859✔
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) {
12,221✔
2444
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
12,221✔
2445
  if (NULL == pObj) {
12,551✔
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);
12,551✔
2452
  if (NULL == pStmt) {
12,551✔
2453
    tscError("stmt init failed, errcode:%s", terrstr());
×
2454
  }
2455
  releaseTscObj(*(int64_t *)taos);
12,551✔
2456

2457
  return pStmt;
12,551✔
2458
}
2459

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

2467
  return stmtPrepare(stmt, sql, length);
6,542,726✔
2468
}
2469

2470
int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags) {
6,225✔
2471
  if (stmt == NULL || name == NULL) {
6,225✔
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);
6,225✔
2478
  if (code) {
6,225✔
2479
    return code;
597✔
2480
  }
2481

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

2486
  return TSDB_CODE_SUCCESS;
×
2487
}
2488

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

2496
  return stmtSetTbName(stmt, name);
8,918,290✔
2497
}
2498

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

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

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

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

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

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

2528
  return stmtGetColFields(stmt, fieldNum, fields);
×
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) {
28,258✔
2539
  if (stmt == NULL || bind == NULL) {
28,258✔
2540
    tscError("NULL parameter for %s", __FUNCTION__);
×
2541
    terrno = TSDB_CODE_INVALID_PARA;
×
2542
    return terrno;
×
2543
  }
2544

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

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

2554
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
8,902,060✔
2555
  if (stmt == NULL || bind == NULL) {
8,902,060✔
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) {
8,922,594✔
2562
    tscError("invalid bind num %d", bind->num);
3,106✔
2563
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
3,106✔
2564
    return terrno;
×
2565
  }
2566

2567
  int32_t insert = 0;
8,920,814✔
2568
  int32_t code = stmtIsInsert(stmt, &insert);
8,920,695✔
2569
  if (TSDB_CODE_SUCCESS != code) {
8,917,606✔
2570
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2571
    return code;
×
2572
  }
2573
  if (0 == insert && bind->num > 1) {
8,917,606✔
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);
8,917,606✔
2580
}
2581

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

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

2595
  int32_t insert = 0;
1,120✔
2596
  int32_t code = stmtIsInsert(stmt, &insert);
1,120✔
2597
  if (TSDB_CODE_SUCCESS != code) {
1,120✔
2598
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2599
    return code;
×
2600
  }
2601
  if (0 == insert && bind->num > 1) {
1,120✔
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);
1,120✔
2608
}
2609

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

2617
  return stmtAddBatch(stmt);
6,851,503✔
2618
}
2619

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

2627
  return stmtExec(stmt);
6,845,396✔
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) {
×
2651
  if (stmt == NULL || type == NULL || NULL == bytes || idx < 0) {
×
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);
×
2658
}
2659

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

2667
  return stmtUseResult(stmt);
9,552✔
2668
}
2669

2670
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
10,203✔
2671

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

2679
  return stmtAffectedRows(stmt);
3,112✔
2680
}
2681

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

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

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

2699
  return stmtClose(stmt);
32,410✔
2700
}
2701

2702
TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option) {
115,837✔
2703
  if (NULL == taos) {
115,837✔
2704
    tscError("NULL parameter for %s", __FUNCTION__);
×
2705
    terrno = TSDB_CODE_INVALID_PARA;
×
2706
    return NULL;
×
2707
  }
2708
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
115,837✔
2709
  if (NULL == pObj) {
116,642✔
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);
116,642✔
2716

2717
  releaseTscObj(*(int64_t *)taos);
116,604✔
2718

2719
  return pStmt;
116,642✔
2720
}
2721

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

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

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

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

2743
  // check query bind number
2744
  bool isQuery = (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt)));
62,980,886✔
2745
  if (isQuery) {
63,361,052✔
2746
    if (bindv->count != 1 || bindv->bind_cols[0]->num != 1) {
11✔
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) {
63,361,052✔
2754
    STMT2_ELOG_E("async bind param is still working, please try again later");
10,055✔
2755
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
10,055✔
2756
    return terrno;
×
2757
  }
2758

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

2766
  for (int i = 0; i < bindv->count; ++i) {
126,781,122✔
2767
    SVCreateTbReq *pCreateTbReq = NULL;
62,186,158✔
2768
    if (!isQuery) {
63,784,023✔
2769
      STMT2_TLOG("start to bind %dth table", i);
63,841,887✔
2770
      if (bindv->tbnames && bindv->tbnames[i]) {
63,828,853✔
2771
        code = stmtSetTbName2(stmt, bindv->tbnames[i]);
1,041,812✔
2772
        if (code) {
1,041,951✔
2773
          terrno = code;
×
2774
          STMT2_ELOG("set tbname failed, code:%s", stmt2Errstr(stmt));
×
2775
          return terrno;
×
2776
        }
2777
      }
2778

2779
      if (bindv->tags && bindv->tags[i]) {
64,185,136✔
2780
        code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
525,383✔
2781
      } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
63,524,541✔
2782
        code = stmtCheckTags2(stmt, &pCreateTbReq);
103,961✔
2783
      } else if (pStmt->sql.autoCreateTbl) {
63,139,627✔
2784
        code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
×
2785
      }
2786

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

2798
    if (bindv->bind_cols && bindv->bind_cols[i]) {
64,109,650✔
2799
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
64,843,356✔
2800

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

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

2824
  return code;
63,832,002✔
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) {
611,837✔
2867
  if (stmt == NULL) {
611,837✔
2868
    tscError("NULL parameter for %s", __FUNCTION__);
×
2869
    terrno = TSDB_CODE_INVALID_PARA;
×
2870
    return terrno;
×
2871
  }
2872

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

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

2883
  return stmtClose2(stmt);
116,642✔
2884
}
2885

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

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

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

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

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

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

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

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

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

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

2943
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2,292✔
2944
  if (NULL == pObj) {
2,292✔
2945
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2946
    tscError("invalid parameter for %s", __func__);
×
2947
    return terrno;
×
2948
  }
2949
  switch (mode) {
2,292✔
2950
    case TAOS_CONN_MODE_BI:
2,292✔
2951
      atomic_store_8(&pObj->biMode, value);
2,292✔
2952
      break;
2,292✔
2953
    default:
×
2954
      tscError("not supported mode.");
×
2955
      code = TSDB_CODE_INVALID_PARA;
×
2956
  }
2957
  releaseTscObj(*(int64_t *)taos);
2,292✔
2958
  return code;
2,292✔
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) {
×
3105
  if (id == NULL || id[0] == 0) {
×
3106
    return terrno = TSDB_CODE_INVALID_PARA;
×
3107
  }
3108

3109
  // Validate string lengths
3110
  size_t idLen = strlen(id);
×
3111
  if (idLen >= TSDB_INSTANCE_ID_LEN) {
×
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) {
×
3117
    size_t typeLen = strlen(type);
×
3118
    if (typeLen >= TSDB_INSTANCE_TYPE_LEN) {
×
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) {
×
3125
    size_t descLen = strlen(desc);
×
3126
    if (descLen >= TSDB_INSTANCE_DESC_LEN) {
×
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();
×
3133
  if (code != TSDB_CODE_SUCCESS) {
×
3134
    return code;
×
3135
  }
3136

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

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

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

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

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

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

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

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

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

3196
  clientRpc = rpcOpen(&rpcInit);
×
3197
  if (clientRpc == NULL) {
×
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);
×
3205
  epSet.eps[0].port = firstEp.port;
×
3206

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

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

3225
  void *pCont = rpcMallocCont(contLen);
×
3226
  if (pCont == NULL) {
×
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) {
×
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;
×
3240
  rpcMsg.contLen = contLen;
×
3241
  rpcMsg.msgType = TDMT_MND_REGISTER_INSTANCE;
×
3242
  rpcMsg.info.ahandle = (void *)0x9528;  // Different magic number from server status
×
3243
  rpcMsg.info.notFreeAhandle = 1;
×
3244

3245
  code = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
×
3246
  if (TSDB_CODE_SUCCESS != code) {
×
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) {
×
3255
    code = rpcRsp.code;
×
3256
    tscError("instance register failed, code:%s", tstrerror(code));
×
3257
  } else {
3258
    code = TSDB_CODE_SUCCESS;
×
3259
  }
3260

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

3266
  terrno = code;
×
3267
  return code;
×
3268
}
3269

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

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

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

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

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

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

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

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

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

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

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

3344
  clientRpc = rpcOpen(&rpcInit);
×
3345
  if (clientRpc == NULL) {
×
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);
×
3353
  epSet.eps[0].port = firstEp.port;
×
3354
  SInstanceListReq req = {0};
×
3355
  if (filter_type != NULL && filter_type[0] != 0) {
×
3356
    tstrncpy(req.filter_type, filter_type, sizeof(req.filter_type));
×
3357
  }
3358

3359
  // Serialize request to get required length
3360
  int32_t contLen = tSerializeSInstanceListReq(NULL, 0, &req);
×
3361
  if (contLen <= 0) {
×
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);
×
3370
  if (pCont == NULL) {
×
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) {
×
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;
×
3387
  rpcMsg.contLen = contLen;
×
3388
  rpcMsg.msgType = TDMT_MND_LIST_INSTANCES;
×
3389
  rpcMsg.info.ahandle = (void *)0x9529;  // Different magic number from register
×
3390
  rpcMsg.info.notFreeAhandle = 1;
×
3391

3392
  code = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
×
3393
  if (TSDB_CODE_SUCCESS != code) {
×
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) {
×
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) {
×
3415
    SInstanceListRsp rsp = {0};
×
3416
    code = tDeserializeSInstanceListRsp(rpcRsp.pCont, rpcRsp.contLen, &rsp);
×
3417
    if (code != TSDB_CODE_SUCCESS) {
×
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;
×
3435
    *pCount = rsp.count;
×
3436
  } else {
3437
    *pList = NULL;
×
3438
    *pCount = 0;
×
3439
  }
3440

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

3446
  return TSDB_CODE_SUCCESS;
×
3447
}
3448

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

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

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