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

taosdata / TDengine / #3572

02 Jan 2025 08:57AM UTC coverage: 63.077% (-0.2%) from 63.276%
#3572

push

travis-ci

web-flow
Merge pull request #29450 from taosdata/fix/TS-5651-skip-sync-heartbeat

fix:[TS-5651]skip-sync-heartbeat

139525 of 284348 branches covered (49.07%)

Branch coverage included in aggregate %.

217427 of 281548 relevant lines covered (77.23%)

18571459.85 hits per line

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

49.96
/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
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, ...) {
1,756✔
42
  if (arg == NULL) {
1,756!
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) {
1,756!
48
    if (i % 1000 == 0) {
×
49
      (void)sched_yield();
×
50
    }
51
  }
52

53
  int ret = taos_options_imp(option, (const char *)arg);
1,756✔
54
  atomic_store_32(&lock, 0);
1,756✔
55
  return ret;
1,756✔
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() {
2,763✔
65
  pTimezoneMap = taosHashInit(0, MurmurHash3_32, false, HASH_ENTRY_LOCK);
2,763✔
66
  if (pTimezoneMap == NULL) {
2,763!
67
    return terrno;
×
68
  }
69
  taosHashSetFreeFp(pTimezoneMap, freeTz);
2,763✔
70

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

78
void tzCleanup() {
2,763✔
79
  taosHashCleanup(pTimezoneMap);
2,763✔
80
  taosHashCleanup(pTimezoneNameMap);
2,763✔
81
}
2,763✔
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) {
2,778✔
222
  tscDebug("start to cleanup client environment");
2,778✔
223
  if (atomic_val_compare_exchange_32(&sentinel, TSC_VAR_NOT_RELEASE, TSC_VAR_RELEASED) != TSC_VAR_NOT_RELEASE) {
2,778✔
224
    return;
15✔
225
  }
226

227
  monitorClose();
2,763✔
228
  tscStopCrashReport();
2,763✔
229

230
  hbMgrCleanUp();
2,763✔
231

232
  catalogDestroy();
2,763✔
233
  schedulerDestroy();
2,763✔
234

235
  fmFuncMgtDestroy();
2,763✔
236
  qCleanupKeywordsTable();
2,763✔
237

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

242
#ifndef WINDOWS
243
  tzCleanup();
2,763✔
244
#endif
245
  tmqMgmtClose();
2,763✔
246

247
  int32_t id = clientReqRefPool;
2,763✔
248
  clientReqRefPool = -1;
2,763✔
249
  taosCloseRef(id);
2,763✔
250

251
  id = clientConnRefPool;
2,763✔
252
  clientConnRefPool = -1;
2,763✔
253
  taosCloseRef(id);
2,763✔
254

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

260
  taosConvDestroy();
2,763✔
261
  DestroyRegexCache();
2,763✔
262

263
  tscInfo("all local resources released");
2,763!
264
  taosCleanupCfg();
2,763✔
265
  taosCloseLog();
2,763✔
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) {
10,900✔
282
  tscDebug("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
10,900✔
283
  if (user == NULL) {
10,900!
284
    user = TSDB_DEFAULT_USER;
×
285
  }
286

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

291
  STscObj *pObj = NULL;
10,900✔
292
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
10,900✔
293
  if (TSDB_CODE_SUCCESS == code) {
10,900✔
294
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
10,816!
295
    if (NULL == rid) {
10,816!
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;
10,816✔
300
    return (TAOS *)rid;
10,816✔
301
  } else {
302
    terrno = code;
84✔
303
  }
304

305
  return NULL;
84✔
306
}
307

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

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

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

350
  releaseTscObj(*(int64_t *)taos);
150✔
351
  return 0;
150✔
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) {
10✔
361
  SFetchWhiteListInfo *pInfo = (SFetchWhiteListInfo *)param;
10✔
362
  TAOS                *taos = &pInfo->connId;
10✔
363
  if (code != TSDB_CODE_SUCCESS) {
10!
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)) {
10!
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));
10!
381
  if (pWhiteLists == NULL) {
10!
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) {
20✔
390
    pWhiteLists[i] = ((uint64_t)wlRsp.pWhiteLists[i].mask << 32) | wlRsp.pWhiteLists[i].ip;
10✔
391
  }
392

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

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

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

409
  int64_t connId = *(int64_t *)taos;
10✔
410

411
  STscObj *pTsc = acquireTscObj(connId);
10✔
412
  if (NULL == pTsc) {
10!
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);
10✔
419
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
10✔
420
  if (msgLen < 0) {
10!
421
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
422
    releaseTscObj(connId);
×
423
    return;
×
424
  }
425

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

433
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
10!
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));
10!
441
  if (pParam == NULL) {
10!
442
    fp(param, terrno, taos, 0, NULL);
×
443
    taosMemoryFree(pReq);
×
444
    releaseTscObj(connId);
×
445
    return;
×
446
  }
447

448
  pParam->connId = connId;
10✔
449
  pParam->userCbFn = fp;
10✔
450
  pParam->userParam = param;
10✔
451
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
10!
452
  if (pSendInfo == NULL) {
10!
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};
10✔
461
  pSendInfo->requestId = generateRequestId();
10✔
462
  pSendInfo->requestObjRefId = 0;
10✔
463
  pSendInfo->param = pParam;
10✔
464
  pSendInfo->fp = fetchWhiteListCallbackFn;
10✔
465
  pSendInfo->msgType = TDMT_MND_GET_USER_WHITELIST;
10✔
466

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

475
void taos_close_internal(void *taos) {
11,306✔
476
  if (taos == NULL) {
11,306✔
477
    return;
1✔
478
  }
479

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

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

488
void taos_close(TAOS *taos) {
11,694✔
489
  if (taos == NULL) {
11,694✔
490
    return;
881✔
491
  }
492

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

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

504
int taos_errno(TAOS_RES *res) {
12,108,239✔
505
  if (res == NULL || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
12,108,239!
506
    return terrno;
1✔
507
  }
508

509
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
12,115,774!
510
    return 0;
×
511
  }
512

513
  return ((SRequestObj *)res)->code;
12,115,921✔
514
}
515

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

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

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

533
void taos_free_result(TAOS_RES *res) {
10,840,592✔
534
  if (NULL == res) {
10,840,592✔
535
    return;
1,333✔
536
  }
537

538
  tscDebug("taos free res %p", res);
10,839,259✔
539

540
  if (TD_RES_QUERY(res)) {
10,846,256✔
541
    SRequestObj *pRequest = (SRequestObj *)res;
10,817,352✔
542
    tscDebug("0x%" PRIx64 " taos_free_result start to free query", pRequest->requestId);
10,817,352✔
543
    destroyRequest(pRequest);
10,817,352✔
544
    return;
10,812,520✔
545
  }
546
  SMqRspObj *pRsp = (SMqRspObj *)res;
28,904✔
547
  if (TD_RES_TMQ(res)) {
28,904✔
548
    tDeleteMqDataRsp(&pRsp->dataRsp);
28,685✔
549
    doFreeReqResultInfo(&pRsp->resInfo);
28,685✔
550
  } else if (TD_RES_TMQ_METADATA(res)) {
219✔
551
    tDeleteSTaosxRsp(&pRsp->dataRsp);
12✔
552
    doFreeReqResultInfo(&pRsp->resInfo);
12✔
553
  } else if (TD_RES_TMQ_META(res)) {
207✔
554
    tDeleteMqMetaRsp(&pRsp->metaRsp);
188✔
555
  } else if (TD_RES_TMQ_BATCH_META(res)) {
19!
556
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
19✔
557
  }
558
  taosMemoryFree(pRsp);
28,904!
559
}
560

561
void taos_kill_query(TAOS *taos) {
10✔
562
  if (NULL == taos) {
10✔
563
    return;
5✔
564
  }
565

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

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

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

583
int taos_num_fields(TAOS_RES *res) { return taos_field_count(res); }
48,705,317✔
584

585
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
16,534,384✔
586
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
16,534,384!
587
    return NULL;
132,182✔
588
  }
589

590
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
16,402,212✔
591
  return pResInfo->userFields;
16,402,212✔
592
}
593

594
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
10,703,378✔
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) {
44,522,269✔
600
  if (res == NULL) {
44,522,269!
601
    return NULL;
×
602
  }
603

604
  if (TD_RES_QUERY(res)) {
44,522,269✔
605
    SRequestObj *pRequest = (SRequestObj *)res;
29,376,117✔
606
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
29,376,117✔
607
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0 || pRequest->killed) {
29,375,855!
608
      return NULL;
264✔
609
    }
610

611
    if (pRequest->inCallback) {
29,375,851!
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);
29,375,851✔
618
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
15,146,152!
619
    SMqRspObj      *msg = ((SMqRspObj *)res);
15,146,152✔
620
    SReqResultInfo *pResultInfo = NULL;
15,146,152✔
621
    if (msg->resIter == -1) {
15,146,152✔
622
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
28,551!
623
        return NULL;
×
624
      }
625
    } else {
626
      pResultInfo = tmqGetCurResInfo(res);
15,117,601✔
627
    }
