|
8 | 8 | * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
|
9 | 9 | * Portions Copyright (c) 1994, Regents of the University of California
|
10 | 10 | *
|
11 |
| - * $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.47 2009/07/14 20:24:10 tgl Exp $ |
| 11 | + * $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.48 2009/08/04 21:56:08 tgl Exp $ |
12 | 12 | *
|
13 | 13 | *-------------------------------------------------------------------------
|
14 | 14 | */
|
@@ -325,6 +325,55 @@ appendStringLiteralDQ(PQExpBuffer buf, const char *str, const char *dqprefix)
|
325 | 325 | }
|
326 | 326 |
|
327 | 327 |
|
| 328 | +/* |
| 329 | + * Convert a bytea value (presented as raw bytes) to an SQL string literal |
| 330 | + * and append it to the given buffer. We assume the specified |
| 331 | + * standard_conforming_strings setting. |
| 332 | + * |
| 333 | + * This is needed in situations where we do not have a PGconn available. |
| 334 | + * Where we do, PQescapeByteaConn is a better choice. |
| 335 | + */ |
| 336 | +void |
| 337 | +appendByteaLiteral(PQExpBuffer buf, const unsigned char *str, size_t length, |
| 338 | + bool std_strings) |
| 339 | +{ |
| 340 | + const unsigned char *source = str; |
| 341 | + char *target; |
| 342 | + |
| 343 | + static const char hextbl[] = "0123456789abcdef"; |
| 344 | + |
| 345 | + /* |
| 346 | + * This implementation is hard-wired to produce hex-format output. |
| 347 | + * We do not know the server version the output will be loaded into, |
| 348 | + * so making an intelligent format choice is impossible. It might be |
| 349 | + * better to always use the old escaped format. |
| 350 | + */ |
| 351 | + if (!enlargePQExpBuffer(buf, 2 * length + 5)) |
| 352 | + return; |
| 353 | + |
| 354 | + target = buf->data + buf->len; |
| 355 | + *target++ = '\''; |
| 356 | + if (!std_strings) |
| 357 | + *target++ = '\\'; |
| 358 | + *target++ = '\\'; |
| 359 | + *target++ = 'x'; |
| 360 | + |
| 361 | + while (length-- > 0) |
| 362 | + { |
| 363 | + unsigned char c = *source++; |
| 364 | + |
| 365 | + *target++ = hextbl[(c >> 4) & 0xF]; |
| 366 | + *target++ = hextbl[c & 0xF]; |
| 367 | + } |
| 368 | + |
| 369 | + /* Write the terminating quote and NUL character. */ |
| 370 | + *target++ = '\''; |
| 371 | + *target = '\0'; |
| 372 | + |
| 373 | + buf->len = target - buf->data; |
| 374 | +} |
| 375 | + |
| 376 | + |
328 | 377 | /*
|
329 | 378 | * Convert backend's version string into a number.
|
330 | 379 | */
|
|
0 commit comments