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

taosdata / TDengine / #4994

18 Mar 2026 06:36AM UTC coverage: 72.244%. Remained the same
#4994

push

travis-ci

web-flow
Merge cab2af0c8 into b1e934936

143 of 168 new or added lines in 10 files covered. (85.12%)

1172 existing lines in 10 files now uncovered.

245100 of 339268 relevant lines covered (72.24%)

354029556.59 hits per line

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

53.99
/source/client/src/clientMain.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "catalog.h"
17
#include "clientInt.h"
18
#include "clientLog.h"
19
#include "clientMonitor.h"
20
#include "clientSession.h"
21
#include "clientStmt.h"
22
#include "clientStmt2.h"
23
#include "functionMgt.h"
24
#include "os.h"
25
#include "query.h"
26
#include "scheduler.h"
27
#include "tcompare.h"
28
#include "tconv.h"
29
#include "tdatablock.h"
30
#include "tglobal.h"
31
#include "tmisce.h"
32
#include "tmsg.h"
33
#include "tref.h"
34
#include "trpc.h"
35
#include "ttime.h"
36
#include "tversion.h"
37
#include "version.h"
38

39
#define TSC_VAR_NOT_RELEASE 1
40
#define TSC_VAR_RELEASED    0
41

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

46
static int32_t sentinel = TSC_VAR_NOT_RELEASE;
47
static int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper);
48

49
int taos_options(TSDB_OPTION option, const void *arg, ...) {
2,100,661,093✔
50
  if (arg == NULL) {
2,100,661,093✔
51
    return TSDB_CODE_INVALID_PARA;
×
52
  }
53
  static int32_t lock = 0;
54

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

61
  int ret = taos_options_imp(option, (const char *)arg);
49,486,386✔
62
  atomic_store_32(&lock, 0);
2,100,738,580✔
63
  return ret;
2,100,736,809✔
64
}
65

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

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

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

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

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

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

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

134
END:
50,531✔
135
  return tz;
83,490✔
136
}
137
#endif
138

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

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

150
  if (option < TSDB_OPTION_CONNECTION_CLEAR || option >= TSDB_MAX_OPTIONS_CONNECTION) {
211,945✔
151
    return terrno = TSDB_CODE_INVALID_PARA;
4,393✔
152
  }
153

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

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

166
  if (option == TSDB_OPTION_CONNECTION_CLEAR) {
207,552✔
167
    val = NULL;
4,393✔
168
  }
169

170
#ifndef DISALLOW_NCHAR_WITHOUT_ICONV
171
  if (option == TSDB_OPTION_CONNECTION_CHARSET || option == TSDB_OPTION_CONNECTION_CLEAR) {
207,552✔
172
    if (val != NULL) {
39,514✔
173
      if (!taosValidateEncodec(val)) {
26,335✔
174
        code = terrno;
6,578✔
175
        goto END;
6,578✔
176
      }
177
      void *tmp = taosConvInit(val);
19,757✔
178
      if (tmp == NULL) {
19,757✔
179
        code = terrno;
4,393✔
180
        goto END;
4,393✔
181
      }
182
      pObj->optionInfo.charsetCxt = tmp;
15,364✔
183
    } else {
184
      pObj->optionInfo.charsetCxt = NULL;
13,179✔
185
    }
186
  }
187
#endif
188
  if (option == TSDB_OPTION_CONNECTION_TIMEZONE || option == TSDB_OPTION_CONNECTION_CLEAR) {
196,581✔
189
#if !defined(WINDOWS) && !defined(TD_ASTRA)
190
    if (val != NULL) {
92,276✔
191
      if (val[0] == 0) {
83,490✔
192
        val = "UTC";
4,393✔
193
      }
194
      timezone_t tz = setConnnectionTz(val);
83,490✔
195
      if (tz == NULL) {
83,490✔
196
        code = terrno;
×
197
        goto END;
×
198
      }
199
      pObj->optionInfo.timezone = tz;
83,490✔
200
    } else {
201
      pObj->optionInfo.timezone = NULL;
8,786✔
202
    }
203
#endif
204
  }
205

206
  if (option == TSDB_OPTION_CONNECTION_USER_APP || option == TSDB_OPTION_CONNECTION_CLEAR) {
196,581✔
207
    if (val != NULL) {
25,806✔
208
      tstrncpy(pObj->optionInfo.userApp, val, sizeof(pObj->optionInfo.userApp));
17,020✔
209
    } else {
210
      pObj->optionInfo.userApp[0] = 0;
8,786✔
211
    }
212
  }
213

214
  if (option == TSDB_OPTION_CONNECTION_CONNECTOR_INFO || option == TSDB_OPTION_CONNECTION_CLEAR) {
196,581✔
215
    if (val != NULL) {
25,806✔
216
      tstrncpy(pObj->optionInfo.cInfo, val, sizeof(pObj->optionInfo.cInfo));
17,020✔
217
    } else {
218
      pObj->optionInfo.cInfo[0] = 0;
8,786✔
219
    }
220
  }
221

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

244
END:
154,859✔
245
  releaseTscObj(*(int64_t *)taos);
207,552✔
246
  return terrno = code;
207,552✔
247
}
248

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

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

260
  monitorClose();
31,542,867✔
261
  tscStopCrashReport();
31,542,867✔
262

263
  hbMgrCleanUp();
31,542,867✔
264

265
  catalogDestroy();
31,542,867✔
266
  schedulerDestroy();
31,542,867✔
267

268
  fmFuncMgtDestroy();
31,542,867✔
269
  qCleanupKeywordsTable();
31,542,867✔
270

271
#if !defined(WINDOWS) && !defined(TD_ASTRA)
272
  tzCleanup();
31,542,867✔
273
#endif
274
  tmqMgmtClose();
31,542,867✔
275

276
  int32_t id = clientReqRefPool;
31,542,867✔
277
  clientReqRefPool = -1;
31,542,867✔
278
  taosCloseRef(id);
31,542,867✔
279

280
  id = clientConnRefPool;
31,542,867✔
281
  clientConnRefPool = -1;
31,542,867✔
282
  taosCloseRef(id);
31,542,867✔
283

284
  nodesDestroyAllocatorSet();
31,542,867✔
285
  cleanupAppInfo();
31,542,867✔
286
  rpcCleanup();
31,542,867✔
287
  tscDebug("rpc cleanup");
31,542,867✔
288

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

293
  sessMgtDestroy();
31,542,867✔
294

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

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

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

320
TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) {
2,146,879,018✔
321
  tscInfo("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
2,146,879,018✔
322
  if (user == NULL) {
2,146,899,764✔
323
    user = TSDB_DEFAULT_USER;
4,433,802✔
324
  }
325

326
  if (pass == NULL) {
2,146,899,764✔
327
    pass = TSDB_DEFAULT_PASS;
4,433,802✔
328
  }
329

330
  STscObj *pObj = NULL;
2,146,899,764✔
331
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
2,146,899,764✔
332
  if (TSDB_CODE_SUCCESS == code) {
2,146,199,069✔
333
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
2,146,156,197✔
334
    if (NULL == rid) {
2,146,235,340✔
335
      tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db);
×
336
      return NULL;
×
337
    }
338
    *rid = pObj->id;
2,146,235,340✔
339
    return (TAOS *)rid;
2,146,251,394✔
340
  } else {
341
    terrno = code;
291,939✔
342
  }
343

344
  return NULL;
295,987✔
345
}
346

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

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

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

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

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

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

391
  if (options && options->count > 0) {
13,110✔
392
    size_t count = (size_t)options->count;
10,925✔
393
    for (size_t i = 0; i < count; ++i) {
54,625✔
394
      const char *key = options->keys[i];
43,700✔
395
      const char *value = options->values[i];
43,700✔
396
      if (key == NULL || value == NULL) {
43,700✔
397
        tscWarn("taos_connect_with option key or value is NULL, index: %zu", i);
6,555✔
398
        continue;
6,555✔
399
      }
400

401
      if (strcmp(key, "ip") == 0) {
37,145✔
402
        ip = value;
8,740✔
403
      } else if (strcmp(key, "user") == 0) {
28,405✔
404
        user = value;
2,185✔
405
      } else if (strcmp(key, "pass") == 0) {
26,220✔
406
        pass = value;
2,185✔
407
      } else if (strcmp(key, "db") == 0) {
24,035✔
408
        db = value;
2,185✔
409
      } else if (strcmp(key, "port") == 0) {
21,850✔
410
        port = (uint16_t)taosStr2Int32(value, NULL, 10);
6,555✔
411
      } else if (strcmp(key, "charset") == 0) {
15,295✔
412
        charset = value;
4,370✔
413
      } else if (strcmp(key, "timezone") == 0) {
10,925✔
414
        timezone = value;
2,185✔
415
      } else if (strcmp(key, "userIp") == 0) {
8,740✔
416
        userIp = value;
2,185✔
417
      } else if (strcmp(key, "userApp") == 0) {
6,555✔
418
        userApp = value;
2,185✔
419
      } else if (strcmp(key, "connectorInfo") == 0) {
4,370✔
420
        connectorInfo = value;
2,185✔
421
      } else {
422
        tscWarn("taos_connect_with unknown option key: %s", key);
2,185✔
423
      }
424
    }
425
  }
426

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

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

437
  return taos;
8,740✔
438
}
439

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

633
  __taos_async_ip_whitelist_fn_t userCbFn;
634
} SFetchIpWhiteListInfo;
635

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

640
  SGetUserIpWhiteListRsp wlRsp = {0};
×
641

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

802
  SUserDateTimeWhiteList wlRsp = {0};
×
803

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

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

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

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

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

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

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

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

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

863
void taos_fetch_datetime_whitelist_a(TAOS *taos, __taos_async_datetime_whitelist_fn_t fp, void *param) {
×
864
  if (NULL == taos) {
×
865
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
866
    return;
×
867
  }
868
  int64_t connId = *(int64_t *)taos;
×
869

870
  STscObj *pTsc = acquireTscObj(connId);
×
871
  if (NULL == pTsc) {
×
872
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
873
    return;
×
874
  }
875

876
  SGetUserWhiteListReq req;
×
877
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
878
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
879
  if (msgLen < 0) {
×
880
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
881
    releaseTscObj(connId);
×
882
    return;
×
883
  }
884

885
  void *pReq = taosMemoryMalloc(msgLen);
×
886
  if (pReq == NULL) {
×
887
    fp(param, terrno, taos, 0, NULL);
×
888
    releaseTscObj(connId);
×
889
    return;
×
890
  }
891

892
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
893
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
894
    taosMemoryFree(pReq);
×
895
    releaseTscObj(connId);
×
896
    return;
×
897
  }
898

899
  SFetchDateTimeWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchDateTimeWhiteListInfo));
×
900
  if (pParam == NULL) {
×
901
    fp(param, terrno, taos, 0, NULL);
×
902
    taosMemoryFree(pReq);
×
903
    releaseTscObj(connId);
×
904
    return;
×
905
  }
906

907
  pParam->connId = connId;
×
908
  pParam->userCbFn = fp;
×
909
  pParam->userParam = param;
×
910

911
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
912
  if (pSendInfo == NULL) {
×
913
    fp(param, terrno, taos, 0, NULL);
×
914
    taosMemoryFree(pParam);
×
915
    taosMemoryFree(pReq);
×
916
    releaseTscObj(connId);
×
917
    return;
×
918
  }
919

920
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
921
  pSendInfo->requestId = generateRequestId();
×
922
  pSendInfo->requestObjRefId = 0;
×
923
  pSendInfo->param = pParam;
×
924
  pSendInfo->fp = fetchDateTimeWhiteListCallbackFn;
×
925
  pSendInfo->msgType = TDMT_MND_GET_USER_DATETIME_WHITELIST;
×
926

927
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
928
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
929
    tscWarn("failed to async send msg to server");
×
930
  }
931
  releaseTscObj(connId);
×
932
  return;
×
933
}
934

935
void taos_close_internal(void *taos) {
2,147,483,647✔
936
  if (taos == NULL) {
2,147,483,647✔
937
    return;
7,820✔
938
  }
939
  int32_t code = 0;
2,147,483,647✔
940

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

944
  SSessParam para = {.type = SESSION_PER_USER, .value = -1, .noCheck = 1};
2,147,483,647✔
945

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

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

958
void taos_close(TAOS *taos) {
2,146,536,318✔
959
  if (taos == NULL) {
2,146,536,318✔
960
    return;
17,664✔
961
  }
962

963
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2,146,518,654✔
964
  if (NULL == pObj) {
2,146,534,087✔
965
    taosMemoryFree(taos);
×
966
    return;
×
967
  }
968

969
  taos_close_internal(pObj);
2,146,534,087✔
970
  releaseTscObj(*(int64_t *)taos);
2,146,525,508✔
971
  taosMemoryFree(taos);
2,146,342,980✔
972
}
973

974
int taos_errno(TAOS_RES *res) {
2,147,483,647✔
975
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
976
    return terrno;
20,237,332✔
977
  }
978

979
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,147,483,647✔
980
    return 0;
9,305,064✔
981
  }
982

983
  return ((SRequestObj *)res)->code;
2,147,483,647✔
984
}
985

986
const char *taos_errstr(TAOS_RES *res) {
2,147,483,647✔
987
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
988
    if (*(taosGetErrMsg()) == 0) {
20,661,360✔
989
      return (const char *)tstrerror(terrno);
20,664,626✔
990
    } else {
991
      (void)snprintf(taosGetErrMsgReturn(), ERR_MSG_LEN, "%s", taosGetErrMsg());
1,380✔
992
      return (const char *)taosGetErrMsgReturn();
1,380✔
993
    }
994
  }
995

996
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,147,483,647✔
997
    return "success";
×
998
  }
999

1000
  SRequestObj *pRequest = (SRequestObj *)res;
2,147,483,647✔
1001
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
2,147,483,647✔
1002
    return pRequest->msgBuf;
697,629,928✔
1003
  } else {
1004
    return (const char *)tstrerror(pRequest->code);
1,818,070,443✔
1005
  }
1006
}
1007

1008
void taos_free_result(TAOS_RES *res) {
2,147,483,647✔
1009
  if (NULL == res) {
2,147,483,647✔
1010
    return;
323,563,816✔
1011
  }
1012

1013
  tscTrace("res:%p, will be freed", res);
2,147,483,647✔
1014

1015
  if (TD_RES_QUERY(res)) {
2,147,483,647✔
1016
    SRequestObj *pRequest = (SRequestObj *)res;
2,147,483,647✔
1017
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
2,147,483,647✔
1018
    destroyRequest(pRequest);
2,147,483,647✔
1019
    return;
2,147,483,647✔
1020
  }
1021

1022
  SMqRspObj *pRsp = (SMqRspObj *)res;
332,378,980✔
1023
  if (TD_RES_TMQ(res)) {
332,378,980✔
1024
    tDeleteMqDataRsp(&pRsp->dataRsp);
330,634,016✔
1025
    doFreeReqResultInfo(&pRsp->resInfo);
330,617,042✔
1026
  } else if (TD_RES_TMQ_METADATA(res)) {
1,799,865✔
1027
    tDeleteSTaosxRsp(&pRsp->dataRsp);
90,988✔
1028
    doFreeReqResultInfo(&pRsp->resInfo);
90,988✔
1029
  } else if (TD_RES_TMQ_META(res)) {
1,708,877✔
1030
    tDeleteMqMetaRsp(&pRsp->metaRsp);
1,542,426✔
1031
  } else if (TD_RES_TMQ_BATCH_META(res)) {
166,451✔
1032
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
166,451✔
1033
  } else if (TD_RES_TMQ_RAW(res)) {
×
1034
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
1035
  }
1036
  taosMemoryFree(pRsp);
332,357,498✔
1037
}
1038

1039
void taos_kill_query(TAOS *taos) {
22,448✔
1040
  if (NULL == taos) {
22,448✔
1041
    return;
15,479✔
1042
  }
1043

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

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

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

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

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

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

1072
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
2,147,483,647✔
1073
TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqid) {
5,543✔
1074
  return taosQueryImplWithReqid(taos, sql, false, reqid);
5,543✔
1075
}
1076

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

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

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

1098
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
2,147,483,647✔
1099
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
2,147,483,647✔
1100
      return NULL;
14,835✔
1101
    }
1102

1103
    if (pRequest->inCallback) {
2,147,483,647✔
1104
      tscError("can not call taos_fetch_row before query callback ends.");
4,393✔
1105
      terrno = TSDB_CODE_TSC_INVALID_OPERATION;
4,393✔
1106
      return NULL;
4,393✔
1107
    }
1108

1109
    return doAsyncFetchRows(pRequest, true, true);
2,147,483,647✔
1110
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,147,483,647✔
1111
    SMqRspObj      *msg = ((SMqRspObj *)res);
2,147,483,647✔
1112
    SReqResultInfo *pResultInfo = NULL;
2,147,483,647✔
1113
    if (msg->resIter == -1) {
2,147,483,647✔
1114
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
320,297,609✔
1115
        return NULL;
×
1116
      }
1117
    } else {
1118
      pResultInfo = tmqGetCurResInfo(res);
2,147,483,647✔
1119
    }
1120

1121
    if (pResultInfo->current < pResultInfo->numOfRows) {
2,147,483,647✔
1122
      doSetOneRowPtr(pResultInfo);
2,147,483,647✔
1123
      pResultInfo->current += 1;
2,147,483,647✔
1124
      return pResultInfo->row;
2,147,483,647✔
1125
    } else {
1126
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
1,259,921,255✔
1127
        return NULL;
320,272,286✔
1128
      }
1129

1130
      doSetOneRowPtr(pResultInfo);
939,538,638✔
1131
      pResultInfo->current += 1;
939,640,482✔
1132
      return pResultInfo->row;
939,631,995✔
1133
    }
1134
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1135
    return NULL;
×
1136
  } else {
1137
    tscError("invalid result passed to taos_fetch_row");
×
1138
    terrno = TSDB_CODE_TMQ_INVALID_DATA;
×
1139
    return NULL;
×
1140
  }
1141
}
1142

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

1153
    if (row[i] == NULL) {
2,147,483,647✔
1154
      len += snprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
2,147,483,647✔
1155
      continue;
2,147,483,647✔
1156
    }
1157

1158
    switch (fields[i].type) {
2,147,483,647✔
1159
      case TSDB_DATA_TYPE_TINYINT:
254,143,767✔
1160
        len += snprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
254,143,767✔
1161
        break;
254,692,662✔
1162

1163
      case TSDB_DATA_TYPE_UTINYINT:
1,255,800✔
1164
        len += snprintf(str + len, size - len, "%u", *((uint8_t *)row[i]));
1,255,800✔
1165
        break;
1,255,800✔
1166

1167
      case TSDB_DATA_TYPE_SMALLINT:
1,259,871✔
1168
        len += snprintf(str + len, size - len, "%d", *((int16_t *)row[i]));
1,259,871✔
1169
        break;
1,259,871✔
1170

1171
      case TSDB_DATA_TYPE_USMALLINT:
1,255,800✔
1172
        len += snprintf(str + len, size - len, "%u", *((uint16_t *)row[i]));
1,255,800✔
1173
        break;
1,255,800✔
1174

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

1179
      case TSDB_DATA_TYPE_UINT:
1,255,800✔
1180
        len += snprintf(str + len, size - len, "%u", *((uint32_t *)row[i]));
1,255,800✔
1181
        break;
1,255,800✔
1182

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

1187
      case TSDB_DATA_TYPE_UBIGINT:
1,255,800✔
1188
        len += snprintf(str + len, size - len, "%" PRIu64, *((uint64_t *)row[i]));
1,255,800✔
1189
        break;
1,255,800✔
1190

1191
      case TSDB_DATA_TYPE_FLOAT: {
501,203,764✔
1192
        float fv = 0;
501,203,764✔
1193
        fv = GET_FLOAT_VAL(row[i]);
501,203,764✔
1194
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
500,347,658✔
1195
      } break;
506,686,757✔
1196

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

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

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

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

1249
        taosMemoryFree(data);
×
1250
      } break;
