psql-bool-display.patch
application/octet-stream
Filename: psql-bool-display.patch
Type: application/octet-stream
Part: 0
Message:
PATCH: psql boolean display
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: unified
| File | + | − |
|---|---|---|
| doc/src/sgml/ref/psql-ref.sgml | 0 | 0 |
| src/bin/psql/command.c | 0 | 0 |
| src/bin/psql/print.c | 0 | 0 |
| src/bin/psql/print.h | 0 | 0 |
| src/test/regress/expected/boolean.out | 0 | 0 |
| src/test/regress/sql/boolean.sql | 0 | 0 |
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
new file mode 100644
index a47af51..e02e4da
*** a/doc/src/sgml/ref/psql-ref.sgml
--- b/doc/src/sgml/ref/psql-ref.sgml
*************** lo_import 152801
*** 1886,1891 ****
--- 1886,1915 ----
Adjustable printing options are:
<variablelist>
<varlistentry>
+ <term><literal>boolfalse</literal></term>
+ <listitem>
+ <para>
+ Sets the string to be printed in place of a false boolean value.
+ The default is to print <literal>f</literal>, which might not be
+ intuitive. For example, one might prefer <literal>\pset boolfalse
+ false</literal>.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><literal>booltrue</literal></term>
+ <listitem>
+ <para>
+ Sets the string to be printed in place of a true boolean value.
+ The default is to print <literal>t</literal>, which might not be
+ intuitive. For example, one might prefer <literal>\pset booltrue
+ true</literal>.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><literal>border</literal></term>
<listitem>
<para>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
new file mode 100644
index 205bb50..672e91a
*** a/src/bin/psql/command.c
--- b/src/bin/psql/command.c
*************** do_pset(const char *param, const char *v
*** 2288,2293 ****
--- 2288,2317 ----
printf(_("Null display is \"%s\".\n"), popt->nullPrint ? popt->nullPrint : "");
}
+ /* booltrue display */
+ else if (strcmp(param, "booltrue") == 0)
+ {
+ if (value)
+ {
+ free(popt->bool_true);
+ popt->bool_true = pg_strdup(value);
+ }
+ if (!quiet)
+ printf(_("Boolean true display is \"%s\".\n"), popt->bool_true ? popt->bool_true : "t");
+ }
+
+ /* boolfalse display */
+ else if (strcmp(param, "boolfalse") == 0)
+ {
+ if (value)
+ {
+ free(popt->bool_false);
+ popt->bool_false = pg_strdup(value);
+ }
+ if (!quiet)
+ printf(_("Boolean false display is \"%s\".\n"), popt->bool_false ? popt->bool_false : "f");
+ }
+
/* field separator for unaligned text */
else if (strcmp(param, "fieldsep") == 0)
{
diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
new file mode 100644
index 8fa5e37..4eb0f05
*** a/src/bin/psql/print.c
--- b/src/bin/psql/print.c
*************** printQuery(const PGresult *result, const
*** 2497,2502 ****
--- 2497,2503 ----
char *cell;
bool mustfree = false;
bool translate;
+ Oid ftype = PQftype(result, c);
if (PQgetisnull(result, r, c))
cell = opt->nullPrint ? opt->nullPrint : "";
*************** printQuery(const PGresult *result, const
*** 2508,2513 ****
--- 2509,2521 ----
cell = format_numeric_locale(cell);
mustfree = true;
}
+ else if (ftype == BOOLOID)
+ {
+ if (opt->bool_true && (strcmp(cell,"t") == 0))
+ cell = opt->bool_true;
+ else if (opt->bool_false && (strcmp(cell,"f") == 0))
+ cell = opt->bool_false;
+ }
}
translate = (opt->translate_columns && opt->translate_columns[c]);
diff --git a/src/bin/psql/print.h b/src/bin/psql/print.h
new file mode 100644
index 2b2ad0b..08f01ee
*** a/src/bin/psql/print.h
--- b/src/bin/psql/print.h
*************** typedef struct printQueryOpt
*** 139,144 ****
--- 139,145 ----
{
printTableOpt topt; /* the options above */
char *nullPrint; /* how to print null entities */
+ char *bool_true, *bool_false; /* how to print boolean values */
bool quote; /* quote all values as much as possible */
char *title; /* override title */
char **footers; /* override footer (default is "(xx rows)") */
diff --git a/src/test/regress/expected/boolean.out b/src/test/regress/expected/boolean.out
new file mode 100644
index e39f550..e787fd5
*** a/src/test/regress/expected/boolean.out
--- b/src/test/regress/expected/boolean.out
*************** SELECT bool '' AS error;
*** 142,147 ****
--- 142,170 ----
ERROR: invalid input syntax for type boolean: ""
LINE 1: SELECT bool '' AS error;
^
+ -- check bool output
+ SELECT true, false;
+ bool | bool
+ ------+------
+ t | f
+ (1 row)
+
+ \pset booltrue 'foo'
+ \pset boolfalse 'bar'
+ SELECT true, false;
+ bool | bool
+ ------+------
+ foo | bar
+ (1 row)
+
+ \pset booltrue 't'
+ \pset boolfalse 'f'
+ SELECT true, false;
+ bool | bool
+ ------+------
+ t | f
+ (1 row)
+
-- and, or, not in qualifications
SELECT bool 't' or bool 'f' AS true;
true
diff --git a/src/test/regress/sql/boolean.sql b/src/test/regress/sql/boolean.sql
new file mode 100644
index d92a41f..3d242b3
*** a/src/test/regress/sql/boolean.sql
--- b/src/test/regress/sql/boolean.sql
*************** SELECT bool '000' AS error;
*** 62,67 ****
--- 62,77 ----
SELECT bool '' AS error;
+ -- check bool output
+
+ SELECT true, false;
+ \pset booltrue 'foo'
+ \pset boolfalse 'bar'
+ SELECT true, false;
+ \pset booltrue 't'
+ \pset boolfalse 'f'
+ SELECT true, false;
+
-- and, or, not in qualifications
SELECT bool 't' or bool 'f' AS true;