2121 *
2222 *
2323 * 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 $
2525 *
2626 * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
2727 *
@@ -101,12 +101,13 @@ extern intoptind,
101101opterr ;
102102
103103/* global decls */
104- bool g_force_quotes ;/* User wants to suppress double-quotes */
105104bool g_verbose ;/* User wants verbose narration of our
106105 * activities. */
107106int g_last_builtin_oid ;/* value of the last builtin oid */
108107FILE * g_fout ;/* the script file */
109108PGconn * g_conn ;/* the database connection */
109+
110+ bool force_quotes ;/* User wants to suppress double-quotes */
110111int dumpData ;/* dump data using proper insert strings */
111112int attrNames ;/* put attr names into insert strings */
112113int schemaOnly ;
@@ -228,14 +229,14 @@ dumpClasses_nodumpData(FILE *fout, const char *classname, const bool oids)
228229if (oids )
229230{
230231fprintf (fout ,"COPY %s WITH OIDS FROM stdin;\n" ,
231- fmtId (classname ));
232+ fmtId (classname , force_quotes ));
232233sprintf (query ,"COPY %s WITH OIDS TO stdout;\n" ,
233- fmtId (classname ));
234+ fmtId (classname , force_quotes ));
234235}
235236else
236237{
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 ));
239240}
240241res = PQexec (g_conn ,query );
241242if (!res ||
@@ -322,7 +323,7 @@ dumpClasses_dumpData(FILE *fout, const char *classname,
322323int tuple ;
323324int field ;
324325
325- sprintf (query ,"SELECT * FROM %s" ,fmtId (classname ));
326+ sprintf (query ,"SELECT * FROM %s" ,fmtId (classname , force_quotes ));
326327res = PQexec (g_conn ,query );
327328if (!res ||
328329PQresultStatus (res )!= PGRES_TUPLES_OK )
@@ -333,7 +334,7 @@ dumpClasses_dumpData(FILE *fout, const char *classname,
333334tuple = 0 ;
334335while (tuple < PQntuples (res ))
335336{
336- fprintf (fout ,"INSERT INTO %s " ,fmtId (classname ));
337+ fprintf (fout ,"INSERT INTO %s " ,fmtId (classname , force_quotes ));
337338if (attrNames )
338339{
339340int j ;
@@ -347,7 +348,7 @@ dumpClasses_dumpData(FILE *fout, const char *classname,
347348sprintf (q ,"%s%s%s" ,
348349q ,
349350(actual_atts > 0 ) ?"," :"" ,
350- fmtId (tblinfo .attnames [j ]));
351+ fmtId (tblinfo .attnames [j ], force_quotes ));
351352actual_atts ++ ;
352353}
353354}
@@ -551,7 +552,7 @@ main(int argc, char **argv)
551552int use_password = 0 ;
552553
553554g_verbose = false;
554- g_force_quotes = true;
555+ force_quotes = true;
555556
556557strcpy (g_comment_start ,"-- " );
557558g_comment_end [0 ]= '\0' ;
@@ -583,10 +584,10 @@ main(int argc, char **argv)
583584pghost = optarg ;
584585break ;
585586case 'n' :/* Do not force double-quotes on identifiers */
586- g_force_quotes = false;
587+ force_quotes = false;
587588break ;
588589case 'N' :/* Force double-quotes on identifiers */
589- g_force_quotes = true;
590+ force_quotes = true;
590591break ;
591592case 'o' :/* Dump oids */
592593oids = 1 ;
@@ -1555,7 +1556,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
15551556
15561557query [0 ]= '\0' ;
15571558if (name [0 ]!= '$' )
1558- sprintf (query ,"CONSTRAINT %s " ,fmtId (name ));
1559+ sprintf (query ,"CONSTRAINT %s " ,fmtId (name , force_quotes ));
15591560sprintf (query + strlen (query ),"CHECK (%s)" ,expr );
15601561tblinfo [i ].check_expr [i2 ]= strdup (query );
15611562}
@@ -1630,7 +1631,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
16301631exit_nicely (g_conn );
16311632}
16321633tgfunc = 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 ));
16341635/* Trigger type */
16351636findx = 0 ;
16361637if (TRIGGER_FOR_BEFORE (tgtype ))
@@ -1658,7 +1659,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
16581659strcat (query ," UPDATE" );
16591660}
16601661sprintf (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 );
16621663for (findx = 0 ;findx < tgnargs ;findx ++ )
16631664{
16641665char * s ,
@@ -2030,7 +2031,7 @@ dumpTypes(FILE *fout, FuncInfo *finfo, int numFuncs,
20302031"CREATE TYPE %s "
20312032"( internallength = %s, externallength = %s, input = %s, "
20322033"output = %s, send = %s, receive = %s, default = '%s'" ,
2033- fmtId (tinfo [i ].typname ),
2034+ fmtId (tinfo [i ].typname , force_quotes ),
20342035tinfo [i ].typlen ,
20352036tinfo [i ].typprtlen ,
20362037tinfo [i ].typinput ,
@@ -2126,7 +2127,7 @@ dumpProcLangs(FILE *fout, FuncInfo *finfo, int numFuncs,
21262127"HANDLER %s LANCOMPILER '%s';\n" ,
21272128(PQgetvalue (res ,i ,i_lanpltrusted )[0 ]== 't' ) ?"TRUSTED " :"" ,
21282129lanname ,
2129- fmtId (finfo [fidx ].proname ),
2130+ fmtId (finfo [fidx ].proname , force_quotes ),
21302131lancompiler );
21312132
21322133free (lanname );
@@ -2237,7 +2238,7 @@ dumpOneFunc(FILE *fout, FuncInfo *finfo, int i,
22372238PQclear (res );
22382239}
22392240
2240- sprintf (q ,"CREATE FUNCTION %s (" ,fmtId (finfo [i ].proname ));
2241+ sprintf (q ,"CREATE FUNCTION %s (" ,fmtId (finfo [i ].proname , force_quotes ));
22412242for (j = 0 ;j < finfo [i ].nargs ;j ++ )
22422243{
22432244char * typname ;
@@ -2246,12 +2247,12 @@ dumpOneFunc(FILE *fout, FuncInfo *finfo, int i,
22462247sprintf (q ,"%s%s%s" ,
22472248q ,
22482249(j > 0 ) ?"," :"" ,
2249- fmtId (typname ));
2250+ fmtId (typname , false ));
22502251}
22512252sprintf (q ,"%s ) RETURNS %s%s AS '%s' LANGUAGE '%s';\n" ,
22522253q ,
22532254(finfo [i ].retset ) ?" SETOF " :"" ,
2254- fmtId (findTypeByOid (tinfo ,numTypes ,finfo [i ].prorettype )),
2255+ fmtId (findTypeByOid (tinfo ,numTypes ,finfo [i ].prorettype ), false ),
22552256func_def ,func_lang );
22562257
22572258fputs (q ,fout );
@@ -2302,13 +2303,13 @@ dumpOprs(FILE *fout, OprInfo *oprinfo, int numOperators,
23022303strcmp (oprinfo [i ].oprkind ,"b" )== 0 )
23032304{
23042305sprintf (leftarg ,", LEFTARG = %s " ,
2305- fmtId (findTypeByOid (tinfo ,numTypes ,oprinfo [i ].oprleft )));
2306+ fmtId (findTypeByOid (tinfo ,numTypes ,oprinfo [i ].oprleft ), false ));
23062307}
23072308if (strcmp (oprinfo [i ].oprkind ,"l" )== 0 ||
23082309strcmp (oprinfo [i ].oprkind ,"b" )== 0 )
23092310{
23102311sprintf (rightarg ,", RIGHTARG = %s " ,
2311- fmtId (findTypeByOid (tinfo ,numTypes ,oprinfo [i ].oprright )));
2312+ fmtId (findTypeByOid (tinfo ,numTypes ,oprinfo [i ].oprright ), false ));
23122313}
23132314if (strcmp (oprinfo [i ].oprcom ,"0" )== 0 )
23142315commutator [0 ]= '\0' ;
@@ -2391,7 +2392,7 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
23912392
23922393sprintf (basetype ,
23932394"BASETYPE = %s, " ,
2394- fmtId (findTypeByOid (tinfo ,numTypes ,agginfo [i ].aggbasetype )));
2395+ fmtId (findTypeByOid (tinfo ,numTypes ,agginfo [i ].aggbasetype ), false ));
23952396
23962397if (strcmp (agginfo [i ].aggtransfn1 ,"-" )== 0 )
23972398sfunc1 [0 ]= '\0' ;
@@ -2400,7 +2401,7 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
24002401sprintf (sfunc1 ,
24012402"SFUNC1 = %s, STYPE1 = %s" ,
24022403agginfo [i ].aggtransfn1 ,
2403- fmtId (findTypeByOid (tinfo ,numTypes ,agginfo [i ].aggtranstype1 )));
2404+ fmtId (findTypeByOid (tinfo ,numTypes ,agginfo [i ].aggtranstype1 ), false ));
24042405if (agginfo [i ].agginitval1 )
24052406sprintf (sfunc1 ,"%s, INITCOND1 = '%s'" ,
24062407sfunc1 ,agginfo [i ].agginitval1 );
@@ -2414,7 +2415,7 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
24142415sprintf (sfunc2 ,
24152416"SFUNC2 = %s, STYPE2 = %s" ,
24162417agginfo [i ].aggtransfn2 ,
2417- fmtId (findTypeByOid (tinfo ,numTypes ,agginfo [i ].aggtranstype2 )));
2418+ fmtId (findTypeByOid (tinfo ,numTypes ,agginfo [i ].aggtranstype2 ), false ));
24182419if (agginfo [i ].agginitval2 )
24192420sprintf (sfunc2 ,"%s, INITCOND2 = '%s'" ,
24202421sfunc2 ,agginfo [i ].agginitval2 );
@@ -2525,7 +2526,7 @@ dumpACL(FILE *fout, TableInfo tbinfo)
25252526 */
25262527fprintf (fout ,
25272528"REVOKE ALL on %s from PUBLIC;\n" ,
2528- fmtId (tbinfo .relname ));
2529+ fmtId (tbinfo .relname , force_quotes ));
25292530
25302531/* Make a working copy of acls so we can use strtok */
25312532aclbuf = strdup (acls );
@@ -2556,7 +2557,7 @@ dumpACL(FILE *fout, TableInfo tbinfo)
25562557{
25572558fprintf (fout ,
25582559"GRANT %s on %s to " ,
2559- priv ,fmtId (tbinfo .relname ));
2560+ priv ,fmtId (tbinfo .relname , force_quotes ));
25602561/* Note: fmtId() can only be called once per printf, so don't
25612562 * try to merge printing of username into the above printf.
25622563 */
@@ -2568,7 +2569,7 @@ dumpACL(FILE *fout, TableInfo tbinfo)
25682569else
25692570{
25702571* eqpos = '\0' ;/* it's ok to clobber aclbuf */
2571- fprintf (fout ,"%s;\n" ,fmtId (tok ));
2572+ fprintf (fout ,"%s;\n" ,fmtId (tok , force_quotes ));
25722573}
25732574}
25742575free (priv );
@@ -2630,7 +2631,7 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
26302631
26312632becomeUser (fout ,tblinfo [i ].usename );
26322633
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 ));
26342635actual_atts = 0 ;
26352636for (j = 0 ;j < tblinfo [i ].numatts ;j ++ )
26362637{
@@ -2639,28 +2640,39 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
26392640if (actual_atts > 0 )
26402641strcat (q ,",\n\t" );
26412642sprintf (q + strlen (q ),"%s " ,
2642- fmtId (tblinfo [i ].attnames [j ]));
2643+ fmtId (tblinfo [i ].attnames [j ], force_quotes ));
26432644
26442645/* Show lengths on bpchar and varchar */
26452646if (!strcmp (tblinfo [i ].typnames [j ],"bpchar" ))
26462647{
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 );
26492653}
26502654else if (!strcmp (tblinfo [i ].typnames [j ],"varchar" ))
26512655{
2652- sprintf (q + strlen (q ),"%s" ,
2653- tblinfo [i ].typnames [j ]);
2656+ sprintf (q + strlen (q ),"character varying" );
26542657if (tblinfo [i ].atttypmod [j ]!= -1 )
26552658{
26562659sprintf (q + strlen (q ),"(%d)" ,
26572660tblinfo [i ].atttypmod [j ]- VARHDRSZ );
26582661}
26592662}
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+ }
26602672else
26612673{
26622674sprintf (q + strlen (q ),"%s" ,
2663- fmtId (tblinfo [i ].typnames [j ]));
2675+ fmtId (tblinfo [i ].typnames [j ], false ));
26642676}
26652677if (tblinfo [i ].adef_expr [j ]!= NULL )
26662678sprintf (q + strlen (q )," DEFAULT %s" ,
@@ -2689,7 +2701,7 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
26892701{
26902702sprintf (q + strlen (q ),"%s%s" ,
26912703(k > 0 ) ?", " :"" ,
2692- fmtId (parentRels [k ]));
2704+ fmtId (parentRels [k ], force_quotes ));
26932705}
26942706strcat (q ,")" );
26952707}
@@ -2807,7 +2819,7 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
28072819attname = tblinfo [tableInd ].attnames [indkey ];
28082820if (funcname )
28092821sprintf (attlist + strlen (attlist ),"%s%s" ,
2810- (k == 0 ) ?"" :", " ,fmtId (attname ));
2822+ (k == 0 ) ?"" :", " ,fmtId (attname , force_quotes ));
28112823else
28122824{
28132825if (k >=nclass )
@@ -2817,8 +2829,8 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
28172829attname ,indinfo [i ].indexrelname );
28182830exit_nicely (g_conn );
28192831}
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 ));
28222834sprintf (attlist + strlen (attlist ),"%s%s %s" ,
28232835(k == 0 ) ?"" :", " ,id1 ,id2 );
28242836free (classname [k ]);
@@ -2833,8 +2845,8 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
28332845 */
28342846becomeUser (fout ,tblinfo [tableInd ].usename );
28352847
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 ));
28382850fprintf (fout ,"CREATE %s INDEX %s on %s using %s (" ,
28392851 (strcmp (indinfo [i ].indisunique ,"t" )== 0 ) ?"UNIQUE" :"" ,
28402852id1 ,
@@ -2843,8 +2855,8 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
28432855if (funcname )
28442856{
28452857/* 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 ));
28482860free (funcname );
28492861free (classname [0 ]);
28502862}
@@ -3058,7 +3070,7 @@ dumpSequence(FILE *fout, TableInfo tbinfo)
30583070sprintf (query ,
30593071"SELECT sequence_name, last_value, increment_by, max_value, "
30603072"min_value, cache_value, is_cycled, is_called from %s" ,
3061- fmtId (tbinfo .relname ));
3073+ fmtId (tbinfo .relname , force_quotes ));
30623074
30633075res = PQexec (g_conn ,query );
30643076if (!res || PQresultStatus (res )!= PGRES_TUPLES_OK )
@@ -3098,7 +3110,7 @@ dumpSequence(FILE *fout, TableInfo tbinfo)
30983110sprintf (query ,
30993111"CREATE SEQUENCE %s start %d increment %d maxvalue %d "
31003112"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 ,
31023114(cycled == 't' ) ?"cycle" :"" );
31033115
31043116fputs (query ,fout );