×
1251

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

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

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

1278
  return len;
2,147,483,647✔
1279
}
1280

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

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

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

1296
  if (taos_is_update_query(res)) {
×
1297
    return NULL;
×
1298
  }
1299

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

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

1355
const char *taos_get_client_info() { return td_version; }
21,246,986✔
1356

1357
// return int32_t
1358
int taos_affected_rows(TAOS_RES *res) {
2,147,483,647✔
1359
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
2,147,483,647✔
1360
      TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1361
    return 0;
×
1362
  }
1363

1364
  SRequestObj    *pRequest = (SRequestObj *)res;
2,147,483,647✔
1365
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
2,147,483,647✔
1366
  return (int)pResInfo->numOfRows;
2,147,483,647✔
1367
}
1368

1369
// return int64_t
1370
int64_t taos_affected_rows64(TAOS_RES *res) {
32,143,949✔
1371
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
32,143,949✔
1372
      TD_RES_TMQ_BATCH_META(res)) {
32,143,949✔
1373
    return 0;
×
1374
  }
1375

1376
  SRequestObj    *pRequest = (SRequestObj *)res;
32,143,949✔
1377
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
32,143,949✔
1378
  return pResInfo->numOfRows;
32,143,949✔
1379
}
1380

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

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

1396
int taos_select_db(TAOS *taos, const char *db) {
1,005,606✔
1397
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
1,005,606✔
1398
  if (pObj == NULL) {
1,005,606✔
1399
    releaseTscObj(*(int64_t *)taos);
×
1400
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1401
    return TSDB_CODE_TSC_DISCONNECTED;
×
1402
  }
1403

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

1411
  char sql[256] = {0};
1,005,606✔
1412
  (void)snprintf(sql, tListLen(sql), "use %s", db);
1,005,606✔
1413

1414
  TAOS_RES *pRequest = taos_query(taos, sql);
1,005,606✔
1415
  int32_t   code = taos_errno(pRequest);
1,006,917✔
1416

1417
  taos_free_result(pRequest);
1,005,560✔
1418
  releaseTscObj(*(int64_t *)taos);
998,085✔
1419
  return code;
1,005,905✔
1420
}
1421

1422
void taos_stop_query(TAOS_RES *res) {
2,147,483,647✔
1423
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
2,147,483,647✔
1424
      TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1425
    return;
×
1426
  }
1427

1428
  stopAllQueries((SRequestObj *)res);
2,147,483,647✔
1429
}
1430

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

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

1448
bool taos_is_update_query(TAOS_RES *res) { return taos_num_fields(res) == 0; }
10,407,960✔
1449

1450
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
2,147,483,647✔
1451
  int32_t numOfRows = 0;
2,147,483,647✔
1452
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
2,147,483,647✔
1453
  return numOfRows;
2,147,483,647✔
1454
}
1455

1456
int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) {
2,147,483,647✔
1457
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1458
    return 0;
×
1459
  }
1460

1461
  if (TD_RES_QUERY(res)) {
2,147,483,647✔
1462
    SRequestObj *pRequest = (SRequestObj *)res;
2,147,483,647✔
1463

1464
    (*rows) = NULL;
2,147,483,647✔
1465
    (*numOfRows) = 0;
2,147,483,647✔
1466

1467
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
2,147,483,647✔
1468
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
2,147,483,647✔
1469
      return pRequest->code;
33,148,106✔
1470
    }
1471

1472
    (void)doAsyncFetchRows(pRequest, false, true);
2,147,483,647✔
1473

1474
    // TODO refactor
1475
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
2,147,483,647✔
1476
    pResultInfo->current = pResultInfo->numOfRows;
2,147,483,647✔
1477

1478
    (*rows) = pResultInfo->row;
2,147,483,647✔
1479
    (*numOfRows) = pResultInfo->numOfRows;
2,147,483,647✔
1480
    return pRequest->code;
2,147,483,647✔
1481
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
18,266,623✔
1482
    SReqResultInfo *pResultInfo = NULL;
18,256,825✔
1483
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
18,256,825✔
1484
    if (code != 0) return code;
18,256,825✔
1485

1486
    pResultInfo->current = pResultInfo->numOfRows;
9,162,993✔
1487
    (*rows) = pResultInfo->row;
9,162,993✔
1488
    (*numOfRows) = pResultInfo->numOfRows;
9,154,253✔
1489
    return 0;
9,154,253✔
1490
  } else {
1491
    tscError("taos_fetch_block_s invalid res type");
×
1492
    return TSDB_CODE_TMQ_INVALID_DATA;
×
1493
  }
1494
}
1495

1496
int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) {
147,522✔
1497
  *numOfRows = 0;
147,522✔
1498
  *pData = NULL;
147,522✔
1499

1500
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
147,522✔
1501
    return 0;
×
1502
  }
1503

1504
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
147,522✔
1505
    SReqResultInfo *pResultInfo = NULL;
56,626✔
1506
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
56,626✔
1507
    if (code != 0) {
56,626✔
1508
      (*numOfRows) = 0;
28,313✔
1509
      return 0;
28,313✔
1510
    }
1511

1512
    pResultInfo->current = pResultInfo->numOfRows;
28,313✔
1513
    (*numOfRows) = pResultInfo->numOfRows;
28,313✔
1514
    (*pData) = (void *)pResultInfo->pData;
28,313✔
1515
    return 0;
28,313✔
1516
  }
1517

1518
  SRequestObj *pRequest = (SRequestObj *)res;
90,896✔
1519

1520
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
90,896✔
1521
      pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
90,896✔
1522
    return pRequest->code;
×
1523
  }
1524

1525
  (void)doAsyncFetchRows(pRequest, false, false);
90,896✔
1526

1527
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
90,896✔
1528

1529
  pResultInfo->current = pResultInfo->numOfRows;
90,896✔
1530
  (*numOfRows) = pResultInfo->numOfRows;
90,896✔
1531
  (*pData) = (void *)pResultInfo->pData;
90,896✔
1532

1533
  return pRequest->code;
90,896✔
1534
}
1535

1536
int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) {
2,147,483,647✔
1537
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1538
    return NULL;
×
1539
  }
1540

1541
  int32_t numOfFields = taos_num_fields(res);
2,147,483,647✔
1542
  if (columnIndex < 0 || columnIndex >= numOfFields || numOfFields == 0) {
2,147,483,647✔
1543
    return NULL;
×
1544
  }
1545

1546
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
1547
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
2,147,483,647✔
1548
  if (!IS_VAR_DATA_TYPE(pField->type)) {
2,147,483,647✔
1549
    return NULL;
×
1550
  }
1551

1552
  return pResInfo->pCol[columnIndex].offset;
2,147,483,647✔
1553
}
1554

1555
int taos_is_null_by_column(TAOS_RES *res, int columnIndex, bool result[], int *rows) {
2,147,483,647✔
1556
  if (res == NULL || result == NULL || rows == NULL || *rows <= 0 || columnIndex < 0 || TD_RES_TMQ_META(res) ||
2,147,483,647✔
1557
      TD_RES_TMQ_RAW(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
1558
    return TSDB_CODE_INVALID_PARA;
×
1559
  }
1560

1561
  int32_t numOfFields = taos_num_fields(res);
2,147,483,647✔
1562
  if (columnIndex >= numOfFields || numOfFields == 0) {
2,147,483,647✔
1563
    return TSDB_CODE_INVALID_PARA;
×
1564
  }
1565

1566
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
1567
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
2,147,483,647✔
1568
  SResultColumn  *pCol = &pResInfo->pCol[columnIndex];
2,147,483,647✔
1569

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

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

1596
  int code = taos_errno(pObj);
×
1597

1598
  taos_free_result(pObj);
×
1599
  return code;
×
1600
}
1601

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

1609
  resetConnectDB(pTscObj);
×
1610

1611
  releaseTscObj(*(int64_t *)taos);
×
1612
}
1613

1614
const char *taos_get_server_info(TAOS *taos) {
113,505✔
1615
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
113,505✔
1616
  if (pTscObj == NULL) {
113,505✔
1617
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1618
    return NULL;
×
1619
  }
1620

1621
  releaseTscObj(*(int64_t *)taos);
113,505✔
1622

1623
  return pTscObj->sDetailVer;
113,505✔
1624
}
1625

1626
int taos_get_current_db(TAOS *taos, char *database, int len, int *required) {
54,740✔
1627
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
54,740✔
1628
  if (pTscObj == NULL) {
54,740✔
1629
    return TSDB_CODE_TSC_DISCONNECTED;
×
1630
  }
1631

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

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

1659
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
4,370✔
1660
  if (pTscObj == NULL) {
4,370✔
1661
    return TSDB_CODE_TSC_DISCONNECTED;
×
1662
  }
1663

1664
  int code = TSDB_CODE_SUCCESS;
4,370✔
1665
  (void)taosThreadMutexLock(&pTscObj->mutex);
4,370✔
1666

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

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

1694
    default:
×
1695
      TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1696
  }
