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

taosdata / TDengine / #4911

04 Jan 2026 09:05AM UTC coverage: 65.028% (-0.8%) from 65.864%
#4911

push

travis-ci

web-flow
merge: from main to 3.0 branch #34156

1206 of 4524 new or added lines in 22 files covered. (26.66%)

1517 existing lines in 134 files now uncovered.

195276 of 300296 relevant lines covered (65.03%)

116931714.52 hits per line

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

77.78
/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) {
196,612,937✔
71
  for (char c = *cmd++; c != 0; c = *cmd++) {
196,733,948✔
72
    if (c != ' ' && c != '\t' && c != ';') {
132,317,816✔
73
      return false;
132,196,805✔
74
    }
75
  }
76
  return true;
64,416,132✔
77
}
78

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

82
  if (shellIsEmptyCommand(command)) {
130,574,620✔
83
    return 0;
64,416,132✔
84
  }
85

86
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
66,158,488✔
87
    return -1;
32✔
88
  }
89

90
  if (shellRegexMatch(command, "^[\t ]*clear[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
66,158,456✔
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;]*$",
66,158,456✔
103
                      REG_EXTENDED | REG_ICASE)) {
104
    strtok(command, " \t");
638✔
105
    strtok(NULL, " \t");
638✔
106
    char *p = strtok(NULL, " \t");
638✔
107
    if (strncasecmp(p, "default", 7) == 0) {
638✔
108
      shell.args.displayWidth = SHELL_DEFAULT_MAX_BINARY_DISPLAY_WIDTH;
×
109
    } else {
110
      int32_t displayWidth = atoi(p);
638✔
111
      displayWidth = TRANGE(displayWidth, 1, 10 * 1024);
638✔
112
      shell.args.displayWidth = displayWidth;
638✔
113
    }
114
    return 0;
638✔
115
  }
116

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

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

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

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

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

173
  if (recordHistory) shellRecordCommandToHistory(command);
66,038,285✔
174

175
  char quote = 0, *cmd = command;
66,038,285✔
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,873✔
179
      continue;
2,873✔
180
    }
181

182
    if (quote == c) {
2,147,483,647✔
183
      quote = 0;
3,008,655✔
184
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
2,147,483,647✔
185
      quote = c;
3,008,655✔
186
    } else if (c == ';' && quote == 0) {
2,147,483,647✔
187
      c = *command;
64,536,367✔
188
      *command = 0;
64,536,367✔
189
      if (shellRunSingleCommand(cmd) < 0) {
64,536,367✔
190
        return -1;
32✔
191
      }
192
      *command = c;
64,536,335✔
193
      cmd = command;
64,536,335✔
194
    }
195
  }
196
  return shellRunSingleCommand(cmd);
66,038,253✔
197
}
198

199
char *strendG(const char *pstr) {
66,157,786✔
200
  if (pstr == NULL) {
66,157,786✔
201
    return NULL;
×
202
  }
203

204
  size_t len = strlen(pstr);
66,157,786✔
205
  if (len < 4) {
66,157,786✔
206
    return NULL;
×
207
  }
208

209
  char *p = (char *)pstr + len - 2;
66,157,786✔
210
  if (strcmp(p, "\\G") == 0) {
66,157,786✔
211
    return p;
5,428✔
212
  }
213

214
  p = (char *)pstr + len - 3;
66,152,358✔
215
  if (strcmp(p, "\\G;") == 0) {
66,152,358✔
216
    return p;
59,852✔
217
  }
218

219
  return NULL;
66,092,506✔
220
}
221

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

229
  if ((sptr = strstr(command, ">>")) != NULL) {
66,157,786✔
230
    fname = sptr + 2;
597,319✔
231
    while (*fname == ' ') fname++;
1,194,574✔
232
    *sptr = '\0';
597,319✔
233

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

240
  if ((sptr = strendG(command)) != NULL) {
66,157,786✔
241
    *sptr = '\0';
65,280✔
242
    printMode = true;  // When output to a file, the switch does not work.
65,280✔
243
  }
244

245
  st = taosGetTimestampUs();
66,157,786✔
246

247
  TAOS_RES *pSql = taos_query(shell.conn, command);
66,157,786✔
248
  if (taos_errno(pSql)) {
66,157,786✔
249
    shellPrintError(pSql, st);
47,051,003✔
250
    return;
47,051,003✔
251
  }
252

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

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

259
    taos_free_result(pSql);
12,490✔
260

261
    return;
12,490✔
262
  }
263

264
  // pre string
265
  char *pre = "Query OK";
19,094,293✔
266
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
19,094,293✔
267
    pre = "Delete OK";
16✔
268
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
19,094,277✔
269
    pre = "Insert OK";
2,535✔
270
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
19,091,742✔
271
    pre = "Create OK";
3,340✔
272
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
19,088,402✔
273
    pre = "Drop OK";
549✔
274
  }
275

276
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
19,094,293✔
277
  if (pFields != NULL) {  // select and show kinds of commands
19,094,293✔
278
    int32_t error_no = 0;
17,803,727✔
279

280
    int64_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode, command);
17,803,727✔
281
    if (numOfRows < 0) return;
17,803,727✔
282

283
    et = taosGetTimestampUs();
17,803,727✔
284
    if (error_no == 0) {
17,803,727✔
285
      printf("Query OK, %" PRId64 " row(s) in set (%.6fs)\r\n", numOfRows, (et - st) / 1E6);
17,803,727✔
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);
17,803,727✔
291
  } else {
292
    int64_t num_rows_affacted = taos_affected_rows64(pSql);
1,290,566✔
293
    taos_free_result(pSql);
1,290,566✔
294
    et = taosGetTimestampUs();
1,290,566✔
295
    printf("%s, %" PRId64 " row(s) affected (%.6fs)\r\n", pre, num_rows_affacted, (et - st) / 1E6);
1,290,566✔
296

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

301
  printf("\r\n");
19,094,293✔
302
}
303

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

310
  time_t  tt;
219,236,688✔
311
  int32_t ms = 0;
219,271,106✔
312
  if (precision == TSDB_TIME_PRECISION_NANO) {
219,271,106✔
313
    tt = (time_t)(val / 1000000000);
320✔
314
    ms = val % 1000000000;
320✔
315
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
219,270,786✔
316
    tt = (time_t)(val / 1000000);
320✔
317
    ms = val % 1000000;
320✔
318
  } else {
319
    tt = (time_t)(val / 1000);
219,270,466✔
320
    ms = val % 1000;
219,270,466✔
321
  }
322

323
  if (tt <= 0 && ms < 0) {
219,271,106✔
324
    tt--;
9,053✔
325
    if (precision == TSDB_TIME_PRECISION_NANO) {
9,053✔
326
      ms += 1000000000;
×
327
    } else if (precision == TSDB_TIME_PRECISION_MICRO) {
9,053✔
328
      ms += 1000000;
×
329
    } else {
330
      ms += 1000;
9,053✔
331
    }
332
  }
333

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

340
  if (precision == TSDB_TIME_PRECISION_NANO) {
219,271,106✔
341
    sprintf(buf + pos, ".%09d", ms);
320✔
342
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
219,270,786✔
343
    sprintf(buf + pos, ".%06d", ms);
320✔
344
  } else {
345
    sprintf(buf + pos, ".%03d", ms);
219,270,466✔
346
  }
347

348
  return buf;
219,271,106✔
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,421,004,299✔
361
  if (val == NULL) {
1,421,004,299✔
362
    taosFprintfFile(pFile, "NULL");
8,204,949✔
363
    return;
8,204,949✔
364
  }
365

366
  char    quotationStr[2] = {'"', 0};
1,412,799,350✔
367
  int32_t width;
368

369
  int n = 0;
1,412,799,350✔
370
#define LENGTH 64
371
  char buf[LENGTH] = {0};
1,412,799,350✔
372
  switch (field->type) {
1,412,799,350✔
373
    case TSDB_DATA_TYPE_BOOL:
279,771,298✔
374
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
279,771,298✔
375
      break;
279,771,298✔
376
    case TSDB_DATA_TYPE_TINYINT:
18,908,073✔
377
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
18,908,073✔
378
      break;
18,908,073✔
379
    case TSDB_DATA_TYPE_UTINYINT:
2,158,569✔
380
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
2,158,569✔
381
      break;
2,158,569✔
382
    case TSDB_DATA_TYPE_SMALLINT:
1,439,775✔
383
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
1,439,775✔
384
      break;
1,439,775✔
385
    case TSDB_DATA_TYPE_USMALLINT:
720,252✔
386
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
720,252✔
387
      break;
720,252✔
388
    case TSDB_DATA_TYPE_INT:
132,835,271✔
389
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
132,835,271✔
390
      break;
132,835,271✔
391
    case TSDB_DATA_TYPE_UINT:
1,438,317✔
392
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
1,438,317✔
393
      break;
1,438,317✔
394
    case TSDB_DATA_TYPE_BIGINT:
172,267,564✔
395
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
172,267,564✔
396
      break;
172,267,564✔
397
    case TSDB_DATA_TYPE_UBIGINT:
1,430,298✔
398
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
1,430,298✔
399
      break;
1,430,298✔
400
    case TSDB_DATA_TYPE_FLOAT:
16,038,320✔
401
      width = SHELL_FLOAT_WIDTH;
16,038,320✔
402
      if (tsEnableScience) {
16,038,320✔
403
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
404
      } else {
405
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
16,038,320✔
406
        if (n > SHELL_FLOAT_WIDTH) {
16,038,320✔
UNCOV
407
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
408
        } else {
409
          taosFprintfFile(pFile, "%s", buf);
16,038,320✔
410
        }
411
      }
412
      break;
16,038,320✔
413
    case TSDB_DATA_TYPE_DOUBLE:
223,816,821✔
414
      width = SHELL_DOUBLE_WIDTH;
223,816,821✔
415
      if (tsEnableScience) {
223,816,821✔
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));
223,816,821✔
420
        if (n > SHELL_DOUBLE_WIDTH) {
223,816,821✔
421
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
118,409,187✔
422
        } else {
423
          taosFprintfFile(pFile, "%s", buf);
105,407,634✔
424
        }
425
      }
426
      break;
223,816,821✔
427
    case TSDB_DATA_TYPE_BINARY:
204,991,588✔
428
    case TSDB_DATA_TYPE_NCHAR:
429
    case TSDB_DATA_TYPE_JSON: {
430
      int32_t bufIndex = 0;
204,991,588✔
431
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
204,991,588✔
432
      if (tmp == NULL) break;
204,991,588✔
433
      for (int32_t i = 0; i < length; i++) {
2,147,483,647✔
434
        tmp[bufIndex] = val[i];
2,054,274,957✔
435
        bufIndex++;
2,054,274,957✔
436
        if (val[i] == '\"') {
2,054,274,957✔
437
          tmp[bufIndex] = val[i];
×
438
          bufIndex++;
×
439
        }
440
      }
441
      tmp[bufIndex] = 0;
204,991,588✔
442

443
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
204,991,588✔
444
      taosMemoryFree(tmp);
204,991,588✔
445
    } break;
204,991,588✔
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:
158,303,225✔
465
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
158,303,225✔
466
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
158,303,225✔
467
      break;
158,303,225✔
468
    case TSDB_DATA_TYPE_DECIMAL64:
198,679,979✔
469
    case TSDB_DATA_TYPE_DECIMAL:
470
      taosFprintfFile(pFile, "%s", val);
198,679,979✔
471
      break;
198,679,979✔
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) {
577,733✔
490
  char fullname[PATH_MAX] = {0};
577,733✔
491
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
577,733✔
492
    tstrncpy(fullname, fname, PATH_MAX);
×
493
  }
494

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

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

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

510
  for (int32_t col = 0; col < num_fields; col++) {
1,399,894✔
511
    if (col > 0) {
822,161✔
512
      taosFprintfFile(pFile, ",");
244,428✔
513
    }
514
    taosFprintfFile(pFile, "%s", fields[col].name);
822,161✔
515
  }
516
  taosFprintfFile(pFile, "\r\n");
577,733✔
517

518
  int64_t numOfRows = 0;
577,733✔
519
  do {
520
    int32_t *length = taos_fetch_lengths(tres);
661,696,119✔
521
    for (int32_t i = 0; i < num_fields; i++) {
2,082,700,418✔
522
      if (i > 0) {
1,421,004,299✔
523
        taosFprintfFile(pFile, ",");
759,308,180✔
524
      }
525
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
1,421,004,299✔
526
    }
527
    taosFprintfFile(pFile, "\r\n");
661,696,119✔
528

529
    numOfRows++;
661,696,119✔
530
    row = taos_fetch_row(tres);
661,696,119✔
531
  } while (row != NULL);
661,696,119✔
532

533
  taosCloseFile(&pFile);
577,733✔
534

535
  return numOfRows;
577,733✔
536
}
537

538
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
84,998,869✔
539
  TdWchar tail[3];
84,983,376✔
540
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
84,998,869✔
541

542
  while (pos < length) {
1,258,629,976✔
543
    TdWchar wc;
1,174,396,831✔
544
    int32_t bytes = taosMbToWchar(&wc, str + pos, MB_CUR_MAX);
1,174,492,375✔
545
    if (bytes <= 0) {
1,174,492,375✔
546
      break;
38,425✔
547
    }
548

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

560
    if (w <= 0) {
1,174,454,046✔
561
      continue;
11,752✔
562
    }
563

564
    if (width <= 0) {
1,174,443,894✔
565
      printf("%lc", wc);
74,554,816✔
566
      continue;
74,554,816✔
567
    }
568

569
    totalCols += w;
1,099,889,078✔
570
    if (totalCols > width) {
1,099,889,078✔
571
      break;
822,939✔
572
    }
573
    if (totalCols <= (width - 3)) {
1,099,066,139✔
574
      printf("%lc", wc);
1,096,513,414✔
575
      cols += w;
1,096,513,414✔
576
    } else {
577
      tail[tailLen] = wc;
2,552,725✔
578
      tailLen++;
2,552,725✔
579
    }
580
  }
581

582
  if (totalCols > width) {
84,998,869✔
583
    // width could be 1 or 2, so printf("...") cannot be used
584
    for (int32_t i = 0; i < 3; i++) {
3,291,756✔
585
      if (cols >= width) {
2,468,817✔
586
        break;
×
587
      }
588
      putchar('.');
2,468,817✔
589
      ++cols;
2,468,817✔
590
    }
591
  } else {
592
    for (int32_t i = 0; i < tailLen; i++) {
84,264,158✔
593
      printf("%lc", tail[i]);
88,228✔
594
    }
595
    cols = totalCols;
84,175,930✔
596
  }
597

598
  for (; cols < width; cols++) {
2,006,337,973✔
599
    putchar(' ');
1,921,339,104✔
600
  }
601
}
84,998,869✔
602

603
void shellPrintString(const char *str, int32_t width) {
226,921,267✔
604
  int32_t len = strlen(str);
226,921,267✔
605

606
  if (width == 0) {
226,921,267✔
607
    printf("%s", str);
×
608
  } else if (len > width) {
226,921,267✔
609
    if (width <= 3) {
13,221✔
610
      printf("%.*s.", width - 1, str);
13,221✔
611
    } else {
612
      printf("%.*s...", width - 3, str);
×
613
    }
614
  } else {
615
    printf("%s%*.s", str, width - len, "");
226,908,046✔
616
  }
617
}
226,921,267✔
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) {
640,358,259✔
646
  if (val == NULL) {
640,358,259✔
647
    shellPrintString(TSDB_DATA_NULL_STR, width);
187,051,207✔
648
    return;
187,051,207✔
649
  }
650

651
  int n = 0;
453,307,052✔
652
#define LENGTH 64
653
  char buf[LENGTH] = {0};
453,307,052✔
654
  switch (field->type) {
453,307,052✔
655
    case TSDB_DATA_TYPE_BOOL:
39,870,060✔
656
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
39,870,060✔
657
      break;
39,870,060✔
658
    case TSDB_DATA_TYPE_TINYINT:
13,222,106✔
659
      printf("%*d", width, *((int8_t *)val));
13,222,106✔
660
      break;
13,222,106✔
661
    case TSDB_DATA_TYPE_UTINYINT:
20,143,906✔
662
      printf("%*u", width, *((uint8_t *)val));
20,143,906✔
663
      break;
20,143,906✔
664
    case TSDB_DATA_TYPE_SMALLINT:
9,752,988✔
665
      printf("%*d", width, *((int16_t *)val));
9,752,988✔
666
      break;
9,752,988✔
667
    case TSDB_DATA_TYPE_USMALLINT:
18,910,812✔
668
      printf("%*u", width, *((uint16_t *)val));
18,910,812✔
669
      break;
18,910,812✔
670
    case TSDB_DATA_TYPE_INT:
38,823,247✔
671
      printf("%*d", width, *((int32_t *)val));
38,823,247✔
672
      break;
38,823,247✔
673
    case TSDB_DATA_TYPE_UINT:
16,234,070✔
674
      printf("%*u", width, *((uint32_t *)val));
16,234,070✔
675
      break;
16,234,070✔
676
    case TSDB_DATA_TYPE_BIGINT:
45,792,677✔
677
      printf("%*" PRId64, width, taosGetInt64Aligned((int64_t *)val));
45,792,677✔
678
      break;
45,792,677✔
679
    case TSDB_DATA_TYPE_UBIGINT:
24,933,882✔
680
      printf("%*" PRIu64, width, taosGetUInt64Aligned((uint64_t *)val));
24,933,882✔
681
      break;
24,933,882✔
682
    case TSDB_DATA_TYPE_FLOAT:
21,417,396✔
683
      width = width >= LENGTH ? LENGTH - 1 : width;
21,417,396✔
684
      if (tsEnableScience) {
21,417,396✔
685
        printf("%*.7e", width, taosGetFloatAligned((float *)val));
×
686
      } else {
687
        snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, taosGetFloatAligned((float *)val));
21,417,396✔
688
        printf("%s", buf);
21,417,396✔
689
      }
690
      break;
21,417,396✔
691
    case TSDB_DATA_TYPE_DOUBLE:
58,239,126✔
692
      width = width >= LENGTH ? LENGTH - 1 : width;
58,239,126✔
693
      if (tsEnableScience) {
58,239,126✔
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));
58,239,126✔
698
        printf("%*s", width, buf);
58,239,126✔
699
      }
700
      break;
58,239,126✔
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:
84,998,869✔
712
    case TSDB_DATA_TYPE_NCHAR:
713
    case TSDB_DATA_TYPE_JSON:
714
      shellPrintNChar(val, length, width);
84,998,869✔
715
      break;
84,998,869✔
716
    case TSDB_DATA_TYPE_GEOMETRY:
×
717
      shellPrintGeometry(val, length, width);
×
718
      break;
×
719
    case TSDB_DATA_TYPE_TIMESTAMP:
60,967,881✔
720
      shellFormatTimestamp(buf, sizeof(buf), taosGetInt64Aligned((int64_t *)val), precision);
60,967,881✔
721
      printf("%s", buf);
60,967,881✔
722
      break;
60,967,881✔
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:
32✔
736
    case TSDB_DATA_TYPE_DECIMAL64:
737
      printf("%*s", width, val);
32✔
738
    default:
32✔
739
      break;
32✔
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) {
17,225,994✔
779
  dump_info->sql = sql;
17,225,994✔
780
  dump_info->vertical = vertical;
17,225,994✔
781
  tsem_init(&dump_info->sem, 0, 0);
17,225,994✔
782
  dump_info->numOfAllRows = 0;
17,225,994✔
783

784
  dump_info->numFields = taos_num_fields(tres);
17,225,994✔
785
  dump_info->fields = taos_fetch_fields(tres);
17,225,994✔
786
  dump_info->precision = taos_result_precision(tres);
17,225,994✔
787

788
  dump_info->resShowMaxNum = UINT64_MAX;
17,225,994✔
789

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

794
  if (vertical) {
17,225,994✔
795
    dump_info->maxColNameLen = 0;
65,280✔
796
    for (int32_t col = 0; col < dump_info->numFields; col++) {
136,091✔
797
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
70,811✔
798
      if (len > dump_info->maxColNameLen) {
70,811✔
799
        dump_info->maxColNameLen = len;
66,926✔
800
      }
801
    }
802
  } else {
803
    for (int32_t col = 0; col < dump_info->numFields; col++) {
53,370,655✔
804
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
36,209,941✔
805
    }
806
    // check create token and set width
807
    if (shellRegexMatch(sql, "^[\t ]*create[ \t]+token[ \t]+.*", REG_EXTENDED | REG_ICASE)) {
17,160,714✔
808
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOKEN_DISPLAY_WIDTH);
×
809
    }
810
  }
811
}
17,225,994✔
812

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

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

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

