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

taosdata / TDengine / #4844

09 Nov 2025 03:44PM UTC coverage: 63.058% (-0.5%) from 63.514%
#4844

push

travis-ci

web-flow
test: minor changes (#33510)

117164 of 185804 relevant lines covered (63.06%)

115657269.29 hits per line

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

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

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

35
#define TSC_VAR_NOT_RELEASE 1
36
#define TSC_VAR_RELEASED    0
37

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

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

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

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

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

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

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

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

82
void tzCleanup() {
1,777,300✔
83
  taosHashCleanup(pTimezoneMap);
1,777,300✔
84
  taosHashCleanup(pTimezoneNameMap);
1,777,300✔
85
}
1,777,300✔
86

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

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

124
END:
×
125
  return tz;
×
126
}
127
#endif
128

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

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

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

156
  if (option == TSDB_OPTION_CONNECTION_CLEAR) {
×
157
    val = NULL;
×
158
  }
159

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

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

204
  if (option == TSDB_OPTION_CONNECTION_CONNECTOR_INFO || option == TSDB_OPTION_CONNECTION_CLEAR) {
×
205
    if (val != NULL) {
×
206
      tstrncpy(pObj->optionInfo.cInfo, val, sizeof(pObj->optionInfo.cInfo));
×
207
    } else {
208
      pObj->optionInfo.cInfo[0] = 0;
×
209
    }
210
  }
211

212
  if (option == TSDB_OPTION_CONNECTION_USER_IP || option == TSDB_OPTION_CONNECTION_CLEAR) {
×
213
    SIpRange dualIp = {0};
×
214
    if (val != NULL) {
×
215
      pObj->optionInfo.userIp = taosInetAddr(val);
×
216
      SIpAddr addr = {0};
×
217
      code = taosGetIpFromFqdn(tsEnableIpv6, val, &addr);
×
218
      if (code == 0) {
×
219
        code = tIpStrToUint(&addr, &pObj->optionInfo.userDualIp);
×
220
      } 
221
      if (code != 0) {
×
222
        tscError("ipv6 flag %d failed to convert user ip %s to dual ip since %s", tsEnableIpv6 ?  1:0, val, tstrerror(code));
×
223
        pObj->optionInfo.userIp = INADDR_NONE; 
×
224
        pObj->optionInfo.userDualIp = dualIp;  
×
225
        code = 0;
×
226
      }
227
    } else {
228
      pObj->optionInfo.userIp = INADDR_NONE;
×
229
      pObj->optionInfo.userDualIp = dualIp;
×
230
    }
231
  }
232

233
END:
×
234
  releaseTscObj(*(int64_t *)taos);
×
235
  return terrno = code;
×
236
}
237

238
int taos_options_connection(TAOS *taos, TSDB_OPTION_CONNECTION option, const void *arg, ...) {
×
239
  return setConnectionOption(taos, option, (const char *)arg);
×
240
}
241

242
// this function may be called by user or system, or by both simultaneously.
243
void taos_cleanup(void) {
1,782,050✔
244
  tscInfo("start to cleanup client environment");
1,782,050✔
245
  if (atomic_val_compare_exchange_32(&sentinel, TSC_VAR_NOT_RELEASE, TSC_VAR_RELEASED) != TSC_VAR_NOT_RELEASE) {
1,782,050✔
246
    return;
4,750✔
247
  }
248

249
  monitorClose();
1,777,300✔
250
  tscStopCrashReport();
1,777,300✔
251

252
  hbMgrCleanUp();
1,777,300✔
253

254
  catalogDestroy();
1,777,300✔
255
  schedulerDestroy();
1,777,300✔
256

257
  fmFuncMgtDestroy();
1,777,300✔
258
  qCleanupKeywordsTable();
1,777,300✔
259

260
#if !defined(WINDOWS) && !defined(TD_ASTRA)
261
  tzCleanup();
1,777,300✔
262
#endif
263
  tmqMgmtClose();
1,777,300✔
264

265
  int32_t id = clientReqRefPool;
1,777,300✔
266
  clientReqRefPool = -1;
1,777,300✔
267
  taosCloseRef(id);
1,777,300✔
268

269
  id = clientConnRefPool;
1,777,300✔
270
  clientConnRefPool = -1;
1,777,300✔
271
  taosCloseRef(id);
1,777,300✔
272

273
  nodesDestroyAllocatorSet();
1,777,300✔
274
  cleanupAppInfo();
1,777,300✔
275
  rpcCleanup();
1,777,300✔
276
  tscDebug("rpc cleanup");
1,777,300✔
277

278
  if (TSDB_CODE_SUCCESS != cleanupTaskQueue()) {
1,777,300✔
279
    tscWarn("failed to cleanup task queue");
×
280
  }
281

282
  taosConvDestroy();
1,777,300✔
283
  DestroyRegexCache();
1,777,300✔
284
#ifdef TAOSD_INTEGRATED
285
  shellStopDaemon();
286
#endif
287
  tscInfo("all local resources released");
1,777,300✔
288
  taosCleanupCfg();
1,777,300✔
289
#ifndef TAOSD_INTEGRATED
290
  taosCloseLog();
1,777,300✔
291
#endif
292
}
293

294
static setConfRet taos_set_config_imp(const char *config) {
469✔
295
  setConfRet ret = {SET_CONF_RET_SUCC, {0}};
469✔
296
  // TODO: need re-implementation
297
  return ret;
469✔
298
}
299

300
setConfRet taos_set_config(const char *config) {
469✔
301
  // TODO  pthread_mutex_lock(&setConfMutex);
302
  setConfRet ret = taos_set_config_imp(config);
469✔
303
  //  pthread_mutex_unlock(&setConfMutex);
304
  return ret;
469✔
305
}
306

307
TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) {
3,634,862✔
308
  tscInfo("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
3,634,862✔
309
  if (user == NULL) {
3,636,116✔
310
    user = TSDB_DEFAULT_USER;
348,190✔
311
  }
312

313
  if (pass == NULL) {
3,636,116✔
314
    pass = TSDB_DEFAULT_PASS;
348,190✔
315
  }
316

317
  STscObj *pObj = NULL;
3,636,116✔
318
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
3,637,312✔
319
  if (TSDB_CODE_SUCCESS == code) {
3,636,265✔
320
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
3,622,897✔
321
    if (NULL == rid) {
3,622,994✔
322
      tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db);
×
323
      return NULL;
×
324
    }
325
    *rid = pObj->id;
3,622,994✔
326
    return (TAOS *)rid;
3,623,252✔
327
  } else {
328
    terrno = code;
13,368✔
329
  }
330

331
  return NULL;
13,368✔
332
}
333

334
int taos_set_notify_cb(TAOS *taos, __taos_notify_fn_t fp, void *param, int type) {
18,550✔
335
  if (taos == NULL) {
18,550✔
336
    terrno = TSDB_CODE_INVALID_PARA;
×
337
    return terrno;
×
338
  }
339

340
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
18,550✔
341
  if (NULL == pObj) {
18,550✔
342
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
343
    tscError("invalid parameter for %s", __func__);
×
344
    return terrno;
×
345
  }
346

347
  switch (type) {
18,550✔
348
    case TAOS_NOTIFY_PASSVER: {
5,300✔
349
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
5,300✔
350
      pObj->passInfo.fp = fp;
5,300✔
351
      pObj->passInfo.param = param;
5,300✔
352
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
5,300✔
353
      break;
5,300✔
354
    }
355
    case TAOS_NOTIFY_WHITELIST_VER: {
×
356
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
357
      pObj->whiteListInfo.fp = fp;
×
358
      pObj->whiteListInfo.param = param;
×
359
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
360
      break;
×
361
    }
362
    case TAOS_NOTIFY_USER_DROPPED: {
13,250✔
363
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
13,250✔
364
      pObj->userDroppedInfo.fp = fp;
13,250✔
365
      pObj->userDroppedInfo.param = param;
13,250✔
366
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
13,250✔
367
      break;
13,250✔
368
    }
369
    default: {
×
370
      terrno = TSDB_CODE_INVALID_PARA;
×
371
      releaseTscObj(*(int64_t *)taos);
×
372
      return terrno;
×
373
    }
374
  }
375

376
  releaseTscObj(*(int64_t *)taos);
18,550✔
377
  return 0;
18,550✔
378
}
379

380
typedef struct SFetchWhiteListInfo {
381
  int64_t                     connId;
382
  __taos_async_whitelist_fn_t userCbFn;
383
  void                       *userParam;
384
} SFetchWhiteListInfo;
385

386
int32_t fetchWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
387
  SFetchWhiteListInfo *pInfo = (SFetchWhiteListInfo *)param;
×
388
  TAOS                *taos = &pInfo->connId;
×
389
  if (code != TSDB_CODE_SUCCESS) {
×
390
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
391
    taosMemoryFree(pMsg->pData);
×
392
    taosMemoryFree(pMsg->pEpSet);
×
393
    taosMemoryFree(pInfo);
×
394
    return code;
×
395
  }
396

397
  SGetUserWhiteListRsp wlRsp;
×
398
  if (TSDB_CODE_SUCCESS != tDeserializeSGetUserWhiteListRsp(pMsg->pData, pMsg->len, &wlRsp)) {
×
399
    taosMemoryFree(pMsg->pData);
×
400
    taosMemoryFree(pMsg->pEpSet);
×
401
    taosMemoryFree(pInfo);
×
402
    tFreeSGetUserWhiteListRsp(&wlRsp);
×
403
    return terrno;
×
404
  }
405

406
  uint64_t *pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(uint64_t));
×
407
  if (pWhiteLists == NULL) {
×
408
    taosMemoryFree(pMsg->pData);
×
409
    taosMemoryFree(pMsg->pEpSet);
×
410
    taosMemoryFree(pInfo);
×
411
    tFreeSGetUserWhiteListRsp(&wlRsp);
×
412
    return terrno;
×
413
  }
414

415
  for (int i = 0; i < wlRsp.numWhiteLists; ++i) {
×
416
    pWhiteLists[i] = ((uint64_t)wlRsp.pWhiteLists[i].mask << 32) | wlRsp.pWhiteLists[i].ip;
×
417
  }
418

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

421
  taosMemoryFree(pWhiteLists);
×
422
  taosMemoryFree(pMsg->pData);
×
423
  taosMemoryFree(pMsg->pEpSet);
×
424
  taosMemoryFree(pInfo);
×
425
  tFreeSGetUserWhiteListRsp(&wlRsp);
×
426
  return code;
×
427
}
428

429
void taos_fetch_whitelist_a(TAOS *taos, __taos_async_whitelist_fn_t fp, void *param) {
×
430
  if (NULL == taos) {
×
431
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
432
    return;
×
433
  }
434

435
  int64_t connId = *(int64_t *)taos;
×
436

437
  STscObj *pTsc = acquireTscObj(connId);
×
438
  if (NULL == pTsc) {
×
439
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
440
    return;
×
441
  }
442

443
  SGetUserWhiteListReq req;
×
444
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
445
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
446
  if (msgLen < 0) {
×
447
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
448
    releaseTscObj(connId);
×
449
    return;
×
450
  }
451

452
  void *pReq = taosMemoryMalloc(msgLen);
×
453
  if (pReq == NULL) {
×
454
    fp(param, terrno, taos, 0, NULL);
×
455
    releaseTscObj(connId);
×
456
    return;
×
457
  }
458

459
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
460
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
461
    taosMemoryFree(pReq);
×
462
    releaseTscObj(connId);
×
463
    return;
×
464
  }
465

466
  SFetchWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchWhiteListInfo));
×
467
  if (pParam == NULL) {
×
468
    fp(param, terrno, taos, 0, NULL);
×
469
    taosMemoryFree(pReq);
×
470
    releaseTscObj(connId);
×
471
    return;
×
472
  }
473

474
  pParam->connId = connId;
×
475
  pParam->userCbFn = fp;
×
476

477
  pParam->userParam = param;
×
478
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
479
  if (pSendInfo == NULL) {
×
480
    fp(param, terrno, taos, 0, NULL);
×
481
    taosMemoryFree(pParam);
×
482
    taosMemoryFree(pReq);
×
483
    releaseTscObj(connId);
×
484
    return;
×
485
  }
486

487
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
488
  pSendInfo->requestId = generateRequestId();
×
489
  pSendInfo->requestObjRefId = 0;
×
490
  pSendInfo->param = pParam;
×
491
  pSendInfo->fp = fetchWhiteListCallbackFn;
×
492
  pSendInfo->msgType = TDMT_MND_GET_USER_WHITELIST;
×
493

494
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
495
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
496
    tscWarn("failed to async send msg to server");
×
497
  }
498
  releaseTscObj(connId);
×
499
  return;
×
500
}
501

502
typedef struct SFetchWhiteListDualStackInfo {
503
  int64_t connId;
504
  void   *userParam;
505

506
  __taos_async_whitelist_dual_stack_fn_t userCbFn;
507
} SFetchWhiteListDualStackInfo;
508

509
int32_t fetchWhiteListDualStackCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
510
  int32_t lino = 0;
×
511
  char  **pWhiteLists = NULL;
×
512

513
  SGetUserWhiteListRsp wlRsp = {0};
×
514

515
  SFetchWhiteListDualStackInfo *pInfo = (SFetchWhiteListDualStackInfo *)param;
×
516
  TAOS *taos = &pInfo->connId;
×
517

518
  if (code != TSDB_CODE_SUCCESS) {
×
519
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
520
    TAOS_CHECK_GOTO(code, &lino, _error);
×
521
  }
522

523
  if ((code = tDeserializeSGetUserWhiteListDualRsp(pMsg->pData, pMsg->len, &wlRsp)) != TSDB_CODE_SUCCESS) {
×
524
    TAOS_CHECK_GOTO(code, &lino, _error);
×
525
  }
526

527
  pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(char *));
×
528
  if (pWhiteLists == NULL) {
×
529
    code = terrno;
×
530
    TAOS_CHECK_GOTO(code, &lino, _error);
×
531
  }
532

533
  for (int32_t i = 0; i < wlRsp.numWhiteLists; i++) {
×
534
    SIpRange *pIpRange = &wlRsp.pWhiteListsDual[i];
×
535
    SIpAddr   ipAddr = {0};
×
536

537
    code = tIpUintToStr(pIpRange, &ipAddr);
×
538
    TAOS_CHECK_GOTO(code, &lino, _error);
×
539

540
    char *ip = taosMemCalloc(1, IP_RESERVE_CAP);
×
541
    if (ip == NULL) {
×
542
      code = terrno;
×
543
      TAOS_CHECK_GOTO(code, &lino, _error);
×
544
    }
545
    if (ipAddr.type == 0) {
×
546
      snprintf(ip, IP_RESERVE_CAP, "%s/%d", ipAddr.ipv4, ipAddr.mask);
×
547
    } else {
548
      if (ipAddr.ipv6[0] == 0) {
×
549
        memcpy(ipAddr.ipv6, "::", 2);
×
550
      }
551
      snprintf(ip, IP_RESERVE_CAP, "%s/%d", ipAddr.ipv6, ipAddr.mask);
×
552
    }
553
    pWhiteLists[i] = ip;
×
554
  }
555

556
  pInfo->userCbFn(pInfo->userParam, code, taos, wlRsp.numWhiteLists, pWhiteLists);
×
557
_error:
×
558
  if (pWhiteLists != NULL) {
×
559
    for (int32_t i = 0; i < wlRsp.numWhiteLists; i++) {
×
560
      taosMemFree(pWhiteLists[i]);
×
561
    }
562
    taosMemoryFree(pWhiteLists);
×
563
  }
564
  taosMemoryFree(pMsg->pData);
×
565
  taosMemoryFree(pMsg->pEpSet);
×
566
  taosMemoryFree(pInfo);
×
567
  tFreeSGetUserWhiteListDualRsp(&wlRsp);
×
568
  return code;
×
569
}
570
void taos_fetch_whitelist_dual_stack_a(TAOS *taos, __taos_async_whitelist_dual_stack_fn_t fp, void *param) {
×
571
  if (NULL == taos) {
×
572
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
573
    return;
×
574
  }
575
  int64_t connId = *(int64_t *)taos;
×
576

577
  STscObj *pTsc = acquireTscObj(connId);
×
578
  if (NULL == pTsc) {
×
579
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
580
    return;
×
581
  }
582

583
  SGetUserWhiteListReq req;
×
584
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
585
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
586
  if (msgLen < 0) {
×
587
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
588
    releaseTscObj(connId);
×
589
    return;
×
590
  }
591

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

599
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
600
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
601
    taosMemoryFree(pReq);
×
602
    releaseTscObj(connId);
×
603
    return;
×
604
  }
605

606
  SFetchWhiteListDualStackInfo *pParam = taosMemoryMalloc(sizeof(SFetchWhiteListDualStackInfo));
×
607
  if (pParam == NULL) {
×
608
    fp(param, terrno, taos, 0, NULL);
×
609
    taosMemoryFree(pReq);
×
610
    releaseTscObj(connId);
×
611
    return;
×
612
  }
613

614
  pParam->connId = connId;
×
615
  pParam->userCbFn = fp;
×
616
  pParam->userParam = param;
×
617

618
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
619
  if (pSendInfo == NULL) {
×
620
    fp(param, terrno, taos, 0, NULL);
×
621
    taosMemoryFree(pParam);
×
622
    taosMemoryFree(pReq);
×
623
    releaseTscObj(connId);
×
624
    return;
×
625
  }
626

627
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
628
  pSendInfo->requestId = generateRequestId();
×
629
  pSendInfo->requestObjRefId = 0;
×
630
  pSendInfo->param = pParam;
×
631
  pSendInfo->fp = fetchWhiteListDualStackCallbackFn;
×
632
  pSendInfo->msgType = TDMT_MND_GET_USER_WHITELIST_DUAL;
×
633

634
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
635
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
636
    tscWarn("failed to async send msg to server");
×
637
  }
638
  releaseTscObj(connId);
×
639
  return;
×
640
}
641

642
void taos_close_internal(void *taos) {
3,717,088✔
643
  if (taos == NULL) {
3,717,088✔
644
    return;
×
645
  }
646

647
  STscObj *pTscObj = (STscObj *)taos;
3,717,088✔
648
  tscDebug("conn:0x%" PRIx64 ", try to close connection, numOfReq:%d", pTscObj->id, pTscObj->numOfReqs);
3,717,088✔
649

650
  if (TSDB_CODE_SUCCESS != taosRemoveRef(clientConnRefPool, pTscObj->id)) {
3,717,088✔
651
    tscError("conn:0x%" PRIx64 ", failed to remove ref from conn pool", pTscObj->id);
×
652
  }
653
}
654

655
void taos_close(TAOS *taos) {
3,622,177✔
656
  if (taos == NULL) {
3,622,177✔
657
    return;
284✔
658
  }
659

660
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
3,621,893✔
661
  if (NULL == pObj) {
3,622,153✔
662
    taosMemoryFree(taos);
×
663
    return;
×
664
  }
665

666
  taos_close_internal(pObj);
3,622,153✔
667
  releaseTscObj(*(int64_t *)taos);
3,621,893✔
668
  taosMemoryFree(taos);
3,622,153✔
669
}
670

671
int taos_errno(TAOS_RES *res) {
825,116,708✔
672
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
825,116,708✔
673
    return terrno;
30,421✔
674
  }
675

676
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
825,087,639✔
677
    return 0;
341,457✔
678
  }
679

680
  return ((SRequestObj *)res)->code;
824,738,681✔
681
}
682

683
const char *taos_errstr(TAOS_RES *res) {
21,773,842✔
684
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
21,773,842✔
685
    return (const char *)tstrerror(terrno);
34,216✔
686
  }
687

688
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
21,739,626✔
689
    return "success";
×
690
  }
691

692
  SRequestObj *pRequest = (SRequestObj *)res;
21,739,626✔
693
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
21,739,626✔
694
    return pRequest->msgBuf;
17,504,961✔
695
  } else {
696
    return (const char *)tstrerror(pRequest->code);
4,234,665✔
697
  }
698
}
699

700
void taos_free_result(TAOS_RES *res) {
652,796,511✔
701
  if (NULL == res) {
652,796,511✔
702
    return;
1,841,968✔
703
  }
704

705
  tscTrace("res:%p, will be freed", res);
650,954,543✔
706

707
  if (TD_RES_QUERY(res)) {
650,954,757✔
708
    SRequestObj *pRequest = (SRequestObj *)res;
647,901,149✔
709
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
647,901,149✔
710
    destroyRequest(pRequest);
647,901,895✔
711
    return;
647,899,678✔
712
  }
713

714
  SMqRspObj *pRsp = (SMqRspObj *)res;
3,054,011✔
715
  if (TD_RES_TMQ(res)) {
3,054,011✔
716
    tDeleteMqDataRsp(&pRsp->dataRsp);
3,046,618✔
717
    doFreeReqResultInfo(&pRsp->resInfo);
3,046,618✔
718
  } else if (TD_RES_TMQ_METADATA(res)) {
10,942✔
719
    tDeleteSTaosxRsp(&pRsp->dataRsp);
451✔
720
    doFreeReqResultInfo(&pRsp->resInfo);
451✔
721
  } else if (TD_RES_TMQ_META(res)) {
10,491✔
722
    tDeleteMqMetaRsp(&pRsp->metaRsp);
9,294✔
723
  } else if (TD_RES_TMQ_BATCH_META(res)) {
1,197✔
724
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
1,197✔
725
  } else if (TD_RES_TMQ_RAW(res)) {
×
726
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
727
  }
728
  taosMemoryFree(pRsp);
3,057,356✔
729
}
730

731
void taos_kill_query(TAOS *taos) {
568✔
732
  if (NULL == taos) {
568✔
733
    return;
284✔
734
  }
735

736
  int64_t  rid = *(int64_t *)taos;
284✔
737
  STscObj *pTscObj = acquireTscObj(rid);
284✔
738
  if (pTscObj) {
284✔
739
    stopAllRequests(pTscObj->pRequests);
284✔
740
  }
741
  releaseTscObj(rid);
284✔
742
}
743

744
int taos_field_count(TAOS_RES *res) {
2,147,483,647✔
745
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
746
    return 0;
×
747
  }
748

749
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
750
  return pResInfo->numOfCols;
2,147,483,647✔
751
}
752

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

755
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
1,490,728,782✔
756
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,490,728,782✔
757
    return NULL;
2,573,677✔
758
  }
759

760
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
1,488,152,613✔
761
  return pResInfo->userFields;
1,488,152,613✔
762
}
763

764
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
646,163,277✔
765
TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqid) {
7,658✔
766
  return taosQueryImplWithReqid(taos, sql, false, reqid);
7,658✔
767
}
768

769
TAOS_FIELD_E *taos_fetch_fields_e(TAOS_RES *res) {
1,544✔
770
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,544✔
771
    return NULL;
×
772
  }
773
  SReqResultInfo* pResInfo = tscGetCurResInfo(res);
1,544✔
774
  return pResInfo->fields;
1,544✔
775
}
776

777
TAOS_ROW taos_fetch_row(TAOS_RES *res) {
2,147,483,647✔
778
  if (res == NULL) {
2,147,483,647✔
779
    return NULL;
×
780
  }
781

782
  if (TD_RES_QUERY(res)) {
2,147,483,647✔
783
    SRequestObj *pRequest = (SRequestObj *)res;
1,629,881,619✔
784
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
1,629,881,619✔
785
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0 || pRequest->killed) {
1,629,920,760✔
786
      return NULL;
×
787
    }
788

789
    if (pRequest->inCallback) {
1,629,833,117✔
790
      tscError("can not call taos_fetch_row before query callback ends.");
×
791
      terrno = TSDB_CODE_TSC_INVALID_OPERATION;
×
792
      return NULL;
×
793
    }
794

795
    return doAsyncFetchRows(pRequest, true, true);
1,629,854,702✔
796
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,387,362,965✔
797
    SMqRspObj      *msg = ((SMqRspObj *)res);
1,387,438,999✔
798
    SReqResultInfo *pResultInfo = NULL;
1,387,438,999✔
799
    if (msg->resIter == -1) {
1,387,439,142✔
800
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
2,671,685✔
801
        return NULL;
×
802
      }
803
    } else {
804
      pResultInfo = tmqGetCurResInfo(res);
1,384,767,171✔
805
    }
806

807
    if (pResultInfo->current < pResultInfo->numOfRows) {
1,387,438,754✔
808
      doSetOneRowPtr(pResultInfo);
1,356,865,032✔
809
      pResultInfo->current += 1;
1,356,862,692✔
810
      return pResultInfo->row;
1,356,862,714✔
811
    } else {
812
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
30,573,891✔
813
        return NULL;
2,671,490✔
814
      }
815

816
      doSetOneRowPtr(pResultInfo);
27,900,528✔
817
      pResultInfo->current += 1;
27,902,401✔
818
      return pResultInfo->row;
27,902,401✔
819
    }
820
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
821
    return NULL;
×
822
  } else {
823
    tscError("invalid result passed to taos_fetch_row");
×
824
    terrno = TSDB_CODE_TMQ_INVALID_DATA;
×
825
    return NULL;
×
826
  }
827
}
828

829
int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
1,388,333,125✔
830
  return taos_print_row_with_size(str, INT32_MAX, row, fields, num_fields);
1,388,333,125✔
831
}
832
int taos_print_row_with_size(char *str, uint32_t size, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
1,388,301,849✔
833
  int32_t len = 0;
1,388,301,849✔
834
  for (int i = 0; i < num_fields; ++i) {
2,147,483,647✔
835
    if (i > 0 && len < size - 1) {
2,147,483,647✔
836
      str[len++] = ' ';
2,147,483,647✔
837
    }
838

839
    if (row[i] == NULL) {
2,147,483,647✔
840
      len += tsnprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
29,982,385✔
841
      continue;
29,982,537✔
842
    }
843

844
    switch (fields[i].type) {
2,147,483,647✔
845
      case TSDB_DATA_TYPE_TINYINT:
1,102,341✔
846
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
1,102,341✔
847
        break;
1,101,823✔
848

849
      case TSDB_DATA_TYPE_UTINYINT:
1,101,823✔
850
        len += tsnprintf(str + len, size - len, "%u", *((uint8_t *)row[i]));
1,101,823✔
851
        break;
1,101,046✔
852

853
      case TSDB_DATA_TYPE_SMALLINT:
1,101,823✔
854
        len += tsnprintf(str + len, size - len, "%d", *((int16_t *)row[i]));
1,101,823✔
855
        break;
1,101,823✔
856

857
      case TSDB_DATA_TYPE_USMALLINT:
1,101,046✔
858
        len += tsnprintf(str + len, size - len, "%u", *((uint16_t *)row[i]));
1,101,046✔
859
        break;
1,101,564✔
860

861
      case TSDB_DATA_TYPE_INT:
1,437,076,121✔
862
        len += tsnprintf(str + len, size - len, "%d", *((int32_t *)row[i]));
1,437,076,121✔
863
        break;
1,437,079,878✔
864

865
      case TSDB_DATA_TYPE_UINT:
1,101,564✔
866
        len += tsnprintf(str + len, size - len, "%u", *((uint32_t *)row[i]));
1,101,564✔
867
        break;
1,101,305✔
868

869
      case TSDB_DATA_TYPE_BIGINT:
1,149,547,617✔
870
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
1,149,547,617✔
871
        break;
1,149,549,709✔
872

873
      case TSDB_DATA_TYPE_UBIGINT:
1,101,046✔
874
        len += tsnprintf(str + len, size - len, "%" PRIu64, *((uint64_t *)row[i]));
1,101,046✔
875
        break;
1,100,269✔
876

877
      case TSDB_DATA_TYPE_FLOAT: {
1,570,660✔
878
        float fv = 0;
1,570,660✔
879
        fv = GET_FLOAT_VAL(row[i]);
1,570,660✔
880
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
1,570,660✔
881
      } break;
1,570,660✔
882

883
      case TSDB_DATA_TYPE_DOUBLE: {
808,329,660✔
884
        double dv = 0;
808,329,660✔
885
        dv = GET_DOUBLE_VAL(row[i]);
808,329,660✔
886
        len += snprintf(str + len, size - len, "%.*g", DBL_DIG, dv);
808,330,058✔
887
      } break;
808,330,411✔
888

889
      case TSDB_DATA_TYPE_VARBINARY: {
1,102,687✔
890
        void    *data = NULL;
1,102,687✔
891
        uint32_t tmp = 0;
1,102,687✔
892
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
1,102,687✔
893
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
1,102,687✔
894
          break;
×
895
        }
896
        uint32_t copyLen = TMIN(size - len - 1, tmp);
1,101,133✔
897
        (void)memcpy(str + len, data, copyLen);
1,101,133✔
898
        len += copyLen;
1,101,133✔
899
        taosMemoryFree(data);
1,101,133✔
900
      } break;
1,102,687✔
901
      case TSDB_DATA_TYPE_BINARY:
1,400,789,010✔
902
      case TSDB_DATA_TYPE_NCHAR:
903
      case TSDB_DATA_TYPE_GEOMETRY: {
904
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
1,400,789,010✔
905
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
1,400,791,312✔
906
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
405,278,646✔
907
          if (charLen > fields[i].bytes || charLen < 0) {
996,618,829✔
908
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
981✔
909
            break;
×
910
          }
911
        } else {
912
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
404,176,715✔
913
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
82✔
914
            break;
×
915
          }
916
        }
917

918
        uint32_t copyLen = TMIN(size - len - 1, charLen);
1,400,814,100✔
919
        (void)memcpy(str + len, row[i], copyLen);
1,400,814,100✔
920
        len += copyLen;
1,400,801,986✔
921
      } break;
1,400,801,986✔
922
      case TSDB_DATA_TYPE_BLOB:
×
923
      case TSDB_DATA_TYPE_MEDIUMBLOB: {
924
        void    *data = NULL;
×
925
        uint32_t tmp = 0;
×
926
        int32_t  charLen = blobDataLen((char *)row[i] - BLOBSTR_HEADER_SIZE);
×
927
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
×
928
          break;
×
929
        }
930

931
        uint32_t copyLen = TMIN(size - len - 1, tmp);
×
932
        (void)memcpy(str + len, data, copyLen);
×
933
        len += copyLen;
×
934

935
        taosMemoryFree(data);
×
936
      } break;
×
937

938
      case TSDB_DATA_TYPE_TIMESTAMP:
1,809,254,260✔
939
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
1,809,254,260✔
940
        break;
1,809,265,885✔
941

942
      case TSDB_DATA_TYPE_BOOL:
1,101,564✔
943
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
1,101,564✔
944
        break;
577,653✔
945
      case TSDB_DATA_TYPE_DECIMAL64:
×
946
      case TSDB_DATA_TYPE_DECIMAL: {
947
        uint32_t decimalLen = strlen(row[i]);
×
948
        uint32_t copyLen = TMIN(size - len - 1, decimalLen);
×
949
        (void)memcpy(str + len, row[i], copyLen);
×
950
        len += copyLen;
×
951
      } break;
×
952
      default:
×
953
        break;
×
954
    }
955

956
    if (len >= size - 1) {
2,147,483,647✔
957
      break;
×
958
    }
959
  }
960
  if (len < size) {
1,388,274,684✔
961
    str[len] = 0;
1,388,335,612✔
962
  }
963

964
  return len;
1,388,336,090✔
965
}
966

967
int *taos_fetch_lengths(TAOS_RES *res) {
2,147,483,647✔
968
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
969
    return NULL;
×
970
  }
971

972
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
973
  return pResInfo->length;
2,147,483,647✔
974
}
975

976
TAOS_ROW *taos_result_block(TAOS_RES *res) {
×
977
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
978
    terrno = TSDB_CODE_INVALID_PARA;
×
979
    return NULL;
×
980
  }
981

982
  if (taos_is_update_query(res)) {
×
983
    return NULL;
×
984
  }
985

986
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
987
  return &pResInfo->row;
×
988
}
989

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

1041
const char *taos_get_client_info() { return td_version; }
1,259,327✔
1042

1043
// return int32_t
1044
int taos_affected_rows(TAOS_RES *res) {
507,208,937✔
1045
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
507,208,937✔
1046
      TD_RES_TMQ_BATCH_META(res)) {
507,213,664✔
1047
    return 0;
×
1048
  }
1049

1050
  SRequestObj    *pRequest = (SRequestObj *)res;
507,212,707✔
1051
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
507,212,707✔
1052
  return (int)pResInfo->numOfRows;
507,214,039✔
1053
}
1054

1055
// return int64_t
1056
int64_t taos_affected_rows64(TAOS_RES *res) {
1,298,535✔
1057
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
1,298,535✔
1058
      TD_RES_TMQ_BATCH_META(res)) {
1,298,535✔
1059
    return 0;
×
1060
  }
1061

1062
  SRequestObj    *pRequest = (SRequestObj *)res;
1,298,535✔
1063
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
1,298,535✔
1064
  return pResInfo->numOfRows;
1,298,535✔
1065
}
1066

1067
int taos_result_precision(TAOS_RES *res) {
1,485,658,577✔
1068
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,485,658,577✔
1069
    return TSDB_TIME_PRECISION_MILLI;
×
1070
  }
1071

1072
  if (TD_RES_QUERY(res)) {
1,485,658,841✔
1073
    SRequestObj *pRequest = (SRequestObj *)res;
94,436,960✔
1074
    return pRequest->body.resInfo.precision;
94,436,960✔
1075
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,391,222,291✔
1076
    SReqResultInfo *info = tmqGetCurResInfo(res);
1,391,220,445✔
1077
    return info->precision;
1,391,220,445✔
1078
  }
1079
  return TSDB_TIME_PRECISION_MILLI;
×
1080
}
1081

1082
int taos_select_db(TAOS *taos, const char *db) {
157,425✔
1083
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
157,425✔
1084
  if (pObj == NULL) {
157,425✔
1085
    releaseTscObj(*(int64_t *)taos);
×
1086
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1087
    return TSDB_CODE_TSC_DISCONNECTED;
×
1088
  }
1089

1090
  if (db == NULL || strlen(db) == 0) {
157,425✔
1091
    releaseTscObj(*(int64_t *)taos);
×
1092
    tscError("invalid parameter for %s", db == NULL ? "db is NULL" : "db is empty");
×
1093
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1094
    return terrno;
×
1095
  }
1096

1097
  char sql[256] = {0};
157,425✔
1098
  (void)snprintf(sql, tListLen(sql), "use %s", db);
157,425✔
1099

1100
  TAOS_RES *pRequest = taos_query(taos, sql);
157,425✔
1101
  int32_t   code = taos_errno(pRequest);
157,425✔
1102

1103
  taos_free_result(pRequest);
157,425✔
1104
  releaseTscObj(*(int64_t *)taos);
157,322✔
1105
  return code;
157,425✔
1106
}
1107

1108
void taos_stop_query(TAOS_RES *res) {
652,591,532✔
1109
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
652,591,532✔
1110
      TD_RES_TMQ_BATCH_META(res)) {
652,594,659✔
1111
    return;
×
1112
  }
1113

1114
  stopAllQueries((SRequestObj *)res);
652,588,650✔
1115
}
1116

1117
bool taos_is_null(TAOS_RES *res, int32_t row, int32_t col) {
×
1118
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1119
    return true;
×
1120
  }
1121
  SReqResultInfo *pResultInfo = tscGetCurResInfo(res);
×
1122
  if (col >= pResultInfo->numOfCols || col < 0 || row >= pResultInfo->numOfRows || row < 0) {
×
1123
    return true;
×
1124
  }
1125

1126
  SResultColumn *pCol = &pResultInfo->pCol[col];
×
1127
  if (IS_VAR_DATA_TYPE(pResultInfo->fields[col].type)) {
×
1128
    return (pCol->offset[row] == -1);
×
1129
  } else {
1130
    return colDataIsNull_f(pCol, row);
×
1131
  }
1132
}
1133

1134
bool taos_is_update_query(TAOS_RES *res) { return taos_num_fields(res) == 0; }
9,288,212✔
1135

1136
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
174,250,501✔
1137
  int32_t numOfRows = 0;
174,250,501✔
1138
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
174,250,501✔
1139
  return numOfRows;
174,250,501✔
1140
}
1141

1142
int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) {
174,250,501✔
1143
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
174,250,501✔
1144
    return 0;
×
1145
  }
1146

1147
  if (TD_RES_QUERY(res)) {
174,250,501✔
1148
    SRequestObj *pRequest = (SRequestObj *)res;
166,821,829✔
1149

1150
    (*rows) = NULL;
166,821,829✔
1151
    (*numOfRows) = 0;
166,821,829✔
1152

1153
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
166,821,829✔
1154
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
166,674,974✔
1155
      return pRequest->code;
784,426✔
1156
    }
1157

1158
    (void)doAsyncFetchRows(pRequest, false, true);
166,037,403✔
1159

1160
    // TODO refactor
1161
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
166,036,783✔
1162
    pResultInfo->current = pResultInfo->numOfRows;
166,037,403✔
1163

1164
    (*rows) = pResultInfo->row;
166,037,403✔
1165
    (*numOfRows) = pResultInfo->numOfRows;
166,037,403✔
1166
    return pRequest->code;
166,037,195✔
1167
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
7,428,672✔
1168
    SReqResultInfo *pResultInfo = NULL;
7,428,672✔
1169
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
7,428,672✔
1170
    if (code != 0) return code;
7,428,672✔
1171

1172
    pResultInfo->current = pResultInfo->numOfRows;
7,094,370✔
1173
    (*rows) = pResultInfo->row;
7,094,370✔
1174
    (*numOfRows) = pResultInfo->numOfRows;
7,094,370✔
1175
    return 0;
7,094,370✔
1176
  } else {
1177
    tscError("taos_fetch_block_s invalid res type");
×
1178
    return TSDB_CODE_TMQ_INVALID_DATA;
×
1179
  }
1180
}
1181

1182
int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) {
1,440,566✔
1183
  *numOfRows = 0;
1,440,566✔
1184
  *pData = NULL;
1,440,566✔
1185

1186
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,440,566✔
1187
    return 0;
×
1188
  }
1189

1190
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,440,566✔
1191
    SReqResultInfo *pResultInfo = NULL;
1,399,185✔
1192
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
1,399,185✔
1193
    if (code != 0) {
1,399,185✔
1194
      (*numOfRows) = 0;
28,973✔
1195
      return 0;
28,973✔
1196
    }
1197

1198
    pResultInfo->current = pResultInfo->numOfRows;
1,370,212✔
1199
    (*numOfRows) = pResultInfo->numOfRows;
1,370,212✔
1200
    (*pData) = (void *)pResultInfo->pData;
1,370,212✔
1201
    return 0;
1,370,212✔
1202
  }
1203

1204
  SRequestObj *pRequest = (SRequestObj *)res;
41,381✔
1205

1206
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
41,381✔
1207
      pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
41,381✔
1208
    return pRequest->code;
×
1209
  }
1210

1211
  (void)doAsyncFetchRows(pRequest, false, false);
41,381✔
1212

1213
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
41,381✔
1214

1215
  pResultInfo->current = pResultInfo->numOfRows;
41,381✔
1216
  (*numOfRows) = pResultInfo->numOfRows;
41,381✔
1217
  (*pData) = (void *)pResultInfo->pData;
41,381✔
1218

1219
  return pRequest->code;
41,381✔
1220
}
1221

1222
int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) {
116,813,034✔
1223
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
116,813,034✔
1224
    return 0;
×
1225
  }
1226

1227
  int32_t numOfFields = taos_num_fields(res);
116,813,034✔
1228
  if (columnIndex < 0 || columnIndex >= numOfFields || numOfFields == 0) {
116,813,034✔
1229
    return 0;
×
1230
  }
1231

1232
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
116,813,034✔
1233
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
116,813,034✔
1234
  if (!IS_VAR_DATA_TYPE(pField->type)) {
116,813,034✔
1235
    return 0;
×
1236
  }
1237

1238
  return pResInfo->pCol[columnIndex].offset;
116,813,034✔
1239
}
1240

1241
int taos_is_null_by_column(TAOS_RES *res, int columnIndex, bool result[], int *rows) {
324,020,587✔
1242
  if (res == NULL || result == NULL || rows == NULL || *rows <= 0 || columnIndex < 0 || TD_RES_TMQ_META(res) ||
324,020,587✔
1243
      TD_RES_TMQ_RAW(res) || TD_RES_TMQ_BATCH_META(res)) {
324,020,587✔
1244
    return TSDB_CODE_INVALID_PARA;
×
1245
  }
1246

1247
  int32_t numOfFields = taos_num_fields(res);
324,020,587✔
1248
  if (columnIndex >= numOfFields || numOfFields == 0) {
324,020,587✔
1249
    return TSDB_CODE_INVALID_PARA;
×
1250
  }
1251

1252
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
324,020,587✔
1253
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
324,020,587✔
1254
  SResultColumn  *pCol = &pResInfo->pCol[columnIndex];
324,020,587✔
1255

1256
  if (*rows > pResInfo->numOfRows) {
324,020,587✔
1257
    *rows = pResInfo->numOfRows;
×
1258
  }
1259
  if (IS_VAR_DATA_TYPE(pField->type)) {
324,020,587✔
1260
    for (int i = 0; i < *rows; i++) {
×
1261
      if (pCol->offset[i] == -1) {
×
1262
        result[i] = true;
×
1263
      } else {
1264
        result[i] = false;
×
1265
      }
1266
    }
1267
  } else {
1268
    for (int i = 0; i < *rows; i++) {
2,147,483,647✔
1269
      if (colDataIsNull_f(pCol, i)) {
2,147,483,647✔
1270
        result[i] = true;
2,147,483,647✔
1271
      } else {
1272
        result[i] = false;
2,147,483,647✔
1273
      }
1274
    }
1275
  }
1276
  return 0;
324,020,587✔
1277
}
1278

1279
int taos_validate_sql(TAOS *taos, const char *sql) {
×
1280
  TAOS_RES *pObj = taosQueryImpl(taos, sql, true, TD_REQ_FROM_APP);
×
1281

1282
  int code = taos_errno(pObj);
×
1283

1284
  taos_free_result(pObj);
×
1285
  return code;
×
1286
}
1287

1288
void taos_reset_current_db(TAOS *taos) {
×
1289
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
1290
  if (pTscObj == NULL) {
×
1291
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1292
    return;
×
1293
  }
1294

1295
  resetConnectDB(pTscObj);
×
1296

1297
  releaseTscObj(*(int64_t *)taos);
×
1298
}
1299

1300
const char *taos_get_server_info(TAOS *taos) {
38,416✔
1301
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
38,416✔
1302
  if (pTscObj == NULL) {
38,416✔
1303
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1304
    return NULL;
×
1305
  }
1306

1307
  releaseTscObj(*(int64_t *)taos);
38,416✔
1308

1309
  return pTscObj->sDetailVer;
38,416✔
1310
}
1311

1312
int taos_get_current_db(TAOS *taos, char *database, int len, int *required) {
604✔
1313
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
604✔
1314
  if (pTscObj == NULL) {
604✔
1315
    return TSDB_CODE_TSC_DISCONNECTED;
×
1316
  }
1317

1318
  int code = TSDB_CODE_SUCCESS;
604✔
1319
  (void)taosThreadMutexLock(&pTscObj->mutex);
604✔
1320
  if (database == NULL || len <= 0) {
604✔
1321
    if (required != NULL) *required = strlen(pTscObj->db) + 1;
302✔
1322
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
302✔
1323
  } else if (len < strlen(pTscObj->db) + 1) {
302✔
1324
    tstrncpy(database, pTscObj->db, len);
151✔
1325
    if (required) *required = strlen(pTscObj->db) + 1;
151✔
1326
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
151✔
1327
  } else {
1328
    tstrncpy(database, pTscObj->db, len);
151✔
1329
    code = 0;
151✔
1330
  }
1331
_return:
604✔
1332
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
604✔
1333
  releaseTscObj(*(int64_t *)taos);
604✔
1334
  return code;
604✔
1335
}
1336

1337
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
1,305,267,220✔
1338
  if (NULL == pWrapper) {
1,305,267,220✔
1339
    return;
655,922,013✔
1340
  }
1341
  destoryCatalogReq(pWrapper->pCatalogReq);
649,345,207✔
1342
  taosMemoryFree(pWrapper->pCatalogReq);
649,333,178✔
1343
  qDestroyParseContext(pWrapper->pParseCtx);
649,324,624✔
1344
  taosMemoryFree(pWrapper);
649,318,590✔
1345
}
1346

1347
void destroyCtxInRequest(SRequestObj *pRequest) {
3,676,367✔
1348
  schedulerFreeJob(&pRequest->body.queryJob, 0);
3,676,367✔
1349
  qDestroyQuery(pRequest->pQuery);
3,676,367✔
1350
  pRequest->pQuery = NULL;
3,676,367✔
1351
  destorySqlCallbackWrapper(pRequest->pWrapper);
3,676,367✔
1352
  pRequest->pWrapper = NULL;
3,676,367✔
1353
}
3,676,367✔
1354

1355
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
185,157,386✔
1356
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
185,157,386✔
1357
  SRequestObj         *pRequest = pWrapper->pRequest;
185,157,386✔
1358
  SQuery              *pQuery = pRequest->pQuery;
185,157,386✔
1359

1360
  qDebug("req:0x%" PRIx64 ", start to semantic analysis, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
185,158,335✔
1361

1362
  int64_t analyseStart = taosGetTimestampUs();
185,158,335✔
1363
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
185,158,335✔
1364
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
185,158,335✔
1365

1366
  if (TSDB_CODE_SUCCESS == code) {
185,157,591✔
1367
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
185,155,714✔
1368
  }
1369

1370
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
185,143,375✔
1371

1372
  if (pRequest->parseOnly) {
185,149,353✔
1373
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
301,844✔
1374
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
301,844✔
1375
  }
1376

1377
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
185,148,013✔
1378
}
185,144,680✔
1379

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

1407
    *ppTarget = pTarget;
×
1408
  }
1409

1410
  return code;
×
1411
}
1412

1413
void handleSubQueryFromAnalyse(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, SNode *pRoot) {
×
1414
  SRequestObj         *pNewRequest = NULL;
×
1415
  SSqlCallbackWrapper *pNewWrapper = NULL;
×
1416
  int32_t              code = buildPreviousRequest(pWrapper->pRequest, pWrapper->pRequest->sqlstr, &pNewRequest);
×
1417
  if (code) {
×
1418
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1419
    return;
×
1420
  }
1421

1422
  pNewRequest->pQuery = NULL;
×
1423
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode **)&pNewRequest->pQuery);
×
1424
  if (pNewRequest->pQuery) {
×
1425
    pNewRequest->pQuery->pRoot = pRoot;
×
1426
    pRoot = NULL;
×
1427
    pNewRequest->pQuery->execStage = QUERY_EXEC_STAGE_ANALYSE;
×
1428
  }
1429
  if (TSDB_CODE_SUCCESS == code) {
×
1430
    code = prepareAndParseSqlSyntax(&pNewWrapper, pNewRequest, false);
×
1431
  }
1432
  if (TSDB_CODE_SUCCESS == code) {
×
1433
    code = cloneCatalogReq(&pNewWrapper->pCatalogReq, pWrapper->pCatalogReq);
×
1434
  }
1435
  if (TSDB_CODE_SUCCESS == code) {
×
1436
    doAsyncQueryFromAnalyse(pResultMeta, pNewWrapper, code);
×
1437
    nodesDestroyNode(pRoot);
×
1438
  } else {
1439
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1440
    return;
×
1441
  }
1442
}
1443

1444
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
185,134,223✔
1445
  SRequestObj *pRequest = pWrapper->pRequest;
185,134,223✔
1446
  SQuery      *pQuery = pRequest->pQuery;
185,144,164✔
1447

1448
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
185,143,951✔
1449
    SNode *prevRoot = pQuery->pPrevRoot;
×
1450
    pQuery->pPrevRoot = NULL;
×
1451
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
×
1452
    return;
×
1453
  }
1454

1455
  if (code == TSDB_CODE_SUCCESS) {
185,147,090✔
1456
    pRequest->stableQuery = pQuery->stableQuery;
168,991,218✔
1457
    if (pQuery->pRoot) {
168,998,626✔
1458
      pRequest->stmtType = pQuery->pRoot->type;
168,982,370✔
1459
    }
1460

1461
    if (pQuery->haveResultSet) {
169,009,721✔
1462
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
90,582,846✔
1463
                              pRequest->stmtBindVersion > 0);
90,582,216✔
1464
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
90,583,475✔
1465
    }
1466
  }
1467

1468
  if (code == TSDB_CODE_SUCCESS) {
185,134,610✔
1469
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
168,990,270✔
1470
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
168,983,380✔
1471
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
168,992,162✔
1472

1473
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
168,981,939✔
1474
  } else {
1475
    destorySqlCallbackWrapper(pWrapper);
16,144,340✔
1476
    pRequest->pWrapper = NULL;
16,144,340✔
1477
    qDestroyQuery(pRequest->pQuery);
16,144,340✔
1478
    pRequest->pQuery = NULL;
16,144,340✔
1479

1480
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
16,144,340✔
1481
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
3,626,716✔
1482
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1483
      restartAsyncQuery(pRequest, code);
3,626,716✔
1484
      return;
3,626,716✔
1485
    }
1486

1487
    // return to app directly
1488
    tscError("req:0x%" PRIx64 ", error occurs, code:%s, return to user app, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
12,517,624✔
1489
             pRequest->requestId);
1490
    pRequest->code = code;
12,517,624✔
1491
    returnToUser(pRequest);
12,517,624✔
1492
  }
1493
}
1494

1495
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
195,313,486✔
1496
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
381,338,534✔
1497
                           .requestId = pWrapper->pParseCtx->requestId,
195,314,504✔
1498
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
195,323,693✔
1499
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
195,313,947✔
1500

1501
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
381,346,120✔
1502

1503
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
204,601,679✔
1504
                                &pWrapper->pRequest->body.queryJob);
195,309,522✔
1505
}
1506

1507
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code);
1508

1509
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
654,056,192✔
1510
  int32_t code = TSDB_CODE_SUCCESS;
654,056,192✔
1511
  switch (pWrapper->pRequest->pQuery->execStage) {
654,056,192✔
1512
    case QUERY_EXEC_STAGE_PARSE: {
10,176,162✔
1513
      // continue parse after get metadata
1514
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
10,176,162✔
1515
      break;
10,176,162✔
1516
    }
1517
    case QUERY_EXEC_STAGE_ANALYSE: {
185,148,771✔
1518
      // analysis after get metadata
1519
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
185,148,771✔
1520
      break;
185,146,459✔
1521
    }
1522
    case QUERY_EXEC_STAGE_SCHEDULE: {
458,744,747✔
1523
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
458,744,747✔
1524
      break;
458,744,860✔
1525
    }
1526
    default:
×
1527
      break;
×
1528
  }
1529
  return code;
654,066,068✔
1530
}
1531

1532
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code) {
10,176,162✔
1533
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
10,176,162✔
1534
  SRequestObj         *pRequest = pWrapper->pRequest;
10,176,162✔
1535
  SQuery              *pQuery = pRequest->pQuery;
10,176,162✔
1536

1537
  pRequest->metric.ctgCostUs += taosGetTimestampUs() - pRequest->metric.ctgStart;
10,176,162✔
1538
  qDebug("req:0x%" PRIx64 ", continue parse query, QID:0x%" PRIx64 ", code:%s", pRequest->self, pRequest->requestId,
10,176,162✔
1539
         tstrerror(code));
1540

1541
  if (code == TSDB_CODE_SUCCESS) {
10,176,162✔
1542
    // pWrapper->pCatalogReq->forceUpdate = false;
1543
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
10,152,431✔
1544
  }
1545

1546
  if (TSDB_CODE_SUCCESS == code) {
10,176,162✔
1547
    code = phaseAsyncQuery(pWrapper);
9,645,793✔
1548
  }
1549

1550
  if (TSDB_CODE_SUCCESS != code) {
10,176,162✔
1551
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
530,369✔
1552
             tstrerror(code), pWrapper->pRequest->requestId);
1553
    destorySqlCallbackWrapper(pWrapper);
530,369✔
1554
    pRequest->pWrapper = NULL;
530,369✔
1555
    terrno = code;
530,369✔
1556
    pRequest->code = code;
530,369✔
1557
    doRequestCallback(pRequest, code);
530,369✔
1558
  }
1559
}
10,176,162✔
1560

1561
void continueInsertFromCsv(SSqlCallbackWrapper *pWrapper, SRequestObj *pRequest) {
11,774✔
1562
  int32_t code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
11,774✔
1563
  if (TSDB_CODE_SUCCESS == code) {
11,774✔
1564
    code = phaseAsyncQuery(pWrapper);
11,774✔
1565
  }
1566

1567
  if (TSDB_CODE_SUCCESS != code) {
11,774✔
1568
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
×
1569
             tstrerror(code), pWrapper->pRequest->requestId);
1570
    destorySqlCallbackWrapper(pWrapper);
×
1571
    pRequest->pWrapper = NULL;
×
1572
    terrno = code;
×
1573
    pRequest->code = code;
×
1574
    doRequestCallback(pRequest, code);
×
1575
  }
1576
}
11,774✔
1577

1578
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
113,980✔
1579
  int64_t connId = *(int64_t *)taos;
113,980✔
1580
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
113,980✔
1581
}
113,980✔
1582

1583
void taos_query_a_with_reqid(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param, int64_t reqid) {
×
1584
  int64_t connId = *(int64_t *)taos;
×
1585
  taosAsyncQueryImplWithReqid(connId, sql, fp, param, false, reqid);
×
1586
}
×
1587

1588
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
649,335,667✔
1589
  const STscObj *pTscObj = pRequest->pTscObj;
649,335,667✔
1590

1591
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
649,340,787✔
1592
  if (*pCxt == NULL) {
649,329,813✔
1593
    return terrno;
×
1594
  }
1595

1596
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
1,271,543,711✔
1597
                           .requestRid = pRequest->self,
649,341,106✔
1598
                           .acctId = pTscObj->acctId,
649,339,499✔
1599
                           .db = pRequest->pDb,
649,341,735✔
1600
                           .topicQuery = false,
1601
                           .pSql = pRequest->sqlstr,
649,342,658✔
1602
                           .sqlLen = pRequest->sqlLen,
649,343,911✔
1603
                           .pMsg = pRequest->msgBuf,
649,344,263✔
1604
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1605
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
649,345,198✔
1606
                           .pStmtCb = NULL,
1607
                           .pUser = pTscObj->user,
649,342,136✔
1608
                           .pEffectiveUser = pRequest->effectiveUser,
649,341,016✔
1609
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
649,337,837✔
1610
                           .enableSysInfo = pTscObj->sysInfo,
649,341,191✔
1611
                           .async = true,
1612
                           .svrVer = pTscObj->sVer,
649,339,806✔
1613
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
649,341,469✔
1614
                           .allocatorId = pRequest->allocatorRefId,
649,339,106✔
1615
                           .parseSqlFp = clientParseSql,
1616
                           .parseSqlParam = pWrapper,
1617
                           .setQueryFp = setQueryRequest,
1618
                           .timezone = pTscObj->optionInfo.timezone,
649,340,872✔
1619
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
649,334,778✔
1620
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
649,336,017✔
1621
  (*pCxt)->biMode = biMode;
649,332,158✔
1622
  return TSDB_CODE_SUCCESS;
649,342,022✔
1623
}
1624

1625
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
649,335,391✔
1626
  int32_t              code = TSDB_CODE_SUCCESS;
649,335,391✔
1627
  STscObj             *pTscObj = pRequest->pTscObj;
649,335,391✔
1628
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
649,343,792✔
1629
  if (pWrapper == NULL) {
649,331,888✔
1630
    code = terrno;
×
1631
  } else {
1632
    pWrapper->pRequest = pRequest;
649,331,888✔
1633
    pRequest->pWrapper = pWrapper;
649,334,704✔
1634
    *ppWrapper = pWrapper;
649,335,193✔
1635
  }
1636

1637
  if (TSDB_CODE_SUCCESS == code) {
649,332,345✔
1638
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
649,337,931✔
1639
  }
1640

1641
  if (TSDB_CODE_SUCCESS == code) {
649,334,790✔
1642
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
649,335,317✔
1643
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
649,347,482✔
1644
  }
1645

1646
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
649,336,504✔
1647
    int64_t syntaxStart = taosGetTimestampUs();
649,343,078✔
1648

1649
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
649,343,078✔
1650
    if (pWrapper->pCatalogReq == NULL) {
649,323,289✔
1651
      code = terrno;
×
1652
    } else {
1653
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
649,327,055✔
1654
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
649,335,114✔
1655
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
649,333,200✔
1656
    }
1657

1658
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
649,333,589✔
1659
  }
1660

1661
  return code;
649,345,033✔
1662
}
1663

1664
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
650,544,730✔
1665
  SSqlCallbackWrapper *pWrapper = NULL;
650,544,730✔
1666
  int32_t              code = TSDB_CODE_SUCCESS;
650,550,380✔
1667

1668
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
650,550,380✔
1669
    code = pRequest->prevCode;
1,208,690✔
1670
    terrno = code;
1,208,690✔
1671
    pRequest->code = code;
1,208,690✔
1672
    tscDebug("req:0x%" PRIx64 ", call sync query cb with code:%s", pRequest->self, tstrerror(code));
1,208,690✔
1673
    doRequestCallback(pRequest, code);
1,208,690✔
1674
    return;
1,208,690✔
1675
  }
1676

1677
  if (TSDB_CODE_SUCCESS == code) {
649,330,294✔
1678
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
649,331,112✔
1679
  }
1680

1681
  if (TSDB_CODE_SUCCESS == code) {
649,323,999✔
1682
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
644,403,938✔
1683
    code = phaseAsyncQuery(pWrapper);
644,415,113✔
1684
  }
1685

1686
  if (TSDB_CODE_SUCCESS != code) {
649,332,019✔
1687
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
4,924,336✔
1688
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
4,795,490✔
1689
               pRequest->requestId);
1690
    } else {
1691
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
128,846✔
1692
               pRequest->requestId);
1693
    }
1694

1695
    destorySqlCallbackWrapper(pWrapper);
4,924,336✔
1696
    pRequest->pWrapper = NULL;
4,924,336✔
1697
    qDestroyQuery(pRequest->pQuery);
4,924,336✔
1698
    pRequest->pQuery = NULL;
4,924,336✔
1699

1700
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
4,924,336✔
1701
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
9,680✔
1702
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1703
      code = refreshMeta(pRequest->pTscObj, pRequest);
9,680✔
1704
      if (code != 0) {
9,680✔
1705
        tscWarn("req:0x%" PRIx64 ", refresh meta failed, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
9,680✔
1706
                pRequest->requestId);
1707
      }
1708
      pRequest->prevCode = code;
9,680✔
1709
      doAsyncQuery(pRequest, true);
9,680✔
1710
      return;
9,680✔
1711
    }
1712

1713
    terrno = code;
4,914,656✔
1714
    pRequest->code = code;
4,914,656✔
1715
    doRequestCallback(pRequest, code);
4,914,656✔
1716
  }
1717
}
1718

1719
void restartAsyncQuery(SRequestObj *pRequest, int32_t code) {
3,676,367✔
1720
  tscInfo("restart request:%s p:%p", pRequest->sqlstr, pRequest);
3,676,367✔
1721
  SRequestObj *pUserReq = pRequest;
3,676,367✔
1722
  (void)acquireRequest(pRequest->self);
3,676,367✔
1723
  while (pUserReq) {
3,676,367✔
1724
    if (pUserReq->self == pUserReq->relation.userRefId || pUserReq->relation.userRefId == 0) {
3,676,367✔
1725
      break;
1726
    } else {
1727
      int64_t nextRefId = pUserReq->relation.nextRefId;
×
1728
      (void)releaseRequest(pUserReq->self);
×
1729
      if (nextRefId) {
×
1730
        pUserReq = acquireRequest(nextRefId);
×
1731
      }
1732
    }
1733
  }
1734
  bool hasSubRequest = pUserReq != pRequest || pRequest->relation.prevRefId != 0;
3,676,367✔
1735
  if (pUserReq) {
3,676,367✔
1736
    destroyCtxInRequest(pUserReq);
3,676,367✔
1737
    pUserReq->prevCode = code;
3,676,367✔
1738
    (void)memset(&pUserReq->relation, 0, sizeof(pUserReq->relation));
3,676,367✔
1739
  } else {
1740
    tscError("User req is missing");
×
1741
    (void)removeFromMostPrevReq(pRequest);
×
1742
    return;
×
1743
  }
1744
  if (hasSubRequest)
3,676,367✔
1745
    (void)removeFromMostPrevReq(pRequest);
×
1746
  else
1747
    (void)releaseRequest(pUserReq->self);
3,676,367✔
1748
  doAsyncQuery(pUserReq, true);
3,676,367✔
1749
}
1750

1751
typedef struct SAsyncFetchParam {
1752
  SRequestObj      *pReq;
1753
  __taos_async_fn_t fp;
1754
  void             *param;
1755
} SAsyncFetchParam;
1756

1757
static int32_t doAsyncFetch(void *pParam) {
108,454,918✔
1758
  SAsyncFetchParam *param = pParam;
108,454,918✔
1759
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
108,454,918✔
1760
  taosMemoryFree(param);
108,454,424✔
1761
  return TSDB_CODE_SUCCESS;
108,453,603✔
1762
}
1763

1764
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
108,457,612✔
1765
  if (res == NULL || fp == NULL) {
108,457,612✔
1766
    tscError("taos_fetch_rows_a invalid paras");
×
1767
    return;
×
1768
  }
1769
  if (!TD_RES_QUERY(res)) {
108,457,612✔
1770
    tscError("taos_fetch_rows_a res is NULL");
×
1771
    fp(param, res, TSDB_CODE_APP_ERROR);
×
1772
    return;
×
1773
  }
1774

1775
  SRequestObj *pRequest = res;
108,457,852✔
1776
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
108,457,852✔
1777
    fp(param, res, 0);
2,915✔
1778
    return;
2,915✔
1779
  }
1780

1781
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
108,455,177✔
1782
  if (!pParam) {
108,454,697✔
1783
    fp(param, res, terrno);
×
1784
    return;
×
1785
  }
1786
  pParam->pReq = pRequest;
108,454,697✔
1787
  pParam->fp = fp;
108,454,697✔
1788
  pParam->param = param;
108,455,177✔
1789
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
108,455,177✔
1790
  if (TSDB_CODE_SUCCESS != code) {
108,455,177✔
1791
    taosMemoryFree(pParam);
×
1792
    fp(param, res, code);
×
1793
    return;
×
1794
  }
1795
}
1796

1797
void taos_fetch_raw_block_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
4,188✔
1798
  if (res == NULL || fp == NULL) {
4,188✔
1799
    tscError("taos_fetch_raw_block_a invalid paras");
×
1800
    return;
×
1801
  }
1802
  if (!TD_RES_QUERY(res)) {
4,188✔
1803
    tscError("taos_fetch_raw_block_a res is NULL");
×
1804
    return;
×
1805
  }
1806
  SRequestObj    *pRequest = res;
4,188✔
1807
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
4,188✔
1808

1809
  // set the current block is all consumed
1810
  pResultInfo->convertUcs4 = false;
4,188✔
1811

1812
  // it is a local executed query, no need to do async fetch
1813
  taos_fetch_rows_a(pRequest, fp, param);
4,188✔
1814
}
1815

1816
const void *taos_get_raw_block(TAOS_RES *res) {
2,463✔
1817
  if (res == NULL) {
2,463✔
1818
    tscError("taos_get_raw_block invalid paras");
×
1819
    return NULL;
×
1820
  }
1821
  if (!TD_RES_QUERY(res)) {
2,463✔
1822
    tscError("taos_get_raw_block res is NULL");
×
1823
    return NULL;
×
1824
  }
1825
  SRequestObj *pRequest = res;
2,463✔
1826

1827
  return pRequest->body.resInfo.pData;
2,463✔
1828
}
1829

1830
int taos_get_db_route_info(TAOS *taos, const char *db, TAOS_DB_ROUTE_INFO *dbInfo) {
×
1831
  if (NULL == taos) {
×
1832
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1833
    return terrno;
×
1834
  }
1835

1836
  if (NULL == db || NULL == dbInfo) {
×
1837
    tscError("invalid input param, db:%p, dbInfo:%p", db, dbInfo);
×
1838
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1839
    return terrno;
×
1840
  }
1841

1842
  int64_t      connId = *(int64_t *)taos;
×
1843
  SRequestObj *pRequest = NULL;
×
1844
  char        *sql = "taos_get_db_route_info";
×
1845
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1846
  if (code != TSDB_CODE_SUCCESS) {
×
1847
    terrno = code;
×
1848
    return terrno;
×
1849
  }
1850

1851
  STscObj  *pTscObj = pRequest->pTscObj;
×
1852
  SCatalog *pCtg = NULL;
×
1853
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1854
  if (code != TSDB_CODE_SUCCESS) {
×
1855
    goto _return;
×
1856
  }
1857

1858
  SRequestConnInfo conn = {
×
1859
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1860

1861
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1862

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

1866
  code = catalogGetDBVgInfo(pCtg, &conn, dbFName, dbInfo);
×
1867
  if (code) {
×
1868
    goto _return;
×
1869
  }
1870

1871
_return:
×
1872

1873
  terrno = code;
×
1874

1875
  destroyRequest(pRequest);
×
1876
  return code;
×
1877
}
1878

1879
int taos_get_table_vgId(TAOS *taos, const char *db, const char *table, int *vgId) {
×
1880
  if (NULL == taos) {
×
1881
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1882
    return terrno;
×
1883
  }
1884

1885
  if (NULL == db || NULL == table || NULL == vgId) {
×
1886
    tscError("invalid input param, db:%p, table:%p, vgId:%p", db, table, vgId);
×
1887
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1888
    return terrno;
×
1889
  }
1890

1891
  int64_t      connId = *(int64_t *)taos;
×
1892
  SRequestObj *pRequest = NULL;
×
1893
  char        *sql = "taos_get_table_vgId";
×
1894
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1895
  if (code != TSDB_CODE_SUCCESS) {
×
1896
    return terrno;
×
1897
  }
1898

1899
  pRequest->syncQuery = true;
×
1900

1901
  STscObj  *pTscObj = pRequest->pTscObj;
×
1902
  SCatalog *pCtg = NULL;
×
1903
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1904
  if (code != TSDB_CODE_SUCCESS) {
×
1905
    goto _return;
×
1906
  }
1907

1908
  SRequestConnInfo conn = {
×
1909
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1910

1911
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1912

1913
  SName tableName = {0};
×
1914
  toName(pTscObj->acctId, db, table, &tableName);
×
1915

1916
  SVgroupInfo vgInfo;
×
1917
  code = catalogGetTableHashVgroup(pCtg, &conn, &tableName, &vgInfo);
×
1918
  if (code) {
×
1919
    goto _return;
×
1920
  }
1921

1922
  *vgId = vgInfo.vgId;
×
1923

1924
_return:
×
1925

1926
  terrno = code;
×
1927

1928
  destroyRequest(pRequest);
×
1929
  return code;
×
1930
}
1931

1932
int taos_get_tables_vgId(TAOS *taos, const char *db, const char *table[], int tableNum, int *vgId) {
×
1933
  if (NULL == taos) {
×
1934
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1935
    return terrno;
×
1936
  }
1937

1938
  if (NULL == db || NULL == table || NULL == vgId || tableNum <= 0) {
×
1939
    tscError("invalid input param, db:%p, table:%p, vgId:%p, tbNum:%d", db, table, vgId, tableNum);
×
1940
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1941
    return terrno;
×
1942
  }
1943

1944
  int64_t      connId = *(int64_t *)taos;
×
1945
  SRequestObj *pRequest = NULL;
×
1946
  char        *sql = "taos_get_table_vgId";
×
1947
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1948
  if (code != TSDB_CODE_SUCCESS) {
×
1949
    return terrno;
×
1950
  }
1951

1952
  pRequest->syncQuery = true;
×
1953

1954
  STscObj  *pTscObj = pRequest->pTscObj;
×
1955
  SCatalog *pCtg = NULL;
×
1956
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1957
  if (code != TSDB_CODE_SUCCESS) {
×
1958
    goto _return;
×
1959
  }
1960

1961
  SRequestConnInfo conn = {
×
1962
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1963

1964
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1965

1966
  code = catalogGetTablesHashVgId(pCtg, &conn, pTscObj->acctId, db, table, tableNum, vgId);
×
1967
  if (code) {
×
1968
    goto _return;
×
1969
  }
1970

1971
_return:
×
1972

1973
  terrno = code;
×
1974

1975
  destroyRequest(pRequest);
×
1976
  return code;
×
1977
}
1978

1979
int taos_load_table_info(TAOS *taos, const char *tableNameList) {
1,240✔
1980
  if (NULL == taos) {
1,240✔
1981
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1982
    return terrno;
×
1983
  }
1984

1985
  int64_t       connId = *(int64_t *)taos;
1,240✔
1986
  const int32_t MAX_TABLE_NAME_LENGTH = 12 * 1024 * 1024;  // 12MB list
1,240✔
1987
  int32_t       code = 0;
1,240✔
1988
  SRequestObj  *pRequest = NULL;
1,240✔
1989
  SCatalogReq   catalogReq = {0};
1,240✔
1990

1991
  if (NULL == tableNameList) {
1,240✔
1992
    return TSDB_CODE_SUCCESS;
×
1993
  }
1994

1995
  int32_t length = (int32_t)strlen(tableNameList);
1,240✔
1996
  if (0 == length) {
1,240✔
1997
    return TSDB_CODE_SUCCESS;
×
1998
  } else if (length > MAX_TABLE_NAME_LENGTH) {
1,240✔
1999
    tscError("tableNameList too long, length:%d, maximum allowed:%d", length, MAX_TABLE_NAME_LENGTH);
×
2000
    return TSDB_CODE_TSC_INVALID_OPERATION;
×
2001
  }
2002

2003
  char *sql = "taos_load_table_info";
1,240✔
2004
  code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
1,240✔
2005
  if (code != TSDB_CODE_SUCCESS) {
1,240✔
2006
    terrno = code;
×
2007
    goto _return;
×
2008
  }
2009

2010
  pRequest->syncQuery = true;
1,240✔
2011

2012
  STscObj *pTscObj = pRequest->pTscObj;
1,240✔
2013
  code = transferTableNameList(tableNameList, pTscObj->acctId, pTscObj->db, &catalogReq.pTableMeta);
1,240✔
2014
  if (code) {
1,240✔
2015
    goto _return;
×
2016
  }
2017

2018
  SCatalog *pCtg = NULL;
1,240✔
2019
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
1,240✔
2020
  if (code != TSDB_CODE_SUCCESS) {
1,240✔
2021
    goto _return;
×
2022
  }
2023

2024
  SRequestConnInfo conn = {
1,240✔
2025
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
1,240✔
2026

2027
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
1,240✔
2028

2029
  code = catalogAsyncGetAllMeta(pCtg, &conn, &catalogReq, syncCatalogFn, pRequest->body.interParam, NULL);
1,240✔
2030
  if (code) {
1,240✔
2031
    goto _return;
×
2032
  }
2033

2034
  SSyncQueryParam *pParam = pRequest->body.interParam;
1,240✔
2035
  code = tsem_wait(&pParam->sem);
1,240✔
2036
  if (code) {
1,240✔
2037
    tscError("tsem wait failed, code:%d - %s", code, tstrerror(code));
×
2038
    goto _return;
×
2039
  }
2040
_return:
1,240✔
2041
  destoryCatalogReq(&catalogReq);
1,240✔
2042
  destroyRequest(pRequest);
1,240✔
2043
  return code;
1,240✔
2044
}
2045

2046
TAOS_STMT *taos_stmt_init(TAOS *taos) {
380,801✔
2047
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
380,801✔
2048
  if (NULL == pObj) {
381,052✔
2049
    tscError("invalid parameter for %s", __FUNCTION__);
×
2050
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2051
    return NULL;
×
2052
  }
2053

2054
  TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL);
381,052✔
2055
  if (NULL == pStmt) {
380,911✔
2056
    tscError("stmt init failed, errcode:%s", terrstr());
×
2057
  }
2058
  releaseTscObj(*(int64_t *)taos);
380,911✔
2059

2060
  return pStmt;
381,197✔
2061
}
2062

2063
TAOS_STMT *taos_stmt_init_with_reqid(TAOS *taos, int64_t reqid) {
×
2064
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
2065
  if (NULL == pObj) {
×
2066
    tscError("invalid parameter for %s", __FUNCTION__);
×
2067
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2068
    return NULL;
×
2069
  }
2070

2071
  TAOS_STMT *pStmt = stmtInit(pObj, reqid, NULL);
×
2072
  if (NULL == pStmt) {
×
2073
    tscError("stmt init failed, errcode:%s", terrstr());
×
2074
  }
2075
  releaseTscObj(*(int64_t *)taos);
×
2076

2077
  return pStmt;
×
2078
}
2079

2080
TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options) {
28,664✔
2081
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
28,664✔
2082
  if (NULL == pObj) {
28,664✔
2083
    tscError("invalid parameter for %s", __FUNCTION__);
×
2084
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2085
    return NULL;
×
2086
  }
2087

2088
  TAOS_STMT *pStmt = stmtInit(pObj, options->reqId, options);
28,664✔
2089
  if (NULL == pStmt) {
28,664✔
2090
    tscError("stmt init failed, errcode:%s", terrstr());
×
2091
  }
2092
  releaseTscObj(*(int64_t *)taos);
28,664✔
2093

2094
  return pStmt;
28,664✔
2095
}
2096

2097
int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
950,848✔
2098
  if (stmt == NULL || sql == NULL) {
950,848✔
2099
    tscError("NULL parameter for %s", __FUNCTION__);
×
2100
    terrno = TSDB_CODE_INVALID_PARA;
×
2101
    return terrno;
×
2102
  }
2103

2104
  return stmtPrepare(stmt, sql, length);
950,902✔
2105
}
2106

2107
int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags) {
20,608✔
2108
  if (stmt == NULL || name == NULL) {
20,608✔
2109
    tscError("NULL parameter for %s", __FUNCTION__);
×
2110
    terrno = TSDB_CODE_INVALID_PARA;
×
2111
    return terrno;
×
2112
  }
2113

2114
  int32_t code = stmtSetTbName(stmt, name);
20,608✔
2115
  if (code) {
20,608✔
2116
    return code;
620✔
2117
  }
2118

2119
  if (tags) {
19,988✔
2120
    return stmtSetTbTags(stmt, tags);
19,988✔
2121
  }
2122

2123
  return TSDB_CODE_SUCCESS;
×
2124
}
2125

2126
int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name) {
13,632,986✔
2127
  if (stmt == NULL || name == NULL) {
13,632,986✔
2128
    tscError("NULL parameter for %s", __FUNCTION__);
×
2129
    terrno = TSDB_CODE_INVALID_PARA;
×
2130
    return terrno;
×
2131
  }
2132

2133
  return stmtSetTbName(stmt, name);
13,636,993✔
2134
}
2135

2136
int taos_stmt_set_tags(TAOS_STMT *stmt, TAOS_MULTI_BIND *tags) {
5,393✔
2137
  if (stmt == NULL || tags == NULL) {
5,393✔
2138
    tscError("NULL parameter for %s", __FUNCTION__);
×
2139
    terrno = TSDB_CODE_INVALID_PARA;
×
2140
    return terrno;
×
2141
  }
2142

2143
  return stmtSetTbTags(stmt, tags);
5,393✔
2144
}
2145

2146
int taos_stmt_set_sub_tbname(TAOS_STMT *stmt, const char *name) { return taos_stmt_set_tbname(stmt, name); }
×
2147

2148
int taos_stmt_get_tag_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
×
2149
  if (stmt == NULL || NULL == fieldNum) {
×
2150
    tscError("NULL parameter for %s", __FUNCTION__);
×
2151
    terrno = TSDB_CODE_INVALID_PARA;
×
2152
    return terrno;
×
2153
  }
2154

2155
  return stmtGetTagFields(stmt, fieldNum, fields);
×
2156
}
2157

2158
int taos_stmt_get_col_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
×
2159
  if (stmt == NULL || NULL == fieldNum) {
×
2160
    tscError("NULL parameter for %s", __FUNCTION__);
×
2161
    terrno = TSDB_CODE_INVALID_PARA;
×
2162
    return terrno;
×
2163
  }
2164

2165
  return stmtGetColFields(stmt, fieldNum, fields);
×
2166
}
2167

2168
// let stmt to reclaim TAOS_FIELD_E that was allocated by `taos_stmt_get_tag_fields`/`taos_stmt_get_col_fields`
2169
void taos_stmt_reclaim_fields(TAOS_STMT *stmt, TAOS_FIELD_E *fields) {
×
2170
  (void)stmt;
2171
  if (!fields) return;
×
2172
  taosMemoryFree(fields);
×
2173
}
2174

2175
int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
72,010✔
2176
  if (stmt == NULL || bind == NULL) {
72,010✔
2177
    tscError("NULL parameter for %s", __FUNCTION__);
×
2178
    terrno = TSDB_CODE_INVALID_PARA;
×
2179
    return terrno;
×
2180
  }
2181

2182
  if (bind->num > 1) {
72,010✔
2183
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
3,888✔
2184
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
3,888✔
2185
    return terrno;
3,888✔
2186
  }
2187

2188
  return stmtBindBatch(stmt, bind, -1);
68,122✔
2189
}
2190

2191
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
335,030,782✔
2192
  if (stmt == NULL || bind == NULL) {
335,030,782✔
2193
    tscError("NULL parameter for %s", __FUNCTION__);
×
2194
    terrno = TSDB_CODE_INVALID_PARA;
×
2195
    return terrno;
×
2196
  }
2197

2198
  if (bind->num <= 0 || bind->num > INT16_MAX) {
338,823,770✔
2199
    tscError("invalid bind num %d", bind->num);
3,658✔
2200
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
3,658✔
2201
    return terrno;
×
2202
  }
2203

2204
  int32_t insert = 0;
340,487,841✔
2205
  int32_t code = stmtIsInsert(stmt, &insert);
338,907,390✔
2206
  if (TSDB_CODE_SUCCESS != code) {
337,111,120✔
2207
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2208
    return code;
×
2209
  }
2210
  if (0 == insert && bind->num > 1) {
337,111,120✔
2211
    tscError("only one row data allowed for query");
×
2212
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2213
    return terrno;
×
2214
  }
2215

2216
  return stmtBindBatch(stmt, bind, -1);
337,111,120✔
2217
}
2218

2219
int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind, int colIdx) {
30,800✔
2220
  if (stmt == NULL || bind == NULL) {
30,800✔
2221
    tscError("NULL parameter for %s", __FUNCTION__);
×
2222
    terrno = TSDB_CODE_INVALID_PARA;
×
2223
    return terrno;
×
2224
  }
2225

2226
  if (colIdx < 0) {
30,800✔
2227
    tscError("invalid bind column idx %d", colIdx);
×
2228
    terrno = TSDB_CODE_INVALID_PARA;
×
2229
    return terrno;
×
2230
  }
2231

2232
  int32_t insert = 0;
30,800✔
2233
  int32_t code = stmtIsInsert(stmt, &insert);
30,800✔
2234
  if (TSDB_CODE_SUCCESS != code) {
30,800✔
2235
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2236
    return code;
×
2237
  }
2238
  if (0 == insert && bind->num > 1) {
30,800✔
2239
    tscError("only one row data allowed for query");
×
2240
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2241
    return terrno;
×
2242
  }
2243

2244
  return stmtBindBatch(stmt, bind, colIdx);
30,800✔
2245
}
2246

2247
int taos_stmt_add_batch(TAOS_STMT *stmt) {
324,558,736✔
2248
  if (stmt == NULL) {
324,558,736✔
2249
    tscError("NULL parameter for %s", __FUNCTION__);
×
2250
    terrno = TSDB_CODE_INVALID_PARA;
×
2251
    return terrno;
×
2252
  }
2253

2254
  return stmtAddBatch(stmt);
324,558,736✔
2255
}
2256

2257
int taos_stmt_execute(TAOS_STMT *stmt) {
4,628,392✔
2258
  if (stmt == NULL) {
4,628,392✔
2259
    tscError("NULL parameter for %s", __FUNCTION__);
×
2260
    terrno = TSDB_CODE_INVALID_PARA;
×
2261
    return terrno;
×
2262
  }
2263

2264
  return stmtExec(stmt);
4,628,392✔
2265
}
2266

2267
int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
×
2268
  if (stmt == NULL || insert == NULL) {
×
2269
    tscError("NULL parameter for %s", __FUNCTION__);
×
2270
    terrno = TSDB_CODE_INVALID_PARA;
×
2271
    return terrno;
×
2272
  }
2273

2274
  return stmtIsInsert(stmt, insert);
×
2275
}
2276

2277
int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) {
×
2278
  if (stmt == NULL || nums == NULL) {
×
2279
    tscError("NULL parameter for %s", __FUNCTION__);
×
2280
    terrno = TSDB_CODE_INVALID_PARA;
×
2281
    return terrno;
×
2282
  }
2283

2284
  return stmtGetParamNum(stmt, nums);
×
2285
}
2286

2287
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
×
2288
  if (stmt == NULL || type == NULL || NULL == bytes || idx < 0) {
×
2289
    tscError("invalid parameter for %s", __FUNCTION__);
×
2290
    terrno = TSDB_CODE_INVALID_PARA;
×
2291
    return terrno;
×
2292
  }
2293

2294
  return stmtGetParam(stmt, idx, type, bytes);
×
2295
}
2296

2297
TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt) {
10,266✔
2298
  if (stmt == NULL) {
10,266✔
2299
    tscError("NULL parameter for %s", __FUNCTION__);
×
2300
    terrno = TSDB_CODE_INVALID_PARA;
×
2301
    return NULL;
×
2302
  }
2303

2304
  return stmtUseResult(stmt);
10,266✔
2305
}
2306

2307
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
10,258✔
2308

2309
int taos_stmt_affected_rows(TAOS_STMT *stmt) {
5,056✔
2310
  if (stmt == NULL) {
5,056✔
2311
    tscError("NULL parameter for %s", __FUNCTION__);
×
2312
    terrno = TSDB_CODE_INVALID_PARA;
×
2313
    return 0;
×
2314
  }
2315

2316
  return stmtAffectedRows(stmt);
5,056✔
2317
}
2318

2319
int taos_stmt_affected_rows_once(TAOS_STMT *stmt) {
4,060✔
2320
  if (stmt == NULL) {
4,060✔
2321
    tscError("NULL parameter for %s", __FUNCTION__);
×
2322
    terrno = TSDB_CODE_INVALID_PARA;
×
2323
    return 0;
×
2324
  }
2325

2326
  return stmtAffectedRowsOnce(stmt);
4,060✔
2327
}
2328

2329
int taos_stmt_close(TAOS_STMT *stmt) {
409,719✔
2330
  if (stmt == NULL) {
409,719✔
2331
    tscError("NULL parameter for %s", __FUNCTION__);
×
2332
    terrno = TSDB_CODE_INVALID_PARA;
×
2333
    return terrno;
×
2334
  }
2335

2336
  return stmtClose(stmt);
409,719✔
2337
}
2338

2339
TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option) {
19,954✔
2340
  if (NULL == taos) {
19,954✔
2341
    tscError("NULL parameter for %s", __FUNCTION__);
×
2342
    terrno = TSDB_CODE_INVALID_PARA;
×
2343
    return NULL;
×
2344
  }
2345
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
19,954✔
2346
  if (NULL == pObj) {
19,954✔
2347
    tscError("invalid parameter for %s", __FUNCTION__);
×
2348
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2349
    return NULL;
×
2350
  }
2351

2352
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
19,954✔
2353

2354
  releaseTscObj(*(int64_t *)taos);
19,954✔
2355

2356
  return pStmt;
19,954✔
2357
}
2358

2359
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
21,970✔
2360
  if (stmt == NULL || sql == NULL) {
21,970✔
2361
    tscError("NULL parameter for %s", __FUNCTION__);
×
2362
    terrno = TSDB_CODE_INVALID_PARA;
×
2363
    return terrno;
×
2364
  }
2365

2366
  return stmtPrepare2(stmt, sql, length);
21,970✔
2367
}
2368

2369
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
7,431,362✔
2370
  if (stmt == NULL) {
7,431,362✔
2371
    tscError("NULL parameter for %s", __FUNCTION__);
×
2372
    terrno = TSDB_CODE_INVALID_PARA;
×
2373
    return terrno;
×
2374
  }
2375

2376
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
7,431,362✔
2377
  STMT2_DLOG_E("start to bind param");
7,431,362✔
2378
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 1) {
7,431,362✔
2379
    STMT2_ELOG_E("async bind param is still working, please try again later");
724✔
2380
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
724✔
2381
    return terrno;
×
2382
  }
2383

2384
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
7,432,052✔
2385
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
2386
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
2387
    }
2388
    pStmt->execSemWaited = true;
×
2389
  }
2390

2391
  int32_t code = TSDB_CODE_SUCCESS;
7,433,136✔
2392
  for (int i = 0; i < bindv->count; ++i) {
23,772,462✔
2393
    if (bindv->tbnames && bindv->tbnames[i]) {
16,335,324✔
2394
      code = stmtSetTbName2(stmt, bindv->tbnames[i]);
16,335,431✔
2395
      if (code) {
16,334,767✔
2396
        terrno = code;
265✔
2397
        STMT2_ELOG("set tbname failed, code:%s", tstrerror(code));
×
2398
        return terrno;
×
2399
      }
2400
    }
2401

2402
    SVCreateTbReq *pCreateTbReq = NULL;
16,335,575✔
2403
    if (bindv->tags && bindv->tags[i]) {
16,334,222✔
2404
      code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
10,106,372✔
2405
    } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
6,229,474✔
2406
      code = stmtCheckTags2(stmt, &pCreateTbReq);
2,013,668✔
2407
    } else if (pStmt->sql.autoCreateTbl) {
4,214,995✔
2408
      // if (pStmt->sql.autoCreateTbl) {
2409
      //   pStmt->sql.autoCreateTbl = false;
2410
      //   STMT2_WLOG_E("sql is autoCreateTbl, but no tags");
2411
      // }
2412
      code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
×
2413
    }
2414

2415
    if (code) {
16,331,582✔
2416
      terrno = code;
×
2417
      STMT2_ELOG("set tags failed, code:%s", tstrerror(code));
×
2418
      return terrno;
×
2419
    }
2420

2421
    if (bindv->bind_cols && bindv->bind_cols[i]) {
16,331,582✔
2422
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
16,338,975✔
2423

2424
      if (bind->num <= 0 || bind->num > INT16_MAX) {
16,338,163✔
2425
        STMT2_ELOG("bind num:%d must > 0 and < INT16_MAX", bind->num);
5,059✔
2426
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
5,059✔
2427
        return terrno;
×
2428
      }
2429

2430
      if (!stmt2IsInsert(stmt) && bind->num > 1) {
16,334,999✔
2431
        STMT2_ELOG_E("only one row data allowed for query");
×
2432
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2433
        return terrno;
×
2434
      }
2435

2436
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
16,335,930✔
2437
      if (TSDB_CODE_SUCCESS != code) {
16,338,697✔
2438
        terrno = code;
270✔
2439
        STMT2_ELOG("bind batch failed, code:%s", tstrerror(code));
×
2440
        return terrno;
×
2441
      }
2442
    }
2443
  }
2444

2445
  return code;
7,437,408✔
2446
}
2447

2448
int taos_stmt2_bind_param_a(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx, __taos_async_fn_t fp,
×
2449
                            void *param) {
2450
  if (stmt == NULL || bindv == NULL || fp == NULL) {
×
2451
    terrno = TSDB_CODE_INVALID_PARA;
×
2452
    return terrno;
×
2453
  }
2454

2455
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2456

2457
  ThreadArgs *args = (ThreadArgs *)taosMemoryMalloc(sizeof(ThreadArgs));
×
2458
  args->stmt = stmt;
×
2459
  args->bindv = bindv;
×
2460
  args->col_idx = col_idx;
×
2461
  args->fp = fp;
×
2462
  args->param = param;
×
2463

2464
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2465
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 0) {
×
2466
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2467
    tscError("async bind param is still working, please try again later");
×
2468
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
×
2469
    return terrno;
×
2470
  }
2471
  (void)atomic_add_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2472
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2473

2474
  int code_s = taosStmt2AsyncBind(stmtAsyncBindThreadFunc, (void *)args);
×
2475
  if (code_s != TSDB_CODE_SUCCESS) {
×
2476
    terrno = code_s;
×
2477
    (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2478
    (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2479
    (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2480
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2481
    tscError("async bind failed, code:%d , %s", code_s, tstrerror(code_s));
×
2482
  }
2483

2484
  return code_s;
×
2485
}
2486

2487
int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows) {
7,435,436✔
2488
  if (stmt == NULL) {
7,435,436✔
2489
    tscError("NULL parameter for %s", __FUNCTION__);
×
2490
    terrno = TSDB_CODE_INVALID_PARA;
×
2491
    return terrno;
×
2492
  }
2493

2494
  return stmtExec2(stmt, affected_rows);
7,435,436✔
2495
}
2496

2497
int taos_stmt2_close(TAOS_STMT2 *stmt) {
19,954✔
2498
  if (stmt == NULL) {
19,954✔
2499
    tscError("NULL parameter for %s", __FUNCTION__);
×
2500
    terrno = TSDB_CODE_INVALID_PARA;
×
2501
    return terrno;
×
2502
  }
2503

2504
  return stmtClose2(stmt);
19,954✔
2505
}
2506

2507
int taos_stmt2_is_insert(TAOS_STMT2 *stmt, int *insert) {
956✔
2508
  if (stmt == NULL || insert == NULL) {
956✔
2509
    tscError("NULL parameter for %s", __FUNCTION__);
×
2510
    terrno = TSDB_CODE_INVALID_PARA;
×
2511
    return terrno;
×
2512
  }
2513
  *insert = stmt2IsInsert(stmt);
956✔
2514
  return TSDB_CODE_SUCCESS;
956✔
2515
}
2516

2517
int taos_stmt2_get_fields(TAOS_STMT2 *stmt, int *count, TAOS_FIELD_ALL **fields) {
710✔
2518
  if (stmt == NULL || count == NULL) {
710✔
2519
    tscError("NULL parameter for %s", __FUNCTION__);
×
2520
    terrno = TSDB_CODE_INVALID_PARA;
×
2521
    return terrno;
×
2522
  }
2523

2524
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
710✔
2525
  if (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type ||
710✔
2526
      (pStmt->sql.type == 0 && stmt2IsInsert(stmt))) {
710✔
2527
    return stmtGetStbColFields2(stmt, count, fields);
710✔
2528
  }
2529
  if (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt))) {
×
2530
    return stmtGetParamNum2(stmt, count);
×
2531
  }
2532

2533
  tscError("Invalid sql for stmt %s", pStmt->sql.sqlStr);
×
2534
  return TSDB_CODE_PAR_SYNTAX_ERROR;
×
2535
}
2536

2537
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_ALL *fields) {
710✔
2538
  (void)stmt;
2539
  if (!fields) return;
710✔
2540
  taosMemoryFree(fields);
710✔
2541
}
2542

2543
TAOS_RES *taos_stmt2_result(TAOS_STMT2 *stmt) {
246✔
2544
  if (stmt == NULL) {
246✔
2545
    tscError("NULL parameter for %s", __FUNCTION__);
×
2546
    terrno = TSDB_CODE_INVALID_PARA;
×
2547
    return NULL;
×
2548
  }
2549

2550
  return stmtUseResult2(stmt);
246✔
2551
}
2552

2553
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmtErrstr2(stmt); }
×
2554

2555
int taos_set_conn_mode(TAOS *taos, int mode, int value) {
3,421✔
2556
  if (taos == NULL) {
3,421✔
2557
    terrno = TSDB_CODE_INVALID_PARA;
×
2558
    return terrno;
×
2559
  }
2560

2561
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
3,421✔
2562
  if (NULL == pObj) {
3,421✔
2563
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2564
    tscError("invalid parameter for %s", __func__);
×
2565
    return terrno;
×
2566
  }
2567
  switch (mode) {
3,421✔
2568
    case TAOS_CONN_MODE_BI:
3,421✔
2569
      atomic_store_8(&pObj->biMode, value);
3,421✔
2570
      break;
3,421✔
2571
    default:
×
2572
      tscError("not supported mode.");
×
2573
      return TSDB_CODE_INVALID_PARA;
×
2574
  }
2575
  return 0;
3,421✔
2576
}
2577

2578
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