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

From Bruce Momjian
Subject Re: Quoting of psql \d output
Date
Msg-id 200312231500.hBNF0gW02709@candle.pha.pa.us
Whole thread Raw
In response to Re: Quoting of psql \d output  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-patches
Tom Lane wrote:
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > psql \d always double-quotes table names:
> >           Table "public.xx"
>
> Yeah, that has bugged me for a while, because it is in fact *wrong* ...
> the above representation of a qualified name is incorrect.

> > This patch uses pg_dump fmtId() to double-quote only when necessary.
>
> Seems good, but I think your Makefile patch is a brick or two shy of
> a load.  Don't you need something to make sure that parser/keywords.o
> is up to date?

OK, got it.  I didn't notice the submake-backend rule before.  Attached.

> More generally, maybe we ought to think about moving fmtId() and perhaps
> other parts of dumputils out of pg_dump and into some
> more-easily-accessible library.

I will return to that.  Right now we have been putting code shared by
the backend and /bin into pgport, but I don't think that is going to be
clean for things shared by just /bin, and we have the keywords.c
dependency, making this even more complex.

--
  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 14:55:38 -0000
***************
*** 15,33 ****

  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

  psql: $(OBJS) $(libpq_builddir)/libpq.a
!     $(CC) $(CFLAGS) $(OBJS) $(libpq) $(LDFLAGS) $(LIBS) -o $@

  help.o: $(srcdir)/sql_help.h

  ifdef PERL
  $(srcdir)/sql_help.h: create_help.pl $(wildcard $(REFDOCDIR)/*.sgml)
      $(PERL) $< $(REFDOCDIR) $@
--- 15,43 ----

  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

! EXTRA_OBJS = $(top_builddir)/src/backend/parser/keywords.o
!
! all: submake-libpq submake-libpgport submake-backend psql

  psql: $(OBJS) $(libpq_builddir)/libpq.a
!     $(CC) $(CFLAGS) $(OBJS) $(EXTRA_OBJS) $(libpq) $(LDFLAGS) $(LIBS) -o $@

  help.o: $(srcdir)/sql_help.h

+ dumputils.c: % : $(top_srcdir)/src/bin/pg_dump/%
+     rm -f $@ && $(LN_S) $< .
+
+ .PHONY: submake-backend
+ submake-backend:
+     $(MAKE) -C $(top_builddir)/src/backend/parser keywords.o
+
  ifdef PERL
  $(srcdir)/sql_help.h: create_help.pl $(wildcard $(REFDOCDIR)/*.sgml)
      $(PERL) $< $(REFDOCDIR) $@
***************
*** 35,40 ****
--- 45,51 ----
  $(srcdir)/sql_help.h:
      @echo "*** Perl is needed to build psql help."
  endif
+

  distprep: $(srcdir)/sql_help.h

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 14:55:39 -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: Quoting of psql \d output
Next
From: Tom Lane
Date:
Subject: Re: Quoting of psql \d output