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

taosdata / TDengine / #3844

10 Apr 2025 08:48AM UTC coverage: 43.852% (-19.2%) from 63.077%
#3844

push

travis-ci

web-flow
fix: the REPLICA parameter supports plural forms when used to create and alter a database (#30732)

104394 of 310659 branches covered (33.6%)

Branch coverage included in aggregate %.

167442 of 309240 relevant lines covered (54.15%)

3866624.94 hits per line

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

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

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

35
#define TSC_VAR_NOT_RELEASE 1
36
#define TSC_VAR_RELEASED    0
37

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

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

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

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

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

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

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

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

82
void tzCleanup() {
30,280✔
83
  taosHashCleanup(pTimezoneMap);
30,280✔
84
  taosHashCleanup(pTimezoneNameMap);
30,280✔
85
}
30,280✔
86

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

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

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

124
END:
20✔
125
  return tz;
34✔
126
}
127
#endif
128

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

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

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

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

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

156
  if (option == TSDB_OPTION_CONNECTION_CLEAR) {
72✔
157
    val = NULL;
2✔
158
  }
159

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

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

204
  if (option == TSDB_OPTION_CONNECTION_USER_IP || option == TSDB_OPTION_CONNECTION_CLEAR) {
68✔
205
    if (val != NULL) {
14✔
206
      pObj->optionInfo.userIp = taosInetAddr(val);
10✔
207
      if (pObj->optionInfo.userIp == INADDR_NONE) {
10✔
208
        code = TSDB_CODE_INVALID_PARA;
6✔
209
        goto END;
6✔
210
      }
211
    } else {
212
      pObj->optionInfo.userIp = INADDR_NONE;
4✔
213
    }
214
  }
215

216
END:
54✔
217
  releaseTscObj(*(int64_t *)taos);
72✔
218
  return terrno = code;
72✔
219
}
220

221
int taos_options_connection(TAOS *taos, TSDB_OPTION_CONNECTION option, const void *arg, ...) {
76✔
222
  return setConnectionOption(taos, option, (const char *)arg);
76✔
223
}
224

225
// this function may be called by user or system, or by both simultaneously.
226
void taos_cleanup(void) {
30,288✔
227
  tscDebug("start to cleanup client environment");
30,288✔
228
  if (atomic_val_compare_exchange_32(&sentinel, TSC_VAR_NOT_RELEASE, TSC_VAR_RELEASED) != TSC_VAR_NOT_RELEASE) {
30,288✔
229
    return;
8✔
230
  }
231

232
  monitorClose();
30,280✔
233
  tscStopCrashReport();
30,280✔
234

235
  hbMgrCleanUp();
30,280✔
236

237
  catalogDestroy();
30,280✔
238
  schedulerDestroy();
30,280✔
239

240
  fmFuncMgtDestroy();
30,280✔
241
  qCleanupKeywordsTable();
30,280✔
242

243
  if (TSDB_CODE_SUCCESS != cleanupTaskQueue()) {
30,280!
244
    tscWarn("failed to cleanup task queue");
×
245
  }
246

247
#if !defined(WINDOWS) && !defined(TD_ASTRA)
248
  tzCleanup();
30,280✔
249
#endif
250
  tmqMgmtClose();
30,280✔
251

252
  int32_t id = clientReqRefPool;
30,280✔
253
  clientReqRefPool = -1;
30,280✔
254
  taosCloseRef(id);
30,280✔
255

256
  id = clientConnRefPool;
30,280✔
257
  clientConnRefPool = -1;
30,280✔
258
  taosCloseRef(id);
30,280✔
259

260
  nodesDestroyAllocatorSet();
30,280✔
261
  cleanupAppInfo();
30,280✔
262
  rpcCleanup();
30,280✔
263
  tscDebug("rpc cleanup");
30,280✔
264

265
  taosConvDestroy();
30,280✔
266
  DestroyRegexCache();
30,280✔
267
#ifdef TAOSD_INTEGRATED
268
  shellStopDaemon();
269
#endif
270
  tscInfo("all local resources released");
30,280!
271
  taosCleanupCfg();
30,280✔
272
#ifndef TAOSD_INTEGRATED
273
  taosCloseLog();
30,280✔
274
#endif
275
}
276

277
static setConfRet taos_set_config_imp(const char *config) {
×
278
  setConfRet ret = {SET_CONF_RET_SUCC, {0}};
×
279
  // TODO: need re-implementation
280
  return ret;
×
281
}
282

283
setConfRet taos_set_config(const char *config) {
×
284
  // TODO  pthread_mutex_lock(&setConfMutex);
285
  setConfRet ret = taos_set_config_imp(config);
×
286
  //  pthread_mutex_unlock(&setConfMutex);
287
  return ret;
×
288
}
289

290
TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) {
36,818✔
291
  tscInfo("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
36,818!
292
  if (user == NULL) {
36,818✔
293
    user = TSDB_DEFAULT_USER;
696✔
294
  }
295

296
  if (pass == NULL) {
36,818✔
297
    pass = TSDB_DEFAULT_PASS;
696✔
298
  }
299

300
  STscObj *pObj = NULL;
36,818✔
301
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
36,818✔
302
  if (TSDB_CODE_SUCCESS == code) {
36,818✔
303
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
36,786!
304
    if (NULL == rid) {
36,786!
305
      tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db);
×
306
      return NULL;
×
307
    }
308
    *rid = pObj->id;
36,786✔
309
    return (TAOS *)rid;
36,786✔
310
  } else {
311
    terrno = code;
32✔
312
  }
313

314
  return NULL;
32✔
315
}
316

317
int taos_set_notify_cb(TAOS *taos, __taos_notify_fn_t fp, void *param, int type) {
300✔
318
  if (taos == NULL) {
300!
319
    terrno = TSDB_CODE_INVALID_PARA;
×
320
    return terrno;
×
321
  }
322

323
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
300✔
324
  if (NULL == pObj) {
300!
325
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
326
    tscError("invalid parameter for %s", __func__);
×
327
    return terrno;
×
328
  }
329

330
  switch (type) {
300!
331
    case TAOS_NOTIFY_PASSVER: {
80✔
332
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
80!
333
      pObj->passInfo.fp = fp;
80✔
334
      pObj->passInfo.param = param;
80✔
335
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
80!
336
      break;
80✔
337
    }
338
    case TAOS_NOTIFY_WHITELIST_VER: {
20✔
339
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
20!
340
      pObj->whiteListInfo.fp = fp;
20✔
341
      pObj->whiteListInfo.param = param;
20✔
342
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
20!
343
      break;
20✔
344
    }
345
    case TAOS_NOTIFY_USER_DROPPED: {
200✔
346
      TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex));
200!
347
      pObj->userDroppedInfo.fp = fp;
200✔
348
      pObj->userDroppedInfo.param = param;
200✔
349
      TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex));
200!
350
      break;
200✔
351
    }
352
    default: {
×
353
      terrno = TSDB_CODE_INVALID_PARA;
×
354
      releaseTscObj(*(int64_t *)taos);
×
355
      return terrno;
×
356
    }
357
  }
358

359
  releaseTscObj(*(int64_t *)taos);
300✔
360
  return 0;
300✔
361
}
362

363
typedef struct SFetchWhiteListInfo {
364
  int64_t                     connId;
365
  __taos_async_whitelist_fn_t userCbFn;
366
  void                       *userParam;
367
} SFetchWhiteListInfo;
368

369
int32_t fetchWhiteListCallbackFn(void *param, SDataBuf *pMsg, int32_t code) {
20✔
370
  SFetchWhiteListInfo *pInfo = (SFetchWhiteListInfo *)param;
20✔
371
  TAOS                *taos = &pInfo->connId;
20✔
372
  if (code != TSDB_CODE_SUCCESS) {
20!
373
    pInfo->userCbFn(pInfo->userParam, code, taos, 0, NULL);
×
374
    taosMemoryFree(pMsg->pData);
×
375
    taosMemoryFree(pMsg->pEpSet);
×
376
    taosMemoryFree(pInfo);
×
377
    return code;
×
378
  }
379

380
  SGetUserWhiteListRsp wlRsp;
381
  if (TSDB_CODE_SUCCESS != tDeserializeSGetUserWhiteListRsp(pMsg->pData, pMsg->len, &wlRsp)) {
20!
382
    taosMemoryFree(pMsg->pData);
×
383
    taosMemoryFree(pMsg->pEpSet);
×
384
    taosMemoryFree(pInfo);
×
385
    tFreeSGetUserWhiteListRsp(&wlRsp);
×
386
    return terrno;
×
387
  }
388

389
  uint64_t *pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(uint64_t));
20!
390
  if (pWhiteLists == NULL) {
20!
391
    taosMemoryFree(pMsg->pData);
×
392
    taosMemoryFree(pMsg->pEpSet);
×
393
    taosMemoryFree(pInfo);
×
394
    tFreeSGetUserWhiteListRsp(&wlRsp);
×
395
    return terrno;
×
396
  }
397

398
  for (int i = 0; i < wlRsp.numWhiteLists; ++i) {
40✔
399
    pWhiteLists[i] = ((uint64_t)wlRsp.pWhiteLists[i].mask << 32) | wlRsp.pWhiteLists[i].ip;
20✔
400
  }
401

402
  pInfo->userCbFn(pInfo->userParam, code, taos, wlRsp.numWhiteLists, pWhiteLists);
20✔
403

404
  taosMemoryFree(pWhiteLists);
20!
405
  taosMemoryFree(pMsg->pData);
20!
406
  taosMemoryFree(pMsg->pEpSet);
20!
407
  taosMemoryFree(pInfo);
20!
408
  tFreeSGetUserWhiteListRsp(&wlRsp);
20✔
409
  return code;
20✔
410
}
411

412
void taos_fetch_whitelist_a(TAOS *taos, __taos_async_whitelist_fn_t fp, void *param) {
20✔
413
  if (NULL == taos) {
20!
414
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
415
    return;
×
416
  }
417

418
  int64_t connId = *(int64_t *)taos;
20✔
419

420
  STscObj *pTsc = acquireTscObj(connId);
20✔
421
  if (NULL == pTsc) {
20!
422
    fp(param, TSDB_CODE_TSC_DISCONNECTED, taos, 0, NULL);
×
423
    return;
×
424
  }
425

426
  SGetUserWhiteListReq req;
427
  (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN);
20✔
428
  int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req);
20✔
429
  if (msgLen < 0) {
20!
430
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
431
    releaseTscObj(connId);
×
432
    return;
×
433
  }
434

435
  void *pReq = taosMemoryMalloc(msgLen);
20!
436
  if (pReq == NULL) {
20!
437
    fp(param, terrno, taos, 0, NULL);
×
438
    releaseTscObj(connId);
×
439
    return;
×
440
  }
441

442
  if (tSerializeSGetUserWhiteListReq(pReq, msgLen, &req) < 0) {
20!
443
    fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL);
×
444
    taosMemoryFree(pReq);
×
445
    releaseTscObj(connId);
×
446
    return;
×
447
  }
448

449
  SFetchWhiteListInfo *pParam = taosMemoryMalloc(sizeof(SFetchWhiteListInfo));
20!
450
  if (pParam == NULL) {
20!
451
    fp(param, terrno, taos, 0, NULL);
×
452
    taosMemoryFree(pReq);
×
453
    releaseTscObj(connId);
×
454
    return;
×
455
  }
456

457
  pParam->connId = connId;
20✔
458
  pParam->userCbFn = fp;
20✔
459
  pParam->userParam = param;
20✔
460
  SMsgSendInfo *pSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
20!
461
  if (pSendInfo == NULL) {
20!
462
    fp(param, terrno, taos, 0, NULL);
×
463
    taosMemoryFree(pParam);
×
464
    taosMemoryFree(pReq);
×
465
    releaseTscObj(connId);
×
466
    return;
×
467
  }
468

469
  pSendInfo->msgInfo = (SDataBuf){.pData = pReq, .len = msgLen, .handle = NULL};
20✔
470
  pSendInfo->requestId = generateRequestId();
20✔
471
  pSendInfo->requestObjRefId = 0;
20✔
472
  pSendInfo->param = pParam;
20✔
473
  pSendInfo->fp = fetchWhiteListCallbackFn;
20✔
474
  pSendInfo->msgType = TDMT_MND_GET_USER_WHITELIST;
20✔
475

476
  SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp);
20✔
477
  if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, NULL, pSendInfo)) {
20!
478
    tscWarn("failed to async send msg to server");
×
479
  }
480
  releaseTscObj(connId);
20✔
481
  return;
20✔
482
}
483

484
void taos_close_internal(void *taos) {
37,628✔
485
  if (taos == NULL) {
37,628!
486
    return;
×
487
  }
488

489
  STscObj *pTscObj = (STscObj *)taos;
37,628✔
490
  tscDebug("conn:0x%" PRIx64 ", try to close connection, numOfReq:%d", pTscObj->id, pTscObj->numOfReqs);
37,628✔
491

492
  if (TSDB_CODE_SUCCESS != taosRemoveRef(clientConnRefPool, pTscObj->id)) {
37,628!
493
    tscError("conn:0x%" PRIx64 ", failed to remove ref from conn pool", pTscObj->id);
×
494
  }
495
}
496

497
void taos_close(TAOS *taos) {
38,564✔
498
  if (taos == NULL) {
38,564✔
499
    return;
1,792✔
500
  }
501

502
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
36,772✔
503
  if (NULL == pObj) {
36,772!
504
    taosMemoryFree(taos);
×
505
    return;
×
506
  }
507

508
  taos_close_internal(pObj);
36,772✔
509
  releaseTscObj(*(int64_t *)taos);
36,772✔
510
  taosMemoryFree(taos);
36,772!
511
}
512

513
int taos_errno(TAOS_RES *res) {
7,533,602✔
514
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
7,533,602!
515
    return terrno;
×
516
  }
517

518
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
7,533,602✔
519
    return 0;
108✔
520
  }
521

522
  return ((SRequestObj *)res)->code;
7,533,494✔
523
}
524

525
const char *taos_errstr(TAOS_RES *res) {
159,282✔
526
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
159,282!
527
    return (const char *)tstrerror(terrno);
32✔
528
  }
529

530
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
159,250!
531
    return "success";
×
532
  }
533

534
  SRequestObj *pRequest = (SRequestObj *)res;
159,250✔
535
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
159,250!
536
    return pRequest->msgBuf;
144,216✔
537
  } else {
538
    return (const char *)tstrerror(pRequest->code);
15,034✔
539
  }
540
}
541

542
void taos_free_result(TAOS_RES *res) {
5,208,606✔
543
  if (NULL == res) {
5,208,606✔
544
    return;
80,222✔
545
  }
546

547
  tscTrace("res:%p, will be freed", res);
5,128,384✔
548

549
  if (TD_RES_QUERY(res)) {
5,128,426✔
550
    SRequestObj *pRequest = (SRequestObj *)res;
4,933,836✔
551
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
4,933,836✔
552
    destroyRequest(pRequest);
4,933,836✔
553
    return;
4,933,844✔
554
  }
555

556
  SMqRspObj *pRsp = (SMqRspObj *)res;
194,590✔
557
  if (TD_RES_TMQ(res)) {
194,590✔
558
    tDeleteMqDataRsp(&pRsp->dataRsp);
194,042✔
559
    doFreeReqResultInfo(&pRsp->resInfo);
194,046✔
560
  } else if (TD_RES_TMQ_METADATA(res)) {
548✔
561
    tDeleteSTaosxRsp(&pRsp->dataRsp);
24✔
562
    doFreeReqResultInfo(&pRsp->resInfo);
24✔
563
  } else if (TD_RES_TMQ_META(res)) {
524✔
564
    tDeleteMqMetaRsp(&pRsp->metaRsp);
454✔
565
  } else if (TD_RES_TMQ_BATCH_META(res)) {
70✔
566
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
36✔
567
  } else if (TD_RES_TMQ_RAW(res)) {
34!
568
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
34✔
569
  }
570
  taosMemoryFree(pRsp);
194,592!
571
}
572

573
void taos_kill_query(TAOS *taos) {
×
574
  if (NULL == taos) {
×
575
    return;
×
576
  }
577

578
  int64_t  rid = *(int64_t *)taos;
×
579
  STscObj *pTscObj = acquireTscObj(rid);
×
580
  if (pTscObj) {
×
581
    stopAllRequests(pTscObj->pRequests);
×
582
  }
583
  releaseTscObj(rid);
×
584
}
585

586
int taos_field_count(TAOS_RES *res) {
110,812,854✔
587
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
110,812,854!
588
    return 0;
166✔
589
  }
590

591
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
110,812,688✔
592
  return pResInfo->numOfCols;
110,812,688✔
593
}
594

595
int taos_num_fields(TAOS_RES *res) { return taos_field_count(res); }
72,352,242✔
596

597
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
33,065,432✔
598
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
33,065,432!
599
    return NULL;
259,144✔
600
  }
601

602
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
32,805,712✔
603
  return pResInfo->userFields;
32,805,712✔
604
}
605

606
TAOS_RES *taos_query(TAOS *taos, const char *sql) { return taosQueryImpl(taos, sql, false, TD_REQ_FROM_APP); }
4,853,150✔
607
TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqid) {
×
608
  return taosQueryImplWithReqid(taos, sql, false, reqid);
×
609
}
610

611
TAOS_FIELD_E *taos_fetch_fields_e(TAOS_RES *res) {
×
612
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
613
    return NULL;
×
614
  }
615
  SReqResultInfo* pResInfo = tscGetCurResInfo(res);
×
616
  return pResInfo->fields;
×
617
}
618

619
TAOS_ROW taos_fetch_row(TAOS_RES *res) {
64,699,744✔
620
  if (res == NULL) {
64,699,744!
621
    return NULL;
×
622
  }
623

624
  if (TD_RES_QUERY(res)) {
64,699,744✔
625
    SRequestObj *pRequest = (SRequestObj *)res;
34,043,226✔
626
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
34,043,226!
627
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0 || pRequest->killed) {
34,042,900!
628
      return NULL;
332✔
629
    }
630

631
    if (pRequest->inCallback) {
34,042,894✔
632
      tscError("can not call taos_fetch_row before query callback ends.");
2!
633
      terrno = TSDB_CODE_TSC_INVALID_OPERATION;
2✔
634
      return NULL;
2✔
635
    }
636

637
    return doAsyncFetchRows(pRequest, true, true);
34,042,892✔
638
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
30,656,518!
639
    SMqRspObj      *msg = ((SMqRspObj *)res);
30,656,518✔
640
    SReqResultInfo *pResultInfo = NULL;
30,656,518✔
641
    if (msg->resIter == -1) {
30,656,518✔
642
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
193,868!
643
        return NULL;
×
644
      }
645
    } else {
646
      pResultInfo = tmqGetCurResInfo(res);
30,462,650✔
647
    }
648

649
    if (pResultInfo->current < pResultInfo->numOfRows) {
30,656,516✔
650
      doSetOneRowPtr(pResultInfo);
29,994,160✔
651
      pResultInfo->current += 1;
29,992,724✔
652
      return pResultInfo->row;
29,992,724✔
653
    } else {
654
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
662,356✔
655
        return NULL;
193,856✔
656
      }
657

658
      doSetOneRowPtr(pResultInfo);
468,466✔
659
      pResultInfo->current += 1;
468,458✔
660
      return pResultInfo->row;
468,458✔
661
    }
662
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
663
    return NULL;
×
664
  } else {
665
    tscError("invalid result passed to taos_fetch_row");
×
666
    terrno = TSDB_CODE_TMQ_INVALID_DATA;
×
667
    return NULL;
×
668
  }
669
}
670

671
int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
30,463,512✔
672
  return taos_print_row_with_size(str, INT32_MAX, row, fields, num_fields);
30,463,512✔
673
}
674
int taos_print_row_with_size(char *str, uint32_t size, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
30,463,302✔
675
  int32_t len = 0;
30,463,302✔
676
  for (int i = 0; i < num_fields; ++i) {
182,388,196✔
677
    if (i > 0 && len < size - 1) {
151,916,400!
678
      str[len++] = ' ';
121,501,728✔
679
    }
680

681
    if (row[i] == NULL) {
151,916,400✔
682
      len += tsnprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
532,242✔
683
      continue;
532,222✔
684
    }
685

686
    switch (fields[i].type) {
151,384,158!
687
      case TSDB_DATA_TYPE_TINYINT:
214✔
688
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
214✔
689
        break;
214✔
690

691
      case TSDB_DATA_TYPE_UTINYINT:
200✔
692
        len += tsnprintf(str + len, size - len, "%u", *((uint8_t *)row[i]));
200✔
693
        break;
200✔
694

695
      case TSDB_DATA_TYPE_SMALLINT:
206✔
696
        len += tsnprintf(str + len, size - len, "%d", *((int16_t *)row[i]));
206✔
697
        break;
206✔
698

699
      case TSDB_DATA_TYPE_USMALLINT:
200✔
700
        len += tsnprintf(str + len, size - len, "%u", *((uint16_t *)row[i]));
200✔
701
        break;
200✔
702

703
      case TSDB_DATA_TYPE_INT:
32,499,314✔
704
        len += tsnprintf(str + len, size - len, "%d", *((int32_t *)row[i]));
32,499,314✔
705
        break;
32,502,196✔
706

707
      case TSDB_DATA_TYPE_UINT:
200✔
708
        len += tsnprintf(str + len, size - len, "%u", *((uint32_t *)row[i]));
200✔
709
        break;
200✔
710

711
      case TSDB_DATA_TYPE_BIGINT:
27,677,416✔
712
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
27,677,416✔
713
        break;
27,676,950✔
714

715
      case TSDB_DATA_TYPE_UBIGINT:
200✔
716
        len += tsnprintf(str + len, size - len, "%" PRIu64, *((uint64_t *)row[i]));
200✔
717
        break;
200✔
718

719
      case TSDB_DATA_TYPE_FLOAT: {
2,884✔
720
        float fv = 0;
2,884✔
721
        fv = GET_FLOAT_VAL(row[i]);
2,884✔
722
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
2,884✔
723
      } break;
2,884✔
724

725
      case TSDB_DATA_TYPE_DOUBLE: {
16,065,434✔
726
        double dv = 0;
16,065,434✔
727
        dv = GET_DOUBLE_VAL(row[i]);
16,065,434✔
728
        len += snprintf(str + len, size - len, "%.*g", DBL_DIG, dv);
16,065,434✔
729
      } break;
16,065,434✔
730

731
      case TSDB_DATA_TYPE_VARBINARY: {
×
732
        void    *data = NULL;
×
733
        uint32_t tmp = 0;
×
734
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
×
735
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
×
736
          break;
×
737
        }
738
        uint32_t copyLen = TMIN(size - len - 1, tmp);
×
739
        (void)memcpy(str + len, data, copyLen);
×
740
        len += copyLen;
×
741
        taosMemoryFree(data);
×
742
      } break;
×
743
      case TSDB_DATA_TYPE_BINARY:
33,803,478✔
744
      case TSDB_DATA_TYPE_NCHAR:
745
      case TSDB_DATA_TYPE_GEOMETRY: {
746
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
33,803,478✔
747
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
33,803,478!
748
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
10,906,602!
749
          if (charLen > fields[i].bytes || charLen < 0) {
22,896,852!
750
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
×
751
            break;
×
752
          }
753
        } else {
754
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
10,906,626✔
755
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
2,374!
756
            break;
×
757
          }
758
        }
759

760
        uint32_t copyLen = TMIN(size - len - 1, charLen);
33,802,706✔
761
        (void)memcpy(str + len, row[i], copyLen);
33,802,706✔
762
        len += copyLen;
33,802,706✔
763
      } break;
33,802,706✔
764

765
      case TSDB_DATA_TYPE_TIMESTAMP:
41,424,590✔
766
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
41,424,590✔
767
        break;
41,431,460✔
768

769
      case TSDB_DATA_TYPE_BOOL:
200✔
770
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
200✔
771
        break;
200✔
772
      case TSDB_DATA_TYPE_DECIMAL64:
×
773
      case TSDB_DATA_TYPE_DECIMAL: {
774
        uint32_t decimalLen = strlen(row[i]);
×
775
        uint32_t copyLen = TMIN(size - len - 1, decimalLen);
×
776
        (void)memcpy(str + len, row[i], copyLen);
×
777
        len += copyLen;
×
778
      } break;
×
779
      default:
×
780
        break;
×
781
    }
782

783
    if (len >= size - 1) {
151,392,672!
784
      break;
×
785
    }
786
  }
787
  if (len < size) {
30,471,796✔
788
    str[len] = 0;
30,461,662✔
789
  }
790

791
  return len;
30,471,796✔
792
}
793

794
int *taos_fetch_lengths(TAOS_RES *res) {
60,062,560✔
795
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
60,062,560!
796
    return NULL;
×
797
  }
798

799
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
60,062,668✔
800
  return pResInfo->length;
60,062,668✔
801
}
802

803
TAOS_ROW *taos_result_block(TAOS_RES *res) {
×
804
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
805
    terrno = TSDB_CODE_INVALID_PARA;
×
806
    return NULL;
×
807
  }
808

809
  if (taos_is_update_query(res)) {
×
810
    return NULL;
×
811
  }
812

813
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
814
  return &pResInfo->row;
×
815
}
816

817
// todo intergrate with tDataTypes
818
const char *taos_data_type(int type) {
×
819
  switch (type) {
×
820
    case TSDB_DATA_TYPE_NULL:
×
821
      return "TSDB_DATA_TYPE_NULL";
×
822
    case TSDB_DATA_TYPE_BOOL:
×
823
      return "TSDB_DATA_TYPE_BOOL";
×
824
    case TSDB_DATA_TYPE_TINYINT:
×
825
      return "TSDB_DATA_TYPE_TINYINT";
×
826
    case TSDB_DATA_TYPE_SMALLINT:
×
827
      return "TSDB_DATA_TYPE_SMALLINT";
×
828
    case TSDB_DATA_TYPE_INT:
×
829
      return "TSDB_DATA_TYPE_INT";
×
830
    case TSDB_DATA_TYPE_BIGINT:
×
831
      return "TSDB_DATA_TYPE_BIGINT";
×
832
    case TSDB_DATA_TYPE_FLOAT:
×
833
      return "TSDB_DATA_TYPE_FLOAT";
×
834
    case TSDB_DATA_TYPE_DOUBLE:
×
835
      return "TSDB_DATA_TYPE_DOUBLE";
×
836
    case TSDB_DATA_TYPE_VARCHAR:
×
837
      return "TSDB_DATA_TYPE_VARCHAR";
×
838
      //    case TSDB_DATA_TYPE_BINARY:          return "TSDB_DATA_TYPE_VARCHAR";
839
    case TSDB_DATA_TYPE_TIMESTAMP:
×
840
      return "TSDB_DATA_TYPE_TIMESTAMP";
×
841
    case TSDB_DATA_TYPE_NCHAR:
×
842
      return "TSDB_DATA_TYPE_NCHAR";
×
843
    case TSDB_DATA_TYPE_JSON:
×
844
      return "TSDB_DATA_TYPE_JSON";
×
845
    case TSDB_DATA_TYPE_GEOMETRY:
×
846
      return "TSDB_DATA_TYPE_GEOMETRY";
×
847
    case TSDB_DATA_TYPE_UTINYINT:
×
848
      return "TSDB_DATA_TYPE_UTINYINT";
×
849
    case TSDB_DATA_TYPE_USMALLINT:
×
850
      return "TSDB_DATA_TYPE_USMALLINT";
×
851
    case TSDB_DATA_TYPE_UINT:
×
852
      return "TSDB_DATA_TYPE_UINT";
×
853
    case TSDB_DATA_TYPE_UBIGINT:
×
854
      return "TSDB_DATA_TYPE_UBIGINT";
×
855
    case TSDB_DATA_TYPE_VARBINARY:
×
856
      return "TSDB_DATA_TYPE_VARBINARY";
×
857
    case TSDB_DATA_TYPE_DECIMAL:
×
858
      return "TSDB_DATA_TYPE_DECIMAL";
×
859
    case TSDB_DATA_TYPE_BLOB:
×
860
      return "TSDB_DATA_TYPE_BLOB";
×
861
    case TSDB_DATA_TYPE_MEDIUMBLOB:
×
862
      return "TSDB_DATA_TYPE_MEDIUMBLOB";
×
863
    default:
×
864
      return "UNKNOWN";
×
865
  }
866
}
867

868
const char *taos_get_client_info() { return td_version; }
26,866✔
869

870
// return int32_t
871
int taos_affected_rows(TAOS_RES *res) {
2,977,028✔
872
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
2,977,028!
873
      TD_RES_TMQ_BATCH_META(res)) {
2,977,058!
874
    return 0;
×
875
  }
876

877
  SRequestObj    *pRequest = (SRequestObj *)res;
2,977,058✔
878
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
2,977,058✔
879
  return (int)pResInfo->numOfRows;
2,977,058✔
880
}
881

882
// return int64_t
883
int64_t taos_affected_rows64(TAOS_RES *res) {
244,830✔
884
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
244,830!
885
      TD_RES_TMQ_BATCH_META(res)) {
244,830!
886
    return 0;
×
887
  }
888

889
  SRequestObj    *pRequest = (SRequestObj *)res;
244,830✔
890
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
244,830✔
891
  return pResInfo->numOfRows;
244,830✔
892
}
893

894
int taos_result_precision(TAOS_RES *res) {
32,456,318✔
895
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
32,456,318!
896
    return TSDB_TIME_PRECISION_MILLI;
×
897
  }
898

899
  if (TD_RES_QUERY(res)) {
32,456,372✔
900
    SRequestObj *pRequest = (SRequestObj *)res;
1,994,420✔
901
    return pRequest->body.resInfo.precision;
1,994,420✔
902
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
30,461,952!
903
    SReqResultInfo *info = tmqGetCurResInfo(res);
30,461,952✔
904
    return info->precision;
30,461,952✔
905
  }
906
  return TSDB_TIME_PRECISION_MILLI;
×
907
}
908

909
int taos_select_db(TAOS *taos, const char *db) {
46✔
910
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
46✔
911
  if (pObj == NULL) {
46!
912
    releaseTscObj(*(int64_t *)taos);
×
913
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
914
    return TSDB_CODE_TSC_DISCONNECTED;
×
915
  }
916

917
  if (db == NULL || strlen(db) == 0) {
46!
918
    releaseTscObj(*(int64_t *)taos);
×
919
    tscError("invalid parameter for %s", db == NULL ? "db is NULL" : "db is empty");
×
920
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
921
    return terrno;
×
922
  }
923

924
  char sql[256] = {0};
46✔
925
  (void)snprintf(sql, tListLen(sql), "use %s", db);
46✔
926

927
  TAOS_RES *pRequest = taos_query(taos, sql);
46✔
928
  int32_t   code = taos_errno(pRequest);
46✔
929

930
  taos_free_result(pRequest);
46✔
931
  releaseTscObj(*(int64_t *)taos);
46✔
932
  return code;
46✔
933
}
934

935
void taos_stop_query(TAOS_RES *res) {
4,974,820✔
936
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
4,974,820!
937
      TD_RES_TMQ_BATCH_META(res)) {
4,974,856!
938
    return;
×
939
  }
940

941
  stopAllQueries((SRequestObj *)res);
4,974,862✔
942
}
943

944
bool taos_is_null(TAOS_RES *res, int32_t row, int32_t col) {
592,359,738✔
945
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
592,359,738!
946
    return true;
×
947
  }
948
  SReqResultInfo *pResultInfo = tscGetCurResInfo(res);
592,359,752✔
949
  if (col >= pResultInfo->numOfCols || col < 0 || row >= pResultInfo->numOfRows || row < 0) {
592,359,752!
950
    return true;
2✔
951
  }
952

953
  SResultColumn *pCol = &pResultInfo->pCol[col];
592,359,750✔
954
  if (IS_VAR_DATA_TYPE(pResultInfo->fields[col].type)) {
592,359,750!
955
    return (pCol->offset[row] == -1);
30✔
956
  } else {
957
    return colDataIsNull_f(pCol->nullbitmap, row);
592,359,720✔
958
  }
959
}
960

961
bool taos_is_update_query(TAOS_RES *res) { return taos_num_fields(res) == 0; }
×
962

963
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
2,494,830✔
964
  int32_t numOfRows = 0;
2,494,830✔
965
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
2,494,830✔
966
  return numOfRows;
2,494,828✔
967
}
968

969
int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) {
2,494,830✔
970
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,494,830!
971
    return 0;
×
972
  }
973

974
  if (TD_RES_QUERY(res)) {
2,494,830✔
975
    SRequestObj *pRequest = (SRequestObj *)res;
2,491,758✔
976

977
    (*rows) = NULL;
2,491,758✔
978
    (*numOfRows) = 0;
2,491,758✔
979

980
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
2,491,758!
981
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
2,486,770!
982
      return pRequest->code;
12,154✔
983
    }
984

985
    (void)doAsyncFetchRows(pRequest, false, true);
2,479,604✔
986

987
    // TODO refactor
988
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
2,479,604✔
989
    pResultInfo->current = pResultInfo->numOfRows;
2,479,604✔
990

991
    (*rows) = pResultInfo->row;
2,479,604✔
992
    (*numOfRows) = pResultInfo->numOfRows;
2,479,604✔
993
    return pRequest->code;
2,479,604✔
994
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
3,072!
995
    SReqResultInfo *pResultInfo = NULL;
3,072✔
996
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
3,072✔
997
    if (code != 0) return code;
3,072✔
998

999
    pResultInfo->current = pResultInfo->numOfRows;
3,020✔
1000
    (*rows) = pResultInfo->row;
3,020✔
1001
    (*numOfRows) = pResultInfo->numOfRows;
