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

taosdata / TDengine / #3625

26 Feb 2025 10:19AM UTC coverage: 63.633% (+0.1%) from 63.485%
#3625

push

travis-ci

web-flow
Merge pull request #29914 from taosdata/feat/TS-5613-3.0

feat:[TS-5613]support bool in cast

148738 of 299799 branches covered (49.61%)

Branch coverage included in aggregate %.

233124 of 300297 relevant lines covered (77.63%)

17654074.26 hits per line

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

73.76
/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 "geosWrapper.h"
22
#include "shellAuto.h"
23
#include "shellInt.h"
24

25
SShellObj shell = {0};
26

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

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

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

41
  uint64_t resShowMaxNum;
42
} tsDumpInfo;
43

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

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

67
static bool shellCmdkilled = false;
68

69
bool shellIsEmptyCommand(const char *cmd) {
785,892✔
70
  for (char c = *cmd++; c != 0; c = *cmd++) {
967,010✔
71
    if (c != ' ' && c != '\t' && c != ';') {
589,605!
72
      return false;
408,487✔
73
    }
74
  }
75
  return true;
377,405✔
76
}
77

78
int32_t shellRunSingleCommand(char *command) {
581,661✔
79
  shellCmdkilled = false;
581,661✔
80

81
  if (shellIsEmptyCommand(command)) {
581,661✔
82
    return 0;
377,405✔
83
  }
84

85
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
204,256✔
86
    return -1;
1✔
87
  }
88

89
  if (shellRegexMatch(command, "^[\t ]*clear[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
204,255!
90
#pragma GCC diagnostic push
91
#pragma GCC diagnostic ignored "-Wunused-result"
92
    system("clear");
×
93
#pragma GCC diagnostic pop
94
    return 0;
×
95
  }
96

97
  if (shellRegexMatch(command, "^[\t ]*set[ \t]+max_binary_display_width[ \t]+(default|[1-9][0-9]*)[ \t;]*$",
204,255✔
98
                      REG_EXTENDED | REG_ICASE)) {
99
    strtok(command, " \t");
3✔
100
    strtok(NULL, " \t");
3✔
101
    char *p = strtok(NULL, " \t");
3✔
102
    if (strncasecmp(p, "default", 7) == 0) {
3!
103
      shell.args.displayWidth = SHELL_DEFAULT_MAX_BINARY_DISPLAY_WIDTH;
×
104
    } else {
105
      int32_t displayWidth = atoi(p);
3✔
106
      displayWidth = TRANGE(displayWidth, 1, 10 * 1024);
3✔
107
      shell.args.displayWidth = displayWidth;
3✔
108
    }
109
    return 0;
3✔
110
  }
111

112
  if (shellRegexMatch(command, "^[ \t]*source[\t ]+[^ ]+[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
204,252✔
113
    /* If source file. */
114
    char *c_ptr = strtok(command, " ;");
4✔
115
    if (c_ptr == NULL) {
4!
116
      shellRunSingleCommandImp(command);
×
117
      return 0;
×
118
    }
119
    c_ptr = strtok(NULL, " ;");
4✔
120
    if (c_ptr == NULL) {
4!
121
      shellRunSingleCommandImp(command);
×
122
      return 0;
×
123
    }
124
    shellSourceFile(c_ptr);
4✔
125
    return 0;
4✔
126
  }
127
#ifdef WEBSOCKET
128
  if (shell.args.restful || shell.args.cloud) {
204,248✔
129
    shellRunSingleCommandWebsocketImp(command);
91✔
130
  } else {
131
#endif
132
    shellRunSingleCommandImp(command);
204,157✔
133
#ifdef WEBSOCKET
134
  }
135
#endif
136
  return 0;
204,248✔
137
}
138

139
void shellRecordCommandToHistory(char *command) {
1,091✔
140
  if (strncasecmp(command, "create user ", 12) == 0 || strncasecmp(command, "alter user ", 11) == 0) {
1,091!
141
    if (taosStrCaseStr(command, " pass ")) {
6!
142
      // have password command forbid record to history because security
143
      return;
×
144
    }
145
  }
146

147
  SShellHistory *pHistory = &shell.history;
1,091✔
148
  if (pHistory->hstart == pHistory->hend ||
1,091✔
149
      pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE] == NULL ||
4!
150
      strcmp(command, pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE]) != 0) {
4!
151
    if (pHistory->hist[pHistory->hend] != NULL) {
1,091!
152
      taosMemoryFreeClear(pHistory->hist[pHistory->hend]);
1,091!
153
    }
154
    pHistory->hist[pHistory->hend] = taosStrdup(command);
1,091!
155

156
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
1,091✔
157
    if (pHistory->hend == pHistory->hstart) {
1,091!
158
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
×
159
    }
160
  }
161
}
162

163
int32_t shellRunCommand(char *command, bool recordHistory) {
204,231✔
164
  if (shellIsEmptyCommand(command)) {
204,231!
165
    return 0;
×
166
  }
167

168
  // add help or help;
169
  if (strncasecmp(command, "help", 4) == 0) {
204,231✔
170
    if (command[4] == ';' || command[4] == ' ' || command[4] == 0) {
1!
171
      showHelp();
1✔
172
      return 0;
1✔
173
    }
174
  }
175

176
  if (recordHistory) shellRecordCommandToHistory(command);
204,230✔
177

178
  char quote = 0, *cmd = command;
204,230✔
179
  for (char c = *command++; c != 0; c = *command++) {
155,557,086✔
180
    if (c == '\\' && (*command == '\'' || *command == '"' || *command == '`')) {
155,352,857!
181
      command++;
2✔
182
      continue;
2✔
183
    }
184

185
    if (quote == c) {
155,352,855✔
186
      quote = 0;
2,187,259✔
187
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
153,165,596✔
188
      quote = c;
2,187,259✔
189
    } else if (c == ';' && quote == 0) {
150,978,337✔
190
      c = *command;
377,432✔
191
      *command = 0;
377,432✔
192
      if (shellRunSingleCommand(cmd) < 0) {
377,432✔
193
        return -1;
1✔
194
      }
195
      *command = c;
377,431✔
196
      cmd = command;
377,431✔
197
    }
198
  }
199
  return shellRunSingleCommand(cmd);
204,229✔
200
}
201

202
char *strendG(const char *pstr) {
204,248✔
203
  if (pstr == NULL) {
204,248!
204
    return NULL;
×
205
  }
206

207
  size_t len = strlen(pstr);
204,248✔
208
  if (len < 4) {
204,248!
209
    return NULL;
×
210
  }
211

212
  char *p = (char *)pstr + len - 2;
204,248✔
213
  if (strcmp(p, "\\G") == 0) {
204,248✔
214
    return p;
10✔
215
  }
216

217
  p = (char *)pstr + len - 3;
204,238✔
218
  if (strcmp(p, "\\G;") == 0) {
204,238✔
219
    return p;
58✔
220
  }
221

222
  return NULL;
204,180✔
223
}
224