1697

1698
_return:
×
1699
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
4,370✔
1700
  releaseTscObj(*(int64_t *)taos);
4,370✔
1701
  return code;
4,370✔
1702
}
1703

1704
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
2,147,483,647✔
1705
  if (NULL == pWrapper) {
2,147,483,647✔
1706
    return;
2,147,483,647✔
1707
  }
1708
  destoryCatalogReq(pWrapper->pCatalogReq);
2,147,483,647✔
1709
  taosMemoryFree(pWrapper->pCatalogReq);
2,147,483,647✔
1710
  qDestroyParseContext(pWrapper->pParseCtx);
2,147,483,647✔
1711
  taosMemoryFree(pWrapper);
2,147,483,647✔
1712
}
1713

1714
void destroyCtxInRequest(SRequestObj *pRequest) {
59,182,910✔
1715
  schedulerFreeJob(&pRequest->body.queryJob, 0);
59,182,910✔
1716
  qDestroyQuery(pRequest->pQuery);
59,182,910✔
1717
  pRequest->pQuery = NULL;
59,182,910✔
1718
  destorySqlCallbackWrapper(pRequest->pWrapper);
59,182,910✔
1719
  pRequest->pWrapper = NULL;
59,182,910✔
1720
}
59,182,910✔
1721

1722
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
2,147,483,647✔
1723
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
2,147,483,647✔
1724
  SRequestObj         *pRequest = pWrapper->pRequest;
2,147,483,647✔
1725
  SQuery              *pQuery = pRequest->pQuery;
2,147,483,647✔
1726

1727
  qDebug("req:0x%" PRIx64 ", start to semantic analysis, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
2,147,483,647✔
1728

1729
  int64_t analyseStart = taosGetTimestampUs();
2,147,483,647✔
1730
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
2,147,483,647✔
1731
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
2,147,483,647✔
1732

1733
  if (TSDB_CODE_SUCCESS == code) {
2,147,483,647✔
1734
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
2,147,483,647✔
1735
  }
1736

1737
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
2,147,483,647✔
1738

1739
  if (pRequest->parseOnly) {
2,147,483,647✔
1740
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
6,927,347✔
1741
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
6,927,347✔
1742
  }
1743

1744
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
2,147,483,647✔
1745
}
2,147,483,647✔
1746

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

1774
    *ppTarget = pTarget;
×
1775
  }
1776

1777
  return code;
×
1778
}
1779

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

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

1811
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
2,147,483,647✔
1812
  SRequestObj *pRequest = pWrapper->pRequest;
2,147,483,647✔
1813
  SQuery      *pQuery = pRequest->pQuery;
2,147,483,647✔
1814

1815
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
2,147,483,647✔
1816
    SNode *prevRoot = pQuery->pPrevRoot;
×
1817
    pQuery->pPrevRoot = NULL;
×
1818
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
×
1819
    return;
×
1820
  }
1821

1822
  if (code == TSDB_CODE_SUCCESS) {
2,147,483,647✔
1823
    pRequest->stableQuery = pQuery->stableQuery;
2,147,483,647✔
1824
    if (pQuery->pRoot) {
2,147,483,647✔
1825
      pRequest->stmtType = pQuery->pRoot->type;
2,147,483,647✔
1826
      if (nodeType(pQuery->pRoot) == QUERY_NODE_DELETE_STMT) {
2,147,483,647✔
1827
        pRequest->secureDelete = ((SDeleteStmt*)pQuery->pRoot)->secureDelete;
37,874,284✔
1828
      }
1829
    }
1830

1831
    if (pQuery->haveResultSet) {
2,147,483,647✔
1832
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
2,147,483,647✔
1833
                              pRequest->stmtBindVersion > 0);
2,147,483,647✔
1834
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
2,147,483,647✔
1835
    }
1836
  }
1837

1838
  if (code == TSDB_CODE_SUCCESS) {
2,147,483,647✔
1839
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
2,147,483,647✔
1840
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
2,147,483,647✔
1841
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
2,147,483,647✔
1842

1843
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
2,147,483,647✔
1844
  } else {
1845
    destorySqlCallbackWrapper(pWrapper);
1,865,042,538✔
1846
    pRequest->pWrapper = NULL;
1,865,423,257✔
1847
    qDestroyQuery(pRequest->pQuery);
1,865,423,257✔
1848
    pRequest->pQuery = NULL;
1,865,374,221✔
1849

1850
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
1,865,374,221✔
1851
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
57,143,845✔
1852
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1853
      restartAsyncQuery(pRequest, code);
57,143,845✔
1854
      return;
57,143,845✔
1855
    }
1856

1857
    // return to app directly
1858
    tscError("req:0x%" PRIx64 ", error occurs, code:%s, return to user app, QID:0x%" PRIx64, pRequest->self,
1,808,242,336✔
1859
             tstrerror(code), pRequest->requestId);
1860
    pRequest->code = code;
1,808,813,955✔
1861
    returnToUser(pRequest);
1,808,813,955✔
1862
  }
1863
}
1864

1865
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
2,147,483,647✔
1866
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
2,147,483,647✔
1867
                           .requestId = pWrapper->pParseCtx->requestId,
2,147,483,647✔
1868
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
2,147,483,647✔
1869
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
2,147,483,647✔
1870

1871
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
2,147,483,647✔
1872

1873
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
2,147,483,647✔
1874
                                &pWrapper->pRequest->body.queryJob);
2,147,483,647✔
1875
}
1876

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

1879
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
2,147,483,647✔
1880
  int32_t      code = TSDB_CODE_SUCCESS;
2,147,483,647✔
1881
  SRequestObj *pRequest = pWrapper->pRequest;
2,147,483,647✔
1882
  switch (pRequest->pQuery->execStage) {
168,053,640✔
1883
    case QUERY_EXEC_STAGE_PARSE: {
1884
      if (atomic_load_32(&pRequest->execPhase) != QUERY_PHASE_CATALOG) {
168,053,640✔
1885
        atomic_store_32(&pRequest->execPhase, QUERY_PHASE_CATALOG);
168,053,640✔
1886
        atomic_store_64(&pRequest->phaseStartTime, taosGetTimestampMs());
1887
      }
2,147,483,647✔
1888
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
1889
      break;
2,147,483,647✔
1890
    }
2,147,483,647✔
1891
    case QUERY_EXEC_STAGE_ANALYSE: {
1892
      if (atomic_load_32(&pRequest->execPhase) != QUERY_PHASE_CATALOG) {
2,147,483,647✔
1893
        atomic_store_32(&pRequest->execPhase, QUERY_PHASE_CATALOG);
2,147,483,647✔
1894
        atomic_store_64(&pRequest->phaseStartTime, taosGetTimestampMs());
2,147,483,647✔
1895
      }
UNCOV
1896
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
×
UNCOV
1897
      break;
×
1898
    }
1899
    case QUERY_EXEC_STAGE_SCHEDULE: {
2,147,483,647✔
1900
      if (atomic_load_32(&pRequest->execPhase) != QUERY_PHASE_SCHEDULE) {
1901
        atomic_store_32(&pRequest->execPhase, QUERY_PHASE_SCHEDULE);
1902
        atomic_store_64(&pRequest->phaseStartTime, taosGetTimestampMs());
168,053,640✔
1903
      }
168,053,640✔
1904
      launchAsyncQuery(pRequest, pRequest->pQuery, NULL, pWrapper);
168,053,640✔
1905
      break;
168,053,640✔
1906
    }
1907
    default:
168,053,640✔
1908
      break;
168,053,640✔
1909
  }
1910
  return code;
1911
}
168,053,640✔
1912

1913
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code) {
167,477,283✔
1914
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
1915
  SRequestObj         *pRequest = pWrapper->pRequest;
1916
  SQuery              *pQuery = pRequest->pQuery;
168,053,318✔
1917

154,795,750✔
1918
  pRequest->metric.ctgCostUs += taosGetTimestampUs() - pRequest->metric.ctgStart;
1919
  qDebug("req:0x%" PRIx64 ", continue parse query, QID:0x%" PRIx64 ", code:%s", pRequest->self, pRequest->requestId,
1920
         tstrerror(code));
168,053,341✔
1921

13,257,568✔
1922
  if (code == TSDB_CODE_SUCCESS) {
1923
    // pWrapper->pCatalogReq->forceUpdate = false;
13,257,568✔
1924
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
13,257,568✔
1925
  }
13,257,568✔
1926

13,257,568✔
1927
  if (TSDB_CODE_SUCCESS == code) {
13,257,568✔
1928
    code = phaseAsyncQuery(pWrapper);
1929
  }
168,053,341✔
1930

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

×
UNCOV
1942
void continueInsertFromCsv(SSqlCallbackWrapper *pWrapper, SRequestObj *pRequest) {
×
UNCOV
1943
  int32_t code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
×
UNCOV
1944
  if (TSDB_CODE_SUCCESS == code) {
×
1945
    code = phaseAsyncQuery(pWrapper);
1946
  }
285,959✔
1947

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

2,147,483,647✔
1959
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
2,147,483,647✔
1960
  int64_t connId = *(int64_t *)taos;
1961
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
2,147,483,647✔
1962
}
2,147,483,647✔
UNCOV
1963

×
1964
void taos_query_a_with_reqid(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param, int64_t reqid) {
1965
  int64_t connId = *(int64_t *)taos;
1966
  taosAsyncQueryImplWithReqid(connId, sql, fp, param, false, reqid);
2,147,483,647✔
1967
}
2,147,483,647✔
1968

2,147,483,647✔
1969
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
2,147,483,647✔
1970
  const STscObj *pTscObj = pRequest->pTscObj;
1971

2,147,483,647✔
1972
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
2,147,483,647✔
1973
  if (*pCxt == NULL) {
2,147,483,647✔
1974
    return terrno;
1975
  }
2,147,483,647✔
1976

1977
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
2,147,483,647✔
1978
                           .requestRid = pRequest->self,
2,147,483,647✔
1979
                           .acctId = pTscObj->acctId,
2,147,483,647✔
1980
                           .db = pRequest->pDb,
2,147,483,647✔
1981
                           .topicQuery = false,
2,147,483,647✔
1982
                           .pSql = pRequest->sqlstr,
2,147,483,647✔
1983
                           .sqlLen = pRequest->sqlLen,
1984
                           .pMsg = pRequest->msgBuf,
2,147,483,647✔
1985
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
2,147,483,647✔
1986
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
2,147,483,647✔
1987
                           .pStmtCb = NULL,
1988
                           .pUser = pTscObj->user,
1989
                           .userId = pTscObj->userId,
1990
                           .pEffectiveUser = pRequest->effectiveUser,
2,147,483,647✔
1991
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
2,147,483,647✔
1992
                           .enableSysInfo = pTscObj->sysInfo,
2,147,483,647✔
1993
                           .privInfo = pWrapper->pParseCtx ? pWrapper->pParseCtx->privInfo : 0,
2,147,483,647✔
1994
                           .async = true,
2,147,483,647✔
1995
                           .svrVer = pTscObj->sVer,
1996
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
1997
                           .allocatorId = pRequest->allocatorRefId,
2,147,483,647✔
1998
                           .parseSqlFp = clientParseSql,
2,147,483,647✔
1999
                           .parseSqlParam = pWrapper,
2,147,483,647✔
2000
                           .setQueryFp = setQueryRequest,
2,147,483,647✔
2001
                           .timezone = pTscObj->optionInfo.timezone,
2,147,483,647✔
UNCOV
2002
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
×
2003
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
2004
  (*pCxt)->biMode = biMode;
2,147,483,647✔
2005
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
2006
}
2,147,483,647✔
2007