826
    int32_t *length = taos_fetch_lengths(tres);
1,283,361✔
827

828
    for (int32_t i = 0; i < dump_info->numFields; i++) {
2,574,255✔
829
      TAOS_FIELD *field = dump_info->fields + i;
1,290,894✔
830

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

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

839
    numOfPintRows++;
1,283,361✔
840
    numOfPrintRowsThisOne++;
1,283,361✔
841

842
    if (numOfPintRows == dump_info->resShowMaxNum) {
1,283,361✔
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,283,361✔
854
      return;
65,280✔
855
    }
856

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

862
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
36,209,941✔
863
  int32_t width = (int32_t)strlen(field->name);
36,209,941✔
864

865
  switch (field->type) {
36,209,941✔
866
    case TSDB_DATA_TYPE_NULL:
×
867
      return TMAX(4, width);  // null
×
868
    case TSDB_DATA_TYPE_BOOL:
1,576,895✔
869
      return TMAX(5, width);  // 'false'
1,576,895✔
870

871
    case TSDB_DATA_TYPE_TINYINT:
1,106,210✔
872
    case TSDB_DATA_TYPE_UTINYINT:
873
      return TMAX(4, width);  // '-127'
1,106,210✔
874

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

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

883
    case TSDB_DATA_TYPE_BIGINT:
6,636,998✔
884
    case TSDB_DATA_TYPE_UBIGINT:
885
      return TMAX(21, width);  // '-9223372036854775807'
6,636,998✔
886

887
    case TSDB_DATA_TYPE_FLOAT:
839,721✔
888
      return TMAX(SHELL_FLOAT_WIDTH, width);
839,721✔
889

890
    case TSDB_DATA_TYPE_DOUBLE:
9,536,701✔
891
      return TMAX(SHELL_DOUBLE_WIDTH, width);
9,536,701✔
892

893
    case TSDB_DATA_TYPE_BINARY:
2,842,177✔
894
    case TSDB_DATA_TYPE_GEOMETRY:
895
      if (field->bytes > shell.args.displayWidth) {
2,842,177✔
896
        return TMAX(shell.args.displayWidth, width);
1,933,496✔
897
      } else {
898
        return TMAX(field->bytes + 2, width);
908,681✔
899
      }
900
    case TSDB_DATA_TYPE_VARBINARY: {
192✔
901
      int32_t bytes = field->bytes * 2 + 2;
192✔
902
      if (bytes > shell.args.displayWidth) {
192✔
903
        return TMAX(shell.args.displayWidth, width);
×
904
      } else {
905
        return TMAX(bytes + 2, width);
192✔
906
      }
907
    }
908
    case TSDB_DATA_TYPE_NCHAR:
1,282,569✔
909
    case TSDB_DATA_TYPE_JSON: {
910
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
1,282,569✔
911
      if (bytes > shell.args.displayWidth) {
1,282,569✔
912
        return TMAX(shell.args.displayWidth, width);
1,282,569✔
913
      } else {
914
        return TMAX(bytes + 2, width);
×
915
      }
916
    }
917

918
    case TSDB_DATA_TYPE_TIMESTAMP:
2,846,281✔
919
      if (shell.args.is_raw_time) {
2,846,281✔
920
        return TMAX(14, width);
48✔
921
      }
922
      if (precision == TSDB_TIME_PRECISION_NANO) {
2,846,233✔
923
        return TMAX(29, width);
32✔
924
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
2,846,201✔
925
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
32✔
926
      } else {
927
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
2,846,169✔
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:
16✔
940
      return TMAX(width, 20);
16✔
941
    case TSDB_DATA_TYPE_DECIMAL:
16✔
942
      return TMAX(width, 40);
16✔
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) {
9,704,049✔
951
  int32_t rowWidth = 0;
9,704,049✔
952
  for (int32_t col = 0; col < num_fields; col++) {
32,339,347✔
953
    TAOS_FIELD *field = fields + col;
22,635,298✔
954
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
22,635,298✔
955
    int32_t     left = padding / 2;
22,635,298✔
956
    printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
22,635,298✔
957
    rowWidth += width[col] + 3;
22,635,298✔
958
  }
959

960
  putchar('\r');
9,704,049✔
961
  putchar('\n');
9,704,049✔
962
  for (int32_t i = 0; i < rowWidth; i++) {
849,293,285✔
963
    putchar('=');
839,589,236✔
964
  }
965
  putchar('\r');
9,704,049✔
966
  putchar('\n');
9,704,049✔
967
}
9,704,049✔
968

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

976
  int64_t numOfPintRows = dump_info->numOfAllRows;
9,770,747✔
977
  int     numOfPrintRowsThisOne = 0;
9,770,747✔
978
  if (numOfPintRows == 0) {
9,770,747✔
979
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
9,704,049✔
980
  }
981

982
  while (row != NULL) {
91,123,763✔
983
    int32_t *length = taos_fetch_lengths(tres);
91,123,763✔
984
    for (int32_t i = 0; i < dump_info->numFields; i++) {
730,191,128✔
985
      putchar(' ');
639,067,365✔
986
      shellPrintField((const char *)row[i], dump_info->fields + i, dump_info->width[i], length[i],
639,067,365✔
987
                      dump_info->precision);
988
      putchar(' ');
639,067,365✔
989
      putchar('|');
639,067,365✔
990
    }
991
    putchar('\r');
91,123,763✔
992
    putchar('\n');
91,123,763✔
993

994
    numOfPintRows++;
91,123,763✔
995
    numOfPrintRowsThisOne++;
91,123,763✔
996

997
    if (numOfPintRows == dump_info->resShowMaxNum) {
91,123,763✔
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) {
91,123,763✔
1013
      return;
9,770,747✔
1014
    }
1015

1016
    row = taos_fetch_row(tres);
81,353,016✔
1017
  }
1018
  return;
×
1019
}
1020

1021
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
27,062,021✔
1022
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
27,062,021✔
1023
  if (num_of_rows > 0) {
27,062,021✔
1024
    dump_info->numOfRows = num_of_rows;
9,836,027✔
1025
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
9,836,027✔
1026
      if (dump_info->vertical) {
9,836,027✔
1027
        shellVerticalPrintResult(tres, dump_info);
65,280✔
1028
      } else {
1029
        shellHorizontalPrintResult(tres, dump_info);
9,770,747✔
1030
      }
1031
    }
1032
    dump_info->numOfAllRows += num_of_rows;
9,836,027✔
1033
    if (!shellCmdkilled) {
9,836,027✔
1034
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
9,836,027✔
1035
    } else {
1036
      tsem_post(&dump_info->sem);
×
1037
    }
1038
  } else {
1039
    if (num_of_rows < 0) {
17,225,994✔
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);
17,225,994✔
1043
  }