628

629
    if (pResultInfo->current < pResultInfo->numOfRows) {
15,146,152✔
630
      doSetOneRowPtr(pResultInfo);
14,818,201✔
631
      pResultInfo->current += 1;
14,817,969✔
632
      return pResultInfo->row;
14,817,969✔
633
    } else {
634
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
327,951✔
635
        return NULL;
28,546✔
636
      }
637

638
      doSetOneRowPtr(pResultInfo);
299,438✔
639
      pResultInfo->current += 1;
299,423✔
640
      return pResultInfo->row;
299,423✔
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) {
15,195,675✔
652
  return taos_print_row_with_size(str, INT32_MAX, row, fields, num_fields);
15,195,675✔
653
}
654
int taos_print_row_with_size(char *str, uint32_t size, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
15,195,651✔
655
  int32_t len = 0;
15,195,651✔
656
  for (int i = 0; i < num_fields; ++i) {
91,782,937✔
657
    if (i > 0 && len < size - 1) {
76,578,055!
658
      str[len++] = ' ';
61,410,216✔
659
    }
660

661
    if (row[i] == NULL) {
76,578,055✔
662
      len += tsnprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
543,312✔
663
      continue;
543,286✔
664
    }
665

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

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

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

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

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

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

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

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

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

705
      case TSDB_DATA_TYPE_DOUBLE: {
8,007,143✔
706
        double dv = 0;
8,007,143✔
707
        dv = GET_DOUBLE_VAL(row[i]);
8,007,143✔
708
        len += tsnprintf(str + len, size - len, "%lf", dv);
8,007,143✔
709
      } break;
8,006,952✔
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:
16,863,544✔
724
      case TSDB_DATA_TYPE_NCHAR:
725
      case TSDB_DATA_TYPE_GEOMETRY: {
726
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
16,863,544✔
727
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
16,863,544!
728
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
5,494,142✔
729
          if (charLen > fields[i].bytes || charLen < 0) {
11,369,403!
730
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
×
731
            break;
×
732
          }
733
        } else {
734
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
5,494,141!
735
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
1,153!
736
            break;
×
737
          }
738
        }
739

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

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

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

755
    if (len >= size - 1) {
76,044,000!
756
      break;
×
757
    }
758
  }
759
  if (len < size) {
15,204,882✔
760
    str[len] = 0;
15,195,098✔
761
  }
762

763
  return len;
15,204,882✔
764
}
765

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

771
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
18,616,353✔
772
  return pResInfo->length;
18,616,353✔
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; }
2,405✔
841

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

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

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

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

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

871
  if (TD_RES_QUERY(res)) {
16,092,717✔
872
    SRequestObj *pRequest = (SRequestObj *)res;
974,732✔
873
    return pRequest->body.resInfo.precision;
974,732✔
874
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
15,117,985!
875
    SReqResultInfo *info = tmqGetCurResInfo(res);
15,117,985✔
876
    return info->precision;
15,117,985✔
877
  }
878
  return TSDB_TIME_PRECISION_MILLI;
×
879
}
880

881
int taos_select_db(TAOS *taos, const char *db) {
111✔
882
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
111✔
883
  if (pObj == NULL) {
111!
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) {
111!
890
    releaseTscObj(*(int64_t *)taos);
×
891
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
892
    return terrno;
×
893
  }
894

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

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

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

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

912
  stopAllQueries((SRequestObj *)res);
10,831,444✔
913
}
914

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

924
  SResultColumn *pCol = &pResultInfo->pCol[col];
274,474,169✔
925
  if (IS_VAR_DATA_TYPE(pResultInfo->fields[col].type)) {
274,474,169!
926
    return (pCol->offset[row] == -1);
16✔
927
  } else {
928
    return colDataIsNull_f(pCol->nullbitmap, row);
274,474,153✔
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) {
1,233,484✔
935
  int32_t numOfRows = 0;
1,233,484✔
936
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
1,233,484✔
937
  return numOfRows;
1,233,481✔
938
}
939

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

945
  if (TD_RES_QUERY(res)) {
1,233,484✔
946
    SRequestObj *pRequest = (SRequestObj *)res;
1,231,839✔
947

948
    (*rows) = NULL;
1,231,839✔
949
    (*numOfRows) = 0;
1,231,839✔
950

951
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
1,231,839!
952
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
1,229,329!
953
      return pRequest->code;
6,107✔
954
    }
955

956
    (void)doAsyncFetchRows(pRequest, false, true);
1,225,732✔
957

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

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

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

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

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

988
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
8!
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

1002
  SRequestObj *pRequest = (SRequestObj *)res;
8✔
1003

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

1009
  (void)doAsyncFetchRows(pRequest, false, false);
8✔
1010

1011
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
8✔
1012

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

1017
  return pRequest->code;
8✔
1018
}
1019

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

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

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

1036
  return pResInfo->pCol[columnIndex].offset;
504,531✔
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

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

1105
  releaseTscObj(*(int64_t *)taos);
5✔
1106

1107
  return pTscObj->sDetailVer;
5✔
1108
}
1109

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

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

1135
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
21,516,772✔
1136
  if (NULL == pWrapper) {
21,516,772✔
1137
    return;
10,829,638✔
1138
  }
1139
  destoryCatalogReq(pWrapper->pCatalogReq);
10,687,134✔
1140
  taosMemoryFree(pWrapper->pCatalogReq);
10,700,840!
1141
  qDestroyParseContext(pWrapper->pParseCtx);
10,719,191✔
1142
  taosMemoryFree(pWrapper);
10,718,403!
1143
}
1144

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

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

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

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

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

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

1170
  if (pRequest->parseOnly) {
1,114,846✔
1171
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
411✔
1172
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
411✔
1173
  }
1174

1175
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
1,114,846✔
1176
}
1,114,803✔
1177

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

1204
    *ppTarget = pTarget;
521✔
1205
  }
1206

1207
  return code;
521✔
1208
}
1209

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

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

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

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

1252
  if (code == TSDB_CODE_SUCCESS) {
1,114,803✔
1253
    pRequest->stableQuery = pQuery->stableQuery;
1,043,962✔
1254
    if (pQuery->pRoot) {
1,043,962!
1255
      pRequest->stmtType = pQuery->pRoot->type;
1,043,964✔
1256
    }
1257

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

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

1269
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
1,043,946✔
1270
  } else {
1271
    destorySqlCallbackWrapper(pWrapper);
70,884✔
1272
    pRequest->pWrapper = NULL;
70,885✔
1273
    qDestroyQuery(pRequest->pQuery);
70,885✔
1274
    pRequest->pQuery = NULL;
70,885✔
1275

1276
    if (NEED_CLIENT_HANDLE_ERROR(code)) {
70,885!
1277
      tscDebug("0x%" PRIx64 " client retry to handle the error, code:%d - %s, tryCount:%d,QID:0x%" PRIx64,
13,534✔
1278
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1279
      restartAsyncQuery(pRequest, code);
13,534✔
1280
      return;
13,534✔
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),
57,351!
1285
             pRequest->requestId);
1286
    pRequest->code = code;
57,351✔
1287
    returnToUser(pRequest);
57,351✔
1288
  }
1289
}
1290

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

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

1299
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
2,294,781✔
1300
                                &pWrapper->pRequest->body.queryJob);
1,147,407✔
1301
}
1302

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

1305
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
10,662,123✔
1306
  int32_t code = TSDB_CODE_SUCCESS;
10,662,123✔
1307
  switch (pWrapper->pRequest->pQuery->execStage) {
10,662,123!
1308
    case QUERY_EXEC_STAGE_PARSE: {
33,051✔
1309
      // continue parse after get metadata
1310
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
33,051✔
1311
      break;
33,051✔
1312
    }
1313
    case QUERY_EXEC_STAGE_ANALYSE: {
1,114,328✔
1314
      // analysis after get metadata
1315
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
1,114,328✔
1316
      break;
1,114,344✔
1317
    }
1318
    case QUERY_EXEC_STAGE_SCHEDULE: {
9,526,930✔
1319
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
9,526,930✔
1320
      break;
9,528,365✔
1321
    }
1322
    default:
×
1323
      break;
×
1324
  }
1325
  return code;
10,663,574✔
1326
}
1327

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

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

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

1342
  if (TSDB_CODE_SUCCESS == code) {
33,049✔
1343
    code = phaseAsyncQuery(pWrapper);
31,945✔
1344
  }
1345

1346
  if (TSDB_CODE_SUCCESS != code) {
33,049✔
1347
    tscError("0x%" PRIx64 " error happens, code:%d - %s,QID:0x%" PRIx64, pWrapper->pRequest->self, code,
1,105!
1348
             tstrerror(code), pWrapper->pRequest->requestId);
1349
    destorySqlCallbackWrapper(pWrapper);
1,105✔
1350
    pRequest->pWrapper = NULL;
1,105✔
1351
    terrno = code;
1,105✔
1352
    pRequest->code = code;
1,105✔
1353
    doRequestCallback(pRequest, code);
1,105✔
1354
  }
1355
}
33,049✔
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

1374
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
515✔
1375
  int64_t connId = *(int64_t *)taos;
515✔
1376
  tscDebug("taos_query_a start with sql:%s", sql);
515!
1377
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
515✔
1378
  tscDebug("taos_query_a end with sql:%s", sql);
515!
1379
}
515✔
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) {
10,703,533✔
1387
  const STscObj *pTscObj = pRequest->pTscObj;
10,703,533✔
1388

1389
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
10,703,533!
1390
  if (*pCxt == NULL) {
10,705,371!
1391
    return terrno;
×
1392
  }
1393

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

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

1435
  if (TSDB_CODE_SUCCESS == code) {
10,711,952!
1436
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
10,713,440✔
1437
  }
1438

1439
  if (TSDB_CODE_SUCCESS == code) {
10,656,580!
1440
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
10,658,346✔
1441
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
10,721,312✔
1442
  }
1443

1444
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
10,700,295✔
1445
    int64_t syntaxStart = taosGetTimestampUs();
10,681,042✔
1446

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

1456
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
10,645,538✔
1457
  }
1458

1459
  return code;
10,665,789✔
1460
}
1461

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

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

1475
  if (TSDB_CODE_SUCCESS == code) {
10,696,099✔
1476
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
10,694,177✔
1477
  }
1478

1479
  if (TSDB_CODE_SUCCESS == code) {
10,657,737✔
1480
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
10,639,211✔
1481
    code = phaseAsyncQuery(pWrapper);
10,639,211✔
1482
  }
1483

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

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

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

1511
void restartAsyncQuery(SRequestObj *pRequest, int32_t code) {
14,098✔
1512
  tscInfo("restart request: %s p: %p", pRequest->sqlstr, pRequest);
14,098!
1513
  SRequestObj *pUserReq = pRequest;
14,098✔
1514
  (void)acquireRequest(pRequest->self);
14,098✔
1515
  while (pUserReq) {
14,121!
1516
    if (pUserReq->self == pUserReq->relation.userRefId || pUserReq->relation.userRefId == 0) {
14,121!
1517
      break;
1518
    } else {
1519
      int64_t nextRefId = pUserReq->relation.nextRefId;
23✔
1520
      (void)releaseRequest(pUserReq->self);
23✔
1521
      if (nextRefId) {
23!
1522
        pUserReq = acquireRequest(nextRefId);
23✔
1523
      }
1524
    }
1525
  }
1526
  bool hasSubRequest = pUserReq != pRequest || pRequest->relation.prevRefId != 0;
14,098!
1527
  if (pUserReq) {
14,098!
1528
    destroyCtxInRequest(pUserReq);
14,098✔
1529
    pUserReq->prevCode = code;
14,098✔
1530
    (void)memset(&pUserReq->relation, 0, sizeof(pUserReq->relation));
14,098✔
1531
  } else {
1532
    tscError("User req is missing");
×
1533
    (void)removeFromMostPrevReq(pRequest);
×
1534
    return;
×
1535
  }
1536
  if (hasSubRequest)
14,098✔
1537
    (void)removeFromMostPrevReq(pRequest);
23✔
1538
  else
1539
    (void)releaseRequest(pUserReq->self);
14,075✔
1540
  doAsyncQuery(pUserReq, true);
14,097✔
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) {
933,588✔
1550
  SAsyncFetchParam *param = pParam;
933,588✔
1551
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
933,588✔
1552
  taosMemoryFree(param);
933,582!
1553
  return TSDB_CODE_SUCCESS;
933,584✔
1554
}
1555

1556
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
933,584✔
1557
  if (res == NULL || fp == NULL) {
933,584!
1558
    tscError("taos_fetch_rows_a invalid paras");
×
1559
    return;
×
1560
  }
