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

taosdata / TDengine / #3544

30 Nov 2024 03:06AM UTC coverage: 60.88% (+0.04%) from 60.842%
#3544

push

travis-ci

web-flow
Merge pull request #28988 from taosdata/main

merge: from main to 3.0 branch

120724 of 253479 branches covered (47.63%)

Branch coverage included in aggregate %.

407 of 489 new or added lines in 21 files covered. (83.23%)

1148 existing lines in 113 files now uncovered.

201919 of 276488 relevant lines covered (73.03%)

18898587.44 hits per line

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

68.04
/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 bool    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) {
803,179✔
70
  for (char c = *cmd++; c != 0; c = *cmd++) {
986,067✔
71
    if (c != ' ' && c != '\t' && c != ';') {
603,786!
72
      return false;
420,898✔
73
    }
74
  }
75
  return true;
382,281✔
76
}
77

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

81
  if (shellIsEmptyCommand(command)) {
593,338✔
82
    return 0;
382,281✔
83
  }
84

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

89
  if (shellRegexMatch(command, "^[\t ]*clear[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
211,057!
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;]*$",
211,057!
98
                      REG_EXTENDED | REG_ICASE)) {
99
    strtok(command, " \t");
×
100
    strtok(NULL, " \t");
×
101
    char *p = strtok(NULL, " \t");
×
102
    if (strncasecmp(p, "default", 7) == 0) {
×
103
      shell.args.displayWidth = SHELL_DEFAULT_MAX_BINARY_DISPLAY_WIDTH;
×
104
    } else {
105
      int32_t displayWidth = atoi(p);
×
106
      displayWidth = TRANGE(displayWidth, 1, 10 * 1024);
×
107
      shell.args.displayWidth = displayWidth;
×
108
    }
109
    return 0;
×
110
  }
111

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

139
void shellRecordCommandToHistory(char *command) {
2,245✔
140
  if (strncasecmp(command, "create user ", 12) == 0 || strncasecmp(command, "alter user ", 11) == 0) {
2,245!
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;
2,245✔
148
  if (pHistory->hstart == pHistory->hend ||
2,245✔
149
      pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE] == NULL ||
1!
150
      strcmp(command, pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE]) != 0) {
1!
151
    if (pHistory->hist[pHistory->hend] != NULL) {
2,245!
152
      taosMemoryFreeClear(pHistory->hist[pHistory->hend]);
2,245!
153
    }
154
    pHistory->hist[pHistory->hend] = taosStrdup(command);
2,245✔
155

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

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

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

176
  if (recordHistory) shellRecordCommandToHistory(command);
209,841✔
177

178
  char quote = 0, *cmd = command;
209,841✔
179
  for (char c = *command++; c != 0; c = *command++) {
157,868,728✔
180
    if (c == '\\' && (*command == '\'' || *command == '"' || *command == '`')) {
157,658,887!
181
      command++;
2✔
182
      continue;
2✔
183
    }
184

185
    if (quote == c) {
157,658,885✔
186
      quote = 0;
2,188,746✔
187
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
155,470,139✔
188
      quote = c;
2,188,746✔
189
    } else if (c == ';' && quote == 0) {
153,281,393✔
190
      c = *command;
383,497✔
191
      *command = 0;
383,497✔
192
      if (shellRunSingleCommand(cmd) < 0) {
383,497!
193
        return -1;
×
194
      }
195
      *command = c;
383,497✔
196
      cmd = command;
383,497✔
197
    }
198
  }
199
  return shellRunSingleCommand(cmd);
209,841✔
200
}
201

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

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

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

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

222
  return NULL;
210,997✔
223
}
224

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

232
  if ((sptr = strstr(command, ">>")) != NULL) {
211,056✔
233
    fname = sptr + 2;
43✔
234
    while (*fname == ' ') fname++;
74✔
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) {
211,056✔
244
    *sptr = '\0';
59✔
245
    printMode = true;  // When output to a file, the switch does not work.
59✔
246
  }
247

248
  st = taosGetTimestampUs();
211,056✔
249

250
  TAOS_RES *pSql = taos_query(shell.conn, command);
211,056✔
251
  if (taos_errno(pSql)) {
211,056✔
252
    shellPrintError(pSql, st);
2,849✔
253
    return;
2,849✔
254
  }
255

256
  if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) {
208,207✔
257
    fprintf(stdout, "Database changed.\r\n\r\n");
170✔
258
    fflush(stdout);
170✔
259

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

263
    taos_free_result(pSql);
170✔
264

265
    return;
170✔
266
  }
267

268
  // pre string
269
  char *pre = "Query OK";
208,037✔
270
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
208,037✔
271
    pre = "Delete OK";
25✔
272
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
208,012✔
273
    pre = "Insert OK";
117,975✔
274
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
90,037✔
275
    pre = "Create OK";
4,139✔
276
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
85,898✔
277
    pre = "Drop OK";
152✔
278
  }
279

280
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
208,037✔
281
  if (pFields != NULL) {  // select and show kinds of commands
208,037✔
282
    int32_t error_no = 0;
79,593✔
283

284
    int64_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode, command);
79,593✔
285
    if (numOfRows < 0) return;
79,593!
286

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

302
    // call auto tab
303
    callbackAutoTab(command, NULL, false);
128,444✔
304
  }
305

306
  printf("\r\n");
208,037✔
307
}
308

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

315
  time_t  tt;
316
  int32_t ms = 0;
4,061,317✔
317
  if (precision == TSDB_TIME_PRECISION_NANO) {
4,061,317!
318
    tt = (time_t)(val / 1000000000);
×
319
    ms = val % 1000000000;
×
320
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
4,061,317!
321
    tt = (time_t)(val / 1000000);
×
322
    ms = val % 1000000;
×
323
  } else {
324
    tt = (time_t)(val / 1000);
4,061,317✔
325
    ms = val % 1000;
4,061,317✔
326
  }
327

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

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

345
  if (precision == TSDB_TIME_PRECISION_NANO) {
4,061,317!
346
    sprintf(buf + pos, ".%09d", ms);
×
347
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
4,061,317!
348
    sprintf(buf + pos, ".%06d", ms);
×
349
  } else {
350
    sprintf(buf + pos, ".%03d", ms);
4,061,317✔
351
  }
352

353
  return buf;
4,061,317✔
354
}
355

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

362
  return buf;
×
363
}
364

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

371
  char    quotationStr[2] = {'"', 0};
2,173,217✔
372
  int32_t width;
373

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

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

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

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

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

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

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

507
  int64_t numOfRows = 0;
43✔
508
  do {
509
    int32_t *length = taos_fetch_lengths(tres);
323,439✔
510
    for (int32_t i = 0; i < num_fields; i++) {
2,509,422✔
511
      if (i > 0) {
2,185,983✔
512
        taosFprintfFile(pFile, ",");
1,862,544✔
513
      }
514
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
2,185,983✔
515
    }
516
    taosFprintfFile(pFile, "\r\n");
323,439✔
517

518
    numOfRows++;
323,439✔
519
    row = taos_fetch_row(tres);
323,439✔
520
  } while (row != NULL);
323,439✔
521

522
  taosCloseFile(&pFile);
43✔
523

524
  return numOfRows;
43✔
525
}
526

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

531
  while (pos < length) {
45,401,177✔
532
    TdWchar wc;
533
    int32_t bytes = taosMbToWchar(&wc, str + pos, MB_CUR_MAX);
44,500,281✔
534
    if (bytes <= 0) {
44,500,281✔
535
      break;
801,703✔
536
    }
537

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

549
    if (w <= 0) {
44,500,272!
550
      continue;
84,719✔
551
    }
552

553
    if (width <= 0) {
44,500,272✔
554
      printf("%lc", wc);
84,719✔
555
      continue;
84,719✔
556
    }
557

558
    totalCols += w;
44,415,553✔
559
    if (totalCols > width) {
44,415,553✔
560
      break;
801,694✔
561
    }
562
    if (totalCols <= (width - 3)) {
43,613,859✔
563
      printf("%lc", wc);
40,808,818✔
564
      cols += w;
40,808,818✔
565
    } else {
566
      tail[tailLen] = wc;
2,805,041✔
567
      tailLen++;
2,805,041✔
568
    }
569
  }
570

571
  if (totalCols > width) {
1,702,599✔
572
    // width could be 1 or 2, so printf("...") cannot be used
573
    for (int32_t i = 0; i < 3; i++) {
3,206,776✔
574
      if (cols >= width) {
2,405,082!
575
        break;
×
576
      }
577
      putchar('.');
2,405,082✔
578
      ++cols;
2,405,082✔
579
    }
580
  } else {
581
    for (int32_t i = 0; i < tailLen; i++) {
1,300,873✔
582
      printf("%lc", tail[i]);
399,968✔
583
    }
584
    cols = totalCols;
900,905✔
585
  }
586

587
  for (; cols < width; cols++) {
8,897,910✔
588
    putchar(' ');
7,195,311✔
589
  }
590
}
1,702,599✔
591

592
void shellPrintString(const char *str, int32_t width) {
1,733,693✔
593
  int32_t len = strlen(str);
1,733,693✔
594

595
  if (width == 0) {
1,733,693!
596
    printf("%s", str);
×
597
  } else if (len > width) {
1,733,693!
598
    if (width <= 3) {
×
599
      printf("%.*s.", width - 1, str);
×
600
    } else {
601
      printf("%.*s...", width - 3, str);
×
602
    }
603
  } else {
604
    printf("%s%*.s", str, width - len, "");
1,733,693✔
605
  }
606
}
1,733,693✔
607

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

614
  int32_t code = TSDB_CODE_FAILED;
×
615

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

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

629
  shellPrintString(outputWKT, width);
×
630

631
  geosFreeBuffer(outputWKT);
×
632
}
633

634
void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision) {
8,876,023✔
635
  if (val == NULL) {
8,876,023✔
636
    shellPrintString(TSDB_DATA_NULL_STR, width);
1,657,324✔
637
    return;
1,657,324✔
638
  }
639

640
  int n = 0;
7,218,699✔
641
#define LENGTH 64
642
  char buf[LENGTH] = {0};
7,218,699✔
643
  switch (field->type) {
7,218,699!
644
    case TSDB_DATA_TYPE_BOOL:
76,369✔
645
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
76,369✔
646
      break;
76,369✔
647
    case TSDB_DATA_TYPE_TINYINT:
133,300✔
648
      printf("%*d", width, *((int8_t *)val));
133,300✔
649
      break;
133,300✔
650
    case TSDB_DATA_TYPE_UTINYINT:
109✔
651
      printf("%*u", width, *((uint8_t *)val));
109✔
652
      break;
109✔
653
    case TSDB_DATA_TYPE_SMALLINT:
108,602✔
654
      printf("%*d", width, *((int16_t *)val));
108,602✔
655
      break;
108,602✔
656
    case TSDB_DATA_TYPE_USMALLINT:
×
657
      printf("%*u", width, *((uint16_t *)val));
×
658
      break;
×
659
    case TSDB_DATA_TYPE_INT:
103,167✔
660
      printf("%*d", width, *((int32_t *)val));
103,167✔
661
      break;
103,167✔
662
    case TSDB_DATA_TYPE_UINT:
×
663
      printf("%*u", width, *((uint32_t *)val));
×
664
      break;
×
665
    case TSDB_DATA_TYPE_BIGINT:
940,627✔
666
      printf("%*" PRId64, width, *((int64_t *)val));
940,627✔
667
      break;
940,627✔
668
    case TSDB_DATA_TYPE_UBIGINT:
×
669
      printf("%*" PRIu64, width, *((uint64_t *)val));
×
670
      break;
×
671
    case TSDB_DATA_TYPE_FLOAT:
92,177✔
672
      if (tsEnableScience) {
92,177!
673
        printf("%*.7e", width, GET_FLOAT_VAL(val));
×
674
      } else {
675
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
92,177✔
676
        if (n > SHELL_FLOAT_WIDTH) {
92,177✔
677
          printf("%*.7e", width, GET_FLOAT_VAL(val));
11,969✔
678
        } else {
679
          printf("%s", buf);
80,208✔
680
        }
681
      }
682
      break;
92,177✔
683
    case TSDB_DATA_TYPE_DOUBLE:
453,867✔
684
      if (tsEnableScience) {
453,867!
685
        snprintf(buf, LENGTH, "%*.15e", width, GET_DOUBLE_VAL(val));
×
686
        printf("%s", buf);
×
687
      } else {
688
        n = tsnprintf(buf, LENGTH, "%*.15f", width, GET_DOUBLE_VAL(val));
453,867✔
689
        if (n > SHELL_DOUBLE_WIDTH) {
453,867✔
690
          printf("%*.15e", width, GET_DOUBLE_VAL(val));
62,163✔
691
        } else {
692
          printf("%*s", width, buf);
391,704✔
693
        }
694
      }
695
      break;
453,867✔
696
    case TSDB_DATA_TYPE_VARBINARY: {
×
697
      void    *data = NULL;
×
698
      uint32_t size = 0;
×
699
      if (taosAscii2Hex(val, length, &data, &size) < 0) {
×
700
        break;
×
701
      }
702
      shellPrintNChar(data, size, width);
×
703
      taosMemoryFree(data);
×
704
      break;
×
705
    }
706
    case TSDB_DATA_TYPE_BINARY:
1,702,599✔
707
    case TSDB_DATA_TYPE_NCHAR:
708
    case TSDB_DATA_TYPE_JSON:
709
      shellPrintNChar(val, length, width);
1,702,599✔
710
      break;
1,702,599✔
711
    case TSDB_DATA_TYPE_GEOMETRY:
×
712
      shellPrintGeometry(val, length, width);
×
713
      break;
×
714
    case TSDB_DATA_TYPE_TIMESTAMP:
3,607,882✔
715
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
3,607,882✔
716
      printf("%s", buf);
3,607,882✔
717
      break;
3,607,882✔
718
    default:
×
719
      break;
×
720
  }
721
}
722

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

746
  return false;
×
747
}
748

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

755
  return false;
×
756
}
757

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

764
  dump_info->numFields = taos_num_fields(tres);
79,550✔
765
  dump_info->fields = taos_fetch_fields(tres);
79,550✔
766
  dump_info->precision = taos_result_precision(tres);
79,550✔
767

768
  dump_info->resShowMaxNum = UINT64_MAX;
79,550✔
769

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

774
  if (vertical) {
79,550✔
775
    dump_info->maxColNameLen = 0;
59✔
776
    for (int32_t col = 0; col < dump_info->numFields; col++) {
120✔
777
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
61✔
778
      if (len > dump_info->maxColNameLen) {
61!
779
        dump_info->maxColNameLen = len;
61✔
780
      }
781
    }
782
  } else {
783
    for (int32_t col = 0; col < dump_info->numFields; col++) {
227,049✔
784
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
147,558✔
785
    }
786
  }
787
}
79,550✔
788

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

796
  int64_t numOfPintRows = dump_info->numOfAllRows;
59✔
797
  int     numOfPrintRowsThisOne = 0;
59✔
798

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

802
    int32_t *length = taos_fetch_lengths(tres);
1,473✔
803

804
    for (int32_t i = 0; i < dump_info->numFields; i++) {
2,950✔
805
      TAOS_FIELD *field = dump_info->fields + i;
1,477✔
806

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

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

815
    numOfPintRows++;
1,473✔
816
    numOfPrintRowsThisOne++;
1,473✔
817

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

829
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
1,473✔
830
      return;
59✔
831
    }
832

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

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

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

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

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

855
    case TSDB_DATA_TYPE_INT:
2,960✔
856
    case TSDB_DATA_TYPE_UINT:
857
      return TMAX(11, width);  // '-2147483648'
2,960✔
858

859
    case TSDB_DATA_TYPE_BIGINT:
29,794✔
860
    case TSDB_DATA_TYPE_UBIGINT:
861
      return TMAX(21, width);  // '-9223372036854775807'
29,794✔
862

863
    case TSDB_DATA_TYPE_FLOAT:
1,010✔
864
      return TMAX(SHELL_FLOAT_WIDTH, width);
1,010✔
865

866
    case TSDB_DATA_TYPE_DOUBLE:
19,533✔
867
      return TMAX(SHELL_DOUBLE_WIDTH, width);
19,533✔
868

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

894
    case TSDB_DATA_TYPE_TIMESTAMP:
32,802✔
895
      if (shell.args.is_raw_time) {
32,802!
896
        return TMAX(14, width);
×
897
      }
898
      if (precision == TSDB_TIME_PRECISION_NANO) {
32,802!
899
        return TMAX(29, width);
×
900
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
32,802!
901
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
×
902
      } else {
903
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
32,802✔
904
      }
905

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

910
  return 0;
×
911
}
912

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

923
  putchar('\r');
74,445✔
924
  putchar('\n');
74,445✔
925
  for (int32_t i = 0; i < rowWidth; i++) {
3,852,531✔
926
    putchar('=');
3,778,086✔
927
  }
928
  putchar('\r');
74,445✔
929
  putchar('\n');
74,445✔
930
}
74,445✔
931

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

939
  int64_t numOfPintRows = dump_info->numOfAllRows;
77,096✔
940
  int     numOfPrintRowsThisOne = 0;
77,096✔
941
  if (numOfPintRows == 0) {
77,096✔
942
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
74,445✔
943
  }
944

945
  while (row != NULL) {
2,828,276!
946
    int32_t *length = taos_fetch_lengths(tres);
2,828,276✔
947
    for (int32_t i = 0; i < dump_info->numFields; i++) {
11,702,822✔
948
      putchar(' ');
8,874,546✔
949
      shellPrintField((const char *)row[i], dump_info->fields + i, dump_info->width[i], length[i],
8,874,546✔
950
                      dump_info->precision);
951
      putchar(' ');
8,874,546✔
952
      putchar('|');
8,874,546✔
953
    }
954
    putchar('\r');
2,828,276✔
955
    putchar('\n');
2,828,276✔
956

957
    numOfPintRows++;
2,828,276✔
958
    numOfPrintRowsThisOne++;
2,828,276✔
959

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

975
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
2,828,276✔
976
      return;
77,096✔
977
    }
978

979
    row = taos_fetch_row(tres);
2,751,180✔
980
  }
981
  return;
×
982
}
983

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

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

1023
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
79,593!
1024
  return num_of_rows;
79,593✔
1025
}
1026

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

1032
  char   *line = taosMemoryMalloc(TSDB_MAX_ALLOWED_SQL_LEN + 1);
2,244✔
1033
  int32_t read_size = 0;
2,244✔
1034
  while ((read_size = taosGetsFile(pFile, TSDB_MAX_ALLOWED_SQL_LEN, line)) > 0) {
6,279,638✔
1035
    line[read_size - 1] = '\0';
6,277,394✔
1036
    taosMemoryFree(pHistory->hist[pHistory->hend]);
6,277,394✔
1037
    pHistory->hist[pHistory->hend] = taosStrdup(line);
6,277,394✔
1038

1039
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
6,277,394✔
1040

1041
    if (pHistory->hend == pHistory->hstart) {
6,277,394✔
1042
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
4,035,638✔
1043
    }
1044
  }
1045

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

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

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

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

1086
void shellCleanupHistory() {
2,244✔
1087
  SShellHistory *pHistory = &shell.history;
2,244✔
1088
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
2,246,244✔
1089
    if (pHistory->hist[i] != NULL) {
2,244,000✔
1090
      taosMemoryFree(pHistory->hist[i]);
2,241,755✔
1091
      pHistory->hist[i] = NULL;
2,241,755✔
1092
    }
1093
  }
1094
}
2,244✔
1095

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

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

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

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

1118
  sprintf(sourceFileCommand, "source %s;", fullname);
827✔
1119
  shellRecordCommandToHistory(sourceFileCommand);
