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

taosdata / TDengine / #4873

04 Dec 2025 01:55AM UTC coverage: 64.558% (-0.1%) from 64.678%
#4873

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

719 of 2219 new or added lines in 36 files covered. (32.4%)

6363 existing lines in 135 files now uncovered.

159381 of 246882 relevant lines covered (64.56%)

108937395.15 hits per line

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

77.62
/tools/shell/src/shellEngine.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define ALLOW_FORBID_FUNC
17
#define _BSD_SOURCE
18
#define _GNU_SOURCE
19
#define _XOPEN_SOURCE
20
#define _DEFAULT_SOURCE
21
#include "../../inc/pub.h"
22
#include "geosWrapper.h"
23
#include "shellAuto.h"
24
#include "shellInt.h"
25

26
SShellObj shell = {0};
27

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

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

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

42
  uint64_t resShowMaxNum;
43
} tsDumpInfo;
44

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

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

68
static bool shellCmdkilled = false;
69

70
bool shellIsEmptyCommand(const char *cmd) {
18,238,618✔
71
  for (char c = *cmd++; c != 0; c = *cmd++) {
18,290,450✔
72
    if (c != ' ' && c != '\t' && c != ';') {
13,649,943✔
73
      return false;
13,598,111✔
74
    }
75
  }
76
  return true;
4,640,507✔
77
}
78

79
int32_t shellRunSingleCommand(char *command) {
11,465,754✔
80
  shellCmdkilled = false;
11,465,754✔
81

82
  if (shellIsEmptyCommand(command)) {
11,465,754✔
83
    return 0;
4,640,507✔
84
  }
85

86
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
6,825,247✔
87
    return -1;
569✔
88
  }
89

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

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

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

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

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

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

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

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

173
  if (recordHistory) shellRecordCommandToHistory(command);
6,772,297✔
174

175
  char quote = 0, *cmd = command;
6,772,297✔
176
  for (char c = *command++; c != 0; c = *command++) {
601,914,655✔
177
    if (c == '\\' && (*command == '\'' || *command == '"' || *command == '`')) {
595,142,927✔
178
      command++;
3,376✔
179
      continue;
3,376✔
180
    }
181

182
    if (quote == c) {
595,139,551✔
183
      quote = 0;
2,300,643✔
184
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
592,838,908✔
185
      quote = c;
2,300,643✔
186
    } else if (c == ';' && quote == 0) {
590,538,265✔
187
      c = *command;
4,694,026✔
188
      *command = 0;
4,694,026✔
189
      if (shellRunSingleCommand(cmd) < 0) {
4,694,026✔
190
        return -1;
569✔
191
      }
192
      *command = c;
4,693,457✔
193
      cmd = command;
4,693,457✔
194
    }
195
  }
196
  return shellRunSingleCommand(cmd);
6,771,728✔
197
}
198

199
char *strendG(const char *pstr) {
6,823,552✔
200
  if (pstr == NULL) {
6,823,552✔
201
    return NULL;
×
202
  }
203

204
  size_t len = strlen(pstr);
6,823,552✔
205
  if (len < 4) {
6,823,552✔
206
    return NULL;
×
207
  }
208

209
  char *p = (char *)pstr + len - 2;
6,823,552✔
210
  if (strcmp(p, "\\G") == 0) {
6,823,552✔
211
    return p;
7,264✔
212
  }
213

214
  p = (char *)pstr + len - 3;
6,816,288✔
215
  if (strcmp(p, "\\G;") == 0) {
6,816,288✔
216
    return p;
55,841✔
217
  }
218

219
  return NULL;
6,760,447✔
220
}
221

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

229
  if ((sptr = strstr(command, ">>")) != NULL) {
6,823,552✔
230
    fname = sptr + 2;
979,738✔
231
    while (*fname == ' ') fname++;
1,958,344✔
232
    *sptr = '\0';
979,738✔
233

234
    cptr = strstr(fname, ";");
979,738✔
235
    if (cptr != NULL) {
979,738✔
236
      *cptr = '\0';
497✔
237
    }
238
  }
239

240
  if ((sptr = strendG(command)) != NULL) {
6,823,552✔
241
    *sptr = '\0';
63,105✔
242
    printMode = true;  // When output to a file, the switch does not work.
63,105✔
243
  }
244

245
  st = taosGetTimestampUs();
6,823,552✔
246

247
  TAOS_RES *pSql = taos_query(shell.conn, command);
6,823,552✔
248
  if (taos_errno(pSql)) {
6,823,552✔
249
    shellPrintError(pSql, st);
1,981,163✔
250
    return;
1,981,163✔
251
  }
252

253
  if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) {
4,842,389✔
254
    printf("Database changed.\r\n\r\n");
10,779✔
255

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

259
    taos_free_result(pSql);
10,779✔
260

261
    return;
10,779✔
262
  }
263

264
  // pre string
265
  char *pre = "Query OK";
4,831,610✔
266
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
4,831,610✔
267
    pre = "Delete OK";
281✔
268
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
4,831,329✔
269
    pre = "Insert OK";
9,550✔
270
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
4,821,779✔
271
    pre = "Create OK";
11,130✔
272
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
4,810,649✔
273
    pre = "Drop OK";
14,642✔
274
  }
275

276
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
4,831,610✔
277
  if (pFields != NULL) {  // select and show kinds of commands
4,831,610✔
278
    int32_t error_no = 0;
3,529,607✔
279

280
    int64_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode, command);
3,529,607✔
281
    if (numOfRows < 0) return;
3,529,607✔
282

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

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

301
  printf("\r\n");
4,831,610✔
302
}
303

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

310
  time_t  tt;
186,810,379✔
311
  int32_t ms = 0;
186,927,114✔
312
  if (precision == TSDB_TIME_PRECISION_NANO) {
186,927,114✔
313
    tt = (time_t)(val / 1000000000);
5,630✔
314
    ms = val % 1000000000;
5,630✔
315
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
186,921,484✔
316
    tt = (time_t)(val / 1000000);
5,630✔
317
    ms = val % 1000000;
5,630✔
318
  } else {
319
    tt = (time_t)(val / 1000);
186,915,854✔
320
    ms = val % 1000;
186,915,854✔
321
  }
322

323
  if (tt <= 0 && ms < 0) {
186,927,114✔
324
    tt--;
11,066✔
325
    if (precision == TSDB_TIME_PRECISION_NANO) {
11,066✔
326
      ms += 1000000000;
×
327
    } else if (precision == TSDB_TIME_PRECISION_MICRO) {
11,066✔
328
      ms += 1000000;
×
329
    } else {
330
      ms += 1000;
11,066✔
331
    }
332
  }
333

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

340
  if (precision == TSDB_TIME_PRECISION_NANO) {
186,927,114✔
341
    sprintf(buf + pos, ".%09d", ms);
5,630✔
342
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
186,921,484✔
343
    sprintf(buf + pos, ".%06d", ms);
5,630✔
344
  } else {
345
    sprintf(buf + pos, ".%03d", ms);
186,915,854✔
346
  }
347

348
  return buf;
186,927,114✔
349
}
350

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

357
  return buf;
×
358
}
359

360
void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, int32_t length, int32_t precision) {
1,529,246,944✔
361
  if (val == NULL) {
1,529,246,944✔
362
    taosFprintfFile(pFile, "NULL");
9,415,419✔
363
    return;
9,415,419✔
364
  }
365

366
  char    quotationStr[2] = {'"', 0};
1,519,831,525✔
367
  int32_t width;
368

369
  int n = 0;
1,519,831,525✔
370
#define LENGTH 64
371
  char buf[LENGTH] = {0};
1,519,831,525✔
372
  switch (field->type) {
1,519,831,525✔
373
    case TSDB_DATA_TYPE_BOOL:
535,530,144✔
374
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
535,530,144✔
375
      break;
535,530,144✔
376
    case TSDB_DATA_TYPE_TINYINT:
35,119,933✔
377
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
35,119,933✔
378
      break;
35,119,933✔
379
    case TSDB_DATA_TYPE_UTINYINT:
1,535,911✔
380
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
1,535,911✔
381
      break;
1,535,911✔
382
    case TSDB_DATA_TYPE_SMALLINT:
1,534,933✔
383
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
1,534,933✔
384
      break;
1,534,933✔
385
    case TSDB_DATA_TYPE_USMALLINT:
1,552,751✔
386
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
1,552,751✔
387
      break;
1,552,751✔
388
    case TSDB_DATA_TYPE_INT:
46,696,492✔
389
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
46,696,492✔
390
      break;
46,696,492✔
391
    case TSDB_DATA_TYPE_UINT:
1,559,047✔
392
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
1,559,047✔
393
      break;
1,559,047✔
394
    case TSDB_DATA_TYPE_BIGINT:
119,652,842✔
395
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
119,652,842✔
396
      break;
119,652,842✔
397
    case TSDB_DATA_TYPE_UBIGINT:
2,306,980✔
398
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
2,306,980✔
399
      break;
2,306,980✔
400
    case TSDB_DATA_TYPE_FLOAT:
35,125,553✔
401
      width = SHELL_FLOAT_WIDTH;
35,125,553✔
402
      if (tsEnableScience) {
35,125,553✔
403
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
404
      } else {
405
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
35,125,553✔
406
        if (n > SHELL_FLOAT_WIDTH) {
35,125,553✔
407
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
406,844✔
408
        } else {
409
          taosFprintfFile(pFile, "%s", buf);
34,718,709✔
410
        }
411
      }
412
      break;
35,125,553✔
413
    case TSDB_DATA_TYPE_DOUBLE:
219,488,632✔
414
      width = SHELL_DOUBLE_WIDTH;
219,488,632✔
415
      if (tsEnableScience) {
219,488,632✔
416
        snprintf(buf, LENGTH, "%*.15e", width, GET_DOUBLE_VAL(val));
×
417
        taosFprintfFile(pFile, "%s", buf);
×
418
      } else {
419
        n = tsnprintf(buf, LENGTH, "%*.15f", width, GET_DOUBLE_VAL(val));
219,488,632✔
420
        if (n > SHELL_DOUBLE_WIDTH) {
219,488,632✔
421
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
187,054,058✔
422
        } else {
423
          taosFprintfFile(pFile, "%s", buf);
32,434,574✔
424
        }
425
      }
426
      break;
219,488,632✔
427
    case TSDB_DATA_TYPE_BINARY:
97,987,891✔
428
    case TSDB_DATA_TYPE_NCHAR:
429
    case TSDB_DATA_TYPE_JSON: {
430
      int32_t bufIndex = 0;
97,987,891✔
431
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
97,987,891✔
432
      if (tmp == NULL) break;
97,987,891✔
433
      for (int32_t i = 0; i < length; i++) {
1,478,694,537✔
434
        tmp[bufIndex] = val[i];
1,380,706,646✔
435
        bufIndex++;
1,380,706,646✔
436
        if (val[i] == '\"') {
1,380,706,646✔
437
          tmp[bufIndex] = val[i];
×
438
          bufIndex++;
×
439
        }
440
      }
441
      tmp[bufIndex] = 0;
97,987,891✔
442

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

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

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

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

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

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

510
  for (int32_t col = 0; col < num_fields; col++) {
2,311,355✔
511
    if (col > 0) {
1,356,854✔
512
      taosFprintfFile(pFile, ",");
402,353✔
513
    }
514
    taosFprintfFile(pFile, "%s", fields[col].name);
1,356,854✔
515
  }
516
  taosFprintfFile(pFile, "\r\n");
954,501✔
517

518
  int64_t numOfRows = 0;
954,501✔
519
  do {
520
    int32_t *length = taos_fetch_lengths(tres);
988,154,279✔
521
    for (int32_t i = 0; i < num_fields; i++) {
2,147,483,647✔
522
      if (i > 0) {
1,529,246,944✔
523
        taosFprintfFile(pFile, ",");
541,092,665✔
524
      }
525
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
1,529,246,944✔
526
    }
527
    taosFprintfFile(pFile, "\r\n");
988,154,279✔
528

529
    numOfRows++;
988,154,279✔
530
    row = taos_fetch_row(tres);
988,154,279✔
531
  } while (row != NULL);
988,154,279✔
532

533
  taosCloseFile(&pFile);
954,501✔
534

535
  return numOfRows;
954,501✔
536
}
537

538
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
83,668,804✔
539
  TdWchar tail[3];
83,563,663✔
540
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
83,668,804✔
541

542
  while (pos < length) {
1,323,647,373✔
543
    TdWchar wc;
1,239,957,679✔
544
    int32_t bytes = taosMbToWchar(&wc, str + pos, MB_CUR_MAX);
1,240,502,458✔
545
    if (bytes <= 0) {
1,240,502,458✔
546
      break;
5,277✔
547
    }
548

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

560
    if (w <= 0) {
1,240,497,181✔
UNCOV
561
      continue;
×
562
    }
563

564
    if (width <= 0) {
1,240,497,181✔
565
      printf("%lc", wc);
63,719,428✔
566
      continue;
63,719,428✔
567
    }
568

569
    totalCols += w;
1,176,777,753✔
570
    if (totalCols > width) {
1,176,777,753✔
571
      break;
518,612✔
572
    }
573
    if (totalCols <= (width - 3)) {
1,176,259,141✔
574
      printf("%lc", wc);
1,174,679,463✔
575
      cols += w;
1,174,679,463✔
576
    } else {
577
      tail[tailLen] = wc;
1,579,678✔
578
      tailLen++;
1,579,678✔
579
    }
580
  }
581

582
  if (totalCols > width) {
83,668,804✔
583
    // width could be 1 or 2, so printf("...") cannot be used
584
    for (int32_t i = 0; i < 3; i++) {
2,074,448✔
585
      if (cols >= width) {
1,555,836✔
586
        break;
×
587
      }
588
      putchar('.');
1,555,836✔
589
      ++cols;
1,555,836✔
590
    }
591
  } else {
592
    for (int32_t i = 0; i < tailLen; i++) {
83,179,173✔
593
      printf("%lc", tail[i]);
28,981✔
594
    }
595
    cols = totalCols;
83,150,192✔
596
  }
597

598
  for (; cols < width; cols++) {
1,136,390,391✔
599
    putchar(' ');
1,052,721,587✔
600
  }
601
}
83,668,804✔
602

603
void shellPrintString(const char *str, int32_t width) {
432,786,327✔
604
  int32_t len = strlen(str);
432,786,327✔
605

606
  if (width == 0) {
432,786,327✔
607
    printf("%s", str);
×
608
  } else if (len > width) {
432,786,327✔
609
    if (width <= 3) {
×
610
      printf("%.*s.", width - 1, str);
×
611
    } else {
612
      printf("%.*s...", width - 3, str);
×
613
    }
614
  } else {
615
    printf("%s%*.s", str, width - len, "");
432,786,327✔
616
  }
617
}
432,786,327✔
618

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

625
  int32_t code = TSDB_CODE_FAILED;
×
626

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

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

640
  shellPrintString(outputWKT, width);
×
641

642
  geosFreeBuffer(outputWKT);
×
643
}
644

645
void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision) {
898,822,401✔
646
  if (val == NULL) {
898,822,401✔
647
    shellPrintString(TSDB_DATA_NULL_STR, width);
400,970,479✔
648
    return;
400,970,479✔
649
  }
650

651
  int n = 0;
497,851,922✔
652
#define LENGTH 64
653
  char buf[LENGTH] = {0};
497,851,922✔
654
  switch (field->type) {
497,851,922✔
655
    case TSDB_DATA_TYPE_BOOL:
31,815,848✔
656
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
31,815,848✔
657
      break;
31,815,848✔
658
    case TSDB_DATA_TYPE_TINYINT:
14,045,920✔
659
      printf("%*d", width, *((int8_t *)val));
14,045,920✔
660
      break;
14,045,920✔
661
    case TSDB_DATA_TYPE_UTINYINT:
21,284,966✔
662
      printf("%*u", width, *((uint8_t *)val));
21,284,966✔
663
      break;
21,284,966✔
664
    case TSDB_DATA_TYPE_SMALLINT:
11,878,792✔
665
      printf("%*d", width, *((int16_t *)val));
11,878,792✔
666
      break;
11,878,792✔
667
    case TSDB_DATA_TYPE_USMALLINT:
25,531,900✔
668
      printf("%*u", width, *((uint16_t *)val));
25,531,900✔
669
      break;
25,531,900✔
670
    case TSDB_DATA_TYPE_INT:
28,317,663✔
671
      printf("%*d", width, *((int32_t *)val));
28,317,663✔
672
      break;
28,317,663✔
673
    case TSDB_DATA_TYPE_UINT:
24,930,740✔
674
      printf("%*u", width, *((uint32_t *)val));
24,930,740✔
675
      break;
24,930,740✔
676
    case TSDB_DATA_TYPE_BIGINT:
54,340,828✔
677
      printf("%*" PRId64, width, taosGetInt64Aligned((int64_t *)val));
54,340,828✔
678
      break;
54,340,828✔
679
    case TSDB_DATA_TYPE_UBIGINT:
26,387,572✔
680
      printf("%*" PRIu64, width, taosGetUInt64Aligned((uint64_t *)val));
26,387,572✔
681
      break;
26,387,572✔
682
    case TSDB_DATA_TYPE_FLOAT:
18,645,179✔
683
      width = width >= LENGTH ? LENGTH - 1 : width;
18,645,179✔
684
      if (tsEnableScience) {
18,645,179✔
685
        printf("%*.7e", width, taosGetFloatAligned((float *)val));
×
686
      } else {
687
        snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, taosGetFloatAligned((float *)val));
18,645,179✔
688
        printf("%s", buf);
18,645,179✔
689
      }
690
      break;
18,645,179✔
691
    case TSDB_DATA_TYPE_DOUBLE:
44,423,838✔
692
      width = width >= LENGTH ? LENGTH - 1 : width;
44,423,838✔
693
      if (tsEnableScience) {
44,423,838✔
694
        snprintf(buf, LENGTH, "%*.15e", width, taosGetDoubleAligned((double *)val));
×
695
        printf("%s", buf);
×
696
      } else {
697
        snprintf(buf, LENGTH, "%*.*g", width, DBL_DIG, taosGetDoubleAligned((double *)val));
44,423,838✔
698
        printf("%*s", width, buf);
44,423,838✔
699
      }
700
      break;
44,423,838✔
701
    case TSDB_DATA_TYPE_VARBINARY: {
×
702
      void    *data = NULL;
×
703
      uint32_t size = 0;
×
704
      if (taosAscii2Hex(val, length, &data, &size) < 0) {
×
705
        break;
×
706
      }
707
      shellPrintNChar(data, size, width);
×
708
      taosMemoryFree(data);
×
709
      break;
×
710
    }
711
    case TSDB_DATA_TYPE_BINARY:
83,668,804✔
712
    case TSDB_DATA_TYPE_NCHAR:
713
    case TSDB_DATA_TYPE_JSON:
714
      shellPrintNChar(val, length, width);
83,668,804✔
715
      break;
83,668,804✔
716
    case TSDB_DATA_TYPE_GEOMETRY:
×
717
      shellPrintGeometry(val, length, width);
×
718
      break;
×
719
    case TSDB_DATA_TYPE_TIMESTAMP:
112,579,302✔
720
      shellFormatTimestamp(buf, sizeof(buf), taosGetInt64Aligned((int64_t *)val), precision);
112,579,302✔
721
      printf("%s", buf);
112,579,302✔
722
      break;
112,579,302✔
723

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

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

766
  return false;
×
767
}
768

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

775
  return false;
×
776
}
777

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

784
  dump_info->numFields = taos_num_fields(tres);
2,575,106✔
785
  dump_info->fields = taos_fetch_fields(tres);
2,575,106✔
786
  dump_info->precision = taos_result_precision(tres);
2,575,106✔
787

788
  dump_info->resShowMaxNum = UINT64_MAX;
2,575,106✔
789

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

794
  if (vertical) {
2,575,106✔
795
    dump_info->maxColNameLen = 0;
63,105✔
796
    for (int32_t col = 0; col < dump_info->numFields; col++) {
138,524✔
797
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
75,419✔
798
      if (len > dump_info->maxColNameLen) {
75,419✔
799
        dump_info->maxColNameLen = len;
67,005✔
800
      }
801
    }
802
  } else {
803
    for (int32_t col = 0; col < dump_info->numFields; col++) {
8,499,684✔
804
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
5,987,683✔
805
    }
806
  }
807
}
2,575,106✔
808

809
void shellVerticalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
63,105✔
810
  TAOS_ROW row = taos_fetch_row(tres);
63,105✔
811
  if (row == NULL) {
63,105✔
812
    printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
813
    return;
×
814
  }
815

816
  int64_t numOfPintRows = dump_info->numOfAllRows;
63,105✔
817
  int     numOfPrintRowsThisOne = 0;
63,105✔
818

819
  while (row != NULL) {
1,114,103✔
820
    printf("*************************** %" PRId64 ".row ***************************\r\n", numOfPintRows + 1);
1,114,103✔
821

822
    int32_t *length = taos_fetch_lengths(tres);
1,114,103✔
823

824
    for (int32_t i = 0; i < dump_info->numFields; i++) {
2,268,500✔
825
      TAOS_FIELD *field = dump_info->fields + i;
1,154,397✔
826

827
      int32_t padding = (int32_t)(dump_info->maxColNameLen - strlen(field->name));
1,154,397✔
828
      printf("%*.s%s: ", padding, " ", field->name);
1,154,397✔
829

830
      shellPrintField((const char *)row[i], field, 0, length[i], dump_info->precision);
1,154,397✔
831
      putchar('\r');
1,154,397✔
832
      putchar('\n');
1,154,397✔
833
    }
834

835
    numOfPintRows++;
1,114,103✔
836
    numOfPrintRowsThisOne++;
1,114,103✔
837

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

849
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
1,114,103✔
850
      return;
63,105✔
851
    }
852

853
    row = taos_fetch_row(tres);
1,050,998✔
854
  }
855
  return;
×
856
}
857

858
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
5,987,683✔
859
  int32_t width = (int32_t)strlen(field->name);
5,987,683✔
860

861
  switch (field->type) {
5,987,683✔
862
    case TSDB_DATA_TYPE_NULL:
×
863
      return TMAX(4, width);  // null
×
864
    case TSDB_DATA_TYPE_BOOL:
257,866✔
865
      return TMAX(5, width);  // 'false'
257,866✔
866

867
    case TSDB_DATA_TYPE_TINYINT:
369,524✔
868
    case TSDB_DATA_TYPE_UTINYINT:
869
      return TMAX(4, width);  // '-127'
369,524✔
870

871
    case TSDB_DATA_TYPE_SMALLINT:
373,178✔
872
    case TSDB_DATA_TYPE_USMALLINT:
873
      return TMAX(6, width);  // '-32767'
373,178✔
874

875
    case TSDB_DATA_TYPE_INT:
545,716✔
876
    case TSDB_DATA_TYPE_UINT:
877
      return TMAX(11, width);  // '-2147483648'
545,716✔
878

879
    case TSDB_DATA_TYPE_BIGINT:
1,095,300✔
880
    case TSDB_DATA_TYPE_UBIGINT:
881
      return TMAX(21, width);  // '-9223372036854775807'
1,095,300✔
882

883
    case TSDB_DATA_TYPE_FLOAT:
234,516✔
884
      return TMAX(SHELL_FLOAT_WIDTH, width);
234,516✔
885

886
    case TSDB_DATA_TYPE_DOUBLE:
920,164✔
887
      return TMAX(SHELL_DOUBLE_WIDTH, width);
920,164✔
888

889
    case TSDB_DATA_TYPE_BINARY:
1,020,348✔
890
    case TSDB_DATA_TYPE_GEOMETRY:
891
      if (field->bytes > shell.args.displayWidth) {
1,020,348✔
892
        return TMAX(shell.args.displayWidth, width);
515,404✔
893
      } else {
894
        return TMAX(field->bytes + 2, width);
504,944✔
895
      }
896
    case TSDB_DATA_TYPE_VARBINARY: {
2,288✔
897
      int32_t bytes = field->bytes * 2 + 2;
2,288✔
898
      if (bytes > shell.args.displayWidth) {
2,288✔
899
        return TMAX(shell.args.displayWidth, width);
×
900
      } else {
901
        return TMAX(bytes + 2, width);
2,288✔
902
      }
903
    }
904
    case TSDB_DATA_TYPE_NCHAR:
380,881✔
905
    case TSDB_DATA_TYPE_JSON: {
906
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
380,881✔
907
      if (bytes > shell.args.displayWidth) {
380,881✔
908
        return TMAX(shell.args.displayWidth, width);
380,881✔
909
      } else {
910
        return TMAX(bytes + 2, width);
×
911
      }
912
    }
913

914
    case TSDB_DATA_TYPE_TIMESTAMP:
787,332✔
915
      if (shell.args.is_raw_time) {
787,332✔
916
        return TMAX(14, width);
572✔
917
      }
918
      if (precision == TSDB_TIME_PRECISION_NANO) {
786,760✔
919
        return TMAX(29, width);
563✔
920
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
786,197✔
921
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
563✔
922
      } else {
923
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
785,634✔
924
      }
925
    case TSDB_DATA_TYPE_BLOB:
×
926
    case TSDB_DATA_TYPE_MEDIUMBLOB: {
927
      int32_t bytes = TSDB_MAX_BLOB_LEN;
×
928
      if (bytes > shell.args.displayWidth) {
×
929
        return TMAX(shell.args.displayWidth, width);
×
930
      } else {
931
        return TMAX(bytes + 2, width);
×
932
      }
933
    } break;
934

935
    case TSDB_DATA_TYPE_DECIMAL64:
285✔
936
      return TMAX(width, 20);
285✔
937
    case TSDB_DATA_TYPE_DECIMAL:
285✔
938
      return TMAX(width, 40);
285✔
939
    default:
×
940
      ASSERT(false);
×
941
  }
