Skip to content

Commit 5ad5353

Browse files
author
Commitfest Bot
committed
[CF 5828] v2 - Add \pset options for boolean value display
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5828 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/[email protected] Author(s): David Johnston
2 parents 8f29467 + 649c0ba commit 5ad5353

File tree

6 files changed

+120
-1
lines changed

6 files changed

+120
-1
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3099,6 +3099,30 @@ 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>, as that is the value
3108+
transmitted by the server. For readability,
3109+
<literal>\pset display_false 'false'</literal> is recommended.
3110+
</para>
3111+
</listitem>
3112+
</varlistentry>
3113+
3114+
<varlistentry id="app-psql-meta-command-pset-display-true">
3115+
<term><literal>display_true</literal></term>
3116+
<listitem>
3117+
<para>
3118+
Sets the string to be printed in place of a true value.
3119+
The default is to print <literal>t</literal>, as that is the value
3120+
transmitted by the server. For readability,
3121+
<literal>\pset display_true 'true'</literal> is recommended.
3122+
</para>
3123+
</listitem>
3124+
</varlistentry>
3125+
31023126
<varlistentry id="app-psql-meta-command-pset-expanded">
31033127
<term><literal>expanded</literal> (or <literal>x</literal>)</term>
31043128
<listitem>

src/bin/psql/command.c

Lines changed: 42 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,12 @@ 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 ?
5783+
popt->falsePrint : "f");
5784+
else if (strcmp(param, "display_true") == 0)
5785+
return pset_quoted_string(popt->truePrint ?
5786+
popt->truePrint : "t");
57465787
else if (strcmp(param, "expanded") == 0)
57475788
return pstrdup(popt->topt.expanded == 2
57485789
? "auto"

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)