From c09b18f21c52cbcf8718d6c267c84fcfea3739a9 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Fri, 8 Apr 2016 20:23:18 -0300 Subject: Support \crosstabview in psql MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit \crosstabview is a completely different way to display results from a query: instead of a vertical display of rows, the data values are placed in a grid where the column and row headers come from the data itself, similar to a spreadsheet. The sort order of the horizontal header can be specified by using another column in the query, and the vertical header determines its ordering from the order in which they appear in the query. This only allows displaying a single value in each cell. If more than one value correspond to the same cell, an error is thrown. Merging of values can be done in the query itself, if necessary. This may be revisited in the future. Author: Daniel Verité Reviewed-by: Pavel Stehule, Dean Rasheed --- src/bin/psql/common.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src/bin/psql/common.c') diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index df3441cc750..437cb568234 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -23,6 +23,7 @@ #include "settings.h" #include "command.h" #include "copy.h" +#include "crosstabview.h" #include "fe_utils/mbprint.h" @@ -1064,6 +1065,8 @@ PrintQueryResults(PGresult *results) success = StoreQueryTuple(results); else if (pset.gexec_flag) success = ExecQueryTuples(results); + else if (pset.crosstab_flag) + success = PrintResultsInCrosstab(results); else success = PrintQueryTuples(results); /* if it's INSERT/UPDATE/DELETE RETURNING, also print status */ @@ -1213,7 +1216,8 @@ SendQuery(const char *query) } } - if (pset.fetch_count <= 0 || pset.gexec_flag || !is_select_command(query)) + if (pset.fetch_count <= 0 || pset.gexec_flag || + pset.crosstab_flag || !is_select_command(query)) { /* Default fetch-it-all-and-print mode */ instr_time before, @@ -1356,6 +1360,24 @@ sendquery_cleanup: /* reset \gexec trigger */ pset.gexec_flag = false; + /* reset \crosstabview trigger */ + pset.crosstab_flag = false; + if (pset.ctv_col_V) + { + free(pset.ctv_col_V); + pset.ctv_col_V = NULL; + } + if (pset.ctv_col_H) + { + free(pset.ctv_col_H); + pset.ctv_col_H = NULL; + } + if (pset.ctv_col_D) + { + free(pset.ctv_col_D); + pset.ctv_col_D = NULL; + } + return OK; } @@ -1501,7 +1523,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) break; } - /* Note we do not deal with \gexec mode here */ + /* Note we do not deal with \gexec or \crosstabview modes here */ ntuples = PQntuples(results); -- cgit v1.2.3