Allow pg_dump to preserve owners of all objects.

This commit is contained in:
Bruce Momjian 1997-06-20 02:20:26 +00:00
parent 0acf9c9b28
commit dd2c410616
2 changed files with 87 additions and 44 deletions

View File

@ -21,7 +21,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.32 1997/06/07 05:29:22 momjian Exp $ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.33 1997/06/20 02:20:17 momjian Exp $
* *
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
* *
@ -263,7 +263,7 @@ dumpClasses_dumpData(FILE *fout, const char *classname,
int tuple; int tuple;
int field; int field;
sprintf(query, "select * from %s;\n", classname); sprintf(query, "select * from %s", classname);
res = PQexec(g_conn, query); res = PQexec(g_conn, query);
if (!res || if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK) { PQresultStatus(res) != PGRES_TUPLES_OK) {
@ -546,6 +546,7 @@ getTypes(int *numTypes)
int i_typdefault; int i_typdefault;
int i_typrelid; int i_typrelid;
int i_typbyval; int i_typbyval;
int i_usename;
res = PQexec(g_conn, "begin"); res = PQexec(g_conn, "begin");
if (!res || if (!res ||
@ -561,9 +562,10 @@ 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, " sprintf(query, "SELECT pg_type.oid, typowner,typname, typlen, typprtlen, "
"typinput, typoutput, typreceive, typsend, typelem, typdelim, " "typinput, typoutput, typreceive, typsend, typelem, typdelim, "
"typdefault, typrelid,typbyval from pg_type"); "typdefault, typrelid,typbyval, usename from pg_type, pg_user "
"where typowner = usesysid");
res = PQexec(g_conn,query); res = PQexec(g_conn,query);
if (!res || if (!res ||
@ -590,6 +592,7 @@ getTypes(int *numTypes)
i_typdefault = PQfnumber(res,"typdefault"); i_typdefault = PQfnumber(res,"typdefault");
i_typrelid = PQfnumber(res,"typrelid"); i_typrelid = PQfnumber(res,"typrelid");
i_typbyval = PQfnumber(res,"typbyval"); i_typbyval = PQfnumber(res,"typbyval");
i_usename = PQfnumber(res,"usename");
for (i=0;i<ntups;i++) { for (i=0;i<ntups;i++) {
tinfo[i].oid = strdup(PQgetvalue(res,i,i_oid)); tinfo[i].oid = strdup(PQgetvalue(res,i,i_oid));
@ -605,6 +608,7 @@ getTypes(int *numTypes)
tinfo[i].typdelim = strdup(PQgetvalue(res,i,i_typdelim)); tinfo[i].typdelim = strdup(PQgetvalue(res,i,i_typdelim));
tinfo[i].typdefault = strdup(PQgetvalue(res,i,i_typdefault)); tinfo[i].typdefault = strdup(PQgetvalue(res,i,i_typdefault));
tinfo[i].typrelid = strdup(PQgetvalue(res,i,i_typrelid)); tinfo[i].typrelid = strdup(PQgetvalue(res,i,i_typrelid));
tinfo[i].usename = strdup(PQgetvalue(res,i,i_usename));
if (strcmp(PQgetvalue(res,i,i_typbyval), "f") == 0) if (strcmp(PQgetvalue(res,i,i_typbyval), "f") == 0)
tinfo[i].passedbyvalue = 0; tinfo[i].passedbyvalue = 0;
@ -662,6 +666,7 @@ getOperators(int *numOprs)
int i_oprcanhash; int i_oprcanhash;
int i_oprlsortop; int i_oprlsortop;
int i_oprrsortop; int i_oprrsortop;
int i_usename;
/* find all operators, including builtin operators, /* find all operators, including builtin operators,
filter out system-defined operators at dump-out time */ filter out system-defined operators at dump-out time */
@ -673,9 +678,11 @@ getOperators(int *numOprs)
} }
PQclear(res); PQclear(res);
sprintf(query, "SELECT oid, oprname, oprkind, oprcode, oprleft, " sprintf(query, "SELECT pg_operator.oid, oprname, oprkind, oprcode, "
"oprright, oprcom, oprnegate, oprrest, oprjoin, oprcanhash, " "oprleft, oprright, oprcom, oprnegate, oprrest, oprjoin, "
"oprlsortop, oprrsortop from pg_operator"); "oprcanhash, oprlsortop, oprrsortop, usename "
"from pg_operator, pg_user "
"where oprowner = usesysid");
res = PQexec(g_conn, query); res = PQexec(g_conn, query);
if (!res || if (!res ||
@ -702,6 +709,7 @@ getOperators(int *numOprs)
i_oprcanhash = PQfnumber(res,"oprcanhash"); i_oprcanhash = PQfnumber(res,"oprcanhash");
i_oprlsortop = PQfnumber(res,"oprlsortop"); i_oprlsortop = PQfnumber(res,"oprlsortop");
i_oprrsortop = PQfnumber(res,"oprrsortop"); i_oprrsortop = PQfnumber(res,"oprrsortop");
i_usename = PQfnumber(res,"usename");
for (i=0;i<ntups;i++) { for (i=0;i<ntups;i++) {
oprinfo[i].oid = strdup(PQgetvalue(res,i,i_oid)); oprinfo[i].oid = strdup(PQgetvalue(res,i,i_oid));
@ -717,6 +725,7 @@ getOperators(int *numOprs)
oprinfo[i].oprcanhash = strdup(PQgetvalue(res,i,i_oprcanhash)); oprinfo[i].oprcanhash = strdup(PQgetvalue(res,i,i_oprcanhash));
oprinfo[i].oprlsortop = strdup(PQgetvalue(res,i,i_oprlsortop)); oprinfo[i].oprlsortop = strdup(PQgetvalue(res,i,i_oprlsortop));
oprinfo[i].oprrsortop = strdup(PQgetvalue(res,i,i_oprrsortop)); oprinfo[i].oprrsortop = strdup(PQgetvalue(res,i,i_oprrsortop));
oprinfo[i].usename = strdup(PQgetvalue(res,i,i_usename));
} }
PQclear(res); PQclear(res);
@ -744,6 +753,7 @@ for(i=0;i<numTypes;++i) {
if(tp[i].typdelim) free(tp[i].typdelim); if(tp[i].typdelim) free(tp[i].typdelim);
if(tp[i].typdefault) free(tp[i].typdefault); if(tp[i].typdefault) free(tp[i].typdefault);
if(tp[i].typrelid) free(tp[i].typrelid); if(tp[i].typrelid) free(tp[i].typrelid);
if(tp[i].usename) free(tp[i].usename);
} }
free(tp); free(tp);
} }
@ -756,7 +766,7 @@ if(!fun) return;
for(i=0;i<numFuncs;++i) { for(i=0;i<numFuncs;++i) {
if(fun[i].oid) free(fun[i].oid); if(fun[i].oid) free(fun[i].oid);
if(fun[i].proname) free(fun[i].proname); if(fun[i].proname) free(fun[i].proname);
if(fun[i].proowner) free(fun[i].proowner); if(fun[i].usename) free(fun[i].usename);
for(a=0;a<8;++a) for(a=0;a<8;++a)
if(fun[i].argtypes[a]) free(fun[i].argtypes[a]); if(fun[i].argtypes[a]) free(fun[i].argtypes[a]);
if(fun[i].prorettype) free(fun[i].prorettype); if(fun[i].prorettype) free(fun[i].prorettype);
@ -783,6 +793,7 @@ for(i=0;i<numTables;++i) {
if(tblinfo[i].inhAttrs) free((int *)tblinfo[i].inhAttrs); if(tblinfo[i].inhAttrs) free((int *)tblinfo[i].inhAttrs);
if(tblinfo[i].attnames) free (tblinfo[i].attnames); if(tblinfo[i].attnames) free (tblinfo[i].attnames);
if(tblinfo[i].typnames) free (tblinfo[i].typnames); if(tblinfo[i].typnames) free (tblinfo[i].typnames);
if(tblinfo[i].usename) free (tblinfo[i].usename);
} }
free(tblinfo); free(tblinfo);
} }
@ -816,6 +827,7 @@ for(i=0;i<numOprs;++i) {
if(opr[i].oprcanhash) free(opr[i].oprcanhash); if(opr[i].oprcanhash) free(opr[i].oprcanhash);
if(opr[i].oprlsortop) free(opr[i].oprlsortop); if(opr[i].oprlsortop) free(opr[i].oprlsortop);
if(opr[i].oprrsortop) free(opr[i].oprrsortop); if(opr[i].oprrsortop) free(opr[i].oprrsortop);
if(opr[i].usename) free(opr[i].usename);
} }
free(opr); free(opr);
} }
@ -854,6 +866,7 @@ for(i=0;i<numArgs;++i) {
if(agginfo[i].aggtranstype2) free (agginfo[i].aggtranstype2); if(agginfo[i].aggtranstype2) free (agginfo[i].aggtranstype2);
if(agginfo[i].agginitval1) free (agginfo[i].agginitval1); if(agginfo[i].agginitval1) free (agginfo[i].agginitval1);
if(agginfo[i].agginitval2) free (agginfo[i].agginitval2); if(agginfo[i].agginitval2) free (agginfo[i].agginitval2);
if(agginfo[i].usename) free (agginfo[i].usename);
} }
free (agginfo); free (agginfo);
} }
@ -886,6 +899,7 @@ getAggregates(int *numAggs)
int i_aggtranstype2; int i_aggtranstype2;
int i_agginitval1; int i_agginitval1;
int i_agginitval2; int i_agginitval2;
int i_usename;
/* find all user-defined aggregates */ /* find all user-defined aggregates */
@ -898,9 +912,10 @@ getAggregates(int *numAggs)
PQclear(res); PQclear(res);
sprintf(query, sprintf(query,
"SELECT oid, aggname, aggtransfn1, aggtransfn2, aggfinalfn, " "SELECT pg_aggregate.oid, aggname, aggtransfn1, aggtransfn2, "
"aggtranstype1, aggbasetype, aggtranstype2, agginitval1, " "aggfinalfn, aggtranstype1, aggbasetype, aggtranstype2, "
"agginitval2 from pg_aggregate;"); "agginitval1, agginitval2, usename from pg_aggregate, pg_user "
"where aggowner = usesysid");
res = PQexec(g_conn, query); res = PQexec(g_conn, query);
if (!res || if (!res ||
@ -924,6 +939,7 @@ getAggregates(int *numAggs)
i_aggtranstype2 = PQfnumber(res,"aggtranstype2"); i_aggtranstype2 = PQfnumber(res,"aggtranstype2");
i_agginitval1 = PQfnumber(res,"agginitval1"); i_agginitval1 = PQfnumber(res,"agginitval1");
i_agginitval2 = PQfnumber(res,"agginitval2"); i_agginitval2 = PQfnumber(res,"agginitval2");
i_usename = PQfnumber(res,"usename");
for (i=0;i<ntups;i++) { for (i=0;i<ntups;i++) {
agginfo[i].oid = strdup(PQgetvalue(res,i,i_oid)); agginfo[i].oid = strdup(PQgetvalue(res,i,i_oid));
@ -936,6 +952,7 @@ getAggregates(int *numAggs)
agginfo[i].aggtranstype2 = strdup(PQgetvalue(res,i,i_aggtranstype2)); agginfo[i].aggtranstype2 = strdup(PQgetvalue(res,i,i_aggtranstype2));
agginfo[i].agginitval1 = strdup(PQgetvalue(res,i,i_agginitval1)); agginfo[i].agginitval1 = strdup(PQgetvalue(res,i,i_agginitval1));
agginfo[i].agginitval2 = strdup(PQgetvalue(res,i,i_agginitval2)); agginfo[i].agginitval2 = strdup(PQgetvalue(res,i,i_agginitval2));
agginfo[i].usename = strdup(PQgetvalue(res,i,i_usename));
} }
PQclear(res); PQclear(res);
@ -965,7 +982,6 @@ getFuncs(int *numFuncs)
int i_oid; int i_oid;
int i_proname; int i_proname;
int i_proowner;
int i_prolang; int i_prolang;
int i_pronargs; int i_pronargs;
int i_proargtypes; int i_proargtypes;
@ -973,6 +989,7 @@ getFuncs(int *numFuncs)
int i_proretset; int i_proretset;
int i_prosrc; int i_prosrc;
int i_probin; int i_probin;
int i_usename;
/* find all user-defined funcs */ /* find all user-defined funcs */
@ -985,9 +1002,10 @@ getFuncs(int *numFuncs)
PQclear(res); PQclear(res);
sprintf(query, sprintf(query,
"SELECT oid, proname, proowner, prolang, pronargs, prorettype, " "SELECT pg_proc.oid, proname, prolang, pronargs, prorettype, "
"proretset, proargtypes, prosrc, probin from pg_proc " "proretset, proargtypes, prosrc, probin, usename "
"where oid > '%d'::oid", "from pg_proc, pg_user "
"where pg_proc.oid > '%d'::oid and proowner = usesysid",
g_last_builtin_oid); g_last_builtin_oid);
res = PQexec(g_conn, query); res = PQexec(g_conn, query);
@ -1005,7 +1023,6 @@ getFuncs(int *numFuncs)
i_oid = PQfnumber(res,"oid"); i_oid = PQfnumber(res,"oid");
i_proname = PQfnumber(res,"proname"); i_proname = PQfnumber(res,"proname");
i_proowner = PQfnumber(res,"proowner");
i_prolang = PQfnumber(res,"prolang"); i_prolang = PQfnumber(res,"prolang");
i_pronargs = PQfnumber(res,"pronargs"); i_pronargs = PQfnumber(res,"pronargs");
i_proargtypes = PQfnumber(res,"proargtypes"); i_proargtypes = PQfnumber(res,"proargtypes");
@ -1013,11 +1030,11 @@ getFuncs(int *numFuncs)
i_proretset = PQfnumber(res,"proretset"); i_proretset = PQfnumber(res,"proretset");
i_prosrc = PQfnumber(res,"prosrc"); i_prosrc = PQfnumber(res,"prosrc");
i_probin = PQfnumber(res,"probin"); i_probin = PQfnumber(res,"probin");
i_usename = PQfnumber(res,"usename");
for (i=0;i<ntups;i++) { for (i=0;i<ntups;i++) {
finfo[i].oid = strdup(PQgetvalue(res,i,i_oid)); finfo[i].oid = strdup(PQgetvalue(res,i,i_oid));
finfo[i].proname = strdup(PQgetvalue(res,i,i_proname)); finfo[i].proname = strdup(PQgetvalue(res,i,i_proname));
finfo[i].proowner = strdup(PQgetvalue(res,i,i_proowner));
finfo[i].prosrc = checkForQuote(PQgetvalue(res,i,i_prosrc)); finfo[i].prosrc = checkForQuote(PQgetvalue(res,i,i_prosrc));
finfo[i].probin = strdup(PQgetvalue(res,i,i_probin)); finfo[i].probin = strdup(PQgetvalue(res,i,i_probin));
@ -1027,6 +1044,8 @@ getFuncs(int *numFuncs)
finfo[i].nargs = atoi(PQgetvalue(res,i,i_pronargs)); finfo[i].nargs = atoi(PQgetvalue(res,i,i_pronargs));
finfo[i].lang = (atoi(PQgetvalue(res,i,i_prolang)) == C_PROLANG_OID); finfo[i].lang = (atoi(PQgetvalue(res,i,i_prolang)) == C_PROLANG_OID);
finfo[i].usename = strdup(PQgetvalue(res,i,i_usename));
parseArgTypes(finfo[i].argtypes, PQgetvalue(res,i,i_proargtypes)); parseArgTypes(finfo[i].argtypes, PQgetvalue(res,i,i_proargtypes));
finfo[i].dumped = 0; finfo[i].dumped = 0;
@ -1063,6 +1082,7 @@ getTables(int *numTables)
int i_relarch; int i_relarch;
int i_relkind; int i_relkind;
int i_relacl; int i_relacl;
int i_usename;
/* find all the user-defined tables (no indices and no catalogs), /* find all the user-defined tables (no indices and no catalogs),
ordering by oid is important so that we always process the parent ordering by oid is important so that we always process the parent
@ -1079,9 +1099,11 @@ getTables(int *numTables)
PQclear(res); PQclear(res);
sprintf(query, sprintf(query,
"SELECT oid, relname, relarch, relkind, relacl from pg_class " "SELECT pg_class.oid, relname, relarch, relkind, relacl, usename "
"where (relkind = 'r' or relkind = 'S') and relname !~ '^pg_' " "from pg_class, pg_user "
"and relname !~ '^xin[xv][0-9]+' order by oid;"); "where relowner = usesysid and "
"(relkind = 'r' or relkind = 'S') and relname !~ '^pg_' "
"and relname !~ '^xin[xv][0-9]+' order by oid");
res = PQexec(g_conn, query); res = PQexec(g_conn, query);
if (!res || if (!res ||
@ -1101,6 +1123,7 @@ getTables(int *numTables)
i_relarch = PQfnumber(res,"relarch"); i_relarch = PQfnumber(res,"relarch");
i_relkind = PQfnumber(res,"relkind"); i_relkind = PQfnumber(res,"relkind");
i_relacl = PQfnumber(res,"relacl"); i_relacl = PQfnumber(res,"relacl");
i_usename = PQfnumber(res,"usename");
for (i=0;i<ntups;i++) { for (i=0;i<ntups;i++) {
tblinfo[i].oid = strdup(PQgetvalue(res,i,i_oid)); tblinfo[i].oid = strdup(PQgetvalue(res,i,i_oid));
@ -1108,6 +1131,7 @@ getTables(int *numTables)
tblinfo[i].relarch = strdup(PQgetvalue(res,i,i_relarch)); tblinfo[i].relarch = strdup(PQgetvalue(res,i,i_relarch));
tblinfo[i].relacl = strdup(PQgetvalue(res,i,i_relacl)); tblinfo[i].relacl = strdup(PQgetvalue(res,i,i_relacl));
tblinfo[i].sequence = (strcmp (PQgetvalue(res,i,i_relkind), "S") == 0); tblinfo[i].sequence = (strcmp (PQgetvalue(res,i,i_relkind), "S") == 0);
tblinfo[i].usename = strdup(PQgetvalue(res,i,i_usename));
} }
PQclear(res); PQclear(res);
@ -1305,7 +1329,7 @@ getIndices(int *numIndices)
"from pg_index i, pg_class t1, pg_class t2, pg_am a " "from pg_index i, pg_class t1, pg_class t2, pg_am a "
"where t1.oid = i.indexrelid and t2.oid = i.indrelid " "where t1.oid = i.indexrelid and t2.oid = i.indrelid "
"and t1.relam = a.oid and i.indexrelid > '%d'::oid " "and t1.relam = a.oid and i.indexrelid > '%d'::oid "
"and t2.relname !~ '^pg_' and t1.relname !~ '^Xinx' ;", "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);
@ -1384,6 +1408,8 @@ dumpTypes(FILE* fout, FuncInfo* finfo, int numFuncs,
if (funcInd != -1) if (funcInd != -1)
dumpOneFunc(fout,finfo,funcInd,tinfo,numTypes); dumpOneFunc(fout,finfo,funcInd,tinfo,numTypes);
fprintf(fout,"\\connect - %s\n",tinfo[i].usename);
sprintf(q, sprintf(q,
"CREATE TYPE %s " "CREATE TYPE %s "
"( internallength = %s, externallength = %s, input = %s, " "( internallength = %s, externallength = %s, input = %s, "
@ -1448,6 +1474,8 @@ dumpOneFunc(FILE* fout, FuncInfo* finfo, int i,
else else
finfo[i].dumped = 1; finfo[i].dumped = 1;
fprintf(fout,"\\connect - %s\n",finfo[i].usename);
sprintf(q,"CREATE FUNCTION %s (",finfo[i].proname); sprintf(q,"CREATE FUNCTION %s (",finfo[i].proname);
for (j=0;j<finfo[i].nargs;j++) { for (j=0;j<finfo[i].nargs;j++) {
char* typname; char* typname;
@ -1547,6 +1575,8 @@ dumpOprs(FILE* fout, OprInfo* oprinfo, int numOperators,
oprinfo[i].oprlsortop)); oprinfo[i].oprlsortop));
} }
fprintf(fout,"\\connect - %s\n", oprinfo[i].usename);
sprintf(q, sprintf(q,
"CREATE OPERATOR %s " "CREATE OPERATOR %s "
"(PROCEDURE = %s %s %s %s %s %s %s %s %s);\n ", "(PROCEDURE = %s %s %s %s %s %s %s %s %s);\n ",
@ -1627,6 +1657,8 @@ dumpAggs(FILE* fout, AggInfo* agginfo, int numAggs,
} else } else
comma2[0] = '\0'; comma2[0] = '\0';
fprintf(fout,"\\connect - %s\n", agginfo[i].usename);
sprintf(q,"CREATE AGGREGATE %s ( %s %s %s %s %s );\n", sprintf(q,"CREATE AGGREGATE %s ( %s %s %s %s %s );\n",
agginfo[i].aggname, agginfo[i].aggname,
sfunc1, sfunc1,
@ -1677,6 +1709,8 @@ void dumpTables(FILE* fout, TableInfo *tblinfo, int numTables,
parentRels = tblinfo[i].parentRels; parentRels = tblinfo[i].parentRels;
numParents = tblinfo[i].numParents; numParents = tblinfo[i].numParents;
fprintf(fout, "\\connect - %s\n", tblinfo[i].usename);
sprintf(q, "CREATE TABLE %s (", tblinfo[i].relname); sprintf(q, "CREATE TABLE %s (", tblinfo[i].relname);
actual_atts = 0; actual_atts = 0;
for (j=0;j<tblinfo[i].numatts;j++) { for (j=0;j<tblinfo[i].numatts;j++) {
@ -2006,7 +2040,7 @@ findLastBuiltinOid(void)
int last_oid; int last_oid;
res = PQexec(g_conn, res = PQexec(g_conn,
"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\n"); fprintf(stderr,"pg_dump error in finding the template1 database\n");
@ -2066,7 +2100,7 @@ static void dumpSequence (FILE* fout, TableInfo tbinfo)
sprintf (query, sprintf (query,
"SELECT sequence_name, last_value, increment_by, max_value, " "SELECT sequence_name, last_value, increment_by, max_value, "
"min_value, cache_value, is_cycled, is_called from %s;", "min_value, cache_value, is_cycled, is_called from %s",
tbinfo.relname); tbinfo.relname);
res = PQexec (g_conn, query); res = PQexec (g_conn, query);

View File

@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.73 1997/06/11 01:03:38 scrappy Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.74 1997/06/20 02:20:26 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -153,10 +153,12 @@ on(bool f)
static void static void
slashUsage(PsqlSettings * ps) slashUsage(PsqlSettings * ps)
{ {
int ch;
fprintf(stderr, " \\? -- help\n"); fprintf(stderr, " \\? -- help\n");
fprintf(stderr, " \\a -- toggle field-alignment (currenty %s)\n", on(ps->opt.align)); fprintf(stderr, " \\a -- toggle field-alignment (currenty %s)\n", on(ps->opt.align));
fprintf(stderr, " \\C [<captn>] -- set html3 caption (currently '%s')\n", ps->opt.caption ? ps->opt.caption : ""); fprintf(stderr, " \\C [<captn>] -- set html3 caption (currently '%s')\n", ps->opt.caption ? ps->opt.caption : "");
fprintf(stderr, " \\connect <dbname> <user> -- connect to new database (currently '%s')\n", PQdb(ps->db)); fprintf(stderr, " \\connect <dbname|-> <user> -- connect to new database (currently '%s')\n", PQdb(ps->db));
fprintf(stderr, " \\copy table {from | to} <fname>\n"); fprintf(stderr, " \\copy table {from | to} <fname>\n");
fprintf(stderr, " \\d [<table>] -- list tables and indicies in database or columns in <table>, * for all\n"); fprintf(stderr, " \\d [<table>] -- list tables and indicies in database or columns in <table>, * for all\n");
fprintf(stderr, " \\di -- list only indicies in database\n"); fprintf(stderr, " \\di -- list only indicies in database\n");
@ -172,6 +174,11 @@ slashUsage(PsqlSettings * ps)
fprintf(stderr, " \\m -- toggle monitor-like table display (currently %s)\n", on(ps->opt.standard)); fprintf(stderr, " \\m -- toggle monitor-like table display (currently %s)\n", on(ps->opt.standard));
fprintf(stderr, " \\o [<fname>] [|<cmd>] -- send all query results to stdout, <fname>, or pipe\n"); fprintf(stderr, " \\o [<fname>] [|<cmd>] -- send all query results to stdout, <fname>, or pipe\n");
fprintf(stderr, " \\p -- print the current query buffer\n"); fprintf(stderr, " \\p -- print the current query buffer\n");
fprintf(stderr, "Press ENTER to continue");
/* eat up any extra characters typed before ENTER */
while ((ch = fgetc(stdin)) != '\r' && ch != '\n')
;
fprintf(stderr, " \\q -- quit\n"); fprintf(stderr, " \\q -- quit\n");
fprintf(stderr, " \\r -- reset(clear) the query buffer\n"); fprintf(stderr, " \\r -- reset(clear) the query buffer\n");
fprintf(stderr, " \\s [<fname>] -- print history or save it in <fname>\n"); fprintf(stderr, " \\s [<fname>] -- print history or save it in <fname>\n");
@ -858,18 +865,16 @@ do_copy(const char *args, PsqlSettings * settings)
static void static void
do_connect(const char *new_dbname, do_connect(const char *new_dbname,
const char *new_user, const char *new_user,
PsqlSettings * settings) PsqlSettings *settings)
{ {
char *dbname = PQdb(settings->db);
if (!new_dbname) if (!new_dbname)
fprintf(stderr, "\\connect must be followed by a database name\n"); fprintf(stderr, "\\connect must be followed by a database name\n");
else { else {
PGconn *olddb = settings->db; PGconn *olddb = settings->db;
static char *userenv = NULL; static char *userenv = NULL;
char *old_userenv = NULL; char *old_userenv = NULL;
const char *dbparam;
printf("closing connection to database: %s\n", dbname);
if (new_user != NULL) { if (new_user != NULL) {
/* /*
PQsetdb() does not allow us to specify the user, PQsetdb() does not allow us to specify the user,
@ -885,23 +890,27 @@ do_connect(const char *new_dbname,
if (old_userenv != NULL) if (old_userenv != NULL)
free(old_userenv); free(old_userenv);
} }
if (strcmp(new_dbname,"-") != 0)
dbparam = new_dbname;
else dbparam = PQdb(olddb);
settings->db = PQsetdb(PQhost(olddb), PQport(olddb), settings->db = PQsetdb(PQhost(olddb), PQport(olddb),
NULL, NULL, new_dbname); NULL, NULL, dbparam);
if (!settings->quiet) {
if (!new_user) if (!new_user)
printf("connecting to new database: %s\n", new_dbname); printf("connecting to new database: %s\n", dbparam);
else if (dbparam != new_dbname)
printf("connecting as new user: %s\n", new_user);
else else
printf("connecting to new database: %s as user: %s\n", printf("connecting to new database: %s as user: %s\n",
new_dbname,new_user); dbparam,new_user);
}
if (PQstatus(settings->db) == CONNECTION_BAD) { if (PQstatus(settings->db) == CONNECTION_BAD) {
fprintf(stderr, "%s\n", PQerrorMessage(settings->db)); fprintf(stderr, "%s\n", PQerrorMessage(settings->db));
printf("reconnecting to %s\n", dbname); fprintf(stderr,"Could not connect to new database. exiting\n");
settings->db = PQsetdb(PQhost(olddb), PQport(olddb),
NULL, NULL, dbname);
if (PQstatus(settings->db) == CONNECTION_BAD) {
fprintf(stderr,
"could not reconnect to %s. exiting\n", dbname);
exit(2); exit(2);
}
} else { } else {
PQfinish(olddb); PQfinish(olddb);
free(settings->prompt); free(settings->prompt);