TDengine/tools/shell/src/shellEngine.c

948 lines
26 KiB
C
Raw Normal View History

2019-07-11 08:36:16 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _BSD_SOURCE
#define _GNU_SOURCE
2019-07-11 08:36:16 +00:00
#define _XOPEN_SOURCE
2019-07-13 06:04:30 +00:00
#define _DEFAULT_SOURCE
2022-04-20 11:13:22 +00:00
#include "shellInt.h"
2019-07-11 08:36:16 +00:00
2022-04-21 13:09:43 +00:00
static bool shellIsEmptyCommand(const char *cmd);
static int32_t shellRunSingleCommand(char *command);
static int32_t shellRunCommand(char *command);
static void shellRunSingleCommandImp(char *command);
static char *shellFormatTimestamp(char *buf, int64_t val, int32_t precision);
static void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, int32_t length,
int32_t precision);
static int32_t shellDumpResultToFile(const char *fname, TAOS_RES *tres);
static void shellPrintNChar(const char *str, int32_t length, int32_t width);
static void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision);
static int32_t shellVerticalPrintResult(TAOS_RES *tres);
static int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision);
static void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields);
static int32_t shellHorizontalPrintResult(TAOS_RES *tres);
static int32_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical);
static void shellReadHistory();
static void shellWriteHistory();
static void shellPrintError(TAOS_RES *tres, int64_t st);
static bool shellIsCommentLine(char *line);
static void shellSourceFile(const char *file);
static void shellGetGrantInfo();
static void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context);
static void shellCleanup(void *arg);
static void *shellCancelHandler(void *arg);
static void *shellThreadLoop(void *arg);
bool shellIsEmptyCommand(const char *cmd) {
for (char c = *cmd++; c != 0; c = *cmd++) {
if (c != ' ' && c != '\t' && c != ';') {
return false;
2019-07-11 08:36:16 +00:00
}
}
return true;
2019-07-11 08:36:16 +00:00
}
2022-04-21 13:09:43 +00:00
int32_t shellRunSingleCommand(char *command) {
if (shellIsEmptyCommand(command)) {
return 0;
2019-07-11 08:36:16 +00:00
}
2022-04-20 11:44:25 +00:00
if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
shellWriteHistory();
return -1;
}
2022-04-20 11:44:25 +00:00
if (shellRegexMatch(command, "^[\t ]*clear[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
2019-07-11 08:36:16 +00:00
system("clear");
return 0;
}
2022-04-20 11:44:25 +00:00
if (shellRegexMatch(command, "^[\t ]*set[ \t]+max_binary_display_width[ \t]+(default|[1-9][0-9]*)[ \t;]*$",
2022-04-21 13:09:43 +00:00
REG_EXTENDED | REG_ICASE)) {
strtok(command, " \t");
strtok(NULL, " \t");
2021-12-17 07:49:27 +00:00
char *p = strtok(NULL, " \t");
2022-04-22 09:59:41 +00:00
if (strncasecmp(p, "default", 7) == 0) {
2022-04-21 13:09:43 +00:00
shell.args.displayWidth = SHELL_DEFAULT_MAX_BINARY_DISPLAY_WIDTH;
} else {
2022-04-22 09:59:41 +00:00
int32_t displayWidth = atoi(p);
displayWidth = TRANGE(displayWidth, 1, 10 * 1024);
shell.args.displayWidth = displayWidth;
}
return 0;
}
2022-04-20 11:44:25 +00:00
if (shellRegexMatch(command, "^[ \t]*source[\t ]+[^ ]+[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
2019-07-11 08:36:16 +00:00
/* If source file. */
char *c_ptr = strtok(command, " ;");
assert(c_ptr != NULL);
c_ptr = strtok(NULL, " ;");
assert(c_ptr != NULL);
2022-04-21 13:09:43 +00:00
shellSourceFile(c_ptr);
return 0;
2019-07-11 08:36:16 +00:00
}
2022-04-21 13:09:43 +00:00
shellRunSingleCommandImp(command);
return 0;
2019-07-11 08:36:16 +00:00
}
2022-04-21 13:09:43 +00:00
int32_t shellRunCommand(char *command) {
if (shellIsEmptyCommand(command)) {
return 0;
}
2022-04-21 13:09:43 +00:00
SShellHistory *pHistory = &shell.history;
if (pHistory->hstart == pHistory->hend ||
pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE] == NULL ||
strcmp(command, pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE]) != 0) {
if (pHistory->hist[pHistory->hend] != NULL) {
taosMemoryFreeClear(pHistory->hist[pHistory->hend]);
}
2022-04-21 13:09:43 +00:00
pHistory->hist[pHistory->hend] = strdup(command);
2022-04-21 13:09:43 +00:00
pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
if (pHistory->hend == pHistory->hstart) {
pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
}
}
2022-04-27 13:29:25 +00:00
char quote = 0, *cmd = command;
for (char c = *command++; c != 0; c = *command++) {
2022-04-27 13:29:25 +00:00
if (c == '\\' && (*command == '\'' || *command == '"' || *command == '`')) {
command ++;
continue;
}
if (quote == c) {
quote = 0;
2022-04-27 13:29:25 +00:00
} else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
quote = c;
2022-04-27 13:29:25 +00:00
} else if (c == ';' && quote == 0) {
c = *command;
*command = 0;
2022-04-21 13:09:43 +00:00
if (shellRunSingleCommand(cmd) < 0) {
return -1;
}
2022-04-27 13:29:25 +00:00
*command = c;
cmd = command;
}
}
2022-04-21 13:09:43 +00:00
return shellRunSingleCommand(cmd);
2020-12-17 05:06:20 +00:00
}
2022-04-21 13:09:43 +00:00
void shellRunSingleCommandImp(char *command) {
2022-04-21 13:11:02 +00:00
int64_t st, et;
char *sptr = NULL;
char *cptr = NULL;
char *fname = NULL;
bool printMode = false;
2019-07-11 08:36:16 +00:00
if ((sptr = strstr(command, ">>")) != NULL) {
cptr = strstr(command, ";");
if (cptr != NULL) {
*cptr = '\0';
}
2022-04-21 13:09:43 +00:00
fname = sptr + 2;
2019-07-11 08:36:16 +00:00
*sptr = '\0';
}
if ((sptr = strstr(command, "\\G")) != NULL) {
cptr = strstr(command, ";");
if (cptr != NULL) {
*cptr = '\0';
}
*sptr = '\0';
printMode = true; // When output to a file, the switch does not work.
}
2019-07-11 08:36:16 +00:00
st = taosGetTimestampUs();
2022-04-21 13:09:43 +00:00
TAOS_RES *pSql = taos_query(shell.conn, command);
2020-06-01 15:30:10 +00:00
if (taos_errno(pSql)) {
2022-04-20 11:44:25 +00:00
shellPrintError(pSql, st);
2019-07-11 08:36:16 +00:00
return;
}
2022-04-20 11:44:25 +00:00
if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) {
2019-07-11 08:36:16 +00:00
fprintf(stdout, "Database changed.\n\n");
fflush(stdout);
2020-06-19 04:13:08 +00:00
2022-04-18 13:14:32 +00:00
taos_free_result(pSql);
2019-07-11 08:36:16 +00:00
return;
}
2022-04-18 13:14:32 +00:00
TAOS_FIELD *pFields = taos_fetch_fields(pSql);
2021-12-17 08:53:14 +00:00
if (pFields != NULL) { // select and show kinds of commands
2022-04-21 13:09:43 +00:00
int32_t error_no = 0;
2022-04-21 13:09:43 +00:00
int32_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode);
if (numOfRows < 0) return;
2019-07-11 08:36:16 +00:00
et = taosGetTimestampUs();
if (error_no == 0) {
printf("Query OK, %d row(s) in set (%.6fs)\n", numOfRows, (et - st) / 1E6);
} else {
printf("Query interrupted (%s), %d row(s) in set (%.6fs)\n", taos_errstr(pSql), numOfRows, (et - st) / 1E6);
2019-07-11 08:36:16 +00:00
}
2022-04-18 13:14:32 +00:00
taos_free_result(pSql);
2019-07-11 08:36:16 +00:00
} else {
2022-04-21 13:09:43 +00:00
int32_t num_rows_affacted = taos_affected_rows(pSql);
taos_free_result(pSql);
2019-07-11 08:36:16 +00:00
et = taosGetTimestampUs();
2021-02-20 05:41:35 +00:00
printf("Query OK, %d of %d row(s) in database (%.6fs)\n", num_rows_affacted, num_rows_affacted, (et - st) / 1E6);
2019-07-11 08:36:16 +00:00
}
printf("\n");
}
2022-04-21 13:09:43 +00:00
char *shellFormatTimestamp(char *buf, int64_t val, int32_t precision) {
if (shell.args.is_raw_time) {
sprintf(buf, "%" PRId64, val);
return buf;
}
2019-07-11 08:36:16 +00:00
2021-12-17 07:49:27 +00:00
time_t tt;
2021-02-06 10:12:56 +00:00
int32_t ms = 0;
if (precision == TSDB_TIME_PRECISION_NANO) {
tt = (time_t)(val / 1000000000);
ms = val % 1000000000;
} else if (precision == TSDB_TIME_PRECISION_MICRO) {
tt = (time_t)(val / 1000000);
2021-02-06 10:12:56 +00:00
ms = val % 1000000;
} else {
tt = (time_t)(val / 1000);
2021-02-06 10:12:56 +00:00
ms = val % 1000;
}
2022-04-21 13:09:43 +00:00
/*
comment out as it make testcases like select_with_tags.sim fail.
2021-12-17 07:49:27 +00:00
but in windows, this may cause the call to localtime crash if tt < 0,
need to find a better solution.
if (tt < 0) {
tt = 0;
}
2022-04-21 13:09:43 +00:00
*/
2020-08-12 05:14:59 +00:00
#ifdef WINDOWS
if (tt < 0) tt = 0;
#endif
2021-03-25 02:59:50 +00:00
if (tt <= 0 && ms < 0) {
2021-02-06 10:12:56 +00:00
tt--;
if (precision == TSDB_TIME_PRECISION_NANO) {
ms += 1000000000;
} else if (precision == TSDB_TIME_PRECISION_MICRO) {
2021-02-06 10:12:56 +00:00
ms += 1000000;
} else {
ms += 1000;
}
}
2020-08-12 05:14:59 +00:00
struct tm *ptm = taosLocalTime(&tt, NULL);
2021-12-17 07:49:27 +00:00
size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", ptm);
if (precision == TSDB_TIME_PRECISION_NANO) {
sprintf(buf + pos, ".%09d", ms);
} else if (precision == TSDB_TIME_PRECISION_MICRO) {
2021-02-06 10:12:56 +00:00
sprintf(buf + pos, ".%06d", ms);
} else {
2021-02-06 10:12:56 +00:00
sprintf(buf + pos, ".%03d", ms);
}
return buf;
}
2022-04-21 13:09:43 +00:00
void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, int32_t length, int32_t precision) {
if (val == NULL) {
taosFprintfFile(pFile, "%s", TSDB_DATA_NULL_STR);
return;
}
char buf[TSDB_MAX_BYTES_PER_ROW];
switch (field->type) {
case TSDB_DATA_TYPE_BOOL:
taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
break;
case TSDB_DATA_TYPE_TINYINT:
taosFprintfFile(pFile, "%d", *((int8_t *)val));
break;
case TSDB_DATA_TYPE_SMALLINT:
taosFprintfFile(pFile, "%d", *((int16_t *)val));
break;
case TSDB_DATA_TYPE_INT:
taosFprintfFile(pFile, "%d", *((int32_t *)val));
break;
case TSDB_DATA_TYPE_BIGINT:
taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
break;
case TSDB_DATA_TYPE_FLOAT:
taosFprintfFile(pFile, "%.5f", GET_FLOAT_VAL(val));
break;
case TSDB_DATA_TYPE_DOUBLE:
taosFprintfFile(pFile, "%.9f", GET_DOUBLE_VAL(val));
break;
case TSDB_DATA_TYPE_BINARY:
case TSDB_DATA_TYPE_NCHAR:
memcpy(buf, val, length);
buf[length] = 0;
taosFprintfFile(pFile, "\'%s\'", buf);
break;
case TSDB_DATA_TYPE_TIMESTAMP:
2022-04-21 13:09:43 +00:00
shellFormatTimestamp(buf, *(int64_t *)val, precision);
taosFprintfFile(pFile, "'%s'", buf);
break;
default:
break;
}
}
2022-04-21 13:09:43 +00:00
int32_t shellDumpResultToFile(const char *fname, TAOS_RES *tres) {
2022-04-22 09:59:41 +00:00
char fullname[PATH_MAX] = {0};
if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
tstrncpy(fullname, fname, PATH_MAX);
}
2020-06-19 04:13:08 +00:00
TAOS_ROW row = taos_fetch_row(tres);
if (row == NULL) {
return 0;
}
2022-04-22 09:59:41 +00:00
TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
if (pFile == NULL) {
2022-04-22 11:27:00 +00:00
fprintf(stderr, "failed to open file: %s\n", fullname);
return -1;
}
2020-06-19 04:13:08 +00:00
TAOS_FIELD *fields = taos_fetch_fields(tres);
2022-04-21 13:09:43 +00:00
int32_t num_fields = taos_num_fields(tres);
int32_t precision = taos_result_precision(tres);
2022-04-21 13:09:43 +00:00
for (int32_t col = 0; col < num_fields; col++) {
if (col > 0) {
taosFprintfFile(pFile, ",");
}
taosFprintfFile(pFile, "%s", fields[col].name);
}
taosFprintfFile(pFile, "\n");
2022-04-21 13:09:43 +00:00
int32_t numOfRows = 0;
do {
2021-12-17 07:49:27 +00:00
int32_t *length = taos_fetch_lengths(tres);
2022-04-21 13:09:43 +00:00
for (int32_t i = 0; i < num_fields; i++) {
if (i > 0) {
taosFprintfFile(pFile, "\n");
}
2022-04-21 13:09:43 +00:00
shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
2019-07-11 08:36:16 +00:00
}
taosFprintfFile(pFile, "\n");
numOfRows++;
2020-06-19 04:13:08 +00:00
row = taos_fetch_row(tres);
2021-12-17 07:49:27 +00:00
} while (row != NULL);
taosCloseFile(&pFile);
2020-06-19 04:13:08 +00:00
return numOfRows;
}
2022-04-21 13:09:43 +00:00
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
2022-03-18 08:48:12 +00:00
TdWchar tail[3];
2022-04-21 13:09:43 +00:00
int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
while (pos < length) {
2022-03-18 08:48:12 +00:00
TdWchar wc;
2022-04-21 13:09:43 +00:00
int32_t bytes = taosMbToWchar(&wc, str + pos, MB_CUR_MAX);
if (bytes == 0) {
break;
}
pos += bytes;
if (pos > length) {
break;
}
#ifdef WINDOWS
2022-04-21 13:09:43 +00:00
int32_t w = bytes;
#else
2022-04-21 13:09:43 +00:00
int32_t w = taosWcharWidth(wc);
#endif
if (w <= 0) {
continue;
}
if (width <= 0) {
printf("%lc", wc);
continue;
}
totalCols += w;
if (totalCols > width) {
break;
}
if (totalCols <= (width - 3)) {
printf("%lc", wc);
cols += w;
} else {
tail[tailLen] = wc;
tailLen++;
}
}
if (totalCols > width) {
// width could be 1 or 2, so printf("...") cannot be used
2022-04-21 13:09:43 +00:00
for (int32_t i = 0; i < 3; i++) {
if (cols >= width) {
break;
}
putchar('.');
++cols;
}
} else {
2022-04-21 13:09:43 +00:00
for (int32_t i = 0; i < tailLen; i++) {
printf("%lc", tail[i]);
}
cols = totalCols;
}
for (; cols < width; cols++) {
putchar(' ');
}
}
2022-04-21 13:09:43 +00:00
void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision) {
if (val == NULL) {
2022-04-21 13:09:43 +00:00
int32_t w = width;
if (field->type < TSDB_DATA_TYPE_TINYINT || field->type > TSDB_DATA_TYPE_DOUBLE) {
w = 0;
2019-07-11 08:36:16 +00:00
}
w = printf("%*s", w, TSDB_DATA_NULL_STR);
for (; w < width; w++) {
putchar(' ');
}
return;
}
2019-07-11 08:36:16 +00:00
char buf[TSDB_MAX_BYTES_PER_ROW];
switch (field->type) {
case TSDB_DATA_TYPE_BOOL:
2020-09-29 06:28:02 +00:00
printf("%*s", width, ((((int32_t)(*((char *)val))) == 1) ? "true" : "false"));
break;
case TSDB_DATA_TYPE_TINYINT:
2020-09-29 06:28:02 +00:00
printf("%*d", width, *((int8_t *)val));
break;
case TSDB_DATA_TYPE_UTINYINT:
printf("%*u", width, *((uint8_t *)val));
break;
case TSDB_DATA_TYPE_SMALLINT:
2020-09-29 06:28:02 +00:00
printf("%*d", width, *((int16_t *)val));
break;
case TSDB_DATA_TYPE_USMALLINT:
printf("%*u", width, *((uint16_t *)val));
break;
case TSDB_DATA_TYPE_INT:
2020-09-29 06:28:02 +00:00
printf("%*d", width, *((int32_t *)val));
break;
case TSDB_DATA_TYPE_UINT:
printf("%*u", width, *((uint32_t *)val));
break;
case TSDB_DATA_TYPE_BIGINT:
printf("%*" PRId64, width, *((int64_t *)val));
break;
case TSDB_DATA_TYPE_UBIGINT:
printf("%*" PRIu64, width, *((uint64_t *)val));
break;
case TSDB_DATA_TYPE_FLOAT:
printf("%*.5f", width, GET_FLOAT_VAL(val));
break;
case TSDB_DATA_TYPE_DOUBLE:
printf("%*.9f", width, GET_DOUBLE_VAL(val));
break;
case TSDB_DATA_TYPE_BINARY:
case TSDB_DATA_TYPE_NCHAR:
shellPrintNChar(val, length, width);
break;
case TSDB_DATA_TYPE_TIMESTAMP:
2022-04-21 13:09:43 +00:00
shellFormatTimestamp(buf, *(int64_t *)val, precision);
printf("%s", buf);
break;
default:
break;
2019-07-11 08:36:16 +00:00
}
}
2019-07-11 08:36:16 +00:00
2022-04-21 13:09:43 +00:00
int32_t shellVerticalPrintResult(TAOS_RES *tres) {
2020-06-01 15:30:10 +00:00
TAOS_ROW row = taos_fetch_row(tres);
if (row == NULL) {
return 0;
}
2022-04-21 13:09:43 +00:00
int32_t num_fields = taos_num_fields(tres);
2020-06-01 15:30:10 +00:00
TAOS_FIELD *fields = taos_fetch_fields(tres);
2022-04-21 13:09:43 +00:00
int32_t precision = taos_result_precision(tres);
2022-04-21 13:09:43 +00:00
int32_t maxColNameLen = 0;
for (int32_t col = 0; col < num_fields; col++) {
int32_t len = (int32_t)strlen(fields[col].name);
if (len > maxColNameLen) {
maxColNameLen = len;
}
}
2021-02-24 11:45:50 +00:00
uint64_t resShowMaxNum = UINT64_MAX;
2022-04-22 09:59:41 +00:00
if (shell.args.commands == NULL && shell.args.file[0] == 0) {
2022-04-21 13:09:43 +00:00
resShowMaxNum = SHELL_DEFAULT_RES_SHOW_NUM;
2021-02-24 10:43:48 +00:00
}
2022-04-21 13:09:43 +00:00
int32_t numOfRows = 0;
int32_t showMore = 1;
do {
2021-02-24 10:43:48 +00:00
if (numOfRows < resShowMaxNum) {
2021-02-25 02:15:01 +00:00
printf("*************************** %d.row ***************************\n", numOfRows + 1);
2021-12-17 07:49:27 +00:00
int32_t *length = taos_fetch_lengths(tres);
2021-02-24 11:45:50 +00:00
2022-04-21 13:09:43 +00:00
for (int32_t i = 0; i < num_fields; i++) {
2021-12-17 07:49:27 +00:00
TAOS_FIELD *field = fields + i;
2022-04-21 13:09:43 +00:00
int32_t padding = (int32_t)(maxColNameLen - strlen(field->name));
2021-02-24 10:43:48 +00:00
printf("%*.s%s: ", padding, " ", field->name);
2022-04-21 13:09:43 +00:00
shellPrintField((const char *)row[i], field, 0, length[i], precision);
2021-02-24 10:43:48 +00:00
putchar('\n');
}
2021-02-24 11:45:50 +00:00
} else if (showMore) {
2021-12-17 07:49:27 +00:00
printf("[100 Rows showed, and more rows are fetching but will not be showed. You can ctrl+c to stop or wait.]\n");
printf("[You can add limit statement to get more or redirect results to specific file to get all.]\n");
showMore = 0;
}
numOfRows++;
2020-06-01 15:30:10 +00:00
row = taos_fetch_row(tres);
2021-12-17 07:49:27 +00:00
} while (row != NULL);
return numOfRows;
}
2022-04-21 13:09:43 +00:00
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
int32_t width = (int32_t)strlen(field->name);
switch (field->type) {
case TSDB_DATA_TYPE_BOOL:
2022-01-24 04:53:17 +00:00
return TMAX(5, width); // 'false'
case TSDB_DATA_TYPE_TINYINT:
case TSDB_DATA_TYPE_UTINYINT:
2022-01-24 04:53:17 +00:00
return TMAX(4, width); // '-127'
case TSDB_DATA_TYPE_SMALLINT:
case TSDB_DATA_TYPE_USMALLINT:
2022-01-24 04:53:17 +00:00
return TMAX(6, width); // '-32767'
case TSDB_DATA_TYPE_INT:
case TSDB_DATA_TYPE_UINT:
2022-01-24 04:53:17 +00:00
return TMAX(11, width); // '-2147483648'
case TSDB_DATA_TYPE_BIGINT:
case TSDB_DATA_TYPE_UBIGINT:
2022-01-24 04:53:17 +00:00
return TMAX(21, width); // '-9223372036854775807'
case TSDB_DATA_TYPE_FLOAT:
2022-01-24 04:53:17 +00:00
return TMAX(20, width);
case TSDB_DATA_TYPE_DOUBLE:
2022-01-24 04:53:17 +00:00
return TMAX(25, width);
case TSDB_DATA_TYPE_BINARY:
2022-04-21 13:09:43 +00:00
if (field->bytes > shell.args.displayWidth) {
return TMAX(shell.args.displayWidth, width);
} else {
2022-01-24 04:53:17 +00:00
return TMAX(field->bytes, width);
}
case TSDB_DATA_TYPE_NCHAR: {
int16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
2022-04-21 13:09:43 +00:00
if (bytes > shell.args.displayWidth) {
return TMAX(shell.args.displayWidth, width);
} else {
2022-01-24 04:53:17 +00:00
return TMAX(bytes, width);
}
}
case TSDB_DATA_TYPE_TIMESTAMP:
2022-04-21 13:09:43 +00:00
if (shell.args.is_raw_time) {
2022-01-24 04:53:17 +00:00
return TMAX(14, width);
2021-12-17 07:49:27 +00:00
}
if (precision == TSDB_TIME_PRECISION_NANO) {
2022-01-24 04:53:17 +00:00
return TMAX(29, width);
} else if (precision == TSDB_TIME_PRECISION_MICRO) {
2022-01-24 04:53:17 +00:00
return TMAX(26, width); // '2020-01-01 00:00:00.000000'
} else {
2022-01-24 04:53:17 +00:00
return TMAX(23, width); // '2020-01-01 00:00:00.000'
2019-11-07 10:12:26 +00:00
}
2019-07-11 08:36:16 +00:00
default:
assert(false);
2019-07-11 08:36:16 +00:00
}
return 0;
}
2019-07-11 08:36:16 +00:00
2022-04-21 13:09:43 +00:00
void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields) {
int32_t rowWidth = 0;
for (int32_t col = 0; col < num_fields; col++) {
2021-12-17 07:49:27 +00:00
TAOS_FIELD *field = fields + col;
2022-04-21 13:09:43 +00:00
int32_t padding = (int32_t)(width[col] - strlen(field->name));
int32_t left = padding / 2;
printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
rowWidth += width[col] + 3;
}
putchar('\n');
2022-04-21 13:09:43 +00:00
for (int32_t i = 0; i < rowWidth; i++) {
putchar('=');
}
putchar('\n');
}
2022-04-21 13:09:43 +00:00
int32_t shellHorizontalPrintResult(TAOS_RES *tres) {
2020-06-01 15:30:10 +00:00
TAOS_ROW row = taos_fetch_row(tres);
if (row == NULL) {
return 0;
}
2022-04-21 13:09:43 +00:00
int32_t num_fields = taos_num_fields(tres);
2020-06-01 15:30:10 +00:00
TAOS_FIELD *fields = taos_fetch_fields(tres);
2022-04-21 13:09:43 +00:00
int32_t precision = taos_result_precision(tres);
2022-04-21 13:09:43 +00:00
int32_t width[TSDB_MAX_COLUMNS];
for (int32_t col = 0; col < num_fields; col++) {
width[col] = shellCalcColWidth(fields + col, precision);
}
2022-04-21 13:09:43 +00:00
shellPrintHeader(fields, width, num_fields);
2021-02-24 11:45:50 +00:00
uint64_t resShowMaxNum = UINT64_MAX;
2022-04-22 09:59:41 +00:00
if (shell.args.commands == NULL && shell.args.file[0] == 0) {
2022-04-21 13:09:43 +00:00
resShowMaxNum = SHELL_DEFAULT_RES_SHOW_NUM;
2021-02-24 10:43:48 +00:00
}
2022-04-21 13:09:43 +00:00
int32_t numOfRows = 0;
int32_t showMore = 1;
do {
2021-12-17 07:49:27 +00:00
int32_t *length = taos_fetch_lengths(tres);
2021-02-24 10:43:48 +00:00
if (numOfRows < resShowMaxNum) {
2022-04-21 13:09:43 +00:00
for (int32_t i = 0; i < num_fields; i++) {
2021-02-24 10:43:48 +00:00
putchar(' ');
2022-04-21 13:09:43 +00:00
shellPrintField((const char *)row[i], fields + i, width[i], length[i], precision);
2021-02-24 10:43:48 +00:00
putchar(' ');
putchar('|');
}
putchar('\n');
2021-02-24 11:45:50 +00:00
} else if (showMore) {
2021-12-17 07:49:27 +00:00
printf("[100 Rows showed, and more rows are fetching but will not be showed. You can ctrl+c to stop or wait.]\n");
printf("[You can add limit statement to show more or redirect results to specific file to get all.]\n");
showMore = 0;
}
numOfRows++;
2020-06-01 15:30:10 +00:00
row = taos_fetch_row(tres);
2021-12-17 07:49:27 +00:00
} while (row != NULL);
return numOfRows;
}
2022-04-21 13:09:43 +00:00
int32_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical) {
int32_t numOfRows = 0;
2019-07-11 08:36:16 +00:00
if (fname != NULL) {
2022-04-21 13:09:43 +00:00
numOfRows = shellDumpResultToFile(fname, tres);
2021-12-17 07:49:27 +00:00
} else if (vertical) {
2022-04-21 13:09:43 +00:00
numOfRows = shellVerticalPrintResult(tres);
} else {
2022-04-21 13:09:43 +00:00
numOfRows = shellHorizontalPrintResult(tres);
2019-07-11 08:36:16 +00:00
}
2020-06-01 15:30:10 +00:00
*error_no = taos_errno(tres);
2019-07-11 08:36:16 +00:00
return numOfRows;
}
2022-04-20 11:44:25 +00:00
void shellReadHistory() {
2022-04-21 13:09:43 +00:00
SShellHistory *pHistory = &shell.history;
TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_READ | TD_FILE_STREAM);
if (pFile == NULL) return;
2019-07-11 08:36:16 +00:00
2022-04-21 13:09:43 +00:00
char *line = NULL;
int32_t read_size = 0;
while ((read_size = taosGetLineFile(pFile, &line)) != -1) {
2019-07-11 08:36:16 +00:00
line[read_size - 1] = '\0';
2022-04-21 13:09:43 +00:00
pHistory->hist[pHistory->hend] = strdup(line);
2019-07-11 08:36:16 +00:00
2022-04-21 13:09:43 +00:00
pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
2019-07-11 08:36:16 +00:00
2022-04-21 13:09:43 +00:00
if (pHistory->hend == pHistory->hstart) {
pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
2019-07-11 08:36:16 +00:00
}
}
2022-04-18 13:14:32 +00:00
if (line != NULL) taosMemoryFree(line);
taosCloseFile(&pFile);
2019-07-11 08:36:16 +00:00
}
2022-04-20 11:44:25 +00:00
void shellWriteHistory() {
2022-04-21 13:09:43 +00:00
SShellHistory *pHistory = &shell.history;
2022-04-25 11:29:15 +00:00
TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_APPEND);
2022-04-21 13:09:43 +00:00
if (pFile == NULL) return;
2019-07-11 08:36:16 +00:00
2022-04-21 13:09:43 +00:00
for (int32_t i = pHistory->hstart; i != pHistory->hend;) {
if (pHistory->hist[i] != NULL) {
taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
taosMemoryFreeClear(pHistory->hist[i]);
2019-07-11 08:36:16 +00:00
}
2022-04-21 13:09:43 +00:00
i = (i + 1) % SHELL_MAX_HISTORY_SIZE;
2019-07-11 08:36:16 +00:00
}
2022-04-22 09:59:41 +00:00
taosFsyncFile(pFile);
taosCloseFile(&pFile);
2019-07-11 08:36:16 +00:00
}
2022-04-20 11:44:25 +00:00
void shellPrintError(TAOS_RES *tres, int64_t st) {
2020-11-10 03:36:13 +00:00
int64_t et = taosGetTimestampUs();
fprintf(stderr, "\nDB error: %s (%.6fs)\n", taos_errstr(tres), (et - st) / 1E6);
2020-06-01 15:30:10 +00:00
taos_free_result(tres);
2019-07-11 08:36:16 +00:00
}
2022-04-21 13:09:43 +00:00
bool shellIsCommentLine(char *line) {
if (line == NULL) return true;
2022-04-20 11:44:25 +00:00
return shellRegexMatch(line, "^\\s*#.*", REG_EXTENDED);
2019-07-11 08:36:16 +00:00
}
2022-04-21 13:09:43 +00:00
void shellSourceFile(const char *file) {
int32_t read_len = 0;
char *cmd = taosMemoryCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN + 1);
size_t cmd_len = 0;
char *line = NULL;
2022-04-22 09:59:41 +00:00
char fullname[PATH_MAX] = {0};
2019-07-11 08:36:16 +00:00
2022-04-22 09:59:41 +00:00
if (taosExpandDir(file, fullname, PATH_MAX) != 0) {
tstrncpy(fullname, file, PATH_MAX);
2019-07-11 08:36:16 +00:00
}
2022-04-22 09:59:41 +00:00
TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_READ | TD_FILE_STREAM);
if (pFile == NULL) {
2022-04-22 11:27:00 +00:00
fprintf(stderr, "failed to open file %s\n", fullname);
2022-03-25 16:29:53 +00:00
taosMemoryFree(cmd);
2019-07-11 08:36:16 +00:00
return;
}
while ((read_len = taosGetLineFile(pFile, &line)) != -1) {
2022-01-27 08:03:26 +00:00
if (read_len >= TSDB_MAX_ALLOWED_SQL_LEN) continue;
2019-07-11 08:36:16 +00:00
line[--read_len] = '\0';
2022-04-21 13:09:43 +00:00
if (read_len == 0 || shellIsCommentLine(line)) { // line starts with #
2019-07-11 08:36:16 +00:00
continue;
}
if (line[read_len - 1] == '\\') {
line[read_len - 1] = ' ';
memcpy(cmd + cmd_len, line, read_len);
cmd_len += read_len;
continue;
}
memcpy(cmd + cmd_len, line, read_len);
2022-04-21 13:09:43 +00:00
printf("%s%s\n", shell.info.promptHeader, cmd);
shellRunCommand(cmd);
2022-01-27 08:03:26 +00:00
memset(cmd, 0, TSDB_MAX_ALLOWED_SQL_LEN);
2019-07-11 08:36:16 +00:00
cmd_len = 0;
}
2022-03-25 16:29:53 +00:00
taosMemoryFree(cmd);
2022-04-18 13:14:32 +00:00
if (line != NULL) taosMemoryFree(line);
taosCloseFile(&pFile);
2019-07-11 08:36:16 +00:00
}
2019-11-07 10:12:26 +00:00
2022-04-21 13:09:43 +00:00
void shellGetGrantInfo() {
2022-04-22 06:18:00 +00:00
char sinfo[1024] = {0};
tstrncpy(sinfo, taos_get_server_info(shell.conn), sizeof(sinfo));
strtok(sinfo, "\n");
2019-11-07 10:12:26 +00:00
char sql[] = "show grants";
2022-04-21 13:09:43 +00:00
TAOS_RES *tres = taos_query(shell.conn, sql);
2020-06-06 07:47:28 +00:00
2022-04-21 13:09:43 +00:00
int32_t code = taos_errno(tres);
2020-01-16 05:11:44 +00:00
if (code != TSDB_CODE_SUCCESS) {
2022-04-21 13:09:43 +00:00
if (code == TSDB_CODE_OPS_NOT_SUPPORT) {
2022-04-22 06:18:00 +00:00
fprintf(stdout, "Server is Community Edition, %s\n\n", sinfo);
2020-01-16 05:11:44 +00:00
} else {
2022-04-22 06:18:00 +00:00
fprintf(stderr, "Failed to check Server Edition, Reason:0x%04x:%s\n\n", taos_errno(shell.conn),
2022-04-21 13:09:43 +00:00
taos_errstr(shell.conn));
2020-01-16 05:11:44 +00:00
}
2019-11-07 10:12:26 +00:00
return;
}
2022-04-21 13:09:43 +00:00
int32_t num_fields = taos_field_count(tres);
2019-11-07 10:12:26 +00:00
if (num_fields == 0) {
fprintf(stderr, "\nInvalid grant information.\n");
exit(0);
} else {
2020-06-19 04:13:08 +00:00
if (tres == NULL) {
2019-11-07 10:12:26 +00:00
fprintf(stderr, "\nGrant information is null.\n");
exit(0);
}
2020-06-19 04:13:08 +00:00
TAOS_FIELD *fields = taos_fetch_fields(tres);
2022-04-21 13:09:43 +00:00
TAOS_ROW row = taos_fetch_row(tres);
2019-11-07 10:12:26 +00:00
if (row == NULL) {
2019-11-14 08:34:13 +00:00
fprintf(stderr, "\nFailed to get grant information from server. Abort.\n");
2019-11-07 10:12:26 +00:00
exit(0);
}
2020-01-16 05:11:44 +00:00
char serverVersion[32] = {0};
2019-11-07 10:12:26 +00:00
char expiretime[32] = {0};
char expired[32] = {0};
2020-01-16 05:11:44 +00:00
memcpy(serverVersion, row[0], fields[0].bytes);
2019-11-07 10:12:26 +00:00
memcpy(expiretime, row[1], fields[1].bytes);
memcpy(expired, row[2], fields[2].bytes);
if (strcmp(expiretime, "unlimited") == 0) {
2022-04-22 06:18:00 +00:00
fprintf(stdout, "Server is Enterprise %s Edition, %s and will never expire.\n", serverVersion, sinfo);
2019-11-07 10:12:26 +00:00
} else {
2022-04-22 06:18:00 +00:00
fprintf(stdout, "Server is Enterprise %s Edition, %s and will expire at %s.\n", serverVersion, sinfo, expiretime);
2019-11-07 10:12:26 +00:00
}
2020-06-19 04:13:08 +00:00
taos_free_result(tres);
2019-11-07 10:12:26 +00:00
}
fprintf(stdout, "\n");
2022-04-21 13:09:43 +00:00
}
void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context) { tsem_post(&shell.cancelSem); }
2022-04-24 12:45:42 +00:00
void shellSigintHandler(int32_t signum, void *sigInfo, void *context) {
// do nothing
}
2022-04-21 13:09:43 +00:00
void shellCleanup(void *arg) { taosResetTerminalMode(); }
void *shellCancelHandler(void *arg) {
setThreadName("shellCancelHandler");
while (1) {
if (tsem_wait(&shell.cancelSem) != 0) {
taosMsleep(10);
continue;
}
taosResetTerminalMode();
2022-04-24 12:45:42 +00:00
printf("\nReceive SIGTERM or other signal, quit shell.\n");
2022-04-22 06:18:00 +00:00
shellWriteHistory();
shellExit();
2022-04-21 13:09:43 +00:00
}
return NULL;
}
void *shellThreadLoop(void *arg) {
setThreadName("shellThreadLoop");
taosGetOldTerminalMode();
taosThreadCleanupPush(shellCleanup, NULL);
char *command = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE);
if (command == NULL) {
printf("failed to malloc command\n");
return NULL;
}
do {
memset(command, 0, SHELL_MAX_COMMAND_SIZE);
taosSetTerminalMode();
if (shellReadCommand(command) != 0) {
break;
}
taosResetTerminalMode();
} while (shellRunCommand(command) == 0);
taosMemoryFreeClear(command);
2022-04-22 06:18:00 +00:00
shellWriteHistory();
shellExit();
2022-04-21 13:29:21 +00:00
2022-04-21 13:09:43 +00:00
taosThreadCleanupPop(1);
return NULL;
}
int32_t shellExecute() {
printf(shell.info.clientVersion, shell.info.osname, taos_get_client_info());
fflush(stdout);
SShellArgs *pArgs = &shell.args;
if (shell.args.auth == NULL) {
shell.conn = taos_connect(pArgs->host, pArgs->user, pArgs->password, pArgs->database, pArgs->port);
} else {
shell.conn = taos_connect_auth(pArgs->host, pArgs->user, pArgs->auth, pArgs->database, pArgs->port);
}
if (shell.conn == NULL) {
fflush(stdout);
return -1;
}
2022-04-22 11:27:00 +00:00
shellReadHistory();
2022-04-22 09:59:41 +00:00
if (pArgs->commands != NULL || pArgs->file[0] != 0) {
2022-04-21 13:09:43 +00:00
if (pArgs->commands != NULL) {
printf("%s%s\n", shell.info.promptHeader, pArgs->commands);
char *cmd = strdup(pArgs->commands);
shellRunCommand(cmd);
taosMemoryFree(cmd);
}
2022-04-22 09:59:41 +00:00
if (pArgs->file[0] != 0) {
2022-04-21 13:09:43 +00:00
shellSourceFile(pArgs->file);
}
taos_close(shell.conn);
shellWriteHistory();
return 0;
}
if (tsem_init(&shell.cancelSem, 0, 0) != 0) {
printf("failed to create cancel semphore\n");
return -1;
}
TdThread spid = {0};
taosThreadCreate(&spid, NULL, shellCancelHandler, NULL);
taosSetSignal(SIGTERM, shellQueryInterruptHandler);
taosSetSignal(SIGHUP, shellQueryInterruptHandler);
taosSetSignal(SIGABRT, shellQueryInterruptHandler);
2022-04-24 12:45:42 +00:00
taosSetSignal(SIGINT, shellSigintHandler);
2022-04-21 13:09:43 +00:00
shellGetGrantInfo(shell.conn);
while (1) {
taosThreadCreate(&shell.pid, NULL, shellThreadLoop, shell.conn);
taosThreadJoin(shell.pid, NULL);
}
return 0;
2019-11-07 10:12:26 +00:00
}