After some discussions about what could be useful since psql scripts now
accepts tests, this patch sets a few variables which can be used by psql
after a "front door" (i.e. actually typed by the user) query:
- RESULT_STATUS: the status of the query
- ERROR: whether the query failed
- ERROR_MESSAGE: ...
- ROW_COUNT: #rows affected
SELECT * FROM ;
\if :ERROR
\echo oops
\q
\endif
I'm not sure that the names are right. Maybe STATUS would be better than
RESULT_STATUS.
--
Fabien.
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 3b86612..7006f23 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -3449,6 +3449,24 @@ bar
</varlistentry>
<varlistentry>
+ <term><varname>ERROR</varname></term>
+ <listitem>
+ <para>
+ Whether the last query failed.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><varname>ERROR_MESSAGE</varname></term>
+ <listitem>
+ <para>
+ If the last query failed, the associated error message.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><varname>FETCH_COUNT</varname></term>
<listitem>
<para>
@@ -3653,6 +3671,25 @@ bar
</varlistentry>
<varlistentry>
+ <term><varname>RESULT_STATUS</varname></term>
+ <listitem>
+ <para>
+ Text status of the last query.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><varname>ROW_COUNT</varname></term>
+ <listitem>
+ <para>
+ When appropriate, how many rows were returned or affected by the
+ last query.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><varname>QUIET</varname></term>
<listitem>
<para>
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index a2f1259..74d22fb 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1213,6 +1213,57 @@ PrintQueryResults(PGresult *results)
return success;
}
+/*
+ * Set special variables for "front door" queries
+ * - RESULT_STATUS: last query status
+ * - ERROR: TRUE/FALSE, whether an error occurred
+ * - ERROR_MESSAGE: message if an error occured
+ * - ROW_COUNT: how many rows were returned or affected, if appropriate
+ */
+static void
+SetResultVariables(PGresult *results)
+{
+ bool success;
+ ExecStatusType restat = PQresultStatus(results);
+
+ SetVariable(pset.vars, "RESULT_STATUS", PQresStatus(restat));
+
+ switch (restat)
+ {
+ case PGRES_EMPTY_QUERY:
+ case PGRES_TUPLES_OK:
+ case PGRES_COMMAND_OK:
+ case PGRES_COPY_OUT:
+ case PGRES_COPY_IN:
+ case PGRES_COPY_BOTH:
+ case PGRES_SINGLE_TUPLE:
+ success = true;
+ SetVariable(pset.vars, "ERROR", "FALSE");
+ SetVariable(pset.vars, "ERROR_MESSAGE", NULL);
+ break;
+ case PGRES_BAD_RESPONSE:
+ case PGRES_NONFATAL_ERROR:
+ case PGRES_FATAL_ERROR:
+ success = false;
+ SetVariable(pset.vars, "ERROR", "TRUE");
+ SetVariable(pset.vars, "ERROR_MESSAGE", PQerrorMessage(pset.db));
+ break;
+ default:
+ /* dead code */
+ success = false;
+ psql_error("unexpected PQresultStatus: %d\n", restat);
+ break;
+ }
+
+ if (success)
+ {
+ /* set variable if non empty */
+ char *ntuples = PQcmdTuples(results);
+ SetVariable(pset.vars, "ROW_COUNT", *ntuples ? ntuples : NULL);
+ }
+ else
+ SetVariable(pset.vars, "ROW_COUNT", NULL);
+}
/*
* SendQuery: send the query string to the backend
@@ -1346,6 +1397,9 @@ SendQuery(const char *query)
elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
+ /* set special variables to reflect the result status */
+ SetResultVariables(results);
+
/* but printing results isn't: */
if (OK && results)
OK = PrintQueryResults(results);
diff --git a/src/test/regress/expected/psql.out b/src/test/regress/expected/psql.out
index d602aee..3ee96b5 100644
--- a/src/test/regress/expected/psql.out
+++ b/src/test/regress/expected/psql.out
@@ -2904,6 +2904,36 @@ bar 'bar' "bar"
\echo 'should print #8-1'
should print #8-1
\endif
+-- special result variables
+SELECT 1 UNION SELECT 2;
+ ?column?
+----------
+ 1
+ 2
+(2 rows)
+
+\echo 'result status: ' :RESULT_STATUS
+result status: PGRES_TUPLES_OK
+\echo 'number of rows: ' :ROW_COUNT
+number of rows: 2
+SELECT 1 UNION;
+ERROR: syntax error at or near ";"
+LINE 1: SELECT 1 UNION;
+ ^
+\echo 'result status: ' :RESULT_STATUS
+result status: PGRES_FATAL_ERROR
+\if :ERROR
+ \echo 'Oops, an error occured...'
+Oops, an error occured...
+ \echo 'error message: ' :ERROR_MESSAGE
+error message: ERROR: syntax error at or near ";"
+LINE 1: SELECT 1 UNION;
+ ^
+
+\endif
+;
+\echo 'result status: ' :RESULT_STATUS
+result status: PGRES_EMPTY_QUERY
-- SHOW_CONTEXT
\set SHOW_CONTEXT never
do $$
diff --git a/src/test/regress/sql/psql.sql b/src/test/regress/sql/psql.sql
index b56a05f..716fe06 100644
--- a/src/test/regress/sql/psql.sql
+++ b/src/test/regress/sql/psql.sql
@@ -526,6 +526,21 @@ select \if false \\ (bogus \else \\ 42 \endif \\ forty_two;
\echo 'should print #8-1'
\endif
+-- special result variables
+SELECT 1 UNION SELECT 2;
+\echo 'result status: ' :RESULT_STATUS
+\echo 'number of rows: ' :ROW_COUNT
+
+SELECT 1 UNION;
+\echo 'result status: ' :RESULT_STATUS
+\if :ERROR
+ \echo 'Oops, an error occured...'
+ \echo 'error message: ' :ERROR_MESSAGE
+\endif
+
+;
+\echo 'result status: ' :RESULT_STATUS
+
-- SHOW_CONTEXT
\set SHOW_CONTEXT never
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers