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

Commit 6895866

Browse files
committed
psql: Improve \pset without arguments
Revert the output of the individual backslash commands that change print settings back to the 9.3 way (not showing the command name in parentheses). Implement \pset without arguments separately, showing all settings with values in a table form.
1 parent 7feaccc commit 6895866

File tree

2 files changed

+161
-64
lines changed

2 files changed

+161
-64
lines changed

src/bin/psql/command.c

Lines changed: 142 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ static void minimal_error_message(PGresult *res);
6969

7070
static void printSSLInfo(void);
7171
static bool printPsetInfo(const char *param, struct printQueryOpt *popt);
72+
static char *pset_value_string(const char *param, struct printQueryOpt *popt);
7273

7374
#ifdef WIN32
7475
static void checkWin32Codepage(void);
@@ -1050,9 +1051,9 @@ exec_command(const char *cmd,
10501051

10511052
int i;
10521053
static const char *const my_list[] = {
1053-
"border", "columns", "expanded", "fieldsep",
1054+
"border", "columns", "expanded", "fieldsep", "fieldsep_zero",
10541055
"footer", "format", "linestyle", "null",
1055-
"numericlocale", "pager", "recordsep",
1056+
"numericlocale", "pager", "recordsep", "recordsep_zero",
10561057
"tableattr", "title", "tuples_only",
10571058
"unicode_border_linestyle",
10581059
"unicode_column_linestyle",
@@ -1061,7 +1062,11 @@ exec_command(const char *cmd,
10611062
};
10621063

10631064
for (i = 0; my_list[i] != NULL; i++)
1064-
printPsetInfo(my_list[i], &pset.popt);
1065+
{
1066+
char *val = pset_value_string(my_list[i], &pset.popt);
1067+
printf("%-24s %s\n", my_list[i], val);
1068+
free(val);
1069+
}
10651070

10661071
success = true;
10671072
}
@@ -2214,10 +2219,6 @@ process_file(char *filename, bool single_txn, bool use_relative_path)
22142219

22152220

22162221

2217-
/*
2218-
* do_pset
2219-
*
2220-
*/
22212222
static const char *
22222223
_align2string(enum printFormat in)
22232224
{
@@ -2287,6 +2288,10 @@ _unicode_linestyle2string(int linestyle)
22872288
return "unknown";
22882289
}
22892290

2291+
/*
2292+
* do_pset
2293+
*
2294+
*/
22902295
bool
22912296
do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
22922297
{
@@ -2536,146 +2541,135 @@ printPsetInfo(const char *param, struct printQueryOpt *popt)
25362541

25372542
/* show border style/width */
25382543
if (strcmp(param, "border") == 0)
2539-
{
2540-
if (!popt->topt.border)
2541-
printf(_("Border style (%s) unset.\n"), param);
2542-
else
2543-
printf(_("Border style (%s) is %d.\n"), param,
2544-
popt->topt.border);
2545-
}
2544+
printf(_("Border style is %d.\n"), popt->topt.border);
25462545

25472546
/* show the target width for the wrapped format */
25482547
else if (strcmp(param, "columns") == 0)
25492548
{
25502549
if (!popt->topt.columns)
2551-
printf(_("Target width (%s) unset.\n"), param);
2550+
printf(_("Target width is unset.\n"));
25522551
else
2553-
printf(_("Target width (%s) is %d.\n"), param,
2554-
popt->topt.columns);
2552+
printf(_("Target width is %d.\n"), popt->topt.columns);
25552553
}
25562554

25572555
/* show expanded/vertical mode */
25582556
else if (strcmp(param, "x") == 0 || strcmp(param, "expanded") == 0 || strcmp(param, "vertical") == 0)
25592557
{
25602558
if (popt->topt.expanded == 1)
2561-
printf(_("Expanded display (%s) is on.\n"), param);
2559+
printf(_("Expanded display is on.\n"));
25622560
else if (popt->topt.expanded == 2)
2563-
printf(_("Expanded display (%s) is used automatically.\n"), param);
2561+
printf(_("Expanded display is used automatically.\n"));
25642562
else
2565-
printf(_("Expanded display (%s) is off.\n"), param);
2563+
printf(_("Expanded display is off.\n"));
25662564
}
25672565

25682566
/* show field separator for unaligned text */
25692567
else if (strcmp(param, "fieldsep") == 0)
25702568
{
25712569
if (popt->topt.fieldSep.separator_zero)
2572-
printf(_("Field separator (%s) is zero byte.\n"), param);
2570+
printf(_("Field separator is zero byte.\n"));
25732571
else
2574-
printf(_("Field separator (%s) is \"%s\".\n"), param,
2572+
printf(_("Field separator is \"%s\".\n"),
25752573
popt->topt.fieldSep.separator);
25762574
}
25772575

25782576
else if (strcmp(param, "fieldsep_zero") == 0)
25792577
{
2580-
printf(_("Field separator (%s) is zero byte.\n"), param);
2578+
printf(_("Field separator is zero byte.\n"));
25812579
}
25822580

25832581
/* show disable "(x rows)" footer */
25842582
else if (strcmp(param, "footer") == 0)
25852583
{
25862584
if (popt->topt.default_footer)
2587-
printf(_("Default footer (%s) is on.\n"), param);
2585+
printf(_("Default footer is on.\n"));
25882586
else
2589-
printf(_("Default footer (%s) is off.\n"), param);
2587+
printf(_("Default footer is off.\n"));
25902588
}
25912589

25922590
/* show format */
25932591
else if (strcmp(param, "format") == 0)
25942592
{
2595-
if (!popt->topt.format)
2596-
printf(_("Output format (%s) is aligned.\n"), param);
2597-
else
2598-
printf(_("Output format (%s) is %s.\n"), param,
2599-
_align2string(popt->topt.format));
2593+
printf(_("Output format is %s.\n"), _align2string(popt->topt.format));
26002594
}
26012595

26022596
/* show table line style */
26032597
else if (strcmp(param, "linestyle") == 0)
26042598
{
2605-
printf(_("Line style (%s) is %s.\n"), param,
2599+
printf(_("Line style is %s.\n"),
26062600
get_line_style(&popt->topt)->name);
26072601
}
26082602

26092603
/* show null display */
26102604
else if (strcmp(param, "null") == 0)
26112605
{
2612-
printf(_("Null display (%s) is \"%s\".\n"), param,
2606+
printf(_("Null display is \"%s\".\n"),
26132607
popt->nullPrint ? popt->nullPrint : "");
26142608
}
26152609

26162610
/* show locale-aware numeric output */
26172611
else if (strcmp(param, "numericlocale") == 0)
26182612
{
26192613
if (popt->topt.numericLocale)
2620-
printf(_("Locale-adjusted numeric output (%s) is on.\n"), param);
2614+
printf(_("Locale-adjusted numeric output is on.\n"));
26212615
else
2622-
printf(_("Locale-adjusted numeric output (%s) is off.\n"), param);
2616+
printf(_("Locale-adjusted numeric output is off.\n"));
26232617
}
26242618

26252619
/* show toggle use of pager */
26262620
else if (strcmp(param, "pager") == 0)
26272621
{
26282622
if (popt->topt.pager == 1)
2629-
printf(_("Pager (%s) is used for long output.\n"), param);
2623+
printf(_("Pager is used for long output.\n"));
26302624
else if (popt->topt.pager == 2)
2631-
printf(_("Pager (%s) is always used.\n"), param);
2625+
printf(_("Pager is always used.\n"));
26322626
else
2633-
printf(_("Pager usage (%s) is off.\n"), param);
2627+
printf(_("Pager usage is off.\n"));
26342628
}
26352629

26362630
/* show record separator for unaligned text */
26372631
else if (strcmp(param, "recordsep") == 0)
26382632
{
26392633
if (popt->topt.recordSep.separator_zero)
2640-
printf(_("Record separator (%s) is zero byte.\n"), param);
2634+
printf(_("Record separator is zero byte.\n"));
26412635
else if (strcmp(popt->topt.recordSep.separator, "\n") == 0)
2642-
printf(_("Record separator (%s) is <newline>.\n"), param);
2636+
printf(_("Record separator is <newline>.\n"));
26432637
else
2644-
printf(_("Record separator (%s) is \"%s\".\n"), param,
2638+
printf(_("Record separator is \"%s\".\n"),
26452639
popt->topt.recordSep.separator);
26462640
}
26472641

26482642
else if (strcmp(param, "recordsep_zero") == 0)
26492643
{
2650-
printf(_("Record separator (%s) is zero byte.\n"), param);
2644+
printf(_("Record separator is zero byte.\n"));
26512645
}
26522646

26532647
/* show HTML table tag options */
26542648
else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
26552649
{
26562650
if (popt->topt.tableAttr)
2657-
printf(_("Table attributes (%s) are \"%s\".\n"), param,
2651+
printf(_("Table attributes are \"%s\".\n"),
26582652
popt->topt.tableAttr);
26592653
else
2660-
printf(_("Table attributes (%s) unset.\n"), param);
2654+
printf(_("Table attributes unset.\n"));
26612655
}
26622656

26632657
/* show title override */
26642658
else if (strcmp(param, "title") == 0)
26652659
{
26662660
if (popt->title)
2667-
printf(_("Title (%s) is \"%s\".\n"), param, popt->title);
2661+
printf(_("Title is \"%s\".\n"), popt->title);
26682662
else
2669-
printf(_("Title (%s) unset.\n"), param);
2663+
printf(_("Title is unset.\n"));
26702664
}
26712665

26722666
/* show toggle between full and tuples-only format */
26732667
else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
26742668
{
26752669
if (popt->topt.tuples_only)
2676-
printf(_("Tuples only (%s) is on.\n"), param);
2670+
printf(_("Tuples only is on.\n"));
26772671
else
2678-
printf(_("Tuples only (%s) is off.\n"), param);
2672+
printf(_("Tuples only is off.\n"));
26792673
}
26802674

26812675
/* unicode style formatting */
@@ -2707,6 +2701,107 @@ printPsetInfo(const char *param, struct printQueryOpt *popt)
27072701
}
27082702

27092703

2704+
static const char *
2705+
pset_bool_string(bool val)
2706+
{
2707+
return val ? "on" : "off";
2708+
}
2709+
2710+
2711+
static char *
2712+
pset_quoted_string(const char *str)
2713+
{
2714+
char *ret = pg_malloc(strlen(str) * 2 + 2);
2715+
char *r = ret;
2716+
2717+
*r++ = '\'';
2718+
2719+
for (; *str; str++)
2720+
{
2721+
if (*str == '\n')
2722+
{
2723+
*r++ = '\\';
2724+
*r++ = 'n';
2725+
}
2726+
else if (*str == '\'')
2727+
{
2728+
*r++ = '\\';
2729+
*r++ = '\'';
2730+
}
2731+
else
2732+
*r++ = *str;
2733+
}
2734+
2735+
*r++ = '\'';
2736+
*r = '\0';
2737+
2738+
return ret;
2739+
}
2740+
2741+
2742+
/*
2743+
* Return a malloc'ed string for the \pset value.
2744+
*
2745+
* Note that for some string parameters, print.c distinguishes between unset
2746+
* and empty string, but for others it doesn't. This function should produce
2747+
* output that produces the correct setting when fed back into \pset.
2748+
*/
2749+
static char *
2750+
pset_value_string(const char *param, struct printQueryOpt *popt)
2751+
{
2752+
Assert(param != NULL);
2753+
2754+
if (strcmp(param, "border") == 0)
2755+
return psprintf("%d", popt->topt.border);
2756+
else if (strcmp(param, "columns") == 0)
2757+
return psprintf("%d", popt->topt.columns);
2758+
else if (strcmp(param, "expanded") == 0)
2759+
return pstrdup(popt->topt.expanded == 2
2760+
? "auto"
2761+
: pset_bool_string(popt->topt.expanded));
2762+
else if (strcmp(param, "fieldsep") == 0)
2763+
return pset_quoted_string(popt->topt.fieldSep.separator
2764+
? popt->topt.fieldSep.separator
2765+
: "");
2766+
else if (strcmp(param, "fieldsep_zero") == 0)
2767+
return pstrdup(pset_bool_string(popt->topt.fieldSep.separator_zero));
2768+
else if (strcmp(param, "footer") == 0)
2769+
return pstrdup(pset_bool_string(popt->topt.default_footer));
2770+
else if (strcmp(param, "format") == 0)
2771+
return psprintf("%s", _align2string(popt->topt.format));
2772+
else if (strcmp(param, "linestyle") == 0)
2773+
return psprintf("%s", get_line_style(&popt->topt)->name);
2774+
else if (strcmp(param, "null") == 0)
2775+
return pset_quoted_string(popt->nullPrint
2776+
? popt->nullPrint
2777+
: "");
2778+
else if (strcmp(param, "numericlocale") == 0)
2779+
return pstrdup(pset_bool_string(popt->topt.numericLocale));
2780+
else if (strcmp(param, "pager") == 0)
2781+
return psprintf("%d", popt->topt.pager);
2782+
else if (strcmp(param, "recordsep") == 0)
2783+
return pset_quoted_string(popt->topt.recordSep.separator
2784+
? popt->topt.recordSep.separator
2785+
: "");
2786+
else if (strcmp(param, "recordsep_zero") == 0)
2787+
return pstrdup(pset_bool_string(popt->topt.recordSep.separator_zero));
2788+
else if (strcmp(param, "tableattr") == 0)
2789+
return popt->topt.tableAttr ? pset_quoted_string(popt->topt.tableAttr) : pstrdup("");
2790+
else if (strcmp(param, "title") == 0)
2791+
return popt->title ? pset_quoted_string(popt->title) : pstrdup("");
2792+
else if (strcmp(param, "tuples_only") == 0)
2793+
return pstrdup(pset_bool_string(popt->topt.tuples_only));
2794+
else if (strcmp(param, "unicode_border_linestyle") == 0)
2795+
return pstrdup(_unicode_linestyle2string(popt->topt.unicode_border_linestyle));
2796+
else if (strcmp(param, "unicode_column_linestyle") == 0)
2797+
return pstrdup(_unicode_linestyle2string(popt->topt.unicode_column_linestyle));
2798+
else if (strcmp(param, "unicode_header_linestyle") == 0)
2799+
return pstrdup(_unicode_linestyle2string(popt->topt.unicode_header_linestyle));
2800+
else
2801+
return pstrdup("ERROR");
2802+
}
2803+
2804+
27102805

27112806
#ifndef WIN32
27122807
#define DEFAULT_SHELL "/bin/sh"

src/test/regress/expected/psql.out

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,25 @@ no rows returned for \gset
5454
\unset FETCH_COUNT
5555
-- show all pset options
5656
\pset
57-
Border style (border) is 1.
58-
Target width (columns) unset.
59-
Expanded display (expanded) is off.
60-
Field separator (fieldsep) is "|".
61-
Default footer (footer) is on.
62-
Output format (format) is aligned.
63-
Line style (linestyle) is ascii.
64-
Null display (null) is "".
65-
Locale-adjusted numeric output (numericlocale) is off.
66-
Pager (pager) is used for long output.
67-
Record separator (recordsep) is <newline>.
68-
Table attributes (tableattr) unset.
69-
Title (title) unset.
70-
Tuples only (tuples_only) is off.
71-
Unicode border linestyle is "single".
72-
Unicode column linestyle is "single".
73-
Unicode border linestyle is "single".
57+
border 1
58+
columns 0
59+
expanded off
60+
fieldsep '|'
61+
fieldsep_zero off
62+
footer on
63+
format aligned
64+
linestyle ascii
65+
null ''
66+
numericlocale off
67+
pager 1
68+
recordsep '\n'
69+
recordsep_zero off
70+
tableattr
71+
title
72+
tuples_only off
73+
unicode_border_linestyle single
74+
unicode_column_linestyle single
75+
unicode_header_linestyle single
7476
-- test multi-line headers, wrapping, and newline indicators
7577
prepare q as select array_to_string(array_agg(repeat('x',2*n)),E'\n') as "ab
7678

0 commit comments

Comments
 (0)