942

943
  return 0;
×
944
}
945

946
void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields) {
2,433,389✔
947
  int32_t rowWidth = 0;
2,433,389✔
948
  for (int32_t col = 0; col < num_fields; col++) {
7,780,977✔
949
    TAOS_FIELD *field = fields + col;
5,347,588✔
950
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
5,347,588✔
951
    int32_t     left = padding / 2;
5,347,588✔
952
    printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
5,347,588✔
953
    rowWidth += width[col] + 3;
5,347,588✔
954
  }
955

956
  putchar('\r');
2,433,389✔
957
  putchar('\n');
2,433,389✔
958
  for (int32_t i = 0; i < rowWidth; i++) {
147,882,498✔
959
    putchar('=');
145,449,109✔
960
  }
961
  putchar('\r');
2,433,389✔
962
  putchar('\n');
2,433,389✔
963
}
2,433,389✔
964

965
void shellHorizontalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
2,455,061✔
966
  TAOS_ROW row = taos_fetch_row(tres);
2,455,061✔
967
  if (row == NULL) {
2,455,061✔
968
    printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
969
    return;
×
970
  }
971

972
  int64_t numOfPintRows = dump_info->numOfAllRows;
2,455,061✔
973
  int     numOfPrintRowsThisOne = 0;
2,455,061✔
974
  if (numOfPintRows == 0) {
2,455,061✔
975
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
2,433,389✔
976
  }
977

978
  while (row != NULL) {
124,563,092✔
979
    int32_t *length = taos_fetch_lengths(tres);
124,563,092✔
980
    for (int32_t i = 0; i < dump_info->numFields; i++) {
1,022,231,096✔
981
      putchar(' ');
897,668,004✔
982
      shellPrintField((const char *)row[i], dump_info->fields + i, dump_info->width[i], length[i],
897,668,004✔
983
                      dump_info->precision);
984
      putchar(' ');
897,668,004✔
985
      putchar('|');
897,668,004✔
986
    }
987
    putchar('\r');
124,563,092✔
988
    putchar('\n');
124,563,092✔
989

990
    numOfPintRows++;
124,563,092✔
991
    numOfPrintRowsThisOne++;
124,563,092✔
992

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

1008
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
124,563,092✔
1009
      return;
2,455,061✔
1010
    }
1011

1012
    row = taos_fetch_row(tres);
122,108,031✔
1013
  }
1014
  return;
×
1015
}
1016

1017
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
5,093,272✔
1018
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
5,093,272✔
1019
  if (num_of_rows > 0) {
5,093,272✔
1020
    dump_info->numOfRows = num_of_rows;
2,518,166✔
1021
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
2,518,166✔
1022
      if (dump_info->vertical) {
2,518,166✔
1023
        shellVerticalPrintResult(tres, dump_info);
63,105✔
1024
      } else {
1025
        shellHorizontalPrintResult(tres, dump_info);
2,455,061✔
1026
      }
1027
    }
1028
    dump_info->numOfAllRows += num_of_rows;
2,518,166✔
1029
    if (!shellCmdkilled) {
2,518,166✔
1030
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
2,518,166✔
1031
    } else {
1032
      tsem_post(&dump_info->sem);
×
1033
    }
1034
  } else {
1035
    if (num_of_rows < 0) {
2,575,106✔
1036
      printf("\033[31masync retrieve failed, code: %d, %s\033[0m\n", num_of_rows, tstrerror(num_of_rows));
×
1037
    }
1038
    tsem_post(&dump_info->sem);
2,575,106✔
1039
  }
1040
}
5,092,707✔
1041

1042
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
3,529,607✔
1043
  int64_t num_of_rows = 0;
3,529,607✔
1044
  if (fname != NULL) {
3,529,607✔
1045
    num_of_rows = shellDumpResultToFile(fname, tres);
954,501✔
1046
  } else {
1047
    tsDumpInfo dump_info;
2,560,399✔
1048
    if (!shellCmdkilled) {
2,575,106✔
1049
      init_dump_info(&dump_info, tres, sql, vertical);
2,575,106✔
1050
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
2,575,106✔
1051
      tsem_wait(&dump_info.sem);
2,575,106✔
1052
      num_of_rows = dump_info.numOfAllRows;
2,575,106✔
1053
    }
1054
  }
1055

1056
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
3,529,607✔
1057
  return num_of_rows;
3,529,607✔
1058
}
1059

1060
void shellReadHistory() {
1,143,115✔
1061
  SShellHistory *pHistory = &shell.history;
1,143,115✔
1062
  TdFilePtr      pFile = taosOpenFile(pHistory->file, TD_FILE_READ | TD_FILE_STREAM);
1,143,115✔
1063
  if (pFile == NULL) return;
1,143,115✔
1064

1065
  char   *line = taosMemoryMalloc(tsMaxSQLLength + 1);
1,116,943✔
1066
  int32_t read_size = 0;
1,116,943✔
1067
  while ((read_size = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
247,404,590✔
1068
    line[read_size - 1] = '\0';
246,287,647✔
1069
    taosMemoryFree(pHistory->hist[pHistory->hend]);
246,287,647✔
1070
    pHistory->hist[pHistory->hend] = taosStrdup(line);
246,287,647✔
1071

1072
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
246,287,647✔
1073

1074
    if (pHistory->hend == pHistory->hstart) {
246,287,647✔
1075
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
×
1076
    }
1077
  }
1078

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

1095
    /* coverity[+retval] */
1096
    taosFsyncFile(pFile);
×
1097
    taosCloseFile(&pFile);
×
1098
  }
1099
  pHistory->hstart = pHistory->hend;
1,116,943✔
1100
}
1101

1102
void shellWriteHistory() {
1,143,115✔
1103
  SShellHistory *pHistory = &shell.history;
1,143,115✔
1104
  if (pHistory->hend == pHistory->hstart) return;
1,143,115✔
1105
  TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_APPEND);
1,142,548✔
1106
  if (pFile == NULL) return;
1,142,548✔
1107

1108
  for (int32_t i = pHistory->hstart; i != pHistory->hend;) {
2,285,659✔
1109
    if (pHistory->hist[i] != NULL) {
1,143,111✔
1110
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
1,143,111✔
1111
      taosMemoryFree(pHistory->hist[i]);
1,143,111✔
1112
      pHistory->hist[i] = NULL;
1,143,111✔
1113
    }
1114
    i = (i + 1) % SHELL_MAX_HISTORY_SIZE;
1,143,111✔
1115
  }
1116
  taosCloseFile(&pFile);
1,142,548✔
1117
}
1118

1119
void shellCleanupHistory() {
1,143,115✔
1120
  SShellHistory *pHistory = &shell.history;
1,143,115✔
1121
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
1,144,258,115✔
1122
    if (pHistory->hist[i] != NULL) {
1,143,115,000✔
1123
      taosMemoryFree(pHistory->hist[i]);
246,287,647✔
1124
      pHistory->hist[i] = NULL;
246,287,647✔
1125
    }
1126
  }
1127
}
1,143,115✔
1128

1129
void shellPrintError(TAOS_RES *tres, int64_t st) {
1,981,163✔
1130
  int64_t et = taosGetTimestampUs();
1,981,163✔
1131
  fprintf(stderr, "\r\nDB error: %s [0x%08X] (%.6fs)\r\n", taos_errstr(tres), taos_errno(tres), (et - st) / 1E6);
1,981,163✔
1132
  taos_free_result(tres);
1,981,163✔
1133
}
1,981,163✔
1134

1135
bool shellIsCommentLine(char *line) {
5,682,561✔
1136
  if (line == NULL) return true;
5,682,561✔
1137
  return shellRegexMatch(line, "^\\s*#.*", REG_EXTENDED);
5,682,561✔
1138
}
1139

1140
void shellSourceFile(const char *file) {
53,375✔
1141
  int32_t read_len = 0;
53,375✔
1142
  char   *cmd = taosMemoryCalloc(1, tsMaxSQLLength + 1);
53,375✔
1143
  size_t  cmd_len = 0;
53,375✔
1144
  char    fullname[PATH_MAX] = {0};
53,375✔
1145
  char    sourceFileCommand[PATH_MAX + 8] = {0};
53,375✔
1146

1147
  if (taosExpandDir(file, fullname, PATH_MAX) != 0) {
53,375✔
1148
    tstrncpy(fullname, file, PATH_MAX);
×
1149
  }
1150

1151
  sprintf(sourceFileCommand, "source %s;", fullname);
53,375✔
1152
  shellRecordCommandToHistory(sourceFileCommand);
53,375✔
1153

1154
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_READ | TD_FILE_STREAM);
53,375✔
1155
  if (pFile == NULL) {
53,375✔
1156
    fprintf(stderr, "failed to open file %s\r\n", fullname);
9,213✔
1157
    taosMemoryFree(cmd);
9,213✔
1158
    return;
9,213✔
1159
  }
1160

1161
  char *line = taosMemoryMalloc(tsMaxSQLLength + 1);
44,162✔
1162
  while ((read_len = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
5,792,659✔
1163
    if (cmd_len + read_len >= tsMaxSQLLength) {
5,748,497✔
UNCOV
1164
      printf("read command line too long over 1M, ignore this line. cmd_len = %d read_len=%d \n", (int32_t)cmd_len,
×
1165
             read_len);
1166
      cmd_len = 0;
×
NEW
1167
      memset(line, 0, tsMaxSQLLength + 1);
×
1168
      continue;
×
1169
    }
1170
    line[--read_len] = '\0';
5,748,497✔
1171

1172
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
5,748,497✔
1173
      continue;
65,936✔
1174
    }
1175

1176
    if (line[read_len - 1] == '\\') {
5,682,561✔
1177
      line[read_len - 1] = ' ';
×
1178
      memcpy(cmd + cmd_len, line, read_len);
×
1179
      cmd_len += read_len;
×
1180
      continue;
×
1181
    }
1182

1183
    if (line[read_len - 1] == '\r') {
5,682,561✔
1184
      line[read_len - 1] = ' ';
×
1185
    }
1186

1187
    memcpy(cmd + cmd_len, line, read_len);
5,682,561✔
1188
    printf("%s%s\r\n", shell.info.promptHeader, cmd);
5,682,561✔
1189
    shellRunCommand(cmd, false);
5,682,561✔
1190
    memset(cmd, 0, tsMaxSQLLength);
5,682,561✔
1191
    cmd_len = 0;
5,682,561✔
1192
  }
1193

1194
  taosMemoryFree(cmd);
44,162✔
1195
  taosMemoryFreeClear(line);
44,162✔
1196
  taosCloseFile(&pFile);
44,162✔
1197
}
1198