3,020✔
1002
    return 0;
3,020✔
1003
  } else {
1004
    tscError("taos_fetch_block_s invalid res type");
×
1005
    return TSDB_CODE_TMQ_INVALID_DATA;
×
1006
  }
1007
}
1008

1009
int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) {
16✔
1010
  *numOfRows = 0;
16✔
1011
  *pData = NULL;
16✔
1012

1013
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
16!
1014
    return 0;
×
1015
  }
1016

1017
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
16!
1018
    SReqResultInfo *pResultInfo = NULL;
×
1019
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
×
1020
    if (code != 0) {
×
1021
      (*numOfRows) = 0;
×
1022
      return 0;
×
1023
    }
1024

1025
    pResultInfo->current = pResultInfo->numOfRows;
×
1026
    (*numOfRows) = pResultInfo->numOfRows;
×
1027
    (*pData) = (void *)pResultInfo->pData;
×
1028
    return 0;
×
1029
  }
1030

1031
  SRequestObj *pRequest = (SRequestObj *)res;
16✔
1032

1033
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
16!
1034
      pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
16!
1035
    return pRequest->code;
×
1036
  }
1037

1038
  (void)doAsyncFetchRows(pRequest, false, false);
16✔
1039

1040
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
16✔
1041

1042
  pResultInfo->current = pResultInfo->numOfRows;
16✔
1043
  (*numOfRows) = pResultInfo->numOfRows;
16✔
1044
  (*pData) = (void *)pResultInfo->pData;
16✔
1045

1046
  return pRequest->code;
16✔
1047
}
1048

1049
int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) {
999,968✔
1050
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
999,968!
1051
    return 0;
×
1052
  }
1053

1054
  int32_t numOfFields = taos_num_fields(res);
999,968✔
1055
  if (columnIndex < 0 || columnIndex >= numOfFields || numOfFields == 0) {
999,968!
1056
    return 0;
×
1057
  }
1058

1059
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
999,968✔
1060
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
999,968✔
1061
  if (!IS_VAR_DATA_TYPE(pField->type)) {
999,968!
1062
    return 0;
×
1063
  }
1064

1065
  return pResInfo->pCol[columnIndex].offset;
999,968✔
1066
}
1067

1068
int taos_is_null_by_column(TAOS_RES *res, int columnIndex, bool result[], int *rows) {
×
1069
  if (res == NULL || result == NULL || rows == NULL || *rows <= 0 || columnIndex < 0 || TD_RES_TMQ_META(res) ||
×
1070
      TD_RES_TMQ_RAW(res) || TD_RES_TMQ_BATCH_META(res)) {
×
1071
    return TSDB_CODE_INVALID_PARA;
×
1072
  }
1073

1074
  int32_t numOfFields = taos_num_fields(res);
×
1075
  if (columnIndex >= numOfFields || numOfFields == 0) {
×
1076
    return TSDB_CODE_INVALID_PARA;
×
1077
  }
1078

1079
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
×
1080
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
×
1081
  SResultColumn  *pCol = &pResInfo->pCol[columnIndex];
×
1082

1083
  if (*rows > pResInfo->numOfRows) {
×
1084
    *rows = pResInfo->numOfRows;
×
1085
  }
1086
  if (IS_VAR_DATA_TYPE(pField->type)) {
×
1087
    for (int i = 0; i < *rows; i++) {
×
1088
      if (pCol->offset[i] == -1) {
×
1089
        result[i] = true;
×
1090
      } else {
1091
        result[i] = false;
×
1092
      }
1093
    }
1094
  } else {
1095
    for (int i = 0; i < *rows; i++) {
×
1096
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
×
1097
        result[i] = true;
×
1098
      } else {
1099
        result[i] = false;
×
1100
      }
1101
    }
1102
  }
1103
  return 0;
×
1104
}
1105

1106
int taos_validate_sql(TAOS *taos, const char *sql) {
×
1107
  TAOS_RES *pObj = taosQueryImpl(taos, sql, true, TD_REQ_FROM_APP);
×
1108

1109
  int code = taos_errno(pObj);
×
1110

1111
  taos_free_result(pObj);
×
1112
  return code;
×
1113
}
1114

1115
void taos_reset_current_db(TAOS *taos) {
×
1116
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
1117
  if (pTscObj == NULL) {
×
1118
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1119
    return;
×
1120
  }
1121

1122
  resetConnectDB(pTscObj);
×
1123

1124
  releaseTscObj(*(int64_t *)taos);
×
1125
}
1126

1127
const char *taos_get_server_info(TAOS *taos) {
×
1128
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
×
1129
  if (pTscObj == NULL) {
×
1130
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1131
    return NULL;
×
1132
  }
1133

1134
  releaseTscObj(*(int64_t *)taos);
×
1135

1136
  return pTscObj->sDetailVer;
×
1137
}
1138

1139
int taos_get_current_db(TAOS *taos, char *database, int len, int *required) {
8✔
1140
  STscObj *pTscObj = acquireTscObj(*(int64_t *)taos);
8✔
1141
  if (pTscObj == NULL) {
8!
1142
    return TSDB_CODE_TSC_DISCONNECTED;
×
1143
  }
1144

1145
  int code = TSDB_CODE_SUCCESS;
8✔
1146
  (void)taosThreadMutexLock(&pTscObj->mutex);
8✔
1147
  if (database == NULL || len <= 0) {
8!
1148
    if (required != NULL) *required = strlen(pTscObj->db) + 1;
4✔
1149
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
4!
1150
  } else if (len < strlen(pTscObj->db) + 1) {
4✔
1151
    tstrncpy(database, pTscObj->db, len);
2✔
1152
    if (required) *required = strlen(pTscObj->db) + 1;
2!
1153
    TSC_ERR_JRET(TSDB_CODE_INVALID_PARA);
2!
1154
  } else {
1155
    tstrncpy(database, pTscObj->db, len);
2✔
1156
    code = 0;
2✔
1157
  }
1158
_return:
8✔
1159
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
8✔
1160
  releaseTscObj(*(int64_t *)taos);
8✔
1161
  return code;
8✔
1162
}
1163

1164
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
9,872,416✔
1165
  if (NULL == pWrapper) {
9,872,416✔
1166
    return;
4,998,354✔
1167
  }
1168
  destoryCatalogReq(pWrapper->pCatalogReq);
4,874,062✔
1169
  taosMemoryFree(pWrapper->pCatalogReq);
4,874,094!
1170
  qDestroyParseContext(pWrapper->pParseCtx);
4,874,098✔
1171
  taosMemoryFree(pWrapper);
4,874,100!
1172
}
1173

1174
void destroyCtxInRequest(SRequestObj *pRequest) {
25,228✔
1175
  schedulerFreeJob(&pRequest->body.queryJob, 0);
25,228✔
1176
  qDestroyQuery(pRequest->pQuery);
25,228✔
1177
  pRequest->pQuery = NULL;
25,228✔
1178
  destorySqlCallbackWrapper(pRequest->pWrapper);
25,228✔
1179
  pRequest->pWrapper = NULL;
25,228✔
1180
}
25,228✔
1181

1182
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
1,843,964✔
1183
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
1,843,964✔
1184
  SRequestObj         *pRequest = pWrapper->pRequest;
1,843,964✔
1185
  SQuery              *pQuery = pRequest->pQuery;
1,843,964✔
1186

1187
  qDebug("req:0x%" PRIx64 ", start to semantic analysis, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
1,843,964✔
1188

1189
  int64_t analyseStart = taosGetTimestampUs();
1,843,964✔
1190
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
1,843,964✔
1191
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
1,843,964✔
1192

1193
  if (TSDB_CODE_SUCCESS == code) {
1,843,964✔
1194
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
1,843,942✔
1195
  }
1196

1197
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
1,843,952✔
1198

1199
  if (pRequest->parseOnly) {
1,843,952✔
1200
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
1,276✔
1201
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
1,276✔
1202
  }
1203

1204
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
1,843,952✔
1205
}
1,843,962✔
1206

1207
int32_t cloneCatalogReq(SCatalogReq **ppTarget, SCatalogReq *pSrc) {
996✔
1208
  int32_t      code = TSDB_CODE_SUCCESS;
996✔
1209
  SCatalogReq *pTarget = taosMemoryCalloc(1, sizeof(SCatalogReq));
996!
1210
  if (pTarget == NULL) {
996!
1211
    code = terrno;
×
1212
  } else {
1213
    pTarget->pDbVgroup = taosArrayDup(pSrc->pDbVgroup, NULL);
996✔
1214
    pTarget->pDbCfg = taosArrayDup(pSrc->pDbCfg, NULL);
996✔
1215
    pTarget->pDbInfo = taosArrayDup(pSrc->pDbInfo, NULL);
996✔
1216
    pTarget->pTableMeta = taosArrayDup(pSrc->pTableMeta, NULL);
996✔
1217
    pTarget->pTableHash = taosArrayDup(pSrc->pTableHash, NULL);
996✔
1218
    pTarget->pUdf = taosArrayDup(pSrc->pUdf, NULL);
996✔
1219
    pTarget->pIndex = taosArrayDup(pSrc->pIndex, NULL);
996✔
1220
    pTarget->pUser = taosArrayDup(pSrc->pUser, NULL);
996✔
1221
    pTarget->pTableIndex = taosArrayDup(pSrc->pTableIndex, NULL);
996✔
1222
    pTarget->pTableCfg = taosArrayDup(pSrc->pTableCfg, NULL);
996✔
1223
    pTarget->pTableTag = taosArrayDup(pSrc->pTableTag, NULL);
996✔
1224
    pTarget->pView = taosArrayDup(pSrc->pView, NULL);
996✔
1225
    pTarget->pTableTSMAs = taosArrayDup(pSrc->pTableTSMAs, NULL);
996✔
1226
    pTarget->pTSMAs = taosArrayDup(pSrc->pTSMAs, NULL);
996✔
1227
    pTarget->qNodeRequired = pSrc->qNodeRequired;
996✔
1228
    pTarget->dNodeRequired = pSrc->dNodeRequired;
996✔
1229
    pTarget->svrVerRequired = pSrc->svrVerRequired;
996✔
1230
    pTarget->forceUpdate = pSrc->forceUpdate;
996✔
1231
    pTarget->cloned = true;
996✔
1232

1233
    *ppTarget = pTarget;
996✔
1234
  }
1235

1236
  return code;
996✔
1237
}
1238

1239
void handleSubQueryFromAnalyse(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, SNode *pRoot) {
996✔
1240
  SRequestObj         *pNewRequest = NULL;
996✔
1241
  SSqlCallbackWrapper *pNewWrapper = NULL;
996✔
1242
  int32_t              code = buildPreviousRequest(pWrapper->pRequest, pWrapper->pRequest->sqlstr, &pNewRequest);
996✔
1243
  if (code) {
996!
1244
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1245
    return;
×
1246
  }
1247

1248
  pNewRequest->pQuery = NULL;
996✔
1249
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode **)&pNewRequest->pQuery);
996✔
1250
  if (pNewRequest->pQuery) {
996!
1251
    pNewRequest->pQuery->pRoot = pRoot;
996✔
1252
    pRoot = NULL;
996✔
1253
    pNewRequest->pQuery->execStage = QUERY_EXEC_STAGE_ANALYSE;
996✔
1254
  }
1255
  if (TSDB_CODE_SUCCESS == code) {
996!
1256
    code = prepareAndParseSqlSyntax(&pNewWrapper, pNewRequest, false);
996✔
1257
  }
1258
  if (TSDB_CODE_SUCCESS == code) {
996!
1259
    code = cloneCatalogReq(&pNewWrapper->pCatalogReq, pWrapper->pCatalogReq);
996✔
1260
  }
1261
  if (TSDB_CODE_SUCCESS == code) {
996!
1262
    doAsyncQueryFromAnalyse(pResultMeta, pNewWrapper, code);
996✔
1263
    nodesDestroyNode(pRoot);
996✔
1264
  } else {
1265
    handleQueryAnslyseRes(pWrapper, pResultMeta, code);
×
1266
    return;
×
1267
  }
1268
}
1269

1270
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
1,844,894✔
1271
  SRequestObj *pRequest = pWrapper->pRequest;
1,844,894✔
1272
  SQuery      *pQuery = pRequest->pQuery;
1,844,894✔
1273

1274
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
1,844,894✔
1275
    SNode *prevRoot = pQuery->pPrevRoot;
996✔
1276
    pQuery->pPrevRoot = NULL;
996✔
1277
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
996✔
1278
    return;
996✔
1279
  }
1280

1281
  if (code == TSDB_CODE_SUCCESS) {
1,843,898✔
1282
    pRequest->stableQuery = pQuery->stableQuery;
1,709,012✔
1283
    if (pQuery->pRoot) {
1,709,012!
1284
      pRequest->stmtType = pQuery->pRoot->type;
1,709,016✔
1285
    }
1286

1287
    if (pQuery->haveResultSet) {
1,709,012✔
1288
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema, pRequest->isStmtBind);
1,513,202✔
1289
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
1,513,200✔
1290
    }
1291
  }
1292

1293
  if (code == TSDB_CODE_SUCCESS) {
1,843,902✔
1294
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
1,709,004✔
1295
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
1,709,004✔
1296
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
1,709,004✔
1297

1298
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
1,709,004✔
1299
  } else {
1300
    destorySqlCallbackWrapper(pWrapper);
134,898✔
1301
    pRequest->pWrapper = NULL;
134,898✔
1302
    qDestroyQuery(pRequest->pQuery);
134,898✔
1303
    pRequest->pQuery = NULL;
134,898✔
1304

1305
    if (NEED_CLIENT_HANDLE_ERROR(code)) {
134,898!
1306
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
24,802✔
1307
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1308
      restartAsyncQuery(pRequest, code);
24,802✔
1309
      return;
24,802✔
1310
    }
1311

1312
    // return to app directly
1313
    tscError("req:0x%" PRIx64 ", error occurs, code:%s, return to user app, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
110,096!
1314
             pRequest->requestId);
1315
    pRequest->code = code;
110,096✔
1316
    returnToUser(pRequest);
110,096✔
1317
  }
1318
}
1319

1320
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
1,897,324✔
1321
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
1,897,324✔
1322
                           .requestId = pWrapper->pParseCtx->requestId,
1,897,324✔
1323
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
1,897,324✔
1324
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
1,897,324✔
1325

1326
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
1,897,324✔
1327

1328
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
3,794,664✔
1329
                                &pWrapper->pRequest->body.queryJob);
1,897,334✔
1330
}
1331

1332
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code);
1333

1334
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
4,879,040✔
1335
  int32_t code = TSDB_CODE_SUCCESS;
4,879,040✔
1336
  switch (pWrapper->pRequest->pQuery->execStage) {
4,879,040!
1337
    case QUERY_EXEC_STAGE_PARSE: {
54,364✔
1338
      // continue parse after get metadata
1339
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
54,364✔
1340
      break;
54,364✔
1341
    }
1342
    case QUERY_EXEC_STAGE_ANALYSE: {
1,842,964✔
1343
      // analysis after get metadata
1344
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
1,842,964✔
1345
      break;
1,842,968✔
1346
    }
1347
    case QUERY_EXEC_STAGE_SCHEDULE: {
2,981,730✔
1348
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
2,981,730✔
1349
      break;
2,981,734✔
1350
    }
1351
    default:
×
1352
      break;
×
1353
  }
1354
  return code;
4,879,048✔
1355
}
1356

1357
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code) {
54,366✔
1358
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
54,366✔
1359
  SRequestObj         *pRequest = pWrapper->pRequest;
54,366✔
1360
  SQuery              *pQuery = pRequest->pQuery;
54,366✔
1361

1362
  pRequest->metric.ctgCostUs += taosGetTimestampUs() - pRequest->metric.ctgStart;
54,366✔
1363
  qDebug("req:0x%" PRIx64 ", continue parse query, QID:0x%" PRIx64 ", code:%s", pRequest->self, pRequest->requestId,
54,366✔
1364
         tstrerror(code));
1365

1366
  if (code == TSDB_CODE_SUCCESS) {
54,366✔
1367
    // pWrapper->pCatalogReq->forceUpdate = false;
1368
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
54,188✔
1369
  }
1370

1371
  if (TSDB_CODE_SUCCESS == code) {
54,366✔
1372
    code = phaseAsyncQuery(pWrapper);
52,534✔
1373
  }
1374

1375
  if (TSDB_CODE_SUCCESS != code) {
54,366✔
1376
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
1,832!
1377
             tstrerror(code), pWrapper->pRequest->requestId);
1378
    destorySqlCallbackWrapper(pWrapper);
1,832✔
1379
    pRequest->pWrapper = NULL;
1,832✔
1380
    terrno = code;
1,832✔
1381
    pRequest->code = code;
1,832✔
1382
    doRequestCallback(pRequest, code);
1,832✔
1383
  }
1384
}
54,366✔
1385

1386
void continueInsertFromCsv(SSqlCallbackWrapper *pWrapper, SRequestObj *pRequest) {
4✔
1387
  int32_t code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
4✔
1388
  if (TSDB_CODE_SUCCESS == code) {
4!
1389
    code = phaseAsyncQuery(pWrapper);
4✔
1390
  }
1391

1392
  if (TSDB_CODE_SUCCESS != code) {
4!
1393
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
×
1394
             tstrerror(code), pWrapper->pRequest->requestId);
1395
    destorySqlCallbackWrapper(pWrapper);
×
1396
    pRequest->pWrapper = NULL;
×
1397
    terrno = code;
×
1398
    pRequest->code = code;
×
1399
    doRequestCallback(pRequest, code);
×
1400
  }
1401
}
4✔
1402

1403
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
1,044✔
1404
  int64_t connId = *(int64_t *)taos;
1,044✔
1405
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
1,044✔
1406
}
1,044✔
1407

1408
void taos_query_a_with_reqid(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param, int64_t reqid) {
×
1409
  int64_t connId = *(int64_t *)taos;
×
1410
  taosAsyncQueryImplWithReqid(connId, sql, fp, param, false, reqid);
×
1411
}
×
1412

1413
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
4,874,056✔
1414
  const STscObj *pTscObj = pRequest->pTscObj;
4,874,056✔
1415

1416
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
4,874,056!
1417
  if (*pCxt == NULL) {
4,874,068!
1418
    return terrno;
×
1419
  }
1420

1421
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
4,874,068✔
1422
                           .requestRid = pRequest->self,
4,874,068✔
1423
                           .acctId = pTscObj->acctId,
4,874,068✔
1424
                           .db = pRequest->pDb,
4,874,068✔
1425
                           .topicQuery = false,
1426
                           .pSql = pRequest->sqlstr,
4,874,068✔
1427
                           .sqlLen = pRequest->sqlLen,
4,874,068✔
1428
                           .pMsg = pRequest->msgBuf,
4,874,068✔
1429
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1430
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
4,874,068✔
1431
                           .pStmtCb = NULL,
1432
                           .pUser = pTscObj->user,
4,874,068✔
1433
                           .pEffectiveUser = pRequest->effectiveUser,
4,874,068✔
1434
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
4,874,068✔
1435
                           .enableSysInfo = pTscObj->sysInfo,
4,874,068✔
1436
                           .async = true,
1437
                           .svrVer = pTscObj->sVer,
4,874,068✔
1438
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
4,874,068✔
1439
                           .allocatorId = pRequest->allocatorRefId,
4,874,068✔
1440
                           .parseSqlFp = clientParseSql,
1441
                           .parseSqlParam = pWrapper,
1442
                           .setQueryFp = setQueryRequest,
1443
                           .timezone = pTscObj->optionInfo.timezone,
4,874,068✔
1444
                           .charsetCxt = pTscObj->optionInfo.charsetCxt,
4,874,068✔
1445
                           .streamRunHistory = pRequest->streamRunHistory};
4,874,068✔
1446
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
4,874,068✔
1447
  (*pCxt)->biMode = biMode;
4,874,060✔
1448
  return TSDB_CODE_SUCCESS;
4,874,060✔
1449
}
1450

1451
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
4,874,048✔
1452
  int32_t              code = TSDB_CODE_SUCCESS;
4,874,048✔
1453
  STscObj             *pTscObj = pRequest->pTscObj;
4,874,048✔
1454
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
4,874,048!
1455
  if (pWrapper == NULL) {
4,874,076!
1456
    code = terrno;
×
1457
  } else {
1458
    pWrapper->pRequest = pRequest;
4,874,076✔
1459
    pRequest->pWrapper = pWrapper;
4,874,076✔
1460
    *ppWrapper = pWrapper;
4,874,076✔
1461
  }
1462

1463
  if (TSDB_CODE_SUCCESS == code) {
4,874,076✔
1464
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
4,874,074✔
1465
  }
1466

1467
  if (TSDB_CODE_SUCCESS == code) {
4,874,068!
1468
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
4,874,076✔
1469
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
4,874,082✔
1470
  }
1471

1472
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
4,874,064✔
1473
    int64_t syntaxStart = taosGetTimestampUs();
4,873,068✔
1474

1475
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
4,873,068!
1476
    if (pWrapper->pCatalogReq == NULL) {
4,873,078!
1477
      code = terrno;
×
1478
    } else {
1479
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
4,873,078✔
1480
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
4,873,078!
1481
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
4,873,082✔
1482
    }
1483

1484
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
4,873,042✔
1485
  }
1486

1487
  return code;
4,874,052✔
1488
}
1489

1490
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
4,881,322✔
1491
  SSqlCallbackWrapper *pWrapper = NULL;
4,881,322✔
1492
  int32_t              code = TSDB_CODE_SUCCESS;
4,881,322✔
1493

1494
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
4,881,322✔
1495
    code = pRequest->prevCode;
8,268✔
1496
    terrno = code;
8,268✔
1497
    pRequest->code = code;
8,268✔
1498
    tscDebug("req:0x%" PRIx64 ", call sync query cb with code:%s", pRequest->self, tstrerror(code));
8,268✔
1499
    doRequestCallback(pRequest, code);
8,268✔
1500
    return;
8,290✔
1501
  }
1502

1503
  if (TSDB_CODE_SUCCESS == code) {
4,873,054!
1504
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
4,873,066✔
1505
  }
1506

1507
  if (TSDB_CODE_SUCCESS == code) {
4,873,048✔
1508
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
4,826,522✔
1509
    code = phaseAsyncQuery(pWrapper);
4,826,522✔
1510
  }
1511

1512
  if (TSDB_CODE_SUCCESS != code) {
4,873,056✔
1513
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
46,544!
1514
             pRequest->requestId);
1515
    destorySqlCallbackWrapper(pWrapper);
46,544✔
1516
    pRequest->pWrapper = NULL;
46,544✔
1517
    qDestroyQuery(pRequest->pQuery);
46,544✔
1518
    pRequest->pQuery = NULL;
46,544✔
1519

1520
    if (NEED_CLIENT_HANDLE_ERROR(code)) {
46,544!
1521
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
22!
1522
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1523
      code = refreshMeta(pRequest->pTscObj, pRequest);
22✔
1524
      if (code != 0) {
22!
1525
        tscWarn("req:0x%" PRIx64 ", refresh meta failed, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
22!
1526
                pRequest->requestId);
1527
      }
1528
      pRequest->prevCode = code;
22✔
1529
      doAsyncQuery(pRequest, true);
22✔
1530
      return;
22✔
1531
    }
1532

1533
    terrno = code;
46,522✔
1534
    pRequest->code = code;
46,522✔
1535
    doRequestCallback(pRequest, code);
46,522✔
1536
  }
1537
}
1538

1539
void restartAsyncQuery(SRequestObj *pRequest, int32_t code) {
25,228✔
1540
  tscInfo("restart request:%s p:%p", pRequest->sqlstr, pRequest);
25,228!
1541
  SRequestObj *pUserReq = pRequest;
25,228✔
1542
  (void)acquireRequest(pRequest->self);
25,228✔
1543
  while (pUserReq) {
25,274!
1544
    if (pUserReq->self == pUserReq->relation.userRefId || pUserReq->relation.userRefId == 0) {
25,274!
1545
      break;
1546
    } else {
1547
      int64_t nextRefId = pUserReq->relation.nextRefId;
46✔
1548
      (void)releaseRequest(pUserReq->self);
46✔
1549
      if (nextRefId) {
46!
1550
        pUserReq = acquireRequest(nextRefId);
46✔
1551
      }
1552
    }
1553
  }
1554
  bool hasSubRequest = pUserReq != pRequest || pRequest->relation.prevRefId != 0;
25,228!
1555
  if (pUserReq) {
25,228!
1556
    destroyCtxInRequest(pUserReq);
25,228✔
1557
    pUserReq->prevCode = code;
25,228✔
1558
    (void)memset(&pUserReq->relation, 0, sizeof(pUserReq->relation));
25,228✔
1559
  } else {
1560
    tscError("User req is missing");
×
1561
    (void)removeFromMostPrevReq(pRequest);
×
1562
    return;
×
1563
  }
1564
  if (hasSubRequest)
25,228✔
1565
    (void)removeFromMostPrevReq(pRequest);
46✔
1566
  else
1567
    (void)releaseRequest(pUserReq->self);
25,182✔
1568
  doAsyncQuery(pUserReq, true);
25,228✔
1569
}
1570

1571
typedef struct SAsyncFetchParam {
1572
  SRequestObj      *pReq;
1573
  __taos_async_fn_t fp;
1574
  void             *param;
1575
} SAsyncFetchParam;
1576

1577
static int32_t doAsyncFetch(void *pParam) {
1,566,672✔
1578
  SAsyncFetchParam *param = pParam;
1,566,672✔
1579
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
1,566,672✔
1580
  taosMemoryFree(param);
1,566,670!
1581
  return TSDB_CODE_SUCCESS;
1,566,672✔
1582
}
1583

1584
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
1,566,682✔
1585
  if (res == NULL || fp == NULL) {
1,566,682!
1586
    tscError("taos_fetch_rows_a invalid paras");
×
1587
    return;
×
1588
  }
1589
  if (!TD_RES_QUERY(res)) {
1,566,682!
1590
    tscError("taos_fetch_rows_a res is NULL");
×
1591
    fp(param, res, TSDB_CODE_APP_ERROR);
×
1592
    return;
×
1593
  }
1594

1595
  SRequestObj *pRequest = res;
1,566,682✔
1596
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
1,566,682✔
1597
    fp(param, res, 0);
10✔
1598
    return;
10✔
1599
  }
1600

1601
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
1,566,672!
1602
  if (!pParam) {
1,566,672!
1603
    fp(param, res, terrno);
×
1604
    return;
×
1605
  }
1606
  pParam->pReq = pRequest;
1,566,672✔
1607
  pParam->fp = fp;
1,566,672✔
1608
  pParam->param = param;
1,566,672✔
1609
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
1,566,672✔
1610
  if (TSDB_CODE_SUCCESS != code) {
1,566,672!
1611
    taosMemoryFree(pParam);
×
1612
    fp(param, res, code);
×
1613
    return;
×
1614
  }
1615
}
1616

1617
void taos_fetch_raw_block_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
8✔
1618
  if (res == NULL || fp == NULL) {
8!
1619
    tscError("taos_fetch_raw_block_a invalid paras");
×
1620
    return;
×
1621
  }
1622
  if (!TD_RES_QUERY(res)) {
8!
1623
    tscError("taos_fetch_raw_block_a res is NULL");
×
1624
    return;
×
1625
  }
1626
  SRequestObj    *pRequest = res;
8✔
1627
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
8✔
1628

1629
  // set the current block is all consumed
1630
  pResultInfo->convertUcs4 = false;
8✔
1631

1632
  // it is a local executed query, no need to do async fetch
1633
  taos_fetch_rows_a(pRequest, fp, param);
8✔
1634
}
1635

1636
const void *taos_get_raw_block(TAOS_RES *res) {
×
1637
  if (res == NULL) {
×
1638
    tscError("taos_get_raw_block invalid paras");
×
1639
    return NULL;
×
1640
  }
1641
  if (!TD_RES_QUERY(res)) {
×
1642
    tscError("taos_get_raw_block res is NULL");
×
1643
    return NULL;
×
1644
  }
1645
  SRequestObj *pRequest = res;
×
1646

1647
  return pRequest->body.resInfo.pData;
×
1648
}
1649

1650
int taos_get_db_route_info(TAOS *taos, const char *db, TAOS_DB_ROUTE_INFO *dbInfo) {
4✔
1651
  if (NULL == taos) {
4!
1652
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1653
    return terrno;
×
1654
  }
1655

1656
  if (NULL == db || NULL == dbInfo) {
4!
1657
    tscError("invalid input param, db:%p, dbInfo:%p", db, dbInfo);
×
1658
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1659
    return terrno;
×
1660
  }
1661

1662
  int64_t      connId = *(int64_t *)taos;
4✔
1663
  SRequestObj *pRequest = NULL;
4✔
1664
  char        *sql = "taos_get_db_route_info";
4✔
1665
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
4✔
1666
  if (code != TSDB_CODE_SUCCESS) {
4!
1667
    terrno = code;
×
1668
    return terrno;
×
1669
  }
1670

1671
  STscObj  *pTscObj = pRequest->pTscObj;
4✔
1672
  SCatalog *pCtg = NULL;
4✔
1673
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
4✔
1674
  if (code != TSDB_CODE_SUCCESS) {
4!
1675
    goto _return;
×
1676
  }
1677

1678
  SRequestConnInfo conn = {
4✔
1679
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
4✔
1680

1681
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
4✔
1682

1683
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
4✔
1684
  (void)snprintf(dbFName, sizeof(dbFName), "%d.%s", pTscObj->acctId, db);
4✔
1685

1686
  code = catalogGetDBVgInfo(pCtg, &conn, dbFName, dbInfo);
4✔
1687
  if (code) {
4!
1688
    goto _return;
×
1689
  }
1690

1691
_return:
4✔
1692

1693
  terrno = code;
4✔
1694

1695
  destroyRequest(pRequest);
4✔
1696
  return code;
4✔
1697
}
1698

1699
int taos_get_table_vgId(TAOS *taos, const char *db, const char *table, int *vgId) {
400✔
1700
  if (NULL == taos) {
400!
1701
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1702
    return terrno;
×
1703
  }
1704

1705
  if (NULL == db || NULL == table || NULL == vgId) {
400!
1706
    tscError("invalid input param, db:%p, table:%p, vgId:%p", db, table, vgId);
×
1707
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1708
    return terrno;
×
1709
  }
1710

1711
  int64_t      connId = *(int64_t *)taos;
400✔
1712
  SRequestObj *pRequest = NULL;
400✔
1713
  char        *sql = "taos_get_table_vgId";
400✔
1714
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
400✔
1715
  if (code != TSDB_CODE_SUCCESS) {
400!
1716
    return terrno;
×
1717
  }
1718

1719
  pRequest->syncQuery = true;
400✔
1720

1721
  STscObj  *pTscObj = pRequest->pTscObj;
400✔
1722
  SCatalog *pCtg = NULL;
400✔
1723
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
400✔
1724
  if (code != TSDB_CODE_SUCCESS) {
400!
1725
    goto _return;
×
1726
  }
1727

1728
  SRequestConnInfo conn = {
400✔
1729
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
400✔
1730

1731
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
400✔
1732

1733
  SName tableName = {0};
400✔
1734
  toName(pTscObj->acctId, db, table, &tableName);
400✔
1735

1736
  SVgroupInfo vgInfo;
1737
  code = catalogGetTableHashVgroup(pCtg, &conn, &tableName, &vgInfo);
400✔
1738
  if (code) {
400!
1739
    goto _return;
×
1740
  }
1741

1742
  *vgId = vgInfo.vgId;
400✔
1743

1744
_return:
400✔
1745

1746
  terrno = code;
400✔
1747

1748
  destroyRequest(pRequest);
400✔
1749
  return code;
400✔
1750
}
1751

1752
int taos_get_tables_vgId(TAOS *taos, const char *db, const char *table[], int tableNum, int *vgId) {
2✔
1753
  if (NULL == taos) {
2!
1754
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1755
    return terrno;
×
1756
  }
1757

1758
  if (NULL == db || NULL == table || NULL == vgId || tableNum <= 0) {
2!
1759
    tscError("invalid input param, db:%p, table:%p, vgId:%p, tbNum:%d", db, table, vgId, tableNum);
×
1760
    terrno = TSDB_CODE_TSC_INVALID_INPUT;
×
1761
    return terrno;
×
1762
  }
1763

1764
  int64_t      connId = *(int64_t *)taos;
2✔
1765
  SRequestObj *pRequest = NULL;
2✔
1766
  char        *sql = "taos_get_table_vgId";
2✔
1767
  int32_t      code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
2✔
1768
  if (code != TSDB_CODE_SUCCESS) {
2!
1769
    return terrno;
×
1770
  }
1771

1772
  pRequest->syncQuery = true;
2✔
1773

1774
  STscObj  *pTscObj = pRequest->pTscObj;
2✔
1775
  SCatalog *pCtg = NULL;
2✔
1776
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
2✔
1777
  if (code != TSDB_CODE_SUCCESS) {
2!
1778
    goto _return;
×
1779
  }
1780

1781
  SRequestConnInfo conn = {
2✔
1782
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
2✔
1783

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

1786
  code = catalogGetTablesHashVgId(pCtg, &conn, pTscObj->acctId, db, table, tableNum, vgId);
2✔
1787
  if (code) {
2!
1788
    goto _return;
×
1789
  }
1790

1791
_return:
2✔
1792

1793
  terrno = code;
2✔
1794

1795
  destroyRequest(pRequest);
2✔
1796
  return code;
2✔
1797
}
1798

1799
int taos_load_table_info(TAOS *taos, const char *tableNameList) {
×
1800
  if (NULL == taos) {
×
1801
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1802
    return terrno;
×
1803
  }
1804

1805
  int64_t       connId = *(int64_t *)taos;
×
1806
  const int32_t MAX_TABLE_NAME_LENGTH = 12 * 1024 * 1024;  // 12MB list
×
1807
  int32_t       code = 0;
×
1808
  SRequestObj  *pRequest = NULL;
×
1809
  SCatalogReq   catalogReq = {0};
×
1810

1811
  if (NULL == tableNameList) {
×
1812
    return TSDB_CODE_SUCCESS;
×
1813
  }
1814

1815
  int32_t length = (int32_t)strlen(tableNameList);
×
1816
  if (0 == length) {
×
1817
    return TSDB_CODE_SUCCESS;
×
1818
  } else if (length > MAX_TABLE_NAME_LENGTH) {
×
1819
    tscError("tableNameList too long, length:%d, maximum allowed:%d", length, MAX_TABLE_NAME_LENGTH);
×
1820
    return TSDB_CODE_TSC_INVALID_OPERATION;
×
1821
  }
1822

1823
  char *sql = "taos_load_table_info";
×
1824
  code = buildRequest(connId, sql, strlen(sql), NULL, false, &pRequest, 0);
×
1825
  if (code != TSDB_CODE_SUCCESS) {
×
1826
    terrno = code;
×
1827
    goto _return;
×
1828
  }
1829

1830
  pRequest->syncQuery = true;
×
1831

1832
  STscObj *pTscObj = pRequest->pTscObj;
×
1833
  code = transferTableNameList(tableNameList, pTscObj->acctId, pTscObj->db, &catalogReq.pTableMeta);
×
1834
  if (code) {
×
1835
    goto _return;
×
1836
  }
1837

1838
  SCatalog *pCtg = NULL;
×
1839
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCtg);
×
1840
  if (code != TSDB_CODE_SUCCESS) {
×
1841
    goto _return;
×
1842
  }
1843

1844
  SRequestConnInfo conn = {
×
1845
      .pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self};
×
1846

1847
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
×
1848

1849
  code = catalogAsyncGetAllMeta(pCtg, &conn, &catalogReq, syncCatalogFn, pRequest->body.interParam, NULL);
×
1850
  if (code) {
×
1851
    goto _return;
×
1852
  }
1853

1854
  SSyncQueryParam *pParam = pRequest->body.interParam;
×
1855
  code = tsem_wait(&pParam->sem);
×
1856
  if (code) {
×
1857
    tscError("tsem wait failed, code:%d - %s", code, tstrerror(code));
×
1858
    goto _return;
×
1859
  }
1860
_return:
×
1861
  destoryCatalogReq(&catalogReq);
×
1862
  destroyRequest(pRequest);
×
1863
  return code;
×
1864
}
1865

1866
TAOS_STMT *taos_stmt_init(TAOS *taos) {
34✔
1867
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
34✔
1868
  if (NULL == pObj) {
34!
1869
    tscError("invalid parameter for %s", __FUNCTION__);
×
1870
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1871
    return NULL;
×
1872
  }
1873

1874
  TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL);
34✔
1875
  if (NULL == pStmt) {
34!
1876
    tscError("stmt init failed, errcode:%s", terrstr());
×
1877
  }
1878
  releaseTscObj(*(int64_t *)taos);
34✔
1879

1880
  return pStmt;
34✔
1881
}
1882

1883
TAOS_STMT *taos_stmt_init_with_reqid(TAOS *taos, int64_t reqid) {
×
1884
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
×
1885
  if (NULL == pObj) {
×
1886
    tscError("invalid parameter for %s", __FUNCTION__);
×
1887
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1888
    return NULL;
×
1889
  }
1890

1891
  TAOS_STMT *pStmt = stmtInit(pObj, reqid, NULL);
×
1892
  if (NULL == pStmt) {
×
1893
    tscError("stmt init failed, errcode:%s", terrstr());
×
1894
  }
1895
  releaseTscObj(*(int64_t *)taos);
×
1896

1897
  return pStmt;
×
1898
}
1899

1900
TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options) {
42✔
1901
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
42✔
1902
  if (NULL == pObj) {
42!
1903
    tscError("invalid parameter for %s", __FUNCTION__);
×
1904
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
1905
    return NULL;
×
1906
  }
1907

1908
  TAOS_STMT *pStmt = stmtInit(pObj, options->reqId, options);
42✔
1909
  if (NULL == pStmt) {
42!
1910
    tscError("stmt init failed, errcode:%s", terrstr());
×
1911
  }
1912
  releaseTscObj(*(int64_t *)taos);
42✔
1913

1914
  return pStmt;
42✔
1915
}
1916

1917
int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
39,988✔
1918
  if (stmt == NULL || sql == NULL) {
39,988!
1919
    tscError("NULL parameter for %s", __FUNCTION__);
×
1920
    terrno = TSDB_CODE_INVALID_PARA;
×
1921
    return terrno;
×
1922
  }
1923

1924
  return stmtPrepare(stmt, sql, length);
40,022✔
1925
}
1926

1927
int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags) {
18✔
1928
  if (stmt == NULL || name == NULL) {
18!
1929
    tscError("NULL parameter for %s", __FUNCTION__);
×
1930
    terrno = TSDB_CODE_INVALID_PARA;
×
1931
    return terrno;
×
1932
  }
1933

1934
  int32_t code = stmtSetTbName(stmt, name);
18✔
1935
  if (code) {
18!
1936
    return code;
×
1937
  }
1938

1939
  if (tags) {
18!
1940
    return stmtSetTbTags(stmt, tags);
18✔
1941
  }
1942

1943
  return TSDB_CODE_SUCCESS;
×
1944
}
1945

1946
int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name) {
80,006✔
1947
  if (stmt == NULL || name == NULL) {
80,006!
1948
    tscError("NULL parameter for %s", __FUNCTION__);
×
1949
    terrno = TSDB_CODE_INVALID_PARA;
×
1950
    return terrno;
×
1951
  }
1952

1953
  return stmtSetTbName(stmt, name);
80,054✔
1954
}
1955

1956
int taos_stmt_set_tags(TAOS_STMT *stmt, TAOS_MULTI_BIND *tags) {
8✔
1957
  if (stmt == NULL || tags == NULL) {
8!
1958
    tscError("NULL parameter for %s", __FUNCTION__);
×
1959
    terrno = TSDB_CODE_INVALID_PARA;
×
1960
    return terrno;
×
1961
  }
1962

1963
  return stmtSetTbTags(stmt, tags);
8✔
1964
}
1965

1966
int taos_stmt_set_sub_tbname(TAOS_STMT *stmt, const char *name) { return taos_stmt_set_tbname(stmt, name); }
4✔
1967

1968
int taos_stmt_get_tag_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
8✔
1969
  if (stmt == NULL || NULL == fieldNum) {
8!
1970
    tscError("NULL parameter for %s", __FUNCTION__);
×
1971
    terrno = TSDB_CODE_INVALID_PARA;
×
1972
    return terrno;
×
1973
  }
1974

1975
  return stmtGetTagFields(stmt, fieldNum, fields);
8✔
1976
}
1977

1978
int taos_stmt_get_col_fields(TAOS_STMT *stmt, int *fieldNum, TAOS_FIELD_E **fields) {
304✔
1979
  if (stmt == NULL || NULL == fieldNum) {
304!
1980
    tscError("NULL parameter for %s", __FUNCTION__);
×
1981
    terrno = TSDB_CODE_INVALID_PARA;
×
1982
    return terrno;
×
1983
  }
1984

1985
  return stmtGetColFields(stmt, fieldNum, fields);
304✔
1986
}
1987

1988
// let stmt to reclaim TAOS_FIELD_E that was allocated by `taos_stmt_get_tag_fields`/`taos_stmt_get_col_fields`
1989
void taos_stmt_reclaim_fields(TAOS_STMT *stmt, TAOS_FIELD_E *fields) {
×
1990
  (void)stmt;
1991
  if (!fields) return;
×
1992
  taosMemoryFree(fields);
×
1993
}
1994

1995
int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
174✔
1996
  if (stmt == NULL || bind == NULL) {
174!
1997
    tscError("NULL parameter for %s", __FUNCTION__);
×
1998
    terrno = TSDB_CODE_INVALID_PARA;
×
1999
    return terrno;
×
2000
  }
2001

2002
  if (bind->num > 1) {
174!
2003
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
×
2004
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2005
    return terrno;
×
2006
  }
2007

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

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

2018
  if (bind->num <= 0 || bind->num > INT16_MAX) {
80,154✔
2019
    tscError("invalid bind num %d", bind->num);
4!
2020
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
4✔
2021
    return terrno;
×
2022
  }
2023

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

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

2046
  if (colIdx < 0) {
900!
2047
    tscError("invalid bind column idx %d", colIdx);
×
2048
    terrno = TSDB_CODE_INVALID_PARA;
×
2049
    return terrno;
×
2050
  }
2051

2052
  int32_t insert = 0;
900✔
2053
  int32_t code = stmtIsInsert(stmt, &insert);
900✔
2054
  if (TSDB_CODE_SUCCESS != code) {
900!
2055
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2056
    return code;
×
2057
  }
2058
  if (0 == insert && bind->num > 1) {
900!
2059
    tscError("only one row data allowed for query");
×
2060
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2061
    return terrno;
×
2062
  }
2063

2064
  return stmtBindBatch(stmt, bind, colIdx);
900✔
2065
}
2066

2067
int taos_stmt_add_batch(TAOS_STMT *stmt) {
80,338✔
2068
  if (stmt == NULL) {
80,338!
2069
    tscError("NULL parameter for %s", __FUNCTION__);
×
2070
    terrno = TSDB_CODE_INVALID_PARA;
×
2071
    return terrno;
×
2072
  }
2073

2074
  return stmtAddBatch(stmt);
80,338✔
2075
}
2076

2077
int taos_stmt_execute(TAOS_STMT *stmt) {
80,028✔
2078
  if (stmt == NULL) {
80,028!
2079
    tscError("NULL parameter for %s", __FUNCTION__);
×
2080
    terrno = TSDB_CODE_INVALID_PARA;
×
2081
    return terrno;
×
2082
  }
2083

2084
  return stmtExec(stmt);
80,028✔
2085
}
2086

2087
int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
12✔
2088
  if (stmt == NULL || insert == NULL) {
12!
2089
    tscError("NULL parameter for %s", __FUNCTION__);
×
2090
    terrno = TSDB_CODE_INVALID_PARA;
×
2091
    return terrno;
×
2092
  }
2093

2094
  return stmtIsInsert(stmt, insert);
12✔
2095
}
2096

2097
int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) {
×
2098
  if (stmt == NULL || nums == NULL) {
×
2099
    tscError("NULL parameter for %s", __FUNCTION__);
×
2100
    terrno = TSDB_CODE_INVALID_PARA;
×
2101
    return terrno;
×
2102
  }
2103

2104
  return stmtGetParamNum(stmt, nums);
×
2105
}
2106

2107
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
1,812✔
2108
  if (stmt == NULL || type == NULL || NULL == bytes || idx < 0) {
1,812!
2109
    tscError("invalid parameter for %s", __FUNCTION__);
×
2110
    terrno = TSDB_CODE_INVALID_PARA;
×
2111
    return terrno;
×
2112
  }
2113

