Quoting of psql \d output - Mailing list pgsql-patches

From Bruce Momjian
Subject Quoting of psql \d output
Date
Msg-id 200312230509.hBN59r003126@candle.pha.pa.us
Whole thread Raw
Responses Re: Quoting of psql \d output
Re: Quoting of psql \d output
Re: Quoting of psql \d output
List pgsql-patches
psql \d always double-quotes table names:

          Table "public.xx"
     Column |  Type   | Modifiers
    --------+---------+-----------
     y      | integer |
    Indexes:
        "ii" btree (y)


With this patch, double-quotes are not used when not required:

    test=> \d xx
           Table public.xx
     Column |  Type   | Modifiers
    --------+---------+-----------
     y      | integer |
    Indexes:
        ii btree (y)


but does in this case:

    test=> \d "xx y"
         Table public."xx y"
     Column |  Type   | Modifiers
    --------+---------+-----------
     y      | integer |
    Indexes:
        vv btree (y)

This patch uses pg_dump fmtId() to double-quote only when necessary.


--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/bin/psql/Makefile
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/Makefile,v
retrieving revision 1.38
diff -c -c -r1.38 Makefile
*** src/bin/psql/Makefile    29 Nov 2003 19:52:06 -0000    1.38
--- src/bin/psql/Makefile    23 Dec 2003 04:52:20 -0000
***************
*** 15,25 ****

  REFDOCDIR= $(top_srcdir)/doc/src/sgml/ref

! override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS) -DFRONTEND

  OBJS=    command.o common.o help.o input.o stringutils.o mainloop.o copy.o \
      startup.o prompt.o variables.o large_obj.o print.o describe.o \
!     tab-complete.o mbprint.o

  all: submake-libpq submake-libpgport psql

--- 15,26 ----

  REFDOCDIR= $(top_srcdir)/doc/src/sgml/ref

! override CPPFLAGS := -I$(top_srcdir)/src/bin/pg_dump -I$(libpq_srcdir) $(CPPFLAGS) -DFRONTEND

  OBJS=    command.o common.o help.o input.o stringutils.o mainloop.o copy.o \
      startup.o prompt.o variables.o large_obj.o print.o describe.o \
!     tab-complete.o mbprint.o \
!     dumputils.o $(top_builddir)/src/backend/parser/keywords.o

  all: submake-libpq submake-libpgport psql

***************
*** 27,32 ****
--- 28,36 ----
      $(CC) $(CFLAGS) $(OBJS) $(libpq) $(LDFLAGS) $(LIBS) -o $@

  help.o: $(srcdir)/sql_help.h
+
+ dumputils.c: % : $(top_srcdir)/src/bin/pg_dump/%
+     rm -f $@ && $(LN_S) $< .

  ifdef PERL
  $(srcdir)/sql_help.h: create_help.pl $(wildcard $(REFDOCDIR)/*.sgml)
Index: src/bin/psql/describe.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/describe.c,v
retrieving revision 1.90
diff -c -c -r1.90 describe.c
*** src/bin/psql/describe.c    1 Dec 2003 22:21:54 -0000    1.90
--- src/bin/psql/describe.c    23 Dec 2003 04:52:21 -0000
***************
*** 16,21 ****
--- 16,23 ----
  #include "print.h"
  #include "variables.h"

+ #include "dumputils.h"
+
  #include <ctype.h>

  #ifdef WIN32
***************
*** 658,663 ****
--- 660,667 ----
      PQExpBufferData tmpbuf;
      int            cols = 0;
      int            numrows = 0;
+     char schema_rel_str[NAMEDATALEN * 2 + 6];
+
      struct
      {
          bool        hasindex;
***************
*** 812,851 ****
  #endif
      }

      /* Make title */
      switch (tableinfo.relkind)
      {
          case 'r':
!             printfPQExpBuffer(&title, _("Table \"%s.%s\""),
!                               schemaname, relationname);
              break;
          case 'v':
!             printfPQExpBuffer(&title, _("View \"%s.%s\""),
!                               schemaname, relationname);
              break;
          case 'S':
!             printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
!                               schemaname, relationname);
              break;
          case 'i':
!             printfPQExpBuffer(&title, _("Index \"%s.%s\""),
!                               schemaname, relationname);
              break;
          case 's':
!             printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
!                               schemaname, relationname);
              break;
          case 't':
!             printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
!                               schemaname, relationname);
              break;
          case 'c':
!             printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
!                               schemaname, relationname);
              break;
          default:
!             printfPQExpBuffer(&title, _("?%c? \"%s.%s\""),
!                             tableinfo.relkind, schemaname, relationname);
              break;
      }