2008
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
2009
  int32_t              code = TSDB_CODE_SUCCESS;
2,147,483,647✔
2010
  STscObj             *pTscObj = pRequest->pTscObj;
2,147,483,647✔
2011
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
2012
  if (pWrapper == NULL) {
2013
    code = terrno;
2,147,483,647✔
2014
  } else {
2,147,483,647✔
2015
    pWrapper->pRequest = pRequest;
2,147,483,647✔
2016
    pRequest->pWrapper = pWrapper;
2017
    *ppWrapper = pWrapper;
2018
  }
2,147,483,647✔
2019

2,147,483,647✔
2020
  if (TSDB_CODE_SUCCESS == code) {
2021
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
2,147,483,647✔
2022
  }
2,147,483,647✔
UNCOV
2023

×
2024
  if (TSDB_CODE_SUCCESS == code) {
2025
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
2,147,483,647✔
2026
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
2,147,483,647✔
2027
  }
2,147,483,647✔
2028

2029
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
2030
    int64_t syntaxStart = taosGetTimestampUs();
2,147,483,647✔
2031

2032
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
2033
    if (pWrapper->pCatalogReq == NULL) {
2,147,483,647✔
2034
      code = terrno;
2035
    } else {
2036
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
2,147,483,647✔
2037
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
2,147,483,647✔
2038
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
2,147,483,647✔
2039
    }
2040

2,147,483,647✔
2041
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
19,059,387✔
2042
  }
19,059,387✔
2043

19,059,387✔
2044
  return code;
19,059,387✔
2045
}
19,059,387✔
2046

19,059,387✔
2047
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
2048
  SSqlCallbackWrapper *pWrapper = NULL;
2049
  int32_t              code = TSDB_CODE_SUCCESS;
2,147,483,647✔
2050

2,147,483,647✔
2051
  if (atomic_load_32(&pRequest->execPhase) != QUERY_PHASE_PARSE) {
2052
    atomic_store_32(&pRequest->execPhase, QUERY_PHASE_PARSE);
2053
    atomic_store_64(&pRequest->phaseStartTime, taosGetTimestampMs());
2,147,483,647✔
2054
  }
2,147,483,647✔
2055

2,147,483,647✔
2056
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
2057
    code = pRequest->prevCode;
2058
    terrno = code;
2,147,483,647✔
2059
    pRequest->code = code;
322,404,179✔
2060
    tscDebug("req:0x%" PRIx64 ", call sync query cb with code:%s", pRequest->self, tstrerror(code));
320,630,396✔
2061
    doRequestCallback(pRequest, code);
2062
    return;
2063
  }
1,773,783✔
2064

2065
  if (TSDB_CODE_SUCCESS == code) {
2066
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
2067
  }
322,429,065✔
2068

322,408,020✔
2069
  if (TSDB_CODE_SUCCESS == code) {
322,408,020✔
2070
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
322,405,973✔
2071
    code = phaseAsyncQuery(pWrapper);
2072
  }
322,405,973✔
2073

290,467✔
2074
  if (TSDB_CODE_SUCCESS != code) {
2075
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
290,467✔
2076
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
290,467✔
2077
               pRequest->requestId);
290,467✔
2078
    } else {
2079
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
2080
               pRequest->requestId);
290,467✔
2081
    }
290,467✔
2082

290,467✔
2083
    destorySqlCallbackWrapper(pWrapper);
2084
    pRequest->pWrapper = NULL;
2085
    qDestroyQuery(pRequest->pQuery);
322,115,506✔
2086
    pRequest->pQuery = NULL;
322,109,158✔
2087

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

×
UNCOV
2101
    terrno = code;
×
UNCOV
2102
    pRequest->code = code;
×
2103
    doRequestCallback(pRequest, code);
2104
  }
2105
}
2106

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

×
UNCOV
2139
typedef struct SAsyncFetchParam {
×
2140
  SRequestObj      *pReq;
2141
  __taos_async_fn_t fp;
2,147,483,647✔
UNCOV
2142
  void             *param;
×
UNCOV
2143
} SAsyncFetchParam;
×
UNCOV
2144

×
2145
static int32_t doAsyncFetch(void *pParam) {
2146
  SAsyncFetchParam *param = pParam;
2147
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
2,147,483,647✔
2148
  taosMemoryFree(param);
2,147,483,647✔
2149
  return TSDB_CODE_SUCCESS;
872,482✔
2150
}
872,482✔
2151

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

2,147,483,647✔
UNCOV
2163
  SRequestObj *pRequest = res;
×
NEW
2164

×
NEW
2165
  // Each fetch call sets phase to IN_PROGRESS
×
2166
  if (atomic_load_32(&pRequest->execPhase) != QUERY_PHASE_FETCH_IN_PROGRESS) {
2167
    atomic_store_32(&pRequest->execPhase, QUERY_PHASE_FETCH_IN_PROGRESS);
2168
    atomic_store_64(&pRequest->phaseStartTime, taosGetTimestampMs());
2169
  }
12,236✔
2170

12,236✔
UNCOV
2171
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
×
UNCOV
2172
    fp(param, res, 0);
×
2173
    return;
2174
  }
12,236✔
UNCOV
2175

×
UNCOV
2176
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
×
2177
  if (!pParam) {
2178
    fp(param, res, terrno);
12,236✔
2179
    return;
12,236✔
2180
  }
2181
  pParam->pReq = pRequest;
2182
  pParam->fp = fp;
12,236✔
2183
  pParam->param = param;
2184
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
2185
  if (TSDB_CODE_SUCCESS != code) {
12,236✔
2186
    taosMemoryFree(pParam);
2187
    fp(param, res, code);
2188
    return;
2,208✔
2189
  }
2,208✔
UNCOV
2190
}
×
UNCOV
2191

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

×
UNCOV
2204
  // set the current block is all consumed
×
UNCOV
2205
  pResultInfo->convertUcs4 = false;
×
2206

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

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

2222
  return pRequest->body.resInfo.pData;
UNCOV
2223
}
×
UNCOV
2224

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

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

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

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

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

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

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

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

×
2266
_return:
×
UNCOV
2267

×
2268
  terrno = code;
×
2269

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

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

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

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

2294
  pRequest->syncQuery = true;
×
2295

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

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

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

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

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

×
2317
  *vgId = vgInfo.vgId;
×
UNCOV
2318

×
2319
_return:
×
UNCOV
2320

×
2321
  terrno = code;
×
2322

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

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

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

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

2347
  pRequest->syncQuery = true;
×
UNCOV
2348

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

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

27,554✔
2359
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
27,554✔
2360

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

2366
_return:
2367

27,554✔
2368
  terrno = code;
27,554✔
UNCOV
2369

×
2370
  destroyRequest(pRequest);
27,554✔
2371
  return code;
×
UNCOV
2372
}
×
2373

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

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

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

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

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

2405
  pRequest->syncQuery = true;
2406

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

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

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

×
UNCOV
2422
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
UNCOV
2423

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

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

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

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

307,924✔
UNCOV
2455
  return pStmt;
×
UNCOV
2456
}
×
UNCOV
2457

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

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

×
2472
  return pStmt;
×
UNCOV
2473
}
×
2474

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

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

13,777✔
2489
  return pStmt;
2490
}
2491

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

213,730,651✔
2499
  return stmtPrepare(stmt, sql, length);
213,730,651✔
UNCOV
2500
}
×
UNCOV
2501

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

12,880✔
2509
  int32_t code = stmtSetTbName(stmt, name);
12,880✔
UNCOV
2510
  if (code) {
×
UNCOV
2511
    return code;
×
UNCOV
2512
  }
×
2513

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

2518
  return TSDB_CODE_SUCCESS;
4,416✔
2519
}
2520

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

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

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

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

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

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

×
UNCOV
2550
  return stmtGetTagFields(stmt, fieldNum, fields);
×
UNCOV
2551
}
×
2552

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

2560
  return stmtGetColFields(stmt, fieldNum, fields);
742,532✔
2561
}
2562

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

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

213,805,240✔
2577
  if (bind->num > 1) {
213,802,917✔
2578
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
213,657,833✔
UNCOV
2579
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
UNCOV
2580
    return terrno;
×
2581
  }
2582

213,657,833✔
UNCOV
2583
  return stmtBindBatch(stmt, bind, -1);
×
UNCOV
2584
}
×
UNCOV
2585

×
2586
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
2587
  if (stmt == NULL || bind == NULL) {
2588
    tscError("NULL parameter for %s", __FUNCTION__);
213,657,833✔
2589
    terrno = TSDB_CODE_INVALID_PARA;
2590
    return terrno;
2591
  }
25,760✔
2592

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

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

25,760✔
UNCOV
2611
  return stmtBindBatch(stmt, bind, -1);
×
UNCOV
2612
}
×
UNCOV
2613

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

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

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

UNCOV
2639
  return stmtBindBatch(stmt, bind, colIdx);
×
UNCOV
2640
}
×
UNCOV
2641

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

UNCOV
2649
  return stmtAddBatch(stmt);
×
UNCOV
2650
}
×
UNCOV
2651

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

2659
  return stmtExec(stmt);
8,832✔
2660
}
8,832✔
UNCOV
2661

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

2669
  return stmtIsInsert(stmt, insert);
220,777✔
2670
}
220,777✔
UNCOV
2671

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

2679
  return stmtGetParamNum(stmt, nums);
256,128✔
2680
}
2681

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

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

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

63,894✔
2699
  return stmtUseResult(stmt);
2700
}
2701

810,313✔
2702
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
810,313✔
UNCOV
2703

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

2711
  return stmtAffectedRows(stmt);
2,945,403✔
2712
}
2,945,403✔
2713

2,208✔
2714
int taos_stmt_affected_rows_once(TAOS_STMT *stmt) {
2,208✔
2715
  if (stmt == NULL) {
2,208✔
2716
    tscError("NULL parameter for %s", __FUNCTION__);
2717
    terrno = TSDB_CODE_INVALID_PARA;
2,943,195✔
2718
    return 0;
2,984,066✔
UNCOV
2719
  }
×
UNCOV
2720

×
UNCOV
2721
  return stmtAffectedRowsOnce(stmt);
×
2722
}
2723

2724
int taos_stmt_close(TAOS_STMT *stmt) {
2,984,066✔
2725
  if (stmt == NULL) {
2726
    tscError("NULL parameter for %s", __FUNCTION__);
2,984,480✔
2727
    terrno = TSDB_CODE_INVALID_PARA;
2728
    return terrno;
2,985,262✔
2729
  }
2730

2731
  return stmtClose(stmt);
3,060,610✔
2732
}
3,060,610✔
2733

2,208✔
2734
TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option) {
2,208✔
2735
  if (NULL == taos) {
2,208✔
2736
    tscError("NULL parameter for %s", __FUNCTION__);
2737
    terrno = TSDB_CODE_INVALID_PARA;
2738
    return NULL;
3,058,402✔
2739
  }
2740
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2741
  if (NULL == pObj) {
1,424,900,876✔
2742
    tscError("invalid parameter for %s", __FUNCTION__);
1,424,900,876✔
2743
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2744
    return NULL;
×
UNCOV
2745
  }
×
2746

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

1,424,900,876✔
2749
  releaseTscObj(*(int64_t *)taos);
1,424,900,876✔
2750

1,424,900,876✔
2751
  return pStmt;
2752
}
2753

1,425,322,489✔
2754
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
1,422,955,743✔
2755
  if (stmt == NULL || sql == NULL) {
86,296✔
UNCOV
2756
    tscError("NULL parameter for %s", __FUNCTION__);
×
UNCOV
2757
    terrno = TSDB_CODE_INVALID_PARA;
×
UNCOV
2758
    return terrno;
×
2759
  }
2760

2761
  return stmtPrepare2(stmt, sql, length);
2762
}
1,422,955,743✔
2763

190,601✔
2764
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
190,601✔
UNCOV
2765
  if (stmt == NULL) {
×
2766
    tscError("NULL parameter for %s", __FUNCTION__);
2767
    terrno = TSDB_CODE_INVALID_PARA;
2768
    return terrno;
1,437,216,525✔
2769
  }
50,784✔
UNCOV
2770

×
2771
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
2772
  int32_t    code = TSDB_CODE_SUCCESS;
50,784✔
2773
  STMT2_DLOG_E("start to bind param");
2774

2775
  // check query bind number
2,147,483,647✔
2776
  bool isQuery = (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt)));
1,418,170,133✔
2777
  if (isQuery) {
1,444,114,800✔
2778
    if (bindv->count != 1 || bindv->bind_cols[0]->num != 1) {
1,446,308,586✔
2779
      terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
1,446,217,805✔
2780
      STMT2_ELOG_E("query only support one table and one row bind");
26,328,997✔
2781
      return terrno;
26,322,074✔
2782
    }
24,288✔
2783
  }
24,288✔
2784

39,744✔
2785
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 1) {
2786
    STMT2_ELOG_E("async bind param is still working, please try again later");
2787
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
2788
    return terrno;
1,460,451,769✔
2789
  }
12,498,016✔
2790

1,435,493,618✔
2791
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
2,457,734✔
2792
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
1,423,933,634✔
2793
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
46,368✔
2794
    }
2795
    pStmt->execSemWaited = true;
2796
  }
1,445,170,339✔
2797

2,208✔
2798
  for (int i = 0; i < bindv->count; ++i) {
2,208✔
2799
    SVCreateTbReq *pCreateTbReq = NULL;
2,208✔
UNCOV
2800
    if (!isQuery) {
×
UNCOV
2801
      STMT2_TLOG("start to bind %dth table", i);
×
2802
      if (bindv->tbnames && bindv->tbnames[i]) {
2803
        code = stmtSetTbName2(stmt, bindv->tbnames[i]);
2,208✔
2804
        if (code) {
2805
          terrno = code;
2806
          STMT2_ELOG("set tbname failed, code:%s", stmt2Errstr(stmt));
2807
          return terrno;
1,442,974,345✔
2808
        }
1,474,452,076✔
2809
      }
2810

1,476,493,303✔
2811
      if (bindv->tags && bindv->tags[i]) {
19,078,086✔
2812
        code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
19,078,086✔
UNCOV
2813
      } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
×
UNCOV
2814
        code = stmtCheckTags2(stmt, &pCreateTbReq);
×
UNCOV
2815
      } else if (pStmt->sql.autoCreateTbl) {
×
2816
        code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
UNCOV
2817
      }
×
2818

2819
      if (code) {
2820
        terrno = code;
1,451,555,162✔
2821
        STMT2_ELOG("set tags failed, code:%s", stmt2Errstr(stmt));
1,419,187,768✔
2822
        if (pCreateTbReq) {
13,248✔
2823
          tdDestroySVCreateTbReq(pCreateTbReq);
13,248✔
2824
          taosMemoryFreeClear(pCreateTbReq);
13,248✔
2825
        }
4,416✔
2826
        return terrno;
4,416✔
2827
      }
2828
    }
13,248✔
2829

2830
    if (bindv->bind_cols && bindv->bind_cols[i]) {
2831
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
2832

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

UNCOV
2843
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
×
2844
      if (TSDB_CODE_SUCCESS != code) {
UNCOV
2845
        terrno = code;
×
UNCOV
2846
        STMT2_ELOG("bind batch failed, code:%s", stmt2Errstr(stmt));
×
UNCOV
2847
        if (pCreateTbReq) {
×
UNCOV
2848
          tdDestroySVCreateTbReq(pCreateTbReq);
×
UNCOV
2849
          taosMemoryFreeClear(pCreateTbReq);
×
UNCOV
2850
        }
×
2851
        return terrno;
UNCOV
2852
      }
×
UNCOV
2853
    }
×
UNCOV
2854
  }
×
UNCOV
2855

×
UNCOV
2856
  return code;
×
UNCOV
2857
}
×
2858

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

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

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

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

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

2895
  return code_s;
736✔
2896
}
736✔
UNCOV
2897

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

2905
  return stmtExec2(stmt, affected_rows);
203,688✔
2906
}
203,688✔
2907

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

2915
  return stmtClose2(stmt);
201,480✔
2916
}
186,024✔
2917

157,320✔
2918
int taos_stmt2_is_insert(TAOS_STMT2 *stmt, int *insert) {
2919
  if (stmt == NULL || insert == NULL) {
44,160✔
2920
    tscError("NULL parameter for %s", __FUNCTION__);
41,952✔
2921
    terrno = TSDB_CODE_INVALID_PARA;
2922
    return terrno;
2923
  }
2,208✔
2924
  *insert = stmt2IsInsert(stmt);
2,208✔
2925
  return TSDB_CODE_SUCCESS;
2926
}
2927

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

75,256✔
UNCOV
2935
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
UNCOV
2936
  STMT2_DLOG_E("start to get fields");
×
UNCOV
2937

×
2938
  if (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type ||
2939
      (pStmt->sql.type == 0 && stmt2IsInsert(stmt))) {
2940
    return stmtGetStbColFields2(stmt, count, fields);
75,256✔
2941
  }
2942
  if (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt))) {
2943
    return stmtGetParamNum2(stmt, count);
68,448✔
2944
  }
2945

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

×
2950
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_ALL *fields) {
2951
  (void)stmt;
2952
  if (!fields) return;
55,108✔
2953
  taosMemoryFree(fields);
55,108✔
UNCOV
2954
}
×
UNCOV
2955

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

×
UNCOV
2963
  return stmtUseResult2(stmt);
×
UNCOV
2964
}
×
2965

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

61,502✔
3115
  code = pRequest->code;
×
3116
  terrno = code;
3117

3118
_cleanup:
3119
  destroyRequest(pRequest);
61,502✔
3120
  return code;
61,502✔
UNCOV
3121
}
×
UNCOV
3122

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

35,144✔
UNCOV
3136
int32_t taos_register_instance(const char *id, const char *type, const char *desc, int32_t expire) {
×
UNCOV
3137
  if (id == NULL || id[0] == 0) {
×
3138
    return terrno = TSDB_CODE_INVALID_PARA;
3139
  }
3140

3141
  // Validate string lengths
61,502✔
3142
  size_t idLen = strlen(id);
61,502✔
UNCOV
3143
  if (idLen >= TSDB_INSTANCE_ID_LEN) {
×
3144
    tscError("instance id length %zu exceeds limit %d", idLen, TSDB_INSTANCE_ID_LEN - 1);
3145
    return terrno = TSDB_CODE_INVALID_PARA;
3146
  }
61,502✔
3147

61,502✔
UNCOV
3148
  if (type != NULL && type[0] != 0) {
×
3149
    size_t typeLen = strlen(type);
3150
    if (typeLen >= TSDB_INSTANCE_TYPE_LEN) {
3151
      tscError("instance type length %zu exceeds limit %d", typeLen, TSDB_INSTANCE_TYPE_LEN - 1);
61,502✔
3152
      return terrno = TSDB_CODE_INVALID_PARA;
61,502✔
UNCOV
3153
    }
×
3154
  }
3155

3156
  if (desc != NULL && desc[0] != 0) {
61,502✔
3157
    size_t descLen = strlen(desc);
61,502✔
3158
    if (descLen >= TSDB_INSTANCE_DESC_LEN) {
61,502✔
3159
      tscError("instance desc length %zu exceeds limit %d", descLen, TSDB_INSTANCE_DESC_LEN - 1);
×
3160
      return terrno = TSDB_CODE_INVALID_PARA;
3161
    }
3162
  }
61,502✔
3163

61,502✔
3164
  int32_t code = taos_init();
61,502✔
3165
  if (code != TSDB_CODE_SUCCESS) {
61,502✔
3166
    return code;
61,502✔
3167
  }
3168

61,502✔
3169
  SConfig *pCfg = taosGetCfg();
61,502✔
3170
  if (pCfg == NULL) {
61,502✔
3171
    return terrno = TSDB_CODE_CFG_NOT_FOUND;
61,502✔
3172
  }
61,502✔
3173

61,502✔
3174
  SConfigItem *pFirstEpItem = cfgGetItem(pCfg, "firstEp");
61,502✔
3175
  if (pFirstEpItem == NULL || pFirstEpItem->str == NULL || pFirstEpItem->str[0] == 0) {
61,502✔
3176
    return terrno = TSDB_CODE_CFG_NOT_FOUND;
3177
  }
61,502✔
3178

61,502✔
3179
  SEp firstEp = {0};
61,502✔
3180
  code = taosGetFqdnPortFromEp(pFirstEpItem->str, &firstEp);
61,502✔
3181
  if (code != TSDB_CODE_SUCCESS) {
61,502✔
3182
    return terrno = code;
3183
  }
3184

61,502✔
3185
  void    *clientRpc = NULL;
61,502✔
3186
  SEpSet   epSet = {.inUse = 0, .numOfEps = 1};
61,502✔
3187
  SRpcMsg  rpcMsg = {0};
61,502✔
3188
  SRpcMsg  rpcRsp = {0};
61,502✔
3189
  SRpcInit rpcInit = {0};
61,502✔
3190

61,502✔
3191
  rpcInit.label = "INST";
61,502✔
3192
  rpcInit.numOfThreads = 1;
3193
  rpcInit.cfp = NULL;
61,502✔
3194
  rpcInit.sessions = 16;
61,502✔
3195
  rpcInit.connType = TAOS_CONN_CLIENT;
61,502✔
3196
  rpcInit.idleTime = tsShellActivityTimer * 1000;
61,502✔
3197
  rpcInit.compressSize = tsCompressMsgSize;
61,502✔
3198
  rpcInit.user = TSDB_DEFAULT_USER;
3199

61,502✔
3200
  rpcInit.rfp = instanceRegisterRpcRfp;
61,502✔
UNCOV
3201
  rpcInit.retryMinInterval = tsRedirectPeriod;
×
UNCOV
3202
  rpcInit.retryStepFactor = tsRedirectFactor;
×
3203
  rpcInit.retryMaxInterval = tsRedirectMaxPeriod;
3204
  rpcInit.retryMaxTimeout =
3205
      tsMaxRetryWaitTime;  // Use a special user for instance registration (can be configured for whitelist)
61,502✔
3206

61,502✔
UNCOV
3207
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
×
UNCOV
3208
  connLimitNum = TMAX(connLimitNum, 10);
×
UNCOV
3209
  connLimitNum = TMIN(connLimitNum, 500);
×
3210
  rpcInit.connLimitNum = connLimitNum;
3211
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
3212
  rpcInit.readTimeout = tsReadTimeout;
3213
  rpcInit.ipv6 = tsEnableIpv6;
61,502✔
3214
  rpcInit.enableSSL = tsEnableTLS;
61,502✔
3215

3216
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
3217
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
61,502✔
3218
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
61,502✔
3219
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
61,502✔
3220
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
35,144✔
3221

3222
  code = taosVersionStrToInt(td_version, &rpcInit.compatibilityVer);
61,502✔
3223
  if (code != TSDB_CODE_SUCCESS) {
35,144✔
3224
    tscError("failed to convert taos version from str to int, errcode:%s", terrstr(code));
3225
    return code;
61,502✔
3226
  }
3227

61,502✔
3228
  clientRpc = rpcOpen(&rpcInit);
61,502✔
UNCOV
3229
  if (clientRpc == NULL) {
×
3230
    code = terrno;
×
3231
    tscError("failed to init instance register client since %s", tstrerror(code));
×
3232
    return code;
3233
  }
3234

61,502✔
3235
  // Prepare epSet
61,502✔
UNCOV
3236
  tstrncpy(epSet.eps[0].fqdn, firstEp.fqdn, TSDB_FQDN_LEN);
×
UNCOV
3237
  epSet.eps[0].port = firstEp.port;
×
UNCOV
3238

×
3239
  // Prepare request
3240
  SInstanceRegisterReq req = {0};
3241
  tstrncpy(req.id, id, sizeof(req.id));
61,502✔
UNCOV
3242
  if (type != NULL && type[0] != 0) {
×
UNCOV
3243
    tstrncpy(req.type, type, sizeof(req.type));
×
UNCOV
3244
  }
×
UNCOV
3245
  if (desc != NULL && desc[0] != 0) {
×
3246
    tstrncpy(req.desc, desc, sizeof(req.desc));
3247
  }
3248
  req.expire = expire;
61,502✔
3249

61,502✔
3250
  int32_t contLen = tSerializeSInstanceRegisterReq(NULL, 0, &req);
61,502✔
3251
  if (contLen <= 0) {
61,502✔
3252
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
61,502✔
3253
    rpcClose(clientRpc);
3254
    return code;
61,502✔
3255
  }
61,502✔
UNCOV
3256

×
3257
  void *pCont = rpcMallocCont(contLen);
3258
  if (pCont == NULL) {
3259
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3260
    rpcClose(clientRpc);
×
3261
    return code;
3262
  }
3263

61,502✔
UNCOV
3264
  if (tSerializeSInstanceRegisterReq(pCont, contLen, &req) < 0) {
×
3265
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
×
3266
    rpcFreeCont(pCont);
3267
    rpcClose(clientRpc);
61,502✔
3268
    return code;
3269
  }
3270

61,502✔
3271
  rpcMsg.pCont = pCont;
61,502✔
3272
  rpcMsg.contLen = contLen;
3273
  rpcMsg.msgType = TDMT_MND_REGISTER_INSTANCE;
61,502✔
3274
  rpcMsg.info.ahandle = (void *)0x9528;  // Different magic number from server status
3275
  rpcMsg.info.notFreeAhandle = 1;
61,502✔
3276

61,502✔
3277
  code = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
3278
  if (TSDB_CODE_SUCCESS != code) {
3279
    tscError("failed to send instance register req since %s", tstrerror(code));
48,323✔
3280
    // rpcSendRecv failed, pCont may not be freed, but check _RETURN1 path
48,323✔
UNCOV
3281
    // In error path, rpcSendRecv may free pCont, but we free it here to be safe
×
3282
    rpcClose(clientRpc);
3283
    return code;
3284
  }
48,323✔
3285

48,323✔
UNCOV
3286
  if (rpcRsp.code != 0) {
×
3287
    code = rpcRsp.code;
×
3288
    tscError("instance register failed, code:%s", tstrerror(code));
3289
  } else {
3290
    code = TSDB_CODE_SUCCESS;
48,323✔
3291
  }
48,323✔
UNCOV
3292

×
UNCOV
3293
  if (rpcRsp.pCont != NULL) {
×
3294
    rpcFreeCont(rpcRsp.pCont);
3295
  }
3296
  rpcClose(clientRpc);
48,323✔
3297

48,323✔
UNCOV
3298
  terrno = code;
×
UNCOV
3299
  return code;
×
3300
}
3301