1199
int32_t shellGetGrantInfo(char *buf) {
729✔
1200
  int32_t verType = TSDB_VERSION_UNKNOWN;
729✔
1201
  char    sinfo[256] = {0};
729✔
1202
  tstrncpy(sinfo, taos_get_server_info(shell.conn), sizeof(sinfo));
729✔
1203
  strtok(sinfo, "\r\n");
729✔
1204

1205
#ifndef TD_ASTRA
1206
  char sql[] = "show grants";
729✔
1207

1208
  TAOS_RES *tres = taos_query(shell.conn, sql);
729✔
1209

1210
  int32_t code = taos_errno(tres);
729✔
1211
  if (code != TSDB_CODE_SUCCESS) {
729✔
1212
    if (code != TSDB_CODE_OPS_NOT_SUPPORT && code != TSDB_CODE_MND_NO_RIGHTS &&
×
1213
        code != TSDB_CODE_PAR_PERMISSION_DENIED) {
1214
      fprintf(stderr, "Failed to check Server Edition, Reason:0x%04x:%s\r\n\r\n", code, taos_errstr(tres));
×
1215
    }
1216
    taos_free_result(tres);
×
1217
    return verType;
×
1218
  }
1219

1220
  int32_t num_fields = taos_field_count(tres);
729✔
1221
  if (num_fields == 0) {
729✔
1222
    fprintf(stderr, "\r\nInvalid grant information.\r\n");
×
1223
    exit(0);
×
1224
  } else {
1225
    if (tres == NULL) {
729✔
1226
      fprintf(stderr, "\r\nGrant information is null.\r\n");
×
1227
      exit(0);
×
1228
    }
1229

1230
    TAOS_FIELD *fields = taos_fetch_fields(tres);
729✔
1231
    TAOS_ROW    row = taos_fetch_row(tres);
729✔
1232
    if (row == NULL) {
729✔
1233
      fprintf(stderr, "\r\nFailed to get grant information from server. Abort.\r\n");
×
1234
      exit(0);
×
1235
    }
1236
    char serverVersion[64] = {0};
729✔
1237
    char expiretime[32] = {0};
729✔
1238
    char expired[32] = {0};
729✔
1239

1240
    tstrncpy(serverVersion, row[0], 64);
729✔
1241
    memcpy(expiretime, row[1], fields[1].bytes);
729✔
1242
    memcpy(expired, row[2], fields[2].bytes);
729✔
1243
    
1244
    trimStr(serverVersion, "trial");
729✔
1245
    
1246
    if (strcmp(serverVersion, "community") == 0) {
729✔
1247
      verType = TSDB_VERSION_OSS;
×
1248
    } else if (strcmp(expiretime, "unlimited") == 0) {
729✔
1249
      verType = TSDB_VERSION_ENTERPRISE;
×
1250
      sprintf(buf, "Server is %s %s. License will never expire.\r\n", serverVersion, sinfo);
×
1251
    } else {
1252
      verType = TSDB_VERSION_ENTERPRISE;
729✔
1253
      sprintf(buf, "Server is %s %s. License will expire at %s.\r\n", serverVersion, sinfo, expiretime);
729✔
1254
    }
1255

1256
    taos_free_result(tres);
729✔
1257
  }
1258

1259
  fprintf(stdout, "\r\n");
729✔
1260
#else
1261
  verType = TSDB_VERSION_ENTERPRISE;
1262
  sprintf(buf, "Server is %s %s. License will never expire.\r\n", TD_PRODUCT_NAME, sinfo);
1263
#endif
1264
  return verType;
729✔
1265
}
1266

1267
#ifdef WINDOWS
1268
BOOL shellQueryInterruptHandler(DWORD fdwCtrlType) {
1269
  tsem_post(&shell.cancelSem);
1270
  return TRUE;
1271
}
1272
#else
1273
void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context) { tsem_post(&shell.cancelSem); }
1,313✔
1274
#endif
1275

1276
void shellCleanup(void *arg) { taosResetTerminalMode(); }
729✔
1277

1278
void *shellCancelHandler(void *arg) {
729✔
1279
  setThreadName("shellCancelHandler");
729✔
1280
  while (1) {
1281
    if (shell.exit == true) {
1,895✔
1282
      break;
729✔
1283
    }
1284

1285
    if (tsem_wait(&shell.cancelSem) != 0) {
1,166✔
1286
      taosMsleep(10);
×
1287
      continue;
×
1288
    }
1289

1290
    if (shell.conn) {
1,166✔
1291
      shellCmdkilled = true;
437✔
1292
      taos_kill_query(shell.conn);
437✔
1293
    }
1294

1295
#ifdef WINDOWS
1296
    printf("\n%s", shell.info.promptHeader);
1297
#endif
1298
  }
1299

1300
  return NULL;
729✔
1301
}
1302

1303
#pragma GCC diagnostic push
1304
#pragma GCC diagnostic ignored "-Wstringop-overflow"
1305

1306
void *shellThreadLoop(void *arg) {
729✔
1307
  setThreadName("shellThreadLoop");
729✔
1308
  taosGetOldTerminalMode();
729✔
1309
  taosThreadCleanupPush(shellCleanup, NULL);
729✔
1310

1311
  do {
1312
    char *command = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE);
729✔
1313
    if (command == NULL) {
729✔
1314
      printf("failed to malloc command\r\n");
×
1315
      break;
×
1316
    }
1317

1318
    do {
1319
      memset(command, 0, SHELL_MAX_COMMAND_SIZE);
1,458✔
1320
      taosSetTerminalMode();
1,458✔
1321

1322
      if (shellReadCommand(command) != 0) {
1,458✔
1323
        break;
729✔
1324
      }
1325

1326
      taosResetTerminalMode();
729✔
1327
    } while (shellRunCommand(command, true) == 0);
729✔
1328

1329
    taosMemoryFreeClear(command);
729✔
1330
    shellWriteHistory();
729✔
1331
    shellExit();
729✔
1332
  } while (0);
1333

1334
  taosThreadCleanupPop(1);
729✔
1335
  return NULL;
729✔
1336
}
1337
#pragma GCC diagnostic pop
1338

