Skip to content

Commit 9b7a84f

Browse files
committed
Tweak psql to print row counts when \x auto chooses non-expanded output.
Noah Misch
1 parent f2f9439 commit 9b7a84f

File tree

5 files changed

+62
-30
lines changed

5 files changed

+62
-30
lines changed

src/bin/psql/command.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,12 +2407,12 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
24072407
else if (strcmp(param, "footer") == 0)
24082408
{
24092409
if (value)
2410-
popt->default_footer = ParseVariableBool(value);
2410+
popt->topt.default_footer = ParseVariableBool(value);
24112411
else
2412-
popt->default_footer = !popt->default_footer;
2412+
popt->topt.default_footer = !popt->topt.default_footer;
24132413
if (!quiet)
24142414
{
2415-
if (popt->default_footer)
2415+
if (popt->topt.default_footer)
24162416
puts(_("Default footer is on."));
24172417
else
24182418
puts(_("Default footer is off."));

src/bin/psql/describe.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@ describeOneTableDetails(const char *schemaname,
11301130

11311131
retval = false;
11321132

1133+
myopt.default_footer = false;
11331134
/* This output looks confusing in expanded mode. */
11341135
myopt.expanded = false;
11351136

@@ -2363,6 +2364,8 @@ describeRoles(const char *pattern, bool verbose)
23632364
const char align = 'l';
23642365
char **attr;
23652366

2367+
myopt.default_footer = false;
2368+
23662369
initPQExpBuffer(&buf);
23672370

23682371
if (pset.sversion >= 80100)
@@ -3362,7 +3365,7 @@ describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
33623365
sprintf(title, _("Text search parser \"%s\""), prsname);
33633366
myopt.title = title;
33643367
myopt.footers = NULL;
3365-
myopt.default_footer = false;
3368+
myopt.topt.default_footer = false;
33663369
myopt.translate_header = true;
33673370
myopt.translate_columns = translate_columns;
33683371

@@ -3393,7 +3396,7 @@ describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
33933396
sprintf(title, _("Token types for parser \"%s\""), prsname);
33943397
myopt.title = title;
33953398
myopt.footers = NULL;
3396-
myopt.default_footer = true;
3399+
myopt.topt.default_footer = true;
33973400
myopt.translate_header = true;
33983401
myopt.translate_columns = NULL;
33993402

@@ -3725,7 +3728,7 @@ describeOneTSConfig(const char *oid, const char *nspname, const char *cfgname,
37253728
myopt.nullPrint = NULL;
37263729
myopt.title = title.data;
37273730
myopt.footers = NULL;
3728-
myopt.default_footer = false;
3731+
myopt.topt.default_footer = false;
37293732
myopt.translate_header = true;
37303733

37313734
printQuery(res, &myopt, pset.queryFout, pset.logfile);

src/bin/psql/print.c

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ static char *decimal_point;
4444
static char *grouping;
4545
static char *thousands_sep;
4646

47+
static char default_footer[100];
48+
static printTableFooter default_footer_cell = { default_footer, NULL };
49+
4750
/* Line style control structures */
4851
const printTextFormat pg_asciiformat =
4952
{
@@ -278,6 +281,34 @@ print_separator(struct separator sep, FILE *fout)
278281
}
279282

280283

284+
/*
285+
* Return the list of explicitly-requested footers or, when applicable, the
286+
* default "(xx rows)" footer. Always omit the default footer when given
287+
* non-default footers, "\pset footer off", or a specific instruction to that
288+
* effect from a calling backslash command. Vertical formats number each row,
289+
* making the default footer redundant; they do not call this function.
290+
*
291+
* The return value may point to static storage; do not keep it across calls.
292+
*/
293+
static printTableFooter *
294+
footers_with_default(const printTableContent *cont)
295+
{
296+
if (cont->footers == NULL && cont->opt->default_footer)
297+
{
298+
unsigned long total_records;
299+
300+
total_records = cont->opt->prior_records + cont->nrows;
301+
snprintf(default_footer, sizeof(default_footer),
302+
ngettext("(%lu row)", "(%lu rows)", total_records),
303+
total_records);
304+
305+
return &default_footer_cell;
306+
}
307+
else
308+
return cont->footers;
309+
}
310+
311+
281312
/*************************/
282313
/* Unaligned text */
283314
/*************************/
@@ -340,11 +371,13 @@ print_unaligned_text(const printTableContent *cont, FILE *fout)
340371
/* print footers */
341372
if (cont->opt->stop_table)
342373
{
343-
if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
374+
printTableFooter *footers = footers_with_default(cont);
375+
376+
if (!opt_tuples_only && footers != NULL && !cancel_pressed)
344377
{
345378
printTableFooter *f;
346379

347-
for (f = cont->footers; f; f = f->next)
380+
for (f = footers; f; f = f->next)
348381
{
349382
if (need_recordsep)
350383
{
@@ -1034,16 +1067,18 @@ print_aligned_text(const printTableContent *cont, FILE *fout)
10341067

10351068
if (cont->opt->stop_table)
10361069
{
1070+
printTableFooter *footers = footers_with_default(cont);
1071+
10371072
if (opt_border == 2 && !cancel_pressed)
10381073
_print_horizontal_line(col_count, width_wrap, opt_border,
10391074
PRINT_RULE_BOTTOM, format, fout);
10401075

10411076
/* print footers */
1042-
if (cont->footers && !opt_tuples_only && !cancel_pressed)
1077+
if (footers && !opt_tuples_only && !cancel_pressed)
10431078
{
10441079
printTableFooter *f;
10451080

1046-
for (f = cont->footers; f; f = f->next)
1081+
for (f = footers; f; f = f->next)
10471082
fprintf(fout, "%s\n", f->data);
10481083
}
10491084

@@ -1447,15 +1482,17 @@ print_html_text(const printTableContent *cont, FILE *fout)
14471482

14481483
if (cont->opt->stop_table)
14491484
{
1485+
printTableFooter *footers = footers_with_default(cont);
1486+
14501487
fputs("</table>\n", fout);
14511488

14521489
/* print footers */
1453-
if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
1490+
if (!opt_tuples_only && footers != NULL && !cancel_pressed)
14541491
{
14551492
printTableFooter *f;
14561493

14571494
fputs("<p>", fout);
1458-
for (f = cont->footers; f; f = f->next)
1495+
for (f = footers; f; f = f->next)
14591496
{
14601497
html_escaped_print(f->data, fout);
14611498
fputs("<br />\n", fout);
@@ -1668,17 +1705,19 @@ print_latex_text(const printTableContent *cont, FILE *fout)
16681705

16691706
if (cont->opt->stop_table)
16701707
{
1708+
printTableFooter *footers = footers_with_default(cont);
1709+
16711710
if (opt_border == 2)
16721711
fputs("\\hline\n", fout);
16731712

16741713
fputs("\\end{tabular}\n\n\\noindent ", fout);
16751714

16761715
/* print footers */
1677-
if (cont->footers && !opt_tuples_only && !cancel_pressed)
1716+
if (footers && !opt_tuples_only && !cancel_pressed)
16781717
{
16791718
printTableFooter *f;
16801719

1681-
for (f = cont->footers; f; f = f->next)
1720+
for (f = footers; f; f = f->next)
16821721
{
16831722
latex_escaped_print(f->data, fout);
16841723
fputs(" \\\\\n", fout);
@@ -1871,14 +1910,16 @@ print_troff_ms_text(const printTableContent *cont, FILE *fout)
18711910

18721911
if (cont->opt->stop_table)
18731912
{
1913+
printTableFooter *footers = footers_with_default(cont);
1914+
18741915
fputs(".TE\n.DS L\n", fout);
18751916

18761917
/* print footers */
1877-
if (cont->footers && !opt_tuples_only && !cancel_pressed)
1918+
if (footers && !opt_tuples_only && !cancel_pressed)
18781919
{
18791920
printTableFooter *f;
18801921

1881-
for (f = cont->footers; f; f = f->next)
1922+
for (f = footers; f; f = f->next)
18821923
{
18831924
troff_ms_escaped_print(f->data, fout);
18841925
fputc('\n', fout);
@@ -2481,18 +2522,6 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, FILE *f
24812522
for (footer = opt->footers; *footer; footer++)
24822523
printTableAddFooter(&cont, *footer);
24832524
}
2484-
else if (!opt->topt.expanded && opt->default_footer)
2485-
{
2486-
unsigned long total_records;
2487-
char default_footer[100];
2488-
2489-
total_records = opt->topt.prior_records + cont.nrows;
2490-
snprintf(default_footer, sizeof(default_footer),
2491-
ngettext("(%lu row)", "(%lu rows)", total_records),
2492-
total_records);
2493-
2494-
printTableAddFooter(&cont, default_footer);
2495-
}
24962525

24972526
printTable(&cont, fout, flog);
24982527
printTableCleanup(&cont);

src/bin/psql/print.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ typedef struct printTableOpt
8585
bool tuples_only; /* don't output headers, row counts, etc. */
8686
bool start_table; /* print start decoration, eg <table> */
8787
bool stop_table; /* print stop decoration, eg </table> */
88+
bool default_footer; /* allow "(xx rows)" default footer */
8889
unsigned long prior_records; /* start offset for record counters */
8990
const printTextFormat *line_style; /* line style (NULL for default) */
9091
struct separator fieldSep; /* field separator for unaligned text mode */
@@ -141,7 +142,6 @@ typedef struct printQueryOpt
141142
bool quote; /* quote all values as much as possible */
142143
char *title; /* override title */
143144
char **footers; /* override footer (default is "(xx rows)") */
144-
bool default_footer; /* print default footer if footers==NULL */
145145
bool translate_header; /* do gettext on column headers */
146146
const bool *translate_columns; /* translate_columns[i-1] => do
147147
* gettext on col i */

src/bin/psql/startup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ main(int argc, char *argv[])
129129
pset.popt.topt.pager = 1;
130130
pset.popt.topt.start_table = true;
131131
pset.popt.topt.stop_table = true;
132-
pset.popt.default_footer = true;
132+
pset.popt.topt.default_footer = true;
133133
/* We must get COLUMNS here before readline() sets it */
134134
pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
135135

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy