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

taosdata / TDengine / #5044

06 May 2026 02:35AM UTC coverage: 73.169% (+0.06%) from 73.107%
#5044

push

travis-ci

web-flow
feat: [6659794715] cpu limit (#35153)

244 of 275 new or added lines in 23 files covered. (88.73%)

526 existing lines in 141 files now uncovered.

277745 of 379596 relevant lines covered (73.17%)

133740972.66 hits per line

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

54.07
/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 CLIENT_CLEANUP_WAIT_TIMEOUT_MS 10000
40

41
#ifdef TAOSD_INTEGRATED
42
extern void shellStopDaemon();
43
#endif
44

45
static int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper);
46

47
static int32_t waitRefSetToBaseCount(int32_t rsetId, const char *name, int64_t startMs, int64_t timeoutMs) {
3,069,006✔
48
  if (rsetId < 0) {
3,069,006✔
49
    return TSDB_CODE_SUCCESS;
78✔
50
  }
51

52
  while (true) {
942,590,130✔
53
    int32_t count = 0;
945,659,058✔
54
    int32_t code = taosGetRefSetCount(rsetId, &count);
945,659,058✔
55
    if (code != TSDB_CODE_SUCCESS) {
945,659,058✔
56
      tscWarn("failed to inspect %s ref pool:%d before cleanup, code:%s", name, rsetId, tstrerror(code));
×
57
      return code;
29,718✔
58
    }
59

60
    if (count <= 1) {
945,659,058✔
61
      return TSDB_CODE_SUCCESS;
2,853,356✔
62
    }
63

64
    if (timeoutMs >= 0 && taosGetTimestampMs() - startMs >= timeoutMs) {
1,885,611,404✔
65
      tscWarn("timeout waiting for %s ref pool:%d to drain, count:%d", name, rsetId, count);
215,572✔
66
      return TSDB_CODE_TIMEOUT_ERROR;
215,572✔
67
    }
68

69
    taosMsleep(1);
942,590,130✔
70
  }
71
}
72

73
int taos_options(TSDB_OPTION option, const void *arg, ...) {
85,034,661✔
74
  if (arg == NULL) {
85,034,661✔
75
    return TSDB_CODE_INVALID_PARA;
×
76
  }
77
  static int32_t lock = 0;
78

79
  for (int i = 1; atomic_val_compare_exchange_32(&lock, 0, 1) != 0; ++i) {
2,147,483,647✔
80
    if (i % 1000 == 0) {
2,147,483,647✔
81
      (void)sched_yield();
10,112,240✔
82
    }
83
  }
84

85
  int ret = taos_options_imp(option, (const char *)arg);
1,006,780✔
86
  atomic_store_32(&lock, 0);
85,038,793✔
87
  return ret;
85,038,793✔
88
}
89

90
#if !defined(WINDOWS) && !defined(TD_ASTRA)
91
static void freeTz(void *p) {
2,278✔
92
  timezone_t tz = *(timezone_t *)p;
2,278✔
93
  tzfree(tz);
2,278✔
94
}
2,278✔
95
#endif
96

97
int32_t tzInit() {
1,534,464✔
98
#if !defined(WINDOWS) && !defined(TD_ASTRA)
99
  pTimezoneMap = taosHashInit(0, MurmurHash3_32, false, HASH_ENTRY_LOCK);
1,534,464✔
100
  if (pTimezoneMap == NULL) {
1,534,464✔
101
    return terrno;
×
102
  }
103
  taosHashSetFreeFp(pTimezoneMap, freeTz);
1,534,464✔
104

105
  pTimezoneNameMap = taosHashInit(0, taosIntHash_64, false, HASH_ENTRY_LOCK);
1,534,464✔
106
  if (pTimezoneNameMap == NULL) {
1,534,464✔
107
    return terrno;
×
108
  }
109
#endif
110
  return 0;
1,534,464✔
111
}
112

113
void tzCleanup() {
1,534,503✔
114
#if !defined(WINDOWS) && !defined(TD_ASTRA)
115
  taosHashCleanup(pTimezoneMap);
1,534,503✔
116
  taosHashCleanup(pTimezoneNameMap);
1,534,503✔
117
#endif
118
}
1,534,503✔
119

120
#if !defined(WINDOWS) && !defined(TD_ASTRA)
121
static timezone_t setConnnectionTz(const char *val) {
3,764✔
122
  timezone_t  tz = NULL;
3,764✔
123
  timezone_t *tmp = taosHashGet(pTimezoneMap, val, strlen(val));
3,764✔
124
  if (tmp != NULL && *tmp != NULL) {
3,764✔
125
    tz = *tmp;
1,486✔
126
    goto END;
1,486✔
127
  }
128

129
  tscDebug("set timezone to %s", val);
2,278✔
130
  tz = tzalloc(val);
2,278✔
131
  if (tz == NULL) {
2,278✔
132
    tscWarn("%s unknown timezone %s change to UTC", __func__, val);
198✔
133
    tz = tzalloc("UTC");
198✔
134
    if (tz == NULL) {
198✔
135
      tscError("%s set timezone UTC error", __func__);
×
136
      terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
137
      goto END;
×
138
    }
139
  }
140
  int32_t code = taosHashPut(pTimezoneMap, val, strlen(val), &tz, sizeof(timezone_t));
2,278✔
141
  if (code != 0) {
2,278✔
142
    tscError("%s put timezone to tz map error:%d", __func__, code);
×
143
    tzfree(tz);
×
144
    tz = NULL;
×
145
    goto END;
×
146
  }
147

148
  time_t tx1 = taosGetTimestampSec();
2,278✔
149
  char   output[TD_TIMEZONE_LEN] = {0};
2,278✔
150
  code = taosFormatTimezoneStr(tx1, val, tz, output);
2,278✔
151
  if (code == 0) {
2,278✔
152
    code = taosHashPut(pTimezoneNameMap, &tz, sizeof(timezone_t), output, strlen(output) + 1);
2,278✔
153
  }
154
  if (code != 0) {
2,278✔
155
    tscError("failed to put timezone %s to map", val);
×
156
  }
157

158
END:
2,278✔
159
  return tz;
3,764✔
160
}
161
#endif
162

163
static int32_t setConnectionOption(TAOS *taos, TSDB_OPTION_CONNECTION option, const char *val) {
9,742✔
164
  if (taos == NULL) {
9,742✔
165
    return terrno = TSDB_CODE_INVALID_PARA;
198✔
166
  }
167

168
#ifdef WINDOWS
169
  if (option == TSDB_OPTION_CONNECTION_TIMEZONE) {
170
    return terrno = TSDB_CODE_NOT_SUPPORTTED_IN_WINDOWS;
171
  }
172
#endif
173

174
  if (option < TSDB_OPTION_CONNECTION_CLEAR || option >= TSDB_MAX_OPTIONS_CONNECTION) {
9,544✔
175
    return terrno = TSDB_CODE_INVALID_PARA;
198✔
176
  }
177

178
  int32_t code = taos_init();
9,346✔
179
  // initialize global config
180
  if (code != 0) {
9,346✔
181
    return terrno = code;
×
182
  }
183

184
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
9,346✔
185
  if (NULL == pObj) {
9,346✔
186
    tscError("invalid parameter for %s", __func__);
×
187
    return terrno;
×
188
  }
189

190
  if (option == TSDB_OPTION_CONNECTION_CLEAR) {
9,346✔
191
    val = NULL;
198✔
192
  }
193

194
#ifndef DISALLOW_NCHAR_WITHOUT_ICONV
195
  if (option == TSDB_OPTION_CONNECTION_CHARSET || option == TSDB_OPTION_CONNECTION_CLEAR) {
9,346✔
196
    if (val != NULL) {
1,780✔
197
      if (!taosValidateEncodec(val)) {
1,186✔
198
        code = terrno;
296✔
199
        goto END;
296✔
200
      }
201
      void *tmp = taosConvInit(val);
890✔
202
      if (tmp == NULL) {
890✔
203
        code = terrno;
198✔
204
        goto END;
198✔
205
      }
206
      pObj->optionInfo.charsetCxt = tmp;
692✔
207
    } else {
208
      pObj->optionInfo.charsetCxt = NULL;
594✔
209
    }
210
  }
211
#endif
212
  if (option == TSDB_OPTION_CONNECTION_TIMEZONE || option == TSDB_OPTION_CONNECTION_CLEAR) {
8,852✔
213
#if !defined(WINDOWS) && !defined(TD_ASTRA)
214
    if (val != NULL) {
4,160✔
215
      if (val[0] == 0) {
3,764✔
216
        val = "UTC";
198✔
217
      }
218
      timezone_t tz = setConnnectionTz(val);
3,764✔
219
      if (tz == NULL) {
3,764✔
220
        code = terrno;
×
221
        goto END;
×
222
      }
223
      pObj->optionInfo.timezone = tz;
3,764✔
224
    } else {
225
      pObj->optionInfo.timezone = NULL;
396✔
226
    }
227
#endif
228
  }
229

230
  if (option == TSDB_OPTION_CONNECTION_USER_APP || option == TSDB_OPTION_CONNECTION_CLEAR) {
8,852✔
231
    if (val != NULL) {
1,159✔
232
      tstrncpy(pObj->optionInfo.userApp, val, sizeof(pObj->optionInfo.userApp));
763✔
233
    } else {
234
      pObj->optionInfo.userApp[0] = 0;
396✔
235
    }
236
  }
237

238
  if (option == TSDB_OPTION_CONNECTION_CONNECTOR_INFO || option == TSDB_OPTION_CONNECTION_CLEAR) {
8,852✔
239
    if (val != NULL) {
1,159✔
240
      tstrncpy(pObj->optionInfo.cInfo, val, sizeof(pObj->optionInfo.cInfo));
763✔
241
    } else {
242
      pObj->optionInfo.cInfo[0] = 0;
396✔
243
    }
244
  }
245

246
  if (option == TSDB_OPTION_CONNECTION_USER_IP || option == TSDB_OPTION_CONNECTION_CLEAR) {
8,852✔
247
    SIpRange dualIp = {0};
1,880✔
248
    if (val != NULL) {
1,880✔
249
      pObj->optionInfo.userIp = taosInetAddr(val);
1,286✔
250
      SIpAddr addr = {0};
1,286✔
251
      code = taosGetIpFromFqdn(tsEnableIpv6, val, &addr);
1,286✔
252
      if (code == 0) {
1,286✔
253
        code = tIpStrToUint(&addr, &pObj->optionInfo.userDualIp);
692✔
254
      }
255
      if (code != 0) {
1,286✔
256
        tscError("ipv6 flag %d failed to convert user ip %s to dual ip since %s", tsEnableIpv6 ? 1 : 0, val,
594✔
257
                 tstrerror(code));
258
        pObj->optionInfo.userIp = INADDR_NONE;
594✔
259
        pObj->optionInfo.userDualIp = dualIp;
594✔
260
        code = 0;
594✔
261
      }
262
    } else {
263
      pObj->optionInfo.userIp = INADDR_NONE;
594✔
264
      pObj->optionInfo.userDualIp = dualIp;
594✔
265
    }
266
  }
267

268
END:
6,972✔
269
  releaseTscObj(*(int64_t *)taos);
9,346✔
270
  return terrno = code;
9,346✔
271
}
272

273
int taos_options_connection(TAOS *taos, TSDB_OPTION_CONNECTION option, const void *arg, ...) {
9,742✔
274
  return setConnectionOption(taos, option, (const char *)arg);
9,742✔
275
}
276

277
// this function may be called by user or system, or by both simultaneously.
278
void taos_cleanup(void) {
1,535,174✔
279
  tscInfo("start to cleanup client environment");
1,535,174✔
280
  if (!beginAsyncWorkShutdown()) {
1,535,174✔
281
    return;
671✔
282
  }
283

284
  int64_t cleanupStartMs = taosGetTimestampMs();
1,534,503✔
285

286
  if (TSDB_CODE_SUCCESS != waitRefSetToBaseCount(clientReqRefPool, "request", cleanupStartMs, CLIENT_CLEANUP_WAIT_TIMEOUT_MS)) {
1,534,503✔
287
    tscWarn("request ref pool did not drain cleanly before cleanup continues");
103,167✔
288
  }
289

290
  monitorClose();
1,534,503✔
291
  tscStopCrashReport();
1,534,503✔
292

293
  hbMgrCleanUp();
1,534,503✔
294

295
  catalogDestroy();
1,534,503✔
296
  schedulerDestroy();
1,534,503✔
297

298
  fmFuncMgtDestroy();
1,534,503✔
299
  qCleanupKeywordsTable();
1,534,503✔
300

301
#if !defined(WINDOWS) && !defined(TD_ASTRA)
302
  tzCleanup();
1,534,503✔
303
#endif
304
  tmqMgmtClose();
1,534,503✔
305

306
  int32_t id = clientReqRefPool;
1,534,503✔
307
  clientReqRefPool = -1;
1,534,503✔
308
  taosCloseRef(id);
1,534,503✔
309

310
  if (TSDB_CODE_SUCCESS != waitRefSetToBaseCount(clientConnRefPool, "connection", cleanupStartMs, CLIENT_CLEANUP_WAIT_TIMEOUT_MS)) {
1,534,503✔
311
    tscWarn("connection ref pool did not drain cleanly before cleanup continues");
112,405✔
312
  }
313

314
  id = clientConnRefPool;
1,534,503✔
315
  clientConnRefPool = -1;
1,534,503✔
316
  taosCloseRef(id);
1,534,503✔
317

318
  nodesDestroyAllocatorSet();
1,534,503✔
319
  cleanupAppInfo();
1,534,503✔
320
  rpcCleanup();
1,534,503✔
321
  tscDebug("rpc cleanup");
1,534,503✔
322

323
  if (TSDB_CODE_SUCCESS != cleanupTaskQueue()) {
1,534,503✔
324
    tscWarn("failed to cleanup task queue");
×
325
  }
326

327
  sessMgtDestroy();
1,534,503✔
328

329
  taosConvDestroy();
1,534,503✔
330
  DestroyRegexCache();
1,534,503✔
331
#ifdef TAOSD_INTEGRATED
332
  shellStopDaemon();
333
#endif
334
  tscInfo("all local resources released");
1,534,503✔
335
  taosCleanupCfg();
1,534,503✔
336
#ifndef TAOSD_INTEGRATED
337
  taosCloseLog();
1,534,503✔
338
#endif
339
}
340

341
static setConfRet taos_set_config_imp(const char *config) {
14✔
342
  setConfRet ret = {SET_CONF_RET_SUCC, {0}};
14✔
343
  // TODO: need re-implementation
344
  return ret;
14✔
345
}
346

347
setConfRet taos_set_config(const char *config) {
14✔
348
  // TODO  pthread_mutex_lock(&setConfMutex);
349
  setConfRet ret = taos_set_config_imp(config);
14✔
350
  //  pthread_mutex_unlock(&setConfMutex);
351
  return ret;
14✔
352
}
353

354
TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) {
87,251,079✔
355
  tscInfo("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
87,251,079✔
356
  if (user == NULL) {
87,251,011✔
357
    user = TSDB_DEFAULT_USER;
185,661✔
358
  }
359

360
  if (pass == NULL) {
87,251,011✔
361
    pass = TSDB_DEFAULT_PASS;
185,661✔
362
  }
363

364
  STscObj *pObj = NULL;
87,251,011✔
365
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
87,251,241✔
366
  if (TSDB_CODE_SUCCESS == code) {
87,237,681✔
367
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
87,227,976✔
368
    if (NULL == rid) {
87,228,838✔
369
      tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db);
×
370
      return NULL;
×
371
    }
372
    *rid = pObj->id;
87,228,838✔
373
    return (TAOS *)rid;
87,228,964✔
374
  } else {
375
    terrno = code;
14,860✔
376
  }
377

378
  return NULL;
15,006✔
379
}
380

381
void taos_set_option(OPTIONS *options, const char *key, const char *value) {
3,442✔
382
  if (options == NULL || key == NULL || value == NULL) {
3,442✔
383
    terrno = TSDB_CODE_INVALID_PARA;
294✔
384
    tscError("taos_set_option invalid parameter, options: %p, key: %p, value: %p", options, key, value);
294✔
385
    return;
294✔
386
  }
387

388
  size_t count = (size_t)options->count;
3,148✔
389
  size_t len = sizeof(options->keys) / sizeof(options->keys[0]);
3,148✔
390
  if (count >= len) {
3,148✔
391
    terrno = TSDB_CODE_INVALID_PARA;
98✔
392
    tscError("taos_set_option overflow, count: %zu, reached capacity: %zu", count, len);
98✔
393
    return;
98✔
394
  }
395

396
  options->keys[count] = key;
3,050✔
397
  options->values[count] = value;
3,050✔
398
  options->count = (uint16_t)(count + 1);
3,050✔
399
}
400

401
static int set_connection_option_or_close(TAOS *taos, TSDB_OPTION_CONNECTION option, const char *value) {
3,048✔
402
  if (value == NULL) return TSDB_CODE_SUCCESS;
3,048✔
403
  int code = taos_options_connection(taos, option, value);
588✔
404
  if (code != TSDB_CODE_SUCCESS) {
588✔
405
    tscError("failed to set option(%d): %s", (int)option, value);
98✔
406
    taos_close(taos);
98✔
407
    return code;
98✔
408
  }
409
  return TSDB_CODE_SUCCESS;
490✔
410
}
411

412
TAOS *taos_connect_with(const OPTIONS *options) {
786✔
413
  const char *ip = NULL;
786✔
414
  const char *user = NULL;
786✔
415
  const char *pass = NULL;
786✔
416
  const char *db = NULL;
786✔
417
  uint16_t    port = 0;
786✔
418

419
  const char *charset = NULL;
786✔
420
  const char *timezone = NULL;
786✔
421
  const char *userIp = NULL;
786✔
422
  const char *userApp = NULL;
786✔
423
  const char *connectorInfo = NULL;
786✔
424

425
  if (options && options->count > 0) {
786✔
426
    size_t count = (size_t)options->count;
688✔
427
    for (size_t i = 0; i < count; ++i) {
3,836✔
428
      const char *key = options->keys[i];
3,148✔
429
      const char *value = options->values[i];
3,148✔
430
      if (key == NULL || value == NULL) {
3,148✔
431
        tscWarn("taos_connect_with option key or value is NULL, index: %zu", i);
294✔
432
        continue;
294✔
433
      }
434

435
      if (strcmp(key, "ip") == 0) {
2,854✔
436
        ip = value;
590✔
437
      } else if (strcmp(key, "user") == 0) {
2,264✔
438
        user = value;
296✔
439
      } else if (strcmp(key, "pass") == 0) {
1,968✔
440
        pass = value;
296✔
441
      } else if (strcmp(key, "db") == 0) {
1,672✔
442
        db = value;
98✔
443
      } else if (strcmp(key, "port") == 0) {
1,574✔
444
        port = (uint16_t)taosStr2Int32(value, NULL, 10);
492✔
445
      } else if (strcmp(key, "charset") == 0) {
1,082✔
446
        charset = value;
196✔
447
      } else if (strcmp(key, "timezone") == 0) {
886✔
448
        timezone = value;
98✔
449
      } else if (strcmp(key, "userIp") == 0) {
788✔
450
        userIp = value;
98✔
451
      } else if (strcmp(key, "userApp") == 0) {
690✔
452
        userApp = value;
98✔
453
      } else if (strcmp(key, "connectorInfo") == 0) {
592✔
454
        connectorInfo = value;
98✔
455
      } else {
456
        tscWarn("taos_connect_with unknown option key: %s", key);
494✔
457
      }
458
    }
459
  }
460

461
  TAOS *taos = taos_connect(ip, user, pass, db, port);
786✔
462
  if (taos == NULL) return NULL;
786✔
463

464
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_CHARSET, charset) != TSDB_CODE_SUCCESS) return NULL;
688✔
465
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_TIMEZONE, timezone) != TSDB_CODE_SUCCESS) return NULL;
590✔
466
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_USER_IP, userIp) != TSDB_CODE_SUCCESS) return NULL;
590✔
467
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_USER_APP, userApp) != TSDB_CODE_SUCCESS) return NULL;
590✔
468
  if (set_connection_option_or_close(taos, TSDB_OPTION_CONNECTION_CONNECTOR_INFO, connectorInfo) != TSDB_CODE_SUCCESS)
590✔
469
    return NULL;
×
470

471
  return taos;
590✔
472
}
473

474
TAOS *taos_connect_with_dsn(const char *dsn) {
98✔
475
  terrno = TSDB_CODE_OPS_NOT_SUPPORT;
98✔
476
  tscError("taos_connect_with_dsn not supported");
98✔
477
  return NULL;
98✔
478
}
479

480
int taos_set_notify_cb(TAOS *taos, __taos_notify_fn_t fp, void *param, int type) {
1,400✔
481
  if (taos == NULL) {
1,400✔
482
    terrno = TSDB_CODE_INVALID_PARA;
×
483
    return terrno;
×
484
  }
485

486
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
1,400✔
487
  if (NULL == pObj) {
1,400✔
488
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
489
    tscError("invalid parameter for %s", __func__);
×
490
    return terrno;
×
491
  }
492

493
  switch (type) {
1,400✔
494
    case TAOS_NOTIFY_PASSVER: {
400✔
495
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
400✔
496
      pObj->passInfo.fp = fp;
400✔
497
      pObj->passInfo.param = param;
400✔
498
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
400✔
499
      break;
400✔
500
    }
501
    case TAOS_NOTIFY_WHITELIST_VER: {
×
502
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
503
      pObj->whiteListInfo.fp = fp;
×
504
      pObj->whiteListInfo.param = param;
×
505
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
506
      break;
×
507
    }
508
    case TAOS_NOTIFY_USER_DROPPED: {
1,000✔
509
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
1,000✔
510
      pObj->userDroppedInfo.fp = fp;
1,000✔
511
      pObj->userDroppedInfo.param = param;
1,000✔
512
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
1,000✔
513
      break;
1,000✔
514
    }
515
    case TAOS_NOTIFY_DATETIME_WHITELIST_VER: {
×
516
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
517
      pObj->dateTimeWhiteListInfo.fp = fp;
×
518
      pObj->dateTimeWhiteListInfo.param = param;
×
519
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
520
      break;
×
521
    }
522
    case TAOS_NOTIFY_TOKEN: {
×
523
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
524
      pObj->tokenNotifyInfo.fp = fp;
×
525
      pObj->tokenNotifyInfo.param = param;
×
526
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
527
      break;
×
528
    }
529
    default: {
×
530
      terrno = TSDB_CODE_INVALID_PARA;
×
531
      releaseTscObj(*(int64_t *)taos);
×
532
      return terrno;
×
533
    }
534
  }
535

536
  releaseTscObj(*(int64_t *)taos);
1,400✔
537
  return 0;
1,400✔
538
}
539

540
typedef struct SFetchWhiteListInfo {
541
  int64_t                     connId;
542
  __taos_async_whitelist_fn_t userCbFn;
543
  void                       *userParam;
544
} SFetchWhiteListInfo;
545

546
int32_t fetchWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
547
  SFetchWhiteListInfo *pInfo = (SFetchWhiteListInfo *)param;
×
548
  TAOS                *taos = &pInfo->connId;
×
549
  if (code != TSDB_CODE_SUCCESS) {
×
550
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
551
    taosMemoryFree(pMsg->pData);
×
552
    taosMemoryFree(pMsg->pEpSet);
×
553
    taosMemoryFree(pInfo);
×
554
    return code;
×
555
  }
556

557
  SGetUserIpWhiteListRsp wlRsp;
×
558
  if (TSDB_CODE_SUCCESS != tDeserializeSGetUserIpWhiteListRsp(pMsg->pData, pMsg->len, &wlRsp)) {
×
559
    taosMemoryFree(pMsg->pData);
×
560
    taosMemoryFree(pMsg->pEpSet);
×
561
    taosMemoryFree(pInfo);
×
562
    tFreeSGetUserIpWhiteListRsp(&wlRsp);
×
563
    return terrno;
×
564
  }
565

566
  uint64_t *pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(uint64_t));
×
567
  if (pWhiteLists == NULL) {
×
568
    taosMemoryFree(pMsg->pData);
×
569
    taosMemoryFree(pMsg->pEpSet);
×
570
    taosMemoryFree(pInfo);
×
571
    tFreeSGetUserIpWhiteListRsp(&wlRsp);
×
572
    return terrno;
×
573
  }
574

575
  for (int i = 0; i < wlRsp.numWhiteLists; ++i) {
×
576
    pWhiteLists[i] = ((uint64_t)wlRsp.pWhiteLists[i].mask << 32) | wlRsp.pWhiteLists[i].ip;
×
577
  }
578

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

581
  taosMemoryFree(pWhiteLists);
×
582
  taosMemoryFree(pMsg->pData);
×
583
  taosMemoryFree(pMsg->pEpSet);
×
584
  taosMemoryFree(pInfo);
×
585
  tFreeSGetUserIpWhiteListRsp(&wlRsp);
×
586
  return code;
×
587
}
588

589
void taos_fetch_whitelist_a(TAOS *taos, __taos_async_whitelist_fn_t fp, void *param) {
×
590
  if (NULL == taos) {
×
591
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
592
    return;
×
593
  }
594

595
  int64_t connId = *(int64_t *)taos;
×
596

597
  STscObj *pTsc = acquireTscObj(connId);
×
598
  if (NULL == pTsc) {
×
599
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
600
    return;
×
601
  }
602

603
  SGetUserWhiteListReq req;
×
604
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
605
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
606
  if (msgLen < 0) {
×
607
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
608
    releaseTscObj(connId);
×
609
    return;
×
610
  }
611

612
  void *pReq = taosMemoryMalloc(msgLen);
×
613
  if (pReq == NULL) {
×
614
    fp(param, terrno, taos, 0, NULL);
×
615
    releaseTscObj(connId);
×
616
    return;
×
617
  }
618

619
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
620
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
621
    taosMemoryFree(pReq);
×
622
    releaseTscObj(connId);
×
623
    return;
×
624
  }
625

626
  SFetchWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchWhiteListInfo));
×
627
  if (pParam == NULL) {
×
628
    fp(param, terrno, taos, 0, NULL);
×
629
    taosMemoryFree(pReq);
×
630
    releaseTscObj(connId);
×
631
    return;
×
632
  }
633

634
  pParam->connId = connId;
×
635
  pParam->userCbFn = fp;
×
636

637
  pParam->userParam = param;
×
638
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
639
  if (pSendInfo == NULL) {
×
640
    fp(param, terrno, taos, 0, NULL);
×
641
    taosMemoryFree(pParam);
×
642
    taosMemoryFree(pReq);
×
643
    releaseTscObj(connId);
×
644
    return;
×
645
  }
646

647
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
648
  pSendInfo->requestId = generateRequestId();
×
649
  pSendInfo->requestObjRefId = 0;
×
650
  pSendInfo->param = pParam;
×
651
  pSendInfo->fp = fetchWhiteListCallbackFn;
×
652
  pSendInfo->msgType = TDMT_MND_GET_USER_IP_WHITELIST;
×
653

654
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
655
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
656
    tscWarn("failed to async send msg to server");
×
657
  }
658
  releaseTscObj(connId);
×
659
  return;
×
660
}
661

662
typedef struct SFetchIpWhiteListInfo {
663
  int64_t connId;
664
  bool    supportNeg;
665
  void   *userParam;
666

667
  __taos_async_ip_whitelist_fn_t userCbFn;
668
} SFetchIpWhiteListInfo;
669

670
int32_t fetchIpWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
671
  int32_t lino = 0;
×
672
  char  **pWhiteLists = NULL;
×
673

674
  SGetUserIpWhiteListRsp wlRsp = {0};
×
675

676
  SFetchIpWhiteListInfo *pInfo = (SFetchIpWhiteListInfo *)param;
×
677
  TAOS                  *taos = &pInfo->connId;
×
678

679
  if (code != TSDB_CODE_SUCCESS) {
×
680
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
681
    TAOS_CHECK_GOTO(code, &lino, _error);
×
682
  }
683

684
  if ((code = tDeserializeSGetUserIpWhiteListDualRsp(pMsg->pData, pMsg->len, &wlRsp)) != TSDB_CODE_SUCCESS) {
×
685
    TAOS_CHECK_GOTO(code, &lino, _error);
×
686
  }
687

688
  pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(char *));
×
689
  if (pWhiteLists == NULL) {
×
690
    code = terrno;
×
691
    TAOS_CHECK_GOTO(code, &lino, _error);
×
692
  }
693

694
  int32_t numWhiteLists = 0;
×
695
  for (int32_t i = 0; i < wlRsp.numWhiteLists; i++) {
×
696
    SIpRange *pIpRange = &wlRsp.pWhiteListsDual[i];
×
697
    if (!pInfo->supportNeg && pIpRange->neg) {
×
698
      continue;
×
699
    }
700
    SIpAddr ipAddr = {0};
×
701

702
    code = tIpUintToStr(pIpRange, &ipAddr);
×
703
    TAOS_CHECK_GOTO(code, &lino, _error);
×
704

705
    char *ip = taosMemCalloc(1, IP_RESERVE_CAP);
×
706
    if (ip == NULL) {
×
707
      code = terrno;
×
708
      TAOS_CHECK_GOTO(code, &lino, _error);
×
709
    }
710
    if (ipAddr.type == 0) {
×
711
      if (pInfo->supportNeg) {
×
712
        snprintf(ip, IP_RESERVE_CAP, "%c %s/%d", pIpRange->neg ? '-' : '+', ipAddr.ipv4, ipAddr.mask);
×
713
      } else {
714
        snprintf(ip, IP_RESERVE_CAP, "%s/%d", ipAddr.ipv4, ipAddr.mask);
×
715
      }
716
    } else {
717
      if (ipAddr.ipv6[0] == 0) {
×
718
        memcpy(ipAddr.ipv6, "::", 2);
×
719
      }
720
      if (pInfo->supportNeg) {
×
721
        snprintf(ip, IP_RESERVE_CAP, "%c %s/%d", pIpRange->neg ? '-' : '+', ipAddr.ipv6, ipAddr.mask);
×
722
      } else {
723
        snprintf(ip, IP_RESERVE_CAP, "%s/%d", ipAddr.ipv6, ipAddr.mask);
×
724
      }
725
    }
726
    pWhiteLists[numWhiteLists++] = ip;
×
727
  }
728

729
  pInfo->userCbFn(pInfo->userParam, code, taos, numWhiteLists, pWhiteLists);
×
730
_error:
×
731
  if (pWhiteLists != NULL) {
×
732
    for (int32_t i = 0; i < numWhiteLists; i++) {
×
733
      taosMemFree(pWhiteLists[i]);
×
734
    }
735
    taosMemoryFree(pWhiteLists);
×
736
  }
737
  taosMemoryFree(pMsg->pData);
×
738
  taosMemoryFree(pMsg->pEpSet);
×
739
  taosMemoryFree(pInfo);
×
740
  tFreeSGetUserIpWhiteListDualRsp(&wlRsp);
×
741
  return code;
×
742
}
743

744
static void taosFetchIpWhiteList(TAOS *taos, __taos_async_whitelist_dual_stack_fn_t fp, void *param, bool supportNeg) {
×
745
  if (NULL == taos) {
×
746
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
747
    return;
×
748
  }
749
  int64_t connId = *(int64_t *)taos;
×
750

751
  STscObj *pTsc = acquireTscObj(connId);
×
752
  if (NULL == pTsc) {
×
753
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
754
    return;
×
755
  }
756

757
  SGetUserWhiteListReq req;
×
758
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
759
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
760
  if (msgLen < 0) {
×
761
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
762
    releaseTscObj(connId);
×
763
    return;
×
764
  }
765

766
  void *pReq = taosMemoryMalloc(msgLen);
×
767
  if (pReq == NULL) {
×
768
    fp(param, terrno, taos, 0, NULL);
×
769
    releaseTscObj(connId);
×
770
    return;
×
771
  }
772

773
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
774
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
775
    taosMemoryFree(pReq);
×
776
    releaseTscObj(connId);
×
777
    return;
×
778
  }
779

780
  SFetchIpWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchIpWhiteListInfo));
×
781
  if (pParam == NULL) {
×
782
    fp(param, terrno, taos, 0, NULL);
×
783
    taosMemoryFree(pReq);
×
784
    releaseTscObj(connId);
×
785
    return;
×
786
  }
787

788
  pParam->connId = connId;
×
789
  pParam->supportNeg = supportNeg;
×
790
  pParam->userCbFn = fp;
×
791
  pParam->userParam = param;
×
792

793
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
794
  if (pSendInfo == NULL) {
×
795
    fp(param, terrno, taos, 0, NULL);
×
796
    taosMemoryFree(pParam);
×
797
    taosMemoryFree(pReq);
×
798
    releaseTscObj(connId);
×
799
    return;
×
800
  }
801

802
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
803
  pSendInfo->requestId = generateRequestId();
×
804
  pSendInfo->requestObjRefId = 0;
×
805
  pSendInfo->param = pParam;
×
806
  pSendInfo->fp = fetchIpWhiteListCallbackFn;
×
807
  pSendInfo->msgType = TDMT_MND_GET_USER_IP_WHITELIST_DUAL;
×
808

809
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
810
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
811
    tscWarn("failed to async send msg to server");
×
812
  }
813
  releaseTscObj(connId);
×
814
  return;
×
815
}
816

817
void taos_fetch_whitelist_dual_stack_a(TAOS *taos, __taos_async_whitelist_dual_stack_fn_t fp, void *param) {
×
818
  taosFetchIpWhiteList(taos, fp, param, false);
×
819
}
×
820

821
void taos_fetch_ip_whitelist_a(TAOS *taos, __taos_async_ip_whitelist_fn_t fp, void *param) {
×
822
  taosFetchIpWhiteList(taos, fp, param, true);
×
823
}
×
824

825
typedef struct SFetchDateTimeWhiteListInfo {
826
  int64_t                              connId;
827
  void                                *userParam;
828
  __taos_async_datetime_whitelist_fn_t userCbFn;
829
} SFetchDateTimeWhiteListInfo;
830

831
static const char *weekdays[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
832
int32_t            fetchDateTimeWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
833
  int32_t lino = 0;
×
834
  char  **pWhiteLists = NULL;
×
835

836
  SUserDateTimeWhiteList wlRsp = {0};
×
837

838
  SFetchDateTimeWhiteListInfo *pInfo = (SFetchDateTimeWhiteListInfo *)param;
×
839
  TAOS                        *taos = &pInfo->connId;
×
840

841
  if (code != TSDB_CODE_SUCCESS) {
×
842
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
843
    TAOS_CHECK_GOTO(code, &lino, _error);
×
844
  }
845

846
  if ((code = tDeserializeSUserDateTimeWhiteList(pMsg->pData, pMsg->len, &wlRsp)) != TSDB_CODE_SUCCESS) {
×
847
    TAOS_CHECK_GOTO(code, &lino, _error);
×
848
  }
849

850
  pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(char *));
×
851
  if (pWhiteLists == NULL) {
×
852
    code = terrno;
×
853
    TAOS_CHECK_GOTO(code, &lino, _error);
×
854
  }
855

856
  int32_t numWhiteLists = 0;
×
857
  for (int32_t i = 0; i < wlRsp.numWhiteLists; i++) {
×
858
    SDateTimeWhiteListItem *item = &wlRsp.pWhiteLists[i];
×
859

860
    char *p = taosMemCalloc(1, 128);
×
861
    if (p == NULL) {
×
862
      code = terrno;
×
863
      TAOS_CHECK_GOTO(code, &lino, _error);
×
864
    }
865

866
    int duration = item->duration / 60;
×
867

868
    if (item->absolute) {
×
869
      struct STm tm;
×
870
      (void)taosTs2Tm(item->start, TSDB_TIME_PRECISION_SECONDS, &tm, NULL);
×
871
      snprintf(p, 128, "%c %04d-%02d-%02d %02d:%02d %d", item->neg ? '-' : '+', tm.tm.tm_year + 1900, tm.tm.tm_mon + 1,
×
872
                          tm.tm.tm_mday, tm.tm.tm_hour, tm.tm.tm_min, duration);
873
    } else {
874
      int day = item->start / 86400;
×
875
      int hour = (item->start % 86400) / 3600;
×
876
      int minute = (item->start % 3600) / 60;
×
877
      snprintf(p, 128, "%c %s %02d:%02d %d", item->neg ? '-' : '+', weekdays[day], hour, minute, duration);
×
878
    }
879
    pWhiteLists[numWhiteLists++] = p;
×
880
  }
881

882
  pInfo->userCbFn(pInfo->userParam, code, taos, numWhiteLists, pWhiteLists);
×
883
_error:
×
884
  if (pWhiteLists != NULL) {
×
885
    for (int32_t i = 0; i < numWhiteLists; i++) {
×
886
      taosMemFree(pWhiteLists[i]);
×
887
    }
888
    taosMemoryFree(pWhiteLists);
×
889
  }
890
  taosMemoryFree(pMsg->pData);
×
891
  taosMemoryFree(pMsg->pEpSet);
×
892
  taosMemoryFree(pInfo);
×
893
  tFreeSUserDateTimeWhiteList(&wlRsp);
×
894
  return code;
×
895
}
896

897
void taos_fetch_datetime_whitelist_a(TAOS *taos, __taos_async_datetime_whitelist_fn_t fp, void *param) {
×
898
  if (NULL == taos) {
×
899
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
900
    return;
×
901
  }
902
  int64_t connId = *(int64_t *)taos;
×
903

904
  STscObj *pTsc = acquireTscObj(connId);
×
905
  if (NULL == pTsc) {
×
906
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
907
    return;
×
908
  }
909

910
  SGetUserWhiteListReq req;
×
911
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
912
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
913
  if (msgLen < 0) {
×
914
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
915
    releaseTscObj(connId);
×
916
    return;
×
917
  }
918

919
  void *pReq = taosMemoryMalloc(msgLen);
×
920
  if (pReq == NULL) {
×
921
    fp(param, terrno, taos, 0, NULL);
×
922
    releaseTscObj(connId);
×
923
    return;
×
924
  }
925

926
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
927
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
928
    taosMemoryFree(pReq);
×
929
    releaseTscObj(connId);
×
930
    return;
×
931
  }
932

933
  SFetchDateTimeWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchDateTimeWhiteListInfo));
×
934
  if (pParam == NULL) {
×
935
    fp(param, terrno, taos, 0, NULL);
×
936
    taosMemoryFree(pReq);
×
937
    releaseTscObj(connId);
×
938
    return;
×
939
  }
940

941
  pParam->connId = connId;
×
942
  pParam->userCbFn = fp;
×
943
  pParam->userParam = param;
×
944

945
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
946
  if (pSendInfo == NULL) {
×
947
    fp(param, terrno, taos, 0, NULL);
×
948
    taosMemoryFree(pParam);
×
949
    taosMemoryFree(pReq);
×
950
    releaseTscObj(connId);
×
951
    return;
×
952
  }
953

954
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
955
  pSendInfo->requestId = generateRequestId();
×
956
  pSendInfo->requestObjRefId = 0;
×
957
  pSendInfo->param = pParam;
×
958
  pSendInfo->fp = fetchDateTimeWhiteListCallbackFn;
×
959
  pSendInfo->msgType = TDMT_MND_GET_USER_DATETIME_WHITELIST;
×
960

961
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
962
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
963
    tscWarn("failed to async send msg to server");
×
964
  }
965
  releaseTscObj(connId);
×
966
  return;
×
967
}
968

969
void taos_close_internal(void *taos) {
87,393,856✔
970
  if (taos == NULL) {
87,393,856✔
971
    return;
395✔
972
  }
973
  int32_t code = 0;
87,393,461✔
974

975
  STscObj *pTscObj = (STscObj *)taos;
87,393,461✔
976
  tscDebug("conn:0x%" PRIx64 ", try to close connection, numOfReq:%d", pTscObj->id, pTscObj->numOfReqs);
87,393,461✔
977

978
  SSessParam para = {.type = SESSION_PER_USER, .value = -1, .noCheck = 1};
87,393,461✔
979

980
  code = tscUpdateSessMetric(pTscObj, &para);
87,393,461✔
981
  if (code != TSDB_CODE_SUCCESS) {
87,394,915✔
982
    tscWarn("conn:0x%" PRIx64 ", failed to update user:%s metric when close connection, code:%d", pTscObj->id,
×
983
            pTscObj->user, code);
984
  }
985

986
  code = tscUnrefSessMetric(pTscObj);
87,394,915✔
987
  if (TSDB_CODE_SUCCESS != taosRemoveRef(clientConnRefPool, pTscObj->id)) {
87,395,455✔
988
    tscError("conn:0x%" PRIx64 ", failed to remove ref from conn pool", pTscObj->id);
×
989
  }
990
}
991

992
void taos_close(TAOS *taos) {
87,238,331✔
993
  if (taos == NULL) {
87,238,331✔
994
    return;
380✔
995
  }
996

997
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
87,237,951✔
998
  if (NULL == pObj) {
87,239,164✔
999
    taosMemoryFree(taos);
×
1000
    return;
×
1001
  }
1002

1003
  taos_close_internal(pObj);
87,239,164✔
1004
  releaseTscObj(*(int64_t *)taos);
87,238,705✔
1005
  taosMemoryFree(taos);
87,236,989✔
1006
}
1007

1008
int taos_errno(TAOS_RES *res) {
1,540,014,747✔
1009
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,540,014,747✔
1010
    return terrno;
9,811,136✔
1011
  }
1012

1013
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,530,207,159✔
1014
    return 0;
15,840,266✔
1015
  }
1016

1017
  return ((SRequestObj *)res)->code;
1,514,352,581✔
1018
}
1019

1020
const char *taos_errstr(TAOS_RES *res) {
114,123,131✔
1021
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
114,123,131✔
1022
    if (*(taosGetErrMsg()) == 0) {
9,851,717✔
1023
      return (const char *)tstrerror(terrno);
9,852,293✔
1024
    } else {
1025
      (void)snprintf(taosGetErrMsgReturn(), ERR_MSG_LEN, "%s", taosGetErrMsg());
39✔
1026
      return (const char *)taosGetErrMsgReturn();
39✔
1027
    }
1028
  }
1029

1030
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
104,271,643✔
UNCOV
1031
    return "success";
×
1032
  }
1033

1034
  SRequestObj *pRequest = (SRequestObj *)res;
104,271,729✔
1035
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
104,271,729✔
1036
    return pRequest->msgBuf;
30,867,741✔
1037
  } else {
1038
    return (const char *)tstrerror(pRequest->code);
73,403,988✔
1039
  }
1040
}
1041

1042
void taos_free_result(TAOS_RES *res) {
1,193,045,419✔
1043
  if (NULL == res) {
1,193,045,419✔
1044
    return;
14,096,228✔
1045
  }
1046

1047
  tscTrace("res:%p, will be freed", res);
1,178,949,191✔
1048

1049
  if (TD_RES_QUERY(res)) {
1,178,954,478✔
1050
    SRequestObj *pRequest = (SRequestObj *)res;
1,130,710,912✔
1051
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
1,130,710,912✔
1052
    destroyRequest(pRequest);
1,130,711,089✔
1053
    return;
1,130,696,078✔
1054
  }
1055

1056
  SMqRspObj *pRsp = (SMqRspObj *)res;
48,246,733✔
1057
  if (TD_RES_TMQ(res)) {
48,246,733✔
1058
    tDeleteMqDataRsp(&pRsp->dataRsp);
48,166,195✔
1059
    doFreeReqResultInfo(&pRsp->resInfo);
48,164,683✔
1060
  } else if (TD_RES_TMQ_METADATA(res)) {
98,093✔
1061
    tDeleteSTaosxRsp(&pRsp->dataRsp);
4,648✔
1062
    doFreeReqResultInfo(&pRsp->resInfo);
4,648✔
1063
  } else if (TD_RES_TMQ_META(res)) {
93,445✔
1064
    tDeleteMqMetaRsp(&pRsp->metaRsp);
85,018✔
1065
  } else if (TD_RES_TMQ_BATCH_META(res)) {
8,427✔
1066
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
8,427✔
1067
  } else if (TD_RES_TMQ_RAW(res)) {
×
1068
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
1069
  }
1070
  taosMemoryFree(pRsp);
48,262,776✔
1071
}
1072

1073
void taos_kill_query(TAOS *taos) {
282✔
1074
  if (NULL == taos) {
282✔
1075
    return;
282✔
1076
  }
1077

UNCOV
1078
  int64_t  rid = *(int64_t *)taos;
×
UNCOV
1079
  STscObj *pTscObj = acquireTscObj(rid);
×
UNCOV
1080
  if (pTscObj) {
×
UNCOV
1081
    stopAllRequests(pTscObj->pRequests);
×
1082
  }
UNCOV
1083
  releaseTscObj(rid);
×
1084
}
1085

1086
int taos_field_count(TAOS_RES *res) {
2,147,483,647✔
1087
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1088
    return 0;
×
1089
  }
1090

1091
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
1092
  return pResInfo->numOfCols;
2,147,483,647✔
1093
}
1094

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

1097
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
2,147,483,647✔
1098
  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✔
1099
    return NULL;
3,706,096✔
1100
  }
1101

1102
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
1103
  return pResInfo->userFields;
2,147,483,647✔
1104
}
1105

1106
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
1,123,003,270✔
1107
TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqid) {
227✔
1108
  return taosQueryImplWithReqid(taos, sql, false, reqid);
227✔
1109
}
1110

1111
TAOS_FIELD_E *taos_fetch_fields_e(TAOS_RES *res) {
84✔
1112
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
84✔
1113
    return NULL;
×
1114
  }
1115
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
84✔
1116
  return pResInfo->fields;
84✔
1117
}
1118

1119
TAOS_ROW taos_fetch_row(TAOS_RES *res) {
2,147,483,647✔
1120
  if (res == NULL) {
2,147,483,647✔
1121
    return NULL;
×
1122
  }
1123

1124
  if (TD_RES_QUERY(res)) {
2,147,483,647✔
1125
    SRequestObj *pRequest = (SRequestObj *)res;
1,418,651,735✔
1126
    if (pRequest->killed) {
1,418,651,735✔
1127
      tscInfo("query has been killed, can not fetch more row.");
×
1128
      pRequest->code = TSDB_CODE_TSC_QUERY_KILLED;
×
1129
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
×
1130
      return NULL;
×
1131
    }
1132

1133
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
1,418,652,644✔
1134
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
1,418,651,717✔
1135
      if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT) {
870✔
1136
        CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
×
1137
      }
1138
      return NULL;
661✔
1139
    }
1140

1141
    if (pRequest->inCallback) {
1,418,629,191✔
1142
      tscError("can not call taos_fetch_row before query callback ends.");
198✔
1143
      terrno = TSDB_CODE_TSC_INVALID_OPERATION;
198✔
1144
      return NULL;
198✔
1145
    }
1146

1147
    return doAsyncFetchRows(pRequest, true, true);
1,418,627,522✔
1148
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,147,483,647✔
1149
    SMqRspObj      *msg = ((SMqRspObj *)res);
2,147,483,647✔
1150
    SReqResultInfo *pResultInfo = NULL;
2,147,483,647✔
1151
    if (msg->resIter == -1) {
2,147,483,647✔
1152
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
32,267,771✔
1153
        return NULL;
×
1154
      }
1155
    } else {
1156
      pResultInfo = tmqGetCurResInfo(res);
2,147,483,647✔
1157
    }
1158

1159
    if (pResultInfo->current < pResultInfo->numOfRows) {
2,147,483,647✔
1160
      doSetOneRowPtr(pResultInfo);
2,147,483,647✔
1161
      pResultInfo->current += 1;
2,147,483,647✔
1162
      return pResultInfo->row;
2,147,483,647✔
1163
    } else {
1164
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
61,114,108✔
1165
        return NULL;
32,266,211✔
1166
      }
1167

1168
      doSetOneRowPtr(pResultInfo);
28,846,813✔
1169
      pResultInfo->current += 1;
28,848,325✔
1170
      return pResultInfo->row;
28,847,947✔
1171
    }
1172
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1173
    return NULL;
×
1174
  } else {
1175
    tscError("invalid result passed to taos_fetch_row");
×
1176
    terrno = TSDB_CODE_TMQ_INVALID_DATA;
×
1177
    return NULL;
×
1178
  }
1179
}
1180

1181
int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
2,147,483,647✔
1182
  return taos_print_row_with_size(str, INT32_MAX, row, fields, num_fields);
2,147,483,647✔
1183
}
1184
int taos_print_row_with_size(char *str, uint32_t size, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
2,147,483,647✔
1185
  int32_t len = 0;
2,147,483,647✔
1186
  for (int i = 0; i < num_fields; ++i) {
2,147,483,647✔
1187
    if (i > 0 && len < size - 1) {
2,147,483,647✔
1188
      str[len++] = ' ';
2,147,483,647✔
1189
    }
1190

1191
    if (row[i] == NULL) {
2,147,483,647✔
1192
      len += snprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
150,338,178✔
1193
      continue;
150,338,178✔
1194
    }
1195

1196
    switch (fields[i].type) {
2,147,483,647✔
1197
      case TSDB_DATA_TYPE_TINYINT:
8,694,643✔
1198
        len += snprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
8,694,643✔
1199
        break;
8,707,229✔
1200

1201
      case TSDB_DATA_TYPE_UTINYINT:
48,389✔
1202
        len += snprintf(str + len, size - len, "%u", *((uint8_t *)row[i]));
48,389✔
1203
        break;
48,389✔
1204

1205
      case TSDB_DATA_TYPE_SMALLINT:
48,585✔
1206
        len += snprintf(str + len, size - len, "%d", *((int16_t *)row[i]));
48,585✔
1207
        break;
48,585✔
1208

1209
      case TSDB_DATA_TYPE_USMALLINT:
48,356✔
1210
        len += snprintf(str + len, size - len, "%u", *((uint16_t *)row[i]));
48,356✔
1211
        break;
48,356✔
1212

1213
      case TSDB_DATA_TYPE_INT:
2,147,483,647✔
1214
        len += snprintf(str + len, size - len, "%d", *((int32_t *)row[i]));
2,147,483,647✔
1215
        break;
2,147,483,647✔
1216

1217
      case TSDB_DATA_TYPE_UINT:
48,356✔
1218
        len += snprintf(str + len, size - len, "%u", *((uint32_t *)row[i]));
48,356✔
1219
        break;
48,356✔
1220

1221
      case TSDB_DATA_TYPE_BIGINT:
2,147,483,647✔
1222
        len += snprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
2,147,483,647✔
1223
        break;
2,147,483,647✔
1224

1225
      case TSDB_DATA_TYPE_UBIGINT:
48,378✔
1226
        len += snprintf(str + len, size - len, "%" PRIu64, *((uint64_t *)row[i]));
48,378✔
1227
        break;
48,378✔
1228

1229
      case TSDB_DATA_TYPE_FLOAT: {
17,327,569✔
1230
        float fv = 0;
17,327,569✔
1231
        fv = GET_FLOAT_VAL(row[i]);
17,327,569✔
1232
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
17,313,939✔
1233
      } break;
17,285,345✔
1234

1235
      case TSDB_DATA_TYPE_DOUBLE: {
2,147,483,647✔
1236
        double dv = 0;
2,147,483,647✔
1237
        dv = GET_DOUBLE_VAL(row[i]);
2,147,483,647✔
1238
        len += snprintf(str + len, size - len, "%.*g", DBL_DIG, dv);
2,147,483,647✔
1239
      } break;
2,147,483,647✔
1240

1241
      case TSDB_DATA_TYPE_VARBINARY: {
48,367✔
1242
        void    *data = NULL;
48,367✔
1243
        uint32_t tmp = 0;
48,367✔
1244
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
48,367✔
1245
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
48,367✔
1246
          break;
×
1247
        }
1248
        uint32_t copyLen = TMIN(size - len - 1, tmp);
48,334✔
1249
        (void)memcpy(str + len, data, copyLen);
48,334✔
1250
        len += copyLen;
48,334✔
1251
        taosMemoryFree(data);
48,334✔
1252
      } break;
48,345✔
1253
      case TSDB_DATA_TYPE_BINARY:
2,147,483,647✔
1254
      case TSDB_DATA_TYPE_NCHAR:
1255
      case TSDB_DATA_TYPE_GEOMETRY: {
1256
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
2,147,483,647✔
1257
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
2,147,483,647✔
1258
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
1,984,934,098✔
1259
          if (charLen > fields[i].bytes || charLen < 0) {
2,147,483,647✔
1260
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
×
1261
            break;
×
1262
          }
1263
        } else {
1264
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
1,984,883,359✔
1265
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
253✔
1266
            break;
×
1267
          }
1268
        }
1269

1270
        uint32_t copyLen = TMIN(size - len - 1, charLen);
2,147,483,647✔
1271
        (void)memcpy(str + len, row[i], copyLen);
2,147,483,647✔
1272
        len += copyLen;
2,147,483,647✔
1273
      } break;
2,147,483,647✔
1274
      case TSDB_DATA_TYPE_BLOB:
×
1275
      case TSDB_DATA_TYPE_MEDIUMBLOB: {
1276
        void    *data = NULL;
×
1277
        uint32_t tmp = 0;
×
1278
        int32_t  charLen = blobDataLen((char *)row[i] - BLOBSTR_HEADER_SIZE);
×
1279
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
×
1280
          break;
×
1281
        }
1282

1283
        uint32_t copyLen = TMIN(size - len - 1, tmp);
×
1284
        (void)memcpy(str + len, data, copyLen);
×
1285
        len += copyLen;
×
1286

1287
        taosMemoryFree(data);
×
1288
      } break;
×
1289

1290
      case TSDB_DATA_TYPE_TIMESTAMP:
2,147,483,647✔
1291
        len += snprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
2,147,483,647✔
1292
        break;
2,147,483,647✔
1293

1294
      case TSDB_DATA_TYPE_BOOL:
48,552✔
1295
        len += snprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
48,552✔
1296
        break;
48,552✔
1297
      case TSDB_DATA_TYPE_DECIMAL64:
×
1298
      case TSDB_DATA_TYPE_DECIMAL: {
1299
        uint32_t decimalLen = strlen(row[i]);
×
1300
        uint32_t copyLen = TMIN(size - len - 1, decimalLen);
×
1301
        (void)memcpy(str + len, row[i], copyLen);
×
1302
        len += copyLen;
×
1303
      } break;
×
1304
      default:
×
1305
        break;
×
1306
    }
1307

1308
    if (len >= size - 1) {
2,147,483,647✔
1309
      break;
×
1310
    }
1311
  }
1312
  if (len < size) {
2,147,483,647✔
1313
    str[len] = 0;
2,147,483,647✔
1314
  }
1315

1316
  return len;
2,147,483,647✔
1317
}
1318

1319
int *taos_fetch_lengths(TAOS_RES *res) {
2,147,483,647✔
1320
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1321
    return NULL;
×
1322
  }
1323

1324
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
1325
  return pResInfo->length;
2,147,483,647✔
1326
}
1327

1328
TAOS_ROW *taos_result_block(TAOS_RES *res) {
×
1329
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1330
    terrno = TSDB_CODE_INVALID_PARA;
×
1331
    return NULL;
×
1332
  }
1333

1334
  if (taos_is_update_query(res)) {
×
1335
    return NULL;
×
1336
  }
1337

1338
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
1339
  return &pResInfo->row;
×
1340
}
1341

1342
// todo intergrate with tDataTypes
1343
const char *taos_data_type(int type) {
×
1344
  switch (type) {
×
1345
    case TSDB_DATA_TYPE_NULL:
×
1346
      return "TSDB_DATA_TYPE_NULL";
×
1347
    case TSDB_DATA_TYPE_BOOL:
×
1348
      return "TSDB_DATA_TYPE_BOOL";
×
1349
    case TSDB_DATA_TYPE_TINYINT:
×
1350
      return "TSDB_DATA_TYPE_TINYINT";
×
1351
    case TSDB_DATA_TYPE_SMALLINT:
×
1352
      return "TSDB_DATA_TYPE_SMALLINT";
×
1353
    case TSDB_DATA_TYPE_INT:
×
1354
      return "TSDB_DATA_TYPE_INT";
×
1355
    case TSDB_DATA_TYPE_BIGINT:
×
1356
      return "TSDB_DATA_TYPE_BIGINT";
×
1357
    case TSDB_DATA_TYPE_FLOAT:
×
1358
      return "TSDB_DATA_TYPE_FLOAT";
×
1359
    case TSDB_DATA_TYPE_DOUBLE:
×
1360
      return "TSDB_DATA_TYPE_DOUBLE";
×
1361
    case TSDB_DATA_TYPE_VARCHAR:
×
1362
      return "TSDB_DATA_TYPE_VARCHAR";
×
1363
      //    case TSDB_DATA_TYPE_BINARY:          return "TSDB_DATA_TYPE_VARCHAR";
1364
    case TSDB_DATA_TYPE_TIMESTAMP:
×
1365
      return "TSDB_DATA_TYPE_TIMESTAMP";
×
1366
    case TSDB_DATA_TYPE_NCHAR:
×
1367
      return "TSDB_DATA_TYPE_NCHAR";
×
1368
    case TSDB_DATA_TYPE_JSON:
×
1369
      return "TSDB_DATA_TYPE_JSON";
×
1370
    case TSDB_DATA_TYPE_GEOMETRY:
×
1371
      return "TSDB_DATA_TYPE_GEOMETRY";
×
1372
    case TSDB_DATA_TYPE_UTINYINT:
×
1373
      return "TSDB_DATA_TYPE_UTINYINT";
×
1374
    case TSDB_DATA_TYPE_USMALLINT:
×
1375
      return "TSDB_DATA_TYPE_USMALLINT";
×
1376
    case TSDB_DATA_TYPE_UINT:
×
1377
      return "TSDB_DATA_TYPE_UINT";
×
1378
    case TSDB_DATA_TYPE_UBIGINT:
×
1379
      return "TSDB_DATA_TYPE_UBIGINT";
×
1380
    case TSDB_DATA_TYPE_VARBINARY:
×
1381
      return "TSDB_DATA_TYPE_VARBINARY";
×
1382
    case TSDB_DATA_TYPE_DECIMAL:
×
1383
      return "TSDB_DATA_TYPE_DECIMAL";
×
1384
    case TSDB_DATA_TYPE_BLOB:
×
1385
      return "TSDB_DATA_TYPE_BLOB";
×
1386
    case TSDB_DATA_TYPE_MEDIUMBLOB:
×
1387
      return "TSDB_DATA_TYPE_MEDIUMBLOB";
×
1388
    default:
×
1389
      return "UNKNOWN";
×
1390
  }
1391
}
1392

1393
const char *taos_get_client_info() { return td_version; }
1,056,776✔
1394

1395
// return int32_t
1396
int taos_affected_rows(TAOS_RES *res) {
625,992,841✔
1397
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
625,992,841✔
1398
      TD_RES_TMQ_BATCH_META(res)) {
625,996,741✔
1399
    return 0;
×
1400
  }
1401

1402
  SRequestObj    *pRequest = (SRequestObj *)res;
625,995,614✔
1403
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
625,995,614✔
1404
  return (int)pResInfo->numOfRows;
625,995,642✔
1405
}
1406

1407
// return int64_t
1408
int64_t taos_affected_rows64(TAOS_RES *res) {
1,454,590✔
1409
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
1,454,590✔
1410
      TD_RES_TMQ_BATCH_META(res)) {
1,454,590✔
1411
    return 0;
×
1412
  }
1413

1414
  SRequestObj    *pRequest = (SRequestObj *)res;
1,454,590✔
1415
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
1,454,590✔
1416
  return pResInfo->numOfRows;
1,454,590✔
1417
}
1418

1419
int taos_result_precision(TAOS_RES *res) {
2,147,483,647✔
1420
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1421
    return TSDB_TIME_PRECISION_MILLI;
×
1422
  }
1423

1424
  if (TD_RES_QUERY(res)) {
2,147,483,647✔
1425
    SRequestObj *pRequest = (SRequestObj *)res;
254,855,846✔
1426
    return pRequest->body.resInfo.precision;
254,855,846✔
1427
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,147,483,647✔
1428
    SReqResultInfo *info = tmqGetCurResInfo(res);
2,147,483,647✔
1429
    return info->precision;
2,147,483,647✔
1430
  }
1431
  return TSDB_TIME_PRECISION_MILLI;
×
1432
}
1433

1434
int taos_select_db(TAOS *taos, const char *db) {
40,192✔
1435
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
40,192✔
1436
  if (pObj == NULL) {
40,192✔
1437
    releaseTscObj(*(int64_t *)taos);
×
1438
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1439
    return TSDB_CODE_TSC_DISCONNECTED;
×
1440
  }
1441

1442
  if (db == NULL || strlen(db) == 0) {
40,192✔
1443
    releaseTscObj(*(int64_t *)taos);
×
1444
    tscError("invalid parameter for %s", db == NULL ? "db is NULL" : "db is empty");
×
1445
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1446
    return terrno;
×
1447
  }
1448

1449
  char sql[256] = {0};
40,192✔
1450
  (void)snprintf(sql, tListLen(sql), "use %s", db);
40,192✔
1451

1452
  TAOS_RES *pRequest = taos_query(taos, sql);
40,192✔
1453
  int32_t   code = taos_errno(pRequest);
40,164✔
1454

1455
  taos_free_result(pRequest);
40,164✔
1456
  releaseTscObj(*(int64_t *)taos);
40,192✔
1457
  return code;
40,192✔
1458
}
1459

1460
void taos_stop_query(TAOS_RES *res) {
1,219,158,754✔
1461
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
1,219,158,754✔
1462
      TD_RES_TMQ_BATCH_META(res)) {
1,219,192,565✔
1463
    return;
×
1464
  }
1465

1466
  stopAllQueries((SRequestObj *)res);
1,219,168,059✔
1467
}
1468

1469
bool taos_is_null(TAOS_RES *res, int32_t row, int32_t col) {
×
1470
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1471
    return true;
×
1472
  }
1473
  SReqResultInfo *pResultInfo = tscGetCurResInfo(res);
×
1474
  if (col >= pResultInfo->numOfCols || col < 0 || row >= pResultInfo->numOfRows || row < 0) {
×
1475
    return true;
×
1476
  }
1477

1478
  SResultColumn *pCol = &pResultInfo->pCol[col];
×
1479
  if (IS_VAR_DATA_TYPE(pResultInfo->fields[col].type)) {
×
1480
    return (pCol->offset[row] == -1);
×
1481
  } else {
1482
    return colDataIsNull_f(pCol, row);
×
1483
  }
1484
}
1485

1486
bool taos_is_update_query(TAOS_RES *res) { return taos_num_fields(res) == 0; }
277,464✔
1487

1488
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
348,153,806✔
1489
  int32_t numOfRows = 0;
348,153,806✔
1490
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
348,153,806✔
1491
  return numOfRows;
348,153,260✔
1492
}
1493

1494
int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) {
348,153,806✔
1495
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
348,153,806✔
1496
    return 0;
×
1497
  }
1498

1499
  if (TD_RES_QUERY(res)) {
348,153,806✔
1500
    SRequestObj *pRequest = (SRequestObj *)res;
316,485,898✔
1501
    (*rows) = NULL;
316,485,898✔
1502
    (*numOfRows) = 0;
316,485,898✔
1503

1504
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
316,485,898✔
1505
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
316,113,724✔
1506
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
2,995,266✔
1507
      return pRequest->code;
1,497,633✔
1508
    }
1509

1510
    (void)doAsyncFetchRows(pRequest, false, true);
314,988,265✔
1511

1512
    // TODO refactor
1513
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
314,986,841✔
1514
    pResultInfo->current = pResultInfo->numOfRows;
314,988,265✔
1515

1516
    (*rows) = pResultInfo->row;
314,988,265✔
1517
    (*numOfRows) = pResultInfo->numOfRows;
314,988,265✔
1518
    return pRequest->code;
314,988,265✔
1519
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
31,667,908✔
1520
    SReqResultInfo *pResultInfo = NULL;
31,667,908✔
1521
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
31,667,908✔
1522
    if (code != 0) return code;
31,667,908✔
1523

1524
    pResultInfo->current = pResultInfo->numOfRows;
15,835,595✔
1525
    (*rows) = pResultInfo->row;
15,835,595✔
1526
    (*numOfRows) = pResultInfo->numOfRows;
15,835,595✔
1527
    return 0;
15,835,595✔
1528
  } else {
1529
    tscError("taos_fetch_block_s invalid res type");
×
1530
    return TSDB_CODE_TMQ_INVALID_DATA;
×
1531
  }
1532
}
1533

1534
int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) {
5,458✔
1535
  *numOfRows = 0;
5,458✔
1536
  *pData = NULL;
5,458✔
1537

1538
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
5,458✔
1539
    return 0;
×
1540
  }
1541

1542
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
5,458✔
1543
    SReqResultInfo *pResultInfo = NULL;
1,920✔
1544
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
1,920✔
1545
    if (code != 0) {
1,920✔
1546
      (*numOfRows) = 0;
960✔
1547
      return 0;
960✔
1548
    }
1549

1550
    pResultInfo->current = pResultInfo->numOfRows;
960✔
1551
    (*numOfRows) = pResultInfo->numOfRows;
960✔
1552
    (*pData) = (void *)pResultInfo->pData;
960✔
1553
    return 0;
960✔
1554
  }
1555

1556
  SRequestObj *pRequest = (SRequestObj *)res;
3,538✔
1557

1558
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
3,538✔
1559
      pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
3,538✔
1560
    if (pRequest->code == TSDB_SQL_RETRIEVE_EMPTY_RESULT) {
×
1561
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
×
1562
    }
1563
    return pRequest->code;
×
1564
  }
1565

1566
  (void)doAsyncFetchRows(pRequest, false, false);
3,538✔
1567

1568
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
3,538✔
1569

1570
  pResultInfo->current = pResultInfo->numOfRows;
3,538✔
1571
  (*numOfRows) = pResultInfo->numOfRows;
3,538✔
1572
  (*pData) = (void *)pResultInfo->pData;
3,538✔
1573

1574
  return pRequest->code;
3,538✔
1575
}
1576

1577
int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) {
256,189,965✔
1578
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
256,189,965✔
1579
    return NULL;
×
1580
  }
1581

1582
  int32_t numOfFields = taos_num_fields(res);
256,189,965✔
1583
  if (columnIndex < 0 || columnIndex >= numOfFields || numOfFields == 0) {
256,189,965✔
1584
    return NULL;
×
1585
  }
1586

1587
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
256,189,965✔
1588
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
256,189,965✔
1589
  if (!IS_VAR_DATA_TYPE(pField->type)) {
256,189,965✔
1590
    return NULL;
×
1591
  }
1592

1593
  return pResInfo->pCol[columnIndex].offset;
256,189,965✔
1594
}
1595

1596
int taos_is_null_by_column(TAOS_RES *res, int columnIndex, bool result[], int *rows) {
873,486,626✔
1597
  if (res == NULL || result == NULL || rows == NULL || *rows <= 0 || columnIndex < 0 || TD_RES_TMQ_META(res) ||
873,486,626✔
1598
      TD_RES_TMQ_RAW(res) || TD_RES_TMQ_BATCH_META(res)) {
873,486,626✔
1599
    return TSDB_CODE_INVALID_PARA;
×
1600
  }
1601

1602
  int32_t numOfFields = taos_num_fields(res);
873,483,981✔
1603
  if (columnIndex >= numOfFields || numOfFields == 0) {
873,486,626✔
1604
    return TSDB_CODE_INVALID_PARA;
×
1605
  }
1606

1607
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
873,486,626✔
1608
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
873,486,626✔
1609
  SResultColumn  *pCol = &pResInfo->pCol[columnIndex];
873,486,626✔
1610

1611
  if (*rows > pResInfo->numOfRows) {
873,486,626✔
1612
    *rows = pResInfo->numOfRows;
×
1613
  }
1614
  if (IS_VAR_DATA_TYPE(pField->type)) {
873,486,626✔
1615
    for (int i = 0; i < *rows; i++) {
×
1616
      if (pCol->offset[i] == -1) {
×
1617
        result[i] = true;
×
1618
      } else {
1619
        result[i] = false;
×
1620
      }
1621
    }
1622
  } else {
1623
    for (int i = 0; i < *rows; i++) {
2,147,483,647✔
1624
      if (colDataIsNull_f(pCol, i)) {
2,147,483,647✔
1625
        result[i] = true;
2,147,483,647✔
1626
      } else {
1627
        result[i] = false;
2,147,483,647✔
1628
      }
1629
    }
1630
  }
1631
  return 0;
873,486,626✔
1632
}
1633

1634
int taos_validate_sql(TAOS *taos, const char *sql) {
×
1635
  TAOS_RES *pObj = taosQueryImpl(taos, sql, true, TD_REQ_FROM_APP);
×
1636

1637
  int code = taos_errno(pObj);
×
1638

1639
  taos_free_result(pObj);
×
1640
  return code;
×
1641
}
1642

1643
void taos_reset_current_db(TAOS *taos) {
×
1644
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
1645
  if (pTscObj == NULL) {
×
1646
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1647
    return;
×
1648
  }
1649

1650
  resetConnectDB(pTscObj);
×
1651

1652
  releaseTscObj(*(int64_t *)taos);
×
1653
}
1654

1655
const char *taos_get_server_info(TAOS *taos) {
3,131✔
1656
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
3,131✔
1657
  if (pTscObj == NULL) {
3,131✔
1658
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1659
    return NULL;
×
1660
  }
1661

1662
  releaseTscObj(*(int64_t *)taos);
3,131✔
1663

1664
  return pTscObj->sDetailVer;
3,131✔
1665
}
1666

1667
int taos_get_current_db(TAOS *taos, char *database, int len, int *required) {
2,528✔
1668
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
2,528✔
1669
  if (pTscObj == NULL) {
2,528✔
1670
    return TSDB_CODE_TSC_DISCONNECTED;
×
1671
  }
1672

1673
  int code = TSDB_CODE_SUCCESS;
2,528✔
1674
  (void)taosThreadMutexLock(&pTscObj->mutex);
2,528✔
1675
  if (database == NULL || len <= 0) {
2,528✔
1676
    if (required != NULL) *required = strlen(pTscObj->db) + 1;
1,264✔
1677
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
1,264✔
1678
  } else if (len < strlen(pTscObj->db) + 1) {
1,264✔
1679
    tstrncpy(database, pTscObj->db, len);
632✔
1680
    if (required) *required = strlen(pTscObj->db) + 1;
632✔
1681
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
632✔
1682
  } else {
1683
    tstrncpy(database, pTscObj->db, len);
632✔
1684
    code = 0;
632✔
1685
  }
1686
_return:
2,528✔
1687
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
2,528✔
1688
  releaseTscObj(*(int64_t *)taos);
2,528✔
1689
  return code;
2,528✔
1690
}
1691

1692
// buffer is allocated by caller, len is in/out parameter, input is buffer length, output is actual length.
1693
// because this is a general purpose api, buffer is not null-terminated string even for string info, and
1694
// the return length is the actual length of the info, not including null-terminator.
1695
int taos_get_connection_info(TAOS *taos, TSDB_CONNECTION_INFO info, char *buffer, int *len) {
196✔
1696
  if (len == NULL) {
196✔
1697
    return TSDB_CODE_INVALID_PARA;
×
1698
  }
1699

1700
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
196✔
1701
  if (pTscObj == NULL) {
196✔
1702
    return TSDB_CODE_TSC_DISCONNECTED;
×
1703
  }
1704

1705
  int code = TSDB_CODE_SUCCESS;
196✔
1706
  (void)taosThreadMutexLock(&pTscObj->mutex);
196✔
1707

1708
  switch (info) {
196✔
1709
    case TSDB_CONNECTION_INFO_USER: {
98✔
1710
      int userLen = strlen(pTscObj->user);
98✔
1711
      if (buffer == NULL || *len < userLen) {
98✔
1712
        *len = userLen;
×
1713
        TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1714
      } else {
1715
        *len = userLen;
98✔
1716
        (void)memcpy(buffer, pTscObj->user, userLen);
98✔
1717
      }
1718
      break;
98✔
1719
    }
1720

1721
    case TSDB_CONNECTION_INFO_TOKEN: {
98✔
1722
      int tokenLen = strlen(pTscObj->tokenName);
98✔
1723
      if (tokenLen == 0) {
98✔
1724
        *len = 0;
×
1725
      } else if (buffer == NULL || *len < tokenLen) {
98✔
1726
        *len = tokenLen;
×
1727
        TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1728
      } else {
1729
        *len = tokenLen;
98✔
1730
        (void)memcpy(buffer, pTscObj->tokenName, tokenLen);
98✔
1731
      }
1732
      break;
98✔
1733
    }
1734

1735
    default:
×
1736
      TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1737
  }
1738

1739
_return:
×
1740
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
196✔
1741
  releaseTscObj(*(int64_t *)taos);
196✔
1742
  return code;
196✔
1743
}
1744

1745
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
2,147,483,647✔
1746
  if (NULL == pWrapper) {
2,147,483,647✔
1747
    return;
1,221,663,039✔
1748
  }
1749
  destoryCatalogReq(pWrapper->pCatalogReq);
1,125,568,921✔
1750
  taosMemoryFree(pWrapper->pCatalogReq);
1,125,653,320✔
1751
  qDestroyParseContext(pWrapper->pParseCtx);
1,125,643,852✔
1752
  taosMemoryFree(pWrapper);
1,125,607,061✔
1753
}
1754

1755
void destroyCtxInRequest(SRequestObj *pRequest) {
2,893,378✔
1756
  schedulerFreeJob(&pRequest->body.queryJob, 0);
2,893,378✔
1757
  qDestroyQuery(pRequest->pQuery);
2,893,378✔
1758
  pRequest->pQuery = NULL;
2,893,378✔
1759
  destorySqlCallbackWrapper(pRequest->pWrapper);
2,893,378✔
1760
  pRequest->pWrapper = NULL;
2,893,378✔
1761
}
2,893,378✔
1762

1763
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
449,338,121✔
1764
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
449,338,121✔
1765
  SRequestObj         *pRequest = pWrapper->pRequest;
449,338,121✔
1766
  SQuery              *pQuery = pRequest->pQuery;
449,338,121✔
1767

1768
  qDebug("req:0x%" PRIx64 ", start to semantic analysis, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
449,338,124✔
1769

1770
  int64_t analyseStart = taosGetTimestampUs();
449,339,012✔
1771
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
449,339,012✔
1772
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
449,339,002✔
1773

1774
  if (TSDB_CODE_SUCCESS == code) {
449,338,986✔
1775
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
449,337,983✔
1776
  }
1777

1778
  if (TSDB_CODE_SUCCESS == code) {
449,311,670✔
1779
    code = sqlSecurityCheckASTLevel(pRequest, pQuery);
372,343,861✔
1780
  }
1781

1782
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
449,317,386✔
1783

1784
  if (pRequest->parseOnly) {
449,306,924✔
1785
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
311,808✔
1786
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
311,808✔
1787
  }
1788

1789
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
449,316,015✔
1790
}
449,294,778✔
1791

1792
int32_t cloneCatalogReq(SCatalogReq **ppTarget, SCatalogReq *pSrc) {
×
1793
  int32_t      code = TSDB_CODE_SUCCESS;
×
1794
  SCatalogReq *pTarget = taosMemoryCalloc(1, sizeof(SCatalogReq));
×
1795
  if (pTarget == NULL) {
×
1796
    code = terrno;
×
1797
  } else {
1798
    pTarget->pDbVgroup = taosArrayDup(pSrc->pDbVgroup, NULL);
×
1799
    pTarget->pDbCfg = taosArrayDup(pSrc->pDbCfg, NULL);
×
1800
    pTarget->pDbInfo = taosArrayDup(pSrc->pDbInfo, NULL);
×
1801
    pTarget->pTableMeta = taosArrayDup(pSrc->pTableMeta, NULL);
×
1802
    pTarget->pTableHash = taosArrayDup(pSrc->pTableHash, NULL);
×
1803
    pTarget->pUdf = taosArrayDup(pSrc->pUdf, NULL);
×
1804
    pTarget->pIndex = taosArrayDup(pSrc->pIndex, NULL);
×
1805
    pTarget->pUser = taosArrayDup(pSrc->pUser, NULL);
×
1806
    pTarget->pTableIndex = taosArrayDup(pSrc->pTableIndex, NULL);
×
1807
    pTarget->pTableCfg = taosArrayDup(pSrc->pTableCfg, NULL);
×
1808
    pTarget->pTableTag = taosArrayDup(pSrc->pTableTag, NULL);
×
1809
    pTarget->pView = taosArrayDup(pSrc->pView, NULL);
×
1810
    pTarget->pTableTSMAs = taosArrayDup(pSrc->pTableTSMAs, NULL);
×
1811
    pTarget->pTSMAs = taosArrayDup(pSrc->pTSMAs, NULL);
×
1812
    pTarget->pVStbRefDbs = taosArrayDup(pSrc->pVStbRefDbs, NULL);
×
1813
    pTarget->qNodeRequired = pSrc->qNodeRequired;
×
1814
    pTarget->dNodeRequired = pSrc->dNodeRequired;
×
1815
    pTarget->svrVerRequired = pSrc->svrVerRequired;
×
1816
    pTarget->forceUpdate = pSrc->forceUpdate;
×
1817
    pTarget->cloned = true;
×
1818

1819
    *ppTarget = pTarget;
×
1820
  }
1821

1822
  return code;
×
1823
}
1824

1825
void handleSubQueryFromAnalyse(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, SNode *pRoot) {
×
1826
  SRequestObj         *pNewRequest = NULL;
×
1827
  SSqlCallbackWrapper *pNewWrapper = NULL;
×
1828
  int32_t              code = buildPreviousRequest(pWrapper->pRequest, pWrapper->pRequest->sqlstr, &pNewRequest);
×
1829
  if (code) {
×
1830
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1831
    return;
×
1832
  }
1833

1834
  pNewRequest->pQuery = NULL;
×
1835
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode **)&pNewRequest->pQuery);
×
1836
  if (pNewRequest->pQuery) {
×
1837
    pNewRequest->pQuery->pRoot = pRoot;
×
1838
    pRoot = NULL;
×
1839
    pNewRequest->pQuery->execStage = QUERY_EXEC_STAGE_ANALYSE;
×
1840
  }
1841
  if (TSDB_CODE_SUCCESS == code) {
×
1842
    code = prepareAndParseSqlSyntax(&pNewWrapper, pNewRequest, false);
×
1843
  }
1844
  if (TSDB_CODE_SUCCESS == code) {
×
1845
    code = cloneCatalogReq(&pNewWrapper->pCatalogReq, pWrapper->pCatalogReq);
×
1846
  }
1847
  if (TSDB_CODE_SUCCESS == code) {
×
1848
    doAsyncQueryFromAnalyse(pResultMeta, pNewWrapper, code);
×
1849
    nodesDestroyNode(pRoot);
×
1850
  } else {
1851
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1852
    return;
×
1853
  }
1854
}
1855

1856
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
449,281,815✔
1857
  SRequestObj *pRequest = pWrapper->pRequest;
449,281,815✔
1858
  SQuery      *pQuery = pRequest->pQuery;
449,289,045✔
1859

1860
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
449,280,336✔
1861
    SNode *prevRoot = pQuery->pPrevRoot;
×
1862
    pQuery->pPrevRoot = NULL;
×
1863
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
×
1864
    return;
×
1865
  }
1866

1867
  if (code == TSDB_CODE_SUCCESS) {
449,284,513✔
1868
    pRequest->stableQuery = pQuery->stableQuery;
372,343,137✔
1869
    if (pQuery->pRoot) {
372,347,803✔
1870
      pRequest->stmtType = pQuery->pRoot->type;
372,333,693✔
1871
      if (nodeType(pQuery->pRoot) == QUERY_NODE_DELETE_STMT) {
372,348,106✔
1872
        pRequest->secureDelete = ((SDeleteStmt*)pQuery->pRoot)->secureDelete;
1,709,877✔
1873
      }
1874
    }
1875

1876
    if (pQuery->haveResultSet) {
372,347,956✔
1877
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
202,932,317✔
1878
                              pRequest->stmtBindVersion > 0);
202,933,446✔
1879
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
202,921,031✔
1880
    }
1881
  }
1882

1883
  if (code == TSDB_CODE_SUCCESS) {
449,290,090✔
1884
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
372,322,859✔
1885
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
372,307,601✔
1886
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
372,314,369✔
1887

1888
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
372,322,419✔
1889
  } else {
1890
    destorySqlCallbackWrapper(pWrapper);
76,967,231✔
1891
    pRequest->pWrapper = NULL;
76,976,584✔
1892
    qDestroyQuery(pRequest->pQuery);
76,976,584✔
1893
    pRequest->pQuery = NULL;
76,975,209✔
1894

1895
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
76,975,209✔
1896
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
2,799,881✔
1897
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1898
      restartAsyncQuery(pRequest, code);
2,799,881✔
1899
      return;
2,799,881✔
1900
    }
1901

1902
    // return to app directly
1903
    tscError("req:0x%" PRIx64 ", error occurs, code:%s, return to user app, QID:0x%" PRIx64, pRequest->self,
74,175,328✔
1904
             tstrerror(code), pRequest->requestId);
1905
    pRequest->code = code;
74,190,275✔
1906
    returnToUser(pRequest);
74,190,275✔
1907
  }
1908
}
1909

1910
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
456,875,399✔
1911
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
737,109,701✔
1912
                           .requestId = pWrapper->pParseCtx->requestId,
456,877,320✔
1913
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
456,885,444✔
1914
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
456,869,944✔
1915

1916
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
737,117,432✔
1917

1918
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
633,551,204✔
1919
                                &pWrapper->pRequest->body.queryJob);
456,906,359✔
1920
}
1921

1922
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code);
1923

1924
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
1,118,344,873✔
1925
  int32_t      code = TSDB_CODE_SUCCESS;
1,118,344,873✔
1926
  SRequestObj *pRequest = pWrapper->pRequest;
1,118,344,873✔
1927

1928
  switch (pRequest->pQuery->execStage) {
1,118,424,143✔
1929
    case QUERY_EXEC_STAGE_PARSE: {
7,585,975✔
1930
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_CATALOG);
15,036,195✔
1931
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
7,585,975✔
1932
      break;
7,585,975✔
1933
    }
1934
    case QUERY_EXEC_STAGE_ANALYSE: {
449,293,277✔
1935
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_CATALOG);
898,613,027✔
1936
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
449,324,583✔
1937
      break;
449,308,516✔
1938
    }
1939
    case QUERY_EXEC_STAGE_SCHEDULE: {
661,519,472✔
1940
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_SCHEDULE);
1,323,109,213✔
1941
      launchAsyncQuery(pRequest, pRequest->pQuery, NULL, pWrapper);
661,622,936✔
1942
      break;
661,518,539✔
1943
    }
1944
    default:
×
1945
      break;
×
1946
  }
1947
  return code;
1,118,406,040✔
1948
}
1949

1950
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code) {
7,585,975✔
1951
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
7,585,975✔
1952
  SRequestObj         *pRequest = pWrapper->pRequest;
7,585,975✔
1953
  SQuery              *pQuery = pRequest->pQuery;
7,585,975✔
1954

1955
  pRequest->metric.ctgCostUs += taosGetTimestampUs() - pRequest->metric.ctgStart;
7,585,975✔
1956
  qDebug("req:0x%" PRIx64 ", continue parse query, QID:0x%" PRIx64 ", code:%s", pRequest->self, pRequest->requestId,
7,585,975✔
1957
         tstrerror(code));
1958

1959
  if (code == TSDB_CODE_SUCCESS) {
7,585,975✔
1960
    // pWrapper->pCatalogReq->forceUpdate = false;
1961
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
7,541,966✔
1962
  }
1963

1964
  if (TSDB_CODE_SUCCESS == code) {
7,585,975✔
1965
    code = phaseAsyncQuery(pWrapper);
6,960,746✔
1966
  }
1967

1968
  if (TSDB_CODE_SUCCESS != code) {
7,585,975✔
1969
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
625,229✔
1970
             tstrerror(code), pWrapper->pRequest->requestId);
1971
    destorySqlCallbackWrapper(pWrapper);
625,229✔
1972
    pRequest->pWrapper = NULL;
625,010✔
1973
    terrno = code;
625,229✔
1974
    pRequest->code = code;
625,010✔
1975
    doRequestCallback(pRequest, code);
625,229✔
1976
  }
1977
}
7,585,756✔
1978

1979
void continueInsertFromCsv(SSqlCallbackWrapper *pWrapper, SRequestObj *pRequest) {
12,879✔
1980
  int32_t code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
12,879✔
1981
  if (TSDB_CODE_SUCCESS == code) {
12,879✔
1982
    code = phaseAsyncQuery(pWrapper);
12,879✔
1983
  }
1984

1985
  if (TSDB_CODE_SUCCESS != code) {
12,879✔
1986
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
×
1987
             tstrerror(code), pWrapper->pRequest->requestId);
1988
    destorySqlCallbackWrapper(pWrapper);
×
1989
    pRequest->pWrapper = NULL;
×
1990
    terrno = code;
×
1991
    pRequest->code = code;
×
1992
    doRequestCallback(pRequest, code);
×
1993
  }
1994
}
12,879✔
1995

1996
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
137,829✔
1997
  int64_t connId = *(int64_t *)taos;
137,829✔
1998
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
137,829✔
1999
}
137,829✔
2000

2001
void taos_query_a_with_reqid(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param, int64_t reqid) {
×
2002
  int64_t connId = *(int64_t *)taos;
×
2003
  taosAsyncQueryImplWithReqid(connId, sql, fp, param, false, reqid);
×
2004
}
×
2005

2006
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
1,125,583,663✔
2007
  const STscObj *pTscObj = pRequest->pTscObj;
1,125,583,663✔
2008

2009
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
1,125,636,665✔
2010
  if (*pCxt == NULL) {
1,125,551,243✔
2011
    return terrno;
×
2012
  }
2013

2014
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
2,147,483,647✔
2015
                           .requestRid = pRequest->self,
1,125,583,156✔
2016
                           .acctId = pTscObj->acctId,
1,125,614,150✔
2017
                           .db = pRequest->pDb,
1,125,621,497✔
2018
                           .topicQuery = false,
2019
                           .pSql = pRequest->sqlstr,
1,125,658,775✔
2020
                           .sqlLen = pRequest->sqlLen,
1,125,654,780✔
2021
                           .pMsg = pRequest->msgBuf,
1,125,645,269✔
2022
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
2023
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
1,125,642,226✔
2024
                           .pStmtCb = NULL,
2025
                           .pUser = pTscObj->user,
1,125,639,745✔
2026
                           .userId = pTscObj->userId,
1,125,651,854✔
2027
                           .pEffectiveUser = pRequest->effectiveUser,
1,125,634,260✔
2028
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
1,125,651,018✔
2029
                           .enableSysInfo = pTscObj->sysInfo,
1,125,656,533✔
2030
                           .privInfo = pWrapper->pParseCtx ? pWrapper->pParseCtx->privInfo : 0,
1,125,622,365✔
2031
                           .sodInitial = pTscObj->pAppInfo->serverCfg.sodInitial,
1,125,653,911✔
2032
                           .async = true,
2033
                           .svrVer = pTscObj->sVer,
1,125,589,380✔
2034
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes), .allocatorId = pRequest->allocatorRefId,
2,063,725,776✔
2035
                           .parseSqlFp = clientParseSql,
2036
                           .parseSqlParam = pWrapper,
2037
                           .setQueryFp = setQueryRequest,
2038
                           .timezone = pTscObj->optionInfo.timezone,
1,125,642,088✔
2039
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
1,125,630,654✔
2040
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
1,125,649,498✔
2041
  (*pCxt)->biMode = biMode;
1,125,577,008✔
2042
  (*pCxt)->minSecLevel = pTscObj->minSecLevel;
1,125,610,873✔
2043
  (*pCxt)->maxSecLevel = pTscObj->maxSecLevel;
1,125,624,265✔
2044
  (*pCxt)->macMode = pTscObj->pAppInfo->serverCfg.macActive;
1,125,654,550✔
2045
  return TSDB_CODE_SUCCESS;
1,125,654,791✔
2046
}
2047

2048
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
1,125,568,849✔
2049
  int32_t              code = TSDB_CODE_SUCCESS;
1,125,568,849✔
2050
  STscObj             *pTscObj = pRequest->pTscObj;
1,125,568,849✔
2051
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
1,125,631,598✔
2052
  if (pWrapper == NULL) {
1,125,585,745✔
2053
    code = terrno;
×
2054
  } else {
2055
    pWrapper->pRequest = pRequest;
1,125,585,745✔
2056
    pRequest->pWrapper = pWrapper;
1,125,576,533✔
2057
    *ppWrapper = pWrapper;
1,125,612,729✔
2058
  }
2059

2060
  if (TSDB_CODE_SUCCESS == code) {
1,125,608,802✔
2061
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
1,125,618,134✔
2062
  }
2063

2064
  if (TSDB_CODE_SUCCESS == code) {
1,125,623,298✔
2065
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
1,125,627,857✔
2066
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
1,125,674,577✔
2067
  }
2068

2069
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
1,125,615,226✔
2070
    int64_t syntaxStart = taosGetTimestampUs();
1,125,666,271✔
2071

2072
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
1,125,666,271✔
2073
    if (pWrapper->pCatalogReq == NULL) {
1,125,524,776✔
2074
      code = terrno;
×
2075
    } else {
2076
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
1,125,564,877✔
2077
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
1,125,629,034✔
2078
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
1,125,589,333✔
2079
    }
2080

2081
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
1,125,624,419✔
2082
  }
2083

2084
  return code;
1,125,658,177✔
2085
}
2086

2087
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
1,126,500,576✔
2088
  SSqlCallbackWrapper *pWrapper = NULL;
1,126,500,576✔
2089
  int32_t              code = TSDB_CODE_SUCCESS;
1,126,546,197✔
2090

2091
  CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_PARSE);
2,147,483,647✔
2092

2093
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
1,126,609,577✔
2094
    code = pRequest->prevCode;
935,508✔
2095
    terrno = code;
935,508✔
2096
    pRequest->code = code;
935,508✔
2097
    tscDebug("req:0x%" PRIx64 ", call sync query cb with code:%s", pRequest->self, tstrerror(code));
935,508✔
2098
    doRequestCallback(pRequest, code);
935,508✔
2099
    return;
935,508✔
2100
  }
2101

2102
  if (TSDB_CODE_SUCCESS == code) {
1,125,610,222✔
2103
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
1,125,579,243✔
2104
  }
2105

2106
  if (TSDB_CODE_SUCCESS == code) {
1,125,572,913✔
2107
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
1,111,445,667✔
2108
    code = phaseAsyncQuery(pWrapper);
1,111,476,261✔
2109
  }
2110

2111
  if (TSDB_CODE_SUCCESS != code) {
1,125,562,734✔
2112
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
14,131,190✔
2113
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
14,051,754✔
2114
               pRequest->requestId);
2115
    } else {
2116
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
79,436✔
2117
               pRequest->requestId);
2118
    }
2119

2120
    destorySqlCallbackWrapper(pWrapper);
14,131,270✔
2121
    pRequest->pWrapper = NULL;
14,131,556✔
2122
    qDestroyQuery(pRequest->pQuery);
14,131,556✔
2123
    pRequest->pQuery = NULL;
14,131,817✔
2124

2125
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
14,131,817✔
2126
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
13,321✔
2127
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
2128
      code = refreshMeta(pRequest->pTscObj, pRequest);
13,321✔
2129
      if (code != 0) {
13,321✔
2130
        tscWarn("req:0x%" PRIx64 ", refresh meta failed, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code,
13,321✔
2131
                tstrerror(code), pRequest->requestId);
2132
      }
2133
      pRequest->prevCode = code;
13,321✔
2134
      doAsyncQuery(pRequest, true);
13,321✔
2135
      return;
13,321✔
2136
    }
2137

2138
    terrno = code;
14,118,496✔
2139
    pRequest->code = code;
14,118,315✔
2140
    doRequestCallback(pRequest, code);
14,118,315✔
2141
  }
2142
}
2143

2144
void restartAsyncQuery(SRequestObj *pRequest, int32_t code) {
2,893,378✔
2145
  tscInfo("restart request:%s p:%p", pRequest->sqlstr, pRequest);
2,893,378✔
2146
  SRequestObj *pUserReq = pRequest;
2,893,378✔
2147
  (void)acquireRequest(pRequest->self);
2,893,378✔
2148
  while (pUserReq) {
2,893,378✔
2149
    if (pUserReq->self == pUserReq->relation.userRefId || pUserReq->relation.userRefId == 0) {
2,893,378✔
2150
      break;
2151
    } else {
2152
      int64_t nextRefId = pUserReq->relation.nextRefId;
×
2153
      (void)releaseRequest(pUserReq->self);
×
2154
      if (nextRefId) {
×
2155
        pUserReq = acquireRequest(nextRefId);
×
2156
      }
2157
    }
2158
  }
2159
  bool hasSubRequest = pUserReq != pRequest || pRequest->relation.prevRefId != 0;
2,893,378✔
2160
  if (pUserReq) {
2,893,378✔
2161
    destroyCtxInRequest(pUserReq);
2,893,378✔
2162
    pUserReq->prevCode = code;
2,893,378✔
2163
    (void)memset(&pUserReq->relation, 0, sizeof(pUserReq->relation));
2,893,378✔
2164
  } else {
2165
    tscError("User req is missing");
×
2166
    (void)removeFromMostPrevReq(pRequest);
×
2167
    return;
×
2168
  }
2169
  if (hasSubRequest)
2,893,378✔
2170
    (void)removeFromMostPrevReq(pRequest);
×
2171
  else
2172
    (void)releaseRequest(pUserReq->self);
2,893,378✔
2173
  doAsyncQuery(pUserReq, true);
2,893,378✔
2174
}
2175

