OSDN Git Service

I was tinkering with creating rules on views (so, for instance, one could
authorBruce Momjian <bruce@momjian.us>
Fri, 5 Apr 2002 11:52:38 +0000 (11:52 +0000)
committerBruce Momjian <bruce@momjian.us>
Fri, 5 Apr 2002 11:52:38 +0000 (11:52 +0000)
insert on a view), and noticed that psql wouldn't show the list of rules
set up on a view, like it does for tables.

The fix was extremely simple, so I figured I'd share it.  Not sure what
the standard is for communicating these things, so I've attached the diff
file for /src/bin/psql/describe.c.

Paul (?)

src/bin/psql/describe.c

index 0c6f544..68bbcfb 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.47 2002/03/20 19:44:45 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.48 2002/04/05 11:52:38 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "describe.h"
@@ -692,12 +692,51 @@ describeTableDetails(const char *name, bool desc)
        }
        else if (view_def)
        {
+               PGresult   *result = NULL;
+               int                     rule_count = 0;
+               int                     count_footers = 0;
+
+               /* count rules */
+               if (!error && tableinfo.hasrules)
+               {
+                       sprintf(buf,
+                                       "SELECT r.rulename\n"
+                                       "FROM pg_rewrite r, pg_class c\n"
+                                       "WHERE c.relname='%s' AND c.oid = r.ev_class\n"
+                                       "AND r.rulename NOT LIKE '_RET%%'",
+                                       name);
+                       result = PSQLexec(buf);
+                       if (!result)
+                               error = true;
+                       else
+                               rule_count = PQntuples(result);
+               }
+
                /* Footer information about a view */
-               footers = xmalloc(2 * sizeof(*footers));
-               footers[0] = xmalloc(64 + strlen(view_def));
-               snprintf(footers[0], 64 + strlen(view_def),
+               footers = xmalloc((rule_count + 2) * sizeof(*footers));
+               footers[count_footers] = xmalloc(64 + strlen(view_def));
+               snprintf(footers[count_footers], 64 + strlen(view_def),
                                 _("View definition: %s"), view_def);
-               footers[1] = NULL;
+               count_footers++;
+
+               /* print rules */
+               for (i = 0; i < rule_count; i++)
+               {
+                       char       *s = _("Rules");
+
+                       if (i == 0)
+                               snprintf(buf, sizeof(buf), "%s: %s", s, PQgetvalue(result, i, 0));
+                       else
+                               snprintf(buf, sizeof(buf), "%*s  %s", (int) strlen(s), "", PQgetvalue(result, i, 0));
+                       if (i < rule_count - 1)
+                               strcat(buf, ",");
+
+                       footers[count_footers++] = xstrdup(buf);
+               }
+               PQclear(result);
+
+               footers[count_footers] = NULL;
+
        }
        else if (tableinfo.relkind == 'r')
        {