2222 *
2323 *
2424 * IDENTIFICATION
25- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.191 2001/02/10 02 :31:27 tgl Exp $
25+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.192 2001/02/13 01 :31:54 pjw Exp $
2626 *
2727 * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
2828 *
9696 * table with the currently implementation, and (b) it's not clear how to restore
9797 * a partial BLOB backup (given the current OID-based BLOB implementation).
9898 *
99- * Modifications - 04-Jan-2000 - pjw@rhyme.com.au
99+ * Modifications - 04-Jan-2001 - pjw@rhyme.com.au
100100 *
101101 * - Check ntuples == 1 for various SELECT statements.
102102 * - Fix handling of --tables=* (multiple tables never worked properly, AFAICT)
103103 *
104+ * Modifications - 13-Feb-2001 - pjw@rhyme.com.au
105+ *
106+ * - Fix help output: replace 'f' with 't' and change desc.
107+ * - Add extra arg to formatStringLiteral to specify how to handle LF & TAB.
108+ * I opted for encoding them except in procedure bodies.
109+ *
104110 *-------------------------------------------------------------------------
105111 */
106112
140146
141147#define atooid (x ) ((Oid) strtoul((x), NULL, 10))
142148
149+
150+ typedef enum _formatLiteralOptions {
151+ CONV_ALL = 0 ,
152+ PASS_LFTAB = 3 /* NOTE: 1 and 2 are reserved in case we want to make a mask. */
153+ /* We could make this a bit mask for control chars, but I don't */
154+ /* see any value in making it more complex...the current code */
155+ /* only checks for 'opts == CONV_ALL' anyway. */
156+ } formatLiteralOptions ;
157+
143158static void dumpComment (Archive * outfile , const char * target , const char * oid );
144159static void dumpSequence (Archive * fout , TableInfo tbinfo );
145160static void dumpACL (Archive * fout , TableInfo tbinfo );
146161static void dumpTriggers (Archive * fout , const char * tablename ,
147162 TableInfo * tblinfo , int numTables );
148163static void dumpRules (Archive * fout , const char * tablename ,
149164 TableInfo * tblinfo , int numTables );
150- static void formatStringLiteral (PQExpBuffer buf , const char * str );
165+ static void formatStringLiteral (PQExpBuffer buf , const char * str , const formatLiteralOptions opts );
151166static void clearTableInfo (TableInfo * , int );
152167static void dumpOneFunc (Archive * fout , FuncInfo * finfo , int i ,
153168 TypeInfo * tinfo , int numTypes );
@@ -209,7 +224,7 @@ help(const char *progname)
209224 " -d, --inserts dump data as INSERT, rather than COPY, commands\n"
210225 " -D, --attribute-inserts dump data as INSERT commands with attribute names\n"
211226 " -f, --file=FILENAME specify output file name\n"
212- " -F, --format {c|f |p} output file format (custom, files , plain text)\n"
227+ " -F, --format {c|t |p} output file format (custom, tar , plain text)\n"
213228 " -h, --host=HOSTNAME server host name\n"
214229 " -i, --ignore-version proceed when database version != pg_dump version\n"
215230 " -n, --no-quotes suppress most quotes around identifiers\n"
@@ -238,7 +253,7 @@ help(const char *progname)
238253 " -d dump data as INSERT, rather than COPY, commands\n"
239254 " -D dump data as INSERT commands with attribute names\n"
240255 " -f FILENAME specify output file name\n"
241- " -F {c|f |p} output file format (custom, files , plain text)\n"
256+ " -F {c|t |p} output file format (custom, tar , plain text)\n"
242257 " -h HOSTNAME server host name\n"
243258 " -i proceed when database version != pg_dump version\n"
244259 " -n suppress most quotes around identifiers\n"
@@ -509,7 +524,7 @@ dumpClasses_dumpData(Archive *fout, char* oid, void *dctxv)
509524 * with appropriate escaping of special characters.
510525 */
511526 resetPQExpBuffer (q );
512- formatStringLiteral (q , PQgetvalue (res , tuple , field ));
527+ formatStringLiteral (q , PQgetvalue (res , tuple , field ), CONV_ALL );
513528 archprintf (fout , "%s" , q -> data );
514529 break ;
515530 }
@@ -528,7 +543,7 @@ dumpClasses_dumpData(Archive *fout, char* oid, void *dctxv)
528543 * The literal is appended to the given PQExpBuffer.
529544 */
530545static void
531- formatStringLiteral (PQExpBuffer buf , const char * str )
546+ formatStringLiteral (PQExpBuffer buf , const char * str , const formatLiteralOptions opts )
532547{
533548 appendPQExpBufferChar (buf , '\'' );
534549 while (* str )
@@ -541,7 +556,9 @@ formatStringLiteral(PQExpBuffer buf, const char *str)
541556 appendPQExpBufferChar (buf , ch );
542557 }
543558 else if ((unsigned char ) ch < (unsigned char ) ' ' &&
544- ch != '\n' && ch != '\t' )
559+ ( opts == CONV_ALL
560+ || (ch != '\n' && ch != '\t' )
561+ ))
545562 {
546563 /* generate octal escape for control chars other than whitespace */
547564 appendPQExpBufferChar (buf , '\\' );
@@ -1099,7 +1116,7 @@ dumpDatabase(Archive *AH)
10991116 /* Get the dba */
11001117 appendPQExpBuffer (dbQry , "select (select usename from pg_user where datdba = usesysid) as dba from pg_database"
11011118 " where datname = " );
1102- formatStringLiteral (dbQry , PQdb (g_conn ));
1119+ formatStringLiteral (dbQry , PQdb (g_conn ), CONV_ALL );
11031120
11041121 res = PQexec (g_conn , dbQry -> data );
11051122 if (!res ||
@@ -1988,7 +2005,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
19882005
19892006 resetPQExpBuffer (query );
19902007 appendPQExpBuffer (query , "SELECT pg_get_viewdef(" );
1991- formatStringLiteral (query , tblinfo [i ].relname );
2008+ formatStringLiteral (query , tblinfo [i ].relname , CONV_ALL );
19922009 appendPQExpBuffer (query , ") as viewdef" );
19932010 res2 = PQexec (g_conn , query -> data );
19942011 if (!res2 || PQresultStatus (res2 ) != PGRES_TUPLES_OK )
@@ -2823,7 +2840,7 @@ dumpComment(Archive *fout, const char *target, const char *oid)
28232840 i_description = PQfnumber (res , "description" );
28242841 resetPQExpBuffer (query );
28252842 appendPQExpBuffer (query , "COMMENT ON %s IS " , target );
2826- formatStringLiteral (query , PQgetvalue (res , 0 , i_description ));
2843+ formatStringLiteral (query , PQgetvalue (res , 0 , i_description ), PASS_LFTAB );
28272844 appendPQExpBuffer (query , ";\n" );
28282845
28292846 ArchiveEntry (fout , oid , target , "COMMENT" , NULL , query -> data , "" /*Del*/ ,
@@ -2859,7 +2876,7 @@ dumpDBComment(Archive *fout)
28592876
28602877 query = createPQExpBuffer ();
28612878 appendPQExpBuffer (query , "SELECT oid FROM pg_database WHERE datname = " );
2862- formatStringLiteral (query , PQdb (g_conn ));
2879+ formatStringLiteral (query , PQdb (g_conn ), CONV_ALL );
28632880
28642881 /*** Execute query ***/
28652882
@@ -2947,7 +2964,7 @@ dumpTypes(Archive *fout, FuncInfo *finfo, int numFuncs,
29472964 fmtId (tinfo [i ].typsend , force_quotes ));
29482965 appendPQExpBuffer (q , " receive = %s, default = " ,
29492966 fmtId (tinfo [i ].typreceive , force_quotes ));
2950- formatStringLiteral (q , tinfo [i ].typdefault );
2967+ formatStringLiteral (q , tinfo [i ].typdefault , CONV_ALL );
29512968
29522969 if (tinfo [i ].isArray )
29532970 {
@@ -2964,7 +2981,7 @@ dumpTypes(Archive *fout, FuncInfo *finfo, int numFuncs,
29642981 }
29652982
29662983 appendPQExpBuffer (q , ", element = %s, delimiter = " , elemType );
2967- formatStringLiteral (q , tinfo [i ].typdelim );
2984+ formatStringLiteral (q , tinfo [i ].typdelim , CONV_ALL );
29682985 }
29692986 if (tinfo [i ].passedbyvalue )
29702987 appendPQExpBuffer (q , ",passedbyvalue);\n" );
@@ -3057,16 +3074,16 @@ dumpProcLangs(Archive *fout, FuncInfo *finfo, int numFuncs,
30573074 lancompiler = PQgetvalue (res , i , i_lancompiler );
30583075
30593076 appendPQExpBuffer (delqry , "DROP PROCEDURAL LANGUAGE " );
3060- formatStringLiteral (delqry , lanname );
3077+ formatStringLiteral (delqry , lanname , CONV_ALL );
30613078 appendPQExpBuffer (delqry , ";\n" );
30623079
30633080 appendPQExpBuffer (defqry , "CREATE %sPROCEDURAL LANGUAGE " ,
30643081 (PQgetvalue (res , i , i_lanpltrusted )[0 ] == 't' ) ?
30653082 "TRUSTED " : "" );
3066- formatStringLiteral (defqry , lanname );
3083+ formatStringLiteral (defqry , lanname , CONV_ALL );
30673084 appendPQExpBuffer (defqry , " HANDLER %s LANCOMPILER " ,
30683085 fmtId (finfo [fidx ].proname , force_quotes ));
3069- formatStringLiteral (defqry , lancompiler );
3086+ formatStringLiteral (defqry , lancompiler , CONV_ALL );
30703087 appendPQExpBuffer (defqry , ";\n" );
30713088
30723089 ArchiveEntry (fout , PQgetvalue (res , i , i_oid ), lanname , "PROCEDURAL LANGUAGE" ,
@@ -3156,19 +3173,19 @@ dumpOneFunc(Archive *fout, FuncInfo *finfo, int i,
31563173 if (strcmp (finfo [i ].probin , "-" ) != 0 )
31573174 {
31583175 appendPQExpBuffer (asPart , "AS " );
3159- formatStringLiteral (asPart , finfo [i ].probin );
3176+ formatStringLiteral (asPart , finfo [i ].probin , CONV_ALL );
31603177 if (strcmp (finfo [i ].prosrc , "-" ) != 0 )
31613178 {
31623179 appendPQExpBuffer (asPart , ", " );
3163- formatStringLiteral (asPart , finfo [i ].prosrc );
3180+ formatStringLiteral (asPart , finfo [i ].prosrc , PASS_LFTAB );
31643181 }
31653182 }
31663183 else
31673184 {
31683185 if (strcmp (finfo [i ].prosrc , "-" ) != 0 )
31693186 {
31703187 appendPQExpBuffer (asPart , "AS " );
3171- formatStringLiteral (asPart , finfo [i ].prosrc );
3188+ formatStringLiteral (asPart , finfo [i ].prosrc , PASS_LFTAB );
31723189 }
31733190 }
31743191
@@ -3233,7 +3250,7 @@ dumpOneFunc(Archive *fout, FuncInfo *finfo, int i,
32333250 (finfo [i ].retset ) ? "SETOF " : "" ,
32343251 rettypename ,
32353252 asPart -> data );
3236- formatStringLiteral (q , func_lang );
3253+ formatStringLiteral (q , func_lang , CONV_ALL );
32373254
32383255 if (finfo [i ].iscachable || finfo [i ].isstrict ) /* OR in new attrs here */
32393256 {
@@ -3477,7 +3494,7 @@ dumpAggs(Archive *fout, AggInfo *agginfo, int numAggs,
34773494 if (agginfo [i ].agginitval )
34783495 {
34793496 appendPQExpBuffer (details , ", INITCOND = " );
3480- formatStringLiteral (details , agginfo [i ].agginitval );
3497+ formatStringLiteral (details , agginfo [i ].agginitval , CONV_ALL );
34813498 }
34823499
34833500 if (!(strcmp (agginfo [i ].aggfinalfn , "-" ) == 0 ))
@@ -4267,7 +4284,7 @@ findLastBuiltinOid(const char* dbname)
42674284
42684285 resetPQExpBuffer (query );
42694286 appendPQExpBuffer (query , "SELECT datlastsysoid from pg_database where datname = " );
4270- formatStringLiteral (query , dbname );
4287+ formatStringLiteral (query , dbname , CONV_ALL );
42714288
42724289 res = PQexec (g_conn , query -> data );
42734290 if (res == NULL ||
@@ -4376,7 +4393,7 @@ dumpSequence(Archive *fout, TableInfo tbinfo)
43764393
43774394 resetPQExpBuffer (query );
43784395 appendPQExpBuffer (query , "SELECT setval (" );
4379- formatStringLiteral (query , fmtId (tbinfo .relname , force_quotes ));
4396+ formatStringLiteral (query , fmtId (tbinfo .relname , force_quotes ), CONV_ALL );
43804397 appendPQExpBuffer (query , ", %d, '%c');\n" , last , called );
43814398
43824399 ArchiveEntry (fout , tbinfo .oid , fmtId (tbinfo .relname , force_quotes ), "SEQUENCE SET" , NULL ,
@@ -4458,7 +4475,7 @@ dumpRules(Archive *fout, const char *tablename,
44584475 " pg_rewrite.oid, pg_rewrite.rulename "
44594476 "FROM pg_rewrite, pg_class, pg_rules "
44604477 "WHERE pg_class.relname = " );
4461- formatStringLiteral (query , tblinfo [t ].relname );
4478+ formatStringLiteral (query , tblinfo [t ].relname , CONV_ALL );
44624479 appendPQExpBuffer (query ,
44634480 " AND pg_rewrite.ev_class = pg_class.oid "
44644481 " AND pg_rules.tablename = pg_class.relname "
0 commit comments