2176
typedef struct SAsyncFetchParam {
2177
  SRequestObj      *pReq;
2178
  __taos_async_fn_t fp;
2179
  void             *param;
2180
} SAsyncFetchParam;
2181

2182
static int32_t doAsyncFetch(void *pParam) {
299,658,588✔
2183
  SAsyncFetchParam *param = pParam;
299,658,588✔
2184
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
299,658,588✔
2185
  taosMemoryFree(param);
299,658,562✔
2186
  return TSDB_CODE_SUCCESS;
299,658,051✔
2187
}
2188

2189
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
299,698,158✔
2190
  if (res == NULL || fp == NULL) {
299,698,158✔
2191
    tscError("taos_fetch_rows_a invalid paras");
×
2192
    return;
×
2193
  }
2194
  if (!TD_RES_QUERY(res)) {
299,698,205✔
2195
    tscError("taos_fetch_rows_a res is NULL");
×
2196
    fp(param, res, TSDB_CODE_APP_ERROR);
×
2197
    return;
×
2198
  }
2199

2200
  SRequestObj *pRequest = res;
299,698,205✔
2201

2202
  // Each fetch call sets phase to IN_PROGRESS
2203

2204
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
299,698,205✔
2205
    CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
79,260✔
2206
    fp(param, res, 0);
39,630✔
2207
    return;
39,630✔
2208
  }
2209

2210
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
299,658,575✔
2211
  if (!pParam) {
299,658,516✔
2212
    CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
×
2213
    fp(param, res, terrno);
×
2214
    return;
×
2215
  }
2216

2217
  CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_IN_PROGRESS);
454,130,935✔
2218
  pParam->pReq = pRequest;
299,658,576✔
2219
  pParam->fp = fp;
299,658,576✔
2220
  pParam->param = param;
299,658,576✔
2221
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
299,658,529✔
2222
  if (TSDB_CODE_SUCCESS != code) {
299,658,066✔
2223
    taosMemoryFree(pParam);
×
2224
    fp(param, res, code);
×
2225
    return;
×
2226
  }
2227
}
2228

2229
void taos_fetch_raw_block_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
546✔
2230
  if (res == NULL || fp == NULL) {
546✔
2231
    tscError("taos_fetch_raw_block_a invalid paras");
×
2232
    return;
×
2233
  }
2234
  if (!TD_RES_QUERY(res)) {
546✔
2235
    tscError("taos_fetch_raw_block_a res is NULL");
×
2236
    return;
×
2237
  }
2238
  SRequestObj    *pRequest = res;
546✔
2239
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
546✔
2240

2241
  CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_IN_PROGRESS);
807✔
2242
  // set the current block is all consumed
2243
  pResultInfo->convertUcs4 = false;
546✔
2244

2245
  // it is a local executed query, no need to do async fetch
2246
  taos_fetch_rows_a(pRequest, fp, param);
546✔
2247
}
2248

2249
const void *taos_get_raw_block(TAOS_RES *res) {
97✔
2250
  if (res == NULL) {
97✔
2251
    tscError("taos_get_raw_block invalid paras");
×
2252
    return NULL;
×
2253
  }
2254
  if (!TD_RES_QUERY(res)) {
97✔
2255
    tscError("taos_get_raw_block res is NULL");
×
2256
    return NULL;
×
2257
  }
2258
  SRequestObj *pRequest = res;
97✔
2259

2260
  return pRequest->body.resInfo.pData;
97✔
2261
}
2262

2263
int taos_get_db_route_info(TAOS *taos, const char *db, TAOS_DB_ROUTE_INFO *dbInfo) {
×
2264
  if (NULL == taos) {
×
2265
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2266
    return terrno;
×
2267
  }
2268

2269
  if (NULL == db || NULL == dbInfo) {
×
2270
    tscError("invalid input param, db:%p, dbInfo:%p", db, dbInfo);
×
2271
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2272
    return terrno;
×
2273
  }
2274

2275
  int64_t      connId = *(int64_t *)taos;
×
2276
  SRequestObj *pRequest = NULL;
×
2277
  char        *sql = "taos_get_db_route_info";
×
2278
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2279
  if (code != TSDB_CODE_SUCCESS) {
×
2280
    terrno = code;
×
2281
    return terrno;
×
2282
  }
2283

2284
  STscObj  *pTscObj = pRequest->pTscObj;
×
2285
  SCatalog *pCtg = NULL;
×
2286
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2287
  if (code != TSDB_CODE_SUCCESS) {
×
2288
    goto _return;
×
2289
  }
2290

2291
  SRequestConnInfo conn = {
×
2292
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2293

2294
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2295

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

2299
  code = catalogGetDBVgInfo(pCtg, &conn, dbFName, dbInfo);
×
2300
  if (code) {
×
2301
    goto _return;
×
2302
  }
2303

2304
_return:
×
2305

2306
  terrno = code;
×
2307

2308
  destroyRequest(pRequest);
×
2309
  return code;
×
2310
}
2311

2312
int taos_get_table_vgId(TAOS *taos, const char *db, const char *table, int *vgId) {
×
2313
  if (NULL == taos) {
×
2314
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2315
    return terrno;
×
2316
  }
2317

2318
  if (NULL == db || NULL == table || NULL == vgId) {
×
2319
    tscError("invalid input param, db:%p, table:%p, vgId:%p", db, table, vgId);
×
2320
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2321
    return terrno;
×
2322
  }
2323

2324
  int64_t      connId = *(int64_t *)taos;
×
2325
  SRequestObj *pRequest = NULL;
×
2326
  char        *sql = "taos_get_table_vgId";
×
2327
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2328
  if (code != TSDB_CODE_SUCCESS) {
×
2329
    return terrno;
×
2330
  }
2331

2332
  pRequest->syncQuery = true;
×
2333

2334
  STscObj  *pTscObj = pRequest->pTscObj;
×
2335
  SCatalog *pCtg = NULL;
×
2336
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2337
  if (code != TSDB_CODE_SUCCESS) {
×
2338
    goto _return;
×
2339
  }
2340

2341
  SRequestConnInfo conn = {
×
2342
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2343

2344
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2345

2346
  SName tableName = {0};
×
2347
  toName(pTscObj->acctId, db, table, &tableName);
×
2348

2349
  SVgroupInfo vgInfo;
×
2350
  code = catalogGetTableHashVgroup(pCtg, &conn, &tableName, &vgInfo);
×
2351
  if (code) {
×
2352
    goto _return;
×
2353
  }
2354

2355
  *vgId = vgInfo.vgId;
×
2356

2357
_return:
×
2358

2359
  terrno = code;
×
2360

2361
  destroyRequest(pRequest);
×
2362
  return code;
×
2363
}
2364

2365
int taos_get_tables_vgId(TAOS *taos, const char *db, const char *table[], int tableNum, int *vgId) {
×
2366
  if (NULL == taos) {
×
2367
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2368
    return terrno;
×
2369
  }
2370

2371
  if (NULL == db || NULL == table || NULL == vgId || tableNum <= 0) {
×
2372
    tscError("invalid input param, db:%p, table:%p, vgId:%p, tbNum:%d", db, table, vgId, tableNum);
×
2373
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
2374
    return terrno;
×
2375
  }
2376

2377
  int64_t      connId = *(int64_t *)taos;
×
2378
  SRequestObj *pRequest = NULL;
×
2379
  char        *sql = "taos_get_table_vgId";
×
2380
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
2381
  if (code != TSDB_CODE_SUCCESS) {
×
2382
    return terrno;
×
2383
  }
2384

2385
  pRequest->syncQuery = true;
×
2386

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

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

2397
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2398

2399
  code = catalogGetTablesHashVgId(pCtg, &conn, pTscObj->acctId, db, table, tableNum, vgId);
×
2400
  if (code) {
×
2401
    goto _return;
×
2402
  }
2403

2404
_return:
×
2405

2406
  terrno = code;
×
2407

2408
  destroyRequest(pRequest);
×
2409
  return code;
×
2410
}
2411

2412
int taos_load_table_info(TAOS *taos, const char *tableNameList) {
1,274✔
2413
  if (NULL == taos) {
1,274✔
2414
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2415
    return terrno;
×
2416
  }
2417

2418
  int64_t       connId = *(int64_t *)taos;
1,274✔
2419
  const int32_t MAX_TABLE_NAME_LENGTH = 12 * 1024 * 1024;  // 12MB list
1,274✔
2420
  int32_t       code = 0;
1,274✔
2421
  SRequestObj  *pRequest = NULL;
1,274✔
2422
  SCatalogReq   catalogReq = {0};
1,274✔
2423

2424
  if (NULL == tableNameList) {
1,274✔
2425
    return TSDB_CODE_SUCCESS;
×
2426
  }
2427

2428
  int32_t length = (int32_t)strlen(tableNameList);
1,274✔
2429
  if (0 == length) {
1,274✔
2430
    return TSDB_CODE_SUCCESS;
×
2431
  } else if (length > MAX_TABLE_NAME_LENGTH) {
1,274✔
2432
    tscError("tableNameList too long, length:%d, maximum allowed:%d", length, MAX_TABLE_NAME_LENGTH);
×
2433
    return TSDB_CODE_TSC_INVALID_OPERATION;
×
2434
  }
2435

2436
  char *sql = "taos_load_table_info";
1,274✔
2437
  code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
1,274✔
2438
  if (code != TSDB_CODE_SUCCESS) {
1,274✔
2439
    terrno = code;
×
2440
    goto _return;
×
2441
  }
2442

2443
  pRequest->syncQuery = true;
1,274✔
2444

2445
  STscObj *pTscObj = pRequest->pTscObj;
1,274✔
2446
  code = transferTableNameList(tableNameList, pTscObj->acctId, pTscObj->db, &catalogReq.pTableMeta);
1,274✔
2447
  if (code) {
1,274✔
2448
    goto _return;
×
2449
  }
2450

2451
  SCatalog *pCtg = NULL;
1,274✔
2452
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
1,274✔
2453
  if (code != TSDB_CODE_SUCCESS) {
1,274✔
2454
    goto _return;
×
2455
  }
2456

2457
  SRequestConnInfo conn = {
1,274✔
2458
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
1,274✔
2459

2460
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
1,274✔
2461

2462
  code = catalogAsyncGetAllMeta(pCtg, &conn, &catalogReq, syncCatalogFn, pRequest->body.interParam, NULL);
1,274✔
2463
  if (code) {
1,274✔
2464
    goto _return;
×
2465
  }
2466

2467
  SSyncQueryParam *pParam = pRequest->body.interParam;
1,274✔
2468
  code = tsem_wait(&pParam->sem);
1,274✔
2469
  if (code) {
1,274✔
2470
    tscError("tsem wait failed, code:%d - %s", code, tstrerror(code));
×
2471
    goto _return;
×
2472
  }
2473
_return:
1,274✔
2474
  destoryCatalogReq(&catalogReq);
1,274✔
2475
  destroyRequest(pRequest);
1,274✔
2476
  return code;
1,274✔
2477
}
2478

2479
TAOS_STMT *taos_stmt_init(TAOS *taos) {
27,684✔
2480
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
27,684✔
2481
  if (NULL == pObj) {
27,684✔
2482
    tscError("invalid parameter for %s", __FUNCTION__);
×
2483
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2484
    return NULL;
×
2485
  }
2486

2487
  TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL);
27,684✔
2488
  if (NULL == pStmt) {
27,684✔
2489
    tscError("stmt init failed, errcode:%s", terrstr());
×
2490
  }
2491
  releaseTscObj(*(int64_t *)taos);
27,684✔
2492

2493
  return pStmt;
27,684✔
2494
}
2495

2496
TAOS_STMT *taos_stmt_init_with_reqid(TAOS *taos, int64_t reqid) {
×
2497
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
2498
  if (NULL == pObj) {
×
2499
    tscError("invalid parameter for %s", __FUNCTION__);
×
2500
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2501
    return NULL;
×
2502
  }
2503

2504
  TAOS_STMT *pStmt = stmtInit(pObj, reqid, NULL);
×
2505
  if (NULL == pStmt) {
×
2506
    tscError("stmt init failed, errcode:%s", terrstr());
×
2507
  }
2508
  releaseTscObj(*(int64_t *)taos);
×
2509

2510
  return pStmt;
×
2511
}
2512

2513
TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options) {
11,746✔
2514
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
11,746✔
2515
  if (NULL == pObj) {
11,805✔
2516
    tscError("invalid parameter for %s", __FUNCTION__);
×
2517
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2518
    return NULL;
×
2519
  }
2520

2521
  TAOS_STMT *pStmt = stmtInit(pObj, options->reqId, options);
11,805✔
2522
  if (NULL == pStmt) {
11,805✔
2523
    tscError("stmt init failed, errcode:%s", terrstr());
×
2524
  }
2525
  releaseTscObj(*(int64_t *)taos);
11,805✔
2526

2527
  return pStmt;
11,805✔
2528
}
2529

2530
int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
6,989,567✔
2531
  if (stmt == NULL || sql == NULL) {
6,989,567✔
UNCOV
2532
    tscError("NULL parameter for %s", __FUNCTION__);
×
UNCOV
2533
    terrno = TSDB_CODE_INVALID_PARA;
×
2534
    return terrno;
×
2535
  }
2536

2537
  return stmtPrepare(stmt, sql, length);
6,990,263✔
2538
}
2539

2540
int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags) {
7,082✔
2541
  if (stmt == NULL || name == NULL) {
7,082✔
2542
    tscError("NULL parameter for %s", __FUNCTION__);
×
2543
    terrno = TSDB_CODE_INVALID_PARA;
×
2544
    return terrno;
×
2545
  }
2546

2547
  int32_t code = stmtSetTbName(stmt, name);
7,082✔
2548
  if (code) {
7,082✔
2549
    return code;
637✔
2550
  }
2551

2552
  if (tags) {
6,445✔
2553
    return stmtSetTbTags(stmt, tags);
6,445✔
2554
  }
2555

2556
  return TSDB_CODE_SUCCESS;
×
2557
}
2558

2559
int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name) {
8,380,823✔
2560
  if (stmt == NULL || name == NULL) {
8,380,823✔
2561
    tscError("NULL parameter for %s", __FUNCTION__);
×
2562
    terrno = TSDB_CODE_INVALID_PARA;
×
2563
    return terrno;
×
2564
  }
2565

2566
  return stmtSetTbName(stmt, name);
8,383,408✔
2567
}
2568

2569
int taos_stmt_set_tags(TAOS_STMT *stmt, TAOS_MULTI_BIND *tags) {
535✔
2570
  if (stmt == NULL || tags == NULL) {
535✔
2571
    tscError("NULL parameter for %s", __FUNCTION__);
×
2572
    terrno = TSDB_CODE_INVALID_PARA;
×
2573
    return terrno;
×
2574
  }
2575

2576
  return stmtSetTbTags(stmt, tags);
535✔
2577
}
2578

2579
int taos_stmt_set_sub_tbname(TAOS_STMT *stmt, const char *name) { return taos_stmt_set_tbname(stmt, name); }
200✔
2580

2581
int taos_stmt_get_tag_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
400✔
2582
  if (stmt == NULL || NULL == fieldNum) {
400✔
2583
    tscError("NULL parameter for %s", __FUNCTION__);
×
2584
    terrno = TSDB_CODE_INVALID_PARA;
×
2585
    return terrno;
×
2586
  }
2587

2588
  return stmtGetTagFields(stmt, fieldNum, fields);
400✔
2589
}
2590

2591
int taos_stmt_get_col_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
600✔
2592
  if (stmt == NULL || NULL == fieldNum) {
600✔
2593
    tscError("NULL parameter for %s", __FUNCTION__);
×
2594
    terrno = TSDB_CODE_INVALID_PARA;
×
2595
    return terrno;
×
2596
  }
2597

2598
  return stmtGetColFields(stmt, fieldNum, fields);
600✔
2599
}
2600

2601
// let stmt to reclaim TAOS_FIELD_E that was allocated by `taos_stmt_get_tag_fields`/`taos_stmt_get_col_fields`
2602
void taos_stmt_reclaim_fields(TAOS_STMT *stmt, TAOS_FIELD_E *fields) {
×
2603
  (void)stmt;
2604
  if (!fields) return;
×
2605
  taosMemoryFree(fields);
×
2606
}
2607

2608
int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
41,659✔
2609
  if (stmt == NULL || bind == NULL) {
41,659✔
2610
    tscError("NULL parameter for %s", __FUNCTION__);
×
2611
    terrno = TSDB_CODE_INVALID_PARA;
×
2612
    return terrno;
×
2613
  }
2614

2615
  if (bind->num > 1) {
41,659✔
2616
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
4,110✔
2617
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
4,110✔
2618
    return terrno;
4,110✔
2619
  }
2620

2621
  return stmtBindBatch(stmt, bind, -1);
37,549✔
2622
}
2623

2624
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
8,385,248✔
2625
  if (stmt == NULL || bind == NULL) {
8,385,248✔
2626
    tscError("NULL parameter for %s", __FUNCTION__);
14✔
2627
    terrno = TSDB_CODE_INVALID_PARA;
14✔
2628
    return terrno;
×
2629
  }
2630

2631
  if (bind->num <= 0 || bind->num > INT16_MAX) {
8,386,923✔
2632
    tscError("invalid bind num %d", bind->num);
2,609✔
2633
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
2,609✔
2634
    return terrno;
×
2635
  }
2636

2637
  int32_t insert = 0;
8,384,927✔
2638
  int32_t code = stmtIsInsert(stmt, &insert);
8,384,210✔
2639
  if (TSDB_CODE_SUCCESS != code) {
8,382,870✔
2640
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2641
    return code;
×
2642
  }
2643
  if (0 == insert && bind->num > 1) {
8,382,870✔
2644
    tscError("only one row data allowed for query");
×
2645
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2646
    return terrno;
×
2647
  }
2648

2649
  return stmtBindBatch(stmt, bind, -1);
8,382,870✔
2650
}
2651