225
void shellRunSingleCommandImp(char *command) {
204,157✔
226
  int64_t st, et;
227
  char   *sptr = NULL;
204,157✔
228
  char   *cptr = NULL;
204,157✔
229
  char   *fname = NULL;
204,157✔
230
  bool    printMode = false;
204,157✔
231

232
  if ((sptr = strstr(command, ">>")) != NULL) {
204,157✔
233
    fname = sptr + 2;
43✔
234
    while (*fname == ' ') fname++;
73✔
235
    *sptr = '\0';
43✔
236

237
    cptr = strstr(fname, ";");
43✔
238
    if (cptr != NULL) {
43✔
239
      *cptr = '\0';
12✔
240
    }
241
  }
242

243
  if ((sptr = strendG(command)) != NULL) {
204,157✔
244
    *sptr = '\0';
62✔
245
    printMode = true;  // When output to a file, the switch does not work.
62✔
246
  }
247

248
  st = taosGetTimestampUs();
204,157✔
249

250
  TAOS_RES *pSql = taos_query(shell.conn, command);
204,157✔
251
  if (taos_errno(pSql)) {
204,157✔
252
    shellPrintError(pSql, st);
14✔
253
    return;
14✔
254
  }
255

256
  if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) {
204,143✔
257
    printf("Database changed.\r\n\r\n");
158✔
258

259
    // call back auto tab module
260
    callbackAutoTab(command, pSql, true);
158✔
261

262
    taos_free_result(pSql);
158✔
263

264
    return;
158✔
265
  }
266

267
  // pre string
268
  char *pre = "Query OK";
203,985✔
269
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
203,985✔
270
    pre = "Delete OK";
26✔
271
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
203,959✔
272
    pre = "Insert OK";
117,989✔
273
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
85,970✔
274
    pre = "Create OK";
4,130✔
275
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
81,840✔
276
    pre = "Drop OK";
155✔
277
  }
278

279
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
203,985✔
280
  if (pFields != NULL) {  // select and show kinds of commands
203,985✔
281
    int32_t error_no = 0;
78,719✔
282

283
    int64_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode, command);
78,719✔
284
    if (numOfRows < 0) return;
78,719!
285

286
    et = taosGetTimestampUs();
78,719✔
287
    if (error_no == 0) {
78,719!
288
      printf("Query OK, %" PRId64 " row(s) in set (%.6fs)\r\n", numOfRows, (et - st) / 1E6);
78,719✔
289
    } else {
290
      terrno = error_no;
×
291
      printf("Query interrupted (%s), %" PRId64 " row(s) in set (%.6fs)\r\n", taos_errstr(NULL), numOfRows,
×
292
             (et - st) / 1E6);
×
293
    }
294
    taos_free_result(pSql);
78,719✔
295
  } else {
296
    int64_t num_rows_affacted = taos_affected_rows64(pSql);
125,266✔
297
    taos_free_result(pSql);
125,266✔
298
    et = taosGetTimestampUs();
125,266✔
299
    printf("%s, %" PRId64 " row(s) affected (%.6fs)\r\n", pre, num_rows_affacted, (et - st) / 1E6);
125,266✔
300

301
    // call auto tab
302
    callbackAutoTab(command, NULL, false);
125,266✔
303
  }
304

305
  printf("\r\n");
203,985✔
306
}
307

308
char *shellFormatTimestamp(char *buf, int32_t bufSize, int64_t val, int32_t precision) {
4,057,218✔
309
  if (shell.args.is_raw_time) {
4,057,218!
310
    sprintf(buf, "%" PRId64, val);
×
311
    return buf;
×
312
  }
313

314
  time_t  tt;
315
  int32_t ms = 0;
4,057,218✔
316
  if (precision == TSDB_TIME_PRECISION_NANO) {
4,057,218✔
317
    tt = (time_t)(val / 1000000000);
30✔
318
    ms = val % 1000000000;
30✔
319
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
4,057,188✔
320
    tt = (time_t)(val / 1000000);
30✔
321
    ms = val % 1000000;
30✔
322
  } else {
323
    tt = (time_t)(val / 1000);
4,057,158✔
324
    ms = val % 1000;
4,057,158✔
325
  }
326

327
  if (tt <= 0 && ms < 0) {
4,057,218✔
328
    tt--;
4,508✔
329
    if (precision == TSDB_TIME_PRECISION_NANO) {
4,508!
330
      ms += 1000000000;
×
331
    } else if (precision == TSDB_TIME_PRECISION_MICRO) {
4,508!
332
      ms += 1000000;
×
333
    } else {
334
      ms += 1000;
4,508✔
335
    }
336
  }
337

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

344
  if (precision == TSDB_TIME_PRECISION_NANO) {
4,057,218✔
345
    sprintf(buf + pos, ".%09d", ms);
30✔
346
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
4,057,188✔
347
    sprintf(buf + pos, ".%06d", ms);
30✔
348
  } else {
349
    sprintf(buf + pos, ".%03d", ms);
4,057,158✔
350
  }
351

352
  return buf;
4,057,218✔
353
}
354

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

361
  return buf;
×
362
}
363

364
void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, int32_t length, int32_t precision) {
2,136,043✔
365
  if (val == NULL) {
2,136,043✔
366
    taosFprintfFile(pFile, "NULL");
12,766✔
367
    return;
12,766✔
368
  }
369

370
  char    quotationStr[2] = {'"', 0};
2,123,277✔
371
  int32_t width;
372

373
  int n = 0;
2,123,277✔
374
#define LENGTH 64
375
  char buf[LENGTH] = {0};
2,123,277✔
376
  switch (field->type) {
2,123,277!
377
    case TSDB_DATA_TYPE_BOOL:
×
378
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
×
379
      break;
×
380
    case TSDB_DATA_TYPE_TINYINT:
×
381
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
×
382
      break;
×
383
    case TSDB_DATA_TYPE_UTINYINT:
×
384
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
×
385
      break;
×
386
    case TSDB_DATA_TYPE_SMALLINT:
×
387
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
×
388
      break;
×
389
    case TSDB_DATA_TYPE_USMALLINT:
×
390
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
×
391
      break;
×
392
    case TSDB_DATA_TYPE_INT:
400,853✔
393
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
400,853✔
394
      break;
400,853✔
395
    case TSDB_DATA_TYPE_UINT:
×
396
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
×
397
      break;
×
398
    case TSDB_DATA_TYPE_BIGINT:
295,858✔
399
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
295,858✔
400
      break;
295,858✔
401
    case TSDB_DATA_TYPE_UBIGINT:
×
402
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
×
403
      break;
×
404
    case TSDB_DATA_TYPE_FLOAT:
30✔
405
      width = SHELL_FLOAT_WIDTH;
30✔
406
      if (tsEnableScience) {
30!
407
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
408
      } else {
409
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
30✔
410
        if (n > SHELL_FLOAT_WIDTH) {
30!
411
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
412
        } else {
413
          taosFprintfFile(pFile, "%s", buf);
30✔
414
        }
415
      }
416
      break;
30✔
417
    case TSDB_DATA_TYPE_DOUBLE:
399,874✔
418
      width = SHELL_DOUBLE_WIDTH;
399,874✔
419
      if (tsEnableScience) {
399,874!
420
        snprintf(buf, LENGTH, "%*.15e", width, GET_DOUBLE_VAL(val));
×
421
        taosFprintfFile(pFile, "%s", buf);
×
422
      } else {
423
        n = tsnprintf(buf, LENGTH, "%*.15f", width, GET_DOUBLE_VAL(val));
399,874✔
424
        if (n > SHELL_DOUBLE_WIDTH) {
399,874✔
425
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
70,645✔
426
        } else {
427
          taosFprintfFile(pFile, "%s", buf);
329,229✔
428
        }
429
      }
430
      break;
399,874✔
431
    case TSDB_DATA_TYPE_BINARY:
583,212✔
432
    case TSDB_DATA_TYPE_NCHAR:
433
    case TSDB_DATA_TYPE_JSON: {
434
      int32_t bufIndex = 0;
583,212✔
435
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
583,212!
436
      if (tmp == NULL) break;
583,212!
437
      for (int32_t i = 0; i < length; i++) {
5,774,236✔
438
        tmp[bufIndex] = val[i];
5,191,024✔
439
        bufIndex++;
5,191,024✔
440
        if (val[i] == '\"') {
5,191,024!
441
          tmp[bufIndex] = val[i];
×
442
          bufIndex++;
×
443
        }
444
      }
445
      tmp[bufIndex] = 0;
583,212✔
446

447
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
583,212✔
448
      taosMemoryFree(tmp);
583,212!
449
    } break;
583,212✔
450
    case TSDB_DATA_TYPE_VARBINARY: {
×
451
      void    *tmp = NULL;
×
452
      uint32_t size = 0;
×
453
      if (taosAscii2Hex(val, length, &tmp, &size) < 0) {
×
454
        break;
×
455
      }
456
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
×
457
      taosMemoryFree(tmp);
×
458
      break;
×
459
    }
460
    case TSDB_DATA_TYPE_GEOMETRY: {
×
461
      char *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
×
462
      if (tmp == NULL) break;
×
463
      shellDumpHexValue(tmp, val, length);
×
464
      taosFprintfFile(pFile, "%s", buf);
×
465
      taosMemoryFree(tmp);
×
466
      break;
×
467
    }
468
    case TSDB_DATA_TYPE_TIMESTAMP:
443,450✔
469
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
443,450✔
470
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
443,450✔
471
      break;
443,450✔
472
    default:
×
473
      break;
×
474
  }
475
}
476

