Commit 68958665 authored by Peter Eisentraut's avatar Peter Eisentraut

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.
parent 7feaccc2
...@@ -69,6 +69,7 @@ static void minimal_error_message(PGresult *res); ...@@ -69,6 +69,7 @@ static void minimal_error_message(PGresult *res);
static void printSSLInfo(void); static void printSSLInfo(void);
static bool printPsetInfo(const char *param, struct printQueryOpt *popt); static bool printPsetInfo(const char *param, struct printQueryOpt *popt);
static char *pset_value_string(const char *param, struct printQueryOpt *popt);
#ifdef WIN32 #ifdef WIN32
static void checkWin32Codepage(void); static void checkWin32Codepage(void);
...@@ -1050,9 +1051,9 @@ exec_command(const char *cmd, ...@@ -1050,9 +1051,9 @@ exec_command(const char *cmd,
int i; int i;
static const char *const my_list[] = { static const char *const my_list[] = {
"border", "columns", "expanded", "fieldsep", "border", "columns", "expanded", "fieldsep", "fieldsep_zero",
"footer", "format", "linestyle", "null", "footer", "format", "linestyle", "null",
"numericlocale", "pager", "recordsep", "numericlocale", "pager", "recordsep", "recordsep_zero",
"tableattr", "title", "tuples_only", "tableattr", "title", "tuples_only",
"unicode_border_linestyle", "unicode_border_linestyle",
"unicode_column_linestyle", "unicode_column_linestyle",
...@@ -1061,7 +1062,11 @@ exec_command(const char *cmd, ...@@ -1061,7 +1062,11 @@ exec_command(const char *cmd,
}; };
for (i = 0; my_list[i] != NULL; i++) for (i = 0; my_list[i] != NULL; i++)
printPsetInfo(my_list[i], &pset.popt); {
char *val = pset_value_string(my_list[i], &pset.popt);
printf("%-24s %s\n", my_list[i], val);
free(val);
}
success = true; success = true;
} }
...@@ -2214,10 +2219,6 @@ error: ...@@ -2214,10 +2219,6 @@ error:
/*
* do_pset
*
*/
static const char * static const char *
_align2string(enum printFormat in) _align2string(enum printFormat in)
{ {
...@@ -2287,6 +2288,10 @@ _unicode_linestyle2string(int linestyle) ...@@ -2287,6 +2288,10 @@ _unicode_linestyle2string(int linestyle)
return "unknown"; return "unknown";
} }
/*
* do_pset
*
*/
bool bool
do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
{ {
...@@ -2536,80 +2541,69 @@ printPsetInfo(const char *param, struct printQueryOpt *popt) ...@@ -2536,80 +2541,69 @@ printPsetInfo(const char *param, struct printQueryOpt *popt)
/* show border style/width */ /* show border style/width */
if (strcmp(param, "border") == 0) if (strcmp(param, "border") == 0)
{ printf(_("Border style is %d.\n"), popt->topt.border);
if (!popt->topt.border)
printf(_("Border style (%s) unset.\n"), param);
else
printf(_("Border style (%s) is %d.\n"), param,
popt->topt.border);
}
/* show the target width for the wrapped format */ /* show the target width for the wrapped format */
else if (strcmp(param, "columns") == 0) else if (strcmp(param, "columns") == 0)
{ {
if (!popt->topt.columns) if (!popt->topt.columns)
printf(_("Target width (%s) unset.\n"), param); printf(_("Target width is unset.\n"));
else else
printf(_("Target width (%s) is %d.\n"), param, printf(_("Target width is %d.\n"), popt->topt.columns);
popt->topt.columns);
} }
/* show expanded/vertical mode */ /* show expanded/vertical mode */
else if (strcmp(param, "x") == 0 || strcmp(param, "expanded") == 0 || strcmp(param, "vertical") == 0) else if (strcmp(param, "x") == 0 || strcmp(param, "expanded") == 0 || strcmp(param, "vertical") == 0)
{ {
if (popt->topt.expanded == 1) if (popt->topt.expanded == 1)
printf(_("Expanded display (%s) is on.\n"), param); printf(_("Expanded display is on.\n"));
else if (popt->topt.expanded == 2) else if (popt->topt.expanded == 2)
printf(_("Expanded display (%s) is used automatically.\n"), param); printf(_("Expanded display is used automatically.\n"));
else else
printf(_("Expanded display (%s) is off.\n"), param); printf(_("Expanded display is off.\n"));
} }
/* show field separator for unaligned text */ /* show field separator for unaligned text */
else if (strcmp(param, "fieldsep") == 0) else if (strcmp(param, "fieldsep") == 0)
{ {
if (popt->topt.fieldSep.separator_zero) if (popt->topt.fieldSep.separator_zero)
printf(_("Field separator (%s) is zero byte.\n"), param); printf(_("Field separator is zero byte.\n"));
else else
printf(_("Field separator (%s) is \"%s\".\n"), param, printf(_("Field separator is \"%s\".\n"),
popt->topt.fieldSep.separator); popt->topt.fieldSep.separator);
} }
else if (strcmp(param, "fieldsep_zero") == 0) else if (strcmp(param, "fieldsep_zero") == 0)
{ {
printf(_("Field separator (%s) is zero byte.\n"), param); printf(_("Field separator is zero byte.\n"));
} }
/* show disable "(x rows)" footer */ /* show disable "(x rows)" footer */
else if (strcmp(param, "footer") == 0) else if (strcmp(param, "footer") == 0)
{ {
if (popt->topt.default_footer) if (popt->topt.default_footer)
printf(_("Default footer (%s) is on.\n"), param); printf(_("Default footer is on.\n"));
else else
printf(_("Default footer (%s) is off.\n"), param); printf(_("Default footer is off.\n"));
} }
/* show format */ /* show format */
else if (strcmp(param, "format") == 0) else if (strcmp(param, "format") == 0)
{ {
if (!popt->topt.format) printf(_("Output format is %s.\n"), _align2string(popt->topt.format));
printf(_("Output format (%s) is aligned.\n"), param);
else
printf(_("Output format (%s) is %s.\n"), param,
_align2string(popt->topt.format));
} }
/* show table line style */ /* show table line style */
else if (strcmp(param, "linestyle") == 0) else if (strcmp(param, "linestyle") == 0)
{ {
printf(_("Line style (%s) is %s.\n"), param, printf(_("Line style is %s.\n"),
get_line_style(&popt->topt)->name); get_line_style(&popt->topt)->name);
} }
/* show null display */ /* show null display */
else if (strcmp(param, "null") == 0) else if (strcmp(param, "null") == 0)
{ {
printf(_("Null display (%s) is \"%s\".\n"), param, printf(_("Null display is \"%s\".\n"),
popt->nullPrint ? popt->nullPrint : ""); popt->nullPrint ? popt->nullPrint : "");
} }
...@@ -2617,65 +2611,65 @@ printPsetInfo(const char *param, struct printQueryOpt *popt) ...@@ -2617,65 +2611,65 @@ printPsetInfo(const char *param, struct printQueryOpt *popt)
else if (strcmp(param, "numericlocale") == 0) else if (strcmp(param, "numericlocale") == 0)
{ {
if (popt->topt.numericLocale) if (popt->topt.numericLocale)
printf(_("Locale-adjusted numeric output (%s) is on.\n"), param); printf(_("Locale-adjusted numeric output is on.\n"));
else else
printf(_("Locale-adjusted numeric output (%s) is off.\n"), param); printf(_("Locale-adjusted numeric output is off.\n"));
} }
/* show toggle use of pager */ /* show toggle use of pager */
else if (strcmp(param, "pager") == 0) else if (strcmp(param, "pager") == 0)
{ {
if (popt->topt.pager == 1) if (popt->topt.pager == 1)
printf(_("Pager (%s) is used for long output.\n"), param); printf(_("Pager is used for long output.\n"));
else if (popt->topt.pager == 2) else if (popt->topt.pager == 2)
printf(_("Pager (%s) is always used.\n"), param); printf(_("Pager is always used.\n"));
else else
printf(_("Pager usage (%s) is off.\n"), param); printf(_("Pager usage is off.\n"));
} }
/* show record separator for unaligned text */ /* show record separator for unaligned text */
else if (strcmp(param, "recordsep") == 0) else if (strcmp(param, "recordsep") == 0)
{ {
if (popt->topt.recordSep.separator_zero) if (popt->topt.recordSep.separator_zero)
printf(_("Record separator (%s) is zero byte.\n"), param); printf(_("Record separator is zero byte.\n"));
else if (strcmp(popt->topt.recordSep.separator, "\n") == 0) else if (strcmp(popt->topt.recordSep.separator, "\n") == 0)
printf(_("Record separator (%s) is <newline>.\n"), param); printf(_("Record separator is <newline>.\n"));
else else
printf(_("Record separator (%s) is \"%s\".\n"), param, printf(_("Record separator is \"%s\".\n"),
popt->topt.recordSep.separator); popt->topt.recordSep.separator);
} }
else if (strcmp(param, "recordsep_zero") == 0) else if (strcmp(param, "recordsep_zero") == 0)
{ {
printf(_("Record separator (%s) is zero byte.\n"), param); printf(_("Record separator is zero byte.\n"));
} }
/* show HTML table tag options */ /* show HTML table tag options */
else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0) else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
{ {
if (popt->topt.tableAttr) if (popt->topt.tableAttr)
printf(_("Table attributes (%s) are \"%s\".\n"), param, printf(_("Table attributes are \"%s\".\n"),
popt->topt.tableAttr); popt->topt.tableAttr);
else else
printf(_("Table attributes (%s) unset.\n"), param); printf(_("Table attributes unset.\n"));
} }
/* show title override */ /* show title override */
else if (strcmp(param, "title") == 0) else if (strcmp(param, "title") == 0)
{ {
if (popt->title) if (popt->title)
printf(_("Title (%s) is \"%s\".\n"), param, popt->title); printf(_("Title is \"%s\".\n"), popt->title);
else else
printf(_("Title (%s) unset.\n"), param); printf(_("Title is unset.\n"));
} }
/* show toggle between full and tuples-only format */ /* show toggle between full and tuples-only format */
else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0) else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
{ {
if (popt->topt.tuples_only) if (popt->topt.tuples_only)
printf(_("Tuples only (%s) is on.\n"), param); printf(_("Tuples only is on.\n"));
else else
printf(_("Tuples only (%s) is off.\n"), param); printf(_("Tuples only is off.\n"));
} }
/* unicode style formatting */ /* unicode style formatting */
...@@ -2707,6 +2701,107 @@ printPsetInfo(const char *param, struct printQueryOpt *popt) ...@@ -2707,6 +2701,107 @@ printPsetInfo(const char *param, struct printQueryOpt *popt)
} }
static const char *
pset_bool_string(bool val)
{
return val ? "on" : "off";
}
static char *
pset_quoted_string(const char *str)
{
char *ret = pg_malloc(strlen(str) * 2 + 2);
char *r = ret;
*r++ = '\'';
for (; *str; str++)
{
if (*str == '\n')
{
*r++ = '\\';
*r++ = 'n';
}
else if (*str == '\'')
{
*r++ = '\\';
*r++ = '\'';
}
else
*r++ = *str;
}
*r++ = '\'';
*r = '\0';
return ret;
}
/*
* Return a malloc'ed string for the \pset value.
*
* Note that for some string parameters, print.c distinguishes between unset
* and empty string, but for others it doesn't. This function should produce
* output that produces the correct setting when fed back into \pset.
*/
static char *
pset_value_string(const char *param, struct printQueryOpt *popt)
{
Assert(param != NULL);
if (strcmp(param, "border") == 0)
return psprintf("%d", popt->topt.border);
else if (strcmp(param, "columns") == 0)
return psprintf("%d", popt->topt.columns);
else if (strcmp(param, "expanded") == 0)
return pstrdup(popt->topt.expanded == 2
? "auto"
: pset_bool_string(popt->topt.expanded));
else if (strcmp(param, "fieldsep") == 0)
return pset_quoted_string(popt->topt.fieldSep.separator
? popt->topt.fieldSep.separator
: "");
else if (strcmp(param, "fieldsep_zero") == 0)
return pstrdup(pset_bool_string(popt->topt.fieldSep.separator_zero));
else if (strcmp(param, "footer") == 0)
return pstrdup(pset_bool_string(popt->topt.default_footer));
else if (strcmp(param, "format") == 0)
return psprintf("%s", _align2string(popt->topt.format));
else if (strcmp(param, "linestyle") == 0)
return psprintf("%s", get_line_style(&popt->topt)->name);
else if (strcmp(param, "null") == 0)
return pset_quoted_string(popt->nullPrint
? popt->nullPrint
: "");
else if (strcmp(param, "numericlocale") == 0)
return pstrdup(pset_bool_string(popt->topt.numericLocale));
else if (strcmp(param, "pager") == 0)
return psprintf("%d", popt->topt.pager);
else if (strcmp(param, "recordsep") == 0)
return pset_quoted_string(popt->topt.recordSep.separator
? popt->topt.recordSep.separator
: "");
else if (strcmp(param, "recordsep_zero") == 0)
return pstrdup(pset_bool_string(popt->topt.recordSep.separator_zero));
else if (strcmp(param, "tableattr") == 0)
return popt->topt.tableAttr ? pset_quoted_string(popt->topt.tableAttr) : pstrdup("");
else if (strcmp(param, "title") == 0)
return popt->title ? pset_quoted_string(popt->title) : pstrdup("");
else if (strcmp(param, "tuples_only") == 0)
return pstrdup(pset_bool_string(popt->topt.tuples_only));
else if (strcmp(param, "unicode_border_linestyle") == 0)
return pstrdup(_unicode_linestyle2string(popt->topt.unicode_border_linestyle));
else if (strcmp(param, "unicode_column_linestyle") == 0)
return pstrdup(_unicode_linestyle2string(popt->topt.unicode_column_linestyle));
else if (strcmp(param, "unicode_header_linestyle") == 0)
return pstrdup(_unicode_linestyle2string(popt->topt.unicode_header_linestyle));
else
return pstrdup("ERROR");
}
#ifndef WIN32 #ifndef WIN32
#define DEFAULT_SHELL "/bin/sh" #define DEFAULT_SHELL "/bin/sh"
......
...@@ -54,23 +54,25 @@ no rows returned for \gset ...@@ -54,23 +54,25 @@ no rows returned for \gset
\unset FETCH_COUNT \unset FETCH_COUNT
-- show all pset options -- show all pset options
\pset \pset
Border style (border) is 1. border 1
Target width (columns) unset. columns 0
Expanded display (expanded) is off. expanded off
Field separator (fieldsep) is "|". fieldsep '|'
Default footer (footer) is on. fieldsep_zero off
Output format (format) is aligned. footer on
Line style (linestyle) is ascii. format aligned
Null display (null) is "". linestyle ascii
Locale-adjusted numeric output (numericlocale) is off. null ''
Pager (pager) is used for long output. numericlocale off
Record separator (recordsep) is <newline>. pager 1
Table attributes (tableattr) unset. recordsep '\n'
Title (title) unset. recordsep_zero off
Tuples only (tuples_only) is off. tableattr
Unicode border linestyle is "single". title
Unicode column linestyle is "single". tuples_only off
Unicode border linestyle is "single". unicode_border_linestyle single
unicode_column_linestyle single
unicode_header_linestyle single
-- test multi-line headers, wrapping, and newline indicators -- test multi-line headers, wrapping, and newline indicators
prepare q as select array_to_string(array_agg(repeat('x',2*n)),E'\n') as "ab prepare q as select array_to_string(array_agg(repeat('x',2*n)),E'\n') as "ab
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment