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

taosdata / TDengine / #4906

30 Dec 2025 10:52AM UTC coverage: 65.514% (+0.09%) from 65.423%
#4906

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

4080 existing lines in 123 files now uncovered.

193840 of 295877 relevant lines covered (65.51%)

120444601.14 hits per line

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

77.56
/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) {
233,780,493✔
71
  for (char c = *cmd++; c != 0; c = *cmd++) {
233,895,095✔
72
    if (c != ' ' && c != '\t' && c != ';') {
157,302,148✔
73
      return false;
157,187,546✔
74
    }
75
  }
76
  return true;
76,592,947✔
77
}
78

79
int32_t shellRunSingleCommand(char *command) {
155,243,620✔
80
  shellCmdkilled = false;
155,243,620✔
81

82
  if (shellIsEmptyCommand(command)) {
155,243,620✔
83
    return 0;
76,592,947✔
84
  }
85

86
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
78,650,673✔
87
    return -1;
28✔
88
  }
89

90
  if (shellRegexMatch(command, "^[\t ]*clear[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
78,650,645✔
91
#pragma GCC diagnostic push
92
#pragma GCC diagnostic ignored "-Wunused-result"
93
#ifndef TD_ASTRA
94
    system("clear");
×
95
#else
96
    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;]*$",
78,650,645✔
103
                      REG_EXTENDED | REG_ICASE)) {
104
    strtok(command, " \t");
604✔
105
    strtok(NULL, " \t");
604✔
106
    char *p = strtok(NULL, " \t");
604✔
107
    if (strncasecmp(p, "default", 7) == 0) {
604✔
108
      shell.args.displayWidth = SHELL_DEFAULT_MAX_BINARY_DISPLAY_WIDTH;
×
109
    } else {
110
      int32_t displayWidth = atoi(p);
604✔
111
      displayWidth = TRANGE(displayWidth, 1, 10 * 1024);
604✔
112
      shell.args.displayWidth = displayWidth;
604✔
113
    }
114
    return 0;
604✔
115
  }
116

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

136
void shellRecordCommandToHistory(char *command) {
1,177,105✔
137
  if (strncasecmp(command, "create user ", 12) == 0 || strncasecmp(command, "alter user ", 11) == 0) {
1,177,105✔
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,177,105✔
145
  if (pHistory->hstart == pHistory->hend ||
1,177,105✔
146
      pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE] == NULL ||
28✔
147
      strcmp(command, pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE]) != 0) {
28✔
148
    if (pHistory->hist[pHistory->hend] != NULL) {
1,177,105✔
149
      taosMemoryFreeClear(pHistory->hist[pHistory->hend]);
×
150
    }
151
    pHistory->hist[pHistory->hend] = taosStrdup(command);
1,177,105✔
152

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

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

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

173
  if (recordHistory) shellRecordCommandToHistory(command);
78,536,845✔
174

175
  char quote = 0, *cmd = command;
78,536,845✔
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++;
2,699✔
179
      continue;
2,699✔
180
    }
181

182
    if (quote == c) {
2,147,483,647✔
183
      quote = 0;
3,844,569✔
184
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
2,147,483,647✔
185
      quote = c;
3,844,569✔
186
    } else if (c == ';' && quote == 0) {
2,147,483,647✔
187
      c = *command;
76,706,803✔
188
      *command = 0;
76,706,803✔
189
      if (shellRunSingleCommand(cmd) < 0) {
76,706,803✔
190
        return -1;
28✔
191
      }
192
      *command = c;
76,706,775✔
193
      cmd = command;
76,706,775✔
194
    }
195
  }
196
  return shellRunSingleCommand(cmd);
78,536,817✔
197
}
198

199
char *strendG(const char *pstr) {
78,650,013✔
200
  if (pstr == NULL) {
78,650,013✔
201
    return NULL;
×
202
  }
203

204
  size_t len = strlen(pstr);
78,650,013✔
205
  if (len < 4) {
78,650,013✔
206
    return NULL;
×
207
  }
208

209
  char *p = (char *)pstr + len - 2;
78,650,013✔
210
  if (strcmp(p, "\\G") == 0) {
78,650,013✔
211
    return p;
5,101✔
212
  }
213

214
  p = (char *)pstr + len - 3;
78,644,912✔
215
  if (strcmp(p, "\\G;") == 0) {
78,644,912✔
216
    return p;
57,584✔
217
  }
218

219
  return NULL;
78,587,328✔
220
}
221

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

229
  if ((sptr = strstr(command, ">>")) != NULL) {
78,650,013✔
230
    fname = sptr + 2;
976,603✔
231
    while (*fname == ' ') fname++;
1,953,150✔
232
    *sptr = '\0';
976,603✔
233

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

240
  if ((sptr = strendG(command)) != NULL) {
78,650,013✔
241
    *sptr = '\0';
62,685✔
242
    printMode = true;  // When output to a file, the switch does not work.
62,685✔
243
  }
244

245
  st = taosGetTimestampUs();
78,650,013✔
246

247
  TAOS_RES *pSql = taos_query(shell.conn, command);
78,650,013✔
248
  if (taos_errno(pSql)) {
78,650,013✔
249
    shellPrintError(pSql, st);
54,638,901✔
250
    return;
54,638,901✔
251
  }
252

253
  if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) {
24,011,112✔
254
    printf("Database changed.\r\n\r\n");
12,324✔
255

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

259
    taos_free_result(pSql);
12,324✔
260

261
    return;
12,324✔
262
  }
263

264
  // pre string
265
  char *pre = "Query OK";
23,998,788✔
266
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
23,998,788✔
267
    pre = "Delete OK";
14✔
268
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
23,998,774✔
269
    pre = "Insert OK";
2,365✔
270
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
23,996,409✔
271
    pre = "Create OK";
2,925✔
272
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
23,993,484✔
273
    pre = "Drop OK";
370✔
274
  }
275

276
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
23,998,788✔
277
  if (pFields != NULL) {  // select and show kinds of commands
23,998,788✔
278
    int32_t error_no = 0;
22,752,343✔
279

280
    int64_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode, command);
22,752,343✔
281
    if (numOfRows < 0) return;
22,752,343✔
282

283
    et = taosGetTimestampUs();
22,752,343✔
284
    if (error_no == 0) {
22,752,343✔
285
      printf("Query OK, %" PRId64 " row(s) in set (%.6fs)\r\n", numOfRows, (et - st) / 1E6);
22,752,343✔
286
    } else {
287
      printf("Query interrupted (%s), %" PRId64 " row(s) in set (%.6fs)\r\n", tstrerror(error_no), numOfRows,
×
288
             (et - st) / 1E6);
×
289
    }
290
    taos_free_result(pSql);
22,752,343✔
291
  } else {
292
    int64_t num_rows_affacted = taos_affected_rows64(pSql);
1,246,445✔
293
    taos_free_result(pSql);
1,246,445✔
294
    et = taosGetTimestampUs();
1,246,445✔
295
    printf("%s, %" PRId64 " row(s) affected (%.6fs)\r\n", pre, num_rows_affacted, (et - st) / 1E6);
1,246,445✔
296

297
    // call auto tab
298
    callbackAutoTab(command, NULL, false);
1,246,445✔
299
  }
300

301
  printf("\r\n");
23,998,788✔
302
}
303

304
char *shellFormatTimestamp(char *buf, int32_t bufSize, int64_t val, int32_t precision) {
483,346,759✔
305
  if (shell.args.is_raw_time) {
483,346,759✔
306
    sprintf(buf, "%" PRId64, val);
×
307
    return buf;
×
308
  }
309

310
  time_t  tt;
483,314,282✔
311
  int32_t ms = 0;
483,346,759✔
312
  if (precision == TSDB_TIME_PRECISION_NANO) {
483,346,759✔
313
    tt = (time_t)(val / 1000000000);
280✔
314
    ms = val % 1000000000;
280✔
315
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
483,346,479✔
316
    tt = (time_t)(val / 1000000);
280✔
317
    ms = val % 1000000;
280✔
318
  } else {
319
    tt = (time_t)(val / 1000);
483,346,199✔
320
    ms = val % 1000;
483,346,199✔
321
  }
322

323
  if (tt <= 0 && ms < 0) {
483,346,759✔
324
    tt--;
8,580✔
325
    if (precision == TSDB_TIME_PRECISION_NANO) {
8,580✔
326
      ms += 1000000000;
×
327
    } else if (precision == TSDB_TIME_PRECISION_MICRO) {
8,580✔
328
      ms += 1000000;
×
329
    } else {
330
      ms += 1000;
8,580✔
331
    }
332
  }
333

334
  struct tm ptm = {0};
483,346,759✔
335
  if (taosLocalTime(&tt, &ptm, buf, bufSize, NULL) == NULL) {
483,346,759✔
336
    return buf;
×
337
  }
338
  size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm);
483,346,759✔
339

340
  if (precision == TSDB_TIME_PRECISION_NANO) {
483,346,759✔
341
    sprintf(buf + pos, ".%09d", ms);
280✔
342
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
483,346,479✔
343
    sprintf(buf + pos, ".%06d", ms);
280✔
344
  } else {
345
    sprintf(buf + pos, ".%03d", ms);
483,346,199✔
346
  }
347

348
  return buf;
483,346,759✔
349
}
350

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

357
  return buf;
×
358
}
359

360
void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, int32_t length, int32_t precision) {
1,892,437,100✔
361
  if (val == NULL) {
1,892,437,100✔
362
    taosFprintfFile(pFile, "NULL");
10,280,710✔
363
    return;
10,280,710✔
364
  }
365

366
  char    quotationStr[2] = {'"', 0};
1,882,156,390✔
367
  int32_t width;
368

369
  int n = 0;
1,882,156,390✔
370
#define LENGTH 64
371
  char buf[LENGTH] = {0};
1,882,156,390✔
372
  switch (field->type) {
1,882,156,390✔
373
    case TSDB_DATA_TYPE_BOOL:
526,355,625✔
374
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
526,355,625✔
375
      break;
526,355,625✔
376
    case TSDB_DATA_TYPE_TINYINT:
31,045,552✔
377
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
31,045,552✔
378
      break;
31,045,552✔
379
    case TSDB_DATA_TYPE_UTINYINT:
2,775,026✔
380
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
2,775,026✔
381
      break;
2,775,026✔
382
    case TSDB_DATA_TYPE_SMALLINT:
1,389,413✔
383
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
1,389,413✔
384
      break;
1,389,413✔
385
    case TSDB_DATA_TYPE_USMALLINT:
2,080,177✔
386
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
2,080,177✔
387
      break;
2,080,177✔
388
    case TSDB_DATA_TYPE_INT:
123,734,987✔
389
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
123,734,987✔
390
      break;
123,734,987✔
UNCOV
391
    case TSDB_DATA_TYPE_UINT:
×
UNCOV
392
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
×
UNCOV
393
      break;
×
394
    case TSDB_DATA_TYPE_BIGINT:
178,072,911✔
395
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
178,072,911✔
396
      break;
178,072,911✔
397
    case TSDB_DATA_TYPE_UBIGINT:
1,388,140✔
398
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
1,388,140✔
399
      break;
1,388,140✔
400
    case TSDB_DATA_TYPE_FLOAT:
33,123,330✔
401
      width = SHELL_FLOAT_WIDTH;
33,123,330✔
402
      if (tsEnableScience) {
33,123,330✔
403
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
404
      } else {
405
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
33,123,330✔
406
        if (n > SHELL_FLOAT_WIDTH) {
33,123,330✔
407
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
826,196✔
408
        } else {
409
          taosFprintfFile(pFile, "%s", buf);
32,297,134✔
410
        }
411
      }
412
      break;
33,123,330✔
413
    case TSDB_DATA_TYPE_DOUBLE:
289,145,495✔
414
      width = SHELL_DOUBLE_WIDTH;
289,145,495✔
415
      if (tsEnableScience) {
289,145,495✔
416
        snprintf(buf, LENGTH, "%*.15e", width, GET_DOUBLE_VAL(val));
×
417
        taosFprintfFile(pFile, "%s", buf);
×
418
      } else {
419
        n = tsnprintf(buf, LENGTH, "%*.15f", width, GET_DOUBLE_VAL(val));
289,145,495✔
420
        if (n > SHELL_DOUBLE_WIDTH) {
289,145,495✔
421
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
188,685,317✔
422
        } else {
423
          taosFprintfFile(pFile, "%s", buf);
100,460,178✔
424
        }
425
      }
426
      break;
289,145,495✔
427
    case TSDB_DATA_TYPE_BINARY:
209,002,176✔
428
    case TSDB_DATA_TYPE_NCHAR:
429
    case TSDB_DATA_TYPE_JSON: {
430
      int32_t bufIndex = 0;
209,002,176✔
431
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
209,002,176✔
432
      if (tmp == NULL) break;
209,002,176✔
433
      for (int32_t i = 0; i < length; i++) {
2,147,483,647✔
434
        tmp[bufIndex] = val[i];
2,147,483,647✔
435
        bufIndex++;
2,147,483,647✔
436
        if (val[i] == '\"') {
2,147,483,647✔
437
          tmp[bufIndex] = val[i];
×
438
          bufIndex++;
×
439
        }
440
      }
441
      tmp[bufIndex] = 0;
209,002,176✔
442

443
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
209,002,176✔
444
      taosMemoryFree(tmp);
209,002,176✔
445
    } break;
209,002,176✔
446
    case TSDB_DATA_TYPE_VARBINARY: {
×
447
      void    *tmp = NULL;
×
448
      uint32_t size = 0;
×
449
      if (taosAscii2Hex(val, length, &tmp, &size) < 0) {
×
450
        break;
×
451
      }
452
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
×
453
      taosMemoryFree(tmp);
×
454
      break;
×
455
    }
456
    case TSDB_DATA_TYPE_GEOMETRY: {
×
457
      char *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
×
458
      if (tmp == NULL) break;
×
459
      shellDumpHexValue(tmp, val, length);
×
460
      taosFprintfFile(pFile, "%s", buf);
×
461
      taosMemoryFree(tmp);
×
462
      break;
×
463
    }
464
    case TSDB_DATA_TYPE_TIMESTAMP:
163,031,761✔
465
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
163,031,761✔
466
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
163,031,761✔
467
      break;
163,031,761✔
468
    case TSDB_DATA_TYPE_DECIMAL64:
321,011,797✔
469
    case TSDB_DATA_TYPE_DECIMAL:
470
      taosFprintfFile(pFile, "%s", val);
321,011,797✔
471
      break;
321,011,797✔
472
    case TSDB_DATA_TYPE_BLOB:
×
473
    case TSDB_DATA_TYPE_MEDIUMBLOB: {
474
      void    *tmp = NULL;
×
475
      uint32_t size = 0;
×
476
      if (taosAscii2Hex(val, length, &tmp, &size) < 0) {
×
477
        break;
×
478
      }
479
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
×
480
      taosMemoryFree(tmp);
×
481

482
      break;
×
483
    }
484
    default:
×
485
      break;
×
486
  }
487
}
488

489
int64_t shellDumpResultToFile(const char *fname, TAOS_RES *tres) {
959,379✔
490
  char fullname[PATH_MAX] = {0};
959,379✔
491
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
959,379✔
492
    tstrncpy(fullname, fname, PATH_MAX);
×
493
  }
494

495
  TAOS_ROW row = taos_fetch_row(tres);
959,379✔
496
  if (row == NULL) {
959,379✔
497
    return 0;
×
498
  }
499

500
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
959,379✔
501
  if (pFile == NULL) {
959,379✔
502
    fprintf(stderr, "failed to open file: %s\r\n", fullname);
×
503
    return -1;
×
504
  }
505

506
  TAOS_FIELD *fields = taos_fetch_fields(tres);
959,379✔
507
  int32_t     num_fields = taos_num_fields(tres);
959,379✔
508
  int32_t     precision = taos_result_precision(tres);
959,379✔
509

510
  for (int32_t col = 0; col < num_fields; col++) {
2,342,027✔
511
    if (col > 0) {
1,382,648✔
512
      taosFprintfFile(pFile, ",");
423,269✔
513
    }
514
    taosFprintfFile(pFile, "%s", fields[col].name);
1,382,648✔
515
  }
516
  taosFprintfFile(pFile, "\r\n");
959,379✔
517

518
  int64_t numOfRows = 0;
959,379✔
519
  do {
520
    int32_t *length = taos_fetch_lengths(tres);
1,014,446,576✔
521
    for (int32_t i = 0; i < num_fields; i++) {
2,147,483,647✔
522
      if (i > 0) {
1,892,437,100✔
523
        taosFprintfFile(pFile, ",");
877,990,524✔
524
      }
525
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
1,892,437,100✔
526
    }
527
    taosFprintfFile(pFile, "\r\n");
1,014,446,576✔
528

529
    numOfRows++;
1,014,446,576✔
530
    row = taos_fetch_row(tres);
1,014,446,576✔
531
  } while (row != NULL);
1,014,446,576✔
532

533
  taosCloseFile(&pFile);
959,379✔
534

535
  return numOfRows;
959,379✔
536
}
537

538
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
270,147,188✔
539
  TdWchar tail[3];
270,132,498✔
540
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
270,147,188✔
541

542
  while (pos < length) {
2,147,483,647✔
543
    TdWchar wc;
2,147,483,647✔
544
    int32_t bytes = taosMbToWchar(&wc, str + pos, MB_CUR_MAX);
2,147,483,647✔
545
    if (bytes <= 0) {
2,147,483,647✔
546
      break;
37,118✔
547
    }
548

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

560
    if (w <= 0) {
2,147,483,647✔
561
      continue;
11,210✔
562
    }
563

564
    if (width <= 0) {
2,147,483,647✔
565
      printf("%lc", wc);
71,731,611✔
566
      continue;
71,731,611✔
567
    }
568

569
    totalCols += w;
2,147,483,647✔
570
    if (totalCols > width) {
2,147,483,647✔
571
      break;
779,188✔
572
    }
573
    if (totalCols <= (width - 3)) {
2,147,483,647✔
574
      printf("%lc", wc);
2,147,483,647✔
575
      cols += w;
2,147,483,647✔
576
    } else {
577
      tail[tailLen] = wc;
2,418,231✔
578
      tailLen++;
2,418,231✔
579
    }
580
  }
581

582
  if (totalCols > width) {
270,147,188✔
583
    // width could be 1 or 2, so printf("...") cannot be used
584
    for (int32_t i = 0; i < 3; i++) {
3,116,752✔
585
      if (cols >= width) {
2,337,564✔
586
        break;
×
587
      }
588
      putchar('.');
2,337,564✔
589
      ++cols;
2,337,564✔
590
    }
591
  } else {
592
    for (int32_t i = 0; i < tailLen; i++) {
269,452,735✔
593
      printf("%lc", tail[i]);
84,735✔
594
    }
595
    cols = totalCols;
269,368,000✔
596
  }
597

598
  for (; cols < width; cols++) {
2,147,483,647✔
599
    putchar(' ');
2,147,483,647✔
600
  }
601
}
270,147,188✔
602

603
void shellPrintString(const char *str, int32_t width) {
1,139,836,707✔
604
  int32_t len = strlen(str);
1,139,836,707✔
605

606
  if (width == 0) {
1,139,836,707✔
607
    printf("%s", str);
×
608
  } else if (len > width) {
1,139,836,707✔
609
    if (width <= 3) {
18,765✔
610
      printf("%.*s.", width - 1, str);
18,765✔
611
    } else {
612
      printf("%.*s...", width - 3, str);
×
613
    }
614
  } else {
615
    printf("%s%*.s", str, width - len, "");
1,139,817,942✔
616
  }
617
}
1,139,836,707✔
618

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

625
  int32_t code = TSDB_CODE_FAILED;
×
626

627
  code = initCtxAsText();
×
628
  if (code != TSDB_CODE_SUCCESS) {
×
629
    shellPrintString(getGeosErrMsg(code), width);
×
630
    return;
×
631
  }
632

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

640
  shellPrintString(outputWKT, width);
×
641

642
  geosFreeBuffer(outputWKT);
×
643
}
644

645
void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision) {
2,147,483,647✔
646
  if (val == NULL) {
2,147,483,647✔
647
    shellPrintString(TSDB_DATA_NULL_STR, width);
1,031,075,259✔
648
    return;
1,031,075,259✔
649
  }
650

651
  int n = 0;
1,591,180,975✔
652
#define LENGTH 64
653
  char buf[LENGTH] = {0};
1,591,180,975✔
654
  switch (field->type) {
1,591,180,975✔
655
    case TSDB_DATA_TYPE_BOOL:
108,761,448✔
656
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
108,761,448✔
657
      break;
108,761,448✔
658
    case TSDB_DATA_TYPE_TINYINT:
52,600,913✔
659
      printf("%*d", width, *((int8_t *)val));
52,600,913✔
660
      break;
52,600,913✔
661
    case TSDB_DATA_TYPE_UTINYINT:
89,096,954✔
662
      printf("%*u", width, *((uint8_t *)val));
89,096,954✔
663
      break;
89,096,954✔
664
    case TSDB_DATA_TYPE_SMALLINT:
40,307,300✔
665
      printf("%*d", width, *((int16_t *)val));
40,307,300✔
666
      break;
40,307,300✔
667
    case TSDB_DATA_TYPE_USMALLINT:
97,049,512✔
668
      printf("%*u", width, *((uint16_t *)val));
97,049,512✔
669
      break;
97,049,512✔
670
    case TSDB_DATA_TYPE_INT:
98,329,616✔
671
      printf("%*d", width, *((int32_t *)val));
98,329,616✔
672
      break;
98,329,616✔
673
    case TSDB_DATA_TYPE_UINT:
72,909,215✔
674
      printf("%*u", width, *((uint32_t *)val));
72,909,215✔
675
      break;
72,909,215✔
676
    case TSDB_DATA_TYPE_BIGINT:
172,556,737✔
677
      printf("%*" PRId64, width, taosGetInt64Aligned((int64_t *)val));
172,556,737✔
678
      break;
172,556,737✔
679
    case TSDB_DATA_TYPE_UBIGINT:
77,535,022✔
680
      printf("%*" PRIu64, width, taosGetUInt64Aligned((uint64_t *)val));
77,535,022✔
681
      break;
77,535,022✔
682
    case TSDB_DATA_TYPE_FLOAT:
65,574,565✔
683
      width = width >= LENGTH ? LENGTH - 1 : width;
65,574,565✔
684
      if (tsEnableScience) {
65,574,565✔
685
        printf("%*.7e", width, taosGetFloatAligned((float *)val));
×
686
      } else {
687
        snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, taosGetFloatAligned((float *)val));
65,574,565✔
688
        printf("%s", buf);
65,574,565✔
689
      }
690
      break;
65,574,565✔
691
    case TSDB_DATA_TYPE_DOUBLE:
125,997,479✔
692
      width = width >= LENGTH ? LENGTH - 1 : width;
125,997,479✔
693
      if (tsEnableScience) {
125,997,479✔
694
        snprintf(buf, LENGTH, "%*.15e", width, taosGetDoubleAligned((double *)val));
×
695
        printf("%s", buf);
×
696
      } else {
697
        snprintf(buf, LENGTH, "%*.*g", width, DBL_DIG, taosGetDoubleAligned((double *)val));
125,997,479✔
698
        printf("%*s", width, buf);
125,997,479✔
699
      }
700
      break;
125,997,479✔
701
    case TSDB_DATA_TYPE_VARBINARY: {
×
702
      void    *data = NULL;
×
703
      uint32_t size = 0;
×
704
      if (taosAscii2Hex(val, length, &data, &size) < 0) {
×
705
        break;
×
706
      }
707
      shellPrintNChar(data, size, width);
×
708
      taosMemoryFree(data);
×
709
      break;
×
710
    }
711
    case TSDB_DATA_TYPE_BINARY:
270,147,188✔
712
    case TSDB_DATA_TYPE_NCHAR:
713
    case TSDB_DATA_TYPE_JSON:
714
      shellPrintNChar(val, length, width);
270,147,188✔
715
      break;
270,147,188✔
716
    case TSDB_DATA_TYPE_GEOMETRY:
×
717
      shellPrintGeometry(val, length, width);
×
718
      break;
×
719
    case TSDB_DATA_TYPE_TIMESTAMP:
320,314,998✔
720
      shellFormatTimestamp(buf, sizeof(buf), taosGetInt64Aligned((int64_t *)val), precision);
320,314,998✔
721
      printf("%s", buf);
320,314,998✔
722
      break;
320,314,998✔
723

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

743
// show whole result for this query return true, like limit or describe
744
bool shellIsShowWhole(const char *sql) {
×
745
  // limit
746
  if (taosStrCaseStr(sql, " limit ") != NULL) {
×
747
    return true;
×
748
  }
749
  // describe
750
  if (taosStrCaseStr(sql, "describe ") != NULL) {
×
751
    return true;
×
752
  }
753
  // desc
754
  if (taosStrCaseStr(sql, "desc ") != NULL) {
×
755
    return true;
×
756
  }
757
  // show
758
  if (taosStrCaseStr(sql, "show ") != NULL) {
×
759
    return true;
×
760
  }
761
  // explain
762
  if (taosStrCaseStr(sql, "explain ") != NULL) {
×
763
    return true;
×
764
  }
765

766
  return false;
×
767
}
768

769
bool shellIsShowQuery(const char *sql) {
×
770
  // todo refactor
771
  if (taosStrCaseStr(sql, "show ") != NULL) {
×
772
    return true;
×
773
  }
774

775
  return false;
×
776
}
777

778
void init_dump_info(tsDumpInfo *dump_info, TAOS_RES *tres, const char *sql, bool vertical) {
21,792,964✔
779
  dump_info->sql = sql;
21,792,964✔
780
  dump_info->vertical = vertical;
21,792,964✔
781
  tsem_init(&dump_info->sem, 0, 0);
21,792,964✔
782
  dump_info->numOfAllRows = 0;
21,792,964✔
783

784
  dump_info->numFields = taos_num_fields(tres);
21,792,964✔
785
  dump_info->fields = taos_fetch_fields(tres);
21,792,964✔
786
  dump_info->precision = taos_result_precision(tres);
21,792,964✔
787

788
  dump_info->resShowMaxNum = UINT64_MAX;
21,792,964✔
789

790
  if (shell.args.commands == NULL && shell.args.file[0] == 0 && !shellIsShowWhole(dump_info->sql)) {
21,792,964✔
791
    dump_info->resShowMaxNum = SHELL_DEFAULT_RES_SHOW_NUM;
×
792
  }
793

794
  if (vertical) {
21,792,964✔
795
    dump_info->maxColNameLen = 0;
62,685✔
796
    for (int32_t col = 0; col < dump_info->numFields; col++) {
130,579✔
797
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
67,894✔
798
      if (len > dump_info->maxColNameLen) {
67,894✔
799
        dump_info->maxColNameLen = len;
64,201✔
800
      }
801
    }
802
  } else {
803
    for (int32_t col = 0; col < dump_info->numFields; col++) {
75,285,662✔
804
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
53,555,383✔
805
    }
806
    // check create token and set width
807
    if (shellRegexMatch(sql, "^[\t ]*create[ \t]+token[ \t]+.*", REG_EXTENDED | REG_ICASE)) {
21,730,279✔
808
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOKEN_DISPLAY_WIDTH);
×
809
    }
810
  }
811
}
21,792,964✔
812

813
void shellVerticalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
62,685✔
814
  TAOS_ROW row = taos_fetch_row(tres);
62,685✔
815
  if (row == NULL) {
62,685✔
816
    printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
817
    return;
×
818
  }
819

820
  int64_t numOfPintRows = dump_info->numOfAllRows;
62,685✔
821
  int     numOfPrintRowsThisOne = 0;
62,685✔
822

823
  while (row != NULL) {
1,235,090✔
824
    printf("*************************** %" PRId64 ".row ***************************\r\n", numOfPintRows + 1);
1,235,090✔
825

826
    int32_t *length = taos_fetch_lengths(tres);
1,235,090✔
827

828
    for (int32_t i = 0; i < dump_info->numFields; i++) {
2,477,133✔
829
      TAOS_FIELD *field = dump_info->fields + i;
1,242,043✔
830

831
      int32_t padding = (int32_t)(dump_info->maxColNameLen - strlen(field->name));
1,242,043✔
832
      printf("%*.s%s: ", padding, " ", field->name);
1,242,043✔
833

834
      shellPrintField((const char *)row[i], field, 0, length[i], dump_info->precision);
1,242,043✔
835
      putchar('\r');
1,242,043✔
836
      putchar('\n');
1,242,043✔
837
    }
838

839
    numOfPintRows++;
1,235,090✔
840
    numOfPrintRowsThisOne++;
1,235,090✔
841

842
    if (numOfPintRows == dump_info->resShowMaxNum) {
1,235,090✔
843
      printf("\r\n");
×
844
      printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
×
845
      printf("         You can use the `LIMIT` clause to get fewer result to show.\r\n");
×
846
      printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
847
      printf("\r\n");
×
848
      printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
×
849
      printf("\r\n");
×
850
      return;
×
851
    }
852

853
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
1,235,090✔
854
      return;
62,685✔
855
    }
856

857
    row = taos_fetch_row(tres);
1,172,405✔
858
  }
859
  return;
×
860
}
861

862
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
53,555,383✔
863
  int32_t width = (int32_t)strlen(field->name);
53,555,383✔
864

865
  switch (field->type) {
53,555,383✔
866
    case TSDB_DATA_TYPE_NULL:
×
867
      return TMAX(4, width);  // null
×
868
    case TSDB_DATA_TYPE_BOOL:
2,446,673✔
869
      return TMAX(5, width);  // 'false'
2,446,673✔
870

871
    case TSDB_DATA_TYPE_TINYINT:
2,558,012✔
872
    case TSDB_DATA_TYPE_UTINYINT:
873
      return TMAX(4, width);  // '-127'
2,558,012✔
874

875
    case TSDB_DATA_TYPE_SMALLINT:
2,290,461✔
876
    case TSDB_DATA_TYPE_USMALLINT:
877
      return TMAX(6, width);  // '-32767'
2,290,461✔
878

879
    case TSDB_DATA_TYPE_INT:
11,767,416✔
880
    case TSDB_DATA_TYPE_UINT:
881
      return TMAX(11, width);  // '-2147483648'
11,767,416✔
882

883
    case TSDB_DATA_TYPE_BIGINT:
9,381,258✔
884
    case TSDB_DATA_TYPE_UBIGINT:
885
      return TMAX(21, width);  // '-9223372036854775807'
9,381,258✔
886

887
    case TSDB_DATA_TYPE_FLOAT:
1,509,177✔
888
      return TMAX(SHELL_FLOAT_WIDTH, width);
1,509,177✔
889

890
    case TSDB_DATA_TYPE_DOUBLE:
12,468,346✔
891
      return TMAX(SHELL_DOUBLE_WIDTH, width);
12,468,346✔
892

893
    case TSDB_DATA_TYPE_BINARY:
4,278,844✔
894
    case TSDB_DATA_TYPE_GEOMETRY:
895
      if (field->bytes > shell.args.displayWidth) {
4,278,844✔
896
        return TMAX(shell.args.displayWidth, width);
2,807,376✔
897
      } else {
898
        return TMAX(field->bytes + 2, width);
1,471,468✔
899
      }
900
    case TSDB_DATA_TYPE_VARBINARY: {
168✔
901
      int32_t bytes = field->bytes * 2 + 2;
168✔
902
      if (bytes > shell.args.displayWidth) {
168✔
903
        return TMAX(shell.args.displayWidth, width);
×
904
      } else {
905
        return TMAX(bytes + 2, width);
168✔
906
      }
907
    }
908
    case TSDB_DATA_TYPE_NCHAR:
2,463,876✔
909
    case TSDB_DATA_TYPE_JSON: {
910
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
2,463,876✔
911
      if (bytes > shell.args.displayWidth) {
2,463,876✔
912
        return TMAX(shell.args.displayWidth, width);
2,463,876✔
913
      } else {
914
        return TMAX(bytes + 2, width);
×
915
      }
916
    }
917

918
    case TSDB_DATA_TYPE_TIMESTAMP:
4,391,124✔
919
      if (shell.args.is_raw_time) {
4,391,124✔
920
        return TMAX(14, width);
42✔
921
      }
922
      if (precision == TSDB_TIME_PRECISION_NANO) {
4,391,082✔
923
        return TMAX(29, width);
28✔
924
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
4,391,054✔
925
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
28✔
926
      } else {
927
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
4,391,026✔
928
      }
929
    case TSDB_DATA_TYPE_BLOB:
×
930
    case TSDB_DATA_TYPE_MEDIUMBLOB: {
931
      int32_t bytes = TSDB_MAX_BLOB_LEN;
×
932
      if (bytes > shell.args.displayWidth) {
×
933
        return TMAX(shell.args.displayWidth, width);
×
934
      } else {
935
        return TMAX(bytes + 2, width);
×
936
      }
937
    } break;
938

939
    case TSDB_DATA_TYPE_DECIMAL64:
14✔
940
      return TMAX(width, 20);
14✔
941
    case TSDB_DATA_TYPE_DECIMAL:
14✔
942
      return TMAX(width, 40);
14✔
943
    default:
×
944
      ASSERT(false);
×
945
  }
946

947
  return 0;
×
948
}
949

950
void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields) {
13,801,643✔
951
  int32_t rowWidth = 0;
13,801,643✔
952
  for (int32_t col = 0; col < num_fields; col++) {
51,626,977✔
953
    TAOS_FIELD *field = fields + col;
37,825,334✔
954
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
37,825,334✔
955
    int32_t     left = padding / 2;
37,825,334✔
956
    printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
37,825,334✔
957
    rowWidth += width[col] + 3;
37,825,334✔
958
  }
959

960
  putchar('\r');
13,801,643✔
961
  putchar('\n');
13,801,643✔
962
  for (int32_t i = 0; i < rowWidth; i++) {
1,315,746,087✔
963
    putchar('=');
1,301,944,444✔
964
  }
965
  putchar('\r');
13,801,643✔
966
  putchar('\n');
13,801,643✔
967
}
13,801,643✔
968

969
void shellHorizontalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
13,926,352✔
970
  TAOS_ROW row = taos_fetch_row(tres);
13,926,352✔
971
  if (row == NULL) {
13,926,352✔
972
    printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
973
    return;
×
974
  }
975

976
  int64_t numOfPintRows = dump_info->numOfAllRows;
13,926,352✔
977
  int     numOfPrintRowsThisOne = 0;
13,926,352✔
978
  if (numOfPintRows == 0) {
13,926,352✔
979
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
13,801,643✔
980
  }
981

982
  while (row != NULL) {
355,175,449✔
983
    int32_t *length = taos_fetch_lengths(tres);
355,175,449✔
984
    for (int32_t i = 0; i < dump_info->numFields; i++) {
2,147,483,647✔
985
      putchar(' ');
2,147,483,647✔
986
      shellPrintField((const char *)row[i], dump_info->fields + i, dump_info->width[i], length[i],
2,147,483,647✔
987
                      dump_info->precision);
988
      putchar(' ');
2,147,483,647✔
989
      putchar('|');
2,147,483,647✔
990
    }
991
    putchar('\r');
355,175,449✔
992
    putchar('\n');
355,175,449✔
993

994
    numOfPintRows++;
355,175,449✔
995
    numOfPrintRowsThisOne++;
355,175,449✔
996

997
    if (numOfPintRows == dump_info->resShowMaxNum) {
355,175,449✔
998
      printf("\r\n");
×
999
      printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
×
1000
      if (shellIsShowQuery(dump_info->sql)) {
×
1001
        printf("         You can use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
1002
      } else {
1003
        printf("         You can use the `LIMIT` clause to get fewer result to show.\r\n");
×
1004
        printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
1005
      }
1006
      printf("\r\n");
×
1007
      printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
×
1008
      printf("\r\n");
×
1009
      return;
×
1010
    }
1011

1012
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
355,175,449✔
1013
      return;
13,926,352✔
1014
    }
1015

1016
    row = taos_fetch_row(tres);
341,249,097✔
1017
  }
1018
  return;
×
1019
}
1020

1021
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
35,782,001✔
1022
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
35,782,001✔
1023
  if (num_of_rows > 0) {
35,782,001✔
1024
    dump_info->numOfRows = num_of_rows;
13,989,037✔
1025
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
13,989,037✔
1026
      if (dump_info->vertical) {
13,989,037✔
1027
        shellVerticalPrintResult(tres, dump_info);
62,685✔
1028
      } else {
1029
        shellHorizontalPrintResult(tres, dump_info);
13,926,352✔
1030
      }
1031
    }
1032
    dump_info->numOfAllRows += num_of_rows;
13,989,037✔
1033
    if (!shellCmdkilled) {
13,989,037✔
1034
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
13,989,037✔
1035
    } else {
1036
      tsem_post(&dump_info->sem);
×
1037
    }
1038
  } else {
1039
    if (num_of_rows < 0) {
21,792,964✔
1040
      printf("\033[31masync retrieve failed, code: %d, %s\033[0m\n", num_of_rows, tstrerror(num_of_rows));
×
1041
    }
1042
    tsem_post(&dump_info->sem);
21,792,964✔
1043
  }
1044
}
35,782,001✔
1045

1046
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
22,752,343✔
1047
  int64_t num_of_rows = 0;
22,752,343✔
1048
  if (fname != NULL) {
22,752,343✔
1049
    num_of_rows = shellDumpResultToFile(fname, tres);
959,379✔
1050
  } else {
1051
    tsDumpInfo dump_info;
21,791,022✔
1052
    if (!shellCmdkilled) {
21,792,964✔
1053
      init_dump_info(&dump_info, tres, sql, vertical);
21,792,964✔
1054
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
21,792,964✔
1055
      tsem_wait(&dump_info.sem);
21,792,964✔
1056
      num_of_rows = dump_info.numOfAllRows;
21,792,964✔
1057
    }
1058
  }
1059

1060
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
22,752,343✔
1061
  return num_of_rows;
22,752,343✔
1062
}
1063

1064
void shellReadHistory() {
1,177,105✔
1065
  SShellHistory *pHistory = &shell.history;
1,177,105✔
1066
  TdFilePtr      pFile = taosOpenFile(pHistory->file, TD_FILE_READ | TD_FILE_STREAM);
1,177,105✔
1067
  if (pFile == NULL) return;
1,177,105✔
1068

1069
  char   *line = taosMemoryMalloc(tsMaxSQLLength + 1);
1,142,185✔
1070
  int32_t read_size = 0;
1,142,185✔
1071
  while ((read_size = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
263,223,722✔
1072
    line[read_size - 1] = '\0';
262,081,537✔
1073
    taosMemoryFree(pHistory->hist[pHistory->hend]);
262,081,537✔
1074
    pHistory->hist[pHistory->hend] = taosStrdup(line);
262,081,537✔
1075

1076
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
262,081,537✔
1077

1078
    if (pHistory->hend == pHistory->hstart) {
262,081,537✔
1079
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
×
1080
    }
1081
  }
1082

1083
  taosMemoryFreeClear(line);
1,142,185✔
1084
  taosCloseFile(&pFile);
1,142,185✔
1085
  int64_t file_size;
1,140,285✔
1086
  if (taosStatFile(pHistory->file, &file_size, NULL, NULL) == 0 && file_size > SHELL_MAX_COMMAND_SIZE) {
1,142,185✔
1087
    TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_TRUNC);
×
1088
    if (pFile == NULL) return;
×
1089
    int32_t endIndex = pHistory->hstart;
×
1090
    if (endIndex != 0) {
×
1091
      endIndex = pHistory->hend;
×
1092
    }
1093
    for (int32_t i = (pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE; i != endIndex;) {
×
1094
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
×
1095
      i = (i + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE;
×
1096
    }
1097
    taosFprintfFile(pFile, "%s\n", pHistory->hist[endIndex]);
×
1098

1099
    /* coverity[+retval] */
1100
    taosFsyncFile(pFile);
×
1101
    taosCloseFile(&pFile);
×
1102
  }
1103
  pHistory->hstart = pHistory->hend;
1,142,185✔
1104
}
1105

1106
void shellWriteHistory() {
1,177,105✔
1107
  SShellHistory *pHistory = &shell.history;
1,177,105✔
1108
  if (pHistory->hend == pHistory->hstart) return;
1,177,105✔
1109
  TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_APPEND);
1,177,077✔
1110
  if (pFile == NULL) return;
1,177,077✔
1111

1112
  for (int32_t i = pHistory->hstart; i != pHistory->hend;) {
2,354,182✔
1113
    if (pHistory->hist[i] != NULL) {
1,177,105✔
1114
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
1,177,105✔
1115
      taosMemoryFree(pHistory->hist[i]);
1,177,105✔
1116
      pHistory->hist[i] = NULL;
1,177,105✔
1117
    }
1118
    i = (i + 1) % SHELL_MAX_HISTORY_SIZE;
1,177,105✔
1119
  }
1120
  taosCloseFile(&pFile);
1,177,077✔
1121
}
1122

1123
void shellCleanupHistory() {
1,177,105✔
1124
  SShellHistory *pHistory = &shell.history;
1,177,105✔
1125
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
1,178,282,105✔
1126
    if (pHistory->hist[i] != NULL) {
1,177,105,000✔
1127
      taosMemoryFree(pHistory->hist[i]);
262,081,537✔
1128
      pHistory->hist[i] = NULL;
262,081,537✔
1129
    }
1130
  }
1131
}
1,177,105✔
1132

1133
void shellPrintError(TAOS_RES *tres, int64_t st) {
54,638,901✔
1134
  int code = taos_errno(tres);
54,638,901✔
1135
  int64_t et = taosGetTimestampUs();
54,638,901✔
1136
  printf("\r\nDB error: %s [0x%08X] (%.6fs)\r\n", taos_errstr(tres), code, (et - st) / 1E6);
54,638,901✔
1137
  taos_free_result(tres);
54,638,901✔
1138

1139
  // tip
1140
  if (code == TSDB_CODE_MND_USER_PASSWORD_EXPIRED) {
54,638,901✔
1141
    fprintf(stdout, "******************** TIPS ********************\n");
13✔
1142
    fprintf(stdout, "Please reset your password using the `ALTER USER <user_name> PASS 'new_password'` command.\n");
13✔
1143
    fprintf(stdout, "**********************************************\n");
13✔
1144
  }
1145
}
54,638,901✔
1146

1147
bool shellIsCommentLine(char *line) {
77,439,288✔
1148
  if (line == NULL) return true;
77,439,288✔
1149
  return shellRegexMatch(line, "^\\s*#.*", REG_EXTENDED);
77,439,288✔
1150
}
1151

1152
void shellSourceFile(const char *file) {
79,548✔
1153
  int32_t read_len = 0;
79,548✔
1154
  char   *cmd = taosMemoryCalloc(1, tsMaxSQLLength + 1);
79,548✔
1155
  size_t  cmd_len = 0;
79,548✔
1156
  char    fullname[PATH_MAX] = {0};
79,548✔
1157
  char    sourceFileCommand[PATH_MAX + 8] = {0};
79,548✔
1158

1159
  if (taosExpandDir(file, fullname, PATH_MAX) != 0) {
79,548✔
1160
    tstrncpy(fullname, file, PATH_MAX);
×
1161
  }
1162

1163
  sprintf(sourceFileCommand, "source %s;", fullname);
79,548✔
1164
  shellRecordCommandToHistory(sourceFileCommand);
79,548✔
1165

1166
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_READ | TD_FILE_STREAM);
79,548✔
1167
  if (pFile == NULL) {
79,548✔
1168
    fprintf(stderr, "failed to open file %s\r\n", fullname);
18,492✔
1169
    taosMemoryFree(cmd);
18,492✔
1170
    return;
18,492✔
1171
  }
1172

1173
  char *line = taosMemoryMalloc(tsMaxSQLLength + 1);
61,056✔
1174
  while ((read_len = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
77,993,686✔
1175
    if (cmd_len + read_len >= tsMaxSQLLength) {
77,932,630✔
1176
      printf("read command line too long over 1M, ignore this line. cmd_len = %d read_len=%d \n", (int32_t)cmd_len,
×
1177
             read_len);
1178
      cmd_len = 0;
×
1179
      memset(line, 0, tsMaxSQLLength + 1);
×
1180
      continue;
×
1181
    }
1182
    line[--read_len] = '\0';
77,932,630✔
1183

1184
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
77,932,630✔
1185
      continue;
493,342✔
1186
    }
1187

1188
    if (line[read_len - 1] == '\\') {
77,439,288✔
1189
      line[read_len - 1] = ' ';
×
1190
      memcpy(cmd + cmd_len, line, read_len);
×
1191
      cmd_len += read_len;
×
1192
      continue;
×
1193
    }
1194

1195
    if (line[read_len - 1] == '\r') {
77,439,288✔
1196
      line[read_len - 1] = ' ';
×
1197
    }
1198

1199
    memcpy(cmd + cmd_len, line, read_len);
77,439,288✔
1200
    printf("%s%s\r\n", shell.info.promptHeader, cmd);
77,439,288✔
1201
    shellRunCommand(cmd, false);
77,439,288✔
1202
    memset(cmd, 0, tsMaxSQLLength);
77,439,288✔
1203
    cmd_len = 0;
77,439,288✔
1204
  }
1205

1206
  taosMemoryFree(cmd);
61,056✔
1207
  taosMemoryFreeClear(line);
61,056✔
1208
  taosCloseFile(&pFile);
61,056✔
1209
}
1210

1211
int32_t shellGetGrantInfo(char *buf) {
205✔
1212
  int32_t verType = TSDB_VERSION_UNKNOWN;
205✔
1213
  char    sinfo[256] = {0};
205✔
1214
  tstrncpy(sinfo, taos_get_server_info(shell.conn), sizeof(sinfo));
205✔
1215
  strtok(sinfo, "\r\n");
205✔
1216

1217
#ifndef TD_ASTRA
1218
  char sql[] = "show grants";
205✔
1219

1220
  TAOS_RES *tres = taos_query(shell.conn, sql);
205✔
1221

1222
  int32_t code = taos_errno(tres);
205✔
1223
  if (code != TSDB_CODE_SUCCESS) {
205✔
1224
    if (code != TSDB_CODE_OPS_NOT_SUPPORT && code != TSDB_CODE_MND_NO_RIGHTS &&
×
1225
        code != TSDB_CODE_PAR_PERMISSION_DENIED) {
1226
      fprintf(stderr, "Failed to check Server Edition, Reason:0x%04x:%s\r\n\r\n", code, taos_errstr(tres));
×
1227
    }
1228
    taos_free_result(tres);
×
1229
    return verType;
×
1230
  }
1231

1232
  int32_t num_fields = taos_field_count(tres);
205✔
1233
  if (num_fields == 0) {
205✔
1234
    fprintf(stderr, "\r\nInvalid grant information.\r\n");
×
1235
    exit(0);
×
1236
  } else {
1237
    if (tres == NULL) {
205✔
1238
      fprintf(stderr, "\r\nGrant information is null.\r\n");
×
1239
      exit(0);
×
1240
    }
1241

1242
    TAOS_FIELD *fields = taos_fetch_fields(tres);
205✔
1243
    TAOS_ROW    row = taos_fetch_row(tres);
205✔
1244
    if (row == NULL) {
205✔
1245
      fprintf(stderr, "\r\nFailed to get grant information from server. Abort.\r\n");
×
1246
      exit(0);
×
1247
    }
1248
    char serverVersion[64] = {0};
205✔
1249
    char expiretime[32] = {0};
205✔
1250
    char expired[32] = {0};
205✔
1251

1252
    tstrncpy(serverVersion, row[0], 64);
205✔
1253
    memcpy(expiretime, row[1], fields[1].bytes);
205✔
1254
    memcpy(expired, row[2], fields[2].bytes);
205✔
1255

1256
    trimStr(serverVersion, "trial");
205✔
1257

1258
    if (strcmp(serverVersion, "community") == 0) {
205✔
1259
      verType = TSDB_VERSION_OSS;
×
1260
    } else if (strcmp(expiretime, "unlimited") == 0) {
205✔
1261
      verType = TSDB_VERSION_ENTERPRISE;
×
1262
      sprintf(buf, "Server is %s %s. License will never expire.\r\n", serverVersion, sinfo);
×
1263
    } else {
1264
      verType = TSDB_VERSION_ENTERPRISE;
205✔
1265
      sprintf(buf, "Server is %s %s. License will expire at %s.\r\n", serverVersion, sinfo, expiretime);
205✔
1266
    }
1267

1268
    taos_free_result(tres);
205✔
1269
  }
1270

1271
  fprintf(stdout, "\r\n");
205✔
1272
#else
1273
  verType = TSDB_VERSION_ENTERPRISE;
1274
  sprintf(buf, "Server is %s %s. License will never expire.\r\n", TD_PRODUCT_NAME, sinfo);
1275
#endif
1276
  return verType;
205✔
1277
}
1278

1279
#ifdef WINDOWS
1280
BOOL shellQueryInterruptHandler(DWORD fdwCtrlType) {
1281
  tsem_post(&shell.cancelSem);
1282
  return TRUE;
1283
}
1284
#else
1285
void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context) { tsem_post(&shell.cancelSem); }
410✔
1286
#endif
1287

1288
void shellCleanup(void *arg) { taosResetTerminalMode(); }
205✔
1289

1290
void *shellCancelHandler(void *arg) {
205✔
1291
  setThreadName("shellCancelHandler");
205✔
1292
  while (1) {
1293
    if (shell.exit == true) {
615✔
1294
      break;
205✔
1295
    }
1296

1297
    if (tsem_wait(&shell.cancelSem) != 0) {
410✔
1298
      taosMsleep(10);
×
1299
      continue;
×
1300
    }
1301

1302
    if (shell.conn) {
410✔
1303
      shellCmdkilled = true;
205✔
1304
      taos_kill_query(shell.conn);
205✔
1305
    }
1306

1307
#ifdef WINDOWS
1308
    printf("\n%s", shell.info.promptHeader);
1309
#endif
1310
  }
1311

1312
  return NULL;
205✔
1313
}
1314

1315
#pragma GCC diagnostic push
1316
#pragma GCC diagnostic ignored "-Wstringop-overflow"
1317

1318
void *shellThreadLoop(void *arg) {
205✔
1319
  setThreadName("shellThreadLoop");
205✔
1320
  taosGetOldTerminalMode();
205✔
1321
  taosThreadCleanupPush(shellCleanup, NULL);
205✔
1322

1323
  do {
1324
    char *command = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE);
205✔
1325
    if (command == NULL) {
205✔
1326
      printf("failed to malloc command\r\n");
×
1327
      break;
×
1328
    }
1329

1330
    do {
1331
      memset(command, 0, SHELL_MAX_COMMAND_SIZE);
410✔
1332
      taosSetTerminalMode();
410✔
1333

1334
      if (shellReadCommand(command) != 0) {
410✔
1335
        break;
205✔
1336
      }
1337

1338
      taosResetTerminalMode();
205✔
1339
    } while (shellRunCommand(command, true) == 0);
205✔
1340

1341
    taosMemoryFreeClear(command);
205✔
1342
    shellWriteHistory();
205✔
1343
    shellExit();
205✔
1344
  } while (0);
1345

1346
  taosThreadCleanupPop(1);
205✔
1347
  return NULL;
205✔
1348
}
1349

1350
bool inputTotpCode(char *totpCode) {
13✔
1351
  bool ret = true;
13✔
1352
  printf("Please enter your TOTP code:");
13✔
1353
  if (scanf("%255s", totpCode) != 1) {
13✔
1354
    fprintf(stderr, "TOTP code reading error\n");
×
1355
    ret = false;
×
1356
  }
1357
  if (EOF == getchar()) {
13✔
1358
    // tip
1359
    fprintf(stdout, "getchar() return EOF\r\n");    
×
1360
  }
1361
  return ret;
13✔
1362
}
1363

1364
#pragma GCC diagnostic pop
1365

1366
TAOS *createConnect(SShellArgs *pArgs) {
1,177,913✔
1367
  char     show[256] = "\0";
1,177,913✔
1368
  char    *host = NULL;
1,177,913✔
1369
  uint16_t port = 0;
1,177,913✔
1370
  char    *user = NULL;
1,177,913✔
1371
  char    *pwd = NULL;
1,177,913✔
1372
  TAOS    *taos = NULL;
1,177,913✔
1373

1374
  // set mode
1375
  if (pArgs->connMode != CONN_MODE_NATIVE && pArgs->dsn) {
1,177,913✔
1376
    // websocket
1377
    memcpy(show, pArgs->dsn, 20);
336✔
1378
    memcpy(show + 20, "...", 3);
336✔
1379
    memcpy(show + 23, pArgs->dsn + strlen(pArgs->dsn) - 10, 10);
336✔
1380

1381
    // connect dsn
1382
    taos = taos_connect_with_dsn(pArgs->dsn);
336✔
1383
  } else {
1384
    host = (char *)pArgs->host;
1,177,577✔
1385
    user = (char *)pArgs->user;
1,177,577✔
1386
    pwd = pArgs->password;
1,177,577✔
1387

1388
    if (pArgs->port_inputted) {
1,177,577✔
1389
      port = pArgs->port;
568✔
1390
    } else {
1391
      port = defaultPort(pArgs->connMode, pArgs->dsn);
1,177,009✔
1392
    }
1393

1394
    sprintf(show, "host:%s port:%d ", host, port);
1,177,577✔
1395

1396
    // connect normal
1397
    if (pArgs->auth) {
1,177,577✔
1398
      taos = taos_connect_auth(host, user, pArgs->auth, pArgs->database, port);
107✔
1399
    } else {
1400
#ifdef TD_ENTERPRISE 
1401
      if (strlen(pArgs->token) > 0) {
1,177,470✔
1402
        // token
1403
        printf("Connect with token ...");
52✔
1404
        taos = taos_connect_token(host, pArgs->token, pArgs->database, port);
52✔
1405
        if (taos != NULL) {
52✔
1406
          printf("... [ OK ]\n");
26✔
1407
          return taos;
26✔
1408
        }
1409
        printf("... [ FAILED ]\n");
26✔
1410
        return NULL;
26✔
1411
      }
1412
#endif      
1413
      taos = taos_connect(host, user, pwd, pArgs->database, port);
1,177,418✔
1414
    }
1415

1416
    if (taos == NULL) {
1,177,525✔
1417
      // failed
1418
      int code = taos_errno(NULL);
739✔
1419
      if (code == TSDB_CODE_MND_WRONG_TOTP_CODE) {
739✔
1420
         // totp
1421
        char totpCode[TSDB_USER_PASSWORD_LONGLEN];
×
1422
        memset(totpCode, 0, sizeof(totpCode));  
13✔
1423
        if (inputTotpCode(totpCode)) {
13✔
1424
          printf("Connect with TOTP code:%s ...", totpCode);
13✔
1425
          taos = taos_connect_totp(host, user, pwd, totpCode, pArgs->database, port);
13✔
1426
          if (taos != NULL) {
13✔
1427
            printf("... [ OK ]\n");
13✔
1428
            return taos;
13✔
1429
          }
1430
          printf("... [ FAILED ]\n");
×
1431
          return NULL;
×
1432
        }
1433
      }
1434
      // token
1435
    }
1436
  }
1437

1438
  return taos;
1,177,848✔
1439
}
1440

1441
int32_t shellExecute(int argc, char *argv[]) {
1,177,913✔
1442
  int32_t code = 0;
1,177,913✔
1443
  printf(shell.info.clientVersion, shell.info.cusName,
2,355,826✔
1444
         workingMode(shell.args.connMode, shell.args.dsn) == CONN_MODE_NATIVE ? STR_NATIVE : STR_WEBSOCKET,
1,177,913✔
1445
         taos_get_client_info(), shell.info.cusName);
1446
  fflush(stdout);
1,177,913✔
1447

1448
  SShellArgs *pArgs = &shell.args;
1,177,913✔
1449
  shell.conn = createConnect(pArgs);
1,177,913✔
1450

1451
  if (shell.conn == NULL) {
1,177,913✔
1452
    printf("failed to connect to server, reason: %s [0x%08X]\n%s", taos_errstr(NULL), taos_errno(NULL),
808✔
1453
           ERROR_CODE_DETAIL);
1454
    fflush(stdout);
808✔
1455
    return -1;
808✔
1456
  }
1457

1458
  bool runOnce = pArgs->commands != NULL || pArgs->file[0] != 0;
1,177,105✔
1459
  shellSetConn(shell.conn, runOnce);
1,177,105✔
1460
  shellReadHistory();
1,177,105✔
1461

1462
  if (shell.args.is_bi_mode) {
1,177,105✔
1463
    // need set bi mode
1464
    printf("Set BI mode is true.\n");
112✔
1465
    taos_set_conn_mode(shell.conn, TAOS_CONN_MODE_BI, 1);
112✔
1466
  }
1467

1468
  if (runOnce) {
1,177,105✔
1469
    if (pArgs->commands != NULL) {
1,176,900✔
1470
      printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
1,097,380✔
1471
      char *cmd = taosStrdup(pArgs->commands);
1,097,380✔
1472
      shellRunCommand(cmd, true);
1,097,380✔
1473
      taosMemoryFree(cmd);
1,097,380✔
1474
    }
1475

1476
    if (pArgs->file[0] != 0) {
1,176,900✔
1477
      shellSourceFile(pArgs->file);
79,520✔
1478
    }
1479

1480
    taos_close(shell.conn);
1,176,900✔
1481

1482
    shellWriteHistory();
1,176,900✔
1483
    shellCleanupHistory();
1,176,900✔
1484
    return 0;
1,176,900✔
1485
  }
1486

1487
  if ((code = tsem_init(&shell.cancelSem, 0, 0)) != 0) {
205✔
1488
    printf("failed to create cancel semaphore since %s\r\n", tstrerror(code));
×
1489
    return code;
×
1490
  }
1491

1492
  TdThread spid = {0};
205✔
1493
  taosThreadCreate(&spid, NULL, shellCancelHandler, NULL);
205✔
1494

1495
  taosSetSignal(SIGTERM, shellQueryInterruptHandler);
205✔
1496
  taosSetSignal(SIGHUP, shellQueryInterruptHandler);
205✔
1497
  taosSetSignal(SIGINT, shellQueryInterruptHandler);
205✔
1498

1499
  char    buf[512] = {0};
205✔
1500
  int32_t verType = shellGetGrantInfo(buf);
205✔
1501
#ifndef WINDOWS
1502
  printfIntroduction(verType);
205✔
1503
#else
1504
  if (verType == TSDB_VERSION_OSS) {
1505
    showAD(false);
1506
  }
1507
#endif
1508
  // printf version
1509
  if (verType == TSDB_VERSION_ENTERPRISE || verType == TSDB_VERSION_CLOUD) {
205✔
1510
    printf("%s\n", buf);
205✔
1511
  }
1512

1513
  while (1) {
1514
    taosThreadCreate(&shell.pid, NULL, shellThreadLoop, NULL);
205✔
1515
    taosThreadJoin(shell.pid, NULL);
205✔
1516
    taosThreadClear(&shell.pid);
205✔
1517
    if (shell.exit) {
205✔
1518
      tsem_post(&shell.cancelSem);
205✔
1519
      break;
205✔
1520
    }
1521
  }
1522

1523
  if (verType == TSDB_VERSION_OSS) {
205✔
1524
    showAD(true);
×
1525
  }
1526

1527
  taosThreadJoin(spid, NULL);
205✔
1528

1529
  shellCleanupHistory();
205✔
1530
  taos_kill_query(shell.conn);
205✔
1531
  taos_close(shell.conn);
205✔
1532

1533
  TAOS_RETURN(code);
205✔
1534
}
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