Commit b7ed6f85 authored by Bruce Momjian's avatar Bruce Momjian

Hi all

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
parent e1ebac31
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
* *
* *
* IDENTIFICATION * 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 * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
* *
...@@ -1383,7 +1383,7 @@ getFuncs(int *numFuncs) ...@@ -1383,7 +1383,7 @@ getFuncs(int *numFuncs)
TableInfo * TableInfo *
getTables(int *numTables, FuncInfo *finfo, int numFuncs) getTables(int *numTables, FuncInfo *finfo, int numFuncs)
{ {
PGresult *res; PGresult *res, *viewres;
int ntups; int ntups;
int i; int i;
char query[MAXQUERYLEN]; char query[MAXQUERYLEN];
...@@ -1414,6 +1414,8 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs) ...@@ -1414,6 +1414,8 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
} }
PQclear(res); PQclear(res);
/* NOTE, when outer joins are here, change this query to get the
view definition all in one go. */
sprintf(query, sprintf(query,
"SELECT pg_class.oid, relname, relkind, relacl, usename, " "SELECT pg_class.oid, relname, relkind, relacl, usename, "
"relchecks, reltriggers " "relchecks, reltriggers "
...@@ -1454,6 +1456,39 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs) ...@@ -1454,6 +1456,39 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks)); tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks));
tblinfo[i].ntrig = atoi(PQgetvalue(res, i, i_reltriggers)); 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 */ /* Get CHECK constraints */
if (tblinfo[i].ncheck > 0) if (tblinfo[i].ncheck > 0)
{ {
...@@ -2468,95 +2503,102 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables, ...@@ -2468,95 +2503,102 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
if (!tablename || (!strcmp(tblinfo[i].relname, tablename))) if (!tablename || (!strcmp(tblinfo[i].relname, tablename)))
{ {
/* Skip VIEW relations */ /* Dump VIEW relations also !-) */
if (isViewRule(tblinfo[i].relname)) if (isViewRule(tblinfo[i].relname))
continue; {
becomeUser(fout, tblinfo[i].usename);
parentRels = tblinfo[i].parentRels;
numParents = tblinfo[i].numParents;
becomeUser(fout, tblinfo[i].usename);
sprintf(q, "CREATE TABLE %s (", fmtId(tblinfo[i].relname)); sprintf(q, "CREATE VIEW %s AS %s\n",
actual_atts = 0; fmtId(tblinfo[i].relname),
for (j = 0; j < tblinfo[i].numatts; j++) tblinfo[i].viewdef);
}
else
{ {
if (tblinfo[i].inhAttrs[j] == 0) parentRels = tblinfo[i].parentRels;
{ numParents = tblinfo[i].numParents;
/* Show lengths on bpchar and varchar */ becomeUser(fout, tblinfo[i].usename);
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)", sprintf(q, "CREATE TABLE %s (", fmtId(tblinfo[i].relname));
q, actual_atts = 0;
tblinfo[i].atttypmod[j] - VARHDRSZ); for (j = 0; j < tblinfo[i].numatts; j++)
actual_atts++; {
} if (tblinfo[i].inhAttrs[j] == 0)
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)", /* Show lengths on bpchar and varchar */
q, if (!strcmp(tblinfo[i].typnames[j], "bpchar"))
tblinfo[i].atttypmod[j] - VARHDRSZ); {
actual_atts++; sprintf(q, "%s%s%s char",
} q,
else (actual_atts > 0) ? ", " : "",
{ fmtId(tblinfo[i].attnames[j]));
strcpy(id1, fmtId(tblinfo[i].attnames[j]));
strcpy(id2, fmtId(tblinfo[i].typnames[j])); sprintf(q, "%s(%d)",
sprintf(q, "%s%s%s %s", q,
q, tblinfo[i].atttypmod[j] - VARHDRSZ);
(actual_atts > 0) ? ", " : "", actual_atts++;
id1, }
id2); else if (!strcmp(tblinfo[i].typnames[j], "varchar"))
actual_atts++; {
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 */ /* put the CONSTRAINTS inside the table def */
for (k = 0; k < tblinfo[i].ncheck; k++) 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++)
{ {
sprintf(q, "%s%s%s", sprintf(q, "%s%s %s",
q, q,
(k > 0) ? ", " : "", (actual_atts + k > 0) ? ", " : "",
fmtId(parentRels[k])); tblinfo[i].check_expr[k]);
} }
strcat(q, ")"); 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); fputs(q, fout);
if (acls) if (acls)
dumpACL(fout, tblinfo[i]); dumpACL(fout, tblinfo[i]);
} }
} }
} }
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* *
* Copyright (c) 1994, Regents of the University of California * 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 * Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2
* *
...@@ -66,6 +66,7 @@ typedef struct _tableInfo ...@@ -66,6 +66,7 @@ typedef struct _tableInfo
{ {
char *oid; char *oid;
char *relname; char *relname;
char *viewdef;
char *relacl; char *relacl;
bool sequence; bool sequence;
int numatts; /* number of attributes */ int numatts; /* number of attributes */
......
.\" This is -*-nroff-*- .\" This is -*-nroff-*-
.\" XXX standard disclaimer belongs here.... .\" 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 .TH PG_DUMP UNIX 7/15/98 PostgreSQL PostgreSQL
.SH NAME .SH NAME
pg_dump - dumps out a Postgres database into a script file pg_dump - dumps out a Postgres database into a script file
...@@ -112,10 +112,10 @@ The limitations mostly stem from ...@@ -112,10 +112,10 @@ The limitations mostly stem from
difficulty in extracting certain meta-information from the system difficulty in extracting certain meta-information from the system
catalogs. catalogs.
.TP .TP
.BR "rules and views" .BR "rules"
pg_dump does not understand user-defined rules and views and pg_dump does not understand user-defined rules and will fail
will fail to dump them properly. (This is due to the fact that to dump them properly. (This is due to the fact that
rules are stored as plans in the catalogs and not textually) rules are stored as plans in the catalogs and not textually.)
.TP .TP
.BR "partial indices" .BR "partial indices"
pg_dump does not understand partial indices. (The reason is pg_dump does not understand partial indices. (The reason is
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment