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

taosdata / TDengine / #4846

10 Nov 2025 08:18AM UTC coverage: 62.306% (-0.8%) from 63.067%
#4846

push

travis-ci

web-flow
Merge 0339ad128 into 51207f009

2 of 6 new or added lines in 2 files covered. (33.33%)

305 existing lines in 1 file now uncovered.

111841 of 179503 relevant lines covered (62.31%)

114415979.45 hits per line

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

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

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

35
#define TSC_VAR_NOT_RELEASE 1
36
#define TSC_VAR_RELEASED    0
37

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

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

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

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

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

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

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

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

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

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

95
  tscDebug("set timezone to %s", val);
×
96
  tz = tzalloc(val);
×
97
  if (tz == NULL) {
×
98
    tscWarn("%s unknown timezone %s change to UTC", __func__, val);
×
99
    tz = tzalloc("UTC");
×
100
    if (tz == NULL) {
×
101
      tscError("%s set timezone UTC error", __func__);
×
102
      terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
103
      goto END;
×
104
    }
105
  }
106
  int32_t code = taosHashPut(pTimezoneMap, val, strlen(val), &tz, sizeof(timezone_t));
×
107
  if (code != 0) {
×
108
    tscError("%s put timezone to tz map error:%d", __func__, code);
×
109
    tzfree(tz);
×
110
    tz = NULL;
×
111
    goto END;
×
112
  }
113

114
  time_t tx1 = taosGetTimestampSec();
×
115
  char   output[TD_TIMEZONE_LEN] = {0};
×
116
  code = taosFormatTimezoneStr(tx1, val, tz, output);
×
117
  if (code == 0) {
×
118
    code = taosHashPut(pTimezoneNameMap, &tz, sizeof(timezone_t), output, strlen(output) + 1);
×
119
  }
120
  if (code != 0) {
×
121
    tscError("failed to put timezone %s to map", val);
×
122
  }
123

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

331
  return NULL;
13,368✔
332
}
333

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

506
  __taos_async_whitelist_dual_stack_fn_t userCbFn;
507
} SFetchWhiteListDualStackInfo;
508

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

513
  SGetUserWhiteListRsp wlRsp = {0};
×
514

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

782
  if (TD_RES_QUERY(res)) {
2,147,483,647✔
783
    SRequestObj *pRequest = (SRequestObj *)res;
1,629,881,619✔
784
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
1,629,881,619✔
785
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
1,629,920,760✔
NEW
786
      return NULL;
×
787
    }
788
    if (pRequest->killed) {
789
      tscInfo("query has been killed, can not fetch more row.");
1,629,833,117✔
NEW
790
      pRequest->code = TSDB_CODE_TSC_QUERY_KILLED;
×
UNCOV
791
      return NULL;
×
UNCOV
792
    }
×
793

794
    if (pRequest->inCallback) {
795
      tscError("can not call taos_fetch_row before query callback ends.");
1,629,854,702✔
796
      terrno = TSDB_CODE_TSC_INVALID_OPERATION;
1,387,362,965✔
797
      return NULL;
1,387,438,999✔
798
    }
1,387,438,999✔
799

1,387,439,142✔
800
    return doAsyncFetchRows(pRequest, true, true);
2,671,685✔
UNCOV
801
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
×
802
    SMqRspObj      *msg = ((SMqRspObj *)res);
803
    SReqResultInfo *pResultInfo = NULL;
804
    if (msg->resIter == -1) {
1,384,767,171✔
805
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
806
        return NULL;
807
      }
1,387,438,754✔
808
    } else {
1,356,865,032✔
809
      pResultInfo = tmqGetCurResInfo(res);
1,356,862,692✔
810
    }
1,356,862,714✔
811

812
    if (pResultInfo->current < pResultInfo->numOfRows) {
30,573,891✔
813
      doSetOneRowPtr(pResultInfo);
2,671,490✔
814
      pResultInfo->current += 1;
815
      return pResultInfo->row;
816
    } else {
27,900,528✔
817
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
27,902,401✔
818
        return NULL;
27,902,401✔
819
      }
UNCOV
820

×
UNCOV
821
      doSetOneRowPtr(pResultInfo);
×
822
      pResultInfo->current += 1;
UNCOV
823
      return pResultInfo->row;
×
UNCOV
824
    }
×
825
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
826
    return NULL;
827
  } else {
828
    tscError("invalid result passed to taos_fetch_row");
829
    terrno = TSDB_CODE_TMQ_INVALID_DATA;
1,388,333,125✔
830
    return NULL;
1,388,333,125✔
831
  }
832
}
1,388,301,849✔
833

1,388,301,849✔
834
int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
2,147,483,647✔
835
  return taos_print_row_with_size(str, INT32_MAX, row, fields, num_fields);