2652
int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind, int colIdx) {
700✔
2653
  if (stmt == NULL || bind == NULL) {
700✔
2654
    tscError("NULL parameter for %s", __FUNCTION__);
×
2655
    terrno = TSDB_CODE_INVALID_PARA;
×
2656
    return terrno;
×
2657
  }
2658

2659
  if (colIdx < 0) {
700✔
2660
    tscError("invalid bind column idx %d", colIdx);
×
2661
    terrno = TSDB_CODE_INVALID_PARA;
×
2662
    return terrno;
×
2663
  }
2664

2665
  int32_t insert = 0;
700✔
2666
  int32_t code = stmtIsInsert(stmt, &insert);
700✔
2667
  if (TSDB_CODE_SUCCESS != code) {
700✔
2668
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2669
    return code;
×
2670
  }
2671
  if (0 == insert && bind->num > 1) {
700✔
2672
    tscError("only one row data allowed for query");
×
2673
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2674
    return terrno;
×
2675
  }
2676

2677
  return stmtBindBatch(stmt, bind, colIdx);
700✔
2678
}
2679

2680
int taos_stmt_add_batch(TAOS_STMT *stmt) {
7,201,493✔
2681
  if (stmt == NULL) {
7,201,493✔
2682
    tscError("NULL parameter for %s", __FUNCTION__);
×
2683
    terrno = TSDB_CODE_INVALID_PARA;
×
2684
    return terrno;
×
2685
  }
2686

2687
  return stmtAddBatch(stmt);
7,201,493✔
2688
}
2689

2690
int taos_stmt_execute(TAOS_STMT *stmt) {
7,201,956✔
2691
  if (stmt == NULL) {
7,201,956✔
2692
    tscError("NULL parameter for %s", __FUNCTION__);
×
2693
    terrno = TSDB_CODE_INVALID_PARA;
×
2694
    return terrno;
×
2695
  }
2696

2697
  return stmtExec(stmt);
7,201,956✔
2698
}
2699

2700
int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
×
2701
  if (stmt == NULL || insert == NULL) {
×
2702
    tscError("NULL parameter for %s", __FUNCTION__);
×
2703
    terrno = TSDB_CODE_INVALID_PARA;
×
2704
    return terrno;
×
2705
  }
2706

2707
  return stmtIsInsert(stmt, insert);
×
2708
}
2709

2710
int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) {
×
2711
  if (stmt == NULL || nums == NULL) {
×
2712
    tscError("NULL parameter for %s", __FUNCTION__);
×
2713
    terrno = TSDB_CODE_INVALID_PARA;
×
2714
    return terrno;
×
2715
  }
2716

2717
  return stmtGetParamNum(stmt, nums);
×
2718
}
2719

2720
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
400✔
2721
  if (stmt == NULL || type == NULL || NULL == bytes || idx < 0) {
400✔
2722
    tscError("invalid parameter for %s", __FUNCTION__);
×
2723
    terrno = TSDB_CODE_INVALID_PARA;
×
2724
    return terrno;
×
2725
  }
2726

2727
  return stmtGetParam(stmt, idx, type, bytes);
400✔
2728
}
2729

2730
TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt) {
13,503✔
2731
  if (stmt == NULL) {
13,503✔
2732
    tscError("NULL parameter for %s", __FUNCTION__);
×
2733
    terrno = TSDB_CODE_INVALID_PARA;
×
2734
    return NULL;
×
2735
  }
2736

2737
  return stmtUseResult(stmt);
13,503✔
2738
}
2739

2740
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
13,194✔
2741

2742
int taos_stmt_affected_rows(TAOS_STMT *stmt) {
3,299✔
2743
  if (stmt == NULL) {
3,299✔
2744
    tscError("NULL parameter for %s", __FUNCTION__);
×
2745
    terrno = TSDB_CODE_INVALID_PARA;
×
2746
    return 0;
×
2747
  }
2748

2749
  return stmtAffectedRows(stmt);
3,299✔
2750
}
2751

2752
int taos_stmt_affected_rows_once(TAOS_STMT *stmt) {
2,910✔
2753
  if (stmt == NULL) {
2,910✔
2754
    tscError("NULL parameter for %s", __FUNCTION__);
×
2755
    terrno = TSDB_CODE_INVALID_PARA;
×
2756
    return 0;
×
2757
  }
2758

2759
  return stmtAffectedRowsOnce(stmt);
2,910✔
2760
}
2761

2762
int taos_stmt_close(TAOS_STMT *stmt) {
39,489✔
2763
  if (stmt == NULL) {
39,489✔
2764
    tscError("NULL parameter for %s", __FUNCTION__);
×
2765
    terrno = TSDB_CODE_INVALID_PARA;
×
2766
    return terrno;
×
2767
  }
2768

2769
  return stmtClose(stmt);
39,489✔
2770
}
2771

2772
TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option) {
128,002✔
2773
  if (NULL == taos) {
128,002✔
2774
    tscError("NULL parameter for %s", __FUNCTION__);
100✔
2775
    terrno = TSDB_CODE_INVALID_PARA;
100✔
2776
    return NULL;
100✔
2777
  }
2778
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
127,902✔
2779
  if (NULL == pObj) {
127,963✔
2780
    tscError("invalid parameter for %s", __FUNCTION__);
×
2781
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2782
    return NULL;
×
2783
  }
2784

2785
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
127,963✔
2786

2787
  releaseTscObj(*(int64_t *)taos);
127,963✔
2788

2789
  return pStmt;
127,963✔
2790
}
2791

2792
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
131,389✔
2793
  if (stmt == NULL || sql == NULL) {
131,389✔
2794
    tscError("NULL parameter for %s", __FUNCTION__);
100✔
2795
    terrno = TSDB_CODE_INVALID_PARA;
100✔
2796
    return terrno;
100✔
2797
  }
2798

2799
  return stmtPrepare2(stmt, sql, length);
131,289✔
2800
}
2801

2802
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
49,962,410✔
2803
  if (stmt == NULL) {
49,962,410✔
2804
    tscError("NULL parameter for %s", __FUNCTION__);
×
2805
    terrno = TSDB_CODE_INVALID_PARA;
×
2806
    return terrno;
×
2807
  }
2808

2809
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
49,962,410✔
2810
  int32_t    code = TSDB_CODE_SUCCESS;
49,962,410✔
2811
  STMT2_DLOG_E("start to bind param");
49,962,410✔
2812

2813
  // check query bind number
2814
  bool isQuery = (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt)));
49,989,134✔
2815
  if (isQuery) {
50,964,280✔
2816
    if (bindv->count != 1 || bindv->bind_cols[0]->num != 1) {
6,791✔
2817
      terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2818
      STMT2_ELOG_E("query only support one table and one row bind");
×
2819
      return terrno;
×
2820
    }
2821
  }
2822

2823
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 1) {
50,964,280✔
2824
    STMT2_ELOG_E("async bind param is still working, please try again later");
6,124✔
2825
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
6,124✔
2826
    return terrno;
×
2827
  }
2828

2829
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
50,526,196✔
2830
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
2,500✔
2831
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
2832
    }
2833
    pStmt->execSemWaited = true;
2,500✔
2834
  }
2835

2836
  for (int i = 0; i < bindv->count; ++i) {
100,641,946✔
2837
    SVCreateTbReq *pCreateTbReq = NULL;
50,370,077✔
2838
    if (!isQuery) {
50,767,499✔
2839
      STMT2_TLOG("start to bind %dth table", i);
50,756,230✔
2840
      if (bindv->tbnames && bindv->tbnames[i]) {
50,736,617✔
2841
        code = stmtSetTbName2(stmt, bindv->tbnames[i]);
882,274✔
2842
        if (code) {
882,115✔
2843
          terrno = code;
1,100✔
2844
          STMT2_ELOG("set tbname failed, code:%s", stmt2Errstr(stmt));
1,100✔
2845
          return terrno;
1,800✔
2846
        }
2847
      }
2848

2849
      if (bindv->tags && bindv->tags[i]) {
51,035,596✔
2850
        code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
463,254✔
2851
      } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
50,451,776✔
2852
        code = stmtCheckTags2(stmt, &pCreateTbReq);
98,872✔
2853
      } else if (pStmt->sql.autoCreateTbl) {
50,249,944✔
2854
        code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
2,100✔
2855
      }
2856

2857
      if (code) {
51,169,336✔
2858
        terrno = code;
100✔
2859
        STMT2_ELOG("set tags failed, code:%s", stmt2Errstr(stmt));
100✔
2860
        if (pCreateTbReq) {
100✔
2861
          tdDestroySVCreateTbReq(pCreateTbReq);
×
2862
          taosMemoryFreeClear(pCreateTbReq);
×
2863
        }
2864
        return terrno;
100✔
2865
      }
2866
    }
2867

2868
    if (bindv->bind_cols && bindv->bind_cols[i]) {
51,180,505✔
2869
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
51,489,212✔
2870

2871
      if (bind->num <= 0 || bind->num > INT16_MAX) {
51,314,869✔
2872
        STMT2_ELOG("bind num:%d must > 0 and < INT16_MAX", bind->num);
47,927✔
2873
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
47,927✔
2874
        if (pCreateTbReq) {
×
2875
          tdDestroySVCreateTbReq(pCreateTbReq);
×
2876
          taosMemoryFreeClear(pCreateTbReq);
×
2877
        }
2878
        return terrno;
×
2879
      }
2880

2881
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
51,519,550✔
2882
      if (TSDB_CODE_SUCCESS != code) {
50,045,510✔
2883
        terrno = code;
770✔
2884
        STMT2_ELOG("bind batch failed, code:%s", stmt2Errstr(stmt));
600✔
2885
        if (pCreateTbReq) {
600✔
2886
          tdDestroySVCreateTbReq(pCreateTbReq);
200✔
2887
          taosMemoryFreeClear(pCreateTbReq);
200✔
2888
        }
2889
        return terrno;
600✔
2890
      }
2891
    }
2892
  }
2893

2894
  return code;
50,275,373✔
2895
}
2896

2897
int taos_stmt2_bind_param_a(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx, __taos_async_fn_t fp,
×
2898
                            void *param) {
2899
  if (stmt == NULL || bindv == NULL || fp == NULL) {
×
2900
    terrno = TSDB_CODE_INVALID_PARA;
×
2901
    return terrno;
×
2902
  }
2903

2904
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2905

2906
  ThreadArgs *args = (ThreadArgs *)taosMemoryMalloc(sizeof(ThreadArgs));
×
2907
  args->stmt = stmt;
×
2908
  args->bindv = bindv;
×
2909
  args->col_idx = col_idx;
×
2910
  args->fp = fp;
×
2911
  args->param = param;
×
2912

2913
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2914
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 0) {
×
2915
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2916
    tscError("async bind param is still working, please try again later");
×
2917
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
×
2918
    return terrno;
×
2919
  }
2920
  (void)atomic_add_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2921
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2922

2923
  int code_s = taosStmt2AsyncBind(stmtAsyncBindThreadFunc, (void *)args);
×
2924
  if (code_s != TSDB_CODE_SUCCESS) {
×
2925
    terrno = code_s;
×
2926
    (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2927
    (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2928
    (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2929
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2930
    tscError("async bind failed, code:%d , %s", code_s, tstrerror(code_s));
×
2931
  }
2932

2933
  return code_s;
×
2934
}
2935

2936
int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows) {
526,981✔
2937
  if (stmt == NULL) {
526,981✔
2938
    tscError("NULL parameter for %s", __FUNCTION__);
×
2939
    terrno = TSDB_CODE_INVALID_PARA;
×
2940
    return terrno;
×
2941
  }
2942

2943
  return stmtExec2(stmt, affected_rows);
526,981✔
2944
}
2945

2946
int taos_stmt2_close(TAOS_STMT2 *stmt) {
127,563✔
2947
  if (stmt == NULL) {
127,563✔
2948
    tscError("NULL parameter for %s", __FUNCTION__);
×
2949
    terrno = TSDB_CODE_INVALID_PARA;
×
2950
    return terrno;
×
2951
  }
2952

2953
  return stmtClose2(stmt);
127,563✔
2954
}
2955

2956
int taos_stmt2_is_insert(TAOS_STMT2 *stmt, int *insert) {
3,068✔
2957
  if (stmt == NULL || insert == NULL) {
3,068✔
2958
    tscError("NULL parameter for %s", __FUNCTION__);
×
2959
    terrno = TSDB_CODE_INVALID_PARA;
×
2960
    return terrno;
×
2961
  }
2962
  *insert = stmt2IsInsert(stmt);
3,068✔
2963
  return TSDB_CODE_SUCCESS;
3,068✔
2964
}
2965

2966
int taos_stmt2_get_fields(TAOS_STMT2 *stmt, int *count, TAOS_FIELD_ALL **fields) {
9,377✔
2967
  if (stmt == NULL || count == NULL) {
9,377✔
2968
    tscError("NULL parameter for %s", __FUNCTION__);
100✔
2969
    terrno = TSDB_CODE_INVALID_PARA;
100✔
2970
    return terrno;
100✔
2971
  }
2972

2973
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
9,277✔
2974
  STMT2_DLOG_E("start to get fields");
9,277✔
2975

2976
  if (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type ||
9,277✔
2977
      (pStmt->sql.type == 0 && stmt2IsInsert(stmt))) {
8,577✔
2978
    return stmtGetStbColFields2(stmt, count, fields);
7,277✔
2979
  }
2980
  if (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt))) {
2,000✔
2981
    return stmtGetParamNum2(stmt, count);
1,900✔
2982
  }
2983

2984
  tscError("Invalid sql for stmt %s", pStmt->sql.sqlStr);
100✔
2985
  return TSDB_CODE_PAR_SYNTAX_ERROR;
100✔
2986
}
2987

2988
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_ALL *fields) {
6,717✔
2989
  (void)stmt;
2990
  if (!fields) return;
6,717✔
2991
  taosMemoryFree(fields);
4,917✔
2992
}
2993

2994
TAOS_RES *taos_stmt2_result(TAOS_STMT2 *stmt) {
6,291✔
2995
  if (stmt == NULL) {
6,291✔
2996
    tscError("NULL parameter for %s", __FUNCTION__);
×
2997
    terrno = TSDB_CODE_INVALID_PARA;
×
2998
    return NULL;
×
2999
  }
3000

3001
  return stmtUseResult2(stmt);
6,291✔
3002
}
3003

3004
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmt2Errstr(stmt); }
4,084✔
3005

3006
int taos_set_conn_mode(TAOS *taos, int mode, int value) {
2,473✔
3007
  int32_t code = 0;
2,473✔
3008
  if (taos == NULL) {
2,473✔
3009
    terrno = TSDB_CODE_INVALID_PARA;
×
3010
    return terrno;
×
3011
  }
3012

3013
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2,473✔
3014
  if (NULL == pObj) {
2,473✔
3015
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
3016
    tscError("invalid parameter for %s", __func__);
×
3017
    return terrno;
×
3018
  }
3019
  switch (mode) {
2,473✔
3020
    case TAOS_CONN_MODE_BI:
2,473✔
3021
      atomic_store_8(&pObj->biMode, value);
2,473✔
3022
      break;
2,473✔
3023
    default:
×
3024
      tscError("not supported mode.");
×
3025
      code = TSDB_CODE_INVALID_PARA;
×
3026
  }
3027
  releaseTscObj(*(int64_t *)taos);
2,473✔
3028
  return code;
2,473✔
3029
}
3030

3031
char *getBuildInfo() { return td_buildinfo; }
×
3032

3033
int32_t taos_connect_is_alive(TAOS *taos) {
×
3034
  int32_t code = 0, lino = 0;
×
3035
  if (taos == NULL) {
×
3036
    terrno = TSDB_CODE_INVALID_PARA;
×
3037
    return terrno;
×
3038
  }
3039

3040
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
3041
  if (NULL == pObj) {
×
3042
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
3043
    tscError("invalid parameter for %s", __func__);
×
3044
    return terrno;
×
3045
  }
3046

3047
  code = tscCheckConnSessionMetric(pObj);
×
3048
  TAOS_CHECK_GOTO(code, &lino, _error);
×
3049

3050
_error:
×
3051
  releaseTscObj(*(int64_t *)taos);
×
3052

3053
  if (code != 0) {
×
3054
    tscError("taos conn failed to check alive, code:%d - %s", code, tstrerror(code));
×
3055
  }
3056

3057
  return code != 0 ? 0 : 1;
×
3058
}
3059
static int32_t buildInstanceRegisterSql(const SInstanceRegisterReq *req, char **ppSql, uint32_t *pLen) {
×
3060
  const char *action = (req->expire < 0) ? "UNREGISTER" : "REGISTER";
×
3061
  int32_t     len = 0;
×
3062

3063
  len += snprintf(NULL, 0, "%s INSTANCE '%s'", action, req->id);
×
3064
  if (req->type[0] != 0) {
×
3065
    len += snprintf(NULL, 0, " TYPE '%s'", req->type);
×
3066
  }
3067
  if (req->desc[0] != 0) {
×
3068
    len += snprintf(NULL, 0, " DESC '%s'", req->desc);
×
3069
  }
3070
  if (req->expire >= 0) {
×
3071
    len += snprintf(NULL, 0, " EXPIRE %d", req->expire);
×
3072
  }
3073

3074
  char *sql = taosMemoryMalloc((size_t)len + 1);
×
3075
  if (sql == NULL) {
×
3076
    return terrno;
×
3077
  }
3078

3079
  int32_t offset = snprintf(sql, (size_t)len + 1, "%s INSTANCE '%s'", action, req->id);
×
3080
  if (req->type[0] != 0) {
×
3081
    offset += snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " TYPE '%s'", req->type);
×
3082
  }
3083
  if (req->desc[0] != 0) {
×
3084
    offset += snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " DESC '%s'", req->desc);
×
3085
  }
3086
  if (req->expire >= 0) {
×
3087
    (void)snprintf(sql + offset, (size_t)len + 1 - (size_t)offset, " EXPIRE %d", req->expire);
×
3088
  }
3089

3090
  *ppSql = sql;
×
3091
  if (pLen != NULL) {
×
3092
    *pLen = (uint32_t)len;
×
3093
  }
3094
  return TSDB_CODE_SUCCESS;
×
3095
}
3096

3097
static int32_t sendInstanceRegisterReq(STscObj *pObj, const SInstanceRegisterReq *req) {
×
3098
  SRequestObj *pRequest = NULL;
×
3099
  int32_t      code = createRequest(pObj->id, TDMT_MND_REGISTER_INSTANCE, 0, &pRequest);
×
3100
  if (code != TSDB_CODE_SUCCESS) {
×
3101
    terrno = code;
×
3102
    return code;
×
3103
  }
3104

3105
  code = buildInstanceRegisterSql(req, &pRequest->sqlstr, (uint32_t *)&pRequest->sqlLen);
×
3106
  if (code != TSDB_CODE_SUCCESS) {
×
3107
    goto _cleanup;
×
3108
  }
3109

3110
  int32_t msgLen = tSerializeSInstanceRegisterReq(NULL, 0, (SInstanceRegisterReq *)req);
×
3111
  if (msgLen <= 0) {
×
3112
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3113
    goto _cleanup;
×
3114
  }
3115

3116
  void *pMsg = taosMemoryMalloc(msgLen);
×
3117
  if (pMsg == NULL) {
×
3118
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3119
    goto _cleanup;
×
3120
  }
3121

3122
  if (tSerializeSInstanceRegisterReq(pMsg, msgLen, (SInstanceRegisterReq *)req) < 0) {
×
3123
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3124
    taosMemoryFree(pMsg);
×
3125
    goto _cleanup;
×
3126
  }
3127

3128
  pRequest->type = TDMT_MND_REGISTER_INSTANCE;
×
3129
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsg, .len = msgLen, .handle = NULL};
×
3130

