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

Commit 48b2d88

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 5c8758e commit 48b2d88

File tree

2 files changed

+152
-61
lines changed

2 files changed

+152
-61
lines changed

src/bin/psql/command.c

+136-47
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,15 +1051,19 @@ 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
NULL
10581059
};
10591060

10601061
for (i = 0; my_list[i] != NULL; i++)
1061-
printPsetInfo(my_list[i], &pset.popt);
1062+
{
1063+
char *val = pset_value_string(my_list[i], &pset.popt);
1064+
printf("%-14s %s\n", my_list[i], val);
1065+
free(val);
1066+
}
10621067

10631068
success = true;
10641069
}
@@ -2199,10 +2204,6 @@ process_file(char *filename, bool single_txn, bool use_relative_path)
21992204

22002205

22012206

2202-
/*
2203-
* do_pset
2204-
*
2205-
*/
22062207
static const char *
22072208
_align2string(enum printFormat in)
22082209
{
@@ -2237,6 +2238,10 @@ _align2string(enum printFormat in)
22372238
}
22382239

22392240

2241+
/*
2242+
* do_pset
2243+
*
2244+
*/
22402245
bool
22412246
do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
22422247
{
@@ -2447,146 +2452,135 @@ printPsetInfo(const char *param, struct printQueryOpt *popt)
24472452

24482453
/* show border style/width */
24492454
if (strcmp(param, "border") == 0)
2450-
{
2451-
if (!popt->topt.border)
2452-
printf(_("Border style (%s) unset.\n"), param);
2453-
else
2454-
printf(_("Border style (%s) is %d.\n"), param,
2455-
popt->topt.border);
2456-
}
2455+
printf(_("Border style is %d.\n"), popt->topt.border);
24572456

24582457
/* show the target width for the wrapped format */
24592458
else if (strcmp(param, "columns") == 0)
24602459
{
24612460
if (!popt->topt.columns)
2462-
printf(_("Target width (%s) unset.\n"), param);
2461+
printf(_("Target width is unset.\n"));
24632462
else
2464-
printf(_("Target width (%s) is %d.\n"), param,
2465-
popt->topt.columns);
2463+
printf(_("Target width is %d.\n"), popt->topt.columns);
24662464
}
24672465

24682466
/* show expanded/vertical mode */
24692467
else if (strcmp(param, "x") == 0 || strcmp(param, "expanded") == 0 || strcmp(param, "vertical") == 0)
24702468
{
24712469
if (popt->topt.expanded == 1)
2472-
printf(_("Expanded display (%s) is on.\n"), param);
2470+
printf(_("Expanded display is on.\n"));
24732471
else if (popt->topt.expanded == 2)
2474-
printf(_("Expanded display (%s) is used automatically.\n"), param);
2472+
printf(_("Expanded display is used automatically.\n"));
24752473
else
2476-
printf(_("Expanded display (%s) is off.\n"), param);
2474+
printf(_("Expanded display is off.\n"));
24772475
}
24782476

24792477
/* show field separator for unaligned text */
24802478
else if (strcmp(param, "fieldsep") == 0)
24812479
{
24822480
if (popt->topt.fieldSep.separator_zero)
2483-
printf(_("Field separator (%s) is zero byte.\n"), param);
2481+
printf(_("Field separator is zero byte.\n"));
24842482
else
2485-
printf(_("Field separator (%s) is \"%s\".\n"), param,
2483+
printf(_("Field separator is \"%s\".\n"),
24862484
popt->topt.fieldSep.separator);
24872485
}
24882486

24892487
else if (strcmp(param, "fieldsep_zero") == 0)
24902488
{
2491-
printf(_("Field separator (%s) is zero byte.\n"), param);
2489+
printf(_("Field separator is zero byte.\n"));
24922490
}
24932491

24942492
/* show disable "(x rows)" footer */
24952493
else if (strcmp(param, "footer") == 0)
24962494
{
24972495
if (popt->topt.default_footer)
2498-
printf(_("Default footer (%s) is on.\n"), param);
2496+
printf(_("Default footer is on.\n"));
24992497
else
2500-
printf(_("Default footer (%s) is off.\n"), param);
2498+
printf(_("Default footer is off.\n"));
25012499
}
25022500

25032501
/* show format */
25042502
else if (strcmp(param, "format") == 0)
25052503
{
2506-
if (!popt->topt.format)
2507-
printf(_("Output format (%s) is aligned.\n"), param);
2508-
else
2509-
printf(_("Output format (%s) is %s.\n"), param,
2510-
_align2string(popt->topt.format));
2504+
printf(_("Output format is %s.\n"), _align2string(popt->topt.format));
25112505
}
25122506

25132507
/* show table line style */
25142508
else if (strcmp(param, "linestyle") == 0)
25152509
{
2516-
printf(_("Line style (%s) is %s.\n"), param,
2510+
printf(_("Line style is %s.\n"),
25172511
get_line_style(&popt->topt)->name);
25182512
}
25192513

25202514
/* show null display */
25212515
else if (strcmp(param, "null") == 0)
25222516
{
2523-
printf(_("Null display (%s) is \"%s\".\n"), param,
2517+
printf(_("Null display is \"%s\".\n"),
25242518
popt->nullPrint ? popt->nullPrint : "");
25252519
}
25262520

25272521
/* show locale-aware numeric output */
25282522
else if (strcmp(param, "numericlocale") == 0)
25292523
{
25302524
if (popt->topt.numericLocale)
2531-
printf(_("Locale-adjusted numeric output (%s) is on.\n"), param);
2525+
printf(_("Locale-adjusted numeric output is on.\n"));
25322526
else
2533-
printf(_("Locale-adjusted numeric output (%s) is off.\n"), param);
2527+
printf(_("Locale-adjusted numeric output is off.\n"));
25342528
}
25352529

25362530
/* show toggle use of pager */
25372531
else if (strcmp(param, "pager") == 0)
25382532
{
25392533
if (popt->topt.pager == 1)
2540-
printf(_("Pager (%s) is used for long output.\n"), param);
2534+
printf(_("Pager is used for long output.\n"));
25412535
else if (popt->topt.pager == 2)
2542-
printf(_("Pager (%s) is always used.\n"), param);
2536+
printf(_("Pager is always used.\n"));
25432537
else
2544-
printf(_("Pager usage (%s) is off.\n"), param);
2538+
printf(_("Pager usage is off.\n"));
25452539
}
25462540

25472541
/* show record separator for unaligned text */
25482542
else if (strcmp(param, "recordsep") == 0)
25492543
{
25502544
if (popt->topt.recordSep.separator_zero)
2551-
printf(_("Record separator (%s) is zero byte.\n"), param);
2545+
printf(_("Record separator is zero byte.\n"));
25522546
else if (strcmp(popt->topt.recordSep.separator, "\n") == 0)
2553-
printf(_("Record separator (%s) is <newline>.\n"), param);
2547+
printf(_("Record separator is <newline>.\n"));
25542548
else
2555-
printf(_("Record separator (%s) is \"%s\".\n"), param,
2549+
printf(_("Record separator is \"%s\".\n"),
25562550
popt->topt.recordSep.separator);
25572551
}
25582552

25592553
else if (strcmp(param, "recordsep_zero") == 0)
25602554
{
2561-
printf(_("Record separator (%s) is zero byte.\n"), param);
2555+
printf(_("Record separator is zero byte.\n"));
25622556
}
25632557

25642558
/* show HTML table tag options */
25652559
else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
25662560
{
25672561
if (popt->topt.tableAttr)
2568-
printf(_("Table attributes (%s) are \"%s\".\n"), param,
2562+
printf(_("Table attributes are \"%s\".\n"),
25692563
popt->topt.tableAttr);
25702564
else
2571-
printf(_("Table attributes (%s) unset.\n"), param);
2565+
printf(_("Table attributes unset.\n"));
25722566
}
25732567

25742568
/* show title override */
25752569
else if (strcmp(param, "title") == 0)
25762570
{
25772571
if (popt->title)
2578-
printf(_("Title (%s) is \"%s\".\n"), param, popt->title);
2572+
printf(_("Title is \"%s\".\n"), popt->title);
25792573
else
2580-
printf(_("Title (%s) unset.\n"), param);
2574+
printf(_("Title is unset.\n"));
25812575
}
25822576

25832577
/* show toggle between full and tuples-only format */
25842578
else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
25852579
{
25862580
if (popt->topt.tuples_only)
2587-
printf(_("Tuples only (%s) is on.\n"), param);
2581+
printf(_("Tuples only is on.\n"));
25882582
else
2589-
printf(_("Tuples only (%s) is off.\n"), param);
2583+
printf(_("Tuples only is off.\n"));
25902584
}
25912585

25922586
else
@@ -2599,6 +2593,101 @@ printPsetInfo(const char *param, struct printQueryOpt *popt)
25992593
}
26002594

26012595

2596+
static const char *
2597+
pset_bool_string(bool val)
2598+
{
2599+
return val ? "on" : "off";
2600+
}
2601+
2602+
2603+
static char *
2604+
pset_quoted_string(const char *str)
2605+
{
2606+
char *ret = pg_malloc(strlen(str) * 2 + 2);
2607+
char *r = ret;
2608+
2609+
*r++ = '\'';
2610+
2611+
for (; *str; str++)
2612+
{
2613+
if (*str == '\n')
2614+
{
2615+
*r++ = '\\';
2616+
*r++ = 'n';
2617+
}
2618+
else if (*str == '\'')
2619+
{
2620+
*r++ = '\\';
2621+
*r++ = '\'';
2622+
}
2623+
else
2624+
*r++ = *str;
2625+
}
2626+
2627+
*r++ = '\'';
2628+
*r = '\0';
2629+
2630+
return ret;
2631+
}
2632+
2633+
2634+
/*
2635+
* Return a malloc'ed string for the \pset value.
2636+
*
2637+
* Note that for some string parameters, print.c distinguishes between unset
2638+
* and empty string, but for others it doesn't. This function should produce
2639+
* output that produces the correct setting when fed back into \pset.
2640+
*/
2641+
static char *
2642+
pset_value_string(const char *param, struct printQueryOpt *popt)
2643+
{
2644+
Assert(param != NULL);
2645+
2646+
if (strcmp(param, "border") == 0)
2647+
return psprintf("%d", popt->topt.border);
2648+
else if (strcmp(param, "columns") == 0)
2649+
return psprintf("%d", popt->topt.columns);
2650+
else if (strcmp(param, "expanded") == 0)
2651+
return pstrdup(popt->topt.expanded == 2
2652+
? "auto"
2653+
: pset_bool_string(popt->topt.expanded));
2654+
else if (strcmp(param, "fieldsep") == 0)
2655+
return pset_quoted_string(popt->topt.fieldSep.separator
2656+
? popt->topt.fieldSep.separator
2657+
: "");
2658+
else if (strcmp(param, "fieldsep_zero") == 0)
2659+
return pstrdup(pset_bool_string(popt->topt.fieldSep.separator_zero));
2660+
else if (strcmp(param, "footer") == 0)
2661+
return pstrdup(pset_bool_string(popt->topt.default_footer));
2662+
else if (strcmp(param, "format") == 0)
2663+
return psprintf("%s", _align2string(popt->topt.format));
2664+
else if (strcmp(param, "linestyle") == 0)
2665+
return psprintf("%s", get_line_style(&popt->topt)->name);
2666+
else if (strcmp(param, "null") == 0)
2667+
return pset_quoted_string(popt->nullPrint
2668+
? popt->nullPrint
2669+
: "");
2670+
else if (strcmp(param, "numericlocale") == 0)
2671+
return pstrdup(pset_bool_string(popt->topt.numericLocale));
2672+
else if (strcmp(param, "pager") == 0)
2673+
return psprintf("%d", popt->topt.pager);
2674+
else if (strcmp(param, "recordsep") == 0)
2675+
return pset_quoted_string(popt->topt.recordSep.separator
2676+
? popt->topt.recordSep.separator
2677+
: "");
2678+
else if (strcmp(param, "recordsep_zero") == 0)
2679+
return pstrdup(pset_bool_string(popt->topt.recordSep.separator_zero));
2680+
else if (strcmp(param, "tableattr") == 0)
2681+
return popt->topt.tableAttr ? pset_quoted_string(popt->topt.tableAttr) : pstrdup("");
2682+
else if (strcmp(param, "title") == 0)
2683+
return popt->title ? pset_quoted_string(popt->title) : pstrdup("");
2684+
else if (strcmp(param, "tuples_only") == 0)
2685+
return pstrdup(pset_bool_string(popt->topt.tuples_only));
2686+
else
2687+
return pstrdup("ERROR");
2688+
}
2689+
2690+
26022691

26032692
#ifndef WIN32
26042693
#define DEFAULT_SHELL "/bin/sh"

src/test/regress/expected/psql.out

+16-14
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,19 @@ 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.
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

0 commit comments

Comments
 (0)