3302
int32_t taos_list_instances(const char *filter_type, char ***pList, int32_t *pCount) {
48,323✔
3303
  if (pList == NULL || pCount == NULL) {
48,323✔
3304
    return TSDB_CODE_INVALID_PARA;
48,323✔
UNCOV
3305
  }
×
UNCOV
3306

×
3307
  int32_t code = taos_init();
3308
  if (code != TSDB_CODE_SUCCESS) {
3309
    terrno = code;
3310
    return code;
48,323✔
3311
  }
48,323✔
3312

48,323✔
3313
  SConfig *pCfg = taosGetCfg();
48,323✔
3314
  if (pCfg == NULL) {
48,323✔
3315
    terrno = TSDB_CODE_CFG_NOT_FOUND;
3316
    return TSDB_CODE_CFG_NOT_FOUND;
48,323✔
3317
  }
48,323✔
3318

48,323✔
3319
  SConfigItem *pFirstEpItem = cfgGetItem(pCfg, "firstEp");
48,323✔
3320
  if (pFirstEpItem == NULL || pFirstEpItem->str == NULL || pFirstEpItem->str[0] == 0) {
48,323✔
3321
    terrno = TSDB_CODE_CFG_NOT_FOUND;
48,323✔
3322
    return TSDB_CODE_CFG_NOT_FOUND;
48,323✔
3323
  }
48,323✔
3324

3325
  SEp firstEp = {0};
48,323✔
3326
  code = taosGetFqdnPortFromEp(pFirstEpItem->str, &firstEp);
48,323✔
3327
  if (code != TSDB_CODE_SUCCESS) {
48,323✔
3328
    terrno = code;
48,323✔
3329
    return code;
48,323✔
3330
  }
3331

3332
  // Initialize RPC connection (similar to taos_register_instance)
48,323✔
3333
  void    *clientRpc = NULL;
48,323✔
3334
  SEpSet   epSet = {.inUse = 0, .numOfEps = 1};
48,323✔
3335
  SRpcMsg  rpcMsg = {0};
48,323✔
3336
  SRpcMsg  rpcRsp = {0};
48,323✔
3337
  SRpcInit rpcInit = {0};
48,323✔
3338

48,323✔
3339
  rpcInit.label = "LIST";
48,323✔
3340
  rpcInit.numOfThreads = 1;
3341
  rpcInit.cfp = NULL;
48,323✔
3342
  rpcInit.sessions = 16;
48,323✔
3343
  rpcInit.connType = TAOS_CONN_CLIENT;
48,323✔
3344
  rpcInit.idleTime = tsShellActivityTimer * 1000;
48,323✔
3345
  rpcInit.compressSize = tsCompressMsgSize;
48,323✔
3346
  rpcInit.user = TSDB_DEFAULT_USER;
3347

48,323✔
3348
  rpcInit.rfp = instanceRegisterRpcRfp;
48,323✔
UNCOV
3349
  rpcInit.retryMinInterval = tsRedirectPeriod;
×
UNCOV
3350
  rpcInit.retryStepFactor = tsRedirectFactor;
×
3351
  rpcInit.retryMaxInterval = tsRedirectMaxPeriod;
3352
  rpcInit.retryMaxTimeout =
3353
      tsMaxRetryWaitTime;  // Use a special user for instance registration (can be configured for whitelist)
48,323✔
3354

48,323✔
UNCOV
3355
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
×
UNCOV
3356
  connLimitNum = TMAX(connLimitNum, 10);
×
UNCOV
3357
  connLimitNum = TMIN(connLimitNum, 500);
×
UNCOV
3358
  rpcInit.connLimitNum = connLimitNum;
×
3359
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
3360
  rpcInit.readTimeout = tsReadTimeout;
3361
  rpcInit.ipv6 = tsEnableIpv6;
48,323✔
3362
  rpcInit.enableSSL = tsEnableTLS;
48,323✔
3363

48,323✔
3364
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
48,323✔
3365
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
30,751✔
3366
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
3367
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
3368
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
3369

48,323✔
3370
  code = taosVersionStrToInt(td_version, &rpcInit.compatibilityVer);
48,323✔
UNCOV
3371
  if (code != TSDB_CODE_SUCCESS) {
×
3372
    tscError("failed to convert taos version from str to int, errcode:%s", terrstr(code));
×
3373
    return code;
×
UNCOV
3374
  }
×
3375

3376
  clientRpc = rpcOpen(&rpcInit);
3377
  if (clientRpc == NULL) {
3378
    code = terrno;
48,323✔
3379
    tscError("failed to init instance list client since %s", tstrerror(code));
48,323✔
3380
    terrno = code;
×
3381
    return code;
×
UNCOV
3382
  }
×
UNCOV
3383

×
3384
  tstrncpy(epSet.eps[0].fqdn, firstEp.fqdn, TSDB_FQDN_LEN);
3385
  epSet.eps[0].port = firstEp.port;
3386
  SInstanceListReq req = {0};
3387
  if (filter_type != NULL && filter_type[0] != 0) {
48,323✔
UNCOV
3388
    tstrncpy(req.filter_type, filter_type, sizeof(req.filter_type));
×
UNCOV
3389
  }
×
UNCOV
3390

×
UNCOV
3391
  // Serialize request to get required length
×
UNCOV
3392
  int32_t contLen = tSerializeSInstanceListReq(NULL, 0, &req);
×
3393
  if (contLen <= 0) {
3394
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
3395
    rpcClose(clientRpc);
48,323✔
3396
    terrno = code;
48,323✔
3397
    return code;
48,323✔
3398
  }
48,323✔
3399

48,323✔
3400
  // Allocate RPC message buffer (includes message header overhead)
3401
  void *pCont = rpcMallocCont(contLen);
48,323✔
3402
  if (pCont == NULL) {
48,323✔
3403
    code = terrno != 0 ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3404
    rpcClose(clientRpc);
×
3405
    terrno = code;
×
3406
    return code;
×
UNCOV
3407
  }
×
3408

3409
  // Serialize request into the content part (after message header)
3410
  if (tSerializeSInstanceListReq(pCont, contLen, &req) < 0) {
3411
    code = terrno != 0 ? terrno : TSDB_CODE_TSC_INTERNAL_ERROR;
48,323✔
3412
    rpcFreeCont(pCont);
×
3413
    rpcClose(clientRpc);
×
3414
    terrno = code;
×
3415
    return code;
×
3416
  }
UNCOV
3417

×
UNCOV
3418
  rpcMsg.pCont = pCont;
×
UNCOV
3419
  rpcMsg.contLen = contLen;
×
3420
  rpcMsg.msgType = TDMT_MND_LIST_INSTANCES;
3421
  rpcMsg.info.ahandle = (void *)0x9529;  // Different magic number from register
3422
  rpcMsg.info.notFreeAhandle = 1;
3423

48,323✔
3424
  code = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
48,323✔
3425
  if (TSDB_CODE_SUCCESS != code) {
48,323✔
3426
    tscError("failed to send instance list req since %s", tstrerror(code));
48,323✔
3427
    rpcFreeCont(pCont);
×
3428
    rpcClose(clientRpc);
×
3429
    terrno = code;
×
3430
    return code;
×
UNCOV
3431
  }
×
3432

3433
  // Check response - rpcRsp.code contains the result code from mnode
UNCOV
3434
  if (rpcRsp.code != 0) {
×
3435
    code = rpcRsp.code;
×
3436
    tscError("instance list failed, code:%s", tstrerror(code));
3437
    if (rpcRsp.pCont != NULL) {
×
3438
      rpcFreeCont(rpcRsp.pCont);
×
UNCOV
3439
    }
×
3440
    rpcClose(clientRpc);
×
3441
    terrno = code;
×
3442
    return code;
3443
  }
48,323✔
3444

48,323✔
3445
  // Deserialize response
UNCOV
3446
  if (rpcRsp.pCont != NULL && rpcRsp.contLen > 0) {
×
UNCOV
3447
    SInstanceListRsp rsp = {0};
×
3448
    code = tDeserializeSInstanceListRsp(rpcRsp.pCont, rpcRsp.contLen, &rsp);
3449
    if (code != TSDB_CODE_SUCCESS) {
3450
      tscError("failed to deserialize instance list rsp, code:%s", tstrerror(code));
48,323✔
3451
      if (rsp.ids != NULL) {
48,323✔
3452
        for (int32_t i = 0; i < rsp.count; i++) {
3453
          if (rsp.ids[i] != NULL) {
48,323✔
3454
            taosMemoryFree(rsp.ids[i]);
3455
          }
48,323✔
3456
        }
3457
        taosMemoryFree(rsp.ids);
3458
        rsp.ids = NULL;
35,144✔
3459
      }
35,144✔
3460
      rsp.count = 0;
×
3461
      rpcFreeCont(rpcRsp.pCont);
3462
      rpcClose(clientRpc);
3463
      terrno = code;
3464
      return code;
109,825✔
3465
    }
74,681✔
3466
    *pList = rsp.ids;
74,681✔
3467
    *pCount = rsp.count;
74,681✔
3468
  } else {
3469
    *pList = NULL;
3470
    *pCount = 0;
3471
  }
3472

35,144✔
3473
  if (rpcRsp.pCont != NULL) {
35,144✔
3474
    rpcFreeCont(rpcRsp.pCont);
3475
  }
3476
  rpcClose(clientRpc);
3477

3478
  return TSDB_CODE_SUCCESS;
3479
}
3480

3481
void taos_free_instances(char ***pList, int32_t count) {
3482
  if (pList == NULL || *pList == NULL || count <= 0) {
3483
    return;
3484
  }
3485

3486
  // Free each string in the array
3487
  for (int32_t i = 0; i < count; i++) {
3488
    if ((*pList)[i] != NULL) {
3489
      taosMemoryFree((*pList)[i]);
3490
      (*pList)[i] = NULL;
3491
    }
3492
  }
3493

3494
  // Free the array itself
3495
  taosMemoryFree(*pList);
3496
  *pList = NULL;
3497
}
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