3131
  SMsgSendInfo *pSend = buildMsgInfoImpl(pRequest);
×
3132
  if (pSend == NULL) {
×
3133
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3134
    taosMemoryFree(pMsg);
×
3135
    pRequest->body.requestMsg.pData = NULL;
×
3136
    goto _cleanup;
×
3137
  }
3138

3139
  SEpSet epSet = getEpSet_s(&pObj->pAppInfo->mgmtEp);
×
3140
  code = asyncSendMsgToServer(pObj->pAppInfo->pTransporter, &epSet, NULL, pSend);
×
3141
  if (code != TSDB_CODE_SUCCESS) {
×
3142
    destroySendMsgInfo(pSend);
×
3143
    pRequest->body.requestMsg = (SDataBuf){0};
×
3144
    goto _cleanup;
×
3145
  }
3146

3147
  code = tsem_wait(&pRequest->body.rspSem);
×
3148
  if (code != TSDB_CODE_SUCCESS) {
×
3149
    code = terrno != 0 ? terrno : code;
×
3150
    goto _cleanup;
×
3151
  }
3152

3153
  code = pRequest->code;
×
3154
  terrno = code;
×
3155

3156
_cleanup:
×
3157
  destroyRequest(pRequest);
×
3158
  return code;
×
3159
}
3160

3161
static bool instanceRegisterRpcRfp(int32_t code, tmsg_t msgType) {
×
3162
  if (NEED_REDIRECT_ERROR(code)) {
×
3163
    return true;
×
3164
  } else if (code == TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY || code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE ||
×
3165
             code == TSDB_CODE_SYN_WRITE_STALL || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
×
3166
             code == TSDB_CODE_SYN_RESTORING) {
3167
    tscDebug("client msg type %s should retry since %s", TMSG_INFO(msgType), tstrerror(code));
×
3168
    return true;
×
3169
  } else {
3170
    return false;
×
3171
  }
3172
}
3173

3174
/** Build epSet from firstEp and secondEp in config. pCfg must have valid firstEp. */
3175
static int32_t instanceBuildEpSetFromCfg(SConfig *pCfg, SEpSet *pEpSet) {
5,742✔
3176
  SConfigItem *pFirstEpItem = cfgGetItem(pCfg, "firstEp");
5,742✔
3177
  if (pFirstEpItem == NULL || pFirstEpItem->str == NULL || pFirstEpItem->str[0] == 0) {
5,742✔
3178
    return TSDB_CODE_CFG_NOT_FOUND;
×
3179
  }
3180
  SEp firstEp = {0};
5,742✔
3181
  int32_t code = taosGetFqdnPortFromEp(pFirstEpItem->str, &firstEp);
5,742✔
3182
  if (code != TSDB_CODE_SUCCESS) {
5,742✔
3183
    return code;
×
3184
  }
3185
  pEpSet->inUse = 0;
5,742✔
3186
  pEpSet->numOfEps = 1;
5,742✔
3187
  tstrncpy(pEpSet->eps[0].fqdn, firstEp.fqdn, TSDB_FQDN_LEN);
5,742✔
3188
  pEpSet->eps[0].port = firstEp.port;
5,742✔
3189

3190
  SConfigItem *pSecondEpItem = cfgGetItem(pCfg, "secondEp");
5,742✔
3191
  if (pSecondEpItem != NULL && pSecondEpItem->str != NULL && pSecondEpItem->str[0] != 0) {
5,742✔
3192
    SEp secondEp = {0};
5,742✔
3193
    if (taosGetFqdnPortFromEp(pSecondEpItem->str, &secondEp) == TSDB_CODE_SUCCESS) {
5,742✔
3194
      tstrncpy(pEpSet->eps[1].fqdn, secondEp.fqdn, TSDB_FQDN_LEN);
5,742✔
3195
      pEpSet->eps[1].port = secondEp.port;
5,742✔
3196
      pEpSet->numOfEps = 2;
5,742✔
3197
    }
3198
  }
3199
  return TSDB_CODE_SUCCESS;
5,742✔
3200
}
3201

3202
/** Init and open instance RPC client. label e.g. "INST" or "LIST". Returns handle or NULL. */
3203
static void *instanceOpenRpcClient(const char *label) {
5,742✔
3204
  SRpcInit rpcInit = {0};
5,742✔
3205
  rpcInit.label = (char *)label;
5,742✔
3206
  rpcInit.numOfThreads = 1;
5,742✔
3207
  rpcInit.cfp = NULL;
5,742✔
3208
  rpcInit.sessions = 16;
5,742✔
3209
  rpcInit.connType = TAOS_CONN_CLIENT;
5,742✔
3210
  rpcInit.idleTime = tsShellActivityTimer * 1000;
5,742✔
3211
  rpcInit.compressSize = tsCompressMsgSize;
5,742✔
3212
  rpcInit.user = TSDB_DEFAULT_USER;
5,742✔
3213
  rpcInit.rfp = instanceRegisterRpcRfp;
5,742✔
3214
  rpcInit.retryMinInterval = tsRedirectPeriod;
5,742✔
3215
  rpcInit.retryStepFactor = tsRedirectFactor;
5,742✔
3216
  rpcInit.retryMaxInterval = tsRedirectMaxPeriod;
5,742✔
3217
  rpcInit.retryMaxTimeout = tsMaxRetryWaitTime;
5,742✔
3218

3219
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
5,742✔
3220
  connLimitNum = TMAX(connLimitNum, 10);
5,742✔
3221
  connLimitNum = TMIN(connLimitNum, 500);
5,742✔
3222
  rpcInit.connLimitNum = connLimitNum;
5,742✔
3223
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
5,742✔
3224
  rpcInit.readTimeout = tsReadTimeout;
5,742✔
3225
  rpcInit.ipv6 = tsEnableIpv6;
5,742✔
3226
  rpcInit.enableSSL = tsEnableTLS;
5,742✔
3227

3228
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
5,742✔
3229
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
5,742✔
3230
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
5,742✔
3231
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
5,742✔
3232
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
5,742✔
3233

3234
  int32_t code = taosVersionStrToInt(td_version, &rpcInit.compatibilityVer);
5,742✔
3235
  if (code != TSDB_CODE_SUCCESS) {
5,742✔
3236
    tscError("failed to convert taos version from str to int, errcode:%s", terrstr(code));
×
3237
    terrno = code;
×
3238
    return NULL;
×
3239
  }
3240

3241
  void *clientRpc = rpcOpen(&rpcInit);
5,742✔
3242
  if (clientRpc == NULL) {
5,742✔
3243
    tscError("failed to init instance rpc client since %s", tstrerror(terrno));
×
3244
  }
3245
  return clientRpc;
5,742✔
3246
}
3247

3248
int32_t taos_register_instance(const char *id, const char *type, const char *desc, int32_t expire) {
3,168✔
3249
  if (id == NULL || id[0] == 0) {
3,168✔
3250
    return terrno = TSDB_CODE_INVALID_PARA;
×
3251
  }
3252

3253
  // Validate string lengths
3254
  size_t idLen = strlen(id);
3,168✔
3255
  if (idLen >= TSDB_INSTANCE_ID_LEN) {
3,168✔
3256
    tscError("instance id length %zu exceeds limit %d", idLen, TSDB_INSTANCE_ID_LEN - 1);
×
3257
    return terrno = TSDB_CODE_INVALID_PARA;
×
3258
  }
3259

3260
  if (type != NULL && type[0] != 0) {
3,168✔
3261
    size_t typeLen = strlen(type);
1,980✔
3262
    if (typeLen >= TSDB_INSTANCE_TYPE_LEN) {
1,980✔
3263
      tscError("instance type length %zu exceeds limit %d", typeLen, TSDB_INSTANCE_TYPE_LEN - 1);
×
3264
      return terrno = TSDB_CODE_INVALID_PARA;
×
3265
    }
3266
  }
3267

3268
  if (desc != NULL && desc[0] != 0) {
3,168✔
3269
    size_t descLen = strlen(desc);
1,980✔
3270
    if (descLen >= TSDB_INSTANCE_DESC_LEN) {
1,980✔
3271
      tscError("instance desc length %zu exceeds limit %d", descLen, TSDB_INSTANCE_DESC_LEN - 1);
×
3272
      return terrno = TSDB_CODE_INVALID_PARA;
×
3273
    }
3274
  }
3275

3276
  int32_t code = taos_init();
3,168✔
3277
  if (code != TSDB_CODE_SUCCESS) {
3,168✔
3278
    return code;
×
3279
  }
3280

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

3286
  SEpSet epSet = {0};
3,168✔
3287
  code = instanceBuildEpSetFromCfg(pCfg, &epSet);
3,168✔
3288
  if (code != TSDB_CODE_SUCCESS) {
3,168✔
3289
    return terrno = code;
×
3290
  }
3291

3292
  void *clientRpc = instanceOpenRpcClient("INST");
3,168✔
3293
  if (clientRpc == NULL) {
3,168✔
3294
    return terrno;
×
3295
  }
3296

3297
  SRpcMsg rpcMsg = {0};
3,168✔
3298
  SRpcMsg rpcRsp = {0};
3,168✔
3299

3300
  // Prepare request
3301
  SInstanceRegisterReq req = {0};
3,168✔
3302
  tstrncpy(req.id, id, sizeof(req.id));
3,168✔
3303
  if (type != NULL && type[0] != 0) {
3,168✔
3304
    tstrncpy(req.type, type, sizeof(req.type));
1,980✔
3305
  }
3306
  if (desc != NULL && desc[0] != 0) {
3,168✔
3307
    tstrncpy(req.desc, desc, sizeof(req.desc));
1,980✔
3308
  }
3309
  req.expire = expire;
3,168✔
3310

3311
  int32_t contLen = tSerializeSInstanceRegisterReq(NULL, 0, &req);
3,168✔
3312
  if (contLen <= 0) {
3,168✔
3313
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3314
    rpcClose(clientRpc);
×
3315
    return code;
×
3316
  }
3317

3318
  void *pCont = rpcMallocCont(contLen);
3,168✔
3319
  if (pCont == NULL) {
3,168✔
3320
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3321
    rpcClose(clientRpc);
×
3322
    return code;
×
3323
  }
3324

3325
  if (tSerializeSInstanceRegisterReq(pCont, contLen, &req) < 0) {
3,168✔
3326
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3327
    rpcFreeCont(pCont);
×
3328
    rpcClose(clientRpc);
×
3329
    return code;
×
3330
  }
3331

3332
  rpcMsg.pCont = pCont;
3,168✔
3333
  rpcMsg.contLen = contLen;
3,168✔
3334
  rpcMsg.msgType = TDMT_MND_REGISTER_INSTANCE;
3,168✔
3335
  rpcMsg.info.ahandle = (void *)0x9528;  // Different magic number from server status
3,168✔
3336
  rpcMsg.info.notFreeAhandle = 1;
3,168✔
3337

3338
  code = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
3,168✔
3339
  if (TSDB_CODE_SUCCESS != code) {
3,168✔
3340
    tscError("failed to send instance register req since %s", tstrerror(code));
×
3341
    // rpcSendRecv failed, pCont may not be freed, but check _RETURN1 path
3342
    // In error path, rpcSendRecv may free pCont, but we free it here to be safe
3343
    rpcClose(clientRpc);
×
3344
    return code;
×
3345
  }
3346

3347
  if (rpcRsp.code != 0) {
3,168✔
3348
    code = rpcRsp.code;
×
3349
    tscError("instance register failed, code:%s", tstrerror(code));
×
3350
  } else {
3351
    code = TSDB_CODE_SUCCESS;
3,168✔
3352
  }
3353

3354
  if (rpcRsp.pCont != NULL) {
3,168✔
3355
    rpcFreeCont(rpcRsp.pCont);
3,168✔
3356
  }
3357
  rpcClose(clientRpc);
3,168✔
3358

3359
  terrno = code;
3,168✔
3360
  return code;
3,168✔
3361
}
3362

3363
int32_t taos_list_instances(const char *filter_type, char ***pList, int32_t *pCount) {
2,574✔
3364
  if (pList == NULL || pCount == NULL) {
2,574✔
3365
    return TSDB_CODE_INVALID_PARA;
×
3366
  }
3367

3368
  int32_t code = taos_init();
2,574✔
3369
  if (code != TSDB_CODE_SUCCESS) {
2,574✔
3370
    terrno = code;
×
3371
    return code;
×
3372
  }
3373

3374
  SConfig *pCfg = taosGetCfg();
2,574✔
3375
  if (pCfg == NULL) {
2,574✔
3376
    terrno = TSDB_CODE_CFG_NOT_FOUND;
×
3377
    return TSDB_CODE_CFG_NOT_FOUND;
×
3378
  }
3379

3380
  SEpSet epSet = {0};
2,574✔
3381
  code = instanceBuildEpSetFromCfg(pCfg, &epSet);
2,574✔
3382
  if (code != TSDB_CODE_SUCCESS) {
2,574✔
3383
    terrno = code;
×
3384
    return code;
×
3385
  }
3386

3387
  void *clientRpc = instanceOpenRpcClient("LIST");
2,574✔
3388
  if (clientRpc == NULL) {
2,574✔
3389
    return terrno;
×
3390
  }
3391

3392
  SRpcMsg rpcMsg = {0};
2,574✔
3393
  SRpcMsg rpcRsp = {0};
2,574✔
3394

3395
  SInstanceListReq req = {0};
2,574✔
3396
  if (filter_type != NULL && filter_type[0] != 0) {
2,574✔
3397
    tstrncpy(req.filter_type, filter_type, sizeof(req.filter_type));
1,584✔
3398
  }
3399

3400
  // Serialize request to get required length
3401
  int32_t contLen = tSerializeSInstanceListReq(NULL, 0, &req);
2,574✔
3402
  if (contLen <= 0) {
2,574✔
3403
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3404
    rpcClose(clientRpc);
×
3405
    terrno = code;
×
3406
    return code;
×
3407
  }
3408

3409
  // Allocate RPC message buffer (includes message header overhead)
3410
  void *pCont = rpcMallocCont(contLen);
2,574✔
3411
  if (pCont == NULL) {
2,574✔
3412
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3413
    rpcClose(clientRpc);
×
3414
    terrno = code;
×
3415
    return code;
×
3416
  }
3417

3418
  // Serialize request into the content part (after message header)
3419
  if (tSerializeSInstanceListReq(pCont, contLen, &req) < 0) {
2,574✔
3420
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3421
    rpcFreeCont(pCont);
×
3422
    rpcClose(clientRpc);
×
3423
    terrno = code;
×
3424
    return code;
×
3425
  }
3426

3427
  rpcMsg.pCont = pCont;
2,574✔
3428
  rpcMsg.contLen = contLen;
2,574✔
3429
  rpcMsg.msgType = TDMT_MND_LIST_INSTANCES;
2,574✔
3430
  rpcMsg.info.ahandle = (void *)0x9529;  // Different magic number from register
2,574✔
3431
  rpcMsg.info.notFreeAhandle = 1;
2,574✔
3432

3433
  code = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
2,574✔
3434
  if (TSDB_CODE_SUCCESS != code) {
2,574✔
3435
    tscError("failed to send instance list req since %s", tstrerror(code));
×
3436
    rpcFreeCont(pCont);
×
3437
    rpcClose(clientRpc);
×
3438
    terrno = code;
×
3439
    return code;
×
3440
  }
3441

3442
  // Check response - rpcRsp.code contains the result code from mnode
3443
  if (rpcRsp.code != 0) {
2,574✔
3444
    code = rpcRsp.code;
×
3445
    tscError("instance list failed, code:%s", tstrerror(code));
×
3446
    if (rpcRsp.pCont != NULL) {
×
3447
      rpcFreeCont(rpcRsp.pCont);
×
3448
    }
3449
    rpcClose(clientRpc);
×
3450
    terrno = code;
×
3451
    return code;
×
3452
  }
3453

3454
  // Deserialize response
3455
  if (rpcRsp.pCont != NULL && rpcRsp.contLen > 0) {
2,574✔
3456
    SInstanceListRsp rsp = {0};
2,574✔
3457
    code = tDeserializeSInstanceListRsp(rpcRsp.pCont, rpcRsp.contLen, &rsp);
2,574✔
3458
    if (code != TSDB_CODE_SUCCESS) {
2,574✔
3459
      tscError("failed to deserialize instance list rsp, code:%s", tstrerror(code));
×
3460
      if (rsp.ids != NULL) {
×
3461
        for (int32_t i = 0; i < rsp.count; i++) {
×
3462
          if (rsp.ids[i] != NULL) {
×
3463
            taosMemoryFree(rsp.ids[i]);
×
3464
          }
3465
        }
3466
        taosMemoryFree(rsp.ids);
×
3467
        rsp.ids = NULL;
×
3468
      }
3469
      rsp.count = 0;
×
3470
      rpcFreeCont(rpcRsp.pCont);
×
3471
      rpcClose(clientRpc);
×
3472
      terrno = code;
×
3473
      return code;
×
3474
    }
3475
    *pList = rsp.ids;
2,574✔
3476
    *pCount = rsp.count;
2,574✔
3477
  } else {
3478
    *pList = NULL;
×
3479
    *pCount = 0;
×
3480
  }
3481

3482
  if (rpcRsp.pCont != NULL) {
2,574✔
3483
    rpcFreeCont(rpcRsp.pCont);
2,574✔
3484
  }
3485
  rpcClose(clientRpc);
2,574✔
3486

3487
  return TSDB_CODE_SUCCESS;
2,574✔
3488
}
3489

3490
void taos_free_instances(char ***pList, int32_t count) {
1,782✔
3491
  if (pList == NULL || *pList == NULL || count <= 0) {
1,782✔
3492
    return;
×
3493
  }
3494

3495
  // Free each string in the array
3496
  for (int32_t i = 0; i < count; i++) {
5,544✔
3497
    if ((*pList)[i] != NULL) {
3,762✔
3498
      taosMemoryFree((*pList)[i]);
3,762✔
3499
      (*pList)[i] = NULL;
3,762✔
3500
    }
3501
  }
3502

3503
  // Free the array itself
3504
  taosMemoryFree(*pList);
1,782✔
3505
  *pList = NULL;
1,782✔
3506
}
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