Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * String-processing utility routines for frontend code
4 : *
5 : * Assorted utility functions that are useful in constructing SQL queries
6 : * and interpreting backend output.
7 : *
8 : *
9 : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
10 : * Portions Copyright (c) 1994, Regents of the University of California
11 : *
12 : * src/fe_utils/string_utils.c
13 : *
14 : *-------------------------------------------------------------------------
15 : */
16 : #include "postgres_fe.h"
17 :
18 : #include <ctype.h>
19 :
20 : #include "common/keywords.h"
21 : #include "fe_utils/string_utils.h"
22 : #include "mb/pg_wchar.h"
23 :
24 : static PQExpBuffer defaultGetLocalPQExpBuffer(void);
25 :
26 : /* Globals exported by this file */
27 : int quote_all_identifiers = 0;
28 : PQExpBuffer (*getLocalPQExpBuffer) (void) = defaultGetLocalPQExpBuffer;
29 :
30 : static int fmtIdEncoding = -1;
31 :
32 :
33 : /*
34 : * Returns a temporary PQExpBuffer, valid until the next call to the function.
35 : * This is used by fmtId and fmtQualifiedId.
36 : *
37 : * Non-reentrant and non-thread-safe but reduces memory leakage. You can
38 : * replace this with a custom version by setting the getLocalPQExpBuffer
39 : * function pointer.
40 : */
41 : static PQExpBuffer
42 373079 : defaultGetLocalPQExpBuffer(void)
43 : {
44 : static PQExpBuffer id_return = NULL;
45 :
46 373079 : if (id_return) /* first time through? */
47 : {
48 : /* same buffer, just wipe contents */
49 372532 : resetPQExpBuffer(id_return);
50 : }
51 : else
52 : {
53 : /* new buffer */
54 547 : id_return = createPQExpBuffer();
55 : }
56 :
57 373079 : return id_return;
58 : }
59 :
60 : /*
61 : * Set the encoding that fmtId() and fmtQualifiedId() use.
62 : *
63 : * This is not safe against multiple connections having different encodings,
64 : * but there is no real other way to address the need to know the encoding for
65 : * fmtId()/fmtQualifiedId() input for safe escaping. Eventually we should get
66 : * rid of fmtId().
67 : */
68 : void
69 10554 : setFmtEncoding(int encoding)
70 : {
71 10554 : fmtIdEncoding = encoding;
72 10554 : }
73 :
74 : /*
75 : * Return the currently configured encoding for fmtId() and fmtQualifiedId().
76 : */
77 : static int
78 255622 : getFmtEncoding(void)
79 : {
80 255622 : if (fmtIdEncoding != -1)
81 255622 : return fmtIdEncoding;
82 :
83 : /*
84 : * In assertion builds it seems best to fail hard if the encoding was not
85 : * set, to make it easier to find places with missing calls. But in
86 : * production builds that seems like a bad idea, thus we instead just
87 : * default to UTF-8.
88 : */
89 : Assert(fmtIdEncoding != -1);
90 :
91 0 : return PG_UTF8;
92 : }
93 :
94 : /*
95 : * Quotes input string if it's not a legitimate SQL identifier as-is.
96 : *
97 : * Note that the returned string must be used before calling fmtIdEnc again,
98 : * since we re-use the same return buffer each time.
99 : */
100 : const char *
101 317140 : fmtIdEnc(const char *rawid, int encoding)
102 : {
103 317140 : PQExpBuffer id_return = getLocalPQExpBuffer();
104 :
105 : const char *cp;
106 317140 : bool need_quotes = false;
107 317140 : size_t remaining = strlen(rawid);
108 :
109 : /*
110 : * These checks need to match the identifier production in scan.l. Don't
111 : * use islower() etc.
112 : */
113 317140 : if (quote_all_identifiers)
114 22224 : need_quotes = true;
115 : /* slightly different rules for first character */
116 294916 : else if (!((rawid[0] >= 'a' && rawid[0] <= 'z') || rawid[0] == '_'))
117 707 : need_quotes = true;
118 : else
119 : {
120 : /* otherwise check the entire string */
121 294209 : cp = rawid;
122 3223497 : for (size_t i = 0; i < remaining; i++, cp++)
123 : {
124 2940325 : if (!((*cp >= 'a' && *cp <= 'z')
125 385668 : || (*cp >= '0' && *cp <= '9')
126 271068 : || (*cp == '_')))
127 : {
128 11037 : need_quotes = true;
129 11037 : break;
130 : }
131 : }
132 : }
133 :
134 317140 : if (!need_quotes)
135 : {
136 : /*
137 : * Check for keyword. We quote keywords except for unreserved ones.
138 : * (In some cases we could avoid quoting a col_name or type_func_name
139 : * keyword, but it seems much harder than it's worth to tell that.)
140 : *
141 : * Note: ScanKeywordLookup() does case-insensitive comparison, but
142 : * that's fine, since we already know we have all-lower-case.
143 : */
144 283172 : int kwnum = ScanKeywordLookup(rawid, &ScanKeywords);
145 :
146 283172 : if (kwnum >= 0 && ScanKeywordCategories[kwnum] != UNRESERVED_KEYWORD)
147 728 : need_quotes = true;
148 : }
149 :
150 317140 : if (!need_quotes)
151 : {
152 : /* no quoting needed */
153 282444 : appendPQExpBufferStr(id_return, rawid);
154 : }
155 : else
156 : {
157 34696 : appendPQExpBufferChar(id_return, '"');
158 :
159 34696 : cp = &rawid[0];
160 407282 : while (remaining > 0)
161 : {
162 : int charlen;
163 :
164 : /* Fast path for plain ASCII */
165 372586 : if (!IS_HIGHBIT_SET(*cp))
166 : {
167 : /*
168 : * Did we find a double-quote in the string? Then make this a
169 : * double double-quote per SQL99. Before, we put in a
170 : * backslash/double-quote pair. - thomas 2000-08-05
171 : */
172 370099 : if (*cp == '"')
173 329 : appendPQExpBufferChar(id_return, '"');
174 370099 : appendPQExpBufferChar(id_return, *cp);
175 370099 : remaining--;
176 370099 : cp++;
177 370099 : continue;
178 : }
179 :
180 : /* Slow path for possible multibyte characters */
181 2487 : charlen = pg_encoding_mblen(encoding, cp);
182 :
183 4946 : if (remaining < charlen ||
184 2459 : pg_encoding_verifymbchar(encoding, cp, charlen) == -1)
185 : {
186 : /*
187 : * Multibyte character is invalid. It's important to verify
188 : * that as invalid multibyte characters could e.g. be used to
189 : * "skip" over quote characters, e.g. when parsing
190 : * character-by-character.
191 : *
192 : * Replace the character's first byte with an invalid
193 : * sequence. The invalid sequence ensures that the escaped
194 : * string will trigger an error on the server-side, even if we
195 : * can't directly report an error here.
196 : *
197 : * It would be a bit faster to verify the whole string the
198 : * first time we encounter a set highbit, but this way we can
199 : * replace just the invalid data, which probably makes it
200 : * easier for users to find the invalidly encoded portion of a
201 : * larger string.
202 : */
203 41 : if (enlargePQExpBuffer(id_return, 2))
204 : {
205 41 : pg_encoding_set_invalid(encoding,
206 41 : id_return->data + id_return->len);
207 41 : id_return->len += 2;
208 41 : id_return->data[id_return->len] = '\0';
209 : }
210 :
211 : /*
212 : * Handle the following bytes as if this byte didn't exist.
213 : * That's safer in case the subsequent bytes contain
214 : * characters that are significant for the caller (e.g. '>' in
215 : * html).
216 : */
217 41 : remaining--;
218 41 : cp++;
219 : }
220 : else
221 : {
222 4905 : for (int i = 0; i < charlen; i++)
223 : {
224 2459 : appendPQExpBufferChar(id_return, *cp);
225 2459 : remaining--;
226 2459 : cp++;
227 : }
228 : }
229 : }
230 :
231 34696 : appendPQExpBufferChar(id_return, '"');
232 : }
233 :
234 317140 : return id_return->data;
235 : }
236 :
237 : /*
238 : * Quotes input string if it's not a legitimate SQL identifier as-is.
239 : *
240 : * Note that the returned string must be used before calling fmtId again,
241 : * since we re-use the same return buffer each time.
242 : *
243 : * NB: This assumes setFmtEncoding() previously has been called to configure
244 : * the encoding of rawid. It is preferable to use fmtIdEnc() with an
245 : * explicit encoding.
246 : */
247 : const char *
248 205095 : fmtId(const char *rawid)
249 : {
250 205095 : return fmtIdEnc(rawid, getFmtEncoding());
251 : }
252 :
253 : /*
254 : * fmtQualifiedIdEnc - construct a schema-qualified name, with quoting as
255 : * needed.
256 : *
257 : * Like fmtId, use the result before calling again.
258 : *
259 : * Since we call fmtId and it also uses getLocalPQExpBuffer() we cannot
260 : * use that buffer until we're finished with calling fmtId().
261 : */
262 : const char *
263 55939 : fmtQualifiedIdEnc(const char *schema, const char *id, int encoding)
264 : {
265 : PQExpBuffer id_return;
266 55939 : PQExpBuffer lcl_pqexp = createPQExpBuffer();
267 :
268 : /* Some callers might fail to provide a schema name */
269 55939 : if (schema && *schema)
270 : {
271 55939 : appendPQExpBuffer(lcl_pqexp, "%s.", fmtIdEnc(schema, encoding));
272 : }
273 55939 : appendPQExpBufferStr(lcl_pqexp, fmtIdEnc(id, encoding));
274 :
275 55939 : id_return = getLocalPQExpBuffer();
276 :
277 55939 : appendPQExpBufferStr(id_return, lcl_pqexp->data);
278 55939 : destroyPQExpBuffer(lcl_pqexp);
279 :
280 55939 : return id_return->data;
281 : }
282 :
283 : /*
284 : * fmtQualifiedId - construct a schema-qualified name, with quoting as needed.
285 : *
286 : * Like fmtId, use the result before calling again.
287 : *
288 : * Since we call fmtId and it also uses getLocalPQExpBuffer() we cannot
289 : * use that buffer until we're finished with calling fmtId().
290 : *
291 : * NB: This assumes setFmtEncoding() previously has been called to configure
292 : * the encoding of schema/id. It is preferable to use fmtQualifiedIdEnc()
293 : * with an explicit encoding.
294 : */
295 : const char *
296 50527 : fmtQualifiedId(const char *schema, const char *id)
297 : {
298 50527 : return fmtQualifiedIdEnc(schema, id, getFmtEncoding());
299 : }
300 :
301 :
302 : /*
303 : * Format a Postgres version number (in the PG_VERSION_NUM integer format
304 : * returned by PQserverVersion()) as a string. This exists mainly to
305 : * encapsulate knowledge about two-part vs. three-part version numbers.
306 : *
307 : * For reentrancy, caller must supply the buffer the string is put in.
308 : * Recommended size of the buffer is 32 bytes.
309 : *
310 : * Returns address of 'buf', as a notational convenience.
311 : */
312 : char *
313 0 : formatPGVersionNumber(int version_number, bool include_minor,
314 : char *buf, size_t buflen)
315 : {
316 0 : if (version_number >= 100000)
317 : {
318 : /* New two-part style */
319 0 : if (include_minor)
320 0 : snprintf(buf, buflen, "%d.%d", version_number / 10000,
321 : version_number % 10000);
322 : else
323 0 : snprintf(buf, buflen, "%d", version_number / 10000);
324 : }
325 : else
326 : {
327 : /* Old three-part style */
328 0 : if (include_minor)
329 0 : snprintf(buf, buflen, "%d.%d.%d", version_number / 10000,
330 0 : (version_number / 100) % 100,
331 : version_number % 100);
332 : else
333 0 : snprintf(buf, buflen, "%d.%d", version_number / 10000,
334 0 : (version_number / 100) % 100);
335 : }
336 0 : return buf;
337 : }
338 :
339 :
340 : /*
341 : * Convert a string value to an SQL string literal and append it to
342 : * the given buffer. We assume the specified client_encoding and
343 : * standard_conforming_strings settings.
344 : *
345 : * This is essentially equivalent to libpq's PQescapeStringInternal,
346 : * except for the output buffer structure. We need it in situations
347 : * where we do not have a PGconn available. Where we do,
348 : * appendStringLiteralConn is a better choice.
349 : */
350 : void
351 38245 : appendStringLiteral(PQExpBuffer buf, const char *str,
352 : int encoding, bool std_strings)
353 : {
354 38245 : size_t length = strlen(str);
355 38245 : const char *source = str;
356 : char *target;
357 38245 : size_t remaining = length;
358 :
359 38245 : if (!enlargePQExpBuffer(buf, 2 * length + 2))
360 0 : return;
361 :
362 38245 : target = buf->data + buf->len;
363 38245 : *target++ = '\'';
364 :
365 921688 : while (remaining > 0)
366 : {
367 883443 : char c = *source;
368 : int charlen;
369 : int i;
370 :
371 : /* Fast path for plain ASCII */
372 883443 : if (!IS_HIGHBIT_SET(c))
373 : {
374 : /* Apply quoting if needed */
375 883388 : if (SQL_STR_DOUBLE(c, !std_strings))
376 231 : *target++ = c;
377 : /* Copy the character */
378 883388 : *target++ = c;
379 883388 : source++;
380 883388 : remaining--;
381 883388 : continue;
382 : }
383 :
384 : /* Slow path for possible multibyte characters */
385 55 : charlen = PQmblen(source, encoding);
386 :
387 82 : if (remaining < charlen ||
388 27 : pg_encoding_verifymbchar(encoding, source, charlen) == -1)
389 : {
390 : /*
391 : * Multibyte character is invalid. It's important to verify that
392 : * as invalid multibyte characters could e.g. be used to "skip"
393 : * over quote characters, e.g. when parsing
394 : * character-by-character.
395 : *
396 : * Replace the character's first byte with an invalid sequence.
397 : * The invalid sequence ensures that the escaped string will
398 : * trigger an error on the server-side, even if we can't directly
399 : * report an error here.
400 : *
401 : * We know there's enough space for the invalid sequence because
402 : * the "target" buffer is 2 * length + 2 long, and at worst we're
403 : * replacing a single input byte with two invalid bytes.
404 : *
405 : * It would be a bit faster to verify the whole string the first
406 : * time we encounter a set highbit, but this way we can replace
407 : * just the invalid data, which probably makes it easier for users
408 : * to find the invalidly encoded portion of a larger string.
409 : */
410 41 : pg_encoding_set_invalid(encoding, target);
411 41 : target += 2;
412 :
413 : /*
414 : * Handle the following bytes as if this byte didn't exist. That's
415 : * safer in case the subsequent bytes contain important characters
416 : * for the caller (e.g. '>' in html).
417 : */
418 41 : source++;
419 41 : remaining--;
420 : }
421 : else
422 : {
423 : /* Copy the character */
424 41 : for (i = 0; i < charlen; i++)
425 : {
426 27 : *target++ = *source++;
427 27 : remaining--;
428 : }
429 : }
430 : }
431 :
432 : /* Write the terminating quote and NUL character. */
433 38245 : *target++ = '\'';
434 38245 : *target = '\0';
435 :
436 38245 : buf->len = target - buf->data;
437 : }
438 :
439 :
440 : /*
441 : * Convert a string value to an SQL string literal and append it to
442 : * the given buffer. Encoding and string syntax rules are as indicated
443 : * by current settings of the PGconn.
444 : */
445 : void
446 4923 : appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
447 : {
448 4923 : size_t length = strlen(str);
449 :
450 : /*
451 : * XXX This is a kluge to silence escape_string_warning in our utility
452 : * programs. It can go away once pre-v19 servers are out of support.
453 : */
454 4923 : if (strchr(str, '\\') != NULL && PQserverVersion(conn) < 190000)
455 : {
456 : /* ensure we are not adjacent to an identifier */
457 0 : if (buf->len > 0 && buf->data[buf->len - 1] != ' ')
458 0 : appendPQExpBufferChar(buf, ' ');
459 0 : appendPQExpBufferChar(buf, ESCAPE_STRING_SYNTAX);
460 0 : appendStringLiteral(buf, str, PQclientEncoding(conn), false);
461 0 : return;
462 : }
463 : /* XXX end kluge */
464 :
465 4923 : if (!enlargePQExpBuffer(buf, 2 * length + 2))
466 0 : return;
467 4923 : appendPQExpBufferChar(buf, '\'');
468 4923 : buf->len += PQescapeStringConn(conn, buf->data + buf->len,
469 : str, length, NULL);
470 4923 : appendPQExpBufferChar(buf, '\'');
471 : }
472 :
473 :
474 : /*
475 : * Convert a string value to a dollar quoted literal and append it to
476 : * the given buffer. If the dqprefix parameter is not NULL then the
477 : * dollar quote delimiter will begin with that (after the opening $).
478 : *
479 : * No escaping is done at all on str, in compliance with the rules
480 : * for parsing dollar quoted strings. Also, we need not worry about
481 : * encoding issues.
482 : */
483 : void
484 1580 : appendStringLiteralDQ(PQExpBuffer buf, const char *str, const char *dqprefix)
485 : {
486 : static const char suffixes[] = "_XXXXXXX";
487 1580 : int nextchar = 0;
488 1580 : PQExpBuffer delimBuf = createPQExpBuffer();
489 :
490 : /* start with $ + dqprefix if not NULL */
491 1580 : appendPQExpBufferChar(delimBuf, '$');
492 1580 : if (dqprefix)
493 0 : appendPQExpBufferStr(delimBuf, dqprefix);
494 :
495 : /*
496 : * Make sure we choose a delimiter which (without the trailing $) is not
497 : * present in the string being quoted. We don't check with the trailing $
498 : * because a string ending in $foo must not be quoted with $foo$.
499 : */
500 2091 : while (strstr(str, delimBuf->data) != NULL)
501 : {
502 511 : appendPQExpBufferChar(delimBuf, suffixes[nextchar++]);
503 511 : nextchar %= sizeof(suffixes) - 1;
504 : }
505 :
506 : /* add trailing $ */
507 1580 : appendPQExpBufferChar(delimBuf, '$');
508 :
509 : /* quote it and we are all done */
510 1580 : appendPQExpBufferStr(buf, delimBuf->data);
511 1580 : appendPQExpBufferStr(buf, str);
512 1580 : appendPQExpBufferStr(buf, delimBuf->data);
513 :
514 1580 : destroyPQExpBuffer(delimBuf);
515 1580 : }
516 :
517 :
518 : /*
519 : * Convert a bytea value (presented as raw bytes) to an SQL string literal
520 : * and append it to the given buffer. We assume the specified
521 : * standard_conforming_strings setting.
522 : *
523 : * This is needed in situations where we do not have a PGconn available.
524 : * Where we do, PQescapeByteaConn is a better choice.
525 : */
526 : void
527 45 : appendByteaLiteral(PQExpBuffer buf, const unsigned char *str, size_t length,
528 : bool std_strings)
529 : {
530 45 : const unsigned char *source = str;
531 : char *target;
532 :
533 : static const char hextbl[] = "0123456789abcdef";
534 :
535 : /*
536 : * This implementation is hard-wired to produce hex-format output. We do
537 : * not know the server version the output will be loaded into, so making
538 : * an intelligent format choice is impossible. It might be better to
539 : * always use the old escaped format.
540 : */
541 45 : if (!enlargePQExpBuffer(buf, 2 * length + 5))
542 0 : return;
543 :
544 45 : target = buf->data + buf->len;
545 45 : *target++ = '\'';
546 45 : if (!std_strings)
547 0 : *target++ = '\\';
548 45 : *target++ = '\\';
549 45 : *target++ = 'x';
550 :
551 4119 : while (length-- > 0)
552 : {
553 4074 : unsigned char c = *source++;
554 :
555 4074 : *target++ = hextbl[(c >> 4) & 0xF];
556 4074 : *target++ = hextbl[c & 0xF];
557 : }
558 :
559 : /* Write the terminating quote and NUL character. */
560 45 : *target++ = '\'';
561 45 : *target = '\0';
562 :
563 45 : buf->len = target - buf->data;
564 : }
565 :
566 :
567 : /*
568 : * Append the given string to the shell command being built in the buffer,
569 : * with shell-style quoting as needed to create exactly one argument.
570 : *
571 : * appendShellString() simply prints an error and dies if LF or CR appears.
572 : * appendShellStringNoError() omits those characters from the result, and
573 : * returns false if there were any.
574 : */
575 : void
576 601 : appendShellString(PQExpBuffer buf, const char *str)
577 : {
578 601 : if (!appendShellStringNoError(buf, str))
579 : {
580 0 : fprintf(stderr,
581 0 : _("shell command argument contains a newline or carriage return: \"%s\"\n"),
582 : str);
583 0 : exit(EXIT_FAILURE);
584 : }
585 601 : }
586 :
587 : bool
588 601 : appendShellStringNoError(PQExpBuffer buf, const char *str)
589 : {
590 : #ifdef WIN32
591 : int backslash_run_length = 0;
592 : #endif
593 601 : bool ok = true;
594 : const char *p;
595 :
596 : /*
597 : * Don't bother with adding quotes if the string is nonempty and clearly
598 : * contains only safe characters.
599 : */
600 601 : if (*str != '\0' &&
601 601 : strspn(str, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_./:") == strlen(str))
602 : {
603 445 : appendPQExpBufferStr(buf, str);
604 445 : return ok;
605 : }
606 :
607 : #ifndef WIN32
608 156 : appendPQExpBufferChar(buf, '\'');
609 4423 : for (p = str; *p; p++)
610 : {
611 4267 : if (*p == '\n' || *p == '\r')
612 : {
613 0 : ok = false;
614 0 : continue;
615 : }
616 :
617 4267 : if (*p == '\'')
618 84 : appendPQExpBufferStr(buf, "'\"'\"'");
619 : else
620 4183 : appendPQExpBufferChar(buf, *p);
621 : }
622 156 : appendPQExpBufferChar(buf, '\'');
623 : #else /* WIN32 */
624 :
625 : /*
626 : * A Windows system() argument experiences two layers of interpretation.
627 : * First, cmd.exe interprets the string. Its behavior is undocumented,
628 : * but a caret escapes any byte except LF or CR that would otherwise have
629 : * special meaning. Handling of a caret before LF or CR differs between
630 : * "cmd.exe /c" and other modes, and it is unusable here.
631 : *
632 : * Second, the new process parses its command line to construct argv (see
633 : * https://msdn.microsoft.com/en-us/library/17w5ykft.aspx). This treats
634 : * backslash-double quote sequences specially.
635 : */
636 : appendPQExpBufferStr(buf, "^\"");
637 : for (p = str; *p; p++)
638 : {
639 : if (*p == '\n' || *p == '\r')
640 : {
641 : ok = false;
642 : continue;
643 : }
644 :
645 : /* Change N backslashes before a double quote to 2N+1 backslashes. */
646 : if (*p == '"')
647 : {
648 : while (backslash_run_length)
649 : {
650 : appendPQExpBufferStr(buf, "^\\");
651 : backslash_run_length--;
652 : }
653 : appendPQExpBufferStr(buf, "^\\");
654 : }
655 : else if (*p == '\\')
656 : backslash_run_length++;
657 : else
658 : backslash_run_length = 0;
659 :
660 : /*
661 : * Decline to caret-escape the most mundane characters, to ease
662 : * debugging and lest we approach the command length limit.
663 : */
664 : if (!((*p >= 'a' && *p <= 'z') ||
665 : (*p >= 'A' && *p <= 'Z') ||
666 : (*p >= '0' && *p <= '9')))
667 : appendPQExpBufferChar(buf, '^');
668 : appendPQExpBufferChar(buf, *p);
669 : }
670 :
671 : /*
672 : * Change N backslashes at end of argument to 2N backslashes, because they
673 : * precede the double quote that terminates the argument.
674 : */
675 : while (backslash_run_length)
676 : {
677 : appendPQExpBufferStr(buf, "^\\");
678 : backslash_run_length--;
679 : }
680 : appendPQExpBufferStr(buf, "^\"");
681 : #endif /* WIN32 */
682 :
683 156 : return ok;
684 : }
685 :
686 :
687 : /*
688 : * Append the given string to the buffer, with suitable quoting for passing
689 : * the string as a value in a keyword/value pair in a libpq connection string.
690 : */
691 : void
692 2408 : appendConnStrVal(PQExpBuffer buf, const char *str)
693 : {
694 : const char *s;
695 : bool needquotes;
696 :
697 : /*
698 : * If the string is one or more plain ASCII characters, no need to quote
699 : * it. This is quite conservative, but better safe than sorry.
700 : */
701 2408 : needquotes = true;
702 16422 : for (s = str; *s; s++)
703 : {
704 14799 : if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
705 1886 : (*s >= '0' && *s <= '9') || *s == '_' || *s == '.'))
706 : {
707 785 : needquotes = true;
708 785 : break;
709 : }
710 14014 : needquotes = false;
711 : }
712 :
713 2408 : if (needquotes)
714 : {
715 785 : appendPQExpBufferChar(buf, '\'');
716 16800 : while (*str)
717 : {
718 : /* ' and \ must be escaped by to \' and \\ */
719 16015 : if (*str == '\'' || *str == '\\')
720 302 : appendPQExpBufferChar(buf, '\\');
721 :
722 16015 : appendPQExpBufferChar(buf, *str);
723 16015 : str++;
724 : }
725 785 : appendPQExpBufferChar(buf, '\'');
726 : }
727 : else
728 1623 : appendPQExpBufferStr(buf, str);
729 2408 : }
730 :
731 :
732 : /*
733 : * Append a psql meta-command that connects to the given database with the
734 : * then-current connection's user, host and port.
735 : */
736 : void
737 131 : appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname)
738 : {
739 : const char *s;
740 : bool complex;
741 :
742 : /*
743 : * If the name is plain ASCII characters, emit a trivial "\connect "foo"".
744 : * For other names, even many not technically requiring it, skip to the
745 : * general case. No database has a zero-length name.
746 : */
747 131 : complex = false;
748 :
749 1610 : for (s = dbname; *s; s++)
750 : {
751 1479 : if (*s == '\n' || *s == '\r')
752 : {
753 0 : fprintf(stderr,
754 0 : _("database name contains a newline or carriage return: \"%s\"\n"),
755 : dbname);
756 0 : exit(EXIT_FAILURE);
757 : }
758 :
759 1479 : if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
760 459 : (*s >= '0' && *s <= '9') || *s == '_' || *s == '.'))
761 : {
762 327 : complex = true;
763 : }
764 : }
765 :
766 131 : if (complex)
767 : {
768 : PQExpBufferData connstr;
769 :
770 10 : initPQExpBuffer(&connstr);
771 :
772 : /*
773 : * Force the target psql's encoding to SQL_ASCII. We don't really
774 : * know the encoding of the database name, and it doesn't matter as
775 : * long as psql will forward it to the server unchanged.
776 : */
777 10 : appendPQExpBufferStr(buf, "\\encoding SQL_ASCII\n");
778 10 : appendPQExpBufferStr(buf, "\\connect -reuse-previous=on ");
779 :
780 10 : appendPQExpBufferStr(&connstr, "dbname=");
781 10 : appendConnStrVal(&connstr, dbname);
782 :
783 : /*
784 : * As long as the name does not contain a newline, SQL identifier
785 : * quoting satisfies the psql meta-command parser. Prefer not to
786 : * involve psql-interpreted single quotes, which behaved differently
787 : * before PostgreSQL 9.2.
788 : */
789 10 : appendPQExpBufferStr(buf, fmtIdEnc(connstr.data, PG_SQL_ASCII));
790 :
791 10 : termPQExpBuffer(&connstr);
792 : }
793 : else
794 : {
795 121 : appendPQExpBufferStr(buf, "\\connect ");
796 121 : appendPQExpBufferStr(buf, fmtIdEnc(dbname, PG_SQL_ASCII));
797 : }
798 131 : appendPQExpBufferChar(buf, '\n');
799 131 : }
800 :
801 :
802 : /*
803 : * Deconstruct the text representation of a 1-dimensional Postgres array
804 : * into individual items.
805 : *
806 : * On success, returns true and sets *itemarray and *nitems to describe
807 : * an array of individual strings. On parse failure, returns false;
808 : * *itemarray may exist or be NULL.
809 : *
810 : * NOTE: free'ing itemarray is sufficient to deallocate the working storage.
811 : */
812 : bool
813 82614 : parsePGArray(const char *atext, char ***itemarray, int *nitems)
814 : {
815 : int inputlen;
816 : char **items;
817 : char *strings;
818 : int curitem;
819 :
820 : /*
821 : * We expect input in the form of "{item,item,item}" where any item is
822 : * either raw data, or surrounded by double quotes (in which case embedded
823 : * characters including backslashes and quotes are backslashed).
824 : *
825 : * We build the result as an array of pointers followed by the actual
826 : * string data, all in one malloc block for convenience of deallocation.
827 : * The worst-case storage need is not more than one pointer and one
828 : * character for each input character (consider "{,,,,,,,,,,}").
829 : */
830 82614 : *itemarray = NULL;
831 82614 : *nitems = 0;
832 82614 : inputlen = strlen(atext);
833 82614 : if (inputlen < 2 || atext[0] != '{' || atext[inputlen - 1] != '}')
834 0 : return false; /* bad input */
835 82614 : items = (char **) malloc(inputlen * (sizeof(char *) + sizeof(char)));
836 82614 : if (items == NULL)
837 0 : return false; /* out of memory */
838 82614 : *itemarray = items;
839 82614 : strings = (char *) (items + inputlen);
840 :
841 82614 : atext++; /* advance over initial '{' */
842 82614 : curitem = 0;
843 228203 : while (*atext != '}')
844 : {
845 145589 : if (*atext == '\0')
846 0 : return false; /* premature end of string */
847 145589 : items[curitem] = strings;
848 2901949 : while (*atext != '}' && *atext != ',')
849 : {
850 2756360 : if (*atext == '\0')
851 0 : return false; /* premature end of string */
852 2756360 : if (*atext != '"')
853 2756173 : *strings++ = *atext++; /* copy unquoted data */
854 : else
855 : {
856 : /* process quoted substring */
857 187 : atext++;
858 6324 : while (*atext != '"')
859 : {
860 6137 : if (*atext == '\0')
861 0 : return false; /* premature end of string */
862 6137 : if (*atext == '\\')
863 : {
864 925 : atext++;
865 925 : if (*atext == '\0')
866 0 : return false; /* premature end of string */
867 : }
868 6137 : *strings++ = *atext++; /* copy quoted data */
869 : }
870 187 : atext++;
871 : }
872 : }
873 145589 : *strings++ = '\0';
874 145589 : if (*atext == ',')
875 64390 : atext++;
876 145589 : curitem++;
877 : }
878 82614 : if (atext[1] != '\0')
879 0 : return false; /* bogus syntax (embedded '}') */
880 82614 : *nitems = curitem;
881 82614 : return true;
882 : }
883 :
884 :
885 : /*
886 : * Append one element to the text representation of a 1-dimensional Postgres
887 : * array.
888 : *
889 : * The caller must provide the initial '{' and closing '}' of the array.
890 : * This function handles all else, including insertion of commas and
891 : * quoting of values.
892 : *
893 : * We assume that typdelim is ','.
894 : */
895 : void
896 7150 : appendPGArray(PQExpBuffer buffer, const char *value)
897 : {
898 : bool needquote;
899 : const char *tmp;
900 :
901 7150 : if (buffer->data[buffer->len - 1] != '{')
902 6686 : appendPQExpBufferChar(buffer, ',');
903 :
904 : /* Decide if we need quotes; this should match array_out()'s choices. */
905 7150 : if (value[0] == '\0')
906 0 : needquote = true; /* force quotes for empty string */
907 7150 : else if (pg_strcasecmp(value, "NULL") == 0)
908 0 : needquote = true; /* force quotes for literal NULL */
909 : else
910 7150 : needquote = false;
911 :
912 7150 : if (!needquote)
913 : {
914 110306 : for (tmp = value; *tmp; tmp++)
915 : {
916 103317 : char ch = *tmp;
917 :
918 103317 : if (ch == '"' || ch == '\\' ||
919 103186 : ch == '{' || ch == '}' || ch == ',' ||
920 : /* these match scanner_isspace(): */
921 103186 : ch == ' ' || ch == '\t' || ch == '\n' ||
922 103156 : ch == '\r' || ch == '\v' || ch == '\f')
923 : {
924 161 : needquote = true;
925 161 : break;
926 : }
927 : }
928 : }
929 :
930 7150 : if (needquote)
931 : {
932 161 : appendPQExpBufferChar(buffer, '"');
933 5850 : for (tmp = value; *tmp; tmp++)
934 : {
935 5689 : char ch = *tmp;
936 :
937 5689 : if (ch == '"' || ch == '\\')
938 812 : appendPQExpBufferChar(buffer, '\\');
939 5689 : appendPQExpBufferChar(buffer, ch);
940 : }
941 161 : appendPQExpBufferChar(buffer, '"');
942 : }
943 : else
944 6989 : appendPQExpBufferStr(buffer, value);
945 7150 : }
946 :
947 :
948 : /*
949 : * Format a reloptions array and append it to the given buffer.
950 : *
951 : * "prefix" is prepended to the option names; typically it's "" or "toast.".
952 : *
953 : * Returns false if the reloptions array could not be parsed (in which case
954 : * nothing will have been appended to the buffer), or true on success.
955 : *
956 : * Note: this logic should generally match the backend's flatten_reloptions()
957 : * (in adt/ruleutils.c).
958 : */
959 : bool
960 219 : appendReloptionsArray(PQExpBuffer buffer, const char *reloptions,
961 : const char *prefix, int encoding, bool std_strings)
962 : {
963 : char **options;
964 : int noptions;
965 : int i;
966 :
967 219 : if (!parsePGArray(reloptions, &options, &noptions))
968 : {
969 0 : free(options);
970 0 : return false;
971 : }
972 :
973 495 : for (i = 0; i < noptions; i++)
974 : {
975 276 : char *option = options[i];
976 : char *name;
977 : char *separator;
978 : char *value;
979 :
980 : /*
981 : * Each array element should have the form name=value. If the "=" is
982 : * missing for some reason, treat it like an empty value.
983 : */
984 276 : name = option;
985 276 : separator = strchr(option, '=');
986 276 : if (separator)
987 : {
988 276 : *separator = '\0';
989 276 : value = separator + 1;
990 : }
991 : else
992 0 : value = "";
993 :
994 276 : if (i > 0)
995 57 : appendPQExpBufferStr(buffer, ", ");
996 276 : appendPQExpBuffer(buffer, "%s%s=", prefix, fmtId(name));
997 :
998 : /*
999 : * In general we need to quote the value; but to avoid unnecessary
1000 : * clutter, do not quote if it is an identifier that would not need
1001 : * quoting. (We could also allow numbers, but that is a bit trickier
1002 : * than it looks --- for example, are leading zeroes significant? We
1003 : * don't want to assume very much here about what custom reloptions
1004 : * might mean.)
1005 : */
1006 276 : if (strcmp(fmtId(value), value) == 0)
1007 32 : appendPQExpBufferStr(buffer, value);
1008 : else
1009 244 : appendStringLiteral(buffer, value, encoding, std_strings);
1010 : }
1011 :
1012 219 : free(options);
1013 :
1014 219 : return true;
1015 : }
1016 :
1017 :
1018 : /*
1019 : * processSQLNamePattern
1020 : *
1021 : * Scan a wildcard-pattern string and generate appropriate WHERE clauses
1022 : * to limit the set of objects returned. The WHERE clauses are appended
1023 : * to the already-partially-constructed query in buf. Returns whether
1024 : * any clause was added.
1025 : *
1026 : * conn: connection query will be sent to (consulted for escaping rules).
1027 : * buf: output parameter.
1028 : * pattern: user-specified pattern option, or NULL if none ("*" is implied).
1029 : * have_where: true if caller already emitted "WHERE" (clauses will be ANDed
1030 : * onto the existing WHERE clause).
1031 : * force_escape: always quote regexp special characters, even outside
1032 : * double quotes (else they are quoted only between double quotes).
1033 : * schemavar: name of query variable to match against a schema-name pattern.
1034 : * Can be NULL if no schema.
1035 : * namevar: name of query variable to match against an object-name pattern.
1036 : * altnamevar: NULL, or name of an alternative variable to match against name.
1037 : * visibilityrule: clause to use if we want to restrict to visible objects
1038 : * (for example, "pg_catalog.pg_table_is_visible(p.oid)"). Can be NULL.
1039 : * dbnamebuf: output parameter receiving the database name portion of the
1040 : * pattern, if any. Can be NULL.
1041 : * dotcnt: how many separators were parsed from the pattern, by reference.
1042 : *
1043 : * Formatting note: the text already present in buf should end with a newline.
1044 : * The appended text, if any, will end with one too.
1045 : */
1046 : bool
1047 3698 : processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern,
1048 : bool have_where, bool force_escape,
1049 : const char *schemavar, const char *namevar,
1050 : const char *altnamevar, const char *visibilityrule,
1051 : PQExpBuffer dbnamebuf, int *dotcnt)
1052 : {
1053 : PQExpBufferData schemabuf;
1054 : PQExpBufferData namebuf;
1055 3698 : bool added_clause = false;
1056 : int dcnt;
1057 :
1058 : #define WHEREAND() \
1059 : (appendPQExpBufferStr(buf, have_where ? " AND " : "WHERE "), \
1060 : have_where = true, added_clause = true)
1061 :
1062 3698 : if (dotcnt == NULL)
1063 6 : dotcnt = &dcnt;
1064 3698 : *dotcnt = 0;
1065 3698 : if (pattern == NULL)
1066 : {
1067 : /* Default: select all visible objects */
1068 254 : if (visibilityrule)
1069 : {
1070 60 : WHEREAND();
1071 60 : appendPQExpBuffer(buf, "%s\n", visibilityrule);
1072 : }
1073 254 : return added_clause;
1074 : }
1075 :
1076 3444 : initPQExpBuffer(&schemabuf);
1077 3444 : initPQExpBuffer(&namebuf);
1078 :
1079 : /*
1080 : * Convert shell-style 'pattern' into the regular expression(s) we want to
1081 : * execute. Quoting/escaping into SQL literal format will be done below
1082 : * using appendStringLiteralConn().
1083 : *
1084 : * If the caller provided a schemavar, we want to split the pattern on
1085 : * ".", otherwise not.
1086 : */
1087 3444 : patternToSQLRegex(PQclientEncoding(conn),
1088 : (schemavar ? dbnamebuf : NULL),
1089 : (schemavar ? &schemabuf : NULL),
1090 : &namebuf,
1091 : pattern, force_escape, true, dotcnt);
1092 :
1093 : /*
1094 : * Now decide what we need to emit. We may run under a hostile
1095 : * search_path, so qualify EVERY name. Note there will be a leading "^("
1096 : * in the patterns in any case.
1097 : *
1098 : * We want the regex matches to use the database's default collation where
1099 : * collation-sensitive behavior is required (for example, which characters
1100 : * match '\w'). That happened by default before PG v12, but if the server
1101 : * is >= v12 then we need to force it through explicit COLLATE clauses,
1102 : * otherwise the "C" collation attached to "name" catalog columns wins.
1103 : */
1104 3444 : if (namevar && namebuf.len > 2)
1105 : {
1106 : /* We have a name pattern, so constrain the namevar(s) */
1107 :
1108 : /* Optimize away a "*" pattern */
1109 3444 : if (strcmp(namebuf.data, "^(.*)$") != 0)
1110 : {
1111 3402 : WHEREAND();
1112 3402 : if (altnamevar)
1113 : {
1114 114 : appendPQExpBuffer(buf,
1115 : "(%s OPERATOR(pg_catalog.~) ", namevar);
1116 114 : appendStringLiteralConn(buf, namebuf.data, conn);
1117 114 : if (PQserverVersion(conn) >= 120000)
1118 114 : appendPQExpBufferStr(buf, " COLLATE pg_catalog.default");
1119 114 : appendPQExpBuffer(buf,
1120 : "\n OR %s OPERATOR(pg_catalog.~) ",
1121 : altnamevar);
1122 114 : appendStringLiteralConn(buf, namebuf.data, conn);
1123 114 : if (PQserverVersion(conn) >= 120000)
1124 114 : appendPQExpBufferStr(buf, " COLLATE pg_catalog.default");
1125 114 : appendPQExpBufferStr(buf, ")\n");
1126 : }
1127 : else
1128 : {
1129 3288 : appendPQExpBuffer(buf, "%s OPERATOR(pg_catalog.~) ", namevar);
1130 3288 : appendStringLiteralConn(buf, namebuf.data, conn);
1131 3288 : if (PQserverVersion(conn) >= 120000)
1132 3288 : appendPQExpBufferStr(buf, " COLLATE pg_catalog.default");
1133 3288 : appendPQExpBufferChar(buf, '\n');
1134 : }
1135 : }
1136 : }
1137 :
1138 3444 : if (schemavar && schemabuf.len > 2)
1139 : {
1140 : /* We have a schema pattern, so constrain the schemavar */
1141 :
1142 : /* Optimize away a "*" pattern */
1143 1441 : if (strcmp(schemabuf.data, "^(.*)$") != 0 && schemavar)
1144 : {
1145 719 : WHEREAND();
1146 719 : appendPQExpBuffer(buf, "%s OPERATOR(pg_catalog.~) ", schemavar);
1147 719 : appendStringLiteralConn(buf, schemabuf.data, conn);
1148 719 : if (PQserverVersion(conn) >= 120000)
1149 719 : appendPQExpBufferStr(buf, " COLLATE pg_catalog.default");
1150 719 : appendPQExpBufferChar(buf, '\n');
1151 : }
1152 : }
1153 : else
1154 : {
1155 : /* No schema pattern given, so select only visible objects */
1156 2722 : if (visibilityrule)
1157 : {
1158 2172 : WHEREAND();
1159 2172 : appendPQExpBuffer(buf, "%s\n", visibilityrule);
1160 : }
1161 : }
1162 :
1163 3444 : termPQExpBuffer(&schemabuf);
1164 3444 : termPQExpBuffer(&namebuf);
1165 :
1166 3444 : return added_clause;
1167 : #undef WHEREAND
1168 : }
1169 :
1170 : /*
1171 : * Transform a possibly qualified shell-style object name pattern into up to
1172 : * three SQL-style regular expressions, converting quotes, lower-casing
1173 : * unquoted letters, and adjusting shell-style wildcard characters into regexp
1174 : * notation.
1175 : *
1176 : * If the dbnamebuf and schemabuf arguments are non-NULL, and the pattern
1177 : * contains two or more dbname/schema/name separators, we parse the portions of
1178 : * the pattern prior to the first and second separators into dbnamebuf and
1179 : * schemabuf, and the rest into namebuf.
1180 : *
1181 : * If dbnamebuf is NULL and schemabuf is non-NULL, and the pattern contains at
1182 : * least one separator, we parse the first portion into schemabuf and the rest
1183 : * into namebuf.
1184 : *
1185 : * Otherwise, we parse all the pattern into namebuf.
1186 : *
1187 : * If the pattern contains more dotted parts than buffers to parse into, the
1188 : * extra dots will be treated as literal characters and written into the
1189 : * namebuf, though they will be counted. Callers should always check the value
1190 : * returned by reference in dotcnt and handle this error case appropriately.
1191 : *
1192 : * We surround the regexps with "^(...)$" to force them to match whole strings,
1193 : * as per SQL practice. We have to have parens in case strings contain "|",
1194 : * else the "^" and "$" will be bound into the first and last alternatives
1195 : * which is not what we want. Whether this is done for dbnamebuf is controlled
1196 : * by the want_literal_dbname parameter.
1197 : *
1198 : * The regexps we parse into the buffers are appended to the data (if any)
1199 : * already present. If we parse fewer fields than the number of buffers we
1200 : * were given, the extra buffers are unaltered.
1201 : *
1202 : * encoding: the character encoding for the given pattern
1203 : * dbnamebuf: output parameter receiving the database name portion of the
1204 : * pattern, if any. Can be NULL.
1205 : * schemabuf: output parameter receiving the schema name portion of the
1206 : * pattern, if any. Can be NULL.
1207 : * namebuf: output parameter receiving the database name portion of the
1208 : * pattern, if any. Can be NULL.
1209 : * pattern: user-specified pattern option, or NULL if none ("*" is implied).
1210 : * force_escape: always quote regexp special characters, even outside
1211 : * double quotes (else they are quoted only between double quotes).
1212 : * want_literal_dbname: if true, regexp special characters within the database
1213 : * name portion of the pattern will not be escaped, nor will the dbname be
1214 : * converted into a regular expression.
1215 : * dotcnt: output parameter receiving the number of separators parsed from the
1216 : * pattern.
1217 : */
1218 : void
1219 3546 : patternToSQLRegex(int encoding, PQExpBuffer dbnamebuf, PQExpBuffer schemabuf,
1220 : PQExpBuffer namebuf, const char *pattern, bool force_escape,
1221 : bool want_literal_dbname, int *dotcnt)
1222 : {
1223 : PQExpBufferData buf[3];
1224 : PQExpBufferData left_literal;
1225 : PQExpBuffer curbuf;
1226 : PQExpBuffer maxbuf;
1227 : int i;
1228 : bool inquotes;
1229 : bool left;
1230 : const char *cp;
1231 :
1232 : Assert(pattern != NULL);
1233 : Assert(namebuf != NULL);
1234 :
1235 : /* callers should never expect "dbname.relname" format */
1236 : Assert(dbnamebuf == NULL || schemabuf != NULL);
1237 : Assert(dotcnt != NULL);
1238 :
1239 3546 : *dotcnt = 0;
1240 3546 : inquotes = false;
1241 3546 : cp = pattern;
1242 :
1243 3546 : if (dbnamebuf != NULL)
1244 2942 : maxbuf = &buf[2];
1245 604 : else if (schemabuf != NULL)
1246 29 : maxbuf = &buf[1];
1247 : else
1248 575 : maxbuf = &buf[0];
1249 :
1250 3546 : curbuf = &buf[0];
1251 3546 : if (want_literal_dbname)
1252 : {
1253 3444 : left = true;
1254 3444 : initPQExpBuffer(&left_literal);
1255 : }
1256 : else
1257 102 : left = false;
1258 3546 : initPQExpBuffer(curbuf);
1259 3546 : appendPQExpBufferStr(curbuf, "^(");
1260 62240 : while (*cp)
1261 : {
1262 58694 : char ch = *cp;
1263 :
1264 58694 : if (ch == '"')
1265 : {
1266 1607 : if (inquotes && cp[1] == '"')
1267 : {
1268 : /* emit one quote, stay in inquotes mode */
1269 3 : appendPQExpBufferChar(curbuf, '"');
1270 3 : if (left)
1271 3 : appendPQExpBufferChar(&left_literal, '"');
1272 3 : cp++;
1273 : }
1274 : else
1275 1604 : inquotes = !inquotes;
1276 1607 : cp++;
1277 : }
1278 57087 : else if (!inquotes && isupper((unsigned char) ch))
1279 : {
1280 120 : appendPQExpBufferChar(curbuf,
1281 120 : pg_tolower((unsigned char) ch));
1282 120 : if (left)
1283 75 : appendPQExpBufferChar(&left_literal,
1284 75 : pg_tolower((unsigned char) ch));
1285 120 : cp++;
1286 : }
1287 56967 : else if (!inquotes && ch == '*')
1288 : {
1289 215 : appendPQExpBufferStr(curbuf, ".*");
1290 215 : if (left)
1291 162 : appendPQExpBufferChar(&left_literal, '*');
1292 215 : cp++;
1293 : }
1294 56752 : else if (!inquotes && ch == '?')
1295 : {
1296 3 : appendPQExpBufferChar(curbuf, '.');
1297 3 : if (left)
1298 3 : appendPQExpBufferChar(&left_literal, '?');
1299 3 : cp++;
1300 : }
1301 56749 : else if (!inquotes && ch == '.')
1302 : {
1303 1417 : left = false;
1304 1417 : if (dotcnt)
1305 1417 : (*dotcnt)++;
1306 1417 : if (curbuf < maxbuf)
1307 : {
1308 1125 : appendPQExpBufferStr(curbuf, ")$");
1309 1125 : curbuf++;
1310 1125 : initPQExpBuffer(curbuf);
1311 1125 : appendPQExpBufferStr(curbuf, "^(");
1312 1125 : cp++;
1313 : }
1314 : else
1315 292 : appendPQExpBufferChar(curbuf, *cp++);
1316 : }
1317 55332 : else if (ch == '$')
1318 : {
1319 : /*
1320 : * Dollar is always quoted, whether inside quotes or not. The
1321 : * reason is that it's allowed in SQL identifiers, so there's a
1322 : * significant use-case for treating it literally, while because
1323 : * we anchor the pattern automatically there is no use-case for
1324 : * having it possess its regexp meaning.
1325 : */
1326 6 : appendPQExpBufferStr(curbuf, "\\$");
1327 6 : if (left)
1328 6 : appendPQExpBufferChar(&left_literal, '$');
1329 6 : cp++;
1330 : }
1331 : else
1332 : {
1333 : /*
1334 : * Ordinary data character, transfer to pattern
1335 : *
1336 : * Inside double quotes, or at all times if force_escape is true,
1337 : * quote regexp special characters with a backslash to avoid
1338 : * regexp errors. Outside quotes, however, let them pass through
1339 : * as-is; this lets knowledgeable users build regexp expressions
1340 : * that are more powerful than shell-style patterns.
1341 : *
1342 : * As an exception to that, though, always quote "[]", as that's
1343 : * much more likely to be an attempt to write an array type name
1344 : * than it is to be the start of a regexp bracket expression.
1345 : */
1346 55326 : if ((inquotes || force_escape) &&
1347 14996 : strchr("|*+?()[]{}.^$\\", ch))
1348 1937 : appendPQExpBufferChar(curbuf, '\\');
1349 53389 : else if (ch == '[' && cp[1] == ']')
1350 3 : appendPQExpBufferChar(curbuf, '\\');
1351 55326 : i = PQmblenBounded(cp, encoding);
1352 110652 : while (i--)
1353 : {
1354 55326 : if (left)
1355 38550 : appendPQExpBufferChar(&left_literal, *cp);
1356 55326 : appendPQExpBufferChar(curbuf, *cp++);
1357 : }
1358 : }
1359 : }
1360 3546 : appendPQExpBufferStr(curbuf, ")$");
1361 :
1362 3546 : if (namebuf)
1363 : {
1364 3546 : appendPQExpBufferStr(namebuf, curbuf->data);
1365 3546 : termPQExpBuffer(curbuf);
1366 3546 : curbuf--;
1367 : }
1368 :
1369 3546 : if (schemabuf && curbuf >= buf)
1370 : {
1371 748 : appendPQExpBufferStr(schemabuf, curbuf->data);
1372 748 : termPQExpBuffer(curbuf);
1373 748 : curbuf--;
1374 : }
1375 :
1376 3546 : if (dbnamebuf && curbuf >= buf)
1377 : {
1378 377 : if (want_literal_dbname)
1379 360 : appendPQExpBufferStr(dbnamebuf, left_literal.data);
1380 : else
1381 17 : appendPQExpBufferStr(dbnamebuf, curbuf->data);
1382 377 : termPQExpBuffer(curbuf);
1383 : }
1384 :
1385 3546 : if (want_literal_dbname)
1386 3444 : termPQExpBuffer(&left_literal);
1387 3546 : }
|