827✔
1120

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

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

1139
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
208,508!
1140
      continue;
85✔
1141
    }
1142

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

1150
    if (line[read_len - 1] == '\r') {
208,423!
1151
      line[read_len - 1] = ' ';
×
1152
    }
1153

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

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

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

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

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

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

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

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

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

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

1220
    taos_free_result(tres);
5✔
1221
  }
1222

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

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

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

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

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

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

1267
  return NULL;
5✔
1268
}
1269

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

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

1282
    do {
1283
      memset(command, 0, SHELL_MAX_COMMAND_SIZE);
10✔
1284
      taosSetTerminalMode();
10✔
1285

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

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

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

1298
  taosThreadCleanupPop(1);
5✔
1299
  return NULL;
5✔
1300
}
1301

1302
int32_t shellExecute() {
2,255✔
1303
  printf(shell.info.clientVersion, shell.info.cusName, taos_get_client_info(), shell.info.cusName);
2,255✔
1304
  fflush(stdout);
2,255✔
1305

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

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

1331
  bool runOnce = pArgs->commands != NULL || pArgs->file[0] != 0;
2,244✔
1332
  shellSetConn(shell.conn, runOnce);
2,244✔
1333
  shellReadHistory();
2,244✔
1334

1335
  if (shell.args.is_bi_mode) {
2,244!
1336
    // need set bi mode
1337
    printf("Set BI mode is true.\n");
×
1338
#ifndef WEBSOCKET
1339
    taos_set_conn_mode(shell.conn, TAOS_CONN_MODE_BI, 1);
×
1340
#endif
1341
  }
1342

1343
  if (runOnce) {
2,244✔
1344
    if (pArgs->commands != NULL) {
2,239✔
1345
      printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
1,413✔
1346
      char *cmd = taosStrdup(pArgs->commands);
1,413✔
1347
      shellRunCommand(cmd, true);
1,413✔
1348
      taosMemoryFree(cmd);
1,413✔
1349
    }
1350

1351
    if (pArgs->file[0] != 0) {
2,239✔
1352
      shellSourceFile(pArgs->file);
826✔
1353
    }
1354
#ifdef WEBSOCKET
1355
    if (shell.args.restful || shell.args.cloud) {
1356
      ws_close(shell.ws_conn);
1357
    } else {
1358
#endif
1359
      taos_close(shell.conn);
2,239✔
1360
#ifdef WEBSOCKET
1361
    }
1362
#endif
1363

1364
    shellWriteHistory();
2,239✔
1365
    shellCleanupHistory();
2,239✔
1366
    return 0;
2,239✔
1367
  }
1368

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

1374
  TdThread spid = {0};
5✔
1375
  taosThreadCreate(&spid, NULL, shellCancelHandler, NULL);
5✔
1376

1377
  taosSetSignal(SIGTERM, shellQueryInterruptHandler);
5✔
1378
  taosSetSignal(SIGHUP, shellQueryInterruptHandler);
5✔
1379
  taosSetSignal(SIGINT, shellQueryInterruptHandler);
5✔
1380

1381
#ifdef WEBSOCKET
1382
  if (!shell.args.restful && !shell.args.cloud) {
1383
#endif
1384
    char *buf = taosMemoryMalloc(512);
5✔
1385
    bool  community = shellGetGrantInfo(buf);
5✔
1386
#ifndef WINDOWS
1387
    printfIntroduction(community);
5✔
1388
#else
1389
#ifndef WEBSOCKET
1390
  if (community) {
1391
    showAD(false);
1392
  }
1393
#endif
1394
#endif
1395
    // printf version
1396
    if (!community) {
5!
1397
      printf("%s\n", buf);
5✔
1398
    }
1399
    taosMemoryFree(buf);
5✔
1400

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

1420
  taosThreadJoin(spid, NULL);
5✔
1421

1422
  shellCleanupHistory();
5✔
1423
  taos_kill_query(shell.conn);
5✔
1424
  taos_close(shell.conn);
5✔
1425

1426
  return 0;
5✔
1427
}
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

© 2025 Coveralls, Inc