Skip to content

Commit 645cb44

Browse files
committed
Add \pset options for boolean value display
New \pset variables display_true and display_false allow the user to change how true and false values are displayed. Author: David G. Johnston <David.G.Johnston@gmail.com> Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de> Discussion: https://postgr.es/m/CAKFQuwYts3vnfQ5AoKhEaKMTNMfJ443MW2kFswKwzn7fiofkrw@mail.gmail.com Discussion: https://postgr.es/m/56308F56.8060908@joh.to
1 parent cf8be02 commit 645cb44

File tree

8 files changed

+123
-5
lines changed

8 files changed

+123
-5
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3099,6 +3099,26 @@ SELECT $1 \parse stmt1
30993099
</listitem>
31003100
</varlistentry>
31013101

3102+
<varlistentry id="app-psql-meta-command-pset-display-false">
3103+
<term><literal>display_false</literal></term>
3104+
<listitem>
3105+
<para>
3106+
Sets the string to be printed in place of a false value.
3107+
The default is to print <literal>f</literal>.
3108+
</para>
3109+
</listitem>
3110+
</varlistentry>
3111+
3112+
<varlistentry id="app-psql-meta-command-pset-display-true">
3113+
<term><literal>display_true</literal></term>
3114+
<listitem>
3115+
<para>
3116+
Sets the string to be printed in place of a true value.
3117+
The default is to print <literal>t</literal>.
3118+
</para>
3119+
</listitem>
3120+
</varlistentry>
3121+
31023122
<varlistentry id="app-psql-meta-command-pset-expanded">
31033123
<term><literal>expanded</literal> (or <literal>x</literal>)</term>
31043124
<listitem>

src/bin/psql/command.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2709,7 +2709,8 @@ exec_command_pset(PsqlScanState scan_state, bool active_branch)
27092709

27102710
int i;
27112711
static const char *const my_list[] = {
2712-
"border", "columns", "csv_fieldsep", "expanded", "fieldsep",
2712+
"border", "columns", "csv_fieldsep",
2713+
"display_false", "display_true", "expanded", "fieldsep",
27132714
"fieldsep_zero", "footer", "format", "linestyle", "null",
27142715
"numericlocale", "pager", "pager_min_lines",
27152716
"recordsep", "recordsep_zero",
@@ -5300,6 +5301,26 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
53005301
}
53015302
}
53025303

5304+
/* 'false' display */
5305+
else if (strcmp(param, "display_false") == 0)
5306+
{
5307+
if (value)
5308+
{
5309+
free(popt->falsePrint);
5310+
popt->falsePrint = pg_strdup(value);
5311+
}
5312+
}
5313+
5314+
/* 'true' display */
5315+
else if (strcmp(param, "display_true") == 0)
5316+
{
5317+
if (value)
5318+
{
5319+
free(popt->truePrint);
5320+
popt->truePrint = pg_strdup(value);
5321+
}
5322+
}
5323+
53035324
/* field separator for unaligned text */
53045325
else if (strcmp(param, "fieldsep") == 0)
53055326
{
@@ -5474,6 +5495,20 @@ printPsetInfo(const char *param, printQueryOpt *popt)
54745495
popt->topt.csvFieldSep);
54755496
}
54765497

5498+
/* show boolean 'false' display */
5499+
else if (strcmp(param, "display_false") == 0)
5500+
{
5501+
printf(_("Boolean false display is \"%s\".\n"),
5502+
popt->falsePrint ? popt->falsePrint : "f");
5503+
}
5504+
5505+
/* show boolean 'true' display */
5506+
else if (strcmp(param, "display_true") == 0)
5507+
{
5508+
printf(_("Boolean true display is \"%s\".\n"),
5509+
popt->truePrint ? popt->truePrint : "t");
5510+
}
5511+
54775512
/* show field separator for unaligned text */
54785513
else if (strcmp(param, "fieldsep") == 0)
54795514
{
@@ -5743,6 +5778,10 @@ pset_value_string(const char *param, printQueryOpt *popt)
57435778
return psprintf("%d", popt->topt.columns);
57445779
else if (strcmp(param, "csv_fieldsep") == 0)
57455780
return pset_quoted_string(popt->topt.csvFieldSep);
5781+
else if (strcmp(param, "display_false") == 0)
5782+
return pset_quoted_string(popt->falsePrint ? popt->falsePrint : "f");
5783+
else if (strcmp(param, "display_true") == 0)
5784+
return pset_quoted_string(popt->truePrint ? popt->truePrint : "t");
57465785
else if (strcmp(param, "expanded") == 0)
57475786
return pstrdup(popt->topt.expanded == 2
57485787
? "auto"

src/bin/psql/help.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,9 @@ slashUsage(unsigned short int pager)
290290
HELPN(" \\H toggle HTML output mode (currently %s)\n",
291291
ON(pset.popt.topt.format == PRINT_HTML));
292292
HELP0(" \\pset [NAME [VALUE]] set table output option\n"
293-
" (border|columns|csv_fieldsep|expanded|fieldsep|\n"
294-
" fieldsep_zero|footer|format|linestyle|null|\n"
295-
" numericlocale|pager|pager_min_lines|recordsep|\n"
293+
" (border|columns|csv_fieldsep|display_false|display_true|\n"
294+
" expanded|fieldsep|fieldsep_zero|footer|format|linestyle|\n"
295+
" null|numericlocale|pager|pager_min_lines|recordsep|\n"
296296
" recordsep_zero|tableattr|title|tuples_only|\n"
297297
" unicode_border_linestyle|unicode_column_linestyle|\n"
298298
" unicode_header_linestyle|xheader_width)\n");
@@ -480,6 +480,10 @@ helpVariables(unsigned short int pager)
480480
" border style (number)\n");
481481
HELP0(" columns\n"
482482
" target width for the wrapped format\n");
483+
HELP0(" display_false\n"
484+
" set the string to be printed in place of a boolean 'false'\n");
485+
HELP0(" display_true\n"
486+
" set the string to be printed in place of a boolean 'true'\n");
483487
HELP0(" expanded (or x)\n"
484488
" expanded output [on, off, auto]\n");
485489
HELPN(" fieldsep\n"

src/bin/psql/tab-complete.in.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5478,7 +5478,8 @@ match_previous_words(int pattern_id,
54785478
else if (TailMatchesCS("\\password"))
54795479
COMPLETE_WITH_QUERY(Query_for_list_of_roles);
54805480
else if (TailMatchesCS("\\pset"))
5481-
COMPLETE_WITH_CS("border", "columns", "csv_fieldsep", "expanded",
5481+
COMPLETE_WITH_CS("border", "columns", "csv_fieldsep",
5482+
"display_false", "display_true", "expanded",
54825483
"fieldsep", "fieldsep_zero", "footer", "format",
54835484
"linestyle", "null", "numericlocale",
54845485
"pager", "pager_min_lines",

src/fe_utils/print.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3775,6 +3775,10 @@ printQuery(const PGresult *result, const printQueryOpt *opt,
37753775

37763776
if (PQgetisnull(result, r, c))
37773777
cell = opt->nullPrint ? opt->nullPrint : "";
3778+
else if (PQftype(result, c) == BOOLOID)
3779+
cell = (PQgetvalue(result, r, c)[0] == 't' ?
3780+
(opt->truePrint ? opt->truePrint : "t") :
3781+
(opt->falsePrint ? opt->falsePrint : "f"));
37783782
else
37793783
{
37803784
cell = PQgetvalue(result, r, c);

src/include/fe_utils/print.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ typedef struct printQueryOpt
184184
{
185185
printTableOpt topt; /* the options above */
186186
char *nullPrint; /* how to print null entities */
187+
char *truePrint; /* how to print boolean true values */
188+
char *falsePrint; /* how to print boolean false values */
187189
char *title; /* override title */
188190
char **footers; /* override footer (default is "(xx rows)") */
189191
bool translate_header; /* do gettext on column headers */

src/test/regress/expected/psql.out

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ environment value
445445
border 1
446446
columns 0
447447
csv_fieldsep ','
448+
display_false 'f'
449+
display_true 't'
448450
expanded off
449451
fieldsep '|'
450452
fieldsep_zero off
@@ -464,6 +466,36 @@ unicode_border_linestyle single
464466
unicode_column_linestyle single
465467
unicode_header_linestyle single
466468
xheader_width full
469+
-- test the simple display substitution settings
470+
prepare q as select null as n, true as t, false as f;
471+
\pset null '(null)'
472+
\pset display_true 'true'
473+
\pset display_false 'false'
474+
execute q;
475+
n | t | f
476+
--------+------+-------
477+
(null) | true | false
478+
(1 row)
479+
480+
\pset null
481+
\pset display_true
482+
\pset display_false
483+
execute q;
484+
n | t | f
485+
--------+------+-------
486+
(null) | true | false
487+
(1 row)
488+
489+
\pset null ''
490+
\pset display_true 't'
491+
\pset display_false 'f'
492+
execute q;
493+
n | t | f
494+
---+---+---
495+
| t | f
496+
(1 row)
497+
498+
deallocate q;
467499
-- test multi-line headers, wrapping, and newline indicators
468500
-- in aligned, unaligned, and wrapped formats
469501
prepare q as select array_to_string(array_agg(repeat('x',2*n)),E'\n') as "ab

src/test/regress/sql/psql.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,22 @@ select 'drop table gexec_test', 'select ''2000-01-01''::date as party_over'
219219
-- show all pset options
220220
\pset
221221

222+
-- test the simple display substitution settings
223+
prepare q as select null as n, true as t, false as f;
224+
\pset null '(null)'
225+
\pset display_true 'true'
226+
\pset display_false 'false'
227+
execute q;
228+
\pset null
229+
\pset display_true
230+
\pset display_false
231+
execute q;
232+
\pset null ''
233+
\pset display_true 't'
234+
\pset display_false 'f'
235+
execute q;
236+
deallocate q;
237+
222238
-- test multi-line headers, wrapping, and newline indicators
223239
-- in aligned, unaligned, and wrapped formats
224240
prepare q as select array_to_string(array_agg(repeat('x',2*n)),E'\n') as "ab

0 commit comments

Comments
 (0)