Hi 2017-08-22 11:22 GMT+02:00 Pavel Stehule <pavel.steh...@gmail.com>:
> > > 2017-08-22 10:46 GMT+02:00 Fabien COELHO <coe...@cri.ensmp.fr>: > >> >> Hello Pavel, >> >> One my idea is introduction new simple output format and execution command >>> with result in this format. >>> >>> It should work something like >>> >>> \setenv GNUPLOT_OPTION '......' >>> SELECT * FROM data >>> \graw | gnuplot ... >>> >> >> I understand that it is kind of a shortcut for: >> >> \pset fieldsep ' ' >> \pset format unaligned >> \pset tuples_only on >> -- possibly other settings... >> SELECT * FROM data \g | gnuplot '...' > > >> And then you have to persuade gnuplot to take its data from stdin? > > > There are some methods > > https://stackoverflow.com/questions/17543386/pipe-plot- > data-to-gnuplot-script/17576571#17576571 > > > postgres=# select pocet_muzu + pocet_zen from obce postgres-# \graw | gnuplot -p -e "set terminal dumb; plot '-' with boxes" 1.4e+06 +-+-----+-------+-------+--------+-------+-------+-------+-----+-+ + + + + + + + + + | * '-' ******* | 1.2e+06 +-+ * +-+ | * | 1e+06 +-+ * +-+ | * | | * | 800000 +-+ * +-+ | * | | * | 600000 +-+ * +-+ | * | | * | 400000 +-+ * * +-+ | * * * | 200000 +-+ * * * +-+ | * * * * | + * * * +** * ** **** * * * * + *** **** + 0 +-+-----****************************************************---+-+ -1000 0 1000 2000 3000 4000 5000 6000 7000 postgres=# > >> >> -- >> Fabien. >> > >
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 96f3a402a4..90f88a8280 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -335,7 +335,8 @@ exec_command(const char *cmd, status = exec_command_errverbose(scan_state, active_branch); else if (strcmp(cmd, "f") == 0) status = exec_command_f(scan_state, active_branch); - else if (strcmp(cmd, "g") == 0 || strcmp(cmd, "gx") == 0) + else if (strcmp(cmd, "g") == 0 || strcmp(cmd, "gx") == 0 || + strcmp(cmd, "graw") == 0 || strcmp(cmd, "graw+") == 0) status = exec_command_g(scan_state, active_branch, cmd); else if (strcmp(cmd, "gexec") == 0) status = exec_command_gexec(scan_state, active_branch); @@ -1300,6 +1301,7 @@ exec_command_f(PsqlScanState scan_state, bool active_branch) /* * \g [filename] -- send query, optionally with output to file/pipe + * \graw [filename] -- same as \g with raw format * \gx [filename] -- same as \g, with expanded mode forced */ static backslashResult @@ -1322,6 +1324,10 @@ exec_command_g(PsqlScanState scan_state, bool active_branch, const char *cmd) free(fname); if (strcmp(cmd, "gx") == 0) pset.g_expanded = true; + else if (strcmp(cmd, "graw") == 0) + pset.g_raw = true; + else if (strcmp(cmd, "graw+") == 0) + pset.g_raw_header = true; status = PSQL_CMD_SEND; } else @@ -3701,6 +3707,8 @@ _align2string(enum printFormat in) case PRINT_TROFF_MS: return "troff-ms"; break; + case PRINT_RAW: + return "raw"; } return "unknown"; } diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 044cdb82a7..05af28ee40 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -816,6 +816,12 @@ PrintQueryTuples(const PGresult *results) /* one-shot expanded output requested via \gx */ if (pset.g_expanded) my_popt.topt.expanded = 1; + else if (pset.g_raw || pset.g_raw_header) + { + my_popt.topt.format = PRINT_RAW; + my_popt.topt.tuples_only = true; + my_popt.topt.column_header = pset.g_raw_header; + } /* write output to \g argument, if any */ if (pset.gfname) @@ -845,6 +851,8 @@ PrintQueryTuples(const PGresult *results) } + + /* * StoreQueryTuple: assuming query result is OK, save data into variables * @@ -1460,6 +1468,10 @@ sendquery_cleanup: /* reset \gx's expanded-mode flag */ pset.g_expanded = false; + /* reset \graw flags */ + pset.g_raw = false; + pset.g_raw_header = false; + /* reset \gset trigger */ if (pset.gset_prefix) { diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index b78f151acd..6a2fb6b2dd 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -92,6 +92,8 @@ typedef struct _psqlSettings char *gfname; /* one-shot file output argument for \g */ bool g_expanded; /* one-shot expanded output requested via \gx */ + bool g_raw; /* one-shot raw format for \graw */ + bool g_raw_header; /* one-shot show header for \graw+ */ char *gset_prefix; /* one-shot prefix argument for \gset */ bool gexec_flag; /* one-shot flag to execute query's results */ bool crosstab_flag; /* one-shot request to crosstab results */ diff --git a/src/fe_utils/print.c b/src/fe_utils/print.c index f756f767e5..63f42a13a0 100644 --- a/src/fe_utils/print.c +++ b/src/fe_utils/print.c @@ -2784,6 +2784,52 @@ print_troff_ms_vertical(const printTableContent *cont, FILE *fout) } +/*************************/ +/* RAW format */ +/*************************/ + +static void +print_raw_text(const printTableContent *cont, FILE *fout) +{ + unsigned int i; + const char *const *ptr; + + if (cancel_pressed) + return; + + if (cont->opt->column_header) + { + bool is_first = true; + + for (ptr = cont->headers; *ptr; ptr++) + { + if (!is_first) + fputc('\t', fout); + else + is_first = false; + fprintf(fout, "%s", *ptr); + } + fputc('\n', fout); + } + + /* print cells */ + for (i = 0, ptr = cont->cells; *ptr; i++, ptr++) + { + if (i % cont->ncolumns != 0) + { + if (cancel_pressed) + break; + fputc('\t', fout); + } + + fprintf(fout, "%s", *ptr); + + if ((i + 1) % cont->ncolumns == 0) + fputc('\n', fout); + } +} + + /********************************/ /* Public functions */ /********************************/ @@ -3262,6 +3308,10 @@ printTable(const printTableContent *cont, else print_troff_ms_text(cont, fout); break; + case PRINT_RAW: + /* expanded mode is ignored in this format */ + print_raw_text(cont, fout); + break; default: fprintf(stderr, _("invalid output format (internal error): %d"), cont->opt->format); diff --git a/src/include/fe_utils/print.h b/src/include/fe_utils/print.h index 36b89e7d57..0a25bd0cf8 100644 --- a/src/include/fe_utils/print.h +++ b/src/include/fe_utils/print.h @@ -33,7 +33,8 @@ enum printFormat PRINT_ASCIIDOC, PRINT_LATEX, PRINT_LATEX_LONGTABLE, - PRINT_TROFF_MS + PRINT_TROFF_MS, + PRINT_RAW /* add your favourite output format here ... */ }; @@ -108,6 +109,7 @@ typedef struct printTableOpt bool start_table; /* print start decoration, eg <table> */ bool stop_table; /* print stop decoration, eg </table> */ bool default_footer; /* allow "(xx rows)" default footer */ + bool column_header; /* print column header in raw mode */ unsigned long prior_records; /* start offset for record counters */ const printTextFormat *line_style; /* line style (NULL for default) */ struct separator fieldSep; /* field separator for unaligned text mode */
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers