1 /*-------------------------------------------------------------------------
3 * String-processing utility routines for frontend code
5 * Assorted utility functions that are useful in constructing SQL queries
6 * and interpreting backend output.
9 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
12 * src/fe_utils/string_utils.c
14 *-------------------------------------------------------------------------
16 #include "postgres_fe.h"
20 #include "common/keywords.h"
21 #include "fe_utils/string_utils.h"
23 static PQExpBuffer
defaultGetLocalPQExpBuffer(void);
25 /* Globals exported by this file */
26 int quote_all_identifiers
= 0;
27 PQExpBuffer (*getLocalPQExpBuffer
) (void) = defaultGetLocalPQExpBuffer
;
31 * Returns a temporary PQExpBuffer, valid until the next call to the function.
32 * This is used by fmtId and fmtQualifiedId.
34 * Non-reentrant and non-thread-safe but reduces memory leakage. You can
35 * replace this with a custom version by setting the getLocalPQExpBuffer
39 defaultGetLocalPQExpBuffer(void)
41 static PQExpBuffer id_return
= NULL
;
43 if (id_return
) /* first time through? */
45 /* same buffer, just wipe contents */
46 resetPQExpBuffer(id_return
);
51 id_return
= createPQExpBuffer();
58 * Quotes input string if it's not a legitimate SQL identifier as-is.
60 * Note that the returned string must be used before calling fmtId again,
61 * since we re-use the same return buffer each time.
64 fmtId(const char *rawid
)
66 PQExpBuffer id_return
= getLocalPQExpBuffer();
69 bool need_quotes
= false;
72 * These checks need to match the identifier production in scan.l. Don't
75 if (quote_all_identifiers
)
77 /* slightly different rules for first character */
78 else if (!((rawid
[0] >= 'a' && rawid
[0] <= 'z') || rawid
[0] == '_'))
82 /* otherwise check the entire string */
83 for (cp
= rawid
; *cp
; cp
++)
85 if (!((*cp
>= 'a' && *cp
<= 'z')
86 || (*cp
>= '0' && *cp
<= '9')
98 * Check for keyword. We quote keywords except for unreserved ones.
99 * (In some cases we could avoid quoting a col_name or type_func_name
100 * keyword, but it seems much harder than it's worth to tell that.)
102 * Note: ScanKeywordLookup() does case-insensitive comparison, but
103 * that's fine, since we already know we have all-lower-case.
105 int kwnum
= ScanKeywordLookup(rawid
, &ScanKeywords
);
107 if (kwnum
>= 0 && ScanKeywordCategories
[kwnum
] != UNRESERVED_KEYWORD
)
113 /* no quoting needed */
114 appendPQExpBufferStr(id_return
, rawid
);
118 appendPQExpBufferChar(id_return
, '"');
119 for (cp
= rawid
; *cp
; cp
++)
122 * Did we find a double-quote in the string? Then make this a
123 * double double-quote per SQL99. Before, we put in a
124 * backslash/double-quote pair. - thomas 2000-08-05
127 appendPQExpBufferChar(id_return
, '"');
128 appendPQExpBufferChar(id_return
, *cp
);
130 appendPQExpBufferChar(id_return
, '"');
133 return id_return
->data
;
137 * fmtQualifiedId - construct a schema-qualified name, with quoting as needed.
139 * Like fmtId, use the result before calling again.
141 * Since we call fmtId and it also uses getLocalPQExpBuffer() we cannot
142 * use that buffer until we're finished with calling fmtId().
145 fmtQualifiedId(const char *schema
, const char *id
)
147 PQExpBuffer id_return
;
148 PQExpBuffer lcl_pqexp
= createPQExpBuffer();
150 /* Some callers might fail to provide a schema name */
151 if (schema
&& *schema
)
153 appendPQExpBuffer(lcl_pqexp
, "%s.", fmtId(schema
));
155 appendPQExpBufferStr(lcl_pqexp
, fmtId(id
));
157 id_return
= getLocalPQExpBuffer();
159 appendPQExpBufferStr(id_return
, lcl_pqexp
->data
);
160 destroyPQExpBuffer(lcl_pqexp
);
162 return id_return
->data
;
167 * Format a Postgres version number (in the PG_VERSION_NUM integer format
168 * returned by PQserverVersion()) as a string. This exists mainly to
169 * encapsulate knowledge about two-part vs. three-part version numbers.
171 * For reentrancy, caller must supply the buffer the string is put in.
172 * Recommended size of the buffer is 32 bytes.
174 * Returns address of 'buf', as a notational convenience.
177 formatPGVersionNumber(int version_number
, bool include_minor
,
178 char *buf
, size_t buflen
)
180 if (version_number
>= 100000)
182 /* New two-part style */
184 snprintf(buf
, buflen
, "%d.%d", version_number
/ 10000,
185 version_number
% 10000);
187 snprintf(buf
, buflen
, "%d", version_number
/ 10000);
191 /* Old three-part style */
193 snprintf(buf
, buflen
, "%d.%d.%d", version_number
/ 10000,
194 (version_number
/ 100) % 100,
195 version_number
% 100);
197 snprintf(buf
, buflen
, "%d.%d", version_number
/ 10000,
198 (version_number
/ 100) % 100);
205 * Convert a string value to an SQL string literal and append it to
206 * the given buffer. We assume the specified client_encoding and
207 * standard_conforming_strings settings.
209 * This is essentially equivalent to libpq's PQescapeStringInternal,
210 * except for the output buffer structure. We need it in situations
211 * where we do not have a PGconn available. Where we do,
212 * appendStringLiteralConn is a better choice.
215 appendStringLiteral(PQExpBuffer buf
, const char *str
,
216 int encoding
, bool std_strings
)
218 size_t length
= strlen(str
);
219 const char *source
= str
;
222 if (!enlargePQExpBuffer(buf
, 2 * length
+ 2))
225 target
= buf
->data
+ buf
->len
;
228 while (*source
!= '\0')
234 /* Fast path for plain ASCII */
235 if (!IS_HIGHBIT_SET(c
))
237 /* Apply quoting if needed */
238 if (SQL_STR_DOUBLE(c
, !std_strings
))
240 /* Copy the character */
246 /* Slow path for possible multibyte characters */
247 len
= PQmblen(source
, encoding
);
249 /* Copy the character */
250 for (i
= 0; i
< len
; i
++)
254 *target
++ = *source
++;
258 * If we hit premature end of string (ie, incomplete multibyte
259 * character), try to pad out to the correct length with spaces. We
260 * may not be able to pad completely, but we will always be able to
261 * insert at least one pad space (since we'd not have quoted a
262 * multibyte character). This should be enough to make a string that
263 * the server will error out on.
267 char *stop
= buf
->data
+ buf
->maxlen
- 2;
279 /* Write the terminating quote and NUL character. */
283 buf
->len
= target
- buf
->data
;
288 * Convert a string value to an SQL string literal and append it to
289 * the given buffer. Encoding and string syntax rules are as indicated
290 * by current settings of the PGconn.
293 appendStringLiteralConn(PQExpBuffer buf
, const char *str
, PGconn
*conn
)
295 size_t length
= strlen(str
);
298 * XXX This is a kluge to silence escape_string_warning in our utility
299 * programs. It should go away someday.
301 if (strchr(str
, '\\') != NULL
&& PQserverVersion(conn
) >= 80100)
303 /* ensure we are not adjacent to an identifier */
304 if (buf
->len
> 0 && buf
->data
[buf
->len
- 1] != ' ')
305 appendPQExpBufferChar(buf
, ' ');
306 appendPQExpBufferChar(buf
, ESCAPE_STRING_SYNTAX
);
307 appendStringLiteral(buf
, str
, PQclientEncoding(conn
), false);
312 if (!enlargePQExpBuffer(buf
, 2 * length
+ 2))
314 appendPQExpBufferChar(buf
, '\'');
315 buf
->len
+= PQescapeStringConn(conn
, buf
->data
+ buf
->len
,
317 appendPQExpBufferChar(buf
, '\'');
322 * Convert a string value to a dollar quoted literal and append it to
323 * the given buffer. If the dqprefix parameter is not NULL then the
324 * dollar quote delimiter will begin with that (after the opening $).
326 * No escaping is done at all on str, in compliance with the rules
327 * for parsing dollar quoted strings. Also, we need not worry about
331 appendStringLiteralDQ(PQExpBuffer buf
, const char *str
, const char *dqprefix
)
333 static const char suffixes
[] = "_XXXXXXX";
335 PQExpBuffer delimBuf
= createPQExpBuffer();
337 /* start with $ + dqprefix if not NULL */
338 appendPQExpBufferChar(delimBuf
, '$');
340 appendPQExpBufferStr(delimBuf
, dqprefix
);
343 * Make sure we choose a delimiter which (without the trailing $) is not
344 * present in the string being quoted. We don't check with the trailing $
345 * because a string ending in $foo must not be quoted with $foo$.
347 while (strstr(str
, delimBuf
->data
) != NULL
)
349 appendPQExpBufferChar(delimBuf
, suffixes
[nextchar
++]);
350 nextchar
%= sizeof(suffixes
) - 1;
354 appendPQExpBufferChar(delimBuf
, '$');
356 /* quote it and we are all done */
357 appendPQExpBufferStr(buf
, delimBuf
->data
);
358 appendPQExpBufferStr(buf
, str
);
359 appendPQExpBufferStr(buf
, delimBuf
->data
);
361 destroyPQExpBuffer(delimBuf
);
366 * Convert a bytea value (presented as raw bytes) to an SQL string literal
367 * and append it to the given buffer. We assume the specified
368 * standard_conforming_strings setting.
370 * This is needed in situations where we do not have a PGconn available.
371 * Where we do, PQescapeByteaConn is a better choice.
374 appendByteaLiteral(PQExpBuffer buf
, const unsigned char *str
, size_t length
,
377 const unsigned char *source
= str
;
380 static const char hextbl
[] = "0123456789abcdef";
383 * This implementation is hard-wired to produce hex-format output. We do
384 * not know the server version the output will be loaded into, so making
385 * an intelligent format choice is impossible. It might be better to
386 * always use the old escaped format.
388 if (!enlargePQExpBuffer(buf
, 2 * length
+ 5))
391 target
= buf
->data
+ buf
->len
;
400 unsigned char c
= *source
++;
402 *target
++ = hextbl
[(c
>> 4) & 0xF];
403 *target
++ = hextbl
[c
& 0xF];
406 /* Write the terminating quote and NUL character. */
410 buf
->len
= target
- buf
->data
;
415 * Append the given string to the shell command being built in the buffer,
416 * with shell-style quoting as needed to create exactly one argument.
418 * Forbid LF or CR characters, which have scant practical use beyond designing
419 * security breaches. The Windows command shell is unusable as a conduit for
420 * arguments containing LF or CR characters. A future major release should
421 * reject those characters in CREATE ROLE and CREATE DATABASE, because use
422 * there eventually leads to errors here.
424 * appendShellString() simply prints an error and dies if LF or CR appears.
425 * appendShellStringNoError() omits those characters from the result, and
426 * returns false if there were any.
429 appendShellString(PQExpBuffer buf
, const char *str
)
431 if (!appendShellStringNoError(buf
, str
))
434 _("shell command argument contains a newline or carriage return: \"%s\"\n"),
441 appendShellStringNoError(PQExpBuffer buf
, const char *str
)
444 int backslash_run_length
= 0;
450 * Don't bother with adding quotes if the string is nonempty and clearly
451 * contains only safe characters.
454 strspn(str
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_./:") == strlen(str
))
456 appendPQExpBufferStr(buf
, str
);
461 appendPQExpBufferChar(buf
, '\'');
462 for (p
= str
; *p
; p
++)
464 if (*p
== '\n' || *p
== '\r')
471 appendPQExpBufferStr(buf
, "'\"'\"'");
473 appendPQExpBufferChar(buf
, *p
);
475 appendPQExpBufferChar(buf
, '\'');
479 * A Windows system() argument experiences two layers of interpretation.
480 * First, cmd.exe interprets the string. Its behavior is undocumented,
481 * but a caret escapes any byte except LF or CR that would otherwise have
482 * special meaning. Handling of a caret before LF or CR differs between
483 * "cmd.exe /c" and other modes, and it is unusable here.
485 * Second, the new process parses its command line to construct argv (see
486 * https://msdn.microsoft.com/en-us/library/17w5ykft.aspx). This treats
487 * backslash-double quote sequences specially.
489 appendPQExpBufferStr(buf
, "^\"");
490 for (p
= str
; *p
; p
++)
492 if (*p
== '\n' || *p
== '\r')
498 /* Change N backslashes before a double quote to 2N+1 backslashes. */
501 while (backslash_run_length
)
503 appendPQExpBufferStr(buf
, "^\\");
504 backslash_run_length
--;
506 appendPQExpBufferStr(buf
, "^\\");
509 backslash_run_length
++;
511 backslash_run_length
= 0;
514 * Decline to caret-escape the most mundane characters, to ease
515 * debugging and lest we approach the command length limit.
517 if (!((*p
>= 'a' && *p
<= 'z') ||
518 (*p
>= 'A' && *p
<= 'Z') ||
519 (*p
>= '0' && *p
<= '9')))
520 appendPQExpBufferChar(buf
, '^');
521 appendPQExpBufferChar(buf
, *p
);
525 * Change N backslashes at end of argument to 2N backslashes, because they
526 * precede the double quote that terminates the argument.
528 while (backslash_run_length
)
530 appendPQExpBufferStr(buf
, "^\\");
531 backslash_run_length
--;
533 appendPQExpBufferStr(buf
, "^\"");
541 * Append the given string to the buffer, with suitable quoting for passing
542 * the string as a value in a keyword/value pair in a libpq connection string.
545 appendConnStrVal(PQExpBuffer buf
, const char *str
)
551 * If the string is one or more plain ASCII characters, no need to quote
552 * it. This is quite conservative, but better safe than sorry.
555 for (s
= str
; *s
; s
++)
557 if (!((*s
>= 'a' && *s
<= 'z') || (*s
>= 'A' && *s
<= 'Z') ||
558 (*s
>= '0' && *s
<= '9') || *s
== '_' || *s
== '.'))
568 appendPQExpBufferChar(buf
, '\'');
571 /* ' and \ must be escaped by to \' and \\ */
572 if (*str
== '\'' || *str
== '\\')
573 appendPQExpBufferChar(buf
, '\\');
575 appendPQExpBufferChar(buf
, *str
);
578 appendPQExpBufferChar(buf
, '\'');
581 appendPQExpBufferStr(buf
, str
);
586 * Append a psql meta-command that connects to the given database with the
587 * then-current connection's user, host and port.
590 appendPsqlMetaConnect(PQExpBuffer buf
, const char *dbname
)
596 * If the name is plain ASCII characters, emit a trivial "\connect "foo"".
597 * For other names, even many not technically requiring it, skip to the
598 * general case. No database has a zero-length name.
602 for (s
= dbname
; *s
; s
++)
604 if (*s
== '\n' || *s
== '\r')
607 _("database name contains a newline or carriage return: \"%s\"\n"),
612 if (!((*s
>= 'a' && *s
<= 'z') || (*s
>= 'A' && *s
<= 'Z') ||
613 (*s
>= '0' && *s
<= '9') || *s
== '_' || *s
== '.'))
619 appendPQExpBufferStr(buf
, "\\connect ");
622 PQExpBufferData connstr
;
624 initPQExpBuffer(&connstr
);
625 appendPQExpBufferStr(&connstr
, "dbname=");
626 appendConnStrVal(&connstr
, dbname
);
628 appendPQExpBufferStr(buf
, "-reuse-previous=on ");
631 * As long as the name does not contain a newline, SQL identifier
632 * quoting satisfies the psql meta-command parser. Prefer not to
633 * involve psql-interpreted single quotes, which behaved differently
634 * before PostgreSQL 9.2.
636 appendPQExpBufferStr(buf
, fmtId(connstr
.data
));
638 termPQExpBuffer(&connstr
);
641 appendPQExpBufferStr(buf
, fmtId(dbname
));
642 appendPQExpBufferChar(buf
, '\n');
647 * Deconstruct the text representation of a 1-dimensional Postgres array
648 * into individual items.
650 * On success, returns true and sets *itemarray and *nitems to describe
651 * an array of individual strings. On parse failure, returns false;
652 * *itemarray may exist or be NULL.
654 * NOTE: free'ing itemarray is sufficient to deallocate the working storage.
657 parsePGArray(const char *atext
, char ***itemarray
, int *nitems
)
665 * We expect input in the form of "{item,item,item}" where any item is
666 * either raw data, or surrounded by double quotes (in which case embedded
667 * characters including backslashes and quotes are backslashed).
669 * We build the result as an array of pointers followed by the actual
670 * string data, all in one malloc block for convenience of deallocation.
671 * The worst-case storage need is not more than one pointer and one
672 * character for each input character (consider "{,,,,,,,,,,}").
676 inputlen
= strlen(atext
);
677 if (inputlen
< 2 || atext
[0] != '{' || atext
[inputlen
- 1] != '}')
678 return false; /* bad input */
679 items
= (char **) malloc(inputlen
* (sizeof(char *) + sizeof(char)));
681 return false; /* out of memory */
683 strings
= (char *) (items
+ inputlen
);
685 atext
++; /* advance over initial '{' */
687 while (*atext
!= '}')
690 return false; /* premature end of string */
691 items
[curitem
] = strings
;
692 while (*atext
!= '}' && *atext
!= ',')
695 return false; /* premature end of string */
697 *strings
++ = *atext
++; /* copy unquoted data */
700 /* process quoted substring */
702 while (*atext
!= '"')
705 return false; /* premature end of string */
710 return false; /* premature end of string */
712 *strings
++ = *atext
++; /* copy quoted data */
722 if (atext
[1] != '\0')
723 return false; /* bogus syntax (embedded '}') */
730 * Append one element to the text representation of a 1-dimensional Postgres
733 * The caller must provide the initial '{' and closing '}' of the array.
734 * This function handles all else, including insertion of commas and
737 * We assume that typdelim is ','.
740 appendPGArray(PQExpBuffer buffer
, const char *value
)
745 if (buffer
->data
[buffer
->len
- 1] != '{')
746 appendPQExpBufferChar(buffer
, ',');
748 /* Decide if we need quotes; this should match array_out()'s choices. */
749 if (value
[0] == '\0')
750 needquote
= true; /* force quotes for empty string */
751 else if (pg_strcasecmp(value
, "NULL") == 0)
752 needquote
= true; /* force quotes for literal NULL */
758 for (tmp
= value
; *tmp
; tmp
++)
762 if (ch
== '"' || ch
== '\\' ||
763 ch
== '{' || ch
== '}' || ch
== ',' ||
764 /* these match scanner_isspace(): */
765 ch
== ' ' || ch
== '\t' || ch
== '\n' ||
766 ch
== '\r' || ch
== '\v' || ch
== '\f')
776 appendPQExpBufferChar(buffer
, '"');
777 for (tmp
= value
; *tmp
; tmp
++)
781 if (ch
== '"' || ch
== '\\')
782 appendPQExpBufferChar(buffer
, '\\');
783 appendPQExpBufferChar(buffer
, ch
);
785 appendPQExpBufferChar(buffer
, '"');
788 appendPQExpBufferStr(buffer
, value
);
793 * Format a reloptions array and append it to the given buffer.
795 * "prefix" is prepended to the option names; typically it's "" or "toast.".
797 * Returns false if the reloptions array could not be parsed (in which case
798 * nothing will have been appended to the buffer), or true on success.
800 * Note: this logic should generally match the backend's flatten_reloptions()
801 * (in adt/ruleutils.c).
804 appendReloptionsArray(PQExpBuffer buffer
, const char *reloptions
,
805 const char *prefix
, int encoding
, bool std_strings
)
811 if (!parsePGArray(reloptions
, &options
, &noptions
))
817 for (i
= 0; i
< noptions
; i
++)
819 char *option
= options
[i
];
825 * Each array element should have the form name=value. If the "=" is
826 * missing for some reason, treat it like an empty value.
829 separator
= strchr(option
, '=');
833 value
= separator
+ 1;
839 appendPQExpBufferStr(buffer
, ", ");
840 appendPQExpBuffer(buffer
, "%s%s=", prefix
, fmtId(name
));
843 * In general we need to quote the value; but to avoid unnecessary
844 * clutter, do not quote if it is an identifier that would not need
845 * quoting. (We could also allow numbers, but that is a bit trickier
846 * than it looks --- for example, are leading zeroes significant? We
847 * don't want to assume very much here about what custom reloptions
850 if (strcmp(fmtId(value
), value
) == 0)
851 appendPQExpBufferStr(buffer
, value
);
853 appendStringLiteral(buffer
, value
, encoding
, std_strings
);
863 * processSQLNamePattern
865 * Scan a wildcard-pattern string and generate appropriate WHERE clauses
866 * to limit the set of objects returned. The WHERE clauses are appended
867 * to the already-partially-constructed query in buf. Returns whether
868 * any clause was added.
870 * conn: connection query will be sent to (consulted for escaping rules).
871 * buf: output parameter.
872 * pattern: user-specified pattern option, or NULL if none ("*" is implied).
873 * have_where: true if caller already emitted "WHERE" (clauses will be ANDed
874 * onto the existing WHERE clause).
875 * force_escape: always quote regexp special characters, even outside
876 * double quotes (else they are quoted only between double quotes).
877 * schemavar: name of query variable to match against a schema-name pattern.
878 * Can be NULL if no schema.
879 * namevar: name of query variable to match against an object-name pattern.
880 * altnamevar: NULL, or name of an alternative variable to match against name.
881 * visibilityrule: clause to use if we want to restrict to visible objects
882 * (for example, "pg_catalog.pg_table_is_visible(p.oid)"). Can be NULL.
883 * dbnamebuf: output parameter receiving the database name portion of the
884 * pattern, if any. Can be NULL.
885 * dotcnt: how many separators were parsed from the pattern, by reference.
887 * Formatting note: the text already present in buf should end with a newline.
888 * The appended text, if any, will end with one too.
891 processSQLNamePattern(PGconn
*conn
, PQExpBuffer buf
, const char *pattern
,
892 bool have_where
, bool force_escape
,
893 const char *schemavar
, const char *namevar
,
894 const char *altnamevar
, const char *visibilityrule
,
895 PQExpBuffer dbnamebuf
, int *dotcnt
)
897 PQExpBufferData schemabuf
;
898 PQExpBufferData namebuf
;
899 bool added_clause
= false;
903 (appendPQExpBufferStr(buf, have_where ? " AND " : "WHERE "), \
904 have_where = true, added_clause = true)
911 /* Default: select all visible objects */
915 appendPQExpBuffer(buf
, "%s\n", visibilityrule
);
920 initPQExpBuffer(&schemabuf
);
921 initPQExpBuffer(&namebuf
);
924 * Convert shell-style 'pattern' into the regular expression(s) we want to
925 * execute. Quoting/escaping into SQL literal format will be done below
926 * using appendStringLiteralConn().
928 * If the caller provided a schemavar, we want to split the pattern on
929 * ".", otherwise not.
931 patternToSQLRegex(PQclientEncoding(conn
),
932 (schemavar
? dbnamebuf
: NULL
),
933 (schemavar
? &schemabuf
: NULL
),
935 pattern
, force_escape
, true, dotcnt
);
938 * Now decide what we need to emit. We may run under a hostile
939 * search_path, so qualify EVERY name. Note there will be a leading "^("
940 * in the patterns in any case.
942 * We want the regex matches to use the database's default collation where
943 * collation-sensitive behavior is required (for example, which characters
944 * match '\w'). That happened by default before PG v12, but if the server
945 * is >= v12 then we need to force it through explicit COLLATE clauses,
946 * otherwise the "C" collation attached to "name" catalog columns wins.
948 if (namevar
&& namebuf
.len
> 2)
950 /* We have a name pattern, so constrain the namevar(s) */
952 /* Optimize away a "*" pattern */
953 if (strcmp(namebuf
.data
, "^(.*)$") != 0)
958 appendPQExpBuffer(buf
,
959 "(%s OPERATOR(pg_catalog.~) ", namevar
);
960 appendStringLiteralConn(buf
, namebuf
.data
, conn
);
961 if (PQserverVersion(conn
) >= 120000)
962 appendPQExpBufferStr(buf
, " COLLATE pg_catalog.default");
963 appendPQExpBuffer(buf
,
964 "\n OR %s OPERATOR(pg_catalog.~) ",
966 appendStringLiteralConn(buf
, namebuf
.data
, conn
);
967 if (PQserverVersion(conn
) >= 120000)
968 appendPQExpBufferStr(buf
, " COLLATE pg_catalog.default");
969 appendPQExpBufferStr(buf
, ")\n");
973 appendPQExpBuffer(buf
, "%s OPERATOR(pg_catalog.~) ", namevar
);
974 appendStringLiteralConn(buf
, namebuf
.data
, conn
);
975 if (PQserverVersion(conn
) >= 120000)
976 appendPQExpBufferStr(buf
, " COLLATE pg_catalog.default");
977 appendPQExpBufferChar(buf
, '\n');
982 if (schemavar
&& schemabuf
.len
> 2)
984 /* We have a schema pattern, so constrain the schemavar */
986 /* Optimize away a "*" pattern */
987 if (strcmp(schemabuf
.data
, "^(.*)$") != 0 && schemavar
)
990 appendPQExpBuffer(buf
, "%s OPERATOR(pg_catalog.~) ", schemavar
);
991 appendStringLiteralConn(buf
, schemabuf
.data
, conn
);
992 if (PQserverVersion(conn
) >= 120000)
993 appendPQExpBufferStr(buf
, " COLLATE pg_catalog.default");
994 appendPQExpBufferChar(buf
, '\n');
999 /* No schema pattern given, so select only visible objects */
1003 appendPQExpBuffer(buf
, "%s\n", visibilityrule
);
1007 termPQExpBuffer(&schemabuf
);
1008 termPQExpBuffer(&namebuf
);
1010 return added_clause
;
1015 * Transform a possibly qualified shell-style object name pattern into up to
1016 * three SQL-style regular expressions, converting quotes, lower-casing
1017 * unquoted letters, and adjusting shell-style wildcard characters into regexp
1020 * If the dbnamebuf and schemabuf arguments are non-NULL, and the pattern
1021 * contains two or more dbname/schema/name separators, we parse the portions of
1022 * the pattern prior to the first and second separators into dbnamebuf and
1023 * schemabuf, and the rest into namebuf.
1025 * If dbnamebuf is NULL and schemabuf is non-NULL, and the pattern contains at
1026 * least one separator, we parse the first portion into schemabuf and the rest
1029 * Otherwise, we parse all the pattern into namebuf.
1031 * If the pattern contains more dotted parts than buffers to parse into, the
1032 * extra dots will be treated as literal characters and written into the
1033 * namebuf, though they will be counted. Callers should always check the value
1034 * returned by reference in dotcnt and handle this error case appropriately.
1036 * We surround the regexps with "^(...)$" to force them to match whole strings,
1037 * as per SQL practice. We have to have parens in case strings contain "|",
1038 * else the "^" and "$" will be bound into the first and last alternatives
1039 * which is not what we want. Whether this is done for dbnamebuf is controlled
1040 * by the want_literal_dbname parameter.
1042 * The regexps we parse into the buffers are appended to the data (if any)
1043 * already present. If we parse fewer fields than the number of buffers we
1044 * were given, the extra buffers are unaltered.
1046 * encoding: the character encoding for the given pattern
1047 * dbnamebuf: output parameter receiving the database name portion of the
1048 * pattern, if any. Can be NULL.
1049 * schemabuf: output parameter receiving the schema name portion of the
1050 * pattern, if any. Can be NULL.
1051 * namebuf: output parameter receiving the database name portion of the
1052 * pattern, if any. Can be NULL.
1053 * pattern: user-specified pattern option, or NULL if none ("*" is implied).
1054 * force_escape: always quote regexp special characters, even outside
1055 * double quotes (else they are quoted only between double quotes).
1056 * want_literal_dbname: if true, regexp special characters within the database
1057 * name portion of the pattern will not be escaped, nor will the dbname be
1058 * converted into a regular expression.
1059 * dotcnt: output parameter receiving the number of separators parsed from the
1063 patternToSQLRegex(int encoding
, PQExpBuffer dbnamebuf
, PQExpBuffer schemabuf
,
1064 PQExpBuffer namebuf
, const char *pattern
, bool force_escape
,
1065 bool want_literal_dbname
, int *dotcnt
)
1067 PQExpBufferData buf
[3];
1068 PQExpBufferData left_literal
;
1076 Assert(pattern
!= NULL
);
1077 Assert(namebuf
!= NULL
);
1079 /* callers should never expect "dbname.relname" format */
1080 Assert(dbnamebuf
== NULL
|| schemabuf
!= NULL
);
1081 Assert(dotcnt
!= NULL
);
1087 if (dbnamebuf
!= NULL
)
1089 else if (schemabuf
!= NULL
)
1095 if (want_literal_dbname
)
1098 initPQExpBuffer(&left_literal
);
1102 initPQExpBuffer(curbuf
);
1103 appendPQExpBufferStr(curbuf
, "^(");
1110 if (inquotes
&& cp
[1] == '"')
1112 /* emit one quote, stay in inquotes mode */
1113 appendPQExpBufferChar(curbuf
, '"');
1115 appendPQExpBufferChar(&left_literal
, '"');
1119 inquotes
= !inquotes
;
1122 else if (!inquotes
&& isupper((unsigned char) ch
))
1124 appendPQExpBufferChar(curbuf
,
1125 pg_tolower((unsigned char) ch
));
1127 appendPQExpBufferChar(&left_literal
,
1128 pg_tolower((unsigned char) ch
));
1131 else if (!inquotes
&& ch
== '*')
1133 appendPQExpBufferStr(curbuf
, ".*");
1135 appendPQExpBufferChar(&left_literal
, '*');
1138 else if (!inquotes
&& ch
== '?')
1140 appendPQExpBufferChar(curbuf
, '.');
1142 appendPQExpBufferChar(&left_literal
, '?');
1145 else if (!inquotes
&& ch
== '.')
1150 if (curbuf
< maxbuf
)
1152 appendPQExpBufferStr(curbuf
, ")$");
1154 initPQExpBuffer(curbuf
);
1155 appendPQExpBufferStr(curbuf
, "^(");
1159 appendPQExpBufferChar(curbuf
, *cp
++);
1164 * Dollar is always quoted, whether inside quotes or not. The
1165 * reason is that it's allowed in SQL identifiers, so there's a
1166 * significant use-case for treating it literally, while because
1167 * we anchor the pattern automatically there is no use-case for
1168 * having it possess its regexp meaning.
1170 appendPQExpBufferStr(curbuf
, "\\$");
1172 appendPQExpBufferChar(&left_literal
, '$');
1178 * Ordinary data character, transfer to pattern
1180 * Inside double quotes, or at all times if force_escape is true,
1181 * quote regexp special characters with a backslash to avoid
1182 * regexp errors. Outside quotes, however, let them pass through
1183 * as-is; this lets knowledgeable users build regexp expressions
1184 * that are more powerful than shell-style patterns.
1186 * As an exception to that, though, always quote "[]", as that's
1187 * much more likely to be an attempt to write an array type name
1188 * than it is to be the start of a regexp bracket expression.
1190 if ((inquotes
|| force_escape
) &&
1191 strchr("|*+?()[]{}.^$\\", ch
))
1192 appendPQExpBufferChar(curbuf
, '\\');
1193 else if (ch
== '[' && cp
[1] == ']')
1194 appendPQExpBufferChar(curbuf
, '\\');
1195 i
= PQmblenBounded(cp
, encoding
);
1199 appendPQExpBufferChar(&left_literal
, *cp
);
1200 appendPQExpBufferChar(curbuf
, *cp
++);
1204 appendPQExpBufferStr(curbuf
, ")$");
1208 appendPQExpBufferStr(namebuf
, curbuf
->data
);
1209 termPQExpBuffer(curbuf
);
1213 if (schemabuf
&& curbuf
>= buf
)
1215 appendPQExpBufferStr(schemabuf
, curbuf
->data
);
1216 termPQExpBuffer(curbuf
);
1220 if (dbnamebuf
&& curbuf
>= buf
)
1222 if (want_literal_dbname
)
1223 appendPQExpBufferStr(dbnamebuf
, left_literal
.data
);
1225 appendPQExpBufferStr(dbnamebuf
, curbuf
->data
);
1226 termPQExpBuffer(curbuf
);
1229 if (want_literal_dbname
)
1230 termPQExpBuffer(&left_literal
);