2,147,483,647✔
836
}
2,147,483,647✔
837
int taos_print_row_with_size(char *str, uint32_t size, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
838
  int32_t len = 0;
839
  for (int i = 0; i < num_fields; ++i) {
2,147,483,647✔
840
    if (i > 0 && len < size - 1) {
29,982,385✔
841
      str[len++] = ' ';
29,982,537✔
842
    }
843

844
    if (row[i] == NULL) {
2,147,483,647✔
845
      len += tsnprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
1,102,341✔
846
      continue;
1,102,341✔
847
    }
1,101,823✔
848

849
    switch (fields[i].type) {
1,101,823✔
850
      case TSDB_DATA_TYPE_TINYINT:
1,101,823✔
851
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
1,101,046✔
852
        break;
853

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

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

1,437,076,121✔
862
      case TSDB_DATA_TYPE_USMALLINT:
1,437,076,121✔
863
        len += tsnprintf(str + len, size - len, "%u", *((uint16_t *)row[i]));
1,437,079,878✔
864
        break;
865

1,101,564✔
866
      case TSDB_DATA_TYPE_INT:
1,101,564✔
867
        len += tsnprintf(str + len, size - len, "%d", *((int32_t *)row[i]));
1,101,305✔
868
        break;
869

1,149,547,617✔
870
      case TSDB_DATA_TYPE_UINT:
1,149,547,617✔
871
        len += tsnprintf(str + len, size - len, "%u", *((uint32_t *)row[i]));
1,149,549,709✔
872
        break;
873

1,101,046✔
874
      case TSDB_DATA_TYPE_BIGINT:
1,101,046✔
875
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
1,100,269✔
876
        break;
877

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

1,570,660✔
882
      case TSDB_DATA_TYPE_FLOAT: {
883
        float fv = 0;
808,329,660✔
884
        fv = GET_FLOAT_VAL(row[i]);
808,329,660✔
885
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
808,329,660✔
886
      } break;
808,330,058✔
887

808,330,411✔
888
      case TSDB_DATA_TYPE_DOUBLE: {
889
        double dv = 0;
1,102,687✔
890
        dv = GET_DOUBLE_VAL(row[i]);
1,102,687✔
891
        len += snprintf(str + len, size - len, "%.*g", DBL_DIG, dv);
1,102,687✔
892
      } break;
1,102,687✔
893

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

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

×
936
        uint32_t copyLen = TMIN(size - len - 1, tmp);
×
937
        (void)memcpy(str + len, data, copyLen);
938
        len += copyLen;
1,809,254,260✔
939

1,809,254,260✔
940
        taosMemoryFree(data);
1,809,265,885✔
941
      } break;
942

1,101,564✔
943
      case TSDB_DATA_TYPE_TIMESTAMP:
1,101,564✔
944
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
577,653✔
UNCOV
945
        break;
×
946

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

1,388,274,684✔
961
    if (len >= size - 1) {
1,388,335,612✔
962
      break;
963
    }
964
  }
1,388,336,090✔
965
  if (len < size) {
966
    str[len] = 0;
967
  }
2,147,483,647✔
968

2,147,483,647✔
UNCOV
969
  return len;
×
970
}
971

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

×
UNCOV
977
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
UNCOV
978
  return pResInfo->length;
×
UNCOV
979
}
×
980

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

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

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

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

507,208,937✔
1046
const char *taos_get_client_info() { return td_version; }
507,213,664✔
UNCOV
1047

×
1048
// return int32_t
1049
int taos_affected_rows(TAOS_RES *res) {
1050
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
507,212,707✔
1051
      TD_RES_TMQ_BATCH_META(res)) {
507,212,707✔
1052
    return 0;
507,214,039✔
1053
  }
1054

1055
  SRequestObj    *pRequest = (SRequestObj *)res;
1056
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
1,298,535✔
1057
  return (int)pResInfo->numOfRows;
1,298,535✔
1058
}
1,298,535✔
UNCOV
1059

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

1067
  SRequestObj    *pRequest = (SRequestObj *)res;
1,485,658,577✔
1068
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
1,485,658,577✔
UNCOV
1069
  return pResInfo->numOfRows;
×
1070
}
1071

1072
int taos_result_precision(TAOS_RES *res) {
1,485,658,841✔
1073
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
94,436,960✔
1074
    return TSDB_TIME_PRECISION_MILLI;
94,436,960✔
1075
  }
1,391,222,291✔
1076

1,391,220,445✔
1077
  if (TD_RES_QUERY(res)) {
1,391,220,445✔
1078
    SRequestObj *pRequest = (SRequestObj *)res;
UNCOV
1079
    return pRequest->body.resInfo.precision;
×
1080
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1081
    SReqResultInfo *info = tmqGetCurResInfo(res);
1082
    return info->precision;
157,425✔
1083
  }
157,425✔
1084
  return TSDB_TIME_PRECISION_MILLI;
157,425✔
UNCOV
1085
}
×
UNCOV
1086

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

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

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

157,322✔
1105
  TAOS_RES *pRequest = taos_query(taos, sql);
157,425✔
1106
  int32_t   code = taos_errno(pRequest);
1107

1108
  taos_free_result(pRequest);
652,591,532✔
1109
  releaseTscObj(*(int64_t *)taos);
652,591,532✔
1110
  return code;
652,594,659✔
UNCOV
1111
}
×
1112

1113
void taos_stop_query(TAOS_RES *res) {
1114
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
652,588,650✔
1115
      TD_RES_TMQ_BATCH_META(res)) {
1116
    return;
UNCOV
1117
  }
×
UNCOV
1118

×
UNCOV
1119
  stopAllQueries((SRequestObj *)res);
×
1120
}
UNCOV
1121

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

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

174,250,501✔
1139
bool taos_is_update_query(TAOS_RES *res) { return taos_num_fields(res) == 0; }
174,250,501✔
1140

1141
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
1142
  int32_t numOfRows = 0;
174,250,501✔
1143
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
174,250,501✔
UNCOV
1144
  return numOfRows;
×
1145
}
1146

1147
int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) {
174,250,501✔
1148
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
166,821,829✔
1149
    return 0;
1150
  }
166,821,829✔
1151

166,821,829✔
1152
  if (TD_RES_QUERY(res)) {
1153
    SRequestObj *pRequest = (SRequestObj *)res;
166,821,829✔
1154

166,674,974✔
1155
    (*rows) = NULL;
784,426✔
1156
    (*numOfRows) = 0;
1157

1158
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
166,037,403✔
1159
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
1160
      return pRequest->code;
1161
    }
166,036,783✔
1162

166,037,403✔
1163
    (void)doAsyncFetchRows(pRequest, false, true);
1164

166,037,403✔
1165
    // TODO refactor
166,037,403✔
1166
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
166,037,195✔
1167
    pResultInfo->current = pResultInfo->numOfRows;
7,428,672✔
1168

7,428,672✔
1169
    (*rows) = pResultInfo->row;
7,428,672✔
1170
    (*numOfRows) = pResultInfo->numOfRows;
7,428,672✔
1171
    return pRequest->code;
1172
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
7,094,370✔
1173
    SReqResultInfo *pResultInfo = NULL;
7,094,370✔
1174
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
7,094,370✔
1175
    if (code != 0) return code;
7,094,370✔
1176

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

1,440,566✔
UNCOV
1187
int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) {
×
1188
  *numOfRows = 0;
1189
  *pData = NULL;
1190

1,440,566✔
1191
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,399,185✔
1192
    return 0;
1,399,185✔
1193
  }
1,399,185✔
1194

28,973✔
1195
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
28,973✔
1196
    SReqResultInfo *pResultInfo = NULL;
1197
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
1198
    if (code != 0) {
1,370,212✔
1199
      (*numOfRows) = 0;
1,370,212✔
1200
      return 0;
1,370,212✔
1201
    }
1,370,212✔
1202

1203
    pResultInfo->current = pResultInfo->numOfRows;
1204
    (*numOfRows) = pResultInfo->numOfRows;
41,381✔
1205
    (*pData) = (void *)pResultInfo->pData;
1206
    return 0;
41,381✔
1207
  }
41,381✔
UNCOV
1208

×
1209
  SRequestObj *pRequest = (SRequestObj *)res;
1210

1211
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
41,381✔
1212
      pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
1213
    return pRequest->code;
41,381✔
1214
  }
1215

41,381✔
1216
  (void)doAsyncFetchRows(pRequest, false, false);
41,381✔
1217

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

41,381✔
1220
  pResultInfo->current = pResultInfo->numOfRows;
1221
  (*numOfRows) = pResultInfo->numOfRows;
1222
  (*pData) = (void *)pResultInfo->pData;
116,813,034✔
1223

116,813,034✔
UNCOV
1224
  return pRequest->code;
×
1225
}
1226

1227
int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) {
116,813,034✔
1228
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
116,813,034✔
1229
    return 0;
×
1230
  }
1231

1232
  int32_t numOfFields = taos_num_fields(res);
116,813,034✔
1233
  if (columnIndex < 0 || columnIndex >= numOfFields || numOfFields == 0) {
116,813,034✔
1234
    return 0;
116,813,034✔
UNCOV
1235
  }
×
1236

1237
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
1238
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
116,813,034✔
1239
  if (!IS_VAR_DATA_TYPE(pField->type)) {
1240
    return 0;
1241
  }
324,020,587✔
1242

324,020,587✔
1243
  return pResInfo->pCol[columnIndex].offset;
324,020,587✔
UNCOV
1244
}
×
1245

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

1252
  int32_t numOfFields = taos_num_fields(res);
324,020,587✔
1253
  if (columnIndex >= numOfFields || numOfFields == 0) {
324,020,587✔
1254
    return TSDB_CODE_INVALID_PARA;
324,020,587✔
1255
  }
1256

324,020,587✔
UNCOV
1257
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
1258
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
1259
  SResultColumn  *pCol = &pResInfo->pCol[columnIndex];
324,020,587✔
UNCOV
1260

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

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

1287
  int code = taos_errno(pObj);
UNCOV
1288

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

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

1300
  resetConnectDB(pTscObj);
38,416✔
1301

38,416✔
1302
  releaseTscObj(*(int64_t *)taos);
38,416✔
UNCOV
1303
}
×
UNCOV
1304

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

1312
  releaseTscObj(*(int64_t *)taos);
604✔
1313

604✔
1314
  return pTscObj->sDetailVer;
604✔
UNCOV
1315
}
×
1316

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

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

649,345,207✔
1342
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
649,333,178✔
1343
  if (NULL == pWrapper) {
649,324,624✔
1344
    return;
649,318,590✔
1345
  }
1346
  destoryCatalogReq(pWrapper->pCatalogReq);
1347
  taosMemoryFree(pWrapper->pCatalogReq);
3,676,367✔
1348
  qDestroyParseContext(pWrapper->pParseCtx);
3,676,367✔
1349
  taosMemoryFree(pWrapper);
3,676,367✔
1350
}
3,676,367✔
1351

3,676,367✔
1352
void destroyCtxInRequest(SRequestObj *pRequest) {
3,676,367✔
1353
  schedulerFreeJob(&pRequest->body.queryJob, 0);
3,676,367✔
1354
  qDestroyQuery(pRequest->pQuery);
1355
  pRequest->pQuery = NULL;
185,157,386✔
1356
  destorySqlCallbackWrapper(pRequest->pWrapper);
185,157,386✔
1357
  pRequest->pWrapper = NULL;
185,157,386✔
1358
}
185,157,386✔
1359

1360
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
185,158,335✔
1361
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
1362
  SRequestObj         *pRequest = pWrapper->pRequest;
185,158,335✔
1363
  SQuery              *pQuery = pRequest->pQuery;
185,158,335✔
1364

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

185,157,591✔
1367
  int64_t analyseStart = taosGetTimestampUs();
185,155,714✔
1368
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
1369
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
1370

185,143,375✔
1371
  if (TSDB_CODE_SUCCESS == code) {
1372
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
185,149,353✔
1373
  }
301,844✔
1374

301,844✔
1375
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
1376

1377
  if (pRequest->parseOnly) {
185,148,013✔
1378
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
185,144,680✔
1379
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
UNCOV
1380
  }
×
UNCOV
1381

×
UNCOV
1382
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
UNCOV
1383
}
×
UNCOV
1384

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

1412
    *ppTarget = pTarget;
UNCOV
1413
  }
×
UNCOV
1414

×
1415
  return code;
×
UNCOV
1416
}
×
UNCOV
1417

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

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

185,143,951✔
UNCOV
1449
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
×
UNCOV
1450
  SRequestObj *pRequest = pWrapper->pRequest;
×
UNCOV
1451
  SQuery      *pQuery = pRequest->pQuery;
×
UNCOV
1452

×
1453
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
1454
    SNode *prevRoot = pQuery->pPrevRoot;
1455
    pQuery->pPrevRoot = NULL;
185,147,090✔
1456
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
168,991,218✔
1457
    return;
168,998,626✔
1458
  }
168,982,370✔
1459

1460
  if (code == TSDB_CODE_SUCCESS) {
1461
    pRequest->stableQuery = pQuery->stableQuery;
169,009,721✔
1462
    if (pQuery->pRoot) {
90,582,846✔
1463
      pRequest->stmtType = pQuery->pRoot->type;
90,582,216✔
1464
    }
90,583,475✔
1465

1466
    if (pQuery->haveResultSet) {
1467
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
1468
                              pRequest->stmtBindVersion > 0);
185,134,610✔
1469
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
168,990,270✔
1470
    }
168,983,380✔
1471
  }
168,992,162✔
1472

1473
  if (code == TSDB_CODE_SUCCESS) {
168,981,939✔
1474
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
1475
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
16,144,340✔
1476
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
16,144,340✔
1477

16,144,340✔
1478
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
16,144,340✔
1479
  } else {
1480
    destorySqlCallbackWrapper(pWrapper);
16,144,340✔
1481
    pRequest->pWrapper = NULL;
3,626,716✔
1482
    qDestroyQuery(pRequest->pQuery);
1483
    pRequest->pQuery = NULL;
3,626,716✔
1484

3,626,716✔
1485
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
1486
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
1487
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1488
      restartAsyncQuery(pRequest, code);
12,517,624✔
1489
      return;
1490
    }
12,517,624✔
1491

12,517,624✔
1492
    // return to app directly
1493
    tscError("req:0x%" PRIx64 ", error occurs, code:%s, return to user app, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
1494
             pRequest->requestId);
1495
    pRequest->code = code;
195,313,486✔
1496
    returnToUser(pRequest);
381,338,534✔
1497
  }
195,314,504✔
1498
}
195,323,693✔
1499

195,313,947✔
1500
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
1501
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
381,346,120✔
1502
                           .requestId = pWrapper->pParseCtx->requestId,
1503
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
204,601,679✔
1504
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
195,309,522✔
1505

1506
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
1507

1508
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
1509
                                &pWrapper->pRequest->body.queryJob);
654,056,192✔
1510
}
654,056,192✔
1511

654,056,192✔
1512
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code);
10,176,162✔
1513

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

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

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

1546
  if (code == TSDB_CODE_SUCCESS) {
10,176,162✔
1547
    // pWrapper->pCatalogReq->forceUpdate = false;
9,645,793✔
1548
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
1549
  }
1550

10,176,162✔
1551
  if (TSDB_CODE_SUCCESS == code) {
530,369✔
1552
    code = phaseAsyncQuery(pWrapper);
1553
  }
530,369✔
1554

530,369✔
1555
  if (TSDB_CODE_SUCCESS != code) {
530,369✔
1556
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
530,369✔
1557
             tstrerror(code), pWrapper->pRequest->requestId);
530,369✔
1558
    destorySqlCallbackWrapper(pWrapper);
1559
    pRequest->pWrapper = NULL;
10,176,162✔
1560
    terrno = code;
1561
    pRequest->code = code;
11,774✔
1562
    doRequestCallback(pRequest, code);
11,774✔
1563
  }
11,774✔
1564
}
11,774✔
1565

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

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

UNCOV
1583
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
×
UNCOV
1584
  int64_t connId = *(int64_t *)taos;
×
UNCOV
1585
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
×
UNCOV
1586
}
×
1587

1588
void taos_query_a_with_reqid(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param, int64_t reqid) {
649,335,667✔
1589
  int64_t connId = *(int64_t *)taos;
649,335,667✔
1590
  taosAsyncQueryImplWithReqid(connId, sql, fp, param, false, reqid);
1591
}
649,340,787✔
1592

649,329,813✔
UNCOV
1593
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
×
1594
  const STscObj *pTscObj = pRequest->pTscObj;
1595

1596
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
1,271,543,711✔
1597
  if (*pCxt == NULL) {
649,341,106✔
1598
    return terrno;
649,339,499✔
1599
  }
649,341,735✔
1600

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

649,331,888✔
UNCOV
1630
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
×
1631
  int32_t              code = TSDB_CODE_SUCCESS;
1632
  STscObj             *pTscObj = pRequest->pTscObj;
649,331,888✔
1633
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
649,334,704✔
1634
  if (pWrapper == NULL) {
649,335,193✔
1635
    code = terrno;
1636
  } else {
1637
    pWrapper->pRequest = pRequest;
649,332,345✔
1638
    pRequest->pWrapper = pWrapper;
649,337,931✔
1639
    *ppWrapper = pWrapper;
1640
  }
1641

649,334,790✔
1642
  if (TSDB_CODE_SUCCESS == code) {
649,335,317✔
1643
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
649,347,482✔
1644
  }
1645

1646
  if (TSDB_CODE_SUCCESS == code) {
649,336,504✔
1647
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
649,343,078✔
1648
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
1649
  }
649,343,078✔
1650

649,323,289✔
UNCOV
1651
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
×
1652
    int64_t syntaxStart = taosGetTimestampUs();
1653

649,327,055✔
1654
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
649,335,114✔
1655
    if (pWrapper->pCatalogReq == NULL) {
649,333,200✔
1656
      code = terrno;
1657
    } else {
1658
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
649,333,589✔
1659
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
1660
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
1661
    }
649,345,033✔
1662

1663
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
1664
  }
650,544,730✔
1665

650,544,730✔
1666
  return code;
650,550,380✔
1667
}
1668

650,550,380✔
1669
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
1,208,690✔
1670
  SSqlCallbackWrapper *pWrapper = NULL;
1,208,690✔
1671
  int32_t              code = TSDB_CODE_SUCCESS;
1,208,690✔
1672

1,208,690✔
1673
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
1,208,690✔
1674
    code = pRequest->prevCode;
1,208,690✔
1675
    terrno = code;
1676
    pRequest->code = code;
1677
    tscDebug("req:0x%" PRIx64 ", call sync query cb with code:%s", pRequest->self, tstrerror(code));
649,330,294✔
1678
    doRequestCallback(pRequest, code);
649,331,112✔
1679
    return;
1680
  }
1681

649,323,999✔
1682
  if (TSDB_CODE_SUCCESS == code) {
644,403,938✔
1683
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
644,415,113✔
1684
  }
1685

1686
  if (TSDB_CODE_SUCCESS == code) {
649,332,019✔
1687
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
4,924,336✔
1688
    code = phaseAsyncQuery(pWrapper);
4,795,490✔
1689
  }
1690

1691
  if (TSDB_CODE_SUCCESS != code) {
128,846✔
1692
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
1693
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
1694
               pRequest->requestId);
1695
    } else {
4,924,336✔
1696
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
4,924,336✔
1697
               pRequest->requestId);
4,924,336✔
1698
    }
4,924,336✔
1699

1700
    destorySqlCallbackWrapper(pWrapper);
4,924,336✔
1701
    pRequest->pWrapper = NULL;
9,680✔
1702
    qDestroyQuery(pRequest->pQuery);
1703
    pRequest->pQuery = NULL;
9,680✔
1704

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

1718
    terrno = code;
1719
    pRequest->code = code;
3,676,367✔
1720
    doRequestCallback(pRequest, code);
3,676,367✔
1721
  }
3,676,367✔
1722
}
3,676,367✔
1723

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

1756
typedef struct SAsyncFetchParam {
1757
  SRequestObj      *pReq;
108,454,918✔
1758
  __taos_async_fn_t fp;
108,454,918✔
1759
  void             *param;
108,454,918✔
1760
} SAsyncFetchParam;
108,454,424✔
1761

108,453,603✔
1762
static int32_t doAsyncFetch(void *pParam) {
1763
  SAsyncFetchParam *param = pParam;
1764
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
108,457,612✔
1765
  taosMemoryFree(param);
108,457,612✔
UNCOV
1766
  return TSDB_CODE_SUCCESS;
×
UNCOV
1767
}
×
1768

1769
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
108,457,612✔
UNCOV
1770
  if (res == NULL || fp == NULL) {
×
1771
    tscError("taos_fetch_rows_a invalid paras");
×
1772
    return;
×
1773
  }
1774
  if (!TD_RES_QUERY(res)) {
1775
    tscError("taos_fetch_rows_a res is NULL");
108,457,852✔
1776
    fp(param, res, TSDB_CODE_APP_ERROR);
108,457,852✔
1777
    return;
2,915✔
1778
  }
2,915✔
1779

1780
  SRequestObj *pRequest = res;
1781
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
108,455,177✔
1782
    fp(param, res, 0);
108,454,697✔
UNCOV
1783
    return;
×
UNCOV
1784
  }
×
1785

1786
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
108,454,697✔
1787
  if (!pParam) {
108,454,697✔
1788
    fp(param, res, terrno);
108,455,177✔
1789
    return;
108,455,177✔
1790
  }
108,455,177✔
UNCOV
1791
  pParam->pReq = pRequest;
×
UNCOV
1792
  pParam->fp = fp;
×
UNCOV
1793
  pParam->param = param;
×
1794
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
1795
  if (TSDB_CODE_SUCCESS != code) {
1796
    taosMemoryFree(pParam);
1797
    fp(param, res, code);
4,188✔
1798
    return;
4,188✔
UNCOV
1799
  }
×
UNCOV
1800
}
×
1801

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

4,188✔
1814
  // set the current block is all consumed
1815
  pResultInfo->convertUcs4 = false;
1816

2,463✔
1817
  // it is a local executed query, no need to do async fetch
2,463✔
UNCOV
1818
  taos_fetch_rows_a(pRequest, fp, param);
×
UNCOV
1819
}
×
1820

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

×
UNCOV
1832
  return pRequest->body.resInfo.pData;
×
UNCOV
1833
}
×
1834

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

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

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

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

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

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

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

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

×
1876
_return:
×
1877

1878
  terrno = code;
UNCOV
1879

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

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

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

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

×
1904
  pRequest->syncQuery = true;
×
UNCOV
1905

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

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

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

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

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

×
1927
  *vgId = vgInfo.vgId;
UNCOV
1928

×
1929
_return:
×
1930

1931
  terrno = code;
UNCOV
1932

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

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

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

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

×
1957
  pRequest->syncQuery = true;
×
UNCOV
1958

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

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

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

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

×
1976
_return:
×
1977

1978
  terrno = code;
1979

1,240✔
1980
  destroyRequest(pRequest);
1,240✔
1981
  return code;
×
UNCOV
1982
}
×
1983

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

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

1,240✔
1996
  if (NULL == tableNameList) {
1,240✔
1997
    return TSDB_CODE_SUCCESS;
×
1998
  }
1,240✔
UNCOV
1999

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

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

1,240✔
UNCOV
2015
  pRequest->syncQuery = true;
×
2016

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

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

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

×
2032
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
2033

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

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

×
UNCOV
2051
TAOS_STMT *taos_stmt_init(TAOS *taos) {
×
2052
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
2053
  if (NULL == pObj) {
2054
    tscError("invalid parameter for %s", __FUNCTION__);
381,052✔
2055
    terrno = TSDB_CODE_TSC_DISCONNECTED;
380,911✔
2056
    return NULL;
×
2057
  }
2058

380,911✔
2059
  TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL);
2060
  if (NULL == pStmt) {
381,197✔
2061
    tscError("stmt init failed, errcode:%s", terrstr());
2062
  }
UNCOV
2063
  releaseTscObj(*(int64_t *)taos);
×
UNCOV
2064

×
UNCOV
2065
  return pStmt;
×
UNCOV
2066
}
×
UNCOV
2067

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

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

28,664✔
2082
  return pStmt;
28,664✔
UNCOV
2083
}
×
UNCOV
2084

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

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

950,848✔
UNCOV
2099
  return pStmt;
×
UNCOV
2100
}
×
UNCOV
2101

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

20,608✔
UNCOV
2109
  return stmtPrepare(stmt, sql, length);
×
UNCOV
2110
}
×
UNCOV
2111

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

2119
  int32_t code = stmtSetTbName(stmt, name);
19,988✔
2120
  if (code) {
19,988✔
2121
    return code;
2122
  }
UNCOV
2123

×
2124
  if (tags) {
2125
    return stmtSetTbTags(stmt, tags);
2126
  }
13,632,986✔
2127

13,632,986✔
2128
  return TSDB_CODE_SUCCESS;
×
UNCOV
2129
}
×
UNCOV
2130

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

5,393✔
UNCOV
2138
  return stmtSetTbName(stmt, name);
×
UNCOV
2139
}
×
UNCOV
2140

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

UNCOV
2148
  return stmtSetTbTags(stmt, tags);
×
UNCOV
2149
}
×
UNCOV
2150

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

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

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

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

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

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

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

2187
  if (bind->num > 1) {
2188
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
68,122✔
2189
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
2190
    return terrno;
2191
  }
335,030,782✔
2192

335,030,782✔
UNCOV
2193
  return stmtBindBatch(stmt, bind, -1);
×
UNCOV
2194
}
×
UNCOV
2195

×
2196
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
2197
  if (stmt == NULL || bind == NULL) {
2198
    tscError("NULL parameter for %s", __FUNCTION__);
338,823,770✔
2199
    terrno = TSDB_CODE_INVALID_PARA;
3,658✔
2200
    return terrno;
3,658✔
UNCOV
2201
  }
×
2202

2203
  if (bind->num <= 0 || bind->num > INT16_MAX) {
2204
    tscError("invalid bind num %d", bind->num);
340,487,841✔
2205
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
338,907,390✔
2206
    return terrno;
337,111,120✔
UNCOV
2207
  }
×
UNCOV
2208

×
2209
  int32_t insert = 0;
2210
  int32_t code = stmtIsInsert(stmt, &insert);
337,111,120✔
UNCOV
2211
  if (TSDB_CODE_SUCCESS != code) {
×
2212
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2213
    return code;
×
2214
  }
2215
  if (0 == insert && bind->num > 1) {
2216
    tscError("only one row data allowed for query");
337,111,120✔
2217
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
2218
    return terrno;
2219
  }
30,800✔
2220

30,800✔
UNCOV
2221
  return stmtBindBatch(stmt, bind, -1);
×
UNCOV
2222
}
×
UNCOV
2223

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

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

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

324,558,736✔
UNCOV
2249
  return stmtBindBatch(stmt, bind, colIdx);
×
UNCOV
2250
}
×
UNCOV
2251

×
2252
int taos_stmt_add_batch(TAOS_STMT *stmt) {
2253
  if (stmt == NULL) {
2254
    tscError("NULL parameter for %s", __FUNCTION__);
324,558,736✔
2255
    terrno = TSDB_CODE_INVALID_PARA;
2256
    return terrno;
2257
  }
4,628,392✔
2258

4,628,392✔
UNCOV
2259
  return stmtAddBatch(stmt);
×
UNCOV
2260
}
×
UNCOV
2261

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

×
UNCOV
2269
  return stmtExec(stmt);
×
UNCOV
2270
}
×
UNCOV
2271

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

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

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

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

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

10,266✔
2299
  return stmtGetParam(stmt, idx, type, bytes);
×
UNCOV
2300
}
×
UNCOV
2301

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

2309
  return stmtUseResult(stmt);
5,056✔
2310
}
5,056✔
UNCOV
2311

×
UNCOV
2312
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
×
UNCOV
2313

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

4,060✔
UNCOV
2321
  return stmtAffectedRows(stmt);
×
UNCOV
2322
}
×
UNCOV
2323

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

409,719✔
UNCOV
2331
  return stmtAffectedRowsOnce(stmt);
×
UNCOV
2332
}
×
UNCOV
2333

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

19,954✔
UNCOV
2341
  return stmtClose(stmt);
×
UNCOV
2342
}
×
UNCOV
2343

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

19,954✔
2357
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
2358

2359
  releaseTscObj(*(int64_t *)taos);
21,970✔
2360

21,970✔
UNCOV
2361
  return pStmt;
×
UNCOV
2362
}
×
UNCOV
2363

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

7,431,362✔
UNCOV
2371
  return stmtPrepare2(stmt, sql, length);
×
UNCOV
2372
}
×
UNCOV
2373

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

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

×
2389
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
2390
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
2391
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
7,433,136✔
2392
    }
23,772,462✔
2393
    pStmt->execSemWaited = true;
16,335,324✔
2394
  }
16,335,431✔
2395

16,334,767✔
2396
  int32_t code = TSDB_CODE_SUCCESS;
265✔
UNCOV
2397
  for (int i = 0; i < bindv->count; ++i) {
×
UNCOV
2398
    if (bindv->tbnames && bindv->tbnames[i]) {
×
2399
      code = stmtSetTbName2(stmt, bindv->tbnames[i]);
2400
      if (code) {
2401
        terrno = code;
2402
        STMT2_ELOG("set tbname failed, code:%s", tstrerror(code));
16,335,575✔
2403
        return terrno;
16,334,222✔
2404
      }
10,106,372✔
2405
    }
6,229,474✔
2406

2,013,668✔
2407
    SVCreateTbReq *pCreateTbReq = NULL;
4,214,995✔
2408
    if (bindv->tags && bindv->tags[i]) {
2409
      code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
2410
    } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
2411
      code = stmtCheckTags2(stmt, &pCreateTbReq);
UNCOV
2412
    } else if (pStmt->sql.autoCreateTbl) {
×
2413
      // if (pStmt->sql.autoCreateTbl) {
2414
      //   pStmt->sql.autoCreateTbl = false;
2415
      //   STMT2_WLOG_E("sql is autoCreateTbl, but no tags");
16,331,582✔
UNCOV
2416
      // }
×
2417
      code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
×
UNCOV
2418
    }
×
2419

2420
    if (code) {
2421
      terrno = code;
16,331,582✔
2422
      STMT2_ELOG("set tags failed, code:%s", tstrerror(code));
16,338,975✔
2423
      return terrno;
2424
    }
16,338,163✔
2425

5,059✔
2426
    if (bindv->bind_cols && bindv->bind_cols[i]) {
5,059✔
UNCOV
2427
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
×
2428

2429
      if (bind->num <= 0 || bind->num > INT16_MAX) {
2430
        STMT2_ELOG("bind num:%d must > 0 and < INT16_MAX", bind->num);
16,334,999✔
UNCOV
2431
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2432
        return terrno;
×
UNCOV
2433
      }
×
2434

2435
      if (!stmt2IsInsert(stmt) && bind->num > 1) {
2436
        STMT2_ELOG_E("only one row data allowed for query");
16,335,930✔
2437
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
16,338,697✔
2438
        return terrno;
270✔
UNCOV
2439
      }
×
UNCOV
2440

×
2441
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
2442
      if (TSDB_CODE_SUCCESS != code) {
2443
        terrno = code;
2444
        STMT2_ELOG("bind batch failed, code:%s", tstrerror(code));
2445
        return terrno;
7,437,408✔
2446
      }
2447
    }
UNCOV
2448
  }
×
2449

UNCOV
2450
  return code;
×
UNCOV
2451
}
×
UNCOV
2452

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

×
2460
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
UNCOV
2461

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

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

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

7,435,436✔
2489
  return code_s;
×
UNCOV
2490
}
×
UNCOV
2491

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

19,954✔
UNCOV
2499
  return stmtExec2(stmt, affected_rows);
×
UNCOV
2500
}
×
UNCOV
2501

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

956✔
UNCOV
2509
  return stmtClose2(stmt);
×
UNCOV
2510
}
×
UNCOV
2511

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

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

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

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

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

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

2555
  return stmtUseResult2(stmt);
3,421✔
2556
}
3,421✔
UNCOV
2557

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

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

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

2583
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