Re: pretty print viewdefs - Mailing list pgsql-hackers

From Andrew Dunstan
Subject Re: pretty print viewdefs
Date
Msg-id 4A95C16A.6090706@dunslane.net
Whole thread Raw
In response to Re: pretty print viewdefs  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: pretty print viewdefs
List pgsql-hackers

Tom Lane wrote:
> Andrew Dunstan <andrew@dunslane.net> writes:
>
>> I do have a solution that wraps when running line length over 80 instead
>> of on every col:
>>
>
>
>>  SELECT sh.shoename, sh.sh_avail, sh.slcolor, sh.slminlen,
>>     sh.slminlen * un.un_fact AS slminlen_cm, sh.slmaxlen,
>>     sh.slmaxlen * un.un_fact AS slmaxlen_cm, sh.slunit
>>    FROM shoe_data sh, unit un
>>   WHERE sh.slunit = un.un_name;
>>
>
>
>> It's not a huge amount of code.
>>
>
> Well, let's see it?  What do you do with expressions that don't fit?
>

See attached.

We don't apply the wrapping unless there has been a column printed on
the line (except for the SELECT line).

So we can run over the limit on a line, but if we do there's only one
column spec. I think that's acceptable.

>
>> Maybe we need a couple of extra pg_get_viewdef() variants. One to wrap
>> on some provided line length, one to wrap on every column. psql could
>> use the first, pg_dump could use the second.
>>
>
> pg_dump doesn't use prettyprinting at all, and won't if I have anything
> to say about it.  But I could see teaching the psql \d views to pass
> along whatever psql thinks the window width is.
>
>
>

OK, but I'd still like to have the only one col per line variant available.

cheers

andrew

Index: src/backend/utils/adt/ruleutils.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v
retrieving revision 1.306
diff -c -r1.306 ruleutils.c
*** src/backend/utils/adt/ruleutils.c    1 Aug 2009 19:59:41 -0000    1.306
--- src/backend/utils/adt/ruleutils.c    26 Aug 2009 23:09:00 -0000
***************
*** 2649,2659 ****
  {
      StringInfo    buf = context->buf;
      char       *sep;
!     int            colno;
      ListCell   *l;

      sep = " ";
      colno = 0;
      foreach(l, targetList)
      {
          TargetEntry *tle = (TargetEntry *) lfirst(l);
--- 2649,2669 ----
  {
      StringInfo    buf = context->buf;
      char       *sep;
!     int            colno, linecol;
      ListCell   *l;
+     int         save_len;
+     char       *line_start;
+
+     line_start = strrchr(buf->data,'\n');
+     if (line_start == NULL)
+         line_start = buf->data;
+     else
+         line_start++;
+

      sep = " ";
      colno = 0;
+     linecol = 1;
      foreach(l, targetList)
      {
          TargetEntry *tle = (TargetEntry *) lfirst(l);
***************
*** 2666,2671 ****
--- 2676,2683 ----
          appendStringInfoString(buf, sep);
          sep = ", ";
          colno++;
+         linecol++;
+         save_len = buf->len;

          /*
           * We special-case Var nodes rather than using get_rule_expr. This is
***************
*** 2703,2708 ****
--- 2715,2748 ----
              if (attname == NULL || strcmp(attname, colname) != 0)
                  appendStringInfo(buf, " AS %s", quote_identifier(colname));
          }
+
+         /* handle line overflow */
+         if (strlen(line_start) > 80 && linecol > 1 && PRETTY_INDENT(context))
+         {
+             StringInfoData thiscol;
+
+             initStringInfo(&thiscol);
+
+             /* save what we just added */
+             appendStringInfoString(&thiscol,buf->data + save_len);
+
+             /* wipe it out from the buffer */
+             buf->len = save_len;
+             buf->data[save_len] = '\0';
+
+             /* add a line break and reindent */
+             appendContextKeyword(context, "", -PRETTYINDENT_STD,
+                                  PRETTYINDENT_STD, PRETTYINDENT_VAR);
+
+             /* and now put back what we wiped out */
+             appendStringInfoString(buf,thiscol.data);
+
+             /* reset the counters */
+             line_start = strrchr(buf->data,'\n') + 1;
+             linecol = 0;
+
+             pfree(thiscol.data);
+         }
      }
  }


pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: pretty print viewdefs
Next
From: Peter Eisentraut
Date:
Subject: Re: 8.5 release timetable, again