* Convert a string value to a dollar quoted literal and append it to * the given buffer. If the dqprefix parameter is not NULL then the * dollar quote delimiter will begin with that (after the opening $). * * No escaping is done at all on str, in compliance with the rules * for parsing dollar quoted strings. Also, we need not worry about * encoding issues. */
| 502 | * encoding issues. |
| 503 | */ |
| 504 | void |
| 505 | appendStringLiteralDQ(PQExpBuffer buf, const char *str, const char *dqprefix) |
| 506 | { |
| 507 | static const char suffixes[] = "_XXXXXXX"; |
| 508 | int nextchar = 0; |
| 509 | PQExpBuffer delimBuf = createPQExpBuffer(); |
| 510 | |
| 511 | /* start with $ + dqprefix if not NULL */ |
| 512 | appendPQExpBufferChar(delimBuf, '$'); |
| 513 | if (dqprefix) |
| 514 | appendPQExpBufferStr(delimBuf, dqprefix); |
| 515 | |
| 516 | /* |
| 517 | * Make sure we choose a delimiter which (without the trailing $) is not |
| 518 | * present in the string being quoted. We don't check with the trailing $ |
| 519 | * because a string ending in $foo must not be quoted with $foo$. |
| 520 | */ |
| 521 | while (strstr(str, delimBuf->data) != NULL) |
| 522 | { |
| 523 | appendPQExpBufferChar(delimBuf, suffixes[nextchar++]); |
| 524 | nextchar %= sizeof(suffixes) - 1; |
| 525 | } |
| 526 | |
| 527 | /* add trailing $ */ |
| 528 | appendPQExpBufferChar(delimBuf, '$'); |
| 529 | |
| 530 | /* quote it and we are all done */ |
| 531 | appendPQExpBufferStr(buf, delimBuf->data); |
| 532 | appendPQExpBufferStr(buf, str); |
| 533 | appendPQExpBufferStr(buf, delimBuf->data); |
| 534 | |
| 535 | destroyPQExpBuffer(delimBuf); |
| 536 | } |
| 537 | |
| 538 | |
| 539 | /* |
no test coverage detected