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

taosdata / TDengine / #4664

08 Aug 2025 08:36AM UTC coverage: 60.536% (+0.2%) from 60.372%
#4664

push

travis-ci

web-flow
test: update cases desc (#32498)

139177 of 291923 branches covered (47.68%)

Branch coverage included in aggregate %.

209648 of 284307 relevant lines covered (73.74%)

18318036.98 hits per line

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

43.86
/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 "clientStmt.h"
21
#include "clientStmt2.h"
22
#include "functionMgt.h"
23
#include "os.h"
24
#include "query.h"
25
#include "scheduler.h"
26
#include "tcompare.h"
27
#include "tconv.h"
28
#include "tdatablock.h"
29
#include "tglobal.h"
30
#include "tmsg.h"
31
#include "tref.h"
32
#include "trpc.h"
33
#include "version.h"
34

35
#define TSC_VAR_NOT_RELEASE 1
36
#define TSC_VAR_RELEASED    0
37

38
#ifdef TAOSD_INTEGRATED
39
extern void shellStopDaemon();
40
#endif
41

42
static int32_t sentinel = TSC_VAR_NOT_RELEASE;
43
static int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper);
44

45
int taos_options(TSDB_OPTION option, const void *arg, ...) {
1,188✔
46
  if (arg == NULL) {
1,188!
47
    return TSDB_CODE_INVALID_PARA;
×
48
  }
49
  static int32_t lock = 0;
50

51
  for (int i = 1; atomic_val_compare_exchange_32(&lock, 0, 1) != 0; ++i) {
1,188!
52
    if (i % 1000 == 0) {
×
53
      (void)sched_yield();
×
54
    }
55
  }
56

57
  int ret = taos_options_imp(option, (const char *)arg);
1,188✔
58
  atomic_store_32(&lock, 0);
1,188✔
59
  return ret;
1,188✔
60
}
61

62
#if !defined(WINDOWS) && !defined(TD_ASTRA)
63
static void freeTz(void *p) {
11✔
64
  timezone_t tz = *(timezone_t *)p;
11✔
65
  tzfree(tz);
11✔
66
}
11✔
67

68
int32_t tzInit() {
3,513✔
69
  pTimezoneMap = taosHashInit(0, MurmurHash3_32, false, HASH_ENTRY_LOCK);
3,513✔
70
  if (pTimezoneMap == NULL) {
3,513!
71
    return terrno;
×
72
  }
73
  taosHashSetFreeFp(pTimezoneMap, freeTz);
3,513✔
74

75
  pTimezoneNameMap = taosHashInit(0, taosIntHash_64, false, HASH_ENTRY_LOCK);
3,513✔
76
  if (pTimezoneNameMap == NULL) {
3,513!
77
    return terrno;
×
78
  }
79
  return 0;
3,513✔
80
}
81

82
void tzCleanup() {
3,514✔
83
  taosHashCleanup(pTimezoneMap);
3,514✔
84
  taosHashCleanup(pTimezoneNameMap);
3,514✔
85
}
3,514✔
86

87
static timezone_t setConnnectionTz(const char *val) {
18✔
88
  timezone_t  tz = NULL;
18✔
89
  timezone_t *tmp = taosHashGet(pTimezoneMap, val, strlen(val));
18✔
90
  if (tmp != NULL && *tmp != NULL) {
18!
91
    tz = *tmp;
7✔
92
    goto END;
7✔
93
  }
94

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

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

124
END:
11✔
125
  return tz;
18✔
126
}
127
#endif
128

129
static int32_t setConnectionOption(TAOS *taos, TSDB_OPTION_CONNECTION option, const char *val) {
39✔
130
  if (taos == NULL) {
39✔
131
    return terrno = TSDB_CODE_INVALID_PARA;
1✔
132
  }
133

134
#ifdef WINDOWS
135
  if (option == TSDB_OPTION_CONNECTION_TIMEZONE) {
136
    return terrno = TSDB_CODE_NOT_SUPPORTTED_IN_WINDOWS;
137
  }
138
#endif
139

140
  if (option < TSDB_OPTION_CONNECTION_CLEAR || option >= TSDB_MAX_OPTIONS_CONNECTION) {
38!
141
    return terrno = TSDB_CODE_INVALID_PARA;
1✔
142
  }
143

144
  int32_t code = taos_init();
37✔
145
  // initialize global config
146
  if (code != 0) {
37!
147
    return terrno = code;
×
148
  }
149

150
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
37✔
151
  if (NULL == pObj) {
37!
152
    tscError("invalid parameter for %s", __func__);
×
153
    return terrno;
×
154
  }
155

156
  if (option == TSDB_OPTION_CONNECTION_CLEAR) {
37✔
157
    val = NULL;
1✔
158
  }
159

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

196
  if (option == TSDB_OPTION_CONNECTION_USER_APP || option == TSDB_OPTION_CONNECTION_CLEAR) {
35✔
197
    if (val != NULL) {
5✔
198
      tstrncpy(pObj->optionInfo.userApp, val, sizeof(pObj->optionInfo.userApp));
3✔
199
    } else {
200
      pObj->optionInfo.userApp[0] = 0;
2✔
201
    }
202
  }
203

204
  if (option == TSDB_OPTION_CONNECTION_USER_IP || option == TSDB_OPTION_CONNECTION_CLEAR) {
35✔
205
    SIpRange dualIp = {0};
7✔
206
    if (val != NULL) {
7✔
207
      pObj->optionInfo.userIp = taosInetAddr(val);
5✔
208
      SIpAddr addr = {0};
5✔
209
      code = taosGetIpFromFqdn(tsEnableIpv6, val, &addr);
5✔
210
      if (code == 0) {
5✔
211
        code = tIpStrToUint(&addr, &pObj->optionInfo.userDualIp);
2✔
212
      } 
213
      if (code != 0) {
5✔
214
        tscError("ipv6 flag %d failed to convert user ip %s to dual ip since %s", tsEnableIpv6 ?  1:0, val, tstrerror(code));
3!
215
        pObj->optionInfo.userIp = INADDR_NONE; 
3✔
216
        pObj->optionInfo.userDualIp = dualIp;  
3✔
217
        code = 0;
3✔
218
      }
219
    } else {
220
      pObj->optionInfo.userIp = INADDR_NONE;
2✔
221
      pObj->optionInfo.userDualIp = dualIp;
2✔
222
    }
223
  }
224

225
END:
28✔
226
  releaseTscObj(*(int64_t *)taos);
37✔
227
  return terrno = code;
37✔
228
}
229

230
int taos_options_connection(TAOS *taos, TSDB_OPTION_CONNECTION option, const void *arg, ...) {
39✔
231
  return setConnectionOption(taos, option, (const char *)arg);
39✔
232
}
233

234
// this function may be called by user or system, or by both simultaneously.
235
void taos_cleanup(void) {
3,524✔
236
  tscInfo("start to cleanup client environment");
3,524!
237
  if (atomic_val_compare_exchange_32(&sentinel, TSC_VAR_NOT_RELEASE, TSC_VAR_RELEASED) != TSC_VAR_NOT_RELEASE) {
3,524✔
238
    return;
10✔
239
  }
240

241
  monitorClose();
3,514✔
242
  tscStopCrashReport();
3,514✔
243

244
  hbMgrCleanUp();
3,514✔
245

246
  catalogDestroy();
3,514✔
247
  schedulerDestroy();
3,514✔
248

249
  fmFuncMgtDestroy();
3,514✔
250
  qCleanupKeywordsTable();
3,514✔
251

252
#if !defined(WINDOWS) && !defined(TD_ASTRA)
253
  tzCleanup();
3,514✔
254
#endif
255
  tmqMgmtClose();
3,514✔
256

257
  int32_t id = clientReqRefPool;
3,514✔
258
  clientReqRefPool = -1;
3,514✔
259
  taosCloseRef(id);
3,514✔
260

261
  id = clientConnRefPool;
3,514✔
262
  clientConnRefPool = -1;
3,514✔
263
  taosCloseRef(id);
3,514✔
264

265
  nodesDestroyAllocatorSet();
3,514✔
266
  cleanupAppInfo();
3,514✔
267
  rpcCleanup();
3,514✔
268
  tscDebug("rpc cleanup");
3,514✔
269

270
  if (TSDB_CODE_SUCCESS != cleanupTaskQueue()) {
3,514!
271
    tscWarn("failed to cleanup task queue");
×
272
  }
273

274
  taosConvDestroy();
3,514✔
275
  DestroyRegexCache();
3,514✔
276
#ifdef TAOSD_INTEGRATED
277
  shellStopDaemon();
278
#endif
279
  tscInfo("all local resources released");
3,514!
280
  taosCleanupCfg();
3,514✔
281
#ifndef TAOSD_INTEGRATED
282
  taosCloseLog();
3,514✔
283
#endif
284
}
285

286
static setConfRet taos_set_config_imp(const char *config) {
×
287
  setConfRet ret = {SET_CONF_RET_SUCC, {0}};
×
288
  // TODO: need re-implementation
289
  return ret;
×
290
}
291

292
setConfRet taos_set_config(const char *config) {
×
293
  // TODO  pthread_mutex_lock(&setConfMutex);
294
  setConfRet ret = taos_set_config_imp(config);
×
295
  //  pthread_mutex_unlock(&setConfMutex);
296
  return ret;
×
297
}
298

299
TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) {
10,509✔
300
  tscInfo("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
10,509!
301
  if (user == NULL) {
10,623✔
302
    user = TSDB_DEFAULT_USER;
1,265✔
303
  }
304

305
  if (pass == NULL) {
10,623✔
306
    pass = TSDB_DEFAULT_PASS;
1,265✔
307
  }
308

309
  STscObj *pObj = NULL;
10,623✔
310
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
10,623✔
311
  if (TSDB_CODE_SUCCESS == code) {
10,639✔
312
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
10,613!
313
    if (NULL == rid) {
10,616!
314
      tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db);
×
315
      return NULL;
×
316
    }
317
    *rid = pObj->id;
10,616✔
318
    return (TAOS *)rid;
10,616✔
319
  } else {
320
    terrno = code;
26✔
321
  }
322

323
  return NULL;
31✔
324
}
325

326
int taos_set_notify_cb(TAOS *taos, __taos_notify_fn_t fp, void *param, int type) {
×
327
  if (taos == NULL) {
×
328
    terrno = TSDB_CODE_INVALID_PARA;
×
329
    return terrno;
×
330
  }
331

332
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
333
  if (NULL == pObj) {
×
334
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
335
    tscError("invalid parameter for %s", __func__);
×
336
    return terrno;
×
337
  }
338

339
  switch (type) {
×
340
    case TAOS_NOTIFY_PASSVER: {
×
341
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
342
      pObj->passInfo.fp = fp;
×
343
      pObj->passInfo.param = param;
×
344
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
345
      break;
×
346
    }
347
    case TAOS_NOTIFY_WHITELIST_VER: {
×
348
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
349
      pObj->whiteListInfo.fp = fp;
×
350
      pObj->whiteListInfo.param = param;
×
351
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
352
      break;
×
353
    }
354
    case TAOS_NOTIFY_USER_DROPPED: {
×
355
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
356
      pObj->userDroppedInfo.fp = fp;
×
357
      pObj->userDroppedInfo.param = param;
×
358
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
359
      break;
×
360
    }
361
    default: {
×
362
      terrno = TSDB_CODE_INVALID_PARA;
×
363
      releaseTscObj(*(int64_t *)taos);
×
364
      return terrno;
×
365
    }
366
  }
367

368
  releaseTscObj(*(int64_t *)taos);
×
369
  return 0;
×
370
}
371

372
typedef struct SFetchWhiteListInfo {
373
  int64_t                     connId;
374
  __taos_async_whitelist_fn_t userCbFn;
375
  void                       *userParam;
376
} SFetchWhiteListInfo;
377

378
int32_t fetchWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
379
  SFetchWhiteListInfo *pInfo = (SFetchWhiteListInfo *)param;
×
380
  TAOS                *taos = &pInfo->connId;
×
381
  if (code != TSDB_CODE_SUCCESS) {
×
382
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
383
    taosMemoryFree(pMsg->pData);
×
384
    taosMemoryFree(pMsg->pEpSet);
×
385
    taosMemoryFree(pInfo);
×
386
    return code;
×
387
  }
388

389
  SGetUserWhiteListRsp wlRsp;
390
  if (TSDB_CODE_SUCCESS != tDeserializeSGetUserWhiteListRsp(pMsg->pData, pMsg->len, &wlRsp)) {
×
391
    taosMemoryFree(pMsg->pData);
×
392
    taosMemoryFree(pMsg->pEpSet);
×
393
    taosMemoryFree(pInfo);
×
394
    tFreeSGetUserWhiteListRsp(&wlRsp);
×
395
    return terrno;
×
396
  }
397

398
  uint64_t *pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(uint64_t));
×
399
  if (pWhiteLists == NULL) {
×
400
    taosMemoryFree(pMsg->pData);
×
401
    taosMemoryFree(pMsg->pEpSet);
×
402
    taosMemoryFree(pInfo);
×
403
    tFreeSGetUserWhiteListRsp(&wlRsp);
×
404
    return terrno;
×
405
  }
406

407
  for (int i = 0; i < wlRsp.numWhiteLists; ++i) {
×
408
    pWhiteLists[i] = ((uint64_t)wlRsp.pWhiteLists[i].mask << 32) | wlRsp.pWhiteLists[i].ip;
×
409
  }
410

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

413
  taosMemoryFree(pWhiteLists);
×
414
  taosMemoryFree(pMsg->pData);
×
415
  taosMemoryFree(pMsg->pEpSet);
×
416
  taosMemoryFree(pInfo);
×
417
  tFreeSGetUserWhiteListRsp(&wlRsp);
×
418
  return code;
×
419
}
420

421
void taos_fetch_whitelist_a(TAOS *taos, __taos_async_whitelist_fn_t fp, void *param) {
×
422
  if (NULL == taos) {
×
423
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
424
    return;
×
425
  }
426

427
  int64_t connId = *(int64_t *)taos;
×
428

429
  STscObj *pTsc = acquireTscObj(connId);
×
430
  if (NULL == pTsc) {
×
431
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
432
    return;
×
433
  }
434

435
  SGetUserWhiteListReq req;
436
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
437
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
438
  if (msgLen < 0) {
×
439
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
440
    releaseTscObj(connId);
×
441
    return;
×
442
  }
443

444
  void *pReq = taosMemoryMalloc(msgLen);
×
445
  if (pReq == NULL) {
×
446
    fp(param, terrno, taos, 0, NULL);
×
447
    releaseTscObj(connId);
×
448
    return;
×
449
  }
450

451
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
452
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
453
    taosMemoryFree(pReq);
×
454
    releaseTscObj(connId);
×
455
    return;
×
456
  }
457

458
  SFetchWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchWhiteListInfo));
×
459
  if (pParam == NULL) {
×
460
    fp(param, terrno, taos, 0, NULL);
×
461
    taosMemoryFree(pReq);
×
462
    releaseTscObj(connId);
×
463
    return;
×
464
  }
465

466
  pParam->connId = connId;
×
467
  pParam->userCbFn = fp;
×
468

469
  pParam->userParam = param;
×
470
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
471
  if (pSendInfo == NULL) {
×
472
    fp(param, terrno, taos, 0, NULL);
×
473
    taosMemoryFree(pParam);
×
474
    taosMemoryFree(pReq);
×
475
    releaseTscObj(connId);
×
476
    return;
×
477
  }
478

479
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
480
  pSendInfo->requestId = generateRequestId();
×
481
  pSendInfo->requestObjRefId = 0;
×
482
  pSendInfo->param = pParam;
×
483
  pSendInfo->fp = fetchWhiteListCallbackFn;
×
484
  pSendInfo->msgType = TDMT_MND_GET_USER_WHITELIST;
×
485

486
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
487
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
488
    tscWarn("failed to async send msg to server");
×
489
  }
490
  releaseTscObj(connId);
×
491
  return;
×
492
}
493

494
typedef struct SFetchWhiteListDualStackInfo {
495
  int64_t connId;
496
  void   *userParam;
497

498
  __taos_async_whitelist_dual_stack_fn_t userCbFn;
499
} SFetchWhiteListDualStackInfo;
500

501
int32_t fetchWhiteListDualStackCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
502
  int32_t lino = 0;
×
503
  char  **pWhiteLists = NULL;
×
504

505
  SGetUserWhiteListRsp wlRsp = {0};
×
506

507
  SFetchWhiteListDualStackInfo *pInfo = (SFetchWhiteListDualStackInfo *)param;
×
508
  TAOS *taos = &pInfo->connId;
×
509

510
  if (code != TSDB_CODE_SUCCESS) {
×
511
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
512
    TAOS_CHECK_GOTO(code, &lino, _error);
×
513
  }
514

515
  if ((code = tDeserializeSGetUserWhiteListDualRsp(pMsg->pData, pMsg->len, &wlRsp)) != TSDB_CODE_SUCCESS) {
×
516
    TAOS_CHECK_GOTO(code, &lino, _error);
×
517
  }
518

519
  pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(char *));
×
520
  if (pWhiteLists == NULL) {
×
521
    code = terrno;
×
522
    TAOS_CHECK_GOTO(code, &lino, _error);
×
523
  }
524

525
  for (int32_t i = 0; i < wlRsp.numWhiteLists; i++) {
×
526
    SIpRange *pIpRange = &wlRsp.pWhiteListsDual[i];
×
527
    SIpAddr   ipAddr = {0};
×
528

529
    code = tIpUintToStr(pIpRange, &ipAddr);
×
530
    TAOS_CHECK_GOTO(code, &lino, _error);
×
531

532
    char *ip = taosMemCalloc(1, IP_RESERVE_CAP);
×
533
    if (ip == NULL) {
×
534
      code = terrno;
×
535
      TAOS_CHECK_GOTO(code, &lino, _error);
×
536
    }
537
    if (ipAddr.type == 0) {
×
538
      snprintf(ip, IP_RESERVE_CAP, "%s/%d", ipAddr.ipv4, ipAddr.mask);
×
539
    } else {
540
      if (ipAddr.ipv6[0] == 0) {
×
541
        memcpy(ipAddr.ipv6, "::", 2);
×
542
      }
543
      snprintf(ip, IP_RESERVE_CAP, "%s/%d", ipAddr.ipv6, ipAddr.mask);
×
544
    }
545
    pWhiteLists[i] = ip;
×
546
  }
547

548
  pInfo->userCbFn(pInfo->userParam, code, taos, wlRsp.numWhiteLists, pWhiteLists);
×
549
_error:
×
550
  if (pWhiteLists != NULL) {
×
551
    for (int32_t i = 0; i < wlRsp.numWhiteLists; i++) {
×
552
      taosMemFree(pWhiteLists[i]);
×
553
    }
554
    taosMemoryFree(pWhiteLists);
×
555
  }
556
  taosMemoryFree(pMsg->pData);
×
557
  taosMemoryFree(pMsg->pEpSet);
×
558
  taosMemoryFree(pInfo);
×
559
  tFreeSGetUserWhiteListDualRsp(&wlRsp);
×
560
  return code;
×
561
}
562
void taos_fetch_whitelist_dual_stack_a(TAOS *taos, __taos_async_whitelist_dual_stack_fn_t fp, void *param) {
×
563
  if (NULL == taos) {
×
564
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
565
    return;
×
566
  }
567
  int64_t connId = *(int64_t *)taos;
×
568

569
  STscObj *pTsc = acquireTscObj(connId);
×
570
  if (NULL == pTsc) {
×
571
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
572
    return;
×
573
  }
574

575
  SGetUserWhiteListReq req;
576
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
577
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
578
  if (msgLen < 0) {
×
579
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
580
    releaseTscObj(connId);
×
581
    return;
×
582
  }
583

584
  void *pReq = taosMemoryMalloc(msgLen);
×
585
  if (pReq == NULL) {
×
586
    fp(param, terrno, taos, 0, NULL);
×
587
    releaseTscObj(connId);
×
588
    return;
×
589
  }
590

591
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
592
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
593
    taosMemoryFree(pReq);
×
594
    releaseTscObj(connId);
×
595
    return;
×
596
  }
597

598
  SFetchWhiteListDualStackInfo *pParam = taosMemoryMalloc(sizeof(SFetchWhiteListDualStackInfo));
×
599
  if (pParam == NULL) {
×
600
    fp(param, terrno, taos, 0, NULL);
×
601
    taosMemoryFree(pReq);
×
602
    releaseTscObj(connId);
×
603
    return;
×
604
  }
605

606
  pParam->connId = connId;
×
607
  pParam->userCbFn = fp;
×
608
  pParam->userParam = param;
×
609

610
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
611
  if (pSendInfo == NULL) {
×
612
    fp(param, terrno, taos, 0, NULL);
×
613
    taosMemoryFree(pParam);
×
614
    taosMemoryFree(pReq);
×
615
    releaseTscObj(connId);
×
616
    return;
×
617
  }
618

619
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
620
  pSendInfo->requestId = generateRequestId();
×
621
  pSendInfo->requestObjRefId = 0;
×
622
  pSendInfo->param = pParam;
×
623
  pSendInfo->fp = fetchWhiteListDualStackCallbackFn;
×
624
  pSendInfo->msgType = TDMT_MND_GET_USER_WHITELIST_DUAL;
×
625

626
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
627
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
628
    tscWarn("failed to async send msg to server");
×
629
  }
630
  releaseTscObj(connId);
×
631
  return;
×
632
}
633

634
void taos_close_internal(void *taos) {
10,771✔
635
  if (taos == NULL) {
10,771!
636
    return;
×
637
  }
638

639
  STscObj *pTscObj = (STscObj *)taos;
10,771✔
640
  tscDebug("conn:0x%" PRIx64 ", try to close connection, numOfReq:%d", pTscObj->id, pTscObj->numOfReqs);
10,771✔
641

642
  if (TSDB_CODE_SUCCESS != taosRemoveRef(clientConnRefPool, pTscObj->id)) {
10,771!
643
    tscError("conn:0x%" PRIx64 ", failed to remove ref from conn pool", pTscObj->id);
×
644
  }
645
}
646

647
void taos_close(TAOS *taos) {
11,050✔
648
  if (taos == NULL) {
11,050✔
649
    return;
427✔
650
  }
651

652
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
10,623✔
653
  if (NULL == pObj) {
10,626!
654
    taosMemoryFree(taos);
×
655
    return;
×
656
  }
657

658
  taos_close_internal(pObj);
10,626✔
659
  releaseTscObj(*(int64_t *)taos);
10,625✔
660
  taosMemoryFree(taos);
10,626!
661
}
662

663
int taos_errno(TAOS_RES *res) {
8,741,166✔
664
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
8,741,166!
665
    return terrno;
×
666
  }
667

668
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
8,750,597!
669
    return 0;
×
670
  }
671

672
  return ((SRequestObj *)res)->code;
8,751,426✔
673
}
674

675
const char *taos_errstr(TAOS_RES *res) {
7,030✔
676
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
7,030!
677
    return (const char *)tstrerror(terrno);
167✔
678
  }
679

680
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
6,863!
681
    return "success";
×
682
  }
683

684
  SRequestObj *pRequest = (SRequestObj *)res;
6,863✔
685
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
6,863!
686
    return pRequest->msgBuf;
826✔
687
  } else {
688
    return (const char *)tstrerror(pRequest->code);
6,037✔
689
  }
690
}
691

692
void taos_free_result(TAOS_RES *res) {
8,637,302✔
693
  if (NULL == res) {
8,637,302✔
694
    return;
23,577✔
695
  }
696

697
  tscTrace("res:%p, will be freed", res);
8,613,725!
698

699
  if (TD_RES_QUERY(res)) {
8,622,906✔
700
    SRequestObj *pRequest = (SRequestObj *)res;
8,622,723✔
701
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
8,622,723✔
702
    destroyRequest(pRequest);
8,622,724✔
703
    return;
8,624,042✔
704
  }
705

706
  SMqRspObj *pRsp = (SMqRspObj *)res;
183✔
707
  if (TD_RES_TMQ(res)) {
183✔
708
    tDeleteMqDataRsp(&pRsp->dataRsp);
178✔
709
    doFreeReqResultInfo(&pRsp->resInfo);
178✔
710
  } else if (TD_RES_TMQ_METADATA(res)) {
5✔
711
    tDeleteSTaosxRsp(&pRsp->dataRsp);
1✔
712
    doFreeReqResultInfo(&pRsp->resInfo);
1✔
713
  } else if (TD_RES_TMQ_META(res)) {
4✔
714
    tDeleteMqMetaRsp(&pRsp->metaRsp);
2✔
715
  } else if (TD_RES_TMQ_BATCH_META(res)) {
2!
716
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
2✔
717
  } else if (TD_RES_TMQ_RAW(res)) {
×
718
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
719
  }
720
  taosMemoryFree(pRsp);
183!
721
}
722

723
void taos_kill_query(TAOS *taos) {
10✔
724
  if (NULL == taos) {
10✔
725
    return;
5✔
726
  }
727

728
  int64_t  rid = *(int64_t *)taos;
5✔
729
  STscObj *pTscObj = acquireTscObj(rid);
5✔
730
  if (pTscObj) {
5!
731
    stopAllRequests(pTscObj->pRequests);
5✔
732
  }
733
  releaseTscObj(rid);
5✔
734
}
735

736
int taos_field_count(TAOS_RES *res) {
4,290,232✔
737
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
4,290,232!
738
    return 0;
6✔
739
  }
740

741
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
4,290,226✔
742
  return pResInfo->numOfCols;
4,290,226✔
743
}
744

745
int taos_num_fields(TAOS_RES *res) { return taos_field_count(res); }
3,846,872✔
746

747
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
459,907✔
748
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
459,907!
749
    return NULL;
3,538✔
750
  }
751

752
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
456,371✔
753
  return pResInfo->userFields;
456,371✔
754
}
755

756
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
8,599,977✔
757
TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqid) {
×
758
  return taosQueryImplWithReqid(taos, sql, false, reqid);
×
759
}
760

761
TAOS_FIELD_E *taos_fetch_fields_e(TAOS_RES *res) {
3✔
762
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
3!
763
    return NULL;
×
764
  }
765
  SReqResultInfo* pResInfo = tscGetCurResInfo(res);
3✔
766
  return pResInfo->fields;
3✔
767
}
768

769
TAOS_ROW taos_fetch_row(TAOS_RES *res) {
3,163,617✔
770
  if (res == NULL) {
3,163,617!
771
    return NULL;
×
772
  }
773

774
  if (TD_RES_QUERY(res)) {
3,163,617✔
775
    SRequestObj *pRequest = (SRequestObj *)res;
3,158,270✔
776
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
3,158,270✔
777
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0 || pRequest->killed) {
3,158,106!
778
      return NULL;
159✔
779
    }
780

781
    if (pRequest->inCallback) {
3,158,093✔
782
      tscError("can not call taos_fetch_row before query callback ends.");
1!
783
      terrno = TSDB_CODE_TSC_INVALID_OPERATION;
1✔
784
      return NULL;
1✔
785
    }
786

787
    return doAsyncFetchRows(pRequest, true, true);
3,158,092✔
788
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
5,347!
789
    SMqRspObj      *msg = ((SMqRspObj *)res);
5,347✔
790
    SReqResultInfo *pResultInfo = NULL;
5,347✔
791
    if (msg->resIter == -1) {
5,347✔
792
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
155!
793
        return NULL;
×
794
      }
795
    } else {
796
      pResultInfo = tmqGetCurResInfo(res);
5,192✔
797
    }
798

799
    if (pResultInfo->current < pResultInfo->numOfRows) {
5,347✔
800
      doSetOneRowPtr(pResultInfo);
2,009✔
801
      pResultInfo->current += 1;
2,009✔
802
      return pResultInfo->row;
2,009✔
803
    } else {
804
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
3,338✔
805
        return NULL;
150✔
806
      }
807

808
      doSetOneRowPtr(pResultInfo);
3,197✔
809
      pResultInfo->current += 1;
3,197✔
810
      return pResultInfo->row;
3,197✔
811
    }
812
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
813
    return NULL;
×
814
  } else {
815
    tscError("invalid result passed to taos_fetch_row");
×
816
    terrno = TSDB_CODE_TMQ_INVALID_DATA;
×
817
    return NULL;
×
818
  }
819
}
820

821
int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
14,308✔
822
  return taos_print_row_with_size(str, INT32_MAX, row, fields, num_fields);
14,308✔
823
}
824
int taos_print_row_with_size(char *str, uint32_t size, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
14,308✔
825
  int32_t len = 0;
14,308✔
826
  for (int i = 0; i < num_fields; ++i) {
96,233✔
827
    if (i > 0 && len < size - 1) {
81,923!
828
      str[len++] = ' ';
67,624✔
829
    }
830

831
    if (row[i] == NULL) {
81,923✔
832
      len += tsnprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
18✔
833
      continue;
18✔
834
    }
835

836
    switch (fields[i].type) {
81,905!
837
      case TSDB_DATA_TYPE_TINYINT:
2,206✔
838
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
2,206✔
839
        break;
2,206✔
840

841
      case TSDB_DATA_TYPE_UTINYINT:
2,200✔
842
        len += tsnprintf(str + len, size - len, "%u", *((uint8_t *)row[i]));
2,200✔
843
        break;
2,200✔
844

845
      case TSDB_DATA_TYPE_SMALLINT:
2,202✔
846
        len += tsnprintf(str + len, size - len, "%d", *((int16_t *)row[i]));
2,202✔
847
        break;
2,202✔
848

849
      case TSDB_DATA_TYPE_USMALLINT:
2,200✔
850
        len += tsnprintf(str + len, size - len, "%u", *((uint16_t *)row[i]));
2,200✔
851
        break;
2,200✔
852

853
      case TSDB_DATA_TYPE_INT:
11,181✔
854
        len += tsnprintf(str + len, size - len, "%d", *((int32_t *)row[i]));
11,181✔
855
        break;
11,181✔
856

857
      case TSDB_DATA_TYPE_UINT:
2,200✔
858
        len += tsnprintf(str + len, size - len, "%u", *((uint32_t *)row[i]));
2,200✔
859
        break;
2,200✔
860

861
      case TSDB_DATA_TYPE_BIGINT:
3,517✔
862
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
3,517✔
863
        break;
3,517✔
864

865
      case TSDB_DATA_TYPE_UBIGINT:
2,200✔
866
        len += tsnprintf(str + len, size - len, "%" PRIu64, *((uint64_t *)row[i]));
2,200✔
867
        break;
2,200✔
868

869
      case TSDB_DATA_TYPE_FLOAT: {
3,520✔
870
        float fv = 0;
3,520✔
871
        fv = GET_FLOAT_VAL(row[i]);
3,520✔
872
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
3,520✔
873
      } break;
3,520✔
874

875
      case TSDB_DATA_TYPE_DOUBLE: {
9,564✔
876
        double dv = 0;
9,564✔
877
        dv = GET_DOUBLE_VAL(row[i]);
9,564✔
878
        len += snprintf(str + len, size - len, "%.*g", DBL_DIG, dv);
9,564✔
879
      } break;
9,564✔
880

881
      case TSDB_DATA_TYPE_VARBINARY: {
2,202✔
882
        void    *data = NULL;
2,202✔
883
        uint32_t tmp = 0;
2,202✔
884
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
2,202✔
885
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
2,202!
886
          break;
×
887
        }
888
        uint32_t copyLen = TMIN(size - len - 1, tmp);
2,202✔
889
        (void)memcpy(str + len, data, copyLen);
2,202✔
890
        len += copyLen;
2,202✔
891
        taosMemoryFree(data);
2,202!
892
      } break;
2,202✔
893
      case TSDB_DATA_TYPE_BINARY:
24,043✔
894
      case TSDB_DATA_TYPE_NCHAR:
895
      case TSDB_DATA_TYPE_GEOMETRY: {
896
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
24,043✔
897
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
24,043!
898
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
4,400✔
899
          if (charLen > fields[i].bytes || charLen < 0) {
21,843!
900
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
×
901
            break;
×
902
          }
903
        } else {
904
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
2,200!
905
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
×
906
            break;
×
907
          }
908
        }
909

910
        uint32_t copyLen = TMIN(size - len - 1, charLen);
24,043✔
911
        (void)memcpy(str + len, row[i], copyLen);
24,043✔
912
        len += copyLen;
24,043✔
913
      } break;
24,043✔
914
      case TSDB_DATA_TYPE_BLOB:
×
915
      case TSDB_DATA_TYPE_MEDIUMBLOB: {
916
        void    *data = NULL;
×
917
        uint32_t tmp = 0;
×
918
        int32_t  charLen = blobDataLen((char *)row[i] - BLOBSTR_HEADER_SIZE);
×
919
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
×
920
          break;
×
921
        }
922

923
        uint32_t copyLen = TMIN(size - len - 1, tmp);
×
924
        (void)memcpy(str + len, data, copyLen);
×
925
        len += copyLen;
×
926

927
        taosMemoryFree(data);
×
928
      } break;
×
929

930
      case TSDB_DATA_TYPE_TIMESTAMP:
12,479✔
931
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
12,479✔
932
        break;
12,481✔
933

934
      case TSDB_DATA_TYPE_BOOL:
2,200✔
935
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
2,200✔
936
        break;
2,200✔
937
      case TSDB_DATA_TYPE_DECIMAL64:
×
938
      case TSDB_DATA_TYPE_DECIMAL: {
939
        uint32_t decimalLen = strlen(row[i]);
×
940
        uint32_t copyLen = TMIN(size - len - 1, decimalLen);
×
941
        (void)memcpy(str + len, row[i], copyLen);
×
942
        len += copyLen;
×
943
      } break;
×
944
      default:
×
945
        break;
×
946
    }
947

948
    if (len >= size - 1) {
81,907!
949
      break;
×
950
    }
951
  }
952
  if (len < size) {
14,310✔
953
    str[len] = 0;
14,307✔
954
  }
955

956
  return len;
14,310✔
957
}
958

959
int *taos_fetch_lengths(TAOS_RES *res) {
936,820✔
960
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
936,820!
961
    return NULL;
×
962
  }
963

964
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
936,825✔
965
  return pResInfo->length;
936,825✔
966
}
967

968
TAOS_ROW *taos_result_block(TAOS_RES *res) {
×
969
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
970
    terrno = TSDB_CODE_INVALID_PARA;
×
971
    return NULL;
×
972
  }
973

974
  if (taos_is_update_query(res)) {
×
975
    return NULL;
×
976
  }
977

978
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
979
  return &pResInfo->row;
×
980
}
981

982
// todo intergrate with tDataTypes
983
const char *taos_data_type(int type) {
×
984
  switch (type) {
×
985
    case TSDB_DATA_TYPE_NULL:
×
986
      return "TSDB_DATA_TYPE_NULL";
×
987
    case TSDB_DATA_TYPE_BOOL:
×
988
      return "TSDB_DATA_TYPE_BOOL";
×
989
    case TSDB_DATA_TYPE_TINYINT:
×
990
      return "TSDB_DATA_TYPE_TINYINT";
×
991
    case TSDB_DATA_TYPE_SMALLINT:
×
992
      return "TSDB_DATA_TYPE_SMALLINT";
×
993
    case TSDB_DATA_TYPE_INT:
×
994
      return "TSDB_DATA_TYPE_INT";
×
995
    case TSDB_DATA_TYPE_BIGINT:
×
996
      return "TSDB_DATA_TYPE_BIGINT";
×
997
    case TSDB_DATA_TYPE_FLOAT:
×
998
      return "TSDB_DATA_TYPE_FLOAT";
×
999
    case TSDB_DATA_TYPE_DOUBLE:
×
1000
      return "TSDB_DATA_TYPE_DOUBLE";
×
1001
    case TSDB_DATA_TYPE_VARCHAR:
×
1002
      return "TSDB_DATA_TYPE_VARCHAR";
×
1003
      //    case TSDB_DATA_TYPE_BINARY:          return "TSDB_DATA_TYPE_VARCHAR";
1004
    case TSDB_DATA_TYPE_TIMESTAMP:
×
1005
      return "TSDB_DATA_TYPE_TIMESTAMP";
×
1006
    case TSDB_DATA_TYPE_NCHAR:
×
1007
      return "TSDB_DATA_TYPE_NCHAR";
×
1008
    case TSDB_DATA_TYPE_JSON:
×
1009
      return "TSDB_DATA_TYPE_JSON";
×
1010
    case TSDB_DATA_TYPE_GEOMETRY:
×
1011
      return "TSDB_DATA_TYPE_GEOMETRY";
×
1012
    case TSDB_DATA_TYPE_UTINYINT:
×
1013
      return "TSDB_DATA_TYPE_UTINYINT";
×
1014
    case TSDB_DATA_TYPE_USMALLINT:
×
1015
      return "TSDB_DATA_TYPE_USMALLINT";
×
1016
    case TSDB_DATA_TYPE_UINT:
×
1017
      return "TSDB_DATA_TYPE_UINT";
×
1018
    case TSDB_DATA_TYPE_UBIGINT:
×
1019
      return "TSDB_DATA_TYPE_UBIGINT";
×
1020
    case TSDB_DATA_TYPE_VARBINARY:
×
1021
      return "TSDB_DATA_TYPE_VARBINARY";
×
1022
    case TSDB_DATA_TYPE_DECIMAL:
×
1023
      return "TSDB_DATA_TYPE_DECIMAL";
×
1024
    case TSDB_DATA_TYPE_BLOB:
×
1025
      return "TSDB_DATA_TYPE_BLOB";
×
1026
    case TSDB_DATA_TYPE_MEDIUMBLOB:
×
1027
      return "TSDB_DATA_TYPE_MEDIUMBLOB";
×
1028
    default:
×
1029
      return "UNKNOWN";
×
1030
  }
1031
}
1032

