]> granicus.if.org Git - postgresql/commitdiff
Hi all
authorBruce Momjian <bruce@momjian.us>
Tue, 6 Oct 1998 03:09:02 +0000 (03:09 +0000)
committerBruce Momjian <bruce@momjian.us>
Tue, 6 Oct 1998 03:09:02 +0000 (03:09 +0000)
Is it too late to add a feature to pg_dump for 6.4??

I just spent most of the day learning pg_dump and modifing it so it
would
dump views also.

This is the first time I have ever contributed any code changes, so I'm
not sure of how to submit it.

The diff's and a readme as a tgz file are attached.

Thanks
Terry Mackintosh <terry@terrym.com>          http://www.terrym.com

src/bin/pg_dump/pg_dump.c
src/bin/pg_dump/pg_dump.h
src/man/pg_dump.1

index ee944f6acc834c308fd1cc09db56371d51b559c6..8e4e32896ed6ba75829efac406be835325421608 100644 (file)
@@ -21,7 +21,7 @@
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.88 1998/10/02 16:43:40 thomas Exp $
+ *       $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.89 1998/10/06 03:08:59 momjian Exp $
  *
  * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
  *
@@ -1383,7 +1383,7 @@ getFuncs(int *numFuncs)
 TableInfo  *
 getTables(int *numTables, FuncInfo *finfo, int numFuncs)
 {
-       PGresult   *res;
+       PGresult   *res, *viewres;
        int                     ntups;
        int                     i;
        char            query[MAXQUERYLEN];
@@ -1414,6 +1414,8 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
        }
        PQclear(res);
 
+/* NOTE, when outer joins are here, change this query to get the 
+view definition all in one go. */
        sprintf(query,
                        "SELECT pg_class.oid, relname, relkind, relacl, usename, "
                        "relchecks, reltriggers "
@@ -1454,6 +1456,39 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
                tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks));
                tblinfo[i].ntrig = atoi(PQgetvalue(res, i, i_reltriggers));
 