2114
  return stmtGetParam(stmt, idx, type, bytes);
1,812✔
2115
}
2116

2117
TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt) {
2✔
2118
  if (stmt == NULL) {
2!
2119
    tscError("NULL parameter for %s", __FUNCTION__);
×
2120
    terrno = TSDB_CODE_INVALID_PARA;
×
2121
    return NULL;
×
2122
  }
2123

2124
  return stmtUseResult(stmt);
2✔
2125
}
2126

2127
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
2✔
2128

2129
int taos_stmt_affected_rows(TAOS_STMT *stmt) {
10✔
2130
  if (stmt == NULL) {
10!
2131
    tscError("NULL parameter for %s", __FUNCTION__);
×
2132
    terrno = TSDB_CODE_INVALID_PARA;
×
2133
    return 0;
×
2134
  }
2135

2136
  return stmtAffectedRows(stmt);
10✔
2137
}
2138

2139
int taos_stmt_affected_rows_once(TAOS_STMT *stmt) {
56✔
2140
  if (stmt == NULL) {
56!
2141
    tscError("NULL parameter for %s", __FUNCTION__);
×
2142
    terrno = TSDB_CODE_INVALID_PARA;
×
2143
    return 0;
×
2144
  }
2145

2146
  return stmtAffectedRowsOnce(stmt);
56✔
2147
}
2148

2149
int taos_stmt_close(TAOS_STMT *stmt) {
72✔
2150
  if (stmt == NULL) {
72!
2151
    tscError("NULL parameter for %s", __FUNCTION__);
×
2152
    terrno = TSDB_CODE_INVALID_PARA;
×
2153
    return terrno;
×
2154
  }
2155

2156
  return stmtClose(stmt);
72✔
2157
}
2158

2159
TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option) {
170✔
2160
  if (NULL == taos) {
170✔
2161
    tscError("NULL parameter for %s", __FUNCTION__);
2!
2162
    terrno = TSDB_CODE_INVALID_PARA;
2✔
2163
    return NULL;
2✔
2164
  }
2165
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
168✔
2166
  if (NULL == pObj) {
168!
2167
    tscError("invalid parameter for %s", __FUNCTION__);
×
2168
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2169
    return NULL;
×
2170
  }
2171

2172
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
168✔
2173

2174
  releaseTscObj(*(int64_t *)taos);
168✔
2175

2176
  return pStmt;
168✔
2177
}
2178

2179
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
168✔
2180
  if (stmt == NULL || sql == NULL) {
168!
2181
    tscError("NULL parameter for %s", __FUNCTION__);
2!
2182
    terrno = TSDB_CODE_INVALID_PARA;
2✔
2183
    return terrno;
2✔
2184
  }
2185

2186
  return stmtPrepare2(stmt, sql, length);
166✔
2187
}
2188

2189
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
162✔
2190
  if (stmt == NULL) {
162!
2191
    tscError("NULL parameter for %s", __FUNCTION__);
×
2192
    terrno = TSDB_CODE_INVALID_PARA;
×
2193
    return terrno;
×
2194
  }
2195

2196
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
162✔
2197
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 1) {
162!
2198
    tscError("async bind param is still working, please try again later");
×
2199
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
2200
  }
2201

2202
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
162!
2203
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
24!
2204
      tscError("wait asyncExecSem failed");
×
2205
    }
2206
    pStmt->execSemWaited = true;
24✔
2207
  }
2208

2209
  SSHashObj *hashTbnames = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR));
162✔
2210
  if (NULL == hashTbnames) {
162!
2211
    tscError("stmt2 bind failed, %s", tstrerror(terrno));
×
2212
    return terrno;
×
2213
  }
2214

2215
  int32_t code = TSDB_CODE_SUCCESS;
162✔
2216
  for (int i = 0; i < bindv->count; ++i) {
518✔
2217
    if (bindv->tbnames && bindv->tbnames[i]) {
366!
2218
      if (pStmt->sql.stbInterlaceMode) {
344✔
2219
        if (tSimpleHashGet(hashTbnames, bindv->tbnames[i], strlen(bindv->tbnames[i])) != NULL) {
140!
2220
          code = terrno = TSDB_CODE_PAR_TBNAME_DUPLICATED;
×
2221
          tscError("stmt2 bind failed, %s %s", tstrerror(terrno), bindv->tbnames[i]);
×
2222
          goto out;
10✔
2223
        }
2224

2225
        code = tSimpleHashPut(hashTbnames, bindv->tbnames[i], strlen(bindv->tbnames[i]), NULL, 0);
140✔
2226
        if (code) {
140!
2227
          goto out;
×
2228
        }
2229
      }
2230

2231
      code = stmtSetTbName2(stmt, bindv->tbnames[i]);
344✔
2232
      if (code) {
344✔
2233
        goto out;
2✔
2234
      }
2235
    }
2236

2237
    SVCreateTbReq *pCreateTbReq = NULL;
364✔
2238
    if (bindv->tags && bindv->tags[i]) {
364!
2239
      code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
224✔
2240
    } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
140✔
2241
      code = stmtCheckTags2(stmt, &pCreateTbReq);
24✔
2242
    } else {
2243
      pStmt->sql.autoCreateTbl = false;
116✔
2244
    }
2245

2246
    if (code) {
364✔
2247
      goto out;
2✔
2248
    }
2249

2250
    if (bindv->bind_cols && bindv->bind_cols[i]) {
362!
2251
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
360✔
2252

2253
      if (bind->num <= 0 || bind->num > INT16_MAX) {
360!
2254
        tscError("invalid bind num %d", bind->num);
×
2255
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2256
        goto out;
6✔
2257
      }
2258

2259
      int32_t insert = 0;
360✔
2260
      (void)stmtIsInsert2(stmt, &insert);
360✔
2261
      if (0 == insert && bind->num > 1) {
360✔
2262
        tscError("only one row data allowed for query");
2!
2263
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
2✔
2264
        goto out;
2✔
2265
      }
2266

2267
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
358✔
2268
      if (TSDB_CODE_SUCCESS != code) {
358✔
2269
        goto out;
4✔
2270
      }
2271
    }
2272
  }
2273

2274
out:
152✔
2275
  tSimpleHashCleanup(hashTbnames);
162✔
2276

2277
  return code;
162✔
2278
}
2279

2280
int taos_stmt2_bind_param_a(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx, __taos_async_fn_t fp,
×
2281
                            void *param) {
2282
  if (stmt == NULL || bindv == NULL || fp == NULL) {
×
2283
    terrno = TSDB_CODE_INVALID_PARA;
×
2284
    return terrno;
×
2285
  }
2286

2287
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2288

2289
  ThreadArgs *args = (ThreadArgs *)taosMemoryMalloc(sizeof(ThreadArgs));
×
2290
  args->stmt = stmt;
×
2291
  args->bindv = bindv;
×
2292
  args->col_idx = col_idx;
×
2293
  args->fp = fp;
×
2294
  args->param = param;
×
2295

2296
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2297
  if (atomic_load_8((int8_t *)&pStmt->asyncBindParam.asyncBindNum) > 0) {
×
2298
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2299
    tscError("async bind param is still working, please try again later");
×
2300
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
2301
  }
2302
  (void)atomic_add_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2303
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2304

2305
  int code_s = taosStmt2AsyncBind(stmtAsyncBindThreadFunc, (void *)args);
×
2306
  if (code_s != TSDB_CODE_SUCCESS) {
×
2307
    (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2308
    (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2309
    (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2310
    (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2311
    tscError("async bind failed, code:%d , %s", code_s, tstrerror(code_s));
×
2312
  }
2313

2314
  return code_s;
×
2315
}
2316

2317
int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows) {
146✔
2318
  if (stmt == NULL) {
146!
2319
    tscError("NULL parameter for %s", __FUNCTION__);
×
2320
    terrno = TSDB_CODE_INVALID_PARA;
×
2321
    return terrno;
×
2322
  }
2323

2324
  return stmtExec2(stmt, affected_rows);
146✔
2325
}
2326

2327
int taos_stmt2_close(TAOS_STMT2 *stmt) {
164✔
2328
  if (stmt == NULL) {
164!
2329
    tscError("NULL parameter for %s", __FUNCTION__);
×
2330
    terrno = TSDB_CODE_INVALID_PARA;
×
2331
    return terrno;
×
2332
  }
2333

2334
  return stmtClose2(stmt);
164✔
2335
}
2336

2337
int taos_stmt2_is_insert(TAOS_STMT2 *stmt, int *insert) {
×
2338
  if (stmt == NULL || insert == NULL) {
×
2339
    tscError("NULL parameter for %s", __FUNCTION__);
×
2340
    terrno = TSDB_CODE_INVALID_PARA;
×
2341
    return terrno;
×
2342
  }
2343

2344
  return stmtIsInsert2(stmt, insert);
×
2345
}
2346

2347
int taos_stmt2_get_fields(TAOS_STMT2 *stmt, int *count, TAOS_FIELD_ALL **fields) {
114✔
2348
  if (stmt == NULL || count == NULL) {
114!
2349
    tscError("NULL parameter for %s", __FUNCTION__);
2!
2350
    terrno = TSDB_CODE_INVALID_PARA;
2✔
2351
    return terrno;
2✔
2352
  }
2353

2354
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
112✔
2355
  if (pStmt->sql.type == 0) {
112✔
2356
    int isInsert = 0;
98✔
2357
    (void)stmtIsInsert2(stmt, &isInsert);
98✔
2358
    if (!isInsert) {
98✔
2359
      pStmt->sql.type = STMT_TYPE_QUERY;
18✔
2360
    }
2361
  }
2362
  if (pStmt->sql.type == STMT_TYPE_QUERY) {
112✔
2363
    return stmtGetParamNum2(stmt, count);
18✔
2364
  }
2365

2366
  return stmtGetStbColFields2(stmt, count, fields);
94✔
2367
}
2368

2369
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_ALL *fields) {
92✔
2370
  (void)stmt;
2371
  if (!fields) return;
92✔
2372
  taosMemoryFree(fields);
46!
2373
}
2374

2375
TAOS_RES *taos_stmt2_result(TAOS_STMT2 *stmt) {
6✔
2376
  if (stmt == NULL) {
6!
2377
    tscError("NULL parameter for %s", __FUNCTION__);
×
2378
    terrno = TSDB_CODE_INVALID_PARA;
×
2379
    return NULL;
×
2380
  }
2381

2382
  return stmtUseResult2(stmt);
6✔
2383
}
2384

2385
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmtErrstr2(stmt); }
6✔
2386

2387
int taos_set_conn_mode(TAOS *taos, int mode, int value) {
8✔
2388
  if (taos == NULL) {
8!
2389
    terrno = TSDB_CODE_INVALID_PARA;
×
2390
    return terrno;
×
2391
  }
2392

2393
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
8✔
2394
  if (NULL == pObj) {
8!
2395
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2396
    tscError("invalid parameter for %s", __func__);
×
2397
    return terrno;
×
2398
  }
2399
  switch (mode) {
8!
2400
    case TAOS_CONN_MODE_BI:
8✔
2401
      atomic_store_8(&pObj->biMode, value);
8✔
2402
      break;
8✔
2403
    default:
×
2404
      tscError("not supported mode.");
×
2405
      return TSDB_CODE_INVALID_PARA;
×
2406
  }
2407
  return 0;
8✔
2408
}
2409

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

© 2026 Coveralls, Inc