Commit 5b214c5d authored by Fujii Masao's avatar Fujii Masao

Add new ECHO mode 'errors' that displays only failed commands in psql.

When the psql variable ECHO is set to 'erros', only failed SQL commands
are printed to standard error output. Also this patch adds -b option into psql.
This is equivalent to setting the variable ECHO to 'errors'.

Pavel Stehule, reviewed by Fabrízio de Royes Mello, Samrat Revagade,
Kumar Rajeev Rastogi, Abhijit Menon-Sen, and me.
parent b043985b
...@@ -72,6 +72,18 @@ PostgreSQL documentation ...@@ -72,6 +72,18 @@ PostgreSQL documentation
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>-b</></term>
<term><option>--echo-errors</></term>
<listitem>
<para>
Print failed SQL commands to standard error output. This is
equivalent to setting the variable <varname>ECHO</varname> to
<literal>errors</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><option>-c <replaceable class="parameter">command</replaceable></></term> <term><option>-c <replaceable class="parameter">command</replaceable></></term>
<term><option>--command=<replaceable class="parameter">command</replaceable></></term> <term><option>--command=<replaceable class="parameter">command</replaceable></></term>
...@@ -2812,7 +2824,9 @@ bar ...@@ -2812,7 +2824,9 @@ bar
<literal>queries</literal>, <literal>queries</literal>,
<application>psql</application> merely prints all queries as <application>psql</application> merely prints all queries as
they are sent to the server. The switch for this is they are sent to the server. The switch for this is
<option>-e</option>. <option>-e</option>. If set to <literal>errors</literal> then only
failed queries are displayed on standard error output. The switch
for this is <option>-b</option>.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
......
...@@ -995,6 +995,9 @@ SendQuery(const char *query) ...@@ -995,6 +995,9 @@ SendQuery(const char *query)
results = NULL; /* PQclear(NULL) does nothing */ results = NULL; /* PQclear(NULL) does nothing */
} }
if (!OK && pset.echo == PSQL_ECHO_ERRORS)
psql_error("STATEMENT: %s\n", query);
/* If we made a temporary savepoint, possibly release/rollback */ /* If we made a temporary savepoint, possibly release/rollback */
if (on_error_rollback_savepoint) if (on_error_rollback_savepoint)
{ {
......
...@@ -87,6 +87,7 @@ usage(void) ...@@ -87,6 +87,7 @@ usage(void)
printf(_("\nInput and output options:\n")); printf(_("\nInput and output options:\n"));
printf(_(" -a, --echo-all echo all input from script\n")); printf(_(" -a, --echo-all echo all input from script\n"));
printf(_(" -b, --echo-errors echo failed commands\n"));
printf(_(" -e, --echo-queries echo commands sent to server\n")); printf(_(" -e, --echo-queries echo commands sent to server\n"));
printf(_(" -E, --echo-hidden display queries that internal commands generate\n")); printf(_(" -E, --echo-hidden display queries that internal commands generate\n"));
printf(_(" -L, --log-file=FILENAME send session log to file\n")); printf(_(" -L, --log-file=FILENAME send session log to file\n"));
......
...@@ -31,6 +31,7 @@ typedef enum ...@@ -31,6 +31,7 @@ typedef enum
{ {
PSQL_ECHO_NONE, PSQL_ECHO_NONE,
PSQL_ECHO_QUERIES, PSQL_ECHO_QUERIES,
PSQL_ECHO_ERRORS,
PSQL_ECHO_ALL PSQL_ECHO_ALL
} PSQL_ECHO; } PSQL_ECHO;
......
...@@ -354,6 +354,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options) ...@@ -354,6 +354,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
{"command", required_argument, NULL, 'c'}, {"command", required_argument, NULL, 'c'},
{"dbname", required_argument, NULL, 'd'}, {"dbname", required_argument, NULL, 'd'},
{"echo-queries", no_argument, NULL, 'e'}, {"echo-queries", no_argument, NULL, 'e'},
{"echo-errors", no_argument, NULL, 'b'},
{"echo-hidden", no_argument, NULL, 'E'}, {"echo-hidden", no_argument, NULL, 'E'},
{"file", required_argument, NULL, 'f'}, {"file", required_argument, NULL, 'f'},
{"field-separator", required_argument, NULL, 'F'}, {"field-separator", required_argument, NULL, 'F'},
...@@ -391,7 +392,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options) ...@@ -391,7 +392,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
memset(options, 0, sizeof *options); memset(options, 0, sizeof *options);
while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01", while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
long_options, &optindex)) != -1) long_options, &optindex)) != -1)
{ {
switch (c) switch (c)
...@@ -402,6 +403,9 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options) ...@@ -402,6 +403,9 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
case 'A': case 'A':
pset.popt.topt.format = PRINT_UNALIGNED; pset.popt.topt.format = PRINT_UNALIGNED;
break; break;
case 'b':
SetVariable(pset.vars, "ECHO", "errors");
break;
case 'c': case 'c':
options->action_string = pg_strdup(optarg); options->action_string = pg_strdup(optarg);
if (optarg[0] == '\\') if (optarg[0] == '\\')
...@@ -720,6 +724,8 @@ echo_hook(const char *newval) ...@@ -720,6 +724,8 @@ echo_hook(const char *newval)
pset.echo = PSQL_ECHO_NONE; pset.echo = PSQL_ECHO_NONE;
else if (strcmp(newval, "queries") == 0) else if (strcmp(newval, "queries") == 0)
pset.echo = PSQL_ECHO_QUERIES; pset.echo = PSQL_ECHO_QUERIES;
else if (strcmp(newval, "errors") == 0)
pset.echo = PSQL_ECHO_ERRORS;
else if (strcmp(newval, "all") == 0) else if (strcmp(newval, "all") == 0)
pset.echo = PSQL_ECHO_ALL; pset.echo = PSQL_ECHO_ALL;
else else
......
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