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

taosdata / TDengine / #4548

22 Jul 2025 02:37AM UTC coverage: 54.273% (-3.0%) from 57.287%
#4548

push

travis-ci

GitHub
Merge pull request #32061 from taosdata/new_testcases

132738 of 315239 branches covered (42.11%)

Branch coverage included in aggregate %.

201371 of 300373 relevant lines covered (67.04%)

3475977.14 hits per line

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

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

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

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

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

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

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

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

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

124
END:
10✔
125
  return tz;
17✔
126
}
127
#endif
128

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

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

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

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

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

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

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

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

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

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

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

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

241
  monitorClose();
2,379✔
242
  tscStopCrashReport();
2,379✔
243

244
  hbMgrCleanUp();
2,379✔
245

246
  catalogDestroy();
2,379✔
247
  schedulerDestroy();
2,379✔
248

249
  fmFuncMgtDestroy();
2,379✔
250
  qCleanupKeywordsTable();
2,379✔
251

252
  if (TSDB_CODE_SUCCESS != cleanupTaskQueue()) {
2,379!
253
    tscWarn("failed to cleanup task queue");
×
254
  }
255

256
#if !defined(WINDOWS) && !defined(TD_ASTRA)
257
  tzCleanup();
2,379✔
258
#endif
259
  tmqMgmtClose();
2,379✔
260

261
  int32_t id = clientReqRefPool;
2,379✔
262
  clientReqRefPool = -1;
2,379✔
263
  taosCloseRef(id);
2,379✔
264

265
  id = clientConnRefPool;
2,379✔
266
  clientConnRefPool = -1;
2,379✔
267
  taosCloseRef(id);
2,379✔
268

269
  nodesDestroyAllocatorSet();
2,379✔
270
  cleanupAppInfo();
2,379✔
271
  rpcCleanup();
2,379✔
272
  tscDebug("rpc cleanup");
2,379✔
273

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

305
  if (pass == NULL) {
8,983✔
306
    pass = TSDB_DEFAULT_PASS;
2,080✔
307
  }
308

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

323
  return NULL;
81✔
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) {
9,085✔
635
  if (taos == NULL) {
9,085!
636
    return;
×
637
  }
638

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

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

647
void taos_close(TAOS *taos) {
9,526✔
648
  if (taos == NULL) {
9,526✔
649
    return;
626✔
650
  }
651

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

658
  taos_close_internal(pObj);
8,900✔
659
  releaseTscObj(*(int64_t *)taos);
8,900✔
660
  taosMemoryFree(taos);
8,900!
661
}
662

663
int taos_errno(TAOS_RES *res) {
610,933✔
664
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
610,933!
665
    return terrno;
39✔
666
  }
667

668
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
610,894!
669
    return 0;
×
670
  }
671

672
  return ((SRequestObj *)res)->code;
610,894✔
673
}
674

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

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

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

692
void taos_free_result(TAOS_RES *res) {
603,323✔
693
  if (NULL == res) {
603,323✔
694
    return;
1,277✔
695
  }
696

697
  tscTrace("res:%p, will be freed", res);
602,046!
698

699
  if (TD_RES_QUERY(res)) {
602,062✔
700
    SRequestObj *pRequest = (SRequestObj *)res;
601,934✔
701
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
601,934✔
702
    destroyRequest(pRequest);
601,934✔
703
    return;
601,928✔
704
  }
705

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

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

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

736
int taos_field_count(TAOS_RES *res) {
3,639,012✔
737
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
3,639,012!
738
    return 0;
×
739
  }
740

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

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

747
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
346,801✔
748
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
346,801!
749
    return NULL;
1,312✔
750
  }
751

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

756
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
600,021✔
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) {
2,740,721✔
770
  if (res == NULL) {
2,740,721!
771
    return NULL;
×
772
  }
773

774
  if (TD_RES_QUERY(res)) {
2,740,721✔
775
    SRequestObj *pRequest = (SRequestObj *)res;
2,735,434✔
776
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
2,735,434✔
777
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0 || pRequest->killed) {
2,735,267!
778
      return NULL;
169✔
779
    }
780

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

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

799
    if (pResultInfo->current < pResultInfo->numOfRows) {
5,287✔
800
      doSetOneRowPtr(pResultInfo);
1,964✔
801
      pResultInfo->current += 1;
1,963✔
802
      return pResultInfo->row;
1,963✔
803
    } else {
804
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
3,323✔
805
        return NULL;
128✔
806
      }
807

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

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

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

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

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

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

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

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

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

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

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

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

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

881
      case TSDB_DATA_TYPE_VARBINARY: {
2,202✔
882
        void    *data = NULL;
2,202✔
883
        uint32_t tmp = 0;
2,202✔
884
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
2,202✔
885
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
2,202!
886
          break;
×
887
        }
888
        uint32_t copyLen = TMIN(size - len - 1, tmp);
2,202✔
889
        (void)memcpy(str + len, data, copyLen);
2,202✔
890
        len += copyLen;
2,202✔
891
        taosMemoryFree(data);
2,202!
892
      } break;
2,202✔
893
      case TSDB_DATA_TYPE_BINARY:
24,039✔
894
      case TSDB_DATA_TYPE_NCHAR:
895
      case TSDB_DATA_TYPE_GEOMETRY: {
896
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
24,039✔
897
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
24,039!
898
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
4,400✔
899
          if (charLen > fields[i].bytes || charLen < 0) {
21,839!
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);
1!
906
            break;
×
907
          }
908
        }
909

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

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

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

930
      case TSDB_DATA_TYPE_TIMESTAMP:
12,433✔
931
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
12,433✔
932
        break;
12,435✔
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,199✔
937
      case TSDB_DATA_TYPE_DECIMAL64:
×
938
      case TSDB_DATA_TYPE_DECIMAL: {
939
        uint32_t decimalLen = strlen(row[i]);
×
940
        uint32_t copyLen = TMIN(size - len - 1, decimalLen);
×
941
        (void)memcpy(str + len, row[i], copyLen);
×
942
        len += copyLen;
×
943
      } break;
×
944
      default:
×
945
        break;
×
946
    }
947

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

956
  return len;
14,264✔
957
}
958

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

964
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
619,165✔
965
  return pResInfo->length;
619,165✔
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; }
1,502✔
1034

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

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

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

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

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

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

1074
int taos_select_db(TAOS *taos, const char *db) {
391✔
1075
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
391✔
1076
  if (pObj == NULL) {
392!
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) {
392!
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};
392✔
1090
  (void)snprintf(sql, tListLen(sql), "use %s", db);
392✔
1091

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

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

1100
void taos_stop_query(TAOS_RES *res) {
612,297✔
1101
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
612,297!
1102
      TD_RES_TMQ_BATCH_META(res)) {
612,321!
1103
    return;
×
1104
  }
1105

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

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

1139
  if (TD_RES_QUERY(res)) {
×
1140
    SRequestObj *pRequest = (SRequestObj *)res;
×
1141

1142
    (*rows) = NULL;
×
1143
    (*numOfRows) = 0;
×
1144

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

1150
    (void)doAsyncFetchRows(pRequest, false, true);
×
1151

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

1156
    (*rows) = pResultInfo->row;
×
1157
    (*numOfRows) = pResultInfo->numOfRows;
×
1158
    return pRequest->code;
×
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) {
×
1175
  *numOfRows = 0;
×
1176
  *pData = NULL;
×
1177

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

1182
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
×
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;
×
1197

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

1203
  (void)doAsyncFetchRows(pRequest, false, false);
×
1204

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

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

1211
  return pRequest->code;
×
1212
}
1213

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

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

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

1230
  return pResInfo->pCol[columnIndex].offset;
×
1231
}
1232

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

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

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

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

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

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

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

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

1329
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
1,215,134✔
1330
  if (NULL == pWrapper) {
1,215,134✔
1331
    return;
613,079✔
1332
  }
1333
  destoryCatalogReq(pWrapper->pCatalogReq);
