psql: make \x affect normal queries only - Mailing list pgsql-patches

From Neil Conway
Subject psql: make \x affect normal queries only
Date
Msg-id 42ACEA83.4040609@samurai.com
Whole thread Raw
Responses Re: psql: make \x affect normal queries only
List pgsql-patches
Per discussion on -hackers, this patch changes psql's "expanded" output
mode to only affect the presentation of normal query results, not the
output of psql slash commands. Documentation updated. I also made some
unrelated minor psql cleanup.

Barring any objections I'll apply this later today or tomorrow.

-Neil
Index: doc/src/sgml/ref/psql-ref.sgml
===================================================================
RCS file: /var/lib/cvs/pgsql/doc/src/sgml/ref/psql-ref.sgml,v
retrieving revision 1.142
diff -c -r1.142 psql-ref.sgml
*** doc/src/sgml/ref/psql-ref.sgml    10 Jun 2005 16:31:48 -0000    1.142
--- doc/src/sgml/ref/psql-ref.sgml    13 Jun 2005 02:02:08 -0000
***************
*** 433,440 ****
        <term><option>--expanded</></term>
        <listitem>
        <para>
!       Turn on the extended table formatting mode. This is equivalent to the
!       command <command>\x</command>.
        </para>
        </listitem>
      </varlistentry>
--- 433,440 ----
        <term><option>--expanded</></term>
        <listitem>
        <para>
!       Turn on the expanded table formatting mode. This is equivalent to the
!       <command>\x</command> command.
        </para>
        </listitem>
      </varlistentry>
***************
*** 1389,1395 ****
            <literal>aligned</literal>, <literal>html</literal>,
            <literal>latex</literal>, or <literal>troff-ms</literal>.
            Unique abbreviations are allowed.  (That would mean one letter
!       is enough.)
            </para>

            <para>
--- 1389,1395 ----
            <literal>aligned</literal>, <literal>html</literal>,
            <literal>latex</literal>, or <literal>troff-ms</literal>.
            Unique abbreviations are allowed.  (That would mean one letter
!           is enough.)
            </para>

            <para>
***************
*** 1429,1438 ****
            <listitem>
            <para>
            Toggles between regular and expanded format. When expanded
!           format is enabled, all output has two columns with the column
!           name on the left and the data on the right. This mode is
!           useful if the data wouldn't fit on the screen in the normal
!           <quote>horizontal</quote> mode.
            </para>

            <para>
--- 1429,1441 ----
            <listitem>
            <para>
            Toggles between regular and expanded format. When expanded
!           format is enabled, query results are displayed in two
!           columns, with the column name on the left and the data on
!           the right. This option only affects how normal query results
!           are displayed; the output of <application>psql</application>
!           meta-commands is always presented using the regular
!           format. This mode is useful if the data wouldn't fit on the
!           screen in the normal <quote>horizontal</quote> mode.
            </para>

            <para>
***************
*** 1722,1728 ****
          <term><literal>\x</literal></term>
          <listitem>
          <para>
!         Toggles extended table formatting mode. As such it is equivalent to
          <literal>\pset expanded</literal>.
         </para>
         </listitem>
--- 1725,1731 ----
          <term><literal>\x</literal></term>
          <listitem>
          <para>
!         Toggles expanded table formatting mode. As such it is equivalent to
          <literal>\pset expanded</literal>.
         </para>
         </listitem>
Index: src/bin/psql/command.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/bin/psql/command.c,v
retrieving revision 1.145
diff -c -r1.145 command.c
*** src/bin/psql/command.c    9 Jun 2005 23:28:09 -0000    1.145
--- src/bin/psql/command.c    13 Jun 2005 00:19:00 -0000
***************
*** 114,124 ****
          {
              /* adjust cmd for possible messages below */
              cmd[1] = '\0';
-
- #if 0                            /* turned out to be too annoying */
-             if (isalpha((unsigned char) cmd[0]))
-                 psql_error("Warning: This syntax is deprecated.\n");
- #endif
          }
      }

--- 114,119 ----
Index: src/bin/psql/common.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/bin/psql/common.c,v
retrieving revision 1.100
diff -c -r1.100 common.c
*** src/bin/psql/common.c    10 Jun 2005 14:49:31 -0000    1.100
--- src/bin/psql/common.c    13 Jun 2005 01:46:31 -0000
***************
*** 64,70 ****

  static bool command_no_begin(const char *query);

-
  /*
   * "Safe" wrapper around strdup()
   */
--- 64,69 ----
***************
*** 189,195 ****
   *
   */
  void
! psql_error(const char *fmt,...)
  {
      va_list        ap;

--- 188,194 ----
   *
   */
  void
! psql_error(const char *fmt, ...)
  {
      va_list        ap;

***************
*** 784,789 ****
--- 783,792 ----
  static bool
  PrintQueryTuples(const PGresult *results)
  {
+     printQueryOpt my_popt = pset.popt;
+
+     my_popt.topt.normal_query = true;
+
      /* write output to \g argument, if any */
      if (pset.gfname)
      {
***************
*** 800,806 ****
              return false;
          }

!         printQuery(results, &pset.popt, pset.queryFout);

          /* close file/pipe, restore old setting */
          setQFout(NULL);
--- 803,809 ----
              return false;
          }

!         printQuery(results, &my_popt, pset.queryFout);

          /* close file/pipe, restore old setting */
          setQFout(NULL);
***************
*** 812,818 ****
          pset.gfname = NULL;
      }
      else
!         printQuery(results, &pset.popt, pset.queryFout);

      return true;
  }
--- 815,821 ----
          pset.gfname = NULL;
      }
      else
!         printQuery(results, &my_popt, pset.queryFout);

      return true;
  }
***************
*** 1001,1007 ****
          if (on_error_rollback_warning == false && pset.sversion < 80000)
          {
              fprintf(stderr, _("The server version (%d) does not support savepoints for ON_ERROR_ROLLBACK.\n"),
!                 pset.sversion);
              on_error_rollback_warning = true;
          }
          else
--- 1004,1010 ----
          if (on_error_rollback_warning == false && pset.sversion < 80000)
          {
              fprintf(stderr, _("The server version (%d) does not support savepoints for ON_ERROR_ROLLBACK.\n"),
!                     pset.sversion);
              on_error_rollback_warning = true;
          }
          else
Index: src/bin/psql/common.h
===================================================================
RCS file: /var/lib/cvs/pgsql/src/bin/psql/common.h,v
retrieving revision 1.43
diff -c -r1.43 common.h
*** src/bin/psql/common.h    30 May 2005 18:28:11 -0000    1.43
--- src/bin/psql/common.h    13 Jun 2005 00:19:19 -0000
***************
*** 22,28 ****

  #define atooid(x)  ((Oid) strtoul((x), NULL, 10))

-
  /*
   * Safer versions of some standard C library functions. If an
   * out-of-memory condition occurs, these functions will bail out
--- 22,27 ----
***************
*** 36,42 ****
  extern bool setQFout(const char *fname);

  extern void
! psql_error(const char *fmt,...)
  /* This lets gcc check the format string for consistency. */
  __attribute__((format(printf, 1, 2)));

--- 35,41 ----
  extern bool setQFout(const char *fname);

  extern void
! psql_error(const char *fmt, ...)
  /* This lets gcc check the format string for consistency. */
  __attribute__((format(printf, 1, 2)));

Index: src/bin/psql/print.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/bin/psql/print.c,v
retrieving revision 1.57
diff -c -r1.57 print.c
*** src/bin/psql/print.c    9 Jun 2005 18:40:06 -0000    1.57
--- src/bin/psql/print.c    13 Jun 2005 01:56:07 -0000
***************
*** 29,35 ****

  #include "mbprint.h"

-
  /*************************/
  /* Unaligned text         */
  /*************************/
--- 29,34 ----
***************
*** 1261,1266 ****
--- 1260,1266 ----
      const char *default_footer[] = {NULL};
      unsigned short int border = opt->border;
      FILE       *output;
+     bool use_expanded;

      if (opt->format == PRINT_NOTHING)
          return;
***************
*** 1271,1276 ****
--- 1271,1286 ----
      if (opt->format != PRINT_HTML && border > 2)
          border = 2;

+     /*
+      * We only want to display the results in "expanded" format if
+      * this is a normal (user-submitted) query, not a table we're
+      * printing for a slash command.
+      */
+     if (opt->expanded && opt->normal_query)
+         use_expanded = true;
+     else
+         use_expanded = false;
+
      if (fout == stdout)
      {
          int            col_count = 0,
***************
*** 1305,1341 ****
      switch (opt->format)
      {
          case PRINT_UNALIGNED:
!             if (opt->expanded)
!                 print_unaligned_vertical(title, headers, cells, footers, opt->fieldSep, opt->recordSep,
opt->tuples_only,output); 
              else
!                 print_unaligned_text(title, headers, cells, footers, opt->fieldSep, opt->recordSep, opt->tuples_only,
output);
              break;
          case PRINT_ALIGNED:
!             if (opt->expanded)
!                 print_aligned_vertical(title, headers, cells, footers, opt->tuples_only, border, opt->encoding,
output);
              else
!                 print_aligned_text(title, headers, cells, footers, align, opt->tuples_only, border, opt->encoding,
output);
              break;
          case PRINT_HTML:
!             if (opt->expanded)
!                 print_html_vertical(title, headers, cells, footers, align, opt->tuples_only, border, opt->tableAttr,
output);
              else
!                 print_html_text(title, headers, cells, footers, align, opt->tuples_only, border, opt->tableAttr,
output);
              break;
          case PRINT_LATEX:
!             if (opt->expanded)
!                 print_latex_vertical(title, headers, cells, footers, align, opt->tuples_only, border, output);
              else
!                 print_latex_text(title, headers, cells, footers, align, opt->tuples_only, border, output);
              break;
          case PRINT_TROFF_MS:
!             if (opt->expanded)
!                 print_troff_ms_vertical(title, headers, cells, footers, align, opt->tuples_only, border, output);
              else
!                 print_troff_ms_text(title, headers, cells, footers, align, opt->tuples_only, border, output);
              break;
          default:
!             fprintf(stderr, "+ Oops, you shouldn't see this!\n");
      }

      /* Only close if we used the pager */
--- 1315,1370 ----
      switch (opt->format)
      {
          case PRINT_UNALIGNED:
!             if (use_expanded)
!                 print_unaligned_vertical(title, headers, cells, footers,
!                                          opt->fieldSep, opt->recordSep,
!                                          opt->tuples_only, output);
              else
!                 print_unaligned_text(title, headers, cells, footers,
!                                      opt->fieldSep, opt->recordSep,
!                                      opt->tuples_only, output);
              break;
          case PRINT_ALIGNED:
!             if (use_expanded)
!                 print_aligned_vertical(title, headers, cells, footers,
!                                        opt->tuples_only, border,
!                                        opt->encoding, output);
              else
!                 print_aligned_text(title, headers, cells, footers,
!                                    align, opt->tuples_only,
!                                    border, opt->encoding, output);
              break;
          case PRINT_HTML:
!             if (use_expanded)
!                 print_html_vertical(title, headers, cells, footers,
!                                     align, opt->tuples_only,
!                                     border, opt->tableAttr, output);
              else
!                 print_html_text(title, headers, cells, footers,
!                                 align, opt->tuples_only, border,
!                                 opt->tableAttr, output);
              break;
          case PRINT_LATEX:
!             if (use_expanded)
!                 print_latex_vertical(title, headers, cells, footers, align,
!                                      opt->tuples_only, border, output);
              else
!                 print_latex_text(title, headers, cells, footers, align,
!                                  opt->tuples_only, border, output);
              break;
          case PRINT_TROFF_MS:
!             if (use_expanded)
!                 print_troff_ms_vertical(title, headers, cells, footers,
!                                         align, opt->tuples_only,
!                                         border, output);
              else
!                 print_troff_ms_text(title, headers, cells, footers,
!                                     align, opt->tuples_only,
!                                     border, output);
              break;
          default:
!             fprintf(stderr, _("illegal output format: %d"), opt->format);
!             exit(EXIT_FAILURE);
      }

      /* Only close if we used the pager */
Index: src/bin/psql/print.h
===================================================================
RCS file: /var/lib/cvs/pgsql/src/bin/psql/print.h,v
retrieving revision 1.23
diff -c -r1.23 print.h
*** src/bin/psql/print.h    9 Jun 2005 15:27:27 -0000    1.23
--- src/bin/psql/print.h    13 Jun 2005 01:47:25 -0000
***************
*** 42,47 ****
--- 42,50 ----
                                   * mode */
      char       *tableAttr;        /* attributes for HTML <table ...> */
      int            encoding;        /* character encoding */
+     bool        normal_query;    /* are we presenting the results of a
+                                  * "normal" query, or a slash
+                                  * command? */
  } printTableOpt;


Index: src/bin/psql/startup.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/bin/psql/startup.c,v
retrieving revision 1.115
diff -c -r1.115 startup.c
*** src/bin/psql/startup.c    29 Apr 2005 14:30:11 -0000    1.115
--- src/bin/psql/startup.c    13 Jun 2005 01:47:50 -0000
***************
*** 143,148 ****
--- 143,149 ----
      pset.queryFout = stdout;
      pset.popt.topt.border = 1;
      pset.popt.topt.pager = 1;
+     pset.popt.topt.normal_query = false;
      pset.popt.default_footer = true;

      SetVariable(pset.vars, "VERSION", PG_VERSION_STR);

pgsql-patches by date:

Previous
From: Tom Lane
Date:
Subject: Re: --enable-integer-datetimes doc patch
Next
From: Neil Conway
Date:
Subject: pl/pgsql dump tree cleanup