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

taosdata / TDengine / #4869

26 Nov 2025 05:46AM UTC coverage: 64.539% (-0.09%) from 64.629%
#4869

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

771 of 945 new or added lines in 33 files covered. (81.59%)

3214 existing lines in 124 files now uncovered.

158203 of 245129 relevant lines covered (64.54%)

113224023.06 hits per line

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

47.98
/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, ...) {
638,363✔
46
  if (arg == NULL) {
638,363✔
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,460,363✔
52
    if (i % 1000 == 0) {
822,000✔
53
      (void)sched_yield();
822✔
54
    }
55
  }
56

57
  int ret = taos_options_imp(option, (const char *)arg);
638,363✔
58
  atomic_store_32(&lock, 0);
638,363✔
59
  return ret;
638,363✔
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,741,568✔
69
  pTimezoneMap = taosHashInit(0, MurmurHash3_32, false, HASH_ENTRY_LOCK);
1,741,568✔
70
  if (pTimezoneMap == NULL) {
1,741,568✔
71
    return terrno;
×
72
  }
73
  taosHashSetFreeFp(pTimezoneMap, freeTz);
1,741,568✔
74

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

82
void tzCleanup() {
1,741,848✔
83
  taosHashCleanup(pTimezoneMap);
1,741,848✔
84
  taosHashCleanup(pTimezoneNameMap);
1,741,848✔
85
}
1,741,848✔
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,745,553✔
244
  tscInfo("start to cleanup client environment");
1,745,553✔
245
  if (atomic_val_compare_exchange_32(&sentinel, TSC_VAR_NOT_RELEASE, TSC_VAR_RELEASED) != TSC_VAR_NOT_RELEASE) {
1,745,553✔
246
    return;
3,705✔
247
  }
248

249
  monitorClose();
1,741,848✔
250
  tscStopCrashReport();
1,741,848✔
251

252
  hbMgrCleanUp();
1,741,848✔
253

254
  catalogDestroy();
1,741,848✔
255
  schedulerDestroy();
1,741,848✔
256

257
  fmFuncMgtDestroy();
1,741,848✔
258
  qCleanupKeywordsTable();
1,741,848✔
259

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

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

269
  id = clientConnRefPool;
1,741,848✔
270
  clientConnRefPool = -1;
1,741,848✔
271
  taosCloseRef(id);
1,741,848✔
272

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

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

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

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

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

307
TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) {
3,654,482✔
308
  tscInfo("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
3,654,482✔
309
  if (user == NULL) {
3,655,310✔
310
    user = TSDB_DEFAULT_USER;
409,725✔
311
  }
312

313
  if (pass == NULL) {
3,655,310✔
314
    pass = TSDB_DEFAULT_PASS;
409,724✔
315
  }
316

317
  STscObj *pObj = NULL;
3,655,310✔
318
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
3,656,514✔
319
  if (TSDB_CODE_SUCCESS == code) {
3,656,560✔
320
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
3,644,042✔
321
    if (NULL == rid) {
3,643,741✔
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,643,741✔
326
    return (TAOS *)rid;
3,644,144✔
327
  } else {
328
    terrno = code;
12,518✔
329
  }
330

331
  return NULL;
12,518✔
332
}
333

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

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

347
  switch (type) {
18,340✔
348
    case TAOS_NOTIFY_PASSVER: {
5,240✔
349
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
5,240✔
350
      pObj->passInfo.fp = fp;
5,240✔
351
      pObj->passInfo.param = param;
5,240✔
352
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
5,240✔
353
      break;
5,240✔
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,100✔
363
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
13,100✔
364
      pObj->userDroppedInfo.fp = fp;
13,100✔
365
      pObj->userDroppedInfo.param = param;
13,100✔
366
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
13,100✔
367
      break;
13,100✔
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,340✔
377
  return 0;
18,340✔
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,742,285✔
643
  if (taos == NULL) {
3,742,285✔
644
    return;
×
645
  }
646

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

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

655
void taos_close(TAOS *taos) {
3,643,962✔
656
  if (taos == NULL) {
3,643,962✔
657
    return;
438✔
658
  }
659

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

666
  taos_close_internal(pObj);
3,643,524✔
667
  releaseTscObj(*(int64_t *)taos);
3,643,524✔
668
  taosMemoryFree(taos);
3,643,524✔
669
}
670

671
int taos_errno(TAOS_RES *res) {
829,688,691✔
672
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
829,688,691✔
673
    return terrno;
33,351✔
674
  }
675

676
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
829,655,769✔
677
    return 0;
328,619✔
678
  }
679

680
  return ((SRequestObj *)res)->code;
829,327,470✔
681
}
682

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

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

692
  SRequestObj *pRequest = (SRequestObj *)res;
21,835,850✔
693
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
21,835,850✔
694
    return pRequest->msgBuf;
17,720,340✔
695
  } else {
696
    return (const char *)tstrerror(pRequest->code);
4,115,510✔
697
  }
698
}
699

700
void taos_free_result(TAOS_RES *res) {
653,928,322✔
701
  if (NULL == res) {
653,928,322✔
702
    return;
1,773,587✔
703
  }
704

705
  tscTrace("res:%p, will be freed", res);
652,154,735✔
706

707
  if (TD_RES_QUERY(res)) {
652,154,893✔
708
    SRequestObj *pRequest = (SRequestObj *)res;
648,889,024✔
709
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
648,889,024✔
710
    destroyRequest(pRequest);
648,891,643✔
711
    return;
648,891,428✔
712
  }
713

714
  SMqRspObj *pRsp = (SMqRspObj *)res;
3,266,976✔
715
  if (TD_RES_TMQ(res)) {
3,266,976✔
716
    tDeleteMqDataRsp(&pRsp->dataRsp);
3,254,822✔
717
    doFreeReqResultInfo(&pRsp->resInfo);
3,254,402✔
718
  } else if (TD_RES_TMQ_METADATA(res)) {
11,726✔
719
    tDeleteSTaosxRsp(&pRsp->dataRsp);
468✔
720
    doFreeReqResultInfo(&pRsp->resInfo);
468✔
721
  } else if (TD_RES_TMQ_META(res)) {
11,258✔
722
    tDeleteMqMetaRsp(&pRsp->metaRsp);
10,012✔
723
  } else if (TD_RES_TMQ_BATCH_META(res)) {
1,246✔
724
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
1,246✔
725
  } else if (TD_RES_TMQ_RAW(res)) {
×
726
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
727
  }
728
  taosMemoryFree(pRsp);
3,266,128✔
729
}
730

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

736
  int64_t  rid = *(int64_t *)taos;
292✔
737
  STscObj *pTscObj = acquireTscObj(rid);
292✔
738
  if (pTscObj) {
292✔
739
    stopAllRequests(pTscObj->pRequests);
292✔
740
  }
741
  releaseTscObj(rid);
292✔
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,607,706,880✔
756
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,607,706,880✔
757
    return NULL;
2,546,820✔
758
  }
759

760
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
1,605,460,338✔
761
  return pResInfo->userFields;
1,605,460,338✔
762
}
763

764
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
647,162,536✔
765
TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqid) {
706✔
766
  return taosQueryImplWithReqid(taos, sql, false, reqid);
706✔
767
}
768

769
TAOS_FIELD_E *taos_fetch_fields_e(TAOS_RES *res) {
1,028✔
770
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,028✔
771
    return NULL;
×
772
  }
773
  SReqResultInfo* pResInfo = tscGetCurResInfo(res);
1,028✔
774
  return pResInfo->fields;
1,028✔
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,761,041,523✔
784
    if (pRequest->killed) {
1,761,041,523✔
785
      tscInfo("query has been killed, can not fetch more row.");
×
786
      pRequest->code = TSDB_CODE_TSC_QUERY_KILLED;
×
787
      return NULL;
×
788
    }
789

790
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
1,760,992,453✔
791
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
1,761,070,541✔
792
      return NULL;
×
793
    }
794

795
    if (pRequest->inCallback) {
1,760,826,688✔
796
      tscError("can not call taos_fetch_row before query callback ends.");
155✔
797
      terrno = TSDB_CODE_TSC_INVALID_OPERATION;
155✔
798
      return NULL;
155✔
799
    }
800

801
    return doAsyncFetchRows(pRequest, true, true);
1,761,012,786✔
802
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,502,091,571✔
803
    SMqRspObj      *msg = ((SMqRspObj *)res);
1,502,132,530✔
804
    SReqResultInfo *pResultInfo = NULL;
1,502,132,530✔
805
    if (msg->resIter == -1) {
1,502,244,280✔
806
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
2,881,697✔
807
        return NULL;
×
808
      }
809
    } else {
810
      pResultInfo = tmqGetCurResInfo(res);
1,499,456,495✔
811
    }
812

813
    if (pResultInfo->current < pResultInfo->numOfRows) {
1,502,338,192✔
814
      doSetOneRowPtr(pResultInfo);
1,471,625,941✔
815
      pResultInfo->current += 1;
1,471,546,125✔
816
      return pResultInfo->row;
1,471,569,386✔
817
    } else {
818
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
30,729,186✔
819
        return NULL;
2,881,702✔
820
      }
821

822
      doSetOneRowPtr(pResultInfo);
27,847,064✔
823
      pResultInfo->current += 1;
27,847,484✔
824
      return pResultInfo->row;
27,847,484✔
825
    }
826
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
827
    return NULL;
×
828
  } else {
829
    tscError("invalid result passed to taos_fetch_row");
×
830
    terrno = TSDB_CODE_TMQ_INVALID_DATA;
×
831
    return NULL;
×
832
  }
833
}
834

835
int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
1,502,115,394✔
836
  return taos_print_row_with_size(str, INT32_MAX, row, fields, num_fields);
1,502,115,394✔
837
}
838
int taos_print_row_with_size(char *str, uint32_t size, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
1,502,055,320✔
839
  int32_t len = 0;
1,502,055,320✔
840
  for (int i = 0; i < num_fields; ++i) {
2,147,483,647✔
841
    if (i > 0 && len < size - 1) {
2,147,483,647✔
842
      str[len++] = ' ';
2,147,483,647✔
843
    }
844

845
    if (row[i] == NULL) {
2,147,483,647✔
846
      len += tsnprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
30,483,806✔
847
      continue;
30,483,806✔
848
    }
849

850
    switch (fields[i].type) {
2,147,483,647✔
851
      case TSDB_DATA_TYPE_TINYINT:
78,611,159✔
852
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
78,611,159✔
853
        break;
78,249,077✔
854

855
      case TSDB_DATA_TYPE_UTINYINT:
1,092,200✔
856
        len += tsnprintf(str + len, size - len, "%u", *((uint8_t *)row[i]));
1,092,200✔
857
        break;
1,092,200✔
858

859
      case TSDB_DATA_TYPE_SMALLINT:
1,092,200✔
860
        len += tsnprintf(str + len, size - len, "%d", *((int16_t *)row[i]));
1,092,200✔
861
        break;
1,092,200✔
862

863
      case TSDB_DATA_TYPE_USMALLINT:
1,092,200✔
864
        len += tsnprintf(str + len, size - len, "%u", *((uint16_t *)row[i]));
1,092,200✔
865
        break;
1,092,200✔
866

867
      case TSDB_DATA_TYPE_INT:
1,542,114,680✔
868
        len += tsnprintf(str + len, size - len, "%d", *((int32_t *)row[i]));
1,542,114,680✔
869
        break;
1,542,116,969✔
870

871
      case TSDB_DATA_TYPE_UINT:
1,092,200✔
872
        len += tsnprintf(str + len, size - len, "%u", *((uint32_t *)row[i]));
1,092,200✔
873
        break;
1,092,200✔
874

875
      case TSDB_DATA_TYPE_BIGINT:
1,185,133,757✔
876
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
1,185,133,757✔
877
        break;
1,185,131,862✔
878

879
      case TSDB_DATA_TYPE_UBIGINT:
1,092,200✔
880
        len += tsnprintf(str + len, size - len, "%" PRIu64, *((uint64_t *)row[i]));
1,092,200✔
881
        break;
1,092,200✔
882

883
      case TSDB_DATA_TYPE_FLOAT: {
155,939,958✔
884
        float fv = 0;
155,939,958✔
885
        fv = GET_FLOAT_VAL(row[i]);
155,939,958✔
886
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
156,200,771✔
887
      } break;
155,250,241✔
888

889
      case TSDB_DATA_TYPE_DOUBLE: {
791,532,612✔
890
        double dv = 0;
791,532,612✔
891
        dv = GET_DOUBLE_VAL(row[i]);
791,532,612✔
892
        len += snprintf(str + len, size - len, "%.*g", DBL_DIG, dv);
791,532,612✔
893
      } break;
791,528,547✔
894

895
      case TSDB_DATA_TYPE_VARBINARY: {
1,092,542✔
896
        void    *data = NULL;
1,092,542✔
897
        uint32_t tmp = 0;
1,092,542✔
898
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
1,092,542✔
899
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
1,092,542✔
900
          break;
×
901
        }
902
        uint32_t copyLen = TMIN(size - len - 1, tmp);
1,092,285✔
903
        (void)memcpy(str + len, data, copyLen);
1,092,285✔
904
        len += copyLen;
1,092,542✔
905
        taosMemoryFree(data);
1,092,542✔
906
      } break;
1,092,542✔
907
      case TSDB_DATA_TYPE_BINARY:
1,515,514,143✔
908
      case TSDB_DATA_TYPE_NCHAR:
909
      case TSDB_DATA_TYPE_GEOMETRY: {
910
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
1,515,514,143✔
911
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
1,515,564,083✔
912
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
406,335,148✔
913
          if (charLen > fields[i].bytes || charLen < 0) {
1,110,365,361✔
914
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
26,133✔
915
            break;
×
916
          }
917
        } else {
918
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
405,243,284✔
919
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
×
920
            break;
×
921
          }
922
        }
923

924
        uint32_t copyLen = TMIN(size - len - 1, charLen);
1,515,587,691✔
925
        (void)memcpy(str + len, row[i], copyLen);
1,515,587,691✔
926
        len += copyLen;
1,515,479,961✔
927
      } break;
1,515,479,961✔
928
      case TSDB_DATA_TYPE_BLOB:
×
929
      case TSDB_DATA_TYPE_MEDIUMBLOB: {
930
        void    *data = NULL;
×
931
        uint32_t tmp = 0;
×
932
        int32_t  charLen = blobDataLen((char *)row[i] - BLOBSTR_HEADER_SIZE);
×
933
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
×
934
          break;
×
935
        }
936

937
        uint32_t copyLen = TMIN(size - len - 1, tmp);
×
938
        (void)memcpy(str + len, data, copyLen);
×
939
        len += copyLen;
×
940

941
        taosMemoryFree(data);
×
942
      } break;
×
943

944
      case TSDB_DATA_TYPE_TIMESTAMP:
1,934,547,703✔
945
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
1,934,547,703✔
946
        break;
1,934,870,450✔
947

948
      case TSDB_DATA_TYPE_BOOL:
1,092,200✔
949
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
1,092,200✔
950
        break;
8,400✔
951
      case TSDB_DATA_TYPE_DECIMAL64:
×
952
      case TSDB_DATA_TYPE_DECIMAL: {
953
        uint32_t decimalLen = strlen(row[i]);
×
954
        uint32_t copyLen = TMIN(size - len - 1, decimalLen);
×
955
        (void)memcpy(str + len, row[i], copyLen);
×
956
        len += copyLen;
×
957
      } break;
×
958
      default:
×
959
        break;
×
960
    }
961

962
    if (len >= size - 1) {
2,147,483,647✔
963
      break;
×
964
    }
965
  }
966
  if (len < size) {
1,500,672,664✔
967
    str[len] = 0;
1,502,770,239✔
968
  }
969

970
  return len;
1,502,830,204✔
971
}
972

973
int *taos_fetch_lengths(TAOS_RES *res) {
2,147,483,647✔
974
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,147,483,647✔
975
    return NULL;
×
976
  }
977

978
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
979
  return pResInfo->length;
2,147,483,647✔
980
}
981

982
TAOS_ROW *taos_result_block(TAOS_RES *res) {
×
983
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
984
    terrno = TSDB_CODE_INVALID_PARA;
×
985
    return NULL;
×
986
  }
987

988
  if (taos_is_update_query(res)) {
×
989
    return NULL;
×
990
  }
991

992
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
993
  return &pResInfo->row;
×
994
}
995

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

1047
const char *taos_get_client_info() { return td_version; }
1,213,365✔
1048

1049
// return int32_t
1050
int taos_affected_rows(TAOS_RES *res) {
504,749,248✔
1051
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
504,749,248✔
1052
      TD_RES_TMQ_BATCH_META(res)) {
504,752,879✔
1053
    return 0;
×
1054
  }
1055

1056
  SRequestObj    *pRequest = (SRequestObj *)res;
504,753,328✔
1057
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
504,753,328✔
1058
  return (int)pResInfo->numOfRows;
504,752,459✔
1059
}
1060

1061
// return int64_t
1062
int64_t taos_affected_rows64(TAOS_RES *res) {
1,273,624✔
1063
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
1,273,624✔
1064
      TD_RES_TMQ_BATCH_META(res)) {
1,273,624✔
1065
    return 0;
×
1066
  }
1067

1068
  SRequestObj    *pRequest = (SRequestObj *)res;
1,273,624✔
1069
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
1,273,624✔
1070
  return pResInfo->numOfRows;
1,273,624✔
1071
}
1072

1073
int taos_result_precision(TAOS_RES *res) {
1,524,443,127✔
1074
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,524,443,127✔
1075
    return TSDB_TIME_PRECISION_MILLI;
×
1076
  }
1077

1078
  if (TD_RES_QUERY(res)) {
1,524,443,218✔
1079
    SRequestObj *pRequest = (SRequestObj *)res;
97,159,082✔
1080
    return pRequest->body.resInfo.precision;
97,159,082✔
1081
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,427,307,721✔
1082
    SReqResultInfo *info = tmqGetCurResInfo(res);
1,427,306,355✔
1083
    return info->precision;
1,427,306,355✔
1084
  }
1085
  return TSDB_TIME_PRECISION_MILLI;
×
1086
}
1087

1088
int taos_select_db(TAOS *taos, const char *db) {
163,665✔
1089
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
163,665✔
1090
  if (pObj == NULL) {
163,665✔
1091
    releaseTscObj(*(int64_t *)taos);
×
1092
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1093
    return TSDB_CODE_TSC_DISCONNECTED;
×
1094
  }
1095

1096
  if (db == NULL || strlen(db) == 0) {
163,665✔
1097
    releaseTscObj(*(int64_t *)taos);
×
1098
    tscError("invalid parameter for %s", db == NULL ? "db is NULL" : "db is empty");
×
1099
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1100
    return terrno;
×
1101
  }
1102

1103
  char sql[256] = {0};
163,665✔
1104
  (void)snprintf(sql, tListLen(sql), "use %s", db);
163,387✔
1105

1106
  TAOS_RES *pRequest = taos_query(taos, sql);
163,387✔
1107
  int32_t   code = taos_errno(pRequest);
163,566✔
1108

1109
  taos_free_result(pRequest);
163,665✔
1110
  releaseTscObj(*(int64_t *)taos);
163,665✔
1111
  return code;
163,665✔
1112
}
1113

1114
void taos_stop_query(TAOS_RES *res) {
653,621,587✔
1115
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
653,621,587✔
1116
      TD_RES_TMQ_BATCH_META(res)) {
653,621,832✔
1117
    return;
100✔
1118
  }
1119

1120
  stopAllQueries((SRequestObj *)res);
653,618,621✔
1121
}
1122

1123
bool taos_is_null(TAOS_RES *res, int32_t row, int32_t col) {
×
1124
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1125
    return true;
×
1126
  }
1127
  SReqResultInfo *pResultInfo = tscGetCurResInfo(res);
×
1128
  if (col >= pResultInfo->numOfCols || col < 0 || row >= pResultInfo->numOfRows || row < 0) {
×
1129
    return true;
×
1130
  }
1131

1132
  SResultColumn *pCol = &pResultInfo->pCol[col];
×
1133
  if (IS_VAR_DATA_TYPE(pResultInfo->fields[col].type)) {
×
1134
    return (pCol->offset[row] == -1);
×
1135
  } else {
1136
    return colDataIsNull_f(pCol, row);
×
1137
  }
1138
}
1139

1140
bool taos_is_update_query(TAOS_RES *res) { return taos_num_fields(res) == 0; }
8,475,966✔
1141

1142
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
176,356,192✔
1143
  int32_t numOfRows = 0;
176,356,192✔
1144
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
176,356,192✔
1145
  return numOfRows;
176,356,192✔
1146
}
1147

1148
int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) {
176,356,192✔
1149
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
176,356,192✔
1150
    return 0;
×
1151
  }
1152

1153
  if (TD_RES_QUERY(res)) {
176,356,192✔
1154
    SRequestObj *pRequest = (SRequestObj *)res;
169,843,058✔
1155

1156
    (*rows) = NULL;
169,843,058✔
1157
    (*numOfRows) = 0;
169,843,058✔
1158

1159
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
169,843,058✔
1160
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
169,692,203✔
1161
      return pRequest->code;
787,453✔
1162
    }
1163

1164
    (void)doAsyncFetchRows(pRequest, false, true);
169,055,605✔
1165

1166
    // TODO refactor
1167
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
169,055,605✔
1168
    pResultInfo->current = pResultInfo->numOfRows;
169,055,605✔
1169

1170
    (*rows) = pResultInfo->row;
169,055,605✔
1171
    (*numOfRows) = pResultInfo->numOfRows;
169,055,605✔
1172
    return pRequest->code;
169,055,605✔
1173
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
6,513,134✔
1174
    SReqResultInfo *pResultInfo = NULL;
6,513,134✔
1175
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
6,513,134✔
1176
    if (code != 0) return code;
6,513,134✔
1177

1178
    pResultInfo->current = pResultInfo->numOfRows;
6,191,742✔
1179
    (*rows) = pResultInfo->row;
6,191,742✔
1180
    (*numOfRows) = pResultInfo->numOfRows;
6,191,742✔
1181
    return 0;
6,191,742✔
1182
  } else {
1183
    tscError("taos_fetch_block_s invalid res type");
×
1184
    return TSDB_CODE_TMQ_INVALID_DATA;
×
1185
  }
1186
}
1187

1188
int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) {
2,474,600✔
1189
  *numOfRows = 0;
2,474,600✔
1190
  *pData = NULL;
2,474,600✔
1191

1192
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,474,600✔
1193
    return 0;
×
1194
  }
1195

1196
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,474,600✔
1197
    SReqResultInfo *pResultInfo = NULL;
2,406,506✔
1198
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
2,406,506✔
1199
    if (code != 0) {
2,406,506✔
1200
      (*numOfRows) = 0;
41,313✔
1201
      return 0;
41,313✔
1202
    }
1203

1204
    pResultInfo->current = pResultInfo->numOfRows;
2,365,193✔
1205
    (*numOfRows) = pResultInfo->numOfRows;
2,365,193✔
1206
    (*pData) = (void *)pResultInfo->pData;
2,365,193✔
1207
    return 0;
2,365,193✔
1208
  }
1209

1210
  SRequestObj *pRequest = (SRequestObj *)res;
68,094✔
1211

1212
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
68,094✔
1213
      pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
68,094✔
1214
    return pRequest->code;
×
1215
  }
1216

1217
  (void)doAsyncFetchRows(pRequest, false, false);
68,094✔
1218

1219
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
68,094✔
1220

1221
  pResultInfo->current = pResultInfo->numOfRows;
68,094✔
1222
  (*numOfRows) = pResultInfo->numOfRows;
68,094✔
1223
  (*pData) = (void *)pResultInfo->pData;
68,094✔
1224

1225
  return pRequest->code;
68,094✔
1226
}
1227

1228
int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) {
121,342,533✔
1229
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
121,342,533✔
1230
    return 0;
×
1231
  }
1232

1233
  int32_t numOfFields = taos_num_fields(res);
121,342,533✔
1234
  if (columnIndex < 0 || columnIndex >= numOfFields || numOfFields == 0) {
121,342,533✔
1235
    return 0;
×
1236
  }
1237

1238
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
121,342,533✔
1239
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
121,342,533✔
1240
  if (!IS_VAR_DATA_TYPE(pField->type)) {
121,342,533✔
1241
    return 0;
×
1242
  }
1243

1244
  return pResInfo->pCol[columnIndex].offset;
121,342,328✔
1245
}
1246

1247
int taos_is_null_by_column(TAOS_RES *res, int columnIndex, bool result[], int *rows) {
329,860,577✔
1248
  if (res == NULL || result == NULL || rows == NULL || *rows <= 0 || columnIndex < 0 || TD_RES_TMQ_META(res) ||
329,860,577✔
1249
      TD_RES_TMQ_RAW(res) || TD_RES_TMQ_BATCH_META(res)) {
329,860,577✔
1250
    return TSDB_CODE_INVALID_PARA;
×
1251
  }
1252

1253
  int32_t numOfFields = taos_num_fields(res);
329,860,577✔
1254
  if (columnIndex >= numOfFields || numOfFields == 0) {
329,860,577✔
1255
    return TSDB_CODE_INVALID_PARA;
×
1256
  }
1257

1258
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
329,860,577✔
1259
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
329,860,577✔
1260
  SResultColumn  *pCol = &pResInfo->pCol[columnIndex];
329,860,577✔
1261

1262
  if (*rows > pResInfo->numOfRows) {
329,860,368✔
1263
    *rows = pResInfo->numOfRows;
×
1264
  }
1265
  if (IS_VAR_DATA_TYPE(pField->type)) {
329,860,368✔
1266
    for (int i = 0; i < *rows; i++) {
×
1267
      if (pCol->offset[i] == -1) {
×
1268
        result[i] = true;
×
1269
      } else {
1270
        result[i] = false;
×
1271
      }
1272
    }
1273
  } else {
1274
    for (int i = 0; i < *rows; i++) {
2,147,483,647✔
1275
      if (colDataIsNull_f(pCol, i)) {
2,147,483,647✔
1276
        result[i] = true;
2,147,483,647✔
1277
      } else {
1278
        result[i] = false;
2,147,483,647✔
1279
      }
1280
    }
1281
  }
1282
  return 0;
329,860,577✔
1283
}
1284

1285
int taos_validate_sql(TAOS *taos, const char *sql) {
×
1286
  TAOS_RES *pObj = taosQueryImpl(taos, sql, true, TD_REQ_FROM_APP);
×
1287

1288
  int code = taos_errno(pObj);
×
1289

1290
  taos_free_result(pObj);
×
1291
  return code;
×
1292
}
1293

1294
void taos_reset_current_db(TAOS *taos) {
×
1295
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
1296
  if (pTscObj == NULL) {
×
1297
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1298
    return;
×
1299
  }
1300

1301
  resetConnectDB(pTscObj);
×
1302

1303
  releaseTscObj(*(int64_t *)taos);
×
1304
}
1305

1306
const char *taos_get_server_info(TAOS *taos) {
38,207✔
1307
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
38,207✔
1308
  if (pTscObj == NULL) {
38,207✔
1309
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1310
    return NULL;
×
1311
  }
1312

1313
  releaseTscObj(*(int64_t *)taos);
38,207✔
1314

1315
  return pTscObj->sDetailVer;
38,207✔
1316
}
1317

1318
int taos_get_current_db(TAOS *taos, char *database, int len, int *required) {
592✔
1319
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
592✔
1320
  if (pTscObj == NULL) {
592✔
1321
    return TSDB_CODE_TSC_DISCONNECTED;
×
1322
  }
1323

1324
  int code = TSDB_CODE_SUCCESS;
592✔
1325
  (void)taosThreadMutexLock(&pTscObj->mutex);
592✔
1326
  if (database == NULL || len <= 0) {
592✔
1327
    if (required != NULL) *required = strlen(pTscObj->db) + 1;
296✔
1328
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
296✔
1329
  } else if (len < strlen(pTscObj->db) + 1) {
296✔
1330
    tstrncpy(database, pTscObj->db, len);
148✔
1331
    if (required) *required = strlen(pTscObj->db) + 1;
148✔
1332
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
148✔
1333
  } else {
1334
    tstrncpy(database, pTscObj->db, len);
148✔
1335
    code = 0;
148✔
1336
  }
1337
_return:
592✔
1338
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
592✔
1339
  releaseTscObj(*(int64_t *)taos);
592✔
1340
  return code;
592✔
1341
}
1342

1343
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
1,305,859,104✔
1344
  if (NULL == pWrapper) {
1,305,859,104✔
1345
    return;
656,074,139✔
1346
  }
1347
  destoryCatalogReq(pWrapper->pCatalogReq);
649,784,965✔
1348
  taosMemoryFree(pWrapper->pCatalogReq);
649,772,756✔
1349
  qDestroyParseContext(pWrapper->pParseCtx);
649,772,843✔
1350
  taosMemoryFree(pWrapper);
649,761,530✔
1351
}
1352

1353
void destroyCtxInRequest(SRequestObj *pRequest) {
2,804,901✔
1354
  schedulerFreeJob(&pRequest->body.queryJob, 0);
2,804,901✔
1355
  qDestroyQuery(pRequest->pQuery);
2,804,901✔
1356
  pRequest->pQuery = NULL;
2,804,901✔
1357
  destorySqlCallbackWrapper(pRequest->pWrapper);
2,804,901✔
1358
  pRequest->pWrapper = NULL;
2,804,901✔
1359
}
2,804,901✔
1360

1361
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
184,256,634✔
1362
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
184,256,634✔
1363
  SRequestObj         *pRequest = pWrapper->pRequest;
184,256,634✔
1364
  SQuery              *pQuery = pRequest->pQuery;
184,256,634✔
1365

1366
  qDebug("req:0x%" PRIx64 ", start to semantic analysis, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
184,256,916✔
1367

1368
  int64_t analyseStart = taosGetTimestampUs();
184,256,916✔
1369
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
184,256,916✔
1370
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
184,256,916✔
1371

1372
  if (TSDB_CODE_SUCCESS == code) {
184,256,658✔
1373
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
184,253,915✔
1374
  }
1375

1376
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
184,246,475✔
1377

1378
  if (pRequest->parseOnly) {
184,253,616✔
1379
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
306,336✔
1380
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
306,336✔
1381
  }
1382

1383
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
184,253,667✔
1384
}
184,245,177✔
1385

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

1413
    *ppTarget = pTarget;
×
1414
  }
1415

1416
  return code;
×
1417
}
1418

1419
void handleSubQueryFromAnalyse(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, SNode *pRoot) {
×
1420
  SRequestObj         *pNewRequest = NULL;
×
1421
  SSqlCallbackWrapper *pNewWrapper = NULL;
×
1422
  int32_t              code = buildPreviousRequest(pWrapper->pRequest, pWrapper->pRequest->sqlstr, &pNewRequest);
×
1423
  if (code) {
×
1424
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1425
    return;
×
1426
  }
1427

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

1450
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
184,235,818✔
1451
  SRequestObj *pRequest = pWrapper->pRequest;
184,235,818✔
1452
  SQuery      *pQuery = pRequest->pQuery;
184,245,727✔
1453

1454
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
184,242,826✔
1455
    SNode *prevRoot = pQuery->pPrevRoot;
×
1456
    pQuery->pPrevRoot = NULL;
×
1457
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
×
1458
    return;
×
1459
  }
1460

1461
  if (code == TSDB_CODE_SUCCESS) {
184,249,196✔
1462
    pRequest->stableQuery = pQuery->stableQuery;
168,815,311✔
1463
    if (pQuery->pRoot) {
168,821,534✔
1464
      pRequest->stmtType = pQuery->pRoot->type;
168,810,680✔
1465
    }
1466

1467
    if (pQuery->haveResultSet) {
168,823,839✔
1468
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
92,761,386✔
1469
                              pRequest->stmtBindVersion > 0);
92,759,537✔
1470
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
92,762,778✔
1471
    }
1472
  }
1473

1474
  if (code == TSDB_CODE_SUCCESS) {
184,232,279✔
1475
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
168,812,535✔
1476
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
168,817,736✔
1477
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
168,812,939✔
1478

1479
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
168,814,398✔
1480
  } else {
1481
    destorySqlCallbackWrapper(pWrapper);
15,419,744✔
1482
    pRequest->pWrapper = NULL;
15,419,744✔
1483
    qDestroyQuery(pRequest->pQuery);
15,419,744✔
1484
    pRequest->pQuery = NULL;
15,419,744✔
1485

1486
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
15,419,744✔
1487
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
2,755,081✔
1488
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1489
      restartAsyncQuery(pRequest, code);
2,755,081✔
1490
      return;
2,755,081✔
1491
    }
1492

1493
    // return to app directly
1494
    tscError("req:0x%" PRIx64 ", error occurs, code:%s, return to user app, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
12,664,663✔
1495
             pRequest->requestId);
1496
    pRequest->code = code;
12,664,663✔
1497
    returnToUser(pRequest);
12,664,663✔
1498
  }
1499
}
1500

1501
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
194,440,135✔
1502
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
380,887,302✔
1503
                           .requestId = pWrapper->pParseCtx->requestId,
194,443,221✔
1504
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
194,448,866✔
1505
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
194,442,435✔
1506

1507
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
380,889,349✔
1508

1509
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
202,442,904✔
1510
                                &pWrapper->pRequest->body.queryJob);
194,443,758✔
1511
}
1512

1513
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code);
1514

1515
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
654,474,783✔
1516
  int32_t code = TSDB_CODE_SUCCESS;
654,474,783✔
1517
  switch (pWrapper->pRequest->pQuery->execStage) {
654,474,783✔
1518
    case QUERY_EXEC_STAGE_PARSE: {
10,204,083✔
1519
      // continue parse after get metadata
1520
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
10,204,083✔
1521
      break;
10,204,083✔
1522
    }
1523
    case QUERY_EXEC_STAGE_ANALYSE: {
184,247,939✔
1524
      // analysis after get metadata
1525
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
184,247,939✔
1526
      break;
184,246,994✔
1527
    }
1528
    case QUERY_EXEC_STAGE_SCHEDULE: {
460,038,939✔
1529
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
460,038,939✔
1530
      break;
460,036,539✔
1531
    }
1532
    default:
×
1533
      break;
×
1534
  }
1535
  return code;
654,487,261✔
1536
}
1537

1538
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code) {
10,204,083✔
1539
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
10,204,083✔
1540
  SRequestObj         *pRequest = pWrapper->pRequest;
10,204,083✔
1541
  SQuery              *pQuery = pRequest->pQuery;
10,204,083✔
1542

1543
  pRequest->metric.ctgCostUs += taosGetTimestampUs() - pRequest->metric.ctgStart;
10,204,083✔
1544
  qDebug("req:0x%" PRIx64 ", continue parse query, QID:0x%" PRIx64 ", code:%s", pRequest->self, pRequest->requestId,
10,204,083✔
1545
         tstrerror(code));
1546

1547
  if (code == TSDB_CODE_SUCCESS) {
10,204,083✔
1548
    // pWrapper->pCatalogReq->forceUpdate = false;
1549
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
10,194,048✔
1550
  }
1551

1552
  if (TSDB_CODE_SUCCESS == code) {
10,204,083✔
1553
    code = phaseAsyncQuery(pWrapper);
9,683,035✔
1554
  }
1555

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

1567
void continueInsertFromCsv(SSqlCallbackWrapper *pWrapper, SRequestObj *pRequest) {
11,789✔
1568
  int32_t code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
11,789✔
1569
  if (TSDB_CODE_SUCCESS == code) {
11,789✔
1570
    code = phaseAsyncQuery(pWrapper);
11,789✔
1571
  }
1572

1573
  if (TSDB_CODE_SUCCESS != code) {
11,789✔
1574
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
×
1575
             tstrerror(code), pWrapper->pRequest->requestId);
1576
    destorySqlCallbackWrapper(pWrapper);
×
1577
    pRequest->pWrapper = NULL;
×
1578
    terrno = code;
×
1579
    pRequest->code = code;
×
1580
    doRequestCallback(pRequest, code);
×
1581
  }
1582
}
11,789✔
1583

1584
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
141,925✔
1585
  int64_t connId = *(int64_t *)taos;
141,925✔
1586
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
141,925✔
1587
}
141,925✔
1588

1589
void taos_query_a_with_reqid(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param, int64_t reqid) {
×
1590
  int64_t connId = *(int64_t *)taos;
×
1591
  taosAsyncQueryImplWithReqid(connId, sql, fp, param, false, reqid);
×
1592
}
×
1593

1594
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
649,783,834✔
1595
  const STscObj *pTscObj = pRequest->pTscObj;
649,783,834✔
1596

1597
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
649,789,311✔
1598
  if (*pCxt == NULL) {
649,779,987✔
1599
    return terrno;
×
1600
  }
1601

1602
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
1,275,176,827✔
1603
                           .requestRid = pRequest->self,
649,786,682✔
1604
                           .acctId = pTscObj->acctId,
649,787,295✔
1605
                           .db = pRequest->pDb,
649,786,936✔
1606
                           .topicQuery = false,
1607
                           .pSql = pRequest->sqlstr,
649,790,286✔
1608
                           .sqlLen = pRequest->sqlLen,
649,788,196✔
1609
                           .pMsg = pRequest->msgBuf,
649,792,116✔
1610
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1611
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
649,790,946✔
1612
                           .pStmtCb = NULL,
1613
                           .pUser = pTscObj->user,
649,785,207✔
1614
                           .pEffectiveUser = pRequest->effectiveUser,
649,791,821✔
1615
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
649,786,589✔
1616
                           .enableSysInfo = pTscObj->sysInfo,
649,786,429✔
1617
                           .async = true,
1618
                           .svrVer = pTscObj->sVer,
649,787,853✔
1619
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
649,791,744✔
1620
                           .allocatorId = pRequest->allocatorRefId,
649,786,690✔
1621
                           .parseSqlFp = clientParseSql,
1622
                           .parseSqlParam = pWrapper,
1623
                           .setQueryFp = setQueryRequest,
1624
                           .timezone = pTscObj->optionInfo.timezone,
649,786,720✔
1625
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
649,786,541✔
1626
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
649,786,447✔
1627
  (*pCxt)->biMode = biMode;
649,779,951✔
1628
  return TSDB_CODE_SUCCESS;
649,785,392✔
1629
}
1630

1631
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
649,782,435✔
1632
  int32_t              code = TSDB_CODE_SUCCESS;
649,782,435✔
1633
  STscObj             *pTscObj = pRequest->pTscObj;
649,782,435✔
1634
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
649,788,610✔
1635
  if (pWrapper == NULL) {
649,778,504✔
1636
    code = terrno;
×
1637
  } else {
1638
    pWrapper->pRequest = pRequest;
649,778,504✔
1639
    pRequest->pWrapper = pWrapper;
649,783,927✔
1640
    *ppWrapper = pWrapper;
649,784,203✔
1641
  }
1642

1643
  if (TSDB_CODE_SUCCESS == code) {
649,782,369✔
1644
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
649,784,562✔
1645
  }
1646

1647
  if (TSDB_CODE_SUCCESS == code) {
649,784,671✔
1648
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
649,784,671✔
1649
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
649,793,114✔
1650
  }
1651

1652
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
649,787,821✔
1653
    int64_t syntaxStart = taosGetTimestampUs();
649,789,349✔
1654

1655
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
649,789,349✔
1656
    if (pWrapper->pCatalogReq == NULL) {
649,775,235✔
1657
      code = terrno;
×
1658
    } else {
1659
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
649,780,662✔
1660
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
649,784,889✔
1661
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
649,781,531✔
1662
    }
1663

1664
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
649,785,058✔
1665
  }
1666

1667
  return code;
649,790,002✔
1668
}
1669

1670
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
650,701,064✔
1671
  SSqlCallbackWrapper *pWrapper = NULL;
650,701,064✔
1672
  int32_t              code = TSDB_CODE_SUCCESS;
650,706,886✔
1673

1674
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
650,706,886✔
1675
    code = pRequest->prevCode;
917,867✔
1676
    terrno = code;
917,867✔
1677
    pRequest->code = code;
917,867✔
1678
    tscDebug("req:0x%" PRIx64 ", call sync query cb with code:%s", pRequest->self, tstrerror(code));
917,867✔
1679
    doRequestCallback(pRequest, code);
917,867✔
1680
    return;
917,867✔
1681
  }
1682

1683
  if (TSDB_CODE_SUCCESS == code) {
649,778,763✔
1684
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
649,778,027✔
1685
  }
1686

1687
  if (TSDB_CODE_SUCCESS == code) {
649,777,624✔
1688
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
644,794,025✔
1689
    code = phaseAsyncQuery(pWrapper);
644,798,294✔
1690
  }
1691

1692
  if (TSDB_CODE_SUCCESS != code) {
649,781,718✔
1693
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
4,987,483✔
1694
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
4,858,088✔
1695
               pRequest->requestId);
1696
    } else {
1697
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
129,395✔
1698
               pRequest->requestId);
1699
    }
1700

1701
    destorySqlCallbackWrapper(pWrapper);
4,987,483✔
1702
    pRequest->pWrapper = NULL;
4,987,483✔
1703
    qDestroyQuery(pRequest->pQuery);
4,987,483✔
1704
    pRequest->pQuery = NULL;
4,987,483✔
1705

1706
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
4,987,483✔
1707
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
9,819✔
1708
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1709
      code = refreshMeta(pRequest->pTscObj, pRequest);
9,819✔
1710
      if (code != 0) {
9,819✔
1711
        tscWarn("req:0x%" PRIx64 ", refresh meta failed, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
9,819✔
1712
                pRequest->requestId);
1713
      }
1714
      pRequest->prevCode = code;
9,819✔
1715
      doAsyncQuery(pRequest, true);
9,819✔
1716
      return;
9,819✔
1717
    }
1718

1719
    terrno = code;
4,977,664✔
1720
    pRequest->code = code;
4,977,664✔
1721
    doRequestCallback(pRequest, code);
4,977,664✔
1722
  }
1723
}
1724

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

1757
typedef struct SAsyncFetchParam {
1758
  SRequestObj      *pReq;
1759
  __taos_async_fn_t fp;
1760
  void             *param;
1761
} SAsyncFetchParam;
1762

1763
static int32_t doAsyncFetch(void *pParam) {
112,355,509✔
1764
  SAsyncFetchParam *param = pParam;
112,355,509✔
1765
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
112,355,509✔
1766
  taosMemoryFree(param);
112,353,777✔
1767
  return TSDB_CODE_SUCCESS;
112,354,996✔
1768
}
1769

1770
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
112,357,941✔
1771
  if (res == NULL || fp == NULL) {
112,357,941✔
1772
    tscError("taos_fetch_rows_a invalid paras");
×
1773
    return;
×
1774
  }
1775
  if (!TD_RES_QUERY(res)) {
112,358,196✔
1776
    tscError("taos_fetch_rows_a res is NULL");
×
1777
    fp(param, res, TSDB_CODE_APP_ERROR);
×
1778
    return;
×
1779
  }
1780

1781
  SRequestObj *pRequest = res;
112,358,196✔
1782
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
112,358,196✔
1783
    fp(param, res, 0);
2,940✔
1784
    return;
2,940✔
1785
  }
1786

1787
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
112,354,721✔
1788
  if (!pParam) {
112,354,477✔
1789
    fp(param, res, terrno);
×
1790
    return;
×
1791
  }
1792
  pParam->pReq = pRequest;
112,354,477✔
1793
  pParam->fp = fp;
112,354,738✔
1794
  pParam->param = param;
112,355,512✔
1795
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
112,355,001✔
1796
  if (TSDB_CODE_SUCCESS != code) {
112,355,767✔
1797
    taosMemoryFree(pParam);
×
1798
    fp(param, res, code);
×
1799
    return;
×
1800
  }
1801
}
1802

1803
void taos_fetch_raw_block_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
7,666✔
1804
  if (res == NULL || fp == NULL) {
7,666✔
1805
    tscError("taos_fetch_raw_block_a invalid paras");
×
1806
    return;
×
1807
  }
1808
  if (!TD_RES_QUERY(res)) {
7,666✔
1809
    tscError("taos_fetch_raw_block_a res is NULL");
×
1810
    return;
×
1811
  }
1812
  SRequestObj    *pRequest = res;
7,666✔
1813
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
7,666✔
1814

1815
  // set the current block is all consumed
1816
  pResultInfo->convertUcs4 = false;
7,666✔
1817

1818
  // it is a local executed query, no need to do async fetch
1819
  taos_fetch_rows_a(pRequest, fp, param);
7,666✔
1820
}
1821

1822
const void *taos_get_raw_block(TAOS_RES *res) {
4,508✔
1823
  if (res == NULL) {
4,508✔
1824
    tscError("taos_get_raw_block invalid paras");
×
1825
    return NULL;
×
1826
  }
1827
  if (!TD_RES_QUERY(res)) {
4,508✔
1828
    tscError("taos_get_raw_block res is NULL");
×
1829
    return NULL;
×
1830
  }
1831
  SRequestObj *pRequest = res;
4,508✔
1832

1833
  return pRequest->body.resInfo.pData;
4,508✔
1834
}
1835

1836
int taos_get_db_route_info(TAOS *taos, const char *db, TAOS_DB_ROUTE_INFO *dbInfo) {
×
1837
  if (NULL == taos) {
×
1838
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1839
    return terrno;
×
1840
  }
1841

1842
  if (NULL == db || NULL == dbInfo) {
×
1843
    tscError("invalid input param, db:%p, dbInfo:%p", db, dbInfo);
×
1844
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1845
    return terrno;
×
1846
  }
1847

1848
  int64_t      connId = *(int64_t *)taos;
×
1849
  SRequestObj *pRequest = NULL;
×
1850
  char        *sql = "taos_get_db_route_info";
×
1851
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1852
  if (code != TSDB_CODE_SUCCESS) {
×
1853
    terrno = code;
×
1854
    return terrno;
×
1855
  }
1856

1857
  STscObj  *pTscObj = pRequest->pTscObj;
×
1858
  SCatalog *pCtg = NULL;
×
1859
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1860
  if (code != TSDB_CODE_SUCCESS) {
×
1861
    goto _return;
×
1862
  }
1863

1864
  SRequestConnInfo conn = {
×
1865
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1866

1867
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1868

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

1872
  code = catalogGetDBVgInfo(pCtg, &conn, dbFName, dbInfo);
×
1873
  if (code) {
×
1874
    goto _return;
×
1875
  }
1876

1877
_return:
×
1878

1879
  terrno = code;
×
1880

1881
  destroyRequest(pRequest);
×
1882
  return code;
×
1883
}
1884

1885
int taos_get_table_vgId(TAOS *taos, const char *db, const char *table, int *vgId) {
×
1886
  if (NULL == taos) {
×
1887
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1888
    return terrno;
×
1889
  }
1890

1891
  if (NULL == db || NULL == table || NULL == vgId) {
×
1892
    tscError("invalid input param, db:%p, table:%p, vgId:%p", db, table, vgId);
×
1893
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1894
    return terrno;
×
1895
  }
1896

1897
  int64_t      connId = *(int64_t *)taos;
×
1898
  SRequestObj *pRequest = NULL;
×
1899
  char        *sql = "taos_get_table_vgId";
×
1900
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1901
  if (code != TSDB_CODE_SUCCESS) {
×
1902
    return terrno;
×
1903
  }
1904

1905
  pRequest->syncQuery = true;
×
1906

1907
  STscObj  *pTscObj = pRequest->pTscObj;
×
1908
  SCatalog *pCtg = NULL;
×
1909
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1910
  if (code != TSDB_CODE_SUCCESS) {
×
1911
    goto _return;
×
1912
  }
1913

1914
  SRequestConnInfo conn = {
×
1915
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1916

1917
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1918

1919
  SName tableName = {0};
×
1920
  toName(pTscObj->acctId, db, table, &tableName);
×
1921

1922
  SVgroupInfo vgInfo;
×
1923
  code = catalogGetTableHashVgroup(pCtg, &conn, &tableName, &vgInfo);
×
1924
  if (code) {
×
1925
    goto _return;
×
1926
  }
1927

1928
  *vgId = vgInfo.vgId;
×
1929

1930
_return:
×
1931

1932
  terrno = code;
×
1933

1934
  destroyRequest(pRequest);
×
1935
  return code;
×
1936
}
1937

1938
int taos_get_tables_vgId(TAOS *taos, const char *db, const char *table[], int tableNum, int *vgId) {
×
1939
  if (NULL == taos) {
×
1940
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1941
    return terrno;
×
1942
  }
1943

1944
  if (NULL == db || NULL == table || NULL == vgId || tableNum <= 0) {
×
1945
    tscError("invalid input param, db:%p, table:%p, vgId:%p, tbNum:%d", db, table, vgId, tableNum);
×
1946
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1947
    return terrno;
×
1948
  }
1949

1950
  int64_t      connId = *(int64_t *)taos;
×
1951
  SRequestObj *pRequest = NULL;
×
1952
  char        *sql = "taos_get_table_vgId";
×
1953
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1954
  if (code != TSDB_CODE_SUCCESS) {
×
1955
    return terrno;
×
1956
  }
1957

1958
  pRequest->syncQuery = true;
×
1959

1960
  STscObj  *pTscObj = pRequest->pTscObj;
×
1961
  SCatalog *pCtg = NULL;
×
1962
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1963
  if (code != TSDB_CODE_SUCCESS) {
×
1964
    goto _return;
×
1965
  }
1966

1967
  SRequestConnInfo conn = {
×
1968
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1969

1970
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1971

1972
  code = catalogGetTablesHashVgId(pCtg, &conn, pTscObj->acctId, db, table, tableNum, vgId);
×
1973
  if (code) {
×
1974
    goto _return;
×
1975
  }
1976

1977
_return:
×
1978

1979
  terrno = code;
×
1980

1981
  destroyRequest(pRequest);
×
1982
  return code;
×
1983
}
1984

1985
int taos_load_table_info(TAOS *taos, const char *tableNameList) {
1,256✔
1986
  if (NULL == taos) {
1,256✔
1987
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1988
    return terrno;
×
1989
  }
1990

1991
  int64_t       connId = *(int64_t *)taos;
1,256✔
1992
  const int32_t MAX_TABLE_NAME_LENGTH = 12 * 1024 * 1024;  // 12MB list
1,256✔
1993
  int32_t       code = 0;
1,256✔
1994
  SRequestObj  *pRequest = NULL;
1,256✔
1995
  SCatalogReq   catalogReq = {0};
1,256✔
1996

1997
  if (NULL == tableNameList) {
1,256✔
1998
    return TSDB_CODE_SUCCESS;
×
1999
  }
2000

2001
  int32_t length = (int32_t)strlen(tableNameList);
1,256✔
2002
  if (0 == length) {
1,256✔
2003
    return TSDB_CODE_SUCCESS;
×
2004
  } else if (length > MAX_TABLE_NAME_LENGTH) {
1,256✔
2005
    tscError("tableNameList too long, length:%d, maximum allowed:%d", length, MAX_TABLE_NAME_LENGTH);
×
2006
    return TSDB_CODE_TSC_INVALID_OPERATION;
×
2007
  }
2008

2009
  char *sql = "taos_load_table_info";
1,256✔
2010
  code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
1,256✔
2011
  if (code != TSDB_CODE_SUCCESS) {
1,256✔
2012
    terrno = code;
×
2013
    goto _return;
×
2014
  }
2015

2016
  pRequest->syncQuery = true;
1,256✔
2017

2018
  STscObj *pTscObj = pRequest->pTscObj;
1,256✔
2019
  code = transferTableNameList(tableNameList, pTscObj->acctId, pTscObj->db, &catalogReq.pTableMeta);
1,256✔
2020
  if (code) {
1,256✔
2021
    goto _return;
×
2022
  }
2023

2024
  SCatalog *pCtg = NULL;
1,256✔
2025
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
1,256✔
2026
  if (code != TSDB_CODE_SUCCESS) {
1,256✔
2027
    goto _return;
×
2028
  }
2029

2030
  SRequestConnInfo conn = {
1,256✔
2031
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
1,256✔
2032

2033
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
1,256✔
2034

2035
  code = catalogAsyncGetAllMeta(pCtg, &conn, &catalogReq, syncCatalogFn, pRequest->body.interParam, NULL);
1,256✔
2036
  if (code) {
1,256✔
2037
    goto _return;
×
2038
  }
2039

2040
  SSyncQueryParam *pParam = pRequest->body.interParam;
1,256✔
2041
  code = tsem_wait(&pParam->sem);
1,256✔
2042
  if (code) {
1,256✔
2043
    tscError("tsem wait failed, code:%d - %s", code, tstrerror(code));
×
2044
    goto _return;
×
2045
  }
2046
_return:
1,256✔
2047
  destoryCatalogReq(&catalogReq);
1,256✔
2048
  destroyRequest(pRequest);
1,256✔
2049
  return code;
1,256✔
2050
}
2051

2052
TAOS_STMT *taos_stmt_init(TAOS *taos) {
387,593✔
2053
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
387,593✔
2054
  if (NULL == pObj) {
387,959✔
2055
    tscError("invalid parameter for %s", __FUNCTION__);
×
2056
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2057
    return NULL;
×
2058
  }
2059

2060
  TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL);
387,959✔
2061
  if (NULL == pStmt) {
387,959✔
2062
    tscError("stmt init failed, errcode:%s", terrstr());
×
2063
  }
2064
  releaseTscObj(*(int64_t *)taos);
387,959✔
2065

2066
  return pStmt;
387,959✔
2067
}
2068

2069
TAOS_STMT *taos_stmt_init_with_reqid(TAOS *taos, int64_t reqid) {
×
2070
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
2071
  if (NULL == pObj) {
×
2072
    tscError("invalid parameter for %s", __FUNCTION__);
×
2073
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2074
    return NULL;
×
2075
  }
2076

2077
  TAOS_STMT *pStmt = stmtInit(pObj, reqid, NULL);
×
2078
  if (NULL == pStmt) {
×
2079
    tscError("stmt init failed, errcode:%s", terrstr());
×
2080
  }
2081
  releaseTscObj(*(int64_t *)taos);
×
2082

2083
  return pStmt;
×
2084
}
2085

2086
TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options) {
32,553✔
2087
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
32,553✔
2088
  if (NULL == pObj) {
32,553✔
2089
    tscError("invalid parameter for %s", __FUNCTION__);
×
2090
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2091
    return NULL;
×
2092
  }
2093

2094
  TAOS_STMT *pStmt = stmtInit(pObj, options->reqId, options);
32,553✔
2095
  if (NULL == pStmt) {
32,553✔
2096
    tscError("stmt init failed, errcode:%s", terrstr());
×
2097
  }
2098
  releaseTscObj(*(int64_t *)taos);
32,553✔
2099

2100
  return pStmt;
32,553✔
2101
}
2102

2103
int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
921,792✔
2104
  if (stmt == NULL || sql == NULL) {
921,792✔
2105
    tscError("NULL parameter for %s", __FUNCTION__);
×
2106
    terrno = TSDB_CODE_INVALID_PARA;
×
2107
    return terrno;
×
2108
  }
2109

2110
  return stmtPrepare(stmt, sql, length);
921,942✔
2111
}
2112

2113
int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags) {
19,984✔
2114
  if (stmt == NULL || name == NULL) {
19,984✔
2115
    tscError("NULL parameter for %s", __FUNCTION__);
×
2116
    terrno = TSDB_CODE_INVALID_PARA;
×
2117
    return terrno;
×
2118
  }
2119

2120
  int32_t code = stmtSetTbName(stmt, name);
19,984✔
2121
  if (code) {
19,984✔
2122
    return code;
628✔
2123
  }
2124

2125
  if (tags) {
19,356✔
2126
    return stmtSetTbTags(stmt, tags);
19,356✔
2127
  }
2128

2129
  return TSDB_CODE_SUCCESS;
×
2130
}
2131

2132
int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name) {
13,697,283✔
2133
  if (stmt == NULL || name == NULL) {
13,697,283✔
2134
    tscError("NULL parameter for %s", __FUNCTION__);
×
2135
    terrno = TSDB_CODE_INVALID_PARA;
×
2136
    return terrno;
×
2137
  }
2138

2139
  return stmtSetTbName(stmt, name);
13,704,246✔
2140
}
2141

2142
int taos_stmt_set_tags(TAOS_STMT *stmt, TAOS_MULTI_BIND *tags) {
9,798✔
2143
  if (stmt == NULL || tags == NULL) {
9,798✔
2144
    tscError("NULL parameter for %s", __FUNCTION__);
×
2145
    terrno = TSDB_CODE_INVALID_PARA;
×
2146
    return terrno;
×
2147
  }
2148

2149
  return stmtSetTbTags(stmt, tags);
9,798✔
2150
}
2151

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

2154
int taos_stmt_get_tag_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
×
2155
  if (stmt == NULL || NULL == fieldNum) {
×
2156
    tscError("NULL parameter for %s", __FUNCTION__);
×
2157
    terrno = TSDB_CODE_INVALID_PARA;
×
2158
    return terrno;
×
2159
  }
2160

2161
  return stmtGetTagFields(stmt, fieldNum, fields);
×
2162
}
2163

2164
int taos_stmt_get_col_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
×
2165
  if (stmt == NULL || NULL == fieldNum) {
×
2166
    tscError("NULL parameter for %s", __FUNCTION__);
×
2167
    terrno = TSDB_CODE_INVALID_PARA;
×
2168
    return terrno;
×
2169
  }
2170

2171
  return stmtGetColFields(stmt, fieldNum, fields);
×
2172
}
2173

2174
// let stmt to reclaim TAOS_FIELD_E that was allocated by `taos_stmt_get_tag_fields`/`taos_stmt_get_col_fields`
2175
void taos_stmt_reclaim_fields(TAOS_STMT *stmt, TAOS_FIELD_E *fields) {
×
2176
  (void)stmt;
2177
  if (!fields) return;
×
2178
  taosMemoryFree(fields);
×
2179
}
2180

2181
int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
55,427✔
2182
  if (stmt == NULL || bind == NULL) {
55,427✔
2183
    tscError("NULL parameter for %s", __FUNCTION__);
×
2184
    terrno = TSDB_CODE_INVALID_PARA;
×
2185
    return terrno;
×
2186
  }
2187

2188
  if (bind->num > 1) {
55,427✔
2189
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
3,960✔
2190
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
3,960✔
2191
    return terrno;
3,960✔
2192
  }
2193

2194
  return stmtBindBatch(stmt, bind, -1);
51,467✔
2195
}
2196

2197
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
339,741,091✔
2198
  if (stmt == NULL || bind == NULL) {
339,741,091✔
2199
    tscError("NULL parameter for %s", __FUNCTION__);
×
2200
    terrno = TSDB_CODE_INVALID_PARA;
×
2201
    return terrno;
×
2202
  }
2203

2204
  if (bind->num <= 0 || bind->num > INT16_MAX) {
344,400,913✔
UNCOV
2205
    tscError("invalid bind num %d", bind->num);
×
UNCOV
2206
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2207
    return terrno;
×
2208
  }
2209

2210
  int32_t insert = 0;
347,285,081✔
2211
  int32_t code = stmtIsInsert(stmt, &insert);
346,640,267✔
2212
  if (TSDB_CODE_SUCCESS != code) {
342,532,349✔
2213
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2214
    return code;
×
2215
  }
2216
  if (0 == insert && bind->num > 1) {
342,532,349✔
2217
    tscError("only one row data allowed for query");
×
2218
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2219
    return terrno;
×
2220
  }
2221

2222
  return stmtBindBatch(stmt, bind, -1);
342,532,349✔
2223
}
2224

2225
int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind, int colIdx) {
54,040✔
2226
  if (stmt == NULL || bind == NULL) {
54,040✔
2227
    tscError("NULL parameter for %s", __FUNCTION__);
×
2228
    terrno = TSDB_CODE_INVALID_PARA;
×
2229
    return terrno;
×
2230
  }
2231

2232
  if (colIdx < 0) {
54,040✔
2233
    tscError("invalid bind column idx %d", colIdx);
×
2234
    terrno = TSDB_CODE_INVALID_PARA;
×
2235
    return terrno;
×
2236
  }
2237

2238
  int32_t insert = 0;
54,040✔
2239
  int32_t code = stmtIsInsert(stmt, &insert);
54,040✔
2240
  if (TSDB_CODE_SUCCESS != code) {
54,040✔
2241
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2242
    return code;
×
2243
  }
2244
  if (0 == insert && bind->num > 1) {
54,040✔
2245
    tscError("only one row data allowed for query");
×
2246
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2247
    return terrno;
×
2248
  }
2249

2250
  return stmtBindBatch(stmt, bind, colIdx);
54,040✔
2251
}
2252

2253
int taos_stmt_add_batch(TAOS_STMT *stmt) {
328,382,393✔
2254
  if (stmt == NULL) {
328,382,393✔
2255
    tscError("NULL parameter for %s", __FUNCTION__);
×
2256
    terrno = TSDB_CODE_INVALID_PARA;
×
2257
    return terrno;
×
2258
  }
2259

2260
  return stmtAddBatch(stmt);
328,382,393✔
2261
}
2262

2263
int taos_stmt_execute(TAOS_STMT *stmt) {
4,663,034✔
2264
  if (stmt == NULL) {
4,663,034✔
2265
    tscError("NULL parameter for %s", __FUNCTION__);
×
2266
    terrno = TSDB_CODE_INVALID_PARA;
×
2267
    return terrno;
×
2268
  }
2269

2270
  return stmtExec(stmt);
4,663,034✔
2271
}
2272

2273
int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
×
2274
  if (stmt == NULL || insert == NULL) {
×
2275
    tscError("NULL parameter for %s", __FUNCTION__);
×
2276
    terrno = TSDB_CODE_INVALID_PARA;
×
2277
    return terrno;
×
2278
  }
2279

2280
  return stmtIsInsert(stmt, insert);
×
2281
}
2282

2283
int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) {
×
2284
  if (stmt == NULL || nums == NULL) {
×
2285
    tscError("NULL parameter for %s", __FUNCTION__);
×
2286
    terrno = TSDB_CODE_INVALID_PARA;
×
2287
    return terrno;
×
2288
  }
2289

2290
  return stmtGetParamNum(stmt, nums);
×
2291
}
2292

2293
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
×
2294
  if (stmt == NULL || type == NULL || NULL == bytes || idx < 0) {
×
2295
    tscError("invalid parameter for %s", __FUNCTION__);
×
2296
    terrno = TSDB_CODE_INVALID_PARA;
×
2297
    return terrno;
×
2298
  }
2299

2300
  return stmtGetParam(stmt, idx, type, bytes);
×
2301
}
2302

2303
TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt) {
10,390✔
2304
  if (stmt == NULL) {
10,390✔
2305
    tscError("NULL parameter for %s", __FUNCTION__);
×
2306
    terrno = TSDB_CODE_INVALID_PARA;
×
2307
    return NULL;
×
2308
  }
2309

2310
  return stmtUseResult(stmt);
10,390✔
2311
}
2312

2313
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
10,435✔
2314

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

2322
  return stmtAffectedRows(stmt);
7,104✔
2323
}
2324