1044
}
27,061,958✔
1045

1046
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
17,803,727✔
1047
  int64_t num_of_rows = 0;
17,803,727✔
1048
  if (fname != NULL) {
17,803,727✔
1049
    num_of_rows = shellDumpResultToFile(fname, tres);
577,733✔
1050
  } else {
1051
    tsDumpInfo dump_info;
17,223,694✔
1052
    if (!shellCmdkilled) {
17,225,994✔
1053
      init_dump_info(&dump_info, tres, sql, vertical);
17,225,994✔
1054
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
17,225,994✔
1055
      tsem_wait(&dump_info.sem);
17,225,994✔
1056
      num_of_rows = dump_info.numOfAllRows;
17,225,994✔
1057
    }
1058
  }
1059

1060
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
17,803,727✔
1061
  return num_of_rows;
17,803,727✔
1062
}
1063

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

1069
  char   *line = taosMemoryMalloc(tsMaxSQLLength + 1);
762,725✔
1070
  int32_t read_size = 0;
762,725✔
1071
  while ((read_size = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
160,950,908✔
1072
    line[read_size - 1] = '\0';
160,188,183✔
1073
    taosMemoryFree(pHistory->hist[pHistory->hend]);
160,188,183✔
1074
    pHistory->hist[pHistory->hend] = taosStrdup(line);
160,188,183✔
1075

1076
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
160,188,183✔
1077

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

1083
  taosMemoryFreeClear(line);
762,725✔
1084
  taosCloseFile(&pFile);
762,725✔
1085
  int64_t file_size;
760,343✔
1086
  if (taosStatFile(pHistory->file, &file_size, NULL, NULL) == 0 && file_size > SHELL_MAX_COMMAND_SIZE) {
762,725✔
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;
762,725✔
1104
}
1105

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

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

1123
void shellCleanupHistory() {
792,286✔
1124
  SShellHistory *pHistory = &shell.history;
792,286✔
1125
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
793,078,286✔
1126
    if (pHistory->hist[i] != NULL) {
792,286,000✔
1127
      taosMemoryFree(pHistory->hist[i]);
160,188,183✔
1128
      pHistory->hist[i] = NULL;
160,188,183✔
1129
    }
1130
  }
1131
}
792,286✔
1132

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

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

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

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

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

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

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

1173
  char *line = taosMemoryMalloc(tsMaxSQLLength + 1);
47,047✔
1174
  while ((read_len = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
65,641,147✔
1175
    if (cmd_len + read_len >= tsMaxSQLLength) {
65,594,100✔
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';
65,594,100✔
1183

1184
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
65,594,100✔
1185
      continue;
281,895✔
1186
    }
1187

1188
    if (line[read_len - 1] == '\\') {
65,312,205✔
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') {
65,312,205✔
1196
      line[read_len - 1] = ' ';
×
1197
    }
1198

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

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

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

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

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

1222
  int32_t code = taos_errno(tres);
341✔
1223
  if (code != TSDB_CODE_SUCCESS) {
341✔
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);
341✔
1233
  if (num_fields == 0) {
341✔
1234
    fprintf(stderr, "\r\nInvalid grant information.\r\n");
×
1235
    exit(0);
×
1236
  } else {
1237
    if (tres == NULL) {
341✔
1238
      fprintf(stderr, "\r\nGrant information is null.\r\n");
×
1239
      exit(0);
×
1240
    }
1241

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

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

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

1258
    if (strcmp(serverVersion, "community") == 0) {
341✔
1259
      verType = TSDB_VERSION_OSS;
×
1260
    } else if (strcmp(expiretime, "unlimited") == 0) {
341✔
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;
341✔
1265
      sprintf(buf, "Server is %s %s. License will expire at %s.\r\n", serverVersion, sinfo, expiretime);
341✔
1266
    }
1267

1268
    taos_free_result(tres);
341✔
1269
  }
1270

1271
  fprintf(stdout, "\r\n");
341✔
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;
341✔
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); }
682✔
1286
#endif
1287

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

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

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

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

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

1312
  return NULL;
341✔
1313
}
1314

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

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

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

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

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

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

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

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

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

1364
#pragma GCC diagnostic pop
1365

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

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

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

1388
    if (pArgs->port_inputted) {
792,965✔
1389
      port = pArgs->port;
784✔
1390
    } else {
1391
      port = defaultPort(pArgs->connMode, pArgs->dsn);
792,181✔
1392
    }
1393

1394
    sprintf(show, "host:%s port:%d ", host, port);
792,965✔
1395

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

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

1438
  return taos;
793,262✔
1439
}
1440

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

1448
  SShellArgs *pArgs = &shell.args;
793,337✔
1449
  shell.conn = createConnect(pArgs);
793,337✔
1450

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

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

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

1468
  if (runOnce) {
792,286✔
1469
    if (pArgs->commands != NULL) {
791,945✔
1470
      printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
725,771✔
1471
      char *cmd = taosStrdup(pArgs->commands);
725,771✔
1472
      shellRunCommand(cmd, true);
725,771✔
1473
      taosMemoryFree(cmd);
725,771✔
1474
    }
1475

1476
    if (pArgs->file[0] != 0) {
791,945✔
1477
      shellSourceFile(pArgs->file);
66,174✔
1478
    }
1479

1480
    taos_close(shell.conn);
791,945✔
1481

1482
    shellWriteHistory();
791,945✔
1483
    shellCleanupHistory();
791,945✔
1484
    return 0;
791,945✔
1485
  }
1486

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

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

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

1499
  char    buf[512] = {0};
341✔
1500
  int32_t verType = shellGetGrantInfo(buf);
341✔
1501
#ifndef WINDOWS
1502
  printfIntroduction(verType);
341✔
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) {
341✔
1510
    printf("%s\n", buf);
341✔
1511
  }
1512

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

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

1527
  taosThreadJoin(spid, NULL);
341✔
1528

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

1533
  TAOS_RETURN(code);
341✔
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