Re: Adding \ev view editor?

Поиск
Список
Период
Сортировка
От Andrew Dunstan
Тема Re: Adding \ev view editor?
Дата
Msg-id 4AB7C53D.7080002@dunslane.net
обсуждение исходный текст
Ответ на Re: Adding \ev view editor?  (Tom Lane <tgl@sss.pgh.pa.us>)
Ответы Re: Adding \ev view editor?  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: Adding \ev view editor?  (daveg <daveg@sonic.net>)
Список pgsql-hackers

Tom Lane wrote:
> It might be worth pointing out that what I don't want pg_dump doing
> is suppressing "useless" parentheses.  Adding whitespace ought to be
> safe enough.  So if anyone wanted to do the work of decoupling those
> two effects of the prettyprint option, we could have "semi pretty
> printed" output in pg_dump.  Dunno if it's worth it.
>
>
>

The attached patch goes part of the way towards doing this, by adding
white space unconditionally to the target list of a viewdef.

The nice thing about that is that it's the part of a viewdef which
basically isn't pretty-printed now, even if you ask for pretty printing.
That would certainly
make editing the view with a \ev command a lot nicer.

Along the way it suppresses putting a newline before a CASE expression
in pretty printing mode, which seems to be an odd thing to do and is
inconsistent with the way other expressions are treated.

Sample output:

    andrew=# select pg_get_viewdef('foo',true);
            pg_get_viewdef
    ------------------------------
      SELECT 'a'::text AS b,
             ( SELECT 1
                FROM dual) AS x,
             random() AS y,
             CASE
                 WHEN true THEN 1
                 ELSE 0
             END AS c,
             1 AS d
        FROM dual;
    (1 row)



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    21 Sep 2009 18:22:01 -0000
***************
*** 2648,2659 ****
                  TupleDesc resultDesc)
  {
      StringInfo    buf = context->buf;
      char       *sep;
!     int            colno;
      ListCell   *l;

      sep = " ";
      colno = 0;
      foreach(l, targetList)
      {
          TargetEntry *tle = (TargetEntry *) lfirst(l);
--- 2648,2671 ----
                  TupleDesc resultDesc)
  {
      StringInfo    buf = context->buf;
+     StringInfoData sep_indent;
      char       *sep;
!     int            colno, len;
      ListCell   *l;

      sep = " ";
      colno = 0;
+
+     /*
+      * Separator to make each target appear on its own line, regardless
+      * of pretty printing.
+      * Try to make them all line up at the same line position.
+      */
+     initStringInfo(&sep_indent);
+     appendStringInfoString(&sep_indent,",\n ");
+     for (len = buf->len -1; len >= 0 && buf->data[len]!= '\n'; len--)
+         appendStringInfoChar(&sep_indent,' ');
+
      foreach(l, targetList)
      {
          TargetEntry *tle = (TargetEntry *) lfirst(l);
***************
*** 2664,2670 ****
              continue;            /* ignore junk entries */

          appendStringInfoString(buf, sep);
!         sep = ", ";
          colno++;

          /*
--- 2676,2682 ----
              continue;            /* ignore junk entries */

          appendStringInfoString(buf, sep);
!         sep = sep_indent.data;
          colno++;

          /*
***************
*** 2704,2709 ****
--- 2716,2726 ----
                  appendStringInfo(buf, " AS %s", quote_identifier(colname));
          }
      }
+
+     if (colno > 1 && ! PRETTY_INDENT(context))
+         appendStringInfoChar(buf,'\n');
+
+     pfree(sep_indent.data);
  }

  static void
***************
*** 4595,4602 ****
                  CaseExpr   *caseexpr = (CaseExpr *) node;
                  ListCell   *temp;

!                 appendContextKeyword(context, "CASE",
!                                      0, PRETTYINDENT_VAR, 0);
                  if (caseexpr->arg)
                  {
                      appendStringInfoChar(buf, ' ');
--- 4612,4620 ----
                  CaseExpr   *caseexpr = (CaseExpr *) node;
                  ListCell   *temp;

!                 appendStringInfoString(buf,"CASE");
!                 if (PRETTY_INDENT(context))
!                     context->indentLevel += PRETTYINDENT_VAR;
                  if (caseexpr->arg)
                  {
                      appendStringInfoChar(buf, ' ');

В списке pgsql-hackers по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Standalone backends run StartupXLOG in an incorrect environment
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Adding \ev view editor?