22
22
*
23
23
*
24
24
* 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 $
26
26
*
27
27
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
28
28
*
96
96
* table with the currently implementation, and (b) it's not clear how to restore
97
97
* a partial BLOB backup (given the current OID-based BLOB implementation).
98
98
*
99
- * Modifications - 04-Jan-2000 - pjw@rhyme.com.au
99
+ * Modifications - 04-Jan-2001 - pjw@rhyme.com.au
100
100
*
101
101
* - Check ntuples == 1 for various SELECT statements.
102
102
* - Fix handling of --tables=* (multiple tables never worked properly, AFAICT)
103
103
*
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
+ *
104
110
*-------------------------------------------------------------------------
105
111
*/
106
112
140
146
141
147
#define atooid (x ) ((Oid) strtoul((x), NULL, 10))
142
148
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
+
143
158
static void dumpComment (Archive * outfile , const char * target , const char * oid );
144
159
static void dumpSequence (Archive * fout , TableInfo tbinfo );
145
160
static void dumpACL (Archive * fout , TableInfo tbinfo );
146
161
static void dumpTriggers (Archive * fout , const char * tablename ,
147
162
TableInfo * tblinfo , int numTables );
148
163
static void dumpRules (Archive * fout , const char * tablename ,
149
164
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 );
151
166
static void clearTableInfo (TableInfo * , int );
152
167
static void dumpOneFunc (Archive * fout , FuncInfo * finfo , int i ,
153
168
TypeInfo * tinfo , int numTypes );
@@ -209,7 +224,7 @@ help(const char *progname)
209
224
" -d, --inserts dump data as INSERT, rather than COPY, commands\n"
210
225
" -D, --attribute-inserts dump data as INSERT commands with attribute names\n"
211
226
" -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"
213
228
" -h, --host=HOSTNAME server host name\n"
214
229
" -i, --ignore-version proceed when database version != pg_dump version\n"
215
230
" -n, --no-quotes suppress most quotes around identifiers\n"
@@ -238,7 +253,7 @@ help(const char *progname)
238
253
" -d dump data as INSERT, rather than COPY, commands\n"
239
254
" -D dump data as INSERT commands with attribute names\n"
240
255
" -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"
242
257
" -h HOSTNAME server host name\n"
243
258
" -i proceed when database version != pg_dump version\n"
244
259
" -n suppress most quotes around identifiers\n"
@@ -509,7 +524,7 @@ dumpClasses_dumpData(Archive *fout, char* oid, void *dctxv)
509
524
* with appropriate escaping of special characters.
510
525
*/
511
526
resetPQExpBuffer (q );
512
- formatStringLiteral (q , PQgetvalue (res , tuple , field ));
527
+ formatStringLiteral (q , PQgetvalue (res , tuple , field ), CONV_ALL );
513
528
archprintf (fout , "%s" , q -> data );
514
529
break ;
515
530
}
@@ -528,7 +543,7 @@ dumpClasses_dumpData(Archive *fout, char* oid, void *dctxv)
528
543
* The literal is appended to the given PQExpBuffer.
529
544
*/
530
545
static void
531
- formatStringLiteral (PQExpBuffer buf , const char * str )
546
+ formatStringLiteral (PQExpBuffer buf , const char * str , const formatLiteralOptions opts )
532
547
{
533
548
appendPQExpBufferChar (buf , '\'' );
534
549
while (* str )
@@ -541,7 +556,9 @@ formatStringLiteral(PQExpBuffer buf, const char *str)
541
556
appendPQExpBufferChar (buf , ch );
542
557
}
543
558
else if ((unsigned char ) ch < (unsigned char ) ' ' &&
544
- ch != '\n' && ch != '\t' )
559
+ ( opts == CONV_ALL
560
+ || (ch != '\n' && ch != '\t' )
561
+ ))
545
562
{
546
563
/* generate octal escape for control chars other than whitespace */
547
564
appendPQExpBufferChar (buf , '\\' );
@@ -1099,7 +1116,7 @@ dumpDatabase(Archive *AH)
1099
1116
/* Get the dba */
1100
1117
appendPQExpBuffer (dbQry , "select (select usename from pg_user where datdba = usesysid) as dba from pg_database"
1101
1118
" where datname = " );
1102
- formatStringLiteral (dbQry , PQdb (g_conn ));
1119
+ formatStringLiteral (dbQry , PQdb (g_conn ), CONV_ALL );
1103
1120
1104
1121
res = PQexec (g_conn , dbQry -> data );
1105
1122
if (!res ||
@@ -1988,7 +2005,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
1988
2005
1989
2006
resetPQExpBuffer (query );
1990
2007
appendPQExpBuffer (query , "SELECT pg_get_viewdef(" );
1991
- formatStringLiteral (query , tblinfo [i ].relname );
2008
+ formatStringLiteral (query , tblinfo [i ].relname , CONV_ALL );
1992
2009
appendPQExpBuffer (query , ") as viewdef" );
1993
2010
res2 = PQexec (g_conn , query -> data );
1994
2011
if (!res2 || PQresultStatus (res2 ) != PGRES_TUPLES_OK )
@@ -2823,7 +2840,7 @@ dumpComment(Archive *fout, const char *target, const char *oid)
2823
2840
i_description = PQfnumber (res , "description" );
2824
2841
resetPQExpBuffer (query );
2825
2842
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 );
2827
2844
appendPQExpBuffer (query , ";\n" );
2828
2845
2829
2846
ArchiveEntry (fout , oid , target , "COMMENT" , NULL , query -> data , "" /*Del*/ ,
@@ -2859,7 +2876,7 @@ dumpDBComment(Archive *fout)
2859
2876
2860
2877
query = createPQExpBuffer ();
2861
2878
appendPQExpBuffer (query , "SELECT oid FROM pg_database WHERE datname = " );
2862
- formatStringLiteral (query , PQdb (g_conn ));
2879
+ formatStringLiteral (query , PQdb (g_conn ), CONV_ALL );
2863
2880
2864
2881
/*** Execute query ***/
2865
2882
@@ -2947,7 +2964,7 @@ dumpTypes(Archive *fout, FuncInfo *finfo, int numFuncs,
2947
2964
fmtId (tinfo [i ].typsend , force_quotes ));
2948
2965
appendPQExpBuffer (q , " receive = %s, default = " ,
2949
2966
fmtId (tinfo [i ].typreceive , force_quotes ));
2950
- formatStringLiteral (q , tinfo [i ].typdefault );
2967
+ formatStringLiteral (q , tinfo [i ].typdefault , CONV_ALL );
2951
2968
2952
2969
if (tinfo [i ].isArray )
2953
2970
{
@@ -2964,7 +2981,7 @@ dumpTypes(Archive *fout, FuncInfo *finfo, int numFuncs,
2964
2981
}
2965
2982
2966
2983
appendPQExpBuffer (q , ", element = %s, delimiter = " , elemType );
2967
- formatStringLiteral (q , tinfo [i ].typdelim );
2984
+ formatStringLiteral (q , tinfo [i ].typdelim , CONV_ALL );
2968
2985
}
2969
2986
if (tinfo [i ].passedbyvalue )
2970
2987
appendPQExpBuffer (q , ",passedbyvalue);\n" );
@@ -3057,16 +3074,16 @@ dumpProcLangs(Archive *fout, FuncInfo *finfo, int numFuncs,
3057
3074
lancompiler = PQgetvalue (res , i , i_lancompiler );
3058
3075
3059
3076
appendPQExpBuffer (delqry , "DROP PROCEDURAL LANGUAGE " );
3060
- formatStringLiteral (delqry , lanname );
3077
+ formatStringLiteral (delqry , lanname , CONV_ALL );
3061
3078
appendPQExpBuffer (delqry , ";\n" );
3062
3079
3063
3080
appendPQExpBuffer (defqry , "CREATE %sPROCEDURAL LANGUAGE " ,
3064
3081
(PQgetvalue (res , i , i_lanpltrusted )[0 ] == 't' ) ?
3065
3082
"TRUSTED " : "" );
3066
- formatStringLiteral (defqry , lanname );
3083
+ formatStringLiteral (defqry , lanname , CONV_ALL );
3067
3084
appendPQExpBuffer (defqry , " HANDLER %s LANCOMPILER " ,
3068
3085
fmtId (finfo [fidx ].proname , force_quotes ));
3069
- formatStringLiteral (defqry , lancompiler );
3086
+ formatStringLiteral (defqry , lancompiler , CONV_ALL );
3070
3087
appendPQExpBuffer (defqry , ";\n" );
3071
3088
3072
3089
ArchiveEntry (fout , PQgetvalue (res , i , i_oid ), lanname , "PROCEDURAL LANGUAGE" ,
@@ -3156,19 +3173,19 @@ dumpOneFunc(Archive *fout, FuncInfo *finfo, int i,
3156
3173
if (strcmp (finfo [i ].probin , "-" ) != 0 )
3157
3174
{
3158
3175
appendPQExpBuffer (asPart , "AS " );
3159
- formatStringLiteral (asPart , finfo [i ].probin );
3176
+ formatStringLiteral (asPart , finfo [i ].probin , CONV_ALL );
3160
3177
if (strcmp (finfo [i ].prosrc , "-" ) != 0 )
3161
3178
{
3162
3179
appendPQExpBuffer (asPart , ", " );
3163
- formatStringLiteral (asPart , finfo [i ].prosrc );
3180
+ formatStringLiteral (asPart , finfo [i ].prosrc , PASS_LFTAB );
3164
3181
}
3165
3182
}
3166
3183
else
3167
3184
{
3168
3185
if (strcmp (finfo [i ].prosrc , "-" ) != 0 )
3169
3186
{
3170
3187
appendPQExpBuffer (asPart , "AS " );
3171
- formatStringLiteral (asPart , finfo [i ].prosrc );
3188
+ formatStringLiteral (asPart , finfo [i ].prosrc , PASS_LFTAB );
3172
3189
}
3173
3190
}
3174
3191
@@ -3233,7 +3250,7 @@ dumpOneFunc(Archive *fout, FuncInfo *finfo, int i,
3233
3250
(finfo [i ].retset ) ? "SETOF " : "" ,
3234
3251
rettypename ,
3235
3252
asPart -> data );
3236
- formatStringLiteral (q , func_lang );
3253
+ formatStringLiteral (q , func_lang , CONV_ALL );
3237
3254
3238
3255
if (finfo [i ].iscachable || finfo [i ].isstrict ) /* OR in new attrs here */
3239
3256
{
@@ -3477,7 +3494,7 @@ dumpAggs(Archive *fout, AggInfo *agginfo, int numAggs,
3477
3494
if (agginfo [i ].agginitval )
3478
3495
{
3479
3496
appendPQExpBuffer (details , ", INITCOND = " );
3480
- formatStringLiteral (details , agginfo [i ].agginitval );
3497
+ formatStringLiteral (details , agginfo [i ].agginitval , CONV_ALL );
3481
3498
}
3482
3499
3483
3500
if (!(strcmp (agginfo [i ].aggfinalfn , "-" ) == 0 ))
@@ -4267,7 +4284,7 @@ findLastBuiltinOid(const char* dbname)
4267
4284
4268
4285
resetPQExpBuffer (query );
4269
4286
appendPQExpBuffer (query , "SELECT datlastsysoid from pg_database where datname = " );
4270
- formatStringLiteral (query , dbname );
4287
+ formatStringLiteral (query , dbname , CONV_ALL );
4271
4288
4272
4289
res = PQexec (g_conn , query -> data );
4273
4290
if (res == NULL ||
@@ -4376,7 +4393,7 @@ dumpSequence(Archive *fout, TableInfo tbinfo)
4376
4393
4377
4394
resetPQExpBuffer (query );
4378
4395
appendPQExpBuffer (query , "SELECT setval (" );
4379
- formatStringLiteral (query , fmtId (tbinfo .relname , force_quotes ));
4396
+ formatStringLiteral (query , fmtId (tbinfo .relname , force_quotes ), CONV_ALL );
4380
4397
appendPQExpBuffer (query , ", %d, '%c');\n" , last , called );
4381
4398
4382
4399
ArchiveEntry (fout , tbinfo .oid , fmtId (tbinfo .relname , force_quotes ), "SEQUENCE SET" , NULL ,
@@ -4458,7 +4475,7 @@ dumpRules(Archive *fout, const char *tablename,
4458
4475
" pg_rewrite.oid, pg_rewrite.rulename "
4459
4476
"FROM pg_rewrite, pg_class, pg_rules "
4460
4477
"WHERE pg_class.relname = " );
4461
- formatStringLiteral (query , tblinfo [t ].relname );
4478
+ formatStringLiteral (query , tblinfo [t ].relname , CONV_ALL );
4462
4479
appendPQExpBuffer (query ,
4463
4480
" AND pg_rewrite.ev_class = pg_class.oid "
4464
4481
" AND pg_rules.tablename = pg_class.relname "
0 commit comments