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

taosdata / TDengine / #5007

29 Mar 2026 04:32AM UTC coverage: 72.25% (-0.02%) from 72.274%
#5007

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253624 of 351039 relevant lines covered (72.25%)

132531546.97 hits per line

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

80.95
/tools/shell/src/shellEngine.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
#define ALLOW_FORBID_FUNC
17
#define _BSD_SOURCE
18
#define _GNU_SOURCE
19
#define _XOPEN_SOURCE
20
#define _DEFAULT_SOURCE
21
#include "../../inc/pub.h"
22
#include "geosWrapper.h"
23
#include "shellAuto.h"
24
#include "shellInt.h"
25

26
SShellObj shell = {0};
27

28
typedef struct {
29
  const char *sql;
30
  bool        vertical;
31
  tsem_t      sem;
32
  int64_t     numOfRows;  // the num of this batch
33
  int64_t     numOfAllRows;
34

35
  int32_t     numFields;
36
  TAOS_FIELD *fields;
37
  int32_t     precision;
38

39
  int32_t maxColNameLen;            // for vertical print
40
  int32_t width[TSDB_MAX_COLUMNS];  // for horizontal print
41

42
  uint64_t resShowMaxNum;
43
} tsDumpInfo;
44

45
static bool    shellIsEmptyCommand(const char *cmd);
46
static int32_t shellRunSingleCommand(char *command);
47
static void    shellRecordCommandToHistory(char *command);
48
static int32_t shellRunCommand(char *command, bool recordHistory);
49
static void    shellRunSingleCommandImp(char *command);
50
static char   *shellFormatTimestamp(char *buf, int32_t bufSize, int64_t val, int32_t precision);
51
static int64_t shellDumpResultToFile(const char *fname, TAOS_RES *tres);
52
static void    shellPrintNChar(const char *str, int32_t length, int32_t width);
53
static void    shellPrintGeometry(const unsigned char *str, int32_t length, int32_t width);
54
static void    shellVerticalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info);
55
static void    shellHorizontalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info);
56
static int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql);
57
static void    shellReadHistory();
58
static void    shellWriteHistory();
59
static void    shellPrintError(TAOS_RES *tres, int64_t st);
60
static bool    shellIsCommentLine(char *line);
61
static void    shellSourceFile(const char *file);
62
static int32_t shellGetGrantInfo(char *buf);
63

64
static void  shellCleanup(void *arg);
65
static void *shellCancelHandler(void *arg);
66
static void *shellThreadLoop(void *arg);
67

68
static bool shellCmdkilled = false;
69

70
bool shellIsEmptyCommand(const char *cmd) {
225,351,055✔
71
  for (char c = *cmd++; c != 0; c = *cmd++) {
225,488,163✔
72
    if (c != ' ' && c != '\t' && c != ';') {
151,970,375✔
73
      return false;
151,833,267✔
74
    }
75
  }
76
  return true;
73,517,788✔
77
}
78

79
int32_t shellRunSingleCommand(char *command) {
149,501,191✔
80
  shellCmdkilled = false;
149,501,191✔
81

82
  if (shellIsEmptyCommand(command)) {
149,501,191✔
83
    return 0;
73,517,788✔
84
  }
85

86
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
75,983,403✔
87
    return -1;
539✔
88
  }
89

90
  if (shellRegexMatch(command, "^[\t ]*clear[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
75,982,864✔
91
#pragma GCC diagnostic push
92
#pragma GCC diagnostic ignored "-Wunused-result"
93
#ifndef TD_ASTRA
94
    (void)system("clear");
×
95
#else
96
    (void)printf("\033[2J\033[H");
97
#endif
98
#pragma GCC diagnostic pop
99
    return 0;
×
100
  }
101

102
  if (shellRegexMatch(command, "^[\t ]*set[ \t]+max_binary_display_width[ \t]+(default|[1-9][0-9]*)[ \t;]*$",
75,982,864✔
103
                      REG_EXTENDED | REG_ICASE)) {
104
    strtok(command, " \t");
7,287✔
105
    strtok(NULL, " \t");
7,287✔
106
    char *p = strtok(NULL, " \t");
7,287✔
107
    if (strncasecmp(p, "default", 7) == 0) {
7,287✔
108
      shell.args.displayWidth = SHELL_DEFAULT_MAX_BINARY_DISPLAY_WIDTH;
×
109
    } else {
110
      int32_t displayWidth = atoi(p);
7,287✔
111
      displayWidth = TRANGE(displayWidth, 1, 10 * 1024);
7,287✔
112
      shell.args.displayWidth = displayWidth;
7,287✔
113
    }
114
    return 0;
7,287✔
115
  }
116

117
  if (shellRegexMatch(command, "^[ \t]*source[\t ]+[^ ]+[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
75,975,577✔
118
    /* If source file. */
119
    char *c_ptr = strtok(command, " ;");
107✔
120
    if (c_ptr == NULL) {
107✔
121
      shellRunSingleCommandImp(command);
×
122
      return 0;
×
123
    }
124
    c_ptr = strtok(NULL, " ;");
107✔
125
    if (c_ptr == NULL) {
107✔
126
      shellRunSingleCommandImp(command);
×
127
      return 0;
×
128
    }
129
    shellSourceFile(c_ptr);
107✔
130
    return 0;
107✔
131
  }
132
  shellRunSingleCommandImp(command);
75,975,470✔
133
  return 0;
75,975,470✔
134
}
135

136
void shellRecordCommandToHistory(char *command) {
1,335,656✔
137
  if (strncasecmp(command, "create user ", 12) == 0 || strncasecmp(command, "alter user ", 11) == 0) {
1,335,656✔
138
    if (taosStrCaseStr(command, " pass ")) {
×
139
      // have password command forbid record to history because security
140
      return;
×
141
    }
142
  }
143

144
  SShellHistory *pHistory = &shell.history;
1,335,656✔
145
  if (pHistory->hstart == pHistory->hend ||
1,335,656✔
146
      pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE] == NULL ||
107✔
147
      strcmp(command, pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE]) != 0) {
107✔
148
    if (pHistory->hist[pHistory->hend] != NULL) {
1,335,656✔
149
      taosMemoryFreeClear(pHistory->hist[pHistory->hend]);
×
150
    }
151
    pHistory->hist[pHistory->hend] = taosStrdup(command);
1,335,656✔
152

153
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
1,335,656✔
154
    if (pHistory->hend == pHistory->hstart) {
1,335,656✔
155
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
×
156
    }
157
  }
158
}
159

160
int32_t shellRunCommand(char *command, bool recordHistory) {
75,849,864✔
161
  if (shellIsEmptyCommand(command)) {
75,849,864✔
162
    return 0;
×
163
  }
164

165
  // add help or help;
166
  if (strncasecmp(command, "help", 4) == 0) {
75,849,864✔
167
    if (command[4] == ';' || command[4] == ' ' || command[4] == 0) {
115✔
168
      showHelp();
115✔
169
      return 0;
115✔
170
    }
171
  }
172

173
  if (recordHistory) shellRecordCommandToHistory(command);
75,849,749✔
174

175
  char quote = 0, *cmd = command;
75,849,749✔
176
  for (char c = *command++; c != 0; c = *command++) {
2,147,483,647✔
177
    if (c == '\\' && (*command == '\'' || *command == '"' || *command == '`')) {
2,147,483,647✔
178
      command++;
1,049✔
179
      continue;
1,049✔
180
    }
181

182
    if (quote == c) {
2,147,483,647✔
183
      quote = 0;
13,450,801✔
184
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
2,147,483,647✔
185
      quote = c;
13,450,801✔
186
    } else if (c == ';' && quote == 0) {
2,147,483,647✔
187
      c = *command;
73,651,981✔
188
      *command = 0;
73,651,981✔
189
      if (shellRunSingleCommand(cmd) < 0) {
73,651,981✔
190
        return -1;
539✔
191
      }
192
      *command = c;
73,651,442✔
193
      cmd = command;
73,651,442✔
194
    }
195
  }
196
  return shellRunSingleCommand(cmd);
75,849,210✔
197
}
198

199
char *strendG(const char *pstr) {
75,975,470✔
200
  if (pstr == NULL) {
75,975,470✔
201
    return NULL;
×
202
  }
203

204
  size_t len = strlen(pstr);
75,975,470✔
205
  if (len < 4) {
75,975,470✔
206
    return NULL;
×
207
  }
208

209
  char *p = (char *)pstr + len - 2;
75,975,470✔
210
  if (strcmp(p, "\\G") == 0) {
75,975,470✔
211
    return p;
6,170✔
212
  }
213

214
  p = (char *)pstr + len - 3;
75,969,300✔
215
  if (strcmp(p, "\\G;") == 0) {
75,969,300✔
216
    return p;
123,543✔
217
  }
218

219
  return NULL;
75,845,757✔
220
}
221

222
void shellRunSingleCommandImp(char *command) {
75,975,470✔
223
  int64_t st, et;
224
  char   *sptr = NULL;
75,975,470✔
225
  char   *cptr = NULL;
75,975,470✔
226
  char   *fname = NULL;
75,975,470✔
227
  bool    printMode = false;
75,975,470✔
228

229
  if ((sptr = strstr(command, ">>")) != NULL) {
75,975,470✔
230
    fname = sptr + 2;
1,048,855✔
231
    while (*fname == ' ') fname++;
2,097,476✔
232
    *sptr = '\0';
1,048,855✔
233

234
    cptr = strstr(fname, ";");
1,048,855✔
235
    if (cptr != NULL) {
1,048,855✔
236
      *cptr = '\0';
1,623✔
237
    }
238
  }
239

240
  if ((sptr = strendG(command)) != NULL) {
75,975,470✔
241
    *sptr = '\0';
129,713✔
242
    printMode = true;  // When output to a file, the switch does not work.
129,713✔
243
  }
244

245
  st = taosGetTimestampUs();
75,975,470✔
246

247
  TAOS_RES *pSql = taos_query(shell.conn, command);
75,975,470✔
248
  if (taos_errno(pSql)) {
75,975,470✔
249
    shellPrintError(pSql, st);
27,876,526✔
250
    return;
27,876,526✔
251
  }
252

253
  if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) {
48,098,944✔
254
    (void)printf("Database changed.\r\n\r\n");
17,174✔
255

256
    // call back auto tab module
257
    callbackAutoTab(command, pSql, true);
17,174✔
258

259
    taos_free_result(pSql);
17,174✔
260

261
    return;
17,174✔
262
  }
263

264
  // pre string
265
  char *pre = "Query OK";
48,081,770✔
266
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
48,081,770✔
267
    pre = "Delete OK";
53✔
268
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
48,081,717✔
269
    pre = "Insert OK";
5,897✔
270
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
48,075,820✔
271
    pre = "Create OK";
5,612✔
272
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
48,070,208✔
273
    pre = "Drop OK";
962✔
274
  }
275

276
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
48,081,770✔
277
  if (pFields != NULL) {  // select and show kinds of commands
48,081,770✔
278
    int32_t error_no = 0;
46,674,109✔
279

280
    int64_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode, command);
46,674,109✔
281
    if (numOfRows < 0) {
46,674,109✔
282
      taos_free_result(pSql);
×
283
      return;
×
284
    }
285

286
    et = taosGetTimestampUs();
46,674,109✔
287
    if (error_no == 0) {
46,674,109✔
288
      (void)printf("Query OK, %" PRId64 " row(s) in set (%.6fs)\r\n", numOfRows, (et - st) / 1E6);
46,674,109✔
289
    } else {
290
      (void)printf("Query interrupted (%s), %" PRId64 " row(s) in set (%.6fs)\r\n", tstrerror(error_no), numOfRows,
×
291
             (et - st) / 1E6);
×
292
    }
293
    taos_free_result(pSql);
46,674,109✔
294
  } else {
295
    int64_t num_rows_affacted = taos_affected_rows64(pSql);
1,407,661✔
296
    taos_free_result(pSql);
1,407,661✔
297
    et = taosGetTimestampUs();
1,407,661✔
298
    (void)printf("%s, %" PRId64 " row(s) affected (%.6fs)\r\n", pre, num_rows_affacted, (et - st) / 1E6);
1,407,661✔
299

300
    // call auto tab
301
    callbackAutoTab(command, NULL, false);
1,407,661✔
302
  }
303

304
  (void)printf("\r\n");
48,081,770✔
305
}
306

307
char *shellFormatTimestamp(char *buf, int32_t bufSize, int64_t val, int32_t precision) {
807,737,712✔
308
  if (shell.args.is_raw_time) {
807,737,712✔
309
    (void)sprintf(buf, "%" PRId64, val);
21,350✔
310
    return buf;
21,350✔
311
  }
312

313
  time_t  tt;
807,245,356✔
314
  int32_t ms = 0;
807,716,362✔
315
  if (precision == TSDB_TIME_PRECISION_NANO) {
807,716,362✔
316
    tt = (time_t)(val / 1000000000);
1,070✔
317
    ms = val % 1000000000;
1,070✔
318
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
807,715,292✔
319
    tt = (time_t)(val / 1000000);
1,070✔
320
    ms = val % 1000000;
1,070✔
321
  } else {
322
    tt = (time_t)(val / 1000);
807,714,222✔
323
    ms = val % 1000;
807,714,222✔
324
  }
325

326
  if (tt <= 0 && ms < 0) {
807,716,362✔
327
    tt--;
9,933✔
328
    if (precision == TSDB_TIME_PRECISION_NANO) {
9,933✔
329
      ms += 1000000000;
×
330
    } else if (precision == TSDB_TIME_PRECISION_MICRO) {
9,933✔
331
      ms += 1000000;
×
332
    } else {
333
      ms += 1000;
9,933✔
334
    }
335
  }
336

337
  struct tm ptm = {0};
807,716,362✔
338
  if (taosLocalTime(&tt, &ptm, buf, bufSize, NULL) == NULL) {
807,716,362✔
339
    return buf;
×
340
  }
341
  size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm);
807,716,362✔
342

343
  if (precision == TSDB_TIME_PRECISION_NANO) {
807,716,362✔
344
    (void)sprintf(buf + pos, ".%09d", ms);
1,070✔
345
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
807,715,292✔
346
    (void)sprintf(buf + pos, ".%06d", ms);
1,070✔
347
  } else {
348
    (void)sprintf(buf + pos, ".%03d", ms);
807,714,222✔
349
  }
350

351
  return buf;
807,716,362✔
352
}
353

354
char *shellDumpHexValue(char *buf, const char *val, int32_t length) {
×
355
  for (int32_t i = 0; i < length; i++) {
×
356
    (void)sprintf(buf + (i * 2), "%02X", val[i]);
×
357
  }
358
  buf[length * 2] = 0;
×
359

360
  return buf;
×
361
}
362

363
void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, int32_t length, int32_t precision) {
2,119,097,512✔
364
  if (val == NULL) {
2,119,097,512✔
365
    taosFprintfFile(pFile, "NULL");
21,572,911✔
366
    return;
21,572,911✔
367
  }
368

369
  char    quotationStr[2] = {'"', 0};
2,097,524,601✔
370
  int32_t width;
371

372
  int n = 0;
2,097,524,601✔
373
#define LENGTH 64
374
  char buf[LENGTH] = {0};
2,097,524,601✔
375
  switch (field->type) {
2,097,524,601✔
376
    case TSDB_DATA_TYPE_BOOL:
568,239,721✔
377
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
568,239,721✔
378
      break;
568,239,721✔
379
    case TSDB_DATA_TYPE_TINYINT:
36,608,081✔
380
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
36,608,081✔
381
      break;
36,608,081✔
382
    case TSDB_DATA_TYPE_UTINYINT:
2,267,039✔
383
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
2,267,039✔
384
      break;
2,267,039✔
385
    case TSDB_DATA_TYPE_SMALLINT:
1,506,175✔
386
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
1,506,175✔
387
      break;
1,506,175✔
388
    case TSDB_DATA_TYPE_USMALLINT:
×
389
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
×
390
      break;
×
391
    case TSDB_DATA_TYPE_INT:
151,736,907✔
392
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
151,736,907✔
393
      break;
151,736,907✔
394
    case TSDB_DATA_TYPE_UINT:
1,511,537✔
395
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
1,511,537✔
396
      break;
1,511,537✔
397
    case TSDB_DATA_TYPE_BIGINT:
198,279,035✔
398
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
198,279,035✔
399
      break;
198,279,035✔
400
    case TSDB_DATA_TYPE_UBIGINT:
750,680✔
401
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
750,680✔
402
      break;
750,680✔
403
    case TSDB_DATA_TYPE_FLOAT:
36,609,151✔
404
      width = SHELL_FLOAT_WIDTH;
36,609,151✔
405
      if (tsEnableScience) {
36,609,151✔
406
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
407
      } else {
408
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
36,609,151✔
409
        if (n > SHELL_FLOAT_WIDTH) {
36,609,151✔
410
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
950,090✔
411
        } else {
412
          taosFprintfFile(pFile, "%s", buf);
35,659,061✔
413
        }
414
      }
415
      break;
36,609,151✔
416
    case TSDB_DATA_TYPE_DOUBLE:
317,648,380✔
417
      width = SHELL_DOUBLE_WIDTH;
317,648,380✔
418
      if (tsEnableScience) {
317,648,380✔
419
        (void)snprintf(buf, LENGTH, "%*.15e", width, GET_DOUBLE_VAL(val));
×
420
        taosFprintfFile(pFile, "%s", buf);
×
421
      } else {
422
        n = tsnprintf(buf, LENGTH, "%*.15f", width, GET_DOUBLE_VAL(val));
317,648,380✔
423
        if (n > SHELL_DOUBLE_WIDTH) {
317,648,380✔
424
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
197,084,898✔
425
        } else {
426
          taosFprintfFile(pFile, "%s", buf);
120,563,482✔
427
        }
428
      }
429
      break;
317,648,380✔
430
    case TSDB_DATA_TYPE_BINARY:
250,578,234✔
431
    case TSDB_DATA_TYPE_NCHAR:
432
    case TSDB_DATA_TYPE_JSON: {
433
      int32_t bufIndex = 0;
250,578,234✔
434
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
250,578,234✔
435
      if (tmp == NULL) break;
250,578,234✔
436
      for (int32_t i = 0; i < length; i++) {
2,147,483,647✔
437
        tmp[bufIndex] = val[i];
2,147,483,647✔
438
        bufIndex++;
2,147,483,647✔
439
        if (val[i] == '\"') {
2,147,483,647✔
440
          tmp[bufIndex] = val[i];
×
441
          bufIndex++;
×
442
        }
443
      }
444
      tmp[bufIndex] = 0;
250,578,234✔
445

446
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
250,578,234✔
447
      taosMemoryFree(tmp);
250,578,234✔
448
    } break;
250,578,234✔
449
    case TSDB_DATA_TYPE_VARBINARY: {
×
450
      void    *tmp = NULL;
×
451
      uint32_t size = 0;
×
452
      if (taosAscii2Hex(val, length, &tmp, &size) < 0) {
×
453
        break;
×
454
      }
455
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
×
456
      taosMemoryFree(tmp);
×
457
      break;
×
458
    }
459
    case TSDB_DATA_TYPE_GEOMETRY: {
×
460
      char *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
×
461
      if (tmp == NULL) break;
×
462
      shellDumpHexValue(tmp, val, length);
×
463
      taosFprintfFile(pFile, "%s", buf);
×
464
      taosMemoryFree(tmp);
×
465
      break;
×
466
    }
467
    case TSDB_DATA_TYPE_TIMESTAMP:
191,396,677✔
468
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
191,396,677✔
469
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
191,396,677✔
470
      break;
191,396,677✔
471
    case TSDB_DATA_TYPE_DECIMAL64:
340,392,984✔
472
    case TSDB_DATA_TYPE_DECIMAL:
473
      taosFprintfFile(pFile, "%s", val);
340,392,984✔
474
      break;
340,392,984✔
475
    case TSDB_DATA_TYPE_BLOB:
×
476
    case TSDB_DATA_TYPE_MEDIUMBLOB: {
477
      void    *tmp = NULL;
×
478
      uint32_t size = 0;
×
479
      if (taosAscii2Hex(val, length, &tmp, &size) < 0) {
×
480
        break;
×
481
      }
482
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
×
483
      taosMemoryFree(tmp);
×
484

485
      break;
×
486
    }
487
    default:
×
488
      break;
×
489
  }
490
}
491

492
int64_t shellDumpResultToFile(const char *fname, TAOS_RES *tres) {
1,023,103✔
493
  char fullname[PATH_MAX] = {0};
1,023,103✔
494
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
1,023,103✔
495
    tstrncpy(fullname, fname, PATH_MAX);
×
496
  }
497

498
  TAOS_ROW row = taos_fetch_row(tres);
1,023,103✔
499
  if (row == NULL) {
1,023,103✔
500
    return 0;
×
501
  }
502

503
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
1,023,103✔
504
  if (pFile == NULL) {
1,023,103✔
505
    (void)fprintf(stderr, "failed to open file: %s\r\n", fullname);
×
506
    return -1;
×
507
  }
508

509
  TAOS_FIELD *fields = taos_fetch_fields(tres);
1,023,103✔
510
  int32_t     num_fields = taos_num_fields(tres);
1,023,103✔
511
  int32_t     precision = taos_result_precision(tres);
1,023,103✔
512

513
  for (int32_t col = 0; col < num_fields; col++) {
2,511,415✔
514
    if (col > 0) {
1,488,312✔
515
      taosFprintfFile(pFile, ",");
465,209✔
516
    }
517
    taosFprintfFile(pFile, "%s", fields[col].name);
1,488,312✔
518
  }
519
  taosFprintfFile(pFile, "\r\n");
1,023,103✔
520

521
  int64_t numOfRows = 0;
1,023,103✔
522
  do {
523
    int32_t *length = taos_fetch_lengths(tres);
1,096,168,408✔
524
    for (int32_t i = 0; i < num_fields; i++) {
2,147,483,647✔
525
      if (i > 0) {
2,119,097,512✔
526
        taosFprintfFile(pFile, ",");
1,022,929,104✔
527
      }
528
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
2,119,097,512✔
529
    }
530
    taosFprintfFile(pFile, "\r\n");
1,096,168,408✔
531

532
    numOfRows++;
1,096,168,408✔
533
    row = taos_fetch_row(tres);
1,096,168,408✔
534
  } while (row != NULL);
1,096,168,408✔
535

536
  taosCloseFile(&pFile);
1,023,103✔
537

538
  return numOfRows;
1,023,103✔
539
}
540

541
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
320,768,734✔
542
  TdWchar tail[3];
320,564,447✔
543
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
320,768,734✔
544

545
  while (pos < length) {
2,147,483,647✔
546
    TdWchar wc;
2,147,483,647✔
547
    int32_t bytes = taosMbToWchar(&wc, str + pos, MB_CUR_MAX);
2,147,483,647✔
548
    if (bytes <= 0) {
2,147,483,647✔
549
      break;
143,345✔
550
    }
551

552
    if (pos + bytes > length) {
2,147,483,647✔
553
      break;
×
554
    }
555
    int w = 0;
2,147,483,647✔
556
    if (*(str + pos) == '\t' || *(str + pos) == '\n' || *(str + pos) == '\r') {
2,147,483,647✔
557
      w = bytes;
×
558
    } else {
559
      w = taosWcharWidth(wc);
2,147,483,647✔
560
    }
561
    pos += bytes;
2,147,483,647✔
562

563
    if (w <= 0) {
2,147,483,647✔
564
      continue;
6,022,137✔
565
    }
566

567
    if (width <= 0) {
2,147,483,647✔
568
      (void)printf("%lc", wc);
221,588,505✔
569
      continue;
221,588,505✔
570
    }
571

572
    totalCols += w;
2,147,483,647✔
573
    if (totalCols > width) {
2,147,483,647✔
574
      break;
1,035,479✔
575
    }
576
    if (totalCols <= (width - 3)) {
2,147,483,647✔
577
      (void)printf("%lc", wc);
2,147,483,647✔
578
      cols += w;
2,147,483,647✔
579
    } else {
580
      tail[tailLen] = wc;
3,338,918✔
581
      tailLen++;
3,338,918✔
582
    }
583
  }
584

585
  if (totalCols > width) {
320,768,734✔
586
    // width could be 1 or 2, so printf("...") cannot be used
587
    for (int32_t i = 0; i < 3; i++) {
4,141,916✔
588
      if (cols >= width) {
3,106,437✔
589
        break;
×
590
      }
591
      putchar('.');
3,106,437✔
592
      ++cols;
3,106,437✔
593
    }
594
  } else {
595
    for (int32_t i = 0; i < tailLen; i++) {
319,970,434✔
596
      (void)printf("%lc", tail[i]);
237,179✔
597
    }
598
    cols = totalCols;
319,733,255✔
599
  }
600

601
  for (; cols < width; cols++) {
2,147,483,647✔
602
    putchar(' ');
2,147,483,647✔
603
  }
604
}
320,768,734✔
605

606
void shellPrintString(const char *str, int32_t width) {
1,521,088,934✔
607
  int32_t len = strlen(str);
1,521,088,934✔
608

609
  if (width == 0) {
1,521,088,934✔
610
    (void)printf("%s", str);
×
611
  } else if (len > width) {
1,521,088,934✔
612
    if (width <= 3) {
7,029✔
613
      (void)printf("%.*s.", width - 1, str);
7,029✔
614
    } else {
615
      (void)printf("%.*s...", width - 3, str);
×
616
    }
617
  } else {
618
    (void)printf("%s%*.s", str, width - len, "");
1,521,081,905✔
619
  }
620
}
1,521,088,934✔
621

622
void shellPrintGeometry(const unsigned char *val, int32_t length, int32_t width) {
×
623
  if (length == 0) {  // empty value
×
624
    shellPrintString("", width);
×
625
    return;
×
626
  }
627

628
  int32_t code = TSDB_CODE_FAILED;
×
629

630
  code = initCtxAsText();
×
631
  if (code != TSDB_CODE_SUCCESS) {
×
632
    shellPrintString(getGeosErrMsg(code), width);
×
633
    return;
×
634
  }
635

636
  char *outputWKT = NULL;
×
637
  code = doAsText(val, length, &outputWKT);
×
638
  if (code != TSDB_CODE_SUCCESS) {
×
639
    shellPrintString(getGeosErrMsg(code), width);  // should NOT happen
×
640
    return;
×
641
  }
642

643
  shellPrintString(outputWKT, width);
×
644

645
  geosFreeBuffer(outputWKT);
×
646
}
647

648
void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision) {
2,147,483,647✔
649
  if (val == NULL) {
2,147,483,647✔
650
    shellPrintString(TSDB_DATA_NULL_STR, width);
1,289,055,545✔
651
    return;
1,289,055,545✔
652
  }
653

654
  int n = 0;
2,147,483,647✔
655
#define LENGTH 64
656
  char buf[LENGTH] = {0};
2,147,483,647✔
657
  switch (field->type) {
2,147,483,647✔
658
    case TSDB_DATA_TYPE_BOOL:
232,033,389✔
659
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
232,033,389✔
660
      break;
232,033,389✔
661
    case TSDB_DATA_TYPE_TINYINT:
62,857,794✔
662
      (void)printf("%*d", width, *((int8_t *)val));
62,857,794✔
663
      break;
62,857,794✔
664
    case TSDB_DATA_TYPE_UTINYINT:
132,363,386✔
665
      (void)printf("%*u", width, *((uint8_t *)val));
132,363,386✔
666
      break;
132,363,386✔
667
    case TSDB_DATA_TYPE_SMALLINT:
49,312,896✔
668
      (void)printf("%*d", width, *((int16_t *)val));
49,312,896✔
669
      break;
49,312,896✔
670
    case TSDB_DATA_TYPE_USMALLINT:
119,755,599✔
671
      (void)printf("%*u", width, *((uint16_t *)val));
119,755,599✔
672
      break;
119,755,599✔
673
    case TSDB_DATA_TYPE_INT:
159,175,706✔
674
      (void)printf("%*d", width, *((int32_t *)val));
159,175,706✔
675
      break;
159,175,706✔
676
    case TSDB_DATA_TYPE_UINT:
169,019,157✔
677
      (void)printf("%*u", width, *((uint32_t *)val));
169,019,157✔
678
      break;
169,019,157✔
679
    case TSDB_DATA_TYPE_BIGINT:
430,488,120✔
680
      (void)printf("%*" PRId64, width, taosGetInt64Aligned((int64_t *)val));
430,488,120✔
681
      break;
430,488,120✔
682
    case TSDB_DATA_TYPE_UBIGINT:
176,087,997✔
683
      (void)printf("%*" PRIu64, width, taosGetUInt64Aligned((uint64_t *)val));
176,087,997✔
684
      break;
176,087,997✔
685
    case TSDB_DATA_TYPE_FLOAT:
78,012,664✔
686
      width = width >= LENGTH ? LENGTH - 1 : width;
78,012,664✔
687
      if (tsEnableScience) {
78,012,664✔
688
        (void)printf("%*.7e", width, taosGetFloatAligned((float *)val));
×
689
      } else {
690
        (void)snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, taosGetFloatAligned((float *)val));
78,012,664✔
691
        (void)printf("%s", buf);
78,012,664✔
692
      }
693
      break;
78,012,664✔
694
    case TSDB_DATA_TYPE_DOUBLE:
336,199,048✔
695
      width = width >= LENGTH ? LENGTH - 1 : width;
336,199,048✔
696
      if (tsEnableScience) {
336,199,048✔
697
        (void)snprintf(buf, LENGTH, "%*.15e", width, taosGetDoubleAligned((double *)val));
×
698
        (void)printf("%s", buf);
×
699
      } else {
700
        (void)snprintf(buf, LENGTH, "%*.*g", width, DBL_DIG, taosGetDoubleAligned((double *)val));
336,199,048✔
701
        (void)printf("%*s", width, buf);
336,199,048✔
702
      }
703
      break;
336,199,048✔
704
    case TSDB_DATA_TYPE_VARBINARY: {
×
705
      void    *data = NULL;
×
706
      uint32_t size = 0;
×
707
      if (taosAscii2Hex(val, length, &data, &size) < 0) {
×
708
        break;
×
709
      }
710
      shellPrintNChar(data, size, width);
×
711
      taosMemoryFree(data);
×
712
      break;
×
713
    }
714
    case TSDB_DATA_TYPE_BINARY:
320,768,734✔
715
    case TSDB_DATA_TYPE_NCHAR:
716
    case TSDB_DATA_TYPE_JSON:
717
      shellPrintNChar(val, length, width);
320,768,734✔
718
      break;
320,768,734✔
719
    case TSDB_DATA_TYPE_GEOMETRY:
×
720
      shellPrintGeometry(val, length, width);
×
721
      break;
×
722
    case TSDB_DATA_TYPE_TIMESTAMP:
616,341,035✔
723
      shellFormatTimestamp(buf, sizeof(buf), taosGetInt64Aligned((int64_t *)val), precision);
616,341,035✔
724
      (void)printf("%s", buf);
616,341,035✔
725
      break;
616,341,035✔
726

727
    case TSDB_DATA_TYPE_BLOB:
×
728
    case TSDB_DATA_TYPE_MEDIUMBLOB: {
729
      void    *data = NULL;
×
730
      uint32_t size = 0;
×
731
      if (taosAscii2Hex(val, length, &data, &size) < 0) {
×
732
        break;
×
733
      }
734
      shellPrintNChar(data, size, width);
×
735
      taosMemoryFree(data);
×
736
      break;
×
737
    }
738
    case TSDB_DATA_TYPE_DECIMAL:
128✔
739
    case TSDB_DATA_TYPE_DECIMAL64:
740
      (void)printf("%*s", width, val);
128✔
741
    default:
128✔
742
      break;
128✔
743
  }
744
}
745

746
// show whole result for this query return true, like limit or describe
747
bool shellIsShowWhole(const char *sql) {
420✔
748
  // limit
749
  char * p = taosStrCaseStr(sql, " limit ");
420✔
750
  if (p != NULL) {
420✔
751
    // except subquery, like "select * from (select * from t limit 10) limit 3", only the last limit is valid
752
    char * p1 = taosStrCaseStr(p + 7, ")");
210✔
753
    if (p1 == NULL) {
210✔
754
      return true;
70✔
755
    }
756
    if (taosStrCaseStr(p1 + 1, " limit ")) {
140✔
757
      return true;
70✔
758
    }
759
  }
760
  // describe
761
  if (taosStrCaseStr(sql, "describe ") != NULL) {
280✔
762
    return true;
70✔
763
  }
764
  // desc
765
  if (taosStrCaseStr(sql, "desc ") != NULL) {
210✔
766
    return true;
70✔
767
  }
768
  // show
769
  if (taosStrCaseStr(sql, "show ") != NULL) {
140✔
770
    return true;
×
771
  }
772
  // explain
773
  if (taosStrCaseStr(sql, "explain ") != NULL) {
140✔
774
    return true;
70✔
775
  }
776

777
  return false;
70✔
778
}
779

780
bool shellIsShowQuery(const char *sql) {
70✔
781
  // todo refactor
782
  if (taosStrCaseStr(sql, "show ") != NULL) {
70✔
783
    return true;
×
784
  }
785

786
  return false;
70✔
787
}
788

789
void init_dump_info(tsDumpInfo *dump_info, TAOS_RES *tres, const char *sql, bool vertical) {
45,651,006✔
790
  dump_info->sql = sql;
45,651,006✔
791
  dump_info->vertical = vertical;
45,651,006✔
792
  tsem_init(&dump_info->sem, 0, 0);
45,651,006✔
793
  dump_info->numOfAllRows = 0;
45,651,006✔
794

795
  dump_info->numFields = taos_num_fields(tres);
45,651,006✔
796
  dump_info->fields = taos_fetch_fields(tres);
45,651,006✔
797
  dump_info->precision = taos_result_precision(tres);
45,651,006✔
798

799
  dump_info->resShowMaxNum = UINT64_MAX;
45,651,006✔
800

801
  if (shell.args.commands == NULL && shell.args.file[0] == 0 && !shellIsShowWhole(dump_info->sql)) {
45,651,006✔
802
    dump_info->resShowMaxNum = SHELL_DEFAULT_RES_SHOW_NUM;
70✔
803
  }
804

805
  if (vertical) {
45,651,006✔
806
    dump_info->maxColNameLen = 0;
120,506✔
807
    for (int32_t col = 0; col < dump_info->numFields; col++) {
247,823✔
808
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
127,317✔
809
      if (len > dump_info->maxColNameLen) {
127,317✔
810
        dump_info->maxColNameLen = len;
122,602✔
811
      }
812
    }
813
  } else {
814
    for (int32_t col = 0; col < dump_info->numFields; col++) {
129,054,980✔
815
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
83,524,480✔
816
    }
817
    // set an appropriate width for token and totp_secret display
818
    if (shellRegexMatch(sql, "^[\t ]*create[ \t]+token[ \t]+.*", REG_EXTENDED | REG_ICASE)) {
45,530,500✔
819
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOKEN_DISPLAY_WIDTH);
×
820
    } else if (shellRegexMatch(sql, "^[\t ]*create[ \t]+totp_secret[ \t]+.*", REG_EXTENDED | REG_ICASE)) {
45,530,500✔
821
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOTP_SECRET_DISPLAY_WIDTH);
×
822
    }
823
  }
824
}
45,651,006✔
825

826
void shellVerticalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
120,506✔
827
  TAOS_ROW row = taos_fetch_row(tres);
120,506✔
828
  if (row == NULL) {
120,506✔
829
    (void)printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
830
    return;
×
831
  }
832

833
  int64_t numOfPintRows = dump_info->numOfAllRows;
120,506✔
834
  int     numOfPrintRowsThisOne = 0;
120,506✔
835

836
  while (row != NULL) {
3,048,087✔
837
    (void)printf("*************************** %" PRId64 ".row ***************************\r\n", numOfPintRows + 1);
3,048,087✔
838

839
    int32_t *length = taos_fetch_lengths(tres);
3,048,087✔
840

841
    for (int32_t i = 0; i < dump_info->numFields; i++) {
6,109,420✔
842
      TAOS_FIELD *field = dump_info->fields + i;
3,061,333✔
843

844
      int32_t padding = (int32_t)(dump_info->maxColNameLen - strlen(field->name));
3,061,333✔
845
      (void)printf("%*.s%s: ", padding, " ", field->name);
3,061,333✔
846

847
      shellPrintField((const char *)row[i], field, 0, length[i], dump_info->precision);
3,061,333✔
848
      putchar('\r');
3,061,333✔
849
      putchar('\n');
3,061,333✔
850
    }
851

852
    numOfPintRows++;
3,048,087✔
853
    numOfPrintRowsThisOne++;
3,048,087✔
854

855
    if (numOfPintRows == dump_info->resShowMaxNum) {
3,048,087✔
856
      (void)printf("\r\n");
×
857
      (void)printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
×
858
      (void)printf("         You can use the `LIMIT` clause to get fewer result to show.\r\n");
×
859
      (void)printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
860
      (void)printf("\r\n");
×
861
      (void)printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
×
862
      (void)printf("\r\n");
×
863
      return;
×
864
    }
865

866
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
3,048,087✔
867
      return;
120,506✔
868
    }
869

870
    row = taos_fetch_row(tres);
2,927,581✔
871
  }
872
  return;
×
873
}
874

875
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
83,524,480✔
876
  int32_t width = (int32_t)strlen(field->name);
83,524,480✔
877

878
  switch (field->type) {
83,524,480✔
879
    case TSDB_DATA_TYPE_NULL:
×
880
      return TMAX(4, width);  // null
×
881
    case TSDB_DATA_TYPE_BOOL:
19,225,711✔
882
      return TMAX(5, width);  // 'false'
19,225,711✔
883

884
    case TSDB_DATA_TYPE_TINYINT:
4,335,813✔
885
    case TSDB_DATA_TYPE_UTINYINT:
886
      return TMAX(4, width);  // '-127'
4,335,813✔
887

888
    case TSDB_DATA_TYPE_SMALLINT:
3,467,642✔
889
    case TSDB_DATA_TYPE_USMALLINT:
890
      return TMAX(6, width);  // '-32767'
3,467,642✔
891

892
    case TSDB_DATA_TYPE_INT:
8,452,167✔
893
    case TSDB_DATA_TYPE_UINT:
894
      return TMAX(11, width);  // '-2147483648'
8,452,167✔
895

896
    case TSDB_DATA_TYPE_BIGINT:
8,154,193✔
897
    case TSDB_DATA_TYPE_UBIGINT:
898
      return TMAX(21, width);  // '-9223372036854775807'
8,154,193✔
899

900
    case TSDB_DATA_TYPE_FLOAT:
2,259,756✔
901
      return TMAX(SHELL_FLOAT_WIDTH, width);
2,259,756✔
902

903
    case TSDB_DATA_TYPE_DOUBLE:
8,038,451✔
904
      return TMAX(SHELL_DOUBLE_WIDTH, width);
8,038,451✔
905

906
    case TSDB_DATA_TYPE_BINARY:
5,494,126✔
907
    case TSDB_DATA_TYPE_GEOMETRY:
908
      if (field->bytes > shell.args.displayWidth) {
5,494,126✔
909
        return TMAX(shell.args.displayWidth, width);
3,683,366✔
910
      } else {
911
        return TMAX(field->bytes + 2, width);
1,810,760✔
912
      }
913
    case TSDB_DATA_TYPE_VARBINARY: {
812✔
914
      int32_t bytes = field->bytes * 2 + 2;
812✔
915
      if (bytes > shell.args.displayWidth) {
812✔
916
        return TMAX(shell.args.displayWidth, width);
×
917
      } else {
918
        return TMAX(bytes + 2, width);
812✔
919
      }
920
    }
921
    case TSDB_DATA_TYPE_NCHAR:
3,618,252✔
922
    case TSDB_DATA_TYPE_JSON: {
923
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
3,618,252✔
924
      if (bytes > shell.args.displayWidth) {
3,618,252✔
925
        return TMAX(shell.args.displayWidth, width);
3,618,252✔
926
      } else {
927
        return TMAX(bytes + 2, width);
×
928
      }
929
    }
930

931
    case TSDB_DATA_TYPE_TIMESTAMP:
20,477,429✔
932
      if (shell.args.is_raw_time) {
20,477,429✔
933
        return TMAX(14, width);
413✔
934
      }
935
      if (precision == TSDB_TIME_PRECISION_NANO) {
20,477,016✔
936
        return TMAX(29, width);
107✔
937
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
20,476,909✔
938
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
107✔
939
      } else {
940
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
20,476,802✔
941
      }
942
    case TSDB_DATA_TYPE_BLOB:
×
943
    case TSDB_DATA_TYPE_MEDIUMBLOB: {
944
      int32_t bytes = TSDB_MAX_BLOB_LEN;
×
945
      if (bytes > shell.args.displayWidth) {
×
946
        return TMAX(shell.args.displayWidth, width);
×
947
      } else {
948
        return TMAX(bytes + 2, width);
×
949
      }
950
    } break;
951

952
    case TSDB_DATA_TYPE_DECIMAL64:
64✔
953
      return TMAX(width, 20);
64✔
954
    case TSDB_DATA_TYPE_DECIMAL:
64✔
955
      return TMAX(width, 40);
64✔
956
    default:
×
957
      ASSERT(false);
×
958
  }
959

960
  return 0;
×
961
}
962

963
void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields) {
32,265,643✔
964
  int32_t rowWidth = 0;
32,265,643✔
965
  for (int32_t col = 0; col < num_fields; col++) {
97,688,938✔
966
    TAOS_FIELD *field = fields + col;
65,423,295✔
967
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
65,423,295✔
968
    int32_t     left = padding / 2;
65,423,295✔
969
    (void)printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
65,423,295✔
970
    rowWidth += width[col] + 3;
65,423,295✔
971
  }
972

973
  putchar('\r');
32,265,643✔
974
  putchar('\n');
32,265,643✔
975
  for (int32_t i = 0; i < rowWidth; i++) {
2,147,483,647✔
976
    putchar('=');
2,147,483,647✔
977
  }
978
  putchar('\r');
32,265,643✔
979
  putchar('\n');
32,265,643✔
980
}
32,265,643✔
981

982
void shellHorizontalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
33,683,588✔
983
  TAOS_ROW row = taos_fetch_row(tres);
33,683,588✔
984
  if (row == NULL) {
33,683,588✔
985
    (void)printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
986
    return;
×
987
  }
988

989
  int64_t numOfPintRows = dump_info->numOfAllRows;
33,683,588✔
990
  int     numOfPrintRowsThisOne = 0;
33,683,588✔
991
  if (numOfPintRows == 0) {
33,683,588✔
992
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
32,265,643✔
993
  }
994

995
  while (row != NULL) {
590,394,901✔
996
    int32_t *length = taos_fetch_lengths(tres);
590,394,901✔
997
    for (int32_t i = 0; i < dump_info->numFields; i++) {
2,147,483,647✔
998
      putchar(' ');
2,147,483,647✔
999
      shellPrintField((const char *)row[i], dump_info->fields + i, dump_info->width[i], length[i],
2,147,483,647✔
1000
                      dump_info->precision);
1001
      putchar(' ');
2,147,483,647✔
1002
      putchar('|');
2,147,483,647✔
1003
    }
1004
    putchar('\r');
590,394,901✔
1005
    putchar('\n');
590,394,901✔
1006

1007
    numOfPintRows++;
590,394,901✔
1008
    numOfPrintRowsThisOne++;
590,394,901✔
1009

1010
    if (numOfPintRows == dump_info->resShowMaxNum) {
590,394,901✔
1011
      (void)printf("\r\n");
70✔
1012
      (void)printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
70✔
1013
      if (shellIsShowQuery(dump_info->sql)) {
70✔
1014
        (void)printf("         You can use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
1015
      } else {
1016
        (void)printf("         You can use the `LIMIT` clause to get fewer result to show.\r\n");
70✔
1017
        (void)printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
70✔
1018
      }
1019
      (void)printf("\r\n");
70✔
1020
      (void)printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
70✔
1021
      (void)printf("\r\n");
70✔
1022
      return;
70✔
1023
    }
1024

1025
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
590,394,831✔
1026
      return;
33,683,518✔
1027
    }
1028

1029
    row = taos_fetch_row(tres);
556,711,313✔
1030
  }
1031
  return;
×
1032
}
1033

1034
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
79,455,100✔
1035
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
79,455,100✔
1036
  if (num_of_rows > 0) {
79,455,100✔
1037
    dump_info->numOfRows = num_of_rows;
33,804,094✔
1038
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
33,804,094✔
1039
      if (dump_info->vertical) {
33,804,094✔
1040
        shellVerticalPrintResult(tres, dump_info);
120,506✔
1041
      } else {
1042
        shellHorizontalPrintResult(tres, dump_info);
33,683,588✔
1043
      }
1044
    }
1045
    dump_info->numOfAllRows += num_of_rows;
33,804,094✔
1046
    if (!shellCmdkilled) {
33,804,094✔
1047
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
33,804,094✔
1048
    } else {
1049
      tsem_post(&dump_info->sem);
×
1050
    }
1051
  } else {
1052
    if (num_of_rows < 0) {
45,651,006✔
1053
      (void)printf("\033[31masync retrieve failed, code: %d, %s\033[0m\n", num_of_rows, tstrerror(num_of_rows));
×
1054
    }
1055
    tsem_post(&dump_info->sem);
45,651,006✔
1056
  }
1057
}
79,455,100✔
1058

1059
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
46,674,109✔
1060
  int64_t num_of_rows = 0;
46,674,109✔
1061
  if (fname != NULL) {
46,674,109✔
1062
    num_of_rows = shellDumpResultToFile(fname, tres);
1,023,103✔
1063
  } else {
1064
    tsDumpInfo dump_info;
40,981,065✔
1065
    if (!shellCmdkilled) {
45,651,006✔
1066
      init_dump_info(&dump_info, tres, sql, vertical);
45,651,006✔
1067
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
45,651,006✔
1068
      tsem_wait(&dump_info.sem);
45,651,006✔
1069
      num_of_rows = dump_info.numOfAllRows;
45,651,006✔
1070
    }
1071
  }
1072

1073
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
46,674,109✔
1074
  return num_of_rows;
46,674,109✔
1075
}
1076

1077
void shellReadHistory() {
1,335,664✔
1078
  SShellHistory *pHistory = &shell.history;
1,335,664✔
1079
  TdFilePtr      pFile = taosOpenFile(pHistory->file, TD_FILE_READ | TD_FILE_STREAM);
1,335,664✔
1080
  if (pFile == NULL) return;
1,335,664✔
1081

1082
  char   *line = taosMemoryMalloc(tsMaxSQLLength + 1);
1,289,370✔
1083
  int32_t read_size = 0;
1,289,370✔
1084
  while ((read_size = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
278,417,091✔
1085
    line[read_size - 1] = '\0';
277,127,721✔
1086
    taosMemoryFree(pHistory->hist[pHistory->hend]);
277,127,721✔
1087
    pHistory->hist[pHistory->hend] = taosStrdup(line);
277,127,721✔
1088

1089
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
277,127,721✔
1090

1091
    if (pHistory->hend == pHistory->hstart) {
277,127,721✔
1092
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
×
1093
    }
1094
  }
1095

1096
  taosMemoryFreeClear(line);
1,289,370✔
1097
  taosCloseFile(&pFile);
1,289,370✔
1098
  int64_t file_size;
1,287,524✔
1099
  if (taosStatFile(pHistory->file, &file_size, NULL, NULL) == 0 && file_size > SHELL_MAX_COMMAND_SIZE) {
1,289,370✔
1100
    TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_TRUNC);
×
1101
    if (pFile == NULL) return;
×
1102
    int32_t endIndex = pHistory->hstart;
×
1103
    if (endIndex != 0) {
×
1104
      endIndex = pHistory->hend;
×
1105
    }
1106
    for (int32_t i = (pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE; i != endIndex;) {
×
1107
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
×
1108
      i = (i + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE;
×
1109
    }
1110
    taosFprintfFile(pFile, "%s\n", pHistory->hist[endIndex]);
×
1111

1112
    /* coverity[+retval] */
1113
    taosFsyncFile(pFile);
×
1114
    taosCloseFile(&pFile);
×
1115
  }
1116
  pHistory->hstart = pHistory->hend;
1,289,370✔
1117
}
1118

1119
void shellWriteHistory() {
1,335,664✔
1120
  SShellHistory *pHistory = &shell.history;
1,335,664✔
1121
  if (pHistory->hend == pHistory->hstart) return;
1,335,664✔
1122
  TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_APPEND);
1,335,549✔
1123
  if (pFile == NULL) return;
1,335,549✔
1124

1125
  for (int32_t i = pHistory->hstart; i != pHistory->hend;) {
2,671,205✔
1126
    if (pHistory->hist[i] != NULL) {
1,335,656✔
1127
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
1,335,656✔
1128
      taosMemoryFree(pHistory->hist[i]);
1,335,656✔
1129
      pHistory->hist[i] = NULL;
1,335,656✔
1130
    }
1131
    i = (i + 1) % SHELL_MAX_HISTORY_SIZE;
1,335,656✔
1132
  }
1133
  taosCloseFile(&pFile);
1,335,549✔
1134
}
1135

1136
void shellCleanupHistory() {
1,335,664✔
1137
  SShellHistory *pHistory = &shell.history;
1,335,664✔
1138
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
1,336,999,664✔
1139
    if (pHistory->hist[i] != NULL) {
1,335,664,000✔
1140
      taosMemoryFree(pHistory->hist[i]);
277,127,721✔
1141
      pHistory->hist[i] = NULL;
277,127,721✔
1142
    }
1143
  }
1144
}
1,335,664✔
1145

1146
void shellPrintError(TAOS_RES *tres, int64_t st) {
27,876,526✔
1147
  int code = taos_errno(tres);
27,876,526✔
1148
  int64_t et = taosGetTimestampUs();
27,876,526✔
1149
  (void)printf("\r\nDB error: %s [0x%08X] (%.6fs)\r\n", taos_errstr(tres), code, (et - st) / 1E6);
27,876,526✔
1150
  taos_free_result(tres);
27,876,526✔
1151

1152
  // tip
1153
  if (code == TSDB_CODE_MND_USER_PASSWORD_EXPIRED) {
27,876,526✔
1154
    (void)fprintf(stdout, "******************** TIPS ********************\n");
50✔
1155
    (void)fprintf(stdout, "Please reset your password using the `ALTER USER <user_name> PASS 'new_password'` command.\n");
50✔
1156
    (void)fprintf(stdout, "**********************************************\n");
50✔
1157
  }
1158
}
27,876,526✔
1159

1160
bool shellIsCommentLine(char *line) {
74,653,677✔
1161
  if (line == NULL) return true;
74,653,677✔
1162
  return shellRegexMatch(line, "^\\s*#.*", REG_EXTENDED);
74,653,677✔
1163
}
1164

1165
void shellSourceFile(const char *file) {
139,154✔
1166
  int32_t read_len = 0;
139,154✔
1167
  char   *cmd = taosMemoryCalloc(1, tsMaxSQLLength + 1);
139,154✔
1168
  size_t  cmd_len = 0;
139,154✔
1169
  char    fullname[PATH_MAX] = {0};
139,154✔
1170
  char    sourceFileCommand[PATH_MAX + 8] = {0};
139,154✔
1171

1172
  if (taosExpandDir(file, fullname, PATH_MAX) != 0) {
139,154✔
1173
    tstrncpy(fullname, file, PATH_MAX);
×
1174
  }
1175

1176
  (void)sprintf(sourceFileCommand, "source %s;", fullname);
139,154✔
1177
  shellRecordCommandToHistory(sourceFileCommand);
139,154✔
1178

1179
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_READ | TD_FILE_STREAM);
139,154✔
1180
  if (pFile == NULL) {
139,154✔
1181
    (void)fprintf(stderr, "failed to open file %s\r\n", fullname);
20,598✔
1182
    taosMemoryFree(cmd);
20,598✔
1183
    return;
20,598✔
1184
  }
1185

1186
  char *line = taosMemoryMalloc(tsMaxSQLLength + 1);
118,556✔
1187
  while ((read_len = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
75,710,279✔
1188
    if (cmd_len + read_len >= tsMaxSQLLength) {
75,591,723✔
1189
      (void)printf("read command line too long over 1M, ignore this line. cmd_len = %d read_len=%d \n", (int32_t)cmd_len,
×
1190
             read_len);
1191
      cmd_len = 0;
×
1192
      memset(line, 0, tsMaxSQLLength + 1);
×
1193
      continue;
×
1194
    }
1195
    line[--read_len] = '\0';
75,591,723✔
1196

1197
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
75,591,723✔
1198
      continue;
938,476✔
1199
    }
1200

1201
    if (line[read_len - 1] == '\\') {
74,653,247✔
1202
      line[read_len - 1] = ' ';
×
1203
      memcpy(cmd + cmd_len, line, read_len);
×
1204
      cmd_len += read_len;
×
1205
      continue;
×
1206
    }
1207

1208
    if (line[read_len - 1] == '\r') {
74,653,247✔
1209
      line[read_len - 1] = ' ';
116,411✔
1210
    }
1211

1212
    memcpy(cmd + cmd_len, line, read_len);
74,653,247✔
1213
    (void)printf("%s%s\r\n", shell.info.promptHeader, cmd);
74,653,247✔
1214
    shellRunCommand(cmd, false);
74,653,247✔
1215
    memset(cmd, 0, tsMaxSQLLength);
74,653,247✔
1216
    cmd_len = 0;
74,653,247✔
1217
  }
1218

1219
  taosMemoryFree(cmd);
118,556✔
1220
  taosMemoryFreeClear(line);
118,556✔
1221
  taosCloseFile(&pFile);
118,556✔
1222
}
1223

1224
int32_t shellGetGrantInfo(char *buf) {
767✔
1225
  int32_t verType = TSDB_VERSION_UNKNOWN;
767✔
1226
  char    sinfo[256] = {0};
767✔
1227
  tstrncpy(sinfo, taos_get_server_info(shell.conn), sizeof(sinfo));
767✔
1228
  strtok(sinfo, "\r\n");
767✔
1229

1230
#ifndef TD_ASTRA
1231
  char sql[] = "show grants";
767✔
1232

1233
  TAOS_RES *tres = taos_query(shell.conn, sql);
767✔
1234

1235
  int32_t code = taos_errno(tres);
767✔
1236
  if (code != TSDB_CODE_SUCCESS) {
767✔
1237
    if (code != TSDB_CODE_OPS_NOT_SUPPORT && code != TSDB_CODE_MND_NO_RIGHTS &&
×
1238
        code != TSDB_CODE_PAR_PERMISSION_DENIED) {
1239
      (void)fprintf(stderr, "Failed to check Server Edition, Reason:0x%04x:%s\r\n\r\n", code, taos_errstr(tres));
×
1240
    }
1241
    taos_free_result(tres);
×
1242
    return verType;
×
1243
  }
1244

1245
  int32_t num_fields = taos_field_count(tres);
767✔
1246
  if (num_fields == 0) {
767✔
1247
    (void)fprintf(stderr, "\r\nInvalid grant information.\r\n");
×
1248
    exit(0);
×
1249
  } else {
1250
    if (tres == NULL) {
767✔
1251
      (void)fprintf(stderr, "\r\nGrant information is null.\r\n");
×
1252
      exit(0);
×
1253
    }
1254

1255
    TAOS_FIELD *fields = taos_fetch_fields(tres);
767✔
1256
    TAOS_ROW    row = taos_fetch_row(tres);
767✔
1257
    if (row == NULL) {
767✔
1258
      (void)fprintf(stderr, "\r\nFailed to get grant information from server. Abort.\r\n");
×
1259
      exit(0);
×
1260
    }
1261
    char serverVersion[64] = {0};
767✔
1262
    char expiretime[32] = {0};
767✔
1263
    char expired[32] = {0};
767✔
1264

1265
    tstrncpy(serverVersion, row[0], 64);
767✔
1266
    memcpy(expiretime, row[1], fields[1].bytes);
767✔
1267
    memcpy(expired, row[2], fields[2].bytes);
767✔
1268

1269
    trimStr(serverVersion, "trial");
767✔
1270

1271
    if (strcmp(serverVersion, "community") == 0) {
767✔
1272
      verType = TSDB_VERSION_OSS;
×
1273
    } else if (strcmp(expiretime, "unlimited") == 0) {
767✔
1274
      verType = TSDB_VERSION_ENTERPRISE;
×
1275
      (void)sprintf(buf, "Server is %s %s. License will never expire.\r\n", serverVersion, sinfo);
×
1276
    } else {
1277
      verType = TSDB_VERSION_ENTERPRISE;
767✔
1278
      (void)sprintf(buf, "Server is %s %s. License will expire at %s.\r\n", serverVersion, sinfo, expiretime);
767✔
1279
    }
1280

1281
    taos_free_result(tres);
767✔
1282
  }
1283

1284
  (void)fprintf(stdout, "\r\n");
767✔
1285
#else
1286
  verType = TSDB_VERSION_ENTERPRISE;
1287
  (void)sprintf(buf, "Server is %s %s. License will never expire.\r\n", TD_PRODUCT_NAME, sinfo);
1288
#endif
1289
  return verType;
767✔
1290
}
1291

1292
#ifdef WINDOWS
1293
BOOL shellQueryInterruptHandler(DWORD fdwCtrlType) {
1294
  tsem_post(&shell.cancelSem);
1295
  return TRUE;
1296
}
1297
#else
1298
void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context) { tsem_post(&shell.cancelSem); }
624✔
1299
#endif
1300

1301
void shellCleanup(void *arg) { taosResetTerminalMode(); }
767✔
1302

1303
void *shellCancelHandler(void *arg) {
767✔
1304
  setThreadName("shellCancelHandler");
767✔
1305
  while (1) {
1306
    if (shell.exit == true) {
1,881✔
1307
      break;
767✔
1308
    }
1309

1310
    if (tsem_wait(&shell.cancelSem) != 0) {
1,114✔
1311
      taosMsleep(10);
×
1312
      continue;
×
1313
    }
1314

1315
    if (shell.conn) {
1,114✔
1316
      shellCmdkilled = true;
347✔
1317
      taos_kill_query(shell.conn);
347✔
1318
    }
1319

1320
#ifdef WINDOWS
1321
    (void)printf("\n%s", shell.info.promptHeader);
1322
#endif
1323
  }
1324

1325
  return NULL;
767✔
1326
}
1327

1328
#pragma GCC diagnostic push
1329
#pragma GCC diagnostic ignored "-Wstringop-overflow"
1330

1331
void *shellThreadLoop(void *arg) {
767✔
1332
  setThreadName("shellThreadLoop");
767✔
1333
  taosGetOldTerminalMode();
767✔
1334
  taosThreadCleanupPush(shellCleanup, NULL);
767✔
1335

1336
  do {
1337
    char *command = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE);
767✔
1338
    if (command == NULL) {
767✔
1339
      (void)printf("failed to malloc command\r\n");
×
1340
      break;
×
1341
    }
1342

1343
    do {
1344
      memset(command, 0, SHELL_MAX_COMMAND_SIZE);
1,114✔
1345
      taosSetTerminalMode();
1,114✔
1346

1347
      if (shellReadCommand(command) != 0) {
1,114✔
1348
        break;
347✔
1349
      }
1350

1351
      taosResetTerminalMode();
767✔
1352
    } while (shellRunCommand(command, true) == 0);
767✔
1353

1354
    taosMemoryFreeClear(command);
767✔
1355
    shellWriteHistory();
767✔
1356
    shellExit();
767✔
1357
  } while (0);
1358

1359
  taosThreadCleanupPop(1);
767✔
1360
  return NULL;
767✔
1361
}
1362

1363
bool inputTotpCode(char *totpCode) {
2,303✔
1364
  bool ret = true;
2,303✔
1365
  (void)printf("Please enter your TOTP code:");
2,303✔
1366
  if (scanf("%255s", totpCode) != 1) {
2,303✔
1367
    (void)fprintf(stderr, "TOTP code reading error\n");
167✔
1368
    ret = false;
167✔
1369
  }
1370
  if (EOF == getchar()) {
2,303✔
1371
    // tip
1372
    (void)fprintf(stdout, "getchar() return EOF\r\n");    
167✔
1373
  }
1374
  return ret;
2,303✔
1375
}
1376

1377
#pragma GCC diagnostic pop
1378

1379
TAOS *createConnect(SShellArgs *pArgs) {
1,340,226✔
1380
  char     show[256] = "\0";
1,340,226✔
1381
  char    *host = NULL;
1,340,226✔
1382
  uint16_t port = 0;
1,340,226✔
1383
  char    *user = NULL;
1,340,226✔
1384
  char    *pwd = NULL;
1,340,226✔
1385
  TAOS    *taos = NULL;
1,340,226✔
1386

1387
  // set mode
1388
  if (pArgs->connMode != CONN_MODE_NATIVE && pArgs->dsn) {
1,340,226✔
1389
    // websocket
1390
    memcpy(show, pArgs->dsn, 20);
1,418✔
1391
    memcpy(show + 20, "...", 3);
1,418✔
1392
    memcpy(show + 23, pArgs->dsn + strlen(pArgs->dsn) - 10, 10);
1,418✔
1393

1394
    // connect dsn
1395
    taos = taos_connect_with_dsn(pArgs->dsn);
1,418✔
1396
  } else {
1397
    host = (char *)pArgs->host;
1,338,808✔
1398
    user = (char *)pArgs->user;
1,338,808✔
1399
    pwd = pArgs->password;
1,338,808✔
1400

1401
    if (pArgs->port_inputted) {
1,338,808✔
1402
      port = pArgs->port;
786✔
1403
    } else {
1404
      port = defaultPort(pArgs->connMode, pArgs->dsn);
1,338,022✔
1405
    }
1406

1407
    (void)sprintf(show, "host:%s port:%d ", host, port);
1,338,808✔
1408

1409
    // connect normal
1410
    if (pArgs->auth) {
1,338,808✔
1411
      taos = taos_connect_auth(host, user, pArgs->auth, pArgs->database, port);
193✔
1412
    } else {
1413
#ifdef TD_ENTERPRISE 
1414
      if (strlen(pArgs->token) > 0) {
1,338,615✔
1415
        // token
1416
        (void)printf("Connect with token ...");
2,592✔
1417
        taos = taos_connect_token(host, pArgs->token, pArgs->database, port);
2,592✔
1418
        if (taos != NULL) {
2,592✔
1419
          (void)printf("... [ OK ]\n");
1,204✔
1420
          return taos;
1,204✔
1421
        }
1422
        (void)printf("... [ FAILED ]\n");
1,388✔
1423
        return NULL;
1,388✔
1424
      }
1425
#endif      
1426
      taos = taos_connect(host, user, pwd, pArgs->database, port);
1,336,023✔
1427
    }
1428

1429
    if (taos == NULL) {
1,336,216✔
1430
      // failed
1431
      int code = taos_errno(NULL);
4,417✔
1432
      if (code == TSDB_CODE_MND_WRONG_TOTP_CODE) {
4,417✔
1433
         // totp
1434
        char totpCode[TSDB_USER_PASSWORD_LONGLEN];
2,303✔
1435
        memset(totpCode, 0, sizeof(totpCode));  
2,303✔
1436
        if (inputTotpCode(totpCode)) {
2,303✔
1437
          (void)printf("Connect with TOTP code:%s ...", totpCode);
2,136✔
1438
          taos = taos_connect_totp(host, user, pwd, totpCode, pArgs->database, port);
2,136✔
1439
          if (taos != NULL) {
2,136✔
1440
            (void)printf("... [ OK ]\n");
1,454✔
1441
            return taos;
1,454✔
1442
          }
1443
          (void)printf("... [ FAILED ]\n");
682✔
1444
          return NULL;
682✔
1445
        }
1446
      }
1447
      // token
1448
    }
1449
  }
1450

1451
  return taos;
1,335,498✔
1452
}
1453

1454
int32_t shellExecute(int argc, char *argv[]) {
1,340,226✔
1455
  int32_t code = 0;
1,340,226✔
1456
  (void)printf(shell.info.clientVersion, shell.info.cusName,
2,680,452✔
1457
         workingMode(shell.args.connMode, shell.args.dsn) == CONN_MODE_NATIVE ? STR_NATIVE : STR_WEBSOCKET,
1,340,226✔
1458
         taos_get_client_info(), shell.info.cusName);
1459
  fflush(stdout);
1,340,226✔
1460

1461
  SShellArgs *pArgs = &shell.args;
1,340,226✔
1462
  shell.conn = createConnect(pArgs);
1,340,226✔
1463

1464
  if (shell.conn == NULL) {
1,340,226✔
1465
    (void)printf("failed to connect to server, reason: %s [0x%08X]\n%s", taos_errstr(NULL), taos_errno(NULL),
4,562✔
1466
           ERROR_CODE_DETAIL);
1467
    fflush(stdout);
4,562✔
1468
    return -1;
4,562✔
1469
  }
1470

1471
  bool runOnce = pArgs->commands != NULL || pArgs->file[0] != 0;
1,335,664✔
1472
  shellSetConn(shell.conn, runOnce);
1,335,664✔
1473
  shellReadHistory();
1,335,664✔
1474

1475
  if (shell.args.is_bi_mode) {
1,335,664✔
1476
    // need set bi mode
1477
    (void)printf("Set BI mode is true.\n");
440✔
1478
    taos_set_conn_mode(shell.conn, TAOS_CONN_MODE_BI, 1);
440✔
1479
  }
1480

1481
  if (runOnce) {
1,335,664✔
1482
    if (pArgs->commands != NULL) {
1,334,897✔
1483
      (void)printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
1,195,850✔
1484
      char *cmd = taosStrdup(pArgs->commands);
1,195,850✔
1485
      shellRunCommand(cmd, true);
1,195,850✔
1486
      taosMemoryFree(cmd);
1,195,850✔
1487
    }
1488

1489
    if (pArgs->file[0] != 0) {
1,334,897✔
1490
      shellSourceFile(pArgs->file);
139,047✔
1491
    }
1492

1493
    taos_close(shell.conn);
1,334,897✔
1494

1495
    shellWriteHistory();
1,334,897✔
1496
    shellCleanupHistory();
1,334,897✔
1497
    return 0;
1,334,897✔
1498
  }
1499

1500
  if ((code = tsem_init(&shell.cancelSem, 0, 0)) != 0) {
767✔
1501
    (void)printf("failed to create cancel semaphore since %s\r\n", tstrerror(code));
×
1502
    return code;
×
1503
  }
1504

1505
  TdThread spid = {0};
767✔
1506
  taosThreadCreate(&spid, NULL, shellCancelHandler, NULL);
767✔
1507

1508
  taosSetSignal(SIGTERM, shellQueryInterruptHandler);
767✔
1509
  taosSetSignal(SIGHUP, shellQueryInterruptHandler);
767✔
1510
  taosSetSignal(SIGINT, shellQueryInterruptHandler);
767✔
1511

1512
  char    buf[512] = {0};
767✔
1513
  int32_t verType = shellGetGrantInfo(buf);
767✔
1514
#ifndef WINDOWS
1515
  printfIntroduction(verType);
767✔
1516
#else
1517
  if (verType == TSDB_VERSION_OSS) {
1518
    showAD(false);
1519
  }
1520
#endif
1521
  // printf version
1522
  if (verType == TSDB_VERSION_ENTERPRISE || verType == TSDB_VERSION_CLOUD) {
767✔
1523
    (void)printf("%s\n", buf);
767✔
1524
  }
1525

1526
  while (1) {
1527
    taosThreadCreate(&shell.pid, NULL, shellThreadLoop, NULL);
767✔
1528
    taosThreadJoin(shell.pid, NULL);
767✔
1529
    taosThreadClear(&shell.pid);
767✔
1530
    if (shell.exit) {
767✔
1531
      tsem_post(&shell.cancelSem);
767✔
1532
      break;
767✔
1533
    }
1534
  }
1535

1536
  if (verType == TSDB_VERSION_OSS) {
767✔
1537
    showAD(true);
×
1538
  }
1539

1540
  taosThreadJoin(spid, NULL);
767✔
1541

1542
  shellCleanupHistory();
767✔
1543
  taos_kill_query(shell.conn);
767✔
1544
  taos_close(shell.conn);
767✔
1545

1546
  TAOS_RETURN(code);
767✔
1547
}
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