[supporting column output per row by row in shell]

This commit is contained in:
huili 2019-09-09 14:31:48 +08:00
parent 1ddd7336f9
commit aded75a8b5
4 changed files with 246 additions and 138 deletions

View File

@ -76,8 +76,8 @@ void source_file(TAOS* con, char* fptr);
void get_history_path(char* history); void get_history_path(char* history);
void cleanup_handler(void* arg); void cleanup_handler(void* arg);
void exitShell(); void exitShell();
int shellDumpResult(TAOS* con, char* fname, int* error_no); int shellDumpResult(TAOS* con, char* fname, int* error_no, bool printMode);
void shellPrintNChar(char* str, int width); void shellPrintNChar(char* str, int width, bool printMode);
#define max(a, b) ((int)(a) < (int)(b) ? (int)(b) : (int)(a)) #define max(a, b) ((int)(a) < (int)(b) ? (int)(b) : (int)(a))
/**************** Global variable declarations ****************/ /**************** Global variable declarations ****************/

View File

@ -128,6 +128,10 @@ void shellReplaceCtrlChar(char *str) {
*pstr = '\t'; *pstr = '\t';
pstr++; pstr++;
break; break;
case 'G':
*pstr++ = '\\';
*pstr++ = *str;
break;
case '\\': case '\\':
*pstr = '\\'; *pstr = '\\';
pstr++; pstr++;
@ -211,6 +215,7 @@ void shellRunCommandOnServer(TAOS *con, char command[]) {
char * sptr = NULL; char * sptr = NULL;
char * cptr = NULL; char * cptr = NULL;
char * fname = NULL; char * fname = NULL;
bool printMode = false;
if ((sptr = strstr(command, ">>")) != NULL) { if ((sptr = strstr(command, ">>")) != NULL) {
cptr = strstr(command, ";"); cptr = strstr(command, ";");
@ -226,6 +231,16 @@ void shellRunCommandOnServer(TAOS *con, char command[]) {
fname = full_path.we_wordv[0]; fname = full_path.we_wordv[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.
}
st = taosGetTimestampUs(); st = taosGetTimestampUs();
if (taos_query(con, command)) { if (taos_query(con, command)) {
@ -242,7 +257,7 @@ void shellRunCommandOnServer(TAOS *con, char command[]) {
int num_fields = taos_field_count(con); int num_fields = taos_field_count(con);
if (num_fields != 0) { // select and show kinds of commands if (num_fields != 0) { // select and show kinds of commands
int error_no = 0; int error_no = 0;
int numOfRows = shellDumpResult(con, fname, &error_no); int numOfRows = shellDumpResult(con, fname, &error_no, printMode);
if (numOfRows < 0) return; if (numOfRows < 0) return;
et = taosGetTimestampUs(); et = taosGetTimestampUs();
@ -294,7 +309,7 @@ int regex_match(const char *s, const char *reg, int cflags) {
return 0; return 0;
} }
int shellDumpResult(TAOS *con, char *fname, int *error_no) { int shellDumpResult(TAOS *con, char *fname, int *error_no, bool printMode) {
TAOS_ROW row = NULL; TAOS_ROW row = NULL;
int numOfRows = 0; int numOfRows = 0;
time_t tt; time_t tt;
@ -334,10 +349,12 @@ int shellDumpResult(TAOS *con, char *fname, int *error_no) {
row = taos_fetch_row(result); row = taos_fetch_row(result);
char t_str[TSDB_MAX_BYTES_PER_ROW] = "\0"; char t_str[TSDB_MAX_BYTES_PER_ROW] = "\0";
int l[TSDB_MAX_COLUMNS] = {0}; int l[TSDB_MAX_COLUMNS] = {0};
int maxLenColumnName = 0;
if (row) { if (row) {
// Print the header indicator // Print the header indicator
if (fname == NULL) { // print to standard output if (fname == NULL) { // print to standard output
if (!printMode) {
for (int col = 0; col < num_fields; col++) { for (int col = 0; col < num_fields; col++) {
switch (fields[col].type) { switch (fields[col].type) {
case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_BOOL:
@ -391,9 +408,15 @@ int shellDumpResult(TAOS *con, char *fname, int *error_no) {
printf("\n"); printf("\n");
for (int k = 0; k < output_bytes; k++) printf("="); for (int k = 0; k < output_bytes; k++) printf("=");
printf("\n"); printf("\n");
} else {
for (int col = 0; col < num_fields; col++) {
if (strlen(fields[col].name) > maxLenColumnName) maxLenColumnName = strlen(fields[col].name);
}
}
// print the elements // print the elements
do { do {
if (!printMode) {
for (int i = 0; i < num_fields; i++) { for (int i = 0; i < num_fields; i++) {
if (row[i] == NULL) { if (row[i] == NULL) {
printf("%*s|", l[i], TSDB_DATA_NULL_STR); printf("%*s|", l[i], TSDB_DATA_NULL_STR);
@ -433,7 +456,7 @@ int shellDumpResult(TAOS *con, char *fname, int *error_no) {
/* printf("%-*s|",max(fields[i].bytes, strlen(fields[i].name)), /* printf("%-*s|",max(fields[i].bytes, strlen(fields[i].name)),
* t_str); */ * t_str); */
/* printf("%-*s|", l[i], t_str); */ /* printf("%-*s|", l[i], t_str); */
shellPrintNChar(t_str, l[i]); shellPrintNChar(t_str, l[i], printMode);
break; break;
case TSDB_DATA_TYPE_TIMESTAMP: case TSDB_DATA_TYPE_TIMESTAMP:
if (args.is_raw_time) { if (args.is_raw_time) {
@ -463,8 +486,83 @@ int shellDumpResult(TAOS *con, char *fname, int *error_no) {
break; break;
} }
} }
printf("\n"); printf("\n");
} else {
printf("*************************** %d.row ***************************\n", numOfRows + 1);
for (int i = 0; i < num_fields; i++) {
// 1. print column name
int left_space = (int)(maxLenColumnName - strlen(fields[i].name));
printf("%*.s%s: ", left_space, " ", fields[i].name);
// 2. print column value
if (row[i] == NULL) {
printf("%s\n", TSDB_DATA_NULL_STR);
continue;
}
switch (fields[i].type) {
case TSDB_DATA_TYPE_BOOL:
printf("%s\n", ((((int)(*((char *)row[i]))) == 1) ? "true" : "false"));
break;
case TSDB_DATA_TYPE_TINYINT:
printf("%d\n", (int)(*((char *)row[i])));
break;
case TSDB_DATA_TYPE_SMALLINT:
printf("%d\n", (int)(*((short *)row[i])));
break;
case TSDB_DATA_TYPE_INT:
printf("%d\n", *((int *)row[i]));
break;
case TSDB_DATA_TYPE_BIGINT:
#ifdef WINDOWS
printf("%lld\n", *((int64_t *)row[i]));
#else
printf("%ld\n", *((int64_t *)row[i]));
#endif
break;
case TSDB_DATA_TYPE_FLOAT:
printf("%.5f\n", *((float *)row[i]));
break;
case TSDB_DATA_TYPE_DOUBLE:
printf("%.9f\n", *((double *)row[i]));
break;
case TSDB_DATA_TYPE_BINARY:
case TSDB_DATA_TYPE_NCHAR:
memset(t_str, 0, TSDB_MAX_BYTES_PER_ROW);
memcpy(t_str, row[i], fields[i].bytes);
l[i] = max(fields[i].bytes, strlen(fields[i].name));
shellPrintNChar(t_str, l[i], printMode);
break;
case TSDB_DATA_TYPE_TIMESTAMP:
if (args.is_raw_time) {
#ifdef WINDOWS
printf("%lld\n", *(int64_t *)row[i]);
#else
printf("%ld\n", *(int64_t *)row[i]);
#endif
} else {
if (taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO) {
tt = *(int64_t *)row[i] / 1000000;
} else {
tt = *(int64_t *)row[i] / 1000;
}
ptm = localtime(&tt);
strftime(buf, 64, "%y-%m-%d %H:%M:%S", ptm);
if (taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO) {
printf("%s.%06d\n", buf, (int)(*(int64_t *)row[i] % 1000000));
} else {
printf("%s.%03d\n", buf, (int)(*(int64_t *)row[i] % 1000));
}
}
break;
default:
break;
}
}
}
numOfRows++; numOfRows++;
} while ((row = taos_fetch_row(result))); } while ((row = taos_fetch_row(result)));

View File

@ -305,7 +305,7 @@ void *shellLoopQuery(void *arg) {
return NULL; return NULL;
} }
void shellPrintNChar(char *str, int width) { void shellPrintNChar(char *str, int width, bool printMode) {
int col_left = width; int col_left = width;
wchar_t wc; wchar_t wc;
while (col_left > 0) { while (col_left > 0) {
@ -323,7 +323,12 @@ void shellPrintNChar(char *str, int width) {
printf(" "); printf(" ");
col_left--; col_left--;
} }
if (!printMode) {
printf("|"); printf("|");
} else {
printf("\n");
}
} }
int get_old_terminal_mode(struct termios *tio) { int get_old_terminal_mode(struct termios *tio) {

View File

@ -216,7 +216,7 @@ void *shellLoopQuery(void *arg) {
return NULL; return NULL;
} }
void shellPrintNChar(char *str, int width) { void shellPrintNChar(char *str, int width, bool printMode) {
int col_left = width; int col_left = width;
wchar_t wc; wchar_t wc;
while (col_left > 0) { while (col_left > 0) {
@ -234,7 +234,12 @@ void shellPrintNChar(char *str, int width) {
printf(" "); printf(" ");
col_left--; col_left--;
} }
if (!printMode) {
printf("|"); printf("|");
} else {
printf("\n");
}
} }
void get_history_path(char *history) { sprintf(history, "%s/%s", ".", HISTORY_FILE); } void get_history_path(char *history) { sprintf(history, "%s/%s", ".", HISTORY_FILE); }