Commit 15526ff0 authored by Bryan Henderson's avatar Bryan Henderson

Add messages to assist in problem diagnosis. Eliminate lines > 80 characters.

parent 634b38aa
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.18 1996/11/26 07:38:19 bryanh Exp $ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.19 1996/12/27 23:12:54 bryanh Exp $
* *
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
* *
...@@ -64,7 +64,7 @@ extern char *optarg; ...@@ -64,7 +64,7 @@ extern char *optarg;
extern int optind, opterr; extern int optind, opterr;
/* global decls */ /* global decls */
int g_verbose; /* verbose flag */ bool g_verbose; /* User wants verbose narration of our activities. */
int g_last_builtin_oid; /* value of the last builtin oid */ int g_last_builtin_oid; /* value of the last builtin oid */
FILE *g_fout; /* the script file */ FILE *g_fout; /* the script file */
PGconn *g_conn; /* the database connection */ PGconn *g_conn; /* the database connection */
...@@ -81,19 +81,33 @@ char g_comment_end[10]; ...@@ -81,19 +81,33 @@ char g_comment_end[10];
static void static void
usage(const char* progname) usage(const char* progname)
{ {
fprintf(stderr, "%s - version 1.13.dhb.2\n\n",progname); fprintf(stderr,
fprintf(stderr, "usage: %s [options] [dbname]\n",progname); "%s - version 1.13.dhb.2\n\n",progname);
fprintf(stderr, "\t -f filename \t\t script output filename\n"); fprintf(stderr,
fprintf(stderr, "\t -H hostname \t\t server host name\n"); "usage: %s [options] [dbname]\n",progname);
fprintf(stderr, "\t -p port \t\t server port number\n"); fprintf(stderr,
fprintf(stderr, "\t -v \t\t verbose\n"); "\t -f filename \t\t script output filename\n");
fprintf(stderr, "\t -d \t\t dump data as proper insert strings\n"); fprintf(stderr,
fprintf(stderr, "\t -D \t\t dump data as inserts with attribute names\n"); "\t -H hostname \t\t server host name\n");
fprintf(stderr, "\t -S \t\t dump out only the schema, no data\n"); fprintf(stderr,
fprintf(stderr, "\t -a \t\t dump out only the data, no schema\n"); "\t -p port \t\t server port number\n");
fprintf(stderr, "\t -t table \t\t dump for this table only\n"); fprintf(stderr,
fprintf(stderr, "\t -o \t\t dump object id's (oids)\n"); "\t -v \t\t verbose\n");
fprintf(stderr, "\nIf dbname is not supplied, then the DATABASE environment name is used.\n"); fprintf(stderr,
"\t -d \t\t dump data as proper insert strings\n");
fprintf(stderr,
"\t -D \t\t dump data as inserts with attribute names\n");
fprintf(stderr,
"\t -S \t\t dump out only the schema, no data\n");
fprintf(stderr,
"\t -a \t\t dump out only the data, no schema\n");
fprintf(stderr,
"\t -t table \t\t dump for this table only\n");
fprintf(stderr,
"\t -o \t\t dump object id's (oids)\n");
fprintf(stderr,
"\nIf dbname is not supplied, then the DATABASE environment "
"variable value is used.\n");
fprintf(stderr, "\n"); fprintf(stderr, "\n");
exit(1); exit(1);
...@@ -107,6 +121,201 @@ exit_nicely(PGconn* conn) ...@@ -107,6 +121,201 @@ exit_nicely(PGconn* conn)
} }
#define COPYBUFSIZ 8192
static void
dumpClasses_nodumpData(FILE *fout, const char *classname, const bool oids) {
PGresult *res;
char query[255];
int ret;
bool copydone;
char copybuf[COPYBUFSIZ];
if (oids) {
fprintf(fout, "COPY %s WITH OIDS FROM stdin;\n",
classname);
sprintf(query, "COPY %s WITH OIDS TO stdout;\n",
classname);
} else {
fprintf(fout, "COPY %s FROM stdin;\n", classname);
sprintf(query, "COPY %s TO stdout;\n", classname);
}
res = PQexec(g_conn, query);
if (!res) {
fprintf(stderr, "SQL query to dump the contents of Table %s "
"did not execute. Explanation from backend: '%s'.\n"
"The query was: '%s'.\n",
classname, PQerrorMessage(g_conn), query);
exit_nicely(g_conn);
} else {
if (PQresultStatus(res) != PGRES_COPY_OUT) {
fprintf(stderr,"SQL query to dump the contents of Table %s "
"executed abnormally.\n"
"PQexec() returned status %d when %d was expected.\n"
"The query was: '%s'.\n",
classname, PQresultStatus(res), PGRES_COPY_OUT, query);
exit_nicely(g_conn);
} else {
copydone = false;
while (!copydone) {
ret = PQgetline(res->conn, copybuf, COPYBUFSIZ);
if (copybuf[0] == '\\' &&
copybuf[1] == '.' &&
copybuf[2] == '\0') {
copydone = true; /* don't print this... */
} else {
fputs(copybuf, fout);
switch (ret) {
case EOF:
copydone = true;
/*FALLTHROUGH*/
case 0:
fputc('\n', fout);
break;
case 1:
break;
}
}
}
fprintf(fout, "\\.\n");
}
PQclear(res);
PQendcopy(res->conn);
}
}
static void
dumpClasses_dumpData(FILE *fout, const char *classname,
const TableInfo tblinfo, bool oids) {
PGresult *res;
char query[255];
int actual_atts; /* number of attrs in this a table */
char expandbuf[COPYBUFSIZ];
char q[MAXQUERYLEN];
int tuple;
int field;
sprintf(query, "select * from %s;\n", classname);
res = PQexec(g_conn, query);
if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr,"dumpClasses(): command failed\n");
exit_nicely(g_conn);
}
tuple=0;
while(tuple < PQntuples(res)) {
fprintf(fout, "insert into %s ", classname);
if (attrNames) {
int j;
actual_atts = 0;
sprintf(q, "(");
for (j=0;j<tblinfo.numatts;j++) {
if (tblinfo.inhAttrs[j] == 0) {
sprintf(q, "%s%s%s",
q,
(actual_atts > 0) ? "," : "",
tblinfo.attnames[j]);
actual_atts++;
}
}
sprintf(q,"%s%s",q, ") ");
fprintf(fout, q);
}
fprintf(fout, "values (");
field=0;
do {
if (PQgetisnull(res,tuple,field)) {
fprintf(fout,"NULL");
} else {
switch(PQftype(res,field)) {
case 21: case 22: case 23: /* int types */
case 810: case 910: /* oldint types */
case 1005: case 1006: case 1007: /* _int types */
case 700: case 701: /* float types */
case 1021: case 1022: /* _float types */
fprintf(fout, "%s",
PQgetvalue(res,tuple,field));
break;
default: {
char *expsrc,*expdest;
/* Before outputting string value, expand all
single quotes to twin single quotes -
dhb - 6/11/96 */
expsrc=PQgetvalue(res,tuple,field);
expdest=expandbuf;
while (*expsrc) {
*expdest++=*expsrc;
if (*expsrc == (char)0x27) /*single quote*/
*expdest++ = *expsrc;
expsrc++;
}
*expdest=*expsrc; /* null term. */
fprintf(fout, "'%s'", expandbuf);
}
break;
}
}
field++;
if(field != PQnfields(res))
fprintf(fout, ",");
} while(field < PQnfields(res));
fprintf(fout, ");\n");
tuple++;
}
PQclear(res);
}
/*
* DumpClasses -
* dump the contents of all the classes.
*/
static void
dumpClasses(const TableInfo tblinfo[], const int numTables, FILE *fout,
const char *onlytable, const bool oids) {
int i;
char *all_only;
if (onlytable = NULL) all_only = "all";
else all_only = "one";
if (g_verbose)
fprintf(stderr, "%s dumping out the contents of %s of %d tables %s\n",
g_comment_start, all_only, numTables, g_comment_end);
for(i = 0; i < numTables; i++) {
const char *classname = tblinfo[i].relname;
if (!onlytable || (!strcmp(classname,onlytable))) {
if (g_verbose)
fprintf(stderr, "%s dumping out the contents of Table %s %s\n",
g_comment_start, classname, g_comment_end);
/* skip archive names*/
if (isArchiveName(classname))
continue;
if(!dumpData)
dumpClasses_nodumpData(fout, classname, oids);
else
dumpClasses_dumpData(fout, classname, tblinfo[i], oids);
}
}
}
int int
main(int argc, char** argv) main(int argc, char** argv)
{ {
...@@ -126,7 +335,7 @@ main(int argc, char** argv) ...@@ -126,7 +335,7 @@ main(int argc, char** argv)
dbname = NULL; dbname = NULL;
filename = NULL; filename = NULL;
tablename = NULL; tablename = NULL;
g_verbose = 0; g_verbose = false;
oids = 0; oids = 0;
strcpy(g_comment_start,"-- "); strcpy(g_comment_start,"-- ");
...@@ -149,7 +358,7 @@ main(int argc, char** argv) ...@@ -149,7 +358,7 @@ main(int argc, char** argv)
pgport = optarg; pgport = optarg;
break; break;
case 'v': /* verbose */ case 'v': /* verbose */
g_verbose = 1; g_verbose = true;
break; break;
case 'S': /* dump schema only */ case 'S': /* dump schema only */
schemaOnly = 1; schemaOnly = 1;
...@@ -182,7 +391,8 @@ main(int argc, char** argv) ...@@ -182,7 +391,8 @@ main(int argc, char** argv)
} else { } else {
g_fout = fopen(filename, "w"); g_fout = fopen(filename, "w");
if (g_fout == NULL) { if (g_fout == NULL) {
fprintf(stderr,"%s: could not open output file named %s for writing\n", fprintf(stderr,
"%s: could not open output file named %s for writing\n",
progname, filename); progname, filename);
exit(2); exit(2);
} }
...@@ -199,7 +409,7 @@ main(int argc, char** argv) ...@@ -199,7 +409,7 @@ main(int argc, char** argv)
/* check to see that the backend connection was successfully made */ /* check to see that the backend connection was successfully made */
if (PQstatus(g_conn) == CONNECTION_BAD) { if (PQstatus(g_conn) == CONNECTION_BAD) {
fprintf(stderr,"Connection to database '%s' failed.\n", dbname); fprintf(stderr,"Connection to database '%s' failed.\n", dbname);
fprintf(stderr,"%s",PQerrorMessage(g_conn)); fprintf(stderr,"%s\n",PQerrorMessage(g_conn));
exit_nicely(g_conn); exit_nicely(g_conn);
} }
...@@ -217,9 +427,6 @@ main(int argc, char** argv) ...@@ -217,9 +427,6 @@ main(int argc, char** argv)
tblinfo = dumpSchema(NULL, &numTables, tablename); tblinfo = dumpSchema(NULL, &numTables, tablename);
if (!schemaOnly) { if (!schemaOnly) {
if (g_verbose)
fprintf(stderr,"%s dumping out the contents of each table %s\n",
g_comment_start, g_comment_end);
dumpClasses(tblinfo, numTables, g_fout, tablename, oids); dumpClasses(tblinfo, numTables, g_fout, tablename, oids);
} }
...@@ -280,12 +487,14 @@ getTypes(int *numTypes) ...@@ -280,12 +487,14 @@ getTypes(int *numTypes)
/* we filter out the built-in types when /* we filter out the built-in types when
we dump out the types */ we dump out the types */
sprintf(query, "SELECT oid, typowner,typname, typlen, typprtlen, typinput, typoutput, typreceive, typsend, typelem, typdelim, typdefault, typrelid,typbyval from pg_type"); sprintf(query, "SELECT oid, typowner,typname, typlen, typprtlen, "
"typinput, typoutput, typreceive, typsend, typelem, typdelim, "
"typdefault, typrelid,typbyval from pg_type");
res = PQexec(g_conn,query); res = PQexec(g_conn,query);
if (!res || if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK) { PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr,"getTypes(): SELECT failed"); fprintf(stderr,"getTypes(): SELECT failed\n");
exit_nicely(g_conn); exit_nicely(g_conn);
} }
...@@ -390,12 +599,14 @@ getOperators(int *numOprs) ...@@ -390,12 +599,14 @@ getOperators(int *numOprs)
} }
PQclear(res); PQclear(res);
sprintf(query, "SELECT oid, oprname, oprkind, oprcode, oprleft, oprright, oprcom, oprnegate, oprrest, oprjoin, oprcanhash, oprlsortop, oprrsortop from pg_operator"); sprintf(query, "SELECT oid, oprname, oprkind, oprcode, oprleft, "
"oprright, oprcom, oprnegate, oprrest, oprjoin, oprcanhash, "
"oprlsortop, oprrsortop from pg_operator");
res = PQexec(g_conn, query); res = PQexec(g_conn, query);
if (!res || if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK) { PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr,"getOperators(): SELECT failed"); fprintf(stderr,"getOperators(): SELECT failed\n");
exit_nicely(g_conn); exit_nicely(g_conn);
} }
...@@ -482,12 +693,14 @@ getAggregates(int *numAggs) ...@@ -482,12 +693,14 @@ getAggregates(int *numAggs)
PQclear(res); PQclear(res);
sprintf(query, sprintf(query,
"SELECT oid, aggname, aggtransfn1, aggtransfn2, aggfinalfn, aggtranstype1, aggbasetype, aggtranstype2, agginitval1, agginitval2 from pg_aggregate;"); "SELECT oid, aggname, aggtransfn1, aggtransfn2, aggfinalfn, "
"aggtranstype1, aggbasetype, aggtranstype2, agginitval1, "
"agginitval2 from pg_aggregate;");
res = PQexec(g_conn, query); res = PQexec(g_conn, query);
if (!res || if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK) { PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr,"getAggregates(): SELECT failed"); fprintf(stderr,"getAggregates(): SELECT failed\n");
exit_nicely(g_conn); exit_nicely(g_conn);
} }
...@@ -567,13 +780,15 @@ getFuncs(int *numFuncs) ...@@ -567,13 +780,15 @@ getFuncs(int *numFuncs)
PQclear(res); PQclear(res);
sprintf(query, sprintf(query,
"SELECT oid, proname, proowner, prolang, pronargs, prorettype, proretset, proargtypes, prosrc, probin from pg_proc where oid > '%d'::oid", "SELECT oid, proname, proowner, prolang, pronargs, prorettype, "
"proretset, proargtypes, prosrc, probin from pg_proc "
"where oid > '%d'::oid",
g_last_builtin_oid); g_last_builtin_oid);
res = PQexec(g_conn, query); res = PQexec(g_conn, query);
if (!res || if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK) { PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr,"getFuncs(): SELECT failed"); fprintf(stderr,"getFuncs(): SELECT failed\n");
exit_nicely(g_conn); exit_nicely(g_conn);
} }
...@@ -657,12 +872,14 @@ getTables(int *numTables) ...@@ -657,12 +872,14 @@ getTables(int *numTables)
PQclear(res); PQclear(res);
sprintf(query, sprintf(query,
"SELECT oid, relname, relarch from pg_class where relkind = 'r' and relname !~ '^pg_' and relname !~ '^Xinv' order by oid;"); "SELECT oid, relname, relarch from pg_class "
"where relkind = 'r' and relname !~ '^pg_' "
"and relname !~ '^Xinv' order by oid;");
res = PQexec(g_conn, query); res = PQexec(g_conn, query);
if (!res || if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK) { PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr,"getTables(): SELECT failed"); fprintf(stderr,"getTables(): SELECT failed\n");
exit_nicely(g_conn); exit_nicely(g_conn);
} }
...@@ -725,7 +942,7 @@ getInherits(int *numInherits) ...@@ -725,7 +942,7 @@ getInherits(int *numInherits)
res = PQexec(g_conn, query); res = PQexec(g_conn, query);
if (!res || if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK) { PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr,"getInherits(): SELECT failed"); fprintf(stderr,"getInherits(): SELECT failed\n");
exit_nicely(g_conn); exit_nicely(g_conn);
} }
...@@ -786,11 +1003,15 @@ getTableAttrs(TableInfo* tblinfo, int numTables) ...@@ -786,11 +1003,15 @@ getTableAttrs(TableInfo* tblinfo, int numTables)
tblinfo[i].relname, tblinfo[i].relname,
g_comment_end); g_comment_end);
sprintf(q,"SELECT a.attnum, a.attname, t.typname, a.attlen from pg_attribute a, pg_type t where a.attrelid = '%s'::oid and a.atttypid = t.oid and a.attnum > 0 order by attnum",tblinfo[i].oid); sprintf(q,"SELECT a.attnum, a.attname, t.typname, a.attlen "
"from pg_attribute a, pg_type t "
"where a.attrelid = '%s'::oid and a.atttypid = t.oid "
"and a.attnum > 0 order by attnum",
tblinfo[i].oid);
res = PQexec(g_conn, q); res = PQexec(g_conn, q);
if (!res || if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK) { PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr,"getTableAttrs(): SELECT failed"); fprintf(stderr,"getTableAttrs(): SELECT failed\n");
exit_nicely(g_conn); exit_nicely(g_conn);
} }
...@@ -863,13 +1084,20 @@ getIndices(int *numIndices) ...@@ -863,13 +1084,20 @@ getIndices(int *numIndices)
PQclear(res); PQclear(res);
sprintf(query, sprintf(query,
"SELECT t1.relname as indexrelname, t2.relname as indrelname, i.indproc, i.indkey[0], o.opcname as indclassname, a.amname as indamname from pg_index i, pg_class t1, pg_class t2, pg_opclass o, pg_am a where t1.oid = i.indexrelid and t2.oid = i.indrelid and o.oid = i.indclass[0] and t1.relam = a.oid and i.indexrelid > '%d'::oid and t2.relname !~ '^pg_' and t1.relname !~ '^Xinx' ;", "SELECT t1.relname as indexrelname, t2.relname as indrelname, "
"i.indproc, i.indkey[0], o.opcname as indclassname, "
"a.amname as indamname from pg_index i, pg_class t1, "
"pg_class t2, pg_opclass o, pg_am a "
"where t1.oid = i.indexrelid and t2.oid = i.indrelid "
"and o.oid = i.indclass[0] and t1.relam = a.oid and "
"i.indexrelid > '%d'::oid and t2.relname !~ '^pg_' "
"and t1.relname !~ '^Xinx' ;",
g_last_builtin_oid); g_last_builtin_oid);
res = PQexec(g_conn, query); res = PQexec(g_conn, query);
if (!res || if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK) { PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr,"getIndices(): SELECT failed"); fprintf(stderr,"getIndices(): SELECT failed\n");
exit_nicely(g_conn); exit_nicely(g_conn);
} }
...@@ -939,7 +1167,9 @@ dumpTypes(FILE* fout, FuncInfo* finfo, int numFuncs, ...@@ -939,7 +1167,9 @@ dumpTypes(FILE* fout, FuncInfo* finfo, int numFuncs,
dumpOneFunc(fout,finfo,funcInd,tinfo,numTypes); dumpOneFunc(fout,finfo,funcInd,tinfo,numTypes);
sprintf(q, sprintf(q,
"CREATE TYPE %s ( internallength = %s, externallength = %s, input = %s, output = %s, send = %s, receive = %s, default = '%s'", "CREATE TYPE %s "
"( internallength = %s, externallength = %s, input = %s, "
"output = %s, send = %s, receive = %s, default = '%s'",
tinfo[i].typname, tinfo[i].typname,
tinfo[i].typlen, tinfo[i].typlen,
tinfo[i].typprtlen, tinfo[i].typprtlen,
...@@ -1100,7 +1330,8 @@ dumpOprs(FILE* fout, OprInfo* oprinfo, int numOperators, ...@@ -1100,7 +1330,8 @@ dumpOprs(FILE* fout, OprInfo* oprinfo, int numOperators,
} }
sprintf(q, sprintf(q,
"CREATE OPERATOR %s (PROCEDURE = %s %s %s %s %s %s %s %s %s);\n ", "CREATE OPERATOR %s "
"(PROCEDURE = %s %s %s %s %s %s %s %s %s);\n ",
oprinfo[i].oprname, oprinfo[i].oprname,
oprinfo[i].oprcode, oprinfo[i].oprcode,
leftarg, leftarg,
...@@ -1332,7 +1563,8 @@ dumpIndices(FILE* fout, IndInfo* indinfo, int numIndices, ...@@ -1332,7 +1563,8 @@ dumpIndices(FILE* fout, IndInfo* indinfo, int numIndices,
not all the funcs. We might not find what we want not all the funcs. We might not find what we want
by looking in FuncInfo**/ by looking in FuncInfo**/
sprintf(q, sprintf(q,
"SELECT proname from pg_proc where pg_proc.oid = '%s'::oid", "SELECT proname from pg_proc "
"where pg_proc.oid = '%s'::oid",
indinfo[i].indproc); indinfo[i].indproc);
res = PQexec(g_conn, q); res = PQexec(g_conn, q);
funcname = strdup(PQgetvalue(res, 0, funcname = strdup(PQgetvalue(res, 0,
...@@ -1359,150 +1591,6 @@ dumpIndices(FILE* fout, IndInfo* indinfo, int numIndices, ...@@ -1359,150 +1591,6 @@ dumpIndices(FILE* fout, IndInfo* indinfo, int numIndices,
} }
} }
/*
* DumpClasses -
* dump the contents of all the classes.
*/
void
dumpClasses(TableInfo *tblinfo, int numTables, FILE *fout, const char *onlytable, int oids)
{
char query[255];
#define COPYBUFSIZ 8192
char copybuf[COPYBUFSIZ];
char expandbuf[COPYBUFSIZ];
char *expsrc,*expdest;
char q[MAXQUERYLEN];
PGresult *res;
int i,j;
int actual_atts; /* number of attrs in this a table */
int ret;
int field;
int tuple;
int copydone;
for(i = 0; i < numTables; i++) {
char *classname = tblinfo[i].relname;
if (!onlytable || (!strcmp(classname,onlytable))) {
/* skip archive names*/
if (isArchiveName(classname))
continue;
if(!dumpData) {
if (oids) {
fprintf(fout, "COPY %s WITH OIDS FROM stdin;\n", classname);
sprintf(query, "COPY %s WITH OIDS TO stdout;\n", classname);
}
else {
fprintf(fout, "COPY %s FROM stdin;\n", classname);
sprintf(query, "COPY %s TO stdout;\n", classname);
}
res = PQexec(g_conn, query);
if (!res ||
PQresultStatus(res) != PGRES_COPY_OUT) {
fprintf(stderr,"dumpClasses(): COPY to stdout failed");
exit_nicely(g_conn);
}
copydone = 0;
while (!copydone) {
ret = PQgetline(res->conn, copybuf, COPYBUFSIZ);
if (copybuf[0] == '\\' &&
copybuf[1] == '.' &&
copybuf[2] == '\0') {
copydone = true; /* don't print this... */
} else {
fputs(copybuf, fout);
switch (ret) {
case EOF:
copydone = true;
/*FALLTHROUGH*/
case 0:
fputc('\n', fout);
break;
case 1:
break;
}
}
}
fprintf(fout, "\\.\n");
PQclear(res);
PQendcopy(res->conn);
} else {
sprintf(query, "select * from %s;\n", classname);
res = PQexec(g_conn, query);
if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr,"dumpClasses(): command failed");
exit_nicely(g_conn);
}
tuple=0;
while(tuple < PQntuples(res)) {
fprintf(fout, "insert into %s ", classname);
if (attrNames) {
actual_atts = 0;
sprintf(q, "(");
for (j=0;j<tblinfo[i].numatts;j++) {
if (tblinfo[i].inhAttrs[j] == 0) {
sprintf(q, "%s%s%s",
q,
(actual_atts > 0) ? "," : "",
tblinfo[i].attnames[j]);
actual_atts++;
}
}
sprintf(q,"%s%s",q, ") ");
fprintf(fout, q);
}
fprintf(fout, "values (");
field=0;
do {
if (PQgetisnull(res,tuple,field)) {
fprintf(fout,"NULL");
}
else {
switch(PQftype(res,field)) {
case 21: case 22: case 23: /* int types */
case 810: case 910: /* oldint types */
case 1005: case 1006: case 1007: /* _int types */
case 700: case 701: /* float types */
case 1021: case 1022: /* _float types */
fprintf(fout, "%s", PQgetvalue(res,tuple,field));
break;
default:
/* Before outputing string value, expand all
single quotes to twin single quotes -
dhb - 6/11/96 */
expsrc=PQgetvalue(res,tuple,field);
expdest=expandbuf;
while (*expsrc) {
*expdest++=*expsrc;
if (*expsrc == (char)0x27) /*sing. quote*/
*expdest++ = *expsrc;
expsrc++;
}
*expdest=*expsrc; /* null term. */
fprintf(fout, "'%s'", expandbuf);
break;
}
}
field++;
if(field != PQnfields(res))
fprintf(fout, ",");
} while(field < PQnfields(res));
fprintf(fout, ");\n");
tuple++;
}
PQclear(res);
}
}
}
}
/* /*
* dumpTuples -- * dumpTuples --
* prints out the tuples in ASCII representation. The output is a valid * prints out the tuples in ASCII representation. The output is a valid
...@@ -1623,12 +1711,13 @@ findLastBuiltinOid(void) ...@@ -1623,12 +1711,13 @@ findLastBuiltinOid(void)
"SELECT oid from pg_database where datname = 'template1';"); "SELECT oid from pg_database where datname = 'template1';");
if (res == NULL || if (res == NULL ||
PQresultStatus(res) != PGRES_TUPLES_OK) { PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr,"pg_dump error in finding the template1 database"); fprintf(stderr,"pg_dump error in finding the template1 database\n");
exit_nicely(g_conn); exit_nicely(g_conn);
} }
ntups = PQntuples(res); ntups = PQntuples(res);
if (ntups != 1) { if (ntups != 1) {
fprintf(stderr,"pg_dump: couldn't find the template1 database. You are really hosed\nGiving up\n"); fprintf(stderr,"pg_dump: couldn't find the template1 database. "
"You are really hosed.\nGiving up.\n");
exit_nicely(g_conn); exit_nicely(g_conn);
} }
last_oid = atoi(PQgetvalue(res, 0, PQfnumber(res, "oid"))); last_oid = atoi(PQgetvalue(res, 0, PQfnumber(res, "oid")));
......
...@@ -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.8 1996/11/26 03:19:49 bryanh Exp $ * $Id: pg_dump.h,v 1.9 1996/12/27 23:12:57 bryanh 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
* *
...@@ -122,7 +122,7 @@ typedef struct _oprInfo { ...@@ -122,7 +122,7 @@ typedef struct _oprInfo {
/* global decls */ /* global decls */
extern int g_verbose; /* verbose flag */ extern bool g_verbose; /* verbose flag */
extern int g_last_builtin_oid; /* value of the last builtin oid */ extern int g_last_builtin_oid; /* value of the last builtin oid */
extern FILE *g_fout; /* the script file */ extern FILE *g_fout; /* the script file */
...@@ -189,8 +189,6 @@ extern void dumpTables(FILE* fout, TableInfo* tbinfo, int numTables, ...@@ -189,8 +189,6 @@ extern void dumpTables(FILE* fout, TableInfo* tbinfo, int numTables,
extern void dumpIndices(FILE* fout, IndInfo* indinfo, int numIndices, extern void dumpIndices(FILE* fout, IndInfo* indinfo, int numIndices,
TableInfo* tbinfo, int numTables, const char *tablename); TableInfo* tbinfo, int numTables, const char *tablename);
extern void dumpClasses(TableInfo *tbinfo, int numTables, FILE *fout,
const char *tablename, int oids);
extern void dumpTuples(PGresult *res, FILE *fout, int *attrmap); extern void dumpTuples(PGresult *res, FILE *fout, int *attrmap);
extern void setMaxOid(FILE *fout); extern void setMaxOid(FILE *fout);
extern char* checkForQuote(const char* s); extern char* checkForQuote(const char* s);
......
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