Thread: cosmetic psql tweaks

cosmetic psql tweaks

From
Neil Conway
Date:
As the subject says -- I've applied this patch to HEAD.

-Neil
Index: src/bin/psql/common.c
===================================================================
RCS file: /Users/neilc/local/cvs/pgsql-server/src/bin/psql/common.c,v
retrieving revision 1.86
diff -c -r1.86 common.c
*** src/bin/psql/common.c    7 May 2004 00:24:58 -0000    1.86
--- src/bin/psql/common.c    23 May 2004 21:20:01 -0000
***************
*** 802,808 ****
                  char        buf[10];

                  success = true;
!                 sprintf(buf, "%u", (unsigned int) PQoidValue(results));
                  if (!QUIET())
                  {
                      if (pset.popt.topt.format == PRINT_HTML)
--- 802,809 ----
                  char        buf[10];

                  success = true;
!                 snprintf(buf, sizeof(buf),
!                          "%u", (unsigned int) PQoidValue(results));
                  if (!QUIET())
                  {
                      if (pset.popt.topt.format == PRINT_HTML)
Index: src/bin/psql/print.c
===================================================================
RCS file: /Users/neilc/local/cvs/pgsql-server/src/bin/psql/print.c,v
retrieving revision 1.47
diff -c -r1.47 print.c
*** src/bin/psql/print.c    18 May 2004 20:18:58 -0000    1.47
--- src/bin/psql/print.c    23 May 2004 22:07:52 -0000
***************
*** 1133,1147 ****
  printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
  {
      int            nfields;
      const char **headers;
      const char **cells;
      char      **footers;
      char       *align;
      int            i;

-
      /* extract headers */
-
      nfields = PQnfields(result);

      headers = calloc(nfields + 1, sizeof(*headers));
--- 1133,1146 ----
  printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
  {
      int            nfields;
+     int            ncells;
      const char **headers;
      const char **cells;
      char      **footers;
      char       *align;
      int            i;

      /* extract headers */
      nfields = PQnfields(result);

      headers = calloc(nfields + 1, sizeof(*headers));
***************
*** 1155,1169 ****
          headers[i] = mbvalidate(PQfname(result, i), opt->topt.encoding);

      /* set cells */
!
!     cells = calloc(nfields * PQntuples(result) + 1, sizeof(*cells));
      if (!cells)
      {
          perror("calloc");
          exit(EXIT_FAILURE);
      }

!     for (i = 0; i < nfields * PQntuples(result); i++)
      {
          if (PQgetisnull(result, i / nfields, i % nfields))
              cells[i] = opt->nullPrint ? opt->nullPrint : "";
--- 1154,1168 ----
          headers[i] = mbvalidate(PQfname(result, i), opt->topt.encoding);

      /* set cells */
!     ncells = PQntuples(result) * nfields;
!     cells = calloc(ncells + 1, sizeof(*cells));
      if (!cells)
      {
          perror("calloc");
          exit(EXIT_FAILURE);
      }

!     for (i = 0; i < ncells; i++)
      {
          if (PQgetisnull(result, i / nfields, i % nfields))
              cells[i] = opt->nullPrint ? opt->nullPrint : "";
***************
*** 1185,1190 ****
--- 1184,1194 ----
          }

          footers[0] = malloc(100);
+         if (!footers[0])
+         {
+             perror("malloc");
+             exit(EXIT_FAILURE);
+         }
          if (PQntuples(result) == 1)
              snprintf(footers[0], 100, gettext("(1 row)"));
          else
***************
*** 1194,1200 ****
          footers = NULL;

      /* set alignment */
-
      align = calloc(nfields + 1, sizeof(*align));
      if (!align)
      {
--- 1198,1203 ----
***************
*** 1221,1233 ****
      }

      /* call table printer */
-
      printTable(opt->title, headers, cells,
!                footers ? (const char *const *) footers : (const char *const *) (opt->footers),
                 align, &opt->topt, fout);

!     free((void *) headers);
!     free((void *) cells);
      if (footers)
      {
          free(footers[0]);
--- 1224,1235 ----
      }

      /* call table printer */
      printTable(opt->title, headers, cells,
!                (const char *const *) footers,
                 align, &opt->topt, fout);

!     free(headers);
!     free(cells);
      if (footers)
      {
          free(footers[0]);

Re: cosmetic psql tweaks

From
Tom Lane
Date:
Neil Conway <neilc@samurai.com> writes:
>       printTable(opt->title, headers, cells,
> !                footers ? (const char *const *) footers : (const char *const *) (opt->footers),
>                  align, &opt->topt, fout);

>       printTable(opt->title, headers, cells,
> !                (const char *const *) footers,
>                  align, &opt->topt, fout);

Er, doesn't that change the behavior?

            regards, tom lane

Re: cosmetic psql tweaks

From
Neil Conway
Date:
Tom Lane wrote:
> Er, doesn't that change the behavior?

I don't believe so. There is an if statement earlier that assigns
opt->footers to footers is the former is non-NULL. In other words:

if (opt->footers)
     footers = opt->footers;
else if (...)
     footers = xyz();
else
     footers = NULL;

Therefore if footers is NULL then opt->footers must also be NULL, so the
previous trinary statement is just confusing.

-Neil