1561
  if (!TD_RES_QUERY(res)) {
933,588!
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;
933,588✔
1568
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
933,588✔
1569
    fp(param, res, 0);
5✔
1570
    return;
5✔
1571
  }
1572

1573
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
933,583!
1574
  if (!pParam) {
933,587!
1575
    fp(param, res, terrno);
×
1576
    return;
×
1577
  }
1578
  pParam->pReq = pRequest;
933,587✔
1579
  pParam->fp = fp;
933,587✔
1580
  pParam->param = param;
933,587✔
1581
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
933,587✔
1582
  if (TSDB_CODE_SUCCESS != code) {
933,587!
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) {
2✔
1623
  if (NULL == taos) {
2!
1624
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1625
    return terrno;
×
1626
  }
1627

1628
  if (NULL == db || NULL == dbInfo) {
2!
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;
2✔
1635
  SRequestObj *pRequest = NULL;
2✔
1636
  char        *sql = "taos_get_db_route_info";
2✔
1637
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
2✔
1638
  if (code != TSDB_CODE_SUCCESS) {
2!
1639
    terrno = code;
×
1640
    return terrno;
×
1641
  }
1642

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

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

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

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

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

1663
_return:
2✔
1664

1665
  terrno = code;
2✔
1666

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

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

1677
  if (NULL == db || NULL == table || NULL == vgId) {
200!
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;
200✔
1684
  SRequestObj *pRequest = NULL;
200✔
1685
  char        *sql = "taos_get_table_vgId";
200✔
1686
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
200✔
1687
  if (code != TSDB_CODE_SUCCESS) {
200!
1688
    return terrno;
×
1689
  }
1690

1691
  pRequest->syncQuery = true;
200✔
1692

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

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

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

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

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

1714
  *vgId = vgInfo.vgId;
200✔
1715

1716
_return:
200✔
1717

1718
  terrno = code;
200✔
1719

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

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

1730
  if (NULL == db || NULL == table || NULL == vgId || tableNum <= 0) {
1!
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;
1✔
1737
  SRequestObj *pRequest = NULL;
1✔
1738
  char        *sql = "taos_get_table_vgId";
1✔
1739
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
1✔
1740
  if (code != TSDB_CODE_SUCCESS) {
1!
1741
    return terrno;
×
1742
  }
1743

1744
  pRequest->syncQuery = true;
1✔
1745

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

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

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

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

1763
_return:
1✔
1764

1765
  terrno = code;
1✔
1766

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

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

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

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

1787
  int32_t length = (int32_t)strlen(tableNameList);
3✔
1788
  if (0 == length) {
3!
1789
    return TSDB_CODE_SUCCESS;
×
1790
  } else if (length > MAX_TABLE_NAME_LENGTH) {
3!
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";
3✔
1796
  code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
3✔
1797
  if (code != TSDB_CODE_SUCCESS) {
3!
1798
    terrno = code;
×
1799
    goto _return;
×
1800
  }
1801

1802
  pRequest->syncQuery = true;
3✔
1803

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

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

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

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

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

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

1838
TAOS_STMT *taos_stmt_init(TAOS *taos) {
340✔
1839
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
340✔
1840
  if (NULL == pObj) {
340!
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);
340✔
1847
  if (NULL == pStmt) {
340!
1848
    tscError("stmt init failed, errcode:%s", terrstr());
×
1849
  }
1850
  releaseTscObj(*(int64_t *)taos);
340✔
1851

1852
  return pStmt;
340✔
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) {
11✔
1873
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
11✔
1874
  if (NULL == pObj) {
11!
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);
11✔
1881
  if (NULL == pStmt) {
11!
1882
    tscError("stmt init failed, errcode:%s", terrstr());
×
1883
  }
1884
  releaseTscObj(*(int64_t *)taos);
11✔
1885

1886
  return pStmt;
11✔
1887
}
1888

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

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

1899
int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags) {
20,083✔
1900
  if (stmt == NULL || name == NULL) {
20,083!
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);
20,083✔
1907
  if (code) {
20,083!
1908
    return code;
×
1909
  }
1910

1911
  if (tags) {
20,083!
1912
    return stmtSetTbTags(stmt, tags);
20,083✔
1913
  }
1914

1915
  return TSDB_CODE_SUCCESS;
×
1916
}
1917

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

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

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

1935
  return stmtSetTbTags(stmt, tags);
20,040✔
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) {
20,040✔
1941
  if (stmt == NULL || NULL == fieldNum) {
20,040!
1942
    tscError("NULL parameter for %s", __FUNCTION__);
×
1943
    terrno = TSDB_CODE_INVALID_PARA;
×
1944
    return terrno;
×
1945
  }
1946

1947
  return stmtGetTagFields(stmt, fieldNum, fields);
20,040✔
1948
}
1949

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

1957
  return stmtGetColFields(stmt, fieldNum, fields);
268,567✔
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

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

1974
  if (bind->num > 1) {
13,820!
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

1980
  return stmtBindBatch(stmt, bind, -1);
13,820✔
1981
}
1982

1983
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
147,959✔
1984
  if (stmt == NULL || bind == NULL) {
147,959!
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) {
147,959!
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;
147,959✔
1997
  int32_t code = stmtIsInsert(stmt, &insert);
147,959✔
1998
  if (TSDB_CODE_SUCCESS != code) {
147,959!
1999
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2000
    return code;
×
2001
  }
2002
  if (0 == insert && bind->num > 1) {
147,959!
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);
147,959✔
2009
}
2010

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

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

2024
  int32_t insert = 0;
1,102,302✔
2025
  int32_t code = stmtIsInsert(stmt, &insert);
1,102,302✔
2026
  if (TSDB_CODE_SUCCESS != code) {
1,102,302!
2027
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2028
    return code;
×
2029
  }
2030
  if (0 == insert && bind->num > 1) {
1,102,302!
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);
1,102,302✔
2037
}
2038

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

2046
  return stmtAddBatch(stmt);
274,085✔
2047
}
2048

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

2056
  return stmtExec(stmt);
113,086✔
2057
}
2058

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

2066
  return stmtIsInsert(stmt, insert);
23,084✔
2067
}
2068

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

2076
  return stmtGetParamNum(stmt, nums);
1,331✔
2077
}
2078

2079
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
2,324,534✔
2080
  if (stmt == NULL || type == NULL || NULL == bytes || idx < 0) {
2,324,534!
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);
2,324,534✔
2087
}
2088

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

2096
  return stmtUseResult(stmt);
21,802✔
2097
}
2098

2099
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
4✔
2100

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

2108
  return stmtAffectedRows(stmt);
297✔
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) {
351✔
2122
  if (stmt == NULL) {
351!
2123
    tscError("NULL parameter for %s", __FUNCTION__);
×
2124
    terrno = TSDB_CODE_INVALID_PARA;
×
2125
    return terrno;
×
2126
  }
2127

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

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

2144
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
×
2145

2146
  releaseTscObj(*(int64_t *)taos);
×
2147

2148
  return pStmt;
×
2149
}
2150

2151
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
×
2152
  if (stmt == NULL || sql == NULL) {
×
2153
    tscError("NULL parameter for %s", __FUNCTION__);
×
2154
    terrno = TSDB_CODE_INVALID_PARA;
×
2155
    return terrno;
×
2156
  }
2157

2158
  return stmtPrepare2(stmt, sql, length);
×
2159
}
2160

2161
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
×
2162
  if (stmt == NULL) {
×
2163
    tscError("NULL parameter for %s", __FUNCTION__);
×
2164
    terrno = TSDB_CODE_INVALID_PARA;
×
2165
    return terrno;
×
2166
  }
2167

2168
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2169
  if (pStmt->options.asyncExecFn && !pStmt->semWaited) {
×
2170
    if (tsem_wait(&pStmt->asyncQuerySem) != 0) {
×
2171
      tscError("wait async query sem failed");
×
2172
    }
2173
    pStmt->semWaited = true;
×
2174
  }
2175

2176
  SSHashObj *hashTbnames = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR));
×
2177
  if (NULL == hashTbnames) {
×
2178
    tscError("stmt2 bind failed: %s", tstrerror(terrno));
×
2179
    return terrno;
×
2180
  }
2181

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

2192
        code = tSimpleHashPut(hashTbnames, bindv->tbnames[i], strlen(bindv->tbnames[i]), NULL, 0);
×
2193
        if (code) {
×
2194
          goto out;
×
2195
        }
2196
      }
2197

2198
      code = stmtSetTbName2(stmt, bindv->tbnames[i]);
×
2199
      if (code) {
×
2200
        goto out;
×
2201
      }
2202
    }
2203

2204
    if (bindv->tags && bindv->tags[i]) {
×
2205
      code = stmtSetTbTags2(stmt, bindv->tags[i]);
×
2206
      if (code) {
×
2207
        goto out;
×
2208
      }
2209
    } else if (pStmt->bInfo.tbType == TSDB_CHILD_TABLE && pStmt->sql.autoCreateTbl) {
×
2210
      code = stmtSetTbTags2(stmt, NULL);
×
2211
      if (code) {
×
2212
        return code;
×
2213
      }
2214
    }
2215

2216
    if (bindv->bind_cols && bindv->bind_cols[i]) {
×
2217
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
×
2218

2219
      if (bind->num <= 0 || bind->num > INT16_MAX) {
×
2220
        tscError("invalid bind num %d", bind->num);
×
2221
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2222
        goto out;
×
2223
      }
2224

2225
      int32_t insert = 0;
×
2226
      (void)stmtIsInsert2(stmt, &insert);
×
2227
      if (0 == insert && bind->num > 1) {
×
2228
        tscError("only one row data allowed for query");
×
2229
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2230
        goto out;
×
2231
      }
2232

2233
      code = stmtBindBatch2(stmt, bind, col_idx);
×
2234
      if (TSDB_CODE_SUCCESS != code) {
×
2235
        goto out;
×
2236
      }
2237
    }
2238
  }
2239

2240
out:
×
2241
  tSimpleHashCleanup(hashTbnames);
×
2242

2243
  return code;
×
2244
}
2245

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

2253
  return stmtExec2(stmt, affected_rows);
×
2254
}
2255

2256
int taos_stmt2_close(TAOS_STMT2 *stmt) {
×
2257
  if (stmt == NULL) {
×
2258
    tscError("NULL parameter for %s", __FUNCTION__);
×
2259
    terrno = TSDB_CODE_INVALID_PARA;
×
2260
    return terrno;
×
2261
  }
2262

2263
  return stmtClose2(stmt);
×
2264
}
2265

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

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

2276
int taos_stmt2_get_fields(TAOS_STMT2 *stmt, int *count, TAOS_FIELD_ALL **fields) {
1✔
2277
  if (stmt == NULL || count == NULL) {
1!
2278
    tscError("NULL parameter for %s", __FUNCTION__);
1!
2279
    terrno = TSDB_CODE_INVALID_PARA;
1✔
2280
    return terrno;
1✔
2281
  }
2282

2283
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2284
  if (pStmt->sql.type == 0) {
×
2285
    int isInsert = 0;
×
2286
    (void)stmtIsInsert2(stmt, &isInsert);
×
2287
    if (!isInsert) {
×
2288
      pStmt->sql.type = STMT_TYPE_QUERY;
×
2289
    }
2290
  }
2291
  if (pStmt->sql.type == STMT_TYPE_QUERY) {
×
2292
    return stmtGetParamNum2(stmt, count);
×
2293
  }
2294

2295
  return stmtGetStbColFields2(stmt, count, fields);
×
2296
}
2297

2298
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_ALL *fields) {
×
2299
  (void)stmt;
2300
  if (!fields) return;
×
2301
  taosMemoryFree(fields);
×
2302
}
2303

2304
TAOS_RES *taos_stmt2_result(TAOS_STMT2 *stmt) {
×
2305
  if (stmt == NULL) {
×
2306
    tscError("NULL parameter for %s", __FUNCTION__);
×
2307
    terrno = TSDB_CODE_INVALID_PARA;
×
2308
    return NULL;
×
2309
  }
2310

2311
  return stmtUseResult2(stmt);
×
2312
}
2313

2314
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmtErrstr2(stmt); }
×
2315

2316
int taos_set_conn_mode(TAOS *taos, int mode, int value) {
5✔
2317
  if (taos == NULL) {
5!
2318
    terrno = TSDB_CODE_INVALID_PARA;
×
2319
    return terrno;
×
2320
  }
2321

2322
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
5✔
2323
  if (NULL == pObj) {
5!
2324
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2325
    tscError("invalid parameter for %s", __func__);
×
2326
    return terrno;
×
2327
  }
2328
  switch (mode) {
5!
2329
    case TAOS_CONN_MODE_BI:
5✔
2330
      atomic_store_8(&pObj->biMode, value);
5✔
2331
      break;
5✔
2332
    default:
×
2333
      tscError("not supported mode.");
×
2334
      return TSDB_CODE_INVALID_PARA;
×
2335
  }
2336
  return 0;
5✔
2337
}
2338

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