1339
TAOS *createConnect(SShellArgs *pArgs) {
1,155,893✔
1340
  char     show[256] = "\0";
1,155,893✔
1341
  char    *host = NULL;
1,155,893✔
1342
  uint16_t port = 0;
1,155,893✔
1343
  char    *user = NULL;
1,155,893✔
1344
  char    *pwd = NULL;
1,155,893✔
1345
  int32_t  code = 0;
1,155,893✔
1346
  char    *dsnc = NULL;
1,155,893✔
1347

1348
  // set mode
1349
  if (pArgs->connMode != CONN_MODE_NATIVE && pArgs->dsn) {
1,160,675✔
1350
    dsnc = strToLowerCopy(pArgs->dsn);
5,340✔
1351
    if (dsnc == NULL) {
5,340✔
1352
      return NULL;
×
1353
    }
1354

1355
    char *cport = NULL;
5,340✔
1356
    char  error[512] = "\0";
5,340✔
1357
    code = parseDsn(dsnc, &host, &cport, &user, &pwd, error);
5,340✔
1358
    if (code) {
5,340✔
1359
      printf("%s dsn=%s\n", error, dsnc);
558✔
1360
      free(dsnc);
558✔
1361
      return NULL;
558✔
1362
    }
1363

1364
    // default ws port
1365
    if (cport == NULL) {
4,782✔
1366
      if (user)
279✔
1367
        port = DEFAULT_PORT_WS_CLOUD;
279✔
1368
      else
1369
        port = DEFAULT_PORT_WS_LOCAL;
×
1370
    } else {
1371
      port = atoi(cport);
4,503✔
1372
    }
1373

1374
    // websocket
1375
    memcpy(show, pArgs->dsn, 20);
4,782✔
1376
    memcpy(show + 20, "...", 3);
4,782✔
1377
    memcpy(show + 23, pArgs->dsn + strlen(pArgs->dsn) - 10, 10);
4,782✔
1378

1379
  } else {
1380
    host = (char *)pArgs->host;
1,150,553✔
1381
    user = (char *)pArgs->user;
1,150,553✔
1382
    pwd = pArgs->password;
1,150,553✔
1383

1384
    if (pArgs->port_inputted) {
1,150,553✔
1385
      port = pArgs->port;
1,980✔
1386
    } else {
1387
      port = defaultPort(pArgs->connMode, pArgs->dsn);
1,148,573✔
1388
    }
1389

1390
    sprintf(show, "host:%s port:%d ", host, port);
1,150,553✔
1391
  }
1392

1393
  // connect main
1394
  TAOS *taos = NULL;
1,155,335✔
1395
  if (pArgs->auth) {
1,155,335✔
1396
    taos = taos_connect_auth(host, user, pArgs->auth, pArgs->database, port);
825✔
1397
  } else {
1398
    taos = taos_connect(host, user, pwd, pArgs->database, port);
1,154,510✔
1399
  }
1400

1401
  // host user pointer in dsnc address
1402
  free(dsnc);
1,155,335✔
1403
  return taos;
1,155,335✔
1404
}
1405

1406
int32_t shellExecute(int argc, char *argv[]) {
1,155,893✔
1407
  int32_t code = 0;
1,155,893✔
1408
  printf(shell.info.clientVersion, shell.info.cusName,
2,311,786✔
1409
         workingMode(shell.args.connMode, shell.args.dsn) == CONN_MODE_NATIVE ? STR_NATIVE : STR_WEBSOCKET,
1,155,893✔
1410
         taos_get_client_info(), shell.info.cusName);
1411
  fflush(stdout);
1,155,893✔
1412

1413
  SShellArgs *pArgs = &shell.args;
1,155,893✔
1414
  shell.conn = createConnect(pArgs);
1,155,893✔
1415

1416
  if (shell.conn == NULL) {
1,155,893✔
1417
    printf("failed to connect to server, reason: %s [0x%08X]\n%s", taos_errstr(NULL), taos_errno(NULL),
12,778✔
1418
           ERROR_CODE_DETAIL);
1419
    fflush(stdout);
12,778✔
1420
    return -1;
12,778✔
1421
  }
1422

1423
  bool runOnce = pArgs->commands != NULL || pArgs->file[0] != 0;
1,143,115✔
1424
  shellSetConn(shell.conn, runOnce);
1,143,115✔
1425
  shellReadHistory();
1,143,115✔
1426

1427
  if (shell.args.is_bi_mode) {
1,143,115✔
1428
    // need set bi mode
1429
    printf("Set BI mode is true.\n");
1,965✔
1430
    taos_set_conn_mode(shell.conn, TAOS_CONN_MODE_BI, 1);
1,965✔
1431
  }
1432

1433
  if (runOnce) {
1,143,115✔
1434
    if (pArgs->commands != NULL) {
1,142,386✔
1435
      printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
1,089,574✔
1436
      char *cmd = taosStrdup(pArgs->commands);
1,089,574✔
1437
      shellRunCommand(cmd, true);
1,089,574✔
1438
      taosMemoryFree(cmd);
1,089,574✔
1439
    }
1440

1441
    if (pArgs->file[0] != 0) {
1,142,386✔
1442
      shellSourceFile(pArgs->file);
52,812✔
1443
    }
1444

1445
    taos_close(shell.conn);
1,142,386✔
1446

1447
    shellWriteHistory();
1,142,386✔
1448
    shellCleanupHistory();
1,142,386✔
1449
    return 0;
1,142,386✔
1450
  }
1451

1452
  if ((code = tsem_init(&shell.cancelSem, 0, 0)) != 0) {
729✔
1453
    printf("failed to create cancel semaphore since %s\r\n", tstrerror(code));
×
1454
    return code;
×
1455
  }
1456

1457
  TdThread spid = {0};
729✔
1458
  taosThreadCreate(&spid, NULL, shellCancelHandler, NULL);
729✔
1459

1460
  taosSetSignal(SIGTERM, shellQueryInterruptHandler);
729✔
1461
  taosSetSignal(SIGHUP, shellQueryInterruptHandler);
729✔
1462
  taosSetSignal(SIGINT, shellQueryInterruptHandler);
729✔
1463

1464
  char    buf[512] = {0};
729✔
1465
  int32_t verType = shellGetGrantInfo(buf);
729✔
1466
#ifndef WINDOWS
1467
  printfIntroduction(verType);
729✔
1468
#else
1469
  if (verType == TSDB_VERSION_OSS) {
1470
    showAD(false);
1471
  }
1472
#endif
1473
  // printf version
1474
  if (verType == TSDB_VERSION_ENTERPRISE || verType == TSDB_VERSION_CLOUD) {
729✔
1475
    printf("%s\n", buf);
729✔
1476
  }
1477

1478
  while (1) {
1479
    taosThreadCreate(&shell.pid, NULL, shellThreadLoop, NULL);
729✔
1480
    taosThreadJoin(shell.pid, NULL);
729✔
1481
    taosThreadClear(&shell.pid);
729✔
1482
    if (shell.exit) {
729✔
1483
      tsem_post(&shell.cancelSem);
729✔
1484
      break;
729✔
1485
    }
1486
  }
1487

1488
  if (verType == TSDB_VERSION_OSS) {
729✔
1489
    showAD(true);
×
1490
  }
1491

1492
  taosThreadJoin(spid, NULL);
729✔
1493

1494
  shellCleanupHistory();
729✔
1495
  taos_kill_query(shell.conn);
729✔
1496
  taos_close(shell.conn);
729✔
1497

1498
  TAOS_RETURN(code);
729✔
1499
}
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