21
21
*
22
22
*
23
23
* 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 $
25
25
*
26
26
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
27
27
*
@@ -117,6 +117,7 @@ intattrNames;/* put attr names into insert strings */
117
117
int schemaOnly ;
118
118
int dataOnly ;
119
119
int aclsOption ;
120
+ bool drop_schema ;
120
121
121
122
char g_opaque_type [10 ];/* name for the opaque type */
122
123
@@ -132,6 +133,8 @@ usage(const char *progname)
132
133
"usage: %s [options] dbname\n" ,progname );
133
134
fprintf (stderr ,
134
135
"\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" );
135
138
fprintf (stderr ,
136
139
"\t -d \t\t dump data as proper insert strings\n" );
137
140
fprintf (stderr ,
@@ -556,6 +559,7 @@ main(int argc, char **argv)
556
559
557
560
g_verbose = false;
558
561
force_quotes = true;
562
+ drop_schema = false;
559
563
560
564
strcpy (g_comment_start ,"-- " );
561
565
g_comment_end [0 ]= '\0' ;
@@ -565,13 +569,16 @@ main(int argc, char **argv)
565
569
566
570
progname = * argv ;
567
571
568
- while ((c = getopt (argc ,argv ,"adDf :h:nNop:st:vzu" ))!= EOF )
572
+ while ((c = getopt (argc ,argv ,"acdDf :h:nNop:st:vzu" ))!= EOF )
569
573
{
570
574
switch (c )
571
575
{
572
576
case 'a' :/* Dump data only */
573
577
dataOnly = 1 ;
574
578
break ;
579
+ case 'c' :/* clean (i.e., drop) schema prior to create */
580
+ drop_schema = true;
581
+ break ;
575
582
case 'd' :/* dump data as proper insert strings */
576
583
dumpData = 1 ;
577
584
break ;
@@ -1638,6 +1645,18 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
1638
1645
exit_nicely (g_conn );
1639
1646
}
1640
1647
tgfunc = 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
+
1641
1660
sprintf (query ,"CREATE TRIGGER %s " ,fmtId (PQgetvalue (res2 ,i2 ,i_tgname ),force_quotes ));
1642
1661
/* Trigger type */
1643
1662
findx = 0 ;
@@ -2034,6 +2053,12 @@ dumpTypes(FILE *fout, FuncInfo *finfo, int numFuncs,
2034
2053
2035
2054
becomeUser (fout ,tinfo [i ].usename );
2036
2055
2056
+ if (drop_schema )
2057
+ {
2058
+ sprintf (q ,"DROP TYPE %s;\n" ,fmtId (tinfo [i ].typname ,force_quotes ));
2059
+ fputs (q ,fout );
2060
+ }
2061
+
2037
2062
sprintf (q ,
2038
2063
"CREATE TYPE %s "
2039
2064
"( internallength = %s, externallength = %s, input = %s, "
@@ -2130,6 +2155,9 @@ dumpProcLangs(FILE *fout, FuncInfo *finfo, int numFuncs,
2130
2155
lanname = checkForQuote (PQgetvalue (res ,i ,i_lanname ));
2131
2156
lancompiler = checkForQuote (PQgetvalue (res ,i ,i_lancompiler ));
2132
2157
2158
+ if (drop_schema )
2159
+ fprintf (fout ,"DROP PROCEDURAL LANGUAGE '%s';\n" ,lanname );
2160
+
2133
2161
fprintf (fout ,"CREATE %sPROCEDURAL LANGUAGE '%s' "
2134
2162
"HANDLER %s LANCOMPILER '%s';\n" ,
2135
2163
(PQgetvalue (res ,i ,i_lanpltrusted )[0 ]== 't' ) ?"TRUSTED " :"" ,
@@ -2245,6 +2273,23 @@ dumpOneFunc(FILE *fout, FuncInfo *finfo, int i,
2245
2273
PQclear (res );
2246
2274
}
2247
2275
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
+
2248
2293
sprintf (q ,"CREATE FUNCTION %s (" ,fmtId (finfo [i ].proname ,force_quotes ));
2249
2294
for (j = 0 ;j < finfo [i ].nargs ;j ++ )
2250
2295
{
@@ -2355,6 +2400,14 @@ dumpOprs(FILE *fout, OprInfo *oprinfo, int numOperators,
2355
2400
2356
2401
becomeUser (fout ,oprinfo [i ].usename );
2357
2402
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
+
2358
2411
sprintf (q ,
2359
2412
"CREATE OPERATOR %s "
2360
2413
"(PROCEDURE = %s %s %s %s %s %s %s %s %s);\n " ,
@@ -2450,6 +2503,13 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
2450
2503
2451
2504
becomeUser (fout ,agginfo [i ].usename );
2452
2505
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
+
2453
2513
sprintf (q ,"CREATE AGGREGATE %s ( %s %s%s %s%s %s );\n" ,
2454
2514
agginfo [i ].aggname ,
2455
2515
basetype ,
@@ -2649,6 +2709,12 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
2649
2709
2650
2710
becomeUser (fout ,tblinfo [i ].usename );
2651
2711
2712
+ if (drop_schema )
2713
+ {
2714
+ sprintf (q ,"DROP TABLE %s;\n" ,fmtId (tblinfo [i ].relname ,force_quotes ));
2715
+ fputs (q ,fout );
2716
+ }
2717
+
2652
2718
sprintf (q ,"CREATE TABLE %s (\n\t" ,fmtId (tblinfo [i ].relname ,force_quotes ));
2653
2719
actual_atts = 0 ;
2654
2720
for (j = 0 ;j < tblinfo [i ].numatts ;j ++ )
@@ -2865,6 +2931,13 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
2865
2931
2866
2932
strcpy (id1 ,fmtId (indinfo [i ].indexrelname ,force_quotes ));
2867
2933
strcpy (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
+
2868
2941
fprintf (fout ,"CREATE %s INDEX %s on %s using %s (" ,
2869
2942
(strcmp (indinfo [i ].indisunique ,"t" )== 0 ) ?"UNIQUE" :"" ,
2870
2943
id1 ,
@@ -3125,6 +3198,12 @@ dumpSequence(FILE *fout, TableInfo tbinfo)
3125
3198
3126
3199
PQclear (res );
3127
3200
3201
+ if (drop_schema )
3202
+ {
3203
+ sprintf (query ,"DROP SEQUENCE %s;\n" ,fmtId (tbinfo .relname ,force_quotes ));
3204
+ fputs (query ,fout );
3205
+ }
3206
+
3128
3207
sprintf (query ,
3129
3208
"CREATE SEQUENCE %s start %d increment %d maxvalue %d "
3130
3209
"minvalue %d cache %d %s;\n" ,