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

taosdata / TDengine / #4870

26 Nov 2025 05:46AM UTC coverage: 64.545% (+0.006%) from 64.539%
#4870

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

768 of 945 new or added lines in 33 files covered. (81.27%)

2982 existing lines in 119 files now uncovered.

158219 of 245129 relevant lines covered (64.55%)

112474797.36 hits per line

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

47.98
/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, ...) {
609,435✔
46
  if (arg == NULL) {
609,435✔
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) {
200,805,395✔
52
    if (i % 1000 == 0) {
200,003,880✔
53
      (void)sched_yield();
201,860✔
54
    }
55
  }
56

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

249
  monitorClose();
1,676,586✔
250
  tscStopCrashReport();
1,676,586✔
251

252
  hbMgrCleanUp();
1,676,586✔
253

254
  catalogDestroy();
1,676,586✔
255
  schedulerDestroy();
1,676,586✔
256

257
  fmFuncMgtDestroy();
1,676,586✔
258
  qCleanupKeywordsTable();
1,676,586✔
259

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

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

269
  id = clientConnRefPool;
1,676,586✔
270
  clientConnRefPool = -1;
1,676,586✔
271
  taosCloseRef(id);
1,676,586✔
272

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

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

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

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

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

307
TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) {
3,501,247✔
308
  tscInfo("try to connect to %s:%u, user:%s db:%s", ip, port, user, db);
3,501,247✔
309
  if (user == NULL) {
3,502,749✔
310
    user = TSDB_DEFAULT_USER;
372,922✔
311
  }
312

313
  if (pass == NULL) {
3,502,749✔
314
    pass = TSDB_DEFAULT_PASS;
372,922✔
315
  }
316

317
  STscObj *pObj = NULL;
3,502,749✔
318
  int32_t  code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj);
3,502,233✔
319
  if (TSDB_CODE_SUCCESS == code) {
3,501,494✔
320
    int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t));
3,488,770✔
321
    if (NULL == rid) {
3,488,169✔
322
      tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db);
×
323
      return NULL;
×
324
    }
325
    *rid = pObj->id;
3,488,169✔
326
    return (TAOS *)rid;
3,489,027✔
327
  } else {
328
    terrno = code;
12,724✔
329
  }
330

331
  return NULL;
12,992✔
332
}
333

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

506
  __taos_async_whitelist_dual_stack_fn_t userCbFn;
507
} SFetchWhiteListDualStackInfo;
508

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

513
  SGetUserWhiteListRsp wlRsp = {0};
×
514

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

655
void taos_close(TAOS *taos) {
3,488,707✔
656
  if (taos == NULL) {
3,488,707✔
657
    return;
646✔
658
  }
659

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

666
  taos_close_internal(pObj);
3,488,167✔
667
  releaseTscObj(*(int64_t *)taos);
3,488,413✔
668
  taosMemoryFree(taos);
3,488,413✔
669
}
670

671
int taos_errno(TAOS_RES *res) {
798,981,256✔
672
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
798,981,256✔
673
    return terrno;
36,815✔
674
  }
675

676
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
798,949,222✔
677
    return 0;
344,830✔
678
  }
679

680
  return ((SRequestObj *)res)->code;
798,603,527✔
681
}
682

683
const char *taos_errstr(TAOS_RES *res) {
21,212,036✔
684
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
21,212,036✔
685
    return (const char *)tstrerror(terrno);
32,761✔
686
  }
687

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

692
  SRequestObj *pRequest = (SRequestObj *)res;
21,179,275✔
693
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
21,179,275✔
694
    return pRequest->msgBuf;
17,192,200✔
695
  } else {
696
    return (const char *)tstrerror(pRequest->code);
3,987,075✔
697
  }
698
}
699

700
void taos_free_result(TAOS_RES *res) {
630,165,133✔
701
  if (NULL == res) {
630,165,133✔
702
    return;
1,428,454✔
703
  }
704

705
  tscTrace("res:%p, will be freed", res);
628,736,679✔
706

707
  if (TD_RES_QUERY(res)) {
628,736,680✔
708
    SRequestObj *pRequest = (SRequestObj *)res;
626,130,423✔
709
    tscDebug("QID:0x%" PRIx64 ", call taos_free_result to free query, res:%p", pRequest->requestId, res);
626,130,423✔
710
    destroyRequest(pRequest);
626,131,156✔
711
    return;
626,130,879✔
712
  }
713

714
  SMqRspObj *pRsp = (SMqRspObj *)res;
2,603,993✔
715
  if (TD_RES_TMQ(res)) {
2,603,993✔
716
    tDeleteMqDataRsp(&pRsp->dataRsp);
2,598,831✔
717
    doFreeReqResultInfo(&pRsp->resInfo);
2,598,831✔
718
  } else if (TD_RES_TMQ_METADATA(res)) {
8,057✔
719
    tDeleteSTaosxRsp(&pRsp->dataRsp);
320✔
720
    doFreeReqResultInfo(&pRsp->resInfo);
320✔
721
  } else if (TD_RES_TMQ_META(res)) {
7,737✔
722
    tDeleteMqMetaRsp(&pRsp->metaRsp);
6,724✔
723
  } else if (TD_RES_TMQ_BATCH_META(res)) {
1,013✔
724
    tDeleteMqBatchMetaRsp(&pRsp->batchMetaRsp);
1,013✔
725
  } else if (TD_RES_TMQ_RAW(res)) {
×
726
    tDeleteMqRawDataRsp(&pRsp->dataRsp);
×
727
  }
728
  taosMemoryFree(pRsp);
2,606,712✔
729
}
730

731
void taos_kill_query(TAOS *taos) {
1,292✔
732
  if (NULL == taos) {
1,292✔
733
    return;
646✔
734
  }
735

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

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

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

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

755
TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) {
1,421,972,085✔
756
  if (taos_num_fields(res) == 0 || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,421,972,085✔
757
    return NULL;
2,458,903✔
758
  }
759

760
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
1,419,535,458✔
761
  return pResInfo->userFields;
1,419,535,458✔
762
}
763

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

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

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