--- 816,852 ----
  #endif
      }

+
+     strcpy(schema_rel_str, fmtId(schemaname));
+     strcat(schema_rel_str, ".");
+     strcat(schema_rel_str, fmtId(relationname));
+
      /* Make title */
      switch (tableinfo.relkind)
      {
          case 'r':
!             printfPQExpBuffer(&title, _("Table %s"), schema_rel_str);
              break;
          case 'v':
!             printfPQExpBuffer(&title, _("View %s"), schema_rel_str);
              break;
          case 'S':
!             printfPQExpBuffer(&title, _("Sequence %s"), schema_rel_str);
              break;
          case 'i':
!             printfPQExpBuffer(&title, _("Index %s"), schema_rel_str);
              break;
          case 's':
!             printfPQExpBuffer(&title, _("Special relation %s"), schema_rel_str);
              break;
          case 't':
!             printfPQExpBuffer(&title, _("TOAST table %s"), schema_rel_str);
              break;
          case 'c':
!             printfPQExpBuffer(&title, _("Composite type %s"), schema_rel_str);
              break;
          default:
!             printfPQExpBuffer(&title, _("?%c? %s"), tableinfo.relkind, schema_rel_str);
              break;
      }

***************
*** 887,895 ****
                  resetPQExpBuffer(&tmpbuf);
              appendPQExpBuffer(&tmpbuf, "%s, ", indamname);

              /* we assume here that index and table are in same schema */
!             appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
!                               schemaname, indtable);

              if (strlen(indpred))
                  appendPQExpBuffer(&tmpbuf, ", predicate (%s)", indpred);
--- 888,899 ----
                  resetPQExpBuffer(&tmpbuf);
              appendPQExpBuffer(&tmpbuf, "%s, ", indamname);

+             strcpy(schema_rel_str, fmtId(schemaname));
+             strcat(schema_rel_str, ".");
+             strcat(schema_rel_str, fmtId(indtable));
+
              /* we assume here that index and table are in same schema */
!             appendPQExpBuffer(&tmpbuf, _("for table %s"), schema_rel_str);

              if (strlen(indpred))
                  appendPQExpBuffer(&tmpbuf, ", predicate (%s)", indpred);
***************
*** 1095,1102 ****
                  const char *usingpos;

                  /* Output index name */
!                 printfPQExpBuffer(&buf, _("    \"%s\""),
!                                   PQgetvalue(result1, i, 0));

                  /* Label as primary key or unique (but not both) */
                  appendPQExpBuffer(&buf,
--- 1099,1106 ----
                  const char *usingpos;

                  /* Output index name */
!                 printfPQExpBuffer(&buf, _("    %s"),
!                                   fmtId(PQgetvalue(result1, i, 0)));

                  /* Label as primary key or unique (but not both) */
                  appendPQExpBuffer(&buf,
***************
*** 1125,1132 ****
              footers[count_footers++] = xstrdup(buf.data);
              for (i = 0; i < check_count; i++)
              {
!                 printfPQExpBuffer(&buf, _("    \"%s\" %s"),
!                                   PQgetvalue(result2, i, 1),
                                    PQgetvalue(result2, i, 0));

                  footers[count_footers++] = xstrdup(buf.data);
--- 1129,1136 ----
              footers[count_footers++] = xstrdup(buf.data);
              for (i = 0; i < check_count; i++)
              {
!                 printfPQExpBuffer(&buf, _("    %s %s"),
!                                   fmtId(PQgetvalue(result2, i, 1)),
                                    PQgetvalue(result2, i, 0));

                  footers[count_footers++] = xstrdup(buf.data);
***************
*** 1140,1147 ****
              footers[count_footers++] = xstrdup(buf.data);
              for (i = 0; i < foreignkey_count; i++)
              {
!                 printfPQExpBuffer(&buf, _("    \"%s\" %s"),
!                                   PQgetvalue(result5, i, 0),
                                    PQgetvalue(result5, i, 1));

                  footers[count_footers++] = xstrdup(buf.data);
--- 1144,1151 ----
              footers[count_footers++] = xstrdup(buf.data);
              for (i = 0; i < foreignkey_count; i++)
              {
!                 printfPQExpBuffer(&buf, _("    %s %s"),
!                                   fmtId(PQgetvalue(result5, i, 0)),
                                    PQgetvalue(result5, i, 1));

                  footers[count_footers++] = xstrdup(buf.data);

pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [GENERAL] Temporary tables and miscellaneous schemas
Next
From: Tom Lane
Date:
Subject: Re: Quoting of psql \d output