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

taosdata / TDengine / #4813

16 Oct 2025 11:40AM UTC coverage: 58.702% (+0.009%) from 58.693%
#4813

push

travis-ci

web-flow
fix(tref): increase TSDB_REF_OBJECTS from 100 to 2000 for improved reference handling (#33281)

139888 of 303532 branches covered (46.09%)

Branch coverage included in aggregate %.

211578 of 295200 relevant lines covered (71.67%)

16853229.31 hits per line

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

43.0
/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, ...) {
788✔
46
  if (arg == NULL) {
788!
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) {
788!
52
    if (i % 1000 == 0) {
×
53
      (void)sched_yield();
×
54
    }
55
  }
56

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

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

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

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

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

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

95
  tscDebug("set timezone to %s", val);
24✔
96
  tz = tzalloc(val);
24✔
97
  if (tz == NULL) {
24✔
98
    tscWarn("%s unknown timezone %s change to UTC", __func__, val);
2!
99
    tz = tzalloc("UTC");
2✔
100
    if (tz == NULL) {
2!
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));
24✔
107
  if (code != 0) {
24!
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();
24✔
115
  char   output[TD_TIMEZONE_LEN] = {0};
24✔
116
  code = taosFormatTimezoneStr(tx1, val, tz, output);
24✔
117
  if (code == 0) {
24!
118
    code = taosHashPut(pTimezoneNameMap, &tz, sizeof(timezone_t), output, strlen(output) + 1);
24✔
119
  }
120
  if (code != 0) {
24!
121
    tscError("failed to put timezone %s to map", val);
×
122
  }
123

124
END:
24✔
125
  return tz;
39✔
126
}
127
#endif
128

129
static int32_t setConnectionOption(TAOS *taos, TSDB_OPTION_CONNECTION option, const char *val) {
85✔
130
  if (taos == NULL) {
85✔
131
    return terrno = TSDB_CODE_INVALID_PARA;
2✔
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) {
83!
141
    return terrno = TSDB_CODE_INVALID_PARA;
2✔
142
  }
143

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

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

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

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

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

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

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

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

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

241
  monitorClose();
4,646✔
242
  tscStopCrashReport();
4,646✔
243

244
  hbMgrCleanUp();
4,646✔
245

246
  catalogDestroy();
4,646✔
247
  schedulerDestroy();
4,646✔
248

249
  fmFuncMgtDestroy();
4,646✔
250
  qCleanupKeywordsTable();
4,646✔
251

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

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

261
  id = clientConnRefPool;
4,646✔
262
  clientConnRefPool = -1;
4,646✔
263
  taosCloseRef(id);
4,646✔
264

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

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

274
  taosConvDestroy();
4,646✔
275
  DestroyRegexCache();
4,646✔
276
#ifdef TAOSD_INTEGRATED
277
  shellStopDaemon();
278
#endif
279
  tscInfo("all local resources released");
4,646!
280
  taosCleanupCfg();
4,646✔
281
#ifndef TAOSD_INTEGRATED
282
  taosCloseLog();
4,646✔
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) {
11,825✔
300
  tscInfo("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
11,825!
301
  if (user == NULL) {
11,947✔
302
    user = TSDB_DEFAULT_USER;
940✔
303
  }
304

305
  if (pass == NULL) {
11,947✔
306
    pass = TSDB_DEFAULT_PASS;
940✔
307
  }
308

309
  STscObj *pObj = NULL;
11,947✔
310
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
11,947✔
311
  if (TSDB_CODE_SUCCESS == code) {
12,036✔
312
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
11,984!
313
    if (NULL == rid) {
11,986!
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;
11,986✔
318
    return (TAOS *)rid;
11,986✔
319
  } else {
320
    terrno = code;
52✔
321
  }
322

323
  return NULL;
53✔
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) {
12,102✔
635
  if (taos == NULL) {
12,102✔
636
    return;
1✔
637
  }
638

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

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

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

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

658
  taos_close_internal(pObj);
11,988✔
659
  releaseTscObj(*(int64_t *)taos);
11,986✔
660
  taosMemoryFree(taos);
11,988!
661
}
662

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

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

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

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

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

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

692
void taos_free_result(TAOS_RES *res) {
8,266,493✔
693
  if (NULL == res) {
8,266,493✔
694
    return;
24,157✔
695
  }
696

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

699
  if (TD_RES_QUERY(res)) {
8,251,791✔
700
    SRequestObj *pRequest = (SRequestObj *)res;
8,251,482✔
701
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
8,251,482✔
702
    destroyRequest(pRequest);
8,251,483✔
703
    return;
8,253,582✔
704
  }
705

706
  SMqRspObj *pRsp = (SMqRspObj *)res;
309✔
707
  if (TD_RES_TMQ(res)) {
309✔
708
    tDeleteMqDataRsp(&pRsp->dataRsp);
101✔
709
    doFreeReqResultInfo(&pRsp->resInfo);
101✔
710
  } else if (TD_RES_TMQ_METADATA(res)) {
208✔
711
    tDeleteSTaosxRsp(&pRsp->dataRsp);
8✔
712
    doFreeReqResultInfo(&pRsp->resInfo);
8✔
713
  } else if (TD_RES_TMQ_META(res)) {
200✔
714
    tDeleteMqMetaRsp(&pRsp->metaRsp);
185✔
715
  } else if (TD_RES_TMQ_BATCH_META(res)) {
15!
716
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
15✔
717
  } else if (TD_RES_TMQ_RAW(res)) {
×
718
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
719
  }
720
  taosMemoryFree(pRsp);
309!
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) {
3,779,693✔
737
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
3,779,693!
738
    return 0;
×
739
  }
740

741
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
3,779,705✔
742
  return pResInfo->numOfCols;
3,779,705✔
743
}
744

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

747
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
159,188✔
748
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
159,188!
749
    return NULL;
2,897✔
750
  }
751

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

756
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
8,231,825✔
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,254,576✔
770
  if (res == NULL) {
3,254,576!
771
    return NULL;
×
772
  }
773

774
  if (TD_RES_QUERY(res)) {
3,254,576✔
775
    SRequestObj *pRequest = (SRequestObj *)res;
3,253,316✔
776
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
3,253,316!
777
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0 || pRequest->killed) {
3,253,287!
778
      return NULL;
3✔
779
    }
780

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

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

799
    if (pResultInfo->current < pResultInfo->numOfRows) {
1,260✔
800
      doSetOneRowPtr(pResultInfo);
1,240✔
801
      pResultInfo->current += 1;
1,240✔
802
      return pResultInfo->row;
1,240✔
803
    } else {
804
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
20✔
805
        return NULL;
27✔
806
      }
807

808
      doSetOneRowPtr(pResultInfo);
8✔
809
      pResultInfo->current += 1;
8✔
810
      return pResultInfo->row;
8✔
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) {
10,549✔
822
  return taos_print_row_with_size(str, INT32_MAX, row, fields, num_fields);
10,549✔
823
}
824
int taos_print_row_with_size(char *str, uint32_t size, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
10,549✔
825
  int32_t len = 0;
10,549✔
826
  for (int i = 0; i < num_fields; ++i) {
81,003✔
827
    if (i > 0 && len < size - 1) {
70,454!
828
      str[len++] = ' ';
59,905✔
829
    }
830

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

836
    switch (fields[i].type) {
70,436!
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:
8,741✔
854
        len += tsnprintf(str + len, size - len, "%d", *((int32_t *)row[i]));
8,741✔
855
        break;
8,741✔
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: {
2,200✔
870
        float fv = 0;
2,200✔
871
        fv = GET_FLOAT_VAL(row[i]);
2,200✔
872
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
2,200✔
873
      } break;
2,200✔
874

875
      case TSDB_DATA_TYPE_DOUBLE: {
8,244✔
876
        double dv = 0;
8,244✔
877
        dv = GET_DOUBLE_VAL(row[i]);
8,244✔
878
        len += snprintf(str + len, size - len, "%.*g", DBL_DIG, dv);
8,244✔
879
      } break;
8,244✔
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:
21,403✔
894
      case TSDB_DATA_TYPE_NCHAR:
895
      case TSDB_DATA_TYPE_GEOMETRY: {
896
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
21,403✔
897
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
21,403!
898
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
4,400✔
899
          if (charLen > fields[i].bytes || charLen < 0) {
19,203!
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);
21,403✔
911
        (void)memcpy(str + len, row[i], copyLen);
21,403✔
912
        len += copyLen;
21,403✔
913
      } break;
21,403✔
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:
8,721✔
931
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
8,721✔
932
        break;
8,721✔
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) {
70,436!
949
      break;
×
950
    }
951
  }
952
  if (len < size) {
10,549!
953
    str[len] = 0;
10,549✔
954
  }
955

956
  return len;
10,549✔
957
}
958

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

964
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
3,112,329✔
965
  return pResInfo->length;
3,112,329✔
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; }
4,077✔
1034

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

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

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

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

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

1064
  if (TD_RES_QUERY(res)) {
26,222!
1065
    SRequestObj *pRequest = (SRequestObj *)res;
26,222✔
1066
    return pRequest->body.resInfo.precision;
26,222✔
1067
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
×
1068
    SReqResultInfo *info = tmqGetCurResInfo(res);
×
1069
    return info->precision;
×
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);
400✔
1094

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

1100
void taos_stop_query(TAOS_RES *res) {
8,258,560✔
1101
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
8,258,560!
1102
      TD_RES_TMQ_BATCH_META(res)) {
8,270,304!
1103
    return;
×
1104
  }
1105

1106
  stopAllQueries((SRequestObj *)res);
8,270,556✔
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) {
25,801✔
1129
  int32_t numOfRows = 0;
25,801✔
1130
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
25,801✔
1131
  return numOfRows;
25,801✔
1132
}
1133

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

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

1142
    (*rows) = NULL;
25,801✔
1143
    (*numOfRows) = 0;
25,801✔
1144

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

1150
    (void)doAsyncFetchRows(pRequest, false, true);
25,504✔
1151

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

1156
    (*rows) = pResultInfo->row;
25,504✔
1157
    (*numOfRows) = pResultInfo->numOfRows;
25,504✔
1158
    return pRequest->code;
25,504✔
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) {
29,383✔
1215
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
29,383!
1216
    return 0;
×
1217
  }
1218

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

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

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

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

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

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

1248
  if (*rows > pResInfo->numOfRows) {
45,517!
1249
    *rows = pResInfo->numOfRows;
×
1250
  }
1251
  if (IS_VAR_DATA_TYPE(pField->type)) {
45,517!
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++) {
304,095✔
1261
      if (colDataIsNull_f(pCol, i)) {
258,578!
1262
        result[i] = true;
6,967✔
1263
      } else {
1264
        result[i] = false;
251,611✔
1265
      }
1266
    }
1267
  }
1268
  return 0;
45,517✔
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) {
109✔
1293
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
109✔
1294
  if (pTscObj == NULL) {
109!
1295
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1296
    return NULL;
×
1297
  }
1298

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

1301
  return pTscObj->sDetailVer;
109✔
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) {
16,479,116✔
1330
  if (NULL == pWrapper) {
16,479,116✔
1331
    return;
8,258,996✔
1332
  }
1333
  destoryCatalogReq(pWrapper->pCatalogReq);
8,220,120✔
1334
  taosMemoryFree(pWrapper->pCatalogReq);
8,229,423!
1335
  qDestroyParseContext(pWrapper->pParseCtx);
8,252,592✔
1336
  taosMemoryFree(pWrapper);
8,253,200!
1337
}
1338

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

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

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

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

1358
  if (TSDB_CODE_SUCCESS == code) {
249,703✔
1359
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
249,702✔
1360
  }
1361

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

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

1369
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
249,648✔
1370
}
249,625✔
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) {
249,592✔
1437
  SRequestObj *pRequest = pWrapper->pRequest;
249,592✔
1438
  SQuery      *pQuery = pRequest->pQuery;
249,592✔
1439

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

1447
  if (code == TSDB_CODE_SUCCESS) {
249,592✔
1448
    pRequest->stableQuery = pQuery->stableQuery;
246,532✔
1449
    if (pQuery->pRoot) {
246,532✔
1450
      pRequest->stmtType = pQuery->pRoot->type;
246,529✔
1451
    }
1452

1453
    if (pQuery->haveResultSet) {
246,532✔
1454
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
143,766✔
1455
                              pRequest->stmtBindVersion > 0);
143,766✔
1456
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
143,773✔
1457
    }
1458
  }
1459

1460
  if (code == TSDB_CODE_SUCCESS) {
249,617✔
1461
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
246,490✔
1462
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
246,490✔
1463
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
246,490✔
1464

1465
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
246,490✔
1466
  } else {
1467
    destorySqlCallbackWrapper(pWrapper);
3,127✔
1468
    pRequest->pWrapper = NULL;
3,127✔
1469
    qDestroyQuery(pRequest->pQuery);
3,127✔
1470
    pRequest->pQuery = NULL;
3,127✔
1471

1472
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
3,127!
1473
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
2,268✔
1474
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1475
      restartAsyncQuery(pRequest, code);
2,268✔
1476
      return;
2,268✔
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),
859!
1481
             pRequest->requestId);
1482
    pRequest->code = code;
859✔
1483
    returnToUser(pRequest);
859✔
1484
  }
1485
}
1486

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

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

1495
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
565,263✔
1496
                                &pWrapper->pRequest->body.queryJob);
282,664✔
1497
}
1498

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

1501
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
8,206,770✔
1502
  int32_t code = TSDB_CODE_SUCCESS;
8,206,770✔
1503
  switch (pWrapper->pRequest->pQuery->execStage) {
8,206,770!
1504
    case QUERY_EXEC_STAGE_PARSE: {
32,984✔
1505
      // continue parse after get metadata
1506
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
32,984✔
1507
      break;
32,985✔
1508
    }
1509
    case QUERY_EXEC_STAGE_ANALYSE: {
249,657✔
1510
      // analysis after get metadata
1511
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
249,657✔
1512
      break;
249,646✔
1513
    }
1514
    case QUERY_EXEC_STAGE_SCHEDULE: {
7,941,034✔
1515
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
7,941,034✔
1516
      break;
7,947,851✔
1517
    }
1518
    default:
×
1519
      break;
×
1520
  }
1521
  return code;
8,213,577✔
1522
}
1523

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

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

1533
  if (code == TSDB_CODE_SUCCESS) {
32,986✔
1534
    // pWrapper->pCatalogReq->forceUpdate = false;
1535
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
32,579✔
1536
  }
1537

1538
  if (TSDB_CODE_SUCCESS == code) {
32,980✔
1539
    code = phaseAsyncQuery(pWrapper);
32,519✔
1540
  }
1541

1542
  if (TSDB_CODE_SUCCESS != code) {
32,981✔
1543
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
462!
1544
             tstrerror(code), pWrapper->pRequest->requestId);
1545
    destorySqlCallbackWrapper(pWrapper);
462✔
1546
    pRequest->pWrapper = NULL;
462✔
1547
    terrno = code;
462✔
1548
    pRequest->code = code;
462✔
1549
    doRequestCallback(pRequest, code);
462✔
1550
  }
1551
}
32,981✔
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) {
9✔
1571
  int64_t connId = *(int64_t *)taos;
9✔
1572
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
9✔
1573
}
9✔
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,232,001✔
1581
  const STscObj *pTscObj = pRequest->pTscObj;
8,232,001✔
1582

1583
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
8,232,001!
1584
  if (*pCxt == NULL) {
8,238,931!
1585
    return terrno;
×
1586
  }
1587

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

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

1629
  if (TSDB_CODE_SUCCESS == code) {
8,245,541!
1630
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
8,247,007✔
1631
  }
1632

1633
  if (TSDB_CODE_SUCCESS == code) {
8,208,875!
1634
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
8,210,630✔
1635
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
8,254,346✔
1636
  }
1637

1638
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
8,255,481!
1639
    int64_t syntaxStart = taosGetTimestampUs();
8,226,891✔
1640

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

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

1653
  return code;
8,202,647✔
1654
}
1655

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

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

1669
  if (TSDB_CODE_SUCCESS == code) {
8,231,109!
1670
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
8,237,118✔
1671
  }
1672

1673
  if (TSDB_CODE_SUCCESS == code) {
8,177,201✔
1674
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
8,191,065✔
1675
    code = phaseAsyncQuery(pWrapper);
8,191,065✔
1676
  }
1677

1678
  if (TSDB_CODE_SUCCESS != code) {
8,184,767✔
1679
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
289!
1680
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
289!
1681
               pRequest->requestId);
1682
    } else {
1683
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
×
1684
               pRequest->requestId);
1685
    }
1686

1687
    destorySqlCallbackWrapper(pWrapper);
289✔
1688
    pRequest->pWrapper = NULL;
289✔
1689
    qDestroyQuery(pRequest->pQuery);
289✔
1690
    pRequest->pQuery = NULL;
289✔
1691

1692
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
289!
1693
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
×
1694
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1695
      code = refreshMeta(pRequest->pTscObj, pRequest);
×
1696
      if (code != 0) {
×
1697
        tscWarn("req:0x%" PRIx64 ", refresh meta failed, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
×
1698
                pRequest->requestId);
1699
      }
1700
      pRequest->prevCode = code;
×
1701
      doAsyncQuery(pRequest, true);
×
1702
      return;
×
1703
    }
1704

1705
    terrno = code;
289✔
1706
    pRequest->code = code;
289✔
1707
    doRequestCallback(pRequest, code);
289✔
1708
  }
1709
}
1710

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

1743
typedef struct SAsyncFetchParam {
1744
  SRequestObj      *pReq;
1745
  __taos_async_fn_t fp;
1746
  void             *param;
1747
} SAsyncFetchParam;
1748

1749
static int32_t doAsyncFetch(void *pParam) {
156,681✔
1750
  SAsyncFetchParam *param = pParam;
156,681✔
1751
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
156,681✔
1752
  taosMemoryFree(param);
156,675!
1753
  return TSDB_CODE_SUCCESS;
156,676✔
1754
}
1755

1756
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
156,676✔
1757
  if (res == NULL || fp == NULL) {
156,676!
1758
    tscError("taos_fetch_rows_a invalid paras");
×
1759
    return;
×
1760
  }
1761
  if (!TD_RES_QUERY(res)) {
156,690!
1762
    tscError("taos_fetch_rows_a res is NULL");
×
1763
    fp(param, res, TSDB_CODE_APP_ERROR);
×
1764
    return;
×
1765
  }
1766

1767
  SRequestObj *pRequest = res;
156,690✔
1768
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
156,690✔
1769
    fp(param, res, 0);
10✔
1770
    return;
10✔
1771
  }
1772

1773
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
156,680!
1774
  if (!pParam) {
156,680!
1775
    fp(param, res, terrno);
×
1776
    return;
×
1777
  }
1778
  pParam->pReq = pRequest;
156,680✔
1779
  pParam->fp = fp;
156,680✔
1780
  pParam->param = param;
156,680✔
1781
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
156,680✔
1782
  if (TSDB_CODE_SUCCESS != code) {
156,681!
1783
    taosMemoryFree(pParam);
×
1784
    fp(param, res, code);
×
1785
    return;
×
1786
  }
1787
}
1788

1789
void taos_fetch_raw_block_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
8✔
1790
  if (res == NULL || fp == NULL) {
8!
1791
    tscError("taos_fetch_raw_block_a invalid paras");
×
1792
    return;
×
1793
  }
1794
  if (!TD_RES_QUERY(res)) {
8!
1795
    tscError("taos_fetch_raw_block_a res is NULL");
×
1796
    return;
×
1797
  }
1798
  SRequestObj    *pRequest = res;
8✔
1799
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
8✔
1800

1801
  // set the current block is all consumed
1802
  pResultInfo->convertUcs4 = false;
8✔
1803

1804
  // it is a local executed query, no need to do async fetch
1805
  taos_fetch_rows_a(pRequest, fp, param);
8✔
1806
}
1807

1808
const void *taos_get_raw_block(TAOS_RES *res) {
×
1809
  if (res == NULL) {
×
1810
    tscError("taos_get_raw_block invalid paras");
×
1811
    return NULL;
×
1812
  }
1813
  if (!TD_RES_QUERY(res)) {
×
1814
    tscError("taos_get_raw_block res is NULL");
×
1815
    return NULL;
×
1816
  }
1817
  SRequestObj *pRequest = res;
×
1818

1819
  return pRequest->body.resInfo.pData;
×
1820
}
1821

1822
int taos_get_db_route_info(TAOS *taos, const char *db, TAOS_DB_ROUTE_INFO *dbInfo) {
×
1823
  if (NULL == taos) {
×
1824
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1825
    return terrno;
×
1826
  }
1827

1828
  if (NULL == db || NULL == dbInfo) {
×
1829
    tscError("invalid input param, db:%p, dbInfo:%p", db, dbInfo);
×
1830
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1831
    return terrno;
×
1832
  }
1833

1834
  int64_t      connId = *(int64_t *)taos;
×
1835
  SRequestObj *pRequest = NULL;
×
1836
  char        *sql = "taos_get_db_route_info";
×
1837
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1838
  if (code != TSDB_CODE_SUCCESS) {
×
1839
    terrno = code;
×
1840
    return terrno;
×
1841
  }
1842

1843
  STscObj  *pTscObj = pRequest->pTscObj;
×
1844
  SCatalog *pCtg = NULL;
×
1845
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1846
  if (code != TSDB_CODE_SUCCESS) {
×
1847
    goto _return;
×
1848
  }
1849

1850
  SRequestConnInfo conn = {
×
1851
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1852

1853
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1854

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

1858
  code = catalogGetDBVgInfo(pCtg, &conn, dbFName, dbInfo);
×
1859
  if (code) {
×
1860
    goto _return;
×
1861
  }
1862

1863
_return:
×
1864

1865
  terrno = code;
×
1866

1867
  destroyRequest(pRequest);
×
1868
  return code;
×
1869
}
1870

1871
int taos_get_table_vgId(TAOS *taos, const char *db, const char *table, int *vgId) {
×
1872
  if (NULL == taos) {
×
1873
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1874
    return terrno;
×
1875
  }
1876

1877
  if (NULL == db || NULL == table || NULL == vgId) {
×
1878
    tscError("invalid input param, db:%p, table:%p, vgId:%p", db, table, vgId);
×
1879
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1880
    return terrno;
×
1881
  }
1882

1883
  int64_t      connId = *(int64_t *)taos;
×
1884
  SRequestObj *pRequest = NULL;
×
1885
  char        *sql = "taos_get_table_vgId";
×
1886
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1887
  if (code != TSDB_CODE_SUCCESS) {
×
1888
    return terrno;
×
1889
  }
1890

1891
  pRequest->syncQuery = true;
×
1892

1893
  STscObj  *pTscObj = pRequest->pTscObj;
×
1894
  SCatalog *pCtg = NULL;
×
1895
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1896
  if (code != TSDB_CODE_SUCCESS) {
×
1897
    goto _return;
×
1898
  }
1899

1900
  SRequestConnInfo conn = {
×
1901
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1902

1903
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1904

1905
  SName tableName = {0};
×
1906
  toName(pTscObj->acctId, db, table, &tableName);
×
1907

1908
  SVgroupInfo vgInfo;
1909
  code = catalogGetTableHashVgroup(pCtg, &conn, &tableName, &vgInfo);
×
1910
  if (code) {
×
1911
    goto _return;
×
1912
  }
1913

1914
  *vgId = vgInfo.vgId;
×
1915

1916
_return:
×
1917

1918
  terrno = code;
×
1919

1920
  destroyRequest(pRequest);
×
1921
  return code;
×
1922
}
1923

1924
int taos_get_tables_vgId(TAOS *taos, const char *db, const char *table[], int tableNum, int *vgId) {
×
1925
  if (NULL == taos) {
×
1926
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1927
    return terrno;
×
1928
  }
1929

1930
  if (NULL == db || NULL == table || NULL == vgId || tableNum <= 0) {
×
1931
    tscError("invalid input param, db:%p, table:%p, vgId:%p, tbNum:%d", db, table, vgId, tableNum);
×
1932
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1933
    return terrno;
×
1934
  }
1935

1936
  int64_t      connId = *(int64_t *)taos;
×
1937
  SRequestObj *pRequest = NULL;
×
1938
  char        *sql = "taos_get_table_vgId";
×
1939
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1940
  if (code != TSDB_CODE_SUCCESS) {
×
1941
    return terrno;
×
1942
  }
1943

1944
  pRequest->syncQuery = true;
×
1945

1946
  STscObj  *pTscObj = pRequest->pTscObj;
×
1947
  SCatalog *pCtg = NULL;
×
1948
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1949
  if (code != TSDB_CODE_SUCCESS) {
×
1950
    goto _return;
×
1951
  }
1952

1953
  SRequestConnInfo conn = {
×
1954
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1955

1956
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1957

1958
  code = catalogGetTablesHashVgId(pCtg, &conn, pTscObj->acctId, db, table, tableNum, vgId);
×
1959
  if (code) {
×
1960
    goto _return;
×
1961
  }
1962

1963
_return:
×
1964

1965
  terrno = code;
×
1966

1967
  destroyRequest(pRequest);
×
1968
  return code;
×
1969
}
1970

1971
int taos_load_table_info(TAOS *taos, const char *tableNameList) {
×
1972
  if (NULL == taos) {
×
1973
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1974
    return terrno;
×
1975
  }
1976

1977
  int64_t       connId = *(int64_t *)taos;
×
1978
  const int32_t MAX_TABLE_NAME_LENGTH = 12 * 1024 * 1024;  // 12MB list
×
1979
  int32_t       code = 0;
×
1980
  SRequestObj  *pRequest = NULL;
×
1981
  SCatalogReq   catalogReq = {0};
×
1982

1983
  if (NULL == tableNameList) {
×
1984
    return TSDB_CODE_SUCCESS;
×
1985
  }
1986

1987
  int32_t length = (int32_t)strlen(tableNameList);
×
1988
  if (0 == length) {
×
1989
    return TSDB_CODE_SUCCESS;
×
1990
  } else if (length > MAX_TABLE_NAME_LENGTH) {
×
1991
    tscError("tableNameList too long, length:%d, maximum allowed:%d", length, MAX_TABLE_NAME_LENGTH);
×
1992
    return TSDB_CODE_TSC_INVALID_OPERATION;
×
1993
  }
1994

1995
  char *sql = "taos_load_table_info";
×
1996
  code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1997
  if (code != TSDB_CODE_SUCCESS) {
×
1998
    terrno = code;
×
1999
    goto _return;
×
2000
  }
2001

2002
  pRequest->syncQuery = true;
×
2003

2004
  STscObj *pTscObj = pRequest->pTscObj;
×
2005
  code = transferTableNameList(tableNameList, pTscObj->acctId, pTscObj->db, &catalogReq.pTableMeta);
×
2006
  if (code) {
×
2007
    goto _return;
×
2008
  }
2009

2010
  SCatalog *pCtg = NULL;
×
2011
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
2012
  if (code != TSDB_CODE_SUCCESS) {
×
2013
    goto _return;
×
2014
  }
2015

2016
  SRequestConnInfo conn = {
×
2017
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
2018

2019
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
2020

2021
  code = catalogAsyncGetAllMeta(pCtg, &conn, &catalogReq, syncCatalogFn, pRequest->body.interParam, NULL);
×
2022
  if (code) {
×
2023
    goto _return;
×
2024
  }
2025

2026
  SSyncQueryParam *pParam = pRequest->body.interParam;
×
2027
  code = tsem_wait(&pParam->sem);
×
2028
  if (code) {
×
2029
    tscError("tsem wait failed, code:%d - %s", code, tstrerror(code));
×
2030
    goto _return;
×
2031
  }
2032
_return:
×
2033
  destoryCatalogReq(&catalogReq);
×
2034
  destroyRequest(pRequest);
×
2035
  return code;
×
2036
}
2037

2038
TAOS_STMT *taos_stmt_init(TAOS *taos) {
2,016✔
2039
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2,016✔
2040
  if (NULL == pObj) {
2,017!
2041
    tscError("invalid parameter for %s", __FUNCTION__);
×
2042
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2043
    return NULL;
×
2044
  }
2045

2046
  TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL);
2,017✔
2047
  if (NULL == pStmt) {
2,014!
2048
    tscError("stmt init failed, errcode:%s", terrstr());
×
2049
  }
2050
  releaseTscObj(*(int64_t *)taos);
2,014✔
2051

2052
  return pStmt;
2,016✔
2053
}
2054

2055
TAOS_STMT *taos_stmt_init_with_reqid(TAOS *taos, int64_t reqid) {
×
2056
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
2057
  if (NULL == pObj) {
×
2058
    tscError("invalid parameter for %s", __FUNCTION__);
×
2059
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2060
    return NULL;
×
2061
  }
2062

2063
  TAOS_STMT *pStmt = stmtInit(pObj, reqid, NULL);
×
2064
  if (NULL == pStmt) {
×
2065
    tscError("stmt init failed, errcode:%s", terrstr());
×
2066
  }
2067
  releaseTscObj(*(int64_t *)taos);
×
2068

2069
  return pStmt;
×
2070
}
2071

2072
TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options) {
96✔
2073
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
96✔
2074
  if (NULL == pObj) {
96!
2075
    tscError("invalid parameter for %s", __FUNCTION__);
×
2076
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2077
    return NULL;
×
2078
  }
2079

2080
  TAOS_STMT *pStmt = stmtInit(pObj, options->reqId, options);
96✔
2081
  if (NULL == pStmt) {
96!
2082
    tscError("stmt init failed, errcode:%s", terrstr());
×
2083
  }
2084
  releaseTscObj(*(int64_t *)taos);
96✔
2085

2086
  return pStmt;
96✔
2087
}
2088

2089
int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
12,099✔
2090
  if (stmt == NULL || sql == NULL) {
12,099!
2091
    tscError("NULL parameter for %s", __FUNCTION__);
×
2092
    terrno = TSDB_CODE_INVALID_PARA;
×
2093
    return terrno;
×
2094
  }
2095

2096
  return stmtPrepare(stmt, sql, length);
12,111✔
2097
}
2098

2099
int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags) {
14✔
2100
  if (stmt == NULL || name == NULL) {
14!
2101
    tscError("NULL parameter for %s", __FUNCTION__);
×
2102
    terrno = TSDB_CODE_INVALID_PARA;
×
2103
    return terrno;
×
2104
  }
2105

2106
  int32_t code = stmtSetTbName(stmt, name);
14✔
2107
  if (code) {
14✔
2108
    return code;
7✔
2109
  }
2110

2111
  if (tags) {
7!
2112
    return stmtSetTbTags(stmt, tags);
7✔
2113
  }
2114

2115
  return TSDB_CODE_SUCCESS;
×
2116
}
2117

2118
int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name) {
15,674✔
2119
  if (stmt == NULL || name == NULL) {
15,674!
2120
    tscError("NULL parameter for %s", __FUNCTION__);
×
2121
    terrno = TSDB_CODE_INVALID_PARA;
×
2122
    return terrno;
×
2123
  }
2124

2125
  return stmtSetTbName(stmt, name);
15,675✔
2126
}
2127

2128
int taos_stmt_set_tags(TAOS_STMT *stmt, TAOS_MULTI_BIND *tags) {
8✔
2129
  if (stmt == NULL || tags == NULL) {
8!
2130
    tscError("NULL parameter for %s", __FUNCTION__);
×
2131
    terrno = TSDB_CODE_INVALID_PARA;
×
2132
    return terrno;
×
2133
  }
2134

2135
  return stmtSetTbTags(stmt, tags);
8✔
2136
}
2137

2138
int taos_stmt_set_sub_tbname(TAOS_STMT *stmt, const char *name) { return taos_stmt_set_tbname(stmt, name); }
4✔
2139

2140
int taos_stmt_get_tag_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
6✔
2141
  if (stmt == NULL || NULL == fieldNum) {
6!
2142
    tscError("NULL parameter for %s", __FUNCTION__);
×
2143
    terrno = TSDB_CODE_INVALID_PARA;
×
2144
    return terrno;
×
2145
  }
2146

2147
  return stmtGetTagFields(stmt, fieldNum, fields);
6✔
2148
}
2149

2150
int taos_stmt_get_col_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
7✔
2151
  if (stmt == NULL || NULL == fieldNum) {
7!
2152
    tscError("NULL parameter for %s", __FUNCTION__);
×
2153
    terrno = TSDB_CODE_INVALID_PARA;
×
2154
    return terrno;
×
2155
  }
2156

2157
  return stmtGetColFields(stmt, fieldNum, fields);
7✔
2158
}
2159

2160
// let stmt to reclaim TAOS_FIELD_E that was allocated by `taos_stmt_get_tag_fields`/`taos_stmt_get_col_fields`
2161
void taos_stmt_reclaim_fields(TAOS_STMT *stmt, TAOS_FIELD_E *fields) {
×
2162
  (void)stmt;
2163
  if (!fields) return;
×
2164
  taosMemoryFree(fields);
×
2165
}
2166

2167
int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
178✔
2168
  if (stmt == NULL || bind == NULL) {
178!
2169
    tscError("NULL parameter for %s", __FUNCTION__);
×
2170
    terrno = TSDB_CODE_INVALID_PARA;
×
2171
    return terrno;
×
2172
  }
2173

2174
  if (bind->num > 1) {
178!
2175
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
×
2176
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2177
    return terrno;
×
2178
  }
2179

2180
  return stmtBindBatch(stmt, bind, -1);
178✔
2181
}
2182

2183
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
407,191✔
2184
  if (stmt == NULL || bind == NULL) {
407,191!
2185
    tscError("NULL parameter for %s", __FUNCTION__);
×
2186
    terrno = TSDB_CODE_INVALID_PARA;
×
2187
    return terrno;
×
2188
  }
2189

2190
  if (bind->num <= 0 || bind->num > INT16_MAX) {
407,513!
2191
    tscError("invalid bind num %d", bind->num);
×
2192
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2193
    return terrno;
×
2194
  }
2195

2196
  int32_t insert = 0;
407,593✔
2197
  int32_t code = stmtIsInsert(stmt, &insert);
407,593✔
2198
  if (TSDB_CODE_SUCCESS != code) {
407,363!
2199
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2200
    return code;
×
2201
  }
2202
  if (0 == insert && bind->num > 1) {
407,363!
2203
    tscError("only one row data allowed for query");
×
2204
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2205
    return terrno;
×
2206
  }
2207

2208
  return stmtBindBatch(stmt, bind, -1);
407,363✔
2209
}
2210

2211
int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind, int colIdx) {
×
2212
  if (stmt == NULL || bind == NULL) {
×
2213
    tscError("NULL parameter for %s", __FUNCTION__);
×
2214
    terrno = TSDB_CODE_INVALID_PARA;
×
2215
    return terrno;
×
2216
  }
2217

2218
  if (colIdx < 0) {
×
2219
    tscError("invalid bind column idx %d", colIdx);
×
2220
    terrno = TSDB_CODE_INVALID_PARA;
×
2221
    return terrno;
×
2222
  }
2223

2224
  int32_t insert = 0;
×
2225
  int32_t code = stmtIsInsert(stmt, &insert);
×
2226
  if (TSDB_CODE_SUCCESS != code) {
×
2227
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2228
    return code;
×
2229
  }
2230
  if (0 == insert && bind->num > 1) {
×
2231
    tscError("only one row data allowed for query");
×
2232
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2233
    return terrno;
×
2234
  }
2235

2236
  return stmtBindBatch(stmt, bind, colIdx);
×
2237
}
2238

2239
int taos_stmt_add_batch(TAOS_STMT *stmt) {
387,559✔
2240
  if (stmt == NULL) {
387,559!
2241
    tscError("NULL parameter for %s", __FUNCTION__);
×
2242
    terrno = TSDB_CODE_INVALID_PARA;
×
2243
    return terrno;
×
2244
  }
2245

2246
  return stmtAddBatch(stmt);
387,559✔
2247
}
2248

2249
int taos_stmt_execute(TAOS_STMT *stmt) {
16,549✔
2250
  if (stmt == NULL) {
16,549!
2251
    tscError("NULL parameter for %s", __FUNCTION__);
×
2252
    terrno = TSDB_CODE_INVALID_PARA;
×
2253
    return terrno;
×
2254
  }
2255

2256
  return stmtExec(stmt);
16,549✔
2257
}
2258

2259
int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
×
2260
  if (stmt == NULL || insert == NULL) {
×
2261
    tscError("NULL parameter for %s", __FUNCTION__);
×
2262
    terrno = TSDB_CODE_INVALID_PARA;
×
2263
    return terrno;
×
2264
  }
2265

2266
  return stmtIsInsert(stmt, insert);
×
2267
}
2268

2269
int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) {
×
2270
  if (stmt == NULL || nums == NULL) {
×
2271
    tscError("NULL parameter for %s", __FUNCTION__);
×
2272
    terrno = TSDB_CODE_INVALID_PARA;
×
2273
    return terrno;
×
2274
  }
2275

2276
  return stmtGetParamNum(stmt, nums);
×
2277
}
2278

2279
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
4✔
2280
  if (stmt == NULL || type == NULL || NULL == bytes || idx < 0) {
4!
2281
    tscError("invalid parameter for %s", __FUNCTION__);
×
2282
    terrno = TSDB_CODE_INVALID_PARA;
×
2283
    return terrno;
×
2284
  }
2285

2286
  return stmtGetParam(stmt, idx, type, bytes);
4✔
2287
}
2288

2289
TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt) {
2✔
2290
  if (stmt == NULL) {
2!
2291
    tscError("NULL parameter for %s", __FUNCTION__);
×
2292
    terrno = TSDB_CODE_INVALID_PARA;
×
2293
    return NULL;
×
2294
  }
2295

2296
  return stmtUseResult(stmt);
2✔
2297
}
2298

2299
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
189✔
2300

2301
int taos_stmt_affected_rows(TAOS_STMT *stmt) {
×
2302
  if (stmt == NULL) {
×
2303
    tscError("NULL parameter for %s", __FUNCTION__);
×
2304
    terrno = TSDB_CODE_INVALID_PARA;
×
2305
    return 0;
×
2306
  }
2307

2308
  return stmtAffectedRows(stmt);
×
2309
}
2310

2311
int taos_stmt_affected_rows_once(TAOS_STMT *stmt) {
56✔
2312
  if (stmt == NULL) {
56!
2313
    tscError("NULL parameter for %s", __FUNCTION__);
×
2314
    terrno = TSDB_CODE_INVALID_PARA;
×
2315
    return 0;
×
2316
  }
2317

2318
  return stmtAffectedRowsOnce(stmt);
56✔
2319
}
2320

2321
int taos_stmt_close(TAOS_STMT *stmt) {
2,096✔
2322
  if (stmt == NULL) {
2,096!
2323
    tscError("NULL parameter for %s", __FUNCTION__);
×
2324
    terrno = TSDB_CODE_INVALID_PARA;
×
2325
    return terrno;
×
2326
  }
2327

2328
  return stmtClose(stmt);
2,096✔
2329
}
2330

2331
TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option) {
195✔
2332
  if (NULL == taos) {
195✔
2333
    tscError("NULL parameter for %s", __FUNCTION__);
1!
2334
    terrno = TSDB_CODE_INVALID_PARA;
1✔
2335
    return NULL;
1✔
2336
  }
2337
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
194✔
2338
  if (NULL == pObj) {
194!
2339
    tscError("invalid parameter for %s", __FUNCTION__);
×
2340
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2341
    return NULL;
×
2342
  }
2343

2344
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
194✔
2345

2346
  releaseTscObj(*(int64_t *)taos);
194✔
2347

2348
  return pStmt;
194✔
2349
}
2350

2351
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
218✔
2352
  if (stmt == NULL || sql == NULL) {
218!
2353
    tscError("NULL parameter for %s", __FUNCTION__);
1!
2354
    terrno = TSDB_CODE_INVALID_PARA;
1✔
2355
    return terrno;
1✔
2356
  }
2357

2358
  return stmtPrepare2(stmt, sql, length);
217✔
2359
}
2360

2361
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
4,615✔
2362
  if (stmt == NULL) {
4,615!
2363
    tscError("NULL parameter for %s", __FUNCTION__);
×
2364
    terrno = TSDB_CODE_INVALID_PARA;
×
2365
    return terrno;
×
2366
  }
2367

2368
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
4,615✔
2369
  STMT2_DLOG_E("start to bind param");
4,615!
2370
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 1) {
4,615!
2371
    STMT2_ELOG_E("async bind param is still working, please try again later");
×
2372
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
×
2373
    return terrno;
×
2374
  }
2375

2376
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
4,616✔
2377
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
18!
2378
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
2379
    }
2380
    pStmt->execSemWaited = true;
18✔
2381
  }
2382

2383
  int32_t code = TSDB_CODE_SUCCESS;
4,616✔
2384
  for (int i = 0; i < bindv->count; ++i) {
12,275✔
2385
    if (bindv->tbnames && bindv->tbnames[i]) {
7,672!
2386
      code = stmtSetTbName2(stmt, bindv->tbnames[i]);
7,652✔
2387
      if (code) {
7,649✔
2388
        terrno = code;
5✔
2389
        STMT2_ELOG("set tbname failed, code:%s", tstrerror(code));
5!
2390
        return terrno;
13✔
2391
      }
2392
    }
2393

2394
    SVCreateTbReq *pCreateTbReq = NULL;
7,671✔
2395
    if (bindv->tags && bindv->tags[i]) {
7,671!
2396
      code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
182✔
2397
    } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
7,489✔
2398
      code = stmtCheckTags2(stmt, &pCreateTbReq);
23✔
2399
    } else if (pStmt->sql.autoCreateTbl) {
7,466✔
2400
      // if (pStmt->sql.autoCreateTbl) {
2401
      //   pStmt->sql.autoCreateTbl = false;
2402
      //   STMT2_WLOG_E("sql is autoCreateTbl, but no tags");
2403
      // }
2404
      code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
21✔
2405
    }
2406

2407
    if (code) {
7,670✔
2408
      terrno = code;
1✔
2409
      STMT2_ELOG("set tags failed, code:%s", tstrerror(code));
1!
2410
      return terrno;
1✔
2411
    }
2412

2413
    if (bindv->bind_cols && bindv->bind_cols[i]) {
7,669!
2414
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
7,671✔
2415

2416
      if (bind->num <= 0 || bind->num > INT16_MAX) {
7,671!
2417
        STMT2_ELOG("bind num:%d must > 0 and < INT16_MAX", bind->num);
×
2418
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2419
        return terrno;
×
2420
      }
2421

2422
      if (!stmt2IsInsert(stmt) && bind->num > 1) {
7,671✔
2423
        STMT2_ELOG_E("only one row data allowed for query");
1!
2424
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
1✔
2425
        return terrno;
1✔
2426
      }
2427

2428
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
7,670✔
2429
      if (TSDB_CODE_SUCCESS != code) {
7,664✔
2430
        terrno = code;
6✔
2431
        STMT2_ELOG("bind batch failed, code:%s", tstrerror(code));
6!
2432
        return terrno;
6✔
2433
      }
2434
    }
2435
  }
2436

2437
  return code;
4,603✔
2438
}
2439

2440
int taos_stmt2_bind_param_a(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx, __taos_async_fn_t fp,
×
2441
                            void *param) {
2442
  if (stmt == NULL || bindv == NULL || fp == NULL) {
×
2443
    terrno = TSDB_CODE_INVALID_PARA;
×
2444
    return terrno;
×
2445
  }
2446

2447
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2448

2449
  ThreadArgs *args = (ThreadArgs *)taosMemoryMalloc(sizeof(ThreadArgs));
×
2450
  args->stmt = stmt;
×
2451
  args->bindv = bindv;
×
2452
  args->col_idx = col_idx;
×
2453
  args->fp = fp;
×
2454
  args->param = param;
×
2455

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

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

2476
  return code_s;
×
2477
}
2478

2479
int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows) {
4,588✔
2480
  if (stmt == NULL) {
4,588!
2481
    tscError("NULL parameter for %s", __FUNCTION__);
×
2482
    terrno = TSDB_CODE_INVALID_PARA;
×
2483
    return terrno;
×
2484
  }
2485

2486
  return stmtExec2(stmt, affected_rows);
4,588✔
2487
}
2488

2489
int taos_stmt2_close(TAOS_STMT2 *stmt) {
191✔
2490
  if (stmt == NULL) {
191!
2491
    tscError("NULL parameter for %s", __FUNCTION__);
×
2492
    terrno = TSDB_CODE_INVALID_PARA;
×
2493
    return terrno;
×
2494
  }
2495

2496
  return stmtClose2(stmt);
191✔
2497
}
2498

2499
int taos_stmt2_is_insert(TAOS_STMT2 *stmt, int *insert) {
×
2500
  if (stmt == NULL || insert == NULL) {
×
2501
    tscError("NULL parameter for %s", __FUNCTION__);
×
2502
    terrno = TSDB_CODE_INVALID_PARA;
×
2503
    return terrno;
×
2504
  }
2505
  *insert = stmt2IsInsert(stmt);
×
2506
  return TSDB_CODE_SUCCESS;
×
2507
}
2508

2509
int taos_stmt2_get_fields(TAOS_STMT2 *stmt, int *count, TAOS_FIELD_ALL **fields) {
81✔
2510
  if (stmt == NULL || count == NULL) {
81!
2511
    tscError("NULL parameter for %s", __FUNCTION__);
1!
2512
    terrno = TSDB_CODE_INVALID_PARA;
1✔
2513
    return terrno;
1✔
2514
  }
2515

2516
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
80✔
2517
  if (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type ||
80!
2518
      (pStmt->sql.type == 0 && stmt2IsInsert(stmt))) {
73✔
2519
    return stmtGetStbColFields2(stmt, count, fields);
69✔
2520
  }
2521
  if (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt))) {
11!
2522
    return stmtGetParamNum2(stmt, count);
10✔
2523
  }
2524

2525
  tscError("Invalid sql for stmt %s", pStmt->sql.sqlStr);
1!
2526
  return TSDB_CODE_PAR_SYNTAX_ERROR;
1✔
2527
}
2528

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

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

2542
  return stmtUseResult2(stmt);
8✔
2543
}
2544

2545
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmtErrstr2(stmt); }
25✔
2546

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

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

2570
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