782
  if (TD_RES_QUERY(res)) {
2,147,483,647✔
783
    SRequestObj *pRequest = (SRequestObj *)res;
1,595,823,740✔
784
    if (pRequest->killed) {
1,595,823,740✔
785
      tscInfo("query has been killed, can not fetch more row.");
×
786
      pRequest->code = TSDB_CODE_TSC_QUERY_KILLED;
×
787
      return NULL;
×
788
    }
789

790
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
1,595,776,886✔
791
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
1,595,841,152✔
792
      return NULL;
×
793
    }
794

795
    if (pRequest->inCallback) {
1,595,672,558✔
UNCOV
796
      tscError("can not call taos_fetch_row before query callback ends.");
×
UNCOV
797
      terrno = TSDB_CODE_TSC_INVALID_OPERATION;
×
UNCOV
798
      return NULL;
×
799
    }
800

801
    return doAsyncFetchRows(pRequest, true, true);
1,595,786,931✔
802
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,320,871,148✔
803
    SMqRspObj      *msg = ((SMqRspObj *)res);
1,320,855,431✔
804
    SReqResultInfo *pResultInfo = NULL;
1,320,855,431✔
805
    if (msg->resIter == -1) {
1,321,006,134✔
806
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
2,209,340✔
807
        return NULL;
×
808
      }
809
    } else {
810
      pResultInfo = tmqGetCurResInfo(res);
1,318,898,429✔
811
    }
812

813
    if (pResultInfo->current < pResultInfo->numOfRows) {
1,321,107,681✔
814
      doSetOneRowPtr(pResultInfo);
1,294,079,323✔
815
      pResultInfo->current += 1;
1,294,190,713✔
816
      return pResultInfo->row;
1,294,304,957✔
817
    } else {
818
      if (tmqGetNextResInfo(res, true, &pResultInfo) != 0) {
27,027,036✔
819
        return NULL;
2,209,190✔
820
      }
821

822
      doSetOneRowPtr(pResultInfo);
24,816,479✔
823
      pResultInfo->current += 1;
24,817,428✔
824
      return pResultInfo->row;
24,817,340✔
825
    }
826
  } else if (TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
×
827
    return NULL;
×
828
  } else {
829
    tscError("invalid result passed to taos_fetch_row");
×
830
    terrno = TSDB_CODE_TMQ_INVALID_DATA;
×
831
    return NULL;
×
832
  }
833
}
834

835
int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
1,321,629,502✔
836
  return taos_print_row_with_size(str, INT32_MAX, row, fields, num_fields);
1,321,629,502✔
837
}
838
int taos_print_row_with_size(char *str, uint32_t size, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) {
1,321,993,557✔
839
  int32_t len = 0;
1,321,993,557✔
840
  for (int i = 0; i < num_fields; ++i) {
2,147,483,647✔
841
    if (i > 0 && len < size - 1) {
2,147,483,647✔
842
      str[len++] = ' ';
2,147,483,647✔
843
    }
844

845
    if (row[i] == NULL) {
2,147,483,647✔
846
      len += tsnprintf(str + len, size - len, "%s", TSDB_DATA_NULL_STR);
26,983,511✔
847
      continue;
26,983,304✔
848
    }
849

850
    switch (fields[i].type) {
2,147,483,647✔
851
      case TSDB_DATA_TYPE_TINYINT:
75,387,019✔
852
        len += tsnprintf(str + len, size - len, "%d", *((int8_t *)row[i]));
75,387,019✔
853
        break;
75,313,564✔
854

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

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

863
      case TSDB_DATA_TYPE_USMALLINT:
1,052,800✔
864
        len += tsnprintf(str + len, size - len, "%u", *((uint16_t *)row[i]));
1,052,800✔
865
        break;
1,052,800✔
866

867
      case TSDB_DATA_TYPE_INT:
1,348,435,271✔
868
        len += tsnprintf(str + len, size - len, "%d", *((int32_t *)row[i]));
1,348,435,271✔
869
        break;
1,348,366,272✔
870

871
      case TSDB_DATA_TYPE_UINT:
1,052,800✔
872
        len += tsnprintf(str + len, size - len, "%u", *((uint32_t *)row[i]));
1,052,800✔
873
        break;
1,052,800✔
874

875
      case TSDB_DATA_TYPE_BIGINT:
1,020,632,074✔
876
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
1,020,632,074✔
877
        break;
1,020,634,270✔
878

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

883
      case TSDB_DATA_TYPE_FLOAT: {
148,927,301✔
884
        float fv = 0;
148,927,301✔
885
        fv = GET_FLOAT_VAL(row[i]);
148,927,301✔
886
        len += snprintf(str + len, size - len, "%.*g", FLT_DIG, fv);
148,892,192✔
887
      } break;
149,556,773✔
888

889
      case TSDB_DATA_TYPE_DOUBLE: {
716,383,820✔
890
        double dv = 0;
716,383,820✔
891
        dv = GET_DOUBLE_VAL(row[i]);
716,383,820✔
892
        len += snprintf(str + len, size - len, "%.*g", DBL_DIG, dv);
716,383,425✔
893
      } break;
716,378,645✔
894

895
      case TSDB_DATA_TYPE_VARBINARY: {
1,053,112✔
896
        void    *data = NULL;
1,053,112✔
897
        uint32_t tmp = 0;
1,053,112✔
898
        int32_t  charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
1,053,112✔
899
        if (taosAscii2Hex(row[i], charLen, &data, &tmp) < 0) {
1,053,112✔
900
          break;
×
901
        }
902
        uint32_t copyLen = TMIN(size - len - 1, tmp);
1,053,112✔
903
        (void)memcpy(str + len, data, copyLen);
1,053,112✔
904
        len += copyLen;
1,053,112✔
905
        taosMemoryFree(data);
1,053,112✔
906
      } break;
1,053,112✔
907
      case TSDB_DATA_TYPE_BINARY:
1,324,167,607✔
908
      case TSDB_DATA_TYPE_NCHAR:
909
      case TSDB_DATA_TYPE_GEOMETRY: {
910
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
1,324,167,607✔
911
        if (fields[i].type == TSDB_DATA_TYPE_BINARY || fields[i].type == TSDB_DATA_TYPE_VARBINARY ||
1,324,134,098✔
912
            fields[i].type == TSDB_DATA_TYPE_GEOMETRY) {
349,951,710✔
913
          if (charLen > fields[i].bytes || charLen < 0) {
975,323,628✔
UNCOV
914
            tscError("taos_print_row error binary. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
×
915
            break;
×
916
          }
917
        } else {
918
          if (charLen > fields[i].bytes * TSDB_NCHAR_SIZE || charLen < 0) {
348,899,478✔
919
            tscError("taos_print_row error. charLen:%d, fields[i].bytes:%d", charLen, fields[i].bytes);
213✔
920
            break;
×
921
          }
922
        }
923

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

937
        uint32_t copyLen = TMIN(size - len - 1, tmp);
×
938
        (void)memcpy(str + len, data, copyLen);
×
939
        len += copyLen;
×
940

941
        taosMemoryFree(data);
×
942
      } break;
×
943

944
      case TSDB_DATA_TYPE_TIMESTAMP:
1,695,406,538✔
945
        len += tsnprintf(str + len, size - len, "%" PRId64, *((int64_t *)row[i]));
1,695,406,538✔
946
        break;
1,695,619,466✔
947

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

962
    if (len >= size - 1) {
2,147,483,647✔
963
      break;
×
964
    }
965
  }
966
  if (len < size) {
1,322,472,129✔
967
    str[len] = 0;
1,322,297,978✔
968
  }
969

970
  return len;
1,322,362,451✔
971
}
972

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

978
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
2,147,483,647✔
979
  return pResInfo->length;
2,147,483,647✔
980
}
981

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

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

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

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

1047
const char *taos_get_client_info() { return td_version; }
1,177,852✔
1048

1049
// return int32_t
1050
int taos_affected_rows(TAOS_RES *res) {
487,310,837✔
1051
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
487,310,837✔
1052
      TD_RES_TMQ_BATCH_META(res)) {
487,315,848✔
1053
    return 0;
×
1054
  }
1055

1056
  SRequestObj    *pRequest = (SRequestObj *)res;
487,315,611✔
1057
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
487,315,611✔
1058
  return (int)pResInfo->numOfRows;
487,315,591✔
1059
}
1060

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

1068
  SRequestObj    *pRequest = (SRequestObj *)res;
1,277,162✔
1069
  SReqResultInfo *pResInfo = &pRequest->body.resInfo;
1,277,162✔
1070
  return pResInfo->numOfRows;
1,277,162✔
1071
}
1072

1073
int taos_result_precision(TAOS_RES *res) {
1,343,116,236✔
1074
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
1,343,116,236✔
1075
    return TSDB_TIME_PRECISION_MILLI;
×
1076
  }
1077

1078
  if (TD_RES_QUERY(res)) {
1,343,118,624✔
1079
    SRequestObj *pRequest = (SRequestObj *)res;
92,776,100✔
1080
    return pRequest->body.resInfo.precision;
92,776,100✔
1081
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
1,250,364,695✔
1082
    SReqResultInfo *info = tmqGetCurResInfo(res);
1,250,366,134✔
1083
    return info->precision;
1,250,366,134✔
1084
  }
1085
  return TSDB_TIME_PRECISION_MILLI;
×
1086
}
1087

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

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

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

1106
  TAOS_RES *pRequest = taos_query(taos, sql);
145,601✔
1107
  int32_t   code = taos_errno(pRequest);
145,601✔
1108

1109
  taos_free_result(pRequest);
145,601✔
1110
  releaseTscObj(*(int64_t *)taos);
145,601✔
1111
  return code;
145,601✔
1112
}
1113

1114
void taos_stop_query(TAOS_RES *res) {
630,648,681✔
1115
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_METADATA(res) ||
630,648,681✔
1116
      TD_RES_TMQ_BATCH_META(res)) {
630,652,908✔
1117
    return;
85✔
1118
  }
1119

1120
  stopAllQueries((SRequestObj *)res);
630,645,277✔
1121
}
1122

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

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

1140
bool taos_is_update_query(TAOS_RES *res) { return taos_num_fields(res) == 0; }
7,437,336✔
1141

1142
int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) {
169,636,452✔
1143
  int32_t numOfRows = 0;
169,636,452✔
1144
  /*int32_t code = */ terrno = taos_fetch_block_s(res, &numOfRows, rows);
169,636,452✔
1145
  return numOfRows;
169,636,452✔
1146
}
1147

1148
int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) {
169,636,452✔
1149
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
169,636,452✔
1150
    return 0;
×
1151
  }
1152

1153
  if (TD_RES_QUERY(res)) {
169,636,452✔
1154
    SRequestObj *pRequest = (SRequestObj *)res;
162,976,098✔
1155

1156
    (*rows) = NULL;
162,976,098✔
1157
    (*numOfRows) = 0;
162,976,098✔
1158

1159
    if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
162,976,098✔
1160
        pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
162,833,909✔
1161
      return pRequest->code;
733,120✔
1162
    }
1163

1164
    (void)doAsyncFetchRows(pRequest, false, true);
162,242,978✔
1165

1166
    // TODO refactor
1167
    SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
162,242,390✔
1168
    pResultInfo->current = pResultInfo->numOfRows;
162,242,978✔
1169

1170
    (*rows) = pResultInfo->row;
162,242,978✔
1171
    (*numOfRows) = pResultInfo->numOfRows;
162,242,978✔
1172
    return pRequest->code;
162,242,586✔
1173
  } else if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
6,660,354✔
1174
    SReqResultInfo *pResultInfo = NULL;
6,660,354✔
1175
    int32_t         code = tmqGetNextResInfo(res, true, &pResultInfo);
6,660,354✔
1176
    if (code != 0) return code;
6,660,354✔
1177

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

1188
int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) {
2,170,566✔
1189
  *numOfRows = 0;
2,170,566✔
1190
  *pData = NULL;
2,170,566✔
1191

1192
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
2,170,566✔
1193
    return 0;
×
1194
  }
1195

1196
  if (TD_RES_TMQ(res) || TD_RES_TMQ_METADATA(res)) {
2,170,566✔
1197
    SReqResultInfo *pResultInfo = NULL;
2,107,621✔
1198
    int32_t         code = tmqGetNextResInfo(res, false, &pResultInfo);
2,107,621✔
1199
    if (code != 0) {
2,107,621✔
1200
      (*numOfRows) = 0;
42,467✔
1201
      return 0;
42,467✔
1202
    }
1203

1204
    pResultInfo->current = pResultInfo->numOfRows;
2,065,154✔
1205
    (*numOfRows) = pResultInfo->numOfRows;
2,065,154✔
1206
    (*pData) = (void *)pResultInfo->pData;
2,065,154✔
1207
    return 0;
2,065,154✔
1208
  }
1209

1210
  SRequestObj *pRequest = (SRequestObj *)res;
62,945✔
1211

1212
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT ||
62,945✔
1213
      pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) {
62,945✔
1214
    return pRequest->code;
×
1215
  }
1216

1217
  (void)doAsyncFetchRows(pRequest, false, false);
62,945✔
1218

1219
  SReqResultInfo *pResultInfo = &pRequest->body.resInfo;
62,945✔
1220

1221
  pResultInfo->current = pResultInfo->numOfRows;
62,945✔
1222
  (*numOfRows) = pResultInfo->numOfRows;
62,945✔
1223
  (*pData) = (void *)pResultInfo->pData;
62,945✔
1224

1225
  return pRequest->code;
62,945✔
1226
}
1227

1228
int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) {
113,940,097✔
1229
  if (res == NULL || TD_RES_TMQ_RAW(res) || TD_RES_TMQ_META(res) || TD_RES_TMQ_BATCH_META(res)) {
113,940,097✔
1230
    return 0;
×
1231
  }
1232

1233
  int32_t numOfFields = taos_num_fields(res);
113,940,097✔
1234
  if (columnIndex < 0 || columnIndex >= numOfFields || numOfFields == 0) {
113,940,097✔
1235
    return 0;
×
1236
  }
1237

1238
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
113,940,097✔
1239
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
113,940,097✔
1240
  if (!IS_VAR_DATA_TYPE(pField->type)) {
113,940,097✔
1241
    return 0;
×
1242
  }
1243

1244
  return pResInfo->pCol[columnIndex].offset;
113,940,097✔
1245
}
1246

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

1253
  int32_t numOfFields = taos_num_fields(res);
318,194,613✔
1254
  if (columnIndex >= numOfFields || numOfFields == 0) {
318,194,613✔
1255
    return TSDB_CODE_INVALID_PARA;
×
1256
  }
1257

1258
  SReqResultInfo *pResInfo = tscGetCurResInfo(res);
318,194,613✔
1259
  TAOS_FIELD     *pField = &pResInfo->userFields[columnIndex];
318,194,613✔
1260
  SResultColumn  *pCol = &pResInfo->pCol[columnIndex];
318,194,613✔
1261

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

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

1288
  int code = taos_errno(pObj);
×
1289

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

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

1301
  resetConnectDB(pTscObj);
×
1302

1303
  releaseTscObj(*(int64_t *)taos);
×
1304
}
1305

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

1313
  releaseTscObj(*(int64_t *)taos);
37,035✔
1314

1315
  return pTscObj->sDetailVer;
37,035✔
1316
}
1317

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

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

1343
void destorySqlCallbackWrapper(SSqlCallbackWrapper *pWrapper) {
1,260,162,053✔
1344
  if (NULL == pWrapper) {
1,260,162,053✔
1345
    return;
633,023,621✔
1346
  }
1347
  destoryCatalogReq(pWrapper->pCatalogReq);
627,138,432✔
1348
  taosMemoryFree(pWrapper->pCatalogReq);
627,126,687✔
1349
  qDestroyParseContext(pWrapper->pParseCtx);
627,116,984✔
1350
  taosMemoryFree(pWrapper);
627,113,412✔
1351
}
1352

1353
void destroyCtxInRequest(SRequestObj *pRequest) {
2,707,498✔
1354
  schedulerFreeJob(&pRequest->body.queryJob, 0);
2,707,498✔
1355
  qDestroyQuery(pRequest->pQuery);
2,707,498✔
1356
  pRequest->pQuery = NULL;
2,707,498✔
1357
  destorySqlCallbackWrapper(pRequest->pWrapper);
2,707,498✔
1358
  pRequest->pWrapper = NULL;
2,707,498✔
1359
}
2,707,498✔
1360

1361
static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t code) {
177,415,313✔
1362
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
177,415,313✔
1363
  SRequestObj         *pRequest = pWrapper->pRequest;
177,415,313✔
1364
  SQuery              *pQuery = pRequest->pQuery;
177,415,313✔
1365

1366
  qDebug("req:0x%" PRIx64 ", start to semantic analysis, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
177,415,289✔
1367

1368
  int64_t analyseStart = taosGetTimestampUs();
177,414,791✔
1369
  pRequest->metric.ctgCostUs = analyseStart - pRequest->metric.ctgStart;
177,414,791✔
1370
  pWrapper->pParseCtx->parseOnly = pRequest->parseOnly;
177,415,306✔
1371

1372
  if (TSDB_CODE_SUCCESS == code) {
177,414,887✔
1373
    code = qAnalyseSqlSemantic(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
177,411,474✔
1374
  }
1375

1376
  pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
177,394,750✔
1377

1378
  if (pRequest->parseOnly) {
177,402,350✔
1379
    (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta));
291,656✔
1380
    (void)memset(pResultMeta, 0, sizeof(*pResultMeta));
291,656✔
1381
  }
1382

1383
  handleQueryAnslyseRes(pWrapper, pResultMeta, code);
177,405,818✔
1384
}
177,401,174✔
1385

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

1413
    *ppTarget = pTarget;
×
1414
  }
1415

1416
  return code;
×
1417
}
1418

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

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

1450
void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code) {
177,387,544✔
1451
  SRequestObj *pRequest = pWrapper->pRequest;
177,387,544✔
1452
  SQuery      *pQuery = pRequest->pQuery;
177,400,467✔
1453

1454
  if (code == TSDB_CODE_SUCCESS && pQuery->pPrevRoot) {
177,382,470✔
1455
    SNode *prevRoot = pQuery->pPrevRoot;
×
1456
    pQuery->pPrevRoot = NULL;
×
1457
    handleSubQueryFromAnalyse(pWrapper, pResultMeta, prevRoot);
×
1458
    return;
×
1459
  }
1460

1461
  if (code == TSDB_CODE_SUCCESS) {
177,395,482✔
1462
    pRequest->stableQuery = pQuery->stableQuery;
162,402,723✔
1463
    if (pQuery->pRoot) {
162,432,441✔
1464
      pRequest->stmtType = pQuery->pRoot->type;
162,415,296✔
1465
    }
1466

1467
    if (pQuery->haveResultSet) {
162,418,309✔
1468
      code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols, pQuery->pResExtSchema,
88,135,116✔
1469
                              pRequest->stmtBindVersion > 0);
88,134,721✔
1470
      setResPrecision(&pRequest->body.resInfo, pQuery->precision);
88,137,527✔
1471
    }
1472
  }
1473

1474
  if (code == TSDB_CODE_SUCCESS) {
177,378,756✔
1475
    TSWAP(pRequest->dbList, (pQuery)->pDbList);
162,422,308✔
1476
    TSWAP(pRequest->tableList, (pQuery)->pTableList);
162,412,604✔
1477
    TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList);
162,400,606✔
1478

1479
    launchAsyncQuery(pRequest, pQuery, pResultMeta, pWrapper);
162,396,268✔
1480
  } else {
1481
    destorySqlCallbackWrapper(pWrapper);
14,956,448✔
1482
    pRequest->pWrapper = NULL;
14,956,448✔
1483
    qDestroyQuery(pRequest->pQuery);
14,956,448✔
1484
    pRequest->pQuery = NULL;
14,956,448✔
1485

1486
    if (NEED_CLIENT_HANDLE_ERROR(code) && pRequest->stmtBindVersion == 0) {
14,956,448✔
1487
      tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%d - %s, tryCount:%d, QID:0x%" PRIx64,
2,658,614✔
1488
               pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId);
1489
      restartAsyncQuery(pRequest, code);
2,658,614✔
1490
      return;
2,658,373✔
1491
    }
1492

1493
    // return to app directly
1494
    tscError("req:0x%" PRIx64 ", error occurs, code:%s, return to user app, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
12,297,834✔
1495
             pRequest->requestId);
1496
    pRequest->code = code;
12,297,834✔
1497
    returnToUser(pRequest);
12,297,834✔
1498
  }
1499
}
1500

1501
static int32_t getAllMetaAsync(SSqlCallbackWrapper *pWrapper, catalogCallback fp) {
187,194,103✔
1502
  SRequestConnInfo conn = {.pTrans = pWrapper->pParseCtx->pTransporter,
366,730,658✔
1503
                           .requestId = pWrapper->pParseCtx->requestId,
187,206,251✔
1504
                           .requestObjRefId = pWrapper->pParseCtx->requestRid,
187,200,309✔
1505
                           .mgmtEps = pWrapper->pParseCtx->mgmtEpSet};
187,206,436✔
1506

1507
  pWrapper->pRequest->metric.ctgStart = taosGetTimestampUs();
366,732,123✔
1508

1509
  return catalogAsyncGetAllMeta(pWrapper->pParseCtx->pCatalog, &conn, pWrapper->pCatalogReq, fp, pWrapper,
194,868,195✔
1510
                                &pWrapper->pRequest->body.queryJob);
187,204,252✔
1511
}
1512

1513
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code);
1514

1515
static int32_t phaseAsyncQuery(SSqlCallbackWrapper *pWrapper) {
631,596,369✔
1516
  int32_t code = TSDB_CODE_SUCCESS;
631,596,369✔
1517
  switch (pWrapper->pRequest->pQuery->execStage) {
631,596,369✔
1518
    case QUERY_EXEC_STAGE_PARSE: {
9,809,394✔
1519
      // continue parse after get metadata
1520
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromParse);
9,809,394✔
1521
      break;
9,809,549✔
1522
    }
1523
    case QUERY_EXEC_STAGE_ANALYSE: {
177,398,971✔
1524
      // analysis after get metadata
1525
      code = getAllMetaAsync(pWrapper, doAsyncQueryFromAnalyse);
177,398,971✔
1526
      break;
177,397,867✔
1527
    }
1528
    case QUERY_EXEC_STAGE_SCHEDULE: {
444,395,568✔
1529
      launchAsyncQuery(pWrapper->pRequest, pWrapper->pRequest->pQuery, NULL, pWrapper);
444,395,568✔
1530
      break;
444,394,074✔
1531
    }
1532
    default:
×
1533
      break;
×
1534
  }
1535
  return code;
631,600,272✔
1536
}
1537

1538
static void doAsyncQueryFromParse(SMetaData *pResultMeta, void *param, int32_t code) {
9,809,549✔
1539
  SSqlCallbackWrapper *pWrapper = (SSqlCallbackWrapper *)param;
9,809,549✔
1540
  SRequestObj         *pRequest = pWrapper->pRequest;
9,809,549✔
1541
  SQuery              *pQuery = pRequest->pQuery;
9,809,549✔
1542

1543
  pRequest->metric.ctgCostUs += taosGetTimestampUs() - pRequest->metric.ctgStart;
9,809,549✔
1544
  qDebug("req:0x%" PRIx64 ", continue parse query, QID:0x%" PRIx64 ", code:%s", pRequest->self, pRequest->requestId,
9,809,549✔
1545
         tstrerror(code));
1546

1547
  if (code == TSDB_CODE_SUCCESS) {
9,809,549✔
1548
    // pWrapper->pCatalogReq->forceUpdate = false;
1549
    code = qContinueParseSql(pWrapper->pParseCtx, pWrapper->pCatalogReq, pResultMeta, pQuery);
9,797,915✔
1550
  }
1551

1552
  if (TSDB_CODE_SUCCESS == code) {
9,809,549✔
1553
    code = phaseAsyncQuery(pWrapper);
9,296,443✔
1554
  }
1555

1556
  if (TSDB_CODE_SUCCESS != code) {
9,809,549✔
1557
    tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pWrapper->pRequest->self, code,
513,106✔
1558
             tstrerror(code), pWrapper->pRequest->requestId);
1559
    destorySqlCallbackWrapper(pWrapper);
513,106✔
1560
    pRequest->pWrapper = NULL;
513,106✔
1561
    terrno = code;
513,106✔
1562
    pRequest->code = code;
513,106✔
1563
    doRequestCallback(pRequest, code);
513,106✔
1564
  }
1565
}
9,809,549✔
1566

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

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

1584
void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param) {
130,746✔
1585
  int64_t connId = *(int64_t *)taos;
130,746✔
1586
  taosAsyncQueryImpl(connId, sql, fp, param, false, TD_REQ_FROM_APP);
130,746✔
1587
}
130,746✔
1588

1589
void taos_query_a_with_reqid(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param, int64_t reqid) {
×
1590
  int64_t connId = *(int64_t *)taos;
×
1591
  taosAsyncQueryImplWithReqid(connId, sql, fp, param, false, reqid);
×
1592
}
×
1593

1594
int32_t createParseContext(const SRequestObj *pRequest, SParseContext **pCxt, SSqlCallbackWrapper *pWrapper) {
627,138,657✔
1595
  const STscObj *pTscObj = pRequest->pTscObj;
627,138,657✔
1596

1597
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
627,142,764✔
1598
  if (*pCxt == NULL) {
627,129,445✔
1599
    return terrno;
×
1600
  }
1601

1602
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
1,230,763,053✔
1603
                           .requestRid = pRequest->self,
627,134,800✔
1604
                           .acctId = pTscObj->acctId,
627,138,816✔
1605
                           .db = pRequest->pDb,
627,135,196✔
1606
                           .topicQuery = false,
1607
                           .pSql = pRequest->sqlstr,
627,137,312✔
1608
                           .sqlLen = pRequest->sqlLen,
627,138,313✔
1609
                           .pMsg = pRequest->msgBuf,
627,139,943✔
1610
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1611
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
627,139,135✔
1612
                           .pStmtCb = NULL,
1613
                           .pUser = pTscObj->user,
627,130,487✔
1614
                           .pEffectiveUser = pRequest->effectiveUser,
627,138,192✔
1615
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
627,134,223✔
1616
                           .enableSysInfo = pTscObj->sysInfo,
627,134,391✔
1617
                           .async = true,
1618
                           .svrVer = pTscObj->sVer,
627,135,974✔
1619
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
627,144,316✔
1620
                           .allocatorId = pRequest->allocatorRefId,
627,134,776✔
1621
                           .parseSqlFp = clientParseSql,
1622
                           .parseSqlParam = pWrapper,
1623
                           .setQueryFp = setQueryRequest,
1624
                           .timezone = pTscObj->optionInfo.timezone,
627,131,650✔
1625
                           .charsetCxt = pTscObj->optionInfo.charsetCxt};
627,131,391✔
1626
  int8_t biMode = atomic_load_8(&((STscObj *)pTscObj)->biMode);
627,129,752✔
1627
  (*pCxt)->biMode = biMode;
627,126,074✔
1628
  return TSDB_CODE_SUCCESS;
627,131,810✔
1629
}
1630

1631
int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *pRequest, bool updateMetaForce) {
627,136,355✔
1632
  int32_t              code = TSDB_CODE_SUCCESS;
627,136,355✔
1633
  STscObj             *pTscObj = pRequest->pTscObj;
627,136,355✔
1634
  SSqlCallbackWrapper *pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
627,139,619✔
1635
  if (pWrapper == NULL) {
627,129,582✔
1636
    code = terrno;
×
1637
  } else {
1638
    pWrapper->pRequest = pRequest;
627,129,582✔
1639
    pRequest->pWrapper = pWrapper;
627,134,360✔
1640
    *ppWrapper = pWrapper;
627,133,702✔
1641
  }
1642

1643
  if (TSDB_CODE_SUCCESS == code) {
627,133,801✔
1644
    code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
627,133,220✔
1645
  }
1646

1647
  if (TSDB_CODE_SUCCESS == code) {
627,128,574✔
1648
    pWrapper->pParseCtx->mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
627,128,574✔
1649
    code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pWrapper->pParseCtx->pCatalog);
627,146,759✔
1650
  }
1651

1652
  if (TSDB_CODE_SUCCESS == code && NULL == pRequest->pQuery) {
627,138,883✔
1653
    int64_t syntaxStart = taosGetTimestampUs();
627,143,246✔
1654

1655
    pWrapper->pCatalogReq = taosMemoryCalloc(1, sizeof(SCatalogReq));
627,143,246✔
1656
    if (pWrapper->pCatalogReq == NULL) {
627,118,773✔
1657
      code = terrno;
×
1658
    } else {
1659
      pWrapper->pCatalogReq->forceUpdate = updateMetaForce;
627,123,342✔
1660
      TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired));
627,126,494✔
1661
      code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq);
627,132,639✔
1662
    }
1663

1664
    pRequest->metric.parseCostUs += taosGetTimestampUs() - syntaxStart;
627,139,127✔
1665
  }
1666

1667
  return code;
627,141,373✔
1668
}
1669

1670
void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) {
628,021,120✔
1671
  SSqlCallbackWrapper *pWrapper = NULL;
628,021,120✔
1672
  int32_t              code = TSDB_CODE_SUCCESS;
628,027,663✔
1673

1674
  if (pRequest->retry++ > REQUEST_TOTAL_EXEC_TIMES) {
628,027,663✔
1675
    code = pRequest->prevCode;
885,405✔
1676
    terrno = code;
885,405✔
1677
    pRequest->code = code;
885,405✔
1678
    tscDebug("req:0x%" PRIx64 ", call sync query cb with code:%s", pRequest->self, tstrerror(code));
885,405✔
1679
    doRequestCallback(pRequest, code);
885,405✔
1680
    return;
885,405✔
1681
  }
1682

1683
  if (TSDB_CODE_SUCCESS == code) {
627,129,797✔
1684
    code = prepareAndParseSqlSyntax(&pWrapper, pRequest, updateMetaForce);
627,132,079✔
1685
  }
1686

1687
  if (TSDB_CODE_SUCCESS == code) {
627,127,648✔
1688
    pRequest->stmtType = pRequest->pQuery->pRoot->type;
622,306,049✔
1689
    code = phaseAsyncQuery(pWrapper);
622,307,290✔
1690
  }
1691

1692
  if (TSDB_CODE_SUCCESS != code) {
627,125,385✔
1693
    if (NULL != pRequest->msgBuf && strlen(pRequest->msgBuf) > 0) {
4,828,809✔
1694
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, pRequest->msgBuf,
4,705,633✔
1695
               pRequest->requestId);
1696
    } else {
1697
      tscError("req:0x%" PRIx64 ", error happens, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
123,176✔
1698
               pRequest->requestId);
1699
    }
1700

1701
    destorySqlCallbackWrapper(pWrapper);
4,828,809✔
1702
    pRequest->pWrapper = NULL;
4,828,809✔
1703
    qDestroyQuery(pRequest->pQuery);
4,828,809✔
1704
    pRequest->pQuery = NULL;
4,828,733✔
1705

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

1719
    terrno = code;
4,819,083✔
1720
    pRequest->code = code;
4,819,159✔
1721
    doRequestCallback(pRequest, code);
4,819,159✔
1722
  }
1723
}
1724

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

1757
typedef struct SAsyncFetchParam {
1758
  SRequestObj      *pReq;
1759
  __taos_async_fn_t fp;
1760
  void             *param;
1761
} SAsyncFetchParam;
1762

1763
static int32_t doAsyncFetch(void *pParam) {
106,244,164✔
1764
  SAsyncFetchParam *param = pParam;
106,244,164✔
1765
  taosAsyncFetchImpl(param->pReq, param->fp, param->param);
106,244,164✔
1766
  taosMemoryFree(param);
106,243,170✔
1767
  return TSDB_CODE_SUCCESS;
106,242,427✔
1768
}
1769

1770
void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) {
106,246,780✔
1771
  if (res == NULL || fp == NULL) {
106,246,780✔
1772
    tscError("taos_fetch_rows_a invalid paras");
×
1773
    return;
×
1774
  }
1775
  if (!TD_RES_QUERY(res)) {
106,247,029✔
1776
    tscError("taos_fetch_rows_a res is NULL");
×
1777
    fp(param, res, TSDB_CODE_APP_ERROR);
×
1778
    return;
×
1779
  }
1780

1781
  SRequestObj *pRequest = res;
106,246,775✔
1782
  if (TSDB_SQL_RETRIEVE_EMPTY_RESULT == pRequest->type) {
106,246,775✔
1783
    fp(param, res, 0);
2,865✔
1784
    return;
2,865✔
1785
  }
1786

1787
  SAsyncFetchParam *pParam = taosMemoryCalloc(1, sizeof(SAsyncFetchParam));
106,243,917✔
1788
  if (!pParam) {
106,243,694✔
1789
    fp(param, res, terrno);
×
1790
    return;
×
1791
  }
1792
  pParam->pReq = pRequest;
106,243,694✔
1793
  pParam->fp = fp;
106,243,447✔
1794
  pParam->param = param;
106,244,164✔
1795
  int32_t code = taosAsyncExec(doAsyncFetch, pParam, NULL);
106,243,917✔
1796
  if (TSDB_CODE_SUCCESS != code) {
106,243,603✔
1797
    taosMemoryFree(pParam);
×
1798
    fp(param, res, code);
×
1799
    return;
×
1800
  }
1801
}
1802

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

1815
  // set the current block is all consumed
1816
  pResultInfo->convertUcs4 = false;
7,035✔
1817

1818
  // it is a local executed query, no need to do async fetch
1819
  taos_fetch_rows_a(pRequest, fp, param);
7,035✔
1820
}
1821

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

1833
  return pRequest->body.resInfo.pData;
4,136✔
1834
}
1835

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

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

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

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

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

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

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

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

1877
_return:
×
1878

1879
  terrno = code;
×
1880

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

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

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

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

1905
  pRequest->syncQuery = true;
×
1906

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

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

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

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

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

1928
  *vgId = vgInfo.vgId;
×
1929

1930
_return:
×
1931

1932
  terrno = code;
×
1933

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

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

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

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

1958
  pRequest->syncQuery = true;
×
1959

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

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

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

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

1977
_return:
×
1978

1979
  terrno = code;
×
1980

1981
  destroyRequest(pRequest);
×
1982
  return code;
×
1983
}
1984

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

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

1997
  if (NULL == tableNameList) {
1,242✔
1998
    return TSDB_CODE_SUCCESS;
×
1999
  }
2000

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

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

2016
  pRequest->syncQuery = true;
1,242✔
2017

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

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

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

2033
  conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
1,242✔
2034

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

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

2052
TAOS_STMT *taos_stmt_init(TAOS *taos) {
374,571✔
2053
  STscObj *pObj = acquireTscObj(*(int64_t *)taos);
374,571✔
2054
  if (NULL == pObj) {
375,708✔
2055
    tscError("invalid parameter for %s", __FUNCTION__);
×
2056
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
2057
    return NULL;
×
2058
  }
2059

2060
  TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL);
375,708✔
2061
  if (NULL == pStmt) {
375,708✔
2062
    tscError("stmt init failed, errcode:%s", terrstr());
×
2063
  }
2064
  releaseTscObj(*(int64_t *)taos);
375,708✔
2065

2066
  return pStmt;
375,708✔
2067
}
2068

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

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

2083
  return pStmt;
×
2084
}
2085

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

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

2100
  return pStmt;
26,557✔
2101
}
2102

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

2110
  return stmtPrepare(stmt, sql, length);
743,759✔
2111
}
2112

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

2120
  int32_t code = stmtSetTbName(stmt, name);
18,777✔
2121
  if (code) {
18,777✔
2122
    return code;
621✔
2123
  }
2124

2125
  if (tags) {
18,156✔
2126
    return stmtSetTbTags(stmt, tags);
18,156✔
2127
  }
2128

2129
  return TSDB_CODE_SUCCESS;
×
2130
}
2131

2132
int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name) {
12,863,395✔
2133
  if (stmt == NULL || name == NULL) {
12,863,395✔
2134
    tscError("NULL parameter for %s", __FUNCTION__);
20✔
2135
    terrno = TSDB_CODE_INVALID_PARA;
20✔
2136
    return terrno;
×
2137
  }
2138

2139
  return stmtSetTbName(stmt, name);
12,872,972✔
2140
}
2141

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

2149
  return stmtSetTbTags(stmt, tags);
8,936✔
2150
}
2151

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

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

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

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

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

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

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

2188
  if (bind->num > 1) {
52,710✔
2189
    tscError("invalid bind number %d for %s", bind->num, __FUNCTION__);
3,834✔
2190
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
3,834✔
2191
    return terrno;
3,834✔
2192
  }
2193

2194
  return stmtBindBatch(stmt, bind, -1);
48,876✔
2195
}
2196

2197
int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
318,624,469✔
2198
  if (stmt == NULL || bind == NULL) {
318,624,469✔
2199
    tscError("NULL parameter for %s", __FUNCTION__);
×
2200
    terrno = TSDB_CODE_INVALID_PARA;
×
2201
    return terrno;
×
2202
  }
2203

2204
  if (bind->num <= 0 || bind->num > INT16_MAX) {
323,274,702✔
2205
    tscError("invalid bind num %d", bind->num);
22,430✔
2206
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
22,430✔
2207
    return terrno;
×
2208
  }
2209

2210
  int32_t insert = 0;
325,173,674✔
2211
  int32_t code = stmtIsInsert(stmt, &insert);
324,394,297✔
2212
  if (TSDB_CODE_SUCCESS != code) {
322,673,324✔
2213
    tscError("stmt insert failed, errcode:%s", tstrerror(code));
×
2214
    return code;
×
2215
  }
2216
  if (0 == insert && bind->num > 1) {
322,673,324✔
2217
    tscError("only one row data allowed for query");
×
2218
    terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2219
    return terrno;
×
2220
  }
2221

2222
  return stmtBindBatch(stmt, bind, -1);
322,673,324✔
2223
}
2224

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

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

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

2250
  return stmtBindBatch(stmt, bind, colIdx);
49,980✔
2251
}
2252

2253
int taos_stmt_add_batch(TAOS_STMT *stmt) {
306,873,136✔
2254
  if (stmt == NULL) {
306,873,136✔
2255
    tscError("NULL parameter for %s", __FUNCTION__);
×
2256
    terrno = TSDB_CODE_INVALID_PARA;
×
2257
    return terrno;
×
2258
  }
2259

2260
  return stmtAddBatch(stmt);
306,873,136✔
2261
}
2262

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

2270
  return stmtExec(stmt);
4,295,902✔
2271
}
2272

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

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

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

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

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

2300
  return stmtGetParam(stmt, idx, type, bytes);
×
2301
}
2302

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

2310
  return stmtUseResult(stmt);
10,248✔
2311
}
2312

2313
char *taos_stmt_errstr(TAOS_STMT *stmt) { return (char *)stmtErrstr(stmt); }
10,155✔
2314

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

2322
  return stmtAffectedRows(stmt);
6,672✔
2323
}
2324

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

2332
  return stmtAffectedRowsOnce(stmt);
2,370✔
2333
}
2334

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

2342
  return stmtClose(stmt);
402,019✔
2343
}
2344

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

2358
  TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
17,736✔
2359

2360
  releaseTscObj(*(int64_t *)taos);
17,736✔
2361

2362
  return pStmt;
17,736✔
2363
}
2364

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

2372
  return stmtPrepare2(stmt, sql, length);
19,600✔
2373
}
2374

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

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

2390
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
6,889,870✔
2391
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
2392
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
2393
    }
2394
    pStmt->execSemWaited = true;
×
2395
  }
2396

2397
  int32_t code = TSDB_CODE_SUCCESS;
6,889,614✔
2398
  for (int i = 0; i < bindv->count; ++i) {
22,023,260✔
2399
    if (bindv->tbnames && bindv->tbnames[i]) {
15,129,883✔
2400
      code = stmtSetTbName2(stmt, bindv->tbnames[i]);
15,131,977✔
2401
      if (code) {
15,130,519✔
2402
        terrno = code;
×
2403
        STMT2_ELOG("set tbname failed, code:%s", tstrerror(code));
×
2404
        return terrno;
×
2405
      }
2406
    }
2407

2408
    SVCreateTbReq *pCreateTbReq = NULL;
15,132,145✔
2409
    if (bindv->tags && bindv->tags[i]) {
15,130,619✔
2410
      code = stmtSetTbTags2(stmt, bindv->tags[i], &pCreateTbReq);
9,332,311✔
2411
    } else if (pStmt->bInfo.tbNameFlag & IS_FIXED_TAG) {
5,799,580✔
2412
      code = stmtCheckTags2(stmt, &pCreateTbReq);
1,862,680✔
2413
    } else if (pStmt->sql.autoCreateTbl) {
3,936,136✔
2414
      // if (pStmt->sql.autoCreateTbl) {
2415
      //   pStmt->sql.autoCreateTbl = false;
2416
      //   STMT2_WLOG_E("sql is autoCreateTbl, but no tags");
2417
      // }
2418
      code = stmtSetTbTags2(stmt, NULL, &pCreateTbReq);
×
2419
    }
2420

2421
    if (code) {
15,127,066✔
2422
      terrno = code;
×
2423
      STMT2_ELOG("set tags failed, code:%s", tstrerror(code));
×
2424
      return terrno;
×
2425
    }
2426

2427
    if (bindv->bind_cols && bindv->bind_cols[i]) {
15,127,066✔
2428
      TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
15,131,987✔
2429

2430
      if (bind->num <= 0 || bind->num > INT16_MAX) {
15,130,967✔
2431
        STMT2_ELOG("bind num:%d must > 0 and < INT16_MAX", bind->num);
2,512✔
2432
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
2,512✔
2433
        return terrno;
×
2434
      }
2435

2436
      if (!stmt2IsInsert(stmt) && bind->num > 1) {
15,128,713✔
2437
        STMT2_ELOG_E("only one row data allowed for query");
×
2438
        code = terrno = TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR;
×
2439
        return terrno;
×
2440
      }
2441

2442
      code = stmtBindBatch2(stmt, bind, col_idx, pCreateTbReq);
15,128,516✔
2443
      if (TSDB_CODE_SUCCESS != code) {
15,133,696✔
UNCOV
2444
        terrno = code;
×
2445
        STMT2_ELOG("bind batch failed, code:%s", tstrerror(code));
×
2446
        return terrno;
×
2447
      }
2448
    }
2449
  }
2450

2451
  return code;
6,894,143✔
2452
}
2453

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

2461
  STscStmt2 *pStmt = (STscStmt2 *)stmt;
×
2462

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

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

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

2490
  return code_s;
×
2491
}
2492

2493
int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows) {
6,893,196✔
2494
  if (stmt == NULL) {
6,893,196✔
2495
    tscError("NULL parameter for %s", __FUNCTION__);
×
2496
    terrno = TSDB_CODE_INVALID_PARA;
×
2497
    return terrno;
×
2498
  }
2499

2500
  return stmtExec2(stmt, affected_rows);
6,893,196✔
2501
}
2502

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

2510
  return stmtClose2(stmt);
17,736✔
2511
}
2512

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

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

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

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

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

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

2556
  return stmtUseResult2(stmt);
×
2557
}
2558

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

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

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

2584
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