21
21
*
22
22
*
23
23
* IDENTIFICATION
24
- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.96 1998/12/05 22:09:57 tgl Exp $
24
+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.97 1998/12/13 23:41:32 thomas Exp $
25
25
*
26
26
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
27
27
*
@@ -101,12 +101,13 @@ extern int optind,
101
101
opterr ;
102
102
103
103
/* global decls */
104
- bool g_force_quotes ; /* User wants to suppress double-quotes */
105
104
bool g_verbose ; /* User wants verbose narration of our
106
105
* activities. */
107
106
int g_last_builtin_oid ; /* value of the last builtin oid */
108
107
FILE * g_fout ; /* the script file */
109
108
PGconn * g_conn ; /* the database connection */
109
+
110
+ bool force_quotes ; /* User wants to suppress double-quotes */
110
111
int dumpData ; /* dump data using proper insert strings */
111
112
int attrNames ; /* put attr names into insert strings */
112
113
int schemaOnly ;
@@ -228,14 +229,14 @@ dumpClasses_nodumpData(FILE *fout, const char *classname, const bool oids)
228
229
if (oids )
229
230
{
230
231
fprintf (fout , "COPY %s WITH OIDS FROM stdin;\n" ,
231
- fmtId (classname ));
232
+ fmtId (classname , force_quotes ));
232
233
sprintf (query , "COPY %s WITH OIDS TO stdout;\n" ,
233
- fmtId (classname ));
234
+ fmtId (classname , force_quotes ));
234
235
}
235
236
else
236
237
{
237
- fprintf (fout , "COPY %s FROM stdin;\n" , fmtId (classname ));
238
- sprintf (query , "COPY %s TO stdout;\n" , fmtId (classname ));
238
+ fprintf (fout , "COPY %s FROM stdin;\n" , fmtId (classname , force_quotes ));
239
+ sprintf (query , "COPY %s TO stdout;\n" , fmtId (classname , force_quotes ));
239
240
}
240
241
res = PQexec (g_conn , query );
241
242
if (!res ||
@@ -322,7 +323,7 @@ dumpClasses_dumpData(FILE *fout, const char *classname,
322
323
int tuple ;
323
324
int field ;
324
325
325
- sprintf (query , "SELECT * FROM %s" , fmtId (classname ));
326
+ sprintf (query , "SELECT * FROM %s" , fmtId (classname , force_quotes ));
326
327
res = PQexec (g_conn , query );
327
328
if (!res ||
328
329
PQresultStatus (res ) != PGRES_TUPLES_OK )
@@ -333,7 +334,7 @@ dumpClasses_dumpData(FILE *fout, const char *classname,
333
334
tuple = 0 ;
334
335
while (tuple < PQntuples (res ))
335
336
{
336
- fprintf (fout , "INSERT INTO %s " , fmtId (classname ));
337
+ fprintf (fout , "INSERT INTO %s " , fmtId (classname , force_quotes ));
337
338
if (attrNames )
338
339
{
339
340
int j ;
@@ -347,7 +348,7 @@ dumpClasses_dumpData(FILE *fout, const char *classname,
347
348
sprintf (q , "%s%s%s" ,
348
349
q ,
349
350
(actual_atts > 0 ) ? "," : "" ,
350
- fmtId (tblinfo .attnames [j ]));
351
+ fmtId (tblinfo .attnames [j ], force_quotes ));
351
352
actual_atts ++ ;
352
353
}
353
354
}
@@ -551,7 +552,7 @@ main(int argc, char **argv)
551
552
int use_password = 0 ;
552
553
553
554
g_verbose = false;
554
- g_force_quotes = true;
555
+ force_quotes = true;
555
556
556
557
strcpy (g_comment_start , "-- " );
557
558
g_comment_end [0 ] = '\0' ;
@@ -583,10 +584,10 @@ main(int argc, char **argv)
583
584
pghost = optarg ;
584
585
break ;
585
586
case 'n' : /* Do not force double-quotes on identifiers */
586
- g_force_quotes = false;
587
+ force_quotes = false;
587
588
break ;
588
589
case 'N' : /* Force double-quotes on identifiers */
589
- g_force_quotes = true;
590
+ force_quotes = true;
590
591
break ;
591
592
case 'o' : /* Dump oids */
592
593
oids = 1 ;
@@ -1555,7 +1556,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
1555
1556
1556
1557
query [0 ] = '\0' ;
1557
1558
if (name [0 ] != '$' )
1558
- sprintf (query , "CONSTRAINT %s " , fmtId (name ));
1559
+ sprintf (query , "CONSTRAINT %s " , fmtId (name , force_quotes ));
1559
1560
sprintf (query + strlen (query ), "CHECK (%s)" , expr );
1560
1561
tblinfo [i ].check_expr [i2 ] = strdup (query );
1561
1562
}
@@ -1630,7 +1631,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
1630
1631
exit_nicely (g_conn );
1631
1632
}
1632
1633
tgfunc = finfo [findx ].proname ;
1633
- sprintf (query , "CREATE TRIGGER %s " , fmtId (PQgetvalue (res2 , i2 , i_tgname )));
1634
+ sprintf (query , "CREATE TRIGGER %s " , fmtId (PQgetvalue (res2 , i2 , i_tgname ), force_quotes ));
1634
1635
/* Trigger type */
1635
1636
findx = 0 ;
1636
1637
if (TRIGGER_FOR_BEFORE (tgtype ))
@@ -1658,7 +1659,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
1658
1659
strcat (query , " UPDATE" );
1659
1660
}
1660
1661
sprintf (query , "%s ON %s FOR EACH ROW EXECUTE PROCEDURE %s (" ,
1661
- query , fmtId (tblinfo [i ].relname ), tgfunc );
1662
+ query , fmtId (tblinfo [i ].relname , force_quotes ), tgfunc );
1662
1663
for (findx = 0 ; findx < tgnargs ; findx ++ )
1663
1664
{
1664
1665
char * s ,
@@ -2030,7 +2031,7 @@ dumpTypes(FILE *fout, FuncInfo *finfo, int numFuncs,
2030
2031
"CREATE TYPE %s "
2031
2032
"( internallength = %s, externallength = %s, input = %s, "
2032
2033
"output = %s, send = %s, receive = %s, default = '%s'" ,
2033
- fmtId (tinfo [i ].typname ),
2034
+ fmtId (tinfo [i ].typname , force_quotes ),
2034
2035
tinfo [i ].typlen ,
2035
2036
tinfo [i ].typprtlen ,
2036
2037
tinfo [i ].typinput ,
@@ -2126,7 +2127,7 @@ dumpProcLangs(FILE *fout, FuncInfo *finfo, int numFuncs,
2126
2127
"HANDLER %s LANCOMPILER '%s';\n" ,
2127
2128
(PQgetvalue (res , i , i_lanpltrusted )[0 ] == 't' ) ? "TRUSTED " : "" ,
2128
2129
lanname ,
2129
- fmtId (finfo [fidx ].proname ),
2130
+ fmtId (finfo [fidx ].proname , force_quotes ),
2130
2131
lancompiler );
2131
2132
2132
2133
free (lanname );
@@ -2237,7 +2238,7 @@ dumpOneFunc(FILE *fout, FuncInfo *finfo, int i,
2237
2238
PQclear (res );
2238
2239
}
2239
2240
2240
- sprintf (q , "CREATE FUNCTION %s (" , fmtId (finfo [i ].proname ));
2241
+ sprintf (q , "CREATE FUNCTION %s (" , fmtId (finfo [i ].proname , force_quotes ));
2241
2242
for (j = 0 ; j < finfo [i ].nargs ; j ++ )
2242
2243
{
2243
2244
char * typname ;
@@ -2246,12 +2247,12 @@ dumpOneFunc(FILE *fout, FuncInfo *finfo, int i,
2246
2247
sprintf (q , "%s%s%s" ,
2247
2248
q ,
2248
2249
(j > 0 ) ? "," : "" ,
2249
- fmtId (typname ));
2250
+ fmtId (typname , false ));
2250
2251
}
2251
2252
sprintf (q , "%s ) RETURNS %s%s AS '%s' LANGUAGE '%s';\n" ,
2252
2253
q ,
2253
2254
(finfo [i ].retset ) ? " SETOF " : "" ,
2254
- fmtId (findTypeByOid (tinfo , numTypes , finfo [i ].prorettype )),
2255
+ fmtId (findTypeByOid (tinfo , numTypes , finfo [i ].prorettype ), false ),
2255
2256
func_def , func_lang );
2256
2257
2257
2258
fputs (q , fout );
@@ -2302,13 +2303,13 @@ dumpOprs(FILE *fout, OprInfo *oprinfo, int numOperators,
2302
2303
strcmp (oprinfo [i ].oprkind , "b" ) == 0 )
2303
2304
{
2304
2305
sprintf (leftarg , ", LEFTARG = %s " ,
2305
- fmtId (findTypeByOid (tinfo , numTypes , oprinfo [i ].oprleft )));
2306
+ fmtId (findTypeByOid (tinfo , numTypes , oprinfo [i ].oprleft ), false ));
2306
2307
}
2307
2308
if (strcmp (oprinfo [i ].oprkind , "l" ) == 0 ||
2308
2309
strcmp (oprinfo [i ].oprkind , "b" ) == 0 )
2309
2310
{
2310
2311
sprintf (rightarg , ", RIGHTARG = %s " ,
2311
- fmtId (findTypeByOid (tinfo , numTypes , oprinfo [i ].oprright )));
2312
+ fmtId (findTypeByOid (tinfo , numTypes , oprinfo [i ].oprright ), false ));
2312
2313
}
2313
2314
if (strcmp (oprinfo [i ].oprcom , "0" ) == 0 )
2314
2315
commutator [0 ] = '\0' ;
@@ -2391,7 +2392,7 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
2391
2392
2392
2393
sprintf (basetype ,
2393
2394
"BASETYPE = %s, " ,
2394
- fmtId (findTypeByOid (tinfo , numTypes , agginfo [i ].aggbasetype )));
2395
+ fmtId (findTypeByOid (tinfo , numTypes , agginfo [i ].aggbasetype ), false ));
2395
2396
2396
2397
if (strcmp (agginfo [i ].aggtransfn1 , "-" ) == 0 )
2397
2398
sfunc1 [0 ] = '\0' ;
@@ -2400,7 +2401,7 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
2400
2401
sprintf (sfunc1 ,
2401
2402
"SFUNC1 = %s, STYPE1 = %s" ,
2402
2403
agginfo [i ].aggtransfn1 ,
2403
- fmtId (findTypeByOid (tinfo , numTypes , agginfo [i ].aggtranstype1 )));
2404
+ fmtId (findTypeByOid (tinfo , numTypes , agginfo [i ].aggtranstype1 ), false ));
2404
2405
if (agginfo [i ].agginitval1 )
2405
2406
sprintf (sfunc1 , "%s, INITCOND1 = '%s'" ,
2406
2407
sfunc1 , agginfo [i ].agginitval1 );
@@ -2414,7 +2415,7 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
2414
2415
sprintf (sfunc2 ,
2415
2416
"SFUNC2 = %s, STYPE2 = %s" ,
2416
2417
agginfo [i ].aggtransfn2 ,
2417
- fmtId (findTypeByOid (tinfo , numTypes , agginfo [i ].aggtranstype2 )));
2418
+ fmtId (findTypeByOid (tinfo , numTypes , agginfo [i ].aggtranstype2 ), false ));
2418
2419
if (agginfo [i ].agginitval2 )
2419
2420
sprintf (sfunc2 , "%s, INITCOND2 = '%s'" ,
2420
2421
sfunc2 , agginfo [i ].agginitval2 );
@@ -2525,7 +2526,7 @@ dumpACL(FILE *fout, TableInfo tbinfo)
2525
2526
*/
2526
2527
fprintf (fout ,
2527
2528
"REVOKE ALL on %s from PUBLIC;\n" ,
2528
- fmtId (tbinfo .relname ));
2529
+ fmtId (tbinfo .relname , force_quotes ));
2529
2530
2530
2531
/* Make a working copy of acls so we can use strtok */
2531
2532
aclbuf = strdup (acls );
@@ -2556,7 +2557,7 @@ dumpACL(FILE *fout, TableInfo tbinfo)
2556
2557
{
2557
2558
fprintf (fout ,
2558
2559
"GRANT %s on %s to " ,
2559
- priv , fmtId (tbinfo .relname ));
2560
+ priv , fmtId (tbinfo .relname , force_quotes ));
2560
2561
/* Note: fmtId() can only be called once per printf, so don't
2561
2562
* try to merge printing of username into the above printf.
2562
2563
*/
@@ -2568,7 +2569,7 @@ dumpACL(FILE *fout, TableInfo tbinfo)
2568
2569
else
2569
2570
{
2570
2571
* eqpos = '\0' ; /* it's ok to clobber aclbuf */
2571
- fprintf (fout , "%s;\n" , fmtId (tok ));
2572
+ fprintf (fout , "%s;\n" , fmtId (tok , force_quotes ));
2572
2573
}
2573
2574
}
2574
2575
free (priv );
@@ -2630,7 +2631,7 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
2630
2631
2631
2632
becomeUser (fout , tblinfo [i ].usename );
2632
2633
2633
- sprintf (q , "CREATE TABLE %s (\n\t" , fmtId (tblinfo [i ].relname ));
2634
+ sprintf (q , "CREATE TABLE %s (\n\t" , fmtId (tblinfo [i ].relname , force_quotes ));
2634
2635
actual_atts = 0 ;
2635
2636
for (j = 0 ; j < tblinfo [i ].numatts ; j ++ )
2636
2637
{
@@ -2639,28 +2640,39 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
2639
2640
if (actual_atts > 0 )
2640
2641
strcat (q , ",\n\t" );
2641
2642
sprintf (q + strlen (q ), "%s " ,
2642
- fmtId (tblinfo [i ].attnames [j ]));
2643
+ fmtId (tblinfo [i ].attnames [j ], force_quotes ));
2643
2644
2644
2645
/* Show lengths on bpchar and varchar */
2645
2646
if (!strcmp (tblinfo [i ].typnames [j ], "bpchar" ))
2646
2647
{
2647
- sprintf (q + strlen (q ), "char(%d)" ,
2648
- tblinfo [i ].atttypmod [j ] - VARHDRSZ );
2648
+ int len = (tblinfo [i ].atttypmod [j ] - VARHDRSZ );
2649
+ sprintf (q + strlen (q ), "character" );
2650
+ if (len > 1 )
2651
+ sprintf (q + strlen (q ), "(%d)" ,
2652
+ tblinfo [i ].atttypmod [j ] - VARHDRSZ );
2649
2653
}
2650
2654
else if (!strcmp (tblinfo [i ].typnames [j ], "varchar" ))
2651
2655
{
2652
- sprintf (q + strlen (q ), "%s" ,
2653
- tblinfo [i ].typnames [j ]);
2656
+ sprintf (q + strlen (q ), "character varying" );
2654
2657
if (tblinfo [i ].atttypmod [j ] != -1 )
2655
2658
{
2656
2659
sprintf (q + strlen (q ), "(%d)" ,
2657
2660
tblinfo [i ].atttypmod [j ] - VARHDRSZ );
2658
2661
}
2659
2662
}
2663
+ /* char is an internal single-byte data type;
2664
+ * Let's make sure we force it through with quotes.
2665
+ * - thomas 1998-12-13
2666
+ */
2667
+ else if (!strcmp (tblinfo [i ].typnames [j ], "char" ))
2668
+ {
2669
+ sprintf (q + strlen (q ), "%s" ,
2670
+ fmtId (tblinfo [i ].typnames [j ], true));
2671
+ }
2660
2672
else
2661
2673
{
2662
2674
sprintf (q + strlen (q ), "%s" ,
2663
- fmtId (tblinfo [i ].typnames [j ]));
2675
+ fmtId (tblinfo [i ].typnames [j ], false ));
2664
2676
}
2665
2677
if (tblinfo [i ].adef_expr [j ] != NULL )
2666
2678
sprintf (q + strlen (q ), " DEFAULT %s" ,
@@ -2689,7 +2701,7 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
2689
2701
{
2690
2702
sprintf (q + strlen (q ), "%s%s" ,
2691
2703
(k > 0 ) ? ", " : "" ,
2692
- fmtId (parentRels [k ]));
2704
+ fmtId (parentRels [k ], force_quotes ));
2693
2705
}
2694
2706
strcat (q , ")" );
2695
2707
}
@@ -2807,7 +2819,7 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
2807
2819
attname = tblinfo [tableInd ].attnames [indkey ];
2808
2820
if (funcname )
2809
2821
sprintf (attlist + strlen (attlist ), "%s%s" ,
2810
- (k == 0 ) ? "" : ", " , fmtId (attname ));
2822
+ (k == 0 ) ? "" : ", " , fmtId (attname , force_quotes ));
2811
2823
else
2812
2824
{
2813
2825
if (k >= nclass )
@@ -2817,8 +2829,8 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
2817
2829
attname , indinfo [i ].indexrelname );
2818
2830
exit_nicely (g_conn );
2819
2831
}
2820
- strcpy (id1 , fmtId (attname ));
2821
- strcpy (id2 , fmtId (classname [k ]));
2832
+ strcpy (id1 , fmtId (attname , force_quotes ));
2833
+ strcpy (id2 , fmtId (classname [k ], force_quotes ));
2822
2834
sprintf (attlist + strlen (attlist ), "%s%s %s" ,
2823
2835
(k == 0 ) ? "" : ", " , id1 , id2 );
2824
2836
free (classname [k ]);
@@ -2833,8 +2845,8 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
2833
2845
*/
2834
2846
becomeUser (fout , tblinfo [tableInd ].usename );
2835
2847
2836
- strcpy (id1 , fmtId (indinfo [i ].indexrelname ));
2837
- strcpy (id2 , fmtId (indinfo [i ].indrelname ));
2848
+ strcpy (id1 , fmtId (indinfo [i ].indexrelname , force_quotes ));
2849
+ strcpy (id2 , fmtId (indinfo [i ].indrelname , force_quotes ));
2838
2850
fprintf (fout , "CREATE %s INDEX %s on %s using %s (" ,
2839
2851
(strcmp (indinfo [i ].indisunique , "t" ) == 0 ) ? "UNIQUE" : "" ,
2840
2852
id1 ,
@@ -2843,8 +2855,8 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
2843
2855
if (funcname )
2844
2856
{
2845
2857
/* need 2 printf's here cuz fmtId has static return area */
2846
- fprintf (fout , " %s" , fmtId (funcname ));
2847
- fprintf (fout , " (%s) %s );\n" , attlist , fmtId (classname [0 ]));
2858
+ fprintf (fout , " %s" , fmtId (funcname , false ));
2859
+ fprintf (fout , " (%s) %s );\n" , attlist , fmtId (classname [0 ], force_quotes ));
2848
2860
free (funcname );
2849
2861
free (classname [0 ]);
2850
2862
}
@@ -3058,7 +3070,7 @@ dumpSequence(FILE *fout, TableInfo tbinfo)
3058
3070
sprintf (query ,
3059
3071
"SELECT sequence_name, last_value, increment_by, max_value, "
3060
3072
"min_value, cache_value, is_cycled, is_called from %s" ,
3061
- fmtId (tbinfo .relname ));
3073
+ fmtId (tbinfo .relname , force_quotes ));
3062
3074
3063
3075
res = PQexec (g_conn , query );
3064
3076
if (!res || PQresultStatus (res ) != PGRES_TUPLES_OK )
@@ -3098,7 +3110,7 @@ dumpSequence(FILE *fout, TableInfo tbinfo)
3098
3110
sprintf (query ,
3099
3111
"CREATE SEQUENCE %s start %d increment %d maxvalue %d "
3100
3112
"minvalue %d cache %d %s;\n" ,
3101
- fmtId (tbinfo .relname ), last , incby , maxv , minv , cache ,
3113
+ fmtId (tbinfo .relname , force_quotes ), last , incby , maxv , minv , cache ,
3102
3114
(cycled == 't' ) ? "cycle" : "" );
3103
3115
3104
3116
fputs (query , fout );
0 commit comments