477
int64_t shellDumpResultToFile(const char *fname, TAOS_RES *tres) {
43✔
478
  char fullname[PATH_MAX] = {0};
43✔
479
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
43!
480
    tstrncpy(fullname, fname, PATH_MAX);
×
481
  }
482

483
  TAOS_ROW row = taos_fetch_row(tres);
43✔
484
  if (row == NULL) {
43!
485
    return 0;
×
486
  }
487

488
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
43✔
489
  if (pFile == NULL) {
43!
490
    fprintf(stderr, "failed to open file: %s\r\n", fullname);
×
491
    return -1;
×
492
  }
493

494
  TAOS_FIELD *fields = taos_fetch_fields(tres);
43✔
495
  int32_t     num_fields = taos_num_fields(tres);
43✔
496
  int32_t     precision = taos_result_precision(tres);
43✔
497

498
  for (int32_t col = 0; col < num_fields; col++) {
221✔
499
    if (col > 0) {
178✔
500
      taosFprintfFile(pFile, ",");
135✔
501
    }
502
    taosFprintfFile(pFile, "%s", fields[col].name);
178✔
503
  }
504
  taosFprintfFile(pFile, "\r\n");
43✔
505

506
  int64_t numOfRows = 0;
43✔
507
  do {
508
    int32_t *length = taos_fetch_lengths(tres);
313,444✔
509
    for (int32_t i = 0; i < num_fields; i++) {
2,449,447✔
510
      if (i > 0) {
2,136,003✔
511
        taosFprintfFile(pFile, ",");
1,822,559✔
512
      }
513
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
2,136,003✔
514
    }
515
    taosFprintfFile(pFile, "\r\n");
313,444✔
516

517
    numOfRows++;
313,444✔
518
    row = taos_fetch_row(tres);
313,444✔
519
  } while (row != NULL);
313,444✔
520

521
  taosCloseFile(&pFile);
43✔
522

523
  return numOfRows;
43✔
524
}
525

526
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
1,760,164✔
527
  TdWchar tail[3];
528
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
1,760,164✔
529

530
  while (pos < length) {
46,018,810✔
531
    TdWchar wc;
532
    int32_t bytes = taosMbToWchar(&wc, str + pos, MB_CUR_MAX);
45,072,259✔
533
    if (bytes <= 0) {
45,072,259✔
534
      break;
813,613✔
535
    }
536

537
    if (pos + bytes > length) {
45,072,250!
538
      break;
×
539
    }
540
    int w = 0;
45,072,250✔
541
    if (*(str + pos) == '\t' || *(str + pos) == '\n' || *(str + pos) == '\r') {
45,072,250!
542
      w = bytes;
×
543
    } else {
544
      w = taosWcharWidth(wc);
45,072,250✔
545
    }
546
    pos += bytes;
45,072,250✔
547

548
    if (w <= 0) {
45,072,250!
549
      continue;
90,005✔
550
    }
551

552
    if (width <= 0) {
45,072,250✔
553
      printf("%lc", wc);
90,005✔
554
      continue;
90,005✔
555
    }
556

557
    totalCols += w;
44,982,245✔
558
    if (totalCols > width) {
44,982,245✔
559
      break;
813,604✔
560
    }
561
    if (totalCols <= (width - 3)) {
44,168,641✔
562
      printf("%lc", wc);
41,329,961✔
563
      cols += w;
41,329,961✔
564
    } else {
565
      tail[tailLen] = wc;
2,838,680✔
566
      tailLen++;
2,838,680✔
567
    }
568
  }
569

570
  if (totalCols > width) {
1,760,164✔
571
    // width could be 1 or 2, so printf("...") cannot be used
572
    for (int32_t i = 0; i < 3; i++) {
3,254,416✔
573
      if (cols >= width) {
2,440,812!
574
        break;
×
575
      }
576
      putchar('.');
2,440,812✔
577
      ++cols;
2,440,812✔
578
    }
579
  } else {
580
    for (int32_t i = 0; i < tailLen; i++) {
1,344,437✔
581
      printf("%lc", tail[i]);
397,877✔
582
    }
583
    cols = totalCols;
946,560✔
584
  }
585

586
  for (; cols < width; cols++) {
10,086,705✔
587
    putchar(' ');
8,326,541✔
588
  }
589
}
1,760,164✔
590

591
void shellPrintString(const char *str, int32_t width) {
1,713,856✔
592
  int32_t len = strlen(str);
1,713,856✔
593

594
  if (width == 0) {
1,713,856✔
595
    printf("%s", str);
120✔
596
  } else if (len > width) {
1,713,736✔
597
    if (width <= 3) {
57!
598
      printf("%.*s.", width - 1, str);
×
599
    } else {
600
      printf("%.*s...", width - 3, str);
57✔
601
    }
602
  } else {
603
    printf("%s%*.s", str, width - len, "");
1,713,679✔
604
  }
605
}
1,713,856✔
606

607
void shellPrintGeometry(const unsigned char *val, int32_t length, int32_t width) {
120✔
608
  if (length == 0) {  // empty value
120!
609
    shellPrintString("", width);
×
610
    return;
×
611
  }
612

613
  int32_t code = TSDB_CODE_FAILED;
120✔
614

615
  code = initCtxAsText();
120✔
616
  if (code != TSDB_CODE_SUCCESS) {
120!
617
    shellPrintString(getGeosErrMsg(code), width);
×
618
    return;
×
619
  }
620

621
  char *outputWKT = NULL;
120✔
622
  code = doAsText(val, length, &outputWKT);
120✔
623
  if (code != TSDB_CODE_SUCCESS) {
120!
624
    shellPrintString(getGeosErrMsg(code), width);  // should NOT happen
×
625
    return;
×
626
  }
627

628
  shellPrintString(outputWKT, width);
120✔
629

630
  geosFreeBuffer(outputWKT);
120✔
631
}
632

633
void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision) {
8,884,517✔
634
  if (val == NULL) {
8,884,517✔
635
    shellPrintString(TSDB_DATA_NULL_STR, width);
1,627,622✔
636
    return;
1,627,622✔
637
  }
638

639
  int n = 0;
7,256,895✔
640
#define LENGTH 64
641
  char buf[LENGTH] = {0};
7,256,895✔
642
  switch (field->type) {
7,256,895!
643
    case TSDB_DATA_TYPE_BOOL:
86,114✔
644
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
86,114✔
645
      break;
86,114✔
646
    case TSDB_DATA_TYPE_TINYINT:
96,600✔
647
      printf("%*d", width, *((int8_t *)val));
96,600✔
648
      break;
96,600✔
649
    case TSDB_DATA_TYPE_UTINYINT:
233✔
650
      printf("%*u", width, *((uint8_t *)val));
233✔
651
      break;
233✔
652
    case TSDB_DATA_TYPE_SMALLINT:
113,266✔
653
      printf("%*d", width, *((int16_t *)val));
113,266✔
654
      break;
113,266✔
655
    case TSDB_DATA_TYPE_USMALLINT:
124✔
656
      printf("%*u", width, *((uint16_t *)val));
124✔
657
      break;
124✔
658
    case TSDB_DATA_TYPE_INT:
120,550✔
659
      printf("%*d", width, *((int32_t *)val));
120,550✔
660
      break;
120,550✔
661
    case TSDB_DATA_TYPE_UINT:
124✔
662
      printf("%*u", width, *((uint32_t *)val));
124✔
663
      break;
124✔
664
    case TSDB_DATA_TYPE_BIGINT:
908,168✔
665
      printf("%*" PRId64, width, *((int64_t *)val));
908,168✔
666
      break;
908,168✔
667
    case TSDB_DATA_TYPE_UBIGINT:
155✔
668
      printf("%*" PRIu64, width, *((uint64_t *)val));
155✔
669
      break;
155✔
670
    case TSDB_DATA_TYPE_FLOAT:
88,228✔
671
      if (tsEnableScience) {
88,228!
672
        printf("%*.7e", width, GET_FLOAT_VAL(val));
×
673
      } else {
674
        n = snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, GET_FLOAT_VAL(val));
88,228✔
675
        if (n > SHELL_FLOAT_WIDTH) {
88,228✔
676
          printf("%*.7e", width, GET_FLOAT_VAL(val));
36✔
677
        } else {
678
          printf("%s", buf);
88,192✔
679
        }
680
      }
681
      break;
88,228✔
682
    case TSDB_DATA_TYPE_DOUBLE:
469,281✔
683
      if (tsEnableScience) {
469,281!
684
        snprintf(buf, LENGTH, "%*.15e", width, GET_DOUBLE_VAL(val));
×
685
        printf("%s", buf);
×
686
      } else {
687
        n = snprintf(buf, LENGTH, "%*.*g", width, DBL_DIG, GET_DOUBLE_VAL(val));
469,281✔
688
        if (n > SHELL_DOUBLE_WIDTH) {
469,281✔
689
          printf("%*.15e", width, GET_DOUBLE_VAL(val));
5,971✔
690
        } else {
691
          printf("%*s", width, buf);
463,310✔
692
        }
693
      }
694
      break;
469,281✔
695
    case TSDB_DATA_TYPE_VARBINARY: {
120✔
696
      void    *data = NULL;
120✔
697
      uint32_t size = 0;
120✔
698
      if (taosAscii2Hex(val, length, &data, &size) < 0) {
120!
699
        break;
×
700
      }
701
      shellPrintNChar(data, size, width);
120✔
702
      taosMemoryFree(data);
120!
703
      break;
120✔
704
    }
705
    case TSDB_DATA_TYPE_BINARY:
1,760,044✔
706
    case TSDB_DATA_TYPE_NCHAR:
707
    case TSDB_DATA_TYPE_JSON:
708
      shellPrintNChar(val, length, width);
1,760,044✔
709
      break;
1,760,044✔
710
    case TSDB_DATA_TYPE_GEOMETRY:
120✔
711
      shellPrintGeometry(val, length, width);
120✔
712
      break;
120✔
713
    case TSDB_DATA_TYPE_TIMESTAMP:
3,613,768✔
714
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
3,613,768✔
715
      printf("%s", buf);
3,613,768✔
716
      break;
3,613,768✔
717
    default:
×
718
      break;
×
719
  }
720
}
721

722
// show whole result for this query return true, like limit or describe
723
bool shellIsShowWhole(const char *sql) {
×
724
  // limit
725
  if (taosStrCaseStr(sql, " limit ") != NULL) {
×
726
    return true;
×
727
  }
728
  // describe
729
  if (taosStrCaseStr(sql, "describe ") != NULL) {
×
730
    return true;
×
731
  }
732
  // desc
733
  if (taosStrCaseStr(sql, "desc ") != NULL) {
×
734
    return true;
×
735
  }
736
  // show
737
  if (taosStrCaseStr(sql, "show ") != NULL) {
×
738
    return true;
×
739
  }
740
  // explain
741
  if (taosStrCaseStr(sql, "explain ") != NULL) {
×
742
    return true;
×
743
  }
744

745
  return false;
×
746
}
747

748
bool shellIsShowQuery(const char *sql) {
×
749
  // todo refactor
750
  if (taosStrCaseStr(sql, "show ") != NULL) {
×
751
    return true;
×
752
  }
753

754
  return false;
×
755
}
756

757
void init_dump_info(tsDumpInfo *dump_info, TAOS_RES *tres, const char *sql, bool vertical) {
78,676✔
758
  dump_info->sql = sql;
78,676✔
759
  dump_info->vertical = vertical;
78,676✔
760
  tsem_init(&dump_info->sem, 0, 0);
78,676✔
761
  dump_info->numOfAllRows = 0;
78,676✔
762

763
  dump_info->numFields = taos_num_fields(tres);
78,676✔
764
  dump_info->fields = taos_fetch_fields(tres);
78,676✔
765
  dump_info->precision = taos_result_precision(tres);
78,676✔
766

767
  dump_info->resShowMaxNum = UINT64_MAX;
78,676✔
768

769
  if (shell.args.commands == NULL && shell.args.file[0] == 0 && !shellIsShowWhole(dump_info->sql)) {
78,676!
770
    dump_info->resShowMaxNum = SHELL_DEFAULT_RES_SHOW_NUM;
×
771
  }
772

773
  if (vertical) {
78,676✔
774
    dump_info->maxColNameLen = 0;
62✔
775
    for (int32_t col = 0; col < dump_info->numFields; col++) {
168✔
776
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
106✔
777
      if (len > dump_info->maxColNameLen) {
106✔
778
        dump_info->maxColNameLen = len;
70✔
779
      }
780
    }
781
  } else {
782
    for (int32_t col = 0; col < dump_info->numFields; col++) {
226,217✔
783
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
147,603✔
784
    }
785
  }
786
}
78,676✔
787

788
void shellVerticalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
62✔
789
  TAOS_ROW row = taos_fetch_row(tres);
62✔
790
  if (row == NULL) {
62!
791
    printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
792
    return;
×
793
  }
794

795
  int64_t numOfPintRows = dump_info->numOfAllRows;
62✔
796
  int     numOfPrintRowsThisOne = 0;
62✔
797

798
  while (row != NULL) {
1,549!
799
    printf("*************************** %" PRId64 ".row ***************************\r\n", numOfPintRows + 1);
1,549✔
800

801
    int32_t *length = taos_fetch_lengths(tres);
1,549✔
802

803
    for (int32_t i = 0; i < dump_info->numFields; i++) {
3,472✔
804
      TAOS_FIELD *field = dump_info->fields + i;
1,923✔
805

806
      int32_t padding = (int32_t)(dump_info->maxColNameLen - strlen(field->name));
1,923✔
807
      printf("%*.s%s: ", padding, " ", field->name);
1,923✔
808

809
      shellPrintField((const char *)row[i], field, 0, length[i], dump_info->precision);
1,923✔
810
      putchar('\r');
1,923✔
811
      putchar('\n');
1,923✔
812
    }
813

814
    numOfPintRows++;
1,549✔
815
    numOfPrintRowsThisOne++;
1,549✔
816

817
    if (numOfPintRows == dump_info->resShowMaxNum) {
1,549!
818
      printf("\r\n");
×
819
      printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
×
820
      printf("         You can use the `LIMIT` clause to get fewer result to show.\r\n");
×
821
      printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
822
      printf("\r\n");
×
823
      printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
×
824
      printf("\r\n");
×
825
      return;
×
826
    }
827

828
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
1,549✔
829
      return;
62✔
830
    }
831

832
    row = taos_fetch_row(tres);
1,487✔
833
  }
834
  return;
×
835
}
836

837
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
147,747✔
838
  int32_t width = (int32_t)strlen(field->name);
147,747✔
839

840
  switch (field->type) {
147,747!
841
    case TSDB_DATA_TYPE_NULL:
×
842
      return TMAX(4, width);  // null
×
843
    case TSDB_DATA_TYPE_BOOL:
980✔
844
      return TMAX(5, width);  // 'false'
980✔
845

846
    case TSDB_DATA_TYPE_TINYINT:
1,487✔
847
    case TSDB_DATA_TYPE_UTINYINT:
848
      return TMAX(4, width);  // '-127'
1,487✔
849

850
    case TSDB_DATA_TYPE_SMALLINT:
4,035✔
851
    case TSDB_DATA_TYPE_USMALLINT:
852
      return TMAX(6, width);  // '-32767'
4,035✔
853

854
    case TSDB_DATA_TYPE_INT:
3,099✔
855
    case TSDB_DATA_TYPE_UINT:
856
      return TMAX(11, width);  // '-2147483648'
3,099✔
857

858
    case TSDB_DATA_TYPE_BIGINT:
30,112✔
859
    case TSDB_DATA_TYPE_UBIGINT:
860
      return TMAX(21, width);  // '-9223372036854775807'
30,112✔
861

862
    case TSDB_DATA_TYPE_FLOAT:
1,094✔
863
      return TMAX(SHELL_FLOAT_WIDTH, width);
1,094✔
864

865
    case TSDB_DATA_TYPE_DOUBLE:
19,602✔
866
      return TMAX(SHELL_DOUBLE_WIDTH, width);
19,602✔
867

868
    case TSDB_DATA_TYPE_BINARY:
45,713✔
869
    case TSDB_DATA_TYPE_GEOMETRY:
870
      if (field->bytes > shell.args.displayWidth) {
45,713✔
871
        return TMAX(shell.args.displayWidth, width);
39,106✔
872
      } else {
873
        return TMAX(field->bytes + 2, width);
6,607✔
874
      }
875
    case TSDB_DATA_TYPE_VARBINARY: {
10✔
876
      int32_t bytes = field->bytes * 2 + 2;
10✔
877
      if (bytes > shell.args.displayWidth) {
10!
878
        return TMAX(shell.args.displayWidth, width);
×
879
      } else {
880
        return TMAX(bytes + 2, width);
10✔
881
      }
882
    }
883
    case TSDB_DATA_TYPE_NCHAR:
8,212✔
884
    case TSDB_DATA_TYPE_JSON: {
885
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
8,212✔
886
      if (bytes > shell.args.displayWidth) {
8,212!
887
        return TMAX(shell.args.displayWidth, width);
8,212✔
888
      } else {
889
        return TMAX(bytes + 2, width);
×
890
      }
891
    }
892

893
    case TSDB_DATA_TYPE_TIMESTAMP:
33,403✔
894
      if (shell.args.is_raw_time) {
33,403✔
895
        return TMAX(14, width);
1✔
896
      }
897
      if (precision == TSDB_TIME_PRECISION_NANO) {
33,402✔
898
        return TMAX(29, width);
3✔
899
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
33,399✔
900
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
3✔
901
      } else {
902
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
33,396✔
903
      }
904

905
    default:
×
906
      ASSERT(false);
×
907
  }
908

909
  return 0;
×
910
}
911

912
void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields) {
73,557✔
913
  int32_t rowWidth = 0;
73,557✔
914
  for (int32_t col = 0; col < num_fields; col++) {
205,461✔
915
    TAOS_FIELD *field = fields + col;
131,904✔
916
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
131,904✔
917
    int32_t     left = padding / 2;
131,904✔
918
    printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
131,904✔
919
    rowWidth += width[col] + 3;
131,904✔
920
  }
921

922
  putchar('\r');
73,557✔
923
  putchar('\n');
73,557✔
924
  for (int32_t i = 0; i < rowWidth; i++) {
3,839,325✔
925
    putchar('=');
3,765,768✔
926
  }
927
  putchar('\r');
73,557✔
928
  putchar('\n');
73,557✔
929
}
73,557✔
930

931
void shellHorizontalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
76,125✔
932
  TAOS_ROW row = taos_fetch_row(tres);
76,125✔
933
  if (row == NULL) {
76,125!
934
    printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
935
    return;
×
936
  }
937

938
  int64_t numOfPintRows = dump_info->numOfAllRows;
76,125✔
939
  int     numOfPrintRowsThisOne = 0;
76,125✔
940
  if (numOfPintRows == 0) {
76,125✔
941
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
73,535✔
942
  }
943

