Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6f0d6a5

Browse files
committed
Rethink \crosstabview's argument parsing logic.
\crosstabview interpreted its arguments in an unusual way, including doing case-insensitive matching of unquoted column names, which is surely not the right thing. Rip that out in favor of doing something equivalent to the dequoting/case-folding rules used by other psql commands. To keep it simple, change the syntax so that the optional sort column is specified as a separate argument, instead of the also-quite-unusual syntax that attached it to the colH argument with a colon. Also, rework the error messages to be closer to project style.
1 parent 4b74c6a commit 6f0d6a5

File tree

7 files changed

+156
-291
lines changed

7 files changed

+156
-291
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -993,9 +993,10 @@ testdb=>
993993
<varlistentry id="APP-PSQL-meta-commands-crosstabview">
994994
<term><literal>\crosstabview [
995995
<replaceable class="parameter">colV</replaceable>
996-
<replaceable class="parameter">colH</replaceable>[:<replaceable class="parameter">scolH</replaceable>]
997-
[<replaceable class="parameter">colD</replaceable>]
998-
] </literal></term>
996+
[ <replaceable class="parameter">colH</replaceable>
997+
[ <replaceable class="parameter">colD</replaceable>
998+
[ <replaceable class="parameter">sortcolH</replaceable>
999+
] ] ] ] </literal></term>
9991000
<listitem>
10001001
<para>
10011002
Executes the current query buffer (like <literal>\g</literal>) and
@@ -1004,16 +1005,11 @@ testdb=&gt;
10041005
The output column identified by <replaceable class="parameter">colV</>
10051006
becomes a vertical header and the output column identified by
10061007
<replaceable class="parameter">colH</replaceable>
1007-
becomes a horizontal header, optionally sorted by ranking data obtained
1008-
from column <replaceable class="parameter">scolH</replaceable>.
1008+
becomes a horizontal header.
10091009
<replaceable class="parameter">colD</replaceable> identifies
10101010
the output column to display within the grid.
1011-
If <replaceable class="parameter">colD</replaceable> is not
1012-
specified and there are exactly three columns in the result set,
1013-
the column that is neither
1014-
<replaceable class="parameter">colV</replaceable> nor
1015-
<replaceable class="parameter">colH</replaceable>
1016-
is displayed; if there are more columns, an error is reported.
1011+
<replaceable class="parameter">sortcolH</replaceable> identifies
1012+
an optional sort column for the horizontal header.
10171013
</para>
10181014

10191015
<para>
@@ -1024,6 +1020,12 @@ testdb=&gt;
10241020
and <replaceable class="parameter">colH</replaceable> as column 2.
10251021
<replaceable class="parameter">colH</replaceable> must differ from
10261022
<replaceable class="parameter">colV</replaceable>.
1023+
If <replaceable class="parameter">colD</replaceable> is not
1024+
specified, then there must be exactly three columns in the query
1025+
result, and the column that is neither
1026+
<replaceable class="parameter">colV</replaceable> nor
1027+
<replaceable class="parameter">colH</replaceable>
1028+
is taken to be <replaceable class="parameter">colD</replaceable>.
10271029
</para>
10281030

10291031
<para>
@@ -1037,11 +1039,11 @@ testdb=&gt;
10371039
found in column <replaceable class="parameter">colH</replaceable>,
10381040
with duplicates removed. By default, these appear in the same order
10391041
as in the query results. But if the
1040-
optional <replaceable class="parameter">scolH</> argument is given, it
1041-
identifies a column whose values must be integer numbers, and the
1042+
optional <replaceable class="parameter">sortcolH</> argument is given,
1043+
it identifies a column whose values must be integer numbers, and the
10421044
values from <replaceable class="parameter">colH</replaceable> will
10431045
appear in the horizontal header sorted according to the
1044-
corresponding <replaceable class="parameter">scolH</> values.
1046+
corresponding <replaceable class="parameter">sortcolH</> values.
10451047
</para>
10461048

10471049
<para>

src/bin/psql/command.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,11 @@ exec_command(const char *cmd,
368368
/* \crosstabview -- execute a query and display results in crosstab */
369369
else if (strcmp(cmd, "crosstabview") == 0)
370370
{
371-
pset.ctv_col_V = psql_scan_slash_option(scan_state,
372-
OT_NORMAL, NULL, false);
373-
pset.ctv_col_H = psql_scan_slash_option(scan_state,
374-
OT_NORMAL, NULL, false);
375-
pset.ctv_col_D = psql_scan_slash_option(scan_state,
376-
OT_NORMAL, NULL, false);
371+
int i;
377372

373+
for (i = 0; i < lengthof(pset.ctv_args); i++)
374+
pset.ctv_args[i] = psql_scan_slash_option(scan_state,
375+
OT_NORMAL, NULL, true);
378376
pset.crosstab_flag = true;
379377
status = PSQL_CMD_SEND;
380378
}

src/bin/psql/common.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@ SendQuery(const char *query)
11301130
PGTransactionStatusType transaction_status;
11311131
double elapsed_msec = 0;
11321132
bool OK = false;
1133+
int i;
11331134
bool on_error_rollback_savepoint = false;
11341135
static bool on_error_rollback_warning = false;
11351136

@@ -1362,20 +1363,10 @@ SendQuery(const char *query)
13621363

13631364
/* reset \crosstabview trigger */
13641365
pset.crosstab_flag = false;
1365-
if (pset.ctv_col_V)
1366+
for (i = 0; i < lengthof(pset.ctv_args); i++)
13661367
{
1367-
free(pset.ctv_col_V);
1368-
pset.ctv_col_V = NULL;
1369-
}
1370-
if (pset.ctv_col_H)
1371-
{
1372-
free(pset.ctv_col_H);
1373-
pset.ctv_col_H = NULL;
1374-
}
1375-
if (pset.ctv_col_D)
1376-
{
1377-
free(pset.ctv_col_D);
1378-
pset.ctv_col_D = NULL;
1368+
pg_free(pset.ctv_args[i]);
1369+
pset.ctv_args[i] = NULL;
13791370
}
13801371

13811372
return OK;

0 commit comments

Comments
 (0)