2121 *
2222 *
2323 * IDENTIFICATION
24- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.99 1999/01/18 06:32:26 momjian Exp $
24+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.100 1999/01/21 22:53:36 momjian Exp $
2525 *
2626 * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
2727 *
@@ -117,6 +117,7 @@ intattrNames;/* put attr names into insert strings */
117117int schemaOnly ;
118118int dataOnly ;
119119int aclsOption ;
120+ bool drop_schema ;
120121
121122char g_opaque_type [10 ];/* name for the opaque type */
122123
@@ -132,6 +133,8 @@ usage(const char *progname)
132133"usage: %s [options] dbname\n" ,progname );
133134fprintf (stderr ,
134135"\t -a \t\t dump out only the data, no schema\n" );
136+ fprintf (stderr ,
137+ "\t -c \t\t clean(drop) schema prior to create\n" );
135138fprintf (stderr ,
136139"\t -d \t\t dump data as proper insert strings\n" );
137140fprintf (stderr ,
@@ -556,6 +559,7 @@ main(int argc, char **argv)
556559
557560g_verbose = false;
558561force_quotes = true;
562+ drop_schema = false;
559563
560564strcpy (g_comment_start ,"-- " );
561565g_comment_end [0 ]= '\0' ;
@@ -565,13 +569,16 @@ main(int argc, char **argv)
565569
566570progname = * argv ;
567571
568- while ((c = getopt (argc ,argv ,"adDf :h:nNop:st:vzu" ))!= EOF )
572+ while ((c = getopt (argc ,argv ,"acdDf :h:nNop:st:vzu" ))!= EOF )
569573{
570574switch (c )
571575{
572576case 'a' :/* Dump data only */
573577dataOnly = 1 ;
574578break ;
579+ case 'c' :/* clean (i.e., drop) schema prior to create */
580+ drop_schema = true;
581+ break ;
575582case 'd' :/* dump data as proper insert strings */
576583dumpData = 1 ;
577584break ;
@@ -1638,6 +1645,18 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
16381645exit_nicely (g_conn );
16391646}
16401647tgfunc = finfo [findx ].proname ;
1648+
1649+ #if 0
1650+ /* XXX - how to emit this DROP TRIGGER? */
1651+ if (drop_schema )
1652+ {
1653+ sprintf (query ,"DROP TRIGGER %s ON %s;\n" ,
1654+ fmtId (PQgetvalue (res2 ,i2 ,i_tgname ),force_quotes ),
1655+ fmtId (tblinfo [i ].relname ,force_quotes ));
1656+ fputs (query ,fout );
1657+ }
1658+ #endif
1659+
16411660sprintf (query ,"CREATE TRIGGER %s " ,fmtId (PQgetvalue (res2 ,i2 ,i_tgname ),force_quotes ));
16421661/* Trigger type */
16431662findx = 0 ;
@@ -2034,6 +2053,12 @@ dumpTypes(FILE *fout, FuncInfo *finfo, int numFuncs,
20342053
20352054becomeUser (fout ,tinfo [i ].usename );
20362055
2056+ if (drop_schema )
2057+ {
2058+ sprintf (q ,"DROP TYPE %s;\n" ,fmtId (tinfo [i ].typname ,force_quotes ));
2059+ fputs (q ,fout );
2060+ }
2061+
20372062sprintf (q ,
20382063"CREATE TYPE %s "
20392064"( internallength = %s, externallength = %s, input = %s, "
@@ -2130,6 +2155,9 @@ dumpProcLangs(FILE *fout, FuncInfo *finfo, int numFuncs,
21302155lanname = checkForQuote (PQgetvalue (res ,i ,i_lanname ));
21312156lancompiler = checkForQuote (PQgetvalue (res ,i ,i_lancompiler ));
21322157
2158+ if (drop_schema )
2159+ fprintf (fout ,"DROP PROCEDURAL LANGUAGE '%s';\n" ,lanname );
2160+
21332161fprintf (fout ,"CREATE %sPROCEDURAL LANGUAGE '%s' "
21342162"HANDLER %s LANCOMPILER '%s';\n" ,
21352163(PQgetvalue (res ,i ,i_lanpltrusted )[0 ]== 't' ) ?"TRUSTED " :"" ,
@@ -2245,6 +2273,23 @@ dumpOneFunc(FILE *fout, FuncInfo *finfo, int i,
22452273PQclear (res );
22462274}
22472275
2276+ if (drop_schema )
2277+ {
2278+ sprintf (q ,"DROP FUNCTION %s (" ,fmtId (finfo [i ].proname ,force_quotes ));
2279+ for (j = 0 ;j < finfo [i ].nargs ;j ++ )
2280+ {
2281+ char * typname ;
2282+
2283+ typname = findTypeByOid (tinfo ,numTypes ,finfo [i ].argtypes [j ]);
2284+ sprintf (q ,"%s%s%s" ,
2285+ q ,
2286+ (j > 0 ) ?"," :"" ,
2287+ fmtId (typname , false));
2288+ }
2289+ sprintf (q ,"%s);\n" ,q );
2290+ fputs (q ,fout );
2291+ }
2292+
22482293sprintf (q ,"CREATE FUNCTION %s (" ,fmtId (finfo [i ].proname ,force_quotes ));
22492294for (j = 0 ;j < finfo [i ].nargs ;j ++ )
22502295{
@@ -2355,6 +2400,14 @@ dumpOprs(FILE *fout, OprInfo *oprinfo, int numOperators,
23552400
23562401becomeUser (fout ,oprinfo [i ].usename );
23572402
2403+ if (drop_schema )
2404+ {
2405+ sprintf (q ,"DROP OPERATOR %s (%s, %s);\n" ,oprinfo [i ].oprname ,
2406+ fmtId (findTypeByOid (tinfo ,numTypes ,oprinfo [i ].oprleft ), false),
2407+ fmtId (findTypeByOid (tinfo ,numTypes ,oprinfo [i ].oprright ), false));
2408+ fputs (q ,fout );
2409+ }
2410+
23582411sprintf (q ,
23592412"CREATE OPERATOR %s "
23602413"(PROCEDURE = %s %s %s %s %s %s %s %s %s);\n " ,
@@ -2450,6 +2503,13 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
24502503
24512504becomeUser (fout ,agginfo [i ].usename );
24522505
2506+ if (drop_schema )
2507+ {
2508+ sprintf (q ,"DROP AGGREGATE %s %s;\n" ,agginfo [i ].aggname ,
2509+ fmtId (findTypeByOid (tinfo ,numTypes ,agginfo [i ].aggbasetype ), false));
2510+ fputs (q ,fout );
2511+ }
2512+
24532513sprintf (q ,"CREATE AGGREGATE %s ( %s %s%s %s%s %s );\n" ,
24542514agginfo [i ].aggname ,
24552515basetype ,
@@ -2649,6 +2709,12 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
26492709
26502710becomeUser (fout ,tblinfo [i ].usename );
26512711
2712+ if (drop_schema )
2713+ {
2714+ sprintf (q ,"DROP TABLE %s;\n" ,fmtId (tblinfo [i ].relname ,force_quotes ));
2715+ fputs (q ,fout );
2716+ }
2717+
26522718sprintf (q ,"CREATE TABLE %s (\n\t" ,fmtId (tblinfo [i ].relname ,force_quotes ));
26532719actual_atts = 0 ;
26542720for (j = 0 ;j < tblinfo [i ].numatts ;j ++ )
@@ -2865,6 +2931,13 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
28652931
28662932strcpy (id1 ,fmtId (indinfo [i ].indexrelname ,force_quotes ));
28672933strcpy (id2 ,fmtId (indinfo [i ].indrelname ,force_quotes ));
2934+
2935+ if (drop_schema )
2936+ {
2937+ sprintf (q ,"DROP INDEX %s;\n" ,id1 );
2938+ fputs (q ,fout );
2939+ }
2940+
28682941fprintf (fout ,"CREATE %s INDEX %s on %s using %s (" ,
28692942 (strcmp (indinfo [i ].indisunique ,"t" )== 0 ) ?"UNIQUE" :"" ,
28702943id1 ,
@@ -3125,6 +3198,12 @@ dumpSequence(FILE *fout, TableInfo tbinfo)
31253198
31263199PQclear (res );
31273200
3201+ if (drop_schema )
3202+ {
3203+ sprintf (query ,"DROP SEQUENCE %s;\n" ,fmtId (tbinfo .relname ,force_quotes ));
3204+ fputs (query ,fout );
3205+ }
3206+
31283207sprintf (query ,
31293208"CREATE SEQUENCE %s start %d increment %d maxvalue %d "
31303209"minvalue %d cache %d %s;\n" ,