+               /* NOTE that at such time as left outer joins become avaliable, 
+               then this will no longer be needed, and can be done in the 
+               above query. */
+
+               sprintf(query,
+                       "select definition from pg_views where viewname = '%s';",
+                       tblinfo[i].relname);
+
+               viewres = PQexec(g_conn, query);
+               if (!viewres ||
+                       PQresultStatus(res) != PGRES_TUPLES_OK)
+               {
+                       fprintf(stderr, "getTables(): SELECT for views failed\n");
+                       exit_nicely(g_conn);
+               }
+
+               /* NOTE: Tryed to use isViewRule here, but it does it's own 
+               BEGIN and END so messed things up.
+               This also needs redone should we ever get outer joins.
+               */
+               if ( PQntuples(viewres) > 0 )
+               {
+                       if ( PQntuples(viewres) != 1 )
+                       {
+                               fprintf(stderr, "getTables(): failed to get view definition.\n");
+                               exit_nicely(g_conn);
+                       }
+
+                       tblinfo[i].viewdef = strdup(PQgetvalue(viewres, 0, 0)); 
+               }
+
+               PQclear(viewres);
+
                /* Get CHECK constraints */
                if (tblinfo[i].ncheck > 0)
                {
@@ -2468,95 +2503,102 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
                if (!tablename || (!strcmp(tblinfo[i].relname, tablename)))
                {
 
-                       /* Skip VIEW relations */
+                       /* Dump VIEW relations also !-) */
                        if (isViewRule(tblinfo[i].relname))
-                               continue;
-
-                       parentRels = tblinfo[i].parentRels;
-                       numParents = tblinfo[i].numParents;
-
-                       becomeUser(fout, tblinfo[i].usename);
+                       {
+                               becomeUser(fout, tblinfo[i].usename);
 
-                       sprintf(q, "CREATE TABLE %s (", fmtId(tblinfo[i].relname));
-                       actual_atts = 0;
-                       for (j = 0; j < tblinfo[i].numatts; j++)
+                               sprintf(q, "CREATE VIEW %s AS %s\n", 
+                                       fmtId(tblinfo[i].relname),
+                                       tblinfo[i].viewdef);
+                       }
+                       else
                        {
-                               if (tblinfo[i].inhAttrs[j] == 0)
-                               {
+                               parentRels = tblinfo[i].parentRels;
+                               numParents = tblinfo[i].numParents;
 
-                                       /* Show lengths on bpchar and varchar */
-                                       if (!strcmp(tblinfo[i].typnames[j], "bpchar"))
-                                       {
-                                               sprintf(q, "%s%s%s char",
-                                                               q,
-                                                               (actual_atts > 0) ? ", " : "",
-                                                               fmtId(tblinfo[i].attnames[j]));
+                               becomeUser(fout, tblinfo[i].usename);
 
-                                               sprintf(q, "%s(%d)",
-                                                               q,
-                                                               tblinfo[i].atttypmod[j] - VARHDRSZ);
-                                               actual_atts++;
-                                       }
-                                       else if (!strcmp(tblinfo[i].typnames[j], "varchar"))
+                               sprintf(q, "CREATE TABLE %s (", fmtId(tblinfo[i].relname));
+                               actual_atts = 0;
+                               for (j = 0; j < tblinfo[i].numatts; j++)
+                               {
+                                       if (tblinfo[i].inhAttrs[j] == 0)
                                        {
-                                               sprintf(q, "%s%s%s %s",
-                                                               q,
-                                                               (actual_atts > 0) ? ", " : "",
-                                                               fmtId(tblinfo[i].attnames[j]),
-                                                               tblinfo[i].typnames[j]);
 
-                                               sprintf(q, "%s(%d)",
-                                                               q,
-                                                               tblinfo[i].atttypmod[j] - VARHDRSZ);
-                                               actual_atts++;
-                                       }
-                                       else
-                                       {
-                                               strcpy(id1, fmtId(tblinfo[i].attnames[j]));
-                                               strcpy(id2, fmtId(tblinfo[i].typnames[j]));
-                                               sprintf(q, "%s%s%s %s",
-                                                               q,
-                                                               (actual_atts > 0) ? ", " : "",
-                                                               id1,
-                                                               id2);
-                                               actual_atts++;
+                                               /* Show lengths on bpchar and varchar */
+                                               if (!strcmp(tblinfo[i].typnames[j], "bpchar"))
+                                               {
+                                                       sprintf(q, "%s%s%s char", 
+                                                                       q, 
+                                                                       (actual_atts > 0) ? ", " : "",
+                                                                       fmtId(tblinfo[i].attnames[j]));
+
+                                                       sprintf(q, "%s(%d)", 
+                                                                       q, 
+                                                                       tblinfo[i].atttypmod[j] - VARHDRSZ);
+                                                       actual_atts++;
+                                               }
+                                               else if (!strcmp(tblinfo[i].typnames[j], "varchar"))
+                                               {
+                                                       sprintf(q, "%s%s%s %s",
+                                                                       q,
+                                                                       (actual_atts > 0) ? ", " : "",
+                                                                       fmtId(tblinfo[i].attnames[j]),
+                                                                       tblinfo[i].typnames[j]);
+
+                                                       sprintf(q, "%s(%d)",
+                                                                       q,
+                                                                       tblinfo[i].atttypmod[j] - VARHDRSZ);
+                                                       actual_atts++;
+                                               }
+                                               else
+                                               {
+                                                       strcpy(id1, fmtId(tblinfo[i].attnames[j]));
+                                                       strcpy(id2, fmtId(tblinfo[i].typnames[j]));
+                                                       sprintf(q, "%s%s%s %s",
+                                                                       q,
+                                                                       (actual_atts > 0) ? ", " : "",
+                                                                       id1,
+                                                                       id2);
+                                                       actual_atts++;
+                                               }
+                                               if (tblinfo[i].adef_expr[j] != NULL)
+                                                       sprintf(q, "%s DEFAULT %s", q, tblinfo[i].adef_expr[j]);
+                                               if (tblinfo[i].notnull[j])
+                                                       sprintf(q, "%s NOT NULL", q);
                                        }
-                                       if (tblinfo[i].adef_expr[j] != NULL)
-                                               sprintf(q, "%s DEFAULT %s", q, tblinfo[i].adef_expr[j]);
-                                       if (tblinfo[i].notnull[j])
-                                               sprintf(q, "%s NOT NULL", q);
                                }
-                       }
 
-                       /* put the CONSTRAINTS inside the table def */
-                       for (k = 0; k < tblinfo[i].ncheck; k++)
-                       {
-                               sprintf(q, "%s%s %s",
-                                               q,
-                                               (actual_atts + k > 0) ? ", " : "",
-                                               tblinfo[i].check_expr[k]);
-                       }
-
-                       strcat(q, ")");
-
-                       if (numParents > 0)
-                       {
-                               sprintf(q, "%s inherits ( ", q);
-                               for (k = 0; k < numParents; k++)
+                               /* put the CONSTRAINTS inside the table def */
+                               for (k = 0; k < tblinfo[i].ncheck; k++)
                                {
-                                       sprintf(q, "%s%s%s",
+                                       sprintf(q, "%s%s %s",
                                                        q,
-                                                       (k > 0) ? ", " : "",
-                                                       fmtId(parentRels[k]));
+                                                       (actual_atts + k > 0) ? ", " : "",
+                                                       tblinfo[i].check_expr[k]);
                                }
+
                                strcat(q, ")");
-                       }
 
-                       strcat(q, ";\n");
+                               if (numParents > 0)
+                               {
+                                       sprintf(q, "%s inherits ( ", q);
+                                       for (k = 0; k < numParents; k++)
+                                       {
+                                               sprintf(q, "%s%s%s",
+                                                               q,
+                                                               (k > 0) ? ", " : "",
+                                                               fmtId(parentRels[k]));
+                                       }
+                                       strcat(q, ")");
+                               }
+                               strcat(q, ";\n");
+                       } /* end of if view ... else .... */
+
                        fputs(q, fout);
                        if (acls)
                                dumpACL(fout, tblinfo[i]);
-
                }
        }
 }
index 0a87093561c7093bbce1f3b1f17ac7aed61fb9c1..3192898b46391971a586e4cd23766da7ff8c2fe6 100644 (file)
@@ -5,7 +5,7 @@
  *
  * Copyright (c) 1994, Regents of the University of California
  *
- * $Id: pg_dump.h,v 1.33 1998/10/02 16:43:41 thomas Exp $
+ * $Id: pg_dump.h,v 1.34 1998/10/06 03:09:01 momjian Exp $
  *
  * Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2
  *
@@ -66,6 +66,7 @@ typedef struct _tableInfo
 {
        char       *oid;
        char       *relname;
+       char       *viewdef;
        char       *relacl;
        bool            sequence;
        int                     numatts;                /* number of attributes */
index 67f04e67c0a89d2e615d0051d49bcf8d10bd006e..ebe2a9f6d060a4497aba4c44845329071fcc20aa 100644 (file)
@@ -1,6 +1,6 @@
 .\" This is -*-nroff-*-
 .\" XXX standard disclaimer belongs here....
-.\" $Header: /cvsroot/pgsql/src/man/Attic/pg_dump.1,v 1.12 1998/07/19 05:24:51 momjian Exp $
+.\" $Header: /cvsroot/pgsql/src/man/Attic/pg_dump.1,v 1.13 1998/10/06 03:09:02 momjian Exp $
 .TH PG_DUMP UNIX 7/15/98 PostgreSQL PostgreSQL
 .SH NAME
 pg_dump - dumps out a Postgres database into a script file
@@ -112,10 +112,10 @@ The limitations mostly stem from
 difficulty in extracting certain meta-information from the system
 catalogs.   
 .TP
-.BR "rules and views"
-pg_dump does not understand user-defined rules and views and
-will fail to dump them properly.  (This is due to the fact that
-rules are stored as plans in the catalogs and not textually)
+.BR "rules"
+pg_dump does not understand user-defined rules and will fail
+to dump them properly.  (This is due to the fact that
+rules are stored as plans in the catalogs and not textually.)
 .TP
 .BR "partial indices"
 pg_dump does not understand partial indices. (The reason is