2325
int taos_stmt_affected_rows_once(TAOS_STMT *stmt) {
2,560✔
2326
  if (stmt == NULL) {
2,560✔
2327
    tscError("NULL parameter for %s", __FUNCTION__);
×
2328
    terrno = TSDB_CODE_INVALID_PARA;
×
2329
    return 0;
×
2330
  }
2331

2332
  return stmtAffectedRowsOnce(stmt);
2,560✔
2333
}
2334

2335
int taos_stmt_close(TAOS_STMT *stmt) {
419,706✔
2336
  if (stmt == NULL) {
419,706✔
2337
    tscError("NULL parameter for %s", __FUNCTION__);
×
2338
    terrno = TSDB_CODE_INVALID_PARA;
×
2339
    return terrno;
×
2340
  }
2341

2342
  return stmtClose(stmt);
419,706✔
2343
}
2344

2345
TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option) {
19,220✔
2346
  if (NULL == taos) {
19,220✔
2347
    tscError("NULL parameter for %s", __FUNCTION__);
×
2348
    terrno = TSDB_CODE_INVALID_PARA;
×
2349
    return NULL;
×
2350
  }
2351
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
19,220✔
2352
  if (NULL == pObj) {
19,776✔
2353
    tscError("invalid parameter for %s", __FUNCTION__);
×
2354
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2355
    return NULL;
×
2356
  }
2357

2358
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
19,776✔
2359

2360
  releaseTscObj(*(int64_t *)taos);
19,776✔
2361

2362
  return pStmt;
19,776✔
2363
}
2364

2365
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
21,691✔
2366
  if (stmt == NULL || sql == NULL) {
21,691✔
2367
    tscError("NULL parameter for %s", __FUNCTION__);
×
2368
    terrno = TSDB_CODE_INVALID_PARA;
×
2369
    return terrno;
×
2370
  }
2371

2372
  return stmtPrepare2(stmt, sql, length);
21,784✔
2373
}
2374

2375
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
7,397,246✔
2376
  if (stmt == NULL) {
7,397,246✔
2377
    tscError("NULL parameter for %s", __FUNCTION__);
×
2378
    terrno = TSDB_CODE_INVALID_PARA;
×
2379
    return terrno;
×
2380
  }
2381

2382
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
7,397,246✔
2383
  STMT2_DLOG_E("start to bind param");
7,397,246✔
2384
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 1) {
7,397,246✔
2385
    STMT2_ELOG_E("async bind param is still working, please try again later");
1,177✔
2386
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
1,177✔
2387
    return terrno;
×
2388
  }
2389

2390
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
7,398,312✔
2391
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
2392
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
2393
    }
2394
    pStmt->execSemWaited = true;
×
2395
  }
2396

2397
  int32_t code = TSDB_CODE_SUCCESS;
7,397,501✔
2398
  for (int i = 0; i < bindv->count; ++i) {
23,633,263✔
2399
    if (bindv->tbnames && bindv->tbnames[i]) {
16,232,418✔
2400
      code = stmtSetTbName2(stmt, bindv->tbnames[i]);
16,234,533✔
2401
      if (code) {
16,233,779✔
2402
        terrno = code;
×
2403
        STMT2_ELOG("set tbname failed, code:%s", tstrerror(code));
×
2404
        return terrno;
×
2405
      }
2406
    }
2407

2408
    SVCreateTbReq *pCreateTbReq = NULL;
16,233,868✔
2409
    if (bindv->tags && bindv->tags[i]) {
16,233,065✔
2410
      code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
10,019,632✔
2411
    } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
6,214,252✔
2412
      code = stmtCheckTags2(stmt, &pCreateTbReq);
2,007,219✔
2413
    } else if (pStmt->sql.autoCreateTbl) {
4,206,760✔
2414
      // if (pStmt->sql.autoCreateTbl) {
2415
      //   pStmt->sql.autoCreateTbl = false;
2416
      //   STMT2_WLOG_E("sql is autoCreateTbl, but no tags");
2417
      // }
2418
      code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
×
2419
    }
2420

2421
    if (code) {
16,230,281✔
2422
      terrno = code;
×
2423
      STMT2_ELOG("set tags failed, code:%s", tstrerror(code));
×
2424
      return terrno;
×
2425
    }
2426

2427
    if (bindv->bind_cols && bindv->bind_cols[i]) {
16,230,281✔
2428
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
16,234,915✔
2429

2430
      if (bind->num <= 0 || bind->num > INT16_MAX) {
16,234,092✔
2431
        STMT2_ELOG("bind num:%d must > 0 and < INT16_MAX", bind->num);
1,582✔
2432
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
1,582✔
2433
        return terrno;
×
2434
      }
2435

2436
      if (!stmt2IsInsert(stmt) && bind->num > 1) {
16,233,606✔
2437
        STMT2_ELOG_E("only one row data allowed for query");
×
2438
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2439
        return terrno;
×
2440
      }
2441

2442
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
16,235,464✔
2443
      if (TSDB_CODE_SUCCESS != code) {
16,235,338✔
2444
        terrno = code;
4✔
2445
        STMT2_ELOG("bind batch failed, code:%s", tstrerror(code));
×
2446
        return terrno;
×
2447
      }
2448
    }
2449
  }
2450

2451
  return code;
7,401,391✔
2452
}
2453

2454
int taos_stmt2_bind_param_a(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx, __taos_async_fn_t fp,
×
2455
                            void *param) {
2456
  if (stmt == NULL || bindv == NULL || fp == NULL) {
×
2457
    terrno = TSDB_CODE_INVALID_PARA;
×
2458
    return terrno;
×
2459
  }
2460

2461
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2462

2463
  ThreadArgs *args = (ThreadArgs *)taosMemoryMalloc(sizeof(ThreadArgs));
×
2464
  args->stmt = stmt;
×
2465
  args->bindv = bindv;
×
2466
  args->col_idx = col_idx;
×
2467
  args->fp = fp;
×
2468
  args->param = param;
×
2469

2470
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2471
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 0) {
×
2472
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2473
    tscError("async bind param is still working, please try again later");
×
2474
    terrno = TSDB_CODE_TSC_STMT_API_ERROR;
×
2475
    return terrno;
×
2476
  }
2477
  (void)atomic_add_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2478
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2479

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

2490
  return code_s;
×
2491
}
2492

2493
int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows) {
7,399,521✔
2494
  if (stmt == NULL) {
7,399,521✔
2495
    tscError("NULL parameter for %s", __FUNCTION__);
×
2496
    terrno = TSDB_CODE_INVALID_PARA;
×
2497
    return terrno;
×
2498
  }
2499

2500
  return stmtExec2(stmt, affected_rows);
7,399,521✔
2501
}
2502

2503
int taos_stmt2_close(TAOS_STMT2 *stmt) {
19,776✔
2504
  if (stmt == NULL) {
19,776✔
2505
    tscError("NULL parameter for %s", __FUNCTION__);
×
2506
    terrno = TSDB_CODE_INVALID_PARA;
×
2507
    return terrno;
×
2508
  }
2509

2510
  return stmtClose2(stmt);
19,776✔
2511
}
2512

2513
int taos_stmt2_is_insert(TAOS_STMT2 *stmt, int *insert) {
×
2514
  if (stmt == NULL || insert == NULL) {
×
2515
    tscError("NULL parameter for %s", __FUNCTION__);
×
2516
    terrno = TSDB_CODE_INVALID_PARA;
×
2517
    return terrno;
×
2518
  }
2519
  *insert = stmt2IsInsert(stmt);
×
2520
  return TSDB_CODE_SUCCESS;
×
2521
}
2522

2523
int taos_stmt2_get_fields(TAOS_STMT2 *stmt, int *count, TAOS_FIELD_ALL **fields) {
×
2524
  if (stmt == NULL || count == NULL) {
×
2525
    tscError("NULL parameter for %s", __FUNCTION__);
×
2526
    terrno = TSDB_CODE_INVALID_PARA;
×
2527
    return terrno;
×
2528
  }
2529

2530
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2531
  if (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type ||
×
2532
      (pStmt->sql.type == 0 && stmt2IsInsert(stmt))) {
×
2533
    return stmtGetStbColFields2(stmt, count, fields);
×
2534
  }
2535
  if (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt))) {
×
2536
    return stmtGetParamNum2(stmt, count);
×
2537
  }
2538

2539
  tscError("Invalid sql for stmt %s", pStmt->sql.sqlStr);
×
2540
  return TSDB_CODE_PAR_SYNTAX_ERROR;
×
2541
}
2542

2543
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_ALL *fields) {
×
2544
  (void)stmt;
2545
  if (!fields) return;
×
2546
  taosMemoryFree(fields);
×
2547
}
2548

2549
TAOS_RES *taos_stmt2_result(TAOS_STMT2 *stmt) {
×
2550
  if (stmt == NULL) {
×
2551
    tscError("NULL parameter for %s", __FUNCTION__);
×
2552
    terrno = TSDB_CODE_INVALID_PARA;
×
2553
    return NULL;
×
2554
  }
2555

2556
  return stmtUseResult2(stmt);
×
2557
}
2558

2559
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmtErrstr2(stmt); }
×
2560

2561
int taos_set_conn_mode(TAOS *taos, int mode, int value) {
3,445✔
2562
  if (taos == NULL) {
3,445✔
2563
    terrno = TSDB_CODE_INVALID_PARA;
×
2564
    return terrno;
×
2565
  }
2566

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

2584
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