944
  while (row != NULL) {
2,830,818!
945
    int32_t *length = taos_fetch_lengths(tres);
2,830,818✔
946
    for (int32_t i = 0; i < dump_info->numFields; i++) {
11,711,688✔
947
      putchar(' ');
8,880,870✔
948
      shellPrintField((const char *)row[i], dump_info->fields + i, dump_info->width[i], length[i],
8,880,870✔
949
                      dump_info->precision);
950
      putchar(' ');
8,880,870✔
951
      putchar('|');
8,880,870✔
952
    }
953
    putchar('\r');
2,830,818✔
954
    putchar('\n');
2,830,818✔
955

956
    numOfPintRows++;
2,830,818✔
957
    numOfPrintRowsThisOne++;
2,830,818✔
958

959
    if (numOfPintRows == dump_info->resShowMaxNum) {
2,830,818!
960
      printf("\r\n");
×
961
      printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
×
962
      if (shellIsShowQuery(dump_info->sql)) {
×
963
        printf("         You can use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
964
      } else {
965
        printf("         You can use the `LIMIT` clause to get fewer result to show.\r\n");
×
966
        printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
967
      }
968
      printf("\r\n");
×
969
      printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
×
970
      printf("\r\n");
×
971
      return;
×
972
    }
973

974
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
2,830,818✔
975
      return;
76,125✔
976
    }
977

978
    row = taos_fetch_row(tres);
2,754,693✔
979
  }
980
  return;
×
981
}
982

983
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
154,863✔
984
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
154,863✔
985
  if (num_of_rows > 0) {
154,863✔
986
    dump_info->numOfRows = num_of_rows;
76,187✔
987
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
76,187!
988
      if (dump_info->vertical) {
76,187✔
989
        shellVerticalPrintResult(tres, dump_info);
62✔
990
      } else {
991
        shellHorizontalPrintResult(tres, dump_info);
76,125✔
992
      }
993
    }
994
    dump_info->numOfAllRows += num_of_rows;
76,187✔
995
    if (!shellCmdkilled) {
76,187!
996
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
76,187✔
997
    } else {
998
      tsem_post(&dump_info->sem);
×
999
    }
1000
  } else {
1001
    if (num_of_rows < 0) {
78,676!
1002
      printf("\033[31masync retrieve failed, code: %d\033[0m\n", num_of_rows);
×
1003
    }
1004
    tsem_post(&dump_info->sem);
78,676✔
1005
  }
1006
}
154,863✔
1007

1008
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
78,719✔
1009
  int64_t num_of_rows = 0;
78,719✔
1010
  if (fname != NULL) {
78,719✔
1011
    num_of_rows = shellDumpResultToFile(fname, tres);
43✔
1012
  } else {
1013
    tsDumpInfo dump_info;
1014
    if (!shellCmdkilled) {
78,676!
1015
      init_dump_info(&dump_info, tres, sql, vertical);
78,676✔
1016
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
78,676✔
1017
      tsem_wait(&dump_info.sem);
78,676✔
1018
      num_of_rows = dump_info.numOfAllRows;
78,676✔
1019
    }
1020
  }
1021

1022
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
78,719!
1023
  return num_of_rows;
78,719✔
1024
}
1025

1026
void shellReadHistory() {
1,088✔
1027
  SShellHistory *pHistory = &shell.history;
1,088✔
1028
  TdFilePtr      pFile = taosOpenFile(pHistory->file, TD_FILE_READ | TD_FILE_STREAM);
1,088✔
1029
  if (pFile == NULL) return;
1,088!
1030

1031
  char   *line = taosMemoryMalloc(TSDB_MAX_ALLOWED_SQL_LEN + 1);
1,088!
1032
  int32_t read_size = 0;
1,088✔
1033
  while ((read_size = taosGetsFile(pFile, TSDB_MAX_ALLOWED_SQL_LEN, line)) > 0) {
4,775,457✔
1034
    line[read_size - 1] = '\0';
4,774,369✔
1035
    taosMemoryFree(pHistory->hist[pHistory->hend]);
4,774,369!
1036
    pHistory->hist[pHistory->hend] = taosStrdup(line);
4,774,369!
1037

1038
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
4,774,369✔
1039

1040
    if (pHistory->hend == pHistory->hstart) {
4,774,369✔
1041
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
3,687,457✔
1042
    }
1043
  }
1044

1045
  taosMemoryFreeClear(line);
1,088!
1046
  taosCloseFile(&pFile);
1,088✔
1047
  int64_t file_size;
1048
  if (taosStatFile(pHistory->file, &file_size, NULL, NULL) == 0 && file_size > SHELL_MAX_COMMAND_SIZE) {
1,088!
1049
    TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_TRUNC);
×
1050
    if (pFile == NULL) return;
×
1051
    int32_t endIndex = pHistory->hstart;
×
1052
    if (endIndex != 0) {
×
1053
      endIndex = pHistory->hend;
×
1054
    }
1055
    for (int32_t i = (pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE; i != endIndex;) {
×
1056
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
×
1057
      i = (i + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE;
×
1058
    }
1059
    taosFprintfFile(pFile, "%s\n", pHistory->hist[endIndex]);
×
1060

1061
    /* coverity[+retval] */
1062
    taosFsyncFile(pFile);
×
1063
    taosCloseFile(&pFile);
×
1064
  }
1065
  pHistory->hstart = pHistory->hend;
1,088✔
1066
}
1067

1068
void shellWriteHistory() {
1,088✔
1069
  SShellHistory *pHistory = &shell.history;
1,088✔
1070
  if (pHistory->hend == pHistory->hstart) return;
1,088✔
1071
  TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_APPEND);
1,087✔
1072
  if (pFile == NULL) return;
1,087!
1073

1074
  for (int32_t i = pHistory->hstart; i != pHistory->hend;) {
2,178✔
1075
    if (pHistory->hist[i] != NULL) {
1,091!
1076
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
1,091✔
1077
      taosMemoryFree(pHistory->hist[i]);
1,091!
1078
      pHistory->hist[i] = NULL;
1,091✔
1079
    }
1080
    i = (i + 1) % SHELL_MAX_HISTORY_SIZE;
1,091✔
1081
  }
1082
  taosCloseFile(&pFile);
1,087✔
1083
}
1084

1085
void shellCleanupHistory() {
1,088✔
1086
  SShellHistory *pHistory = &shell.history;
1,088✔
1087
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
1,089,088✔
1088
    if (pHistory->hist[i] != NULL) {
1,088,000✔
1089
      taosMemoryFree(pHistory->hist[i]);
1,086,909!
1090
      pHistory->hist[i] = NULL;
1,086,909✔
1091
    }
1092
  }
1093
}
1,088✔
1094

1095
void shellPrintError(TAOS_RES *tres, int64_t st) {
14✔
1096
  int64_t et = taosGetTimestampUs();
14✔
1097
  fprintf(stderr, "\r\nDB error: %s[0x%08X] (%.6fs)\r\n", taos_errstr(tres), taos_errno(tres), (et - st) / 1E6);
14✔
1098
  taos_free_result(tres);
14✔
1099
}
14✔
1100

1101
bool shellIsCommentLine(char *line) {
203,965✔
1102
  if (line == NULL) return true;
203,965!
1103
  return shellRegexMatch(line, "^\\s*#.*", REG_EXTENDED);
203,965✔
1104
}
1105

1106
void shellSourceFile(const char *file) {
826✔
1107
  int32_t read_len = 0;
826✔
1108
  char   *cmd = taosMemoryCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN + 1);
826!
1109
  size_t  cmd_len = 0;
826✔
1110
  char    fullname[PATH_MAX] = {0};
826✔
1111
  char    sourceFileCommand[PATH_MAX + 8] = {0};
826✔
1112

1113
  if (taosExpandDir(file, fullname, PATH_MAX) != 0) {
826!
1114
    tstrncpy(fullname, file, PATH_MAX);
×
1115
  }
1116

1117
  sprintf(sourceFileCommand, "source %s;", fullname);
826✔
1118
  shellRecordCommandToHistory(sourceFileCommand);
826✔
1119

1120
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_READ | TD_FILE_STREAM);
826✔
1121
  if (pFile == NULL) {
826✔
1122
    fprintf(stderr, "failed to open file %s\r\n", fullname);
24✔
1123
    taosMemoryFree(cmd);
24!
1124
    return;
24✔
1125
  }
1126

1127
  char *line = taosMemoryMalloc(TSDB_MAX_ALLOWED_SQL_LEN + 1);
802!
1128
  while ((read_len = taosGetsFile(pFile, TSDB_MAX_ALLOWED_SQL_LEN, line)) > 0) {
204,840✔
1129
    if (cmd_len + read_len >= TSDB_MAX_ALLOWED_SQL_LEN) {
204,038!
1130
      printf("read command line too long over 1M, ignore this line. cmd_len = %d read_len=%d \n", (int32_t)cmd_len,
×
1131
             read_len);
1132
      cmd_len = 0;
×
1133
      memset(line, 0, TSDB_MAX_ALLOWED_SQL_LEN + 1);
×
1134
      continue;
×
1135
    }
1136
    line[--read_len] = '\0';
204,038✔
1137

1138
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
204,038!
1139
      continue;
73✔
1140
    }
1141

1142
    if (line[read_len - 1] == '\\') {
203,965!
1143
      line[read_len - 1] = ' ';
×
1144
      memcpy(cmd + cmd_len, line, read_len);
×
1145
      cmd_len += read_len;
×
1146
      continue;
×
1147
    }
1148

1149
    if (line[read_len - 1] == '\r') {
203,965!
1150
      line[read_len - 1] = ' ';
×
1151
    }
1152

1153
    memcpy(cmd + cmd_len, line, read_len);
203,965✔
1154
    printf("%s%s\r\n", shell.info.promptHeader, cmd);
203,965✔
1155
    shellRunCommand(cmd, false);
203,965✔
1156
    memset(cmd, 0, TSDB_MAX_ALLOWED_SQL_LEN);
203,965✔
1157
    cmd_len = 0;
203,965✔
1158
  }
1159

1160
  taosMemoryFree(cmd);
802!
1161
  taosMemoryFreeClear(line);
802!
1162
  taosCloseFile(&pFile);
802✔
1163
}
1164

1165
int32_t shellGetGrantInfo(char *buf) {
5✔
1166
  int32_t verType = TSDB_VERSION_UNKNOWN;
5✔
1167
  char    sinfo[256] = {0};
5✔
1168
  tstrncpy(sinfo, taos_get_server_info(shell.conn), sizeof(sinfo));
5✔
1169
  strtok(sinfo, "\r\n");
5✔
1170

1171
  char sql[] = "show grants";
5✔
1172

1173
  TAOS_RES *tres = taos_query(shell.conn, sql);
5✔
1174

1175
  int32_t code = taos_errno(tres);
5✔
1176
  if (code != TSDB_CODE_SUCCESS) {
5!
1177
    if (code != TSDB_CODE_OPS_NOT_SUPPORT && code != TSDB_CODE_MND_NO_RIGHTS &&
×
1178
        code != TSDB_CODE_PAR_PERMISSION_DENIED) {
1179
      fprintf(stderr, "Failed to check Server Edition, Reason:0x%04x:%s\r\n\r\n", code, taos_errstr(tres));
×
1180
    }
1181
    taos_free_result(tres);
×
1182
    return verType;
×
1183
  }
1184

1185
  int32_t num_fields = taos_field_count(tres);
5✔
1186
  if (num_fields == 0) {
5!
1187
    fprintf(stderr, "\r\nInvalid grant information.\r\n");
×
1188
    exit(0);
×
1189
  } else {
1190
    if (tres == NULL) {
5!
1191
      fprintf(stderr, "\r\nGrant information is null.\r\n");
×
1192
      exit(0);
×
1193
    }
1194

1195
    TAOS_FIELD *fields = taos_fetch_fields(tres);
5✔
1196
    TAOS_ROW    row = taos_fetch_row(tres);
5✔
1197
    if (row == NULL) {
5!
1198
      fprintf(stderr, "\r\nFailed to get grant information from server. Abort.\r\n");
×
1199
      exit(0);
×
1200
    }
1201
    char serverVersion[64] = {0};
5✔
1202
    char expiretime[32] = {0};
5✔
1203
    char expired[32] = {0};
5✔
1204

1205
    tstrncpy(serverVersion, row[0], 64);
5✔
1206
    memcpy(expiretime, row[1], fields[1].bytes);
5✔
1207
    memcpy(expired, row[2], fields[2].bytes);
5✔
1208

1209
    if (strcmp(serverVersion, "community") == 0) {
5!
1210
      verType = TSDB_VERSION_OSS;
×
1211
    } else if (strcmp(expiretime, "unlimited") == 0) {
5!
1212
      verType = TSDB_VERSION_ENTERPRISE;
×
1213
      sprintf(buf, "Server is %s, %s and will never expire.\r\n", serverVersion, sinfo);
×
1214
    } else {
1215
      verType = TSDB_VERSION_ENTERPRISE;
5✔
1216
      sprintf(buf, "Server is %s, %s and will expire at %s.\r\n", serverVersion, sinfo, expiretime);
5✔
1217
    }
1218

1219
    taos_free_result(tres);
5✔
1220
  }
1221

1222
  fprintf(stdout, "\r\n");
5✔
1223
  return verType;
5✔
1224
}
1225

1226
#ifdef WINDOWS
1227
BOOL shellQueryInterruptHandler(DWORD fdwCtrlType) {
1228
  tsem_post(&shell.cancelSem);
1229
  return TRUE;
1230
}
1231
#else
1232
void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context) { tsem_post(&shell.cancelSem); }
6✔
1233
#endif
1234

1235
void shellCleanup(void *arg) { taosResetTerminalMode(); }
5✔
1236

1237
void *shellCancelHandler(void *arg) {
5✔
1238
  setThreadName("shellCancelHandler");
5✔
1239
  while (1) {
1240
    if (shell.exit == true) {
15✔
1241
      break;
5✔
1242
    }
1243

1244
    if (tsem_wait(&shell.cancelSem) != 0) {
10!
1245
      taosMsleep(10);
×
1246
      continue;
×
1247
    }
1248

1249
#ifdef WEBSOCKET
1250
    if (shell.args.restful || shell.args.cloud) {
10!
1251
      shell.stop_query = true;
×
1252
    } else {
1253
#endif
1254
      if (shell.conn) {
10✔
1255
        shellCmdkilled = true;
5✔
1256
        taos_kill_query(shell.conn);
5✔
1257
      }
1258
#ifdef WEBSOCKET
1259
    }
1260
#endif
1261
#ifdef WINDOWS
1262
    printf("\n%s", shell.info.promptHeader);
1263
#endif
1264
  }
1265

1266
  return NULL;
5✔
1267
}
1268

1269
#pragma GCC diagnostic push
1270
#pragma GCC diagnostic ignored "-Wstringop-overflow"
1271

1272
void *shellThreadLoop(void *arg) {
5✔
1273
  setThreadName("shellThreadLoop");
5✔
1274
  taosGetOldTerminalMode();
5✔
1275
  taosThreadCleanupPush(shellCleanup, NULL);
5!
1276

1277
  do {
1278
    char *command = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE);
5!
1279
    if (command == NULL) {
5!
1280
      printf("failed to malloc command\r\n");
×
1281
      break;
×
1282
    }
1283

1284
    do {
1285
      memset(command, 0, SHELL_MAX_COMMAND_SIZE);
10✔
1286
      taosSetTerminalMode();
10✔
1287

1288
      if (shellReadCommand(command) != 0) {
10✔
1289
        break;
5✔
1290
      }
1291

1292
      taosResetTerminalMode();
5✔
1293
    } while (shellRunCommand(command, true) == 0);
5!
1294

1295
    taosMemoryFreeClear(command);
5!
1296
    shellWriteHistory();
5✔
1297
    shellExit();
5✔
1298
  } while (0);
1299

1300
  taosThreadCleanupPop(1);
5✔
1301
  return NULL;
5✔
1302
}
1303
#pragma GCC diagnostic pop
1304

1305
int32_t shellExecute() {
1,116✔
1306
  printf(shell.info.clientVersion, shell.info.cusName, taos_get_client_info(), shell.info.cusName);
1,116✔
1307
  fflush(stdout);
1,116✔
1308

1309
  SShellArgs *pArgs = &shell.args;
1,116✔
1310
#ifdef WEBSOCKET
1311
  if (shell.args.restful || shell.args.cloud) {
1,116✔
1312
    if (shell_conn_ws_server(1)) {
19✔
1313
      printf("failed to connect to server, reason: %s[0x%08X]\n%s", ws_errstr(NULL), ws_errno(NULL), ERROR_CODE_DETAIL);
4✔
1314
      fflush(stdout);
4✔
1315
      return -1;
4✔
1316
    }
1317
  } else {
1318
#endif
1319
    if (shell.args.auth == NULL) {
1,097✔
1320
      shell.conn = taos_connect(pArgs->host, pArgs->user, pArgs->password, pArgs->database, pArgs->port);
1,093✔
1321
    } else {
1322
      shell.conn = taos_connect_auth(pArgs->host, pArgs->user, pArgs->auth, pArgs->database, pArgs->port);
4✔
1323
    }
1324

1325
    if (shell.conn == NULL) {
1,097✔
1326
      printf("failed to connect to server, reason: %s[0x%08X]\n%s", taos_errstr(NULL), taos_errno(NULL), ERROR_CODE_DETAIL);
24✔
1327
      fflush(stdout);
24✔
1328
      return -1;
24✔
1329
    }
1330
#ifdef WEBSOCKET
1331
  }
1332
#endif
1333

1334
  bool runOnce = pArgs->commands != NULL || pArgs->file[0] != 0;
1,088✔
1335
  shellSetConn(shell.conn, runOnce);
1,088✔
1336
  shellReadHistory();
1,088✔
1337

1338
  if (shell.args.is_bi_mode) {
1,088✔
1339
    // need set bi mode
1340
    printf("Set BI mode is true.\n");
4✔
1341
#ifndef WEBSOCKET
1342
    taos_set_conn_mode(shell.conn, TAOS_CONN_MODE_BI, 1);
1343
#endif
1344
  }
1345

1346
  if (runOnce) {
1,088✔
1347
    if (pArgs->commands != NULL) {
1,083✔
1348
      printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
261✔
1349
      char *cmd = taosStrdup(pArgs->commands);
261!
1350
      shellRunCommand(cmd, true);
261✔
1351
      taosMemoryFree(cmd);
261!
1352
    }
1353

1354
    if (pArgs->file[0] != 0) {
1,083✔
1355
      shellSourceFile(pArgs->file);
822✔
1356
    }
1357
#ifdef WEBSOCKET
1358
    if (shell.args.restful || shell.args.cloud) {
1,083✔
1359
      ws_close(shell.ws_conn);
15✔
1360
    } else {
1361
#endif
1362
      taos_close(shell.conn);
1,068✔
1363
#ifdef WEBSOCKET
1364
    }
1365
#endif
1366

1367
    shellWriteHistory();
1,083✔
1368
    shellCleanupHistory();
1,083✔
1369
    return 0;
1,083✔
1370
  }
1371

1372
  if (tsem_init(&shell.cancelSem, 0, 0) != 0) {
5!
1373
    printf("failed to create cancel semaphore\r\n");
×
1374
    return -1;
×
1375
  }
1376

1377
  TdThread spid = {0};
5✔
1378
  taosThreadCreate(&spid, NULL, shellCancelHandler, NULL);
5✔
1379

1380
  taosSetSignal(SIGTERM, shellQueryInterruptHandler);
5✔
1381
  taosSetSignal(SIGHUP, shellQueryInterruptHandler);
5✔
1382
  taosSetSignal(SIGINT, shellQueryInterruptHandler);
5✔
1383

1384
#ifdef WEBSOCKET
1385
  if (!shell.args.restful && !shell.args.cloud) {
5!
1386
#endif
1387
    char    buf[512] = {0};
5✔
1388
    int32_t verType = shellGetGrantInfo(buf);
5✔
1389
#ifndef WINDOWS
1390
    printfIntroduction(verType);
5✔
1391
#else
1392
#ifndef WEBSOCKET
1393
  if (verType == TSDB_VERSION_OSS) {
1394
    showAD(false);
1395
  }
1396
#endif
1397
#endif
1398
    // printf version
1399
    if (verType == TSDB_VERSION_ENTERPRISE || verType == TSDB_VERSION_CLOUD) {
5!
1400
      printf("%s\n", buf);
5✔
1401
    }
1402

1403
#ifdef WEBSOCKET
1404
  }
1405
#endif
1406
  while (1) {
1407
    taosThreadCreate(&shell.pid, NULL, shellThreadLoop, NULL);
5✔
1408
    taosThreadJoin(shell.pid, NULL);
5✔
1409
    taosThreadClear(&shell.pid);
5✔
1410
    if (shell.exit) {
5!
1411
      tsem_post(&shell.cancelSem);
5✔
1412
      break;
5✔
1413
    }
1414
  }
1415
#ifndef WEBSOCKET
1416
  // commnuity
1417
  if (verType == TSDB_VERSION_OSS) {
1418
    showAD(true);
1419
  }
1420
#endif
1421

1422
  taosThreadJoin(spid, NULL);
5✔
1423

1424
  shellCleanupHistory();
5✔
1425
  taos_kill_query(shell.conn);
5✔
1426
  taos_close(shell.conn);
5✔
1427

1428
  return 0;
5✔
1429
}
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