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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

53922.97 hits per line

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

28.19
/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 "tdatablock.h"
28
#include "tglobal.h"
29
#include "tmsg.h"
30
#include "tref.h"
31
#include "trpc.h"
32
#include "version.h"
33
#include "tconv.h"
34

35
#define TSC_VAR_NOT_RELEASE 1
36
#define TSC_VAR_RELEASED    0
37

38
static int32_t sentinel = TSC_VAR_NOT_RELEASE;
39
static int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper);
40

41
int taos_options(TSDB_OPTION option, const void *arg, ...) {
38✔
42
  if (arg == NULL) {
38!
43
    return TSDB_CODE_INVALID_PARA;
×
44
  }
45
  static int32_t lock = 0;
46

47
  for (int i = 1; atomic_val_compare_exchange_32(&lock, 0, 1) != 0; ++i) {
38!
48
    if (i % 1000 == 0) {
×
49
      (void)sched_yield();
×
50
    }
51
  }
52

53
  int ret = taos_options_imp(option, (const char *)arg);
38✔
54
  atomic_store_32(&lock, 0);
38✔
55
  return ret;
38✔
56
}
57

58
#ifndef WINDOWS
59
static void freeTz(void *p){
×
60
  timezone_t tz = *(timezone_t *)p;
×
61
  tzfree(tz);
×
62
}
×
63

64
int32_t tzInit(){
217✔
65
  pTimezoneMap = taosHashInit(0, MurmurHash3_32, false, HASH_ENTRY_LOCK);
217✔
66
  if (pTimezoneMap == NULL) {
217!
67
    return terrno;
×
68
  }
69
  taosHashSetFreeFp(pTimezoneMap, freeTz);
217✔
70

71
  pTimezoneNameMap = taosHashInit(0, taosIntHash_64, false, HASH_ENTRY_LOCK);
217✔
72
  if (pTimezoneNameMap == NULL) {
217!
73
    return terrno;
×
74
  }
75
  return 0;
217✔
76
}
77

78
void tzCleanup(){
217✔
79
  taosHashCleanup(pTimezoneMap);
217✔
80
  taosHashCleanup(pTimezoneNameMap);
217✔
81
}
217✔
82

83
static timezone_t setConnnectionTz(const char* val){
×
84
  timezone_t tz = NULL;
×
85
  timezone_t *tmp = taosHashGet(pTimezoneMap, val, strlen(val));
×
86
  if (tmp != NULL && *tmp != NULL){
×
87
    tz = *tmp;
×
88
    goto END;
×
89
  }
90

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

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

120
END:
×
121
  return tz;
×
122
}
123
#endif
124

125
static int32_t setConnectionOption(TAOS *taos, TSDB_OPTION_CONNECTION option, const char* val){
×
126
  if (taos == NULL) {
×
127
    return terrno = TSDB_CODE_INVALID_PARA;
×
128
  }
129

130
#ifdef WINDOWS
131
  if (option == TSDB_OPTION_CONNECTION_TIMEZONE){
132
    return terrno = TSDB_CODE_NOT_SUPPORTTED_IN_WINDOWS;
133
  }
134
#endif
135

136
  if (option < TSDB_OPTION_CONNECTION_CLEAR || option >= TSDB_MAX_OPTIONS_CONNECTION){
×
137
    return terrno = TSDB_CODE_INVALID_PARA;
×
138
  }
139

140
  int32_t code = taos_init();
×
141
  // initialize global config
142
  if (code != 0) {
×
143
    return terrno = code;
×
144
  }
145

146
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
147
  if (NULL == pObj) {
×
148
    tscError("invalid parameter for %s", __func__);
×
149
    return terrno;
×
150
  }
151

152
  if (option == TSDB_OPTION_CONNECTION_CLEAR){
×
153
    val = NULL;
×
154
  }
155

156
  if (option == TSDB_OPTION_CONNECTION_CHARSET || option == TSDB_OPTION_CONNECTION_CLEAR) {
×
157
    if (val != NULL) {
×
158
      if (!taosValidateEncodec(val)) {
×
159
        code = terrno;
×
160
        goto END;
×
161
      }
162
      void *tmp = taosConvInit(val);
×
163
      if (tmp == NULL) {
×
164
        code = terrno;
×
165
        goto END;
×
166
      }
167
      pObj->optionInfo.charsetCxt = tmp;
×
168
    }else{
169
      pObj->optionInfo.charsetCxt = NULL;
×
170
    }
171
  }
172

173
  if (option == TSDB_OPTION_CONNECTION_TIMEZONE || option == TSDB_OPTION_CONNECTION_CLEAR) {
×
174
#ifndef WINDOWS
175
    if (val != NULL){
×
176
      if (val[0] == 0){
×
177
        val = "UTC";
×
178
      }
179
      timezone_t tz = setConnnectionTz(val);
×
180
      if (tz == NULL){
×
181
        code = terrno;
×
182
        goto END;
×
183
      }
184
      pObj->optionInfo.timezone = tz;
×
185
    } else {
186
      pObj->optionInfo.timezone = NULL;
×
187
    }
188
#endif
189
  }
190

191
  if (option == TSDB_OPTION_CONNECTION_USER_APP || option == TSDB_OPTION_CONNECTION_CLEAR) {
×
192
    if (val != NULL) {
×
193
      tstrncpy(pObj->optionInfo.userApp, val, sizeof(pObj->optionInfo.userApp));
×
194
    } else {
195
      pObj->optionInfo.userApp[0] = 0;
×
196
    }
197
  }
198

199
  if (option == TSDB_OPTION_CONNECTION_USER_IP || option == TSDB_OPTION_CONNECTION_CLEAR) {
×
200
    if (val != NULL) {
×
201
      pObj->optionInfo.userIp = taosInetAddr(val);
×
202
      if (pObj->optionInfo.userIp == INADDR_NONE){
×
203
        code = TSDB_CODE_INVALID_PARA;
×
204
        goto END;
×
205
      }
206
    } else {
207
      pObj->optionInfo.userIp = INADDR_NONE;
×
208
    }
209
  }
210

211
END:
×
212
  releaseTscObj(*(int64_t *)taos);
×
213
  return terrno = code;
×
214
}
215

216
int taos_options_connection(TAOS *taos, TSDB_OPTION_CONNECTION option, const void *arg, ...){
×
217
  return setConnectionOption(taos, option, (const char *)arg);
×
218
}
219

220
// this function may be called by user or system, or by both simultaneously.
221
void taos_cleanup(void) {
217✔
222
  tscDebug("start to cleanup client environment");
217✔
223
  if (atomic_val_compare_exchange_32(&sentinel, TSC_VAR_NOT_RELEASE, TSC_VAR_RELEASED) != TSC_VAR_NOT_RELEASE) {
217!
UNCOV
224
    return;
×
225
  }
226

227
  monitorClose();
217✔
228
  tscStopCrashReport();
217✔
229

230
  hbMgrCleanUp();
217✔
231

232
  catalogDestroy();
217✔
233
  schedulerDestroy();
217✔
234

235
  fmFuncMgtDestroy();
217✔
236
  qCleanupKeywordsTable();
217✔
237

238
  if (TSDB_CODE_SUCCESS != cleanupTaskQueue()) {
217!
239
    tscWarn("failed to cleanup task queue");
×
240
  }
241

242
#ifndef WINDOWS
243
  tzCleanup();
217✔
244
#endif
245
  tmqMgmtClose();
217✔
246

247
  int32_t id = clientReqRefPool;
217✔
248
  clientReqRefPool = -1;
217✔
249
  taosCloseRef(id);
217✔
250

251
  id = clientConnRefPool;
217✔
252
  clientConnRefPool = -1;
217✔
253
  taosCloseRef(id);
217✔
254

255
  nodesDestroyAllocatorSet();
217✔
256
  //  cleanupAppInfo();
257
  rpcCleanup();
217✔
258
  tscDebug("rpc cleanup");
217✔
259

260
  taosConvDestroy();
217✔
261
  DestroyRegexCache();
217✔
262

263
  tscInfo("all local resources released");
217!
264
  taosCleanupCfg();
217✔
265
  taosCloseLog();
217✔
266
}
267

268
static setConfRet taos_set_config_imp(const char *config) {
×
269
  setConfRet ret = {SET_CONF_RET_SUCC, {0}};
×
270
  // TODO: need re-implementation
271
  return ret;
×
272
}
273

274
setConfRet taos_set_config(const char *config) {
×
275
  // TODO  pthread_mutex_lock(&setConfMutex);
276
  setConfRet ret = taos_set_config_imp(config);
×
277
  //  pthread_mutex_unlock(&setConfMutex);
278
  return ret;
×
279
}
280

281
TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) {
819✔
282
  tscDebug("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
819✔
283
  if (user == NULL) {
819!
284
    user = TSDB_DEFAULT_USER;
×
285
  }
286

287
  if (pass == NULL) {
819!
288
    pass = TSDB_DEFAULT_PASS;
×
289
  }
290

291
  STscObj *pObj = NULL;
819✔
292
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
819✔
293
  if (TSDB_CODE_SUCCESS == code) {
819✔
294
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
803!
295
    if (NULL == rid) {
803!
296
      tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db);
×
297
      return NULL;
×
298
    }
299
    *rid = pObj->id;
803✔
300
    return (TAOS *)rid;
803✔
301
  } else {
302
    terrno = code;
16✔
303
  }
304

305
  return NULL;
16✔
306
}
307

UNCOV
308
int taos_set_notify_cb(TAOS *taos, __taos_notify_fn_t fp, void *param, int type) {
×
UNCOV
309
  if (taos == NULL) {
×
310
    terrno = TSDB_CODE_INVALID_PARA;
×
311
    return terrno;
×
312
  }
313

UNCOV
314
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
UNCOV
315
  if (NULL == pObj) {
×
316
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
317
    tscError("invalid parameter for %s", __func__);
×
318
    return terrno;
×
319
  }
320

UNCOV
321
  switch (type) {
×
UNCOV
322
    case TAOS_NOTIFY_PASSVER: {
×
UNCOV
323
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
UNCOV
324
      pObj->passInfo.fp = fp;
×
UNCOV
325
      pObj->passInfo.param = param;
×
UNCOV
326
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
UNCOV
327
      break;
×
328
    }
329
    case TAOS_NOTIFY_WHITELIST_VER: {
×
330
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
331
      pObj->whiteListInfo.fp = fp;
×
332
      pObj->whiteListInfo.param = param;
×
333
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
334
      break;
×
335
    }
UNCOV
336
    case TAOS_NOTIFY_USER_DROPPED: {
×
UNCOV
337
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
×
UNCOV
338
      pObj->userDroppedInfo.fp = fp;
×
UNCOV
339
      pObj->userDroppedInfo.param = param;
×
UNCOV
340
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
×
UNCOV
341
      break;
×
342
    }
343
    default: {
×
344
      terrno = TSDB_CODE_INVALID_PARA;
×
345
      releaseTscObj(*(int64_t *)taos);
×
346
      return terrno;
×
347
    }
348
  }
349

UNCOV
350
  releaseTscObj(*(int64_t *)taos);
×
UNCOV
351
  return 0;
×
352
}
353

354
typedef struct SFetchWhiteListInfo {
355
  int64_t                     connId;
356
  __taos_async_whitelist_fn_t userCbFn;
357
  void                       *userParam;
358
} SFetchWhiteListInfo;
359

360
int32_t fetchWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
×
361
  SFetchWhiteListInfo *pInfo = (SFetchWhiteListInfo *)param;
×
362
  TAOS                *taos = &pInfo->connId;
×
363
  if (code != TSDB_CODE_SUCCESS) {
×
364
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
365
    taosMemoryFree(pMsg->pData);
×
366
    taosMemoryFree(pMsg->pEpSet);
×
367
    taosMemoryFree(pInfo);
×
368
    return code;
×
369
  }
370

371
  SGetUserWhiteListRsp wlRsp;
372
  if (TSDB_CODE_SUCCESS != tDeserializeSGetUserWhiteListRsp(pMsg->pData, pMsg->len, &wlRsp)) {
×
373
    taosMemoryFree(pMsg->pData);
×
374
    taosMemoryFree(pMsg->pEpSet);
×
375
    taosMemoryFree(pInfo);
×
376
    tFreeSGetUserWhiteListRsp(&wlRsp);
×
377
    return terrno;
×
378
  }
379

380
  uint64_t *pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(uint64_t));
×
381
  if (pWhiteLists == NULL) {
×
382
    taosMemoryFree(pMsg->pData);
×
383
    taosMemoryFree(pMsg->pEpSet);
×
384
    taosMemoryFree(pInfo);
×
385
    tFreeSGetUserWhiteListRsp(&wlRsp);
×
386
    return terrno;
×
387
  }
388

389
  for (int i = 0; i < wlRsp.numWhiteLists; ++i) {
×
390
    pWhiteLists[i] = ((uint64_t)wlRsp.pWhiteLists[i].mask << 32) | wlRsp.pWhiteLists[i].ip;
×
391
  }
392

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

395
  taosMemoryFree(pWhiteLists);
×
396
  taosMemoryFree(pMsg->pData);
×
397
  taosMemoryFree(pMsg->pEpSet);
×
398
  taosMemoryFree(pInfo);
×
399
  tFreeSGetUserWhiteListRsp(&wlRsp);
×
400
  return code;
×
401
}
402

403
void taos_fetch_whitelist_a(TAOS *taos, __taos_async_whitelist_fn_t fp, void *param) {
×
404
  if (NULL == taos) {
×
405
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
406
    return;
×
407
  }
408

409
  int64_t connId = *(int64_t *)taos;
×
410

411
  STscObj *pTsc = acquireTscObj(connId);
×
412
  if (NULL == pTsc) {
×
413
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
414
    return;
×
415
  }
416

417
  SGetUserWhiteListReq req;
418
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
×
419
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
×
420
  if (msgLen < 0) {
×
421
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
422
    releaseTscObj(connId);
×
423
    return;
×
424
  }
425

426
  void *pReq = taosMemoryMalloc(msgLen);
×
427
  if (pReq == NULL) {
×
428
    fp(param, terrno, taos, 0, NULL);
×
429
    releaseTscObj(connId);
×
430
    return;
×
431
  }
432

433
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
×
434
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
435
    taosMemoryFree(pReq);
×
436
    releaseTscObj(connId);
×
437
    return;
×
438
  }
439

440
  SFetchWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchWhiteListInfo));
×
441
  if (pParam == NULL) {
×
442
    fp(param, terrno, taos, 0, NULL);
×
443
    taosMemoryFree(pReq);
×
444
    releaseTscObj(connId);
×
445
    return;
×
446
  }
447

448
  pParam->connId = connId;
×
449
  pParam->userCbFn = fp;
×
450
  pParam->userParam = param;
×
451
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
452
  if (pSendInfo == NULL) {
×
453
    fp(param, terrno, taos, 0, NULL);
×
454
    taosMemoryFree(pParam);
×
455
    taosMemoryFree(pReq);
×
456
    releaseTscObj(connId);
×
457
    return;
×
458
  }
459

460
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
×
461
  pSendInfo->requestId = generateRequestId();
×
462
  pSendInfo->requestObjRefId = 0;
×
463
  pSendInfo->param = pParam;
×
464
  pSendInfo->fp = fetchWhiteListCallbackFn;
×
465
  pSendInfo->msgType = TDMT_MND_GET_USER_WHITELIST;
×
466

467
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
×
468
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
×
469
    tscWarn("failed to async send msg to server");
×
470
  }
471
  releaseTscObj(connId);
×
472
  return;
×
473
}
474

475
void taos_close_internal(void *taos) {
821✔
476
  if (taos == NULL) {
821!
477
    return;
×
478
  }
479

480
  STscObj *pTscObj = (STscObj *)taos;
821✔
481
  tscDebug("0x%" PRIx64 " try to close connection, numOfReq:%d", pTscObj->id, pTscObj->numOfReqs);
821✔
482

483
  if (TSDB_CODE_SUCCESS != taosRemoveRef(clientConnRefPool, pTscObj->id)) {
821!
484
    tscError("0x%" PRIx64 " failed to remove ref from conn pool", pTscObj->id);
×
485
  }
486
}
487

488
void taos_close(TAOS *taos) {
819✔
489
  if (taos == NULL) {
819✔
490
    return;
17✔
491
  }
492

493
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
802✔
494
  if (NULL == pObj) {
802!
495
    taosMemoryFree(taos);
×
496
    return;
×
497
  }
498

499
  taos_close_internal(pObj);
802✔
500
  releaseTscObj(*(int64_t *)taos);
802✔
501
  taosMemoryFree(taos);
802!
502
}
503

504
int taos_errno(TAOS_RES *res) {
7,893✔
505
  if (res == NULL || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
7,893!
506
    return terrno;
6✔
507
  }
508

509
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
7,887!
UNCOV
510
    return 0;
×
511
  }
512

513
  return ((SRequestObj *)res)->code;
7,887✔
514
}
515

516
const char *taos_errstr(TAOS_RES *res) {
39✔
517
  if (res == NULL || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
39!
518
    return (const char *)tstrerror(terrno);
20✔
519
  }
520

521
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
19!
522
    return "success";
×
523
  }
524

525
  SRequestObj *pRequest = (SRequestObj *)res;
19✔
526
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
19!
527
    return pRequest->msgBuf;
10✔
528
  } else {
529
    return (const char *)tstrerror(pRequest->code);
9✔
530
  }
531
}
532

533
void taos_free_result(TAOS_RES *res) {
7,543✔
534
  if (NULL == res) {
7,543✔
535
    return;
32✔
536
  }
537

538
  tscDebug("taos free res %p", res);
7,511✔
539

540
  if (TD_RES_QUERY(res)) {
7,511!
541
    SRequestObj *pRequest = (SRequestObj *)res;
7,511✔
542
    tscDebug("0x%" PRIx64 " taos_free_result start to free query", pRequest->requestId);
7,511✔
543
    destroyRequest(pRequest);
7,511✔
544
    return;
7,510✔
545
  }
UNCOV
546
  SMqRspObj *pRsp = (SMqRspObj *)res;
×
UNCOV
547
  if (TD_RES_TMQ(res)) {
×
UNCOV
548
    tDeleteMqDataRsp(&pRsp->dataRsp);
×
UNCOV
549
    doFreeReqResultInfo(&pRsp->resInfo);
×
UNCOV
550
  } else if (TD_RES_TMQ_METADATA(res)) {
×
UNCOV
551
    tDeleteSTaosxRsp(&pRsp->dataRsp);
×
UNCOV
552
    doFreeReqResultInfo(&pRsp->resInfo);
×
UNCOV
553
  } else if (TD_RES_TMQ_META(res)) {
×
UNCOV
554
    tDeleteMqMetaRsp(&pRsp->metaRsp);
×
UNCOV
555
  } else if (TD_RES_TMQ_BATCH_META(res)) {
×
UNCOV
556
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
×
557
  }
UNCOV
558
  taosMemoryFree(pRsp);
×
559
}
560

UNCOV
561
void taos_kill_query(TAOS *taos) {
×
UNCOV
562
  if (NULL == taos) {
×
UNCOV
563
    return;
×
564
  }
565

UNCOV
566
  int64_t  rid = *(int64_t *)taos;
×
UNCOV
567
  STscObj *pTscObj = acquireTscObj(rid);
×
UNCOV
568
  if (pTscObj) {
×
UNCOV
569
    stopAllRequests(pTscObj->pRequests);
×
570
  }
UNCOV
571
  releaseTscObj(rid);
×
572
}
573

574
int taos_field_count(TAOS_RES *res) {
21,169✔
575
  if (res == NULL || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
21,169!
576
    return 0;
×
577
  }
578

579
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
21,169✔
580
  return pResInfo->numOfCols;
21,169✔
581
}
582

583
int taos_num_fields(TAOS_RES *res) { return taos_field_count(res); }
16,083✔
584

585
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
1,008✔
586
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,008!
587
    return NULL;
49✔
588
  }
589

590
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
959✔
591
  return pResInfo->userFields;
959✔
592
}
593

594
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
7,297✔
595
TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqid) {
×
596
  return taosQueryImplWithReqid(taos, sql, false, reqid);
×
597
}
598

599
TAOS_ROW taos_fetch_row(TAOS_RES *res) {
14,071✔
600
  if (res == NULL) {
14,071!
601
    return NULL;
×
602
  }
603

604
  if (TD_RES_QUERY(res)) {
14,071!
605
    SRequestObj *pRequest = (SRequestObj *)res;
14,071✔
606
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
14,071!
607
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0 || pRequest->killed) {
14,071!
608
      return NULL;
3✔
609
    }
610

611
    if (pRequest->inCallback) {
14,068!
612
      tscError("can not call taos_fetch_row before query callback ends.");
×
613
      terrno = TSDB_CODE_TSC_INVALID_OPERATION;
×
614
      return NULL;
×
615
    }
616

617
    return doAsyncFetchRows(pRequest, true, true);
14,068✔
UNCOV
618
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
×
UNCOV
619
    SMqRspObj      *msg = ((SMqRspObj *)res);
×
UNCOV
620
    SReqResultInfo *pResultInfo = NULL;
×
UNCOV
621
    if (msg->resIter == -1) {
×
UNCOV
622
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
×
623
        return NULL;
×
624
      }
625
    } else {
UNCOV
626
      pResultInfo = tmqGetCurResInfo(res);
×
627
    }
628

UNCOV
629
    if (pResultInfo->current < pResultInfo->numOfRows) {
×
UNCOV
630
      doSetOneRowPtr(pResultInfo);
×
UNCOV
631
      pResultInfo->current += 1;
×
UNCOV
632
      return pResultInfo->row;
×
633
    } else {
UNCOV
634
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
×
UNCOV
635
        return NULL;
×
636
      }
637

UNCOV
638
      doSetOneRowPtr(pResultInfo);
×
UNCOV
639
      pResultInfo->current += 1;
×
UNCOV
640
      return pResultInfo->row;
×
641
    }
642
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
643
    return NULL;
×
644
  } else {
645
    tscError("invalid result passed to taos_fetch_row");
×
646
    terrno = TSDB_CODE_TSC_INTERNAL_ERROR;
×
647
    return NULL;
×
648
  }
649
}
650

651
int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
516✔
652
  return taos_print_row_with_size(str, INT32_MAX, row, fields, num_fields);
516✔
653
}
654
int taos_print_row_with_size(char *str, uint32_t size, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
516✔
655
  int32_t len = 0;
516✔
656
  for (int i = 0; i < num_fields; ++i) {
1,064✔
657
    if (i > 0 && len < size - 1) {
548!
658
      str[len++] = ' ';
32✔
659
    }
660

661
    if (row[i] == NULL) {
548✔
662
      len += tsnprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
12✔
663
      continue;
12✔
664
    }
665

666
    switch (fields[i].type) {
536!
667
      case TSDB_DATA_TYPE_TINYINT:
6✔
668
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
6✔
669
        break;
6✔
670

671
      case TSDB_DATA_TYPE_UTINYINT:
×
672
        len += tsnprintf(str + len, size - len, "%u", *((uint8_t *)row[i]));
×
673
        break;
×
674

675
      case TSDB_DATA_TYPE_SMALLINT:
2✔
676
        len += tsnprintf(str + len, size - len, "%d", *((int16_t *)row[i]));
2✔
677
        break;
2✔
678

679
      case TSDB_DATA_TYPE_USMALLINT:
×
680
        len += tsnprintf(str + len, size - len, "%u", *((uint16_t *)row[i]));
×
681
        break;
×
682

683
      case TSDB_DATA_TYPE_INT:
8✔
684
        len += tsnprintf(str + len, size - len, "%d", *((int32_t *)row[i]));
8✔
685
        break;
8✔
686

687
      case TSDB_DATA_TYPE_UINT:
×
688
        len += tsnprintf(str + len, size - len, "%u", *((uint32_t *)row[i]));
×
689
        break;
×
690

691
      case TSDB_DATA_TYPE_BIGINT:
6✔
692
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
6✔
693
        break;
6✔
694

695
      case TSDB_DATA_TYPE_UBIGINT:
×
696
        len += tsnprintf(str + len, size - len, "%" PRIu64, *((uint64_t *)row[i]));
×
697
        break;
×
698

UNCOV
699
      case TSDB_DATA_TYPE_FLOAT: {
×
UNCOV
700
        float fv = 0;
×
UNCOV
701
        fv = GET_FLOAT_VAL(row[i]);
×
UNCOV
702
        len += tsnprintf(str + len, size - len, "%f", fv);
×
UNCOV
703
      } break;
×
704

UNCOV
705
      case TSDB_DATA_TYPE_DOUBLE: {
×
UNCOV
706
        double dv = 0;
×
UNCOV
707
        dv = GET_DOUBLE_VAL(row[i]);
×
UNCOV
708
        len += tsnprintf(str + len, size - len, "%lf", dv);
×
UNCOV
709
      } break;
×
710

711
      case TSDB_DATA_TYPE_VARBINARY: {
×
712
        void    *data = NULL;
×
713
        uint32_t tmp = 0;
×
714
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
×
715
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
×
716
          break;
×
717
        }
718
        uint32_t copyLen = TMIN(size - len - 1, tmp);
×
719
        (void)memcpy(str + len, data, copyLen);
×
720
        len += copyLen;
×
721
        taosMemoryFree(data);
×
722
      } break;
×
723
      case TSDB_DATA_TYPE_BINARY:
513✔
724
      case TSDB_DATA_TYPE_NCHAR:
725
      case TSDB_DATA_TYPE_GEOMETRY: {
726
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
513✔
727
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
513!
UNCOV
728
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
×
729
          if (charLen > fields[i].bytes || charLen < 0) {
513!
730
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
×
731
            break;
×
732
          }
733
        } else {
UNCOV
734
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
×
UNCOV
735
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
×
736
            break;
×
737
          }
738
        }
739

740
        uint32_t copyLen = TMIN(size - len - 1, charLen);
513✔
741
        (void)memcpy(str + len, row[i], copyLen);
513✔
742
        len += copyLen;
513✔
743
      } break;
513✔
744

745
      case TSDB_DATA_TYPE_TIMESTAMP:
1✔
746
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
1✔
747
        break;
1✔
748

749
      case TSDB_DATA_TYPE_BOOL:
×
750
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
×
751
      default:
×
752
        break;
×
753
    }
754

755
    if (len >= size - 1) {
536!
756
      break;
×
757
    }
758
  }
759
  if (len < size) {
516!
760
    str[len] = 0;
516✔
761
  }
762

763
  return len;
516✔
764
}
765

766
int *taos_fetch_lengths(TAOS_RES *res) {
721✔
767
  if (res == NULL || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
721!
768
    return NULL;
×
769
  }
770

771
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
721✔
772
  return pResInfo->length;
721✔
773
}
774

775
TAOS_ROW *taos_result_block(TAOS_RES *res) {
×
776
  if (res == NULL || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
777
    terrno = TSDB_CODE_INVALID_PARA;
×
778
    return NULL;
×
779
  }
780

781
  if (taos_is_update_query(res)) {
×
782
    return NULL;
×
783
  }
784

785
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
786
  return &pResInfo->row;
×
787
}
788

789
// todo intergrate with tDataTypes
790
const char *taos_data_type(int type) {
×
791
  switch (type) {
×
792
    case TSDB_DATA_TYPE_NULL:
×
793
      return "TSDB_DATA_TYPE_NULL";
×
794
    case TSDB_DATA_TYPE_BOOL:
×
795
      return "TSDB_DATA_TYPE_BOOL";
×
796
    case TSDB_DATA_TYPE_TINYINT:
×
797
      return "TSDB_DATA_TYPE_TINYINT";
×
798
    case TSDB_DATA_TYPE_SMALLINT:
×
799
      return "TSDB_DATA_TYPE_SMALLINT";
×
800
    case TSDB_DATA_TYPE_INT:
×
801
      return "TSDB_DATA_TYPE_INT";
×
802
    case TSDB_DATA_TYPE_BIGINT:
×
803
      return "TSDB_DATA_TYPE_BIGINT";
×
804
    case TSDB_DATA_TYPE_FLOAT:
×
805
      return "TSDB_DATA_TYPE_FLOAT";
×
806
    case TSDB_DATA_TYPE_DOUBLE:
×
807
      return "TSDB_DATA_TYPE_DOUBLE";
×
808
    case TSDB_DATA_TYPE_VARCHAR:
×
809
      return "TSDB_DATA_TYPE_VARCHAR";
×
810
      //    case TSDB_DATA_TYPE_BINARY:          return "TSDB_DATA_TYPE_VARCHAR";
811
    case TSDB_DATA_TYPE_TIMESTAMP:
×
812
      return "TSDB_DATA_TYPE_TIMESTAMP";
×
813
    case TSDB_DATA_TYPE_NCHAR:
×
814
      return "TSDB_DATA_TYPE_NCHAR";
×
815
    case TSDB_DATA_TYPE_JSON:
×
816
      return "TSDB_DATA_TYPE_JSON";
×
817
    case TSDB_DATA_TYPE_GEOMETRY:
×
818
      return "TSDB_DATA_TYPE_GEOMETRY";
×
819
    case TSDB_DATA_TYPE_UTINYINT:
×
820
      return "TSDB_DATA_TYPE_UTINYINT";
×
821
    case TSDB_DATA_TYPE_USMALLINT:
×
822
      return "TSDB_DATA_TYPE_USMALLINT";
×
823
    case TSDB_DATA_TYPE_UINT:
×
824
      return "TSDB_DATA_TYPE_UINT";
×
825
    case TSDB_DATA_TYPE_UBIGINT:
×
826
      return "TSDB_DATA_TYPE_UBIGINT";
×
827
    case TSDB_DATA_TYPE_VARBINARY:
×
828
      return "TSDB_DATA_TYPE_VARBINARY";
×
829
    case TSDB_DATA_TYPE_DECIMAL:
×
830
      return "TSDB_DATA_TYPE_DECIMAL";
×
831
    case TSDB_DATA_TYPE_BLOB:
×
832
      return "TSDB_DATA_TYPE_BLOB";
×
833
    case TSDB_DATA_TYPE_MEDIUMBLOB:
×
834
      return "TSDB_DATA_TYPE_MEDIUMBLOB";
×
835
    default:
×
836
      return "UNKNOWN";
×
837
  }
838
}
839

840
const char *taos_get_client_info() { return td_version; }
297✔
841

842
// return int32_t
843
int taos_affected_rows(TAOS_RES *res) {
4,319✔
844
  if (res == NULL || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
4,319!
845
      TD_RES_TMQ_BATCH_META(res)) {
4,319!
846
    return 0;
×
847
  }
848

849
  SRequestObj    *pRequest = (SRequestObj *)res;
4,319✔
850
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
4,319✔
851
  return (int)pResInfo->numOfRows;
4,319✔
852
}
853

854
// return int64_t
855
int64_t taos_affected_rows64(TAOS_RES *res) {
42✔
856
  if (res == NULL || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
42!
857
      TD_RES_TMQ_BATCH_META(res)) {
42!
858
    return 0;
×
859
  }
860

861
  SRequestObj    *pRequest = (SRequestObj *)res;
42✔
862
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
42✔
863
  return pResInfo->numOfRows;
42✔
864
}
865

866
int taos_result_precision(TAOS_RES *res) {
773✔
867
  if (res == NULL || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
773!
868
    return TSDB_TIME_PRECISION_MILLI;
×
869
  }
870

871
  if (TD_RES_QUERY(res)) {
773!
872
    SRequestObj *pRequest = (SRequestObj *)res;
773✔
873
    return pRequest->body.resInfo.precision;
773✔
UNCOV
874
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
×
UNCOV
875
    SReqResultInfo *info = tmqGetCurResInfo(res);
×
UNCOV
876
    return info->precision;
×
877
  }
878
  return TSDB_TIME_PRECISION_MILLI;
×
879
}
880

881
int taos_select_db(TAOS *taos, const char *db) {
61✔
882
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
61✔
883
  if (pObj == NULL) {
61!
884
    releaseTscObj(*(int64_t *)taos);
×
885
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
886
    return TSDB_CODE_TSC_DISCONNECTED;
×
887
  }
888

889
  if (db == NULL || strlen(db) == 0) {
61!
890
    releaseTscObj(*(int64_t *)taos);
×
891
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
892
    return terrno;
×
893
  }
894

895
  char sql[256] = {0};
61✔
896
  (void)snprintf(sql, tListLen(sql), "use %s", db);
61✔
897

898
  TAOS_RES *pRequest = taos_query(taos, sql);
61✔
899
  int32_t   code = taos_errno(pRequest);
61✔
900

901
  taos_free_result(pRequest);
61✔
902
  releaseTscObj(*(int64_t *)taos);
61✔
903
  return code;
61✔
904
}
905

906
void taos_stop_query(TAOS_RES *res) {
8,391✔
907
  if (res == NULL || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
8,391!
908
      TD_RES_TMQ_BATCH_META(res)) {
8,391!
909
    return;
×
910
  }
911

912
  stopAllQueries((SRequestObj *)res);
8,391✔
913
}
914

915
bool taos_is_null(TAOS_RES *res, int32_t row, int32_t col) {
514✔
916
  if (res == NULL || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
514!
917
    return true;
×
918
  }
919
  SReqResultInfo *pResultInfo = tscGetCurResInfo(res);
514✔
920
  if (col >= pResultInfo->numOfCols || col < 0 || row >= pResultInfo->numOfRows || row < 0) {
514!
UNCOV
921
    return true;
×
922
  }
923

924
  SResultColumn *pCol = &pResultInfo->pCol[col];
514✔
925
  if (IS_VAR_DATA_TYPE(pResultInfo->fields[col].type)) {
514!
UNCOV
926
    return (pCol->offset[row] == -1);
×
927
  } else {
928
    return colDataIsNull_f(pCol->nullbitmap, row);
514✔
929
  }
930
}
931

932
bool taos_is_update_query(TAOS_RES *res) { return taos_num_fields(res) == 0; }
×
933

934
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
351✔
935
  int32_t numOfRows = 0;
351✔
936
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
351✔
937
  return numOfRows;
351✔
938
}
939

940
int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) {
351✔
941
  if (res == NULL || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
351!
942
    return 0;
×
943
  }
944

945
  if (TD_RES_QUERY(res)) {
351!
946
    SRequestObj *pRequest = (SRequestObj *)res;
351✔
947

948
    (*rows) = NULL;
351✔
949
    (*numOfRows) = 0;
351✔
950

951
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
351!
952
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
351!
UNCOV
953
      return pRequest->code;
×
954
    }
955

956
    (void)doAsyncFetchRows(pRequest, false, true);
351✔
957

958
    // TODO refactor
959
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
351✔
960
    pResultInfo->current = pResultInfo->numOfRows;
351✔
961

962
    (*rows) = pResultInfo->row;
351✔
963
    (*numOfRows) = pResultInfo->numOfRows;
351✔
964
    return pRequest->code;
351✔
UNCOV
965
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
×
UNCOV
966
    SReqResultInfo *pResultInfo = NULL;
×
UNCOV
967
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
×
UNCOV
968
    if (code != 0) return code;
×
969

UNCOV
970
    pResultInfo->current = pResultInfo->numOfRows;
×
UNCOV
971
    (*rows) = pResultInfo->row;
×
UNCOV
972
    (*numOfRows) = pResultInfo->numOfRows;
×
UNCOV
973
    return 0;
×
974
  } else {
975
    tscError("taos_fetch_block_s invalid res type");
×
976
    return -1;
×
977
  }
978
}
979

UNCOV
980
int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) {
×
UNCOV
981
  *numOfRows = 0;
×
UNCOV
982
  *pData = NULL;
×
983

UNCOV
984
  if (res == NULL || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
985
    return 0;
×
986
  }
987

UNCOV
988
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
×
989
    SReqResultInfo *pResultInfo = NULL;
×
990
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
×
991
    if (code != 0) {
×
992
      (*numOfRows) = 0;
×
993
      return 0;
×
994
    }
995

996
    pResultInfo->current = pResultInfo->numOfRows;
×
997
    (*numOfRows) = pResultInfo->numOfRows;
×
998
    (*pData) = (void *)pResultInfo->pData;
×
999
    return 0;
×
1000
  }
1001

UNCOV
1002
  SRequestObj *pRequest = (SRequestObj *)res;
×
1003

UNCOV
1004
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
×
UNCOV
1005
      pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
×
1006
    return pRequest->code;
×
1007
  }
1008

UNCOV
1009
  (void)doAsyncFetchRows(pRequest, false, false);
×
1010

UNCOV
1011
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
×
1012

UNCOV
1013
  pResultInfo->current = pResultInfo->numOfRows;
×
UNCOV
1014
  (*numOfRows) = pResultInfo->numOfRows;
×
UNCOV
1015
  (*pData) = (void *)pResultInfo->pData;
×
1016

UNCOV
1017
  return pRequest->code;
×
1018
}
1019

1020
int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) {
180✔
1021
  if (res == NULL || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
180!
1022
    return 0;
×
1023
  }
1024

1025
  int32_t numOfFields = taos_num_fields(res);
180✔
1026
  if (columnIndex < 0 || columnIndex >= numOfFields || numOfFields == 0) {
180!
1027
    return 0;
×
1028
  }
1029

1030
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
180✔
1031
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
180✔
1032
  if (!IS_VAR_DATA_TYPE(pField->type)) {
180!
1033
    return 0;
×
1034
  }
1035

1036
  return pResInfo->pCol[columnIndex].offset;
180✔
1037
}
1038

1039
int taos_is_null_by_column(TAOS_RES *res, int columnIndex, bool result[], int *rows) {
×
1040
  if (res == NULL || result == NULL || rows == NULL || *rows <= 0 || columnIndex < 0 || TD_RES_TMQ_META(res) ||
×
1041
      TD_RES_TMQ_BATCH_META(res)) {
×
1042
    return TSDB_CODE_INVALID_PARA;
×
1043
  }
1044

1045
  int32_t numOfFields = taos_num_fields(res);
×
1046
  if (columnIndex >= numOfFields || numOfFields == 0) {
×
1047
    return TSDB_CODE_INVALID_PARA;
×
1048
  }
1049

1050
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
1051
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
×
1052
  SResultColumn  *pCol = &pResInfo->pCol[columnIndex];
×
1053

1054
  if (*rows > pResInfo->numOfRows) {
×
1055
    *rows = pResInfo->numOfRows;
×
1056
  }
1057
  if (IS_VAR_DATA_TYPE(pField->type)) {
×
1058
    for (int i = 0; i < *rows; i++) {
×
1059
      if (pCol->offset[i] == -1) {
×
1060
        result[i] = true;
×
1061
      } else {
1062
        result[i] = false;
×
1063
      }
1064
    }
1065
  } else {
1066
    for (int i = 0; i < *rows; i++) {
×
1067
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
×
1068
        result[i] = true;
×
1069
      } else {
1070
        result[i] = false;
×
1071
      }
1072
    }
1073
  }
1074
  return 0;
×
1075
}
1076

1077
int taos_validate_sql(TAOS *taos, const char *sql) {
×
1078
  TAOS_RES *pObj = taosQueryImpl(taos, sql, true, TD_REQ_FROM_APP);
×
1079

1080
  int code = taos_errno(pObj);
×
1081

1082
  taos_free_result(pObj);
×
1083
  return code;
×
1084
}
1085

1086
void taos_reset_current_db(TAOS *taos) {
×
1087
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
1088
  if (pTscObj == NULL) {
×
1089
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1090
    return;
×
1091
  }
1092

1093
  resetConnectDB(pTscObj);
×
1094

1095
  releaseTscObj(*(int64_t *)taos);
×
1096
}
1097

UNCOV
1098
const char *taos_get_server_info(TAOS *taos) {
×
UNCOV
1099
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
UNCOV
1100
  if (pTscObj == NULL) {
×
1101
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1102
    return NULL;
×
1103
  }
1104

UNCOV
1105
  releaseTscObj(*(int64_t *)taos);
×
1106

UNCOV
1107
  return pTscObj->sDetailVer;
×
1108
}
1109

UNCOV
1110
int taos_get_current_db(TAOS *taos, char *database, int len, int *required) {
×
UNCOV
1111
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
UNCOV
1112
  if (pTscObj == NULL) {
×
1113
    return TSDB_CODE_TSC_DISCONNECTED;
×
1114
  }
1115

UNCOV
1116
  int code = TSDB_CODE_SUCCESS;
×
UNCOV
1117
  (void)taosThreadMutexLock(&pTscObj->mutex);
×
UNCOV
1118
  if (database == NULL || len <= 0) {
×
UNCOV
1119
    if (required != NULL) *required = strlen(pTscObj->db) + 1;
×
UNCOV
1120
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
UNCOV
1121
  } else if (len < strlen(pTscObj->db) + 1) {
×
UNCOV
1122
    tstrncpy(database, pTscObj->db, len);
×
UNCOV
1123
    if (required) *required = strlen(pTscObj->db) + 1;
×
UNCOV
1124
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
1125
  } else {
UNCOV
1126
    tstrncpy(database, pTscObj->db, len);
×
UNCOV
1127
    code = 0;
×
1128
  }
UNCOV
1129
_return:
×
UNCOV
1130
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
×
UNCOV
1131
  releaseTscObj(*(int64_t *)taos);
×
UNCOV
1132
  return code;
×
1133
}
1134

1135
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
15,774✔
1136
  if (NULL == pWrapper) {
15,774✔
1137
    return;
8,441✔
1138
  }
1139
  destoryCatalogReq(pWrapper->pCatalogReq);
7,333✔
1140
  taosMemoryFree(pWrapper->pCatalogReq);
7,334!
1141
  qDestroyParseContext(pWrapper->pParseCtx);
7,334✔
1142
  taosMemoryFree(pWrapper);
7,334!
1143
}
1144

1145
void destroyCtxInRequest(SRequestObj *pRequest) {
53✔
1146
  schedulerFreeJob(&pRequest->body.queryJob, 0);
53✔
1147
  qDestroyQuery(pRequest->pQuery);
53✔
1148
  pRequest->pQuery = NULL;
53✔
1149
  destorySqlCallbackWrapper(pRequest->pWrapper);
53✔
1150
  pRequest->pWrapper = NULL;
53✔
1151
}
53✔
1152

1153
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
1,526✔
1154
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
1,526✔
1155
  SRequestObj         *pRequest = pWrapper->pRequest;
1,526✔
1156
  SQuery              *pQuery = pRequest->pQuery;
1,526✔
1157

1158
  qDebug("0x%" PRIx64 " start to semantic analysis,QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
1,526✔
1159

1160
  int64_t analyseStart = taosGetTimestampUs();
1,526✔
1161
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
1,526✔
1162
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
1,526✔
1163

1164
  if (TSDB_CODE_SUCCESS == code) {
1,526!
1165
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
1,526✔
1166
  }
1167

1168
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
1,526✔
1169

1170
  if (pRequest->parseOnly) {
1,526!
UNCOV
1171
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
×
UNCOV
1172
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
×
1173
  }
1174

1175
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
1,526✔
1176
}
1,526✔
1177

1178
int32_t cloneCatalogReq(SCatalogReq **ppTarget, SCatalogReq *pSrc) {
1✔
1179
  int32_t      code = TSDB_CODE_SUCCESS;
1✔
1180
  SCatalogReq *pTarget = taosMemoryCalloc(1, sizeof(SCatalogReq));
1!
1181
  if (pTarget == NULL) {
1!
1182
    code = terrno;
×
1183
  } else {
1184
    pTarget->pDbVgroup = taosArrayDup(pSrc->pDbVgroup, NULL);
1✔
1185
    pTarget->pDbCfg = taosArrayDup(pSrc->pDbCfg, NULL);
1✔
1186
    pTarget->pDbInfo = taosArrayDup(pSrc->pDbInfo, NULL);
1✔
1187
    pTarget->pTableMeta = taosArrayDup(pSrc->pTableMeta, NULL);
1✔
1188
    pTarget->pTableHash = taosArrayDup(pSrc->pTableHash, NULL);
1✔
1189
    pTarget->pUdf = taosArrayDup(pSrc->pUdf, NULL);
1✔
1190
    pTarget->pIndex = taosArrayDup(pSrc->pIndex, NULL);
1✔
1191
    pTarget->pUser = taosArrayDup(pSrc->pUser, NULL);
1✔
1192
    pTarget->pTableIndex = taosArrayDup(pSrc->pTableIndex, NULL);
1✔
1193
    pTarget->pTableCfg = taosArrayDup(pSrc->pTableCfg, NULL);
1✔
1194
    pTarget->pTableTag = taosArrayDup(pSrc->pTableTag, NULL);
1✔
1195
    pTarget->pView = taosArrayDup(pSrc->pView, NULL);
1✔
1196
    pTarget->pTableTSMAs = taosArrayDup(pSrc->pTableTSMAs, NULL);
1✔
1197
    pTarget->pTSMAs = taosArrayDup(pSrc->pTSMAs, NULL);
1✔
1198
    pTarget->qNodeRequired = pSrc->qNodeRequired;
1✔
1199
    pTarget->dNodeRequired = pSrc->dNodeRequired;
1✔
1200
    pTarget->svrVerRequired = pSrc->svrVerRequired;
1✔
1201
    pTarget->forceUpdate = pSrc->forceUpdate;
1✔
1202
    pTarget->cloned = true;
1✔
1203

1204
    *ppTarget = pTarget;
1✔
1205
  }
1206

1207
  return code;
1✔
1208
}
1209

1210
void handleSubQueryFromAnalyse(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, SNode *pRoot) {
1✔
1211
  SRequestObj         *pNewRequest = NULL;
1✔
1212
  SSqlCallbackWrapper *pNewWrapper = NULL;
1✔
1213
  int32_t              code = buildPreviousRequest(pWrapper->pRequest, pWrapper->pRequest->sqlstr, &pNewRequest);
1✔
1214
  if (code) {
1!
1215
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1216
    return;
×
1217
  }
1218

1219
  pNewRequest->pQuery = NULL;
1✔
1220
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode **)&pNewRequest->pQuery);
1✔
1221
  if (pNewRequest->pQuery) {
1!
1222
    pNewRequest->pQuery->pRoot = pRoot;
1✔
1223
    pRoot = NULL;
1✔
1224
    pNewRequest->pQuery->execStage = QUERY_EXEC_STAGE_ANALYSE;
1✔
1225
  }
1226
  if (TSDB_CODE_SUCCESS == code) {
1!
1227
    code = prepareAndParseSqlSyntax(&pNewWrapper, pNewRequest, false);
1✔
1228
  }
1229
  if (TSDB_CODE_SUCCESS == code) {
1!
1230
    code = cloneCatalogReq(&pNewWrapper->pCatalogReq, pWrapper->pCatalogReq);
1✔
1231
  }
1232
  if (TSDB_CODE_SUCCESS == code) {
1!
1233
    doAsyncQueryFromAnalyse(pResultMeta, pNewWrapper, code);
1✔
1234
    nodesDestroyNode(pRoot);
1✔
1235
  } else {
1236
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1237
    return;
×
1238
  }
1239
}
1240

1241
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
1,527✔
1242
  SRequestObj *pRequest = pWrapper->pRequest;
1,527✔
1243
  SQuery      *pQuery = pRequest->pQuery;
1,527✔
1244

1245
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
1,527✔
1246
    SNode *prevRoot = pQuery->pPrevRoot;
1✔
1247
    pQuery->pPrevRoot = NULL;
1✔
1248
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
1✔
1249
    return;
1✔
1250
  }
1251

1252
  if (code == TSDB_CODE_SUCCESS) {
1,526✔
1253
    pRequest->stableQuery = pQuery->stableQuery;
1,464✔
1254
    if (pQuery->pRoot) {
1,464!
1255
      pRequest->stmtType = pQuery->pRoot->type;
1,464✔
1256
    }
1257

1258
    if (pQuery->haveResultSet) {
1,464✔
1259
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols);
382✔
1260
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
382✔
1261
    }
1262
  }
1263

1264
  if (code == TSDB_CODE_SUCCESS) {
1,526✔
1265
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
1,464✔
1266
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
1,464✔
1267
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
1,464✔
1268

1269
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
1,464✔
1270
  } else {
1271
    destorySqlCallbackWrapper(pWrapper);
62✔
1272
    pRequest->pWrapper = NULL;
62✔
1273
    qDestroyQuery(pRequest->pQuery);
62✔
1274
    pRequest->pQuery = NULL;
62✔
1275

1276
    if (NEED_CLIENT_HANDLE_ERROR(code)) {
62!
1277
      tscDebug("0x%" PRIx64 " client retry to handle the error, code:%d - %s, tryCount:%d,QID:0x%" PRIx64,
51✔
1278
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1279
      restartAsyncQuery(pRequest, code);
51✔
1280
      return;
51✔
1281
    }
1282

1283
    // return to app directly
1284
    tscError("0x%" PRIx64 " error occurs, code:%s, return to user app,QID:0x%" PRIx64, pRequest->self, tstrerror(code),
11!
1285
             pRequest->requestId);
1286
    pRequest->code = code;
11✔
1287
    returnToUser(pRequest);
11✔
1288
  }
1289
}
1290

1291
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
1,526✔
1292
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
1,526✔
1293
                           .requestId = pWrapper->pParseCtx->requestId,
1,526✔
1294
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
1,526✔
1295
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
1,526✔
1296

1297
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
1,526✔
1298

1299
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
3,052✔
1300
                                &pWrapper->pRequest->body.queryJob);
1,526✔
1301
}
1302

1303
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code);
1304

1305
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
7,324✔
1306
  int32_t code = TSDB_CODE_SUCCESS;
7,324✔
1307
  switch (pWrapper->pRequest->pQuery->execStage) {
7,324!
1308
    case QUERY_EXEC_STAGE_PARSE: {
1✔
1309
      // continue parse after get metadata
1310
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
1✔
1311
      break;
1✔
1312
    }
1313
    case QUERY_EXEC_STAGE_ANALYSE: {
1,525✔
1314
      // analysis after get metadata
1315
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
1,525✔
1316
      break;
1,525✔
1317
    }
1318
    case QUERY_EXEC_STAGE_SCHEDULE: {
5,798✔
1319
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
5,798✔
1320
      break;
5,798✔
1321
    }
1322
    default:
×
1323
      break;
×
1324
  }
1325
  return code;
7,324✔
1326
}
1327

1328
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code) {
1✔
1329
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
1✔
1330
  SRequestObj         *pRequest = pWrapper->pRequest;
1✔
1331
  SQuery              *pQuery = pRequest->pQuery;
1✔
1332

1333
  pRequest->metric.ctgCostUs += taosGetTimestampUs() - pRequest->metric.ctgStart;
1✔
1334
  qDebug("0x%" PRIx64 " start to continue parse,QID:0x%" PRIx64 ", code:%s", pRequest->self, pRequest->requestId,
1!
1335
         tstrerror(code));
1336

1337
  if (code == TSDB_CODE_SUCCESS) {
1!
1338
    // pWrapper->pCatalogReq->forceUpdate = false;
1339
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
1✔
1340
  }
1341

1342
  if (TSDB_CODE_SUCCESS == code) {
1!
1343
    code = phaseAsyncQuery(pWrapper);
1✔
1344
  }
1345

1346
  if (TSDB_CODE_SUCCESS != code) {
1!
UNCOV
1347
    tscError("0x%" PRIx64 " error happens, code:%d - %s,QID:0x%" PRIx64, pWrapper->pRequest->self, code,
×
1348
             tstrerror(code), pWrapper->pRequest->requestId);
UNCOV
1349
    destorySqlCallbackWrapper(pWrapper);
×
UNCOV
1350
    pRequest->pWrapper = NULL;
×
UNCOV
1351
    terrno = code;
×
UNCOV
1352
    pRequest->code = code;
×
UNCOV
1353
    doRequestCallback(pRequest, code);
×
1354
  }
1355
}
1✔
1356

1357
void continueInsertFromCsv(SSqlCallbackWrapper *pWrapper, SRequestObj *pRequest) {
×
1358
  int32_t code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
×
1359
  if (TSDB_CODE_SUCCESS == code) {
×
1360
    code = phaseAsyncQuery(pWrapper);
×
1361
  }
1362

1363
  if (TSDB_CODE_SUCCESS != code) {
×
1364
    tscError("0x%" PRIx64 " error happens, code:%d - %s,QID:0x%" PRIx64, pWrapper->pRequest->self, code,
×
1365
             tstrerror(code), pWrapper->pRequest->requestId);
1366
    destorySqlCallbackWrapper(pWrapper);
×
1367
    pRequest->pWrapper = NULL;
×
1368
    terrno = code;
×
1369
    pRequest->code = code;
×
1370
    doRequestCallback(pRequest, code);
×
1371
  }
1372
}
×
1373

UNCOV
1374
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
×
UNCOV
1375
  int64_t connId = *(int64_t *)taos;
×
UNCOV
1376
  tscDebug("taos_query_a start with sql:%s", sql);
×
UNCOV
1377
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
×
UNCOV
1378
  tscDebug("taos_query_a end with sql:%s", sql);
×
UNCOV
1379
}
×
1380

1381
void taos_query_a_with_reqid(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param, int64_t reqid) {
×
1382
  int64_t connId = *(int64_t *)taos;
×
1383
  taosAsyncQueryImplWithReqid(connId, sql, fp, param, false, reqid);
×
1384
}
×
1385

1386
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
7,334✔
1387
  const STscObj *pTscObj = pRequest->pTscObj;
7,334✔
1388

1389
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
7,334!
1390
  if (*pCxt == NULL) {
7,334!
1391
    return terrno;
×
1392
  }
1393

1394
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
7,334✔
1395
                           .requestRid = pRequest->self,
7,334✔
1396
                           .acctId = pTscObj->acctId,
7,334✔
1397
                           .db = pRequest->pDb,
7,334✔
1398
                           .topicQuery = false,
1399
                           .pSql = pRequest->sqlstr,
7,334✔
1400
                           .sqlLen = pRequest->sqlLen,
7,334✔
1401
                           .pMsg = pRequest->msgBuf,
7,334✔
1402
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1403
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
7,334✔
1404
                           .pStmtCb = NULL,
1405
                           .pUser = pTscObj->user,
7,334✔
1406
                           .pEffectiveUser = pRequest->effectiveUser,
7,334✔
1407
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
7,334✔
1408
                           .enableSysInfo = pTscObj->sysInfo,
7,334✔
1409
                           .async = true,
1410
                           .svrVer = pTscObj->sVer,
7,334✔
1411
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
7,334✔
1412
                           .allocatorId = pRequest->allocatorRefId,
7,334✔
1413
                           .parseSqlFp = clientParseSql,
1414
                           .parseSqlParam = pWrapper,
1415
                           .setQueryFp = setQueryRequest,
1416
                           .timezone = pTscObj->optionInfo.timezone,
7,334✔
1417
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
7,334✔
1418
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
7,334✔
1419
  (*pCxt)->biMode = biMode;
7,334✔
1420
  return TSDB_CODE_SUCCESS;
7,334✔
1421
}
1422

1423
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
7,334✔
1424
  int32_t              code = TSDB_CODE_SUCCESS;
7,334✔
1425
  STscObj             *pTscObj = pRequest->pTscObj;
7,334✔
1426
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
7,334!
1427
  if (pWrapper == NULL) {
7,334!
1428
    code = terrno;
×
1429
  } else {
1430
    pWrapper->pRequest = pRequest;
7,334✔
1431
    pRequest->pWrapper = pWrapper;
7,334✔
1432
    *ppWrapper = pWrapper;
7,334✔
1433
  }
1434

1435
  if (TSDB_CODE_SUCCESS == code) {
7,334!
1436
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
7,334✔
1437
  }
1438

1439
  if (TSDB_CODE_SUCCESS == code) {
7,334!
1440
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
7,334✔
1441
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
7,334✔
1442
  }
1443

1444
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
7,333!
1445
    int64_t syntaxStart = taosGetTimestampUs();
7,333✔
1446

1447
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
7,333!
1448
    if (pWrapper->pCatalogReq == NULL) {
7,333!
1449
      code = terrno;
×
1450
    } else {
1451
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
7,333✔
1452
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
7,333!
1453
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
7,333✔
1454
    }
1455

1456
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
7,333✔
1457
  }
1458

1459
  return code;
7,333✔
1460
}
1461

1462
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
7,350✔
1463
  SSqlCallbackWrapper *pWrapper = NULL;
7,350✔
1464
  int32_t              code = TSDB_CODE_SUCCESS;
7,350✔
1465

1466
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
7,350✔
1467
    code = pRequest->prevCode;
17✔
1468
    terrno = code;
17✔
1469
    pRequest->code = code;
17✔
1470
    tscDebug("call sync query cb with code: %s", tstrerror(code));
17✔
1471
    doRequestCallback(pRequest, code);
17✔
1472
    return;
17✔
1473
  }
1474

1475
  if (TSDB_CODE_SUCCESS == code) {
7,333!
1476
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
7,333✔
1477
  }
1478

1479
  if (TSDB_CODE_SUCCESS == code) {
7,332✔
1480
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
7,323✔
1481
    code = phaseAsyncQuery(pWrapper);
7,323✔
1482
  }
1483

1484
  if (TSDB_CODE_SUCCESS != code) {
7,332✔
1485
    tscError("0x%" PRIx64 " error happens, code:%d - %s,QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
9!
1486
             pRequest->requestId);
1487
    destorySqlCallbackWrapper(pWrapper);
9✔
1488
    pRequest->pWrapper = NULL;
9✔
1489
    qDestroyQuery(pRequest->pQuery);
9✔
1490
    pRequest->pQuery = NULL;
9✔
1491

1492
    if (NEED_CLIENT_HANDLE_ERROR(code)) {
9!
UNCOV
1493
      tscDebug("0x%" PRIx64 " client retry to handle the error, code:%d - %s, tryCount:%d,QID:0x%" PRIx64,
×
1494
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
UNCOV
1495
      code = refreshMeta(pRequest->pTscObj, pRequest);
×
UNCOV
1496
      if (code != 0) {
×
UNCOV
1497
        tscWarn("0x%" PRIx64 " refresh meta failed, code:%d - %s,QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
×
1498
                pRequest->requestId);
1499
      }
UNCOV
1500
      pRequest->prevCode = code;
×
UNCOV
1501
      doAsyncQuery(pRequest, true);
×
UNCOV
1502
      return;
×
1503
    }
1504

1505
    terrno = code;
9✔
1506
    pRequest->code = code;
9✔
1507
    doRequestCallback(pRequest, code);
9✔
1508
  }
1509
}
1510

1511
void restartAsyncQuery(SRequestObj *pRequest, int32_t code) {
53✔
1512
  tscInfo("restart request: %s p: %p", pRequest->sqlstr, pRequest);
53!
1513
  SRequestObj *pUserReq = pRequest;
53✔
1514
  (void)acquireRequest(pRequest->self);
53✔
1515
  while (pUserReq) {
53!
1516
    if (pUserReq->self == pUserReq->relation.userRefId || pUserReq->relation.userRefId == 0) {
53!
1517
      break;
1518
    } else {
UNCOV
1519
      int64_t nextRefId = pUserReq->relation.nextRefId;
×
UNCOV
1520
      (void)releaseRequest(pUserReq->self);
×
UNCOV
1521
      if (nextRefId) {
×
UNCOV
1522
        pUserReq = acquireRequest(nextRefId);
×
1523
      }
1524
    }
1525
  }
1526
  bool hasSubRequest = pUserReq != pRequest || pRequest->relation.prevRefId != 0;
53!
1527
  if (pUserReq) {
53!
1528
    destroyCtxInRequest(pUserReq);
53✔
1529
    pUserReq->prevCode = code;
53✔
1530
    (void)memset(&pUserReq->relation, 0, sizeof(pUserReq->relation));
53✔
1531
  } else {
1532
    tscError("User req is missing");
×
1533
    (void)removeFromMostPrevReq(pRequest);
×
1534
    return;
×
1535
  }
1536
  if (hasSubRequest)
53!
UNCOV
1537
    (void)removeFromMostPrevReq(pRequest);
×
1538
  else
1539
    (void)releaseRequest(pUserReq->self);
53✔
1540
  doAsyncQuery(pUserReq, true);
53✔
1541
}
1542

1543
typedef struct SAsyncFetchParam {
1544
  SRequestObj      *pReq;
1545
  __taos_async_fn_t fp;
1546
  void             *param;
1547
} SAsyncFetchParam;
1548

1549
static int32_t doAsyncFetch(void *pParam) {
399✔
1550
  SAsyncFetchParam *param = pParam;
399✔
1551
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
399✔
1552
  taosMemoryFree(param);
399!
1553
  return TSDB_CODE_SUCCESS;
399✔
1554
}
1555

1556
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
399✔
1557
  if (res == NULL || fp == NULL) {
399!
1558
    tscError("taos_fetch_rows_a invalid paras");
×
1559
    return;
×
1560
  }
1561
  if (!TD_RES_QUERY(res)) {
399!
1562
    tscError("taos_fetch_rows_a res is NULL");
×
1563
    fp(param, res, TSDB_CODE_APP_ERROR);
×
1564
    return;
×
1565
  }
1566

1567
  SRequestObj *pRequest = res;
399✔
1568
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
399!
UNCOV
1569
    fp(param, res, 0);
×
UNCOV
1570
    return;
×
1571
  }
1572

1573
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
399!
1574
  if (!pParam) {
399!
1575
    fp(param, res, terrno);
×
1576
    return;
×
1577
  }
1578
  pParam->pReq = pRequest;
399✔
1579
  pParam->fp = fp;
399✔
1580
  pParam->param = param;
399✔
1581
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
399✔
1582
  if (TSDB_CODE_SUCCESS != code) {
399!
1583
    taosMemoryFree(pParam);
×
1584
    fp(param, res, code);
×
1585
    return;
×
1586
  }
1587
}
1588

1589
void taos_fetch_raw_block_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
×
1590
  if (res == NULL || fp == NULL) {
×
1591
    tscError("taos_fetch_raw_block_a invalid paras");
×
1592
    return;
×
1593
  }
1594
  if (!TD_RES_QUERY(res)) {
×
1595
    tscError("taos_fetch_raw_block_a res is NULL");
×
1596
    return;
×
1597
  }
1598
  SRequestObj    *pRequest = res;
×
1599
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
×
1600

1601
  // set the current block is all consumed
1602
  pResultInfo->convertUcs4 = false;
×
1603

1604
  // it is a local executed query, no need to do async fetch
1605
  taos_fetch_rows_a(pRequest, fp, param);
×
1606
}
1607

1608
const void *taos_get_raw_block(TAOS_RES *res) {
×
1609
  if (res == NULL) {
×
1610
    tscError("taos_get_raw_block invalid paras");
×
1611
    return NULL;
×
1612
  }
1613
  if (!TD_RES_QUERY(res)) {
×
1614
    tscError("taos_get_raw_block res is NULL");
×
1615
    return NULL;
×
1616
  }
1617
  SRequestObj *pRequest = res;
×
1618

1619
  return pRequest->body.resInfo.pData;
×
1620
}
1621

1622
int taos_get_db_route_info(TAOS *taos, const char *db, TAOS_DB_ROUTE_INFO *dbInfo) {
×
1623
  if (NULL == taos) {
×
1624
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1625
    return terrno;
×
1626
  }
1627

1628
  if (NULL == db || NULL == dbInfo) {
×
1629
    tscError("invalid input param, db:%p, dbInfo:%p", db, dbInfo);
×
1630
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1631
    return terrno;
×
1632
  }
1633

1634
  int64_t      connId = *(int64_t *)taos;
×
1635
  SRequestObj *pRequest = NULL;
×
1636
  char        *sql = "taos_get_db_route_info";
×
1637
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1638
  if (code != TSDB_CODE_SUCCESS) {
×
1639
    terrno = code;
×
1640
    return terrno;
×
1641
  }
1642

1643
  STscObj  *pTscObj = pRequest->pTscObj;
×
1644
  SCatalog *pCtg = NULL;
×
1645
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1646
  if (code != TSDB_CODE_SUCCESS) {
×
1647
    goto _return;
×
1648
  }
1649

1650
  SRequestConnInfo conn = {
×
1651
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1652

1653
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1654

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

1658
  code = catalogGetDBVgInfo(pCtg, &conn, dbFName, dbInfo);
×
1659
  if (code) {
×
1660
    goto _return;
×
1661
  }
1662

1663
_return:
×
1664

1665
  terrno = code;
×
1666

1667
  destroyRequest(pRequest);
×
1668
  return code;
×
1669
}
1670

1671
int taos_get_table_vgId(TAOS *taos, const char *db, const char *table, int *vgId) {
×
1672
  if (NULL == taos) {
×
1673
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1674
    return terrno;
×
1675
  }
1676

1677
  if (NULL == db || NULL == table || NULL == vgId) {
×
1678
    tscError("invalid input param, db:%p, table:%p, vgId:%p", db, table, vgId);
×
1679
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1680
    return terrno;
×
1681
  }
1682

1683
  int64_t      connId = *(int64_t *)taos;
×
1684
  SRequestObj *pRequest = NULL;
×
1685
  char        *sql = "taos_get_table_vgId";
×
1686
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1687
  if (code != TSDB_CODE_SUCCESS) {
×
1688
    return terrno;
×
1689
  }
1690

1691
  pRequest->syncQuery = true;
×
1692

1693
  STscObj  *pTscObj = pRequest->pTscObj;
×
1694
  SCatalog *pCtg = NULL;
×
1695
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1696
  if (code != TSDB_CODE_SUCCESS) {
×
1697
    goto _return;
×
1698
  }
1699

1700
  SRequestConnInfo conn = {
×
1701
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1702

1703
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1704

1705
  SName tableName = {0};
×
1706
  toName(pTscObj->acctId, db, table, &tableName);
×
1707

1708
  SVgroupInfo vgInfo;
1709
  code = catalogGetTableHashVgroup(pCtg, &conn, &tableName, &vgInfo);
×
1710
  if (code) {
×
1711
    goto _return;
×
1712
  }
1713

1714
  *vgId = vgInfo.vgId;
×
1715

1716
_return:
×
1717

1718
  terrno = code;
×
1719

1720
  destroyRequest(pRequest);
×
1721
  return code;
×
1722
}
1723

1724
int taos_get_tables_vgId(TAOS *taos, const char *db, const char *table[], int tableNum, int *vgId) {
×
1725
  if (NULL == taos) {
×
1726
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1727
    return terrno;
×
1728
  }
1729

1730
  if (NULL == db || NULL == table || NULL == vgId || tableNum <= 0) {
×
1731
    tscError("invalid input param, db:%p, table:%p, vgId:%p, tbNum:%d", db, table, vgId, tableNum);
×
1732
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1733
    return terrno;
×
1734
  }
1735

1736
  int64_t      connId = *(int64_t *)taos;
×
1737
  SRequestObj *pRequest = NULL;
×
1738
  char        *sql = "taos_get_table_vgId";
×
1739
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1740
  if (code != TSDB_CODE_SUCCESS) {
×
1741
    return terrno;
×
1742
  }
1743

1744
  pRequest->syncQuery = true;
×
1745

1746
  STscObj  *pTscObj = pRequest->pTscObj;
×
1747
  SCatalog *pCtg = NULL;
×
1748
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1749
  if (code != TSDB_CODE_SUCCESS) {
×
1750
    goto _return;
×
1751
  }
1752

1753
  SRequestConnInfo conn = {
×
1754
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1755

1756
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1757

1758
  code = catalogGetTablesHashVgId(pCtg, &conn, pTscObj->acctId, db, table, tableNum, vgId);
×
1759
  if (code) {
×
1760
    goto _return;
×
1761
  }
1762

1763
_return:
×
1764

1765
  terrno = code;
×
1766

1767
  destroyRequest(pRequest);
×
1768
  return code;
×
1769
}
1770

1771
int taos_load_table_info(TAOS *taos, const char *tableNameList) {
×
1772
  if (NULL == taos) {
×
1773
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1774
    return terrno;
×
1775
  }
1776

1777
  int64_t       connId = *(int64_t *)taos;
×
1778
  const int32_t MAX_TABLE_NAME_LENGTH = 12 * 1024 * 1024;  // 12MB list
×
1779
  int32_t       code = 0;
×
1780
  SRequestObj  *pRequest = NULL;
×
1781
  SCatalogReq   catalogReq = {0};
×
1782

1783
  if (NULL == tableNameList) {
×
1784
    return TSDB_CODE_SUCCESS;
×
1785
  }
1786

1787
  int32_t length = (int32_t)strlen(tableNameList);
×
1788
  if (0 == length) {
×
1789
    return TSDB_CODE_SUCCESS;
×
1790
  } else if (length > MAX_TABLE_NAME_LENGTH) {
×
1791
    tscError("tableNameList too long, length:%d, maximum allowed:%d", length, MAX_TABLE_NAME_LENGTH);
×
1792
    return TSDB_CODE_TSC_INVALID_OPERATION;
×
1793
  }
1794

1795
  char *sql = "taos_load_table_info";
×
1796
  code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1797
  if (code != TSDB_CODE_SUCCESS) {
×
1798
    terrno = code;
×
1799
    goto _return;
×
1800
  }
1801

1802
  pRequest->syncQuery = true;
×
1803

1804
  STscObj *pTscObj = pRequest->pTscObj;
×
1805
  code = transferTableNameList(tableNameList, pTscObj->acctId, pTscObj->db, &catalogReq.pTableMeta);
×
1806
  if (code) {
×
1807
    goto _return;
×
1808
  }
1809

1810
  SCatalog *pCtg = NULL;
×
1811
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1812
  if (code != TSDB_CODE_SUCCESS) {
×
1813
    goto _return;
×
1814
  }
1815

1816
  SRequestConnInfo conn = {
×
1817
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1818

1819
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1820

1821
  code = catalogAsyncGetAllMeta(pCtg, &conn, &catalogReq, syncCatalogFn, pRequest->body.interParam, NULL);
×
1822
  if (code) {
×
1823
    goto _return;
×
1824
  }
1825

1826
  SSyncQueryParam *pParam = pRequest->body.interParam;
×
1827
  code = tsem_wait(&pParam->sem);
×
1828
  if (code) {
×
1829
    tscError("tsem wait failed, code:%d - %s", code, tstrerror(code));
×
1830
    goto _return;
×
1831
  }
1832
_return:
×
1833
  destoryCatalogReq(&catalogReq);
×
1834
  destroyRequest(pRequest);
×
1835
  return code;
×
1836
}
1837

1838
TAOS_STMT *taos_stmt_init(TAOS *taos) {
8✔
1839
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
8✔
1840
  if (NULL == pObj) {
8!
1841
    tscError("invalid parameter for %s", __FUNCTION__);
×
1842
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1843
    return NULL;
×
1844
  }
1845

1846
  TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL);
8✔
1847
  if (NULL == pStmt) {
8!
1848
    tscError("stmt init failed, errcode:%s", terrstr());
×
1849
  }
1850
  releaseTscObj(*(int64_t *)taos);
8✔
1851

1852
  return pStmt;
8✔
1853
}
1854

1855
TAOS_STMT *taos_stmt_init_with_reqid(TAOS *taos, int64_t reqid) {
×
1856
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
1857
  if (NULL == pObj) {
×
1858
    tscError("invalid parameter for %s", __FUNCTION__);
×
1859
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1860
    return NULL;
×
1861
  }
1862

1863
  TAOS_STMT *pStmt = stmtInit(pObj, reqid, NULL);
×
1864
  if (NULL == pStmt) {
×
1865
    tscError("stmt init failed, errcode:%s", terrstr());
×
1866
  }
1867
  releaseTscObj(*(int64_t *)taos);
×
1868

1869
  return pStmt;
×
1870
}
1871

1872
TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options) {
4✔
1873
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
4✔
1874
  if (NULL == pObj) {
4!
1875
    tscError("invalid parameter for %s", __FUNCTION__);
×
1876
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1877
    return NULL;
×
1878
  }
1879

1880
  TAOS_STMT *pStmt = stmtInit(pObj, options->reqId, options);
4✔
1881
  if (NULL == pStmt) {
4!
1882
    tscError("stmt init failed, errcode:%s", terrstr());
×
1883
  }
1884
  releaseTscObj(*(int64_t *)taos);
4✔
1885

1886
  return pStmt;
4✔
1887
}
1888

1889
int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
20✔
1890
  if (stmt == NULL || sql == NULL) {
20!
1891
    tscError("NULL parameter for %s", __FUNCTION__);
×
1892
    terrno = TSDB_CODE_INVALID_PARA;
×
1893
    return terrno;
×
1894
  }
1895

1896
  return stmtPrepare(stmt, sql, length);
20✔
1897
}
1898

1899
int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags) {
×
1900
  if (stmt == NULL || name == NULL) {
×
1901
    tscError("NULL parameter for %s", __FUNCTION__);
×
1902
    terrno = TSDB_CODE_INVALID_PARA;
×
1903
    return terrno;
×
1904
  }
1905

1906
  int32_t code = stmtSetTbName(stmt, name);
×
1907
  if (code) {
×
1908
    return code;
×
1909
  }
1910

1911
  if (tags) {
×
1912
    return stmtSetTbTags(stmt, tags);
×
1913
  }
1914

1915
  return TSDB_CODE_SUCCESS;
×
1916
}
1917

1918
int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name) {
48✔
1919
  if (stmt == NULL || name == NULL) {
48!
1920
    tscError("NULL parameter for %s", __FUNCTION__);
×
1921
    terrno = TSDB_CODE_INVALID_PARA;
×
1922
    return terrno;
×
1923
  }
1924

1925
  return stmtSetTbName(stmt, name);
48✔
1926
}
1927

1928
int taos_stmt_set_tags(TAOS_STMT *stmt, TAOS_MULTI_BIND *tags) {
×
1929
  if (stmt == NULL || tags == NULL) {
×
1930
    tscError("NULL parameter for %s", __FUNCTION__);
×
1931
    terrno = TSDB_CODE_INVALID_PARA;
×
1932
    return terrno;
×
1933
  }
1934

1935
  return stmtSetTbTags(stmt, tags);
×
1936
}
1937

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

1940
int taos_stmt_get_tag_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
×
1941
  if (stmt == NULL || NULL == fieldNum) {
×
1942
    tscError("NULL parameter for %s", __FUNCTION__);
×
1943
    terrno = TSDB_CODE_INVALID_PARA;
×
1944
    return terrno;
×
1945
  }
1946

1947
  return stmtGetTagFields(stmt, fieldNum, fields);
×
1948
}
1949

1950
int taos_stmt_get_col_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
×
1951
  if (stmt == NULL || NULL == fieldNum) {
×
1952
    tscError("NULL parameter for %s", __FUNCTION__);
×
1953
    terrno = TSDB_CODE_INVALID_PARA;
×
1954
    return terrno;
×
1955
  }
1956

1957
  return stmtGetColFields(stmt, fieldNum, fields);
×
1958
}
1959

1960
// let stmt to reclaim TAOS_FIELD_E that was allocated by `taos_stmt_get_tag_fields`/`taos_stmt_get_col_fields`
1961
void taos_stmt_reclaim_fields(TAOS_STMT *stmt, TAOS_FIELD_E *fields) {
×
1962
  (void)stmt;
1963
  if (!fields) return;
×
1964
  taosMemoryFree(fields);
×
1965
}
1966

UNCOV
1967
int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
×
UNCOV
1968
  if (stmt == NULL || bind == NULL) {
×
1969
    tscError("NULL parameter for %s", __FUNCTION__);
×
1970
    terrno = TSDB_CODE_INVALID_PARA;
×
1971
    return terrno;
×
1972
  }
1973

UNCOV
1974
  if (bind->num > 1) {
×
1975
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
×
1976
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
1977
    return terrno;
×
1978
  }
1979

UNCOV
1980
  return stmtBindBatch(stmt, bind, -1);
×
1981
}
1982

1983
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
48✔
1984
  if (stmt == NULL || bind == NULL) {
48!
1985
    tscError("NULL parameter for %s", __FUNCTION__);
×
1986
    terrno = TSDB_CODE_INVALID_PARA;
×
1987
    return terrno;
×
1988
  }
1989

1990
  if (bind->num <= 0 || bind->num > INT16_MAX) {
48!
1991
    tscError("invalid bind num %d", bind->num);
×
1992
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
1993
    return terrno;
×
1994
  }
1995

1996
  int32_t insert = 0;
48✔
1997
  int32_t code = stmtIsInsert(stmt, &insert);
48✔
1998
  if (TSDB_CODE_SUCCESS != code) {
48!
1999
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2000
    return code;
×
2001
  }
2002
  if (0 == insert && bind->num > 1) {
48!
2003
    tscError("only one row data allowed for query");
×
2004
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2005
    return terrno;
×
2006
  }
2007

2008
  return stmtBindBatch(stmt, bind, -1);
48✔
2009
}
2010

2011
int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind, int colIdx) {
×
2012
  if (stmt == NULL || bind == NULL) {
×
2013
    tscError("NULL parameter for %s", __FUNCTION__);
×
2014
    terrno = TSDB_CODE_INVALID_PARA;
×
2015
    return terrno;
×
2016
  }
2017

2018
  if (colIdx < 0) {
×
2019
    tscError("invalid bind column idx %d", colIdx);
×
2020
    terrno = TSDB_CODE_INVALID_PARA;
×
2021
    return terrno;
×
2022
  }
2023

2024
  int32_t insert = 0;
×
2025
  int32_t code = stmtIsInsert(stmt, &insert);
×
2026
  if (TSDB_CODE_SUCCESS != code) {
×
2027
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2028
    return code;
×
2029
  }
2030
  if (0 == insert && bind->num > 1) {
×
2031
    tscError("only one row data allowed for query");
×
2032
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2033
    return terrno;
×
2034
  }
2035

2036
  return stmtBindBatch(stmt, bind, colIdx);
×
2037
}
2038

2039
int taos_stmt_add_batch(TAOS_STMT *stmt) {
48✔
2040
  if (stmt == NULL) {
48!
2041
    tscError("NULL parameter for %s", __FUNCTION__);
×
2042
    terrno = TSDB_CODE_INVALID_PARA;
×
2043
    return terrno;
×
2044
  }
2045

2046
  return stmtAddBatch(stmt);
48✔
2047
}
2048

2049
int taos_stmt_execute(TAOS_STMT *stmt) {
48✔
2050
  if (stmt == NULL) {
48!
2051
    tscError("NULL parameter for %s", __FUNCTION__);
×
2052
    terrno = TSDB_CODE_INVALID_PARA;
×
2053
    return terrno;
×
2054
  }
2055

2056
  return stmtExec(stmt);
48✔
2057
}
2058

2059
int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
×
2060
  if (stmt == NULL || insert == NULL) {
×
2061
    tscError("NULL parameter for %s", __FUNCTION__);
×
2062
    terrno = TSDB_CODE_INVALID_PARA;
×
2063
    return terrno;
×
2064
  }
2065

2066
  return stmtIsInsert(stmt, insert);
×
2067
}
2068

2069
int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) {
×
2070
  if (stmt == NULL || nums == NULL) {
×
2071
    tscError("NULL parameter for %s", __FUNCTION__);
×
2072
    terrno = TSDB_CODE_INVALID_PARA;
×
2073
    return terrno;
×
2074
  }
2075

2076
  return stmtGetParamNum(stmt, nums);
×
2077
}
2078

2079
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
×
2080
  if (stmt == NULL || type == NULL || NULL == bytes || idx < 0) {
×
2081
    tscError("invalid parameter for %s", __FUNCTION__);
×
2082
    terrno = TSDB_CODE_INVALID_PARA;
×
2083
    return terrno;
×
2084
  }
2085

2086
  return stmtGetParam(stmt, idx, type, bytes);
×
2087
}
2088

2089
TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt) {
×
2090
  if (stmt == NULL) {
×
2091
    tscError("NULL parameter for %s", __FUNCTION__);
×
2092
    terrno = TSDB_CODE_INVALID_PARA;
×
2093
    return NULL;
×
2094
  }
2095

2096
  return stmtUseResult(stmt);
×
2097
}
2098

UNCOV
2099
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
×
2100

UNCOV
2101
int taos_stmt_affected_rows(TAOS_STMT *stmt) {
×
UNCOV
2102
  if (stmt == NULL) {
×
2103
    tscError("NULL parameter for %s", __FUNCTION__);
×
2104
    terrno = TSDB_CODE_INVALID_PARA;
×
2105
    return 0;
×
2106
  }
2107

UNCOV
2108
  return stmtAffectedRows(stmt);
×
2109
}
2110

2111
int taos_stmt_affected_rows_once(TAOS_STMT *stmt) {
×
2112
  if (stmt == NULL) {
×
2113
    tscError("NULL parameter for %s", __FUNCTION__);
×
2114
    terrno = TSDB_CODE_INVALID_PARA;
×
2115
    return 0;
×
2116
  }
2117

2118
  return stmtAffectedRowsOnce(stmt);
×
2119
}
2120

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

2128
  return stmtClose(stmt);
12✔
2129
}
2130

2131
TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option) {
×
2132
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
2133
  if (NULL == pObj) {
×
2134
    tscError("invalid parameter for %s", __FUNCTION__);
×
2135
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2136
    return NULL;
×
2137
  }
2138

2139
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
×
2140

2141
  releaseTscObj(*(int64_t *)taos);
×
2142

2143
  return pStmt;
×
2144
}
2145

2146
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
×
2147
  if (stmt == NULL || sql == NULL) {
×
2148
    tscError("NULL parameter for %s", __FUNCTION__);
×
2149
    terrno = TSDB_CODE_INVALID_PARA;
×
2150
    return terrno;
×
2151
  }
2152

2153
  return stmtPrepare2(stmt, sql, length);
×
2154
}
2155

2156
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
×
2157
  if (stmt == NULL) {
×
2158
    tscError("NULL parameter for %s", __FUNCTION__);
×
2159
    terrno = TSDB_CODE_INVALID_PARA;
×
2160
    return terrno;
×
2161
  }
2162

2163
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2164
  if (pStmt->options.asyncExecFn && !pStmt->semWaited) {
×
2165
    if (tsem_wait(&pStmt->asyncQuerySem) != 0) {
×
2166
      tscError("wait async query sem failed");
×
2167
    }
2168
    pStmt->semWaited = true;
×
2169
  }
2170

2171
  SSHashObj *hashTbnames = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR));
×
2172
  if (NULL == hashTbnames) {
×
2173
    tscError("stmt2 bind failed: %s", tstrerror(terrno));
×
2174
    return terrno;
×
2175
  }
2176

2177
  int32_t code = TSDB_CODE_SUCCESS;
×
2178
  for (int i = 0; i < bindv->count; ++i) {
×
2179
    if (bindv->tbnames && bindv->tbnames[i]) {
×
2180
      if (pStmt->sql.stbInterlaceMode) {
×
2181
        if (tSimpleHashGet(hashTbnames, bindv->tbnames[i], strlen(bindv->tbnames[i])) != NULL) {
×
2182
          code = terrno = TSDB_CODE_PAR_TBNAME_DUPLICATED;
×
2183
          tscError("stmt2 bind failed: %s %s", tstrerror(terrno), bindv->tbnames[i]);
×
2184
          goto out;
×
2185
        }
2186

2187
        code = tSimpleHashPut(hashTbnames, bindv->tbnames[i], strlen(bindv->tbnames[i]), NULL, 0);
×
2188
        if (code) {
×
2189
          goto out;
×
2190
        }
2191
      }
2192

2193
      code = stmtSetTbName2(stmt, bindv->tbnames[i]);
×
2194
      if (code) {
×
2195
        goto out;
×
2196
      }
2197
    }
2198

2199
    if (bindv->tags && bindv->tags[i]) {
×
2200
      code = stmtSetTbTags2(stmt, bindv->tags[i]);
×
2201
      if (code) {
×
2202
        goto out;
×
2203
      }
2204
    }
2205

2206
    if (bindv->bind_cols && bindv->bind_cols[i]) {
×
2207
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
×
2208

2209
      if (bind->num <= 0 || bind->num > INT16_MAX) {
×
2210
        tscError("invalid bind num %d", bind->num);
×
2211
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2212
        goto out;
×
2213
      }
2214

2215
      int32_t insert = 0;
×
2216
      (void)stmtIsInsert2(stmt, &insert);
×
2217
      if (0 == insert && bind->num > 1) {
×
2218
        tscError("only one row data allowed for query");
×
2219
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2220
        goto out;
×
2221
      }
2222

2223
      code = stmtBindBatch2(stmt, bind, col_idx);
×
2224
      if (TSDB_CODE_SUCCESS != code) {
×
2225
        goto out;
×
2226
      }
2227
    }
2228
  }
2229

2230
out:
×
2231
  tSimpleHashCleanup(hashTbnames);
×
2232

2233
  return code;
×
2234
}
2235

2236
int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows) {
×
2237
  if (stmt == NULL) {
×
2238
    tscError("NULL parameter for %s", __FUNCTION__);
×
2239
    terrno = TSDB_CODE_INVALID_PARA;
×
2240
    return terrno;
×
2241
  }
2242

2243
  return stmtExec2(stmt, affected_rows);
×
2244
}
2245

2246
int taos_stmt2_close(TAOS_STMT2 *stmt) {
×
2247
  if (stmt == NULL) {
×
2248
    tscError("NULL parameter for %s", __FUNCTION__);
×
2249
    terrno = TSDB_CODE_INVALID_PARA;
×
2250
    return terrno;
×
2251
  }
2252

2253
  return stmtClose2(stmt);
×
2254
}
2255
/*
2256
int taos_stmt2_param_count(TAOS_STMT2 *stmt, int *nums) {
2257
  if (stmt == NULL || nums == NULL) {
2258
    tscError("NULL parameter for %s", __FUNCTION__);
2259
    terrno = TSDB_CODE_INVALID_PARA;
2260
    return terrno;
2261
  }
2262
  return stmtGetParamNum2(stmt, nums);
2263
}
2264
*/
2265
int taos_stmt2_is_insert(TAOS_STMT2 *stmt, int *insert) {
×
2266
  if (stmt == NULL || insert == NULL) {
×
2267
    tscError("NULL parameter for %s", __FUNCTION__);
×
2268
    terrno = TSDB_CODE_INVALID_PARA;
×
2269
    return terrno;
×
2270
  }
2271

2272
  return stmtIsInsert2(stmt, insert);
×
2273
}
2274

2275
// int taos_stmt2_get_fields(TAOS_STMT2 *stmt, TAOS_FIELD_T field_type, int *count, TAOS_FIELD_E **fields) {
2276
//   if (stmt == NULL || count == NULL) {
2277
//     tscError("NULL parameter for %s", __FUNCTION__);
2278
//     terrno = TSDB_CODE_INVALID_PARA;
2279
//     return terrno;
2280
//   }
2281

2282
//   if (field_type == TAOS_FIELD_COL) {
2283
//     return stmtGetColFields2(stmt, count, fields);
2284
//   } else if (field_type == TAOS_FIELD_TAG) {
2285
//     return stmtGetTagFields2(stmt, count, fields);
2286
//   } else if (field_type == TAOS_FIELD_QUERY) {
2287
//     return stmtGetParamNum2(stmt, count);
2288
//   } else if (field_type == TAOS_FIELD_TBNAME) {
2289
//     return stmtGetParamTbName(stmt, count);
2290
//   } else {
2291
//     tscError("invalid parameter for %s", __FUNCTION__);
2292
//     terrno = TSDB_CODE_INVALID_PARA;
2293
//     return terrno;
2294
//   }
2295
// }
2296

2297
int taos_stmt2_get_fields(TAOS_STMT2 *stmt, int *count, TAOS_FIELD_ALL **fields) {
×
2298
  if (stmt == NULL || count == NULL) {
×
2299
    tscError("NULL parameter for %s", __FUNCTION__);
×
2300
    terrno = TSDB_CODE_INVALID_PARA;
×
2301
    return terrno;
×
2302
  }
2303

2304
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2305
  if (pStmt->sql.type == 0) {
×
2306
    int isInsert = 0;
×
2307
    (void)stmtIsInsert2(stmt, &isInsert);
×
2308
    if (!isInsert) {
×
2309
      pStmt->sql.type = STMT_TYPE_QUERY;
×
2310
    }
2311
  }
2312
  if (pStmt->sql.type == STMT_TYPE_QUERY) {
×
2313
    return stmtGetParamNum2(stmt, count);
×
2314
  }
2315

2316
  return stmtGetStbColFields2(stmt, count, fields);
×
2317
}
2318

2319
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_ALL *fields) {
×
2320
  (void)stmt;
2321
  if (!fields) return;
×
2322
  taosMemoryFree(fields);
×
2323
}
2324

2325
TAOS_RES *taos_stmt2_result(TAOS_STMT2 *stmt) {
×
2326
  if (stmt == NULL) {
×
2327
    tscError("NULL parameter for %s", __FUNCTION__);
×
2328
    terrno = TSDB_CODE_INVALID_PARA;
×
2329
    return NULL;
×
2330
  }
2331

2332
  return stmtUseResult2(stmt);
×
2333
}
2334

2335
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmtErrstr2(stmt); }
×
2336

UNCOV
2337
int taos_set_conn_mode(TAOS *taos, int mode, int value) {
×
UNCOV
2338
  if (taos == NULL) {
×
2339
    terrno = TSDB_CODE_INVALID_PARA;
×
2340
    return terrno;
×
2341
  }
2342

UNCOV
2343
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
UNCOV
2344
  if (NULL == pObj) {
×
2345
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2346
    tscError("invalid parameter for %s", __func__);
×
2347
    return terrno;
×
2348
  }
UNCOV
2349
  switch (mode) {
×
UNCOV
2350
    case TAOS_CONN_MODE_BI:
×
UNCOV
2351
      atomic_store_8(&pObj->biMode, value);
×
UNCOV
2352
      break;
×
2353
    default:
×
2354
      tscError("not supported mode.");
×
2355
      return TSDB_CODE_INVALID_PARA;
×
2356
  }
UNCOV
2357
  return 0;
×
2358
}
2359

2360
char *getBuildInfo() { return td_buildinfo; }
18✔
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