1033
const char *taos_get_client_info() { return td_version; }
2,363✔
1034

1035
// return int32_t
1036
int taos_affected_rows(TAOS_RES *res) {
412,070✔
1037
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
412,070!
1038
      TD_RES_TMQ_BATCH_META(res)) {
412,086!
1039
    return 0;
×
1040
  }
1041

1042
  SRequestObj    *pRequest = (SRequestObj *)res;
412,086✔
1043
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
412,086✔
1044
  return (int)pResInfo->numOfRows;
412,086✔
1045
}
1046

1047
// return int64_t
1048
int64_t taos_affected_rows64(TAOS_RES *res) {
3,249✔
1049
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
3,249!
1050
      TD_RES_TMQ_BATCH_META(res)) {
3,249!
1051
    return 0;
×
1052
  }
1053

1054
  SRequestObj    *pRequest = (SRequestObj *)res;
3,249✔
1055
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
3,249✔
1056
  return pResInfo->numOfRows;
3,249✔
1057
}
1058

1059
int taos_result_precision(TAOS_RES *res) {
241,140✔
1060
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
241,140!
1061
    return TSDB_TIME_PRECISION_MILLI;
×
1062
  }
1063

1064
  if (TD_RES_QUERY(res)) {
241,140✔
1065
    SRequestObj *pRequest = (SRequestObj *)res;
237,181✔
1066
    return pRequest->body.resInfo.precision;
237,181✔
1067
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
3,959!
1068
    SReqResultInfo *info = tmqGetCurResInfo(res);
3,959✔
1069
    return info->precision;
3,959✔
1070
  }
1071
  return TSDB_TIME_PRECISION_MILLI;
×
1072
}
1073

1074
int taos_select_db(TAOS *taos, const char *db) {
402✔
1075
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
402✔
1076
  if (pObj == NULL) {
402!
1077
    releaseTscObj(*(int64_t *)taos);
×
1078
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1079
    return TSDB_CODE_TSC_DISCONNECTED;
×
1080
  }
1081

1082
  if (db == NULL || strlen(db) == 0) {
402!
1083
    releaseTscObj(*(int64_t *)taos);
×
1084
    tscError("invalid parameter for %s", db == NULL ? "db is NULL" : "db is empty");
×
1085
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1086
    return terrno;
×
1087
  }
1088

1089
  char sql[256] = {0};
402✔
1090
  (void)snprintf(sql, tListLen(sql), "use %s", db);
402✔
1091

1092
  TAOS_RES *pRequest = taos_query(taos, sql);
402✔
1093
  int32_t   code = taos_errno(pRequest);
402✔
1094

1095
  taos_free_result(pRequest);
401✔
1096
  releaseTscObj(*(int64_t *)taos);
401✔
1097
  return code;
401✔
1098
}
1099

1100
void taos_stop_query(TAOS_RES *res) {
8,624,145✔
1101
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
8,624,145!
1102
      TD_RES_TMQ_BATCH_META(res)) {
8,634,346!
1103
    return;
×
1104
  }
1105

1106
  stopAllQueries((SRequestObj *)res);
8,634,597✔
1107
}
1108

1109
bool taos_is_null(TAOS_RES *res, int32_t row, int32_t col) {
×
1110
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1111
    return true;
×
1112
  }
1113
  SReqResultInfo *pResultInfo = tscGetCurResInfo(res);
×
1114
  if (col >= pResultInfo->numOfCols || col < 0 || row >= pResultInfo->numOfRows || row < 0) {
×
1115
    return true;
×
1116
  }
1117

1118
  SResultColumn *pCol = &pResultInfo->pCol[col];
×
1119
  if (IS_VAR_DATA_TYPE(pResultInfo->fields[col].type)) {
×
1120
    return (pCol->offset[row] == -1);
×
1121
  } else {
1122
    return colDataIsNull_f(pCol, row);
×
1123
  }
1124
}
1125

1126
bool taos_is_update_query(TAOS_RES *res) { return taos_num_fields(res) == 0; }
×
1127

1128
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
13,388✔
1129
  int32_t numOfRows = 0;
13,388✔
1130
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
13,388✔
1131
  return numOfRows;
13,388✔
1132
}
1133

1134
int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) {
13,388✔
1135
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
13,388!
1136
    return 0;
×
1137
  }
1138

1139
  if (TD_RES_QUERY(res)) {
13,388!
1140
    SRequestObj *pRequest = (SRequestObj *)res;
13,388✔
1141

1142
    (*rows) = NULL;
13,388✔
1143
    (*numOfRows) = 0;
13,388✔
1144

1145
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
13,388!
1146
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
13,388!
1147
      return pRequest->code;
143✔
1148
    }
1149

1150
    (void)doAsyncFetchRows(pRequest, false, true);
13,245✔
1151

1152
    // TODO refactor
1153
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
13,245✔
1154
    pResultInfo->current = pResultInfo->numOfRows;
13,245✔
1155

1156
    (*rows) = pResultInfo->row;
13,245✔
1157
    (*numOfRows) = pResultInfo->numOfRows;
13,245✔
1158
    return pRequest->code;
13,245✔
1159
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
×
1160
    SReqResultInfo *pResultInfo = NULL;
×
1161
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
×
1162
    if (code != 0) return code;
×
1163

1164
    pResultInfo->current = pResultInfo->numOfRows;
×
1165
    (*rows) = pResultInfo->row;
×
1166
    (*numOfRows) = pResultInfo->numOfRows;
×
1167
    return 0;
×
1168
  } else {
1169
    tscError("taos_fetch_block_s invalid res type");
×
1170
    return TSDB_CODE_TMQ_INVALID_DATA;
×
1171
  }
1172
}
1173

1174
int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) {
8✔
1175
  *numOfRows = 0;
8✔
1176
  *pData = NULL;
8✔
1177

1178
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
8!
1179
    return 0;
×
1180
  }
1181

1182
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
8!
1183
    SReqResultInfo *pResultInfo = NULL;
×
1184
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
×
1185
    if (code != 0) {
×
1186
      (*numOfRows) = 0;
×
1187
      return 0;
×
1188
    }
1189

1190
    pResultInfo->current = pResultInfo->numOfRows;
×
1191
    (*numOfRows) = pResultInfo->numOfRows;
×
1192
    (*pData) = (void *)pResultInfo->pData;
×
1193
    return 0;
×
1194
  }
1195

1196
  SRequestObj *pRequest = (SRequestObj *)res;
8✔
1197

1198
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
8!
1199
      pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
8!
1200
    return pRequest->code;
×
1201
  }
1202

1203
  (void)doAsyncFetchRows(pRequest, false, false);
8✔
1204

1205
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
8✔
1206

1207
  pResultInfo->current = pResultInfo->numOfRows;
8✔
1208
  (*numOfRows) = pResultInfo->numOfRows;
8✔
1209
  (*pData) = (void *)pResultInfo->pData;
8✔
1210

1211
  return pRequest->code;
8✔
1212
}
1213

1214
int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) {
20,036✔
1215
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
20,036!
1216
    return 0;
×
1217
  }
1218

1219
  int32_t numOfFields = taos_num_fields(res);
20,036✔
1220
  if (columnIndex < 0 || columnIndex >= numOfFields || numOfFields == 0) {
20,036!
1221
    return 0;
×
1222
  }
1223

1224
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
20,036✔
1225
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
20,036✔
1226
  if (!IS_VAR_DATA_TYPE(pField->type)) {
20,036!
1227
    return 0;
×
1228
  }
1229

1230
  return pResInfo->pCol[columnIndex].offset;
20,036✔
1231
}
1232

1233
int taos_is_null_by_column(TAOS_RES *res, int columnIndex, bool result[], int *rows) {
25,863✔
1234
  if (res == NULL || result == NULL || rows == NULL || *rows <= 0 || columnIndex < 0 || TD_RES_TMQ_META(res) ||
25,863!
1235
      TD_RES_TMQ_RAW(res) || TD_RES_TMQ_BATCH_META(res)) {
25,863!
1236
    return TSDB_CODE_INVALID_PARA;
×
1237
  }
1238

1239
  int32_t numOfFields = taos_num_fields(res);
25,863✔
1240
  if (columnIndex >= numOfFields || numOfFields == 0) {
25,863!
1241
    return TSDB_CODE_INVALID_PARA;
×
1242
  }
1243

1244
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
25,863✔
1245
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
25,863✔
1246
  SResultColumn  *pCol = &pResInfo->pCol[columnIndex];
25,863✔
1247

1248
  if (*rows > pResInfo->numOfRows) {
25,863!
1249
    *rows = pResInfo->numOfRows;
×
1250
  }
1251
  if (IS_VAR_DATA_TYPE(pField->type)) {
25,863!
1252
    for (int i = 0; i < *rows; i++) {
×
1253
      if (pCol->offset[i] == -1) {
×
1254
        result[i] = true;
×
1255
      } else {
1256
        result[i] = false;
×
1257
      }
1258
    }
1259
  } else {
1260
    for (int i = 0; i < *rows; i++) {
191,079✔
1261
      if (colDataIsNull_f(pCol, i)) {
165,216!
1262
        result[i] = true;
14,877✔
1263
      } else {
1264
        result[i] = false;
150,339✔
1265
      }
1266
    }
1267
  }
1268
  return 0;
25,863✔
1269
}
1270

1271
int taos_validate_sql(TAOS *taos, const char *sql) {
×
1272
  TAOS_RES *pObj = taosQueryImpl(taos, sql, true, TD_REQ_FROM_APP);
×
1273

1274
  int code = taos_errno(pObj);
×
1275

1276
  taos_free_result(pObj);
×
1277
  return code;
×
1278
}
1279

1280
void taos_reset_current_db(TAOS *taos) {
×
1281
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
1282
  if (pTscObj == NULL) {
×
1283
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1284
    return;
×
1285
  }
1286

1287
  resetConnectDB(pTscObj);
×
1288

1289
  releaseTscObj(*(int64_t *)taos);
×
1290
}
1291

1292
const char *taos_get_server_info(TAOS *taos) {
91✔
1293
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
91✔
1294
  if (pTscObj == NULL) {
91!
1295
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1296
    return NULL;
×
1297
  }
1298

1299
  releaseTscObj(*(int64_t *)taos);
91✔
1300

1301
  return pTscObj->sDetailVer;
91✔
1302
}
1303

1304
int taos_get_current_db(TAOS *taos, char *database, int len, int *required) {
4✔
1305
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
4✔
1306
  if (pTscObj == NULL) {
4!
1307
    return TSDB_CODE_TSC_DISCONNECTED;
×
1308
  }
1309

1310
  int code = TSDB_CODE_SUCCESS;
4✔
1311
  (void)taosThreadMutexLock(&pTscObj->mutex);
4✔
1312
  if (database == NULL || len <= 0) {
4!
1313
    if (required != NULL) *required = strlen(pTscObj->db) + 1;
2✔
1314
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
2!
1315
  } else if (len < strlen(pTscObj->db) + 1) {
2✔
1316
    tstrncpy(database, pTscObj->db, len);
1✔
1317
    if (required) *required = strlen(pTscObj->db) + 1;
1!
1318
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
1!
1319
  } else {
1320
    tstrncpy(database, pTscObj->db, len);
1✔
1321
    code = 0;
1✔
1322
  }
1323
_return:
4✔
1324
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
4✔
1325
  releaseTscObj(*(int64_t *)taos);
4✔
1326
  return code;
4✔
1327
}
1328

1329
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
17,225,516✔
1330
  if (NULL == pWrapper) {
17,225,516✔
1331
    return;
8,630,329✔
1332
  }
1333
  destoryCatalogReq(pWrapper->pCatalogReq);
8,595,187✔
1334
  taosMemoryFree(pWrapper->pCatalogReq);
8,606,021!
1335
  qDestroyParseContext(pWrapper->pParseCtx);
8,622,983✔
1336
  taosMemoryFree(pWrapper);
8,622,770!
1337
}
1338

1339
void destroyCtxInRequest(SRequestObj *pRequest) {
2,184✔
1340
  schedulerFreeJob(&pRequest->body.queryJob, 0);
2,184✔
1341
  qDestroyQuery(pRequest->pQuery);
2,184✔
1342
  pRequest->pQuery = NULL;
2,184✔
1343
  destorySqlCallbackWrapper(pRequest->pWrapper);
2,184✔
1344
  pRequest->pWrapper = NULL;
2,184✔
1345
}
2,184✔
1346

1347
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
266,311✔
1348
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
266,311✔
1349
  SRequestObj         *pRequest = pWrapper->pRequest;
266,311✔
1350
  SQuery              *pQuery = pRequest->pQuery;
266,311✔
1351

1352
  qDebug("req:0x%" PRIx64 ", start to semantic analysis, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
266,311✔
1353

1354
  int64_t analyseStart = taosGetTimestampUs();
266,311✔
1355
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
266,311✔
1356
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
266,311✔
1357

1358
  if (TSDB_CODE_SUCCESS == code) {
266,311✔
1359
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
266,269✔
1360
  }
1361

1362
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
266,224✔
1363

1364
  if (pRequest->parseOnly) {
266,224✔
1365
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
663✔
1366
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
663✔
1367
  }
1368

1369
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
266,224✔
1370
}
266,230✔
1371

1372
int32_t cloneCatalogReq(SCatalogReq **ppTarget, SCatalogReq *pSrc) {
×
1373
  int32_t      code = TSDB_CODE_SUCCESS;
×
1374
  SCatalogReq *pTarget = taosMemoryCalloc(1, sizeof(SCatalogReq));
×
1375
  if (pTarget == NULL) {
×
1376
    code = terrno;
×
1377
  } else {
1378
    pTarget->pDbVgroup = taosArrayDup(pSrc->pDbVgroup, NULL);
×
1379
    pTarget->pDbCfg = taosArrayDup(pSrc->pDbCfg, NULL);
×
1380
    pTarget->pDbInfo = taosArrayDup(pSrc->pDbInfo, NULL);
×
1381
    pTarget->pTableMeta = taosArrayDup(pSrc->pTableMeta, NULL);
×
1382
    pTarget->pTableHash = taosArrayDup(pSrc->pTableHash, NULL);
×
1383
    pTarget->pUdf = taosArrayDup(pSrc->pUdf, NULL);
×
1384
    pTarget->pIndex = taosArrayDup(pSrc->pIndex, NULL);
×
1385
    pTarget->pUser = taosArrayDup(pSrc->pUser, NULL);
×
1386
    pTarget->pTableIndex = taosArrayDup(pSrc->pTableIndex, NULL);
×
1387
    pTarget->pTableCfg = taosArrayDup(pSrc->pTableCfg, NULL);
×
1388
    pTarget->pTableTag = taosArrayDup(pSrc->pTableTag, NULL);
×
1389
    pTarget->pView = taosArrayDup(pSrc->pView, NULL);
×
1390
    pTarget->pTableTSMAs = taosArrayDup(pSrc->pTableTSMAs, NULL);
×
1391
    pTarget->pTSMAs = taosArrayDup(pSrc->pTSMAs, NULL);
×
1392
    pTarget->pVStbRefDbs = taosArrayDup(pSrc->pVStbRefDbs, NULL);
×
1393
    pTarget->qNodeRequired = pSrc->qNodeRequired;
×
1394
    pTarget->dNodeRequired = pSrc->dNodeRequired;
×
1395
    pTarget->svrVerRequired = pSrc->svrVerRequired;
×
1396
    pTarget->forceUpdate = pSrc->forceUpdate;
×
1397
    pTarget->cloned = true;
×
1398

1399
    *ppTarget = pTarget;
×
1400
  }
1401

1402
  return code;
×
1403
}
1404

1405
void handleSubQueryFromAnalyse(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, SNode *pRoot) {
×
1406
  SRequestObj         *pNewRequest = NULL;
×
1407
  SSqlCallbackWrapper *pNewWrapper = NULL;
×
1408
  int32_t              code = buildPreviousRequest(pWrapper->pRequest, pWrapper->pRequest->sqlstr, &pNewRequest);
×
1409
  if (code) {
×
1410
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1411
    return;
×
1412
  }
1413

1414
  pNewRequest->pQuery = NULL;
×
1415
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode **)&pNewRequest->pQuery);
×
1416
  if (pNewRequest->pQuery) {
×
1417
    pNewRequest->pQuery->pRoot = pRoot;
×
1418
    pRoot = NULL;
×
1419
    pNewRequest->pQuery->execStage = QUERY_EXEC_STAGE_ANALYSE;
×
1420
  }
1421
  if (TSDB_CODE_SUCCESS == code) {
×
1422
    code = prepareAndParseSqlSyntax(&pNewWrapper, pNewRequest, false);
×
1423
  }
1424
  if (TSDB_CODE_SUCCESS == code) {
×
1425
    code = cloneCatalogReq(&pNewWrapper->pCatalogReq, pWrapper->pCatalogReq);
×
1426
  }
1427
  if (TSDB_CODE_SUCCESS == code) {
×
1428
    doAsyncQueryFromAnalyse(pResultMeta, pNewWrapper, code);
×
1429
    nodesDestroyNode(pRoot);
×
1430
  } else {
1431
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1432
    return;
×
1433
  }
1434
}
1435

1436
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
266,220✔
1437
  SRequestObj *pRequest = pWrapper->pRequest;
266,220✔
1438
  SQuery      *pQuery = pRequest->pQuery;
266,220✔
1439

1440
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
266,220!
1441
    SNode *prevRoot = pQuery->pPrevRoot;
×
1442
    pQuery->pPrevRoot = NULL;
×
1443
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
×
1444
    return;
×
1445
  }
1446

1447
  if (code == TSDB_CODE_SUCCESS) {
266,220✔
1448
    pRequest->stableQuery = pQuery->stableQuery;
261,379✔
1449
    if (pQuery->pRoot) {
261,379✔
1450
      pRequest->stmtType = pQuery->pRoot->type;
261,375✔
1451
    }
1452

1453
    if (pQuery->haveResultSet) {
261,379✔
1454
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
150,366✔
1455
                              pRequest->stmtBindVersion > 0);
150,366✔
1456
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
150,365✔
1457
    }
1458
  }
1459

1460
  if (code == TSDB_CODE_SUCCESS) {
266,245✔
1461
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
261,346✔
1462
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
261,346✔
1463
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
261,346✔
1464

1465
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
261,346✔
1466
  } else {
1467
    destorySqlCallbackWrapper(pWrapper);
4,899✔
1468
    pRequest->pWrapper = NULL;
4,899✔
1469
    qDestroyQuery(pRequest->pQuery);
4,899✔
1470
    pRequest->pQuery = NULL;
4,899✔
1471

1472
    if (NEED_CLIENT_HANDLE_ERROR(code)) {
4,899!
1473
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
1,922✔
1474
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1475
      restartAsyncQuery(pRequest, code);
1,922✔
1476
      return;
1,922✔
1477
    }
1478

1479
    // return to app directly
1480
    tscError("req:0x%" PRIx64 ", error occurs, code:%s, return to user app, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
2,977!
1481
             pRequest->requestId);
1482
    pRequest->code = code;
2,977✔
1483
    returnToUser(pRequest);
2,977✔
1484
  }
1485
}
1486

1487
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
322,391✔
1488
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
322,391✔
1489
                           .requestId = pWrapper->pParseCtx->requestId,
322,391✔
1490
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
322,391✔
1491
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
322,391✔
1492

1493
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
322,391✔
1494

1495
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
644,790✔
1496
                                &pWrapper->pRequest->body.queryJob);
322,423✔
1497
}
1498

1499
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code);
1500

1501
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
8,607,138✔
1502
  int32_t code = TSDB_CODE_SUCCESS;
8,607,138✔
1503
  switch (pWrapper->pRequest->pQuery->execStage) {
8,607,138!
1504
    case QUERY_EXEC_STAGE_PARSE: {
56,144✔
1505
      // continue parse after get metadata
1506
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
56,144✔
1507
      break;
56,145✔
1508
    }
1509
    case QUERY_EXEC_STAGE_ANALYSE: {
266,263✔
1510
      // analysis after get metadata
1511
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
266,263✔
1512
      break;
266,250✔
1513
    }
1514
    case QUERY_EXEC_STAGE_SCHEDULE: {
8,303,201✔
1515
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
8,303,201✔
1516
      break;
8,301,682✔
1517
    }
1518
    default:
×
1519
      break;
×
1520
  }
1521
  return code;
8,605,607✔
1522
}
1523

1524
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code) {
56,151✔
1525
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
56,151✔
1526
  SRequestObj         *pRequest = pWrapper->pRequest;
56,151✔
1527
  SQuery              *pQuery = pRequest->pQuery;
56,151✔
1528

1529
  pRequest->metric.ctgCostUs += taosGetTimestampUs() - pRequest->metric.ctgStart;
56,151✔
1530
  qDebug("req:0x%" PRIx64 ", continue parse query, QID:0x%" PRIx64 ", code:%s", pRequest->self, pRequest->requestId,
56,151✔
1531
         tstrerror(code));
1532

1533
  if (code == TSDB_CODE_SUCCESS) {
56,150✔
1534
    // pWrapper->pCatalogReq->forceUpdate = false;
1535
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
55,752✔
1536
  }
1537

1538
  if (TSDB_CODE_SUCCESS == code) {
56,137✔
1539
    code = phaseAsyncQuery(pWrapper);
55,477✔
1540
  }
1541

1542
  if (TSDB_CODE_SUCCESS != code) {
56,141✔
1543
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
661!
1544
             tstrerror(code), pWrapper->pRequest->requestId);
1545
    destorySqlCallbackWrapper(pWrapper);
661✔
1546
    pRequest->pWrapper = NULL;
661✔
1547
    terrno = code;
661✔
1548
    pRequest->code = code;
660✔
1549
    doRequestCallback(pRequest, code);
660✔
1550
  }
1551
}
56,141✔
1552

1553
void continueInsertFromCsv(SSqlCallbackWrapper *pWrapper, SRequestObj *pRequest) {
×
1554
  int32_t code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
×
1555
  if (TSDB_CODE_SUCCESS == code) {
×
1556
    code = phaseAsyncQuery(pWrapper);
×
1557
  }
1558

1559
  if (TSDB_CODE_SUCCESS != code) {
×
1560
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
×
1561
             tstrerror(code), pWrapper->pRequest->requestId);
1562
    destorySqlCallbackWrapper(pWrapper);
×
1563
    pRequest->pWrapper = NULL;
×
1564
    terrno = code;
×
1565
    pRequest->code = code;
×
1566
    doRequestCallback(pRequest, code);
×
1567
  }
1568
}
×
1569

1570
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
141✔
1571
  int64_t connId = *(int64_t *)taos;
141✔
1572
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
141✔
1573
}
141✔
1574

1575
void taos_query_a_with_reqid(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param, int64_t reqid) {
×
1576
  int64_t connId = *(int64_t *)taos;
×
1577
  taosAsyncQueryImplWithReqid(connId, sql, fp, param, false, reqid);
×
1578
}
×
1579

1580
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
8,607,235✔
1581
  const STscObj *pTscObj = pRequest->pTscObj;
8,607,235✔
1582

1583
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
8,607,235!
1584
  if (*pCxt == NULL) {
8,611,550!
1585
    return terrno;
×
1586
  }
1587

1588
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
8,611,550✔
1589
                           .requestRid = pRequest->self,
8,611,550✔
1590
                           .acctId = pTscObj->acctId,
8,611,550✔
1591
                           .db = pRequest->pDb,
8,611,550✔
1592
                           .topicQuery = false,
1593
                           .pSql = pRequest->sqlstr,
8,611,550✔
1594
                           .sqlLen = pRequest->sqlLen,
8,611,550✔
1595
                           .pMsg = pRequest->msgBuf,
8,611,550✔
1596
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1597
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
8,611,550✔
1598
                           .pStmtCb = NULL,
1599
                           .pUser = pTscObj->user,
8,611,550✔
1600
                           .pEffectiveUser = pRequest->effectiveUser,
8,611,550✔
1601
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
8,611,550✔
1602
                           .enableSysInfo = pTscObj->sysInfo,
8,611,550✔
1603
                           .async = true,
1604
                           .svrVer = pTscObj->sVer,
8,611,550✔
1605
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
8,611,550✔
1606
                           .allocatorId = pRequest->allocatorRefId,
8,611,550✔
1607
                           .parseSqlFp = clientParseSql,
1608
                           .parseSqlParam = pWrapper,
1609
                           .setQueryFp = setQueryRequest,
1610
                           .timezone = pTscObj->optionInfo.timezone,
8,611,550✔
1611
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
8,611,550✔
1612
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
8,611,550✔
1613
  (*pCxt)->biMode = biMode;
8,582,562✔
1614
  return TSDB_CODE_SUCCESS;
8,582,562✔
1615
}
1616

1617
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
8,602,681✔
1618
  int32_t              code = TSDB_CODE_SUCCESS;
8,602,681✔
1619
  STscObj             *pTscObj = pRequest->pTscObj;
8,602,681✔
1620
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
8,602,681!
1621
  if (pWrapper == NULL) {
8,617,742!
1622
    code = terrno;
×
1623
  } else {
1624
    pWrapper->pRequest = pRequest;
8,617,742✔
1625
    pRequest->pWrapper = pWrapper;
8,617,742✔
1626
    *ppWrapper = pWrapper;
8,617,742✔
1627
  }
1628

1629
  if (TSDB_CODE_SUCCESS == code) {
8,617,742!
1630
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
8,618,779✔
1631
  }
1632

1633
  if (TSDB_CODE_SUCCESS == code) {
8,581,335!
1634
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
8,582,616✔
1635
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
8,624,278✔
1636
  }
1637

1638
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
8,620,145!
1639
    int64_t syntaxStart = taosGetTimestampUs();
8,597,971✔
1640

1641
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
8,597,971!
1642
    if (pWrapper->pCatalogReq == NULL) {
8,619,551!
1643
      code = terrno;
×
1644
    } else {
1645
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
8,619,551✔
1646
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
8,619,551!
1647
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
8,616,294✔
1648
    }
1649

1650
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
8,550,220✔
1651
  }
1652

1653
  return code;
8,572,108✔
1654
}
1655

1656
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
8,606,215✔
1657
  SSqlCallbackWrapper *pWrapper = NULL;
8,606,215✔
1658
  int32_t              code = TSDB_CODE_SUCCESS;
8,606,215✔
1659

1660
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
8,606,215✔
1661
    code = pRequest->prevCode;
650✔
1662
    terrno = code;
650✔
1663
    pRequest->code = code;
650✔
1664
    tscDebug("req:0x%" PRIx64 ", call sync query cb with code:%s", pRequest->self, tstrerror(code));
650✔
1665
    doRequestCallback(pRequest, code);
650✔
1666
    return;
657✔
1667
  }
1668

1669
  if (TSDB_CODE_SUCCESS == code) {
8,605,565✔
1670
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
8,604,373✔
1671
  }
1672

1673
  if (TSDB_CODE_SUCCESS == code) {
8,564,453✔
1674
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
8,567,575✔
1675
    code = phaseAsyncQuery(pWrapper);
8,567,575✔
1676
  }
1677

1678
  if (TSDB_CODE_SUCCESS != code) {
8,564,550✔
1679
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
1,743!
1680
             pRequest->requestId);
1681
    destorySqlCallbackWrapper(pWrapper);
1,743✔
1682
    pRequest->pWrapper = NULL;
1,743✔
1683
    qDestroyQuery(pRequest->pQuery);
1,743✔
1684
    pRequest->pQuery = NULL;
1,743✔
1685

1686
    if (NEED_CLIENT_HANDLE_ERROR(code)) {
1,743!
1687
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
7!
1688
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1689
      code = refreshMeta(pRequest->pTscObj, pRequest);
7✔
1690
      if (code != 0) {
7!
1691
        tscWarn("req:0x%" PRIx64 ", refresh meta failed, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
7!
1692
                pRequest->requestId);
1693
      }
1694
      pRequest->prevCode = code;
7✔
1695
      doAsyncQuery(pRequest, true);
7✔
1696
      return;
7✔
1697
    }
1698

1699
    terrno = code;
1,736✔
1700
    pRequest->code = code;
1,736✔
1701
    doRequestCallback(pRequest, code);
1,736✔
1702
  }
1703
}
1704

1705
void restartAsyncQuery(SRequestObj *pRequest, int32_t code) {
2,184✔
1706
  tscInfo("restart request:%s p:%p", pRequest->sqlstr, pRequest);
2,184!
1707
  SRequestObj *pUserReq = pRequest;
2,184✔
1708
  (void)acquireRequest(pRequest->self);
2,184✔
1709
  while (pUserReq) {
2,184!
1710
    if (pUserReq->self == pUserReq->relation.userRefId || pUserReq->relation.userRefId == 0) {
2,184!
1711
      break;
1712
    } else {
1713
      int64_t nextRefId = pUserReq->relation.nextRefId;
×
1714
      (void)releaseRequest(pUserReq->self);
×
1715
      if (nextRefId) {
×
1716
        pUserReq = acquireRequest(nextRefId);
×
1717
      }
1718
    }
1719
  }
1720
  bool hasSubRequest = pUserReq != pRequest || pRequest->relation.prevRefId != 0;
2,184!
1721
  if (pUserReq) {
2,184!
1722
    destroyCtxInRequest(pUserReq);
2,184✔
1723
    pUserReq->prevCode = code;
2,184✔
1724
    (void)memset(&pUserReq->relation, 0, sizeof(pUserReq->relation));
2,184✔
1725
  } else {
1726
    tscError("User req is missing");
×
1727
    (void)removeFromMostPrevReq(pRequest);
×
1728
    return;
×
1729
  }
1730
  if (hasSubRequest)
2,184!
1731
    (void)removeFromMostPrevReq(pRequest);
×
1732
  else
1733
    (void)releaseRequest(pUserReq->self);
2,184✔
1734
  doAsyncQuery(pUserReq, true);
2,184✔
1735
}
1736

1737
typedef struct SAsyncFetchParam {
1738
  SRequestObj      *pReq;
1739
  __taos_async_fn_t fp;
1740
  void             *param;
1741
} SAsyncFetchParam;
1742

1743
static int32_t doAsyncFetch(void *pParam) {
161,485✔
1744
  SAsyncFetchParam *param = pParam;
161,485✔
1745
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
161,485✔
1746
  taosMemoryFree(param);
161,477!
1747
  return TSDB_CODE_SUCCESS;
161,482✔
1748
}
1749

1750
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
161,487✔
1751
  if (res == NULL || fp == NULL) {
161,487!
1752
    tscError("taos_fetch_rows_a invalid paras");
×
1753
    return;
×
1754
  }
1755
  if (!TD_RES_QUERY(res)) {
161,491!
1756
    tscError("taos_fetch_rows_a res is NULL");
×
1757
    fp(param, res, TSDB_CODE_APP_ERROR);
×
1758
    return;
×
1759
  }
1760

1761
  SRequestObj *pRequest = res;
161,491✔
1762
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
161,491✔
1763
    fp(param, res, 0);
10✔
1764
    return;
10✔
1765
  }
1766

1767
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
161,481!
1768
  if (!pParam) {
161,484!
1769
    fp(param, res, terrno);
×
1770
    return;
×
1771
  }
1772
  pParam->pReq = pRequest;
161,484✔
1773
  pParam->fp = fp;
161,484✔
1774
  pParam->param = param;
161,484✔
1775
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
161,484✔
1776
  if (TSDB_CODE_SUCCESS != code) {
161,483!
1777
    taosMemoryFree(pParam);
×
1778
    fp(param, res, code);
×
1779
    return;
×
1780
  }
1781
}
1782

1783
void taos_fetch_raw_block_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
4✔
1784
  if (res == NULL || fp == NULL) {
4!
1785
    tscError("taos_fetch_raw_block_a invalid paras");
×
1786
    return;
×
1787
  }
1788
  if (!TD_RES_QUERY(res)) {
4!
1789
    tscError("taos_fetch_raw_block_a res is NULL");
×
1790
    return;
×
1791
  }
1792
  SRequestObj    *pRequest = res;
4✔
1793
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
4✔
1794

1795
  // set the current block is all consumed
1796
  pResultInfo->convertUcs4 = false;
4✔
1797

1798
  // it is a local executed query, no need to do async fetch
1799
  taos_fetch_rows_a(pRequest, fp, param);
4✔
1800
}
1801

1802
const void *taos_get_raw_block(TAOS_RES *res) {
×
1803
  if (res == NULL) {
×
1804
    tscError("taos_get_raw_block invalid paras");
×
1805
    return NULL;
×
1806
  }
1807
  if (!TD_RES_QUERY(res)) {
×
1808
    tscError("taos_get_raw_block res is NULL");
×
1809
    return NULL;
×
1810
  }
1811
  SRequestObj *pRequest = res;
×
1812

1813
  return pRequest->body.resInfo.pData;
×
1814
}
1815

1816
int taos_get_db_route_info(TAOS *taos, const char *db, TAOS_DB_ROUTE_INFO *dbInfo) {
×
1817
  if (NULL == taos) {
×
1818
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1819
    return terrno;
×
1820
  }
1821

1822
  if (NULL == db || NULL == dbInfo) {
×
1823
    tscError("invalid input param, db:%p, dbInfo:%p", db, dbInfo);
×
1824
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1825
    return terrno;
×
1826
  }
1827

1828
  int64_t      connId = *(int64_t *)taos;
×
1829
  SRequestObj *pRequest = NULL;
×
1830
  char        *sql = "taos_get_db_route_info";
×
1831
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1832
  if (code != TSDB_CODE_SUCCESS) {
×
1833
    terrno = code;
×
1834
    return terrno;
×
1835
  }
1836

1837
  STscObj  *pTscObj = pRequest->pTscObj;
×
1838
  SCatalog *pCtg = NULL;
×
1839
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1840
  if (code != TSDB_CODE_SUCCESS) {
×
1841
    goto _return;
×
1842
  }
1843

1844
  SRequestConnInfo conn = {
×
1845
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1846

1847
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1848

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

1852
  code = catalogGetDBVgInfo(pCtg, &conn, dbFName, dbInfo);
×
1853
  if (code) {
×
1854
    goto _return;
×
1855
  }
1856

1857
_return:
×
1858

1859
  terrno = code;
×
1860

1861
  destroyRequest(pRequest);
×
1862
  return code;
×
1863
}
1864

1865
int taos_get_table_vgId(TAOS *taos, const char *db, const char *table, int *vgId) {
×
1866
  if (NULL == taos) {
×
1867
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1868
    return terrno;
×
1869
  }
1870

1871
  if (NULL == db || NULL == table || NULL == vgId) {
×
1872
    tscError("invalid input param, db:%p, table:%p, vgId:%p", db, table, vgId);
×
1873
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1874
    return terrno;
×
1875
  }
1876

1877
  int64_t      connId = *(int64_t *)taos;
×
1878
  SRequestObj *pRequest = NULL;
×
1879
  char        *sql = "taos_get_table_vgId";
×
1880
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1881
  if (code != TSDB_CODE_SUCCESS) {
×
1882
    return terrno;
×
1883
  }
1884

1885
  pRequest->syncQuery = true;
×
1886

1887
  STscObj  *pTscObj = pRequest->pTscObj;
×
1888
  SCatalog *pCtg = NULL;
×
1889
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1890
  if (code != TSDB_CODE_SUCCESS) {
×
1891
    goto _return;
×
1892
  }
1893

1894
  SRequestConnInfo conn = {
×
1895
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1896

1897
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1898

1899
  SName tableName = {0};
×
1900
  toName(pTscObj->acctId, db, table, &tableName);
×
1901

1902
  SVgroupInfo vgInfo;
1903
  code = catalogGetTableHashVgroup(pCtg, &conn, &tableName, &vgInfo);
×
1904
  if (code) {
×
1905
    goto _return;
×
1906
  }
1907

1908
  *vgId = vgInfo.vgId;
×
1909

1910
_return:
×
1911

1912
  terrno = code;
×
1913

1914
  destroyRequest(pRequest);
×
1915
  return code;
×
1916
}
1917

1918
int taos_get_tables_vgId(TAOS *taos, const char *db, const char *table[], int tableNum, int *vgId) {
×
1919
  if (NULL == taos) {
×
1920
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1921
    return terrno;
×
1922
  }
1923

1924
  if (NULL == db || NULL == table || NULL == vgId || tableNum <= 0) {
×
1925
    tscError("invalid input param, db:%p, table:%p, vgId:%p, tbNum:%d", db, table, vgId, tableNum);
×
1926
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1927
    return terrno;
×
1928
  }
1929

1930
  int64_t      connId = *(int64_t *)taos;
×
1931
  SRequestObj *pRequest = NULL;
×
1932
  char        *sql = "taos_get_table_vgId";
×
1933
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1934
  if (code != TSDB_CODE_SUCCESS) {
×
1935
    return terrno;
×
1936
  }
1937

1938
  pRequest->syncQuery = true;
×
1939

1940
  STscObj  *pTscObj = pRequest->pTscObj;
×
1941
  SCatalog *pCtg = NULL;
×
1942
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1943
  if (code != TSDB_CODE_SUCCESS) {
×
1944
    goto _return;
×
1945
  }
1946

1947
  SRequestConnInfo conn = {
×
1948
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1949

1950
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1951

1952
  code = catalogGetTablesHashVgId(pCtg, &conn, pTscObj->acctId, db, table, tableNum, vgId);
×
1953
  if (code) {
×
1954
    goto _return;
×
1955
  }
1956

1957
_return:
×
1958

1959
  terrno = code;
×
1960

1961
  destroyRequest(pRequest);
×
1962
  return code;
×
1963
}
1964

1965
int taos_load_table_info(TAOS *taos, const char *tableNameList) {
×
1966
  if (NULL == taos) {
×
1967
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1968
    return terrno;
×
1969
  }
1970

1971
  int64_t       connId = *(int64_t *)taos;
×
1972
  const int32_t MAX_TABLE_NAME_LENGTH = 12 * 1024 * 1024;  // 12MB list
×
1973
  int32_t       code = 0;
×
1974
  SRequestObj  *pRequest = NULL;
×
1975
  SCatalogReq   catalogReq = {0};
×
1976

1977
  if (NULL == tableNameList) {
×
1978
    return TSDB_CODE_SUCCESS;
×
1979
  }
1980

1981
  int32_t length = (int32_t)strlen(tableNameList);
×
1982
  if (0 == length) {
×
1983
    return TSDB_CODE_SUCCESS;
×
1984
  } else if (length > MAX_TABLE_NAME_LENGTH) {
×
1985
    tscError("tableNameList too long, length:%d, maximum allowed:%d", length, MAX_TABLE_NAME_LENGTH);
×
1986
    return TSDB_CODE_TSC_INVALID_OPERATION;
×
1987
  }
1988

1989
  char *sql = "taos_load_table_info";
×
1990
  code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1991
  if (code != TSDB_CODE_SUCCESS) {
×
1992
    terrno = code;
×
1993
    goto _return;
×
1994
  }
1995

1996
  pRequest->syncQuery = true;
×
1997

1998
  STscObj *pTscObj = pRequest->pTscObj;
×
1999
  code = transferTableNameList(tableNameList, pTscObj->acctId, pTscObj->db, &catalogReq.pTableMeta);
×
2000
  if (code) {
×
2001
    goto _return;
×
2002
  }
2003

2004
  SCatalog *pCtg = NULL;
×
2005
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2006
  if (code != TSDB_CODE_SUCCESS) {
×
2007
    goto _return;
×
2008
  }
2009

2010
  SRequestConnInfo conn = {
×
2011
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2012

2013
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2014

2015
  code = catalogAsyncGetAllMeta(pCtg, &conn, &catalogReq, syncCatalogFn, pRequest->body.interParam, NULL);
×
2016
  if (code) {
×
2017
    goto _return;
×
2018
  }
2019

2020
  SSyncQueryParam *pParam = pRequest->body.interParam;
×
2021
  code = tsem_wait(&pParam->sem);
×
2022
  if (code) {
×
2023
    tscError("tsem wait failed, code:%d - %s", code, tstrerror(code));
×
2024
    goto _return;
×
2025
  }
2026
_return:
×
2027
  destoryCatalogReq(&catalogReq);
×
2028
  destroyRequest(pRequest);
×
2029
  return code;
×
2030
}
2031

2032
TAOS_STMT *taos_stmt_init(TAOS *taos) {
1,704✔
2033
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
1,704✔
2034
  if (NULL == pObj) {
1,704!
2035
    tscError("invalid parameter for %s", __FUNCTION__);
×
2036
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2037
    return NULL;
×
2038
  }
2039

2040
  TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL);
1,704✔
2041
  if (NULL == pStmt) {
1,704!
2042
    tscError("stmt init failed, errcode:%s", terrstr());
×
2043
  }
2044
  releaseTscObj(*(int64_t *)taos);
1,704✔
2045

2046
  return pStmt;
1,704✔
2047
}
2048

2049
TAOS_STMT *taos_stmt_init_with_reqid(TAOS *taos, int64_t reqid) {
×
2050
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
2051
  if (NULL == pObj) {
×
2052
    tscError("invalid parameter for %s", __FUNCTION__);
×
2053
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2054
    return NULL;
×
2055
  }
2056

2057
  TAOS_STMT *pStmt = stmtInit(pObj, reqid, NULL);
×
2058
  if (NULL == pStmt) {
×
2059
    tscError("stmt init failed, errcode:%s", terrstr());
×
2060
  }
2061
  releaseTscObj(*(int64_t *)taos);
×
2062

2063
  return pStmt;
×
2064
}
2065

2066
TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options) {
95✔
2067
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
95✔
2068
  if (NULL == pObj) {
95!
2069
    tscError("invalid parameter for %s", __FUNCTION__);
×
2070
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2071
    return NULL;
×
2072
  }
2073

2074
  TAOS_STMT *pStmt = stmtInit(pObj, options->reqId, options);
95✔
2075
  if (NULL == pStmt) {
95!
2076
    tscError("stmt init failed, errcode:%s", terrstr());
×
2077
  }
2078
  releaseTscObj(*(int64_t *)taos);
95✔
2079

2080
  return pStmt;
95✔
2081
}
2082

2083
int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
11,782✔
2084
  if (stmt == NULL || sql == NULL) {
11,782!
2085
    tscError("NULL parameter for %s", __FUNCTION__);
×
2086
    terrno = TSDB_CODE_INVALID_PARA;
×
2087
    return terrno;
×
2088
  }
2089

2090
  return stmtPrepare(stmt, sql, length);
11,795✔
2091
}
2092

2093
int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags) {
7✔
2094
  if (stmt == NULL || name == NULL) {
7!
2095
    tscError("NULL parameter for %s", __FUNCTION__);
×
2096
    terrno = TSDB_CODE_INVALID_PARA;
×
2097
    return terrno;
×
2098
  }
2099

2100
  int32_t code = stmtSetTbName(stmt, name);
7✔
2101
  if (code) {
7!
2102
    return code;
×
2103
  }
2104

2105
  if (tags) {
7!
2106
    return stmtSetTbTags(stmt, tags);
7✔
2107
  }
2108

2109
  return TSDB_CODE_SUCCESS;
×
2110
}
2111

2112
int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name) {
15,633✔
2113
  if (stmt == NULL || name == NULL) {
15,633!
2114
    tscError("NULL parameter for %s", __FUNCTION__);
×
2115
    terrno = TSDB_CODE_INVALID_PARA;
×
2116
    return terrno;
×
2117
  }
2118

2119
  return stmtSetTbName(stmt, name);
15,653✔
2120
}
2121

2122
int taos_stmt_set_tags(TAOS_STMT *stmt, TAOS_MULTI_BIND *tags) {
4✔
2123
  if (stmt == NULL || tags == NULL) {
4!
2124
    tscError("NULL parameter for %s", __FUNCTION__);
×
2125
    terrno = TSDB_CODE_INVALID_PARA;
×
2126
    return terrno;
×
2127
  }
2128

2129
  return stmtSetTbTags(stmt, tags);
4✔
2130
}
2131

2132
int taos_stmt_set_sub_tbname(TAOS_STMT *stmt, const char *name) { return taos_stmt_set_tbname(stmt, name); }
2✔
2133

2134
int taos_stmt_get_tag_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
4✔
2135
  if (stmt == NULL || NULL == fieldNum) {
4!
2136
    tscError("NULL parameter for %s", __FUNCTION__);
×
2137
    terrno = TSDB_CODE_INVALID_PARA;
×
2138
    return terrno;
×
2139
  }
2140

2141
  return stmtGetTagFields(stmt, fieldNum, fields);
4✔
2142
}
2143

2144
int taos_stmt_get_col_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
6✔
2145
  if (stmt == NULL || NULL == fieldNum) {
6!
2146
    tscError("NULL parameter for %s", __FUNCTION__);
×
2147
    terrno = TSDB_CODE_INVALID_PARA;
×
2148
    return terrno;
×
2149
  }
2150

2151
  return stmtGetColFields(stmt, fieldNum, fields);
6✔
2152
}
2153

2154
// let stmt to reclaim TAOS_FIELD_E that was allocated by `taos_stmt_get_tag_fields`/`taos_stmt_get_col_fields`
2155
void taos_stmt_reclaim_fields(TAOS_STMT *stmt, TAOS_FIELD_E *fields) {
×
2156
  (void)stmt;
2157
  if (!fields) return;
×
2158
  taosMemoryFree(fields);
×
2159
}
2160

2161
int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
95✔
2162
  if (stmt == NULL || bind == NULL) {
95!
2163
    tscError("NULL parameter for %s", __FUNCTION__);
×
2164
    terrno = TSDB_CODE_INVALID_PARA;
×
2165
    return terrno;
×
2166
  }
2167

2168
  if (bind->num > 1) {
95!
2169
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
×
2170
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2171
    return terrno;
×
2172
  }
2173

2174
  return stmtBindBatch(stmt, bind, -1);
95✔
2175
}
2176

2177
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
424,016✔
2178
  if (stmt == NULL || bind == NULL) {
424,016!
2179
    tscError("NULL parameter for %s", __FUNCTION__);
×
2180
    terrno = TSDB_CODE_INVALID_PARA;
×
2181
    return terrno;
×
2182
  }
2183

2184
  if (bind->num <= 0 || bind->num > INT16_MAX) {
424,252!
2185
    tscError("invalid bind num %d", bind->num);
×
2186
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2187
    return terrno;
×
2188
  }
2189

2190
  int32_t insert = 0;
424,464✔
2191
  int32_t code = stmtIsInsert(stmt, &insert);
424,464✔
2192
  if (TSDB_CODE_SUCCESS != code) {
423,201!
2193
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2194
    return code;
×
2195
  }
2196
  if (0 == insert && bind->num > 1) {
423,201!
2197
    tscError("only one row data allowed for query");
×
2198
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2199
    return terrno;
×
2200
  }
2201

2202
  return stmtBindBatch(stmt, bind, -1);
423,201✔
2203
}
2204

2205
int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind, int colIdx) {
×
2206
  if (stmt == NULL || bind == NULL) {
×
2207
    tscError("NULL parameter for %s", __FUNCTION__);
×
2208
    terrno = TSDB_CODE_INVALID_PARA;
×
2209
    return terrno;
×
2210
  }
2211

2212
  if (colIdx < 0) {
×
2213
    tscError("invalid bind column idx %d", colIdx);
×
2214
    terrno = TSDB_CODE_INVALID_PARA;
×
2215
    return terrno;
×
2216
  }
2217

2218
  int32_t insert = 0;
×
2219
  int32_t code = stmtIsInsert(stmt, &insert);
×
2220
  if (TSDB_CODE_SUCCESS != code) {
×
2221
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2222
    return code;
×
2223
  }
2224
  if (0 == insert && bind->num > 1) {
×
2225
    tscError("only one row data allowed for query");
×
2226
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2227
    return terrno;
×
2228
  }
2229

2230
  return stmtBindBatch(stmt, bind, colIdx);
×
2231
}
2232

2233
int taos_stmt_add_batch(TAOS_STMT *stmt) {
400,493✔
2234
  if (stmt == NULL) {
400,493!
2235
    tscError("NULL parameter for %s", __FUNCTION__);
×
2236
    terrno = TSDB_CODE_INVALID_PARA;
×
2237
    return terrno;
×
2238
  }
2239

2240
  return stmtAddBatch(stmt);
400,493✔
2241
}
2242

2243
int taos_stmt_execute(TAOS_STMT *stmt) {
16,178✔
2244
  if (stmt == NULL) {
16,178!
2245
    tscError("NULL parameter for %s", __FUNCTION__);
×
2246
    terrno = TSDB_CODE_INVALID_PARA;
×
2247
    return terrno;
×
2248
  }
2249

2250
  return stmtExec(stmt);
16,178✔
2251
}
2252

2253
int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
×
2254
  if (stmt == NULL || insert == NULL) {
×
2255
    tscError("NULL parameter for %s", __FUNCTION__);
×
2256
    terrno = TSDB_CODE_INVALID_PARA;
×
2257
    return terrno;
×
2258
  }
2259

2260
  return stmtIsInsert(stmt, insert);
×
2261
}
2262

2263
int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) {
×
2264
  if (stmt == NULL || nums == NULL) {
×
2265
    tscError("NULL parameter for %s", __FUNCTION__);
×
2266
    terrno = TSDB_CODE_INVALID_PARA;
×
2267
    return terrno;
×
2268
  }
2269

2270
  return stmtGetParamNum(stmt, nums);
×
2271
}
2272

2273
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
4✔
2274
  if (stmt == NULL || type == NULL || NULL == bytes || idx < 0) {
4!
2275
    tscError("invalid parameter for %s", __FUNCTION__);
×
2276
    terrno = TSDB_CODE_INVALID_PARA;
×
2277
    return terrno;
×
2278
  }
2279

2280
  return stmtGetParam(stmt, idx, type, bytes);
4✔
2281
}
2282

2283
TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt) {
2✔
2284
  if (stmt == NULL) {
2!
2285
    tscError("NULL parameter for %s", __FUNCTION__);
×
2286
    terrno = TSDB_CODE_INVALID_PARA;
×
2287
    return NULL;
×
2288
  }
2289

2290
  return stmtUseResult(stmt);
2✔
2291
}
2292

2293
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
8✔
2294

2295
int taos_stmt_affected_rows(TAOS_STMT *stmt) {
×
2296
  if (stmt == NULL) {
×
2297
    tscError("NULL parameter for %s", __FUNCTION__);
×
2298
    terrno = TSDB_CODE_INVALID_PARA;
×
2299
    return 0;
×
2300
  }
2301

2302
  return stmtAffectedRows(stmt);
×
2303
}
2304

2305
int taos_stmt_affected_rows_once(TAOS_STMT *stmt) {
28✔
2306
  if (stmt == NULL) {
28!
2307
    tscError("NULL parameter for %s", __FUNCTION__);
×
2308
    terrno = TSDB_CODE_INVALID_PARA;
×
2309
    return 0;
×
2310
  }
2311

2312
  return stmtAffectedRowsOnce(stmt);
28✔
2313
}
2314

2315
int taos_stmt_close(TAOS_STMT *stmt) {
1,797✔
2316
  if (stmt == NULL) {
1,797!
2317
    tscError("NULL parameter for %s", __FUNCTION__);
×
2318
    terrno = TSDB_CODE_INVALID_PARA;
×
2319
    return terrno;
×
2320
  }
2321

2322
  return stmtClose(stmt);
1,797✔
2323
}
2324

2325
TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option) {
172✔
2326
  if (NULL == taos) {
172✔
2327
    tscError("NULL parameter for %s", __FUNCTION__);
1!
2328
    terrno = TSDB_CODE_INVALID_PARA;
1✔
2329
    return NULL;
1✔
2330
  }
2331
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
171✔
2332
  if (NULL == pObj) {
171!
2333
    tscError("invalid parameter for %s", __FUNCTION__);
×
2334
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2335
    return NULL;
×
2336
  }
2337

2338
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
171✔
2339

2340
  releaseTscObj(*(int64_t *)taos);
171✔
2341

2342
  return pStmt;
171✔
2343
}
2344

2345
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
196✔
2346
  if (stmt == NULL || sql == NULL) {
196!
2347
    tscError("NULL parameter for %s", __FUNCTION__);
1!
2348
    terrno = TSDB_CODE_INVALID_PARA;
1✔
2349
    return terrno;
1✔
2350
  }
2351

2352
  return stmtPrepare2(stmt, sql, length);
195✔
2353
}
2354

2355
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
4,596✔
2356
  if (stmt == NULL) {
4,596!
2357
    tscError("NULL parameter for %s", __FUNCTION__);
×
2358
    terrno = TSDB_CODE_INVALID_PARA;
×
2359
    return terrno;
×
2360
  }
2361

2362
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
4,596✔
2363
  STMT2_DLOG_E("start to bind param");
4,596!
2364
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 1) {
4,596!
2365
    STMT2_ELOG_E("async bind param is still working, please try again later");
×
2366
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
×
2367
    return terrno;
×
2368
  }
2369

2370
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
4,596✔
2371
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
13!
2372
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
2373
    }
2374
    pStmt->execSemWaited = true;
13✔
2375
  }
2376

2377
  int32_t code = TSDB_CODE_SUCCESS;
4,596✔
2378
  for (int i = 0; i < bindv->count; ++i) {
12,235✔
2379
    if (bindv->tbnames && bindv->tbnames[i]) {
7,647!
2380
      code = stmtSetTbName2(stmt, bindv->tbnames[i]);
7,630✔
2381
      if (code) {
7,630✔
2382
        terrno = code;
3✔
2383
        STMT2_ELOG("set tbname failed, code:%s", tstrerror(code));
3!
2384
        return terrno;
9✔
2385
      }
2386
    }
2387

2388
    SVCreateTbReq *pCreateTbReq = NULL;
7,644✔
2389
    if (bindv->tags && bindv->tags[i]) {
7,644!
2390
      code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
168✔
2391
    } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
7,476✔
2392
      code = stmtCheckTags2(stmt, &pCreateTbReq);
22✔
2393
    } else if (pStmt->sql.autoCreateTbl) {
7,454✔
2394
      // if (pStmt->sql.autoCreateTbl) {
2395
      //   pStmt->sql.autoCreateTbl = false;
2396
      //   STMT2_WLOG_E("sql is autoCreateTbl, but no tags");
2397
      // }
2398
      code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
21✔
2399
    }
2400

2401
    if (code) {
7,644✔
2402
      terrno = code;
1✔
2403
      STMT2_ELOG("set tags failed, code:%s", tstrerror(code));
1!
2404
      return terrno;
1✔
2405
    }
2406

2407
    if (bindv->bind_cols && bindv->bind_cols[i]) {
7,643!
2408
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
7,642✔
2409

2410
      if (bind->num <= 0 || bind->num > INT16_MAX) {
7,642!
2411
        STMT2_ELOG("bind num:%d must > 0 and < INT16_MAX", bind->num);
×
2412
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2413
        return terrno;
5✔
2414
      }
2415

2416
      int32_t insert = 0;
7,642✔
2417
      (void)stmtIsInsert2(stmt, &insert);
7,642✔
2418
      if (0 == insert && bind->num > 1) {
7,642✔
2419
        STMT2_ELOG_E("only one row data allowed for query");
1!
2420
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
1✔
2421
        return terrno;
1✔
2422
      }
2423

2424
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
7,641✔
2425
      if (TSDB_CODE_SUCCESS != code) {
7,642✔
2426
        terrno = code;
4✔
2427
        STMT2_ELOG("bind batch failed, code:%s", tstrerror(code));
4!
2428
        return terrno;
4✔
2429
      }
2430
    }
2431
  }
2432

2433
  return code;
4,588✔
2434
}
2435

2436
int taos_stmt2_bind_param_a(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx, __taos_async_fn_t fp,
×
2437
                            void *param) {
2438
  if (stmt == NULL || bindv == NULL || fp == NULL) {
×
2439
    terrno = TSDB_CODE_INVALID_PARA;
×
2440
    return terrno;
×
2441
  }
2442

2443
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2444

2445
  ThreadArgs *args = (ThreadArgs *)taosMemoryMalloc(sizeof(ThreadArgs));
×
2446
  args->stmt = stmt;
×
2447
  args->bindv = bindv;
×
2448
  args->col_idx = col_idx;
×
2449
  args->fp = fp;
×
2450
  args->param = param;
×
2451

2452
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2453
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 0) {
×
2454
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2455
    tscError("async bind param is still working, please try again later");
×
2456
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
×
2457
    return terrno;
×
2458
  }
2459
  (void)atomic_add_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2460
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2461

2462
  int code_s = taosStmt2AsyncBind(stmtAsyncBindThreadFunc, (void *)args);
×
2463
  if (code_s != TSDB_CODE_SUCCESS) {
×
2464
    terrno = code_s;
×
2465
    (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2466
    (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2467
    (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2468
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2469
    tscError("async bind failed, code:%d , %s", code_s, tstrerror(code_s));
×
2470
  }
2471

2472
  return code_s;
×
2473
}
2474

2475
int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows) {
4,572✔
2476
  if (stmt == NULL) {
4,572!
2477
    tscError("NULL parameter for %s", __FUNCTION__);
×
2478
    terrno = TSDB_CODE_INVALID_PARA;
×
2479
    return terrno;
×
2480
  }
2481

2482
  return stmtExec2(stmt, affected_rows);
4,572✔
2483
}
2484

2485
int taos_stmt2_close(TAOS_STMT2 *stmt) {
170✔
2486
  if (stmt == NULL) {
170!
2487
    tscError("NULL parameter for %s", __FUNCTION__);
×
2488
    terrno = TSDB_CODE_INVALID_PARA;
×
2489
    return terrno;
×
2490
  }
2491

2492
  return stmtClose2(stmt);
170✔
2493
}
2494

2495
int taos_stmt2_is_insert(TAOS_STMT2 *stmt, int *insert) {
1✔
2496
  if (stmt == NULL || insert == NULL) {
1!
2497
    tscError("NULL parameter for %s", __FUNCTION__);
×
2498
    terrno = TSDB_CODE_INVALID_PARA;
×
2499
    return terrno;
×
2500
  }
2501

2502
  return stmtIsInsert2(stmt, insert);
1✔
2503
}
2504

2505
int taos_stmt2_get_fields(TAOS_STMT2 *stmt, int *count, TAOS_FIELD_ALL **fields) {
72✔
2506
  if (stmt == NULL || count == NULL) {
72!
2507
    tscError("NULL parameter for %s", __FUNCTION__);
1!
2508
    terrno = TSDB_CODE_INVALID_PARA;
1✔
2509
    return terrno;
1✔
2510
  }
2511

2512
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
71✔
2513
  if (pStmt->sql.type == 0) {
71✔
2514
    int isInsert = 0;
64✔
2515
    (void)stmtIsInsert2(stmt, &isInsert);
64✔
2516
    if (!isInsert) {
64✔
2517
      pStmt->sql.type = STMT_TYPE_QUERY;
13✔
2518
    }
2519
  }
2520

2521
  if (pStmt->sql.type == STMT_TYPE_QUERY) {
71✔
2522
    return stmtGetParamNum2(stmt, count);
13✔
2523
  }
2524

2525
  return stmtGetStbColFields2(stmt, count, fields);
58✔
2526
}
2527

2528
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_ALL *fields) {
56✔
2529
  (void)stmt;
2530
  if (!fields) return;
56✔
2531
  taosMemoryFree(fields);
37!
2532
}
2533

2534
TAOS_RES *taos_stmt2_result(TAOS_STMT2 *stmt) {
5✔
2535
  if (stmt == NULL) {
5!
2536
    tscError("NULL parameter for %s", __FUNCTION__);
×
2537
    terrno = TSDB_CODE_INVALID_PARA;
×
2538
    return NULL;
×
2539
  }
2540

2541
  return stmtUseResult2(stmt);
5✔
2542
}
2543

2544
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmtErrstr2(stmt); }
5✔
2545

2546
int taos_set_conn_mode(TAOS *taos, int mode, int value) {
8✔
2547
  if (taos == NULL) {
8!
2548
    terrno = TSDB_CODE_INVALID_PARA;
×
2549
    return terrno;
×
2550
  }
2551

2552
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
8✔
2553
  if (NULL == pObj) {
8!
2554
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2555
    tscError("invalid parameter for %s", __func__);
×
2556
    return terrno;
×
2557
  }
2558
  switch (mode) {
8!
2559
    case TAOS_CONN_MODE_BI:
8✔
2560
      atomic_store_8(&pObj->biMode, value);
8✔
2561
      break;
8✔
2562
    default:
×
2563
      tscError("not supported mode.");
×
2564
      return TSDB_CODE_INVALID_PARA;
×
2565
  }
2566
  return 0;
8✔
2567
}
2568

2569
char *getBuildInfo() { return td_buildinfo; }
×
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