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

taosdata / TDengine / #4847

11 Nov 2025 05:50AM UTC coverage: 62.651% (+0.3%) from 62.306%
#4847

push

travis-ci

web-flow
Merge e78cd6509 into 47a2ea7a0

542 of 650 new or added lines in 16 files covered. (83.38%)

1515 existing lines in 91 files now uncovered.

113826 of 181682 relevant lines covered (62.65%)

113230552.12 hits per line

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

48.82
/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, ...) {
620,216✔
46
  if (arg == NULL) {
620,216✔
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) {
12,865,976✔
52
    if (i % 1000 == 0) {
12,352,704✔
53
      (void)sched_yield();
14,592✔
54
    }
55
  }
56

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

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

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

249
  monitorClose();
1,727,353✔
250
  tscStopCrashReport();
1,727,353✔
251

252
  hbMgrCleanUp();
1,727,353✔
253

254
  catalogDestroy();
1,727,353✔
255
  schedulerDestroy();
1,727,353✔
256

257
  fmFuncMgtDestroy();
1,727,353✔
258
  qCleanupKeywordsTable();
1,727,353✔
259

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

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

269
  id = clientConnRefPool;
1,727,353✔
270
  clientConnRefPool = -1;
1,727,353✔
271
  taosCloseRef(id);
1,727,353✔
272

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

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

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

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

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

307
TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) {
3,708,781✔
308
  tscInfo("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
3,708,781✔
309
  if (user == NULL) {
3,710,751✔
310
    user = TSDB_DEFAULT_USER;
363,059✔
311
  }
312

313
  if (pass == NULL) {
3,710,751✔
314
    pass = TSDB_DEFAULT_PASS;
363,059✔
315
  }
316

317
  STscObj *pObj = NULL;
3,710,751✔
318
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
3,710,751✔
319
  if (TSDB_CODE_SUCCESS == code) {
3,710,007✔
320
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
3,696,780✔
321
    if (NULL == rid) {
3,697,254✔
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,697,254✔
326
    return (TAOS *)rid;
3,697,588✔
327
  } else {
328
    terrno = code;
13,227✔
329
  }
330

331
  return NULL;
13,227✔
332
}
333

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

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

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

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

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

655
void taos_close(TAOS *taos) {
3,696,202✔
656
  if (taos == NULL) {
3,696,202✔
657
    return;
141✔
658
  }
659

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

666
  taos_close_internal(pObj);
3,696,061✔
667
  releaseTscObj(*(int64_t *)taos);
3,695,727✔
668
  taosMemoryFree(taos);
3,696,395✔
669
}
670

671
int taos_errno(TAOS_RES *res) {
820,615,224✔
672
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
820,615,224✔
673
    return terrno;
32,769✔
674
  }
675

676
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
820,586,201✔
677
    return 0;
345,853✔
678
  }
679

680
  return ((SRequestObj *)res)->code;
820,236,602✔
681
}
682

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

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

692
  SRequestObj *pRequest = (SRequestObj *)res;
21,603,264✔
693
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
21,603,264✔
694
    return pRequest->msgBuf;
17,515,267✔
695
  } else {
696
    return (const char *)tstrerror(pRequest->code);
4,087,997✔
697
  }
698
}
699

700
void taos_free_result(TAOS_RES *res) {
648,775,378✔
701
  if (NULL == res) {
648,775,378✔
702
    return;
2,059,620✔
703
  }
704

705
  tscTrace("res:%p, will be freed", res);
646,715,758✔
706

707
  if (TD_RES_QUERY(res)) {
646,715,999✔
708
    SRequestObj *pRequest = (SRequestObj *)res;
643,614,347✔
709
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
643,614,347✔
710
    destroyRequest(pRequest);
643,614,347✔
711
    return;
643,613,544✔
712
  }
713

714
  SMqRspObj *pRsp = (SMqRspObj *)res;
3,105,435✔
715
  if (TD_RES_TMQ(res)) {
3,105,435✔
716
    tDeleteMqDataRsp(&pRsp->dataRsp);
3,097,349✔
717
    doFreeReqResultInfo(&pRsp->resInfo);
3,097,349✔
718
  } else if (TD_RES_TMQ_METADATA(res)) {
10,933✔
719
    tDeleteSTaosxRsp(&pRsp->dataRsp);
453✔
720
    doFreeReqResultInfo(&pRsp->resInfo);
453✔
721
  } else if (TD_RES_TMQ_META(res)) {
10,480✔
722
    tDeleteMqMetaRsp(&pRsp->metaRsp);
9,281✔
723
  } else if (TD_RES_TMQ_BATCH_META(res)) {
1,199✔
724
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
1,199✔
725
  } else if (TD_RES_TMQ_RAW(res)) {
×
726
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
727
  }
728
  taosMemoryFree(pRsp);
3,108,076✔
729
}
730

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

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

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

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

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

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

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

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

795
    return doAsyncFetchRows(pRequest, true, true);
1,632,316,617✔
796
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,359,110,001✔
797
    SMqRspObj      *msg = ((SMqRspObj *)res);
1,359,155,112✔
798
    SReqResultInfo *pResultInfo = NULL;
1,359,155,112✔
799
    if (msg->resIter == -1) {
1,359,154,393✔
800
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
2,726,214✔
801
        return NULL;
×
802
      }
803
    } else {
804
      pResultInfo = tmqGetCurResInfo(res);
1,356,429,001✔
805
    }
806

807
    if (pResultInfo->current < pResultInfo->numOfRows) {
1,359,155,112✔
808
      doSetOneRowPtr(pResultInfo);
1,329,150,181✔
809
      pResultInfo->current += 1;
1,329,145,535✔
810
      return pResultInfo->row;
1,329,145,410✔
811
    } else {
812
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
30,004,861✔
813
        return NULL;
2,726,024✔
814
      }
815

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

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

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

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

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

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

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

861
      case TSDB_DATA_TYPE_INT:
1,433,664,601✔
862
        len += tsnprintf(str + len, size - len, "%d", *((int32_t *)row[i]));
1,433,664,601✔
863
        break;
1,433,664,857✔
864

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

869
      case TSDB_DATA_TYPE_BIGINT:
1,121,050,778✔
870
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
1,121,050,778✔
871
        break;
1,121,052,000✔
872

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

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

883
      case TSDB_DATA_TYPE_DOUBLE: {
791,478,425✔
884
        double dv = 0;
791,478,425✔
885
        dv = GET_DOUBLE_VAL(row[i]);
791,478,425✔
886
        len += snprintf(str + len, size - len, "%.*g", DBL_DIG, dv);
791,478,903✔
887
      } break;
791,479,618✔
888

889
      case TSDB_DATA_TYPE_VARBINARY: {
1,101,150✔
890
        void    *data = NULL;
1,101,150✔
891
        uint32_t tmp = 0;
1,101,150✔
892
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
1,101,150✔
893
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
1,101,150✔
894
          break;
×
895
        }
896
        uint32_t copyLen = TMIN(size - len - 1, tmp);
1,100,892✔
897
        (void)memcpy(str + len, data, copyLen);
1,100,892✔
898
        len += copyLen;
1,100,892✔
899
        taosMemoryFree(data);
1,100,892✔
900
      } break;
1,100,892✔
901
      case TSDB_DATA_TYPE_BINARY:
1,368,563,304✔
902
      case TSDB_DATA_TYPE_NCHAR:
903
      case TSDB_DATA_TYPE_GEOMETRY: {
904
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
1,368,563,304✔
905
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
1,368,559,584✔
906
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
398,479,150✔
907
          if (charLen > fields[i].bytes || charLen < 0) {
971,190,864✔
908
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
289✔
909
            break;
×
910
          }
911
        } else {
912
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
397,378,140✔
UNCOV
913
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
×
914
            break;
×
915
          }
916
        }
917

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

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

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

938
      case TSDB_DATA_TYPE_TIMESTAMP:
1,779,239,537✔
939
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
1,779,239,537✔
940
        break;
1,779,234,576✔
941

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

956
    if (len >= size - 1) {
2,147,483,647✔
957
      break;
×
958
    }
959
  }
960
  if (len < size) {
1,360,022,689✔
961
    str[len] = 0;
1,360,015,970✔
962
  }
963

964
  return len;
1,360,016,592✔
965
}
966

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

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

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

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

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

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

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

1043
// return int32_t
1044
int taos_affected_rows(TAOS_RES *res) {
501,805,773✔
1045
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
501,805,773✔
1046
      TD_RES_TMQ_BATCH_META(res)) {
501,812,611✔
1047
    return 0;
×
1048
  }
1049

1050
  SRequestObj    *pRequest = (SRequestObj *)res;
501,807,637✔
1051
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
501,807,637✔
1052
  return (int)pResInfo->numOfRows;