602,055✔
1334
  taosMemoryFree(pWrapper->pCatalogReq);
602,107!
1335
  qDestroyParseContext(pWrapper->pParseCtx);
602,072✔
1336
  taosMemoryFree(pWrapper);
602,103!
1337
}
1338

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

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

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

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

1358
  if (TSDB_CODE_SUCCESS == code) {
84,458!
1359
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
84,458✔
1360
  }
1361

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

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

1369
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
84,416✔
1370
}
84,429✔
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) {
84,399✔
1437
  SRequestObj *pRequest = pWrapper->pRequest;
84,399✔
1438
  SQuery      *pQuery = pRequest->pQuery;
84,399✔
1439

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

1447
  if (code == TSDB_CODE_SUCCESS) {
84,399✔
1448
    pRequest->stableQuery = pQuery->stableQuery;
80,773✔
1449
    if (pQuery->pRoot) {
80,773!
1450
      pRequest->stmtType = pQuery->pRoot->type;
80,774✔
1451
    }
1452

1453
    if (pQuery->haveResultSet) {
80,773✔
1454
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema, pRequest->isStmtBind);
35,745✔
1455
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
35,755✔
1456
    }
1457
  }
1458

1459
  if (code == TSDB_CODE_SUCCESS) {
84,434✔
1460
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
80,771✔
1461
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
80,771✔
1462
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
80,771✔
1463

1464
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
80,771✔
1465
  } else {
1466
    destorySqlCallbackWrapper(pWrapper);
3,663✔
1467
    pRequest->pWrapper = NULL;
3,663✔
1468
    qDestroyQuery(pRequest->pQuery);
3,663✔
1469
    pRequest->pQuery = NULL;
3,663✔
1470

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

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

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

1492
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
106,511✔
1493

1494
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
213,079✔
1495
                                &pWrapper->pRequest->body.queryJob);
106,537✔
1496
}
1497

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

1500
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
620,657✔
1501
  int32_t code = TSDB_CODE_SUCCESS;
620,657✔
1502
  switch (pWrapper->pRequest->pQuery->execStage) {
620,657!
1503
    case QUERY_EXEC_STAGE_PARSE: {
22,113✔
1504
      // continue parse after get metadata
1505
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
22,113✔
1506
      break;
22,113✔
1507
    }
1508
    case QUERY_EXEC_STAGE_ANALYSE: {
84,438✔
1509
      // analysis after get metadata
1510
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
84,438✔
1511
      break;
84,432✔
1512
    }
1513
    case QUERY_EXEC_STAGE_SCHEDULE: {
514,138✔
1514
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
514,138✔
1515
      break;
514,112✔
1516
    }
1517
    default:
×
1518
      break;
×
1519
  }
1520
  return code;
620,625✔
1521
}
1522

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

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

1532
  if (code == TSDB_CODE_SUCCESS) {
22,113✔
1533
    // pWrapper->pCatalogReq->forceUpdate = false;
1534
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
22,054✔
1535
  }
1536

1537
  if (TSDB_CODE_SUCCESS == code) {
22,113✔
1538
    code = phaseAsyncQuery(pWrapper);
21,538✔
1539
  }
1540

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

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

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

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

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

1579
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
602,067✔
1580
  const STscObj *pTscObj = pRequest->pTscObj;
602,067✔
1581

1582
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
602,067!
1583
  if (*pCxt == NULL) {
601,895!
1584
    return terrno;
×
1585
  }
1586

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

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

1628
  if (TSDB_CODE_SUCCESS == code) {
601,982!
1629
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
602,001✔
1630
  }
1631

1632
  if (TSDB_CODE_SUCCESS == code) {
602,029!
1633
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
602,058✔
1634
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
602,115✔
1635
  }
1636

1637
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
602,127!
1638
    int64_t syntaxStart = taosGetTimestampUs();
602,064✔
1639

1640
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
602,064!
1641
    if (pWrapper->pCatalogReq == NULL) {
601,982!
1642
      code = terrno;
×
1643
    } else {
1644
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
601,982✔
1645
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
601,982!
1646
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
601,992✔
1647
    }
1648

1649
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
602,021✔
1650
  }
1651

1652
  return code;
602,074✔
1653
}
1654

1655
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
602,561✔
1656
  SSqlCallbackWrapper *pWrapper = NULL;
602,561✔
1657
  int32_t              code = TSDB_CODE_SUCCESS;
602,561✔
1658

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

1668
  if (TSDB_CODE_SUCCESS == code) {
602,074✔
1669
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
602,070✔
1670
  }
1671

1672
  if (TSDB_CODE_SUCCESS == code) {
602,052✔
1673
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
599,140✔
1674
    code = phaseAsyncQuery(pWrapper);
599,140✔
1675
  }
1676

1677
  if (TSDB_CODE_SUCCESS != code) {
602,021✔
1678
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
2,917!
1679
             pRequest->requestId);
1680
    destorySqlCallbackWrapper(pWrapper);
2,917✔
1681
    pRequest->pWrapper = NULL;
2,917✔
1682
    qDestroyQuery(pRequest->pQuery);
2,917✔
1683
    pRequest->pQuery = NULL;
2,917✔
1684

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

1698
    terrno = code;
2,906✔
1699
    pRequest->code = code;
2,906✔
1700
    doRequestCallback(pRequest, code);
2,906✔
1701
  }
1702
}
1703

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

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

1742
static int32_t doAsyncFetch(void *pParam) {
46,599✔
1743
  SAsyncFetchParam *param = pParam;
46,599✔
1744
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
46,599✔
1745
  taosMemoryFree(param);
46,598!
1746
  return TSDB_CODE_SUCCESS;
46,599✔
1747
}
1748

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1856
_return:
×
1857

1858
  terrno = code;
×
1859

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

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

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

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

1884
  pRequest->syncQuery = true;
×
1885

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

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

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

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

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

1907
  *vgId = vgInfo.vgId;
×
1908

1909
_return:
×
1910

1911
  terrno = code;
×
1912

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

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

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

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

1937
  pRequest->syncQuery = true;
×
1938

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

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

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

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

1956
_return:
×
1957

1958
  terrno = code;
×
1959

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

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

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

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

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

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

1995
  pRequest->syncQuery = true;
×
1996

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

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

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

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

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

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

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

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

2045
  return pStmt;
556✔
2046
}
2047

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

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

2062
  return pStmt;
×
2063
}
2064

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

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

2079
  return pStmt;
85✔
2080
}
2081

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

2089
  return stmtPrepare(stmt, sql, length);
650✔
2090
}
2091

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

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

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

2108
  return TSDB_CODE_SUCCESS;
×
2109
}
2110

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

2118
  return stmtSetTbName(stmt, name);
5,655✔
2119
}
2120

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

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

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

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

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

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

2150
  return stmtGetColFields(stmt, fieldNum, fields);
1✔
2151
}
2152

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

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

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

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

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

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

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

2201
  return stmtBindBatch(stmt, bind, -1);
48,364✔
2202
}
2203

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

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

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

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

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

2239
  return stmtAddBatch(stmt);
47,190✔
2240
}
2241

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

2249
  return stmtExec(stmt);
5,102✔
2250
}
2251

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

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

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

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

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

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

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

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

2292
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
×
2293

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

2301
  return stmtAffectedRows(stmt);
×
2302
}
2303

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

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

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

2321
  return stmtClose(stmt);
639✔
2322
}
2323

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

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

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

2341
  return pStmt;
171✔
2342
}
2343

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

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

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

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

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

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

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

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

2406
    if (bindv->bind_cols && bindv->bind_cols[i]) {
7,639!
2407
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
7,640✔
2408

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

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

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

2432
  return code;
4,585✔
2433
}
2434

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

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

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

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

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

2471
  return code_s;
×
2472
}
2473

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

2481
  return stmtExec2(stmt, affected_rows);
4,570✔
2482
}
2483

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2568
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