3
3
*
4
4
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
5
5
*
6
- * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.62 2006/05/31 11:02:42 momjian Exp $
6
+ * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.63 2006/06/01 00:15:36 tgl Exp $
7
7
*/
8
8
#include "postgres_fe.h"
9
9
#include "copy.h"
19
19
#include "libpq-fe.h"
20
20
#include "pqexpbuffer.h"
21
21
#include "pqsignal.h"
22
+ #include "dumputils.h"
22
23
23
24
#include "settings.h"
24
25
#include "common.h"
@@ -113,6 +114,7 @@ parse_slash_copy(const char *args)
113
114
char * line ;
114
115
char * token ;
115
116
const char * whitespace = " \t\n\r" ;
117
+ char nonstd_backslash = standard_strings () ? 0 : '\\' ;
116
118
117
119
if (args )
118
120
line = pg_strdup (args );
@@ -216,7 +218,7 @@ parse_slash_copy(const char *args)
216
218
goto error ;
217
219
218
220
token = strtokx (NULL , whitespace , NULL , "'" ,
219
- standard_strings () ? 0 : '\\' , true, pset .encoding );
221
+ nonstd_backslash , true, pset .encoding );
220
222
if (!token )
221
223
goto error ;
222
224
@@ -255,7 +257,7 @@ parse_slash_copy(const char *args)
255
257
if (token && pg_strcasecmp (token , "delimiters" ) == 0 )
256
258
{
257
259
token = strtokx (NULL , whitespace , NULL , "'" ,
258
- standard_strings () ? 0 : '\\' , false, pset .encoding );
260
+ nonstd_backslash , false, pset .encoding );
259
261
if (!token )
260
262
goto error ;
261
263
result -> delim = pg_strdup (token );
@@ -290,10 +292,10 @@ parse_slash_copy(const char *args)
290
292
else if (pg_strcasecmp (token , "delimiter" ) == 0 )
291
293
{
292
294
token = strtokx (NULL , whitespace , NULL , "'" ,
293
- standard_strings () ? 0 : '\\' , false, pset .encoding );
295
+ nonstd_backslash , false, pset .encoding );
294
296
if (token && pg_strcasecmp (token , "as" ) == 0 )
295
297
token = strtokx (NULL , whitespace , NULL , "'" ,
296
- standard_strings () ? 0 : '\\' , false, pset .encoding );
298
+ nonstd_backslash , false, pset .encoding );
297
299
if (token )
298
300
result -> delim = pg_strdup (token );
299
301
else
@@ -302,10 +304,10 @@ parse_slash_copy(const char *args)
302
304
else if (pg_strcasecmp (token , "null" ) == 0 )
303
305
{
304
306
token = strtokx (NULL , whitespace , NULL , "'" ,
305
- standard_strings () ? 0 : '\\' , false, pset .encoding );
307
+ nonstd_backslash , false, pset .encoding );
306
308
if (token && pg_strcasecmp (token , "as" ) == 0 )
307
309
token = strtokx (NULL , whitespace , NULL , "'" ,
308
- standard_strings () ? 0 : '\\' , false, pset .encoding );
310
+ nonstd_backslash , false, pset .encoding );
309
311
if (token )
310
312
result -> null = pg_strdup (token );
311
313
else
@@ -314,10 +316,10 @@ parse_slash_copy(const char *args)
314
316
else if (pg_strcasecmp (token , "quote" ) == 0 )
315
317
{
316
318
token = strtokx (NULL , whitespace , NULL , "'" ,
317
- standard_strings () ? 0 : '\\' , false, pset .encoding );
319
+ nonstd_backslash , false, pset .encoding );
318
320
if (token && pg_strcasecmp (token , "as" ) == 0 )
319
321
token = strtokx (NULL , whitespace , NULL , "'" ,
320
- standard_strings () ? 0 : '\\' , false, pset .encoding );
322
+ nonstd_backslash , false, pset .encoding );
321
323
if (token )
322
324
result -> quote = pg_strdup (token );
323
325
else
@@ -326,10 +328,10 @@ parse_slash_copy(const char *args)
326
328
else if (pg_strcasecmp (token , "escape" ) == 0 )
327
329
{
328
330
token = strtokx (NULL , whitespace , NULL , "'" ,
329
- standard_strings () ? 0 : '\\' , false, pset .encoding );
331
+ nonstd_backslash , false, pset .encoding );
330
332
if (token && pg_strcasecmp (token , "as" ) == 0 )
331
333
token = strtokx (NULL , whitespace , NULL , "'" ,
332
- standard_strings () ? 0 : '\\' , false, pset .encoding );
334
+ nonstd_backslash , false, pset .encoding );
333
335
if (token )
334
336
result -> escape = pg_strdup (token );
335
337
else
@@ -461,23 +463,27 @@ do_copy(const char *args)
461
463
/* Uses old COPY syntax for backward compatibility 2002-06-19 */
462
464
if (options -> delim )
463
465
{
466
+ /* if user gave a quoted string, use it as-is */
464
467
if (options -> delim [0 ] == '\'' )
465
- appendPQExpBuffer (& query , " USING DELIMITERS %c%s" ,
466
- NEED_E_STR (options -> delim ), options -> delim );
468
+ appendPQExpBuffer (& query , " USING DELIMITERS %s" , options -> delim );
467
469
else
468
- appendPQExpBuffer (& query , " USING DELIMITERS %c'%s'" ,
469
- NEED_E_STR (options -> delim ), options -> delim );
470
+ {
471
+ appendPQExpBuffer (& query , " USING DELIMITERS " );
472
+ appendStringLiteralConn (& query , options -> delim , pset .db );
473
+ }
470
474
}
471
475
472
476
/* There is no backward-compatible CSV syntax */
473
477
if (options -> null )
474
478
{
479
+ /* if user gave a quoted string, use it as-is */
475
480
if (options -> null [0 ] == '\'' )
476
- appendPQExpBuffer (& query , " WITH NULL AS %c%s" ,
477
- NEED_E_STR (options -> null ), options -> null );
481
+ appendPQExpBuffer (& query , " WITH NULL AS %s" , options -> null );
478
482
else
479
- appendPQExpBuffer (& query , " WITH NULL AS %c'%s'" ,
480
- NEED_E_STR (options -> null ), options -> null );
483
+ {
484
+ appendPQExpBuffer (& query , " WITH NULL AS " );
485
+ appendStringLiteralConn (& query , options -> null , pset .db );
486
+ }
481
487
}
482
488
483
489
if (options -> csv_mode )
@@ -488,22 +494,26 @@ do_copy(const char *args)
488
494
489
495
if (options -> quote )
490
496
{
497
+ /* if user gave a quoted string, use it as-is */
491
498
if (options -> quote [0 ] == '\'' )
492
- appendPQExpBuffer (& query , " QUOTE AS %c%s" ,
493
- NEED_E_STR (options -> quote ), options -> quote );
499
+ appendPQExpBuffer (& query , " QUOTE AS %s" , options -> quote );
494
500
else
495
- appendPQExpBuffer (& query , " QUOTE AS %c'%s'" ,
496
- NEED_E_STR (options -> quote ), options -> quote );
501
+ {
502
+ appendPQExpBuffer (& query , " QUOTE AS " );
503
+ appendStringLiteralConn (& query , options -> quote , pset .db );
504
+ }
497
505
}
498
506
499
507
if (options -> escape )
500
508
{
509
+ /* if user gave a quoted string, use it as-is */
501
510
if (options -> escape [0 ] == '\'' )
502
- appendPQExpBuffer (& query , " ESCAPE AS %c%s" ,
503
- NEED_E_STR (options -> escape ), options -> escape );
511
+ appendPQExpBuffer (& query , " ESCAPE AS %s" , options -> escape );
504
512
else
505
- appendPQExpBuffer (& query , " ESCAPE AS %c'%s'" ,
506
- NEED_E_STR (options -> escape ), options -> escape );
513
+ {
514
+ appendPQExpBuffer (& query , " ESCAPE AS " );
515
+ appendStringLiteralConn (& query , options -> escape , pset .db );
516
+ }
507
517
}
508
518
509
519
if (options -> force_quote_list )
0 commit comments