501,810,906✔
1053
}
1054

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

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

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

1072
  if (TD_RES_QUERY(res)) {
1,456,872,802✔
1073
    SRequestObj *pRequest = (SRequestObj *)res;
93,946,311✔
1074
    return pRequest->body.resInfo.precision;
93,946,311✔
1075
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,362,920,399✔
1076
    SReqResultInfo *info = tmqGetCurResInfo(res);
1,362,928,587✔
1077
    return info->precision;
1,362,928,587✔
1078
  }
1079
  return TSDB_TIME_PRECISION_MILLI;
×
1080
}
1081

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

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

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

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

1103
  taos_free_result(pRequest);
160,213✔
1104
  releaseTscObj(*(int64_t *)taos);
160,213✔
1105
  return code;
160,116✔
1106
}
1107

1108
void taos_stop_query(TAOS_RES *res) {
648,376,932✔
1109
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
648,376,932✔
1110
      TD_RES_TMQ_BATCH_META(res)) {
648,383,033✔
1111
    return;
×
1112
  }
1113

1114
  stopAllQueries((SRequestObj *)res);
648,374,123✔
1115
}
1116

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

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

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

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

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

1147
  if (TD_RES_QUERY(res)) {
174,028,108✔
1148
    SRequestObj *pRequest = (SRequestObj *)res;
166,570,870✔
1149

1150
    (*rows) = NULL;
166,570,870✔
1151
    (*numOfRows) = 0;
166,570,870✔
1152

1153
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
166,570,870✔
1154
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
166,424,406✔
1155
      return pRequest->code;
771,747✔
1156
    }
1157

1158
    (void)doAsyncFetchRows(pRequest, false, true);
165,799,123✔
1159

1160
    // TODO refactor
1161
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
165,798,715✔
1162
    pResultInfo->current = pResultInfo->numOfRows;
165,799,123✔
1163

1164
    (*rows) = pResultInfo->row;
165,799,123✔
1165
    (*numOfRows) = pResultInfo->numOfRows;
165,799,123✔
1166
    return pRequest->code;
165,798,439✔
1167
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
7,457,442✔
1168
    SReqResultInfo *pResultInfo = NULL;
7,457,442✔
1169
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
7,457,442✔
1170
    if (code != 0) return code;
7,457,442✔
1171

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

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

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

1190
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,578,175✔
1191
    SReqResultInfo *pResultInfo = NULL;
1,536,774✔
1192
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
1,536,774✔
1193
    if (code != 0) {
1,536,774✔
1194
      (*numOfRows) = 0;
20,192✔
1195
      return 0;
20,192✔
1196
    }
1197

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1282
  int code = taos_errno(pObj);
×
1283

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

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

1295
  resetConnectDB(pTscObj);
×
1296

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

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

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

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

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

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

1337
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
1,295,701,085✔
1338
  if (NULL == pWrapper) {
1,295,701,085✔
1339
    return;
651,138,934✔
1340
  }
1341
  destoryCatalogReq(pWrapper->pCatalogReq);
644,562,151✔
1342
  taosMemoryFree(pWrapper->pCatalogReq);
644,550,544✔
1343
  qDestroyParseContext(pWrapper->pParseCtx);
644,545,899✔
1344
  taosMemoryFree(pWrapper);
644,542,679✔
1345
}
1346

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

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

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

1362
  int64_t analyseStart = taosGetTimestampUs();
182,064,924✔
1363
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
182,064,924✔
1364
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
182,064,393✔
1365

1366
  if (TSDB_CODE_SUCCESS == code) {
182,063,939✔
1367
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
182,058,230✔
1368
  }
1369

1370
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
182,052,634✔
1371

1372
  if (pRequest->parseOnly) {
182,056,587✔
1373
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
301,308✔
1374
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
301,308✔
1375
  }
1376

1377
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
182,056,223✔
1378
}
182,055,010✔
1379

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

1407
    *ppTarget = pTarget;
×
1408
  }
1409

1410
  return code;
×
1411
}
1412

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

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

1444
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
182,043,429✔
1445
  SRequestObj *pRequest = pWrapper->pRequest;
182,043,429✔
1446
  SQuery      *pQuery = pRequest->pQuery;
182,054,682✔
1447

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

