]> granicus.if.org Git - postgresql/commitdiff
Cause psql to report both the returned data and the command status tag
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 13 Aug 2006 21:10:04 +0000 (21:10 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 13 Aug 2006 21:10:04 +0000 (21:10 +0000)
for INSERT/UPDATE/DELETE RETURNING.  Per discussion.

src/bin/psql/common.c

index ca77b81808fec65e267b577682a069255e463a02..3233dfc2faeaf48fb5b360c3832172c16dfaab68 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2006, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.123 2006/08/11 19:20:59 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.124 2006/08/13 21:10:04 tgl Exp $
  */
 #include "postgres_fe.h"
 #include "common.h"
@@ -683,6 +683,36 @@ ProcessCopyResult(PGresult *results)
 }
 
 
+/*
+ * PrintQueryStatus: report command status as required
+ *
+ * Note: Utility function for use by PrintQueryResults() only.
+ */
+static void
+PrintQueryStatus(PGresult *results)
+{
+       char            buf[16];
+
+       if (!QUIET())
+       {
+               if (pset.popt.topt.format == PRINT_HTML)
+               {
+                       fputs("<p>", pset.queryFout);
+                       html_escaped_print(PQcmdStatus(results), pset.queryFout);
+                       fputs("</p>\n", pset.queryFout);
+               }
+               else
+                       fprintf(pset.queryFout, "%s\n", PQcmdStatus(results));
+       }
+
+       if (pset.logfile)
+               fprintf(pset.logfile, "%s\n", PQcmdStatus(results));
+
+       snprintf(buf, sizeof(buf), "%u", (unsigned int) PQoidValue(results));
+       SetVariable(pset.vars, "LASTOID", buf);
+}
+
+
 /*
  * PrintQueryResults: print out query results as required
  *
@@ -694,6 +724,7 @@ static bool
 PrintQueryResults(PGresult *results)
 {
        bool            success = false;
+       const char *cmdstatus;
 
        if (!results)
                return false;
@@ -701,33 +732,20 @@ PrintQueryResults(PGresult *results)
        switch (PQresultStatus(results))
        {
                case PGRES_TUPLES_OK:
+                       /* print the data ... */
                        success = PrintQueryTuples(results);
+                       /* if it's INSERT/UPDATE/DELETE RETURNING, also print status */
+                       cmdstatus = PQcmdStatus(results);
+                       if (strncmp(cmdstatus, "INSERT", 6) == 0 ||
+                               strncmp(cmdstatus, "UPDATE", 6) == 0 ||
+                               strncmp(cmdstatus, "DELETE", 6) == 0)
+                               PrintQueryStatus(results);
                        break;
 
                case PGRES_COMMAND_OK:
-                       {
-                               char            buf[10];
-
-                               success = true;
-                               snprintf(buf, sizeof(buf),
-                                                "%u", (unsigned int) PQoidValue(results));
-                               if (!QUIET())
-                               {
-                                       if (pset.popt.topt.format == PRINT_HTML)
-                                       {
-                                               fputs("<p>", pset.queryFout);
-                                               html_escaped_print(PQcmdStatus(results),
-                                                                                  pset.queryFout);
-                                               fputs("</p>\n", pset.queryFout);
-                                       }
-                                       else
-                                               fprintf(pset.queryFout, "%s\n", PQcmdStatus(results));
-                               }
-                               if (pset.logfile)
-                                       fprintf(pset.logfile, "%s\n", PQcmdStatus(results));
-                               SetVariable(pset.vars, "LASTOID", buf);
-                               break;
-                       }
+                       PrintQueryStatus(results);
+                       success = true;
+                       break;
 
                case PGRES_EMPTY_QUERY:
                        success = true;