1455
  if (code == TSDB_CODE_SUCCESS) {
182,050,366✔
1456
    pRequest->stableQuery = pQuery->stableQuery;
166,467,260✔
1457
    if (pQuery->pRoot) {
166,475,996✔
1458
      pRequest->stmtType = pQuery->pRoot->type;
166,465,254✔
1459
    }
1460

1461
    if (pQuery->haveResultSet) {
166,475,898✔
1462
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
90,831,867✔
1463
                              pRequest->stmtBindVersion > 0);
90,830,363✔
1464
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
90,831,336✔
1465
    }
1466
  }
1467

1468
  if (code == TSDB_CODE_SUCCESS) {
182,038,544✔
1469
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
166,460,598✔
1470
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
166,462,221✔
1471
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
166,471,345✔
1472

1473
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
166,450,301✔
1474
  } else {
1475
    destorySqlCallbackWrapper(pWrapper);
15,577,946✔
1476
    pRequest->pWrapper = NULL;
15,577,946✔
1477
    qDestroyQuery(pRequest->pQuery);
15,577,946✔
1478
    pRequest->pQuery = NULL;
15,577,946✔
1479

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

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

1495
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
192,136,510✔
1496
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
375,964,993✔
1497
                           .requestId = pWrapper->pParseCtx->requestId,
192,137,326✔
1498
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
192,141,543✔
1499
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
192,136,675✔
1500

1501
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
375,969,435✔
1502

1503
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
200,455,580✔
1504
                                &pWrapper->pRequest->body.queryJob);
192,144,430✔
1505
}
1506

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

1509
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
649,177,795✔
1510
  int32_t code = TSDB_CODE_SUCCESS;
649,177,795✔
1511
  switch (pWrapper->pRequest->pQuery->execStage) {
649,177,795✔
1512
    case QUERY_EXEC_STAGE_PARSE: {
10,088,159✔
1513
      // continue parse after get metadata
1514
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
10,088,159✔
1515
      break;
10,088,159✔
1516
    }
1517
    case QUERY_EXEC_STAGE_ANALYSE: {
182,054,993✔
1518
      // analysis after get metadata
1519
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
182,054,993✔
1520
      break;
182,048,889✔
1521
    }
1522
    case QUERY_EXEC_STAGE_SCHEDULE: {
457,044,769✔
1523
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
457,044,769✔
1524
      break;
457,043,426✔
1525
    }
1526
    default:
×
1527
      break;
×
1528
  }
1529
  return code;
649,179,742✔
1530
}
1531

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

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

1541
  if (code == TSDB_CODE_SUCCESS) {
10,088,159✔
1542
    // pWrapper->pCatalogReq->forceUpdate = false;
1543
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
10,061,218✔
1544
  }
1545

1546
  if (TSDB_CODE_SUCCESS == code) {
10,088,159✔
1547
    code = phaseAsyncQuery(pWrapper);
9,554,140✔
1548
  }
1549

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

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

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

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

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

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

1591
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
644,560,892✔
1592
  if (*pCxt == NULL) {
644,544,455✔
1593
    return terrno;
×
1594
  }
1595

1596
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
1,263,097,209✔
1597
                           .requestRid = pRequest->self,
644,554,014✔
1598
                           .acctId = pTscObj->acctId,
644,552,763✔
1599
                           .db = pRequest->pDb,
644,556,316✔
1600
                           .topicQuery = false,
1601
                           .pSql = pRequest->sqlstr,
644,562,610✔
1602
                           .sqlLen = pRequest->sqlLen,
644,561,432✔
1603
                           .pMsg = pRequest->msgBuf,
644,567,578✔
1604
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1605
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
644,563,193✔
1606
                           .pStmtCb = NULL,
1607
                           .pUser = pTscObj->user,
644,559,815✔
1608
                           .pEffectiveUser = pRequest->effectiveUser,
644,561,421✔
1609
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
644,559,651✔
1610
                           .enableSysInfo = pTscObj->sysInfo,
644,562,778✔
1611
                           .async = true,
1612
                           .svrVer = pTscObj->sVer,
644,563,645✔
1613
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
644,563,311✔
1614
                           .allocatorId = pRequest->allocatorRefId,
644,561,805✔
1615
                           .parseSqlFp = clientParseSql,
1616
                           .parseSqlParam = pWrapper,
1617
                           .setQueryFp = setQueryRequest,
1618
                           .timezone = pTscObj->optionInfo.timezone,
644,561,654✔
1619
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
644,559,366✔
1620
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
644,561,535✔
1621
  (*pCxt)->biMode = biMode;
644,551,439✔
1622
  return TSDB_CODE_SUCCESS;
644,559,502✔
1623
}
1624

1625
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
644,558,153✔
1626
  int32_t              code = TSDB_CODE_SUCCESS;
644,558,153✔
1627
  STscObj             *pTscObj = pRequest->pTscObj;
644,558,153✔
1628
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
644,564,944✔
1629
  if (pWrapper == NULL) {
644,553,966✔
1630
    code = terrno;
×
1631
  } else {
1632
    pWrapper->pRequest = pRequest;
644,553,966✔
1633
    pRequest->pWrapper = pWrapper;
644,555,264✔
1634
    *ppWrapper = pWrapper;
644,557,304✔
1635
  }
1636

1637
  if (TSDB_CODE_SUCCESS == code) {
644,557,148✔
1638
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
644,557,436✔
1639
  }
1640

1641
  if (TSDB_CODE_SUCCESS == code) {
644,553,646✔
1642
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
644,554,227✔
1643
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
644,567,906✔
1644
  }
1645

1646
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
644,562,119✔
1647
    int64_t syntaxStart = taosGetTimestampUs();
644,565,408✔
1648

1649
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
644,565,408✔
1650
    if (pWrapper->pCatalogReq == NULL) {
644,547,776✔
1651
      code = terrno;
×
1652
    } else {
1653
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
644,551,354✔
1654
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
644,561,231✔
1655
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
644,556,058✔
1656
    }
1657

1658
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
644,555,564✔
1659
  }
1660

1661
  return code;
644,566,864✔
1662
}
1663

1664
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
645,576,202✔
1665
  SSqlCallbackWrapper *pWrapper = NULL;
645,576,202✔
1666
  int32_t              code = TSDB_CODE_SUCCESS;
645,583,375✔
1667

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

1677
  if (TSDB_CODE_SUCCESS == code) {
644,552,077✔
1678
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
644,556,536✔
1679
  }
1680

1681
  if (TSDB_CODE_SUCCESS == code) {
644,549,221✔
1682
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
639,621,184✔
1683
    code = phaseAsyncQuery(pWrapper);
639,631,949✔
1684
  }
1685

1686
  if (TSDB_CODE_SUCCESS != code) {
644,554,702✔
1687
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
4,935,369✔
1688
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
4,807,923✔
1689
               pRequest->requestId);
1690
    } else {
1691
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
127,446✔
1692
               pRequest->requestId);
1693
    }
1694

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

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

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

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

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

1757
static int32_t doAsyncFetch(void *pParam) {
107,973,421✔
1758
  SAsyncFetchParam *param = pParam;
107,973,421✔
1759
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
107,973,421✔
1760
  taosMemoryFree(param);
107,972,069✔
1761
  return TSDB_CODE_SUCCESS;
107,971,370✔
1762
}
1763

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

1775
  SRequestObj *pRequest = res;
107,976,341✔
1776
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
107,976,341✔
1777
    fp(param, res, 0);
2,920✔
1778
    return;
2,920✔
1779
  }
1780

1781
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
107,972,666✔
1782
  if (!pParam) {
107,972,145✔
1783
    fp(param, res, terrno);
×
1784
    return;
×
1785
  }
1786
  pParam->pReq = pRequest;
107,972,145✔
1787
  pParam->fp = fp;
107,971,872✔
1788
  pParam->param = param;
107,972,388✔
1789
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
107,971,872✔
1790
  if (TSDB_CODE_SUCCESS != code) {
107,973,421✔
1791
    taosMemoryFree(pParam);
×
1792
    fp(param, res, code);
×
1793
    return;
×
1794
  }
1795
}
1796

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

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

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

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

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

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

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

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

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

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

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

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

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

1871
_return:
×
1872

1873
  terrno = code;
×
1874

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

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

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

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

1899
  pRequest->syncQuery = true;
×
1900

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

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

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

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

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

1922
  *vgId = vgInfo.vgId;
×
1923

1924
_return:
×
1925

1926
  terrno = code;
×
1927

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

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

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

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

1952
  pRequest->syncQuery = true;
×
1953

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

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

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

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

1971
_return:
×
1972

1973
  terrno = code;
×
1974

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2060
  return pStmt;
490,493✔
2061
}
2062

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

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

2077
  return pStmt;
×
2078
}
2079

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

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

2094
  return pStmt;
32,250✔
2095
}
2096

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

2104
  return stmtPrepare(stmt, sql, length);
1,063,897✔
2105
}
2106

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

2114
  int32_t code = stmtSetTbName(stmt, name);
19,099✔
2115
  if (code) {
19,099✔
2116
    return code;
619✔
2117
  }
2118

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

2123
  return TSDB_CODE_SUCCESS;
×
2124
}
2125

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

2133
  return stmtSetTbName(stmt, name);
13,429,453✔
2134
}
2135

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

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

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

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

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

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

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

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

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

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

2188
  return stmtBindBatch(stmt, bind, -1);
52,832✔
2189
}
2190

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

2198
  if (bind->num <= 0 || bind->num > INT16_MAX) {
346,887,153✔
2199
    tscError("invalid bind num %d", bind->num);
43,372✔
2200
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
43,372✔
2201
    return terrno;
×
2202
  }
2203

2204
  int32_t insert = 0;
349,156,738✔
2205
  int32_t code = stmtIsInsert(stmt, &insert);
347,829,462✔
2206
  if (TSDB_CODE_SUCCESS != code) {
341,503,978✔
2207
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2208
    return code;
×
2209
  }
2210
  if (0 == insert && bind->num > 1) {
341,503,978✔
2211
    tscError("only one row data allowed for query");
×
2212
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2213
    return terrno;
×
2214
  }
2215

2216
  return stmtBindBatch(stmt, bind, -1);
341,503,978✔
2217
}
2218

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

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

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

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

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

2254
  return stmtAddBatch(stmt);
328,983,040✔
2255
}
2256

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

2264
  return stmtExec(stmt);
4,585,256✔
2265
}
2266

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

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

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

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

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

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

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

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

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

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

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

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

2326
  return stmtAffectedRowsOnce(stmt);
2,530✔
2327
}
2328

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

2336
  return stmtClose(stmt);
522,743✔
2337
}
2338

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

2352
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
18,799✔
2353

2354
  releaseTscObj(*(int64_t *)taos);
18,799✔
2355

2356
  return pStmt;
18,799✔
2357
}
2358

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

2366
  return stmtPrepare2(stmt, sql, length);
20,799✔
2367
}
2368

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

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

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

2391
  int32_t code = TSDB_CODE_SUCCESS;
7,228,396✔
2392
  for (int i = 0; i < bindv->count; ++i) {
23,286,764✔
2393
    if (bindv->tbnames && bindv->tbnames[i]) {
16,056,920✔
2394
      code = stmtSetTbName2(stmt, bindv->tbnames[i]);
16,057,560✔
2395
      if (code) {
16,056,427✔
UNCOV
2396
        terrno = code;
×
2397
        STMT2_ELOG("set tbname failed, code:%s", tstrerror(code));
×
2398
        return terrno;
×
2399
      }
2400
    }
2401

2402
    SVCreateTbReq *pCreateTbReq = NULL;
16,056,808✔
2403
    if (bindv->tags && bindv->tags[i]) {
16,055,888✔
2404
      code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
10,064,881✔
2405
    } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
5,991,467✔
2406
      code = stmtCheckTags2(stmt, &pCreateTbReq);
1,999,040✔
2407
    } else if (pStmt->sql.autoCreateTbl) {
3,991,507✔
2408
      // if (pStmt->sql.autoCreateTbl) {
2409
      //   pStmt->sql.autoCreateTbl = false;
2410
      //   STMT2_WLOG_E("sql is autoCreateTbl, but no tags");
2411
      // }
2412
      code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
×
2413
    }
2414

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

2421
    if (bindv->bind_cols && bindv->bind_cols[i]) {
16,055,055✔
2422
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
16,058,817✔
2423

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

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

2436
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
16,058,450✔
2437
      if (TSDB_CODE_SUCCESS != code) {
16,057,801✔
UNCOV
2438
        terrno = code;
×
2439
        STMT2_ELOG("bind batch failed, code:%s", tstrerror(code));
×
2440
        return terrno;
×
2441
      }
2442
    }
2443
  }
2444

2445
  return code;
7,229,344✔
2446
}
2447

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

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

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

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

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

2484
  return code_s;
×
2485
}
2486

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

2494
  return stmtExec2(stmt, affected_rows);
7,228,817✔
2495
}
2496

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

2504
  return stmtClose2(stmt);
18,799✔
2505
}
2506

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

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

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

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

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

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

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

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

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

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

2578
char *getBuildInfo() { return td_buildinfo; }
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc